pthread_cond_wait: Use atomic_t to protect the waiter count

The load/compare and RMW to wait_count need protection. Using atomic
operations should resolve both issues.

NOTE:
The assumption that the user will call pthread_cond_signal /
pthread_cond_broadcast with the mutex given to pthread_cond_wait held is
simply not true. It MAY hold it, but it is not forced. Thus, using the
user space lock for protecting the wait counter as well is not valid!

The pthread_cond_signal() or pthread_cond_broadcast() functions may be called by a thread whether or not it currently owns the mutex that threads calling pthread_cond_wait() or pthread_cond_timedwait() have associated with the condition variable during their waits; however, if predictable scheduling behaviour is required, then that mutex is locked by the thread calling pthread_cond_signal() or pthread_cond_broadcast().

[1] https://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_cond_signal.html
This commit is contained in:
Ville Juven
2025-01-20 23:55:26 +08:00
committed by Xiang Xiao
parent 1a9241ca1c
commit bb85ad849e
6 changed files with 33 additions and 25 deletions
+1 -1
View File
@@ -269,7 +269,7 @@ struct pthread_cond_s
{
sem_t sem;
clockid_t clockid;
uint16_t wait_count;
int wait_count;
};
#ifndef __PTHREAD_COND_T_DEFINED