nrf_atomic.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /**
  2. * Copyright (c) 2018 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "nrf_atomic.h"
  41. #ifndef NRF_ATOMIC_USE_BUILD_IN
  42. #if (defined(__GNUC__) && defined(WIN32))
  43. #define NRF_ATOMIC_USE_BUILD_IN 1
  44. #else
  45. #define NRF_ATOMIC_USE_BUILD_IN 0
  46. #endif
  47. #endif // NRF_ATOMIC_USE_BUILD_IN
  48. #if ((__CORTEX_M >= 0x03U) || (__CORTEX_SC >= 300U))
  49. #define STREX_LDREX_PRESENT
  50. #else
  51. #include "app_util_platform.h"
  52. #endif
  53. #if (NRF_ATOMIC_USE_BUILD_IN == 0) && defined(STREX_LDREX_PRESENT)
  54. #include "nrf_atomic_internal.h"
  55. #endif
  56. uint32_t nrf_atomic_u32_fetch_store(nrf_atomic_u32_t * p_data, uint32_t value)
  57. {
  58. #if NRF_ATOMIC_USE_BUILD_IN
  59. return __atomic_exchange_n(p_data, value, __ATOMIC_SEQ_CST);
  60. #elif defined(STREX_LDREX_PRESENT)
  61. uint32_t old_val;
  62. uint32_t new_val;
  63. NRF_ATOMIC_OP(mov, old_val, new_val, p_data, value);
  64. UNUSED_PARAMETER(old_val);
  65. UNUSED_PARAMETER(new_val);
  66. return old_val;
  67. #else
  68. CRITICAL_REGION_ENTER();
  69. uint32_t old_val = *p_data;
  70. *p_data = value;
  71. CRITICAL_REGION_EXIT();
  72. return old_val;
  73. #endif //NRF_ATOMIC_USE_BUILD_IN
  74. }
  75. uint32_t nrf_atomic_u32_store(nrf_atomic_u32_t * p_data, uint32_t value)
  76. {
  77. #if NRF_ATOMIC_USE_BUILD_IN
  78. __atomic_store_n(p_data, value, __ATOMIC_SEQ_CST);
  79. return value;
  80. #elif defined(STREX_LDREX_PRESENT)
  81. uint32_t old_val;
  82. uint32_t new_val;
  83. NRF_ATOMIC_OP(mov, old_val, new_val, p_data, value);
  84. UNUSED_PARAMETER(old_val);
  85. UNUSED_PARAMETER(new_val);
  86. return new_val;
  87. #else
  88. CRITICAL_REGION_ENTER();
  89. *p_data = value;
  90. CRITICAL_REGION_EXIT();
  91. return value;
  92. #endif //NRF_ATOMIC_USE_BUILD_IN
  93. }
  94. uint32_t nrf_atomic_u32_fetch_or(nrf_atomic_u32_t * p_data, uint32_t value)
  95. {
  96. #if NRF_ATOMIC_USE_BUILD_IN
  97. return __atomic_fetch_or(p_data, value, __ATOMIC_SEQ_CST);
  98. #elif defined(STREX_LDREX_PRESENT)
  99. uint32_t old_val;
  100. uint32_t new_val;
  101. NRF_ATOMIC_OP(orr, old_val, new_val, p_data, value);
  102. UNUSED_PARAMETER(old_val);
  103. UNUSED_PARAMETER(new_val);
  104. return old_val;
  105. #else
  106. CRITICAL_REGION_ENTER();
  107. uint32_t old_val = *p_data;
  108. *p_data |= value;
  109. CRITICAL_REGION_EXIT();
  110. return old_val;
  111. #endif //NRF_ATOMIC_USE_BUILD_IN
  112. }
  113. uint32_t nrf_atomic_u32_or(nrf_atomic_u32_t * p_data, uint32_t value)
  114. {
  115. #if NRF_ATOMIC_USE_BUILD_IN
  116. return __atomic_or_fetch(p_data, value, __ATOMIC_SEQ_CST);
  117. #elif defined(STREX_LDREX_PRESENT)
  118. uint32_t old_val;
  119. uint32_t new_val;
  120. NRF_ATOMIC_OP(orr, old_val, new_val, p_data, value);
  121. UNUSED_PARAMETER(old_val);
  122. UNUSED_PARAMETER(new_val);
  123. return new_val;
  124. #else
  125. CRITICAL_REGION_ENTER();
  126. *p_data |= value;
  127. uint32_t new_value = *p_data;
  128. CRITICAL_REGION_EXIT();
  129. return new_value;
  130. #endif //NRF_ATOMIC_USE_BUILD_IN
  131. }
  132. uint32_t nrf_atomic_u32_fetch_and(nrf_atomic_u32_t * p_data, uint32_t value)
  133. {
  134. #if NRF_ATOMIC_USE_BUILD_IN
  135. return __atomic_fetch_and(p_data, value, __ATOMIC_SEQ_CST);
  136. #elif defined(STREX_LDREX_PRESENT)
  137. uint32_t old_val;
  138. uint32_t new_val;
  139. NRF_ATOMIC_OP(and, old_val, new_val, p_data, value);
  140. UNUSED_PARAMETER(old_val);
  141. UNUSED_PARAMETER(new_val);
  142. return old_val;
  143. #else
  144. CRITICAL_REGION_ENTER();
  145. uint32_t old_val = *p_data;
  146. *p_data &= value;
  147. CRITICAL_REGION_EXIT();
  148. return old_val;
  149. #endif //NRF_ATOMIC_USE_BUILD_IN
  150. }
  151. uint32_t nrf_atomic_u32_and(nrf_atomic_u32_t * p_data, uint32_t value)
  152. {
  153. #if NRF_ATOMIC_USE_BUILD_IN
  154. return __atomic_and_fetch(p_data, value, __ATOMIC_SEQ_CST);
  155. #elif defined(STREX_LDREX_PRESENT)
  156. uint32_t old_val;
  157. uint32_t new_val;
  158. NRF_ATOMIC_OP(and, old_val, new_val, p_data, value);
  159. UNUSED_PARAMETER(old_val);
  160. UNUSED_PARAMETER(new_val);
  161. return new_val;
  162. #else
  163. CRITICAL_REGION_ENTER();
  164. *p_data &= value;
  165. uint32_t new_value = *p_data;
  166. CRITICAL_REGION_EXIT();
  167. return new_value;
  168. #endif //NRF_ATOMIC_USE_BUILD_IN
  169. }
  170. uint32_t nrf_atomic_u32_fetch_xor(nrf_atomic_u32_t * p_data, uint32_t value)
  171. {
  172. #if NRF_ATOMIC_USE_BUILD_IN
  173. return __atomic_fetch_xor(p_data, value, __ATOMIC_SEQ_CST);
  174. #elif defined(STREX_LDREX_PRESENT)
  175. uint32_t old_val;
  176. uint32_t new_val;
  177. NRF_ATOMIC_OP(eor, old_val, new_val, p_data, value);
  178. UNUSED_PARAMETER(old_val);
  179. UNUSED_PARAMETER(new_val);
  180. return old_val;
  181. #else
  182. CRITICAL_REGION_ENTER();
  183. uint32_t old_val = *p_data;
  184. *p_data ^= value;
  185. CRITICAL_REGION_EXIT();
  186. return old_val;
  187. #endif //NRF_ATOMIC_USE_BUILD_IN
  188. }
  189. uint32_t nrf_atomic_u32_xor(nrf_atomic_u32_t * p_data, uint32_t value)
  190. {
  191. #if NRF_ATOMIC_USE_BUILD_IN
  192. return __atomic_xor_fetch(p_data, value, __ATOMIC_SEQ_CST);
  193. #elif defined(STREX_LDREX_PRESENT)
  194. uint32_t old_val;
  195. uint32_t new_val;
  196. NRF_ATOMIC_OP(eor, old_val, new_val, p_data, value);
  197. UNUSED_PARAMETER(old_val);
  198. UNUSED_PARAMETER(new_val);
  199. return new_val;
  200. #else
  201. CRITICAL_REGION_ENTER();
  202. *p_data ^= value;
  203. uint32_t new_value = *p_data;
  204. CRITICAL_REGION_EXIT();
  205. return new_value;
  206. #endif //NRF_ATOMIC_USE_BUILD_IN
  207. }
  208. uint32_t nrf_atomic_u32_fetch_add(nrf_atomic_u32_t * p_data, uint32_t value)
  209. {
  210. #if NRF_ATOMIC_USE_BUILD_IN
  211. return __atomic_fetch_add(p_data, value, __ATOMIC_SEQ_CST);
  212. #elif defined(STREX_LDREX_PRESENT)
  213. uint32_t old_val;
  214. uint32_t new_val;
  215. NRF_ATOMIC_OP(add, old_val, new_val, p_data, value);
  216. UNUSED_PARAMETER(old_val);
  217. UNUSED_PARAMETER(new_val);
  218. return old_val;
  219. #else
  220. CRITICAL_REGION_ENTER();
  221. uint32_t old_val = *p_data;
  222. *p_data += value;
  223. CRITICAL_REGION_EXIT();
  224. return old_val;
  225. #endif //NRF_ATOMIC_USE_BUILD_IN
  226. }
  227. uint32_t nrf_atomic_u32_add(nrf_atomic_u32_t * p_data, uint32_t value)
  228. {
  229. #if NRF_ATOMIC_USE_BUILD_IN
  230. return __atomic_add_fetch(p_data, value, __ATOMIC_SEQ_CST);
  231. #elif defined(STREX_LDREX_PRESENT)
  232. uint32_t old_val;
  233. uint32_t new_val;
  234. NRF_ATOMIC_OP(add, old_val, new_val, p_data, value);
  235. UNUSED_PARAMETER(old_val);
  236. UNUSED_PARAMETER(new_val);
  237. return new_val;
  238. #else
  239. CRITICAL_REGION_ENTER();
  240. *p_data += value;
  241. uint32_t new_value = *p_data;
  242. CRITICAL_REGION_EXIT();
  243. return new_value;
  244. #endif //NRF_ATOMIC_USE_BUILD_IN
  245. }
  246. uint32_t nrf_atomic_u32_fetch_sub(nrf_atomic_u32_t * p_data, uint32_t value)
  247. {
  248. #if NRF_ATOMIC_USE_BUILD_IN
  249. return __atomic_fetch_sub(p_data, value, __ATOMIC_SEQ_CST);
  250. #elif defined(STREX_LDREX_PRESENT)
  251. uint32_t old_val;
  252. uint32_t new_val;
  253. NRF_ATOMIC_OP(sub, old_val, new_val, p_data, value);
  254. UNUSED_PARAMETER(old_val);
  255. UNUSED_PARAMETER(new_val);
  256. return old_val;
  257. #else
  258. CRITICAL_REGION_ENTER();
  259. uint32_t old_val = *p_data;
  260. *p_data -= value;
  261. CRITICAL_REGION_EXIT();
  262. return old_val;
  263. #endif //NRF_ATOMIC_USE_BUILD_IN
  264. }
  265. uint32_t nrf_atomic_u32_sub(nrf_atomic_u32_t * p_data, uint32_t value)
  266. {
  267. #if NRF_ATOMIC_USE_BUILD_IN
  268. return __atomic_sub_fetch(p_data, value, __ATOMIC_SEQ_CST);
  269. #elif defined(STREX_LDREX_PRESENT)
  270. uint32_t old_val;
  271. uint32_t new_val;
  272. NRF_ATOMIC_OP(sub, old_val, new_val, p_data, value);
  273. UNUSED_PARAMETER(old_val);
  274. UNUSED_PARAMETER(new_val);
  275. return new_val;
  276. #else
  277. CRITICAL_REGION_ENTER();
  278. *p_data -= value;
  279. uint32_t new_value = *p_data;
  280. CRITICAL_REGION_EXIT();
  281. return new_value;
  282. #endif //NRF_ATOMIC_USE_BUILD_IN
  283. }
  284. bool nrf_atomic_u32_cmp_exch(nrf_atomic_u32_t * p_data,
  285. uint32_t * p_expected,
  286. uint32_t desired)
  287. {
  288. #if NRF_ATOMIC_USE_BUILD_IN
  289. return __atomic_compare_exchange(p_data,
  290. p_expected,
  291. &desired,
  292. 1,
  293. __ATOMIC_SEQ_CST,
  294. __ATOMIC_SEQ_CST);
  295. #elif defined(STREX_LDREX_PRESENT)
  296. return nrf_atomic_internal_cmp_exch(p_data, p_expected, desired);
  297. #else
  298. bool ret;
  299. CRITICAL_REGION_ENTER();
  300. if (*p_data == *p_expected)
  301. {
  302. *p_data = desired;
  303. ret = true;
  304. }
  305. else
  306. {
  307. *p_expected = *p_data;
  308. ret = false;
  309. }
  310. CRITICAL_REGION_EXIT();
  311. return ret;
  312. #endif
  313. }
  314. uint32_t nrf_atomic_u32_fetch_sub_hs(nrf_atomic_u32_t * p_data, uint32_t value)
  315. {
  316. #if NRF_ATOMIC_USE_BUILD_IN
  317. uint32_t expected = *p_data;
  318. uint32_t new_val;
  319. bool success;
  320. do
  321. {
  322. if (expected >= value)
  323. {
  324. new_val = expected - value;
  325. }
  326. else
  327. {
  328. new_val = expected;
  329. }
  330. success = __atomic_compare_exchange(p_data,
  331. &expected,
  332. &new_val,
  333. 1,
  334. __ATOMIC_SEQ_CST,
  335. __ATOMIC_SEQ_CST);
  336. } while(!success);
  337. return expected;
  338. #elif defined(STREX_LDREX_PRESENT)
  339. uint32_t old_val;
  340. uint32_t new_val;
  341. NRF_ATOMIC_OP(sub_hs, old_val, new_val, p_data, value);
  342. UNUSED_PARAMETER(old_val);
  343. UNUSED_PARAMETER(new_val);
  344. return old_val;
  345. #else
  346. CRITICAL_REGION_ENTER();
  347. uint32_t old_val = *p_data;
  348. *p_data -= value;
  349. CRITICAL_REGION_EXIT();
  350. return old_val;
  351. #endif //NRF_ATOMIC_USE_BUILD_IN
  352. }
  353. uint32_t nrf_atomic_u32_sub_hs(nrf_atomic_u32_t * p_data, uint32_t value)
  354. {
  355. #if NRF_ATOMIC_USE_BUILD_IN
  356. uint32_t expected = *p_data;
  357. uint32_t new_val;
  358. bool success;
  359. do
  360. {
  361. if (expected >= value)
  362. {
  363. new_val = expected - value;
  364. }
  365. else
  366. {
  367. new_val = expected;
  368. }
  369. success = __atomic_compare_exchange(p_data,
  370. &expected,
  371. &new_val,
  372. 1,
  373. __ATOMIC_SEQ_CST,
  374. __ATOMIC_SEQ_CST);
  375. } while(!success);
  376. return new_val;
  377. #elif defined(STREX_LDREX_PRESENT)
  378. uint32_t old_val;
  379. uint32_t new_val;
  380. NRF_ATOMIC_OP(sub_hs, old_val, new_val, p_data, value);
  381. UNUSED_PARAMETER(old_val);
  382. UNUSED_PARAMETER(new_val);
  383. return new_val;
  384. #else
  385. CRITICAL_REGION_ENTER();
  386. *p_data -= value;
  387. uint32_t new_value = *p_data;
  388. CRITICAL_REGION_EXIT();
  389. return new_value;
  390. #endif //NRF_ATOMIC_USE_BUILD_IN
  391. }
  392. uint32_t nrf_atomic_flag_set_fetch(nrf_atomic_flag_t * p_data)
  393. {
  394. return nrf_atomic_u32_fetch_or(p_data, 1);
  395. }
  396. uint32_t nrf_atomic_flag_set(nrf_atomic_flag_t * p_data)
  397. {
  398. return nrf_atomic_u32_or(p_data, 1);
  399. }
  400. uint32_t nrf_atomic_flag_clear_fetch(nrf_atomic_flag_t * p_data)
  401. {
  402. return nrf_atomic_u32_fetch_and(p_data, 0);
  403. }
  404. uint32_t nrf_atomic_flag_clear(nrf_atomic_flag_t * p_data)
  405. {
  406. return nrf_atomic_u32_and(p_data, 0);
  407. }