mirror of
https://github.com/fluencelabs/musl
synced 2025-06-29 22:51:55 +00:00
if there is already a waiter for a lock, spinning on the lock is essentially an attempt to steal it from whichever waiter would obtain it via any priority rules in place, and is therefore undesirable. in the current implementation, there is always an inherent race window at unlock during which a newly-arriving thread may steal the lock from the existing waiters, but we should aim to keep this window minimal rather than enlarging it.
18 lines
439 B
C
18 lines
439 B
C
#include "pthread_impl.h"
|
|
|
|
void __wait(volatile int *addr, volatile int *waiters, int val, int priv)
|
|
{
|
|
int spins=100;
|
|
if (priv) priv = 128;
|
|
while (spins-- && (!waiters || !*waiters)) {
|
|
if (*addr==val) a_spin();
|
|
else return;
|
|
}
|
|
if (waiters) a_inc(waiters);
|
|
while (*addr==val) {
|
|
__syscall(SYS_futex, addr, FUTEX_WAIT|priv, val, 0) != -ENOSYS
|
|
|| __syscall(SYS_futex, addr, FUTEX_WAIT, val, 0);
|
|
}
|
|
if (waiters) a_dec(waiters);
|
|
}
|