OSDN Git Service

ff6132f9cbb05317a1544000f3c81503af4dc92e
[linuxjm/LDP_man-pages.git] / draft / 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 .\"
9 .\" Japanese Version Copyright(C) 2003 Suzuki Takashi
10 .\"         all rights reserved.
11 .\" Translated Fri Oct 24 10:37:10 JST 2003
12 .\"         by Suzuki Takashi.
13 .\"
14 .\"WORD: contend                ¶¥¹ç¤¹¤ë
15 .\"WORD: sleep                  µ¯¾²ÂÔ¤Á¤¹¤ë
16 .\"WORD: wake                   µ¯¾²¤¹¤ë
17 .\"WORD: wake up                µ¯¾²¤¹¤ë
18 .\"
19 .TH FUTEX 7 2002-12-31 "Linux" "Linux Programmer's Manual"
20 .\"O .SH NAME
21 .SH Ì¾Á°
22 .\"O futex \- Fast Userspace Locking
23 futex - ¹â®¥æ¡¼¥¶¶õ´Ö¥í¥Ã¥¯µ¡¹½
24 .\"O .SH SYNOPSIS
25 .SH ½ñ¼°
26 .nf
27 .B #include <linux/futex.h>
28 .fi
29 .\"O .SH DESCRIPTION
30 .SH ÀâÌÀ
31 .PP
32 .\"O The Linux kernel provides futexes ("Fast Userspace muTexes")
33 .\"O as a building block for fast userspace
34 .\"O locking and semaphores.
35 .\"O Futexes are very basic and lend themselves well for building higher level
36 .\"O locking abstractions such as POSIX mutexes.
37 Linux ¥«¡¼¥Í¥ë¤Ï¡¢¥æ¡¼¥¶¶õ´Ö¤Ç¹â®¤Ê¥í¥Ã¥¯µ¡¹½¤ä¥»¥Þ¥Õ¥©¤ò»ÈÍѤ¹¤ë¤¿¤á¤Î
38 ´ðÁÃŪÍ×ÁǤȤ·¤Æ futex ("Fast Userspace muTexes"; ¹â®¥æ¡¼¥¶¶õ´Ö mutex) ¤ò
39 Ä󶡤·¤Æ¤¤¤ë¡£
40 futex ¤ÏÈó¾ï¤Ë´ðËÜŪ¤Ê¤â¤Î¤Ç¡¢ POSIX mutex ¤Î¤è¤¦¤Ê¹âÅ٤ʥí¥Ã¥¯µ¡¹½¤Î³µÇ°¤ò
41 ¹½ÃÛ¤¹¤ë¤Î¤ËÌòΩ¤Ã¤Æ¤¤¤ë¡£
42 .PP
43 .\"O This page does not set out to document all design decisions
44 .\"O but restricts itself to issues relevant for
45 .\"O application and library development.
46 .\"O Most programmers will in fact not be using futexes directly but
47 .\"O instead rely on system libraries built on them,
48 .\"O such as the NPTL pthreads implementation.
49 ¤³¤Î¥Ú¡¼¥¸¤Ï¤¹¤Ù¤Æ¤ÎÀß·×·èÄê¤òµ­½Ò¤¹¤ë¤è¤¦¤Ë¤Ï¤Ê¤Ã¤Æ¤ª¤é¤º¡¢
50 ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤ä¥é¥¤¥Ö¥é¥ê¤Î³«È¯¤Ë´Ø·¸¤¹¤ë¤³¤È¤¬¤é¤Ë¸Â¤Ã¤Æ¤¤¤ë¡£
51 ¼ÂºÝ¤Ë¤Ï¥×¥í¥°¥é¥Þ¤Î¿¤¯¤ÏľÀܤϠfutex ¤ò°·¤ï¤Ê¤¤¤¬¡¢¤½¤ÎÂå¤ï¤ê
52 futex ¤Ë´ð¤Å¤¤¤Æ¹½ÃÛ¤µ¤ì¤¿¥·¥¹¥Æ¥à¥é¥¤¥Ö¥é¥ê (Î㤨¤Ð NPTL ¥¹¥ì¥Ã¥É) ¤Ë
53 °Í¸¤¹¤ë¤³¤È¤Ë¤Ê¤ë¤À¤í¤¦¡£
54 .PP
55 .\"O A futex is identified by a piece of memory which can be
56 .\"O shared between different processes.
57 .\"O In these different processes, it need not have identical addresses.
58 .\"O In its bare form, a futex has semaphore semantics;
59 .\"O it is a counter that can be incremented and decremented atomically;
60 .\"O processes can wait for the value to become positive.
61 futex ¤Ï°Û¤Ê¤ë¥×¥í¥»¥¹´Ö¤Ç¶¦Í­¤¹¤ë¤³¤È¤Î¤Ç¤­¤ë¥á¥â¥êÊҤǼ±Ê̤µ¤ì¤ë¡£
62 ¤³¤ì¤é¤Î°Û¤Ê¤ë¥×¥í¥»¥¹¤Ç¤Ï¡¢Æ±¤¸¥¢¥É¥ì¥¹¤¬ÉÕÍ¿¤µ¤ì¤Æ¤¤¤ëɬÍפϤʤ¤¡£
63 Íç¤Î»Ñ¤Ç¤Ï futex ¤Î¥»¥Þ¥ó¥Æ¥£¥¯¥¹¤Ï¥»¥Þ¥Õ¥©¤ÈƱ¤¸¤Ç¤¢¤ë¡£
64 futex ¤ÏÉÔ²ÄʬÁàºî¤Ç (atomically) ¥¤¥ó¥¯¥ê¥á¥ó¥È¤·¤¿¤ê¥Ç¥¯¥ê¥á¥ó¥È¤·¤¿¤ê¤Ç¤­¤ë
65 ¥«¥¦¥ó¥¿¤Ç¡¢¥×¥í¥»¥¹¤ÏÃͤ¬Àµ¤Ë¤Ê¤ë¤Î¤òÂԤĤ³¤È¤¬¤Ç¤­¤ë¡£
66 .PP
67 .\"O Futex operation is entirely userspace for the noncontended case.
68 .\"O The kernel is only involved to arbitrate the contended case.
69 .\"O As any sane design will strive for noncontention,
70 .\"O futexes are also optimized for this situation.
71 futex ¤ÎÁàºî¤Ï¡¢¶¥¹ç¤¬¤Ê¤¤¾ì¹ç¤Ë¤Ï´°Á´¤Ë¥æ¡¼¥¶¶õ´Ö¤Ç¹Ô¤Ê¤ï¤ì¤ë¡£
72 ¥«¡¼¥Í¥ë¤Ï¶¥¹ç¤¬µ¯¤³¤Ã¤¿¾ì¹ç¤ÎÃçºÛ¤Ë´ØÍ¿¤¹¤ë¤À¤±¤Ç¤¢¤ë¡£
73 Îɼ±¤¢¤ëÀ߷פǤ϶¥¹ç¤¬µ¯¤³¤é¤Ê¤¤¤è¤¦ÅØÎϤ¹¤ë¤¬¡¢
74 futex ¤â¶¥¹ç¾õÂ֤˴ؤ·¤ÆºÇŬ²½¤µ¤ì¤Æ¤¤¤ë¡£
75 .PP
76 .\"O In its bare form, a futex is an aligned integer which is
77 .\"O only touched by atomic assembler instructions.
78 .\"O Processes can share this integer using
79 .\"O .BR mmap (2),
80 .\"O via shared memory segments or because they share memory space,
81 .\"O in which case the application is commonly called multithreaded.
82 Íç¤Î»Ñ¤Ç¤Ï¡¢ futex ¤ÏÉÔ²Äʬ¤Ê¥¢¥»¥ó¥Ö¥êÌ¿Îá¤Ç¤Î¤ßÁàºî¤µ¤ì¤ë
83 ¥¢¥é¥¤¥ó¥á¥ó¥È¤Î·¤Ã¤¿ int ·¿¤ÎÊÑ¿ô¤Ç¤¢¤ë¡£
84 Ê£¿ô¤Î¥×¥í¥»¥¹¤Ï¤³¤Î int ·¿ÊÑ¿ô¤ò¡¢
85 .BR mmap (2)
86 ¤òÍѤ¤¤ë¤«¡¢
87 ¶¦Í­¥á¥â¥ê¥»¥°¥á¥ó¥È¤ò²ð¤¹¤ë¤«¡¢
88 ¥á¥â¥ê¶õ´Ö¤ò¶¦Í­¤¹¤ë (¤³¤Î¾ì¹ç¡¢
89 ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ï°ìÈÌŪ¤Ë¥Þ¥ë¥Á¡¦¥¹¥ì¥Ã¥É¤Ç¤¢¤ë¤È¸Æ¤Ð¤ì¤ë) ¤«ÊýË¡¤Ç¶¦Í­¤¹¤ë¡£
90 .\"O .SS "Semantics"
91 .SS "¥»¥Þ¥ó¥Æ¥£¥¯¥¹"
92 .PP
93 .\"O Any futex operation starts in userspace,
94 .\"O but it may necessary to communicate with the kernel using the
95 .\"O .BR futex (2)
96 .\"O system call.
97 futex ¤ÎÁàºî¤Ï¤¹¤Ù¤Æ¥æ¡¼¥¶¶õ´Ö¤«¤é»Ï¤Þ¤ë¤¬¡¢É¬Íפ˱þ¤¸¤Æ
98 .BR futex (2)
99 ¥·¥¹¥Æ¥à¥³¡¼¥ë¤òÍѤ¤¤Æ¥«¡¼¥Í¥ë¤ÈÄÌ¿®¤¹¤ë¡£
100 .PP
101 .\"O To "up" a futex, execute the proper assembler instructions that
102 .\"O will cause the host CPU to atomically increment the integer.
103 .\"O Afterward, check if it has in fact changed from 0 to 1, in which case
104 .\"O there were no waiters and the operation is done.
105 .\"O This is the noncontended case which is fast and should be common.
106 futex ¤ò "up" ¤¹¤ë¤Ë¤Ï¡¢
107 ¥Û¥¹¥È CPU ¤ËÂФ· int ·¿ÊÑ¿ô¤òÉÔ²ÄʬÁàºî¤Ç¥¤¥ó¥¯¥ê¥á¥ó¥È¤¹¤ë¤è¤¦¤Ê¡¢
108 ŬÀڤʥ¢¥»¥ó¥Ö¥êÌ¿Îá¤ò¼Â¹Ô¤¹¤ë¡£
109 ¤½¤Î¤¢¤È¡¢¼ÂºÝ¤Ë 0 ¤«¤é 1 ¤ËÊѲ½¤·¤¿¤«¤É¤¦¤«¤ò¥Á¥§¥Ã¥¯¤·¡¢
110 ÊѲ½¤·¤Æ¤¤¤ì¤ÐÂÔ¤Á¥×¥í¥»¥¹ (waiter) ¤Ï¤Ê¤¤¤È¤¤¤¦¤³¤È¤Ç¤¢¤ê¡¢Áàºî¤Ï´°Î»¤¹¤ë¡£
111 ¤³¤ì¤Ï¶¥¹ç¤Î¤Ê¤¤¾ì¹ç¤Ç¤¢¤ê¡¢¹â®¤Ç¤è¤¯µ¯¤³¤ë¤Ï¤º¤Ç¤¢¤ë¡£
112 .PP
113 .\"O In the contended case, the atomic increment changed the counter
114 .\"O from \-1  (or some other negative number).
115 .\"O If this is detected, there are waiters.
116 .\"O Userspace should now set the counter to 1 and instruct the
117 .\"O kernel to wake up any waiters using the
118 .\"O .B FUTEX_WAKE
119 .\"O operation.
120 ¶¥¹ç¤¬¤¢¤ë¾ì¹ç¡¢ÉÔ²ÄʬÁàºî¤Î¥¤¥ó¥¯¥ê¥á¥ó¥È¤Ç¥«¥¦¥ó¥¿¤Ï \-1 (¤Þ¤¿¤Ï¾¤ÎÉé¤Î¿ô)
121 ¤«¤éÊѲ½¤¹¤ë¡£¤³¤ì¤¬¸¡½Ð¤µ¤ì¤ë¤È¡¢ÂÔ¤Á¥×¥í¥»¥¹¤¬¤¢¤ë¤È¤¤¤¦¤³¤È¤Ç¤¢¤ë¡£
122 ¥æ¡¼¥¶¶õ´Ö¤Ç¤Ï¥«¥¦¥ó¥¿¤ò 1 ¤ËÀßÄꤷ¡¢
123 .B FUTEX_WAKE
124 ¤òÍѤ¤¤Æ¥«¡¼¥Í¥ë¤ËÂÔ¤Á¥×¥í¥»¥¹¤ò wake (µ¯¾²) ¤µ¤»¤ë¤è¤¦»Ø¼¨¤¹¤ë¡£
125 .PP
126 .\"O Waiting on a futex, to "down" it, is the reverse operation.
127 .\"O Atomically decrement the counter and check if it changed to 0,
128 .\"O in which case the operation is done and the futex was uncontended.
129 .\"O In all other circumstances, the process should set the counter to \-1
130 .\"O and request that the kernel wait for another process to up the futex.
131 .\"O This is done using the
132 .\"O .B FUTEX_WAIT
133 .\"O operation.
134 futex ¤Î³ÍÆÀ¤òÂԤġ¢¤¹¤Ê¤ï¤Á futex ¤ò "down" ¤¹¤ë¤Ë¤ÏÈ¿ÂФÎÁàºî¤ò¹Ô¤Ê¤¦¡£
135 ÉÔ²ÄʬÁàºî¤Ç¥«¥¦¥ó¥¿¤ò¥Ç¥¯¥ê¥á¥ó¥È¤·¡¢¥«¥¦¥ó¥¿¤¬ 0 ¤ËÊѲ½¤·¤¿¤«¤É¤¦¤«¤ò
136 ¥Á¥§¥Ã¥¯¤¹¤ë¡£ÊѲ½¤·¤Æ¤¤¤ì¤ÐÁàºî¤Ï´°Î»¤· futex ¤Ï¶¥¹ç¤·¤Æ¤¤¤Ê¤¤¤È¤¤¤¦¤³¤È¤Ç¤¢¤ë¡£
137 0 ¤Ë¤Ê¤é¤Ê¤«¤Ã¤¿¾ì¹ç¡¢¥×¥í¥»¥¹¤Ï¥«¥¦¥ó¥¿¤ò \-1 ¤ËÀßÄꤷ¡¢
138 ¾¤Î¥×¥í¥»¥¹¤¬¤½¤Î futex ¤ò up ¤¹¤ë¤Î¤òÂԤĤ褦¥«¡¼¥Í¥ë¤ËÍ׵ᤷ¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
139 ¤³¤ì¤Ï
140 .B FUTEX_WAIT
141 ¤ò¹Ô¤Ê¤¦¤³¤È¤Ç¼Â¸½¤µ¤ì¤ë¡£
142 .PP
143 .\"O The
144 .\"O .BR futex (2)
145 .\"O system call can optionally be passed a timeout specifying how long
146 .\"O the kernel should
147 .\"O wait for the futex to be upped.
148 .\"O In this case, semantics are more complex and the programmer is referred
149 .\"O to
150 .\"O .BR futex (2)
151 .\"O for
152 .\"O more details.
153 .\"O The same holds for asynchronous futex waiting.
154 .BR futex (2)
155 ¥·¥¹¥Æ¥à¥³¡¼¥ë¤Ë¤Ï¡¢¾Êά²Äǽ¤Ê°ú¿ô¤È¤·¤Æ¥¿¥¤¥à¥¢¥¦¥È¤òÅϤ¹¤³¤È¤¬¤Ç¤­¡¢
156 ¥«¡¼¥Í¥ë¤Ï¤½¤Î futex ¤¬ up ¤µ¤ì¤ë¤Î¤ò¤É¤ì¤¯¤é¤¤¤Î´ü´ÖÂԤĤ٤­¤«¤ò
157 »ØÄꤹ¤ë¤³¤È¤¬¤Ç¤­¤ë¡£¤³¤Î¾ì¹ç¡¢¥»¥Þ¥ó¥Æ¥£¥¯¥¹¤Ï¤â¤Ã¤ÈÊ£»¨¤Ë¤Ê¤ë¤¿¤á¡¢
158 ¤è¤ê¾ÜºÙ¤Ê¾ðÊó¤òÆÀ¤ë¤Ë¤Ï¥×¥í¥°¥é¥Þ¤Ï
159 .BR futex (2)
160 ¤ò»²¾È¤¹¤ë¤³¤È¡£
161 Ʊ¤¸¥Ú¡¼¥¸¤ËÈóƱ´ü¤Î futex ÂÔ¤Á¤Ë¤Ä¤¤¤Æ¤âµ­¤µ¤ì¤Æ¤¤¤ë¡£
162 .\"O .SH "VERSIONS"
163 .SH ¥Ð¡¼¥¸¥ç¥ó
164 .PP
165 .\"O Initial futex support was merged in Linux 2.5.7
166 .\"O but with different semantics from those described above.
167 .\"O Current semantics are available from Linux 2.5.40 onward.
168 ºÇ½é¤Î futex Âбþ¤Ï Linux 2.5.7 ¤ÇÁȤ߹þ¤Þ¤ì¤¿¤¬¡¢
169 ¾åµ­¤Î¥»¥Þ¥ó¥Æ¥£¥¯¥¹¤È¤Ï°Û¤Ê¤ë¡£
170 ¸½ºß¤Î¥»¥Þ¥ó¥Æ¥£¥¯¥¹¤Ï Linux 2.5.40 °Ê¹ß¤ÇÍøÍѲÄǽ¤Ç¤¢¤ë¡£
171 .\"O .SH "NOTES"
172 .SH Ãí°Õ
173 .PP
174 .\"O To reiterate, bare futexes are not intended as an easy to use
175 .\"O abstraction for end-users.
176 .\"O Implementors are expected to be assembly literate and to have read
177 .\"O the sources of the futex userspace library referenced
178 .\"O below.
179 ºÆ¤Ó·«¤êÊÖ¤·¤Æ¤ª¤¯¤¬¡¢Íç¤Î futex ¤Ï¥¨¥ó¥É¥æ¡¼¥¶¤¬Íưפ˻Ȥ¨¤ë³µÇ°¤È¤·¤Æ
180 °Õ¿Þ¤µ¤ì¤¿¤â¤Î¤Ç¤Ï¤Ê¤¤¡£
181 ¼ÂÁõ¼Ô¤Ï¡¢¥¢¥»¥ó¥Ö¥ê¸À¸ì¤Ë´·¤ì¤Æ¤ª¤ê¡¢°Ê²¼¤Ëµó¤²¤ë futex ¥æ¡¼¥¶¶õ´Ö¥é¥¤¥Ö¥é¥ê¤Î
182 ¥½¡¼¥¹¤òÆɤ߽ª¤¨¤Æ¤¤¤ë¤³¤È¤¬Í׵ᤵ¤ì¤ë¡£
183 .PP
184 .\"O This man page illustrates the most common use of the
185 .\"O .BR futex (2)
186 .\"O primitives: it is by no means the only one.
187 ¤³¤Î¥Þ¥Ë¥å¥¢¥ë¥Ú¡¼¥¸¤Ë¤Ï
188 .BR futex (2)
189 ¥×¥ê¥ß¥Æ¥£¥Ö¤ÎºÇ¤â°ìÈÌŪ¤Ê»ÈÍÑË¡¤¬
190 µ­¤µ¤ì¤Æ¤¤¤ë¡£¤³¤ì¤Ï·è¤·¤ÆÍ£°ì¤Î»ÈÍÑË¡¤Ç¤Ï¤Ê¤¤¡£
191 .\"O .\" .SH "AUTHORS"
192 .\" .SH Ãø¼Ô
193 .\" .PP
194 .\"O .\" Futexes were designed and worked on by Hubertus Franke
195 .\"O .\" (IBM Thomas J. Watson Research Center),
196 .\"O .\" Matthew Kirkwood, Ingo Molnar (Red Hat) and
197 .\"O .\" Rusty Russell (IBM Linux Technology Center).
198 .\"O .\" This page written by bert hubert.
199 .\" futex ¤Ï Hubertus Franke (IBM Thomas J. Watson Research Center) ¤È
200 .\" Matthew Kirkwood ¡¢ Ingo Molnar (Red Hat) ¡¢
201 .\" Rusty Russell (IBM Linux Technology Center) ¤¬À߷פ·¡¢¸¡Æ¤¤·¤¿¡£
202 .\" ¤³¤Î¥Ú¡¼¥¸¤Ï bert hubert ¤¬µ­¤·¤¿¡£
203 .\"O .SH "SEE ALSO"
204 .SH ´ØÏ¢¹àÌÜ
205 .BR futex (2)
206 .PP
207 .\"O .IR "Fuss, Futexes and Furwocks: Fast Userlevel Locking in Linux"
208 .\"O (proceedings of the Ottawa Linux Symposium 2002),
209 .\"O futex example library, futex-*.tar.bz2
210 .\"O <URL:ftp://ftp.kernel.org/pub/linux/kernel/people/rusty/>.
211 .IR "Fuss, Futexes and Furwocks: Fast Userlevel Locking in Linux"
212 (proceedings of the Ottawa Linux Symposium 2002),
213 futex ¤Î»ÈÍÑÎã¥é¥¤¥Ö¥é¥ê, futex-*.tar.bz2
214 <URL:ftp://ftp.kernel.org/pub/linux/kernel/people/rusty/>.