-.\" Copyright (C) 2001 Andries Brouwer (aeb@cwi.nl)
+\" Copyright (C) 2001 Andries Brouwer (aeb@cwi.nl)
+.\" and Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
.\"
+.\" %%%LICENSE_START(VERBATIM)
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
+.\" %%%LICENSE_END
.\"
-.TH MAKECONTEXT 3 2001-11-15 "GNU" "Linux Programmer's Manual"
+.\" 2006-08-02, mtk, Added example program
+.\"
+.TH MAKECONTEXT 3 2017-09-15 "GNU" "Linux Programmer's Manual"
.SH NAME
makecontext, swapcontext \- manipulate user context
.SH SYNOPSIS
.B #include <ucontext.h>
-.sp
+.PP
.BI "void makecontext(ucontext_t *" ucp ", void (*" func )(),
.BI "int " argc ", ...);"
-.br
-.BI "int swapcontext(ucontext_t *" oucp ", ucontext_t *" ucp );
+.PP
+.BI "int swapcontext(ucontext_t *" oucp ", const ucontext_t *" ucp );
.SH DESCRIPTION
In a System V-like environment, one has the type \fIucontext_t\fP defined in
.I <ucontext.h>
and the four functions
-.BR getcontext (2),
-.BR setcontext (2),
+.BR getcontext (3),
+.BR setcontext (3),
.BR makecontext ()
and
.BR swapcontext ()
that allow user-level context switching
between multiple threads of control within a process.
-.LP
+.PP
For the type and the first two functions, see
-.BR getcontext (2).
-.LP
+.BR getcontext (3).
+.PP
The
.BR makecontext ()
function modifies the context pointed to
by \fIucp\fP (which was obtained from a call to
-.BR getcontext (2)).
+.BR getcontext (3)).
Before invoking
.BR makecontext (),
the caller must allocate a new stack
-for this context and assign its address to \fIucp->uc_stack\fP,
+for this context and assign its address to \fIucp\->uc_stack\fP,
and define a successor context and
-assign its address to \fIucp->uc_link\fP.
-
+assign its address to \fIucp\->uc_link\fP.
+.PP
When this context is later activated (using
-.BR setcontext (2)
+.BR setcontext (3)
or
.BR swapcontext ())
the function \fIfunc\fP is called,
.IR argc .
When this function returns, the successor context is activated.
If the successor context pointer is NULL, the thread exits.
-.LP
+.PP
The
.BR swapcontext ()
function saves the current context in
the structure pointed to by \fIoucp\fP, and then activates the
context pointed to by \fIucp\fP.
-.SH "RETURN VALUE"
+.SH RETURN VALUE
When successful,
.BR swapcontext ()
does not return.
.TP
.B ENOMEM
Insufficient stack space left.
-.SH "CONFORMING TO"
+.SH VERSIONS
+.BR makecontext ()
+and
+.BR swapcontext ()
+are provided in glibc since version 2.1.
+.SH ATTRIBUTES
+For an explanation of the terms used in this section, see
+.BR attributes (7).
+.TS
+allbox;
+lb lb lb
+l l l.
+Interface Attribute Value
+T{
+.BR makecontext ()
+T} Thread safety MT-Safe race:ucp
+T{
+.BR swapcontext ()
+T} Thread safety MT-Safe race:oucp race:ucp
+.TE
+.SH CONFORMING TO
SUSv2, POSIX.1-2001.
+POSIX.1-2008 removes the specifications of
+.BR makecontext ()
+and
+.BR swapcontext (),
+citing portability issues, and
+recommending that applications be rewritten to use POSIX threads instead.
.SH NOTES
-The interpretation of \fIucp->uc_stack\fP is just as in
+The interpretation of \fIucp\->uc_stack\fP is just as in
.BR sigaltstack (2),
namely, this struct contains the start and length of a memory area
to be used as the stack, regardless of the direction of growth of
the stack.
Thus, it is not necessary for the user program to
worry about this direction.
+.PP
+On architectures where
+.I 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
+.BR makecontext ()
+following
+.IR 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
+.IR int s.
+Nevertheless, starting with version 2.8, glibc makes some changes to
+.BR makecontext (),
+to permit this on some 64-bit architectures (e.g., x86-64).
.SH EXAMPLE
.PP
The example program below demonstrates the use of
-.BR getcontext (2),
+.BR getcontext (3),
.BR makecontext (),
and
.BR swapcontext ().
Running the program produces the following output:
-.in +0.5i
-.nf
-
-$ ./a.out
+.PP
+.in +4n
+.EX
+.RB "$" " ./a.out"
main: swapcontext(&uctx_main, &uctx_func2)
func2: started
func2: swapcontext(&uctx_func2, &uctx_func1)
func2: returning
func1: returning
main: exiting
-
-.fi
-.in -0.5i
-.nf
+.EE
+.in
+.SS Program source
+\&
+.EX
#include <ucontext.h>
#include <stdio.h>
#include <stdlib.h>
static ucontext_t uctx_main, uctx_func1, uctx_func2;
-#define die(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
+#define handle_error(msg) \\
+ do { perror(msg); exit(EXIT_FAILURE); } while (0)
static void
func1(void)
printf("func1: started\\n");
printf("func1: swapcontext(&uctx_func1, &uctx_func2)\\n");
if (swapcontext(&uctx_func1, &uctx_func2) == \-1)
- die("swapcontext");
+ handle_error("swapcontext");
printf("func1: returning\\n");
}
printf("func2: started\\n");
printf("func2: swapcontext(&uctx_func2, &uctx_func1)\\n");
if (swapcontext(&uctx_func2, &uctx_func1) == \-1)
- die("swapcontext");
+ handle_error("swapcontext");
printf("func2: returning\\n");
}
char func2_stack[16384];
if (getcontext(&uctx_func1) == \-1)
- die("getcontext");
+ handle_error("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)
- die("getcontext");
+ handle_error("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 */
printf("main: swapcontext(&uctx_main, &uctx_func2)\\n");
if (swapcontext(&uctx_main, &uctx_func2) == \-1)
- die("swapcontext");
+ handle_error("swapcontext");
printf("main: exiting\\n");
exit(EXIT_SUCCESS);
}
-.fi
-.SH "SEE ALSO"
-.BR getcontext (2),
+.EE
+.SH SEE ALSO
.BR sigaction (2),
.BR sigaltstack (2),
.BR sigprocmask (2),
+.BR getcontext (3),
.BR sigsetjmp (3)