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

NAME

6       futex - Fast Userspace Locking
7

SYNOPSIS

9       #include <linux/futex.h>
10

DESCRIPTION

12       The  Linux  kernel  provides  futexes  ('Fast  Userspace muTexes') as a
13       building block for fast userspace locking and semaphores.  Futexes  are
14       very  basic  and lend themselves well for building higher level locking
15       abstractions such as POSIX mutexes.
16
17       This page does not  set  out  to  document  all  design  decisions  but
18       restricts  itself to issues relevant for application and library devel‐
19       opment.  Most programmers will in fact not be  using  futexes  directly
20       but  instead  rely  on system libraries built on them, such as the NPTL
21       pthreads implementation.
22
23       A futex is identified by a piece of memory which can be shared  between
24       different  processes.   In  these different processes, it need not have
25       identical addresses.  In its bare form, a futex  has  semaphore  seman‐
26       tics;  it  is  a counter that can be incremented and decremented atomi‐
27       cally; processes can wait for the value to become positive.
28
29       Futex operation is entirely userspace for the non-contended case.   The
30       kernel  is  only involved to arbitrate the contended case.  As any sane
31       design will strive for non-contention, futexes are also  optimised  for
32       this situation.
33
34       In  its  bare form, a futex is an aligned integer which is only touched
35       by atomic assembler instructions.  Processes  can  share  this  integer
36       using  mmap(),  via shared memory segments or because they share memory
37       space, in which case the application is commonly called multithreaded.
38

SEMANTICS

40       Any futex operation starts in userspace, but it may necessary to commu‐
41       nicate with the kernel using the futex(2) system call.
42
43       To  'up'  a  futex, execute the proper assembler instructions that will
44       cause the host CPU to atomically increment  the  integer.   Afterwards,
45       check  if  it has in fact changed from 0 to 1, in which case there were
46       no waiters and the operation is done.  This is the  non-contended  case
47       which is fast and should be common.
48
49       In the contended case, the atomic increment changed the counter from -1
50       (or some other negative number).  If this is detected, there are  wait‐
51       ers.  Userspace should now set the counter to 1 and instruct the kernel
52       to wake up any waiters using the FUTEX_WAKE operation.
53
54       Waiting on a futex, to 'down' it, is the reverse operation.  Atomically
55       decrement  the  counter and check if it changed to 0, in which case the
56       operation is done and the futex was uncontended.  In all other  circum‐
57       stances,  the process should set the counter to -1 and request that the
58       kernel wait for another process to up the futex.  This  is  done  using
59       the FUTEX_WAIT operation.
60
61       The  futex()  system call can optionally be passed a timeout specifying
62       how long the kernel should wait for the futex to  be  upped.   In  this
63       case,  semantics  are  more  complex  and the programmer is referred to
64       futex(2) for more details. The same holds for asynchronous futex  wait‐
65       ing.
66

NOTES

68       To  reiterate, bare futexes are not intended as an easy to use abstrac‐
69       tion for end-users.  Implementors are expected to be assembly  literate
70       and  to have read the sources of the futex userspace library referenced
71       below.
72
73       This man page illustrates the most common use of  the  futex(2)  primi‐
74       tives: it is by no means the only one.
75

AUTHORS

77       Futexes  were  designed and worked on by Hubertus Franke (IBM Thomas J.
78       Watson Research Center), Matthew Kirkwood, Ingo Molnar  (Red  Hat)  and
79       Rusty Russell (IBM Linux Technology Center).  This page written by bert
80       hubert.
81

VERSIONS

83       Initial futex support was merged in  Linux  2.5.7  but  with  different
84       semantics  from those described above.  Current semantics are available
85       from Linux 2.5.40 onwards.
86

SEE ALSO

88       futex(2), `Fuss, Futexes and Furwocks: Fast Userlevel Locking in Linux'
89       (proceedings  of  the  Ottawa  Linux  Symposium  2002),  futex  example
90       library,   futex-*.tar.bz2    <URL:ftp://ftp.kernel.org:/pub/linux/ker‐
91       nel/people/rusty/>.
92
93
94
95                                  2002-12-31                          FUTEX(7)
Impressum