makecontext

Section: C Library Functions (3)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

makecontext, swapcontext - ユーザーコンテキストを操作する  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#include <ucontext.h>

void makecontext(ucontext_t *ucp, typeof(void (int arg0, ...)) *func,
                 int argc, ...);
int swapcontext(ucontext_t *restrict oucp,
                 const ucontext_t *restrict ucp);
 

説明

In a System V-like environment, one has the type ucontext_t (defined in <ucontext.h> and described in getcontext(3)) and the four functions getcontext(3), setcontext(3), makecontext(), and swapcontext() that allow user-level context switching between multiple threads of control within a process.

makecontext() 関数は、ポインター ucp が指すコンテキストを変更する (ucp は以前の getcontext(3) 呼び出しで得られたものである)。 makecontext() を起動する前には、呼び出し者は、このコンテキスト用に 新しいスタックを確保し、そのアドレスを ucp->uc_stack に代入し、 さらに後継のコンテキストを定義し、そのアドレスを ucp->uc_link に 代入しなければならない。

このコンテキストが将来 (setcontext(3) または swapcontext() によって) 有効にされると、関数 func が呼ばれ、 引数として argc 以降の整数 (int) 引数の列が渡される。 呼び出し者は argc にこれらの引数の個数を指定しなければならない。 この関数が戻ると、後継のコンテキストが有効になる。 後継コンテキストのポインターが NULL の場合、そのスレッドが終了する。

swapcontext() 関数は現在のコンテキストを ポインター oucp が指す構造体に保存し、 ポインター ucp が指すコンテキストを有効にする。  

返り値

When successful, swapcontext() does not return. (But we may return later, in case oucp is activated, in which case it looks like swapcontext() returns 0.) On error, swapcontext() returns -1 and sets errno to indicate the error.  

エラー

ENOMEM
スタックに割り当てる空間が残っていない。
 

属性

この節で使用されている用語の説明については、 attributes(7) を参照。
インターフェース属性値
makecontext() Thread safety MT-Safe race:ucp
swapcontext() Thread safety MT-Safe race:oucp race:ucp
 

標準規格

なし。  

歴史

glibc 2.1. SUSv2, POSIX.1-2001. Removed in POSIX.1-2008, citing portability issues, and recommending that applications be rewritten to use POSIX threads instead.  

注意

ucp->uc_stack の解釈は sigaltstack(2) の場合と同じである。 すなわちこの構造体には、 スタックとして用いられるメモリー領域の開始アドレスと長さが含まれ、 これはスタックが伸びる方向がどちらであるかには関係しない。 したがって、ユーザープログラムはこの件については心配しなくてよい。

On architectures where int and pointer types are the same size (e.g., x86-32, where both types are 32 bits), you may be able to get away with passing pointers as arguments to makecontext() following argc. However, doing this is not guaranteed to be portable, is undefined according to the standards, and won't work on architectures where pointers are larger than ints. Nevertheless, starting with glibc 2.8, glibc makes some changes to makecontext(), to permit this on some 64-bit architectures (e.g., x86-64).  

例

以下のサンプルプログラムは、 getcontext(3), makecontext(), swapcontext() の使用方法の例を示すものである。 このプログラムを実行すると、以下のような出力が得られる:

$ ./a.out
main: swapcontext(&uctx_main, &uctx_func2)
func2: started
func2: swapcontext(&uctx_func2, &uctx_func1)
func1: started
func1: swapcontext(&uctx_func1, &uctx_func2)
func2: returning
func1: returning
main: exiting
 

プログラムのソース

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <ucontext.h>

static ucontext_t uctx_main, uctx_func1, uctx_func2;

static void
func1(void)
{
    printf("%s: started\n", __func__);
    printf("%s: swapcontext(&uctx_func1, &uctx_func2)\n", __func__);
    if (swapcontext(&uctx_func1, &uctx_func2) == -1)
        err(EXIT_FAILURE, "swapcontext");
    printf("%s: returning\n", __func__);
}

static void
func2(void)
{
    printf("%s: started\n", __func__);
    printf("%s: swapcontext(&uctx_func2, &uctx_func1)\n", __func__);
    if (swapcontext(&uctx_func2, &uctx_func1) == -1)
        err(EXIT_FAILURE, "swapcontext");
    printf("%s: returning\n", __func__);
}

int
main(int argc, char *argv[])
{
    char func1_stack[16384];
    char func2_stack[16384];

    if (getcontext(&uctx_func1) == -1)
        err(EXIT_FAILURE, "getcontext");
    uctx_func1.uc_stack.ss_sp = func1_stack;
    uctx_func1.uc_stack.ss_size = sizeof(func1_stack);
    uctx_func1.uc_link = &uctx_main;
    makecontext(&uctx_func1, func1, 0);

    if (getcontext(&uctx_func2) == -1)
        err(EXIT_FAILURE, "getcontext");
    uctx_func2.uc_stack.ss_sp = func2_stack;
    uctx_func2.uc_stack.ss_size = sizeof(func2_stack);
    /* Successor context is f1(), unless argc > 1 */
    uctx_func2.uc_link = (argc > 1) ? NULL : &uctx_func1;
    makecontext(&uctx_func2, func2, 0);

    printf("%s: swapcontext(&uctx_main, &uctx_func2)\n", __func__);
    if (swapcontext(&uctx_main, &uctx_func2) == -1)
        err(EXIT_FAILURE, "swapcontext");

    printf("%s: exiting\n", __func__);
    exit(EXIT_SUCCESS);
}
 

関連項目

sigaction(2), sigaltstack(2), sigprocmask(2), getcontext(3), sigsetjmp(3)


 

Index

名前
ライブラリ
書式
説明
返り値
エラー
属性
標準規格
歴史
注意
例
プログラムのソース
関連項目

This document was created by man2html, using the manual pages.