stdatomic.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 CMP_EXCHANGE(n, type) bool __atomic_compare_exchange_ ## n (type* mem, type* expect, type desired, int success, int failure) \
  35. { \
  36. bool ret = false; \
  37. unsigned state = _ATOMIC_ENTER_CRITICAL(); \
  38. if (*mem == *expect) { \
  39. ret = true; \
  40. *mem = desired; \
  41. } else { \
  42. *expect = *mem; \
  43. } \
  44. _ATOMIC_EXIT_CRITICAL(state); \
  45. return ret; \
  46. }
  47. #define FETCH_ADD(n, type) type __atomic_fetch_add_ ## n (type* ptr, type value, int memorder) \
  48. { \
  49. unsigned state = _ATOMIC_ENTER_CRITICAL(); \
  50. type ret = *ptr; \
  51. *ptr = *ptr + value; \
  52. _ATOMIC_EXIT_CRITICAL(state); \
  53. return ret; \
  54. }
  55. #define FETCH_SUB(n, type) type __atomic_fetch_sub_ ## 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_AND(n, type) type __atomic_fetch_and_ ## 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_OR(n, type) type __atomic_fetch_or_ ## 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_XOR(n, type) type __atomic_fetch_xor_ ## 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. #ifndef XCHAL_HAVE_S32C1I
  88. #error "XCHAL_HAVE_S32C1I not defined, include correct header!"
  89. #endif
  90. //this piece of code should only be compiled if the cpu doesn't support atomic compare and swap (s32c1i)
  91. #if XCHAL_HAVE_S32C1I == 0
  92. #pragma GCC diagnostic ignored "-Wbuiltin-declaration-mismatch"
  93. CMP_EXCHANGE(1, uint8_t)
  94. CMP_EXCHANGE(2, uint16_t)
  95. CMP_EXCHANGE(4, uint32_t)
  96. CMP_EXCHANGE(8, uint64_t)
  97. FETCH_ADD(1, uint8_t)
  98. FETCH_ADD(2, uint16_t)
  99. FETCH_ADD(4, uint32_t)
  100. FETCH_ADD(8, uint64_t)
  101. FETCH_SUB(1, uint8_t)
  102. FETCH_SUB(2, uint16_t)
  103. FETCH_SUB(4, uint32_t)
  104. FETCH_SUB(8, uint64_t)
  105. FETCH_AND(1, uint8_t)
  106. FETCH_AND(2, uint16_t)
  107. FETCH_AND(4, uint32_t)
  108. FETCH_AND(8, uint64_t)
  109. FETCH_OR(1, uint8_t)
  110. FETCH_OR(2, uint16_t)
  111. FETCH_OR(4, uint32_t)
  112. FETCH_OR(8, uint64_t)
  113. FETCH_XOR(1, uint8_t)
  114. FETCH_XOR(2, uint16_t)
  115. FETCH_XOR(4, uint32_t)
  116. FETCH_XOR(8, uint64_t)
  117. #endif