1MAKECONTEXT(P)             POSIX Programmer's Manual            MAKECONTEXT(P)
2
3
4

NAME

6       makecontext, swapcontext - manipulate user contexts
7

SYNOPSIS

9       #include <ucontext.h>
10
11       void makecontext(ucontext_t *ucp, void (*func)(void),
12              int argc, ...);
13       int swapcontext(ucontext_t *restrict oucp,
14              const ucontext_t *restrict ucp);
15
16

DESCRIPTION

18       The  makecontext()  function shall modify the context specified by ucp,
19       which has been initialized using getcontext().  When  this  context  is
20       resumed  using  swapcontext()  or setcontext(), program execution shall
21       continue by calling func, passing it the arguments that follow argc  in
22       the makecontext() call.
23
24       Before  a  call  is made to makecontext(), the application shall ensure
25       that the context being modified has  a  stack  allocated  for  it.  The
26       application  shall  ensure that the value of argc matches the number of
27       arguments of type int passed to func; otherwise, the behavior is  unde‐
28       fined.
29
30       The  uc_link  member  is  used  to  determine the context that shall be
31       resumed when the context being modified by makecontext()  returns.  The
32       application  shall  ensure that the uc_link member is initialized prior
33       to the call to makecontext().
34
35       The swapcontext() function shall save the current context in  the  con‐
36       text structure pointed to by oucp and shall set the context to the con‐
37       text structure pointed to by ucp.
38

RETURN VALUE

40       Upon successful completion, swapcontext() shall return 0. Otherwise, -1
41       shall be returned and errno set to indicate the error.
42

ERRORS

44       The swapcontext() function shall fail if:
45
46       ENOMEM The ucp argument does not have enough stack left to complete the
47              operation.
48
49
50       The following sections are informative.
51

EXAMPLES

53       The following example illustrates the use of makecontext():
54
55
56              #include <stdio.h>
57              #include <ucontext.h>
58
59
60              static ucontext_t ctx[3];
61
62
63              static void
64              f1 (void)
65              {
66                  puts("start f1");
67                  swapcontext(&ctx[1], &ctx[2]);
68                  puts("finish f1");
69              }
70
71
72              static void
73              f2 (void)
74              {
75                  puts("start f2");
76                  swapcontext(&ctx[2], &ctx[1]);
77                  puts("finish f2");
78              }
79
80
81              int
82              main (void)
83              {
84                  char st1[8192];
85                  char st2[8192];
86
87
88                  getcontext(&ctx[1]);
89                  ctx[1].uc_stack.ss_sp = st1;
90                  ctx[1].uc_stack.ss_size = sizeof st1;
91                  ctx[1].uc_link = &ctx[0];
92                  makecontext(&ctx[1], f1, 0);
93
94
95                  getcontext(&ctx[2]);
96                  ctx[2].uc_stack.ss_sp = st2;
97                  ctx[2].uc_stack.ss_size = sizeof st2;
98                  ctx[2].uc_link = &ctx[1];
99                  makecontext(&ctx[2], f2, 0);
100
101
102                  swapcontext(&ctx[0], &ctx[2]);
103                  return 0;
104              }
105

APPLICATION USAGE

107       None.
108

RATIONALE

110       None.
111

FUTURE DIRECTIONS

113       None.
114

SEE ALSO

116       exit() , getcontext() , sigaction() , sigprocmask() , the Base  Defini‐
117       tions volume of IEEE Std 1003.1-2001, <ucontext.h>
118
120       Portions  of  this text are reprinted and reproduced in electronic form
121       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
122       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
123       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
124       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
125       event of any discrepancy between this version and the original IEEE and
126       The  Open Group Standard, the original IEEE and The Open Group Standard
127       is the referee document. The original Standard can be  obtained  online
128       at http://www.opengroup.org/unix/online.html .
129
130
131
132IEEE/The Open Group                  2003                       MAKECONTEXT(P)
Impressum