2 * FreeRTOS Kernel V10.4.3
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
29 * @brief FreeRTOS atomic operation support.
31 * This file implements atomic functions by disabling interrupts globally.
32 * Implementations with architecture specific atomic instructions can be
33 * provided under each compiler directory.
39 #ifndef INC_FREERTOS_H
40 #error "include FreeRTOS.h must appear in source files before include atomic.h"
43 /* Standard includes. */
53 * Port specific definitions -- entering/exiting critical section.
54 * Refer template -- ./lib/FreeRTOS/portable/Compiler/Arch/portmacro.h
56 * Every call to ATOMIC_EXIT_CRITICAL() must be closely paired with
57 * ATOMIC_ENTER_CRITICAL().
60 #if defined( portSET_INTERRUPT_MASK_FROM_ISR )
62 /* Nested interrupt scheme is supported in this port. */
63 #define ATOMIC_ENTER_CRITICAL() \
64 UBaseType_t uxCriticalSectionType = portSET_INTERRUPT_MASK_FROM_ISR()
66 #define ATOMIC_EXIT_CRITICAL() \
67 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxCriticalSectionType )
71 /* Nested interrupt scheme is NOT supported in this port. */
72 #define ATOMIC_ENTER_CRITICAL() portENTER_CRITICAL()
73 #define ATOMIC_EXIT_CRITICAL() portEXIT_CRITICAL()
75 #endif /* portSET_INTERRUPT_MASK_FROM_ISR() */
78 * Port specific definition -- "always inline".
79 * Inline is compiler specific, and may not always get inlined depending on your
80 * optimization level. Also, inline is considered as performance optimization
81 * for atomic. Thus, if portFORCE_INLINE is not provided by portmacro.h,
82 * instead of resulting error, simply define it away.
84 #ifndef portFORCE_INLINE
85 #define portFORCE_INLINE
88 #define ATOMIC_COMPARE_AND_SWAP_SUCCESS 0x1U /**< Compare and swap succeeded, swapped. */
89 #define ATOMIC_COMPARE_AND_SWAP_FAILURE 0x0U /**< Compare and swap failed, did not swap. */
91 /*----------------------------- Swap && CAS ------------------------------*/
94 * Atomic compare-and-swap
96 * @brief Performs an atomic compare-and-swap operation on the specified values.
98 * @param[in, out] pulDestination Pointer to memory location from where value is
99 * to be loaded and checked.
100 * @param[in] ulExchange If condition meets, write this value to memory.
101 * @param[in] ulComparand Swap condition.
103 * @return Unsigned integer of value 1 or 0. 1 for swapped, 0 for not swapped.
105 * @note This function only swaps *pulDestination with ulExchange, if previous
106 * *pulDestination value equals ulComparand.
108 static portFORCE_INLINE uint32_t Atomic_CompareAndSwap_u32( uint32_t volatile * pulDestination,
110 uint32_t ulComparand )
112 uint32_t ulReturnValue;
114 ATOMIC_ENTER_CRITICAL();
116 if( *pulDestination == ulComparand )
118 *pulDestination = ulExchange;
119 ulReturnValue = ATOMIC_COMPARE_AND_SWAP_SUCCESS;
123 ulReturnValue = ATOMIC_COMPARE_AND_SWAP_FAILURE;
126 ATOMIC_EXIT_CRITICAL();
128 return ulReturnValue;
130 /*-----------------------------------------------------------*/
133 * Atomic swap (pointers)
135 * @brief Atomically sets the address pointed to by *ppvDestination to the value
138 * @param[in, out] ppvDestination Pointer to memory location from where a pointer
139 * value is to be loaded and written back to.
140 * @param[in] pvExchange Pointer value to be written to *ppvDestination.
142 * @return The initial value of *ppvDestination.
144 static portFORCE_INLINE void * Atomic_SwapPointers_p32( void * volatile * ppvDestination,
149 ATOMIC_ENTER_CRITICAL();
151 pReturnValue = *ppvDestination;
152 *ppvDestination = pvExchange;
154 ATOMIC_EXIT_CRITICAL();
158 /*-----------------------------------------------------------*/
161 * Atomic compare-and-swap (pointers)
163 * @brief Performs an atomic compare-and-swap operation on the specified pointer
166 * @param[in, out] ppvDestination Pointer to memory location from where a pointer
167 * value is to be loaded and checked.
168 * @param[in] pvExchange If condition meets, write this value to memory.
169 * @param[in] pvComparand Swap condition.
171 * @return Unsigned integer of value 1 or 0. 1 for swapped, 0 for not swapped.
173 * @note This function only swaps *ppvDestination with pvExchange, if previous
174 * *ppvDestination value equals pvComparand.
176 static portFORCE_INLINE uint32_t Atomic_CompareAndSwapPointers_p32( void * volatile * ppvDestination,
180 uint32_t ulReturnValue = ATOMIC_COMPARE_AND_SWAP_FAILURE;
182 ATOMIC_ENTER_CRITICAL();
184 if( *ppvDestination == pvComparand )
186 *ppvDestination = pvExchange;
187 ulReturnValue = ATOMIC_COMPARE_AND_SWAP_SUCCESS;
190 ATOMIC_EXIT_CRITICAL();
192 return ulReturnValue;
196 /*----------------------------- Arithmetic ------------------------------*/
201 * @brief Atomically adds count to the value of the specified pointer points to.
203 * @param[in,out] pulAddend Pointer to memory location from where value is to be
204 * loaded and written back to.
205 * @param[in] ulCount Value to be added to *pulAddend.
207 * @return previous *pulAddend value.
209 static portFORCE_INLINE uint32_t Atomic_Add_u32( uint32_t volatile * pulAddend,
214 ATOMIC_ENTER_CRITICAL();
216 ulCurrent = *pulAddend;
217 *pulAddend += ulCount;
219 ATOMIC_EXIT_CRITICAL();
223 /*-----------------------------------------------------------*/
228 * @brief Atomically subtracts count from the value of the specified pointer
231 * @param[in,out] pulAddend Pointer to memory location from where value is to be
232 * loaded and written back to.
233 * @param[in] ulCount Value to be subtract from *pulAddend.
235 * @return previous *pulAddend value.
237 static portFORCE_INLINE uint32_t Atomic_Subtract_u32( uint32_t volatile * pulAddend,
242 ATOMIC_ENTER_CRITICAL();
244 ulCurrent = *pulAddend;
245 *pulAddend -= ulCount;
247 ATOMIC_EXIT_CRITICAL();
251 /*-----------------------------------------------------------*/
256 * @brief Atomically increments the value of the specified pointer points to.
258 * @param[in,out] pulAddend Pointer to memory location from where value is to be
259 * loaded and written back to.
261 * @return *pulAddend value before increment.
263 static portFORCE_INLINE uint32_t Atomic_Increment_u32( uint32_t volatile * pulAddend )
267 ATOMIC_ENTER_CRITICAL();
269 ulCurrent = *pulAddend;
272 ATOMIC_EXIT_CRITICAL();
276 /*-----------------------------------------------------------*/
281 * @brief Atomically decrements the value of the specified pointer points to
283 * @param[in,out] pulAddend Pointer to memory location from where value is to be
284 * loaded and written back to.
286 * @return *pulAddend value before decrement.
288 static portFORCE_INLINE uint32_t Atomic_Decrement_u32( uint32_t volatile * pulAddend )
292 ATOMIC_ENTER_CRITICAL();
294 ulCurrent = *pulAddend;
297 ATOMIC_EXIT_CRITICAL();
302 /*----------------------------- Bitwise Logical ------------------------------*/
307 * @brief Performs an atomic OR operation on the specified values.
309 * @param [in, out] pulDestination Pointer to memory location from where value is
310 * to be loaded and written back to.
311 * @param [in] ulValue Value to be ORed with *pulDestination.
313 * @return The original value of *pulDestination.
315 static portFORCE_INLINE uint32_t Atomic_OR_u32( uint32_t volatile * pulDestination,
320 ATOMIC_ENTER_CRITICAL();
322 ulCurrent = *pulDestination;
323 *pulDestination |= ulValue;
325 ATOMIC_EXIT_CRITICAL();
329 /*-----------------------------------------------------------*/
334 * @brief Performs an atomic AND operation on the specified values.
336 * @param [in, out] pulDestination Pointer to memory location from where value is
337 * to be loaded and written back to.
338 * @param [in] ulValue Value to be ANDed with *pulDestination.
340 * @return The original value of *pulDestination.
342 static portFORCE_INLINE uint32_t Atomic_AND_u32( uint32_t volatile * pulDestination,
347 ATOMIC_ENTER_CRITICAL();
349 ulCurrent = *pulDestination;
350 *pulDestination &= ulValue;
352 ATOMIC_EXIT_CRITICAL();
356 /*-----------------------------------------------------------*/
361 * @brief Performs an atomic NAND operation on the specified values.
363 * @param [in, out] pulDestination Pointer to memory location from where value is
364 * to be loaded and written back to.
365 * @param [in] ulValue Value to be NANDed with *pulDestination.
367 * @return The original value of *pulDestination.
369 static portFORCE_INLINE uint32_t Atomic_NAND_u32( uint32_t volatile * pulDestination,
374 ATOMIC_ENTER_CRITICAL();
376 ulCurrent = *pulDestination;
377 *pulDestination = ~( ulCurrent & ulValue );
379 ATOMIC_EXIT_CRITICAL();
383 /*-----------------------------------------------------------*/
388 * @brief Performs an atomic XOR operation on the specified values.
390 * @param [in, out] pulDestination Pointer to memory location from where value is
391 * to be loaded and written back to.
392 * @param [in] ulValue Value to be XORed with *pulDestination.
394 * @return The original value of *pulDestination.
396 static portFORCE_INLINE uint32_t Atomic_XOR_u32( uint32_t volatile * pulDestination,
401 ATOMIC_ENTER_CRITICAL();
403 ulCurrent = *pulDestination;
404 *pulDestination ^= ulValue;
406 ATOMIC_EXIT_CRITICAL();
417 #endif /* ATOMIC_H */