pthread_cond_broadcast use wait_count for judement

This commit fixes the comment from https://github.com/apache/nuttx/pull/14581

Signed-off-by: hujun5 <hujun5@xiaomi.com>
This commit is contained in:
hujun5 2024-11-14 19:19:31 +08:00 committed by Xiang Xiao
parent d2a7e454bb
commit ea20ae588a
6 changed files with 25 additions and 28 deletions

View file

@ -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

View file

@ -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);

View file

@ -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 */

View file

@ -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 */

View file

@ -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);
}
}

View file

@ -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);