From e9b683d0b338b4c92190702d151e5180d0b10ca0 Mon Sep 17 00:00:00 2001 From: Shell Date: Tue, 6 Aug 2024 11:20:38 +0800 Subject: [PATCH] feat: set RT_USING_STDC_ATOMIC to first priority RT_USING_STDC_ATOMIC is a user selected option on current config system. While the RT_USING_HW_ATOMIC is a forced option selected by Kconfig under libcpu. And the RT_USING_STDC_ATOMIC will be meaningless if we set RT_USING_HW_ATOMIC to first priority if the arch has hw-atomic. Changes: - set RT_USING_STDC_ATOMIC to first priority on rttypes.h Signed-off-by: Shell --- include/rtatomic.h | 28 ++++++++++++++-------------- include/rttypes.h | 13 +++++++++---- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/include/rtatomic.h b/include/rtatomic.h index b91145e40b..1c8cde5396 100644 --- a/include/rtatomic.h +++ b/include/rtatomic.h @@ -27,20 +27,7 @@ void rt_hw_atomic_flag_clear(volatile rt_atomic_t *ptr); rt_atomic_t rt_hw_atomic_flag_test_and_set(volatile rt_atomic_t *ptr); rt_atomic_t rt_hw_atomic_compare_exchange_strong(volatile rt_atomic_t *ptr, rt_atomic_t *expected, rt_atomic_t desired); -#if defined(RT_USING_HW_ATOMIC) -#define rt_atomic_load(ptr) rt_hw_atomic_load(ptr) -#define rt_atomic_store(ptr, v) rt_hw_atomic_store(ptr, v) -#define rt_atomic_add(ptr, v) rt_hw_atomic_add(ptr, v) -#define rt_atomic_sub(ptr, v) rt_hw_atomic_sub(ptr, v) -#define rt_atomic_and(ptr, v) rt_hw_atomic_and(ptr, v) -#define rt_atomic_or(ptr, v) rt_hw_atomic_or(ptr, v) -#define rt_atomic_xor(ptr, v) rt_hw_atomic_xor(ptr, v) -#define rt_atomic_exchange(ptr, v) rt_hw_atomic_exchange(ptr, v) -#define rt_atomic_flag_clear(ptr) rt_hw_atomic_flag_clear(ptr) -#define rt_atomic_flag_test_and_set(ptr) rt_hw_atomic_flag_test_and_set(ptr) -#define rt_atomic_compare_exchange_strong(ptr, v,des) rt_hw_atomic_compare_exchange_strong(ptr, v ,des) - -#elif defined(RT_USING_STDC_ATOMIC) +#if defined(RT_USING_STDC_ATOMIC) #ifndef __STDC_NO_ATOMICS__ #define rt_atomic_load(ptr) atomic_load(ptr) @@ -58,6 +45,19 @@ rt_atomic_t rt_hw_atomic_compare_exchange_strong(volatile rt_atomic_t *ptr, rt_a #error "The standard library C doesn't support the atomic operation" #endif /* __STDC_NO_ATOMICS__ */ +#elif defined(RT_USING_HW_ATOMIC) +#define rt_atomic_load(ptr) rt_hw_atomic_load(ptr) +#define rt_atomic_store(ptr, v) rt_hw_atomic_store(ptr, v) +#define rt_atomic_add(ptr, v) rt_hw_atomic_add(ptr, v) +#define rt_atomic_sub(ptr, v) rt_hw_atomic_sub(ptr, v) +#define rt_atomic_and(ptr, v) rt_hw_atomic_and(ptr, v) +#define rt_atomic_or(ptr, v) rt_hw_atomic_or(ptr, v) +#define rt_atomic_xor(ptr, v) rt_hw_atomic_xor(ptr, v) +#define rt_atomic_exchange(ptr, v) rt_hw_atomic_exchange(ptr, v) +#define rt_atomic_flag_clear(ptr) rt_hw_atomic_flag_clear(ptr) +#define rt_atomic_flag_test_and_set(ptr) rt_hw_atomic_flag_test_and_set(ptr) +#define rt_atomic_compare_exchange_strong(ptr, v,des) rt_hw_atomic_compare_exchange_strong(ptr, v ,des) + #else #include #define rt_atomic_load(ptr) rt_soft_atomic_load(ptr) diff --git a/include/rttypes.h b/include/rttypes.h index 78d6c63011..0cd12d7614 100644 --- a/include/rttypes.h +++ b/include/rttypes.h @@ -78,14 +78,19 @@ typedef rt_base_t rt_flag_t; /**< Type for flags */ typedef rt_ubase_t rt_dev_t; /**< Type for device */ typedef rt_base_t rt_off_t; /**< Type for offset */ +#if defined(RT_USING_STDC_ATOMIC) && __STDC_VERSION__ < 201112L +#undef RT_USING_STDC_ATOMIC +#warning Not using C11 or beyond! Maybe you should change the -std option on your compiler +#endif + #ifdef __cplusplus typedef rt_base_t rt_atomic_t; #else - #if defined(RT_USING_HW_ATOMIC) - typedef rt_base_t rt_atomic_t; - #elif defined(RT_USING_STDC_ATOMIC) + #if defined(RT_USING_STDC_ATOMIC) #include - typedef atomic_intptr_t rt_atomic_t; + typedef _Atomic(rt_base_t) rt_atomic_t; + #elif defined(RT_USING_HW_ATOMIC) + typedef rt_base_t rt_atomic_t; #else typedef rt_base_t rt_atomic_t; #endif /* RT_USING_STDC_ATOMIC */