OSDN Git Service

(split) LDP: Update drafts from ja.po of LDP v3.50
[linuxjm/LDP_man-pages.git] / release / man3 / pthread_cleanup_push.3
1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\"     <mtk.manpages@gmail.com>
3 .\"
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\"
24 .\"*******************************************************************
25 .\"
26 .\" This file was generated with po4a. Translate the source file.
27 .\"
28 .\"*******************************************************************
29 .TH PTHREAD_CLEANUP_PUSH 3 2008\-11\-24 Linux "Linux Programmer's Manual"
30 .SH 名前
31 pthread_cleanup_push, pthread_cleanup_pop \- スレッドの
32 キャンセルクリーンアップハンドラの push/pop を行う
33 .SH 書式
34 .nf
35 \fB#include <pthread.h>\fP
36
37 \fBvoid pthread_cleanup_push(void (*\fP\fIroutine\fP\fB)(void *),\fP
38 \fB                          void *\fP\fIarg\fP\fB);\fP
39 \fBvoid pthread_cleanup_pop(int \fP\fIexecute\fP\fB);\fP
40 .sp
41 \fI\-pthread\fP でコンパイルしてリンクする。
42 .fi
43 .SH 説明
44 これらの関数は、呼び出したスレッドのスレッドキャンセル時のクリーンアッ
45 プハンドラのスタックの操作を行う。クリーンアップハンドラは、スレッドが
46 キャンセルされた場合 (や以下で説明する他の種々の状況において) 自動的に
47 実行される関数である。例えば、mutex のロック解除を行い、プロセス内の
48 他のスレッドが利用できるようにする関数などが考えられる。
49
50 \fBpthread_cleanup_push\fP() 関数は、 \fIroutine\fP をクリーンアップ
51 ハンドラのスタックの一番上にプッシュする。 \fIroutine\fP が後で
52 起動される際には、 \fIarg\fP が関数の引き数と渡される。
53
54 \fBpthread_cleanup_pop\fP() 関数は、クリーンアップハンドラの
55 スタックの一番上のルーチンを削除する。
56 \fIexecute\fP が 0 以外の場合にはそのルーチンを追加で実行する。
57
58 キャンセルクリーンアップハンドラは、以下に示す場合に
59 スタックから取り出され実行される。
60 .IP 1. 3
61 スレッドがキャンセルされた際に、スタックに登録された全てのクリーン
62 アップハンドラが取り出されて、実行される。クリーンアップハンドラの
63 実行は、スタックに登録されたのと逆の順序で行われる。
64 .IP 2.
65 スレッドが \fBpthread_exit\fP(3) を呼び出して終了する際に、全てのクリーン
66 アップハンドラが上の項目で述べたのと同様に実行される。
67 (スレッドがスレッド開始関数からの \fIreturn\fP の実行により終了する場合に
68 は、クリーンアップハンドラは\fI呼び出されない\fP。)
69 .IP 3.
70 スレッドが 0 以外の \fIexecute\fP 引き数で \fBpthread_cleanup_pop\fP() を
71 呼び出した際に、スタックの一番上のクリーンアップハンドラが取り出されて
72 実行される。
73 .PP
74 POSIX.1 では、 \fBpthread_cleanup_push\fP() と \fBpthread_cleanup_pop\fP() を
75 それぞれ \(aq\fB{\fP\(aq と \(aq\fB}\fP\(aq を含むテキストに展開するマクロと
76 して実装することを許容している。
77 このため、呼び出し側では、これらの関数の呼び出しが同じ関数の中で対と
78 なり、かつ文法的に同じネストレベル (nesting level) になることを保証
79 しなければならない。 (言い換えると、クリーンアップハンドラは、コード
80 の特定のセクションの実行の中でのみ設定するものであると言える。)
81
82 \fBlongjmp\fP(3) (\fBsiglongjmp\fP(3)) の呼び出しは、
83 \fBpthread_cleanup_push\fP() や \fBpthread_cleanup_pop\fP() の呼び出しが対と
84 なる呼び出しがない状態で行われた場合には、どのような結果になるかは不定
85 である。これは jump バッファは \fBsetjmp\fP(3) (\fBsigsetjmp\fP(3)) により設
86 定されるからである。同様に、クリーンアップハンドラ内からの
87 \fBlongjmp\fP(3) (\fBsiglongjmp\fP(3)) の呼び出しも、jump バッファがハンドラ
88 内で \fBsetjmp\fP(3) (\fBsigsetjmp\fP(3)) で設定されていない限り、どのような
89 結果になるかは不定である。
90 .SH 返り値
91 これらの関数は値を返さない。
92 .SH エラー
93 .\" SH VERSIONS
94 .\" Available since glibc 2.0
95 エラーはない。
96 .SH 準拠
97 POSIX.1\-2001.
98 .SH 注意
99 Linux では、関数 \fBpthread_cleanup_push\fP() と \fBpthread_cleanup_pop\fP()
100 は、それぞれ \(aq\fB{\fP\(aq と \(aq\fB}\fP\(aq を含むテキストに展開する
101 マクロとして実装されている。このことは、これらの関数を対で呼び出した
102 スコープ内で宣言された変数は、そのスコープの中でしか参照できない
103 ということを意味している。
104
105 .\" The text was actually added in the 2004 TC2
106 POSIX.1 には、括弧を含む \fBpthread_cleanup_push\fP() と
107 \fBpthread_cleanup_pop\fP() のブロックをそのままにしたままで、
108 \fIreturn\fP, \fIbreak\fP, \fIcontinue\fP, \fIgoto\fP を使った場合の影響は
109 不定であると書かれている。
110 移植性が必要なアプリケーションではこれを行うのは避けるべきである。
111 .SH 例
112 以下のプログラムは、このページで説明した関数の簡単な使用例を示すもので
113 ある。このプログラムは \fBpthread_cleanup_push\fP() と
114 \fBpthread_cleanup_pop\fP() で囲まれたループを実行するスレッドを作成する。
115 このループではグローバル変数 \fIcnt\fP を 1 秒に 1 ずつ増やしていく。
116 指定されたコマンドライン引き数の内容に基づいて、メインスレッドはもう一
117 つのスレッドにキャンセル要求を送ったり、もう一つのスレッドがループを
118 抜けて (\fIreturn\fP を呼び出して) 正常終了するようにグローバル変数を
119 設定したりする。
120
121 以下のシェルセッションでは、メインスレッドはもう一つのスレッドに
122 キャンセル要求を送信する。
123
124 .in +4n
125 .nf
126 $ \fB./a.out\fP
127 New thread started
128 cnt = 0
129 cnt = 1
130 Canceling thread
131 Called clean\-up handler
132 Thread was canceled; cnt = 0
133 .fi
134 .in
135
136 上記の実行例から、スレッドがキャンセルされ、
137 キャンセルクリーンアップハンドラが呼び出され、
138 グローバル変数 \fIcnt\fP の値が 0 にリセットされていることが確認できる。
139
140 次の実行例では、メインプログラムはグローバル変数を設定して、
141 もう一つのスレッドが正常終了するようにしている。
142
143 .in +4n
144 .nf
145 $ \fB./a.out x\fP
146 New thread started
147 cnt = 0
148 cnt = 1
149 Thread terminated normally; cnt = 2
150 .fi
151 .in
152
153 上記では、 (\fIcleanup_pop_arg\fP が 0 なので) クリーンアップハンドラは
154 実行されておらず、その結果 \fIcnt\fP の値はリセットされていないことが
155 分かる。
156
157 次の実行例では、メインプログラムはグローバル変数を設定して、
158 もう一つのスレッドが正常終了するようにし、さらに
159 \fIcleanup_pop_arg\fP に 0 以外の値を渡している。
160
161 .in +4n
162 .nf
163 $ \fB./a.out x 1\fP
164 New thread started
165 cnt = 0
166 cnt = 1
167 Called clean\-up handler
168 Thread terminated normally; cnt = 0
169 .fi
170 .in
171
172 上記では、スレッドはキャンセルされていないが、クリーンアップハンドラが
173 実行されていないことが分かる。これは \fBpthread_cleanup_pop\fP() の引き数
174 に 0 以外を渡したからである。
175 .SS プログラムのソース
176 \&
177 .nf
178 #include <pthread.h>
179 #include <sys/types.h>
180 #include <stdio.h>
181 #include <stdlib.h>
182 #include <unistd.h>
183 #include <errno.h>
184
185 #define handle_error_en(en, msg) \e
186         do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
187
188 static int done = 0;
189 static int cleanup_pop_arg = 0;
190 static int cnt = 0;
191
192 static void
193 cleanup_handler(void *arg)
194 {
195     printf("Called clean\-up handler\en");
196     cnt = 0;
197 }
198
199 static void *
200 thread_start(void *arg)
201 {
202     time_t start, curr;
203
204     printf("New thread started\en");
205
206     pthread_cleanup_push(cleanup_handler, NULL);
207
208     curr = start = time(NULL);
209
210     while (!done) {
211         pthread_testcancel();           /* A cancellation point */
212         if (curr < time(NULL)) {
213             curr = time(NULL);
214             printf("cnt = %d\en", cnt);  /* A cancellation point */
215             cnt++;
216         }
217     }
218
219     pthread_cleanup_pop(cleanup_pop_arg);
220     return NULL;
221 }
222
223 int
224 main(int argc, char *argv[])
225 {
226     pthread_t thr;
227     int s;
228     void *res;
229
230     s = pthread_create(&thr, NULL, thread_start, NULL);
231     if (s != 0)
232         handle_error_en(s, "pthread_create");
233
234     sleep(2);           /* Allow new thread to run a while */
235
236     if (argc > 1) {
237         if (argc > 2)
238             cleanup_pop_arg = atoi(argv[2]);
239         done = 1;
240
241     } else {
242         printf("Canceling thread\en");
243         s = pthread_cancel(thr);
244         if (s != 0)
245             handle_error_en(s, "pthread_cancel");
246     }
247
248     s = pthread_join(thr, &res);
249     if (s != 0)
250         handle_error_en(s, "pthread_join");
251
252     if (res == PTHREAD_CANCELED)
253         printf("Thread was canceled; cnt = %d\en", cnt);
254     else
255         printf("Thread terminated normally; cnt = %d\en", cnt);
256     exit(EXIT_SUCCESS);
257 }
258 .fi
259 .SH 関連項目
260 \fBpthread_cancel\fP(3), \fBpthread_cleanup_push_defer_np\fP(3),
261 \fBpthread_setcancelstate\fP(3), \fBpthread_testcancel\fP(3), \fBpthreads\fP(7)
262 .SH この文書について
263 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.41 の一部
264 である。プロジェクトの説明とバグ報告に関する情報は
265 http://www.kernel.org/doc/man\-pages/ に書かれている。