]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man7/futex.7
All pages: Remove the 5th argument to .TH
[thirdparty/man-pages.git] / man7 / futex.7
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>.
6 .\"
7 .\" SPDX-License-Identifier: MIT
8 .\"
9 .TH FUTEX 7 2017-09-15 "Linux man-pages (unreleased)"
10 .SH NAME
11 futex \- fast user-space locking
12 .SH SYNOPSIS
13 .nf
14 .B #include <linux/futex.h>
15 .fi
16 .SH DESCRIPTION
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.
23 .PP
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
27 .BR pthreads (7)).
28 .PP
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.
35 .PP
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.
40 .PP
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
45 .BR mmap (2),
46 via shared memory segments, or because they share memory space,
47 in which case the application is commonly called multithreaded.
48 .SS Semantics
49 Any futex operation starts in user space,
50 but it may be necessary to communicate with the kernel using the
51 .BR futex (2)
52 system call.
53 .PP
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.
59 .PP
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
65 .B FUTEX_WAKE
66 operation.
67 .PP
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
74 .B FUTEX_WAIT
75 operation.
76 .PP
77 The
78 .BR futex (2)
79 system call can optionally be passed a timeout specifying how long
80 the kernel should
81 wait for the futex to be upped.
82 In this case, semantics are more complex and the programmer is referred
83 to
84 .BR futex (2)
85 for
86 more details.
87 The same holds for asynchronous futex waiting.
88 .SH VERSIONS
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.
92 .SH NOTES
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
97 below.
98 .PP
99 This man page illustrates the most common use of the
100 .BR futex (2)
101 primitives; it is by no means the only one.
102 .\" .SH AUTHORS
103 .\" .PP
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.
109 .SH SEE ALSO
110 .BR clone (2),
111 .BR futex (2),
112 .BR get_robust_list (2),
113 .BR set_robust_list (2),
114 .BR set_tid_address (2),
115 .BR pthreads (7)
116 .PP
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/
121 .UE .