atomic.h 13 KB

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