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