Blame man7/futex.7

Packit 7cfc04
.\" This manpage has been automatically generated by docbook2man
Packit 7cfc04
.\" from a DocBook document.  This tool can be found at:
Packit 7cfc04
.\" <http://shell.ipoline.com/~elmert/comp/docbook2X/>
Packit 7cfc04
.\" Please send any bug reports, improvements, comments, patches,
Packit 7cfc04
.\" etc. to Steve Cheng <steve@ggi-project.org>.
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(MIT)
Packit 7cfc04
.\" This page is made available under the MIT license.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.TH FUTEX 7 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
futex \- fast user-space locking
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <linux/futex.h>
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
.PP
Packit 7cfc04
The Linux kernel provides futexes ("Fast user-space mutexes")
Packit 7cfc04
as a building block for fast user-space
Packit 7cfc04
locking and semaphores.
Packit 7cfc04
Futexes are very basic and lend themselves well for building higher-level
Packit 7cfc04
locking abstractions such as
Packit 7cfc04
mutexes, condition variables, read-write locks, barriers, and semaphores.
Packit 7cfc04
.PP
Packit 7cfc04
Most programmers will in fact not be using futexes directly but will
Packit 7cfc04
instead rely on system libraries built on them,
Packit 7cfc04
such as the Native POSIX Thread Library (NPTL) (see
Packit 7cfc04
.BR pthreads (7)).
Packit 7cfc04
.PP
Packit 7cfc04
A futex is identified by a piece of memory which can be
Packit 7cfc04
shared between processes or threads.
Packit 7cfc04
In these different processes, the futex need not have identical addresses.
Packit 7cfc04
In its bare form, a futex has semaphore semantics;
Packit 7cfc04
it is a counter that can be incremented and decremented atomically;
Packit 7cfc04
processes can wait for the value to become positive.
Packit 7cfc04
.PP
Packit 7cfc04
Futex operation occurs entirely in user space for the noncontended case.
Packit 7cfc04
The kernel is involved only to arbitrate the contended case.
Packit 7cfc04
As any sane design will strive for noncontention,
Packit 7cfc04
futexes are also optimized for this situation.
Packit 7cfc04
.PP
Packit 7cfc04
In its bare form, a futex is an aligned integer which is
Packit 7cfc04
touched only by atomic assembler instructions.
Packit 7cfc04
This integer is four bytes long on all platforms.
Packit 7cfc04
Processes can share this integer using
Packit 7cfc04
.BR mmap (2),
Packit 7cfc04
via shared memory segments, or because they share memory space,
Packit 7cfc04
in which case the application is commonly called multithreaded.
Packit 7cfc04
.SS Semantics
Packit 7cfc04
.PP
Packit 7cfc04
Any futex operation starts in user space,
Packit 7cfc04
but it may be necessary to communicate with the kernel using the
Packit 7cfc04
.BR futex (2)
Packit 7cfc04
system call.
Packit 7cfc04
.PP
Packit 7cfc04
To "up" a futex, execute the proper assembler instructions that
Packit 7cfc04
will cause the host CPU to atomically increment the integer.
Packit 7cfc04
Afterward, check if it has in fact changed from 0 to 1, in which case
Packit 7cfc04
there were no waiters and the operation is done.
Packit 7cfc04
This is the noncontended case which is fast and should be common.
Packit 7cfc04
.PP
Packit 7cfc04
In the contended case, the atomic increment changed the counter
Packit 7cfc04
from \-1  (or some other negative number).
Packit 7cfc04
If this is detected, there are waiters.
Packit 7cfc04
User space should now set the counter to 1 and instruct the
Packit 7cfc04
kernel to wake up any waiters using the
Packit 7cfc04
.B FUTEX_WAKE
Packit 7cfc04
operation.
Packit 7cfc04
.PP
Packit 7cfc04
Waiting on a futex, to "down" it, is the reverse operation.
Packit 7cfc04
Atomically decrement the counter and check if it changed to 0,
Packit 7cfc04
in which case the operation is done and the futex was uncontended.
Packit 7cfc04
In all other circumstances, the process should set the counter to \-1
Packit 7cfc04
and request that the kernel wait for another process to up the futex.
Packit 7cfc04
This is done using the
Packit 7cfc04
.B FUTEX_WAIT
Packit 7cfc04
operation.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.BR futex (2)
Packit 7cfc04
system call can optionally be passed a timeout specifying how long
Packit 7cfc04
the kernel should
Packit 7cfc04
wait for the futex to be upped.
Packit 7cfc04
In this case, semantics are more complex and the programmer is referred
Packit 7cfc04
to
Packit 7cfc04
.BR futex (2)
Packit 7cfc04
for
Packit 7cfc04
more details.
Packit 7cfc04
The same holds for asynchronous futex waiting.
Packit 7cfc04
.SH VERSIONS
Packit 7cfc04
.PP
Packit 7cfc04
Initial futex support was merged in Linux 2.5.7
Packit 7cfc04
but with different semantics from those described above.
Packit 7cfc04
Current semantics are available from Linux 2.5.40 onward.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
.PP
Packit 7cfc04
To reiterate, bare futexes are not intended as an easy-to-use
Packit 7cfc04
abstraction for end users.
Packit 7cfc04
Implementors are expected to be assembly literate and to have read
Packit 7cfc04
the sources of the futex user-space library referenced
Packit 7cfc04
below.
Packit 7cfc04
.PP
Packit 7cfc04
This man page illustrates the most common use of the
Packit 7cfc04
.BR futex (2)
Packit 7cfc04
primitives; it is by no means the only one.
Packit 7cfc04
.\" .SH AUTHORS
Packit 7cfc04
.\" .PP
Packit 7cfc04
.\" Futexes were designed and worked on by Hubertus Franke
Packit 7cfc04
.\" (IBM Thomas J. Watson Research Center),
Packit 7cfc04
.\" Matthew Kirkwood, Ingo Molnar (Red Hat) and
Packit 7cfc04
.\" Rusty Russell (IBM Linux Technology Center).
Packit 7cfc04
.\" This page written by bert hubert.
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR clone (2),
Packit 7cfc04
.BR futex (2),
Packit 7cfc04
.BR get_robust_list (2),
Packit 7cfc04
.BR set_robust_list (2),
Packit 7cfc04
.BR set_tid_address (2),
Packit 7cfc04
.BR pthreads (7)
Packit 7cfc04
.PP
Packit 7cfc04
.IR "Fuss, Futexes and Furwocks: Fast Userlevel Locking in Linux"
Packit 7cfc04
(proceedings of the Ottawa Linux Symposium 2002),
Packit 7cfc04
futex example library, futex-*.tar.bz2
Packit 7cfc04
.UR ftp://ftp.kernel.org\:/pub\:/linux\:/kernel\:/people\:/rusty/
Packit 7cfc04
.UE .
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.