OSDN Git Service

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