From 4c7366045ce3381384d63f14e153a320a2e7e363 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Sun, 22 Jun 2025 03:21:23 +0800 Subject: [PATCH] libc/semaphore: Go the fast path even arch doesn't support atomic since the simulated atomic operation is still fast than the slow path Signed-off-by: Xiang Xiao --- libs/libc/semaphore/sem_post.c | 21 +++++---------------- libs/libc/semaphore/sem_trywait.c | 22 +++++----------------- libs/libc/semaphore/sem_wait.c | 21 +++++---------------- 3 files changed, 15 insertions(+), 49 deletions(-) diff --git a/libs/libc/semaphore/sem_post.c b/libs/libc/semaphore/sem_post.c index d34b6fea566..363b46fc9d0 100644 --- a/libs/libc/semaphore/sem_post.c +++ b/libs/libc/semaphore/sem_post.c @@ -119,37 +119,30 @@ int sem_post(FAR sem_t *sem) int nxsem_post(FAR sem_t *sem) { - bool mutex; bool fastpath = true; + bool mutex; DEBUGASSERT(sem != NULL); - /* We don't do atomic fast path in case of LIBC_ARCH_ATOMIC because that - * uses spinlocks, which can't be called from userspace. Also in the kernel - * taking the slow path directly is faster than locking first in here - */ - -#ifndef CONFIG_LIBC_ARCH_ATOMIC - mutex = NXSEM_IS_MUTEX(sem); /* Disable fast path if priority protection is enabled on the semaphore */ -# ifdef CONFIG_PRIORITY_PROTECT +#ifdef CONFIG_PRIORITY_PROTECT if ((sem->flags & SEM_PRIO_MASK) == SEM_PRIO_PROTECT) { fastpath = false; } -# endif +#endif /* Disable fast path on a counting semaphore with priority inheritance */ -# ifdef CONFIG_PRIORITY_INHERITANCE +#ifdef CONFIG_PRIORITY_INHERITANCE if (!mutex && (sem->flags & SEM_PRIO_MASK) != SEM_PRIO_NONE) { fastpath = false; } -# endif +#endif while (fastpath) { @@ -181,10 +174,6 @@ int nxsem_post(FAR sem_t *sem) return OK; } } -#else - UNUSED(mutex); - UNUSED(fastpath); -#endif return nxsem_post_slow(sem); } diff --git a/libs/libc/semaphore/sem_trywait.c b/libs/libc/semaphore/sem_trywait.c index 8f1686ea023..8a62ac9bd88 100644 --- a/libs/libc/semaphore/sem_trywait.c +++ b/libs/libc/semaphore/sem_trywait.c @@ -107,8 +107,8 @@ int sem_trywait(FAR sem_t *sem) int nxsem_trywait(FAR sem_t *sem) { - bool mutex; bool fastpath = true; + bool mutex; DEBUGASSERT(sem != NULL); @@ -119,32 +119,25 @@ int nxsem_trywait(FAR sem_t *sem) up_interrupt_context()); #endif - /* We don't do atomic fast path in case of LIBC_ARCH_ATOMIC because that - * uses spinlocks, which can't be called from userspace. Also in the kernel - * taking the slow path directly is faster than locking first in here - */ - -#ifndef CONFIG_LIBC_ARCH_ATOMIC - mutex = NXSEM_IS_MUTEX(sem); /* Disable fast path if priority protection is enabled on the semaphore */ -# ifdef CONFIG_PRIORITY_PROTECT +#ifdef CONFIG_PRIORITY_PROTECT if ((sem->flags & SEM_PRIO_MASK) == SEM_PRIO_PROTECT) { fastpath = false; } -# endif +#endif /* Disable fast path on a counting semaphore with priority inheritance */ -# ifdef CONFIG_PRIORITY_INHERITANCE +#ifdef CONFIG_PRIORITY_INHERITANCE if (!mutex && (sem->flags & SEM_PRIO_MASK) != SEM_PRIO_NONE) { fastpath = false; } -# endif +#endif while (fastpath) { @@ -177,10 +170,5 @@ int nxsem_trywait(FAR sem_t *sem) } } -#else - UNUSED(mutex); - UNUSED(fastpath); -#endif - return nxsem_trywait_slow(sem); } diff --git a/libs/libc/semaphore/sem_wait.c b/libs/libc/semaphore/sem_wait.c index dca20eb4bd6..ca39cf6587a 100644 --- a/libs/libc/semaphore/sem_wait.c +++ b/libs/libc/semaphore/sem_wait.c @@ -135,8 +135,8 @@ errout_with_cancelpt: int nxsem_wait(FAR sem_t *sem) { - bool mutex; bool fastpath = true; + bool mutex; DEBUGASSERT(sem != NULL); @@ -147,32 +147,25 @@ int nxsem_wait(FAR sem_t *sem) up_interrupt_context()); #endif - /* We don't do atomic fast path in case of LIBC_ARCH_ATOMIC because that - * uses spinlocks, which can't be called from userspace. Also in the kernel - * taking the slow path directly is faster than locking first in here - */ - -#ifndef CONFIG_LIBC_ARCH_ATOMIC - mutex = NXSEM_IS_MUTEX(sem); /* Disable fast path if priority protection is enabled on the semaphore */ -# ifdef CONFIG_PRIORITY_PROTECT +#ifdef CONFIG_PRIORITY_PROTECT if ((sem->flags & SEM_PRIO_MASK) == SEM_PRIO_PROTECT) { fastpath = false; } -# endif +#endif /* Disable fast path on a counting semaphore with priority inheritance */ -# ifdef CONFIG_PRIORITY_INHERITANCE +#ifdef CONFIG_PRIORITY_INHERITANCE if (!mutex && (sem->flags & SEM_PRIO_MASK) != SEM_PRIO_NONE) { fastpath = false; } -# endif +#endif while (fastpath) { @@ -204,10 +197,6 @@ int nxsem_wait(FAR sem_t *sem) return OK; } } -#else - UNUSED(mutex); - UNUSED(fastpath); -#endif return nxsem_wait_slow(sem); }