volatile int seq = 0, lock = 1, n = 0; void lock() { atomic_inc(n); while(1) { lseq = seq; if (atomic_dec(&lock)) return; futex_wait(&seq, lseq); } } void unlock() { lock = 1; atomic_inc(seq); atomic_dec(n); if (n > 0) futex_wake(&seq, 1); }