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