This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 1fe17c22c6278697e9e2442e1b62ac82d2e900ca
Author: Marco Casaroli <[email protected]>
AuthorDate: Wed Nov 11 13:32:53 2026 +0100

    libs/libc/machine: Do not disable the interrupts in user space atomics.
    
    With CONFIG_LIBC_ATOMIC_IRQ, the atomic functions of arch_atomic.c
    disable the interrupts with up_irq_save().  The 8-byte atomics of
    arch_atomic64.c disable them with spin_lock_irqsave() with every atomics
    option.  The compiler calls these functions on the 32-bit targets with
    no 8-byte atomic instructions (RISC-V, Xtensa, ARM M-profile).
    
    The user space libc of a PROTECTED or KERNEL build has these functions
    too: the semaphores, the condition variables, the temporary buffers and
    the C11 and C++ atomics of the programs use them.  User code cannot
    disable the interrupts.  RISC-V traps on the mstatus or sstatus access
    and Xtensa traps on rsil.  ARM M-profile ignores cpsid and the BASEPRI
    write in unprivileged mode, so the operation is not atomic.
    
    In user space, lock the scheduler instead.  As with up_irq_save(), no
    other thread runs on this CPU during the operation.  The 8-byte atomics
    keep their spinlock for the other CPUs of an SMP build, with a try loop:
    spin_lock() can call kernel functions.  Flat builds and the kernel do
    not change.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 libs/libc/machine/arch_atomic.c   | 16 ++++++++++++++++
 libs/libc/machine/arch_atomic64.c | 26 ++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)

diff --git a/libs/libc/machine/arch_atomic.c b/libs/libc/machine/arch_atomic.c
index 802514c3bd4..e53a857c52f 100644
--- a/libs/libc/machine/arch_atomic.c
+++ b/libs/libc/machine/arch_atomic.c
@@ -26,6 +26,7 @@
 
 #include <nuttx/config.h>
 
+#include <sched.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <nuttx/irq.h>
@@ -52,6 +53,21 @@ static inline void atomic_unlock(irqstate_t flags)
 {
   hwspin_unlock_restore(&g_atomic_hwspinlock, flags);
 }
+#elif !defined(CONFIG_BUILD_FLAT) && !defined(__KERNEL__)
+/* User code cannot disable the interrupts.  Lock the scheduler instead:
+ * as with up_irq_save(), no other thread runs on this CPU meanwhile.
+ */
+
+static inline irqstate_t atomic_lock(void)
+{
+  sched_lock();
+  return 0;
+}
+
+static inline void atomic_unlock(irqstate_t flags)
+{
+  sched_unlock();
+}
 #elif defined(CONFIG_LIBC_ATOMIC_IRQ)
 static inline irqstate_t atomic_lock(void)
 {
diff --git a/libs/libc/machine/arch_atomic64.c 
b/libs/libc/machine/arch_atomic64.c
index 41cdeafeaf0..0b90fd8cb9f 100644
--- a/libs/libc/machine/arch_atomic64.c
+++ b/libs/libc/machine/arch_atomic64.c
@@ -26,6 +26,8 @@
 
 #include <nuttx/config.h>
 
+#include <sched.h>
+
 #include <nuttx/spinlock.h>
 
 #include "arch_atomic.h"
@@ -40,6 +42,29 @@ static spinlock_t g_atomic_lock = SP_UNLOCKED;
  * Private Functions
  ****************************************************************************/
 
+#if !defined(CONFIG_BUILD_FLAT) && !defined(__KERNEL__)
+/* User code cannot disable the interrupts.  Lock the scheduler instead:
+ * as with up_irq_save(), no other thread runs on this CPU meanwhile.  The
+ * spinlock keeps out the other CPUs of an SMP build.  spin_lock() can call
+ * kernel functions, so try the lock in a loop.
+ */
+
+static inline irqstate_t atomic_lock(void)
+{
+  sched_lock();
+  while (!spin_trylock_notrace(&g_atomic_lock))
+    {
+    }
+
+  return 0;
+}
+
+static inline void atomic_unlock(irqstate_t flags)
+{
+  spin_unlock_notrace(&g_atomic_lock);
+  sched_unlock();
+}
+#else
 static inline irqstate_t atomic_lock(void)
 {
   return spin_lock_irqsave(&g_atomic_lock);
@@ -49,6 +74,7 @@ static inline void atomic_unlock(irqstate_t flags)
 {
   spin_unlock_irqrestore(&g_atomic_lock, flags);
 }
+#endif
 
 /****************************************************************************
  * Public Functions

Reply via email to