From 08c4df809a31135988c04f80307bb5cdc60225f7 Mon Sep 17 00:00:00 2001 From: hujun5 Date: Fri, 15 Sep 2023 08:59:44 +0800 Subject: [PATCH] pthread_once: use mutex replace sched_[un]lock sched_[un]lock can not prohibit pre-emption in smp Signed-off-by: hujun5 --- 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 05d3b995a0d91..56afb8fe71357 100644 --- a/libs/libc/pthread/pthread_once.c +++ b/libs/libc/pthread/pthread_once.c @@ -28,7 +28,7 @@ #include #include #include -#include +#include #include /**************************************************************************** @@ -61,6 +61,8 @@ * ****************************************************************************/ +static mutex_t g_lock = NXMUTEX_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(); + nxmutex_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(); + nxmutex_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(); + nxmutex_unlock(&g_lock); return OK; }