OSDN Git Service

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