stdatomic.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //replacement for gcc built-in functions
  15. #include "sdkconfig.h"
  16. #include <stdbool.h>
  17. #include "xtensa/config/core-isa.h"
  18. #include "xtensa/xtruntime.h"
  19. //reserved to measure atomic operation time
  20. #define atomic_benchmark_intr_disable()
  21. #define atomic_benchmark_intr_restore(STATE)
  22. // This allows nested interrupts disabling and restoring via local registers or stack.
  23. // They can be called from interrupts too.
  24. // WARNING: Only applies to current CPU.
  25. #define _ATOMIC_ENTER_CRITICAL(void) ({ \
  26. unsigned state = XTOS_SET_INTLEVEL(XCHAL_EXCM_LEVEL); \
  27. atomic_benchmark_intr_disable(); \
  28. state; \
  29. })
  30. #define _ATOMIC_EXIT_CRITICAL(state) do { \
  31. atomic_benchmark_intr_restore(state); \
  32. XTOS_RESTORE_JUST_INTLEVEL(state); \
  33. } while (0)
  34. #define ATOMIC_EXCHANGE(n, type) type __atomic_exchange_ ## n (type* mem, type val, int memorder) \
  35. { \
  36. unsigned state = _ATOMIC_ENTER_CRITICAL(); \
  37. type ret = *mem; \
  38. *mem = val; \
  39. _ATOMIC_EXIT_CRITICAL(state); \
  40. return ret; \
  41. }
  42. #define CMP_EXCHANGE(n, type) bool __atomic_compare_exchange_ ## n (type* mem, type* expect, type desired, int success, int failure) \
  43. { \
  44. bool ret = false; \
  45. unsigned state = _ATOMIC_ENTER_CRITICAL(); \
  46. if (*mem == *expect) { \
  47. ret = true; \
  48. *mem = desired; \
  49. } else { \
  50. *expect = *mem; \
  51. } \
  52. _ATOMIC_EXIT_CRITICAL(state); \
  53. return ret; \
  54. }
  55. #define FETCH_ADD(n, type) type __atomic_fetch_add_ ## n (type* ptr, type value, int memorder) \
  56. { \
  57. unsigned state = _ATOMIC_ENTER_CRITICAL(); \
  58. type ret = *ptr; \
  59. *ptr = *ptr + value; \
  60. _ATOMIC_EXIT_CRITICAL(state); \
  61. return ret; \
  62. }
  63. #define FETCH_SUB(n, type) type __atomic_fetch_sub_ ## n (type* ptr, type value, int memorder) \
  64. { \
  65. unsigned state = _ATOMIC_ENTER_CRITICAL(); \
  66. type ret = *ptr; \
  67. *ptr = *ptr - value; \
  68. _ATOMIC_EXIT_CRITICAL(state); \
  69. return ret; \
  70. }
  71. #define FETCH_AND(n, type) type __atomic_fetch_and_ ## n (type* ptr, type value, int memorder) \
  72. { \
  73. unsigned state = _ATOMIC_ENTER_CRITICAL(); \
  74. type ret = *ptr; \
  75. *ptr = *ptr & value; \
  76. _ATOMIC_EXIT_CRITICAL(state); \
  77. return ret; \
  78. }
  79. #define FETCH_OR(n, type) type __atomic_fetch_or_ ## n (type* ptr, type value, int memorder) \
  80. { \
  81. unsigned state = _ATOMIC_ENTER_CRITICAL(); \
  82. type ret = *ptr; \
  83. *ptr = *ptr | value; \
  84. _ATOMIC_EXIT_CRITICAL(state); \
  85. return ret; \
  86. }
  87. #define FETCH_XOR(n, type) type __atomic_fetch_xor_ ## n (type* ptr, type value, int memorder) \
  88. { \
  89. unsigned state = _ATOMIC_ENTER_CRITICAL(); \
  90. type ret = *ptr; \
  91. *ptr = *ptr ^ value; \
  92. _ATOMIC_EXIT_CRITICAL(state); \
  93. return ret; \
  94. }
  95. #define SYNC_FETCH_OP(op, n, type) type __sync_fetch_and_ ## op ##_ ## n (type* ptr, type value, ...) \
  96. { \
  97. return __atomic_fetch_ ## op ##_ ## n (ptr, value, __ATOMIC_SEQ_CST); \
  98. }
  99. #define SYNC_BOOL_CMP_EXCHANGE(n, type) bool __sync_bool_compare_and_swap_ ## n (type *ptr, type oldval, type newval, ...) \
  100. { \
  101. bool ret = false; \
  102. unsigned state = _ATOMIC_ENTER_CRITICAL(); \
  103. if (*ptr == oldval) { \
  104. *ptr = newval; \
  105. ret = true; \
  106. } \
  107. _ATOMIC_EXIT_CRITICAL(state); \
  108. return ret; \
  109. }
  110. #define SYNC_VAL_CMP_EXCHANGE(n, type) type __sync_val_compare_and_swap_ ## n (type *ptr, type oldval, type newval, ...) \
  111. { \
  112. unsigned state = _ATOMIC_ENTER_CRITICAL(); \
  113. type ret = *ptr; \
  114. if (*ptr == oldval) { \
  115. *ptr = newval; \
  116. } \
  117. _ATOMIC_EXIT_CRITICAL(state); \
  118. return ret; \
  119. }
  120. #ifndef XCHAL_HAVE_S32C1I
  121. #error "XCHAL_HAVE_S32C1I not defined, include correct header!"
  122. #endif
  123. //this piece of code should only be compiled if the cpu doesn't support atomic compare and swap (s32c1i)
  124. #if XCHAL_HAVE_S32C1I == 0
  125. #pragma GCC diagnostic ignored "-Wbuiltin-declaration-mismatch"
  126. ATOMIC_EXCHANGE(1, uint8_t)
  127. ATOMIC_EXCHANGE(2, uint16_t)
  128. ATOMIC_EXCHANGE(4, uint32_t)
  129. ATOMIC_EXCHANGE(8, uint64_t)
  130. CMP_EXCHANGE(1, uint8_t)
  131. CMP_EXCHANGE(2, uint16_t)
  132. CMP_EXCHANGE(4, uint32_t)
  133. CMP_EXCHANGE(8, uint64_t)
  134. FETCH_ADD(1, uint8_t)
  135. FETCH_ADD(2, uint16_t)
  136. FETCH_ADD(4, uint32_t)
  137. FETCH_ADD(8, uint64_t)
  138. FETCH_SUB(1, uint8_t)
  139. FETCH_SUB(2, uint16_t)
  140. FETCH_SUB(4, uint32_t)
  141. FETCH_SUB(8, uint64_t)
  142. FETCH_AND(1, uint8_t)
  143. FETCH_AND(2, uint16_t)
  144. FETCH_AND(4, uint32_t)
  145. FETCH_AND(8, uint64_t)
  146. FETCH_OR(1, uint8_t)
  147. FETCH_OR(2, uint16_t)
  148. FETCH_OR(4, uint32_t)
  149. FETCH_OR(8, uint64_t)
  150. FETCH_XOR(1, uint8_t)
  151. FETCH_XOR(2, uint16_t)
  152. FETCH_XOR(4, uint32_t)
  153. FETCH_XOR(8, uint64_t)
  154. SYNC_FETCH_OP(add, 1, uint8_t)
  155. SYNC_FETCH_OP(add, 2, uint16_t)
  156. SYNC_FETCH_OP(add, 4, uint32_t)
  157. SYNC_FETCH_OP(add, 8, uint64_t)
  158. SYNC_FETCH_OP(sub, 1, uint8_t)
  159. SYNC_FETCH_OP(sub, 2, uint16_t)
  160. SYNC_FETCH_OP(sub, 4, uint32_t)
  161. SYNC_FETCH_OP(sub, 8, uint64_t)
  162. SYNC_FETCH_OP(and, 1, uint8_t)
  163. SYNC_FETCH_OP(and, 2, uint16_t)
  164. SYNC_FETCH_OP(and, 4, uint32_t)
  165. SYNC_FETCH_OP(and, 8, uint64_t)
  166. SYNC_FETCH_OP(or, 1, uint8_t)
  167. SYNC_FETCH_OP(or, 2, uint16_t)
  168. SYNC_FETCH_OP(or, 4, uint32_t)
  169. SYNC_FETCH_OP(or, 8, uint64_t)
  170. SYNC_FETCH_OP(xor, 1, uint8_t)
  171. SYNC_FETCH_OP(xor, 2, uint16_t)
  172. SYNC_FETCH_OP(xor, 4, uint32_t)
  173. SYNC_FETCH_OP(xor, 8, uint64_t)
  174. SYNC_BOOL_CMP_EXCHANGE(1, uint8_t)
  175. SYNC_BOOL_CMP_EXCHANGE(2, uint16_t)
  176. SYNC_BOOL_CMP_EXCHANGE(4, uint32_t)
  177. SYNC_BOOL_CMP_EXCHANGE(8, uint64_t)
  178. SYNC_VAL_CMP_EXCHANGE(1, uint8_t)
  179. SYNC_VAL_CMP_EXCHANGE(2, uint16_t)
  180. SYNC_VAL_CMP_EXCHANGE(4, uint32_t)
  181. SYNC_VAL_CMP_EXCHANGE(8, uint64_t)
  182. #endif