void lock(futex_t *futex) { int c; if ((c = CAS(&futex->val, 0, 1) != 0) do { if (c == 2 || (CAS(&futex->val, 1, 2) != 1)) futex_wait(futex, 2); while ((c = CAS(&futex->val, 0, 2)) != 0)) } void unlock(futex_t *futex) { if (atomic_dec(&futex->val) != 1) { futex->val = 0; futex_wake(futex); } }