From e01e5f500889caf2f8b2e38b0bedc7fec2fdb6c5 Mon Sep 17 00:00:00 2001 From: zhuyanlin Date: Sun, 29 Aug 2021 22:51:47 +0800 Subject: [PATCH] libc:machine: add atomic_load/store/exchange API Add atomic_load/store/exchange API Change-Id: I6196b077d1e76cd1e0506ee66e2885e541525eb1 --- libs/libc/machine/arch_atomic.c | 113 ++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/libs/libc/machine/arch_atomic.c b/libs/libc/machine/arch_atomic.c index 16e7495494..9d2004d347 100644 --- a/libs/libc/machine/arch_atomic.c +++ b/libs/libc/machine/arch_atomic.c @@ -32,6 +32,47 @@ * Pre-processor Definitions ****************************************************************************/ +#define STORE(n, type) \ + \ + void __atomic_store_ ## n (type *ptr, \ + type value, \ + int memorder) \ + { \ + irqstate_t irqstate = spin_lock_irqsave(NULL); \ + \ + *ptr = value; \ + \ + spin_unlock_irqrestore(NULL, irqstate); \ + } + +#define LOAD(n, type) \ + \ + type __atomic_load_ ## n (type *ptr, \ + int memorder) \ + { \ + irqstate_t irqstate = spin_lock_irqsave(NULL); \ + \ + type ret = *ptr; \ + \ + spin_unlock_irqrestore(NULL, irqstate); \ + return ret; \ + } + +#define EXCHANGE(n, type) \ + \ + type __atomic_exchange_ ## n (type *ptr, \ + type value, \ + int memorder) \ + { \ + irqstate_t irqstate = spin_lock_irqsave(NULL); \ + \ + type ret = *ptr; \ + *ptr = value; \ + \ + spin_unlock_irqrestore(NULL, irqstate); \ + return ret; \ + } + #define CMP_EXCHANGE(n, type) \ \ bool __atomic_compare_exchange_ ## n (type *mem, \ @@ -138,6 +179,78 @@ * Public Functions ****************************************************************************/ +/**************************************************************************** + * Name: __atomic_store_1 + ****************************************************************************/ + +STORE(1, uint8_t) + +/**************************************************************************** + * Name: __atomic_store_2 + ****************************************************************************/ + +STORE(2, uint16_t) + +/**************************************************************************** + * Name: __atomic_store_4 + ****************************************************************************/ + +STORE(4, uint32_t) + +/**************************************************************************** + * Name: __atomic_store_8 + ****************************************************************************/ + +STORE(8, uint64_t) + +/**************************************************************************** + * Name: __atomic_load_1 + ****************************************************************************/ + +LOAD(1, uint8_t) + +/**************************************************************************** + * Name: __atomic_load__2 + ****************************************************************************/ + +LOAD(2, uint16_t) + +/**************************************************************************** + * Name: __atomic_load__4 + ****************************************************************************/ + +LOAD(4, uint32_t) + +/**************************************************************************** + * Name: __atomic_load__8 + ****************************************************************************/ + +LOAD(8, uint64_t) + +/**************************************************************************** + * Name: __atomic_exchange_1 + ****************************************************************************/ + +EXCHANGE(1, uint8_t) + +/**************************************************************************** + * Name: __atomic_exchange__2 + ****************************************************************************/ + +EXCHANGE(2, uint16_t) + +/**************************************************************************** + * Name: __atomic_exchange__4 + ****************************************************************************/ + +EXCHANGE(4, uint32_t) + +/**************************************************************************** + * Name: __atomic_exchange__8 + ****************************************************************************/ + +EXCHANGE(8, uint64_t) + /**************************************************************************** * Name: __atomic_compare_exchange_1 ****************************************************************************/