Futex

In computing, a futex (short for "fast userspace mutex") is a kernel system call that programmers can use to implement basic locking, or as a building block for higher-level locking abstractions such as semaphores and POSIX mutexes or condition variables.

A futex consists of a kernel-space wait queue that is attached to an atomic integer in userspace. Multiple processes or threads operate on the integer entirely in userspace (using atomic operations to avoid interfering with one another), and only resort to relatively expensive system calls to request operations on the wait queue (for example to wake up waiting processes, or to put the current process on the wait queue). A properly programmed futex-based lock will not use system calls except when the lock has contention; since most operations do not require arbitration between processes, this will not happen in most cases.

History
Hubertus Franke (IBM Thomas J. Watson Research Center), Matthew Kirkwood, Ingo Molnár (Red Hat), and Rusty Russell (IBM Linux Technology Center) originated the futex mechanism on Linux in 2002. In the same year, discussions took place on a proposal to make futexes accessible via the file system by creating a special node in  or. However, Linus Torvalds strongly opposed this idea and rejected any related patches.

Futexes then appeared for the first time in version 2.5.7 of the Linux kernel development series; the semantics stabilized as of version 2.5.40, and futexes have been part of the Linux kernel mainline since the December 2003 release of 2.6.x stable kernel series.

Futex functionality has been implemented in Microsoft Windows since Windows 8 or Windows Server 2012 under the name WaitOnAddress.

In 2013, Microsoft patented futex-related WaitOnAddress and the patent was granted in 2014. In May 2014, the CVE system announced a vulnerability discovered in the Linux kernel's futex subsystem that allowed denial-of-service attacks or local privilege escalation.

In May 2015, the Linux kernel introduced a deadlock bug via Commit b0c29f79ecea that caused a hang in user applications. The bug affected many enterprise Linux distributions, including 3.x and 4.x kernels, and Red Hat Enterprise Linux version 5, 6 and 7, SUSE Linux 12 and Amazon Linux.

Futexes have been implemented in OpenBSD since 2016.

The futex mechanism is one of the core concepts of the Zircon kernel in Google's Fuchsia operating system since at least April 2018.

Apple implemented futex in iOS/iPadOS/tvOS 17.4, macOS 14.4, watchOS 10.4 and visionOS 1.1.

Operations
Futexes have two basic operations,  and.


 * If the value stored at the address  is , puts the current thread to sleep.
 * If the value stored at the address  is , puts the current thread to sleep.


 * Wakes up  number of threads waiting on the address.
 * Wakes up  number of threads waiting on the address.

For more advanced uses, there are a number of other operations, the most used being  and , which both function as more generic   operations.


 * If the value stored at the address  is , wakes   threads waiting on the address  , and enqueues   threads waiting on the address   to now wait on the address  . This can be used to avoid the thundering herd problem on wake.
 * If the value stored at the address  is , wakes   threads waiting on the address  , and enqueues   threads waiting on the address   to now wait on the address  . This can be used to avoid the thundering herd problem on wake.


 * Will read, perform   with   on it, and store the result back to  . Then it will wake   threads waiting on  , and, if the previously read value from   matches   using comparison  , will wake   threads waiting on  . This very flexible and generic wake mechanism is useful for implementing many synchronization primitives.
 * Will read, perform   with   on it, and store the result back to  . Then it will wake   threads waiting on  , and, if the previously read value from   matches   using comparison  , will wake   threads waiting on  . This very flexible and generic wake mechanism is useful for implementing many synchronization primitives.