OSDN Git Service

10d77374b17fac94df1751767de1a06dbbed346f
[linuxjm/jm.git] / manual / LDP_man-pages / draft / 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 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" 2006-08-02, mtk, Added example program
27 .\"
28 .\"*******************************************************************
29 .\"
30 .\" This file was generated with po4a. Translate the source file.
31 .\"
32 .\"*******************************************************************
33 .\"
34 .\" Japanese Version Copyright (c) 2001 NAKANO Takeo all rights reserved.
35 .\" Translated Sat Dec 15 2001 by NAKANO Takeo <nakano@apm.seikei.ac.jp>
36 .\" Updated 2008-12-24, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v3.15
37 .\"
38 .TH MAKECONTEXT 3 2020\-12\-21 GNU "Linux Programmer's Manual"
39 .SH 名前
40 makecontext, swapcontext \- ユーザーコンテキストを操作する
41 .SH 書式
42 \fB#include <ucontext.h>\fP
43 .PP
44 \fBvoid makecontext(ucontext_t *\fP\fIucp\fP\fB, void (*\fP\fIfunc\fP\fB)(),\fP \fBint
45 \fP\fIargc\fP\fB, ...);\fP
46 .PP
47 \fBint swapcontext(ucontext_t *\fP\fIoucp\fP\fB, const ucontext_t *\fP\fIucp\fP\fB);\fP
48 .SH 説明
49 In a System V\-like environment, one has the type \fIucontext_t\fP (defined in
50 \fI<ucontext.h>\fP and described in \fBgetcontext\fP(3))  and the four
51 functions \fBgetcontext\fP(3), \fBsetcontext\fP(3), \fBmakecontext\fP(), and
52 \fBswapcontext\fP()  that allow user\-level context switching between multiple
53 threads of control within a process.
54 .PP
55 \fBmakecontext\fP()  関数は、ポインター \fIucp\fP が指すコンテキストを変更する (\fIucp\fP は以前の
56 \fBgetcontext\fP(3)  呼び出しで得られたものである)。 \fBmakecontext\fP()
57 を起動する前には、呼び出し者は、このコンテキスト用に 新しいスタックを確保し、そのアドレスを \fIucp\->uc_stack\fP に代入し、
58 さらに後継のコンテキストを定義し、そのアドレスを \fIucp\->uc_link\fP に 代入しなければならない。
59 .PP
60 このコンテキストが将来 (\fBsetcontext\fP(3)  または \fBswapcontext\fP()  によって)  有効にされると、関数
61 \fIfunc\fP が呼ばれ、 引き数として \fIargc\fP 以降の整数 (\fIint\fP)  引き数の列が渡される。 呼び出し者は \fIargc\fP
62 にこれらの引き数の個数を指定しなければならない。 この関数が戻ると、後継のコンテキストが有効になる。 後継コンテキストのポインターが NULL
63 の場合、そのスレッドが終了する。
64 .PP
65 \fBswapcontext\fP()  関数は現在のコンテキストを ポインター \fIoucp\fP が指す構造体に保存し、 ポインター \fIucp\fP
66 が指すコンテキストを有効にする。
67 .SH 返り値
68 成功すると、 \fBswapcontext\fP()  は返らない (しかし後に \fIoucp\fP が有効になった場合には返ることがある。 このときには
69 \fBswapcontext\fP()  は 0 を返すように見える。)  失敗すると、 \fBswapcontext\fP()  は \-1 を返し、
70 \fIerrno\fP をエラーに応じて設定する。
71 .SH エラー
72 .TP 
73 \fBENOMEM\fP
74 スタックに割り当てる空間が残っていない。
75 .SH バージョン
76 \fBmakecontext\fP()  と \fBswapcontext\fP()  は、バージョン 2.1 以降の glibc で提供されている。
77 .SH 属性
78 この節で使用されている用語の説明については、 \fBattributes\fP(7) を参照。
79 .TS
80 allbox;
81 lb lb lb
82 l l l.
83 インターフェース        属性  値
84 T{
85 \fBmakecontext\fP()
86 T}      Thread safety   MT\-Safe race:ucp
87 T{
88 \fBswapcontext\fP()
89 T}      Thread safety   MT\-Safe race:oucp race:ucp
90 .TE
91 .SH 準拠
92 SUSv2, POSIX.1\-2001.  POSIX.1\-2008 では、移植性の問題から \fBmakecontext\fP()  と
93 \fBswapcontext\fP()  の仕様が削除されている。 代わりに、アプリケーションを POSIX スレッドを使って書き直すことが 推奨されている。
94 .SH 注意
95 \fIucp\->uc_stack\fP の解釈は \fBsigaltstack\fP(2)  の場合と同じである。 すなわちこの構造体には、
96 スタックとして用いられるメモリー領域の開始アドレスと長さが含まれ、 これはスタックが伸びる方向がどちらであるかには関係しない。
97 したがって、ユーザープログラムはこの件については心配しなくてよい。
98 .PP
99 \fIint\fP とポインター型が同じ大きさであるアーキテクチャーでは (x86\-32 はその例であり、両方の型とも 32 ビットである)、
100 \fBmakecontext\fP()  の \fIargc\fP 以降の引き数としてポインターを渡してもうまく動くかもしれない。
101 しかしながら、このようにすると、移植性は保証されず、 標準に従えば動作は未定義であり、ポインターが \fIint\fP
102 よりも大きいアーキテクチャーでは正しく動作しないことだろう。 それにも関わらず、バージョン 2.8 以降の glibc では、
103 \fBmakecontext\fP()  に変更が行われ、(x86\-64 などの) いくつかの 64 ビットアーキテクチャーで
104 引き数としてポインターを渡すことができるようになっている。
105 .SH EXAMPLES
106 以下のサンプルプログラムは、 \fBgetcontext\fP(3), \fBmakecontext\fP(), \fBswapcontext\fP()
107 の使用方法の例を示すものである。 このプログラムを実行すると、以下のような出力が得られる:
108 .PP
109 .in +4n
110 .EX
111 $\fB ./a.out\fP
112 main: swapcontext(&uctx_main, &uctx_func2)
113 func2: started
114 func2: swapcontext(&uctx_func2, &uctx_func1)
115 func1: started
116 func1: swapcontext(&uctx_func1, &uctx_func2)
117 func2: returning
118 func1: returning
119 main: exiting
120 .EE
121 .in
122 .SS プログラムのソース
123 \&
124 .EX
125 #include <ucontext.h>
126 #include <stdio.h>
127 #include <stdlib.h>
128
129 static ucontext_t uctx_main, uctx_func1, uctx_func2;
130
131 #define handle_error(msg) \e
132     do { perror(msg); exit(EXIT_FAILURE); } while (0)
133
134 static void
135 func1(void)
136 {
137     printf("func1: started\en");
138     printf("func1: swapcontext(&uctx_func1, &uctx_func2)\en");
139     if (swapcontext(&uctx_func1, &uctx_func2) == \-1)
140         handle_error("swapcontext");
141     printf("func1: returning\en");
142 }
143
144 static void
145 func2(void)
146 {
147     printf("func2: started\en");
148     printf("func2: swapcontext(&uctx_func2, &uctx_func1)\en");
149     if (swapcontext(&uctx_func2, &uctx_func1) == \-1)
150         handle_error("swapcontext");
151     printf("func2: returning\en");
152 }
153
154 int
155 main(int argc, char *argv[])
156 {
157     char func1_stack[16384];
158     char func2_stack[16384];
159
160     if (getcontext(&uctx_func1) == \-1)
161         handle_error("getcontext");
162     uctx_func1.uc_stack.ss_sp = func1_stack;
163     uctx_func1.uc_stack.ss_size = sizeof(func1_stack);
164     uctx_func1.uc_link = &uctx_main;
165     makecontext(&uctx_func1, func1, 0);
166
167     if (getcontext(&uctx_func2) == \-1)
168         handle_error("getcontext");
169     uctx_func2.uc_stack.ss_sp = func2_stack;
170     uctx_func2.uc_stack.ss_size = sizeof(func2_stack);
171     /* Successor context is f1(), unless argc > 1 */
172     uctx_func2.uc_link = (argc > 1) ? NULL : &uctx_func1;
173     makecontext(&uctx_func2, func2, 0);
174
175     printf("main: swapcontext(&uctx_main, &uctx_func2)\en");
176     if (swapcontext(&uctx_main, &uctx_func2) == \-1)
177         handle_error("swapcontext");
178
179     printf("main: exiting\en");
180     exit(EXIT_SUCCESS);
181 }
182 .EE
183 .SH 関連項目
184 \fBsigaction\fP(2), \fBsigaltstack\fP(2), \fBsigprocmask\fP(2), \fBgetcontext\fP(3),
185 \fBsigsetjmp\fP(3)
186 .SH この文書について
187 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は
188 \%https://www.kernel.org/doc/man\-pages/ に書かれている。