1FUTEX(2) Linux Programmer's Manual FUTEX(2)
2
3
4
6 futex - fast user-space locking
7
9 #include <linux/futex.h>
10 #include <sys/time.h>
11
12 int futex(int *uaddr, int op, int val, const struct timespec *timeout,
13 int *uaddr2, int val3);
14
16 The futex() system call provides a method for a program to wait for a
17 value at a given address to change, and a method to wake up anyone
18 waiting on a particular address (while the addresses for the same mem‐
19 ory in separate processes may not be equal, the kernel maps them inter‐
20 nally so the same memory mapped in different locations will correspond
21 for futex() calls). This system call is typically used to implement
22 the contended case of a lock in shared memory, as described in
23 futex(7).
24
25 When a futex(7) operation did not finish uncontended in user space, a
26 call needs to be made to the kernel to arbitrate. Arbitration can
27 either mean putting the calling process to sleep or, conversely, waking
28 a waiting process.
29
30 Callers of this function are expected to adhere to the semantics as set
31 out in futex(7). As these semantics involve writing nonportable assem‐
32 bly instructions, this in turn probably means that most users will in
33 fact be library authors and not general application developers.
34
35 The uaddr argument needs to point to an aligned integer which stores
36 the counter. The operation to execute is passed via the op argument,
37 along with a value val.
38
39 Five operations are currently defined:
40
41 FUTEX_WAIT
42 This operation atomically verifies that the futex address uaddr
43 still contains the value val, and sleeps awaiting FUTEX_WAKE on
44 this futex address. If the timeout argument is non-NULL, its
45 contents describe the minimum duration of the wait, which is
46 infinite otherwise. The arguments uaddr2 and val3 are ignored.
47
48 For futex(7), this call is executed if decrementing the count
49 gave a negative value (indicating contention), and will sleep
50 until another process releases the futex and executes the
51 FUTEX_WAKE operation.
52
53 FUTEX_WAKE
54 This operation wakes at most val processes waiting on this futex
55 address (i.e., inside FUTEX_WAIT). The arguments timeout,
56 uaddr2 and val3 are ignored.
57
58 For futex(7), this is executed if incrementing the count showed
59 that there were waiters, once the futex value has been set to 1
60 (indicating that it is available).
61
62 FUTEX_FD (present up to and including Linux 2.6.25)
63 To support asynchronous wakeups, this operation associates a
64 file descriptor with a futex. If another process executes a
65 FUTEX_WAKE, the process will receive the signal number that was
66 passed in val. The calling process must close the returned file
67 descriptor after use. The arguments timeout, uaddr2 and val3
68 are ignored.
69
70 To prevent race conditions, the caller should test if the futex
71 has been upped after FUTEX_FD returns.
72
73 Because it was inherently racy, FUTEX_FD has been removed from
74 Linux 2.6.26 onward.
75
76 FUTEX_REQUEUE (since Linux 2.5.70)
77 This operation was introduced in order to avoid a "thundering
78 herd" effect when FUTEX_WAKE is used and all processes woken up
79 need to acquire another futex. This call wakes up val pro‐
80 cesses, and requeues all other waiters on the futex at address
81 uaddr2. The arguments timeout and val3 are ignored.
82
83 FUTEX_CMP_REQUEUE (since Linux 2.6.7)
84 There was a race in the intended use of FUTEX_REQUEUE, so
85 FUTEX_CMP_REQUEUE was introduced. This is similar to
86 FUTEX_REQUEUE, but first checks whether the location uaddr still
87 contains the value val3. If not, the operation fails with the
88 error EAGAIN. The argument timeout is ignored.
89
91 In the event of an error, all operations return -1, and set errno to
92 indicate the error. The return value on success depends on the opera‐
93 tion, as described in the following list:
94
95 FUTEX_WAIT
96 Returns 0 if the process was woken by a FUTEX_WAKE call. See
97 ERRORS for the various possible error returns.
98
99 FUTEX_WAKE
100 Returns the number of processes woken up.
101
102 FUTEX_FD
103 Returns the new file descriptor associated with the futex.
104
105 FUTEX_REQUEUE
106 Returns the number of processes woken up.
107
108 FUTEX_CMP_REQUEUE
109 Returns the number of processes woken up.
110
112 EACCES No read access to futex memory.
113
114 EAGAIN FUTEX_CMP_REQUEUE detected that the value pointed to by uaddr is
115 not equal to the expected value val3. (This probably indicates
116 a race; use the safe FUTEX_WAKE now.)
117
118 EFAULT Error retrieving timeout information from user space.
119
120 EINTR A FUTEX_WAIT operation was interrupted by a signal (see sig‐
121 nal(7)) or a spurious wakeup.
122
123 EINVAL Invalid argument.
124
125 ENFILE The system limit on the total number of open files has been
126 reached.
127
128 ENOSYS Invalid operation specified in op.
129
130 ETIMEDOUT
131 Timeout during the FUTEX_WAIT operation.
132
133 EWOULDBLOCK
134 op was FUTEX_WAIT and the value pointed to by uaddr was not
135 equal to the expected value val at the time of the call.
136
138 Initial futex support was merged in Linux 2.5.7 but with different
139 semantics from what was described above. A 4-argument system call with
140 the semantics described in this page was introduced in Linux 2.5.40.
141 In Linux 2.5.70 one argument was added. In Linux 2.6.7 a sixth argu‐
142 ment was added—messy, especially on the s390 architecture.
143
145 This system call is Linux-specific.
146
148 To reiterate, bare futexes are not intended as an easy-to-use abstrac‐
149 tion for end-users. (There is no wrapper function for this system call
150 in glibc.) Implementors are expected to be assembly literate and to
151 have read the sources of the futex user-space library referenced below.
152
154 restart_syscall(2), futex(7)
155
156 Fuss, Futexes and Furwocks: Fast Userlevel Locking in Linux (proceed‐
157 ings of the Ottawa Linux Symposium 2002), online at
158 ⟨http://kernel.org/doc/ols/2002/ols2002-pages-479-495.pdf⟩
159
160 Futex example library, futex-*.tar.bz2 at
161 ⟨ftp://ftp.nl.kernel.org/pub/linux/kernel/people/rusty/⟩
162
164 This page is part of release 3.53 of the Linux man-pages project. A
165 description of the project, and information about reporting bugs, can
166 be found at http://www.kernel.org/doc/man-pages/.
167
168
169
170Linux 2013-07-30 FUTEX(2)