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