Skip to content

Commit

Permalink
pthread_once: use rmutex replace sched_[un]lock
Browse files Browse the repository at this point in the history
sched_[un]lock can not prohibit pre-emption in smp

Signed-off-by: hujun5 <[email protected]>
  • Loading branch information
hujun260 committed Sep 20, 2023
1 parent 6b070b2 commit 21b08d2
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions libs/libc/pthread/pthread_once.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <errno.h>
#include <stdbool.h>
#include <pthread.h>
#include <sched.h>
#include <nuttx/mutex.h>
#include <debug.h>

/****************************************************************************
Expand Down Expand Up @@ -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))
{
Expand All @@ -73,23 +75,23 @@ 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)
{
*once_control = true;

/* Call the init_routine with pre-emption enabled. */

sched_unlock();
init_routine();
nxrmutex_unlock(&g_lock);
return OK;
}

/* The init_routine has already been called.
* Restore pre-emption and return.
*/

sched_unlock();
nxrmutex_unlock(&g_lock);
return OK;
}

0 comments on commit 21b08d2

Please sign in to comment.