1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| | #include "pthread_impl.h"
void __lock(volatile int *addr)
{
if (libc.threads_minus_1) {
int volatile* waiters = addr+1;
for (;;) {
for (int spins = 0; spins < 10000; ++spins) {
if (a_tas(addr)) a_spin();
else {
/* a lock must ensure that all data
that is written before will be
visible */
a_aqf();
return;
}
}
a_inc(waiters);
__syscall(SYS_futex, addr, FUTEX_WAIT|THRD_PRIVATE, 1, 0);
a_dec(waiters);
}
}
}
void __unlock(volatile int *l)
{
a_spunl(l);
if (l[1]) __wake_priv(l, 1);
}
|