1 .\" Copyright (C) 2001 Andries Brouwer (aeb@cwi.nl)
2 .\" and Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
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.
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.
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
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" 2006-08-02, mtk, Added example program
28 .\"*******************************************************************
30 .\" This file was generated with po4a. Translate the source file.
32 .\"*******************************************************************
34 .\" Japanese Version Copyright (c) 2001 NAKANO Takeo all rights reserved.
35 .\" Translated Sat Dec 15 2001 by NAKANO Takeo <nakano@apm.seikei.ac.jp>
36 .\" Updated 2008-12-24, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v3.15
38 .TH MAKECONTEXT 3 2020\-12\-21 GNU "Linux Programmer's Manual"
40 makecontext, swapcontext \- ユーザーコンテキストを操作する
42 \fB#include <ucontext.h>\fP
44 \fBvoid makecontext(ucontext_t *\fP\fIucp\fP\fB, void (*\fP\fIfunc\fP\fB)(),\fP \fBint
45 \fP\fIargc\fP\fB, ...);\fP
47 \fBint swapcontext(ucontext_t *\fP\fIoucp\fP\fB, const ucontext_t *\fP\fIucp\fP\fB);\fP
49 In a System V\-like environment, one has the type \fIucontext_t\fP (defined in
50 \fI<ucontext.h>\fP and described in \fBgetcontext\fP(3)) and the four
51 functions \fBgetcontext\fP(3), \fBsetcontext\fP(3), \fBmakecontext\fP(), and
52 \fBswapcontext\fP() that allow user\-level context switching between multiple
53 threads of control within a process.
55 \fBmakecontext\fP() 関数は、ポインター \fIucp\fP が指すコンテキストを変更する (\fIucp\fP は以前の
56 \fBgetcontext\fP(3) 呼び出しで得られたものである)。 \fBmakecontext\fP()
57 を起動する前には、呼び出し者は、このコンテキスト用に 新しいスタックを確保し、そのアドレスを \fIucp\->uc_stack\fP に代入し、
58 さらに後継のコンテキストを定義し、そのアドレスを \fIucp\->uc_link\fP に 代入しなければならない。
60 このコンテキストが将来 (\fBsetcontext\fP(3) または \fBswapcontext\fP() によって) 有効にされると、関数
61 \fIfunc\fP が呼ばれ、 引き数として \fIargc\fP 以降の整数 (\fIint\fP) 引き数の列が渡される。 呼び出し者は \fIargc\fP
62 にこれらの引き数の個数を指定しなければならない。 この関数が戻ると、後継のコンテキストが有効になる。 後継コンテキストのポインターが NULL
65 \fBswapcontext\fP() 関数は現在のコンテキストを ポインター \fIoucp\fP が指す構造体に保存し、 ポインター \fIucp\fP
68 成功すると、 \fBswapcontext\fP() は返らない (しかし後に \fIoucp\fP が有効になった場合には返ることがある。 このときには
69 \fBswapcontext\fP() は 0 を返すように見える。) 失敗すると、 \fBswapcontext\fP() は \-1 を返し、
70 \fIerrno\fP をエラーに応じて設定する。
76 \fBmakecontext\fP() と \fBswapcontext\fP() は、バージョン 2.1 以降の glibc で提供されている。
78 この節で使用されている用語の説明については、 \fBattributes\fP(7) を参照。
86 T} Thread safety MT\-Safe race:ucp
89 T} Thread safety MT\-Safe race:oucp race:ucp
92 SUSv2, POSIX.1\-2001. POSIX.1\-2008 では、移植性の問題から \fBmakecontext\fP() と
93 \fBswapcontext\fP() の仕様が削除されている。 代わりに、アプリケーションを POSIX スレッドを使って書き直すことが 推奨されている。
95 \fIucp\->uc_stack\fP の解釈は \fBsigaltstack\fP(2) の場合と同じである。 すなわちこの構造体には、
96 スタックとして用いられるメモリー領域の開始アドレスと長さが含まれ、 これはスタックが伸びる方向がどちらであるかには関係しない。
97 したがって、ユーザープログラムはこの件については心配しなくてよい。
99 \fIint\fP とポインター型が同じ大きさであるアーキテクチャーでは (x86\-32 はその例であり、両方の型とも 32 ビットである)、
100 \fBmakecontext\fP() の \fIargc\fP 以降の引き数としてポインターを渡してもうまく動くかもしれない。
101 しかしながら、このようにすると、移植性は保証されず、 標準に従えば動作は未定義であり、ポインターが \fIint\fP
102 よりも大きいアーキテクチャーでは正しく動作しないことだろう。 それにも関わらず、バージョン 2.8 以降の glibc では、
103 \fBmakecontext\fP() に変更が行われ、(x86\-64 などの) いくつかの 64 ビットアーキテクチャーで
104 引き数としてポインターを渡すことができるようになっている。
106 以下のサンプルプログラムは、 \fBgetcontext\fP(3), \fBmakecontext\fP(), \fBswapcontext\fP()
107 の使用方法の例を示すものである。 このプログラムを実行すると、以下のような出力が得られる:
112 main: swapcontext(&uctx_main, &uctx_func2)
114 func2: swapcontext(&uctx_func2, &uctx_func1)
116 func1: swapcontext(&uctx_func1, &uctx_func2)
125 #include <ucontext.h>
129 static ucontext_t uctx_main, uctx_func1, uctx_func2;
131 #define handle_error(msg) \e
132 do { perror(msg); exit(EXIT_FAILURE); } while (0)
137 printf("func1: started\en");
138 printf("func1: swapcontext(&uctx_func1, &uctx_func2)\en");
139 if (swapcontext(&uctx_func1, &uctx_func2) == \-1)
140 handle_error("swapcontext");
141 printf("func1: returning\en");
147 printf("func2: started\en");
148 printf("func2: swapcontext(&uctx_func2, &uctx_func1)\en");
149 if (swapcontext(&uctx_func2, &uctx_func1) == \-1)
150 handle_error("swapcontext");
151 printf("func2: returning\en");
155 main(int argc, char *argv[])
157 char func1_stack[16384];
158 char func2_stack[16384];
160 if (getcontext(&uctx_func1) == \-1)
161 handle_error("getcontext");
162 uctx_func1.uc_stack.ss_sp = func1_stack;
163 uctx_func1.uc_stack.ss_size = sizeof(func1_stack);
164 uctx_func1.uc_link = &uctx_main;
165 makecontext(&uctx_func1, func1, 0);
167 if (getcontext(&uctx_func2) == \-1)
168 handle_error("getcontext");
169 uctx_func2.uc_stack.ss_sp = func2_stack;
170 uctx_func2.uc_stack.ss_size = sizeof(func2_stack);
171 /* Successor context is f1(), unless argc > 1 */
172 uctx_func2.uc_link = (argc > 1) ? NULL : &uctx_func1;
173 makecontext(&uctx_func2, func2, 0);
175 printf("main: swapcontext(&uctx_main, &uctx_func2)\en");
176 if (swapcontext(&uctx_main, &uctx_func2) == \-1)
177 handle_error("swapcontext");
179 printf("main: exiting\en");
184 \fBsigaction\fP(2), \fBsigaltstack\fP(2), \fBsigprocmask\fP(2), \fBgetcontext\fP(3),
187 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は
188 \%https://www.kernel.org/doc/man\-pages/ に書かれている。