diff --git a/include/pthread.h b/include/pthread.h index 75764d96060..a7badf27463 100644 --- a/include/pthread.h +++ b/include/pthread.h @@ -269,7 +269,7 @@ struct pthread_cond_s { sem_t sem; clockid_t clockid; - volatile int16_t lock_count; + uint16_t wait_count; }; #ifndef __PTHREAD_COND_T_DEFINED diff --git a/libs/libc/pthread/pthread_condinit.c b/libs/libc/pthread/pthread_condinit.c index 6c88b19905a..c174f98bb6e 100644 --- a/libs/libc/pthread/pthread_condinit.c +++ b/libs/libc/pthread/pthread_condinit.c @@ -74,7 +74,7 @@ int pthread_cond_init(FAR pthread_cond_t *cond, else { cond->clockid = attr ? attr->clockid : CLOCK_REALTIME; - cond->lock_count = 0; + cond->wait_count = 0; } sinfo("Returning %d\n", ret); diff --git a/sched/pthread/pthread_condbroadcast.c b/sched/pthread/pthread_condbroadcast.c index 599bd976c23..c6d72bd7e81 100644 --- a/sched/pthread/pthread_condbroadcast.c +++ b/sched/pthread/pthread_condbroadcast.c @@ -42,7 +42,10 @@ * * 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 * @@ -56,7 +59,6 @@ int pthread_cond_broadcast(FAR pthread_cond_t *cond) { int ret = OK; - int sval; sinfo("cond=%p\n", cond); @@ -73,31 +75,22 @@ int pthread_cond_broadcast(FAR pthread_cond_t *cond) sched_lock(); - /* Get the current value of the semaphore */ + /* Loop until all of the waiting threads have been restarted. */ - if (nxsem_get_value(&cond->sem, &sval) != OK) + while (cond->wait_count > 0) { - ret = EINVAL; - } - else - { - /* Loop until all of the waiting threads have been restarted. */ + /* If the value is less than zero (meaning that one or more + * thread is waiting), then post the condition semaphore. + * Only the highest priority waiting thread will get to execute + */ - while (sval < 0) - { - /* If the value is less than zero (meaning that one or more - * thread is waiting), then post the condition semaphore. - * Only the highest priority waiting thread will get to execute - */ + ret = -nxsem_post(&cond->sem); - ret = -nxsem_post(&cond->sem); + /* Increment the semaphore count (as was done by the + * above post). + */ - /* Increment the semaphore count (as was done by the - * above post). - */ - - sval++; - } + cond->wait_count--; } /* Now we can let the restarted threads run */ diff --git a/sched/pthread/pthread_condclockwait.c b/sched/pthread/pthread_condclockwait.c index 36019913361..77a62101f9a 100644 --- a/sched/pthread/pthread_condclockwait.c +++ b/sched/pthread/pthread_condclockwait.c @@ -113,7 +113,7 @@ int pthread_cond_clockwait(FAR pthread_cond_t *cond, sinfo("Give up mutex...\n"); - cond->lock_count--; + cond->wait_count++; /* Give up the mutex */ diff --git a/sched/pthread/pthread_condsignal.c b/sched/pthread/pthread_condsignal.c index 1cdfa684480..4e9055f773d 100644 --- a/sched/pthread/pthread_condsignal.c +++ b/sched/pthread/pthread_condsignal.c @@ -41,6 +41,10 @@ * * 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 @@ -64,10 +68,10 @@ int pthread_cond_signal(FAR pthread_cond_t *cond) } else { - if (cond->lock_count < 0) + if (cond->wait_count > 0) { sinfo("Signalling...\n"); - cond->lock_count++; + cond->wait_count--; ret = -nxsem_post(&cond->sem); } } diff --git a/sched/pthread/pthread_condwait.c b/sched/pthread/pthread_condwait.c index 43942de6fb3..ca8f05511e4 100644 --- a/sched/pthread/pthread_condwait.c +++ b/sched/pthread/pthread_condwait.c @@ -88,7 +88,7 @@ int pthread_cond_wait(FAR pthread_cond_t *cond, FAR pthread_mutex_t *mutex) sinfo("Give up mutex / take cond\n"); - cond->lock_count--; + cond->wait_count++; ret = pthread_mutex_breaklock(mutex, &nlocks); status = -nxsem_wait_uninterruptible(&cond->sem);