This is an automated email from the ASF dual-hosted git repository. GUIDINGLI pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit b25f6f8a866c4f161319c108ef71f625b6b4bf53 Author: zhangyu117 <[email protected]> AuthorDate: Wed Aug 19 22:30:24 2026 +0800 libc/machine: realize atomic based on hwspinlock Implement atomic_lock/atomic_unlock using hwspinlock when CONFIG_LIBC_ATOMIC_HWSPINLOCK is selected, and using up_irq_save/ up_irq_restore when CONFIG_LIBC_ATOMIC_IRQ is selected. Rename arch_atomic_irq.c to arch_atomic.c. The 64-bit atomic operations use spinlock (spin_lock_irqsave) regardless of the selected backend, ensuring multi-core safety. Signed-off-by: zhangyu117 <[email protected]> --- arch/arm/Kconfig | 2 +- libs/libc/machine/CMakeLists.txt | 4 ++-- libs/libc/machine/Kconfig | 16 +++++++++++----- libs/libc/machine/Make.defs | 4 ++-- .../libc/machine/{arch_atomic_irq.c => arch_atomic.c} | 19 ++++++++++++++++++- 5 files changed, 34 insertions(+), 11 deletions(-) diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig index efc38de6baa..c105efb1204 100644 --- a/arch/arm/Kconfig +++ b/arch/arm/Kconfig @@ -1076,7 +1076,7 @@ config ARCH_ARMV6M bool default n select ARCH_HAVE_CPUINFO - select LIBC_ATOMIC_IRQ + select LIBC_ATOMIC_IRQ if !LIBC_ATOMIC_HWSPINLOCK config ARCH_CORTEXM0 bool diff --git a/libs/libc/machine/CMakeLists.txt b/libs/libc/machine/CMakeLists.txt index 148039dec6c..35bbf1cbaff 100644 --- a/libs/libc/machine/CMakeLists.txt +++ b/libs/libc/machine/CMakeLists.txt @@ -22,8 +22,8 @@ add_subdirectory(${CONFIG_ARCH}) -if(CONFIG_LIBC_ATOMIC_IRQ) - target_sources(c PRIVATE arch_atomic_irq.c) +if(CONFIG_LIBC_ATOMIC_IRQ OR CONFIG_LIBC_ATOMIC_HWSPINLOCK) + target_sources(c PRIVATE arch_atomic.c) endif() target_sources(c PRIVATE arch_atomic64.c) diff --git a/libs/libc/machine/Kconfig b/libs/libc/machine/Kconfig index e07b9790c3b..87a349f6258 100644 --- a/libs/libc/machine/Kconfig +++ b/libs/libc/machine/Kconfig @@ -9,21 +9,27 @@ menu "Architecture-Specific Support" -config LIBC_ATOMIC_IRQ +config LIBC_ATOMIC_ARCH bool default n ---help--- - atomic function by irq disable/enable + arch_atomic by arch instruction -config LIBC_ATOMIC_ARCH +config LIBC_ATOMIC_HWSPINLOCK bool default n ---help--- - arch_atomic by arch instruction + arch_atomic by chip hwspinlock + +config LIBC_ATOMIC_IRQ + bool + default n + ---help--- + atomic function by irq disable/enable config LIBC_ATOMIC_TOOLCHAIN bool - default y if !LIBC_ATOMIC_IRQ && !LIBC_ATOMIC_ARCH + default y if !LIBC_ATOMIC_ARCH && !LIBC_ATOMIC_HWSPINLOCK && !LIBC_ATOMIC_IRQ default n ---help--- atomic function from toolchain diff --git a/libs/libc/machine/Make.defs b/libs/libc/machine/Make.defs index 5821f401ef7..e8e03b6543f 100644 --- a/libs/libc/machine/Make.defs +++ b/libs/libc/machine/Make.defs @@ -20,8 +20,8 @@ # ############################################################################ -ifeq ($(CONFIG_LIBC_ATOMIC_IRQ),y) - CSRCS += arch_atomic_irq.c +ifneq ($(filter y,$(CONFIG_LIBC_ATOMIC_IRQ)$(CONFIG_LIBC_ATOMIC_HWSPINLOCK)),) + CSRCS += arch_atomic.c endif CSRCS += arch_atomic64.c diff --git a/libs/libc/machine/arch_atomic_irq.c b/libs/libc/machine/arch_atomic.c similarity index 97% rename from libs/libc/machine/arch_atomic_irq.c rename to libs/libc/machine/arch_atomic.c index 483d7b1fed3..802514c3bd4 100644 --- a/libs/libc/machine/arch_atomic_irq.c +++ b/libs/libc/machine/arch_atomic.c @@ -1,5 +1,5 @@ /**************************************************************************** - * libs/libc/machine/arch_atomic_irq.c + * libs/libc/machine/arch_atomic.c * * SPDX-License-Identifier: Apache-2.0 * @@ -30,6 +30,9 @@ #include <stdint.h> #include <nuttx/irq.h> #include <nuttx/macro.h> +#if defined(CONFIG_LIBC_ATOMIC_HWSPINLOCK) +# include <nuttx/hwspinlock/hwspinlock.h> +#endif #include "arch_atomic.h" @@ -37,6 +40,19 @@ * Private Functions ****************************************************************************/ +#if defined(CONFIG_LIBC_ATOMIC_HWSPINLOCK) +extern struct hwspinlock_dev_s g_atomic_hwspinlock; + +static inline irqstate_t atomic_lock(void) +{ + return hwspin_lock_irqsave(&g_atomic_hwspinlock); +} + +static inline void atomic_unlock(irqstate_t flags) +{ + hwspin_unlock_restore(&g_atomic_hwspinlock, flags); +} +#elif defined(CONFIG_LIBC_ATOMIC_IRQ) static inline irqstate_t atomic_lock(void) { return up_irq_save(); @@ -46,6 +62,7 @@ static inline void atomic_unlock(irqstate_t flags) { up_irq_restore(flags); } +#endif /**************************************************************************** * Public Functions
