From aad1f7abde43cd893bb96bba448211e7906b6124 Mon Sep 17 00:00:00 2001 From: Sachin Parekh Date: Thu, 22 Apr 2021 19:19:58 +0530 Subject: [PATCH] stdatomic: Implemented legacy __sync APIs and __atomic_exchange_n These APIs are used when the architecture doesn't have atomic instruction support Closes https://github.com/espressif/esp-idf/issues/6463 --- components/riscv/stdatomic.c | 85 +++++++++++++++++++++++++++++++++++ components/xtensa/stdatomic.c | 77 +++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+) diff --git a/components/riscv/stdatomic.c b/components/riscv/stdatomic.c index 7c0c395db5..9cae8ee518 100644 --- a/components/riscv/stdatomic.c +++ b/components/riscv/stdatomic.c @@ -37,6 +37,15 @@ portEXIT_CRITICAL_NESTED(state); \ } while (0) +#define ATOMIC_EXCHANGE(n, type) type __atomic_exchange_ ## n (type* mem, type val, int memorder) \ +{ \ + unsigned state = _ATOMIC_ENTER_CRITICAL(); \ + type ret = *mem; \ + *mem = val; \ + _ATOMIC_EXIT_CRITICAL(state); \ + return ret; \ +} + #define CMP_EXCHANGE(n, type) bool __atomic_compare_exchange_ ## n (type* mem, type* expect, type desired, int success, int failure) \ { \ bool ret = false; \ @@ -96,8 +105,47 @@ return ret; \ } +#define SYNC_FETCH_OP(op, n, type) type __sync_fetch_and_ ## op ##_ ## n (type* ptr, type value, ...) \ +{ \ + return __atomic_fetch_ ## op ##_ ## n (ptr, value, __ATOMIC_SEQ_CST); \ +} + +#define SYNC_BOOL_CMP_EXCHANGE(n, type) bool __sync_bool_compare_and_swap_ ## n (type *ptr, type oldval, type newval, ...) \ +{ \ + bool ret = false; \ + unsigned state = _ATOMIC_ENTER_CRITICAL(); \ + if (*ptr == oldval) { \ + *ptr = newval; \ + ret = true; \ + } \ + _ATOMIC_EXIT_CRITICAL(state); \ + return ret; \ +} + +#define SYNC_VAL_CMP_EXCHANGE(n, type) type __sync_val_compare_and_swap_ ## n (type *ptr, type oldval, type newval, ...) \ +{ \ + unsigned state = _ATOMIC_ENTER_CRITICAL(); \ + type ret = *ptr; \ + if (*ptr == oldval) { \ + *ptr = newval; \ + } \ + _ATOMIC_EXIT_CRITICAL(state); \ + return ret; \ +} + +#ifndef __riscv_atomic // GCC toolchain will define this pre-processor if "A" extension is supported +#define __riscv_atomic 0 +#endif + +#if (__riscv_atomic == 0) + #pragma GCC diagnostic ignored "-Wbuiltin-declaration-mismatch" +ATOMIC_EXCHANGE(1, uint8_t) +ATOMIC_EXCHANGE(2, uint16_t) +ATOMIC_EXCHANGE(4, uint32_t) +ATOMIC_EXCHANGE(8, uint64_t) + CMP_EXCHANGE(1, uint8_t) CMP_EXCHANGE(2, uint16_t) CMP_EXCHANGE(4, uint32_t) @@ -127,3 +175,40 @@ FETCH_XOR(1, uint8_t) FETCH_XOR(2, uint16_t) FETCH_XOR(4, uint32_t) FETCH_XOR(8, uint64_t) + +SYNC_FETCH_OP(add, 1, uint8_t) +SYNC_FETCH_OP(add, 2, uint16_t) +SYNC_FETCH_OP(add, 4, uint32_t) +SYNC_FETCH_OP(add, 8, uint64_t) + +SYNC_FETCH_OP(sub, 1, uint8_t) +SYNC_FETCH_OP(sub, 2, uint16_t) +SYNC_FETCH_OP(sub, 4, uint32_t) +SYNC_FETCH_OP(sub, 8, uint64_t) + +SYNC_FETCH_OP(and, 1, uint8_t) +SYNC_FETCH_OP(and, 2, uint16_t) +SYNC_FETCH_OP(and, 4, uint32_t) +SYNC_FETCH_OP(and, 8, uint64_t) + +SYNC_FETCH_OP(or, 1, uint8_t) +SYNC_FETCH_OP(or, 2, uint16_t) +SYNC_FETCH_OP(or, 4, uint32_t) +SYNC_FETCH_OP(or, 8, uint64_t) + +SYNC_FETCH_OP(xor, 1, uint8_t) +SYNC_FETCH_OP(xor, 2, uint16_t) +SYNC_FETCH_OP(xor, 4, uint32_t) +SYNC_FETCH_OP(xor, 8, uint64_t) + +SYNC_BOOL_CMP_EXCHANGE(1, uint8_t) +SYNC_BOOL_CMP_EXCHANGE(2, uint16_t) +SYNC_BOOL_CMP_EXCHANGE(4, uint32_t) +SYNC_BOOL_CMP_EXCHANGE(8, uint64_t) + +SYNC_VAL_CMP_EXCHANGE(1, uint8_t) +SYNC_VAL_CMP_EXCHANGE(2, uint16_t) +SYNC_VAL_CMP_EXCHANGE(4, uint32_t) +SYNC_VAL_CMP_EXCHANGE(8, uint64_t) + +#endif diff --git a/components/xtensa/stdatomic.c b/components/xtensa/stdatomic.c index dad6170fb3..4dc425c502 100644 --- a/components/xtensa/stdatomic.c +++ b/components/xtensa/stdatomic.c @@ -37,6 +37,15 @@ XTOS_RESTORE_JUST_INTLEVEL(state); \ } while (0) +#define ATOMIC_EXCHANGE(n, type) type __atomic_exchange_ ## n (type* mem, type val, int memorder) \ +{ \ + unsigned state = _ATOMIC_ENTER_CRITICAL(); \ + type ret = *mem; \ + *mem = val; \ + _ATOMIC_EXIT_CRITICAL(state); \ + return ret; \ +} + #define CMP_EXCHANGE(n, type) bool __atomic_compare_exchange_ ## n (type* mem, type* expect, type desired, int success, int failure) \ { \ bool ret = false; \ @@ -96,6 +105,34 @@ return ret; \ } +#define SYNC_FETCH_OP(op, n, type) type __sync_fetch_and_ ## op ##_ ## n (type* ptr, type value, ...) \ +{ \ + return __atomic_fetch_ ## op ##_ ## n (ptr, value, __ATOMIC_SEQ_CST); \ +} + +#define SYNC_BOOL_CMP_EXCHANGE(n, type) bool __sync_bool_compare_and_swap_ ## n (type *ptr, type oldval, type newval, ...) \ +{ \ + bool ret = false; \ + unsigned state = _ATOMIC_ENTER_CRITICAL(); \ + if (*ptr == oldval) { \ + *ptr = newval; \ + ret = true; \ + } \ + _ATOMIC_EXIT_CRITICAL(state); \ + return ret; \ +} + +#define SYNC_VAL_CMP_EXCHANGE(n, type) type __sync_val_compare_and_swap_ ## n (type *ptr, type oldval, type newval, ...) \ +{ \ + unsigned state = _ATOMIC_ENTER_CRITICAL(); \ + type ret = *ptr; \ + if (*ptr == oldval) { \ + *ptr = newval; \ + } \ + _ATOMIC_EXIT_CRITICAL(state); \ + return ret; \ +} + #ifndef XCHAL_HAVE_S32C1I #error "XCHAL_HAVE_S32C1I not defined, include correct header!" #endif @@ -105,6 +142,11 @@ #pragma GCC diagnostic ignored "-Wbuiltin-declaration-mismatch" +ATOMIC_EXCHANGE(1, uint8_t) +ATOMIC_EXCHANGE(2, uint16_t) +ATOMIC_EXCHANGE(4, uint32_t) +ATOMIC_EXCHANGE(8, uint64_t) + CMP_EXCHANGE(1, uint8_t) CMP_EXCHANGE(2, uint16_t) CMP_EXCHANGE(4, uint32_t) @@ -135,4 +177,39 @@ FETCH_XOR(2, uint16_t) FETCH_XOR(4, uint32_t) FETCH_XOR(8, uint64_t) +SYNC_FETCH_OP(add, 1, uint8_t) +SYNC_FETCH_OP(add, 2, uint16_t) +SYNC_FETCH_OP(add, 4, uint32_t) +SYNC_FETCH_OP(add, 8, uint64_t) + +SYNC_FETCH_OP(sub, 1, uint8_t) +SYNC_FETCH_OP(sub, 2, uint16_t) +SYNC_FETCH_OP(sub, 4, uint32_t) +SYNC_FETCH_OP(sub, 8, uint64_t) + +SYNC_FETCH_OP(and, 1, uint8_t) +SYNC_FETCH_OP(and, 2, uint16_t) +SYNC_FETCH_OP(and, 4, uint32_t) +SYNC_FETCH_OP(and, 8, uint64_t) + +SYNC_FETCH_OP(or, 1, uint8_t) +SYNC_FETCH_OP(or, 2, uint16_t) +SYNC_FETCH_OP(or, 4, uint32_t) +SYNC_FETCH_OP(or, 8, uint64_t) + +SYNC_FETCH_OP(xor, 1, uint8_t) +SYNC_FETCH_OP(xor, 2, uint16_t) +SYNC_FETCH_OP(xor, 4, uint32_t) +SYNC_FETCH_OP(xor, 8, uint64_t) + +SYNC_BOOL_CMP_EXCHANGE(1, uint8_t) +SYNC_BOOL_CMP_EXCHANGE(2, uint16_t) +SYNC_BOOL_CMP_EXCHANGE(4, uint32_t) +SYNC_BOOL_CMP_EXCHANGE(8, uint64_t) + +SYNC_VAL_CMP_EXCHANGE(1, uint8_t) +SYNC_VAL_CMP_EXCHANGE(2, uint16_t) +SYNC_VAL_CMP_EXCHANGE(4, uint32_t) +SYNC_VAL_CMP_EXCHANGE(8, uint64_t) + #endif