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 aaa61aaf34ad2e40e93d489bd306c066eb56561d
Author: Marco Casaroli <[email protected]>
AuthorDate: Wed Nov 11 13:33:14 2026 +0100

    libs/libc/semaphore: Skip the fast path in user space on IRQ atomics.
    
    The fast paths of nxsem_wait(), nxsem_trywait() and nxsem_post() read the
    semaphore and change it with a compare and exchange.  In the user space of
    a PROTECTED or KERNEL build with CONFIG_LIBC_ATOMIC_IRQ, each of these
    atomics is two system calls (sched_lock() and sched_unlock()): one fast
    path try is four system calls.  The slow path does the whole operation in
    the kernel with one system call.
    
    Add NXSEM_FASTPATH: it is false in user space with these atomics.  There,
    the operations go to the slow path at once.  Flat builds, the kernel and
    the other atomics do not change.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 include/nuttx/semaphore.h         | 12 ++++++++++++
 libs/libc/semaphore/sem_post.c    |  2 +-
 libs/libc/semaphore/sem_trywait.c |  2 +-
 libs/libc/semaphore/sem_wait.c    |  2 +-
 4 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/include/nuttx/semaphore.h b/include/nuttx/semaphore.h
index 5756b6564b9..2b767caa2b8 100644
--- a/include/nuttx/semaphore.h
+++ b/include/nuttx/semaphore.h
@@ -103,6 +103,18 @@
 #  define NXSEM_RESTORE_UNINTERRUPTIBLE(tcb, flags) do { } while (0)
 #endif
 
+/* In user space, the atomics of LIBC_ATOMIC_IRQ are system calls.  There,
+ * the semaphore operations skip the fast path: the slow path is one system
+ * call.
+ */
+
+#if !defined(CONFIG_BUILD_FLAT) && !defined(__KERNEL__) && \
+    defined(CONFIG_LIBC_ATOMIC_IRQ)
+#  define NXSEM_FASTPATH      false
+#else
+#  define NXSEM_FASTPATH      true
+#endif
+
 /* Mutex related helper macros */
 
 #define NXSEM_MBLOCKING_BIT   ((uint32_t)0x80000000)
diff --git a/libs/libc/semaphore/sem_post.c b/libs/libc/semaphore/sem_post.c
index 363b46fc9d0..62ac19e8b0b 100644
--- a/libs/libc/semaphore/sem_post.c
+++ b/libs/libc/semaphore/sem_post.c
@@ -119,7 +119,7 @@ int sem_post(FAR sem_t *sem)
 
 int nxsem_post(FAR sem_t *sem)
 {
-  bool fastpath = true;
+  bool fastpath = NXSEM_FASTPATH;
   bool mutex;
 
   DEBUGASSERT(sem != NULL);
diff --git a/libs/libc/semaphore/sem_trywait.c 
b/libs/libc/semaphore/sem_trywait.c
index a7a8bd39894..9471a666b59 100644
--- a/libs/libc/semaphore/sem_trywait.c
+++ b/libs/libc/semaphore/sem_trywait.c
@@ -107,7 +107,7 @@ int sem_trywait(FAR sem_t *sem)
 
 int nxsem_trywait(FAR sem_t *sem)
 {
-  bool fastpath = true;
+  bool fastpath = NXSEM_FASTPATH;
   bool mutex;
 
   DEBUGASSERT(sem != NULL);
diff --git a/libs/libc/semaphore/sem_wait.c b/libs/libc/semaphore/sem_wait.c
index 9c6225ea9d6..84711f382ab 100644
--- a/libs/libc/semaphore/sem_wait.c
+++ b/libs/libc/semaphore/sem_wait.c
@@ -135,7 +135,7 @@ errout_with_cancelpt:
 
 int nxsem_wait(FAR sem_t *sem)
 {
-  bool fastpath = true;
+  bool fastpath = NXSEM_FASTPATH;
   bool mutex;
 
   DEBUGASSERT(sem != NULL);

Reply via email to