#include <linux/futex.h>

       The  Linux  kernel  provides  futexes  ("Fast user-space mutexes") as a
       building block for fast user-space locking and semaphores.  Futexes are
       very  basic  and lend themselves well for building higher level locking
       abstractions such as POSIX mutexes.

       This page does not  set  out  to  document  all  design  decisions  but
       restricts  itself to issues relevant for application and library devel-
       opment.  Most programmers will in fact not be  using  futexes  directly
       but  instead  rely  on system libraries built on them, such as the NPTL
       pthreads implementation.

       A futex is identified by a piece of memory which can be shared  between
       different  processes.   In  these different processes, it need not have
       identical addresses.  In its bare form, a futex  has  semaphore  seman-
       tics;  it  is  a counter that can be incremented and decremented atomi-
       cally; processes can wait for the value to become positive.

       Futex operation is entirely user space for the noncontended case.   The
       kernel  is  involved only to arbitrate the contended case.  As any sane
       design will strive for noncontention, futexes are  also  optimized  for
       this situation.

       In  its  bare form, a futex is an aligned integer which is touched only
       by atomic assembler instructions.  Processes  can  share  this  integer
       using  mmap(2), via shared memory segments or because they share memory
       space, in which case the application is commonly called multithreaded.

       Any futex operation starts in user space, but it may  be  necessary  to
       communicate with the kernel using the futex(2) system call.

       To  "up"  a  futex, execute the proper assembler instructions that will
       cause the host CPU to atomically  increment  the  integer.   Afterward,
       check  if  it has in fact changed from 0 to 1, in which case there were
       no waiters and the operation is done.  This is  the  noncontended  case
       which is fast and should be common.

       In the contended case, the atomic increment changed the counter from -1
       (or some other negative number).  If this is detected, there are  wait-
       ers.   User space should now set the counter to 1 and instruct the ker-
       nel to wake up any waiters using the FUTEX_WAKE operation.

       Waiting on a futex, to "down" it, is the reverse operation.  Atomically
       decrement  the  counter and check if it changed to 0, in which case the
       operation is done and the futex was uncontended.  In all other  circum-
       stances,  the process should set the counter to -1 and request that the
       kernel wait for another process to up the futex.  This  is  done  using
       the FUTEX_WAIT operation.

       To  reiterate, bare futexes are not intended as an easy to use abstrac-
       tion for end-users.  Implementors are expected to be assembly  literate
       and to have read the sources of the futex user-space library referenced

       This man page illustrates the most common use of  the  futex(2)  primi-
       tives: it is by no means the only one.


       Fuss,  Futexes  and Furwocks: Fast Userlevel Locking in Linux (proceed-
       ings of the  Ottawa  Linux  Symposium  2002),  futex  example  library,
       futex-*.tar.bz2 <ftp://ftp.kernel.org/pub/linux/kernel/people/rusty/>.

       This  page  is  part of release 3.54 of the Linux man-pages project.  A
       description of the project, and information about reporting  bugs,  can
       be found at http://www.kernel.org/doc/man-pages/.

Linux                             2012-08-05                          FUTEX(7)
Man Pages Copyright Respective Owners. Site Copyright (C) 1994 - 2019 Hurricane Electric. All Rights Reserved.