Skip to content

Commit

Permalink
pthread_once: use [enter|leave]_critical_section 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 <hujun5@xiaomi.com>
  • Loading branch information
hujun260 committed Sep 18, 2023
1 parent b98e78b commit c9beee3
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/irq.h>
#include <debug.h>

/****************************************************************************
Expand Down Expand Up @@ -64,6 +64,8 @@
int pthread_once(FAR pthread_once_t *once_control,
CODE void (*init_routine)(void))
{
irqstate_t flags;

/* Sanity checks */

if (once_control == NULL || init_routine == NULL)
Expand All @@ -73,15 +75,15 @@ int pthread_once(FAR pthread_once_t *once_control,

/* Prohibit pre-emption while we test and set the once_control. */

sched_lock();
flags = enter_critical_section();

if (!*once_control)
{
*once_control = true;

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

sched_unlock();
leave_critical_section(flags);
init_routine();
return OK;
}
Expand All @@ -90,6 +92,6 @@ int pthread_once(FAR pthread_once_t *once_control,
* Restore pre-emption and return.
*/

sched_unlock();
leave_critical_section(flags);
return OK;
}

0 comments on commit c9beee3

Please sign in to comment.