1FUTEX(2)                   Linux Programmer's Manual                  FUTEX(2)
2
3
4

NAME

6       futex - Fast Userspace Locking system call
7

SYNOPSIS

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

DESCRIPTION

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).  It is typically used to  implement  the  contended
22       case of a lock in shared memory, as described in futex(7).
23
24       When  a  futex(7)  operation did not finish uncontended in userspace, a
25       call needs to be made to the  kernel  to  arbitrate.   Arbitration  can
26       either mean putting the calling process to sleep or, conversely, waking
27       a waiting process.
28
29       Callers of this function are expected to adhere to the semantics as set
30       out in futex(7).  As these semantics involve writing nonportable assem‐
31       bly instructions, this in turn probably means that most users  will  in
32       fact be library authors and not general application developers.
33
34       The  uaddr  argument  needs to point to an aligned integer which stores
35       the counter.  The operation to execute is passed via the  op  argument,
36       along with a value val.
37
38       Five operations are currently defined:
39
40       FUTEX_WAIT
41              This  operation atomically verifies that the futex address uaddr
42              still contains the value val, and sleeps awaiting FUTEX_WAKE  on
43              this  futex  address.   If the timeout argument is non-NULL, its
44              contents describe the maximum duration of  the  wait,  which  is
45              infinite otherwise.  The arguments uaddr2 and val3 are ignored.
46
47              For  futex(7),  this  call is executed if decrementing the count
48              gave a negative value (indicating contention),  and  will  sleep
49              until  another  process  releases  the  futex  and  executes the
50              FUTEX_WAKE operation.
51
52       FUTEX_WAKE
53              This operation wakes at most val processes waiting on this futex
54              address  (i.e.,  inside  FUTEX_WAIT).   The  arguments  timeout,
55              uaddr2 and val3 are ignored.
56
57              For futex(7), this is executed if incrementing the count  showed
58              that  there were waiters, once the futex value has been set to 1
59              (indicating that it is available).
60
61       FUTEX_FD (present up to and including Linux 2.6.25)
62              To support asynchronous wakeups,  this  operation  associates  a
63              file  descriptor  with  a  futex.  If another process executes a
64              FUTEX_WAKE, the process will receive the signal number that  was
65              passed in val.  The calling process must close the returned file
66              descriptor after use.  The arguments timeout,  uaddr2  and  val3
67              are ignored.
68
69              To  prevent race conditions, the caller should test if the futex
70              has been upped after FUTEX_FD returns.
71
72              Because it was inherently racy, FUTEX_FD has been  removed  from
73              Linux 2.6.26 onwards.
74
75       FUTEX_REQUEUE (since Linux 2.5.70)
76              This  operation  was  introduced in order to avoid a "thundering
77              herd" effect when FUTEX_WAKE is used and all processes woken  up
78              need  to  acquire  another  futex.   This call wakes up val pro‐
79              cesses, and requeues all other waiters on the futex  at  address
80              uaddr2.  The arguments timeout and val3 are ignored.
81
82       FUTEX_CMP_REQUEUE (since Linux 2.6.7)
83              There  was  a  race  in  the  intended  use of FUTEX_REQUEUE, so
84              FUTEX_CMP_REQUEUE  was   introduced.    This   is   similar   to
85              FUTEX_REQUEUE, but first checks whether the location uaddr still
86              contains the value val3.  If not, the operation fails  with  the
87              error EAGAIN.  The argument timeout is ignored.
88

RETURN VALUE

90       Depending  on  which  operation  was executed, the returned value for a
91       successful call can have differing meanings.
92
93       FUTEX_WAIT
94              Returns 0 if the process was woken by  a  FUTEX_WAKE  call.   In
95              case  of  timeout, the operation fails with the error ETIMEDOUT.
96              If the futex was not equal to the expected value, the  operation
97              fails  with  the  error EWOULDBLOCK.  Signals (see signal(7)) or
98              other spurious wakeups cause FUTEX_WAIT to fail with  the  error
99              EINTR.
100
101       FUTEX_WAKE
102              Returns the number of processes woken up.
103
104       FUTEX_FD
105              Returns the new file descriptor associated with the futex.
106
107       FUTEX_REQUEUE
108              Returns the number of processes woken up.
109
110       FUTEX_CMP_REQUEUE
111              Returns the number of processes woken up.
112
113       In  the  event  of an error, all operations return -1, and set errno to
114       indicate the error.
115

ERRORS

117       EACCES No read access to futex memory.
118
119       EAGAIN FUTEX_CMP_REQUEUE found an unexpected futex value.  (This proba‐
120              bly indicates a race; use the safe FUTEX_WAKE now.)
121
122       EFAULT Error in getting timeout information from userspace.
123
124       EINVAL An operation was not defined or error in page alignment.
125
126       ENFILE The  system  limit  on  the  total number of open files has been
127              reached.
128
129       ENOSYS Invalid operation specified in op.
130

VERSIONS

132       Initial futex support was merged in  Linux  2.5.7  but  with  different
133       semantics from what was described above.  A 4-argument system call with
134       the semantics given here was introduced  in  Linux  2.5.40.   In  Linux
135       2.5.70  one  argument  was  added.  In Linux 2.6.7 a sixth argument was
136       added — messy, especially on the s390 architecture.
137

CONFORMING TO

139       This system call is Linux-specific.
140

NOTES

142       To reiterate, bare futexes are not intended as an easy-to-use  abstrac‐
143       tion for end-users.  (There is no wrapper function for this system call
144       in glibc.)  Implementors are expected to be assembly  literate  and  to
145       have read the sources of the futex userspace library referenced below.
146

SEE ALSO

148       futex(7)
149
150       Fuss,  Futexes  and Furwocks: Fast Userlevel Locking in Linux (proceed‐
151       ings of the Ottawa Linux Symposium 2002), online at
152       http://kernel.org/doc/ols/2002/ols2002-pages-479-495.pdf
153
154       Futex example library, futex-*.tar.bz2 at
155       ftp://ftp.nl.kernel.org/pub/linux/kernel/people/rusty/.
156

COLOPHON

158       This page is part of release 3.25 of the Linux  man-pages  project.   A
159       description  of  the project, and information about reporting bugs, can
160       be found at http://www.kernel.org/doc/man-pages/.
161
162
163
164Linux                             2010-05-22                          FUTEX(2)
Impressum