]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/futex.2
futex.2: Document FUTEX_PRIVATE_FLAG
[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.
fc30eb79
TG
116The options that may be included in
117.I op
118are as follows:
119.TP
120.BR FUTEX_PRIVATE_FLAG " (since Linux 2.6.22)"
121.\" commit 34f01cc1f512fa783302982776895c73714ebbc2
122This option bit can be employed with all futex operations.
123It tells the kernel that the futex is process private and not shared
124with another process.
125This allows the kernel to choose the fast path for validating
126the user-space address and avoids expensive VMA lookups,
127taking reference counts on file backing store, and so on.
6be4bad7
MK
128.PP
129The operation specified in
130.I op
131is one of the following:
fea681da
MK
132.TP
133.B FUTEX_WAIT
134This operation atomically verifies that the futex address
135.I uaddr
136still contains the value
137.IR val ,
682edefb
MK
138and sleeps awaiting
139.B FUTEX_WAKE
140on this futex address.
c13182ef 141If the
fea681da 142.I timeout
82a6092b
MK
143argument is non-NULL, its contents specify the duration of the wait.
144(This interval will be rounded up to the system clock granularity,
145and kernel scheduling delays mean that the
146blocking interval may overrun by a small amount.)
147If
148.I timeout
149is NULL, the call blocks indefinitely.
4798a7f3 150
c13182ef 151The arguments
fea681da
MK
152.I uaddr2
153and
154.I val3
155are ignored.
156
157For
a8bda636 158.BR futex (7),
fea681da
MK
159this call is executed if decrementing the count gave a negative value
160(indicating contention), and will sleep until another process releases
682edefb
MK
161the futex and executes the
162.B FUTEX_WAKE
163operation.
fea681da
MK
164.TP
165.B FUTEX_WAKE
a8d55537 166This operation wakes at most \fIval\fP
b87dcfb9 167processes waiting on this futex address (i.e., inside
682edefb 168.BR FUTEX_WAIT ).
4798a7f3 169
fea681da
MK
170The arguments
171.IR timeout ,
172.I uaddr2
173and
174.I val3
175are ignored.
176
177For
a8bda636 178.BR futex (7),
fea681da
MK
179this is executed if incrementing
180the count showed that there were waiters, once the futex value has been set
181to 1 (indicating that it is available).
182.TP
da36351e 183.BR FUTEX_FD " (present up to and including Linux 2.6.25)"
fea681da
MK
184To support asynchronous wakeups, this operation associates a file descriptor
185with a futex.
186.\" , suitable for .BR poll (2).
682edefb
MK
187If another process executes a
188.BR FUTEX_WAKE ,
189the process will receive the signal number that was passed in
fea681da
MK
190.IR val .
191The calling process must close the returned file descriptor after use.
4798a7f3 192
fea681da
MK
193The arguments
194.IR timeout ,
195.I uaddr2
196and
197.I val3
198are ignored.
199
c13182ef 200To prevent race conditions, the caller should test if the futex has
682edefb
MK
201been upped after
202.B FUTEX_FD
203returns.
266a5e91 204
da36351e 205Because it was inherently racy,
682edefb 206.B FUTEX_FD
5fab2e7c 207has been removed from Linux 2.6.26 onward.
fea681da
MK
208.TP
209.BR FUTEX_REQUEUE " (since Linux 2.5.70)"
210This operation was introduced in order to avoid a "thundering herd" effect
682edefb
MK
211when
212.B FUTEX_WAKE
213is used and all processes woken up need to acquire another futex.
2abb73b9 214The argument
fea681da 215.I val
2abb73b9
TG
216contains the number of waiters on
217.I uaddr
218that are immediately woken up.
219The
fea681da 220.I timeout
2abb73b9
TG
221argument is (ab)used to specify the number of waiters
222that are requeued to the futex at
223.IR uaddr2 ;
224the kernel casts the
225.I timeout
226value to
227.IR u32 .
228.\" FIXME What are the constraints (if any) on the values of 'val' vs
229.\" 'timeout' vs [the number of waites on 'uaddr']?
230
231The argument
fea681da 232.I val3
2abb73b9 233is ignored.
fea681da
MK
234.TP
235.BR FUTEX_CMP_REQUEUE " (since Linux 2.6.7)"
682edefb
MK
236There was a race in the intended use of
237.BR FUTEX_REQUEUE ,
238so
239.B FUTEX_CMP_REQUEUE
240was introduced.
a72a3aeb 241.\" FIXME should there be a statement in the description of FUTEX_REQUEUE
a1f47699 242.\" to say that it should be avoided in favor of FUTEX_CMP_REQUEUE?
03433acb 243This operation is similar to
682edefb 244.BR FUTEX_REQUEUE ,
fea681da
MK
245but first checks whether the location
246.I uaddr
247still contains the value
248.IR val3 .
e808bba0
MK
249If not, the operation fails with the error
250.BR EAGAIN .
4798a7f3 251
03433acb
MK
252The arguments
253.IR val ,
254.IR uaddr ,
255.IR uaddr2 ,
256and
fea681da 257.I timeout
03433acb
MK
258are as for
259.BR FUTEX_REQUEUE .
47297adb 260.SH RETURN VALUE
fea681da 261.PP
e808bba0
MK
262In the event of an error, all operations return \-1, and set
263.I errno
264to indicate the error.
265The return value on success depends on the operation,
266as described in the following list:
fea681da
MK
267.TP
268.B FUTEX_WAIT
682edefb
MK
269Returns 0 if the process was woken by a
270.B FUTEX_WAKE
271call.
e808bba0 272See ERRORS for the various possible error returns.
fea681da
MK
273.TP
274.B FUTEX_WAKE
275Returns the number of processes woken up.
276.TP
277.B FUTEX_FD
278Returns the new file descriptor associated with the futex.
279.TP
280.B FUTEX_REQUEUE
281Returns the number of processes woken up.
282.TP
283.B FUTEX_CMP_REQUEUE
284Returns the number of processes woken up.
285.SH ERRORS
286.TP
287.B EACCES
288No read access to futex memory.
289.TP
290.B EAGAIN
682edefb 291.B FUTEX_CMP_REQUEUE
e808bba0 292detected that the value pointed to by
9f6c40c0
МК
293.I uaddr
294is not equal to the expected value
295.IR val3 .
fd1dc4c2 296.\" FIXME: Is the following sentence correct?
fea681da 297(This probably indicates a race;
682edefb
MK
298use the safe
299.B FUTEX_WAKE
300now.)
fea681da
MK
301.TP
302.B EFAULT
1ea901e8
MK
303A required pointer argument (i.e.,
304.IR uaddr ,
305.IR uaddr2 ,
306or
307.IR timeout )
496df304 308did not point to a valid user-space address.
fea681da 309.TP
9f6c40c0 310.B EINTR
e808bba0 311A
9f6c40c0 312.B FUTEX_WAIT
e808bba0
MK
313operation was interrupted by a signal (see
314.BR signal (7))
315or a spurious wakeup.
9f6c40c0 316.TP
fea681da 317.B EINVAL
fb2f4c27
MK
318.RB ( FUTEX_WAIT ,
319.BR FUTEX_WAIT_REQUEUE_PI )
320The supplied
321.I timeout
322argument was invalid
323.RI ( tv_sec
324was less than zero, or
325.IR tv_nsec
326was not less than 1000,000,000).
327.TP
328.B EINVAL
ea355b7f 329.RB ( FUTEX_WAIT ,
caf1ff25 330.BR FUTEX_WAKE ,
a1f47699
MK
331.BR FUTEX_REQUEUE ,
332.BR FUTEX_CMP_REQUEUE )
51ee94be 333.I uaddr
caf1ff25 334or (for
a1f47699
MK
335.BR FUTEX_REQUEUE
336and
337.BR FUTEX_CMP_REQUEUE )
caf1ff25 338.I uaddr2
51ee94be
MK
339does not point to a valid object\(emthat is,
340the address is not 4-byte-aligned.
341.TP
342.B EINVAL
bae14b6c 343.RB ( FUTEX_WAKE ,
e169277f
MK
344.BR FUTEX_REQUEUE ,
345.BR FUTEX_CMP_REQUEUE )
496df304 346The kernel detected an inconsistency between the user-space state at
9534086b
TG
347.I uaddr
348and the kernel state\(emthat is, it detected a waiter which waits in
349.BR FUTEX_LOCK_PI .
350.TP
351.B EINVAL
add875c0
MK
352.RB ( FUTEX_REQUEUE )
353.\" FIXME tglx suggested adding this, but does this error really
354.\" occur for FUTEX_REQUEUE?
355.I uaddr
356equals
357.IR uaddr2
358(i.e., an attempt was made to requeue to the same futex).
359.TP
360.B EINVAL
4832b48a 361Invalid argument.
fea681da
MK
362.TP
363.B ENFILE
364The system limit on the total number of open files has been reached.
4701fc28
MK
365.TP
366.B ENOSYS
367Invalid operation specified in
368.IR op .
9f6c40c0
МК
369.TP
370.B ETIMEDOUT
d1926d78
MK
371.RB ( FUTEX_WAIT )
372The operation timed out.
9f6c40c0
МК
373.TP
374.B EWOULDBLOCK
6b5025a6
MK
375.RB ( FUTEX_WAIT )
376The atomic enqueueing failed.
377.TP
378.B EWOULDBLOCK
e808bba0
MK
379.I op
380was
381.BR FUTEX_WAIT
382and the value pointed to by
9f6c40c0
МК
383.I uaddr
384was not equal to the expected value
385.I val
e808bba0 386at the time of the call.
47297adb 387.SH VERSIONS
a1d5f77c
MK
388.PP
389Initial futex support was merged in Linux 2.5.7 but with different semantics
390from what was described above.
c4bb193f 391A 4-argument system call with the semantics
fd3fa7ef 392described in this page was introduced in Linux 2.5.40.
11b520ed 393In Linux 2.5.70, one argument
a1d5f77c 394was added.
11b520ed 395In Linux 2.6.7, a sixth argument was added\(emmessy, especially
a1d5f77c 396on the s390 architecture.
47297adb 397.SH CONFORMING TO
8382f16d 398This system call is Linux-specific.
47297adb 399.SH NOTES
fea681da 400.PP
fcdad7d6 401To reiterate, bare futexes are not intended as an easy-to-use abstraction
c13182ef 402for end-users.
fcdad7d6 403(There is no wrapper function for this system call in glibc.)
c13182ef 404Implementors are expected to be assembly literate and to have
7fac88a9 405read the sources of the futex user-space library referenced below.
d282bb24 406.\" .SH AUTHORS
fea681da
MK
407.\" .PP
408.\" Futexes were designed and worked on by
409.\" Hubertus Franke (IBM Thomas J. Watson Research Center),
410.\" Matthew Kirkwood, Ingo Molnar (Red Hat)
411.\" and Rusty Russell (IBM Linux Technology Center).
412.\" This page written by bert hubert.
47297adb 413.SH SEE ALSO
d806bc05 414.BR restart_syscall (2),
14d8dd3b 415.BR futex (7)
fea681da 416.PP
52087dd3 417\fIFuss, Futexes and Furwocks: Fast Userlevel Locking in Linux\fP
9b936e9e
MK
418(proceedings of the Ottawa Linux Symposium 2002), online at
419.br
608bf950
SK
420.UR http://kernel.org\:/doc\:/ols\:/2002\:/ols2002-pages-479-495.pdf
421.UE
9b936e9e
MK
422.PP
423Futex example library, futex-*.tar.bz2 at
424.br
a605264d 425.UR ftp://ftp.kernel.org\:/pub\:/linux\:/kernel\:/people\:/rusty/
608bf950 426.UE