2 .\" Copyright (C) 2001 Andries Brouwer (aeb@cwi.nl)
3 .\" and Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
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.
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.
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
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
27 .\" 2006-08-02, mtk, Added example program
29 .\"*******************************************************************
31 .\" This file was generated with po4a. Translate the source file.
33 .\"*******************************************************************
34 .TH MAKECONTEXT 3 2013\-02\-12 GNU "Linux Programmer's Manual"
36 makecontext, swapcontext \- ユーザコンテキストを操作する
38 \fB#include <ucontext.h>\fP
40 \fBvoid makecontext(ucontext_t *\fP\fIucp\fP\fB, void (*\fP\fIfunc\fP\fB)(),\fP \fBint
41 \fP\fIargc\fP\fB, ...);\fP
43 \fBint swapcontext(ucontext_t *\fP\fIoucp\fP\fB, ucontext_t *\fP\fIucp\fP\fB);\fP
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 ユーザレベルのコンテキスト切替えができるようになっている。
50 これらの型と、最初の 2 つの関数については、 \fBgetcontext\fP(3) を参照のこと。
52 \fBmakecontext\fP() 関数は、ポインタ \fIucp\fP が指すコンテキストを変更する (\fIucp\fP は以前の
53 \fBgetcontext\fP(3) 呼び出しで得られたものである)。 \fBmakecontext\fP()
54 を起動する前には、呼び出し者は、このコンテキスト用に 新しいスタックを確保し、そのアドレスを \fIucp\->uc_stack\fP に代入し、
55 さらに後継のコンテキストを定義し、そのアドレスを \fIucp\->uc_link\fP に 代入しなければならない。
57 このコンテキストが将来 (\fBsetcontext\fP(3) または \fBswapcontext\fP() によって) 有効にされると、関数
58 \fIfunc\fP が呼ばれ、 引き数として \fIargc\fP 以降の整数 (\fIint\fP) 引き数の列が渡される。 呼び出し者は \fIargc\fP
59 にこれらの引き数の個数を指定しなければならない。 この関数が戻ると、後継のコンテキストが有効になる。 後継コンテキストのポインタが NULL
62 \fBswapcontext\fP() 関数は現在のコンテキストを ポインタ \fIoucp\fP が指す構造体に保存し、 ポインタ \fIucp\fP
65 成功すると、 \fBswapcontext\fP() は返らない (しかし後に \fIoucp\fP が有効になった場合には返ることがある。 このときには
66 \fBswapcontext\fP() は 0 を返すように見える。) 失敗すると、 \fBswapcontext\fP() は \-1 を返し、
67 \fIerrno\fP をエラーに応じて設定する。
73 \fBmakecontext\fP() と \fBswapcontext\fP() は、バージョン 2.1 以降の glibc で提供されている。
75 SUSv2, POSIX.1\-2001. POSIX.1\-2008 では、移植性の問題から \fBmakecontext\fP() と
76 \fBswapcontext\fP() の仕様が削除されている。 代わりに、アプリケーションを POSIX スレッドを使って書き直すことが 推奨されている。
78 \fIucp\->uc_stack\fP の解釈は \fBsigaltstack\fP(2) の場合と同じである。 すなわちこの構造体には、
79 スタックとして用いられるメモリ領域の開始アドレスと長さが含まれ、 これはスタックが伸びる方向がどちらであるかには関係しない。
80 したがって、ユーザプログラムはこの件については心配しなくてよい。
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 引き数としてポインタを渡すことができるようになっている。
90 以下のサンプル・プログラムは、 \fBgetcontext\fP(3), \fBmakecontext\fP(), \fBswapcontext\fP()
91 の使用方法の例を示すものである。 このプログラムを実行すると、以下のような出力が得られる:
96 main: swapcontext(&uctx_main, &uctx_func2)
98 func2: swapcontext(&uctx_func2, &uctx_func1)
100 func1: swapcontext(&uctx_func1, &uctx_func2)
109 #include <ucontext.h>
113 static ucontext_t uctx_main, uctx_func1, uctx_func2;
115 #define handle_error(msg) \e
116 do { perror(msg); exit(EXIT_FAILURE); } while (0)
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");
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");
139 main(int argc, char *argv[])
141 char func1_stack[16384];
142 char func2_stack[16384];
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);
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);
159 printf("main: swapcontext(&uctx_main, &uctx_func2)\en");
160 if (swapcontext(&uctx_main, &uctx_func2) == \-1)
161 handle_error("swapcontext");
163 printf("main: exiting\en");
168 \fBsigaction\fP(2), \fBsigaltstack\fP(2), \fBsigprocmask\fP(2), \fBgetcontext\fP(3),
171 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.52 の一部
172 である。プロジェクトの説明とバグ報告に関する情報は
173 http://www.kernel.org/doc/man\-pages/ に書かれている。