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