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
The following commit(s) were added to refs/heads/master by this push:
new d5791319c3b mutex: do not return in case of EINTR or ECANCELED
d5791319c3b is described below
commit d5791319c3bc1f2e593ce1d442caedfe99d89c75
Author: xuxingliang <[email protected]>
AuthorDate: Fri Jul 25 15:28:33 2025 +0800
mutex: do not return in case of EINTR or ECANCELED
Many usecase of mutex won't check the return value, thus it's better
to make it handle errno of EINTR and ECANCELED.
nxmutex_ticklock() recomputes the remaining timeout after an
interrupted wait. The internal tree did that with
clock_systime_ticks(), which is not available to user space in the
protected build; use clock() instead, with the tick/usec conversion.
Signed-off-by: xuxingliang <[email protected]>
Signed-off-by: ligd <[email protected]>
---
include/nuttx/mutex.h | 16 ++++++++--
libs/libc/misc/lib_mutex.c | 74 +++++++++++++++++++++++++++++++++++-----------
2 files changed, 69 insertions(+), 21 deletions(-)
diff --git a/include/nuttx/mutex.h b/include/nuttx/mutex.h
index 3e70d1df510..7f2659894aa 100644
--- a/include/nuttx/mutex.h
+++ b/include/nuttx/mutex.h
@@ -500,10 +500,20 @@ static inline_function int nxmutex_lock(FAR mutex_t
*mutex)
{
int ret;
- ret = nxsem_wait(&mutex->sem);
- if (ret >= 0)
+ for (; ; )
{
- nxmutex_add_backtrace(mutex);
+ /* Take the semaphore (perhaps waiting) */
+
+ ret = nxsem_wait(&mutex->sem);
+ if (ret >= 0)
+ {
+ nxmutex_add_backtrace(mutex);
+ break;
+ }
+ else if (ret != -EINTR && ret != -ECANCELED)
+ {
+ break;
+ }
}
return ret;
diff --git a/libs/libc/misc/lib_mutex.c b/libs/libc/misc/lib_mutex.c
index a9420276a59..8f87882c31b 100644
--- a/libs/libc/misc/lib_mutex.c
+++ b/libs/libc/misc/lib_mutex.c
@@ -140,22 +140,52 @@ uint8_t nxmutex_is_hold(FAR mutex_t *mutex)
int nxmutex_ticklock(FAR mutex_t *mutex, uint32_t delay)
{
+ uint64_t end;
int ret;
- /* Wait until we get the lock or until the timeout expires */
+ /* If delay is zero, then this function is equivalent to sem_trywait() */
- if (delay)
- {
- ret = nxsem_tickwait(&mutex->sem, delay);
- }
- else
+ if (delay == 0)
{
ret = nxsem_trywait(&mutex->sem);
+ if (ret >= 0)
+ {
+ nxmutex_add_backtrace(mutex);
+ }
+
+ return ret;
}
- if (ret >= 0)
+ /* Wait until we get the lock or until the timeout expires.
+ * clock() is used because clock_systime_ticks() is not available
+ * to user space in the protected build.
+ */
+
+ end = (uint64_t)clock() + TICK2USEC((uint64_t)delay);
+
+ for (; ; )
{
- nxmutex_add_backtrace(mutex);
+ ret = nxsem_tickwait(&mutex->sem, delay);
+ if (ret >= 0)
+ {
+ nxmutex_add_backtrace(mutex);
+ break;
+ }
+ else if (ret != -EINTR && ret != -ECANCELED)
+ {
+ break;
+ }
+
+ {
+ int64_t remaining = (int64_t)(end - (uint64_t)clock());
+
+ if (remaining < 0)
+ {
+ remaining = 0;
+ }
+
+ delay = (uint32_t)(remaining / USEC_PER_TICK);
+ }
}
return ret;
@@ -193,18 +223,26 @@ int nxmutex_clocklock(FAR mutex_t *mutex, clockid_t
clockid,
/* Wait until we get the lock or until the timeout expires */
- if (abstime)
- {
- ret = nxsem_clockwait(&mutex->sem, clockid, abstime);
- }
- else
+ for (; ; )
{
- ret = nxsem_wait(&mutex->sem);
- }
+ if (abstime)
+ {
+ ret = nxsem_clockwait(&mutex->sem, clockid, abstime);
+ }
+ else
+ {
+ ret = nxsem_wait(&mutex->sem);
+ }
- if (ret >= 0)
- {
- nxmutex_add_backtrace(mutex);
+ if (ret >= 0)
+ {
+ nxmutex_add_backtrace(mutex);
+ break;
+ }
+ else if (ret != -EINTR && ret != -ECANCELED)
+ {
+ break;
+ }
}
return ret;