]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/futex.2
futex.2: Minor wording fixes
[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.\"
47f5c4ba
MK
15.\" FIXME Still to integrate are some points from Torvald Riegel's mail of
16.\" 2015-01-23:
17.\" http://thread.gmane.org/gmane.linux.kernel/1703405/focus=7977
18.\"
78e85692 19.\" FIXME Do we need to add some text regarding Torvald Riegel's 2015-01-24 mail
02182e7c
MK
20.\" at http://thread.gmane.org/gmane.linux.kernel/1703405/focus=1873242
21.\"
3d155313 22.TH FUTEX 2 2014-05-21 "Linux" "Linux Programmer's Manual"
fea681da 23.SH NAME
ce154705 24futex \- fast user-space locking
fea681da 25.SH SYNOPSIS
9d9dc1e8 26.nf
fea681da
MK
27.sp
28.B "#include <linux/futex.h>"
fea681da
MK
29.B "#include <sys/time.h>"
30.sp
d33602c4 31.BI "int futex(int *" uaddr ", int " futex_op ", int " val ,
768d3c23 32.BI " const struct timespec *" timeout , \
c6dc40a2 33" \fR /* or: \fBuint32_t \fIval2\fP */
9d9dc1e8 34.BI " int *" uaddr2 ", int " val3 );
9d9dc1e8 35.fi
409f08b0 36
b939d6e4
MK
37.IR Note :
38There is no glibc wrapper for this system call; see NOTES.
47297adb 39.SH DESCRIPTION
fea681da
MK
40.PP
41The
e511ffb6 42.BR futex ()
4b35dc5d 43system call provides a method for waiting until a certain condition becomes
077981d4
MK
44true.
45It is typically used as a blocking construct in the context of
d45f244c
MK
46shared-memory synchronization.
47When using futexes, the majority of
48the synchronization operations are performed in user space.
bc54ed38 49A user-space program employs the
d45f244c 50.BR futex ()
ca4e5b2b 51system call only when it is likely that the program has to block for
4c8cb0ff 52a longer time until the condition becomes true.
bc54ed38 53Other
d45f244c 54.BR futex ()
bc54ed38
MK
55operations can be used to wake any processes or threads waiting
56for a particular condition.
4b35dc5d 57
7e8dcabc
MK
58A futex is a 32-bit value\(emreferred to below as a
59.IR "futex word" \(emwhose
60address is supplied to the
4b35dc5d 61.BR futex ()
7e8dcabc 62system call.
c3f4c019 63(Futexes are 32 bits in size on all platforms, including 64-bit systems.)
7e8dcabc
MK
64All futex operations are governed by this value.
65In order to share a futex between processes,
66the futex is placed in a region of shared memory,
67created using (for example)
68.BR mmap (2)
69or
70.BR shmat (2).
c3f4c019 71(Thus, the futex word may have different
7e8dcabc
MK
72virtual addresses in different processes,
73but these addresses all refer to the same location in physical memory.)
ca4e5b2b
MK
74In a multithreaded program, it is sufficient to place the futex word
75in a global variable shared by all threads.
809ca3ae 76
0c3ec26b
MK
77When executing a futex operation that requests to block a thread,
78the kernel will block only if the futex word has the value that the
55f9e85e
MK
79calling thread supplied (as one of the arguments of the
80.BR futex ()
81call) as the expected value of the futex word.
9d32a39b
MK
82The loading of the futex word's value,
83the comparison of that value with the expected value,
bc54ed38 84and the actual blocking will happen atomically and will be totally ordered
da894b18 85with respect to concurrent operations performed by other threads
0fb87d16 86on the same futex word.
da894b18
MK
87.\" Notes from Darren Hart (Dec 2015):
88.\" Totally ordered with respect futex operations refers to semantics
89.\" of the ACQUIRE/RELEASE operations and how they impact ordering of
90.\" memory reads and writes. The kernel futex operations are protected
91.\" by spinlocks, which ensure that that all operations are serialized
92.\" with respect to one another.
93.\"
94.\" This is a lot to attempt to define in this document. Perhaps a
95.\" reference to linux/Documentation/memory-barriers.txt as a footnote
96.\" would be sufficient? Or perhaps for this manual, "serialized" would
97.\" be sufficient, with a footnote regarding "totally ordered" and a
98.\" pointer to the memory-barrier documentation?
b80daba2 99Thus, the futex word is used to connect the synchronization in user space
9d32a39b 100with the implementation of blocking by the kernel.
55f9e85e 101Analogously to an atomic
4b35dc5d 102compare-and-exchange operation that potentially changes shared memory,
077981d4 103blocking via a futex is an atomic compare-and-block operation.
d6bb5a38
MK
104.\" FIXME(Torvald Riegel):
105.\" Eventually we want to have some text in NOTES to satisfy
106.\" the reference in the following sentence
107.\" See NOTES for
108.\" a detailed specification of the synchronization semantics.
4b35dc5d 109
ca4e5b2b 110One use of futexes is for implementing locks.
c0dc758e
MK
111The state of the lock (i.e., acquired or not acquired)
112can be represented as an atomically accessed flag in shared memory.
4c8cb0ff 113In the uncontended case,
c3f4c019 114a thread can access or modify the lock state with atomic instructions,
4c8cb0ff
MK
115for example atomically changing it from not acquired to acquired
116using an atomic compare-and-exchange instruction.
55f9e85e
MK
117(Such instructions are performed entirely in user mode,
118and the kernel maintains no information about the lock state.)
119On the other hand, a thread may be unable to acquire a lock because
8e754e12 120it is already acquired by another thread.
55f9e85e 121It then may pass the lock's flag as a futex word and the value
0c3ec26b 122representing the acquired state as the expected value to a
8e754e12
HS
123.BR futex ()
124wait operation.
55f9e85e 125This
8e754e12 126.BR futex ()
bc54ed38
MK
127operation will block if and only if the lock is still acquired
128(i.e., the value in the futex word still matches the "acquiired state").
077981d4 129When releasing the lock, a thread has to first reset the
0c3ec26b 130lock state to not acquired and then execute a futex
55f9e85e 131operation that wakes threads blocked on the lock flag used as a futex word
4c8cb0ff 132(this can be be further optimized to avoid unnecessary wake-ups).
077981d4 133See
4b35dc5d
TR
134.BR futex (7)
135for more detail on how to use futexes.
136
137Besides the basic wait and wake-up futex functionality, there are further
077981d4 138futex operations aimed at supporting more complex use cases.
ca4e5b2b
MK
139
140Note that
4c8cb0ff
MK
141no explicit initialization or destruction are necessary to use futexes;
142the kernel maintains a futex
143(i.e., the kernel-internal implementation artifact)
4b35dc5d
TR
144only while operations such as
145.BR FUTEX_WAIT ,
146described below, are being performed on a particular futex word.
a663ca5a
MK
147.\"
148.SS Arguments
fea681da
MK
149The
150.I uaddr
077981d4
MK
151argument points to the futex word.
152On all platforms, futexes are four-byte
4b35dc5d 153integers that must be aligned on a four-byte boundary.
f388ba70
MK
154The operation to perform on the futex is specified in the
155.I futex_op
156argument;
157.IR val
158is a value whose meaning and purpose depends on
159.IR futex_op .
36ab2074
MK
160
161The remaining arguments
162.RI ( timeout ,
163.IR uaddr2 ,
164and
165.IR val3 )
166are required only for certain of the futex operations described below.
167Where one of these arguments is not required, it is ignored.
768d3c23 168
36ab2074
MK
169For several blocking operations, the
170.I timeout
171argument is a pointer to a
172.IR timespec
173structure that specifies a timeout for the operation.
174However, notwithstanding the prototype shown above, for some operations,
10022b8e 175the least significant four bytes are used as an integer whose meaning
36ab2074 176is determined by the operation.
768d3c23
MK
177For these operations, the kernel casts the
178.I timeout
10022b8e
HS
179value first to
180.IR "unsigned long",
181then to
c6dc40a2 182.IR uint32_t ,
768d3c23
MK
183and in the remainder of this page, this argument is referred to as
184.I val2
185when interpreted in this fashion.
186
de5a3bb4 187Where it is required, the
36ab2074 188.IR uaddr2
4c8cb0ff
MK
189argument is a pointer to a second futex word that is employed
190by the operation.
36ab2074
MK
191The interpretation of the final integer argument,
192.IR val3 ,
193depends on the operation.
a663ca5a
MK
194.\"
195.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
196.\"
197.SS Futex operations
6be4bad7 198The
d33602c4 199.I futex_op
6be4bad7
MK
200argument consists of two parts:
201a command that specifies the operation to be performed,
202bit-wise ORed with zero or or more options that
203modify the behaviour of the operation.
fc30eb79 204The options that may be included in
d33602c4 205.I futex_op
fc30eb79
TG
206are as follows:
207.TP
208.BR FUTEX_PRIVATE_FLAG " (since Linux 2.6.22)"
209.\" commit 34f01cc1f512fa783302982776895c73714ebbc2
210This option bit can be employed with all futex operations.
e45f9735 211It tells the kernel that the futex is process-private and not shared
0c3ec26b
MK
212with another process (i.e., it is being used for synchronization
213only between threads of the same process).
943ccc52
MK
214This allows the kernel to make some additional performance optimizations.
215.\" I.e., It allows the kernel choose the fast path for validating
216.\" the user-space address and avoids expensive VMA lookups,
217.\" taking reference counts on file backing store, and so on.
ae2c1774
MK
218
219As a convenience,
220.IR <linux/futex.h>
221defines a set of constants with the suffix
222.BR _PRIVATE
223that are equivalents of all of the operations listed below,
dcdfde26 224.\" except the obsolete FUTEX_FD, for which the "private" flag was
ae2c1774
MK
225.\" meaningless
226but with the
227.BR FUTEX_PRIVATE_FLAG
228ORed into the constant value.
229Thus, there are
230.BR FUTEX_WAIT_PRIVATE ,
231.BR FUTEX_WAKE_PRIVATE ,
232and so on.
2e98bbc2
TG
233.TP
234.BR FUTEX_CLOCK_REALTIME " (since Linux 2.6.28)"
235.\" commit 1acdac104668a0834cfa267de9946fac7764d486
4a7e5b05 236This option bit can be employed only with the
2e98bbc2
TG
237.BR FUTEX_WAIT_BITSET
238and
239.BR FUTEX_WAIT_REQUEUE_PI
c84cf68c 240operations.
2e98bbc2 241
f2103b26
MK
242If this option is set, the kernel treats
243.I timeout
244as an absolute time based on
2e98bbc2
TG
245.BR CLOCK_REALTIME .
246
f2103b26
MK
247If this option is not set, the kernel treats
248.I timeout
bc54ed38 249as a relative time,
1c952cf5
MK
250measured against the
251.BR CLOCK_MONOTONIC
252clock.
6be4bad7
MK
253.PP
254The operation specified in
d33602c4 255.I futex_op
6be4bad7 256is one of the following:
70b06b90
MK
257.\"
258.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
259.\"
fea681da 260.TP
81c9d87e
MK
261.BR FUTEX_WAIT " (since Linux 2.6.0)"
262.\" Strictly speaking, since some time in 2.5.x
f065673c 263This operation tests that the value at the
4b35dc5d 264futex word pointed to by the address
fea681da 265.I uaddr
4b35dc5d 266still contains the expected value
fea681da 267.IR val ,
fd105614 268and if so, then sleeps waiting for a
682edefb 269.B FUTEX_WAKE
fd105614 270operation on the futex word.
077981d4 271The load of the value of the futex word is an atomic memory
4b35dc5d 272access (i.e., using atomic machine instructions of the respective
077981d4
MK
273architecture).
274This load, the comparison with the expected value, and
fd105614 275starting to sleep are performed atomically
da56650a
MK
276.\" FIXME: Torvald, I think we may need to add some explanation of
277.\" "totally odered" here.
fd105614
MK
278and totally ordered
279with respect to other futex operations on the same futex word.
c0dc758e
MK
280If the thread starts to sleep,
281it is considered a waiter on this futex word.
f065673c
MK
282If the futex value does not match
283.IR val ,
4710334a 284then the call fails immediately with the error
badbf70c 285.BR EAGAIN .
4b35dc5d
TR
286
287The purpose of the comparison with the expected value is to prevent lost
fd105614
MK
288wake-ups.
289If another thread changed the value of the futex word after the
c0dc758e
MK
290calling thread decided to block based on the prior value,
291and if the other thread executed a
4b35dc5d
TR
292.BR FUTEX_WAKE
293operation (or similar wake-up) after the value change and before this
f065673c 294.BR FUTEX_WAIT
bc54ed38
MK
295operation, then the calling thread will observe the
296value change and will not start to sleep.
1909e523 297
c13182ef 298If the
fea681da 299.I timeout
53ba4030 300argument is non-NULL, its contents specify a relative timeout for the wait,
1c952cf5
MK
301measured according to the
302.BR CLOCK_MONOTONIC
303clock.
82a6092b 304(This interval will be rounded up to the system clock granularity,
8297383e 305and is guaranteed not to expire early.)
82a6092b
MK
306If
307.I timeout
308is NULL, the call blocks indefinitely.
4798a7f3 309
c13182ef 310The arguments
fea681da
MK
311.I uaddr2
312and
313.I val3
314are ignored.
315
74f58a64
MK
316.\" FIXME(Torvald) I think we should remove this. Or maybe adapt to a
317.\" different example.
4b35dc5d
TR
318.\" For
319.\" .BR futex (7),
320.\" this call is executed if decrementing the count gave a negative value
321.\" (indicating contention),
322.\" and will sleep until another process or thread releases
323.\" the futex and executes the
324.\" .B FUTEX_WAKE
325.\" operation.
70b06b90
MK
326.\"
327.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
328.\"
fea681da 329.TP
81c9d87e
MK
330.BR FUTEX_WAKE " (since Linux 2.6.0)"
331.\" Strictly speaking, since Linux 2.5.x
f065673c
MK
332This operation wakes at most
333.I val
4b35dc5d 334of the waiters that are waiting (e.g., inside
f065673c 335.BR FUTEX_WAIT )
4b35dc5d 336on the futex word at the address
f065673c
MK
337.IR uaddr .
338Most commonly,
339.I val
340is specified as either 1 (wake up a single waiter) or
341.BR INT_MAX
342(wake up all waiters).
730bfbda
MK
343No guarantee is provided about which waiters are awoken
344(e.g., a waiter with a higher scheduling priority is not guaranteed
345to be awoken in preference to a waiter with a lower priority).
4798a7f3 346
fea681da
MK
347The arguments
348.IR timeout ,
c8b921bd 349.IR uaddr2 ,
fea681da
MK
350and
351.I val3
352are ignored.
353
74f58a64
MK
354.\" FIXME(Torvald) I think we should remove this. Or maybe adapt to
355.\" a different example.
4c8cb0ff
MK
356.\" For
357.\" .BR futex (7),
358.\" this is executed if incrementing the count showed that
359.\" there were waiters,
360.\" once the futex value has been set to 1
361.\" (indicating that it is available).
362.\"
363.\" FIXME How does "incrementing the count show that there were waiters"?
70b06b90
MK
364.\"
365.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
366.\"
a7c2bf45
MK
367.TP
368.BR FUTEX_FD " (from Linux 2.6.0 up to and including Linux 2.6.25)"
369.\" Strictly speaking, from Linux 2.5.x to 2.6.25
4c8cb0ff
MK
370This operation creates a file descriptor that is associated with
371the futex at
a7c2bf45 372.IR uaddr .
bdc5957a
MK
373The caller must close the returned file descriptor after use.
374When another process or thread performs a
a7c2bf45 375.BR FUTEX_WAKE
4b35dc5d 376on the futex word, the file descriptor indicates as being readable with
a7c2bf45
MK
377.BR select (2),
378.BR poll (2),
379and
380.BR epoll (7)
381
f1d2171d 382The file descriptor can be used to obtain asynchronous notifications: if
a7c2bf45 383.I val
ca4e5b2b 384is nonzero, then, when another process or thread executes a
a7c2bf45
MK
385.BR FUTEX_WAKE ,
386the caller will receive the signal number that was passed in
387.IR val .
388
389The arguments
390.IR timeout ,
391.I uaddr2
392and
393.I val3
394are ignored.
395
a7c2bf45
MK
396Because it was inherently racy,
397.B FUTEX_FD
398has been removed
399.\" commit 82af7aca56c67061420d618cc5a30f0fd4106b80
400from Linux 2.6.26 onward.
70b06b90
MK
401.\"
402.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
403.\"
a7c2bf45
MK
404.TP
405.BR FUTEX_REQUEUE " (since Linux 2.6.0)"
a7c2bf45 406This operation performs the same task as
27dd3a6e
MK
407.BR FUTEX_CMP_REQUEUE
408(see below), except that no check is made using the value in
a7c2bf45
MK
409.IR val3 .
410(The argument
411.I val3
412is ignored.)
70b06b90
MK
413.\"
414.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
415.\"
a7c2bf45
MK
416.TP
417.BR FUTEX_CMP_REQUEUE " (since Linux 2.6.7)"
4b35dc5d 418This operation first checks whether the location
a7c2bf45
MK
419.I uaddr
420still contains the value
421.IR val3 .
422If not, the operation fails with the error
423.BR EAGAIN .
4b35dc5d 424Otherwise, the operation wakes up a maximum of
a7c2bf45
MK
425.I val
426waiters that are waiting on the futex at
427.IR uaddr .
428If there are more than
429.I val
430waiters, then the remaining waiters are removed
431from the wait queue of the source futex at
432.I uaddr
433and added to the wait queue of the target futex at
434.IR uaddr2 .
435The
768d3c23 436.I val2
936876a9 437argument specifies an upper limit on the number of waiters
a7c2bf45 438that are requeued to the futex at
768d3c23 439.IR uaddr2 .
a7c2bf45 440
d6bb5a38
MK
441.\" FIXME(Torvald) Is the following correct? Or is just the decision
442.\" which threads to wake or requeue part of the atomic operation?
4b35dc5d
TR
443The load from
444.I uaddr
4c8cb0ff
MK
445is an atomic memory access (i.e., using atomic machine instructions of
446the respective architecture).
077981d4 447This load, the comparison with
4b35dc5d 448.IR val3 ,
4c8cb0ff
MK
449and the requeueing of any waiters are performed atomically and totally
450ordered with respect to other operations on the same futex word.
ee65b0e8
MK
451.\" Notes from a f2f conversation with Thomas Gleixner (Aug 2015): ###
452.\" The operation is serialized with respect to operations on both
453.\" source and target futex. No other waiter can enqueue itself
454.\" for waiting and no other waiter can dequeue itself because of
455.\" a timeout or signal.
4b35dc5d 456
a7c2bf45
MK
457Typical values to specify for
458.I val
459are 0 or or 1.
460(Specifying
461.BR INT_MAX
462is not useful, because it would make the
463.BR FUTEX_CMP_REQUEUE
464operation equivalent to
465.BR FUTEX_WAKE .)
936876a9 466The limit value specified via
768d3c23
MK
467.I val2
468is typically either 1 or
a7c2bf45
MK
469.BR INT_MAX .
470(Specifying the argument as 0 is not useful, because it would make the
471.BR FUTEX_CMP_REQUEUE
472operation equivalent to
473.BR FUTEX_WAIT .)
627b50ce
MK
474
475The
476.B FUTEX_CMP_REQUEUE
477operation was added as a replacement for the earlier
478.BR FUTEX_REQUEUE .
479The difference is that the check of the value at
480.I uaddr
481can be used to ensure that requeueing happens only under certain
482conditions, which allows race conditions to be avoided in certain use cases.
dcb410c3 483.\" But, as Rich Felker points out, there remain valid use cases for
627b50ce
MK
484.\" FUTEX_REQUEUE, for example, when the calling thread is requeuing
485.\" the target(s) to a lock that the calling thread owns
486.\" From: Rich Felker <dalias@libc.org>
487.\" Date: Wed, 29 Oct 2014 22:43:17 -0400
488.\" To: Darren Hart <dvhart@infradead.org>
489.\" CC: libc-alpha@sourceware.org, ...
490.\" Subject: Re: Add futex wrapper to glibc?
491
492Both
493.BR FUTEX_REQUEUE
494and
495.BR FUTEX_CMP_REQUEUE
496can be used to avoid "thundering herd" wake-ups that could occur when using
497.B FUTEX_WAKE
498in cases where all of the waiters that are woken need to acquire
499another futex.
500Consider the following scenario,
501where multiple waiter threads are waiting on B,
502a wait queue implemented using a futex:
503
504.in +4n
505.nf
506lock(A)
507while (!check_value(V)) {
508 unlock(A);
509 block_on(B);
510 lock(A);
511};
512unlock(A);
513.fi
514.in
515
516If a waker thread used
517.BR FUTEX_WAKE ,
518then all waiters waiting on B would be woken up,
519and they would would all try to acquire lock A.
520However, waking all of the threads in this manner would be pointless because
521all except one of the threads would immediately block on lock A again.
522By contrast, a requeue operation wakes just one waiter and moves
523the other waiters to lock A,
524and when the woken waiter unlocks A then the next waiter can proceed.
43d16602 525.\"
70b06b90
MK
526.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
527.\"
a5956430
MK
528.\" FIXME I added a lengthy piece of text on FUTEX_WAKE_OP text,
529.\" and I'd be happy if someone checked it.
fea681da 530.TP
d67e21f5
MK
531.BR FUTEX_WAKE_OP " (since Linux 2.6.14)"
532.\" commit 4732efbeb997189d9f9b04708dc26bf8613ed721
6bac3b85
MK
533.\" Author: Jakub Jelinek <jakub@redhat.com>
534.\" Date: Tue Sep 6 15:16:25 2005 -0700
4c8cb0ff
MK
535.\" FIXME(Torvald) The glibc condvar implementation is currently being
536.\" revised (e.g., to not use an internal lock anymore).
537.\" It is probably more future-proof to remove this paragraph.
d6bb5a38 538.\" [Torvald, do you have an update here?]
6bac3b85
MK
539This operation was added to support some user-space use cases
540where more than one futex must be handled at the same time.
541The most notable example is the implementation of
542.BR pthread_cond_signal (3),
543which requires operations on two futexes,
544the one used to implement the mutex and the one used in the implementation
545of the wait queue associated with the condition variable.
546.BR FUTEX_WAKE_OP
547allows such cases to be implemented without leading to
548high rates of contention and context switching.
549
550The
57f2d48b 551.BR FUTEX_WAKE_OP
e61abc20 552operation is equivalent to executing the following code atomically
4c8cb0ff
MK
553and totally ordered with respect to other futex operations on
554any of the two supplied futex words:
6bac3b85
MK
555
556.in +4n
557.nf
558int oldval = *(int *) uaddr2;
559*(int *) uaddr2 = oldval \fIop\fP \fIoparg\fP;
560futex(uaddr, FUTEX_WAKE, val, 0, 0, 0);
561if (oldval \fIcmp\fP \fIcmparg\fP)
768d3c23 562 futex(uaddr2, FUTEX_WAKE, val2, 0, 0, 0);
6bac3b85
MK
563.fi
564.in
565
566In other words,
57f2d48b 567.BR FUTEX_WAKE_OP
6bac3b85
MK
568does the following:
569.RS
570.IP * 3
4b35dc5d
TR
571saves the original value of the futex word at
572.IR uaddr2
573and performs an operation to modify the value of the futex at
6bac3b85 574.IR uaddr2 ;
4c8cb0ff
MK
575this is an atomic read-modify-write memory access (i.e., using atomic
576machine instructions of the respective architecture)
6bac3b85
MK
577.IP *
578wakes up a maximum of
579.I val
4b35dc5d 580waiters on the futex for the futex word at
6bac3b85
MK
581.IR uaddr ;
582and
583.IP *
4c8cb0ff
MK
584dependent on the results of a test of the original value of the
585futex word at
6bac3b85
MK
586.IR uaddr2 ,
587wakes up a maximum of
768d3c23 588.I val2
4b35dc5d 589waiters on the futex for the futex word at
6bac3b85
MK
590.IR uaddr2 .
591.RE
592.IP
6bac3b85
MK
593The operation and comparison that are to be performed are encoded
594in the bits of the argument
595.IR val3 .
596Pictorially, the encoding is:
597
f6af90e7 598.in +8n
6bac3b85 599.nf
f6af90e7
MK
600+---+---+-----------+-----------+
601|op |cmp| oparg | cmparg |
602+---+---+-----------+-----------+
603 4 4 12 12 <== # of bits
6bac3b85
MK
604.fi
605.in
606
607Expressed in code, the encoding is:
608
609.in +4n
610.nf
611#define FUTEX_OP(op, oparg, cmp, cmparg) \\
612 (((op & 0xf) << 28) | \\
613 ((cmp & 0xf) << 24) | \\
614 ((oparg & 0xfff) << 12) | \\
615 (cmparg & 0xfff))
616.fi
617.in
618
619In the above,
620.I op
621and
622.I cmp
623are each one of the codes listed below.
624The
625.I oparg
626and
627.I cmparg
628components are literal numeric values, except as noted below.
629
630The
631.I op
632component has one of the following values:
633
634.in +4n
635.nf
636FUTEX_OP_SET 0 /* uaddr2 = oparg; */
637FUTEX_OP_ADD 1 /* uaddr2 += oparg; */
638FUTEX_OP_OR 2 /* uaddr2 |= oparg; */
639FUTEX_OP_ANDN 3 /* uaddr2 &= ~oparg; */
640FUTEX_OP_XOR 4 /* uaddr2 ^= oparg; */
641.fi
642.in
643
644In addition, bit-wise ORing the following value into
645.I op
646causes
647.IR "(1\ <<\ oparg)"
648to be used as the operand:
649
650.in +4n
651.nf
652FUTEX_OP_ARG_SHIFT 8 /* Use (1 << oparg) as operand */
653.fi
654.in
655
656The
657.I cmp
658field is one of the following:
659
660.in +4n
661.nf
662FUTEX_OP_CMP_EQ 0 /* if (oldval == cmparg) wake */
663FUTEX_OP_CMP_NE 1 /* if (oldval != cmparg) wake */
664FUTEX_OP_CMP_LT 2 /* if (oldval < cmparg) wake */
665FUTEX_OP_CMP_LE 3 /* if (oldval <= cmparg) wake */
666FUTEX_OP_CMP_GT 4 /* if (oldval > cmparg) wake */
667FUTEX_OP_CMP_GE 5 /* if (oldval >= cmparg) wake */
668.fi
669.in
670
671The return value of
672.BR FUTEX_WAKE_OP
673is the sum of the number of waiters woken on the futex
674.IR uaddr
675plus the number of waiters woken on the futex
676.IR uaddr2 .
70b06b90
MK
677.\"
678.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
679.\"
d67e21f5 680.TP
79c9b436
TG
681.BR FUTEX_WAIT_BITSET " (since Linux 2.6.25)"
682.\" commit cd689985cf49f6ff5c8eddc48d98b9d581d9475d
fd9e59d4 683This operation is like
79c9b436
TG
684.BR FUTEX_WAIT
685except that
686.I val3
5e1456d4
MK
687is used to provide a 32-bit mask to the kernel.
688This bit mask is stored in the kernel-internal state of the waiter.
79c9b436
TG
689See the description of
690.BR FUTEX_WAKE_BITSET
691for further details.
692
fd9e59d4
MK
693The
694.BR FUTEX_WAIT_BITSET
9732dd8b 695operation also interprets the
fd9e59d4
MK
696.I timeout
697argument differently from
698.BR FUTEX_WAIT .
699See the discussion of
700.BR FUTEX_CLOCK_REALTIME ,
701above.
702
79c9b436
TG
703The
704.I uaddr2
705argument is ignored.
70b06b90
MK
706.\"
707.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
708.\"
79c9b436 709.TP
d67e21f5
MK
710.BR FUTEX_WAKE_BITSET " (since Linux 2.6.25)"
711.\" commit cd689985cf49f6ff5c8eddc48d98b9d581d9475d
55cc422d
TG
712This operation is the same as
713.BR FUTEX_WAKE
714except that the
715.I val3
5e1456d4
MK
716argument is used to provide a 32-bit bit mask to the kernel.
717This bit mask is used to select which waiters should be woken up.
718The selection is done by a bit-wise AND of the "wake" bit mask
98d769c0
MK
719(i.e., the value in
720.IR val3 )
5e1456d4
MK
721and the bit mask which is stored in the kernel-internal
722state of the waiter (the "wait" bit mask that is set using
98d769c0
MK
723.BR FUTEX_WAIT_BITSET ).
724All of the waiters for which the result of the AND is nonzero are woken up;
725the remaining waiters are left sleeping.
726
e9d4496b
MK
727The effect of
728.BR FUTEX_WAIT_BITSET
729and
730.BR FUTEX_WAKE_BITSET
9732dd8b
MK
731is to allow selective wake-ups among multiple waiters that are blocked
732on the same futex.
ac894879 733However, note that, depending on the use case,
5e1456d4 734employing this bit-mask multiplexing feature on a
ac894879 735futex can be less efficient than simply using multiple futexes,
5e1456d4 736because employing bit-mask multiplexing requires the kernel
e9d4496b
MK
737to check all waiters on a futex,
738including those that are not interested in being woken up
5e1456d4 739(i.e., they do not have the relevant bit set in their "wait" bit mask).
e9d4496b
MK
740.\" According to http://locklessinc.com/articles/futex_cheat_sheet/:
741.\"
742.\" "The original reason for the addition of these extensions
743.\" was to improve the performance of pthread read-write locks
744.\" in glibc. However, the pthreads library no longer uses the
745.\" same locking algorithm, and these extensions are not used
746.\" without the bitset parameter being all ones.
747.\"
748.\" The page goes on to note that the FUTEX_WAIT_BITSET operation
5e1456d4 749.\" is nevertheless used (with a bit mask of all ones) in order to
e9d4496b
MK
750.\" obtain the absolute timeout functionality that is useful
751.\" for efficiently implementing Pthreads APIs (which use absolute
752.\" timeouts); FUTEX_WAIT provides only relative timeouts.
753
98d769c0
MK
754The
755.I uaddr2
756and
757.I timeout
758arguments are ignored.
9732dd8b
MK
759
760The
761.BR FUTEX_WAIT
762and
763.BR FUTEX_WAKE
764operations correspond to
765.BR FUTEX_WAIT_BITSET
766and
767.BR FUTEX_WAKE_BITSET
5e1456d4 768operations where the bit masks are all ones.
bd90a5f9 769.\"
70b06b90 770.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
bd90a5f9
MK
771.\"
772.SS Priority-inheritance futexes
b52e1cd4
MK
773Linux supports priority-inheritance (PI) futexes in order to handle
774priority-inversion problems that can be encountered with
775normal futex locks.
b565548b 776Priority inversion is the problem that occurs when a high-priority
bdc5957a
MK
777task is blocked waiting to acquire a lock held by a low-priority task,
778while tasks at an intermediate priority continuously preempt
779the low-priority task from the CPU.
780Consequently, the low-priority task makes no progress toward
781releasing the lock, and the high-priority task remains blocked.
7f315ae3 782
7d20efd7
MK
783Priority inheritance is a mechanism for dealing with
784the priority-inversion problem.
bdc5957a
MK
785With this mechanism, when a high-priority task becomes blocked
786by a lock held by a low-priority task,
9cee832c
MK
787the priority of the low-priority task is temporarily raised
788to that of the high-priority task,
bdc5957a 789so that it is not preempted by any intermediate level tasks,
7d20efd7
MK
790and can thus make progress toward releasing the lock.
791To be effective, priority inheritance must be transitive,
bdc5957a 792meaning that if a high-priority task blocks on a lock
ca4e5b2b 793held by a lower-priority task that is itself blocked by a lock
bdc5957a 794held by another intermediate-priority task
7d20efd7 795(and so on, for chains of arbitrary length),
b0f35fbb 796then both of those tasks
bdc5957a
MK
797(or more generally, all of the tasks in a lock chain)
798have their priorities raised to be the same as the high-priority task.
7d20efd7 799
9e2b90ee 800From a user-space perspective,
39e9b2e1
MK
801what makes a futex PI-aware is a policy agreement (described below)
802between user space and the kernel about the value of the futex word,
601399f3
MK
803coupled with the use of the PI-futex operations described below.
804(Unlike the other futex operations described above,
805the PI-futex operations are designed
806for the implementation of very specific IPC mechanisms.)
807.\"
9e2b90ee
MK
808.\" Quoting Darren Hart:
809.\" These opcodes paired with the PI futex value policy (described below)
810.\" defines a "futex" as PI aware. These were created very specifically
811.\" in support of PI pthread_mutexes, so it makes a lot more sense to
812.\" talk about a PI aware pthread_mutex, than a PI aware futex, since
813.\" there is a lot of policy and scaffolding that has to be built up
814.\" around it to use it properly (this is what a PI pthread_mutex is).
815
ac894879 816.\" mtk: The following text is drawn from the Hart/Guniguntala paper
1af427a4 817.\" (listed in SEE ALSO), but I have reworded some pieces
f0a9e8f4 818.\" significntly.
79d918c7 819.\"
f0a9e8f4 820The PI-futex operations described below differ from the other
4b35dc5d
TR
821futex operations in that they impose policy on the use of the value of the
822futex word:
79d918c7 823.IP * 3
4b35dc5d 824If the lock is not acquired, the futex word's value shall be 0.
79d918c7 825.IP *
4c8cb0ff
MK
826If the lock is acquired, the futex word's value shall
827be the thread ID (TID;
4b35dc5d 828see
79d918c7
MK
829.BR gettid (2))
830of the owning thread.
831.IP *
79d918c7
MK
832If the lock is owned and there are threads contending for the lock,
833then the
834.B FUTEX_WAITERS
4b35dc5d 835bit shall be set in the futex word's value; in other words, this value is:
79d918c7
MK
836
837 FUTEX_WAITERS | TID
9e2b90ee 838
601399f3
MK
839.IP
840(Note that is invalid for a PI futex word to have no owner and
841.BR FUTEX_WAITERS
842set.)
79d918c7
MK
843.PP
844With this policy in place,
fd105614 845a user-space application can acquire an unacquired
601399f3 846lock or release a lock using atomic instructions executed in user mode
fd105614 847(e.g., a compare-and-swap operation such as
b52e1cd4
MK
848.I cmpxchg
849on the x86 architecture).
4c8cb0ff
MK
850Acquiring a lock simply consists of using compare-and-swap to atomically
851set the futex word's value to the caller's TID if its previous value was 0.
4b35dc5d
TR
852Releasing a lock requires using compare-and-swap to set the futex word's
853value to 0 if the previous value was the expected TID.
b52e1cd4 854
4b35dc5d 855If a futex is already acquired (i.e., has a nonzero value),
b52e1cd4 856waiters must employ the
79d918c7
MK
857.B FUTEX_LOCK_PI
858operation to acquire the lock.
4b35dc5d 859If other threads are waiting for the lock, then the
79d918c7 860.B FUTEX_WAITERS
4c8cb0ff
MK
861bit is set in the futex value;
862in this case, the lock owner must employ the
79d918c7 863.B FUTEX_UNLOCK_PI
b52e1cd4
MK
864operation to release the lock.
865
79d918c7
MK
866In the cases where callers are forced into the kernel
867(i.e., required to perform a
868.BR futex ()
0c3ec26b 869call),
79d918c7
MK
870they then deal directly with a so-called RT-mutex,
871a kernel locking mechanism which implements the required
872priority-inheritance semantics.
873After the RT-mutex is acquired, the futex value is updated accordingly,
874before the calling thread returns to user space.
79d918c7 875
a59fca75 876It is important to note
ac894879 877.\" tglx (July 2015):
30239c10
MK
878.\" If there are multiple waiters on a pi futex then a wake pi operation
879.\" will wake the first waiter and hand over the lock to this waiter. This
880.\" includes handing over the rtmutex which represents the futex in the
881.\" kernel. The strict requirement is that the futex owner and the rtmutex
882.\" owner must be the same, except for the update period which is
883.\" serialized by the futex internal locking. That means the kernel must
1d09c150 884.\" update the user-space value prior to returning to user space
4b35dc5d 885that the kernel will update the futex word's value prior
79d918c7 886to returning to user space.
601399f3
MK
887(This prevents the possibility of the futex word's value ending
888up in an invalid state, such as having an owner but the value being 0,
889or having waiters but not having the
890.B FUTEX_WAITERS
891bit set.)
30239c10
MK
892
893If a futex has an associated RT-mutex in the kernel
894(i.e., there are blocked waiters)
895and the owner of the futex/RT-mutex dies unexpectedly,
896then the kernel cleans up the RT-mutex and hands it over to the next waiter.
897This in turn requires that the user-space value is updated accordingly.
898To indicate that this is required, the kernel sets the
899.B FUTEX_OWNER_DIED
900bit in the futex word along with the thread ID of the new owner.
1d09c150
MK
901User space is then responsible for cleaning up the stale state left over by
902the dead owner.
30239c10
MK
903.\" tglx (July 2015):
904.\" The FUTEX_OWNER_DIED bit can also be set on uncontended futexes, where
905.\" the kernel has no state associated. This happens via the robust futex
906.\" mechanism. In that case the futex value will be set to
907.\" FUTEX_OWNER_DIED. The robust futex mechanism is also available for non
908.\" PI futexes.
bd90a5f9 909
601399f3
MK
910PI futexes are operated on by specifying one of the values listed below in
911.IR futex_op .
912Note that the PI futex operations must be used as paired operations
913and are subject to some additional requirements:
914.IP * 3
915.B FUTEX_LOCK_PI
916and
917.B FUTEX_TRYLOCK_PI
918pair with
919.BR FUTEX_UNLOCK_PI.
920.B FUTEX_UNLOCK_PI
921must be called only on a futex owned by the calling thread,
922as defined by the value policy, otherwise the error
923.B EPERM
924results.
925.IP *
926.B FUTEX_WAIT_REQUEUE_PI
927pairs with
928.BR FUTEX_CMP_REQUEUE_PI .
929This must be performed from a non-PI futex to a distinct PI futex
930(or the error
931.B EINVAL
932results).
933Additionally,
934.I val
935(the number of waiters to be woken) must be 1
936(or the error
937.B EINVAL
938results).
939.P
940The PI futex operations are as follows:
70b06b90
MK
941.\"
942.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
943.\"
d67e21f5
MK
944.TP
945.BR FUTEX_LOCK_PI " (since Linux 2.6.18)"
946.\" commit c87e2837be82df479a6bae9f155c43516d2feebc
bc54ed38 947This operation is used after an attempt to acquire
fd105614 948the lock via an atomic user-mode instruction failed
4b35dc5d 949because the futex word has a nonzero value\(emspecifically,
8297383e 950because it contained the (PID-namespace-specific) TID of the lock owner.
67833bec 951
4b35dc5d 952The operation checks the value of the futex word at the address
67833bec 953.IR uaddr .
70b06b90
MK
954If the value is 0, then the kernel tries to atomically set
955the futex value to the caller's TID.
c3875d1d 956If the futex word's value is nonzero,
67833bec 957the kernel atomically sets the
e0547e70 958.B FUTEX_WAITERS
67833bec
MK
959bit, which signals the futex owner that it cannot unlock the futex in
960user space atomically by setting the futex value to 0.
c3875d1d
MK
961.\" tglx (July 2015):
962.\" The operation here is similar to the FUTEX_WAIT logic. When the user
963.\" space atomic acquire does not succeed because the futex value was non
964.\" zero, then the waiter goes into the kernel, takes the kernel internal
965.\" lock and retries the acquisition under the lock. If the acquisition
966.\" does not succeed either, then it sets the FUTEX_WAITERS bit, to signal
967.\" the lock owner that it needs to go into the kernel. Here is the pseudo
968.\" code:
969.\"
970.\" lock(kernel_lock);
971.\" retry:
972.\"
973.\" /*
974.\" * Owner might have unlocked in userspace before we
975.\" * were able to set the waiter bit.
976.\" */
977.\" if (atomic_acquire(futex) == SUCCESS) {
978.\" unlock(kernel_lock());
979.\" return 0;
980.\" }
981.\"
982.\" /*
983.\" * Owner might have unlocked after the above atomic_acquire()
984.\" * attempt.
985.\" */
986.\" if (atomic_set_waiters_bit(futex) != SUCCESS)
987.\" goto retry;
988.\"
989.\" queue_waiter();
990.\" unlock(kernel_lock);
991.\" block();
992.\"
993After that, the kernel:
994.RS
995.IP 1. 3
996Tries to find the thread which is associated with the owner TID.
997.IP 2.
998Creates or reuses kernel state on behalf of the owner.
999(If this is the first waiter, there is no kernel state for this
1000futex, so kernel state is created by locking the RT-mutex
1001and the futex owner is made the owner of the RT-mutex.
1002If there are existing waiters, then the existing state is reused.)
1003.IP 3.
ca4e5b2b 1004Attaches the waiter to the futex
c3875d1d
MK
1005(i.e., the waiter is enqueued on the RT-mutex waiter list).
1006.RE
1007.IP
ac894879
MK
1008If more than one waiter exists,
1009the enqueueing of the waiter is in descending priority order.
1010(For information on priority ordering, see the discussion of the
1011.BR SCHED_DEADLINE ,
1012.BR SCHED_FIFO ,
1013and
1014.BR SCHED_RR
1015scheduling policies in
1016.BR sched (7).)
1017The owner inherits either the waiter's CPU bandwidth
1018(if the waiter is scheduled under the
1019.BR SCHED_DEADLINE
1020policy) or the waiter's priority (if the waiter is scheduled under the
1021.BR SCHED_RR
1022or
1023.BR SCHED_FIFO
1024policy).
1d09c150
MK
1025.\" August 2015:
1026.\" mtk: If the realm is restricted purely to SCHED_OTHER (SCHED_NORMAL)
1027.\" processes, does the nice value come into play also?
1028.\"
1029.\" tglx: No. SCHED_OTHER/NORMAL tasks are handled in FIFO order
c3875d1d 1030This inheritance follows the lock chain in the case of nested locking
ca4e5b2b
MK
1031.\" (i.e., task 1 blocks on lock A, held by task 2,
1032.\" while task 2 blocks on lock B, held by task 3)
c3875d1d
MK
1033and performs deadlock detection.
1034
e0547e70
TG
1035The
1036.I timeout
9ce19cf1
MK
1037argument provides a timeout for the lock attempt.
1038It is interpreted as an absolute time, measured against the
1039.BR CLOCK_REALTIME
1040clock.
1041If
1042.I timeout
1043is NULL, the operation will block indefinitely.
e0547e70 1044
a449c634 1045The
e0547e70
TG
1046.IR uaddr2 ,
1047.IR val ,
1048and
1049.IR val3
a449c634 1050arguments are ignored.
67833bec 1051.\"
70b06b90
MK
1052.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1053.\"
d67e21f5 1054.TP
12fdbe23 1055.BR FUTEX_TRYLOCK_PI " (since Linux 2.6.18)"
d67e21f5 1056.\" commit c87e2837be82df479a6bae9f155c43516d2feebc
3fbb1be1 1057This operation tries to acquire the lock at
12fdbe23 1058.IR uaddr .
c3875d1d
MK
1059It is invoked when a user-space atomic acquire did not
1060succeed because the futex word was not 0.
1061
3fbb1be1
MK
1062The acquisition of the lock might succeed if performed by the
1063kernel in cases where the futex word contains stale state
c3875d1d
MK
1064.RB ( FUTEX_WAITERS
1065and/or
1066.BR FUTEX_OWNER_DIED ).
1067This can happen when the owner of the futex died.
1d09c150
MK
1068User space cannot handle this condition in a race-free manner,
1069but the kernel can fix this up and acquire the futex.
ee65b0e8
MK
1070.\" Paraphrasing a f2f conversation with Thomas Gleixner about the
1071.\" above point (Aug 2015): ###
1072.\" There is a rare possibility of a race condition involving an
1073.\" uncontended futex with no owner, but with waiters. The
1074.\" kernel-user-space contract is that if a futex is nonzero, you must
1075.\" go into kernel. The futex was owned by a task, and that task dies
1076.\" but there are no waiters, so the futex value is non zero.
1077.\" Therefore, the next locker has to go into the kernel,
1078.\" so that the kernel has a chance to clean up. (CMXCH on zero
1079.\" in user space would fail, so kernel has to clean up.)
084744ef
MK
1080
1081The
1082.IR uaddr2 ,
1083.IR val ,
1084.IR timeout ,
1085and
1086.IR val3
1087arguments are ignored.
70b06b90
MK
1088.\"
1089.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1090.\"
d67e21f5 1091.TP
12fdbe23 1092.BR FUTEX_UNLOCK_PI " (since Linux 2.6.18)"
d67e21f5 1093.\" commit c87e2837be82df479a6bae9f155c43516d2feebc
d4ba4328 1094This operation wakes the top priority waiter that is waiting in
ecae2099
TG
1095.B FUTEX_LOCK_PI
1096on the futex address provided by the
1097.I uaddr
1098argument.
1099
1d09c150 1100This is called when the user-space value at
ecae2099
TG
1101.I uaddr
1102cannot be changed atomically from a TID (of the owner) to 0.
1103
1104The
1105.IR uaddr2 ,
1106.IR val ,
1107.IR timeout ,
1108and
1109.IR val3
11a194bf 1110arguments are ignored.
70b06b90
MK
1111.\"
1112.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1113.\"
d67e21f5 1114.TP
d67e21f5
MK
1115.BR FUTEX_CMP_REQUEUE_PI " (since Linux 2.6.31)"
1116.\" commit 52400ba946759af28442dee6265c5c0180ac7122
f812a08b
DH
1117This operation is a PI-aware variant of
1118.BR FUTEX_CMP_REQUEUE .
1119It requeues waiters that are blocked via
1120.B FUTEX_WAIT_REQUEUE_PI
1121on
1122.I uaddr
1123from a non-PI source futex
1124.RI ( uaddr )
1125to a PI target futex
1126.RI ( uaddr2 ).
1127
9e54d26d
MK
1128As with
1129.BR FUTEX_CMP_REQUEUE ,
1130this operation wakes up a maximum of
1131.I val
1132waiters that are waiting on the futex at
1133.IR uaddr .
1134However, for
1135.BR FUTEX_CMP_REQUEUE_PI ,
1136.I val
6fbeb8f4 1137is required to be 1
939ca89f 1138(since the main point is to avoid a thundering herd).
9e54d26d
MK
1139The remaining waiters are removed from the wait queue of the source futex at
1140.I uaddr
1141and added to the wait queue of the target futex at
1142.IR uaddr2 .
f812a08b 1143
9e54d26d 1144The
768d3c23 1145.I val2
c6d8cf21
MK
1146.\" val2 is the cap on the number of requeued waiters.
1147.\" In the glibc pthread_cond_broadcast() implementation, this argument
1148.\" is specified as INT_MAX, and for pthread_cond_signal() it is 0.
9e54d26d 1149and
768d3c23 1150.I val3
9e54d26d
MK
1151arguments serve the same purposes as for
1152.BR FUTEX_CMP_REQUEUE .
70b06b90 1153.\"
8297383e 1154.\" The page at http://locklessinc.com/articles/futex_cheat_sheet/
be376673 1155.\" notes that "priority-inheritance Futex to priority-inheritance
8297383e
MK
1156.\" Futex requeues are currently unsupported". However, probably
1157.\" the page does not need to say nothing about this, since
1158.\" Thomas Gleixner commented (July 2015): "they never will be
1159.\" supported because they make no sense at all"
70b06b90
MK
1160.\"
1161.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1162.\"
d67e21f5
MK
1163.TP
1164.BR FUTEX_WAIT_REQUEUE_PI " (since Linux 2.6.31)"
1165.\" commit 52400ba946759af28442dee6265c5c0180ac7122
70b06b90 1166.\"
c3875d1d 1167Wait on a non-PI futex at
6ff1b4c0 1168.I uaddr
c3875d1d
MK
1169and potentially be requeued (via a
1170.BR FUTEX_CMP_REQUEUE_PI
1171operation in another task) onto a PI futex at
6ff1b4c0
TG
1172.IR uaddr2 .
1173The wait operation on
1174.I uaddr
c3875d1d 1175is the same as for
6ff1b4c0 1176.BR FUTEX_WAIT .
c3875d1d 1177
6ff1b4c0
TG
1178The waiter can be removed from the wait on
1179.I uaddr
6ff1b4c0 1180without requeueing on
c3875d1d
MK
1181.IR uaddr2
1182via a
1d09c150 1183.BR FUTEX_WAKE
c3875d1d
MK
1184operation in another task.
1185In this case, the
1186.BR FUTEX_WAIT_REQUEUE_PI
3fbb1be1
MK
1187operation fails with the error
1188.BR EAGAIN .
a4e69912 1189
63bea7dc
MK
1190If
1191.I timeout
1192is not NULL, it specifies a timeout for the wait operation;
1193this timeout is interpreted as outlined above in the description of the
1194.BR FUTEX_CLOCK_REALTIME
1195option.
1196If
1197.I timeout
1198is NULL, the operation can block indefinitely.
1199
a4e69912
MK
1200The
1201.I val3
1202argument is ignored.
abb571e8
MK
1203
1204The
1205.BR FUTEX_WAIT_REQUEUE_PI
1206and
1207.BR FUTEX_CMP_REQUEUE_PI
1208were added to support a fairly specific use case:
1209support for priority-inheritance-aware POSIX threads condition variables.
1210The idea is that these operations should always be paired,
1211in order to ensure that user space and the kernel remain in sync.
1212Thus, in the
1213.BR FUTEX_WAIT_REQUEUE_PI
1214operation, the user-space application pre-specifies the target
1215of the requeue that takes place in the
1216.BR FUTEX_CMP_REQUEUE_PI
1217operation.
1218.\"
1219.\" Darren Hart notes that a patch to allow glibc to fully support
1af427a4 1220.\" PI-aware pthreads condition variables has not yet been accepted into
abb571e8
MK
1221.\" glibc. The story is complex, and can be found at
1222.\" https://sourceware.org/bugzilla/show_bug.cgi?id=11588
1223.\" Darren notes that in the meantime, the patch is shipped with various
1af427a4 1224.\" PREEMPT_RT-enabled Linux systems.
abb571e8
MK
1225.\"
1226.\" Related to the preceding, Darren proposed that somewhere, man-pages
1227.\" should document the following point:
1af427a4 1228.\"
4c8cb0ff
MK
1229.\" While the Linux kernel, since 2.6.31, supports requeueing of
1230.\" priority-inheritance (PI) aware mutexes via the
1231.\" FUTEX_WAIT_REQUEUE_PI and FUTEX_CMP_REQUEUE_PI futex operations,
1232.\" the glibc implementation does not yet take full advantage of this.
1233.\" Specifically, the condvar internal data lock remains a non-PI aware
1234.\" mutex, regardless of the type of the pthread_mutex associated with
1235.\" the condvar. This can lead to an unbounded priority inversion on
1236.\" the internal data lock even when associating a PI aware
1237.\" pthread_mutex with a condvar during a pthread_cond*_wait
1238.\" operation. For this reason, it is not recommended to rely on
1239.\" priority inheritance when using pthread condition variables.
1af427a4
MK
1240.\"
1241.\" The problem is that the obvious location for this text is
1242.\" the pthread_cond*wait(3) man page. However, such a man page
abb571e8 1243.\" does not currently exist.
70b06b90 1244.\"
6700de24 1245.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
70b06b90 1246.\"
47297adb 1247.SH RETURN VALUE
fea681da 1248.PP
a5c5a06a
MK
1249In the event of an error (and assuming that
1250.BR futex ()
1251was invoked via
1252.BR syscall (2)),
1253all operations return \-1 and set
e808bba0 1254.I errno
6f147f79 1255to indicate the cause of the error.
adc89a97 1256
e808bba0
MK
1257The return value on success depends on the operation,
1258as described in the following list:
fea681da
MK
1259.TP
1260.B FUTEX_WAIT
077981d4 1261Returns 0 if the caller was woken up.
4c8cb0ff
MK
1262Note that a wake-up can also be caused by common futex usage patterns
1263in unrelated code that happened to have previously used the futex word's
1264memory location (e.g., typical futex-based implementations of
1265Pthreads mutexes can cause this under some conditions).
1266Therefore, callers should always conservatively assume that a return
1267value of 0 can mean a spurious wake-up, and use the futex word's value
bc54ed38
MK
1268(i.e., the user-space synchronization scheme)
1269to decide whether to continue to block or not.
fea681da
MK
1270.TP
1271.B FUTEX_WAKE
bdc5957a 1272Returns the number of waiters that were woken up.
fea681da
MK
1273.TP
1274.B FUTEX_FD
1275Returns the new file descriptor associated with the futex.
1276.TP
1277.B FUTEX_REQUEUE
bdc5957a 1278Returns the number of waiters that were woken up.
fea681da
MK
1279.TP
1280.B FUTEX_CMP_REQUEUE
bdc5957a 1281Returns the total number of waiters that were woken up or
4b35dc5d 1282requeued to the futex for the futex word at
3dfcc11d
MK
1283.IR uaddr2 .
1284If this value is greater than
1285.IR val ,
fd105614 1286then the difference is the number of waiters requeued to the futex for the
4c8cb0ff 1287futex word at
3dfcc11d 1288.IR uaddr2 .
dcad19c0
MK
1289.TP
1290.B FUTEX_WAKE_OP
a8b5b324 1291Returns the total number of waiters that were woken up.
4c8cb0ff
MK
1292This is the sum of the woken waiters on the two futexes for
1293the futex words at
a8b5b324
MK
1294.I uaddr
1295and
1296.IR uaddr2 .
dcad19c0
MK
1297.TP
1298.B FUTEX_WAIT_BITSET
077981d4
MK
1299Returns 0 if the caller was woken up.
1300See
4b35dc5d
TR
1301.B FUTEX_WAIT
1302for how to interpret this correctly in practice.
dcad19c0
MK
1303.TP
1304.B FUTEX_WAKE_BITSET
bdc5957a 1305Returns the number of waiters that were woken up.
dcad19c0
MK
1306.TP
1307.B FUTEX_LOCK_PI
bf02a260 1308Returns 0 if the futex was successfully locked.
dcad19c0
MK
1309.TP
1310.B FUTEX_TRYLOCK_PI
5c716eef 1311Returns 0 if the futex was successfully locked.
dcad19c0
MK
1312.TP
1313.B FUTEX_UNLOCK_PI
52bb928f 1314Returns 0 if the futex was successfully unlocked.
dcad19c0
MK
1315.TP
1316.B FUTEX_CMP_REQUEUE_PI
bdc5957a 1317Returns the total number of waiters that were woken up or
4b35dc5d 1318requeued to the futex for the futex word at
dddd395a
MK
1319.IR uaddr2 .
1320If this value is greater than
1321.IR val ,
4c8cb0ff
MK
1322then difference is the number of waiters requeued to the futex for
1323the futex word at
dddd395a 1324.IR uaddr2 .
dcad19c0
MK
1325.TP
1326.B FUTEX_WAIT_REQUEUE_PI
4c8cb0ff
MK
1327Returns 0 if the caller was successfully requeued to the futex for
1328the futex word at
22c15de9 1329.IR uaddr2 .
70b06b90
MK
1330.\"
1331.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1332.\"
fea681da
MK
1333.SH ERRORS
1334.TP
1335.B EACCES
4b35dc5d 1336No read access to the memory of a futex word.
fea681da
MK
1337.TP
1338.B EAGAIN
f48516d1 1339.RB ( FUTEX_WAIT ,
4b35dc5d 1340.BR FUTEX_WAIT_BITSET ,
f48516d1 1341.BR FUTEX_WAIT_REQUEUE_PI )
badbf70c
MK
1342The value pointed to by
1343.I uaddr
1344was not equal to the expected value
1345.I val
1346at the time of the call.
9732dd8b
MK
1347
1348.BR Note :
1349on Linux, the symbolic names
1350.B EAGAIN
1351and
1352.B EWOULDBLOCK
77da5feb 1353(both of which appear in different parts of the kernel futex code)
9732dd8b 1354have the same value.
badbf70c
MK
1355.TP
1356.B EAGAIN
8f2068bb
MK
1357.RB ( FUTEX_CMP_REQUEUE ,
1358.BR FUTEX_CMP_REQUEUE_PI )
ce5602fd 1359The value pointed to by
9f6c40c0
МК
1360.I uaddr
1361is not equal to the expected value
1362.IR val3 .
fea681da 1363.TP
5662f56a
MK
1364.BR EAGAIN
1365.RB ( FUTEX_LOCK_PI ,
aaec9032
MK
1366.BR FUTEX_TRYLOCK_PI ,
1367.BR FUTEX_CMP_REQUEUE_PI )
1368The futex owner thread ID of
1369.I uaddr
1370(for
1371.BR FUTEX_CMP_REQUEUE_PI :
1372.IR uaddr2 )
1373is about to exit,
5662f56a
MK
1374but has not yet handled the internal state cleanup.
1375Try again.
1376.TP
7a39e745
MK
1377.BR EDEADLK
1378.RB ( FUTEX_LOCK_PI ,
9732dd8b
MK
1379.BR FUTEX_TRYLOCK_PI ,
1380.BR FUTEX_CMP_REQUEUE_PI )
4b35dc5d 1381The futex word at
7a39e745
MK
1382.I uaddr
1383is already locked by the caller.
1384.TP
662c0da8 1385.BR EDEADLK
c3875d1d 1386.\" FIXME . I see that kernel/locking/rtmutex.c uses EDEADLK in some
d6bb5a38 1387.\" places, and EDEADLOCK in others. On almost all architectures
4c8cb0ff
MK
1388.\" these constants are synonymous. Is there a reason that both
1389.\" names are used?
8297383e
MK
1390.\"
1391.\" tglx (July 2015): "No. We should probably fix that."
1392.\"
662c0da8 1393.RB ( FUTEX_CMP_REQUEUE_PI )
4b35dc5d 1394While requeueing a waiter to the PI futex for the futex word at
662c0da8
MK
1395.IR uaddr2 ,
1396the kernel detected a deadlock.
1397.TP
fea681da 1398.B EFAULT
1ea901e8
MK
1399A required pointer argument (i.e.,
1400.IR uaddr ,
1401.IR uaddr2 ,
1402or
1403.IR timeout )
496df304 1404did not point to a valid user-space address.
fea681da 1405.TP
9f6c40c0 1406.B EINTR
e808bba0 1407A
9f6c40c0 1408.B FUTEX_WAIT
2674f781
MK
1409or
1410.B FUTEX_WAIT_BITSET
e808bba0 1411operation was interrupted by a signal (see
f529fd20
MK
1412.BR signal (7)).
1413In kernels before Linux 2.6.22, this error could also be returned for
1414on a spurious wakeup; since Linux 2.6.22, this no longer happens.
9f6c40c0 1415.TP
fea681da 1416.B EINVAL
180f97b7
MK
1417The operation in
1418.IR futex_op
1419is one of those that employs a timeout, but the supplied
fb2f4c27
MK
1420.I timeout
1421argument was invalid
1422.RI ( tv_sec
1423was less than zero, or
1424.IR tv_nsec
cabee29d 1425was not less than 1,000,000,000).
fb2f4c27
MK
1426.TP
1427.B EINVAL
0c74df0b 1428The operation specified in
025e1374 1429.IR futex_op
0c74df0b 1430employs one or both of the pointers
51ee94be 1431.I uaddr
a1f47699 1432and
0c74df0b
MK
1433.IR uaddr2 ,
1434but one of these does not point to a valid object\(emthat is,
1435the address is not four-byte-aligned.
51ee94be
MK
1436.TP
1437.B EINVAL
55cc422d
TG
1438.RB ( FUTEX_WAIT_BITSET ,
1439.BR FUTEX_WAKE_BITSET )
5e1456d4 1440The bit mask supplied in
79c9b436
TG
1441.IR val3
1442is zero.
1443.TP
1444.B EINVAL
2abcba67 1445.RB ( FUTEX_CMP_REQUEUE_PI )
add875c0
MK
1446.I uaddr
1447equals
1448.IR uaddr2
1449(i.e., an attempt was made to requeue to the same futex).
1450.TP
ff597681
MK
1451.BR EINVAL
1452.RB ( FUTEX_FD )
1453The signal number supplied in
1454.I val
1455is invalid.
1456.TP
6bac3b85 1457.B EINVAL
476debd7
MK
1458.RB ( FUTEX_WAKE ,
1459.BR FUTEX_WAKE_OP ,
1460.BR FUTEX_WAKE_BITSET ,
1461.BR FUTEX_REQUEUE ,
1462.BR FUTEX_CMP_REQUEUE )
1463The kernel detected an inconsistency between the user-space state at
1464.I uaddr
1465and the kernel state\(emthat is, it detected a waiter which waits in
1466.BR FUTEX_LOCK_PI
1467on
1468.IR uaddr .
1469.TP
1470.B EINVAL
a218ef20 1471.RB ( FUTEX_LOCK_PI ,
ce022f18
MK
1472.BR FUTEX_TRYLOCK_PI ,
1473.BR FUTEX_UNLOCK_PI )
a218ef20
MK
1474The kernel detected an inconsistency between the user-space state at
1475.I uaddr
1476and the kernel state.
ce022f18 1477This indicates either state corruption
ce022f18 1478or that the kernel found a waiter on
a218ef20
MK
1479.I uaddr
1480which is waiting via
1481.BR FUTEX_WAIT
1482or
1483.BR FUTEX_WAIT_BITSET .
1484.TP
1485.B EINVAL
f9250b1a
MK
1486.RB ( FUTEX_CMP_REQUEUE_PI )
1487The kernel detected an inconsistency between the user-space state at
99c0041d
MK
1488.I uaddr2
1489and the kernel state;
ee65b0e8
MK
1490.\" From a conversation with Thomas Gleixner (Aug 2015): ###
1491.\" The kernel sees: I have non PI state for a futex you tried to
1492.\" tell me was PI
99c0041d
MK
1493that is, the kernel detected a waiter which waits via
1494.BR FUTEX_WAIT
8297383e
MK
1495or
1496.BR FUTEX_WAIT_BITSET
99c0041d
MK
1497on
1498.IR uaddr2 .
1499.TP
1500.B EINVAL
1501.RB ( FUTEX_CMP_REQUEUE_PI )
1502The kernel detected an inconsistency between the user-space state at
f9250b1a
MK
1503.I uaddr
1504and the kernel state;
1505that is, the kernel detected a waiter which waits via
75299c8d 1506.BR FUTEX_WAIT
99c0041d 1507or
75299c8d 1508.BR FUTEX_WAIT_BITESET
f9250b1a
MK
1509on
1510.IR uaddr .
1511.TP
1512.B EINVAL
99c0041d 1513.RB ( FUTEX_CMP_REQUEUE_PI )
75299c8d
MK
1514The kernel detected an inconsistency between the user-space state at
1515.I uaddr
1516and the kernel state;
1517that is, the kernel detected a waiter which waits on
1518.I uaddr
1519via
1520.BR FUTEX_LOCK_PI
1521(instead of
1522.BR FUTEX_WAIT_REQUEUE_PI ).
99c0041d
MK
1523.TP
1524.B EINVAL
9786b3ca 1525.RB ( FUTEX_CMP_REQUEUE_PI )
8297383e
MK
1526.\" This deals with the case:
1527.\" wait_requeue_pi(A, B);
1528.\" requeue_pi(A, C);
9786b3ca
MK
1529An attempt was made to requeue a waiter to a futex other than that
1530specified by the matching
1531.B FUTEX_WAIT_REQUEUE_PI
1532call for that waiter.
1533.TP
1534.B EINVAL
f0c0d61c
MK
1535.RB ( FUTEX_CMP_REQUEUE_PI )
1536The
1537.I val
1538argument is not 1.
1539.TP
1540.B EINVAL
4832b48a 1541Invalid argument.
fea681da 1542.TP
a449c634
MK
1543.BR ENOMEM
1544.RB ( FUTEX_LOCK_PI ,
e34a8fb6
MK
1545.BR FUTEX_TRYLOCK_PI ,
1546.BR FUTEX_CMP_REQUEUE_PI )
a449c634
MK
1547The kernel could not allocate memory to hold state information.
1548.TP
fea681da 1549.B ENFILE
ff597681 1550.RB ( FUTEX_FD )
fea681da 1551The system limit on the total number of open files has been reached.
4701fc28
MK
1552.TP
1553.B ENOSYS
1554Invalid operation specified in
d33602c4 1555.IR futex_op .
9f6c40c0 1556.TP
4a7e5b05
MK
1557.B ENOSYS
1558The
1559.BR FUTEX_CLOCK_REALTIME
1560option was specified in
1afcee7c 1561.IR futex_op ,
4a7e5b05
MK
1562but the accompanying operation was neither
1563.BR FUTEX_WAIT_BITSET
1564nor
1565.BR FUTEX_WAIT_REQUEUE_PI .
1566.TP
a9dcb4d1
MK
1567.BR ENOSYS
1568.RB ( FUTEX_LOCK_PI ,
f2424fae 1569.BR FUTEX_TRYLOCK_PI ,
4945ff19 1570.BR FUTEX_UNLOCK_PI ,
4cf92894 1571.BR FUTEX_CMP_REQUEUE_PI ,
794bb106 1572.BR FUTEX_WAIT_REQUEUE_PI )
4b35dc5d 1573A run-time check determined that the operation is not available.
f0a9e8f4 1574The PI-futex operations are not implemented on all architectures and
077981d4 1575are not supported on some CPU variants.
a9dcb4d1 1576.TP
c7589177
MK
1577.BR EPERM
1578.RB ( FUTEX_LOCK_PI ,
dc2742a8
MK
1579.BR FUTEX_TRYLOCK_PI ,
1580.BR FUTEX_CMP_REQUEUE_PI )
04331c3f 1581The caller is not allowed to attach itself to the futex at
dc2742a8
MK
1582.I uaddr
1583(for
1584.BR FUTEX_CMP_REQUEUE_PI :
1585the futex at
1586.IR uaddr2 ).
c7589177
MK
1587(This may be caused by a state corruption in user space.)
1588.TP
76f347ba 1589.BR EPERM
87276709 1590.RB ( FUTEX_UNLOCK_PI )
4b35dc5d 1591The caller does not own the lock represented by the futex word.
76f347ba 1592.TP
0b0e4934
MK
1593.BR ESRCH
1594.RB ( FUTEX_LOCK_PI ,
9732dd8b
MK
1595.BR FUTEX_TRYLOCK_PI ,
1596.BR FUTEX_CMP_REQUEUE_PI )
4b35dc5d 1597The thread ID in the futex word at
0b0e4934
MK
1598.I uaddr
1599does not exist.
1600.TP
360f773c
MK
1601.BR ESRCH
1602.RB ( FUTEX_CMP_REQUEUE_PI )
4b35dc5d 1603The thread ID in the futex word at
360f773c
MK
1604.I uaddr2
1605does not exist.
1606.TP
9f6c40c0 1607.B ETIMEDOUT
4d85047f
MK
1608The operation in
1609.IR futex_op
1610employed the timeout specified in
1611.IR timeout ,
1612and the timeout expired before the operation completed.
70b06b90
MK
1613.\"
1614.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1615.\"
47297adb 1616.SH VERSIONS
a1d5f77c 1617.PP
81c9d87e
MK
1618Futexes were first made available in a stable kernel release
1619with Linux 2.6.0.
1620
4c8cb0ff
MK
1621Initial futex support was merged in Linux 2.5.7 but with different
1622semantics from what was described above.
52dee70e 1623A four-argument system call with the semantics
fd3fa7ef 1624described in this page was introduced in Linux 2.5.40.
d0442d14
MK
1625A fifth argument was added in Linux 2.5.70,
1626and a sixth argument was added in Linux 2.6.7.
47297adb 1627.SH CONFORMING TO
8382f16d 1628This system call is Linux-specific.
47297adb 1629.SH NOTES
baf0f1f4
MK
1630Glibc does not provide a wrapper for this system call; call it using
1631.BR syscall (2).
cf44281c 1632
02f7b623
MK
1633Several higher-level programming abstractions are implemented via futexes,
1634including POSIX semaphores and
1635various POSIX threads synchronization mechanisms
1636(mutexes, condition variables, read-write locks, and barriers).
74f58a64
MK
1637.\" TODO FIXME(Torvald) Above, we cite this section and claim it contains
1638.\" details on the synchronization semantics; add the C11 equivalents
1639.\" here (or whatever we find consensus for).
305cc415
MK
1640.\"
1641.\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1642.\"
1643.SH EXAMPLE
1644.\" FIXME Is it worth having an example program?
1645.\" FIXME Anything obviously broken in the example program?
1646.\"
bc54ed38
MK
1647The program below demonstrates use of futexes in a program where a parent
1648process and a child process use a pair of futexes located inside a
305cc415
MK
1649shared anonymous mapping to synchronize access to a shared resource:
1650the terminal.
1651The two processes each write
1652.IR nloops
1653(a command-line argument that defaults to 5 if omitted)
1654messages to the terminal and employ a synchronization protocol
1655that ensures that they alternate in writing messages.
1656Upon running this program we see output such as the following:
1657
1658.in +4n
1659.nf
1660$ \fB./futex_demo\fP
1661Parent (18534) 0
1662Child (18535) 0
1663Parent (18534) 1
1664Child (18535) 1
1665Parent (18534) 2
1666Child (18535) 2
1667Parent (18534) 3
1668Child (18535) 3
1669Parent (18534) 4
1670Child (18535) 4
1671.fi
1672.in
1673.SS Program source
1674\&
1675.nf
1676/* futex_demo.c
1677
1678 Usage: futex_demo [nloops]
1679 (Default: 5)
1680
1681 Demonstrate the use of futexes in a program where parent and child
1682 use a pair of futexes located inside a shared anonymous mapping to
1683 synchronize access to a shared resource: the terminal. The two
1684 processes each write \(aqnum\-loops\(aq messages to the terminal and employ
1685 a synchronization protocol that ensures that they alternate in
1686 writing messages.
1687*/
1688#define _GNU_SOURCE
1689#include <stdio.h>
1690#include <errno.h>
1691#include <stdlib.h>
1692#include <unistd.h>
1693#include <sys/wait.h>
1694#include <sys/mman.h>
1695#include <sys/syscall.h>
1696#include <linux/futex.h>
1697#include <sys/time.h>
1698
1699#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
1700 } while (0)
1701
1702static int *futex1, *futex2, *iaddr;
1703
1704static int
1705futex(int *uaddr, int futex_op, int val,
1706 const struct timespec *timeout, int *uaddr2, int val3)
1707{
1708 return syscall(SYS_futex, uaddr, futex_op, val,
1709 timeout, uaddr, val3);
1710}
1711
1712/* Acquire the futex pointed to by \(aqfutexp\(aq: wait for its value to
1713 become 1, and then set the value to 0. */
1714
1715static void
1716fwait(int *futexp)
1717{
1718 int s;
1719
1720 /* __sync_bool_compare_and_swap(ptr, oldval, newval) is a gcc
1721 built\-in function. It atomically performs the equivalent of:
1722
1723 if (*ptr == oldval)
1724 *ptr = newval;
1725
1726 It returns true if the test yielded true and *ptr was updated.
1727 The alternative here would be to employ the equivalent atomic
1728 machine\-language instructions. For further information, see
1729 the GCC Manual. */
1730
305cc415 1731 while (1) {
83e80dda 1732
63ad44cb 1733 /* Is the futex available? */
83e80dda 1734
305cc415
MK
1735 if (__sync_bool_compare_and_swap(futexp, 1, 0))
1736 break; /* Yes */
1737
63ad44cb 1738 /* Futex is not available; wait */
83e80dda 1739
63ad44cb
HS
1740 s = futex(futexp, FUTEX_WAIT, 0, NULL, NULL, 0);
1741 if (s == \-1 && errno != EAGAIN)
1742 errExit("futex\-FUTEX_WAIT");
305cc415
MK
1743 }
1744}
1745
1746/* Release the futex pointed to by \(aqfutexp\(aq: if the futex currently
1747 has the value 0, set its value to 1 and the wake any futex waiters,
1748 so that if the peer is blocked in fpost(), it can proceed. */
1749
1750static void
1751fpost(int *futexp)
1752{
1753 int s;
1754
1755 /* __sync_bool_compare_and_swap() was described in comments above */
1756
1757 if (__sync_bool_compare_and_swap(futexp, 0, 1)) {
1758
1759 s = futex(futexp, FUTEX_WAKE, 1, NULL, NULL, 0);
1760 if (s == \-1)
1761 errExit("futex\-FUTEX_WAKE");
1762 }
1763}
1764
1765int
1766main(int argc, char *argv[])
1767{
1768 pid_t childPid;
1769 int j, nloops;
1770
1771 setbuf(stdout, NULL);
1772
1773 nloops = (argc > 1) ? atoi(argv[1]) : 5;
1774
1775 /* Create a shared anonymous mapping that will hold the futexes.
1776 Since the futexes are being shared between processes, we
1777 subsequently use the "shared" futex operations (i.e., not the
1778 ones suffixed "_PRIVATE") */
1779
1780 iaddr = mmap(NULL, sizeof(int) * 2, PROT_READ | PROT_WRITE,
1781 MAP_ANONYMOUS | MAP_SHARED, \-1, 0);
1782 if (iaddr == MAP_FAILED)
1783 errExit("mmap");
1784
1785 futex1 = &iaddr[0];
1786 futex2 = &iaddr[1];
1787
1788 *futex1 = 0; /* State: unavailable */
1789 *futex2 = 1; /* State: available */
1790
1791 /* Create a child process that inherits the shared anonymous
35764662 1792 mapping */
305cc415
MK
1793
1794 childPid = fork();
92a46690 1795 if (childPid == \-1)
305cc415
MK
1796 errExit("fork");
1797
1798 if (childPid == 0) { /* Child */
1799 for (j = 0; j < nloops; j++) {
1800 fwait(futex1);
1801 printf("Child (%ld) %d\\n", (long) getpid(), j);
1802 fpost(futex2);
1803 }
1804
1805 exit(EXIT_SUCCESS);
1806 }
1807
1808 /* Parent falls through to here */
1809
1810 for (j = 0; j < nloops; j++) {
1811 fwait(futex2);
1812 printf("Parent (%ld) %d\\n", (long) getpid(), j);
1813 fpost(futex1);
1814 }
1815
1816 wait(NULL);
1817
1818 exit(EXIT_SUCCESS);
1819}
1820.fi
47297adb 1821.SH SEE ALSO
4c222281 1822.ad l
9913033c 1823.BR get_robust_list (2),
d806bc05 1824.BR restart_syscall (2),
e0074751 1825.BR pthread_mutexattr_getprotocol (3),
ac894879
MK
1826.BR futex (7),
1827.BR sched (7)
fea681da 1828.PP
f5ad572f
MK
1829The following kernel source files:
1830.IP * 2
1831.I Documentation/pi-futex.txt
1832.IP *
1833.I Documentation/futex-requeue-pi.txt
1834.IP *
1835.I Documentation/locking/rt-mutex.txt
1836.IP *
1837.I Documentation/locking/rt-mutex-design.txt
8fe019c7
MK
1838.IP *
1839.I Documentation/robust-futex-ABI.txt
43b99089 1840.PP
4c222281 1841Franke, H., Russell, R., and Kirwood, M., 2002.
52087dd3 1842\fIFuss, Futexes and Furwocks: Fast Userlevel Locking in Linux\fP
4c222281 1843(from proceedings of the Ottawa Linux Symposium 2002),
9b936e9e 1844.br
608bf950
SK
1845.UR http://kernel.org\:/doc\:/ols\:/2002\:/ols2002-pages-479-495.pdf
1846.UE
f42eb21b 1847
4c222281 1848Hart, D., 2009. \fIA futex overview and update\fP,
2ed26199
MK
1849.UR http://lwn.net/Articles/360699/
1850.UE
1851
4c222281 1852Hart, D. and Guniguntala, D., 2009.
0483b6cc 1853\fIRequeue-PI: Making Glibc Condvars PI-Aware\fP
4c222281 1854(from proceedings of the 2009 Real-Time Linux Workshop),
0483b6cc
MK
1855.UR http://lwn.net/images/conf/rtlws11/papers/proc/p10.pdf
1856.UE
1857
4c222281 1858Drepper, U., 2011. \fIFutexes Are Tricky\fP,
f42eb21b
MK
1859.UR http://www.akkadia.org/drepper/futex.pdf
1860.UE
9b936e9e
MK
1861.PP
1862Futex example library, futex-*.tar.bz2 at
1863.br
a605264d 1864.UR ftp://ftp.kernel.org\:/pub\:/linux\:/kernel\:/people\:/rusty/
608bf950 1865.UE
34f14794
MK
1866.\"
1867.\" FIXME Are there any other resources that should be listed
1868.\" in the SEE ALSO section?
74f58a64 1869.\" FIXME(Torvald) We should probably refer to the glibc code here, in
4c8cb0ff
MK
1870.\" particular the glibc-internal futex wrapper functions that are
1871.\" WIP, and the generic pthread_mutex_t and perhaps condvar
1872.\" implementations.