OSDN Git Service

(split) Convert release and draft pages to UTF-8.
[linuxjm/LDP_man-pages.git] / release / man3 / makecontext.3
1 .\" Copyright (C) 2001 Andries Brouwer (aeb@cwi.nl)
2 .\" and Copyright (C) 2006 Michael Kerrisk <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 .\" 2006-08-02, mtk, Added example program
24 .\"
25 .\" Japanese Version Copyright (c) 2001 NAKANO Takeo all rights reserved.
26 .\" Translated Sat Dec 15 2001 by NAKANO Takeo <nakano@apm.seikei.ac.jp>
27 .\" Updated 2008-12-24, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v3.15
28 .\"
29 .TH MAKECONTEXT 3 2009-03-31 "GNU" "Linux Programmer's Manual"
30 .SH 名前
31 makecontext, swapcontext \- ユーザコンテキストを操作する
32 .SH 書式
33 .B #include <ucontext.h>
34 .sp
35 .BI "void makecontext(ucontext_t *" ucp ", void (*" func )(),
36 .BI "int " argc ", ...);"
37 .sp
38 .BI "int swapcontext(ucontext_t *" oucp ", ucontext_t *" ucp );
39 .SH 説明
40 System V 的な環境では、
41 \fBmcontext_t\fP および \fBucontext_t\fP という 2 つの型と、
42 .BR getcontext (2),
43 .BR setcontext (2),
44 .BR makecontext (),
45 .BR swapcontext ()
46 という 4 つの関数が
47 .I <ucontext.h>
48 で定義されており、あるプロセス内部で制御下にある複数のスレッド間で、
49 ユーザレベルのコンテキスト切替えができるようになっている。
50 .LP
51 これらの型と、最初の 2 つの関数については、
52 .BR getcontext (2)
53 を参照のこと。
54 .LP
55 .BR makecontext ()
56 関数は、ポインタ \fIucp\fP が指すコンテキストを変更する
57 (\fIucp\fP は以前の
58 .BR getcontext (2)
59 呼び出しで得られたものである)。
60 .BR makecontext ()
61 を起動する前には、呼び出し者は、このコンテキスト用に
62 新しいスタックを確保し、そのアドレスを \fIucp\->uc_stack\fP に代入し、
63 さらに後継のコンテキストを定義し、そのアドレスを \fIucp\->uc_link\fP に
64 代入しなければならない。
65
66 このコンテキストが将来
67 .RB ( setcontext (2)
68 または
69 .BR swapcontext ()
70 によって)
71 有効にされると、関数 \fIfunc\fP が呼ばれ、
72 引き数として
73 .I argc
74 以降の整数
75 .RI ( int )
76 引き数の列が渡される。
77 呼び出し者は
78 .I argc
79 にこれらの引き数の個数を指定しなければならない。
80 この関数が戻ると、後継のコンテキストが有効になる。
81 後継コンテキストのポインタが NULL の場合、そのスレッドが終了する。
82 .LP
83 .BR swapcontext ()
84 関数は現在のコンテキストを
85 ポインタ \fIoucp\fP が指す構造体に保存し、
86 ポインタ \fIucp\fP が指すコンテキストを有効にする。
87 .SH 返り値
88 成功すると、
89 .BR swapcontext ()
90 は返らない
91 (しかし後に \fIoucp\fP が有効になった場合には返ることがある。
92 このときには
93 .BR swapcontext ()
94 は 0 を返すように見える。)
95 失敗すると、
96 .BR swapcontext ()
97 は \-1 を返し、
98 \fIerrno\fP をエラーに応じて設定する。
99 .SH エラー
100 .TP
101 .B ENOMEM
102 スタックに割り当てる空間が残っていない。
103 .SH バージョン
104 .BR makecontext ()
105
106 .BR swapcontext ()
107 は、バージョン 2.1 以降の glibc で提供されている。
108 .SH 準拠
109 SUSv2, POSIX.1-2001.
110 POSIX.1-2008 では、移植性の問題から
111 .BR makecontext ()
112
113 .BR swapcontext ()
114 の仕様が削除されている。
115 代わりに、アプリケーションを POSIX スレッドを使って書き直すことが
116 推奨されている。
117 .SH 注意
118 \fIucp\->uc_stack\fP の解釈は
119 .BR sigaltstack (2)
120 の場合と同じである。
121 すなわちこの構造体には、
122 スタックとして用いられるメモリ領域の開始アドレスと長さが含まれ、
123 これはスタックが伸びる方向がどちらであるかには関係しない。
124 したがって、ユーザプログラムはこの件については心配しなくてよい。
125
126 .I int
127 とポインタ型が同じ大きさであるアーキテクチャでは
128 (x86-32 はその例であり、両方の型とも 32 ビットである)、
129 .BR makecontext ()
130
131 .I argc
132 以降の引き数としてポインタを渡してもうまく動くかもしれない。
133 しかしながら、このようにすると、移植性は保証されず、
134 標準に従えば動作は未定義であり、ポインタが
135 .I int
136 よりも大きいアーキテクチャでは正しく動作しないことだろう。
137 それにも関わらず、バージョン 2.8 以降の glibc では、
138 .BR makecontext ()
139 に変更が行われ、(x86-64 などの) いくつかの 64 ビットアーキテクチャで
140 引き数としてポインタを渡すことができるようになっている。
141 .SH 例
142 .PP
143 以下のサンプル・プログラムは、
144 .BR getcontext (2),
145 .BR makecontext (),
146 .BR swapcontext ()
147 の使用方法の例を示すものである。
148 このプログラムを実行すると、以下のような出力が得られる:
149 .in +4n
150 .nf
151
152 .RB "$" " ./a.out"
153 main: swapcontext(&uctx_main, &uctx_func2)
154 func2: started
155 func2: swapcontext(&uctx_func2, &uctx_func1)
156 func1: started
157 func1: swapcontext(&uctx_func1, &uctx_func2)
158 func2: returning
159 func1: returning
160 main: exiting
161 .fi
162 .in
163 .SS プログラムのソース
164 \&
165 .nf
166 #include <ucontext.h>
167 #include <stdio.h>
168 #include <stdlib.h>
169
170 static ucontext_t uctx_main, uctx_func1, uctx_func2;
171
172 #define handle_error(msg) \\
173     do { perror(msg); exit(EXIT_FAILURE); } while (0)
174
175 static void
176 func1(void)
177 {
178     printf("func1: started\\n");
179     printf("func1: swapcontext(&uctx_func1, &uctx_func2)\\n");
180     if (swapcontext(&uctx_func1, &uctx_func2) == \-1)
181         handle_error("swapcontext");
182     printf("func1: returning\\n");
183 }
184
185 static void
186 func2(void)
187 {
188     printf("func2: started\\n");
189     printf("func2: swapcontext(&uctx_func2, &uctx_func1)\\n");
190     if (swapcontext(&uctx_func2, &uctx_func1) == \-1)
191         handle_error("swapcontext");
192     printf("func2: returning\\n");
193 }
194
195 int
196 main(int argc, char *argv[])
197 {
198     char func1_stack[16384];
199     char func2_stack[16384];
200
201     if (getcontext(&uctx_func1) == \-1)
202         handle_error("getcontext");
203     uctx_func1.uc_stack.ss_sp = func1_stack;
204     uctx_func1.uc_stack.ss_size = sizeof(func1_stack);
205     uctx_func1.uc_link = &uctx_main;
206     makecontext(&uctx_func1, func1, 0);
207
208     if (getcontext(&uctx_func2) == \-1)
209         handle_error("getcontext");
210     uctx_func2.uc_stack.ss_sp = func2_stack;
211     uctx_func2.uc_stack.ss_size = sizeof(func2_stack);
212     /* Successor context is f1(), unless argc > 1 */
213     uctx_func2.uc_link = (argc > 1) ? NULL : &uctx_func1;
214     makecontext(&uctx_func2, func2, 0);
215
216     printf("main: swapcontext(&uctx_main, &uctx_func2)\\n");
217     if (swapcontext(&uctx_main, &uctx_func2) == \-1)
218         handle_error("swapcontext");
219
220     printf("main: exiting\\n");
221     exit(EXIT_SUCCESS);
222 }
223 .fi
224 .SH 関連項目
225 .BR getcontext (2),
226 .BR sigaction (2),
227 .BR sigaltstack (2),
228 .BR sigprocmask (2),
229 .BR sigsetjmp (3)