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 a0acce1a13f libs/libc/misc: Restart the count of a recursive mutex 
taken over.
a0acce1a13f is described below

commit a0acce1a13febc8ba7eb9ab2a2043368eb9a6ef0
Author: dengwenqi <[email protected]>
AuthorDate: Tue Nov 24 17:56:56 2026 +0800

    libs/libc/misc: Restart the count of a recursive mutex taken over.
    
    A thread that exits while it holds a recursive mutex, for example a
    thread that is cancelled or calls pthread_exit() with a stream locked by
    flockfile() or inside printf(), leaves its thread id and the count of
    its locks in the mutex.  The next nxrmutex_lock() takes the mutex over
    in nxsem_wait_slow(), which treats a holder that no longer exists as no
    holder, but it adds its own lock to the old count.  Its last unlock then
    leaves the count above zero and never releases the mutex, so every other
    thread blocks on it, or fails to try-lock it, forever.
    
    Start the count again when the mutex itself is acquired.  Only the
    blocking variants can take the mutex over: nxrmutex_trylock() fails
    while the id of the old holder is set.
    
    Signed-off-by: dengwenqi <[email protected]>
    Signed-off-by: ganjing <[email protected]>
---
 libs/libc/misc/lib_mutex.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/libs/libc/misc/lib_mutex.c b/libs/libc/misc/lib_mutex.c
index 53ad6cb225a..a9420276a59 100644
--- a/libs/libc/misc/lib_mutex.c
+++ b/libs/libc/misc/lib_mutex.c
@@ -275,6 +275,15 @@ int nxrmutex_lock(FAR rmutex_t *rmutex)
   if (!nxrmutex_is_hold(rmutex))
     {
       ret = nxmutex_lock(&rmutex->mutex);
+      if (ret >= 0)
+        {
+          /* The mutex can be taken over from a holder that exited without
+           * unlocking it.  That holder's count is still here and would
+           * keep the mutex locked after our last unlock, so start again.
+           */
+
+          rmutex->count = 0;
+        }
     }
 
   if (ret >= 0)
@@ -360,6 +369,10 @@ int nxrmutex_ticklock(FAR rmutex_t *rmutex, uint32_t delay)
   if (!nxrmutex_is_hold(rmutex))
     {
       ret = nxmutex_ticklock(&rmutex->mutex, delay);
+      if (ret >= 0)
+        {
+          rmutex->count = 0; /* See nxrmutex_lock() */
+        }
     }
 
   if (ret >= 0)
@@ -404,6 +417,10 @@ int nxrmutex_clocklock(FAR rmutex_t *rmutex, clockid_t 
clockid,
   if (!nxrmutex_is_hold(rmutex))
     {
       ret = nxmutex_clocklock(&rmutex->mutex, clockid, abstime);
+      if (ret >= 0)
+        {
+          rmutex->count = 0; /* See nxrmutex_lock() */
+        }
     }
 
   if (ret >= 0)
@@ -447,6 +464,10 @@ int nxrmutex_timedlock(FAR rmutex_t *rmutex, unsigned int 
timeout)
   if (!nxrmutex_is_hold(rmutex))
     {
       ret = nxmutex_timedlock(&rmutex->mutex, timeout);
+      if (ret >= 0)
+        {
+          rmutex->count = 0; /* See nxrmutex_lock() */
+        }
     }
 
   if (ret >= 0)

Reply via email to