2 * FreeRTOS Kernel V10.2.1
\r
3 * Copyright (C) 2019 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 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
30 * @brief FreeRTOS atomic operation support.
\r
32 * This file implements atomic by disabling interrupts globally.
\r
33 * Implementation with architecture specific atomic instructions
\r
34 * are to be provided under each compiler directory.
\r
40 #ifndef INC_FREERTOS_H
\r
41 #error "include FreeRTOS.h must appear in source files before include atomic.h"
\r
44 /* Standard includes. */
\r
51 /* Port specific definitions -- entering/exiting critical section.
\r
52 * Refer template -- ./lib/FreeRTOS/portable/Compiler/Arch/portmacro.h
\r
54 * Every call to ATOMIC_EXIT_CRITICAL() must be closely paired with
\r
55 * ATOMIC_ENTER_CRITICAL().
\r
57 #if defined( portSET_INTERRUPT_MASK_FROM_ISR )
\r
59 /* Nested interrupt scheme is supported in this port. */
\r
60 #define ATOMIC_ENTER_CRITICAL() \
\r
61 UBaseType_t uxCriticalSectionType = portSET_INTERRUPT_MASK_FROM_ISR()
\r
63 #define ATOMIC_EXIT_CRITICAL() \
\r
64 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxCriticalSectionType )
\r
68 /* Nested interrupt scheme is NOT supported in this port. */
\r
69 #define ATOMIC_ENTER_CRITICAL() portENTER_CRITICAL()
\r
70 #define ATOMIC_EXIT_CRITICAL() portEXIT_CRITICAL()
\r
72 #endif /* portSET_INTERRUPT_MASK_FROM_ISR() */
\r
74 /* Port specific definition -- "always inline".
\r
75 * Inline is compiler specific, and may not always get inlined depending on your optimization level.
\r
76 * Also, inline is considerred as performance optimization for atomic.
\r
77 * Thus, if portFORCE_INLINE is not provided by portmacro.h, instead of resulting error,
\r
78 * simply define it.
\r
80 #ifndef portFORCE_INLINE
\r
81 #define portFORCE_INLINE
\r
84 #define ATOMIC_COMPARE_AND_SWAP_SUCCESS 0x1U /**< Compare and swap succeeded, swapped. */
\r
85 #define ATOMIC_COMPARE_AND_SWAP_FAILURE 0x0U /**< Compare and swap failed, did not swap. */
\r
87 /*----------------------------- Swap && CAS ------------------------------*/
\r
90 * Atomic compare-and-swap
\r
92 * @brief Performs an atomic compare-and-swap operation on the specified values.
\r
94 * @param[in, out] pDestination Pointer to memory location from where value is
\r
95 * to be loaded and checked.
\r
96 * @param[in] ulExchange If condition meets, write this value to memory.
\r
97 * @param[in] ulComparand Swap condition.
\r
99 * @return Unsigned integer of value 1 or 0. 1 for swapped, 0 for not swapped.
\r
101 * @note This function only swaps *pDestination with ulExchange, if previous
\r
102 * *pDestination value equals ulComparand.
\r
104 static portFORCE_INLINE uint32_t Atomic_CompareAndSwap_u32(
\r
105 uint32_t volatile * pDestination,
\r
106 uint32_t ulExchange,
\r
107 uint32_t ulComparand )
\r
110 uint32_t ulReturnValue = ATOMIC_COMPARE_AND_SWAP_FAILURE;
\r
112 ATOMIC_ENTER_CRITICAL();
\r
114 if ( *pDestination == ulComparand )
\r
116 *pDestination = ulExchange;
\r
117 ulReturnValue = ATOMIC_COMPARE_AND_SWAP_SUCCESS;
\r
120 ATOMIC_EXIT_CRITICAL();
\r
122 return ulReturnValue;
\r
127 * Atomic swap (pointers)
\r
129 * @brief Atomically sets the address pointed to by *ppDestination to the value
\r
132 * @param[in, out] ppDestination Pointer to memory location from where a pointer
\r
133 * value is to be loaded and written back to.
\r
134 * @param[in] pExchange Pointer value to be written to *ppDestination.
\r
136 * @return The initial value of *ppDestination.
\r
138 static portFORCE_INLINE void * Atomic_SwapPointers_p32(
\r
139 void * volatile * ppDestination,
\r
142 void * pReturnValue;
\r
144 ATOMIC_ENTER_CRITICAL();
\r
146 pReturnValue = *ppDestination;
\r
148 *ppDestination = pExchange;
\r
150 ATOMIC_EXIT_CRITICAL();
\r
152 return pReturnValue;
\r
156 * Atomic compare-and-swap (pointers)
\r
158 * @brief Performs an atomic compare-and-swap operation on the specified pointer
\r
161 * @param[in, out] ppDestination Pointer to memory location from where a pointer
\r
162 * value is to be loaded and checked.
\r
163 * @param[in] pExchange If condition meets, write this value to memory.
\r
164 * @param[in] pComparand Swap condition.
\r
166 * @return Unsigned integer of value 1 or 0. 1 for swapped, 0 for not swapped.
\r
168 * @note This function only swaps *ppDestination with pExchange, if previous
\r
169 * *ppDestination value equals pComparand.
\r
171 static portFORCE_INLINE uint32_t Atomic_CompareAndSwapPointers_p32(
\r
172 void * volatile * ppDestination,
\r
173 void * pExchange, void * pComparand )
\r
175 uint32_t ulReturnValue = ATOMIC_COMPARE_AND_SWAP_FAILURE;
\r
177 ATOMIC_ENTER_CRITICAL();
\r
179 if ( *ppDestination == pComparand )
\r
181 *ppDestination = pExchange;
\r
182 ulReturnValue = ATOMIC_COMPARE_AND_SWAP_SUCCESS;
\r
185 ATOMIC_EXIT_CRITICAL();
\r
187 return ulReturnValue;
\r
191 /*----------------------------- Arithmetic ------------------------------*/
\r
196 * @brief Atomically adds count to the value of the specified pointer points to.
\r
198 * @param[in,out] pAddend Pointer to memory location from where value is to be
\r
199 * loaded and written back to.
\r
200 * @param[in] ulCount Value to be added to *pAddend.
\r
202 * @return previous *pAddend value.
\r
204 static portFORCE_INLINE uint32_t Atomic_Add_u32(
\r
205 uint32_t volatile * pAddend,
\r
208 uint32_t ulCurrent;
\r
210 ATOMIC_ENTER_CRITICAL();
\r
212 ulCurrent = *pAddend;
\r
214 *pAddend += ulCount;
\r
216 ATOMIC_EXIT_CRITICAL();
\r
224 * @brief Atomically subtracts count from the value of the specified pointer
\r
227 * @param[in,out] pAddend Pointer to memory location from where value is to be
\r
228 * loaded and written back to.
\r
229 * @param[in] ulCount Value to be subtract from *pAddend.
\r
231 * @return previous *pAddend value.
\r
233 static portFORCE_INLINE uint32_t Atomic_Subtract_u32(
\r
234 uint32_t volatile * pAddend,
\r
237 uint32_t ulCurrent;
\r
239 ATOMIC_ENTER_CRITICAL();
\r
241 ulCurrent = *pAddend;
\r
243 *pAddend -= ulCount;
\r
245 ATOMIC_EXIT_CRITICAL();
\r
253 * @brief Atomically increments the value of the specified pointer points to.
\r
255 * @param[in,out] pAddend Pointer to memory location from where value is to be
\r
256 * loaded and written back to.
\r
258 * @return *pAddend value before increment.
\r
260 static portFORCE_INLINE uint32_t Atomic_Increment_u32( uint32_t volatile * pAddend )
\r
262 uint32_t ulCurrent;
\r
264 ATOMIC_ENTER_CRITICAL();
\r
266 ulCurrent = *pAddend;
\r
270 ATOMIC_EXIT_CRITICAL();
\r
278 * @brief Atomically decrements the value of the specified pointer points to
\r
280 * @param[in,out] pAddend Pointer to memory location from where value is to be
\r
281 * loaded and written back to.
\r
283 * @return *pAddend value before decrement.
\r
285 static portFORCE_INLINE uint32_t Atomic_Decrement_u32( uint32_t volatile * pAddend )
\r
287 uint32_t ulCurrent;
\r
289 ATOMIC_ENTER_CRITICAL();
\r
291 ulCurrent = *pAddend;
\r
295 ATOMIC_EXIT_CRITICAL();
\r
300 /*----------------------------- Bitwise Logical ------------------------------*/
\r
305 * @brief Performs an atomic OR operation on the specified values.
\r
307 * @param [in, out] pDestination Pointer to memory location from where value is
\r
308 * to be loaded and written back to.
\r
309 * @param [in] ulValue Value to be ORed with *pDestination.
\r
311 * @return The original value of *pDestination.
\r
313 static portFORCE_INLINE uint32_t Atomic_OR_u32(
\r
314 uint32_t volatile * pDestination,
\r
317 uint32_t ulCurrent;
\r
319 ATOMIC_ENTER_CRITICAL();
\r
321 ulCurrent = *pDestination;
\r
323 *pDestination |= ulValue;
\r
325 ATOMIC_EXIT_CRITICAL();
\r
333 * @brief Performs an atomic AND operation on the specified values.
\r
335 * @param [in, out] pDestination Pointer to memory location from where value is
\r
336 * to be loaded and written back to.
\r
337 * @param [in] ulValue Value to be ANDed with *pDestination.
\r
339 * @return The original value of *pDestination.
\r
341 static portFORCE_INLINE uint32_t Atomic_AND_u32(
\r
342 uint32_t volatile * pDestination,
\r
345 uint32_t ulCurrent;
\r
347 ATOMIC_ENTER_CRITICAL();
\r
349 ulCurrent = *pDestination;
\r
351 *pDestination &= ulValue;
\r
353 ATOMIC_EXIT_CRITICAL();
\r
361 * @brief Performs an atomic NAND operation on the specified values.
\r
363 * @param [in, out] pDestination 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 *pDestination.
\r
367 * @return The original value of *pDestination.
\r
369 static portFORCE_INLINE uint32_t Atomic_NAND_u32(
\r
370 uint32_t volatile * pDestination,
\r
373 uint32_t ulCurrent;
\r
375 ATOMIC_ENTER_CRITICAL();
\r
377 ulCurrent = *pDestination;
\r
379 *pDestination = ~(ulCurrent & ulValue);
\r
381 ATOMIC_EXIT_CRITICAL();
\r
389 * @brief Performs an atomic XOR operation on the specified values.
\r
391 * @param [in, out] pDestination Pointer to memory location from where value is
\r
392 * to be loaded and written back to.
\r
393 * @param [in] ulValue Value to be XORed with *pDestination.
\r
395 * @return The original value of *pDestination.
\r
397 static portFORCE_INLINE uint32_t Atomic_XOR_u32(
\r
398 uint32_t volatile * pDestination,
\r
401 uint32_t ulCurrent;
\r
403 ATOMIC_ENTER_CRITICAL();
\r
405 ulCurrent = *pDestination;
\r
407 *pDestination ^= ulValue;
\r
409 ATOMIC_EXIT_CRITICAL();
\r
418 #endif /* ATOMIC_H */
\r