]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/futex.2
futex.2: Note that 'op' argument consists of two parts: operation + options
[thirdparty/man-pages.git] / man2 / futex.2
CommitLineData
8f0aff2a 1.\" Page by b.hubert
2297bf0e 2.\"
2e46a6e7 3.\" %%%LICENSE_START(FREELY_REDISTRIBUTABLE)
8f0aff2a 4.\" may be freely modified and distributed
8ff7380d 5.\" %%%LICENSE_END
fea681da
MK
6.\"
7.\" Niki A. Rahimi (LTC Security Development, narahimi@us.ibm.com)
8.\" added ERRORS section.
9.\"
10.\" Modified 2004-06-17 mtk
11.\" Modified 2004-10-07 aeb, added FUTEX_REQUEUE, FUTEX_CMP_REQUEUE
12.\"
bea08fec 13.\" FIXME .
4f58b197 14.\" See also https://bugzilla.kernel.org/show_bug.cgi?id=14303
40d5cf23 15.\" 2.6.14 adds FUTEX_WAKE_OP
4f58b197
MK
16.\" commit 4732efbeb997189d9f9b04708dc26bf8613ed721
17.\" Author: Jakub Jelinek <jakub@redhat.com>
18.\" Date: Tue Sep 6 15:16:25 2005 -0700
19.\"
bea08fec 20.\" FIXME .
c13182ef
MK
21.\" 2.6.18 adds (Ingo Molnar) priority inheritance support:
22.\" FUTEX_LOCK_PI, FUTEX_UNLOCK_PI, and FUTEX_TRYLOCK_PI. These need
34f7665a
MK
23.\" to be documented in the manual page. Probably there is sufficient
24.\" material in the kernel source file Documentation/pi-futex.txt.
4f58b197
MK
25.\" commit c87e2837be82df479a6bae9f155c43516d2feebc
26.\" Author: Ingo Molnar <mingo@elte.hu>
27.\" Date: Tue Jun 27 02:54:58 2006 -0700
28.\"
29.\" commit e2970f2fb6950183a34e8545faa093eb49d186e1
30.\" Author: Ingo Molnar <mingo@elte.hu>
31.\" Date: Tue Jun 27 02:54:47 2006 -0700
32.\"
27b38e1c 33.\" See Documentation/pi-futex.txt
4f58b197 34.\"
bea08fec 35.\" FIXME .
40d5cf23 36.\" 2.6.25 adds FUTEX_WAKE_BITSET, FUTEX_WAIT_BITSET
4f58b197
MK
37.\" commit cd689985cf49f6ff5c8eddc48d98b9d581d9475d
38.\" Author: Thomas Gleixner <tglx@linutronix.de>
39.\" Date: Fri Feb 1 17:45:14 2008 +0100
40.\"
bea08fec 41.\" FIXME .
4f58b197
MK
42.\" 2.6.31 adds FUTEX_WAIT_REQUEUE_PI, FUTEX_CMP_REQUEUE_PI
43.\" commit 52400ba946759af28442dee6265c5c0180ac7122
44.\" Author: Darren Hart <dvhltc@us.ibm.com>
45.\" Date: Fri Apr 3 13:40:49 2009 -0700
46.\"
47.\" commit ba9c22f2c01cf5c88beed5a6b9e07d42e10bd358
48.\" Author: Darren Hart <dvhltc@us.ibm.com>
49.\" Date: Mon Apr 20 22:22:22 2009 -0700
50.\"
51.\" See Documentation/futex-requeue-pi.txt
34f7665a 52.\"
3d155313 53.TH FUTEX 2 2014-05-21 "Linux" "Linux Programmer's Manual"
fea681da 54.SH NAME
ce154705 55futex \- fast user-space locking
fea681da 56.SH SYNOPSIS
9d9dc1e8 57.nf
fea681da
MK
58.sp
59.B "#include <linux/futex.h>"
fea681da
MK
60.B "#include <sys/time.h>"
61.sp
9d9dc1e8
MK
62.BI "int futex(int *" uaddr ", int " op ", int " val \
63", const struct timespec *" timeout ,
64.br
65.BI " int *" uaddr2 ", int " val3 );
fea681da 66.\" int *? void *? u32 *?
9d9dc1e8 67.fi
409f08b0 68
b939d6e4
MK
69.IR Note :
70There is no glibc wrapper for this system call; see NOTES.
47297adb 71.SH DESCRIPTION
fea681da
MK
72.PP
73The
e511ffb6 74.BR futex ()
fea681da
MK
75system call provides a method for
76a program to wait for a value at a given address to change, and a
77method to wake up anyone waiting on a particular address (while the
78addresses for the same memory in separate processes may not be
79equal, the kernel maps them internally so the same memory mapped in
80different locations will correspond for
e511ffb6 81.BR futex ()
c13182ef 82calls).
fd3fa7ef 83This system call is typically used to
fea681da
MK
84implement the contended case of a lock in shared memory, as
85described in
a8bda636 86.BR futex (7).
fea681da 87.PP
c13182ef 88When a
a8bda636 89.BR futex (7)
7fac88a9 90operation did not finish uncontended in user space, a call needs to be made
c13182ef
MK
91to the kernel to arbitrate.
92Arbitration can either mean putting the calling
fea681da
MK
93process to sleep or, conversely, waking a waiting process.
94.PP
95Callers of this function are expected to adhere to the semantics as set out in
a8bda636 96.BR futex (7).
fea681da 97As these
d603cc27 98semantics involve writing nonportable assembly instructions, this in turn
fea681da
MK
99probably means that most users will in fact be library authors and not
100general application developers.
101.PP
102The
103.I uaddr
104argument needs to point to an aligned integer which stores the counter.
105The operation to execute is passed via the
106.I op
c4bb193f 107argument, along with a value
fea681da
MK
108.IR val .
109.PP
6be4bad7
MK
110The
111.I op
112argument consists of two parts:
113a command that specifies the operation to be performed,
114bit-wise ORed with zero or or more options that
115modify the behaviour of the operation.
116.PP
117The operation specified in
118.I op
119is one of the following:
fea681da
MK
120.TP
121.B FUTEX_WAIT
122This operation atomically verifies that the futex address
123.I uaddr
124still contains the value
125.IR val ,
682edefb
MK
126and sleeps awaiting
127.B FUTEX_WAKE
128on this futex address.
c13182ef 129If the
fea681da 130.I timeout
82a6092b
MK
131argument is non-NULL, its contents specify the duration of the wait.
132(This interval will be rounded up to the system clock granularity,
133and kernel scheduling delays mean that the
134blocking interval may overrun by a small amount.)
135If
136.I timeout
137is NULL, the call blocks indefinitely.
4798a7f3 138
c13182ef 139The arguments
fea681da
MK
140.I uaddr2
141and
142.I val3
143are ignored.
144
145For
a8bda636 146.BR futex (7),
fea681da
MK
147this call is executed if decrementing the count gave a negative value
148(indicating contention), and will sleep until another process releases
682edefb
MK
149the futex and executes the
150.B FUTEX_WAKE
151operation.
fea681da
MK
152.TP
153.B FUTEX_WAKE
a8d55537 154This operation wakes at most \fIval\fP
b87dcfb9 155processes waiting on this futex address (i.e., inside
682edefb 156.BR FUTEX_WAIT ).
4798a7f3 157
fea681da
MK
158The arguments
159.IR timeout ,
160.I uaddr2
161and
162.I val3
163are ignored.
164
165For
a8bda636 166.BR futex (7),
fea681da
MK
167this is executed if incrementing
168the count showed that there were waiters, once the futex value has been set
169to 1 (indicating that it is available).
170.TP
da36351e 171.BR FUTEX_FD " (present up to and including Linux 2.6.25)"
fea681da
MK
172To support asynchronous wakeups, this operation associates a file descriptor
173with a futex.
174.\" , suitable for .BR poll (2).
682edefb
MK
175If another process executes a
176.BR FUTEX_WAKE ,
177the process will receive the signal number that was passed in
fea681da
MK
178.IR val .
179The calling process must close the returned file descriptor after use.
4798a7f3 180
fea681da
MK
181The arguments
182.IR timeout ,
183.I uaddr2
184and
185.I val3
186are ignored.
187
c13182ef 188To prevent race conditions, the caller should test if the futex has
682edefb
MK
189been upped after
190.B FUTEX_FD
191returns.
266a5e91 192
da36351e 193Because it was inherently racy,
682edefb 194.B FUTEX_FD
5fab2e7c 195has been removed from Linux 2.6.26 onward.
fea681da
MK
196.TP
197.BR FUTEX_REQUEUE " (since Linux 2.5.70)"
198This operation was introduced in order to avoid a "thundering herd" effect
682edefb
MK
199when
200.B FUTEX_WAKE
201is used and all processes woken up need to acquire another futex.
2abb73b9 202The argument
fea681da 203.I val
2abb73b9
TG
204contains the number of waiters on
205.I uaddr
206that are immediately woken up.
207The
fea681da 208.I timeout
2abb73b9
TG
209argument is (ab)used to specify the number of waiters
210that are requeued to the futex at
211.IR uaddr2 ;
212the kernel casts the
213.I timeout
214value to
215.IR u32 .
216.\" FIXME What are the constraints (if any) on the values of 'val' vs
217.\" 'timeout' vs [the number of waites on 'uaddr']?
218
219The argument
fea681da 220.I val3
2abb73b9 221is ignored.
fea681da
MK
222.TP
223.BR FUTEX_CMP_REQUEUE " (since Linux 2.6.7)"
682edefb
MK
224There was a race in the intended use of
225.BR FUTEX_REQUEUE ,
226so
227.B FUTEX_CMP_REQUEUE
228was introduced.
a72a3aeb 229.\" FIXME should there be a statement in the description of FUTEX_REQUEUE
a1f47699 230.\" to say that it should be avoided in favor of FUTEX_CMP_REQUEUE?
03433acb 231This operation is similar to
682edefb 232.BR FUTEX_REQUEUE ,
fea681da
MK
233but first checks whether the location
234.I uaddr
235still contains the value
236.IR val3 .
e808bba0
MK
237If not, the operation fails with the error
238.BR EAGAIN .
4798a7f3 239
03433acb
MK
240The arguments
241.IR val ,
242.IR uaddr ,
243.IR uaddr2 ,
244and
fea681da 245.I timeout
03433acb
MK
246are as for
247.BR FUTEX_REQUEUE .
47297adb 248.SH RETURN VALUE
fea681da 249.PP
e808bba0
MK
250In the event of an error, all operations return \-1, and set
251.I errno
252to indicate the error.
253The return value on success depends on the operation,
254as described in the following list:
fea681da
MK
255.TP
256.B FUTEX_WAIT
682edefb
MK
257Returns 0 if the process was woken by a
258.B FUTEX_WAKE
259call.
e808bba0 260See ERRORS for the various possible error returns.
fea681da
MK
261.TP
262.B FUTEX_WAKE
263Returns the number of processes woken up.
264.TP
265.B FUTEX_FD
266Returns the new file descriptor associated with the futex.
267.TP
268.B FUTEX_REQUEUE
269Returns the number of processes woken up.
270.TP
271.B FUTEX_CMP_REQUEUE
272Returns the number of processes woken up.
273.SH ERRORS
274.TP
275.B EACCES
276No read access to futex memory.
277.TP
278.B EAGAIN
682edefb 279.B FUTEX_CMP_REQUEUE
e808bba0 280detected that the value pointed to by
9f6c40c0
МК
281.I uaddr
282is not equal to the expected value
283.IR val3 .
fd1dc4c2 284.\" FIXME: Is the following sentence correct?
fea681da 285(This probably indicates a race;
682edefb
MK
286use the safe
287.B FUTEX_WAKE
288now.)
fea681da
MK
289.TP
290.B EFAULT
1ea901e8
MK
291A required pointer argument (i.e.,
292.IR uaddr ,
293.IR uaddr2 ,
294or
295.IR timeout )
496df304 296did not point to a valid user-space address.
fea681da 297.TP
9f6c40c0 298.B EINTR
e808bba0 299A
9f6c40c0 300.B FUTEX_WAIT
e808bba0
MK
301operation was interrupted by a signal (see
302.BR signal (7))
303or a spurious wakeup.
9f6c40c0 304.TP
fea681da 305.B EINVAL
fb2f4c27
MK
306.RB ( FUTEX_WAIT ,
307.BR FUTEX_WAIT_REQUEUE_PI )
308The supplied
309.I timeout
310argument was invalid
311.RI ( tv_sec
312was less than zero, or
313.IR tv_nsec
314was not less than 1000,000,000).
315.TP
316.B EINVAL
ea355b7f 317.RB ( FUTEX_WAIT ,
caf1ff25 318.BR FUTEX_WAKE ,
a1f47699
MK
319.BR FUTEX_REQUEUE ,
320.BR FUTEX_CMP_REQUEUE )
51ee94be 321.I uaddr
caf1ff25 322or (for
a1f47699
MK
323.BR FUTEX_REQUEUE
324and
325.BR FUTEX_CMP_REQUEUE )
caf1ff25 326.I uaddr2
51ee94be
MK
327does not point to a valid object\(emthat is,
328the address is not 4-byte-aligned.
329.TP
330.B EINVAL
bae14b6c 331.RB ( FUTEX_WAKE ,
e169277f
MK
332.BR FUTEX_REQUEUE ,
333.BR FUTEX_CMP_REQUEUE )
496df304 334The kernel detected an inconsistency between the user-space state at
9534086b
TG
335.I uaddr
336and the kernel state\(emthat is, it detected a waiter which waits in
337.BR FUTEX_LOCK_PI .
338.TP
339.B EINVAL
add875c0
MK
340.RB ( FUTEX_REQUEUE )
341.\" FIXME tglx suggested adding this, but does this error really
342.\" occur for FUTEX_REQUEUE?
343.I uaddr
344equals
345.IR uaddr2
346(i.e., an attempt was made to requeue to the same futex).
347.TP
348.B EINVAL
4832b48a 349Invalid argument.
fea681da
MK
350.TP
351.B ENFILE
352The system limit on the total number of open files has been reached.
4701fc28
MK
353.TP
354.B ENOSYS
355Invalid operation specified in
356.IR op .
9f6c40c0
МК
357.TP
358.B ETIMEDOUT
d1926d78
MK
359.RB ( FUTEX_WAIT )
360The operation timed out.
9f6c40c0
МК
361.TP
362.B EWOULDBLOCK
6b5025a6
MK
363.RB ( FUTEX_WAIT )
364The atomic enqueueing failed.
365.TP
366.B EWOULDBLOCK
e808bba0
MK
367.I op
368was
369.BR FUTEX_WAIT
370and the value pointed to by
9f6c40c0
МК
371.I uaddr
372was not equal to the expected value
373.I val
e808bba0 374at the time of the call.
47297adb 375.SH VERSIONS
a1d5f77c
MK
376.PP
377Initial futex support was merged in Linux 2.5.7 but with different semantics
378from what was described above.
c4bb193f 379A 4-argument system call with the semantics
fd3fa7ef 380described in this page was introduced in Linux 2.5.40.
11b520ed 381In Linux 2.5.70, one argument
a1d5f77c 382was added.
11b520ed 383In Linux 2.6.7, a sixth argument was added\(emmessy, especially
a1d5f77c 384on the s390 architecture.
47297adb 385.SH CONFORMING TO
8382f16d 386This system call is Linux-specific.
47297adb 387.SH NOTES
fea681da 388.PP
fcdad7d6 389To reiterate, bare futexes are not intended as an easy-to-use abstraction
c13182ef 390for end-users.
fcdad7d6 391(There is no wrapper function for this system call in glibc.)
c13182ef 392Implementors are expected to be assembly literate and to have
7fac88a9 393read the sources of the futex user-space library referenced below.
d282bb24 394.\" .SH AUTHORS
fea681da
MK
395.\" .PP
396.\" Futexes were designed and worked on by
397.\" Hubertus Franke (IBM Thomas J. Watson Research Center),
398.\" Matthew Kirkwood, Ingo Molnar (Red Hat)
399.\" and Rusty Russell (IBM Linux Technology Center).
400.\" This page written by bert hubert.
47297adb 401.SH SEE ALSO
d806bc05 402.BR restart_syscall (2),
14d8dd3b 403.BR futex (7)
fea681da 404.PP
52087dd3 405\fIFuss, Futexes and Furwocks: Fast Userlevel Locking in Linux\fP
9b936e9e
MK
406(proceedings of the Ottawa Linux Symposium 2002), online at
407.br
608bf950
SK
408.UR http://kernel.org\:/doc\:/ols\:/2002\:/ols2002-pages-479-495.pdf
409.UE
9b936e9e
MK
410.PP
411Futex example library, futex-*.tar.bz2 at
412.br
a605264d 413.UR ftp://ftp.kernel.org\:/pub\:/linux\:/kernel\:/people\:/rusty/
608bf950 414.UE