stdatomic.c 2.1 KB

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