mbed_critical.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * Copyright (c) 2015-2016, ARM Limited, All Rights Reserved
  3. * SPDX-License-Identifier: Apache-2.0
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. * not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #ifndef __MBED_UTIL_CRITICAL_H__
  18. #define __MBED_UTIL_CRITICAL_H__
  19. #include <stdbool.h>
  20. #include <stdint.h>
  21. #include <stddef.h>
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /** \addtogroup platform */
  26. /** @{*/
  27. /**
  28. * \defgroup platform_critical critical section function
  29. * @{
  30. */
  31. /** Determine the current interrupts enabled state
  32. *
  33. * This function can be called to determine whether or not interrupts are currently enabled.
  34. * @note
  35. * NOTE:
  36. * This function works for both cortex-A and cortex-M, although the underlyng implementation
  37. * differs.
  38. * @return true if interrupts are enabled, false otherwise
  39. */
  40. bool core_util_are_interrupts_enabled(void);
  41. /** Determine if this code is executing from an interrupt
  42. *
  43. * This function can be called to determine if the code is running on interrupt context.
  44. * @note
  45. * NOTE:
  46. * This function works for both cortex-A and cortex-M, although the underlyng implementation
  47. * differs.
  48. * @return true if in an isr, false otherwise
  49. */
  50. bool core_util_is_isr_active(void);
  51. /** Mark the start of a critical section
  52. *
  53. * This function should be called to mark the start of a critical section of code.
  54. * @note
  55. * NOTES:
  56. * 1) The use of this style of critical section is targetted at C based implementations.
  57. * 2) These critical sections can be nested.
  58. * 3) The interrupt enable state on entry to the first critical section (of a nested set, or single
  59. * section) will be preserved on exit from the section.
  60. * 4) This implementation will currently only work on code running in privileged mode.
  61. */
  62. void core_util_critical_section_enter(void);
  63. /** Mark the end of a critical section
  64. *
  65. * This function should be called to mark the end of a critical section of code.
  66. * @note
  67. * NOTES:
  68. * 1) The use of this style of critical section is targetted at C based implementations.
  69. * 2) These critical sections can be nested.
  70. * 3) The interrupt enable state on entry to the first critical section (of a nested set, or single
  71. * section) will be preserved on exit from the section.
  72. * 4) This implementation will currently only work on code running in privileged mode.
  73. */
  74. void core_util_critical_section_exit(void);
  75. /**
  76. * Determine if we are currently in a critical section
  77. *
  78. * @return true if in a critical section, false otherwise.
  79. */
  80. bool core_util_in_critical_section(void);
  81. /**
  82. * Atomic compare and set. It compares the contents of a memory location to a
  83. * given value and, only if they are the same, modifies the contents of that
  84. * memory location to a given new value. This is done as a single atomic
  85. * operation. The atomicity guarantees that the new value is calculated based on
  86. * up-to-date information; if the value had been updated by another thread in
  87. * the meantime, the write would fail due to a mismatched expectedCurrentValue.
  88. *
  89. * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
  90. * you to the article on compare-and swap].
  91. *
  92. * @param ptr The target memory location.
  93. * @param[in,out] expectedCurrentValue A pointer to some location holding the
  94. * expected current value of the data being set atomically.
  95. * The computed 'desiredValue' should be a function of this current value.
  96. * @note: This is an in-out parameter. In the
  97. * failure case of atomic_cas (where the
  98. * destination isn't set), the pointee of expectedCurrentValue is
  99. * updated with the current value.
  100. * @param[in] desiredValue The new value computed based on '*expectedCurrentValue'.
  101. *
  102. * @return true if the memory location was atomically
  103. * updated with the desired value (after verifying
  104. * that it contained the expectedCurrentValue),
  105. * false otherwise. In the failure case,
  106. * exepctedCurrentValue is updated with the new
  107. * value of the target memory location.
  108. *
  109. * pseudocode:
  110. * function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
  111. * if *p != *old {
  112. * *old = *p
  113. * return false
  114. * }
  115. * *p = new
  116. * return true
  117. * }
  118. *
  119. * @note: In the failure case (where the destination isn't set), the value
  120. * pointed to by expectedCurrentValue is instead updated with the current value.
  121. * This property helps writing concise code for the following incr:
  122. *
  123. * function incr(p : pointer to int, a : int) returns int {
  124. * done = false
  125. * value = *p // This fetch operation need not be atomic.
  126. * while not done {
  127. * done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
  128. * }
  129. * return value + a
  130. * }
  131. *
  132. * @note: This corresponds to the C11 "atomic_compare_exchange_strong" - it
  133. * always succeeds if the current value is expected, as per the pseudocode
  134. * above; it will not spuriously fail as "atomic_compare_exchange_weak" may.
  135. */
  136. bool core_util_atomic_cas_u8(volatile uint8_t *ptr, uint8_t *expectedCurrentValue, uint8_t desiredValue);
  137. /**
  138. * Atomic compare and set. It compares the contents of a memory location to a
  139. * given value and, only if they are the same, modifies the contents of that
  140. * memory location to a given new value. This is done as a single atomic
  141. * operation. The atomicity guarantees that the new value is calculated based on
  142. * up-to-date information; if the value had been updated by another thread in
  143. * the meantime, the write would fail due to a mismatched expectedCurrentValue.
  144. *
  145. * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
  146. * you to the article on compare-and swap].
  147. *
  148. * @param ptr The target memory location.
  149. * @param[in,out] expectedCurrentValue A pointer to some location holding the
  150. * expected current value of the data being set atomically.
  151. * The computed 'desiredValue' should be a function of this current value.
  152. * @note: This is an in-out parameter. In the
  153. * failure case of atomic_cas (where the
  154. * destination isn't set), the pointee of expectedCurrentValue is
  155. * updated with the current value.
  156. * @param[in] desiredValue The new value computed based on '*expectedCurrentValue'.
  157. *
  158. * @return true if the memory location was atomically
  159. * updated with the desired value (after verifying
  160. * that it contained the expectedCurrentValue),
  161. * false otherwise. In the failure case,
  162. * exepctedCurrentValue is updated with the new
  163. * value of the target memory location.
  164. *
  165. * pseudocode:
  166. * function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
  167. * if *p != *old {
  168. * *old = *p
  169. * return false
  170. * }
  171. * *p = new
  172. * return true
  173. * }
  174. *
  175. * @note: In the failure case (where the destination isn't set), the value
  176. * pointed to by expectedCurrentValue is instead updated with the current value.
  177. * This property helps writing concise code for the following incr:
  178. *
  179. * function incr(p : pointer to int, a : int) returns int {
  180. * done = false
  181. * value = *p // This fetch operation need not be atomic.
  182. * while not done {
  183. * done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
  184. * }
  185. * return value + a
  186. * }
  187. *
  188. * @note: This corresponds to the C11 "atomic_compare_exchange_strong" - it
  189. * always succeeds if the current value is expected, as per the pseudocode
  190. * above; it will not spuriously fail as "atomic_compare_exchange_weak" may.
  191. */
  192. bool core_util_atomic_cas_u16(volatile uint16_t *ptr, uint16_t *expectedCurrentValue, uint16_t desiredValue);
  193. /**
  194. * Atomic compare and set. It compares the contents of a memory location to a
  195. * given value and, only if they are the same, modifies the contents of that
  196. * memory location to a given new value. This is done as a single atomic
  197. * operation. The atomicity guarantees that the new value is calculated based on
  198. * up-to-date information; if the value had been updated by another thread in
  199. * the meantime, the write would fail due to a mismatched expectedCurrentValue.
  200. *
  201. * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
  202. * you to the article on compare-and swap].
  203. *
  204. * @param ptr The target memory location.
  205. * @param[in,out] expectedCurrentValue A pointer to some location holding the
  206. * expected current value of the data being set atomically.
  207. * The computed 'desiredValue' should be a function of this current value.
  208. * @note: This is an in-out parameter. In the
  209. * failure case of atomic_cas (where the
  210. * destination isn't set), the pointee of expectedCurrentValue is
  211. * updated with the current value.
  212. * @param[in] desiredValue The new value computed based on '*expectedCurrentValue'.
  213. *
  214. * @return true if the memory location was atomically
  215. * updated with the desired value (after verifying
  216. * that it contained the expectedCurrentValue),
  217. * false otherwise. In the failure case,
  218. * exepctedCurrentValue is updated with the new
  219. * value of the target memory location.
  220. *
  221. * pseudocode:
  222. * function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
  223. * if *p != *old {
  224. * *old = *p
  225. * return false
  226. * }
  227. * *p = new
  228. * return true
  229. * }
  230. *
  231. * @note: In the failure case (where the destination isn't set), the value
  232. * pointed to by expectedCurrentValue is instead updated with the current value.
  233. * This property helps writing concise code for the following incr:
  234. *
  235. * function incr(p : pointer to int, a : int) returns int {
  236. * done = false
  237. * value = *p // This fetch operation need not be atomic.
  238. * while not done {
  239. * done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
  240. * }
  241. * return value + a
  242. *
  243. * @note: This corresponds to the C11 "atomic_compare_exchange_strong" - it
  244. * always succeeds if the current value is expected, as per the pseudocode
  245. * above; it will not spuriously fail as "atomic_compare_exchange_weak" may.
  246. * }
  247. */
  248. bool core_util_atomic_cas_u32(volatile uint32_t *ptr, uint32_t *expectedCurrentValue, uint32_t desiredValue);
  249. /**
  250. * Atomic compare and set. It compares the contents of a memory location to a
  251. * given value and, only if they are the same, modifies the contents of that
  252. * memory location to a given new value. This is done as a single atomic
  253. * operation. The atomicity guarantees that the new value is calculated based on
  254. * up-to-date information; if the value had been updated by another thread in
  255. * the meantime, the write would fail due to a mismatched expectedCurrentValue.
  256. *
  257. * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
  258. * you to the article on compare-and swap].
  259. *
  260. * @param ptr The target memory location.
  261. * @param[in,out] expectedCurrentValue A pointer to some location holding the
  262. * expected current value of the data being set atomically.
  263. * The computed 'desiredValue' should be a function of this current value.
  264. * @note: This is an in-out parameter. In the
  265. * failure case of atomic_cas (where the
  266. * destination isn't set), the pointee of expectedCurrentValue is
  267. * updated with the current value.
  268. * @param[in] desiredValue The new value computed based on '*expectedCurrentValue'.
  269. *
  270. * @return true if the memory location was atomically
  271. * updated with the desired value (after verifying
  272. * that it contained the expectedCurrentValue),
  273. * false otherwise. In the failure case,
  274. * exepctedCurrentValue is updated with the new
  275. * value of the target memory location.
  276. *
  277. * pseudocode:
  278. * function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
  279. * if *p != *old {
  280. * *old = *p
  281. * return false
  282. * }
  283. * *p = new
  284. * return true
  285. * }
  286. *
  287. * @note: In the failure case (where the destination isn't set), the value
  288. * pointed to by expectedCurrentValue is instead updated with the current value.
  289. * This property helps writing concise code for the following incr:
  290. *
  291. * function incr(p : pointer to int, a : int) returns int {
  292. * done = false
  293. * value = *p // This fetch operation need not be atomic.
  294. * while not done {
  295. * done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
  296. * }
  297. * return value + a
  298. * }
  299. *
  300. * @note: This corresponds to the C11 "atomic_compare_exchange_strong" - it
  301. * always succeeds if the current value is expected, as per the pseudocode
  302. * above; it will not spuriously fail as "atomic_compare_exchange_weak" may.
  303. */
  304. bool core_util_atomic_cas_ptr(void *volatile *ptr, void **expectedCurrentValue, void *desiredValue);
  305. /**
  306. * Atomic increment.
  307. * @param valuePtr Target memory location being incremented.
  308. * @param delta The amount being incremented.
  309. * @return The new incremented value.
  310. */
  311. uint8_t core_util_atomic_incr_u8(volatile uint8_t *valuePtr, uint8_t delta);
  312. /**
  313. * Atomic increment.
  314. * @param valuePtr Target memory location being incremented.
  315. * @param delta The amount being incremented.
  316. * @return The new incremented value.
  317. */
  318. uint16_t core_util_atomic_incr_u16(volatile uint16_t *valuePtr, uint16_t delta);
  319. /**
  320. * Atomic increment.
  321. * @param valuePtr Target memory location being incremented.
  322. * @param delta The amount being incremented.
  323. * @return The new incremented value.
  324. */
  325. uint32_t core_util_atomic_incr_u32(volatile uint32_t *valuePtr, uint32_t delta);
  326. /**
  327. * Atomic increment.
  328. * @param valuePtr Target memory location being incremented.
  329. * @param delta The amount being incremented in bytes.
  330. * @return The new incremented value.
  331. *
  332. * @note The type of the pointer argument is not taken into account
  333. * and the pointer is incremented by bytes.
  334. */
  335. void *core_util_atomic_incr_ptr(void *volatile *valuePtr, ptrdiff_t delta);
  336. /**
  337. * Atomic decrement.
  338. * @param valuePtr Target memory location being decremented.
  339. * @param delta The amount being decremented.
  340. * @return The new decremented value.
  341. */
  342. uint8_t core_util_atomic_decr_u8(volatile uint8_t *valuePtr, uint8_t delta);
  343. /**
  344. * Atomic decrement.
  345. * @param valuePtr Target memory location being decremented.
  346. * @param delta The amount being decremented.
  347. * @return The new decremented value.
  348. */
  349. uint16_t core_util_atomic_decr_u16(volatile uint16_t *valuePtr, uint16_t delta);
  350. /**
  351. * Atomic decrement.
  352. * @param valuePtr Target memory location being decremented.
  353. * @param delta The amount being decremented.
  354. * @return The new decremented value.
  355. */
  356. uint32_t core_util_atomic_decr_u32(volatile uint32_t *valuePtr, uint32_t delta);
  357. /**
  358. * Atomic decrement.
  359. * @param valuePtr Target memory location being decremented.
  360. * @param delta The amount being decremented in bytes.
  361. * @return The new decremented value.
  362. *
  363. * @note The type of the pointer argument is not taken into account
  364. * and the pointer is decremented by bytes
  365. */
  366. void *core_util_atomic_decr_ptr(void *volatile *valuePtr, ptrdiff_t delta);
  367. #ifdef __cplusplus
  368. } // extern "C"
  369. #endif
  370. /**@}*/
  371. /**@}*/
  372. #endif // __MBED_UTIL_CRITICAL_H__