]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/futex.2
futex.7: Minor wording and grammar fixes
[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
b939d6e4
MK
68.IR Note :
69There is no glibc wrapper for this system call; see NOTES.
47297adb 70.SH DESCRIPTION
fea681da
MK
71.PP
72The
e511ffb6 73.BR futex ()
fea681da
MK
74system call provides a method for
75a program to wait for a value at a given address to change, and a
76method to wake up anyone waiting on a particular address (while the
77addresses for the same memory in separate processes may not be
78equal, the kernel maps them internally so the same memory mapped in
79different locations will correspond for
e511ffb6 80.BR futex ()
c13182ef 81calls).
fd3fa7ef 82This system call is typically used to
fea681da
MK
83implement the contended case of a lock in shared memory, as
84described in
a8bda636 85.BR futex (7).
fea681da 86.PP
c13182ef 87When a
a8bda636 88.BR futex (7)
7fac88a9 89operation did not finish uncontended in user space, a call needs to be made
c13182ef
MK
90to the kernel to arbitrate.
91Arbitration can either mean putting the calling
fea681da
MK
92process to sleep or, conversely, waking a waiting process.
93.PP
94Callers of this function are expected to adhere to the semantics as set out in
a8bda636 95.BR futex (7).
fea681da 96As these
d603cc27 97semantics involve writing nonportable assembly instructions, this in turn
fea681da
MK
98probably means that most users will in fact be library authors and not
99general application developers.
100.PP
101The
102.I uaddr
103argument needs to point to an aligned integer which stores the counter.
104The operation to execute is passed via the
105.I op
c4bb193f 106argument, along with a value
fea681da
MK
107.IR val .
108.PP
109Five operations are currently defined:
110.TP
111.B FUTEX_WAIT
112This operation atomically verifies that the futex address
113.I uaddr
114still contains the value
115.IR val ,
682edefb
MK
116and sleeps awaiting
117.B FUTEX_WAKE
118on this futex address.
c13182ef 119If the
fea681da 120.I timeout
82a6092b
MK
121argument is non-NULL, its contents specify the duration of the wait.
122(This interval will be rounded up to the system clock granularity,
123and kernel scheduling delays mean that the
124blocking interval may overrun by a small amount.)
125If
126.I timeout
127is NULL, the call blocks indefinitely.
c13182ef 128The arguments
fea681da
MK
129.I uaddr2
130and
131.I val3
132are ignored.
133
134For
a8bda636 135.BR futex (7),
fea681da
MK
136this call is executed if decrementing the count gave a negative value
137(indicating contention), and will sleep until another process releases
682edefb
MK
138the futex and executes the
139.B FUTEX_WAKE
140operation.
fea681da
MK
141.TP
142.B FUTEX_WAKE
a8d55537 143This operation wakes at most \fIval\fP
b87dcfb9 144processes waiting on this futex address (i.e., inside
682edefb 145.BR FUTEX_WAIT ).
fea681da
MK
146The arguments
147.IR timeout ,
148.I uaddr2
149and
150.I val3
151are ignored.
152
153For
a8bda636 154.BR futex (7),
fea681da
MK
155this is executed if incrementing
156the count showed that there were waiters, once the futex value has been set
157to 1 (indicating that it is available).
158.TP
da36351e 159.BR FUTEX_FD " (present up to and including Linux 2.6.25)"
fea681da
MK
160To support asynchronous wakeups, this operation associates a file descriptor
161with a futex.
162.\" , suitable for .BR poll (2).
682edefb
MK
163If another process executes a
164.BR FUTEX_WAKE ,
165the process will receive the signal number that was passed in
fea681da
MK
166.IR val .
167The calling process must close the returned file descriptor after use.
168The arguments
169.IR timeout ,
170.I uaddr2
171and
172.I val3
173are ignored.
174
c13182ef 175To prevent race conditions, the caller should test if the futex has
682edefb
MK
176been upped after
177.B FUTEX_FD
178returns.
266a5e91 179
da36351e 180Because it was inherently racy,
682edefb 181.B FUTEX_FD
5fab2e7c 182has been removed from Linux 2.6.26 onward.
fea681da
MK
183.TP
184.BR FUTEX_REQUEUE " (since Linux 2.5.70)"
185This operation was introduced in order to avoid a "thundering herd" effect
682edefb
MK
186when
187.B FUTEX_WAKE
188is used and all processes woken up need to acquire another futex.
c13182ef 189This call wakes up
fea681da
MK
190.I val
191processes, and requeues all other waiters on the futex at address
192.IR uaddr2 .
193The arguments
194.I timeout
195and
196.I val3
197are ignored.
198.TP
199.BR FUTEX_CMP_REQUEUE " (since Linux 2.6.7)"
682edefb
MK
200There was a race in the intended use of
201.BR FUTEX_REQUEUE ,
202so
203.B FUTEX_CMP_REQUEUE
204was introduced.
205This is similar to
206.BR FUTEX_REQUEUE ,
fea681da
MK
207but first checks whether the location
208.I uaddr
209still contains the value
210.IR val3 .
e808bba0
MK
211If not, the operation fails with the error
212.BR EAGAIN .
fea681da
MK
213The argument
214.I timeout
215is ignored.
47297adb 216.SH RETURN VALUE
fea681da 217.PP
e808bba0
MK
218In the event of an error, all operations return \-1, and set
219.I errno
220to indicate the error.
221The return value on success depends on the operation,
222as described in the following list:
fea681da
MK
223.TP
224.B FUTEX_WAIT
682edefb
MK
225Returns 0 if the process was woken by a
226.B FUTEX_WAKE
227call.
e808bba0 228See ERRORS for the various possible error returns.
fea681da
MK
229.TP
230.B FUTEX_WAKE
231Returns the number of processes woken up.
232.TP
233.B FUTEX_FD
234Returns the new file descriptor associated with the futex.
235.TP
236.B FUTEX_REQUEUE
237Returns the number of processes woken up.
238.TP
239.B FUTEX_CMP_REQUEUE
240Returns the number of processes woken up.
241.SH ERRORS
242.TP
243.B EACCES
244No read access to futex memory.
245.TP
246.B EAGAIN
682edefb 247.B FUTEX_CMP_REQUEUE
e808bba0 248detected that the value pointed to by
9f6c40c0
МК
249.I uaddr
250is not equal to the expected value
251.IR val3 .
fea681da 252(This probably indicates a race;
682edefb
MK
253use the safe
254.B FUTEX_WAKE
255now.)
fea681da
MK
256.TP
257.B EFAULT
e808bba0 258Error retrieving
fea681da 259.I timeout
7fac88a9 260information from user space.
fea681da 261.TP
9f6c40c0 262.B EINTR
e808bba0 263A
9f6c40c0 264.B FUTEX_WAIT
e808bba0
MK
265operation was interrupted by a signal (see
266.BR signal (7))
267or a spurious wakeup.
9f6c40c0 268.TP
fea681da 269.B EINVAL
4832b48a 270Invalid argument.
fea681da
MK
271.TP
272.B ENFILE
273The system limit on the total number of open files has been reached.
4701fc28
MK
274.TP
275.B ENOSYS
276Invalid operation specified in
277.IR op .
9f6c40c0
МК
278.TP
279.B ETIMEDOUT
280Timeout during the
281.B FUTEX_WAIT
282operation.
283.TP
284.B EWOULDBLOCK
e808bba0
MK
285.I op
286was
287.BR FUTEX_WAIT
288and the value pointed to by
9f6c40c0
МК
289.I uaddr
290was not equal to the expected value
291.I val
e808bba0 292at the time of the call.
47297adb 293.SH VERSIONS
a1d5f77c
MK
294.PP
295Initial futex support was merged in Linux 2.5.7 but with different semantics
296from what was described above.
c4bb193f 297A 4-argument system call with the semantics
fd3fa7ef 298described in this page was introduced in Linux 2.5.40.
11b520ed 299In Linux 2.5.70, one argument
a1d5f77c 300was added.
11b520ed 301In Linux 2.6.7, a sixth argument was added\(emmessy, especially
a1d5f77c 302on the s390 architecture.
47297adb 303.SH CONFORMING TO
8382f16d 304This system call is Linux-specific.
47297adb 305.SH NOTES
fea681da 306.PP
fcdad7d6 307To reiterate, bare futexes are not intended as an easy-to-use abstraction
c13182ef 308for end-users.
fcdad7d6 309(There is no wrapper function for this system call in glibc.)
c13182ef 310Implementors are expected to be assembly literate and to have
7fac88a9 311read the sources of the futex user-space library referenced below.
d282bb24 312.\" .SH AUTHORS
fea681da
MK
313.\" .PP
314.\" Futexes were designed and worked on by
315.\" Hubertus Franke (IBM Thomas J. Watson Research Center),
316.\" Matthew Kirkwood, Ingo Molnar (Red Hat)
317.\" and Rusty Russell (IBM Linux Technology Center).
318.\" This page written by bert hubert.
47297adb 319.SH SEE ALSO
d806bc05 320.BR restart_syscall (2),
14d8dd3b 321.BR futex (7)
fea681da 322.PP
52087dd3 323\fIFuss, Futexes and Furwocks: Fast Userlevel Locking in Linux\fP
9b936e9e
MK
324(proceedings of the Ottawa Linux Symposium 2002), online at
325.br
608bf950
SK
326.UR http://kernel.org\:/doc\:/ols\:/2002\:/ols2002-pages-479-495.pdf
327.UE
9b936e9e
MK
328.PP
329Futex example library, futex-*.tar.bz2 at
330.br
a605264d 331.UR ftp://ftp.kernel.org\:/pub\:/linux\:/kernel\:/people\:/rusty/
608bf950 332.UE