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