1 .\" Copyright (C) 2001 Andries Brouwer (aeb@cwi.nl)
2 .\" and Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" 2006-08-02, mtk, Added example program
25 .\" Japanese Version Copyright (c) 2001 NAKANO Takeo all rights reserved.
26 .\" Translated Sat Dec 15 2001 by NAKANO Takeo <nakano@apm.seikei.ac.jp>
27 .\" Updated 2008-12-24, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v3.15
29 .TH MAKECONTEXT 3 2009-03-31 "GNU" "Linux Programmer's Manual"
31 makecontext, swapcontext \- ユーザコンテキストを操作する
33 .B #include <ucontext.h>
35 .BI "void makecontext(ucontext_t *" ucp ", void (*" func )(),
36 .BI "int " argc ", ...);"
38 .BI "int swapcontext(ucontext_t *" oucp ", ucontext_t *" ucp );
41 \fBmcontext_t\fP および \fBucontext_t\fP という 2 つの型と、
48 で定義されており、あるプロセス内部で制御下にある複数のスレッド間で、
49 ユーザレベルのコンテキスト切替えができるようになっている。
51 これらの型と、最初の 2 つの関数については、
56 関数は、ポインタ \fIucp\fP が指すコンテキストを変更する
61 を起動する前には、呼び出し者は、このコンテキスト用に
62 新しいスタックを確保し、そのアドレスを \fIucp\->uc_stack\fP に代入し、
63 さらに後継のコンテキストを定義し、そのアドレスを \fIucp\->uc_link\fP に
71 有効にされると、関数 \fIfunc\fP が呼ばれ、
79 にこれらの引き数の個数を指定しなければならない。
80 この関数が戻ると、後継のコンテキストが有効になる。
81 後継コンテキストのポインタが NULL の場合、そのスレッドが終了する。
85 ポインタ \fIoucp\fP が指す構造体に保存し、
86 ポインタ \fIucp\fP が指すコンテキストを有効にする。
91 (しかし後に \fIoucp\fP が有効になった場合には返ることがある。
98 \fIerrno\fP をエラーに応じて設定する。
107 は、バージョン 2.1 以降の glibc で提供されている。
110 POSIX.1-2008 では、移植性の問題から
115 代わりに、アプリケーションを POSIX スレッドを使って書き直すことが
118 \fIucp\->uc_stack\fP の解釈は
122 スタックとして用いられるメモリ領域の開始アドレスと長さが含まれ、
123 これはスタックが伸びる方向がどちらであるかには関係しない。
124 したがって、ユーザプログラムはこの件については心配しなくてよい。
127 とポインタ型が同じ大きさであるアーキテクチャでは
128 (x86-32 はその例であり、両方の型とも 32 ビットである)、
132 以降の引き数としてポインタを渡してもうまく動くかもしれない。
133 しかしながら、このようにすると、移植性は保証されず、
134 標準に従えば動作は未定義であり、ポインタが
136 よりも大きいアーキテクチャでは正しく動作しないことだろう。
137 それにも関わらず、バージョン 2.8 以降の glibc では、
139 に変更が行われ、(x86-64 などの) いくつかの 64 ビットアーキテクチャで
140 引き数としてポインタを渡すことができるようになっている。
148 このプログラムを実行すると、以下のような出力が得られる:
153 main: swapcontext(&uctx_main, &uctx_func2)
155 func2: swapcontext(&uctx_func2, &uctx_func1)
157 func1: swapcontext(&uctx_func1, &uctx_func2)
166 #include <ucontext.h>
170 static ucontext_t uctx_main, uctx_func1, uctx_func2;
172 #define handle_error(msg) \\
173 do { perror(msg); exit(EXIT_FAILURE); } while (0)
178 printf("func1: started\\n");
179 printf("func1: swapcontext(&uctx_func1, &uctx_func2)\\n");
180 if (swapcontext(&uctx_func1, &uctx_func2) == \-1)
181 handle_error("swapcontext");
182 printf("func1: returning\\n");
188 printf("func2: started\\n");
189 printf("func2: swapcontext(&uctx_func2, &uctx_func1)\\n");
190 if (swapcontext(&uctx_func2, &uctx_func1) == \-1)
191 handle_error("swapcontext");
192 printf("func2: returning\\n");
196 main(int argc, char *argv[])
198 char func1_stack[16384];
199 char func2_stack[16384];
201 if (getcontext(&uctx_func1) == \-1)
202 handle_error("getcontext");
203 uctx_func1.uc_stack.ss_sp = func1_stack;
204 uctx_func1.uc_stack.ss_size = sizeof(func1_stack);
205 uctx_func1.uc_link = &uctx_main;
206 makecontext(&uctx_func1, func1, 0);
208 if (getcontext(&uctx_func2) == \-1)
209 handle_error("getcontext");
210 uctx_func2.uc_stack.ss_sp = func2_stack;
211 uctx_func2.uc_stack.ss_size = sizeof(func2_stack);
212 /* Successor context is f1(), unless argc > 1 */
213 uctx_func2.uc_link = (argc > 1) ? NULL : &uctx_func1;
214 makecontext(&uctx_func2, func2, 0);
216 printf("main: swapcontext(&uctx_main, &uctx_func2)\\n");
217 if (swapcontext(&uctx_main, &uctx_func2) == \-1)
218 handle_error("swapcontext");
220 printf("main: exiting\\n");