atomic.h 13 KB

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