atomic.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * FreeRTOS Kernel V10.3.1
  3. * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  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:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  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.
  21. *
  22. * http://www.FreeRTOS.org
  23. * http://aws.amazon.com/freertos
  24. *
  25. * 1 tab == 4 spaces!
  26. */
  27. /**
  28. * @file atomic.h
  29. * @brief FreeRTOS atomic operation support.
  30. *
  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.
  34. */
  35. #ifndef ATOMIC_H
  36. #define ATOMIC_H
  37. #ifndef INC_FREERTOS_H
  38. #error "include FreeRTOS.h must appear in source files before include atomic.h"
  39. #endif
  40. /* Standard includes. */
  41. #include <stdint.h>
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. /*
  46. * Port specific definitions -- entering/exiting critical section.
  47. * Refer template -- ./lib/FreeRTOS/portable/Compiler/Arch/portmacro.h
  48. *
  49. * Every call to ATOMIC_EXIT_CRITICAL() must be closely paired with
  50. * ATOMIC_ENTER_CRITICAL().
  51. *
  52. */
  53. #if defined( portSET_INTERRUPT_MASK_FROM_ISR )
  54. /* Nested interrupt scheme is supported in this port. */
  55. #define ATOMIC_ENTER_CRITICAL() \
  56. UBaseType_t uxCriticalSectionType = portSET_INTERRUPT_MASK_FROM_ISR()
  57. #define ATOMIC_EXIT_CRITICAL() \
  58. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxCriticalSectionType )
  59. #else
  60. /* Nested interrupt scheme is NOT supported in this port. */
  61. #define ATOMIC_ENTER_CRITICAL() portENTER_CRITICAL()
  62. #define ATOMIC_EXIT_CRITICAL() portEXIT_CRITICAL()
  63. #endif /* portSET_INTERRUPT_MASK_FROM_ISR() */
  64. /*
  65. * Port specific definition -- "always inline".
  66. * Inline is compiler specific, and may not always get inlined depending on your
  67. * optimization level. Also, inline is considered as performance optimization
  68. * for atomic. Thus, if portFORCE_INLINE is not provided by portmacro.h,
  69. * instead of resulting error, simply define it away.
  70. */
  71. #ifndef portFORCE_INLINE
  72. #define portFORCE_INLINE
  73. #endif
  74. #define ATOMIC_COMPARE_AND_SWAP_SUCCESS 0x1U /**< Compare and swap succeeded, swapped. */
  75. #define ATOMIC_COMPARE_AND_SWAP_FAILURE 0x0U /**< Compare and swap failed, did not swap. */
  76. /*----------------------------- Swap && CAS ------------------------------*/
  77. /**
  78. * Atomic compare-and-swap
  79. *
  80. * @brief Performs an atomic compare-and-swap operation on the specified values.
  81. *
  82. * @param[in, out] pulDestination Pointer to memory location from where value is
  83. * to be loaded and checked.
  84. * @param[in] ulExchange If condition meets, write this value to memory.
  85. * @param[in] ulComparand Swap condition.
  86. *
  87. * @return Unsigned integer of value 1 or 0. 1 for swapped, 0 for not swapped.
  88. *
  89. * @note This function only swaps *pulDestination with ulExchange, if previous
  90. * *pulDestination value equals ulComparand.
  91. */
  92. static portFORCE_INLINE uint32_t Atomic_CompareAndSwap_u32( uint32_t volatile * pulDestination,
  93. uint32_t ulExchange,
  94. uint32_t ulComparand )
  95. {
  96. uint32_t ulReturnValue;
  97. ATOMIC_ENTER_CRITICAL();
  98. {
  99. if( *pulDestination == ulComparand )
  100. {
  101. *pulDestination = ulExchange;
  102. ulReturnValue = ATOMIC_COMPARE_AND_SWAP_SUCCESS;
  103. }
  104. else
  105. {
  106. ulReturnValue = ATOMIC_COMPARE_AND_SWAP_FAILURE;
  107. }
  108. }
  109. ATOMIC_EXIT_CRITICAL();
  110. return ulReturnValue;
  111. }
  112. /*-----------------------------------------------------------*/
  113. /**
  114. * Atomic swap (pointers)
  115. *
  116. * @brief Atomically sets the address pointed to by *ppvDestination to the value
  117. * of *pvExchange.
  118. *
  119. * @param[in, out] ppvDestination Pointer to memory location from where a pointer
  120. * value is to be loaded and written back to.
  121. * @param[in] pvExchange Pointer value to be written to *ppvDestination.
  122. *
  123. * @return The initial value of *ppvDestination.
  124. */
  125. static portFORCE_INLINE void * Atomic_SwapPointers_p32( void * volatile * ppvDestination,
  126. void * pvExchange )
  127. {
  128. void * pReturnValue;
  129. ATOMIC_ENTER_CRITICAL();
  130. {
  131. pReturnValue = *ppvDestination;
  132. *ppvDestination = pvExchange;
  133. }
  134. ATOMIC_EXIT_CRITICAL();
  135. return pReturnValue;
  136. }
  137. /*-----------------------------------------------------------*/
  138. /**
  139. * Atomic compare-and-swap (pointers)
  140. *
  141. * @brief Performs an atomic compare-and-swap operation on the specified pointer
  142. * values.
  143. *
  144. * @param[in, out] ppvDestination Pointer to memory location from where a pointer
  145. * value is to be loaded and checked.
  146. * @param[in] pvExchange If condition meets, write this value to memory.
  147. * @param[in] pvComparand Swap condition.
  148. *
  149. * @return Unsigned integer of value 1 or 0. 1 for swapped, 0 for not swapped.
  150. *
  151. * @note This function only swaps *ppvDestination with pvExchange, if previous
  152. * *ppvDestination value equals pvComparand.
  153. */
  154. static portFORCE_INLINE uint32_t Atomic_CompareAndSwapPointers_p32( void * volatile * ppvDestination,
  155. void * pvExchange,
  156. void * pvComparand )
  157. {
  158. uint32_t ulReturnValue = ATOMIC_COMPARE_AND_SWAP_FAILURE;
  159. ATOMIC_ENTER_CRITICAL();
  160. {
  161. if( *ppvDestination == pvComparand )
  162. {
  163. *ppvDestination = pvExchange;
  164. ulReturnValue = ATOMIC_COMPARE_AND_SWAP_SUCCESS;
  165. }
  166. }
  167. ATOMIC_EXIT_CRITICAL();
  168. return ulReturnValue;
  169. }
  170. /*----------------------------- Arithmetic ------------------------------*/
  171. /**
  172. * Atomic add
  173. *
  174. * @brief Atomically adds count to the value of the specified pointer points to.
  175. *
  176. * @param[in,out] pulAddend Pointer to memory location from where value is to be
  177. * loaded and written back to.
  178. * @param[in] ulCount Value to be added to *pulAddend.
  179. *
  180. * @return previous *pulAddend value.
  181. */
  182. static portFORCE_INLINE uint32_t Atomic_Add_u32( uint32_t volatile * pulAddend,
  183. uint32_t ulCount )
  184. {
  185. uint32_t ulCurrent;
  186. ATOMIC_ENTER_CRITICAL();
  187. {
  188. ulCurrent = *pulAddend;
  189. *pulAddend += ulCount;
  190. }
  191. ATOMIC_EXIT_CRITICAL();
  192. return ulCurrent;
  193. }
  194. /*-----------------------------------------------------------*/
  195. /**
  196. * Atomic subtract
  197. *
  198. * @brief Atomically subtracts count from the value of the specified pointer
  199. * pointers to.
  200. *
  201. * @param[in,out] pulAddend Pointer to memory location from where value is to be
  202. * loaded and written back to.
  203. * @param[in] ulCount Value to be subtract from *pulAddend.
  204. *
  205. * @return previous *pulAddend value.
  206. */
  207. static portFORCE_INLINE uint32_t Atomic_Subtract_u32( uint32_t volatile * pulAddend,
  208. uint32_t ulCount )
  209. {
  210. uint32_t ulCurrent;
  211. ATOMIC_ENTER_CRITICAL();
  212. {
  213. ulCurrent = *pulAddend;
  214. *pulAddend -= ulCount;
  215. }
  216. ATOMIC_EXIT_CRITICAL();
  217. return ulCurrent;
  218. }
  219. /*-----------------------------------------------------------*/
  220. /**
  221. * Atomic increment
  222. *
  223. * @brief Atomically increments the value of the specified pointer points to.
  224. *
  225. * @param[in,out] pulAddend Pointer to memory location from where value is to be
  226. * loaded and written back to.
  227. *
  228. * @return *pulAddend value before increment.
  229. */
  230. static portFORCE_INLINE uint32_t Atomic_Increment_u32( uint32_t volatile * pulAddend )
  231. {
  232. uint32_t ulCurrent;
  233. ATOMIC_ENTER_CRITICAL();
  234. {
  235. ulCurrent = *pulAddend;
  236. *pulAddend += 1;
  237. }
  238. ATOMIC_EXIT_CRITICAL();
  239. return ulCurrent;
  240. }
  241. /*-----------------------------------------------------------*/
  242. /**
  243. * Atomic decrement
  244. *
  245. * @brief Atomically decrements the value of the specified pointer points to
  246. *
  247. * @param[in,out] pulAddend Pointer to memory location from where value is to be
  248. * loaded and written back to.
  249. *
  250. * @return *pulAddend value before decrement.
  251. */
  252. static portFORCE_INLINE uint32_t Atomic_Decrement_u32( uint32_t volatile * pulAddend )
  253. {
  254. uint32_t ulCurrent;
  255. ATOMIC_ENTER_CRITICAL();
  256. {
  257. ulCurrent = *pulAddend;
  258. *pulAddend -= 1;
  259. }
  260. ATOMIC_EXIT_CRITICAL();
  261. return ulCurrent;
  262. }
  263. /*----------------------------- Bitwise Logical ------------------------------*/
  264. /**
  265. * Atomic OR
  266. *
  267. * @brief Performs an atomic OR operation on the specified values.
  268. *
  269. * @param [in, out] pulDestination Pointer to memory location from where value is
  270. * to be loaded and written back to.
  271. * @param [in] ulValue Value to be ORed with *pulDestination.
  272. *
  273. * @return The original value of *pulDestination.
  274. */
  275. static portFORCE_INLINE uint32_t Atomic_OR_u32( uint32_t volatile * pulDestination,
  276. uint32_t ulValue )
  277. {
  278. uint32_t ulCurrent;
  279. ATOMIC_ENTER_CRITICAL();
  280. {
  281. ulCurrent = *pulDestination;
  282. *pulDestination |= ulValue;
  283. }
  284. ATOMIC_EXIT_CRITICAL();
  285. return ulCurrent;
  286. }
  287. /*-----------------------------------------------------------*/
  288. /**
  289. * Atomic AND
  290. *
  291. * @brief Performs an atomic AND operation on the specified values.
  292. *
  293. * @param [in, out] pulDestination Pointer to memory location from where value is
  294. * to be loaded and written back to.
  295. * @param [in] ulValue Value to be ANDed with *pulDestination.
  296. *
  297. * @return The original value of *pulDestination.
  298. */
  299. static portFORCE_INLINE uint32_t Atomic_AND_u32( uint32_t volatile * pulDestination,
  300. uint32_t ulValue )
  301. {
  302. uint32_t ulCurrent;
  303. ATOMIC_ENTER_CRITICAL();
  304. {
  305. ulCurrent = *pulDestination;
  306. *pulDestination &= ulValue;
  307. }
  308. ATOMIC_EXIT_CRITICAL();
  309. return ulCurrent;
  310. }
  311. /*-----------------------------------------------------------*/
  312. /**
  313. * Atomic NAND
  314. *
  315. * @brief Performs an atomic NAND operation on the specified values.
  316. *
  317. * @param [in, out] pulDestination Pointer to memory location from where value is
  318. * to be loaded and written back to.
  319. * @param [in] ulValue Value to be NANDed with *pulDestination.
  320. *
  321. * @return The original value of *pulDestination.
  322. */
  323. static portFORCE_INLINE uint32_t Atomic_NAND_u32( uint32_t volatile * pulDestination,
  324. uint32_t ulValue )
  325. {
  326. uint32_t ulCurrent;
  327. ATOMIC_ENTER_CRITICAL();
  328. {
  329. ulCurrent = *pulDestination;
  330. *pulDestination = ~( ulCurrent & ulValue );
  331. }
  332. ATOMIC_EXIT_CRITICAL();
  333. return ulCurrent;
  334. }
  335. /*-----------------------------------------------------------*/
  336. /**
  337. * Atomic XOR
  338. *
  339. * @brief Performs an atomic XOR operation on the specified values.
  340. *
  341. * @param [in, out] pulDestination Pointer to memory location from where value is
  342. * to be loaded and written back to.
  343. * @param [in] ulValue Value to be XORed with *pulDestination.
  344. *
  345. * @return The original value of *pulDestination.
  346. */
  347. static portFORCE_INLINE uint32_t Atomic_XOR_u32( uint32_t volatile * pulDestination,
  348. uint32_t ulValue )
  349. {
  350. uint32_t ulCurrent;
  351. ATOMIC_ENTER_CRITICAL();
  352. {
  353. ulCurrent = *pulDestination;
  354. *pulDestination ^= ulValue;
  355. }
  356. ATOMIC_EXIT_CRITICAL();
  357. return ulCurrent;
  358. }
  359. #ifdef __cplusplus
  360. }
  361. #endif
  362. #endif /* ATOMIC_H */