mirror of
https://github.com/fluencelabs/musl
synced 2025-06-25 20:51:53 +00:00
a reader unlocking the lock need only wake one waiter (necessarily a writer, but a writer unlocking the lock must wake all waiters (necessarily readers). if it only wakes one, the remainder can remain blocked indefinitely, or at least until the first reader unlocks (in which case the whole lock becomes serialized and behaves as a mutex rather than a read lock).
19 lines
374 B
C
19 lines
374 B
C
#include "pthread_impl.h"
|
|
|
|
int pthread_rwlock_unlock(pthread_rwlock_t *rw)
|
|
{
|
|
int val, cnt, waiters, new;
|
|
|
|
do {
|
|
val = rw->_rw_lock;
|
|
cnt = val & 0x7fffffff;
|
|
waiters = rw->_rw_waiters;
|
|
new = (cnt == 0x7fffffff || cnt == 1) ? 0 : val-1;
|
|
} while (a_cas(&rw->_rw_lock, val, new) != val);
|
|
|
|
if (!new && (waiters || val<0))
|
|
__wake(&rw->_rw_lock, cnt, 0);
|
|
|
|
return 0;
|
|
}
|