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

xiaoxiang 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 62660a692dc sched/task: move lock to mhead scope
62660a692dc is described below

commit 62660a692dc1629e309e3e7e7c00110ed0bc07a5
Author: chao an <[email protected]>
AuthorDate: Sun Sep 28 16:53:41 2025 +0800

    sched/task: move lock to mhead scope
    
    1. rename mutex_lock to mhead_lock to declare purpose of lock
    2. move lock to mhead scope
    
    Signed-off-by: chao an <[email protected]>
---
 include/nuttx/sched.h          |  5 +----
 sched/init/nx_start.c          |  4 ++--
 sched/pthread/pthread_create.c |  2 +-
 sched/pthread/pthread_mutex.c  | 12 ++++++------
 sched/task/task_fork.c         |  4 ++--
 sched/task/task_init.c         |  4 ++--
 6 files changed, 14 insertions(+), 17 deletions(-)

diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h
index 91b75eec63f..53dbe6eae67 100644
--- a/include/nuttx/sched.h
+++ b/include/nuttx/sched.h
@@ -671,6 +671,7 @@ struct tcb_s
 
 #if !defined(CONFIG_DISABLE_PTHREAD) && !defined(CONFIG_PTHREAD_MUTEX_UNSAFE)
   FAR struct pthread_mutex_s *mhead;     /* List of mutexes held by thread  */
+  spinlock_t mhead_lock;
 #endif
 
   /* CPU load monitoring support ********************************************/
@@ -726,10 +727,6 @@ struct tcb_s
   size_t level_deepest;
   size_t level;
 #endif
-
-#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
-  spinlock_t mutex_lock;
-#endif
 };
 
 /* struct task_tcb_s ********************************************************/
diff --git a/sched/init/nx_start.c b/sched/init/nx_start.c
index 374de7501e0..3f4a4e1820c 100644
--- a/sched/init/nx_start.c
+++ b/sched/init/nx_start.c
@@ -451,8 +451,8 @@ static void idle_group_initialize(void)
 
       nxtask_joininit(tcb);
 
-#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
-      spin_lock_init(&tcb->mutex_lock);
+#if !defined(CONFIG_DISABLE_PTHREAD) && !defined(CONFIG_PTHREAD_MUTEX_UNSAFE)
+      spin_lock_init(&tcb->mhead_lock);
 #endif
 
 #ifdef CONFIG_SMP
diff --git a/sched/pthread/pthread_create.c b/sched/pthread/pthread_create.c
index 142b6df5286..108e1651028 100644
--- a/sched/pthread/pthread_create.c
+++ b/sched/pthread/pthread_create.c
@@ -223,7 +223,7 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR 
pthread_t *thread,
   nxtask_joininit(&ptcb->cmn);
 
 #ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
-  spin_lock_init(&ptcb->cmn.mutex_lock);
+  spin_lock_init(&ptcb->cmn.mhead_lock);
 #endif
 
   /* Bind the parent's group to the new TCB (we have not yet joined the
diff --git a/sched/pthread/pthread_mutex.c b/sched/pthread/pthread_mutex.c
index 34ecada6be0..3bd5e62c9c9 100644
--- a/sched/pthread/pthread_mutex.c
+++ b/sched/pthread/pthread_mutex.c
@@ -65,10 +65,10 @@ static void pthread_mutex_add(FAR struct pthread_mutex_s 
*mutex)
 
   /* Add the mutex to the list of mutexes held by this pthread */
 
-  flags        = spin_lock_irqsave(&rtcb->mutex_lock);
+  flags        = spin_lock_irqsave(&rtcb->mhead_lock);
   mutex->flink = rtcb->mhead;
   rtcb->mhead  = mutex;
-  spin_unlock_irqrestore(&rtcb->mutex_lock, flags);
+  spin_unlock_irqrestore(&rtcb->mhead_lock, flags);
 }
 
 /****************************************************************************
@@ -92,7 +92,7 @@ static void pthread_mutex_remove(FAR struct pthread_mutex_s 
*mutex)
   FAR struct pthread_mutex_s *prev;
   irqstate_t flags;
 
-  flags = spin_lock_irqsave(&rtcb->mutex_lock);
+  flags = spin_lock_irqsave(&rtcb->mhead_lock);
 
   /* Remove the mutex from the list of mutexes held by this task */
 
@@ -118,7 +118,7 @@ static void pthread_mutex_remove(FAR struct pthread_mutex_s 
*mutex)
     }
 
   mutex->flink = NULL;
-  spin_unlock_irqrestore(&rtcb->mutex_lock, flags);
+  spin_unlock_irqrestore(&rtcb->mhead_lock, flags);
 }
 
 /****************************************************************************
@@ -372,7 +372,7 @@ void pthread_mutex_inconsistent(FAR struct tcb_s *tcb)
 
   DEBUGASSERT(tcb != NULL);
 
-  flags = spin_lock_irqsave(&tcb->mutex_lock);
+  flags = spin_lock_irqsave(&tcb->mhead_lock);
 
   /* Remove and process each mutex held by this task */
 
@@ -390,5 +390,5 @@ void pthread_mutex_inconsistent(FAR struct tcb_s *tcb)
       mutex_unlock(&mutex->mutex);
     }
 
-  spin_unlock_irqrestore(&tcb->mutex_lock, flags);
+  spin_unlock_irqrestore(&tcb->mhead_lock, flags);
 }
diff --git a/sched/task/task_fork.c b/sched/task/task_fork.c
index 39dd755f40c..0176e57dfb5 100644
--- a/sched/task/task_fork.c
+++ b/sched/task/task_fork.c
@@ -164,8 +164,8 @@ FAR struct task_tcb_s *nxtask_setup_fork(start_t retaddr)
 
   nxtask_joininit(&child->cmn);
 
-#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
-  spin_lock_init(&child->cmn.mutex_lock);
+#if !defined(CONFIG_DISABLE_PTHREAD) && !defined(CONFIG_PTHREAD_MUTEX_UNSAFE)
+  spin_lock_init(&child->cmn.mhead_lock);
 #endif
 
   /* Allocate a new task group with the same privileges as the parent */
diff --git a/sched/task/task_init.c b/sched/task/task_init.c
index 0fa96f99727..a2f49431390 100644
--- a/sched/task/task_init.c
+++ b/sched/task/task_init.c
@@ -126,8 +126,8 @@ int nxtask_init(FAR struct task_tcb_s *tcb, const char 
*name, int priority,
   nxtask_joininit(&tcb->cmn);
 #endif
 
-#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
-  spin_lock_init(&tcb->cmn.mutex_lock);
+#if !defined(CONFIG_DISABLE_PTHREAD) && !defined(CONFIG_PTHREAD_MUTEX_UNSAFE)
+  spin_lock_init(&tcb->cmn.mhead_lock);
 #endif
 
   /* Duplicate the parent tasks environment */

Reply via email to