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

NAME

6       futex - fast user-space locking
7

SYNOPSIS

9       #include <linux/futex.h>
10

DESCRIPTION

12       The  Linux  kernel  provides  futexes  ("Fast user-space mutexes") as a
13       building block for fast user-space locking and semaphores.  Futexes are
14       very  basic  and lend themselves well for building higher-level locking
15       abstractions such as mutexes, condition  variables,  read-write  locks,
16       barriers, and semaphores.
17
18       Most  programmers  will  in fact not be using futexes directly but will
19       instead rely on system libraries built on  them,  such  as  the  Native
20       POSIX Thread Library (NPTL) (see pthreads(7)).
21
22       A  futex is identified by a piece of memory which can be shared between
23       processes or threads.  In these different processes, the futex need not
24       have  identical  addresses.   In  its  bare form, a futex has semaphore
25       semantics; it is a counter that  can  be  incremented  and  decremented
26       atomically; processes can wait for the value to become positive.
27
28       Futex  operation  occurs  entirely  in  user space for the noncontended
29       case.  The kernel is involved only to arbitrate the contended case.  As
30       any  sane  design will strive for noncontention, futexes are also opti‐
31       mized for this situation.
32
33       In its bare form, a futex is an aligned integer which is  touched  only
34       by  atomic  assembler instructions.  This integer is four bytes long on
35       all platforms.  Processes can share this  integer  using  mmap(2),  via
36       shared  memory  segments,  or because they share memory space, in which
37       case the application is commonly called multithreaded.
38
39   Semantics
40       Any futex operation starts in user space, but it may  be  necessary  to
41       communicate 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.   Afterward,
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  noncontended  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.   User space should now set the counter to 1 and instruct the ker‐
52       nel 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(2) 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

VERSIONS

68       Initial  futex  support  was  merged  in Linux 2.5.7 but with different
69       semantics from those described above.  Current semantics are  available
70       from Linux 2.5.40 onward.
71

NOTES

73       To  reiterate, bare futexes are not intended as an easy-to-use abstrac‐
74       tion for end users.  Implementors are expected to be assembly  literate
75       and to have read the sources of the futex user-space library referenced
76       below.
77
78       This man page illustrates the most common use of  the  futex(2)  primi‐
79       tives; it is by no means the only one.
80

SEE ALSO

82       clone(2),     futex(2),     get_robust_list(2),     set_robust_list(2),
83       set_tid_address(2), pthreads(7)
84
85       Fuss, Futexes and Furwocks: Fast Userlevel Locking in  Linux  (proceed‐
86       ings  of  the  Ottawa  Linux  Symposium  2002),  futex example library,
87       futex-*.tar.bz2 ⟨ftp://ftp.kernel.org/pub/linux/kernel/people/rusty/⟩.
88

COLOPHON

90       This page is part of release 4.15 of the Linux  man-pages  project.   A
91       description  of  the project, information about reporting bugs, and the
92       latest    version    of    this    page,    can     be     found     at
93       https://www.kernel.org/doc/man-pages/.
94
95
96
97Linux                             2017-09-15                          FUTEX(7)
Impressum