stdatomic.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 "freertos/FreeRTOS.h"
  17. #include "freertos/portmacro.h"
  18. #include "xtensa/config/core-isa.h"
  19. #define CMP_EXCHANGE(n, type) bool __atomic_compare_exchange_ ## n (type* mem, type* expect, type desired, int success, int failure) \
  20. { \
  21. bool ret = false; \
  22. unsigned state = portENTER_CRITICAL_NESTED(); \
  23. if (*mem == *expect) { \
  24. ret = true; \
  25. *mem = desired; \
  26. } else { \
  27. *expect = *mem; \
  28. } \
  29. portEXIT_CRITICAL_NESTED(state); \
  30. return ret; \
  31. }
  32. #define FETCH_ADD(n, type) type __atomic_fetch_add_ ## n (type* ptr, type value, int memorder) \
  33. { \
  34. unsigned state = portENTER_CRITICAL_NESTED(); \
  35. type ret = *ptr; \
  36. *ptr = *ptr + value; \
  37. portEXIT_CRITICAL_NESTED(state); \
  38. return ret; \
  39. }
  40. #define FETCH_SUB(n, type) type __atomic_fetch_sub_ ## n (type* ptr, type value, int memorder) \
  41. { \
  42. unsigned state = portENTER_CRITICAL_NESTED(); \
  43. type ret = *ptr; \
  44. *ptr = *ptr - value; \
  45. portEXIT_CRITICAL_NESTED(state); \
  46. return ret; \
  47. }
  48. //this piece of code should only be compiled if the cpu doesn't support atomic compare and swap (s32c1i)
  49. #if XCHAL_HAVE_S32C1I == 0
  50. #pragma GCC diagnostic ignored "-Wbuiltin-declaration-mismatch"
  51. CMP_EXCHANGE(1, uint8_t)
  52. CMP_EXCHANGE(2, uint16_t)
  53. CMP_EXCHANGE(4, uint32_t)
  54. CMP_EXCHANGE(8, uint64_t)
  55. FETCH_ADD(1, uint8_t)
  56. FETCH_ADD(2, uint16_t)
  57. FETCH_ADD(4, uint32_t)
  58. FETCH_ADD(8, uint64_t)
  59. FETCH_SUB(1, uint8_t)
  60. FETCH_SUB(2, uint16_t)
  61. FETCH_SUB(4, uint32_t)
  62. FETCH_SUB(8, uint64_t)
  63. #endif