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