|
NAME | LIBRARY | SYNOPSIS | DESCRIPTION | RETURN VALUE | ERRORS | STANDARDS | HISTORY | EXAMPLES | SEE ALSO | COLOPHON |
|
|
|
futex(2) System Calls Manual futex(2)
futex - fast user-space locking
Standard C library (libc, -lc)
#include <linux/futex.h> /* Definition of FUTEX_* constants */
#include <sys/syscall.h> /* Definition of SYS_* constants */
#include <unistd.h>
long syscall(SYS_futex, uint32_t *uaddr, int op, ...);
The futex() system call provides a method for waiting until a
certain condition becomes true. It is typically used as a
blocking construct in the context of shared-memory
synchronization. When using futexes, the majority of the
synchronization operations are performed in user space. A user-
space program employs the futex() system call only when it is
likely that the program has to block for a longer time until the
condition becomes true. Other futex() operations can be used to
wake any processes or threads waiting for a particular condition.
A futex is a 32-bit value—referred to below as a futex word—whose
address is supplied to the futex() system call. (Futexes are 32
bits in size on all platforms, including 64-bit systems.) All
futex operations are governed by this value. In order to share a
futex between processes, the futex is placed in a region of shared
memory, created using (for example) mmap(2) or shmat(2). (Thus,
the futex word may have different virtual addresses in different
processes, but these addresses all refer to the same location in
physical memory.) In a multithreaded program, it is sufficient to
place the futex word in a global variable shared by all threads.
When executing a futex operation that requests to block a thread,
the kernel will block only if the futex word has the value that
the calling thread supplied (as one of the arguments of the
futex() call) as the expected value of the futex word. The
loading of the futex word's value, the comparison of that value
with the expected value, and the actual blocking will happen
atomically and will be totally ordered with respect to concurrent
operations performed by other threads on the same futex word.
Thus, the futex word is used to connect the synchronization in
user space with the implementation of blocking by the kernel.
Analogously to an atomic compare-and-exchange operation that
potentially changes shared memory, blocking via a futex is an
atomic compare-and-block operation.
One use of futexes is for implementing locks. The state of the
lock (i.e., acquired or not acquired) can be represented as an
atomically accessed flag in shared memory. In the uncontended
case, a thread can access or modify the lock state with atomic
instructions, for example atomically changing it from not acquired
to acquired using an atomic compare-and-exchange instruction.
(Such instructions are performed entirely in user mode, and the
kernel maintains no information about the lock state.) On the
other hand, a thread may be unable to acquire a lock because it is
already acquired by another thread. It then may pass the lock's
flag as a futex word and the value representing the acquired state
as the expected value to a futex() wait operation. This futex()
operation will block if and only if the lock is still acquired
(i.e., the value in the futex word still matches the "acquired
state"). When releasing the lock, a thread has to first reset the
lock state to not acquired and then execute a futex operation that
wakes threads blocked on the lock flag used as a futex word (this
can be further optimized to avoid unnecessary wake-ups). See
futex(7) for more detail on how to use futexes.
Besides the basic wait and wake-up futex functionality, there are
further futex operations aimed at supporting more complex use
cases.
Note that no explicit initialization or destruction is necessary
to use futexes; the kernel maintains a futex (i.e., the kernel-
internal implementation artifact) only while operations such as
FUTEX_WAIT(2const) are being performed on a particular futex word.
Arguments
The uaddr argument points to the futex word. On all platforms,
futexes are four-byte integers that must be aligned on a four-byte
boundary. The operation to perform on the futex is specified in
the op argument.
Futex operations
The op argument consists of two parts: a command that specifies
the operation to be performed, bitwise ORed with zero or more
options that modify the behaviour of the operation. The options
that may be included in op are as follows:
FUTEX_PRIVATE_FLAG (since Linux 2.6.22)
This option bit can be employed with all futex operations.
It tells the kernel that the futex is process-private and
not shared with another process (i.e., it is being used for
synchronization only between threads of the same process).
This allows the kernel to make some additional performance
optimizations.
As a convenience, <linux/futex.h> defines a set of
constants with the suffix _PRIVATE that are equivalents of
all of the operations listed below, but with the
FUTEX_PRIVATE_FLAG ORed into the constant value. Thus,
there are FUTEX_WAIT_PRIVATE, FUTEX_WAKE_PRIVATE, and so
on.
FUTEX_CLOCK_REALTIME (since Linux 2.6.28)
This option bit can be employed only with the
FUTEX_WAIT_BITSET(2const), FUTEX_WAIT_REQUEUE_PI(2const),
(since Linux 4.5) FUTEX_WAIT(2const), and (since Linux
5.14) FUTEX_LOCK_PI2(2const) operations.
If this option is set, the kernel measures the timeout
against the CLOCK_REALTIME clock.
If this option is not set, the kernel measures the timeout
against the CLOCK_MONOTONIC clock.
The operation specified in op is one of the following:
FUTEX_WAIT(2const)
FUTEX_WAKE(2const)
FUTEX_FD(2const)
FUTEX_REQUEUE(2const)
FUTEX_CMP_REQUEUE(2const)
FUTEX_WAKE_OP(2const)
FUTEX_WAIT_BITSET(2const)
FUTEX_WAKE_BITSET(2const)
Priority-inheritance futexes
Linux supports priority-inheritance (PI) futexes in order to
handle priority-inversion problems that can be encountered with
normal futex locks. Priority inversion is the problem that occurs
when a high-priority task is blocked waiting to acquire a lock
held by a low-priority task, while tasks at an intermediate
priority continuously preempt the low-priority task from the CPU.
Consequently, the low-priority task makes no progress toward
releasing the lock, and the high-priority task remains blocked.
Priority inheritance is a mechanism for dealing with the priority-
inversion problem. With this mechanism, when a high-priority task
becomes blocked by a lock held by a low-priority task, the
priority of the low-priority task is temporarily raised to that of
the high-priority task, so that it is not preempted by any
intermediate level tasks, and can thus make progress toward
releasing the lock. To be effective, priority inheritance must be
transitive, meaning that if a high-priority task blocks on a lock
held by a lower-priority task that is itself blocked by a lock
held by another intermediate-priority task (and so on, for chains
of arbitrary length), then both of those tasks (or more generally,
all of the tasks in a lock chain) have their priorities raised to
be the same as the high-priority task.
From a user-space perspective, what makes a futex PI-aware is a
policy agreement (described below) between user space and the
kernel about the value of the futex word, coupled with the use of
the PI-futex operations described below. (Unlike the other futex
operations described above, the PI-futex operations are designed
for the implementation of very specific IPC mechanisms.)
The PI-futex operations described below differ from the other
futex operations in that they impose policy on the use of the
value of the futex word:
• If the lock is not acquired, the futex word's value shall be 0.
• If the lock is acquired, the futex word's value shall be the
thread ID (TID; see gettid(2)) of the owning thread.
• If the lock is owned and there are threads contending for the
lock, then the FUTEX_WAITERS bit shall be set in the futex
word's value; in other words, this value is:
FUTEX_WAITERS | TID
(Note that is invalid for a PI futex word to have no owner and
FUTEX_WAITERS set.)
With this policy in place, a user-space application can acquire an
unacquired lock or release a lock using atomic instructions
executed in user mode (e.g., a compare-and-swap operation such as
cmpxchg on the x86 architecture). Acquiring a lock simply
consists of using compare-and-swap to atomically set the futex
word's value to the caller's TID if its previous value was 0.
Releasing a lock requires using compare-and-swap to set the futex
word's value to 0 if the previous value was the expected TID.
If a futex is already acquired (i.e., has a nonzero value),
waiters must employ the FUTEX_LOCK_PI(2const) or
FUTEX_LOCK_PI2(2const) operations to acquire the lock. If other
threads are waiting for the lock, then the FUTEX_WAITERS bit is
set in the futex value; in this case, the lock owner must employ
the FUTEX_UNLOCK_PI(2const) operation to release the lock.
In the cases where callers are forced into the kernel (i.e.,
required to perform a futex() call), they then deal directly with
a so-called RT-mutex, a kernel locking mechanism which implements
the required priority-inheritance semantics. After the RT-mutex
is acquired, the futex value is updated accordingly, before the
calling thread returns to user space.
It is important to note that the kernel will update the futex
word's value prior to returning to user space. (This prevents the
possibility of the futex word's value ending up in an invalid
state, such as having an owner but the value being 0, or having
waiters but not having the FUTEX_WAITERS bit set.)
If a futex has an associated RT-mutex in the kernel (i.e., there
are blocked waiters) and the owner of the futex/RT-mutex dies
unexpectedly, then the kernel cleans up the RT-mutex and hands it
over to the next waiter. This in turn requires that the user-
space value is updated accordingly. To indicate that this is
required, the kernel sets the FUTEX_OWNER_DIED bit in the futex
word along with the thread ID of the new owner. User space can
detect this situation via the presence of the FUTEX_OWNER_DIED bit
and is then responsible for cleaning up the stale state left over
by the dead owner.
PI futexes are operated on by specifying one of the values listed
below in op. Note that the PI futex operations must be used as
paired operations and are subject to some additional requirements:
• FUTEX_LOCK_PI(2const), FUTEX_LOCK_PI2(2const), and
FUTEX_TRYLOCK_PI(2const) pair with FUTEX_UNLOCK_PI(2const).
FUTEX_UNLOCK_PI(2const) must be called only on a futex owned by
the calling thread, as defined by the value policy, otherwise
the error EPERM results.
•