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