]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/futex.2
futex.2: tfix
[thirdparty/man-pages.git] / man2 / futex.2
CommitLineData
8f0aff2a 1.\" Page by b.hubert
1abce893
MK
2.\" and Copyright (C) 2015, Thomas Gleixner <tglx@linutronix.de>
3.\" and Copyright (C) 2015, Michael Kerrisk <mtk.manpages@gmail.com>
2297bf0e 4.\"
2e46a6e7 5.\" %%%LICENSE_START(FREELY_REDISTRIBUTABLE)
8f0aff2a 6.\" may be freely modified and distributed
8ff7380d 7.\" %%%LICENSE_END
fea681da
MK
8.\"
9.\" Niki A. Rahimi (LTC Security Development, narahimi@us.ibm.com)
10.\" added ERRORS section.
11.\"
12.\" Modified 2004-06-17 mtk
13.\" Modified 2004-10-07 aeb, added FUTEX_REQUEUE, FUTEX_CMP_REQUEUE
14.\"
c13182ef
MK
15.\" 2.6.18 adds (Ingo Molnar) priority inheritance support:
16.\" FUTEX_LOCK_PI, FUTEX_UNLOCK_PI, and FUTEX_TRYLOCK_PI. These need
34f7665a
MK
17.\" to be documented in the manual page. Probably there is sufficient
18.\" material in the kernel source file Documentation/pi-futex.txt.
4f58b197
MK
19.\" commit c87e2837be82df479a6bae9f155c43516d2feebc
20.\" Author: Ingo Molnar <mingo@elte.hu>
21.\" Date: Tue Jun 27 02:54:58 2006 -0700
22.\"
23.\" commit e2970f2fb6950183a34e8545faa093eb49d186e1
24.\" Author: Ingo Molnar <mingo@elte.hu>
25.\" Date: Tue Jun 27 02:54:47 2006 -0700
26.\"
27b38e1c 27.\" See Documentation/pi-futex.txt
4f58b197 28.\"
4f58b197
MK
29.\" 2.6.31 adds FUTEX_WAIT_REQUEUE_PI, FUTEX_CMP_REQUEUE_PI
30.\" commit 52400ba946759af28442dee6265c5c0180ac7122
31.\" Author: Darren Hart <dvhltc@us.ibm.com>
32.\" Date: Fri Apr 3 13:40:49 2009 -0700
33.\"
34.\" commit ba9c22f2c01cf5c88beed5a6b9e07d42e10bd358
35.\" Author: Darren Hart <dvhltc@us.ibm.com>
36.\" Date: Mon Apr 20 22:22:22 2009 -0700
37.\"
38.\" See Documentation/futex-requeue-pi.txt
34f7665a 39.\"
3d155313 40.TH FUTEX 2 2014-05-21 "Linux" "Linux Programmer's Manual"
fea681da 41.SH NAME
ce154705 42futex \- fast user-space locking
fea681da 43.SH SYNOPSIS
9d9dc1e8 44.nf
fea681da
MK
45.sp
46.B "#include <linux/futex.h>"
fea681da
MK
47.B "#include <sys/time.h>"
48.sp
d33602c4
MK
49.BI "int futex(int *" uaddr ", int " futex_op ", int " val ,
50.BI " const struct timespec *" timeout ,
9d9dc1e8 51.BI " int *" uaddr2 ", int " val3 );
fea681da 52.\" int *? void *? u32 *?
9d9dc1e8 53.fi
409f08b0 54
b939d6e4
MK
55.IR Note :
56There is no glibc wrapper for this system call; see NOTES.
47297adb 57.SH DESCRIPTION
fea681da
MK
58.PP
59The
e511ffb6 60.BR futex ()
fea681da
MK
61system call provides a method for
62a program to wait for a value at a given address to change, and a
63method to wake up anyone waiting on a particular address (while the
64addresses for the same memory in separate processes may not be
65equal, the kernel maps them internally so the same memory mapped in
66different locations will correspond for
e511ffb6 67.BR futex ()
c13182ef 68calls).
fd3fa7ef 69This system call is typically used to
fea681da
MK
70implement the contended case of a lock in shared memory, as
71described in
a8bda636 72.BR futex (7).
fea681da 73.PP
f388ba70
MK
74When a futex operation did not finish uncontended in user space, a
75.BR futex ()
76call needs to be made to the kernel to arbitrate.
c13182ef 77Arbitration can either mean putting the calling
fea681da
MK
78process to sleep or, conversely, waking a waiting process.
79.PP
f388ba70
MK
80Callers of
81.BR futex ()
82are expected to adhere to the semantics described in
a8bda636 83.BR futex (7).
fea681da 84As these
d603cc27 85semantics involve writing nonportable assembly instructions, this in turn
fea681da
MK
86probably means that most users will in fact be library authors and not
87general application developers.
88.PP
89The
90.I uaddr
f388ba70
MK
91argument points to an integer which stores the counter (futex).
92On all platforms, futexes are four-byte integers that
93must be aligned on a four-byte boundary.
94The operation to perform on the futex is specified in the
95.I futex_op
96argument;
97.IR val
98is a value whose meaning and purpose depends on
99.IR futex_op .
36ab2074
MK
100
101The remaining arguments
102.RI ( timeout ,
103.IR uaddr2 ,
104and
105.IR val3 )
106are required only for certain of the futex operations described below.
107Where one of these arguments is not required, it is ignored.
108For several blocking operations, the
109.I timeout
110argument is a pointer to a
111.IR timespec
112structure that specifies a timeout for the operation.
113However, notwithstanding the prototype shown above, for some operations,
114this argument is instead a four-byte integer whose meaning
115is determined by the operation.
116Where it is required,
117.IR uaddr2
118is a pointer to a second futex that is employed by the operation.
119The interpretation of the final integer argument,
120.IR val3 ,
121depends on the operation.
122
6be4bad7 123The
d33602c4 124.I futex_op
6be4bad7
MK
125argument consists of two parts:
126a command that specifies the operation to be performed,
127bit-wise ORed with zero or or more options that
128modify the behaviour of the operation.
fc30eb79 129The options that may be included in
d33602c4 130.I futex_op
fc30eb79
TG
131are as follows:
132.TP
133.BR FUTEX_PRIVATE_FLAG " (since Linux 2.6.22)"
134.\" commit 34f01cc1f512fa783302982776895c73714ebbc2
135This option bit can be employed with all futex operations.
136It tells the kernel that the futex is process private and not shared
137with another process.
138This allows the kernel to choose the fast path for validating
139the user-space address and avoids expensive VMA lookups,
140taking reference counts on file backing store, and so on.
ae2c1774
MK
141
142As a convenience,
143.IR <linux/futex.h>
144defines a set of constants with the suffix
145.BR _PRIVATE
146that are equivalents of all of the operations listed below,
147.\" except the obsolete FUTEX_FD for which the "private" flag was
148.\" meaningless
149but with the
150.BR FUTEX_PRIVATE_FLAG
151ORed into the constant value.
152Thus, there are
153.BR FUTEX_WAIT_PRIVATE ,
154.BR FUTEX_WAKE_PRIVATE ,
155and so on.
2e98bbc2
TG
156.TP
157.BR FUTEX_CLOCK_REALTIME " (since Linux 2.6.28)"
158.\" commit 1acdac104668a0834cfa267de9946fac7764d486
4a7e5b05 159This option bit can be employed only with the
2e98bbc2
TG
160.BR FUTEX_WAIT_BITSET
161and
162.BR FUTEX_WAIT_REQUEUE_PI
163operations (described below).
164
165If this option is set,
166the kernel treats the user space supplied timeout as an absolute
167time based on
168.BR CLOCK_REALTIME .
169
170If this option is not set,
1c952cf5
MK
171the kernel treats the user space supplied timeout as relative time,
172.\" FIXME I added CLOCK_MONOTONIC here. Is it correct?
173measured against the
174.BR CLOCK_MONOTONIC
175clock.
6be4bad7
MK
176.PP
177The operation specified in
d33602c4 178.I futex_op
6be4bad7 179is one of the following:
fea681da 180.TP
81c9d87e
MK
181.BR FUTEX_WAIT " (since Linux 2.6.0)"
182.\" Strictly speaking, since some time in 2.5.x
f065673c
MK
183This operation tests that the value at the
184location pointed to by the futex address
fea681da
MK
185.I uaddr
186still contains the value
187.IR val ,
f065673c 188and then sleeps awaiting
682edefb 189.B FUTEX_WAKE
f065673c
MK
190on the futex address.
191The test and sleep steps are performed atomically.
192If the futex value does not match
193.IR val ,
4710334a 194then the call fails immediately with the error
f065673c
MK
195.BR EWOULDBLOCK .
196.\" FIXME I added the following sentence. Please confirm that it is correct.
197The purpose of the test step is to detect races where
198another process changes that value of the futex between
199the time it was last checked and the time of the
200.BR FUTEX_WAIT
63d3f911 201operation.
1909e523 202
c13182ef 203If the
fea681da 204.I timeout
1c952cf5
MK
205argument is non-NULL, its contents specify a relative timeout for the wait
206.\" FIXME I added CLOCK_MONOTONIC here. Is it correct?
207measured according to the
208.BR CLOCK_MONOTONIC
209clock.
82a6092b
MK
210(This interval will be rounded up to the system clock granularity,
211and kernel scheduling delays mean that the
212blocking interval may overrun by a small amount.)
213If
214.I timeout
215is NULL, the call blocks indefinitely.
4798a7f3 216
c13182ef 217The arguments
fea681da
MK
218.I uaddr2
219and
220.I val3
221are ignored.
222
223For
a8bda636 224.BR futex (7),
fea681da
MK
225this call is executed if decrementing the count gave a negative value
226(indicating contention), and will sleep until another process releases
682edefb
MK
227the futex and executes the
228.B FUTEX_WAKE
229operation.
fea681da 230.TP
81c9d87e
MK
231.BR FUTEX_WAKE " (since Linux 2.6.0)"
232.\" Strictly speaking, since Linux 2.5.x
f065673c
MK
233This operation wakes at most
234.I val
235processes waiting (i.e., inside
236.BR FUTEX_WAIT )
237on the futex at the address
238.IR uaddr .
239Most commonly,
240.I val
241is specified as either 1 (wake up a single waiter) or
242.BR INT_MAX
243(wake up all waiters).
730bfbda
MK
244.\" FIXME Please confirm that the following is correct:
245No guarantee is provided about which waiters are awoken
246(e.g., a waiter with a higher scheduling priority is not guaranteed
247to be awoken in preference to a waiter with a lower priority).
4798a7f3 248
fea681da
MK
249The arguments
250.IR timeout ,
251.I uaddr2
252and
253.I val3
254are ignored.
255
256For
a8bda636 257.BR futex (7),
fea681da
MK
258this is executed if incrementing
259the count showed that there were waiters, once the futex value has been set
260to 1 (indicating that it is available).
6bac3b85
MK
261.\"
262.\" FIXME I added some FUTEX_WAKE_OP text, and I'd be happy if someone
263.\" checked it.
fea681da 264.TP
d67e21f5
MK
265.BR FUTEX_WAKE_OP " (since Linux 2.6.14)"
266.\" commit 4732efbeb997189d9f9b04708dc26bf8613ed721
6bac3b85
MK
267.\" Author: Jakub Jelinek <jakub@redhat.com>
268.\" Date: Tue Sep 6 15:16:25 2005 -0700
269This operation was added to support some user-space use cases
270where more than one futex must be handled at the same time.
271The most notable example is the implementation of
272.BR pthread_cond_signal (3),
273which requires operations on two futexes,
274the one used to implement the mutex and the one used in the implementation
275of the wait queue associated with the condition variable.
276.BR FUTEX_WAKE_OP
277allows such cases to be implemented without leading to
278high rates of contention and context switching.
279
280The
281.BR FUTEX_WAIT_OP
282operation is equivalent to atomically executing the following code:
283
284.in +4n
285.nf
286int oldval = *(int *) uaddr2;
287*(int *) uaddr2 = oldval \fIop\fP \fIoparg\fP;
288futex(uaddr, FUTEX_WAKE, val, 0, 0, 0);
289if (oldval \fIcmp\fP \fIcmparg\fP)
290 futex(uaddr2, FUTEX_WAKE, nr_wake2, 0, 0, 0);
291.fi
292.in
293
294In other words,
295.BR FUTEX_WAIT_OP
296does the following:
297.RS
298.IP * 3
299saves the original value of the futex at
300.IR uaddr2 ;
301.IP *
302performs an operation to modify the value of the futex at
303.IR uaddr2 ;
304.IP *
305wakes up a maximum of
306.I val
307waiters on the futex
308.IR uaddr ;
309and
310.IP *
311dependent on the results of a test of the original value of the futex at
312.IR uaddr2 ,
313wakes up a maximum of
314.I nr_wake2
315waiters on the futex
316.IR uaddr2 .
317.RE
318.IP
319The
320.I nr_wake2
321value is actually the
322.BR futex ()
323.I timeout
324argument (ab)used to specify how many of the waiters on the futex at
325.IR uaddr2
326are to be woken up;
327the kernel casts the
328.I timeout
329value to
330.IR u32 .
331
332The operation and comparison that are to be performed are encoded
333in the bits of the argument
334.IR val3 .
335Pictorially, the encoding is:
336
337.in +4n
338.nf
339 +-----+-----+---------------+---------------+
340 | op | cmp | oparg | cmparg |
341 +-----+-----+---------------+---------------+
342# of bits: 4 4 12 12
343
344.fi
345.in
346
347Expressed in code, the encoding is:
348
349.in +4n
350.nf
351#define FUTEX_OP(op, oparg, cmp, cmparg) \\
352 (((op & 0xf) << 28) | \\
353 ((cmp & 0xf) << 24) | \\
354 ((oparg & 0xfff) << 12) | \\
355 (cmparg & 0xfff))
356.fi
357.in
358
359In the above,
360.I op
361and
362.I cmp
363are each one of the codes listed below.
364The
365.I oparg
366and
367.I cmparg
368components are literal numeric values, except as noted below.
369
370The
371.I op
372component has one of the following values:
373
374.in +4n
375.nf
376FUTEX_OP_SET 0 /* uaddr2 = oparg; */
377FUTEX_OP_ADD 1 /* uaddr2 += oparg; */
378FUTEX_OP_OR 2 /* uaddr2 |= oparg; */
379FUTEX_OP_ANDN 3 /* uaddr2 &= ~oparg; */
380FUTEX_OP_XOR 4 /* uaddr2 ^= oparg; */
381.fi
382.in
383
384In addition, bit-wise ORing the following value into
385.I op
386causes
387.IR "(1\ <<\ oparg)"
388to be used as the operand:
389
390.in +4n
391.nf
392FUTEX_OP_ARG_SHIFT 8 /* Use (1 << oparg) as operand */
393.fi
394.in
395
396The
397.I cmp
398field is one of the following:
399
400.in +4n
401.nf
402FUTEX_OP_CMP_EQ 0 /* if (oldval == cmparg) wake */
403FUTEX_OP_CMP_NE 1 /* if (oldval != cmparg) wake */
404FUTEX_OP_CMP_LT 2 /* if (oldval < cmparg) wake */
405FUTEX_OP_CMP_LE 3 /* if (oldval <= cmparg) wake */
406FUTEX_OP_CMP_GT 4 /* if (oldval > cmparg) wake */
407FUTEX_OP_CMP_GE 5 /* if (oldval >= cmparg) wake */
408.fi
409.in
410
411The return value of
412.BR FUTEX_WAKE_OP
413is the sum of the number of waiters woken on the futex
414.IR uaddr
415plus the number of waiters woken on the futex
416.IR uaddr2 .
d67e21f5 417.TP
79c9b436
TG
418.BR FUTEX_WAIT_BITSET " (since Linux 2.6.25)"
419.\" commit cd689985cf49f6ff5c8eddc48d98b9d581d9475d
fd9e59d4 420This operation is like
79c9b436
TG
421.BR FUTEX_WAIT
422except that
423.I val3
424is used to provide a 32-bit bitset to the kernel.
425This bitset is stored in the kernel-internal state of the waiter.
426See the description of
427.BR FUTEX_WAKE_BITSET
428for further details.
429
fd9e59d4
MK
430The
431.BR FUTEX_WAIT_BITSET
432also interprets the
433.I timeout
434argument differently from
435.BR FUTEX_WAIT .
436See the discussion of
437.BR FUTEX_CLOCK_REALTIME ,
438above.
439
79c9b436
TG
440The
441.I uaddr2
442argument is ignored.
443.TP
d67e21f5
MK
444.BR FUTEX_WAKE_BITSET " (since Linux 2.6.25)"
445.\" commit cd689985cf49f6ff5c8eddc48d98b9d581d9475d
55cc422d
TG
446This operation is the same as
447.BR FUTEX_WAKE
448except that the
449.I val3
450argument is used to provide a 32-bit bitset to the kernel.
98d769c0
MK
451This bitset is used to select which waiters should be woken up.
452The selection is done by a bit-wise AND of the "wake" bitset
453(i.e., the value in
454.IR val3 )
455and the bitset which is stored in the kernel-internal
e9d4496b 456state of the waiter (the "wait" biset that is set using
98d769c0
MK
457.BR FUTEX_WAIT_BITSET ).
458All of the waiters for which the result of the AND is nonzero are woken up;
459the remaining waiters are left sleeping.
460
e9d4496b
MK
461.\" FIXME please review this paragraph that I added
462The effect of
463.BR FUTEX_WAIT_BITSET
464and
465.BR FUTEX_WAKE_BITSET
466is to allow selective wake-ups among multiple waiters that are waiting
467on the same futex;
468since a futex has a size of 32 bits,
469these operations provide 32 wakeup "channels".
470(The
471.BR FUTEX_WAIT
472and
473.BR FUTEX_WAKE
474operations correspond to
475.BR FUTEX_WAIT_BITSET
476and
477.BR FUTEX_WAKE_BITSET
478operations where the bitsets are all ones.)
63d3f911 479Note, however, that using this bitset multiplexing featurefeature on a
e9d4496b
MK
480futex is less efficient than simply using multiple futexes,
481because employing bitset multiplexing requires the kernel
482to check all waiters on a futex,
483including those that are not interested in being woken up
484(i.e., they do not have the relevant bit set in their "wait" bitset).
485.\" According to http://locklessinc.com/articles/futex_cheat_sheet/:
486.\"
487.\" "The original reason for the addition of these extensions
488.\" was to improve the performance of pthread read-write locks
489.\" in glibc. However, the pthreads library no longer uses the
490.\" same locking algorithm, and these extensions are not used
491.\" without the bitset parameter being all ones.
492.\"
493.\" The page goes on to note that the FUTEX_WAIT_BITSET operation
494.\" is nevertheless used (with a bitset of all ones) in order to
495.\" obtain the absolute timeout functionality that is useful
496.\" for efficiently implementing Pthreads APIs (which use absolute
497.\" timeouts); FUTEX_WAIT provides only relative timeouts.
498
98d769c0
MK
499The
500.I uaddr2
501and
502.I timeout
503arguments are ignored.
d67e21f5
MK
504.TP
505.BR FUTEX_LOCK_PI " (since Linux 2.6.18)"
506.\" commit c87e2837be82df479a6bae9f155c43516d2feebc
507.\" FIXME to complete
6b060884
MK
508.\"
509.\" FIXME Employs 'timeout' argument
510.\"
d67e21f5
MK
511[As yet undocumented]
512.TP
513.BR FUTEX_UNLOCK_PI " (since Linux 2.6.18)"
514.\" commit c87e2837be82df479a6bae9f155c43516d2feebc
515.\" FIXME to complete
516[As yet undocumented]
517.TP
518.BR FUTEX_TRYLOCK_PI " (since Linux 2.6.18)"
519.\" commit c87e2837be82df479a6bae9f155c43516d2feebc
520.\" FIXME to complete
521[As yet undocumented]
522.TP
81c9d87e
MK
523.BR FUTEX_FD " (from Linux 2.6.0 up to and including Linux 2.6.25)"
524.\" Strictly speaking, from Linux 2.5.x to 2.6.25
ff597681
MK
525This operation creates a file descriptor that is associated with the futex at
526.IR uaddr .
fea681da 527.\" , suitable for .BR poll (2).
ff597681
MK
528The calling process must close the returned file descriptor after use.
529When another process performs a
530.BR FUTEX_WAKE
531on the futex, the file descriptor indicates as being readable with
532.BR select (2),
533.BR poll (2),
534and
535.BR epoll (7)
536
537The file descriptor can be used to obtain asynchronous notifications:
538if
539.I val
540is nonzero, then when another process executes a
682edefb 541.BR FUTEX_WAKE ,
ff597681 542the caller will receive the signal number that was passed in
fea681da 543.IR val .
4798a7f3 544
fea681da
MK
545The arguments
546.IR timeout ,
547.I uaddr2
548and
549.I val3
550are ignored.
551
c13182ef 552To prevent race conditions, the caller should test if the futex has
682edefb
MK
553been upped after
554.B FUTEX_FD
555returns.
266a5e91 556
da36351e 557Because it was inherently racy,
682edefb 558.B FUTEX_FD
75bc6c11
MK
559has been removed
560.\" commit 82af7aca56c67061420d618cc5a30f0fd4106b80
561from Linux 2.6.26 onward.
fea681da 562.TP
81c9d87e
MK
563.BR FUTEX_REQUEUE " (since Linux 2.6.0)"
564.\" Strictly speaking: from Linux 2.5.70
4ac63a6c
MK
565.\"
566.\" FIXME I added this warning. Okay?
567.IR "Avoid using this operation" .
dd05d612 568It is broken (unavoidably racy) for its intended purpose.
4ac63a6c
MK
569Use
570.BR FUTEX_CMP_REQUEUE
571instead.
572
dd05d612
MK
573This operation performs the same task as
574.BR FUTEX_CMP_REQUEUE ,
575except that no check is made using the value in
576.IR val3 .
577(The argument
fea681da 578.I val3
dd05d612 579is ignored.)
fea681da
MK
580.TP
581.BR FUTEX_CMP_REQUEUE " (since Linux 2.6.7)"
3dfcc11d 582This operation was added as a replacement for the earlier
682edefb 583.BR FUTEX_REQUEUE ,
3dfcc11d
MK
584because that operation was racy for its intended use.
585
586As with
682edefb 587.BR FUTEX_REQUEUE ,
3dfcc11d
MK
588the
589.BR FUTEX_CMP_REQUEUE
590operation is used to avoid a "thundering herd" effect when
591.B FUTEX_WAKE
592is used and all processes woken up need to acquire another futex.
593It differs from
594.BR FUTEX_REQUEUE
595in that it first checks whether the location
fea681da
MK
596.I uaddr
597still contains the value
598.IR val3 .
e808bba0
MK
599If not, the operation fails with the error
600.BR EAGAIN .
3dfcc11d
MK
601.\" FIXME I added the following sentence on rational for FUTEX_CMP_REQUEUE.
602.\" Is it correct? SHould it be expanded?
603This additional feature of
604.BR FUTEX_CMP_REQUEUE
605can be used by the caller to (atomically) detect changes
606in the value of the target futex at
607.IR uaddr2 .
4798a7f3 608
3dfcc11d
MK
609The operation wakes up a maximum of
610.I val
611waiters that are waiting on the futex at
612.IR uaddr .
613If there are more than
614.I val
615waiters, then the remaining waiters are removed
616from the wait queue of the source futex at
617.I uaddr
618and added to the wait queue of the target futex at
619.IR uaddr2 .
620The
621.I timeout
622argument is (ab)used to specify a cap on the number of waiters
623that are requeued to the futex at
624.IR uaddr2 ;
625the kernel casts the
fea681da 626.I timeout
3dfcc11d
MK
627value to
628.IR u32 .
629
630.\" FIXME Please review the following new paragraph to see if it is
631.\" accurate.
632Typical values to specify for
633.I val
634are 0 or or 1.
635(Specifying
636.BR INT_MAX
637is not useful, because it would make the
638.BR FUTEX_CMP_REQUEUE
639operation equivalent to
640.BR FUTEX_WAKE .)
641The cap value specified via the (abused)
642.I timeout
643argument is typically either 1 or
644.BR INT_MAX .
645(Specifying the argument as 0 is not useful, because it would make the
646.BR FUTEX_CMP_REQUEUE
647operation equivalent to
648.BR FUTEX_WAIT .)
d67e21f5
MK
649.TP
650.BR FUTEX_CMP_REQUEUE_PI " (since Linux 2.6.31)"
651.\" commit 52400ba946759af28442dee6265c5c0180ac7122
652.\" FIXME to complete
653[As yet undocumented]
654.TP
655.BR FUTEX_WAIT_REQUEUE_PI " (since Linux 2.6.31)"
656.\" commit 52400ba946759af28442dee6265c5c0180ac7122
657.\" FIXME to complete
658[As yet undocumented]
47297adb 659.SH RETURN VALUE
fea681da 660.PP
e808bba0
MK
661In the event of an error, all operations return \-1, and set
662.I errno
663to indicate the error.
664The return value on success depends on the operation,
665as described in the following list:
fea681da
MK
666.TP
667.B FUTEX_WAIT
682edefb
MK
668Returns 0 if the process was woken by a
669.B FUTEX_WAKE
670call.
e808bba0 671See ERRORS for the various possible error returns.
fea681da
MK
672.TP
673.B FUTEX_WAKE
674Returns the number of processes woken up.
675.TP
676.B FUTEX_FD
677Returns the new file descriptor associated with the futex.
678.TP
679.B FUTEX_REQUEUE
680Returns the number of processes woken up.
681.TP
682.B FUTEX_CMP_REQUEUE
3dfcc11d
MK
683Returns the total number of processes woken up or requeued to the futex at
684.IR uaddr2 .
685If this value is greater than
686.IR val ,
687then difference is the number of waiters requeued to the futex at
688.IR uaddr2 .
519f2c3d
MK
689.\"
690.\" FIXME Add success returns for other operations
fea681da
MK
691.SH ERRORS
692.TP
693.B EACCES
694No read access to futex memory.
695.TP
696.B EAGAIN
682edefb 697.B FUTEX_CMP_REQUEUE
e808bba0 698detected that the value pointed to by
9f6c40c0
МК
699.I uaddr
700is not equal to the expected value
701.IR val3 .
fd1dc4c2 702.\" FIXME: Is the following sentence correct?
fea681da 703(This probably indicates a race;
682edefb
MK
704use the safe
705.B FUTEX_WAKE
706now.)
fea681da
MK
707.TP
708.B EFAULT
1ea901e8
MK
709A required pointer argument (i.e.,
710.IR uaddr ,
711.IR uaddr2 ,
712or
713.IR timeout )
496df304 714did not point to a valid user-space address.
fea681da 715.TP
9f6c40c0 716.B EINTR
e808bba0 717A
9f6c40c0 718.B FUTEX_WAIT
2674f781
MK
719or
720.B FUTEX_WAIT_BITSET
e808bba0
MK
721operation was interrupted by a signal (see
722.BR signal (7))
723or a spurious wakeup.
9f6c40c0 724.TP
fea681da 725.B EINVAL
180f97b7
MK
726The operation in
727.IR futex_op
728is one of those that employs a timeout, but the supplied
fb2f4c27
MK
729.I timeout
730argument was invalid
731.RI ( tv_sec
732was less than zero, or
733.IR tv_nsec
734was not less than 1000,000,000).
735.TP
736.B EINVAL
0c74df0b
MK
737The operation specified in
738.BR futex_op
739employs one or both of the pointers
51ee94be 740.I uaddr
a1f47699 741and
0c74df0b
MK
742.IR uaddr2 ,
743but one of these does not point to a valid object\(emthat is,
744the address is not four-byte-aligned.
51ee94be
MK
745.TP
746.B EINVAL
bae14b6c 747.RB ( FUTEX_WAKE ,
5447735d 748.BR FUTEX_WAKE_OP ,
98d769c0 749.BR FUTEX_WAKE_BITSET ,
e169277f
MK
750.BR FUTEX_REQUEUE ,
751.BR FUTEX_CMP_REQUEUE )
496df304 752The kernel detected an inconsistency between the user-space state at
9534086b
TG
753.I uaddr
754and the kernel state\(emthat is, it detected a waiter which waits in
5447735d
MK
755.BR FUTEX_LOCK_PI
756on
757.IR uaddr .
9534086b
TG
758.TP
759.B EINVAL
55cc422d
TG
760.RB ( FUTEX_WAIT_BITSET ,
761.BR FUTEX_WAKE_BITSET )
79c9b436
TG
762The bitset supplied in
763.IR val3
764is zero.
765.TP
766.B EINVAL
add875c0
MK
767.RB ( FUTEX_REQUEUE )
768.\" FIXME tglx suggested adding this, but does this error really
769.\" occur for FUTEX_REQUEUE?
770.I uaddr
771equals
772.IR uaddr2
773(i.e., an attempt was made to requeue to the same futex).
774.TP
ff597681
MK
775.BR EINVAL
776.RB ( FUTEX_FD )
777The signal number supplied in
778.I val
779is invalid.
780.TP
6bac3b85 781.B EINVAL
4832b48a 782Invalid argument.
fea681da
MK
783.TP
784.B ENFILE
ff597681 785.RB ( FUTEX_FD )
fea681da 786The system limit on the total number of open files has been reached.
4701fc28
MK
787.TP
788.B ENOSYS
789Invalid operation specified in
d33602c4 790.IR futex_op .
9f6c40c0 791.TP
4a7e5b05
MK
792.B ENOSYS
793The
794.BR FUTEX_CLOCK_REALTIME
795option was specified in
1afcee7c 796.IR futex_op ,
4a7e5b05
MK
797but the accompanying operation was neither
798.BR FUTEX_WAIT_BITSET
799nor
800.BR FUTEX_WAIT_REQUEUE_PI .
801.TP
9f6c40c0 802.B ETIMEDOUT
4d85047f
MK
803The operation in
804.IR futex_op
805employed the timeout specified in
806.IR timeout ,
807and the timeout expired before the operation completed.
9f6c40c0
МК
808.TP
809.B EWOULDBLOCK
0582b19d
MK
810.RB ( FUTEX_WAIT )
811The value pointed to by
9f6c40c0
МК
812.I uaddr
813was not equal to the expected value
814.I val
e808bba0 815at the time of the call.
47297adb 816.SH VERSIONS
a1d5f77c 817.PP
81c9d87e
MK
818Futexes were first made available in a stable kernel release
819with Linux 2.6.0.
820
a1d5f77c
MK
821Initial futex support was merged in Linux 2.5.7 but with different semantics
822from what was described above.
52dee70e 823A four-argument system call with the semantics
fd3fa7ef 824described in this page was introduced in Linux 2.5.40.
11b520ed 825In Linux 2.5.70, one argument
a1d5f77c 826was added.
11b520ed 827In Linux 2.6.7, a sixth argument was added\(emmessy, especially
a1d5f77c 828on the s390 architecture.
47297adb 829.SH CONFORMING TO
8382f16d 830This system call is Linux-specific.
47297adb 831.SH NOTES
fea681da 832.PP
fcdad7d6 833To reiterate, bare futexes are not intended as an easy-to-use abstraction
c13182ef 834for end-users.
fcdad7d6 835(There is no wrapper function for this system call in glibc.)
c13182ef 836Implementors are expected to be assembly literate and to have
7fac88a9 837read the sources of the futex user-space library referenced below.
d282bb24 838.\" .SH AUTHORS
fea681da
MK
839.\" .PP
840.\" Futexes were designed and worked on by
841.\" Hubertus Franke (IBM Thomas J. Watson Research Center),
842.\" Matthew Kirkwood, Ingo Molnar (Red Hat)
843.\" and Rusty Russell (IBM Linux Technology Center).
844.\" This page written by bert hubert.
47297adb 845.SH SEE ALSO
d806bc05 846.BR restart_syscall (2),
14d8dd3b 847.BR futex (7)
fea681da 848.PP
52087dd3 849\fIFuss, Futexes and Furwocks: Fast Userlevel Locking in Linux\fP
9b936e9e
MK
850(proceedings of the Ottawa Linux Symposium 2002), online at
851.br
608bf950
SK
852.UR http://kernel.org\:/doc\:/ols\:/2002\:/ols2002-pages-479-495.pdf
853.UE
f42eb21b
MK
854
855\fIFutexes Are Tricky\fP (updated in 2011), Ulrich Drepper
856.UR http://www.akkadia.org/drepper/futex.pdf
857.UE
9b936e9e
MK
858.PP
859Futex example library, futex-*.tar.bz2 at
860.br
a605264d 861.UR ftp://ftp.kernel.org\:/pub\:/linux\:/kernel\:/people\:/rusty/
608bf950 862.UE