2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
\r
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * SPDX-License-Identifier: MIT
\r
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
8 * this software and associated documentation files (the "Software"), to deal in
\r
9 * the Software without restriction, including without limitation the rights to
\r
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
11 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
12 * subject to the following conditions:
\r
14 * The above copyright notice and this permission notice shall be included in all
\r
15 * copies or substantial portions of the Software.
\r
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
24 * https://www.FreeRTOS.org
\r
25 * https://github.com/FreeRTOS
\r
31 * @brief FreeRTOS atomic operation support.
\r
33 * This file implements atomic functions by disabling interrupts globally.
\r
34 * Implementations with architecture specific atomic instructions can be
\r
35 * provided under each compiler directory.
\r
41 #ifndef INC_FREERTOS_H
\r
42 #error "include FreeRTOS.h must appear in source files before include atomic.h"
\r
45 /* Standard includes. */
\r
55 * Port specific definitions -- entering/exiting critical section.
\r
56 * Refer template -- ./lib/FreeRTOS/portable/Compiler/Arch/portmacro.h
\r
58 * Every call to ATOMIC_EXIT_CRITICAL() must be closely paired with
\r
59 * ATOMIC_ENTER_CRITICAL().
\r
62 #if defined( portSET_INTERRUPT_MASK_FROM_ISR )
\r
64 /* Nested interrupt scheme is supported in this port. */
\r
65 #define ATOMIC_ENTER_CRITICAL() \
\r
66 UBaseType_t uxCriticalSectionType = portSET_INTERRUPT_MASK_FROM_ISR()
\r
68 #define ATOMIC_EXIT_CRITICAL() \
\r
69 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxCriticalSectionType )
\r
73 /* Nested interrupt scheme is NOT supported in this port. */
\r
74 #define ATOMIC_ENTER_CRITICAL() portENTER_CRITICAL()
\r
75 #define ATOMIC_EXIT_CRITICAL() portEXIT_CRITICAL()
\r
77 #endif /* portSET_INTERRUPT_MASK_FROM_ISR() */
\r
80 * Port specific definition -- "always inline".
\r
81 * Inline is compiler specific, and may not always get inlined depending on your
\r
82 * optimization level. Also, inline is considered as performance optimization
\r
83 * for atomic. Thus, if portFORCE_INLINE is not provided by portmacro.h,
\r
84 * instead of resulting error, simply define it away.
\r
86 #ifndef portFORCE_INLINE
\r
87 #define portFORCE_INLINE
\r
90 #define ATOMIC_COMPARE_AND_SWAP_SUCCESS 0x1U /**< Compare and swap succeeded, swapped. */
\r
91 #define ATOMIC_COMPARE_AND_SWAP_FAILURE 0x0U /**< Compare and swap failed, did not swap. */
\r
93 /*----------------------------- Swap && CAS ------------------------------*/
\r
96 * Atomic compare-and-swap
\r
98 * @brief Performs an atomic compare-and-swap operation on the specified values.
\r
100 * @param[in, out] pulDestination Pointer to memory location from where value is
\r
101 * to be loaded and checked.
\r
102 * @param[in] ulExchange If condition meets, write this value to memory.
\r
103 * @param[in] ulComparand Swap condition.
\r
105 * @return Unsigned integer of value 1 or 0. 1 for swapped, 0 for not swapped.
\r
107 * @note This function only swaps *pulDestination with ulExchange, if previous
\r
108 * *pulDestination value equals ulComparand.
\r
110 static portFORCE_INLINE uint32_t Atomic_CompareAndSwap_u32( uint32_t volatile * pulDestination,
\r
111 uint32_t ulExchange,
\r
112 uint32_t ulComparand )
\r
114 uint32_t ulReturnValue;
\r
116 ATOMIC_ENTER_CRITICAL();
\r
118 if( *pulDestination == ulComparand )
\r
120 *pulDestination = ulExchange;
\r
121 ulReturnValue = ATOMIC_COMPARE_AND_SWAP_SUCCESS;
\r
125 ulReturnValue = ATOMIC_COMPARE_AND_SWAP_FAILURE;
\r
128 ATOMIC_EXIT_CRITICAL();
\r
130 return ulReturnValue;
\r
132 /*-----------------------------------------------------------*/
\r
135 * Atomic swap (pointers)
\r
137 * @brief Atomically sets the address pointed to by *ppvDestination to the value
\r
140 * @param[in, out] ppvDestination Pointer to memory location from where a pointer
\r
141 * value is to be loaded and written back to.
\r
142 * @param[in] pvExchange Pointer value to be written to *ppvDestination.
\r
144 * @return The initial value of *ppvDestination.
\r
146 static portFORCE_INLINE void * Atomic_SwapPointers_p32( void * volatile * ppvDestination,
\r
147 void * pvExchange )
\r
149 void * pReturnValue;
\r
151 ATOMIC_ENTER_CRITICAL();
\r
153 pReturnValue = *ppvDestination;
\r
154 *ppvDestination = pvExchange;
\r
156 ATOMIC_EXIT_CRITICAL();
\r
158 return pReturnValue;
\r
160 /*-----------------------------------------------------------*/
\r
163 * Atomic compare-and-swap (pointers)
\r
165 * @brief Performs an atomic compare-and-swap operation on the specified pointer
\r
168 * @param[in, out] ppvDestination Pointer to memory location from where a pointer
\r
169 * value is to be loaded and checked.
\r
170 * @param[in] pvExchange If condition meets, write this value to memory.
\r
171 * @param[in] pvComparand Swap condition.
\r
173 * @return Unsigned integer of value 1 or 0. 1 for swapped, 0 for not swapped.
\r
175 * @note This function only swaps *ppvDestination with pvExchange, if previous
\r
176 * *ppvDestination value equals pvComparand.
\r
178 static portFORCE_INLINE uint32_t Atomic_CompareAndSwapPointers_p32( void * volatile * ppvDestination,
\r
180 void * pvComparand )
\r
182 uint32_t ulReturnValue = ATOMIC_COMPARE_AND_SWAP_FAILURE;
\r
184 ATOMIC_ENTER_CRITICAL();
\r
186 if( *ppvDestination == pvComparand )
\r
188 *ppvDestination = pvExchange;
\r
189 ulReturnValue = ATOMIC_COMPARE_AND_SWAP_SUCCESS;
\r
192 ATOMIC_EXIT_CRITICAL();
\r
194 return ulReturnValue;
\r
198 /*----------------------------- Arithmetic ------------------------------*/
\r
203 * @brief Atomically adds count to the value of the specified pointer points to.
\r
205 * @param[in,out] pulAddend Pointer to memory location from where value is to be
\r
206 * loaded and written back to.
\r
207 * @param[in] ulCount Value to be added to *pulAddend.
\r
209 * @return previous *pulAddend value.
\r
211 static portFORCE_INLINE uint32_t Atomic_Add_u32( uint32_t volatile * pulAddend,
\r
214 uint32_t ulCurrent;
\r
216 ATOMIC_ENTER_CRITICAL();
\r
218 ulCurrent = *pulAddend;
\r
219 *pulAddend += ulCount;
\r
221 ATOMIC_EXIT_CRITICAL();
\r
225 /*-----------------------------------------------------------*/
\r
230 * @brief Atomically subtracts count from the value of the specified pointer
\r
233 * @param[in,out] pulAddend Pointer to memory location from where value is to be
\r
234 * loaded and written back to.
\r
235 * @param[in] ulCount Value to be subtract from *pulAddend.
\r
237 * @return previous *pulAddend value.
\r
239 static portFORCE_INLINE uint32_t Atomic_Subtract_u32( uint32_t volatile * pulAddend,
\r
242 uint32_t ulCurrent;
\r
244 ATOMIC_ENTER_CRITICAL();
\r
246 ulCurrent = *pulAddend;
\r
247 *pulAddend -= ulCount;
\r
249 ATOMIC_EXIT_CRITICAL();
\r
253 /*-----------------------------------------------------------*/
\r
258 * @brief Atomically increments the value of the specified pointer points to.
\r
260 * @param[in,out] pulAddend Pointer to memory location from where value is to be
\r
261 * loaded and written back to.
\r
263 * @return *pulAddend value before increment.
\r
265 static portFORCE_INLINE uint32_t Atomic_Increment_u32( uint32_t volatile * pulAddend )
\r
267 uint32_t ulCurrent;
\r
269 ATOMIC_ENTER_CRITICAL();
\r
271 ulCurrent = *pulAddend;
\r
274 ATOMIC_EXIT_CRITICAL();
\r
278 /*-----------------------------------------------------------*/
\r
283 * @brief Atomically decrements the value of the specified pointer points to
\r
285 * @param[in,out] pulAddend Pointer to memory location from where value is to be
\r
286 * loaded and written back to.
\r
288 * @return *pulAddend value before decrement.
\r
290 static portFORCE_INLINE uint32_t Atomic_Decrement_u32( uint32_t volatile * pulAddend )
\r
292 uint32_t ulCurrent;
\r
294 ATOMIC_ENTER_CRITICAL();
\r
296 ulCurrent = *pulAddend;
\r
299 ATOMIC_EXIT_CRITICAL();
\r
304 /*----------------------------- Bitwise Logical ------------------------------*/
\r
309 * @brief Performs an atomic OR operation on the specified values.
\r
311 * @param [in, out] pulDestination Pointer to memory location from where value is
\r
312 * to be loaded and written back to.
\r
313 * @param [in] ulValue Value to be ORed with *pulDestination.
\r
315 * @return The original value of *pulDestination.
\r
317 static portFORCE_INLINE uint32_t Atomic_OR_u32( uint32_t volatile * pulDestination,
\r
320 uint32_t ulCurrent;
\r
322 ATOMIC_ENTER_CRITICAL();
\r
324 ulCurrent = *pulDestination;
\r
325 *pulDestination |= ulValue;
\r
327 ATOMIC_EXIT_CRITICAL();
\r
331 /*-----------------------------------------------------------*/
\r
336 * @brief Performs an atomic AND operation on the specified values.
\r
338 * @param [in, out] pulDestination Pointer to memory location from where value is
\r
339 * to be loaded and written back to.
\r
340 * @param [in] ulValue Value to be ANDed with *pulDestination.
\r
342 * @return The original value of *pulDestination.
\r
344 static portFORCE_INLINE uint32_t Atomic_AND_u32( uint32_t volatile * pulDestination,
\r
347 uint32_t ulCurrent;
\r
349 ATOMIC_ENTER_CRITICAL();
\r
351 ulCurrent = *pulDestination;
\r
352 *pulDestination &= ulValue;
\r
354 ATOMIC_EXIT_CRITICAL();
\r
358 /*-----------------------------------------------------------*/
\r
363 * @brief Performs an atomic NAND operation on the specified values.
\r
365 * @param [in, out] pulDestination Pointer to memory location from where value is
\r
366 * to be loaded and written back to.
\r
367 * @param [in] ulValue Value to be NANDed with *pulDestination.
\r
369 * @return The original value of *pulDestination.
\r
371 static portFORCE_INLINE uint32_t Atomic_NAND_u32( uint32_t volatile * pulDestination,
\r
374 uint32_t ulCurrent;
\r
376 ATOMIC_ENTER_CRITICAL();
\r
378 ulCurrent = *pulDestination;
\r
379 *pulDestination = ~( ulCurrent & ulValue );
\r
381 ATOMIC_EXIT_CRITICAL();
\r
385 /*-----------------------------------------------------------*/
\r
390 * @brief Performs an atomic XOR operation on the specified values.
\r
392 * @param [in, out] pulDestination Pointer to memory location from where value is
\r
393 * to be loaded and written back to.
\r
394 * @param [in] ulValue Value to be XORed with *pulDestination.
\r
396 * @return The original value of *pulDestination.
\r
398 static portFORCE_INLINE uint32_t Atomic_XOR_u32( uint32_t volatile * pulDestination,
\r
401 uint32_t ulCurrent;
\r
403 ATOMIC_ENTER_CRITICAL();
\r
405 ulCurrent = *pulDestination;
\r
406 *pulDestination ^= ulValue;
\r
408 ATOMIC_EXIT_CRITICAL();
\r
419 #endif /* ATOMIC_H */
\r