OSDN Git Service

95a0ea1ef400ca620dc8ee2f0f069ee80e6e3c03
[linuxjm/LDP_man-pages.git] / original / man7 / futex.7
1 .\" This page is made available under the MIT license.
2 .\"
3 .\" This manpage has been automatically generated by docbook2man
4 .\" from a DocBook document.  This tool can be found at:
5 .\" <http://shell.ipoline.com/~elmert/comp/docbook2X/>
6 .\" Please send any bug reports, improvements, comments, patches,
7 .\" etc. to Steve Cheng <steve@ggi-project.org>.
8 .TH FUTEX 7 2002-12-31 "Linux" "Linux Programmer's Manual"
9 .SH NAME
10 futex \- Fast Userspace Locking
11 .SH SYNOPSIS
12 .nf
13 .B #include <linux/futex.h>
14 .fi
15 .SH DESCRIPTION
16 .PP
17 The Linux kernel provides futexes ("Fast Userspace muTexes")
18 as a building block for fast userspace
19 locking and semaphores.
20 Futexes are very basic and lend themselves well for building higher level
21 locking abstractions such as POSIX mutexes.
22 .PP
23 This page does not set out to document all design decisions
24 but restricts itself to issues relevant for
25 application and library development.
26 Most programmers will in fact not be using futexes directly but
27 instead rely on system libraries built on them,
28 such as the NPTL pthreads implementation.
29 .PP
30 A futex is identified by a piece of memory which can be
31 shared between different processes.
32 In these different processes, it need not have identical addresses.
33 In its bare form, a futex has semaphore semantics;
34 it is a counter that can be incremented and decremented atomically;
35 processes can wait for the value to become positive.
36 .PP
37 Futex operation is entirely userspace for the noncontended case.
38 The kernel is only involved to arbitrate the contended case.
39 As any sane design will strive for noncontention,
40 futexes are also optimized for this situation.
41 .PP
42 In its bare form, a futex is an aligned integer which is
43 only touched by atomic assembler instructions.
44 Processes can share this integer using
45 .BR mmap (2),
46 via shared memory segments or because they share memory space,
47 in which case the application is commonly called multithreaded.
48 .SS "Semantics"
49 .PP
50 Any futex operation starts in userspace,
51 but it may necessary to communicate with the kernel using the
52 .BR futex (2)
53 system call.
54 .PP
55 To "up" a futex, execute the proper assembler instructions that
56 will cause the host CPU to atomically increment the integer.
57 Afterward, check if it has in fact changed from 0 to 1, in which case
58 there were no waiters and the operation is done.
59 This is the noncontended case which is fast and should be common.
60 .PP
61 In the contended case, the atomic increment changed the counter
62 from \-1  (or some other negative number).
63 If this is detected, there are waiters.
64 Userspace should now set the counter to 1 and instruct the
65 kernel to wake up any waiters using the
66 .B FUTEX_WAKE
67 operation.
68 .PP
69 Waiting on a futex, to "down" it, is the reverse operation.
70 Atomically decrement the counter and check if it changed to 0,
71 in which case the operation is done and the futex was uncontended.
72 In all other circumstances, the process should set the counter to \-1
73 and request that the kernel wait for another process to up the futex.
74 This is done using the
75 .B FUTEX_WAIT
76 operation.
77 .PP
78 The
79 .BR futex (2)
80 system call can optionally be passed a timeout specifying how long
81 the kernel should
82 wait for the futex to be upped.
83 In this case, semantics are more complex and the programmer is referred
84 to
85 .BR futex (2)
86 for
87 more details.
88 The same holds for asynchronous futex waiting.
89 .SH "VERSIONS"
90 .PP
91 Initial futex support was merged in Linux 2.5.7
92 but with different semantics from those described above.
93 Current semantics are available from Linux 2.5.40 onward.
94 .SH "NOTES"
95 .PP
96 To reiterate, bare futexes are not intended as an easy to use
97 abstraction for end-users.
98 Implementors are expected to be assembly literate and to have read
99 the sources of the futex userspace library referenced
100 below.
101 .PP
102 This man page illustrates the most common use of the
103 .BR futex (2)
104 primitives: it is by no means the only one.
105 .\" .SH "AUTHORS"
106 .\" .PP
107 .\" Futexes were designed and worked on by Hubertus Franke
108 .\" (IBM Thomas J. Watson Research Center),
109 .\" Matthew Kirkwood, Ingo Molnar (Red Hat) and
110 .\" Rusty Russell (IBM Linux Technology Center).
111 .\" This page written by bert hubert.
112 .SH "SEE ALSO"
113 .BR futex (2)
114 .PP
115 .IR "Fuss, Futexes and Furwocks: Fast Userlevel Locking in Linux"
116 (proceedings of the Ottawa Linux Symposium 2002),
117 futex example library, futex-*.tar.bz2
118 <URL:ftp://ftp.kernel.org/pub/linux/kernel/people/rusty/>.