1 .\" This manpage has been automatically generated by docbook2man
2 .\" from a DocBook document. This tool can be found at:
3 .\" <http://shell.ipoline.com/~elmert/comp/docbook2X/>
4 .\" Please send any bug reports, improvements, comments, patches,
5 .\" etc. to Steve Cheng <steve@ggi-project.org>.
7 .\" SPDX-License-Identifier: MIT
9 .TH futex 7 (date) "Linux man-pages (unreleased)"
11 futex \- fast user-space locking
14 .B #include <linux/futex.h>
17 The Linux kernel provides futexes ("Fast user-space mutexes")
18 as a building block for fast user-space
19 locking and semaphores.
20 Futexes are very basic and lend themselves well for building higher-level
21 locking abstractions such as
22 mutexes, condition variables, read-write locks, barriers, and semaphores.
24 Most programmers will in fact not be using futexes directly but will
25 instead rely on system libraries built on them,
26 such as the Native POSIX Thread Library (NPTL) (see
29 A futex is identified by a piece of memory which can be
30 shared between processes or threads.
31 In these different processes, the futex need not have identical addresses.
32 In its bare form, a futex has semaphore semantics;
33 it is a counter that can be incremented and decremented atomically;
34 processes can wait for the value to become positive.
36 Futex operation occurs entirely in user space for the noncontended case.
37 The kernel is involved only to arbitrate the contended case.
38 As any sane design will strive for noncontention,
39 futexes are also optimized for this situation.
41 In its bare form, a futex is an aligned integer which is
42 touched only by atomic assembler instructions.
43 This integer is four bytes long on all platforms.
44 Processes can share this integer using
46 via shared memory segments, or because they share memory space,
47 in which case the application is commonly called multithreaded.
49 Any futex operation starts in user space,
50 but it may be necessary to communicate with the kernel using the
54 To "up" a futex, execute the proper assembler instructions that
55 will cause the host CPU to atomically increment the integer.
56 Afterward, check if it has in fact changed from 0 to 1, in which case
57 there were no waiters and the operation is done.
58 This is the noncontended case which is fast and should be common.
60 In the contended case, the atomic increment changed the counter
61 from \-1 (or some other negative number).
62 If this is detected, there are waiters.
63 User space should now set the counter to 1 and instruct the
64 kernel to wake up any waiters using the
68 Waiting on a futex, to "down" it, is the reverse operation.
69 Atomically decrement the counter and check if it changed to 0,
70 in which case the operation is done and the futex was uncontended.
71 In all other circumstances, the process should set the counter to \-1
72 and request that the kernel wait for another process to up the futex.
73 This is done using the
79 system call can optionally be passed a timeout specifying how long
81 wait for the futex to be upped.
82 In this case, semantics are more complex and the programmer is referred
87 The same holds for asynchronous futex waiting.
89 Initial futex support was merged in Linux 2.5.7
90 but with different semantics from those described above.
91 Current semantics are available from Linux 2.5.40 onward.
93 To reiterate, bare futexes are not intended as an easy-to-use
94 abstraction for end users.
95 Implementors are expected to be assembly literate and to have read
96 the sources of the futex user-space library referenced
99 This man page illustrates the most common use of the
101 primitives; it is by no means the only one.
104 .\" Futexes were designed and worked on by Hubertus Franke
105 .\" (IBM Thomas J. Watson Research Center),
106 .\" Matthew Kirkwood, Ingo Molnar (Red Hat) and
107 .\" Rusty Russell (IBM Linux Technology Center).
108 .\" This page written by bert hubert.
112 .BR get_robust_list (2),
113 .BR set_robust_list (2),
114 .BR set_tid_address (2),
117 .I Fuss, Futexes and Furwocks: Fast Userlevel Locking in Linux
118 (proceedings of the Ottawa Linux Symposium 2002),
119 futex example library, futex-*.tar.bz2
120 .UR https://mirrors.kernel.org\:/pub\:/linux\:/kernel\:/people\:/rusty/