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