]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/futex.2
futex.2: Reorder operations list more or less chronologically
[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).
a7c2bf45
MK
261.TP
262.BR FUTEX_FD " (from Linux 2.6.0 up to and including Linux 2.6.25)"
263.\" Strictly speaking, from Linux 2.5.x to 2.6.25
264This operation creates a file descriptor that is associated with the futex at
265.IR uaddr .
266.\" , suitable for .BR poll (2).
267The calling process must close the returned file descriptor after use.
268When another process performs a
269.BR FUTEX_WAKE
270on the futex, the file descriptor indicates as being readable with
271.BR select (2),
272.BR poll (2),
273and
274.BR epoll (7)
275
276The file descriptor can be used to obtain asynchronous notifications:
277if
278.I val
279is nonzero, then when another process executes a
280.BR FUTEX_WAKE ,
281the caller will receive the signal number that was passed in
282.IR val .
283
284The arguments
285.IR timeout ,
286.I uaddr2
287and
288.I val3
289are ignored.
290
291To prevent race conditions, the caller should test if the futex has
292been upped after
293.B FUTEX_FD
294returns.
295
296Because it was inherently racy,
297.B FUTEX_FD
298has been removed
299.\" commit 82af7aca56c67061420d618cc5a30f0fd4106b80
300from Linux 2.6.26 onward.
301.TP
302.BR FUTEX_REQUEUE " (since Linux 2.6.0)"
303.\" Strictly speaking: from Linux 2.5.70
304.\"
305.\" FIXME I added this warning. Okay?
306.IR "Avoid using this operation" .
307It is broken (unavoidably racy) for its intended purpose.
308Use
309.BR FUTEX_CMP_REQUEUE
310instead.
311
312This operation performs the same task as
313.BR FUTEX_CMP_REQUEUE ,
314except that no check is made using the value in
315.IR val3 .
316(The argument
317.I val3
318is ignored.)
319.TP
320.BR FUTEX_CMP_REQUEUE " (since Linux 2.6.7)"
321This operation was added as a replacement for the earlier
322.BR FUTEX_REQUEUE ,
323because that operation was racy for its intended use.
324
325As with
326.BR FUTEX_REQUEUE ,
327the
328.BR FUTEX_CMP_REQUEUE
329operation is used to avoid a "thundering herd" effect when
330.B FUTEX_WAKE
331is used and all processes woken up need to acquire another futex.
332It differs from
333.BR FUTEX_REQUEUE
334in that it first checks whether the location
335.I uaddr
336still contains the value
337.IR val3 .
338If not, the operation fails with the error
339.BR EAGAIN .
340.\" FIXME I added the following sentence on rational for FUTEX_CMP_REQUEUE.
341.\" Is it correct? SHould it be expanded?
342This additional feature of
343.BR FUTEX_CMP_REQUEUE
344can be used by the caller to (atomically) detect changes
345in the value of the target futex at
346.IR uaddr2 .
347
348The operation wakes up a maximum of
349.I val
350waiters that are waiting on the futex at
351.IR uaddr .
352If there are more than
353.I val
354waiters, then the remaining waiters are removed
355from the wait queue of the source futex at
356.I uaddr
357and added to the wait queue of the target futex at
358.IR uaddr2 .
359The
360.I timeout
361argument is (ab)used to specify a cap on the number of waiters
362that are requeued to the futex at
363.IR uaddr2 ;
364the kernel casts the
365.I timeout
366value to
367.IR u32 .
368
369.\" FIXME Please review the following new paragraph to see if it is
370.\" accurate.
371Typical values to specify for
372.I val
373are 0 or or 1.
374(Specifying
375.BR INT_MAX
376is not useful, because it would make the
377.BR FUTEX_CMP_REQUEUE
378operation equivalent to
379.BR FUTEX_WAKE .)
380The cap value specified via the (abused)
381.I timeout
382argument is typically either 1 or
383.BR INT_MAX .
384(Specifying the argument as 0 is not useful, because it would make the
385.BR FUTEX_CMP_REQUEUE
386operation equivalent to
387.BR FUTEX_WAIT .)
6bac3b85
MK
388.\"
389.\" FIXME I added some FUTEX_WAKE_OP text, and I'd be happy if someone
390.\" checked it.
fea681da 391.TP
d67e21f5
MK
392.BR FUTEX_WAKE_OP " (since Linux 2.6.14)"
393.\" commit 4732efbeb997189d9f9b04708dc26bf8613ed721
6bac3b85
MK
394.\" Author: Jakub Jelinek <jakub@redhat.com>
395.\" Date: Tue Sep 6 15:16:25 2005 -0700
396This operation was added to support some user-space use cases
397where more than one futex must be handled at the same time.
398The most notable example is the implementation of
399.BR pthread_cond_signal (3),
400which requires operations on two futexes,
401the one used to implement the mutex and the one used in the implementation
402of the wait queue associated with the condition variable.
403.BR FUTEX_WAKE_OP
404allows such cases to be implemented without leading to
405high rates of contention and context switching.
406
407The
408.BR FUTEX_WAIT_OP
409operation is equivalent to atomically executing the following code:
410
411.in +4n
412.nf
413int oldval = *(int *) uaddr2;
414*(int *) uaddr2 = oldval \fIop\fP \fIoparg\fP;
415futex(uaddr, FUTEX_WAKE, val, 0, 0, 0);
416if (oldval \fIcmp\fP \fIcmparg\fP)
417 futex(uaddr2, FUTEX_WAKE, nr_wake2, 0, 0, 0);
418.fi
419.in
420
421In other words,
422.BR FUTEX_WAIT_OP
423does the following:
424.RS
425.IP * 3
426saves the original value of the futex at
427.IR uaddr2 ;
428.IP *
429performs an operation to modify the value of the futex at
430.IR uaddr2 ;
431.IP *
432wakes up a maximum of
433.I val
434waiters on the futex
435.IR uaddr ;
436and
437.IP *
438dependent on the results of a test of the original value of the futex at
439.IR uaddr2 ,
440wakes up a maximum of
441.I nr_wake2
442waiters on the futex
443.IR uaddr2 .
444.RE
445.IP
446The
447.I nr_wake2
448value is actually the
449.BR futex ()
450.I timeout
451argument (ab)used to specify how many of the waiters on the futex at
452.IR uaddr2
453are to be woken up;
454the kernel casts the
455.I timeout
456value to
457.IR u32 .
458
459The operation and comparison that are to be performed are encoded
460in the bits of the argument
461.IR val3 .
462Pictorially, the encoding is:
463
464.in +4n
465.nf
466 +-----+-----+---------------+---------------+
467 | op | cmp | oparg | cmparg |
468 +-----+-----+---------------+---------------+
469# of bits: 4 4 12 12
470
471.fi
472.in
473
474Expressed in code, the encoding is:
475
476.in +4n
477.nf
478#define FUTEX_OP(op, oparg, cmp, cmparg) \\
479 (((op & 0xf) << 28) | \\
480 ((cmp & 0xf) << 24) | \\
481 ((oparg & 0xfff) << 12) | \\
482 (cmparg & 0xfff))
483.fi
484.in
485
486In the above,
487.I op
488and
489.I cmp
490are each one of the codes listed below.
491The
492.I oparg
493and
494.I cmparg
495components are literal numeric values, except as noted below.
496
497The
498.I op
499component has one of the following values:
500
501.in +4n
502.nf
503FUTEX_OP_SET 0 /* uaddr2 = oparg; */
504FUTEX_OP_ADD 1 /* uaddr2 += oparg; */
505FUTEX_OP_OR 2 /* uaddr2 |= oparg; */
506FUTEX_OP_ANDN 3 /* uaddr2 &= ~oparg; */
507FUTEX_OP_XOR 4 /* uaddr2 ^= oparg; */
508.fi
509.in
510
511In addition, bit-wise ORing the following value into
512.I op
513causes
514.IR "(1\ <<\ oparg)"
515to be used as the operand:
516
517.in +4n
518.nf
519FUTEX_OP_ARG_SHIFT 8 /* Use (1 << oparg) as operand */
520.fi
521.in
522
523The
524.I cmp
525field is one of the following:
526
527.in +4n
528.nf
529FUTEX_OP_CMP_EQ 0 /* if (oldval == cmparg) wake */
530FUTEX_OP_CMP_NE 1 /* if (oldval != cmparg) wake */
531FUTEX_OP_CMP_LT 2 /* if (oldval < cmparg) wake */
532FUTEX_OP_CMP_LE 3 /* if (oldval <= cmparg) wake */
533FUTEX_OP_CMP_GT 4 /* if (oldval > cmparg) wake */
534FUTEX_OP_CMP_GE 5 /* if (oldval >= cmparg) wake */
535.fi
536.in
537
538The return value of
539.BR FUTEX_WAKE_OP
540is the sum of the number of waiters woken on the futex
541.IR uaddr
542plus the number of waiters woken on the futex
543.IR uaddr2 .
d67e21f5 544.TP
79c9b436
TG
545.BR FUTEX_WAIT_BITSET " (since Linux 2.6.25)"
546.\" commit cd689985cf49f6ff5c8eddc48d98b9d581d9475d
fd9e59d4 547This operation is like
79c9b436
TG
548.BR FUTEX_WAIT
549except that
550.I val3
551is used to provide a 32-bit bitset to the kernel.
552This bitset is stored in the kernel-internal state of the waiter.
553See the description of
554.BR FUTEX_WAKE_BITSET
555for further details.
556
fd9e59d4
MK
557The
558.BR FUTEX_WAIT_BITSET
559also interprets the
560.I timeout
561argument differently from
562.BR FUTEX_WAIT .
563See the discussion of
564.BR FUTEX_CLOCK_REALTIME ,
565above.
566
79c9b436
TG
567The
568.I uaddr2
569argument is ignored.
570.TP
d67e21f5
MK
571.BR FUTEX_WAKE_BITSET " (since Linux 2.6.25)"
572.\" commit cd689985cf49f6ff5c8eddc48d98b9d581d9475d
55cc422d
TG
573This operation is the same as
574.BR FUTEX_WAKE
575except that the
576.I val3
577argument is used to provide a 32-bit bitset to the kernel.
98d769c0
MK
578This bitset is used to select which waiters should be woken up.
579The selection is done by a bit-wise AND of the "wake" bitset
580(i.e., the value in
581.IR val3 )
582and the bitset which is stored in the kernel-internal
e9d4496b 583state of the waiter (the "wait" biset that is set using
98d769c0
MK
584.BR FUTEX_WAIT_BITSET ).
585All of the waiters for which the result of the AND is nonzero are woken up;
586the remaining waiters are left sleeping.
587
e9d4496b
MK
588.\" FIXME please review this paragraph that I added
589The effect of
590.BR FUTEX_WAIT_BITSET
591and
592.BR FUTEX_WAKE_BITSET
593is to allow selective wake-ups among multiple waiters that are waiting
594on the same futex;
595since a futex has a size of 32 bits,
596these operations provide 32 wakeup "channels".
597(The
598.BR FUTEX_WAIT
599and
600.BR FUTEX_WAKE
601operations correspond to
602.BR FUTEX_WAIT_BITSET
603and
604.BR FUTEX_WAKE_BITSET
605operations where the bitsets are all ones.)
63d3f911 606Note, however, that using this bitset multiplexing featurefeature on a
e9d4496b
MK
607futex is less efficient than simply using multiple futexes,
608because employing bitset multiplexing requires the kernel
609to check all waiters on a futex,
610including those that are not interested in being woken up
611(i.e., they do not have the relevant bit set in their "wait" bitset).
612.\" According to http://locklessinc.com/articles/futex_cheat_sheet/:
613.\"
614.\" "The original reason for the addition of these extensions
615.\" was to improve the performance of pthread read-write locks
616.\" in glibc. However, the pthreads library no longer uses the
617.\" same locking algorithm, and these extensions are not used
618.\" without the bitset parameter being all ones.
619.\"
620.\" The page goes on to note that the FUTEX_WAIT_BITSET operation
621.\" is nevertheless used (with a bitset of all ones) in order to
622.\" obtain the absolute timeout functionality that is useful
623.\" for efficiently implementing Pthreads APIs (which use absolute
624.\" timeouts); FUTEX_WAIT provides only relative timeouts.
625
98d769c0
MK
626The
627.I uaddr2
628and
629.I timeout
630arguments are ignored.
d67e21f5
MK
631.TP
632.BR FUTEX_LOCK_PI " (since Linux 2.6.18)"
633.\" commit c87e2837be82df479a6bae9f155c43516d2feebc
634.\" FIXME to complete
6b060884
MK
635.\"
636.\" FIXME Employs 'timeout' argument
637.\"
d67e21f5
MK
638[As yet undocumented]
639.TP
640.BR FUTEX_UNLOCK_PI " (since Linux 2.6.18)"
641.\" commit c87e2837be82df479a6bae9f155c43516d2feebc
642.\" FIXME to complete
643[As yet undocumented]
644.TP
645.BR FUTEX_TRYLOCK_PI " (since Linux 2.6.18)"
646.\" commit c87e2837be82df479a6bae9f155c43516d2feebc
647.\" FIXME to complete
648[As yet undocumented]
649.TP
d67e21f5
MK
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