標準/拡張機能 | C/C++ | 依存項目 |
---|---|---|
XPG4.2 |
両方 | POSIX(ON) |
#define _XOPEN_SOURCE_EXTENDED 1
#include <ucontext.h>
void makecontext(ucontext_t *ucp, void (*func)(), int argc, …);
makecontext() 関数は、ucp で指定されたコンテキストを 変更します。このコンテキストは getcontext() を使って初期化されたものです。このコンテキストが setcontext() または swapcontext() を使って 再開されると、プログラム実行は func() を 呼び出し、makecontext() 呼び出しの argc に続く引数を これに渡して続行します。
argc の値は、func() に渡される整数引数の 番号に一致しなければなりません。一致していない場合、動作は未定義になります。
ucontext_t の uc_link メンバーは、makecontext() により変更中のコンテキストが戻るときに再開されるコンテキストを判別する場合に使用します。uc_link メンバーが 0 でない場合、uc_link メンバーにより指示されたコンテキストを使用する setcontext() 呼び出しのあとであるかのように、プロセスが継続します。uc_link メンバーが 0 の場合、プロセスは、exit() が呼び出されたかのように終了します。uc_link メンバーは、makecontext() 呼び出しの前に 初期化しておいてください。
この関数がサポートされるのは、POSIX プログラムだけです。
この関数は、AMODE 31 XPLINK 環境ではサポートされません (例えば、 AMODE 31 のときで、main() 関数が XPLINK オプションを指定してコンパイルされたか、 または XPLINK(ON) ランタイム・オプションが指定された環境)。
mcontext_t uc_mcontext A machine-specific representation
of the saved context.
ucontext_t *uc_link Pointer to the context that will
be resumed when this context returns.
sigset_t uc_sigmask The set of signals that are blocked
when this context is active.
stack_t uc_stack The stack used by this context.
C++ の特殊な動作
C: void func(int, int);
:
makecontext(&context, func, 2, arg1, arg2);
C++: extern "C" void func();
:
makecontext(&context, func, 2, arg1, arg2);
AMODE 64 に関する考慮事項
スタック用のストレージは 2 GB 境界より上でなければなりません。 これは、__malloc24() または __malloc31() 関数で獲得されたストレージではない場合があります。 スタックは、1 M の保護ページ (1 M 境界上に位置合わせされる) を作成するのに十分な大きさである必要があります。
環境変数 _EDC_CONTEXT_GUARD は、どのようなときにスタックを保護するか、保護しないかを制御するために使用できます。_EDC_CONTEXT_GUARD 環境変数の詳細については、「z/OS XL C/C++ プログラミング・ガイド」の『環境変数の使用』の章を参照してください。
makecontext() は、値を戻しません。
argc 引数が 0 未満の値を指定する。
/* This example shows the usage of makecontext(). */
#define _XOPEN_SOURCE_EXTENDED 1
#include <stdlib.h>
#include <stdio.h>
#include <ucontext.h>
#include <errno.h>
#ifdef _LP64
#define STACK_SIZE 2097152+16384 /* large enough value for AMODE 64 */
#else
#define STACK_SIZE 16384 /* AMODE 31 addressing */
#endif
void func(int);
ucontext_t context, *cp = &context;
int main(void) {
int value = 1;
getcontext(cp);
context.uc_link = 0;
if ((context.uc_stack.ss_sp = (char *) malloc(STACK_SIZE)) != NULL) {
context.uc_stack.ss_size = STACK_SIZE;
context.uc_stack.ss_flags = 0;
errno = 0;
makecontext(cp,func,1,value);
if(errno != 0){
perror("Error reported by makecontext()");
return -1; /* Error occurred exit */ }
}
else {
perror("not enough storage for stack");
abort();
}
printf("context has been built¥n");
setcontext(cp);
perror("returned from setcontext");
abort();
}
void func(int arg) {
printf("function called with value %d¥n",arg);
printf("process will exit when function returns¥n");
return;
}
context has been built
function called with value 1
process will exit when function returns