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 .\"*******************************************************************
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
39 .TH MAKECONTEXT 3 2014\-04\-14 GNU "Linux Programmer's Manual"
41 makecontext, swapcontext \- ユーザコンテキストを操作する
43 \fB#include <ucontext.h>\fP
45 \fBvoid makecontext(ucontext_t *\fP\fIucp\fP\fB, void (*\fP\fIfunc\fP\fB)(),\fP \fBint
46 \fP\fIargc\fP\fB, ...);\fP
48 \fBint swapcontext(ucontext_t *\fP\fIoucp\fP\fB, ucontext_t *\fP\fIucp\fP\fB);\fP
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 ユーザレベルのコンテキスト切替えができるようになっている。
55 これらの型と、最初の 2 つの関数については、 \fBgetcontext\fP(3) を参照のこと。
57 \fBmakecontext\fP() 関数は、ポインタ \fIucp\fP が指すコンテキストを変更する (\fIucp\fP は以前の
58 \fBgetcontext\fP(3) 呼び出しで得られたものである)。 \fBmakecontext\fP()
59 を起動する前には、呼び出し者は、このコンテキスト用に 新しいスタックを確保し、そのアドレスを \fIucp\->uc_stack\fP に代入し、
60 さらに後継のコンテキストを定義し、そのアドレスを \fIucp\->uc_link\fP に 代入しなければならない。
62 このコンテキストが将来 (\fBsetcontext\fP(3) または \fBswapcontext\fP() によって) 有効にされると、関数
63 \fIfunc\fP が呼ばれ、 引き数として \fIargc\fP 以降の整数 (\fIint\fP) 引き数の列が渡される。 呼び出し者は \fIargc\fP
64 にこれらの引き数の個数を指定しなければならない。 この関数が戻ると、後継のコンテキストが有効になる。 後継コンテキストのポインタが NULL
67 \fBswapcontext\fP() 関数は現在のコンテキストを ポインタ \fIoucp\fP が指す構造体に保存し、 ポインタ \fIucp\fP
70 成功すると、 \fBswapcontext\fP() は返らない (しかし後に \fIoucp\fP が有効になった場合には返ることがある。 このときには
71 \fBswapcontext\fP() は 0 を返すように見える。) 失敗すると、 \fBswapcontext\fP() は \-1 を返し、
72 \fIerrno\fP をエラーに応じて設定する。
78 \fBmakecontext\fP() と \fBswapcontext\fP() は、バージョン 2.1 以降の glibc で提供されている。
80 .SS "マルチスレッディング (pthreads(7) 参照)"
81 関数 \fBmakecontext\fP() と \fBswapcontext\fP() はスレッドセーフである。
83 SUSv2, POSIX.1\-2001. POSIX.1\-2008 では、移植性の問題から \fBmakecontext\fP() と
84 \fBswapcontext\fP() の仕様が削除されている。 代わりに、アプリケーションを POSIX スレッドを使って書き直すことが 推奨されている。
86 \fIucp\->uc_stack\fP の解釈は \fBsigaltstack\fP(2) の場合と同じである。 すなわちこの構造体には、
87 スタックとして用いられるメモリ領域の開始アドレスと長さが含まれ、 これはスタックが伸びる方向がどちらであるかには関係しない。
88 したがって、ユーザプログラムはこの件については心配しなくてよい。
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 引き数としてポインタを渡すことができるようになっている。
98 以下のサンプル・プログラムは、 \fBgetcontext\fP(3), \fBmakecontext\fP(), \fBswapcontext\fP()
99 の使用方法の例を示すものである。 このプログラムを実行すると、以下のような出力が得られる:
104 main: swapcontext(&uctx_main, &uctx_func2)
106 func2: swapcontext(&uctx_func2, &uctx_func1)
108 func1: swapcontext(&uctx_func1, &uctx_func2)
117 #include <ucontext.h>
121 static ucontext_t uctx_main, uctx_func1, uctx_func2;
123 #define handle_error(msg) \e
124 do { perror(msg); exit(EXIT_FAILURE); } while (0)
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");
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");
147 main(int argc, char *argv[])
149 char func1_stack[16384];
150 char func2_stack[16384];
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);
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);
167 printf("main: swapcontext(&uctx_main, &uctx_func2)\en");
168 if (swapcontext(&uctx_main, &uctx_func2) == \-1)
169 handle_error("swapcontext");
171 printf("main: exiting\en");
176 \fBsigaction\fP(2), \fBsigaltstack\fP(2), \fBsigprocmask\fP(2), \fBgetcontext\fP(3),
179 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.67 の一部
180 である。プロジェクトの説明とバグ報告に関する情報は
181 http://www.kernel.org/doc/man\-pages/ に書かれている。