diff --git a/include/pthread.h b/include/pthread.h index 191bbf4e8bf15..39972b91912a4 100644 --- a/include/pthread.h +++ b/include/pthread.h @@ -269,7 +269,8 @@ struct pthread_cond_s { sem_t sem; clockid_t clockid; - uint16_t wait_count; + unsigned int wait_count; + mutex_t mutex; }; #ifndef __PTHREAD_COND_T_DEFINED diff --git a/libs/libc/pthread/pthread_condinit.c b/libs/libc/pthread/pthread_condinit.c index c174f98bb6e5e..fdcb283d555bb 100644 --- a/libs/libc/pthread/pthread_condinit.c +++ b/libs/libc/pthread/pthread_condinit.c @@ -31,6 +31,8 @@ #include #include +#include + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -75,6 +77,7 @@ int pthread_cond_init(FAR pthread_cond_t *cond, { cond->clockid = attr ? attr->clockid : CLOCK_REALTIME; cond->wait_count = 0; + nxmutex_init(&cond->mutex); } sinfo("Returning %d\n", ret); diff --git a/sched/pthread/pthread_condbroadcast.c b/sched/pthread/pthread_condbroadcast.c index 581666548710f..2c584484e6c9a 100644 --- a/sched/pthread/pthread_condbroadcast.c +++ b/sched/pthread/pthread_condbroadcast.c @@ -31,6 +31,8 @@ #include #include +#include + #include "pthread/pthread.h" /**************************************************************************** @@ -42,10 +44,7 @@ * * Description: * A thread broadcast on a condition variable. - * pthread_cond_broadcast shall unblock all threads currently blocked on a - * specified condition variable cond. We need own the mutex that threads - * calling pthread_cond_wait or pthread_cond_timedwait have associated - * with the condition variable during their wait. + * * Input Parameters: * None * @@ -68,9 +67,11 @@ int pthread_cond_broadcast(FAR pthread_cond_t *cond) } else { + nxmutex_lock(&cond->mutex); + /* Loop until all of the waiting threads have been restarted. */ - while (cond->wait_count > 0) + while (atomic_load(&cond->wait_count) > 0) { /* If the value is less than zero (meaning that one or more * thread is waiting), then post the condition semaphore. @@ -83,8 +84,10 @@ int pthread_cond_broadcast(FAR pthread_cond_t *cond) * above post). */ - cond->wait_count--; + atomic_fetch_sub(&cond->wait_count, 1); } + + nxmutex_unlock(&cond->mutex); } sinfo("Returning %d\n", ret); diff --git a/sched/pthread/pthread_condclockwait.c b/sched/pthread/pthread_condclockwait.c index 77a62101f9adc..34e2038af961e 100644 --- a/sched/pthread/pthread_condclockwait.c +++ b/sched/pthread/pthread_condclockwait.c @@ -36,6 +36,7 @@ #include #include +#include #include #include #include @@ -113,7 +114,7 @@ int pthread_cond_clockwait(FAR pthread_cond_t *cond, sinfo("Give up mutex...\n"); - cond->wait_count++; + atomic_fetch_add(&cond->wait_count, 1); /* Give up the mutex */ diff --git a/sched/pthread/pthread_condsignal.c b/sched/pthread/pthread_condsignal.c index 4e9055f773d97..ff3a067a5415c 100644 --- a/sched/pthread/pthread_condsignal.c +++ b/sched/pthread/pthread_condsignal.c @@ -30,6 +30,8 @@ #include #include +#include + #include "pthread/pthread.h" /**************************************************************************** @@ -41,10 +43,6 @@ * * Description: * A thread can signal on a condition variable. - * pthread_cond_signal shall unblock a thread currently blocked on a - * specified condition variable cond. We need own the mutex that threads - * calling pthread_cond_wait or pthread_cond_timedwait have associated - * with the condition variable during their wait. * * Input Parameters: * None @@ -68,12 +66,16 @@ int pthread_cond_signal(FAR pthread_cond_t *cond) } else { - if (cond->wait_count > 0) + nxmutex_lock(&cond->mutex); + + if (atomic_load(&cond->wait_count) > 0) { sinfo("Signalling...\n"); - cond->wait_count--; + atomic_fetch_sub(&cond->wait_count, 1); ret = -nxsem_post(&cond->sem); } + + nxmutex_unlock(&cond->mutex); } sinfo("Returning %d\n", ret); diff --git a/sched/pthread/pthread_condwait.c b/sched/pthread/pthread_condwait.c index ca8f05511e407..8cf53fd3cd215 100644 --- a/sched/pthread/pthread_condwait.c +++ b/sched/pthread/pthread_condwait.c @@ -32,6 +32,7 @@ #include #include +#include #include #include "pthread/pthread.h" @@ -88,7 +89,7 @@ int pthread_cond_wait(FAR pthread_cond_t *cond, FAR pthread_mutex_t *mutex) sinfo("Give up mutex / take cond\n"); - cond->wait_count++; + atomic_fetch_add(&cond->wait_count, 1); ret = pthread_mutex_breaklock(mutex, &nlocks); status = -nxsem_wait_uninterruptible(&cond->sem);