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