futex.2: Document FUTEX_LOCK_PI2
[man-pages.git] / man2 / futex.2
blob2f340e0e0aad104ec51a4ea954efd7c08adc2d69
1 .\" Page by b.hubert
2 .\" and Copyright (C) 2015, Thomas Gleixner <tglx@linutronix.de>
3 .\" and Copyright (C) 2015, Michael Kerrisk <mtk.manpages@gmail.com>
4 .\"
5 .\" %%%LICENSE_START(FREELY_REDISTRIBUTABLE)
6 .\" may be freely modified and distributed
7 .\" %%%LICENSE_END
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 .\"
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 .\"
19 .\" FIXME Do we need to add some text regarding Torvald Riegel's 2015-01-24 mail
20 .\"       http://thread.gmane.org/gmane.linux.kernel/1703405/focus=1873242
21 .\"
22 .TH FUTEX 2 2021-03-22 "Linux" "Linux Programmer's Manual"
23 .SH NAME
24 futex \- fast user-space locking
25 .SH SYNOPSIS
26 .nf
27 .PP
28 .BR "#include <linux/futex.h>" "      /* Definition of " FUTEX_* " constants */"
29 .BR "#include <sys/syscall.h>" "      /* Definition of " SYS_* " constants */"
30 .B #include <unistd.h>
31 .PP
32 .BI "long syscall(SYS_futex, uint32_t *" uaddr ", int " futex_op \
33 ", uint32_t " val ,
34 .BI "             const struct timespec *" timeout , \
35 " \fR  /* or: \fBuint32_t \fIval2\fP */"
36 .BI "             uint32_t *" uaddr2 ", uint32_t " val3 );
37 .fi
38 .PP
39 .IR Note :
40 glibc provides no wrapper for
41 .BR futex (),
42 necessitating the use of
43 .BR syscall (2).
44 .SH DESCRIPTION
45 The
46 .BR futex ()
47 system call provides a method for waiting until a certain condition becomes
48 true.
49 It is typically used as a blocking construct in the context of
50 shared-memory synchronization.
51 When using futexes, the majority of
52 the synchronization operations are performed in user space.
53 A user-space program employs the
54 .BR futex ()
55 system call only when it is likely that the program has to block for
56 a longer time until the condition becomes true.
57 Other
58 .BR futex ()
59 operations can be used to wake any processes or threads waiting
60 for a particular condition.
61 .PP
62 A futex is a 32-bit value\(emreferred to below as a
63 .IR "futex word" \(emwhose
64 address is supplied to the
65 .BR futex ()
66 system call.
67 (Futexes are 32 bits in size on all platforms, including 64-bit systems.)
68 All futex operations are governed by this value.
69 In order to share a futex between processes,
70 the futex is placed in a region of shared memory,
71 created using (for example)
72 .BR mmap (2)
74 .BR shmat (2).
75 (Thus, the futex word may have different
76 virtual addresses in different processes,
77 but these addresses all refer to the same location in physical memory.)
78 In a multithreaded program, it is sufficient to place the futex word
79 in a global variable shared by all threads.
80 .PP
81 When executing a futex operation that requests to block a thread,
82 the kernel will block only if the futex word has the value that the
83 calling thread supplied (as one of the arguments of the
84 .BR futex ()
85 call) as the expected value of the futex word.
86 The loading of the futex word's value,
87 the comparison of that value with the expected value,
88 and the actual blocking will happen atomically and will be totally ordered
89 with respect to concurrent operations performed by other threads
90 on the same futex word.
91 .\" Notes from Darren Hart (Dec 2015):
92 .\"     Totally ordered with respect futex operations refers to semantics
93 .\"     of the ACQUIRE/RELEASE operations and how they impact ordering of
94 .\"     memory reads and writes. The kernel futex operations are protected
95 .\"     by spinlocks, which ensure that all operations are serialized
96 .\"     with respect to one another.
97 .\"
98 .\"     This is a lot to attempt to define in this document. Perhaps a
99 .\"     reference to linux/Documentation/memory-barriers.txt as a footnote
100 .\"     would be sufficient? Or perhaps for this manual, "serialized" would
101 .\"     be sufficient, with a footnote regarding "totally ordered" and a
102 .\"     pointer to the memory-barrier documentation?
103 Thus, the futex word is used to connect the synchronization in user space
104 with the implementation of blocking by the kernel.
105 Analogously to an atomic
106 compare-and-exchange operation that potentially changes shared memory,
107 blocking via a futex is an atomic compare-and-block operation.
108 .\" FIXME(Torvald Riegel):
109 .\" Eventually we want to have some text in NOTES to satisfy
110 .\" the reference in the following sentence
111 .\"     See NOTES for a detailed specification of
112 .\"     the synchronization semantics.
114 One use of futexes is for implementing locks.
115 The state of the lock (i.e., acquired or not acquired)
116 can be represented as an atomically accessed flag in shared memory.
117 In the uncontended case,
118 a thread can access or modify the lock state with atomic instructions,
119 for example atomically changing it from not acquired to acquired
120 using an atomic compare-and-exchange instruction.
121 (Such instructions are performed entirely in user mode,
122 and the kernel maintains no information about the lock state.)
123 On the other hand, a thread may be unable to acquire a lock because
124 it is already acquired by another thread.
125 It then may pass the lock's flag as a futex word and the value
126 representing the acquired state as the expected value to a
127 .BR futex ()
128 wait operation.
129 This
130 .BR futex ()
131 operation will block if and only if the lock is still acquired
132 (i.e., the value in the futex word still matches the "acquired state").
133 When releasing the lock, a thread has to first reset the
134 lock state to not acquired and then execute a futex
135 operation that wakes threads blocked on the lock flag used as a futex word
136 (this can be further optimized to avoid unnecessary wake-ups).
138 .BR futex (7)
139 for more detail on how to use futexes.
141 Besides the basic wait and wake-up futex functionality, there are further
142 futex operations aimed at supporting more complex use cases.
144 Note that
145 no explicit initialization or destruction is necessary to use futexes;
146 the kernel maintains a futex
147 (i.e., the kernel-internal implementation artifact)
148 only while operations such as
149 .BR FUTEX_WAIT ,
150 described below, are being performed on a particular futex word.
152 .SS Arguments
154 .I uaddr
155 argument points to the futex word.
156 On all platforms, futexes are four-byte
157 integers that must be aligned on a four-byte boundary.
158 The operation to perform on the futex is specified in the
159 .I futex_op
160 argument;
161 .IR val
162 is a value whose meaning and purpose depends on
163 .IR futex_op .
165 The remaining arguments
166 .RI ( timeout ,
167 .IR uaddr2 ,
169 .IR val3 )
170 are required only for certain of the futex operations described below.
171 Where one of these arguments is not required, it is ignored.
173 For several blocking operations, the
174 .I timeout
175 argument is a pointer to a
176 .IR timespec
177 structure that specifies a timeout for the operation.
178 However,  notwithstanding the prototype shown above, for some operations,
179 the least significant four bytes of this argument are instead
180 used as an integer whose meaning is determined by the operation.
181 For these operations, the kernel casts the
182 .I timeout
183 value first to
184 .IR "unsigned long",
185 then to
186 .IR uint32_t ,
187 and in the remainder of this page, this argument is referred to as
188 .I val2
189 when interpreted in this fashion.
191 Where it is required, the
192 .IR uaddr2
193 argument is a pointer to a second futex word that is employed
194 by the operation.
196 The interpretation of the final integer argument,
197 .IR val3 ,
198 depends on the operation.
200 .\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
202 .SS Futex operations
204 .I futex_op
205 argument consists of two parts:
206 a command that specifies the operation to be performed,
207 bitwise ORed with zero or more options that
208 modify the behaviour of the operation.
209 The options that may be included in
210 .I futex_op
211 are as follows:
213 .BR FUTEX_PRIVATE_FLAG " (since Linux 2.6.22)"
214 .\" commit 34f01cc1f512fa783302982776895c73714ebbc2
215 This option bit can be employed with all futex operations.
216 It tells the kernel that the futex is process-private and not shared
217 with another process (i.e., it is being used for synchronization
218 only between threads of the same process).
219 This allows the kernel to make some additional performance optimizations.
220 .\" I.e., It allows the kernel choose the fast path for validating
221 .\" the user-space address and avoids expensive VMA lookups,
222 .\" taking reference counts on file backing store, and so on.
224 As a convenience,
225 .IR <linux/futex.h>
226 defines a set of constants with the suffix
227 .BR _PRIVATE
228 that are equivalents of all of the operations listed below,
229 .\" except the obsolete FUTEX_FD, for which the "private" flag was
230 .\" meaningless
231 but with the
232 .BR FUTEX_PRIVATE_FLAG
233 ORed into the constant value.
234 Thus, there are
235 .BR FUTEX_WAIT_PRIVATE ,
236 .BR FUTEX_WAKE_PRIVATE ,
237 and so on.
239 .BR FUTEX_CLOCK_REALTIME " (since Linux 2.6.28)"
240 .\" commit 1acdac104668a0834cfa267de9946fac7764d486
241 This option bit can be employed only with the
242 .BR FUTEX_WAIT_BITSET ,
243 .BR FUTEX_WAIT_REQUEUE_PI ,
244 (since Linux 4.5)
245 .\" commit 337f13046ff03717a9e99675284a817527440a49
246 .BR FUTEX_WAIT ,
248 (since Linux v5.14.0)
249 .\" commit bf22a6976897977b0a3f1aeba6823c959fc4fdae
250 .BR FUTEX_LOCK_PI2
251 operations.
253 If this option is set, the kernel measures the
254 .I timeout
255 against the
256 .BR CLOCK_REALTIME
257 clock.
259 If this option is not set, the kernel measures the
260 .I timeout
261 against the
262 .BR CLOCK_MONOTONIC
263 clock.
265 The operation specified in
266 .I futex_op
267 is one of the following:
269 .\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
272 .BR FUTEX_WAIT " (since Linux 2.6.0)"
273 .\" Strictly speaking, since some time in 2.5.x
274 This operation tests that the value at the
275 futex word pointed to by the address
276 .I uaddr
277 still contains the expected value
278 .IR val ,
279 and if so, then sleeps waiting for a
280 .B FUTEX_WAKE
281 operation on the futex word.
282 The load of the value of the futex word is an atomic memory
283 access (i.e., using atomic machine instructions of the respective
284 architecture).
285 This load, the comparison with the expected value, and
286 starting to sleep are performed atomically
287 .\" FIXME: Torvald, I think we may need to add some explanation of
288 .\" "totally ordered" here.
289 and totally ordered
290 with respect to other futex operations on the same futex word.
291 If the thread starts to sleep,
292 it is considered a waiter on this futex word.
293 If the futex value does not match
294 .IR val ,
295 then the call fails immediately with the error
296 .BR EAGAIN .
298 The purpose of the comparison with the expected value is to prevent lost
299 wake-ups.
300 If another thread changed the value of the futex word after the
301 calling thread decided to block based on the prior value,
302 and if the other thread executed a
303 .BR FUTEX_WAKE
304 operation (or similar wake-up) after the value change and before this
305 .BR FUTEX_WAIT
306 operation, then the calling thread will observe the
307 value change and will not start to sleep.
309 If the
310 .I timeout
311 is not NULL, the structure it points to specifies a
312 timeout for the wait.
313 (This interval will be rounded up to the system clock granularity,
314 and is guaranteed not to expire early.)
315 The timeout is by default measured according to the
316 .BR CLOCK_MONOTONIC
317 clock, but, since Linux 4.5, the
318 .BR CLOCK_REALTIME
319 clock can be selected by specifying
320 .BR FUTEX_CLOCK_REALTIME
322 .IR futex_op .
324 .I timeout
325 is NULL, the call blocks indefinitely.
327 .IR Note :
329 .BR FUTEX_WAIT ,
330 .IR timeout
331 is interpreted as a
332 .IR relative
333 value.
334 This differs from other futex operations, where
335 .I timeout
336 is interpreted as an absolute value.
337 To obtain the equivalent of
338 .BR FUTEX_WAIT
339 with an absolute timeout, employ
340 .BR FUTEX_WAIT_BITSET
341 with
342 .IR val3
343 specified as
344 .BR FUTEX_BITSET_MATCH_ANY .
346 The arguments
347 .I uaddr2
349 .I val3
350 are ignored.
351 .\" FIXME . (Torvald) I think we should remove this.  Or maybe adapt to a
352 .\" different example.
354 .\"     For
355 .\"     .BR futex (7),
356 .\"     this call is executed if decrementing the count gave a negative value
357 .\"     (indicating contention),
358 .\"     and will sleep until another process or thread releases
359 .\"     the futex and executes the
360 .\"     .B FUTEX_WAKE
361 .\"     operation.
363 .\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
366 .BR FUTEX_WAKE " (since Linux 2.6.0)"
367 .\" Strictly speaking, since Linux 2.5.x
368 This operation wakes at most
369 .I val
370 of the waiters that are waiting (e.g., inside
371 .BR FUTEX_WAIT )
372 on the futex word at the address
373 .IR uaddr .
374 Most commonly,
375 .I val
376 is specified as either 1 (wake up a single waiter) or
377 .BR INT_MAX
378 (wake up all waiters).
379 No guarantee is provided about which waiters are awoken
380 (e.g., a waiter with a higher scheduling priority is not guaranteed
381 to be awoken in preference to a waiter with a lower priority).
383 The arguments
384 .IR timeout ,
385 .IR uaddr2 ,
387 .I val3
388 are ignored.
389 .\" FIXME . (Torvald) I think we should remove this.  Or maybe adapt to
390 .\" a different example.
392 .\"     For
393 .\"     .BR futex (7),
394 .\"     this is executed if incrementing the count showed that
395 .\"     there were waiters,
396 .\"     once the futex value has been set to 1
397 .\"     (indicating that it is available).
399 .\" How does "incrementing the count show that there were waiters"?
401 .\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
404 .BR FUTEX_FD " (from Linux 2.6.0 up to and including Linux 2.6.25)"
405 .\" Strictly speaking, from Linux 2.5.x to 2.6.25
406 This operation creates a file descriptor that is associated with
407 the futex at
408 .IR uaddr .
409 The caller must close the returned file descriptor after use.
410 When another process or thread performs a
411 .BR FUTEX_WAKE
412 on the futex word, the file descriptor indicates as being readable with
413 .BR select (2),
414 .BR poll (2),
416 .BR epoll (7)
418 The file descriptor can be used to obtain asynchronous notifications: if
419 .I val
420 is nonzero, then, when another process or thread executes a
421 .BR FUTEX_WAKE ,
422 the caller will receive the signal number that was passed in
423 .IR val .
425 The arguments
426 .IR timeout ,
427 .IR uaddr2 ,
429 .I val3
430 are ignored.
432 Because it was inherently racy,
433 .B FUTEX_FD
434 has been removed
435 .\" commit 82af7aca56c67061420d618cc5a30f0fd4106b80
436 from Linux 2.6.26 onward.
438 .\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
441 .BR FUTEX_REQUEUE " (since Linux 2.6.0)"
442 This operation performs the same task as
443 .BR FUTEX_CMP_REQUEUE
444 (see below), except that no check is made using the value in
445 .IR  val3 .
446 (The argument
447 .I val3
448 is ignored.)
450 .\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
453 .BR FUTEX_CMP_REQUEUE " (since Linux 2.6.7)"
454 This operation first checks whether the location
455 .I uaddr
456 still contains the value
457 .IR val3 .
458 If not, the operation fails with the error
459 .BR EAGAIN .
460 Otherwise, the operation wakes up a maximum of
461 .I val
462 waiters that are waiting on the futex at
463 .IR uaddr .
464 If there are more than
465 .I val
466 waiters, then the remaining waiters are removed
467 from the wait queue of the source futex at
468 .I uaddr
469 and added to the wait queue of the target futex at
470 .IR uaddr2 .
472 .I val2
473 argument specifies an upper limit on the number of waiters
474 that are requeued to the futex at
475 .IR uaddr2 .
477 .\" FIXME(Torvald) Is the following correct?  Or is just the decision
478 .\" which threads to wake or requeue part of the atomic operation?
479 The load from
480 .I uaddr
481 is an atomic memory access (i.e., using atomic machine instructions of
482 the respective architecture).
483 This load, the comparison with
484 .IR val3 ,
485 and the requeueing of any waiters are performed atomically and totally
486 ordered with respect to other operations on the same futex word.
487 .\" Notes from a f2f conversation with Thomas Gleixner (Aug 2015): ###
488 .\"     The operation is serialized with respect to operations on both
489 .\"     source and target futex. No other waiter can enqueue itself
490 .\"     for waiting and no other waiter can dequeue itself because of
491 .\"     a timeout or signal.
493 Typical values to specify for
494 .I val
495 are 0 or 1.
496 (Specifying
497 .BR INT_MAX
498 is not useful, because it would make the
499 .BR FUTEX_CMP_REQUEUE
500 operation equivalent to
501 .BR FUTEX_WAKE .)
502 The limit value specified via
503 .I val2
504 is typically either 1 or
505 .BR INT_MAX .
506 (Specifying the argument as 0 is not useful, because it would make the
507 .BR FUTEX_CMP_REQUEUE
508 operation equivalent to
509 .BR FUTEX_WAIT .)
512 .B FUTEX_CMP_REQUEUE
513 operation was added as a replacement for the earlier
514 .BR FUTEX_REQUEUE .
515 The difference is that the check of the value at
516 .I uaddr
517 can be used to ensure that requeueing happens only under certain
518 conditions, which allows race conditions to be avoided in certain use cases.
519 .\" But, as Rich Felker points out, there remain valid use cases for
520 .\" FUTEX_REQUEUE, for example, when the calling thread is requeuing
521 .\" the target(s) to a lock that the calling thread owns
522 .\"     From: Rich Felker <dalias@libc.org>
523 .\"     Date: Wed, 29 Oct 2014 22:43:17 -0400
524 .\"     To: Darren Hart <dvhart@infradead.org>
525 .\"     CC: libc-alpha@sourceware.org, ...
526 .\"     Subject: Re: Add futex wrapper to glibc?
528 Both
529 .BR FUTEX_REQUEUE
531 .BR FUTEX_CMP_REQUEUE
532 can be used to avoid "thundering herd" wake-ups that could occur when using
533 .B FUTEX_WAKE
534 in cases where all of the waiters that are woken need to acquire
535 another futex.
536 Consider the following scenario,
537 where multiple waiter threads are waiting on B,
538 a wait queue implemented using a futex:
540 .in +4n
542 lock(A)
543 while (!check_value(V)) {
544     unlock(A);
545     block_on(B);
546     lock(A);
548 unlock(A);
552 If a waker thread used
553 .BR FUTEX_WAKE ,
554 then all waiters waiting on B would be woken up,
555 and they would all try to acquire lock A.
556 However, waking all of the threads in this manner would be pointless because
557 all except one of the threads would immediately block on lock A again.
558 By contrast, a requeue operation wakes just one waiter and moves
559 the other waiters to lock A,
560 and when the woken waiter unlocks A then the next waiter can proceed.
562 .\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
565 .BR FUTEX_WAKE_OP " (since Linux 2.6.14)"
566 .\" commit 4732efbeb997189d9f9b04708dc26bf8613ed721
567 .\"     Author: Jakub Jelinek <jakub@redhat.com>
568 .\"     Date:   Tue Sep 6 15:16:25 2005 -0700
569 .\" FIXME. (Torvald) The glibc condvar implementation is currently being
570 .\"     revised (e.g., to not use an internal lock anymore).
571 .\"     It is probably more future-proof to remove this paragraph.
572 .\" [Torvald, do you have an update here?]
573 This operation was added to support some user-space use cases
574 where more than one futex must be handled at the same time.
575 The most notable example is the implementation of
576 .BR pthread_cond_signal (3),
577 which requires operations on two futexes,
578 the one used to implement the mutex and the one used in the implementation
579 of the wait queue associated with the condition variable.
580 .BR FUTEX_WAKE_OP
581 allows such cases to be implemented without leading to
582 high rates of contention and context switching.
585 .BR FUTEX_WAKE_OP
586 operation is equivalent to executing the following code atomically
587 and totally ordered with respect to other futex operations on
588 any of the two supplied futex words:
590 .in +4n
592 uint32_t oldval = *(uint32_t *) uaddr2;
593 *(uint32_t *) uaddr2 = oldval \fIop\fP \fIoparg\fP;
594 futex(uaddr, FUTEX_WAKE, val, 0, 0, 0);
595 if (oldval \fIcmp\fP \fIcmparg\fP)
596     futex(uaddr2, FUTEX_WAKE, val2, 0, 0, 0);
600 In other words,
601 .BR FUTEX_WAKE_OP
602 does the following:
604 .IP * 3
605 saves the original value of the futex word at
606 .IR uaddr2
607 and performs an operation to modify the value of the futex at
608 .IR uaddr2 ;
609 this is an atomic read-modify-write memory access (i.e., using atomic
610 machine instructions of the respective architecture)
611 .IP *
612 wakes up a maximum of
613 .I val
614 waiters on the futex for the futex word at
615 .IR uaddr ;
617 .IP *
618 dependent on the results of a test of the original value of the
619 futex word at
620 .IR uaddr2 ,
621 wakes up a maximum of
622 .I val2
623 waiters on the futex for the futex word at
624 .IR uaddr2 .
627 The operation and comparison that are to be performed are encoded
628 in the bits of the argument
629 .IR val3 .
630 Pictorially, the encoding is:
632 .in +4n
634 +---+---+-----------+-----------+
635 |op |cmp|   oparg   |  cmparg   |
636 +---+---+-----------+-----------+
637   4   4       12          12    <== # of bits
641 Expressed in code, the encoding is:
643 .in +4n
645 #define FUTEX_OP(op, oparg, cmp, cmparg) \e
646                 (((op & 0xf) << 28) | \e
647                 ((cmp & 0xf) << 24) | \e
648                 ((oparg & 0xfff) << 12) | \e
649                 (cmparg & 0xfff))
653 In the above,
654 .I op
656 .I cmp
657 are each one of the codes listed below.
659 .I oparg
661 .I cmparg
662 components are literal numeric values, except as noted below.
665 .I op
666 component has one of the following values:
668 .in +4n
670 FUTEX_OP_SET        0  /* uaddr2 = oparg; */
671 FUTEX_OP_ADD        1  /* uaddr2 += oparg; */
672 FUTEX_OP_OR         2  /* uaddr2 |= oparg; */
673 FUTEX_OP_ANDN       3  /* uaddr2 &= \(tioparg; */
674 FUTEX_OP_XOR        4  /* uaddr2 \(ha= oparg; */
678 In addition, bitwise ORing the following value into
679 .I op
680 causes
681 .IR "(1\ <<\ oparg)"
682 to be used as the operand:
684 .in +4n
686 FUTEX_OP_ARG_SHIFT  8  /* Use (1 << oparg) as operand */
691 .I cmp
692 field is one of the following:
694 .in +4n
696 FUTEX_OP_CMP_EQ     0  /* if (oldval == cmparg) wake */
697 FUTEX_OP_CMP_NE     1  /* if (oldval != cmparg) wake */
698 FUTEX_OP_CMP_LT     2  /* if (oldval < cmparg) wake */
699 FUTEX_OP_CMP_LE     3  /* if (oldval <= cmparg) wake */
700 FUTEX_OP_CMP_GT     4  /* if (oldval > cmparg) wake */
701 FUTEX_OP_CMP_GE     5  /* if (oldval >= cmparg) wake */
705 The return value of
706 .BR FUTEX_WAKE_OP
707 is the sum of the number of waiters woken on the futex
708 .IR uaddr
709 plus the number of waiters woken on the futex
710 .IR uaddr2 .
712 .\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
715 .BR FUTEX_WAIT_BITSET " (since Linux 2.6.25)"
716 .\" commit cd689985cf49f6ff5c8eddc48d98b9d581d9475d
717 This operation is like
718 .BR FUTEX_WAIT
719 except that
720 .I val3
721 is used to provide a 32-bit bit mask to the kernel.
722 This bit mask, in which at least one bit must be set,
723 is stored in the kernel-internal state of the waiter.
724 See the description of
725 .BR FUTEX_WAKE_BITSET
726 for further details.
729 .I timeout
730 is not NULL, the structure it points to specifies
731 an absolute timeout for the wait operation.
733 .I timeout
734 is NULL, the operation can block indefinitely.
737 .I uaddr2
738 argument is ignored.
740 .\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
743 .BR FUTEX_WAKE_BITSET " (since Linux 2.6.25)"
744 .\" commit cd689985cf49f6ff5c8eddc48d98b9d581d9475d
745 This operation is the same as
746 .BR FUTEX_WAKE
747 except that the
748 .I val3
749 argument is used to provide a 32-bit bit mask to the kernel.
750 This bit mask, in which at least one bit must be set,
751 is used to select which waiters should be woken up.
752 The selection is done by a bitwise AND of the "wake" bit mask
753 (i.e., the value in
754 .IR val3 )
755 and the bit mask which is stored in the kernel-internal
756 state of the waiter (the "wait" bit mask that is set using
757 .BR FUTEX_WAIT_BITSET ).
758 All of the waiters for which the result of the AND is nonzero are woken up;
759 the remaining waiters are left sleeping.
761 The effect of
762 .BR FUTEX_WAIT_BITSET
764 .BR FUTEX_WAKE_BITSET
765 is to allow selective wake-ups among multiple waiters that are blocked
766 on the same futex.
767 However, note that, depending on the use case,
768 employing this bit-mask multiplexing feature on a
769 futex can be less efficient than simply using multiple futexes,
770 because employing bit-mask multiplexing requires the kernel
771 to check all waiters on a futex,
772 including those that are not interested in being woken up
773 (i.e., they do not have the relevant bit set in their "wait" bit mask).
774 .\" According to http://locklessinc.com/articles/futex_cheat_sheet/:
776 .\"    "The original reason for the addition of these extensions
777 .\"     was to improve the performance of pthread read-write locks
778 .\"     in glibc. However, the pthreads library no longer uses the
779 .\"     same locking algorithm, and these extensions are not used
780 .\"     without the bitset parameter being all ones.
782 .\" The page goes on to note that the FUTEX_WAIT_BITSET operation
783 .\" is nevertheless used (with a bit mask of all ones) in order to
784 .\" obtain the absolute timeout functionality that is useful
785 .\" for efficiently implementing Pthreads APIs (which use absolute
786 .\" timeouts); FUTEX_WAIT provides only relative timeouts.
788 The constant
789 .BR FUTEX_BITSET_MATCH_ANY ,
790 which corresponds to all 32 bits set in the bit mask, can be used as the
791 .I val3
792 argument for
793 .BR FUTEX_WAIT_BITSET
795 .BR FUTEX_WAKE_BITSET .
796 Other than differences in the handling of the
797 .I timeout
798 argument, the
799 .BR FUTEX_WAIT
800 operation is equivalent to
801 .BR FUTEX_WAIT_BITSET
802 with
803 .IR val3
804 specified as
805 .BR FUTEX_BITSET_MATCH_ANY ;
806 that is, allow a wake-up by any waker.
808 .BR FUTEX_WAKE
809 operation is equivalent to
810 .BR FUTEX_WAKE_BITSET
811 with
812 .IR val3
813 specified as
814 .BR FUTEX_BITSET_MATCH_ANY ;
815 that is, wake up any waiter(s).
818 .I uaddr2
820 .I timeout
821 arguments are ignored.
823 .\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
825 .SS Priority-inheritance futexes
826 Linux supports priority-inheritance (PI) futexes in order to handle
827 priority-inversion problems that can be encountered with
828 normal futex locks.
829 Priority inversion is the problem that occurs when a high-priority
830 task is blocked waiting to acquire a lock held by a low-priority task,
831 while tasks at an intermediate priority continuously preempt
832 the low-priority task from the CPU.
833 Consequently, the low-priority task makes no progress toward
834 releasing the lock, and the high-priority task remains blocked.
836 Priority inheritance is a mechanism for dealing with
837 the priority-inversion problem.
838 With this mechanism, when a high-priority task becomes blocked
839 by a lock held by a low-priority task,
840 the priority of the low-priority task is temporarily raised
841 to that of the high-priority task,
842 so that it is not preempted by any intermediate level tasks,
843 and can thus make progress toward releasing the lock.
844 To be effective, priority inheritance must be transitive,
845 meaning that if a high-priority task blocks on a lock
846 held by a lower-priority task that is itself blocked by a lock
847 held by another intermediate-priority task
848 (and so on, for chains of arbitrary length),
849 then both of those tasks
850 (or more generally, all of the tasks in a lock chain)
851 have their priorities raised to be the same as the high-priority task.
853 From a user-space perspective,
854 what makes a futex PI-aware is a policy agreement (described below)
855 between user space and the kernel about the value of the futex word,
856 coupled with the use of the PI-futex operations described below.
857 (Unlike the other futex operations described above,
858 the PI-futex operations are designed
859 for the implementation of very specific IPC mechanisms.)
861 .\" Quoting Darren Hart:
862 .\"     These opcodes paired with the PI futex value policy (described below)
863 .\"     defines a "futex" as PI aware. These were created very specifically
864 .\"     in support of PI pthread_mutexes, so it makes a lot more sense to
865 .\"     talk about a PI aware pthread_mutex, than a PI aware futex, since
866 .\"     there is a lot of policy and scaffolding that has to be built up
867 .\"     around it to use it properly (this is what a PI pthread_mutex is).
869 .\"       mtk: The following text is drawn from the Hart/Guniguntala paper
870 .\"       (listed in SEE ALSO), but I have reworded some pieces
871 .\"       significantly.
873 The PI-futex operations described below differ from the other
874 futex operations in that they impose policy on the use of the value of the
875 futex word:
876 .IP * 3
877 If the lock is not acquired, the futex word's value shall be 0.
878 .IP *
879 If the lock is acquired, the futex word's value shall
880 be the thread ID (TID;
882 .BR gettid (2))
883 of the owning thread.
884 .IP *
885 If the lock is owned and there are threads contending for the lock,
886 then the
887 .B FUTEX_WAITERS
888 bit shall be set in the futex word's value; in other words, this value is:
890     FUTEX_WAITERS | TID
892 (Note that is invalid for a PI futex word to have no owner and
893 .BR FUTEX_WAITERS
894 set.)
896 With this policy in place,
897 a user-space application can acquire an unacquired
898 lock or release a lock using atomic instructions executed in user mode
899 (e.g., a compare-and-swap operation such as
900 .I cmpxchg
901 on the x86 architecture).
902 Acquiring a lock simply consists of using compare-and-swap to atomically
903 set the futex word's value to the caller's TID if its previous value was 0.
904 Releasing a lock requires using compare-and-swap to set the futex word's
905 value to 0 if the previous value was the expected TID.
907 If a futex is already acquired (i.e., has a nonzero value),
908 waiters must employ the
909 .B FUTEX_LOCK_PI
911 .B FUTEX_LOCK_PI2
912 operations to acquire the lock.
913 If other threads are waiting for the lock, then the
914 .B FUTEX_WAITERS
915 bit is set in the futex value;
916 in this case, the lock owner must employ the
917 .B FUTEX_UNLOCK_PI
918 operation to release the lock.
920 In the cases where callers are forced into the kernel
921 (i.e., required to perform a
922 .BR futex ()
923 call),
924 they then deal directly with a so-called RT-mutex,
925 a kernel locking mechanism which implements the required
926 priority-inheritance semantics.
927 After the RT-mutex is acquired, the futex value is updated accordingly,
928 before the calling thread returns to user space.
930 It is important to note
931 .\" tglx (July 2015):
932 .\"     If there are multiple waiters on a pi futex then a wake pi operation
933 .\"     will wake the first waiter and hand over the lock to this waiter. This
934 .\"     includes handing over the rtmutex which represents the futex in the
935 .\"     kernel. The strict requirement is that the futex owner and the rtmutex
936 .\"     owner must be the same, except for the update period which is
937 .\"     serialized by the futex internal locking. That means the kernel must
938 .\"     update the user-space value prior to returning to user space
939 that the kernel will update the futex word's value prior
940 to returning to user space.
941 (This prevents the possibility of the futex word's value ending
942 up in an invalid state, such as having an owner but the value being 0,
943 or having waiters but not having the
944 .B FUTEX_WAITERS
945 bit set.)
947 If a futex has an associated RT-mutex in the kernel
948 (i.e., there are blocked waiters)
949 and the owner of the futex/RT-mutex dies unexpectedly,
950 then the kernel cleans up the RT-mutex and hands it over to the next waiter.
951 This in turn requires that the user-space value is updated accordingly.
952 To indicate that this is required, the kernel sets the
953 .B FUTEX_OWNER_DIED
954 bit in the futex word along with the thread ID of the new owner.
955 User space can detect this situation via the presence of the
956 .B FUTEX_OWNER_DIED
957 bit and is then responsible for cleaning up the stale state left over by
958 the dead owner.
959 .\" tglx (July 2015):
960 .\"     The FUTEX_OWNER_DIED bit can also be set on uncontended futexes, where
961 .\"     the kernel has no state associated. This happens via the robust futex
962 .\"     mechanism. In that case the futex value will be set to
963 .\"     FUTEX_OWNER_DIED. The robust futex mechanism is also available for non
964 .\"     PI futexes.
966 PI futexes are operated on by specifying one of the values listed below in
967 .IR futex_op .
968 Note that the PI futex operations must be used as paired operations
969 and are subject to some additional requirements:
970 .IP * 3
971 .B FUTEX_LOCK_PI
973 .B FUTEX_LOCK_PI2
975 .B FUTEX_TRYLOCK_PI
976 pair with
977 .BR FUTEX_UNLOCK_PI .
978 .B FUTEX_UNLOCK_PI
979 must be called only on a futex owned by the calling thread,
980 as defined by the value policy, otherwise the error
981 .B EPERM
982 results.
983 .IP *
984 .B FUTEX_WAIT_REQUEUE_PI
985 pairs with
986 .BR FUTEX_CMP_REQUEUE_PI .
987 This must be performed from a non-PI futex to a distinct PI futex
988 (or the error
989 .B EINVAL
990 results).
991 Additionally,
992 .I val
993 (the number of waiters to be woken) must be 1
994 (or the error
995 .B EINVAL
996 results).
998 The PI futex operations are as follows:
1000 .\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1003 .BR FUTEX_LOCK_PI " (since Linux 2.6.18)"
1004 .\" commit c87e2837be82df479a6bae9f155c43516d2feebc
1005 This operation is used after an attempt to acquire
1006 the lock via an atomic user-mode instruction failed
1007 because the futex word has a nonzero value\(emspecifically,
1008 because it contained the (PID-namespace-specific) TID of the lock owner.
1010 The operation checks the value of the futex word at the address
1011 .IR uaddr .
1012 If the value is 0, then the kernel tries to atomically set
1013 the futex value to the caller's TID.
1014 If the futex word's value is nonzero,
1015 the kernel atomically sets the
1016 .B FUTEX_WAITERS
1017 bit, which signals the futex owner that it cannot unlock the futex in
1018 user space atomically by setting the futex value to 0.
1019 .\" tglx (July 2015):
1020 .\"     The operation here is similar to the FUTEX_WAIT logic. When the user
1021 .\"     space atomic acquire does not succeed because the futex value was non
1022 .\"     zero, then the waiter goes into the kernel, takes the kernel internal
1023 .\"     lock and retries the acquisition under the lock. If the acquisition
1024 .\"     does not succeed either, then it sets the FUTEX_WAITERS bit, to signal
1025 .\"     the lock owner that it needs to go into the kernel. Here is the pseudo
1026 .\"     code:
1028 .\"             lock(kernel_lock);
1029 .\"     retry:
1031 .\"             /*
1032 .\"              * Owner might have unlocked in user space before we
1033 .\"              * were able to set the waiter bit.
1034 .\"              */
1035 .\"             if (atomic_acquire(futex) == SUCCESS) {
1036 .\"                unlock(kernel_lock());
1037 .\"                return 0;
1038 .\"             }
1040 .\"             /*
1041 .\"              * Owner might have unlocked after the above atomic_acquire()
1042 .\"              * attempt.
1043 .\"              */
1044 .\"             if (atomic_set_waiters_bit(futex) != SUCCESS)
1045 .\"                goto retry;
1047 .\"             queue_waiter();
1048 .\"             unlock(kernel_lock);
1049 .\"             block();
1051 After that, the kernel:
1053 .IP 1. 3
1054 Tries to find the thread which is associated with the owner TID.
1055 .IP 2.
1056 Creates or reuses kernel state on behalf of the owner.
1057 (If this is the first waiter, there is no kernel state for this
1058 futex, so kernel state is created by locking the RT-mutex
1059 and the futex owner is made the owner of the RT-mutex.
1060 If there are existing waiters, then the existing state is reused.)
1061 .IP 3.
1062 Attaches the waiter to the futex
1063 (i.e., the waiter is enqueued on the RT-mutex waiter list).
1066 If more than one waiter exists,
1067 the enqueueing of the waiter is in descending priority order.
1068 (For information on priority ordering, see the discussion of the
1069 .BR SCHED_DEADLINE ,
1070 .BR SCHED_FIFO ,
1072 .BR SCHED_RR
1073 scheduling policies in
1074 .BR sched (7).)
1075 The owner inherits either the waiter's CPU bandwidth
1076 (if the waiter is scheduled under the
1077 .BR SCHED_DEADLINE
1078 policy) or the waiter's priority (if the waiter is scheduled under the
1079 .BR SCHED_RR
1081 .BR SCHED_FIFO
1082 policy).
1083 .\" August 2015:
1084 .\"     mtk: If the realm is restricted purely to SCHED_OTHER (SCHED_NORMAL)
1085 .\"          processes, does the nice value come into play also?
1087 .\"     tglx: No. SCHED_OTHER/NORMAL tasks are handled in FIFO order
1088 This inheritance follows the lock chain in the case of nested locking
1089 .\" (i.e., task 1 blocks on lock A, held by task 2,
1090 .\" while task 2 blocks on lock B, held by task 3)
1091 and performs deadlock detection.
1094 .I timeout
1095 argument provides a timeout for the lock attempt.
1097 .I timeout
1098 is not NULL, the structure it points to specifies
1099 an absolute timeout, measured against the
1100 .BR CLOCK_REALTIME
1101 clock.
1102 .\" 2016-07-07 response from Thomas Gleixner on LKML:
1103 .\" From: Thomas Gleixner <tglx@linutronix.de>
1104 .\" Date: 6 July 2016 at 20:57
1105 .\" Subject: Re: futex: Allow FUTEX_CLOCK_REALTIME with FUTEX_WAIT op
1107 .\" On Thu, 23 Jun 2016, Michael Kerrisk (man-pages) wrote:
1108 .\" > On 06/23/2016 08:28 PM, Darren Hart wrote:
1109 .\" > > And as a follow-on, what is the reason for FUTEX_LOCK_PI only using
1110 .\" > > CLOCK_REALTIME? It seems reasonable to me that a user may want to wait a
1111 .\" > > specific amount of time, regardless of wall time.
1112 .\" >
1113 .\" > Yes, that's another weird inconsistency.
1115 .\" The reason is that phtread_mutex_timedlock() uses absolute timeouts based on
1116 .\" CLOCK_REALTIME. glibc folks asked to make that the default behaviour back
1117 .\" then when we added LOCK_PI.
1119 .I timeout
1120 is NULL, the operation will block indefinitely.
1123 .IR uaddr2 ,
1124 .IR val ,
1126 .IR val3
1127 arguments are ignored.
1129 .\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1132 .BR FUTEX_LOCK_PI2 " (since Linux 5.14.0)"
1133 .\" commit bf22a6976897977b0a3f1aeba6823c959fc4fdae
1134 This operation works similar like
1135 .BR FUTEX_LOCK_PI .
1136 The only difference is the
1137 timeout argument.
1138 .BR FUTEX_LOCK_PI2
1139 has support for selectable clocks.
1142 .I timeout
1143 is not NULL, the structure it points to specifies
1144 an absolute timeout.
1146 .I timeout
1147 is NULL, the operation can block indefinitely.
1150 .\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1153 .BR FUTEX_TRYLOCK_PI " (since Linux 2.6.18)"
1154 .\" commit c87e2837be82df479a6bae9f155c43516d2feebc
1155 This operation tries to acquire the lock at
1156 .IR uaddr .
1157 It is invoked when a user-space atomic acquire did not
1158 succeed because the futex word was not 0.
1160 Because the kernel has access to more state information than user space,
1161 acquisition of the lock might succeed if performed by the
1162 kernel in cases where the futex word
1163 (i.e., the state information accessible to use-space) contains stale state
1164 .RB ( FUTEX_WAITERS
1165 and/or
1166 .BR FUTEX_OWNER_DIED ).
1167 This can happen when the owner of the futex died.
1168 User space cannot handle this condition in a race-free manner,
1169 but the kernel can fix this up and acquire the futex.
1170 .\" Paraphrasing a f2f conversation with Thomas Gleixner about the
1171 .\" above point (Aug 2015): ###
1172 .\"     There is a rare possibility of a race condition involving an
1173 .\"     uncontended futex with no owner, but with waiters.  The
1174 .\"     kernel-user-space contract is that if a futex is nonzero, you must
1175 .\"     go into kernel.  The futex was owned by a task, and that task dies
1176 .\"     but there are no waiters, so the futex value is non zero.
1177 .\"     Therefore, the next locker has to go into the kernel,
1178 .\"     so that the kernel has a chance to clean up. (CMXCH on zero
1179 .\"     in user space would fail, so kernel has to clean up.)
1180 .\" Darren Hart (Oct 2015):
1181 .\"     The trylock in the kernel has more state, so it can independently
1182 .\"     verify the flags that user space must trust implicitly.
1185 .IR uaddr2 ,
1186 .IR val ,
1187 .IR timeout ,
1189 .IR val3
1190 arguments are ignored.
1192 .\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1195 .BR FUTEX_UNLOCK_PI " (since Linux 2.6.18)"
1196 .\" commit c87e2837be82df479a6bae9f155c43516d2feebc
1197 This operation wakes the top priority waiter that is waiting in
1198 .B FUTEX_LOCK_PI
1200 .B FUTEX_LOCK_PI2
1201 on the futex address provided by the
1202 .I uaddr
1203 argument.
1205 This is called when the user-space value at
1206 .I uaddr
1207 cannot be changed atomically from a TID (of the owner) to 0.
1210 .IR uaddr2 ,
1211 .IR val ,
1212 .IR timeout ,
1214 .IR val3
1215 arguments are ignored.
1217 .\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1220 .BR FUTEX_CMP_REQUEUE_PI " (since Linux 2.6.31)"
1221 .\" commit 52400ba946759af28442dee6265c5c0180ac7122
1222 This operation is a PI-aware variant of
1223 .BR FUTEX_CMP_REQUEUE .
1224 It requeues waiters that are blocked via
1225 .B FUTEX_WAIT_REQUEUE_PI
1227 .I uaddr
1228 from a non-PI source futex
1229 .RI ( uaddr )
1230 to a PI target futex
1231 .RI ( uaddr2 ).
1233 As with
1234 .BR FUTEX_CMP_REQUEUE ,
1235 this operation wakes up a maximum of
1236 .I val
1237 waiters that are waiting on the futex at
1238 .IR uaddr .
1239 However, for
1240 .BR FUTEX_CMP_REQUEUE_PI ,
1241 .I val
1242 is required to be 1
1243 (since the main point is to avoid a thundering herd).
1244 The remaining waiters are removed from the wait queue of the source futex at
1245 .I uaddr
1246 and added to the wait queue of the target futex at
1247 .IR uaddr2 .
1250 .I val2
1251 .\" val2 is the cap on the number of requeued waiters.
1252 .\" In the glibc pthread_cond_broadcast() implementation, this argument
1253 .\" is specified as INT_MAX, and for pthread_cond_signal() it is 0.
1255 .I val3
1256 arguments serve the same purposes as for
1257 .BR FUTEX_CMP_REQUEUE .
1259 .\"       The page at http://locklessinc.com/articles/futex_cheat_sheet/
1260 .\"       notes that "priority-inheritance Futex to priority-inheritance
1261 .\"       Futex requeues are currently unsupported". However, probably
1262 .\"       the page does not need to say nothing about this, since
1263 .\"       Thomas Gleixner commented (July 2015): "they never will be
1264 .\"       supported because they make no sense at all"
1266 .\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1269 .BR FUTEX_WAIT_REQUEUE_PI " (since Linux 2.6.31)"
1270 .\" commit 52400ba946759af28442dee6265c5c0180ac7122
1272 Wait on a non-PI futex at
1273 .I uaddr
1274 and potentially be requeued (via a
1275 .BR FUTEX_CMP_REQUEUE_PI
1276 operation in another task) onto a PI futex at
1277 .IR uaddr2 .
1278 The wait operation on
1279 .I uaddr
1280 is the same as for
1281 .BR FUTEX_WAIT .
1283 The waiter can be removed from the wait on
1284 .I uaddr
1285 without requeueing on
1286 .IR uaddr2
1287 via a
1288 .BR FUTEX_WAKE
1289 operation in another task.
1290 In this case, the
1291 .BR FUTEX_WAIT_REQUEUE_PI
1292 operation fails with the error
1293 .BR EAGAIN .
1296 .I timeout
1297 is not NULL, the structure it points to specifies
1298 an absolute timeout for the wait operation.
1300 .I timeout
1301 is NULL, the operation can block indefinitely.
1304 .I val3
1305 argument is ignored.
1308 .BR FUTEX_WAIT_REQUEUE_PI
1310 .BR FUTEX_CMP_REQUEUE_PI
1311 were added to support a fairly specific use case:
1312 support for priority-inheritance-aware POSIX threads condition variables.
1313 The idea is that these operations should always be paired,
1314 in order to ensure that user space and the kernel remain in sync.
1315 Thus, in the
1316 .BR FUTEX_WAIT_REQUEUE_PI
1317 operation, the user-space application pre-specifies the target
1318 of the requeue that takes place in the
1319 .BR FUTEX_CMP_REQUEUE_PI
1320 operation.
1322 .\" Darren Hart notes that a patch to allow glibc to fully support
1323 .\" PI-aware pthreads condition variables has not yet been accepted into
1324 .\" glibc. The story is complex, and can be found at
1325 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=11588
1326 .\" Darren notes that in the meantime, the patch is shipped with various
1327 .\" PREEMPT_RT-enabled Linux systems.
1329 .\" Related to the preceding, Darren proposed that somewhere, man-pages
1330 .\" should document the following point:
1332 .\"     While the Linux kernel, since 2.6.31, supports requeueing of
1333 .\"     priority-inheritance (PI) aware mutexes via the
1334 .\"     FUTEX_WAIT_REQUEUE_PI and FUTEX_CMP_REQUEUE_PI futex operations,
1335 .\"     the glibc implementation does not yet take full advantage of this.
1336 .\"     Specifically, the condvar internal data lock remains a non-PI aware
1337 .\"     mutex, regardless of the type of the pthread_mutex associated with
1338 .\"     the condvar. This can lead to an unbounded priority inversion on
1339 .\"     the internal data lock even when associating a PI aware
1340 .\"     pthread_mutex with a condvar during a pthread_cond*_wait
1341 .\"     operation. For this reason, it is not recommended to rely on
1342 .\"     priority inheritance when using pthread condition variables.
1344 .\" The problem is that the obvious location for this text is
1345 .\" the pthread_cond*wait(3) man page. However, such a man page
1346 .\" does not currently exist.
1348 .\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1350 .SH RETURN VALUE
1351 In the event of an error (and assuming that
1352 .BR futex ()
1353 was invoked via
1354 .BR syscall (2)),
1355 all operations return \-1 and set
1356 .I errno
1357 to indicate the error.
1359 The return value on success depends on the operation,
1360 as described in the following list:
1362 .B FUTEX_WAIT
1363 Returns 0 if the caller was woken up.
1364 Note that a wake-up can also be caused by common futex usage patterns
1365 in unrelated code that happened to have previously used the futex word's
1366 memory location (e.g., typical futex-based implementations of
1367 Pthreads mutexes can cause this under some conditions).
1368 Therefore, callers should always conservatively assume that a return
1369 value of 0 can mean a spurious wake-up, and use the futex word's value
1370 (i.e., the user-space synchronization scheme)
1371 to decide whether to continue to block or not.
1373 .B FUTEX_WAKE
1374 Returns the number of waiters that were woken up.
1376 .B FUTEX_FD
1377 Returns the new file descriptor associated with the futex.
1379 .B FUTEX_REQUEUE
1380 Returns the number of waiters that were woken up.
1382 .B FUTEX_CMP_REQUEUE
1383 Returns the total number of waiters that were woken up or
1384 requeued to the futex for the futex word at
1385 .IR uaddr2 .
1386 If this value is greater than
1387 .IR val ,
1388 then the difference is the number of waiters requeued to the futex for the
1389 futex word at
1390 .IR uaddr2 .
1392 .B FUTEX_WAKE_OP
1393 Returns the total number of waiters that were woken up.
1394 This is the sum of the woken waiters on the two futexes for
1395 the futex words at
1396 .I uaddr
1398 .IR uaddr2 .
1400 .B FUTEX_WAIT_BITSET
1401 Returns 0 if the caller was woken up.
1403 .B FUTEX_WAIT
1404 for how to interpret this correctly in practice.
1406 .B FUTEX_WAKE_BITSET
1407 Returns the number of waiters that were woken up.
1409 .B FUTEX_LOCK_PI
1410 Returns 0 if the futex was successfully locked.
1412 .B FUTEX_LOCK_PI2
1413 Returns 0 if the futex was successfully locked.
1415 .B FUTEX_TRYLOCK_PI
1416 Returns 0 if the futex was successfully locked.
1418 .B FUTEX_UNLOCK_PI
1419 Returns 0 if the futex was successfully unlocked.
1421 .B FUTEX_CMP_REQUEUE_PI
1422 Returns the total number of waiters that were woken up or
1423 requeued to the futex for the futex word at
1424 .IR uaddr2 .
1425 If this value is greater than
1426 .IR val ,
1427 then difference is the number of waiters requeued to the futex for
1428 the futex word at
1429 .IR uaddr2 .
1431 .B FUTEX_WAIT_REQUEUE_PI
1432 Returns 0 if the caller was successfully requeued to the futex for
1433 the futex word at
1434 .IR uaddr2 .
1436 .\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1438 .SH ERRORS
1440 .B EACCES
1441 No read access to the memory of a futex word.
1443 .B EAGAIN
1444 .RB ( FUTEX_WAIT ,
1445 .BR FUTEX_WAIT_BITSET ,
1446 .BR FUTEX_WAIT_REQUEUE_PI )
1447 The value pointed to by
1448 .I uaddr
1449 was not equal to the expected value
1450 .I val
1451 at the time of the call.
1453 .BR Note :
1454 on Linux, the symbolic names
1455 .B EAGAIN
1457 .B EWOULDBLOCK
1458 (both of which appear in different parts of the kernel futex code)
1459 have the same value.
1461 .B EAGAIN
1462 .RB ( FUTEX_CMP_REQUEUE ,
1463 .BR FUTEX_CMP_REQUEUE_PI )
1464 The value pointed to by
1465 .I uaddr
1466 is not equal to the expected value
1467 .IR val3 .
1469 .BR EAGAIN
1470 .RB ( FUTEX_LOCK_PI ,
1471 .BR FUTEX_LOCK_PI2 ,
1472 .BR FUTEX_TRYLOCK_PI ,
1473 .BR FUTEX_CMP_REQUEUE_PI )
1474 The futex owner thread ID of
1475 .I uaddr
1476 (for
1477 .BR FUTEX_CMP_REQUEUE_PI :
1478 .IR uaddr2 )
1479 is about to exit,
1480 but has not yet handled the internal state cleanup.
1481 Try again.
1483 .BR EDEADLK
1484 .RB ( FUTEX_LOCK_PI ,
1485 .BR FUTEX_LOCK_PI2 ,
1486 .BR FUTEX_TRYLOCK_PI ,
1487 .BR FUTEX_CMP_REQUEUE_PI )
1488 The futex word at
1489 .I uaddr
1490 is already locked by the caller.
1492 .BR EDEADLK
1493 .\" FIXME . I see that kernel/locking/rtmutex.c uses EDEADLK in some
1494 .\"       places, and EDEADLOCK in others. On almost all architectures
1495 .\"       these constants are synonymous. Is there a reason that both
1496 .\"       names are used?
1498 .\"       tglx (July 2015): "No. We should probably fix that."
1500 .RB ( FUTEX_CMP_REQUEUE_PI )
1501 While requeueing a waiter to the PI futex for the futex word at
1502 .IR uaddr2 ,
1503 the kernel detected a deadlock.
1505 .B EFAULT
1506 A required pointer argument (i.e.,
1507 .IR uaddr ,
1508 .IR uaddr2 ,
1510 .IR timeout )
1511 did not point to a valid user-space address.
1513 .B EINTR
1515 .B FUTEX_WAIT
1517 .B FUTEX_WAIT_BITSET
1518 operation was interrupted by a signal (see
1519 .BR signal (7)).
1520 In kernels before Linux 2.6.22, this error could also be returned for
1521 a spurious wakeup; since Linux 2.6.22, this no longer happens.
1523 .B EINVAL
1524 The operation in
1525 .IR futex_op
1526 is one of those that employs a timeout, but the supplied
1527 .I timeout
1528 argument was invalid
1529 .RI ( tv_sec
1530 was less than zero, or
1531 .IR tv_nsec
1532 was not less than 1,000,000,000).
1534 .B EINVAL
1535 The operation specified in
1536 .IR futex_op
1537 employs one or both of the pointers
1538 .I uaddr
1540 .IR uaddr2 ,
1541 but one of these does not point to a valid object\(emthat is,
1542 the address is not four-byte-aligned.
1544 .B EINVAL
1545 .RB ( FUTEX_WAIT_BITSET ,
1546 .BR FUTEX_WAKE_BITSET )
1547 The bit mask supplied in
1548 .IR val3
1549 is zero.
1551 .B EINVAL
1552 .RB ( FUTEX_CMP_REQUEUE_PI )
1553 .I uaddr
1554 equals
1555 .IR uaddr2
1556 (i.e., an attempt was made to requeue to the same futex).
1558 .BR EINVAL
1559 .RB ( FUTEX_FD )
1560 The signal number supplied in
1561 .I val
1562 is invalid.
1564 .B EINVAL
1565 .RB ( FUTEX_WAKE ,
1566 .BR FUTEX_WAKE_OP ,
1567 .BR FUTEX_WAKE_BITSET ,
1568 .BR FUTEX_REQUEUE ,
1569 .BR FUTEX_CMP_REQUEUE )
1570 The kernel detected an inconsistency between the user-space state at
1571 .I uaddr
1572 and the kernel state\(emthat is, it detected a waiter which waits in
1573 .BR FUTEX_LOCK_PI
1575 .BR FUTEX_LOCK_PI2
1577 .IR uaddr .
1579 .B EINVAL
1580 .RB ( FUTEX_LOCK_PI ,
1581 .BR FUTEX_LOCK_PI2 ,
1582 .BR FUTEX_TRYLOCK_PI ,
1583 .BR FUTEX_UNLOCK_PI )
1584 The kernel detected an inconsistency between the user-space state at
1585 .I uaddr
1586 and the kernel state.
1587 This indicates either state corruption
1588 or that the kernel found a waiter on
1589 .I uaddr
1590 which is waiting via
1591 .BR FUTEX_WAIT
1593 .BR FUTEX_WAIT_BITSET .
1595 .B EINVAL
1596 .RB ( FUTEX_CMP_REQUEUE_PI )
1597 The kernel detected an inconsistency between the user-space state at
1598 .I uaddr2
1599 and the kernel state;
1600 .\" From a conversation with Thomas Gleixner (Aug 2015): ###
1601 .\"     The kernel sees: I have non PI state for a futex you tried to
1602 .\"     tell me was PI
1603 that is, the kernel detected a waiter which waits via
1604 .BR FUTEX_WAIT
1606 .BR FUTEX_WAIT_BITSET
1608 .IR uaddr2 .
1610 .B EINVAL
1611 .RB ( FUTEX_CMP_REQUEUE_PI )
1612 The kernel detected an inconsistency between the user-space state at
1613 .I uaddr
1614 and the kernel state;
1615 that is, the kernel detected a waiter which waits via
1616 .BR FUTEX_WAIT
1618 .BR FUTEX_WAIT_BITSET
1620 .IR uaddr .
1622 .B EINVAL
1623 .RB ( FUTEX_CMP_REQUEUE_PI )
1624 The kernel detected an inconsistency between the user-space state at
1625 .I uaddr
1626 and the kernel state;
1627 that is, the kernel detected a waiter which waits on
1628 .I uaddr
1630 .BR FUTEX_LOCK_PI
1632 .BR FUTEX_LOCK_PI2
1633 (instead of
1634 .BR FUTEX_WAIT_REQUEUE_PI ).
1636 .B EINVAL
1637 .RB ( FUTEX_CMP_REQUEUE_PI )
1638 .\" This deals with the case:
1639 .\"     wait_requeue_pi(A, B);
1640 .\"     requeue_pi(A, C);
1641 An attempt was made to requeue a waiter to a futex other than that
1642 specified by the matching
1643 .B FUTEX_WAIT_REQUEUE_PI
1644 call for that waiter.
1646 .B EINVAL
1647 .RB ( FUTEX_CMP_REQUEUE_PI )
1649 .I val
1650 argument is not 1.
1652 .B EINVAL
1653 Invalid argument.
1655 .B ENFILE
1656 .RB ( FUTEX_FD )
1657 The system-wide limit on the total number of open files has been reached.
1659 .BR ENOMEM
1660 .RB ( FUTEX_LOCK_PI ,
1661 .BR FUTEX_LOCK_PI2 ,
1662 .BR FUTEX_TRYLOCK_PI ,
1663 .BR FUTEX_CMP_REQUEUE_PI )
1664 The kernel could not allocate memory to hold state information.
1666 .B ENOSYS
1667 Invalid operation specified in
1668 .IR futex_op .
1670 .B ENOSYS
1672 .BR FUTEX_CLOCK_REALTIME
1673 option was specified in
1674 .IR futex_op ,
1675 but the accompanying operation was neither
1676 .BR FUTEX_WAIT ,
1677 .BR FUTEX_WAIT_BITSET ,
1678 .BR FUTEX_WAIT_REQUEUE_PI ,
1680 .BR FUTEX_LOCK_PI2 .
1682 .BR ENOSYS
1683 .RB ( FUTEX_LOCK_PI ,
1684 .BR FUTEX_LOCK_PI2 ,
1685 .BR FUTEX_TRYLOCK_PI ,
1686 .BR FUTEX_UNLOCK_PI ,
1687 .BR FUTEX_CMP_REQUEUE_PI ,
1688 .BR FUTEX_WAIT_REQUEUE_PI )
1689 A run-time check determined that the operation is not available.
1690 The PI-futex operations are not implemented on all architectures and
1691 are not supported on some CPU variants.
1693 .BR EPERM
1694 .RB ( FUTEX_LOCK_PI ,
1695 .BR FUTEX_LOCK_PI2 ,
1696 .BR FUTEX_TRYLOCK_PI ,
1697 .BR FUTEX_CMP_REQUEUE_PI )
1698 The caller is not allowed to attach itself to the futex at
1699 .I uaddr
1700 (for
1701 .BR FUTEX_CMP_REQUEUE_PI :
1702 the futex at
1703 .IR uaddr2 ).
1704 (This may be caused by a state corruption in user space.)
1706 .BR EPERM
1707 .RB ( FUTEX_UNLOCK_PI )
1708 The caller does not own the lock represented by the futex word.
1710 .BR ESRCH
1711 .RB ( FUTEX_LOCK_PI ,
1712 .BR FUTEX_LOCK_PI2 ,
1713 .BR FUTEX_TRYLOCK_PI ,
1714 .BR FUTEX_CMP_REQUEUE_PI )
1715 The thread ID in the futex word at
1716 .I uaddr
1717 does not exist.
1719 .BR ESRCH
1720 .RB ( FUTEX_CMP_REQUEUE_PI )
1721 The thread ID in the futex word at
1722 .I uaddr2
1723 does not exist.
1725 .B ETIMEDOUT
1726 The operation in
1727 .IR futex_op
1728 employed the timeout specified in
1729 .IR timeout ,
1730 and the timeout expired before the operation completed.
1732 .\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1734 .SH VERSIONS
1735 Futexes were first made available in a stable kernel release
1736 with Linux 2.6.0.
1738 Initial futex support was merged in Linux 2.5.7 but with different
1739 semantics from what was described above.
1740 A four-argument system call with the semantics
1741 described in this page was introduced in Linux 2.5.40.
1742 A fifth argument was added in Linux 2.5.70,
1743 and a sixth argument was added in Linux 2.6.7.
1744 .SH CONFORMING TO
1745 This system call is Linux-specific.
1746 .SH NOTES
1747 Several higher-level programming abstractions are implemented via futexes,
1748 including POSIX semaphores and
1749 various POSIX threads synchronization mechanisms
1750 (mutexes, condition variables, read-write locks, and barriers).
1751 .\" TODO FIXME(Torvald) Above, we cite this section and claim it contains
1752 .\" details on the synchronization semantics; add the C11 equivalents
1753 .\" here (or whatever we find consensus for).
1755 .\""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1757 .SH EXAMPLES
1758 The program below demonstrates use of futexes in a program where a parent
1759 process and a child process use a pair of futexes located inside a
1760 shared anonymous mapping to synchronize access to a shared resource:
1761 the terminal.
1762 The two processes each write
1763 .IR nloops
1764 (a command-line argument that defaults to 5 if omitted)
1765 messages to the terminal and employ a synchronization protocol
1766 that ensures that they alternate in writing messages.
1767 Upon running this program we see output such as the following:
1769 .in +4n
1771 $ \fB./futex_demo\fP
1772 Parent (18534) 0
1773 Child  (18535) 0
1774 Parent (18534) 1
1775 Child  (18535) 1
1776 Parent (18534) 2
1777 Child  (18535) 2
1778 Parent (18534) 3
1779 Child  (18535) 3
1780 Parent (18534) 4
1781 Child  (18535) 4
1784 .SS Program source
1787 /* futex_demo.c
1789    Usage: futex_demo [nloops]
1790                     (Default: 5)
1792    Demonstrate the use of futexes in a program where parent and child
1793    use a pair of futexes located inside a shared anonymous mapping to
1794    synchronize access to a shared resource: the terminal. The two
1795    processes each write \(aqnum\-loops\(aq messages to the terminal and employ
1796    a synchronization protocol that ensures that they alternate in
1797    writing messages.
1799 #define _GNU_SOURCE
1800 #include <stdio.h>
1801 #include <errno.h>
1802 #include <stdatomic.h>
1803 #include <stdint.h>
1804 #include <stdlib.h>
1805 #include <unistd.h>
1806 #include <sys/wait.h>
1807 #include <sys/mman.h>
1808 #include <sys/syscall.h>
1809 #include <linux/futex.h>
1810 #include <sys/time.h>
1812 #define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \e
1813                         } while (0)
1815 static uint32_t *futex1, *futex2, *iaddr;
1817 static int
1818 futex(uint32_t *uaddr, int futex_op, uint32_t val,
1819       const struct timespec *timeout, uint32_t *uaddr2, uint32_t val3)
1821     return syscall(SYS_futex, uaddr, futex_op, val,
1822                    timeout, uaddr2, val3);
1825 /* Acquire the futex pointed to by \(aqfutexp\(aq: wait for its value to
1826    become 1, and then set the value to 0. */
1828 static void
1829 fwait(uint32_t *futexp)
1831     long s;
1833     /* atomic_compare_exchange_strong(ptr, oldval, newval)
1834        atomically performs the equivalent of:
1836            if (*ptr == *oldval)
1837                *ptr = newval;
1839        It returns true if the test yielded true and *ptr was updated. */
1841     while (1) {
1843         /* Is the futex available? */
1844         const uint32_t one = 1;
1845         if (atomic_compare_exchange_strong(futexp, &one, 0))
1846             break;      /* Yes */
1848         /* Futex is not available; wait. */
1850         s = futex(futexp, FUTEX_WAIT, 0, NULL, NULL, 0);
1851         if (s == \-1 && errno != EAGAIN)
1852             errExit("futex\-FUTEX_WAIT");
1853     }
1856 /* Release the futex pointed to by \(aqfutexp\(aq: if the futex currently
1857    has the value 0, set its value to 1 and the wake any futex waiters,
1858    so that if the peer is blocked in fwait(), it can proceed. */
1860 static void
1861 fpost(uint32_t *futexp)
1863     long s;
1865     /* atomic_compare_exchange_strong() was described
1866        in comments above. */
1868     const uint32_t zero = 0;
1869     if (atomic_compare_exchange_strong(futexp, &zero, 1)) {
1870         s = futex(futexp, FUTEX_WAKE, 1, NULL, NULL, 0);
1871         if (s  == \-1)
1872             errExit("futex\-FUTEX_WAKE");
1873     }
1877 main(int argc, char *argv[])
1879     pid_t childPid;
1880     int nloops;
1882     setbuf(stdout, NULL);
1884     nloops = (argc > 1) ? atoi(argv[1]) : 5;
1886     /* Create a shared anonymous mapping that will hold the futexes.
1887        Since the futexes are being shared between processes, we
1888        subsequently use the "shared" futex operations (i.e., not the
1889        ones suffixed "_PRIVATE"). */
1891     iaddr = mmap(NULL, sizeof(*iaddr) * 2, PROT_READ | PROT_WRITE,
1892                 MAP_ANONYMOUS | MAP_SHARED, \-1, 0);
1893     if (iaddr == MAP_FAILED)
1894         errExit("mmap");
1896     futex1 = &iaddr[0];
1897     futex2 = &iaddr[1];
1899     *futex1 = 0;        /* State: unavailable */
1900     *futex2 = 1;        /* State: available */
1902     /* Create a child process that inherits the shared anonymous
1903        mapping. */
1905     childPid = fork();
1906     if (childPid == \-1)
1907         errExit("fork");
1909     if (childPid == 0) {        /* Child */
1910         for (int j = 0; j < nloops; j++) {
1911             fwait(futex1);
1912             printf("Child  (%jd) %d\en", (intmax_t) getpid(), j);
1913             fpost(futex2);
1914         }
1916         exit(EXIT_SUCCESS);
1917     }
1919     /* Parent falls through to here. */
1921     for (int j = 0; j < nloops; j++) {
1922         fwait(futex2);
1923         printf("Parent (%jd) %d\en", (intmax_t) getpid(), j);
1924         fpost(futex1);
1925     }
1927     wait(NULL);
1929     exit(EXIT_SUCCESS);
1932 .SH SEE ALSO
1933 .ad l
1934 .BR get_robust_list (2),
1935 .BR restart_syscall (2),
1936 .BR pthread_mutexattr_getprotocol (3),
1937 .BR futex (7),
1938 .BR sched (7)
1940 The following kernel source files:
1941 .IP * 2
1942 .I Documentation/pi\-futex.txt
1943 .IP *
1944 .I Documentation/futex\-requeue\-pi.txt
1945 .IP *
1946 .I Documentation/locking/rt\-mutex.txt
1947 .IP *
1948 .I Documentation/locking/rt\-mutex\-design.txt
1949 .IP *
1950 .I Documentation/robust\-futex\-ABI.txt
1952 Franke, H., Russell, R., and Kirwood, M., 2002.
1953 \fIFuss, Futexes and Furwocks: Fast Userlevel Locking in Linux\fP
1954 (from proceedings of the Ottawa Linux Symposium 2002),
1956 .UR http://kernel.org\:/doc\:/ols\:/2002\:/ols2002\-pages\-479\-495.pdf
1959 Hart, D., 2009. \fIA futex overview and update\fP,
1960 .UR http://lwn.net/Articles/360699/
1963 Hart, D.\& and Guniguntala, D., 2009.
1964 \fIRequeue-PI: Making Glibc Condvars PI-Aware\fP
1965 (from proceedings of the 2009 Real-Time Linux Workshop),
1966 .UR http://lwn.net/images/conf/rtlws11/papers/proc/p10.pdf
1969 Drepper, U., 2011. \fIFutexes Are Tricky\fP,
1970 .UR http://www.akkadia.org/drepper/futex.pdf
1973 Futex example library, futex-*.tar.bz2 at
1975 .UR ftp://ftp.kernel.org\:/pub\:/linux\:/kernel\:/people\:/rusty/
1978 .\" FIXME(Torvald) We should probably refer to the glibc code here, in
1979 .\" particular the glibc-internal futex wrapper functions that are
1980 .\" WIP, and the generic pthread_mutex_t and perhaps condvar
1981 .\" implementations.