OSDN Git Service

(split) LDP: Translate capabilities.7.
[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 .\" 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 .\" 2006-08-02, mtk, Added example program
25 .\"
26 .\"*******************************************************************
27 .\"
28 .\" This file was generated with po4a. Translate the source file.
29 .\"
30 .\"*******************************************************************
31 .TH MAKECONTEXT 3 2009\-03\-31 GNU "Linux Programmer's Manual"
32 .SH 名前
33 makecontext, swapcontext \- ユーザコンテキストを操作する
34 .SH 書式
35 \fB#include <ucontext.h>\fP
36 .sp
37 \fBvoid makecontext(ucontext_t *\fP\fIucp\fP\fB, void (*\fP\fIfunc\fP\fB)(),\fP \fBint
38 \fP\fIargc\fP\fB, ...);\fP
39 .sp
40 \fBint swapcontext(ucontext_t *\fP\fIoucp\fP\fB, ucontext_t *\fP\fIucp\fP\fB);\fP
41 .SH 説明
42 System V 的な環境では、 \fBmcontext_t\fP および \fBucontext_t\fP という 2 つの型と、
43 \fBgetcontext\fP(2), \fBsetcontext\fP(2), \fBmakecontext\fP(), \fBswapcontext\fP()  という
44 4 つの関数が \fI<ucontext.h>\fP で定義されており、あるプロセス内部で制御下にある複数のスレッド間で、
45 ユーザレベルのコンテキスト切替えができるようになっている。
46 .LP
47 これらの型と、最初の 2 つの関数については、 \fBgetcontext\fP(2)  を参照のこと。
48 .LP
49 \fBmakecontext\fP()  関数は、ポインタ \fIucp\fP が指すコンテキストを変更する (\fIucp\fP は以前の
50 \fBgetcontext\fP(2)  呼び出しで得られたものである)。 \fBmakecontext\fP()
51 を起動する前には、呼び出し者は、このコンテキスト用に 新しいスタックを確保し、そのアドレスを \fIucp\->uc_stack\fP に代入し、
52 さらに後継のコンテキストを定義し、そのアドレスを \fIucp\->uc_link\fP に 代入しなければならない。
53
54 このコンテキストが将来 (\fBsetcontext\fP(2)  または \fBswapcontext\fP()  によって)  有効にされると、関数
55 \fIfunc\fP が呼ばれ、 引き数として \fIargc\fP 以降の整数 (\fIint\fP)  引き数の列が渡される。 呼び出し者は \fIargc\fP
56 にこれらの引き数の個数を指定しなければならない。 この関数が戻ると、後継のコンテキストが有効になる。 後継コンテキストのポインタが NULL
57 の場合、そのスレッドが終了する。
58 .LP
59 \fBswapcontext\fP()  関数は現在のコンテキストを ポインタ \fIoucp\fP が指す構造体に保存し、 ポインタ \fIucp\fP
60 が指すコンテキストを有効にする。
61 .SH 返り値
62 成功すると、 \fBswapcontext\fP()  は返らない (しかし後に \fIoucp\fP が有効になった場合には返ることがある。 このときには
63 \fBswapcontext\fP()  は 0 を返すように見える。)  失敗すると、 \fBswapcontext\fP()  は \-1 を返し、
64 \fIerrno\fP をエラーに応じて設定する。
65 .SH エラー
66 .TP 
67 \fBENOMEM\fP
68 スタックに割り当てる空間が残っていない。
69 .SH バージョン
70 \fBmakecontext\fP()  と \fBswapcontext\fP()  は、バージョン 2.1 以降の glibc で提供されている。
71 .SH 準拠
72 SUSv2, POSIX.1\-2001.  POSIX.1\-2008 では、移植性の問題から \fBmakecontext\fP()  と
73 \fBswapcontext\fP()  の仕様が削除されている。 代わりに、アプリケーションを POSIX スレッドを使って書き直すことが 推奨されている。
74 .SH 注意
75 \fIucp\->uc_stack\fP の解釈は \fBsigaltstack\fP(2)  の場合と同じである。 すなわちこの構造体には、
76 スタックとして用いられるメモリ領域の開始アドレスと長さが含まれ、 これはスタックが伸びる方向がどちらであるかには関係しない。
77 したがって、ユーザプログラムはこの件については心配しなくてよい。
78
79 \fIint\fP とポインタ型が同じ大きさであるアーキテクチャでは (x86\-32 はその例であり、両方の型とも 32 ビットである)、
80 \fBmakecontext\fP()  の \fIargc\fP 以降の引き数としてポインタを渡してもうまく動くかもしれない。
81 しかしながら、このようにすると、移植性は保証されず、 標準に従えば動作は未定義であり、ポインタが \fIint\fP
82 よりも大きいアーキテクチャでは正しく動作しないことだろう。 それにも関わらず、バージョン 2.8 以降の glibc では、
83 \fBmakecontext\fP()  に変更が行われ、(x86\-64 などの) いくつかの 64 ビットアーキテクチャで
84 引き数としてポインタを渡すことができるようになっている。
85 .SH 例
86 .PP
87 以下のサンプル・プログラムは、 \fBgetcontext\fP(2), \fBmakecontext\fP(), \fBswapcontext\fP()
88 の使用方法の例を示すものである。 このプログラムを実行すると、以下のような出力が得られる:
89 .in +4n
90 .nf
91
92 $\fB ./a.out\fP
93 main: swapcontext(&uctx_main, &uctx_func2)
94 func2: started
95 func2: swapcontext(&uctx_func2, &uctx_func1)
96 func1: started
97 func1: swapcontext(&uctx_func1, &uctx_func2)
98 func2: returning
99 func1: returning
100 main: exiting
101 .fi
102 .in
103 .SS プログラムのソース
104 \&
105 .nf
106 #include <ucontext.h>
107 #include <stdio.h>
108 #include <stdlib.h>
109
110 static ucontext_t uctx_main, uctx_func1, uctx_func2;
111
112 #define handle_error(msg) \e
113     do { perror(msg); exit(EXIT_FAILURE); } while (0)
114
115 static void
116 func1(void)
117 {
118     printf("func1: started\en");
119     printf("func1: swapcontext(&uctx_func1, &uctx_func2)\en");
120     if (swapcontext(&uctx_func1, &uctx_func2) == \-1)
121         handle_error("swapcontext");
122     printf("func1: returning\en");
123 }
124
125 static void
126 func2(void)
127 {
128     printf("func2: started\en");
129     printf("func2: swapcontext(&uctx_func2, &uctx_func1)\en");
130     if (swapcontext(&uctx_func2, &uctx_func1) == \-1)
131         handle_error("swapcontext");
132     printf("func2: returning\en");
133 }
134
135 int
136 main(int argc, char *argv[])
137 {
138     char func1_stack[16384];
139     char func2_stack[16384];
140
141     if (getcontext(&uctx_func1) == \-1)
142         handle_error("getcontext");
143     uctx_func1.uc_stack.ss_sp = func1_stack;
144     uctx_func1.uc_stack.ss_size = sizeof(func1_stack);
145     uctx_func1.uc_link = &uctx_main;
146     makecontext(&uctx_func1, func1, 0);
147
148     if (getcontext(&uctx_func2) == \-1)
149         handle_error("getcontext");
150     uctx_func2.uc_stack.ss_sp = func2_stack;
151     uctx_func2.uc_stack.ss_size = sizeof(func2_stack);
152     /* Successor context is f1(), unless argc > 1 */
153     uctx_func2.uc_link = (argc > 1) ? NULL : &uctx_func1;
154     makecontext(&uctx_func2, func2, 0);
155
156     printf("main: swapcontext(&uctx_main, &uctx_func2)\en");
157     if (swapcontext(&uctx_main, &uctx_func2) == \-1)
158         handle_error("swapcontext");
159
160     printf("main: exiting\en");
161     exit(EXIT_SUCCESS);
162 }
163 .fi
164 .SH 関連項目
165 \fBgetcontext\fP(2), \fBsigaction\fP(2), \fBsigaltstack\fP(2), \fBsigprocmask\fP(2),
166 \fBsigsetjmp\fP(3)
167 .SH この文書について
168 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.41 の一部
169 である。プロジェクトの説明とバグ報告に関する情報は
170 http://www.kernel.org/doc/man\-pages/ に書かれている。