1 .\" Copyright (C) 2001 Andries Brouwer (aeb@cwi.nl)
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .TH MAKECONTEXT 3 2001-11-15 "Linux 2.4" "Linux Programmer's Manual"
25 makecontext, swapcontext \- manipulate user context
27 .B #include <ucontext.h>
29 .BI "void makecontext(ucontext_t *" ucp ", void *" func (),
30 .BI "int " argc ", ...);"
32 .BI "int swapcontext(ucontext_t *" oucp ", ucontext_t *" ucp );
34 In a System V-like environment, one has the type \fIucontext_t\fP defined in
36 and the four functions
37 \fBgetcontext\fP(), \fBsetcontext\fP(), \fBmakecontext\fP()
38 and \fBswapcontext\fP() that allow user-level context switching
39 between multiple threads of control within a process.
41 For the type and the first two functions, see
44 The \fBmakecontext\fP() function modifies the context pointed to
45 by \fIucp\fP (which was obtained from a call to \fBgetcontext\fP()).
46 Before invoking \fBmakecontext\fP(), the caller must allocate a new stack
47 for this context and assign its address to \fIucp->uc_stack\fP,
48 and define a successor context and
49 assign its address to \fIucp->uc_link\fP.
51 When this context is later activated (using \fBsetcontext\fP() or
52 \fBswapcontext\fP()) the function \fIfunc\fP is called,
53 and passed the series of integer
57 the caller must specify the number of these arguments in
59 When this function returns, the successor context is activated.
60 If the successor context pointer is NULL, the thread exits.
62 The \fBswapcontext\fP() function saves the current context in
63 the structure pointed to by \fIoucp\fP, and then activates the
64 context pointed to by \fIucp\fP.
66 When successful, \fBswapcontext\fP()
68 (But we may return later, in case \fIoucp\fP is
69 activated, in which case it looks like \fBswapcontext\fP() returns 0.)
70 On error, \fBswapcontext\fP() returns \-1 and
71 sets \fIerrno\fP appropriately.
75 Insufficient stack space left.
77 The interpretation of \fIucp->uc_stack\fP is just as in
79 namely, this struct contains the start and length of a memory area
80 to be used as the stack, regardless of the direction of growth of
82 Thus, it is not necessary for the user program to
83 worry about this direction.
88 The example program below demonstrates the use of
93 Running the program produces the following output:
98 main: swapcontext(&uctx_main, &uctx_func2)
100 func2: swapcontext(&uctx_func2, &uctx_func1)
102 func1: swapcontext(&uctx_func1, &uctx_func2)
110 #include <ucontext.h>
114 static ucontext_t uctx_main, uctx_func1, uctx_func2;
116 #define die(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
121 printf("func1: started\\n");
122 printf("func1: swapcontext(&uctx_func1, &uctx_func2)\\n");
123 if (swapcontext(&uctx_func1, &uctx_func2) == -1)
125 printf("func1: returning\\n");
131 printf("func2: started\\n");
132 printf("func2: swapcontext(&uctx_func2, &uctx_func1)\\n");
133 if (swapcontext(&uctx_func2, &uctx_func1) == -1)
135 printf("func2: returning\\n");
139 main(int argc, char *argv[])
141 char func1_stack[16384];
142 char func2_stack[16384];
144 if (getcontext(&uctx_func1) == -1)
146 uctx_func1.uc_stack.ss_sp = func1_stack;
147 uctx_func1.uc_stack.ss_size = sizeof(func1_stack);
148 uctx_func1.uc_link = &uctx_main;
149 makecontext(&uctx_func1, func1, 0);
151 if (getcontext(&uctx_func2) == -1)
153 uctx_func2.uc_stack.ss_sp = func2_stack;
154 uctx_func2.uc_stack.ss_size = sizeof(func2_stack);
155 /* Successor context is f1(), unless argc > 1 */
156 uctx_func2.uc_link = (argc > 1) ? NULL : &uctx_func1;
157 makecontext(&uctx_func2, func2, 0);
159 printf("main: swapcontext(&uctx_main, &uctx_func2)\\n");
160 if (swapcontext(&uctx_main, &uctx_func2) == -1)
163 printf("main: exiting\\n");