fast userspace locking
futex(volatile uint32_t *uaddr,
int op, int val,
const struct timespec *timeout,
volatile uint32_t *uaddr2);
syscall provides sleep and wakeup primitives related to a particular
Three op operations are currently supported:
- If val is equal to *uaddr, the
calling thread is blocked on the “wait channel” identified
by uaddr until timeout expires
or until another thread issues a
FUTEX_REQUEUEoperation with the same uaddr address. uaddr2 is ignored.
- Unblocks val threads sleeping on the wait channel identified by uaddr. timeout and uaddr2 are ignored.
- Similar to
FUTEX_WAKEbut also requeue remaining threads from the wait channel uaddr to uaddr2. In this case, pass uint32_t val2 as the fourth argument instead of timeout. At most that number of threads is requeued.
returns the number of woken threads.
futex() returns zero if woken by a matching
call, otherwise an error number is returned to indicate the error.
futex() will fail if:
- The op argument is invalid.
- The userspace address uaddr is invalid.
- The value pointed to by uaddr is not the same as the expected value val.
- The timeout specified a second value less than zero, or a nanosecond value less than zero or greater than or equal to 1000 million.
- The timeout expired before the thread was woken up
- A signal arrived.
- A signal arrived and SA_RESTART was set.
sigaction(2), pthread_cond_wait(3), pthread_mutex_lock(3), tsleep(9)
Ulrich Drepper, Futexes Are Tricky, https://www.akkadia.org/drepper/futex.pdf, November 5, 2011.
futex() syscall first appeared in
Linux 2.5.7 and was added to OpenBSD 6.2.