204 lines
5.8 KiB
HTML
204 lines
5.8 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of FUTEX</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>FUTEX</H1>
|
|
Section: Linux Programmer's Manual (7)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
futex - fast user-space locking
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/linux/futex.h">linux/futex.h</A>></B>
|
|
</PRE>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
<P>
|
|
|
|
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
|
|
mutexes, condition variables, read-write locks, barriers, and semaphores.
|
|
<P>
|
|
|
|
Most programmers will in fact not be using futexes directly but will
|
|
instead rely on system libraries built on them,
|
|
such as the Native POSIX Thread Library (NPTL) (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+pthreads">pthreads</A></B>(7)).
|
|
|
|
<P>
|
|
|
|
A futex is identified by a piece of memory which can be
|
|
shared between processes or threads.
|
|
In these different processes, the futex need not have identical addresses.
|
|
In its bare form, a futex has semaphore semantics;
|
|
it is a counter that can be incremented and decremented atomically;
|
|
processes can wait for the value to become positive.
|
|
<P>
|
|
|
|
Futex operation occurs entirely in 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.
|
|
<P>
|
|
|
|
In its bare form, a futex is an aligned integer which is
|
|
touched only by atomic assembler instructions.
|
|
This integer is four bytes long on all platforms.
|
|
Processes can share this integer using
|
|
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2),
|
|
|
|
via shared memory segments, or because they share memory space,
|
|
in which case the application is commonly called multithreaded.
|
|
<A NAME="lbAE"> </A>
|
|
<H3>Semantics</H3>
|
|
|
|
<P>
|
|
|
|
Any futex operation starts in user space,
|
|
but it may be necessary to communicate with the kernel using the
|
|
<B><A HREF="/cgi-bin/man/man2html?2+futex">futex</A></B>(2)
|
|
|
|
system call.
|
|
<P>
|
|
|
|
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.
|
|
<P>
|
|
|
|
In the contended case, the atomic increment changed the counter
|
|
from -1 (or some other negative number).
|
|
If this is detected, there are waiters.
|
|
User space should now set the counter to 1 and instruct the
|
|
kernel to wake up any waiters using the
|
|
<B>FUTEX_WAKE</B>
|
|
|
|
operation.
|
|
<P>
|
|
|
|
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 circumstances, 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
|
|
<B>FUTEX_WAIT</B>
|
|
|
|
operation.
|
|
<P>
|
|
|
|
The
|
|
<B><A HREF="/cgi-bin/man/man2html?2+futex">futex</A></B>(2)
|
|
|
|
system call can optionally be passed a timeout specifying how long
|
|
the kernel should
|
|
wait for the futex to be upped.
|
|
In this case, semantics are more complex and the programmer is referred
|
|
to
|
|
<B><A HREF="/cgi-bin/man/man2html?2+futex">futex</A></B>(2)
|
|
|
|
for
|
|
more details.
|
|
The same holds for asynchronous futex waiting.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
<P>
|
|
|
|
Initial futex support was merged in Linux 2.5.7
|
|
but with different semantics from those described above.
|
|
Current semantics are available from Linux 2.5.40 onward.
|
|
<A NAME="lbAG"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
<P>
|
|
|
|
To reiterate, bare futexes are not intended as an easy-to-use
|
|
abstraction for end users.
|
|
Implementors are expected to be assembly literate and to have read
|
|
the sources of the futex user-space library referenced
|
|
below.
|
|
<P>
|
|
|
|
This man page illustrates the most common use of the
|
|
<B><A HREF="/cgi-bin/man/man2html?2+futex">futex</A></B>(2)
|
|
|
|
primitives; it is by no means the only one.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<A NAME="lbAH"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clone">clone</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+futex">futex</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+get_robust_list">get_robust_list</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+set_robust_list">set_robust_list</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+set_tid_address">set_tid_address</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+pthreads">pthreads</A></B>(7)
|
|
|
|
<P>
|
|
|
|
<I>Fuss, Futexes and Furwocks: Fast Userlevel Locking in Linux</I>
|
|
|
|
(proceedings of the Ottawa Linux Symposium 2002),
|
|
futex example library, futex-*.tar.bz2
|
|
|
|
|
|
<A NAME="lbAI"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DL>
|
|
<DT id="4"><A HREF="#lbAE">Semantics</A><DD>
|
|
</DL>
|
|
<DT id="5"><A HREF="#lbAF">VERSIONS</A><DD>
|
|
<DT id="6"><A HREF="#lbAG">NOTES</A><DD>
|
|
<DT id="7"><A HREF="#lbAH">SEE ALSO</A><DD>
|
|
<DT id="8"><A HREF="#lbAI">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:06:08 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|