OSDN Git Service

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