1 .\" Page by b.hubert - may be freely modified and distributed
3 .\" Niki A. Rahimi (LTC Security Development, narahimi@us.ibm.com)
4 .\" added ERRORS section.
6 .\" Modified 2004-06-17 mtk
7 .\" Modified 2004-10-07 aeb, added FUTEX_REQUEUE, FUTEX_CMP_REQUEUE
10 .\" 2.6.14 adds FUTEX_WAKE_OP
11 .\" 2.6.18 adds (Ingo Molnar) priority inheritance support:
12 .\" FUTEX_LOCK_PI, FUTEX_UNLOCK_PI, and FUTEX_TRYLOCK_PI. These need
13 .\" to be documented in the manual page. Probably there is sufficient
14 .\" material in the kernel source file Documentation/pi-futex.txt.
15 .\" 2.6.25 adds FUTEX_WAKE_BITSET, FUTEX_WAIT_BITSET
17 .TH FUTEX 2 2010-05-22 "Linux" "Linux Programmer's Manual"
19 futex \- Fast Userspace Locking system call
23 .B "#include <linux/futex.h>"
24 .B "#include <sys/time.h>"
26 .BI "int futex(int *" uaddr ", int " op ", int " val \
27 ", const struct timespec *" timeout ,
29 .BI " int *" uaddr2 ", int " val3 );
30 .\" int *? void *? u32 *?
36 system call provides a method for
37 a program to wait for a value at a given address to change, and a
38 method to wake up anyone waiting on a particular address (while the
39 addresses for the same memory in separate processes may not be
40 equal, the kernel maps them internally so the same memory mapped in
41 different locations will correspond for
44 It is typically used to
45 implement the contended case of a lock in shared memory, as
51 operation did not finish uncontended in userspace, a call needs to be made
52 to the kernel to arbitrate.
53 Arbitration can either mean putting the calling
54 process to sleep or, conversely, waking a waiting process.
56 Callers of this function are expected to adhere to the semantics as set out in
59 semantics involve writing nonportable assembly instructions, this in turn
60 probably means that most users will in fact be library authors and not
61 general application developers.
65 argument needs to point to an aligned integer which stores the counter.
66 The operation to execute is passed via the
68 argument, along with a value
71 Five operations are currently defined:
74 This operation atomically verifies that the futex address
76 still contains the value
80 on this futex address.
83 argument is non-NULL, its contents describe the maximum
84 duration of the wait, which is infinite otherwise.
93 this call is executed if decrementing the count gave a negative value
94 (indicating contention), and will sleep until another process releases
95 the futex and executes the
100 This operation wakes at most \fIval\fP
101 processes waiting on this futex address (i.e., inside
112 this is executed if incrementing
113 the count showed that there were waiters, once the futex value has been set
114 to 1 (indicating that it is available).
116 .BR FUTEX_FD " (present up to and including Linux 2.6.25)"
117 To support asynchronous wakeups, this operation associates a file descriptor
119 .\" , suitable for .BR poll (2).
120 If another process executes a
122 the process will receive the signal number that was passed in
124 The calling process must close the returned file descriptor after use.
132 To prevent race conditions, the caller should test if the futex has
137 Because it was inherently racy,
139 has been removed from Linux 2.6.26 onwards.
141 .BR FUTEX_REQUEUE " (since Linux 2.5.70)"
142 This operation was introduced in order to avoid a "thundering herd" effect
145 is used and all processes woken up need to acquire another futex.
148 processes, and requeues all other waiters on the futex at address
156 .BR FUTEX_CMP_REQUEUE " (since Linux 2.6.7)"
157 There was a race in the intended use of
164 but first checks whether the location
166 still contains the value
168 If not, the operation fails with the error
175 Depending on which operation was executed,
176 the returned value for a successful call can have differing meanings.
179 Returns 0 if the process was woken by a
183 the operation fails with the error
185 If the futex was not equal to the expected value,
186 the operation fails with the error
190 or other spurious wakeups cause
192 to fail with the error
196 Returns the number of processes woken up.
199 Returns the new file descriptor associated with the futex.
202 Returns the number of processes woken up.
205 Returns the number of processes woken up.
207 In the event of an error, all operations return \-1, and set
209 to indicate the error.
213 No read access to futex memory.
217 found an unexpected futex value.
218 (This probably indicates a race;
226 information from userspace.
229 An operation was not defined or error in page alignment.
232 The system limit on the total number of open files has been reached.
235 Invalid operation specified in
239 Initial futex support was merged in Linux 2.5.7 but with different semantics
240 from what was described above.
241 A 4-argument system call with the semantics
242 given here was introduced in Linux 2.5.40.
243 In Linux 2.5.70 one argument
245 In Linux 2.6.7 a sixth argument was added \(em messy, especially
246 on the s390 architecture.
248 This system call is Linux-specific.
251 To reiterate, bare futexes are not intended as an easy-to-use abstraction
253 (There is no wrapper function for this system call in glibc.)
254 Implementors are expected to be assembly literate and to have
255 read the sources of the futex userspace library referenced below.
258 .\" Futexes were designed and worked on by
259 .\" Hubertus Franke (IBM Thomas J. Watson Research Center),
260 .\" Matthew Kirkwood, Ingo Molnar (Red Hat)
261 .\" and Rusty Russell (IBM Linux Technology Center).
262 .\" This page written by bert hubert.
266 \fIFuss, Futexes and Furwocks: Fast Userlevel Locking in Linux\fP
267 (proceedings of the Ottawa Linux Symposium 2002), online at
269 http://kernel.org/doc/ols/2002/ols2002-pages-479-495.pdf
271 Futex example library, futex-*.tar.bz2 at
273 ftp://ftp.nl.kernel.org/pub/linux/kernel/people/rusty/.