]>
Commit | Line | Data |
---|---|---|
8f0aff2a | 1 | .\" Page by b.hubert |
2297bf0e | 2 | .\" |
2e46a6e7 | 3 | .\" %%%LICENSE_START(FREELY_REDISTRIBUTABLE) |
8f0aff2a | 4 | .\" may be freely modified and distributed |
8ff7380d | 5 | .\" %%%LICENSE_END |
fea681da MK |
6 | .\" |
7 | .\" Niki A. Rahimi (LTC Security Development, narahimi@us.ibm.com) | |
8 | .\" added ERRORS section. | |
9 | .\" | |
10 | .\" Modified 2004-06-17 mtk | |
11 | .\" Modified 2004-10-07 aeb, added FUTEX_REQUEUE, FUTEX_CMP_REQUEUE | |
12 | .\" | |
bea08fec | 13 | .\" FIXME . |
4f58b197 | 14 | .\" See also https://bugzilla.kernel.org/show_bug.cgi?id=14303 |
40d5cf23 | 15 | .\" 2.6.14 adds FUTEX_WAKE_OP |
4f58b197 MK |
16 | .\" commit 4732efbeb997189d9f9b04708dc26bf8613ed721 |
17 | .\" Author: Jakub Jelinek <jakub@redhat.com> | |
18 | .\" Date: Tue Sep 6 15:16:25 2005 -0700 | |
19 | .\" | |
bea08fec | 20 | .\" FIXME . |
c13182ef MK |
21 | .\" 2.6.18 adds (Ingo Molnar) priority inheritance support: |
22 | .\" FUTEX_LOCK_PI, FUTEX_UNLOCK_PI, and FUTEX_TRYLOCK_PI. These need | |
34f7665a MK |
23 | .\" to be documented in the manual page. Probably there is sufficient |
24 | .\" material in the kernel source file Documentation/pi-futex.txt. | |
4f58b197 MK |
25 | .\" commit c87e2837be82df479a6bae9f155c43516d2feebc |
26 | .\" Author: Ingo Molnar <mingo@elte.hu> | |
27 | .\" Date: Tue Jun 27 02:54:58 2006 -0700 | |
28 | .\" | |
29 | .\" commit e2970f2fb6950183a34e8545faa093eb49d186e1 | |
30 | .\" Author: Ingo Molnar <mingo@elte.hu> | |
31 | .\" Date: Tue Jun 27 02:54:47 2006 -0700 | |
32 | .\" | |
27b38e1c | 33 | .\" See Documentation/pi-futex.txt |
4f58b197 | 34 | .\" |
bea08fec | 35 | .\" FIXME . |
40d5cf23 | 36 | .\" 2.6.25 adds FUTEX_WAKE_BITSET, FUTEX_WAIT_BITSET |
4f58b197 MK |
37 | .\" commit cd689985cf49f6ff5c8eddc48d98b9d581d9475d |
38 | .\" Author: Thomas Gleixner <tglx@linutronix.de> | |
39 | .\" Date: Fri Feb 1 17:45:14 2008 +0100 | |
40 | .\" | |
bea08fec | 41 | .\" FIXME . |
4f58b197 MK |
42 | .\" 2.6.31 adds FUTEX_WAIT_REQUEUE_PI, FUTEX_CMP_REQUEUE_PI |
43 | .\" commit 52400ba946759af28442dee6265c5c0180ac7122 | |
44 | .\" Author: Darren Hart <dvhltc@us.ibm.com> | |
45 | .\" Date: Fri Apr 3 13:40:49 2009 -0700 | |
46 | .\" | |
47 | .\" commit ba9c22f2c01cf5c88beed5a6b9e07d42e10bd358 | |
48 | .\" Author: Darren Hart <dvhltc@us.ibm.com> | |
49 | .\" Date: Mon Apr 20 22:22:22 2009 -0700 | |
50 | .\" | |
51 | .\" See Documentation/futex-requeue-pi.txt | |
34f7665a | 52 | .\" |
3d155313 | 53 | .TH FUTEX 2 2014-05-21 "Linux" "Linux Programmer's Manual" |
fea681da | 54 | .SH NAME |
ce154705 | 55 | futex \- fast user-space locking |
fea681da | 56 | .SH SYNOPSIS |
9d9dc1e8 | 57 | .nf |
fea681da MK |
58 | .sp |
59 | .B "#include <linux/futex.h>" | |
fea681da MK |
60 | .B "#include <sys/time.h>" |
61 | .sp | |
9d9dc1e8 MK |
62 | .BI "int futex(int *" uaddr ", int " op ", int " val \ |
63 | ", const struct timespec *" timeout , | |
64 | .br | |
65 | .BI " int *" uaddr2 ", int " val3 ); | |
fea681da | 66 | .\" int *? void *? u32 *? |
9d9dc1e8 | 67 | .fi |
b939d6e4 MK |
68 | .IR Note : |
69 | There is no glibc wrapper for this system call; see NOTES. | |
47297adb | 70 | .SH DESCRIPTION |
fea681da MK |
71 | .PP |
72 | The | |
e511ffb6 | 73 | .BR futex () |
fea681da MK |
74 | system call provides a method for |
75 | a program to wait for a value at a given address to change, and a | |
76 | method to wake up anyone waiting on a particular address (while the | |
77 | addresses for the same memory in separate processes may not be | |
78 | equal, the kernel maps them internally so the same memory mapped in | |
79 | different locations will correspond for | |
e511ffb6 | 80 | .BR futex () |
c13182ef | 81 | calls). |
fd3fa7ef | 82 | This system call is typically used to |
fea681da MK |
83 | implement the contended case of a lock in shared memory, as |
84 | described in | |
a8bda636 | 85 | .BR futex (7). |
fea681da | 86 | .PP |
c13182ef | 87 | When a |
a8bda636 | 88 | .BR futex (7) |
7fac88a9 | 89 | operation did not finish uncontended in user space, a call needs to be made |
c13182ef MK |
90 | to the kernel to arbitrate. |
91 | Arbitration can either mean putting the calling | |
fea681da MK |
92 | process to sleep or, conversely, waking a waiting process. |
93 | .PP | |
94 | Callers of this function are expected to adhere to the semantics as set out in | |
a8bda636 | 95 | .BR futex (7). |
fea681da | 96 | As these |
d603cc27 | 97 | semantics involve writing nonportable assembly instructions, this in turn |
fea681da MK |
98 | probably means that most users will in fact be library authors and not |
99 | general application developers. | |
100 | .PP | |
101 | The | |
102 | .I uaddr | |
103 | argument needs to point to an aligned integer which stores the counter. | |
104 | The operation to execute is passed via the | |
105 | .I op | |
c4bb193f | 106 | argument, along with a value |
fea681da MK |
107 | .IR val . |
108 | .PP | |
109 | Five operations are currently defined: | |
110 | .TP | |
111 | .B FUTEX_WAIT | |
112 | This operation atomically verifies that the futex address | |
113 | .I uaddr | |
114 | still contains the value | |
115 | .IR val , | |
682edefb MK |
116 | and sleeps awaiting |
117 | .B FUTEX_WAKE | |
118 | on this futex address. | |
c13182ef | 119 | If the |
fea681da | 120 | .I timeout |
82a6092b MK |
121 | argument is non-NULL, its contents specify the duration of the wait. |
122 | (This interval will be rounded up to the system clock granularity, | |
123 | and kernel scheduling delays mean that the | |
124 | blocking interval may overrun by a small amount.) | |
125 | If | |
126 | .I timeout | |
127 | is NULL, the call blocks indefinitely. | |
4798a7f3 | 128 | |
c13182ef | 129 | The arguments |
fea681da MK |
130 | .I uaddr2 |
131 | and | |
132 | .I val3 | |
133 | are ignored. | |
134 | ||
135 | For | |
a8bda636 | 136 | .BR futex (7), |
fea681da MK |
137 | this call is executed if decrementing the count gave a negative value |
138 | (indicating contention), and will sleep until another process releases | |
682edefb MK |
139 | the futex and executes the |
140 | .B FUTEX_WAKE | |
141 | operation. | |
fea681da MK |
142 | .TP |
143 | .B FUTEX_WAKE | |
a8d55537 | 144 | This operation wakes at most \fIval\fP |
b87dcfb9 | 145 | processes waiting on this futex address (i.e., inside |
682edefb | 146 | .BR FUTEX_WAIT ). |
4798a7f3 | 147 | |
fea681da MK |
148 | The arguments |
149 | .IR timeout , | |
150 | .I uaddr2 | |
151 | and | |
152 | .I val3 | |
153 | are ignored. | |
154 | ||
155 | For | |
a8bda636 | 156 | .BR futex (7), |
fea681da MK |
157 | this is executed if incrementing |
158 | the count showed that there were waiters, once the futex value has been set | |
159 | to 1 (indicating that it is available). | |
160 | .TP | |
da36351e | 161 | .BR FUTEX_FD " (present up to and including Linux 2.6.25)" |
fea681da MK |
162 | To support asynchronous wakeups, this operation associates a file descriptor |
163 | with a futex. | |
164 | .\" , suitable for .BR poll (2). | |
682edefb MK |
165 | If another process executes a |
166 | .BR FUTEX_WAKE , | |
167 | the process will receive the signal number that was passed in | |
fea681da MK |
168 | .IR val . |
169 | The calling process must close the returned file descriptor after use. | |
4798a7f3 | 170 | |
fea681da MK |
171 | The arguments |
172 | .IR timeout , | |
173 | .I uaddr2 | |
174 | and | |
175 | .I val3 | |
176 | are ignored. | |
177 | ||
c13182ef | 178 | To prevent race conditions, the caller should test if the futex has |
682edefb MK |
179 | been upped after |
180 | .B FUTEX_FD | |
181 | returns. | |
266a5e91 | 182 | |
da36351e | 183 | Because it was inherently racy, |
682edefb | 184 | .B FUTEX_FD |
5fab2e7c | 185 | has been removed from Linux 2.6.26 onward. |
fea681da MK |
186 | .TP |
187 | .BR FUTEX_REQUEUE " (since Linux 2.5.70)" | |
188 | This operation was introduced in order to avoid a "thundering herd" effect | |
682edefb MK |
189 | when |
190 | .B FUTEX_WAKE | |
191 | is used and all processes woken up need to acquire another futex. | |
2abb73b9 | 192 | The argument |
fea681da | 193 | .I val |
2abb73b9 TG |
194 | contains the number of waiters on |
195 | .I uaddr | |
196 | that are immediately woken up. | |
197 | The | |
fea681da | 198 | .I timeout |
2abb73b9 TG |
199 | argument is (ab)used to specify the number of waiters |
200 | that are requeued to the futex at | |
201 | .IR uaddr2 ; | |
202 | the kernel casts the | |
203 | .I timeout | |
204 | value to | |
205 | .IR u32 . | |
206 | .\" FIXME What are the constraints (if any) on the values of 'val' vs | |
207 | .\" 'timeout' vs [the number of waites on 'uaddr']? | |
208 | ||
209 | The argument | |
fea681da | 210 | .I val3 |
2abb73b9 | 211 | is ignored. |
fea681da MK |
212 | .TP |
213 | .BR FUTEX_CMP_REQUEUE " (since Linux 2.6.7)" | |
682edefb MK |
214 | There was a race in the intended use of |
215 | .BR FUTEX_REQUEUE , | |
216 | so | |
217 | .B FUTEX_CMP_REQUEUE | |
218 | was introduced. | |
219 | This is similar to | |
220 | .BR FUTEX_REQUEUE , | |
fea681da MK |
221 | but first checks whether the location |
222 | .I uaddr | |
223 | still contains the value | |
224 | .IR val3 . | |
e808bba0 MK |
225 | If not, the operation fails with the error |
226 | .BR EAGAIN . | |
4798a7f3 | 227 | |
fea681da MK |
228 | The argument |
229 | .I timeout | |
230 | is ignored. | |
47297adb | 231 | .SH RETURN VALUE |
fea681da | 232 | .PP |
e808bba0 MK |
233 | In the event of an error, all operations return \-1, and set |
234 | .I errno | |
235 | to indicate the error. | |
236 | The return value on success depends on the operation, | |
237 | as described in the following list: | |
fea681da MK |
238 | .TP |
239 | .B FUTEX_WAIT | |
682edefb MK |
240 | Returns 0 if the process was woken by a |
241 | .B FUTEX_WAKE | |
242 | call. | |
e808bba0 | 243 | See ERRORS for the various possible error returns. |
fea681da MK |
244 | .TP |
245 | .B FUTEX_WAKE | |
246 | Returns the number of processes woken up. | |
247 | .TP | |
248 | .B FUTEX_FD | |
249 | Returns the new file descriptor associated with the futex. | |
250 | .TP | |
251 | .B FUTEX_REQUEUE | |
252 | Returns the number of processes woken up. | |
253 | .TP | |
254 | .B FUTEX_CMP_REQUEUE | |
255 | Returns the number of processes woken up. | |
256 | .SH ERRORS | |
257 | .TP | |
258 | .B EACCES | |
259 | No read access to futex memory. | |
260 | .TP | |
261 | .B EAGAIN | |
682edefb | 262 | .B FUTEX_CMP_REQUEUE |
e808bba0 | 263 | detected that the value pointed to by |
9f6c40c0 МК |
264 | .I uaddr |
265 | is not equal to the expected value | |
266 | .IR val3 . | |
fea681da | 267 | (This probably indicates a race; |
682edefb MK |
268 | use the safe |
269 | .B FUTEX_WAKE | |
270 | now.) | |
fea681da MK |
271 | .TP |
272 | .B EFAULT | |
1ea901e8 MK |
273 | A required pointer argument (i.e., |
274 | .IR uaddr , | |
275 | .IR uaddr2 , | |
276 | or | |
277 | .IR timeout ) | |
496df304 | 278 | did not point to a valid user-space address. |
fea681da | 279 | .TP |
9f6c40c0 | 280 | .B EINTR |
e808bba0 | 281 | A |
9f6c40c0 | 282 | .B FUTEX_WAIT |
e808bba0 MK |
283 | operation was interrupted by a signal (see |
284 | .BR signal (7)) | |
285 | or a spurious wakeup. | |
9f6c40c0 | 286 | .TP |
fea681da | 287 | .B EINVAL |
fb2f4c27 MK |
288 | .RB ( FUTEX_WAIT , |
289 | .BR FUTEX_WAIT_REQUEUE_PI ) | |
290 | The supplied | |
291 | .I timeout | |
292 | argument was invalid | |
293 | .RI ( tv_sec | |
294 | was less than zero, or | |
295 | .IR tv_nsec | |
296 | was not less than 1000,000,000). | |
297 | .TP | |
298 | .B EINVAL | |
ea355b7f | 299 | .RB ( FUTEX_WAIT , |
caf1ff25 MK |
300 | .BR FUTEX_WAKE , |
301 | .BR FUTEX_REQUEUE ) | |
51ee94be | 302 | .I uaddr |
caf1ff25 MK |
303 | or (for |
304 | .BR FUTEX_REQUEUE ) | |
305 | .I uaddr2 | |
51ee94be MK |
306 | does not point to a valid object\(emthat is, |
307 | the address is not 4-byte-aligned. | |
308 | .TP | |
309 | .B EINVAL | |
bae14b6c MK |
310 | .RB ( FUTEX_WAKE , |
311 | .BR FUTEX_REQUEUE ) | |
496df304 | 312 | The kernel detected an inconsistency between the user-space state at |
9534086b TG |
313 | .I uaddr |
314 | and the kernel state\(emthat is, it detected a waiter which waits in | |
315 | .BR FUTEX_LOCK_PI . | |
316 | .TP | |
317 | .B EINVAL | |
4832b48a | 318 | Invalid argument. |
fea681da MK |
319 | .TP |
320 | .B ENFILE | |
321 | The system limit on the total number of open files has been reached. | |
4701fc28 MK |
322 | .TP |
323 | .B ENOSYS | |
324 | Invalid operation specified in | |
325 | .IR op . | |
9f6c40c0 МК |
326 | .TP |
327 | .B ETIMEDOUT | |
d1926d78 MK |
328 | .RB ( FUTEX_WAIT ) |
329 | The operation timed out. | |
9f6c40c0 МК |
330 | .TP |
331 | .B EWOULDBLOCK | |
6b5025a6 MK |
332 | .RB ( FUTEX_WAIT ) |
333 | The atomic enqueueing failed. | |
334 | .TP | |
335 | .B EWOULDBLOCK | |
e808bba0 MK |
336 | .I op |
337 | was | |
338 | .BR FUTEX_WAIT | |
339 | and the value pointed to by | |
9f6c40c0 МК |
340 | .I uaddr |
341 | was not equal to the expected value | |
342 | .I val | |
e808bba0 | 343 | at the time of the call. |
47297adb | 344 | .SH VERSIONS |
a1d5f77c MK |
345 | .PP |
346 | Initial futex support was merged in Linux 2.5.7 but with different semantics | |
347 | from what was described above. | |
c4bb193f | 348 | A 4-argument system call with the semantics |
fd3fa7ef | 349 | described in this page was introduced in Linux 2.5.40. |
11b520ed | 350 | In Linux 2.5.70, one argument |
a1d5f77c | 351 | was added. |
11b520ed | 352 | In Linux 2.6.7, a sixth argument was added\(emmessy, especially |
a1d5f77c | 353 | on the s390 architecture. |
47297adb | 354 | .SH CONFORMING TO |
8382f16d | 355 | This system call is Linux-specific. |
47297adb | 356 | .SH NOTES |
fea681da | 357 | .PP |
fcdad7d6 | 358 | To reiterate, bare futexes are not intended as an easy-to-use abstraction |
c13182ef | 359 | for end-users. |
fcdad7d6 | 360 | (There is no wrapper function for this system call in glibc.) |
c13182ef | 361 | Implementors are expected to be assembly literate and to have |
7fac88a9 | 362 | read the sources of the futex user-space library referenced below. |
d282bb24 | 363 | .\" .SH AUTHORS |
fea681da MK |
364 | .\" .PP |
365 | .\" Futexes were designed and worked on by | |
366 | .\" Hubertus Franke (IBM Thomas J. Watson Research Center), | |
367 | .\" Matthew Kirkwood, Ingo Molnar (Red Hat) | |
368 | .\" and Rusty Russell (IBM Linux Technology Center). | |
369 | .\" This page written by bert hubert. | |
47297adb | 370 | .SH SEE ALSO |
d806bc05 | 371 | .BR restart_syscall (2), |
14d8dd3b | 372 | .BR futex (7) |
fea681da | 373 | .PP |
52087dd3 | 374 | \fIFuss, Futexes and Furwocks: Fast Userlevel Locking in Linux\fP |
9b936e9e MK |
375 | (proceedings of the Ottawa Linux Symposium 2002), online at |
376 | .br | |
608bf950 SK |
377 | .UR http://kernel.org\:/doc\:/ols\:/2002\:/ols2002-pages-479-495.pdf |
378 | .UE | |
9b936e9e MK |
379 | .PP |
380 | Futex example library, futex-*.tar.bz2 at | |
381 | .br | |
a605264d | 382 | .UR ftp://ftp.kernel.org\:/pub\:/linux\:/kernel\:/people\:/rusty/ |
608bf950 | 383 | .UE |