Skip to content

Commit

Permalink
pthread_once: g_lock may lead deadlock
Browse files Browse the repository at this point in the history
For programs with the dependencies logic in pthread_once callback , using global locks may cause deadlock:

task A
pthread_once()
|
|-> nxrmutex_lock(&g_lock);
 -> init_routine(); // callback to wait task B
                                                  task B
                                                  pthread_once()
                                                  |
                                                   ->nxrmutex_lock(&g_lock); // Deadlock
                                                   ->init_routine(); // hold resource to wake up task A

Signed-off-by: hujun5 <[email protected]>
  • Loading branch information
hujun260 committed Sep 23, 2023
1 parent 2bce0f4 commit 6d51443
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
13 changes: 10 additions & 3 deletions include/pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@

/* Used to initialize a pthread_once_t */

#define PTHREAD_ONCE_INIT (false)
#define PTHREAD_ONCE_INIT {false, PTHREAD_MUTEX_INITIALIZER}

/* This is returned by pthread_barrier_wait. It must not match any errno
* in errno.h
Expand Down Expand Up @@ -371,8 +371,14 @@ typedef struct pthread_barrier_s pthread_barrier_t;
# define __PTHREAD_BARRIER_T_DEFINED 1
#endif

struct pthread_once_s
{
bool done;
pthread_mutex_t mutex;
};

#ifndef __PTHREAD_ONCE_T_DEFINED
typedef bool pthread_once_t;
typedef struct pthread_once_s pthread_once_t;
# define __PTHREAD_ONCE_T_DEFINED 1
#endif

Expand Down Expand Up @@ -839,7 +845,8 @@ typedef FAR struct pthread_spinlock_s pthread_spinlock_t;
#endif /* CONFIG_PTHREAD_SPINLOCKS */

#ifndef __PTHREAD_ONCE_T_DEFINED
typedef bool pthread_once_t;
struct pthread_once_s;
typedef struct pthread_once_s pthread_once_t;
# define __PTHREAD_ONCE_T_DEFINED 1
#endif

Expand Down
12 changes: 4 additions & 8 deletions libs/libc/pthread/pthread_once.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@
*
****************************************************************************/

static rmutex_t g_lock = NXRMUTEX_INITIALIZER;

int pthread_once(FAR pthread_once_t *once_control,
CODE void (*init_routine)(void))
{
Expand All @@ -75,23 +73,21 @@ int pthread_once(FAR pthread_once_t *once_control,

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

nxrmutex_lock(&g_lock);
pthread_mutex_lock(&once_control->mutex);

if (!*once_control)
if (!once_control->done)
{
*once_control = true;
once_control->done = true;

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

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

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

nxrmutex_unlock(&g_lock);
pthread_mutex_unlock(&once_control->mutex);
return OK;
}

0 comments on commit 6d51443

Please sign in to comment.