Seth Woolley's Man Viewer

Manual for futex - man futex

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title

FUTEX(2)                   Linux Programmer's Manual                  FUTEX(2)



NAME
       futex(2,4) - Fast Userspace Locking system call

SYNOPSIS
       #include <linux/futex.h>

       #include <sys/time.h>

       int futex(2,4) (int *uaddr, int op, int val, const struct timespec *timeout(1,3x,3x cbreak),
       int *uaddr2, int val3);

DESCRIPTION
       The futex(2,4) system call provides a method for a program  to  wait  for  a
       value  at  a  given  address  to change, and a method to wake up anyone
       waiting on a particular address (while the addresses for the same  mem-
       ory in(1,8) separate processes may not be equal, the kernel maps them inter-
       nally so the same memory mapped in(1,8) different locations will  correspond
       for futex(2,4) calls).  It is typically used to implement the contended case
       of a lock in(1,8) shared memory, as described in(1,8) futex(2,4)(4).

       When a futex(2,4)(4) operation did not finish uncontended  in(1,8)  userspace,  a
       call  needs  to  be  made  to  the kernel to arbitrate. Arbitration can
       either mean putting the calling process to sleep(1,3) or, conversely, waking
       a waiting process.

       Callers of this function are expected to adhere to the semantics as set(7,n,1 builtins)
       out in(1,8) futex(2,4)(4).   As  these  semantics  involve  writing  non-portable
       assembly instructions, this in(1,8) turn probably means that most users(1,5) will
       in(1,8) fact be library authors and not general application developers.

       The uaddr argument needs to point to an aligned  integer  which  stores
       the  counter.  The operation to execute is passed via the op parameter,
       along with a value val.

       Five operations are currently defined:

       FUTEX_WAIT
              This operation atomically verifies that the futex(2,4) address  uaddr
              still  contains the value val, and sleeps awaiting FUTEX_WAKE on
              this futex(2,4) address. If the timeout(1,3x,3x cbreak)  argument  is  non-NULL,  its
              contents  describe  the  maximum  duration of the wait, which is
              infinite otherwise. The arguments uaddr2 and val3 are ignored.

              For futex(2,4)(4), this call is executed if(3,n)  decrementing  the  count
              gave  a  negative  value (indicating contention), and will sleep(1,3)
              until another  process  releases  the  futex(2,4)  and  executes  the
              FUTEX_WAKE operation.

       FUTEX_WAKE
              This operation wakes at most val processes waiting on this futex(2,4)
              address (ie. inside FUTEX_WAIT).  The arguments timeout(1,3x,3x cbreak),  uaddr2
              and val3 are ignored.

              For  futex(2,4)(4), this is executed if(3,n) incrementing the count showed
              that there were waiters, once the futex(2,4) value has been set(7,n,1 builtins) to  1
              (indicating that it is available).

       FUTEX_FD
              To  support  asynchronous  wakeups,  this operation associates a
              file(1,n) descriptor with a futex.  If  another  process  executes  a
              FUTEX_WAKE,  the process will receive the signal(2,7) number that was
              passed in(1,8) val.  The calling process must close(2,7,n) the returned file(1,n)
              descriptor  after  use.   The arguments timeout(1,3x,3x cbreak), uaddr2 and val3
              are ignored.

              To prevent race conditions, the caller should test if(3,n) the  futex(2,4)
              has been upped after FUTEX_FD returns.

       FUTEX_REQUEUE (since Linux 2.5.70)
              This  operation  was  introduced in(1,8) order to avoid a "thundering
              herd" effect when FUTEX_WAKE is used and all processes woken  up
              need to acquire another futex. This call wakes up val processes,
              and requeues all other waiters on the futex(2,4) at  address  uaddr2.
              The arguments timeout(1,3x,3x cbreak) and val3 are ignored.

       FUTEX_CMP_REQUEUE (since Linux 2.6.7)
              There  was  a  race  in(1,8)  the  intended  use of FUTEX_REQUEUE, so
              FUTEX_CMP_REQUEUE   was   introduced.   This   is   similar   to
              FUTEX_REQUEUE, but first checks whether the location uaddr still
              contains the value val3.  If not, an error(8,n) EAGAIN  is  returned.
              The argument timeout(1,3x,3x cbreak) is ignored.

RETURN VALUE
       Depending  on which operation was executed, the returned value can have
       differing meanings.

       FUTEX_WAIT
              Returns 0 if(3,n) the process was woken by a FUTEX_WAKE call. In case
              of timeout(1,3x,3x cbreak), ETIMEDOUT is returned. If the futex(2,4) was not equal to
              the expected value, the operation returns  EWOULDBLOCK.  Signals
              (or other spurious wakeups) cause FUTEX_WAIT to return EINTR.

       FUTEX_WAKE
              Returns the number of processes woken up.

       FUTEX_FD
              Returns the new file(1,n) descriptor associated with the futex.

       FUTEX_REQUEUE
              Returns the number of processes woken up.

       FUTEX_CMP_REQUEUE
              Returns the number of processes woken up.

ERRORS
       EACCES No read(2,n,1 builtins) access(2,5) to futex(2,4) memory.

       EAGAIN FUTEX_CMP_REQUEUE found an unexpected futex(2,4) value.  (This proba-
              bly indicates a race; use the safe FUTEX_WAKE now.)

       EFAULT Error in(1,8) getting timeout(1,3x,3x cbreak) information from userspace.

       EINVAL An operation was not defined or error(8,n) in(1,8) page alignment.

       ENFILE The system limit on the total number  of  open(2,3,n)  files  has  been
              reached.

NOTES
       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(2,n,1 builtins) the sources of the futex(2,4) userspace library referenced
       below.

VERSIONS
       Initial futex(2,4) support was merged in(1,8)  Linux  2.5.7  but  with  different
       semantics from what was described above. A 4-parameter system call with
       the semantics given here was  introduced  in(1,8)  Linux  2.5.40.  In  Linux
       2.5.70  one  parameter  was added. In Linux 2.6.7 a sixth parameter was
       added - messy, especially on the s390 architecture.

SEE ALSO
       futex(2,4)(4), `Fuss, Futexes and Furwocks: Fast Userlevel Locking in(1,8) Linux'
       (proceedings  of  the  Ottawa  Linux  Symposium  2002),  futex(2,4)  example
       library,  futex-*.tar.bz2  <URL:ftp://ftp.nl.kernel.org:/pub/linux/ker-
       nel/people/rusty/>.



Linux 2.6.7                       2004-10-07                          FUTEX(2)

References for this manual (incoming links)