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 4268b2e297 pthread_once: use rmutex replace sched_[un]lock
4268b2e297 is described below

commit 4268b2e29700055b02bef4421e8e1058a8aaeac6
Author: hujun5 <[email protected]>
AuthorDate: Fri Sep 15 08:59:44 2023 +0800

    pthread_once: use rmutex replace sched_[un]lock
    
    sched_[un]lock can not prohibit pre-emption in smp
    
    Signed-off-by: hujun5 <[email protected]>
---
 libs/libc/pthread/pthread_once.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/libs/libc/pthread/pthread_once.c b/libs/libc/pthread/pthread_once.c
index 05d3b995a0..2862ddba2f 100644
--- a/libs/libc/pthread/pthread_once.c
+++ b/libs/libc/pthread/pthread_once.c
@@ -28,7 +28,7 @@
 #include <errno.h>
 #include <stdbool.h>
 #include <pthread.h>
-#include <sched.h>
+#include <nuttx/mutex.h>
 #include <debug.h>
 
 /****************************************************************************
@@ -61,6 +61,8 @@
  *
  ****************************************************************************/
 
+static rmutex_t g_lock = NXRMUTEX_INITIALIZER;
+
 int pthread_once(FAR pthread_once_t *once_control,
                  CODE void (*init_routine)(void))
 {
@@ -73,7 +75,7 @@ int pthread_once(FAR pthread_once_t *once_control,
 
   /* Prohibit pre-emption while we test and set the once_control. */
 
-  sched_lock();
+  nxrmutex_lock(&g_lock);
 
   if (!*once_control)
     {
@@ -81,8 +83,8 @@ int pthread_once(FAR pthread_once_t *once_control,
 
       /* Call the init_routine with pre-emption enabled. */
 
-      sched_unlock();
       init_routine();
+      nxrmutex_unlock(&g_lock);
       return OK;
     }
 
@@ -90,6 +92,6 @@ int pthread_once(FAR pthread_once_t *once_control,
    * Restore pre-emption and return.
    */
 
-  sched_unlock();
+  nxrmutex_unlock(&g_lock);
   return OK;
 }

Reply via email to