2004-11-03 13:51:07 +00:00
|
|
|
.\" Copyright (C) 2001 Andries Brouwer (aeb@cwi.nl)
|
2008-06-11 13:43:34 +00:00
|
|
|
.\" and Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
|
2004-11-03 13:51:07 +00:00
|
|
|
.\"
|
|
|
|
.\" 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.
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to copy and distribute modified versions of this
|
|
|
|
.\" manual under the conditions for verbatim copying, provided that the
|
|
|
|
.\" entire resulting derived work is distributed under the terms of a
|
|
|
|
.\" permission notice identical to this one.
|
2007-04-12 22:42:49 +00:00
|
|
|
.\"
|
2004-11-03 13:51:07 +00:00
|
|
|
.\" Since the Linux kernel and libraries are constantly changing, this
|
|
|
|
.\" manual page may be incorrect or out-of-date. The author(s) assume no
|
|
|
|
.\" responsibility for errors or omissions, or for damages resulting from
|
|
|
|
.\" the use of the information contained herein. The author(s) may not
|
|
|
|
.\" have taken the same level of care in the production of this manual,
|
|
|
|
.\" which is licensed free of charge, as they might when working
|
|
|
|
.\" professionally.
|
2007-04-12 22:42:49 +00:00
|
|
|
.\"
|
2004-11-03 13:51:07 +00:00
|
|
|
.\" Formatted or processed versions of this manual, if unaccompanied by
|
|
|
|
.\" the source, must acknowledge the copyright and authors of this work.
|
2008-06-11 13:43:34 +00:00
|
|
|
.\" 2006-08-02, mtk, Added example program
|
2004-11-03 13:51:07 +00:00
|
|
|
.\"
|
2009-03-30 21:07:16 +00:00
|
|
|
.TH MAKECONTEXT 3 2009-03-31 "GNU" "Linux Programmer's Manual"
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH NAME
|
|
|
|
makecontext, swapcontext \- manipulate user context
|
|
|
|
.SH SYNOPSIS
|
|
|
|
.B #include <ucontext.h>
|
|
|
|
.sp
|
2007-07-13 08:12:17 +00:00
|
|
|
.BI "void makecontext(ucontext_t *" ucp ", void (*" func )(),
|
2004-11-03 13:51:07 +00:00
|
|
|
.BI "int " argc ", ...);"
|
2007-12-23 13:45:24 +00:00
|
|
|
.sp
|
2006-05-31 22:16:55 +00:00
|
|
|
.BI "int swapcontext(ucontext_t *" oucp ", ucontext_t *" ucp );
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH DESCRIPTION
|
2006-07-27 04:55:50 +00:00
|
|
|
In a System V-like environment, one has the type \fIucontext_t\fP defined in
|
2004-11-03 13:51:07 +00:00
|
|
|
.I <ucontext.h>
|
|
|
|
and the four functions
|
2007-06-02 06:19:03 +00:00
|
|
|
.BR getcontext (2),
|
|
|
|
.BR setcontext (2),
|
2007-05-12 09:06:04 +00:00
|
|
|
.BR makecontext ()
|
|
|
|
and
|
|
|
|
.BR swapcontext ()
|
|
|
|
that allow user-level context switching
|
2004-11-03 13:51:07 +00:00
|
|
|
between multiple threads of control within a process.
|
|
|
|
.LP
|
|
|
|
For the type and the first two functions, see
|
|
|
|
.BR getcontext (2).
|
|
|
|
.LP
|
2007-05-12 09:06:04 +00:00
|
|
|
The
|
|
|
|
.BR makecontext ()
|
|
|
|
function modifies the context pointed to
|
|
|
|
by \fIucp\fP (which was obtained from a call to
|
2007-06-02 06:19:03 +00:00
|
|
|
.BR getcontext (2)).
|
2007-05-12 09:06:04 +00:00
|
|
|
Before invoking
|
|
|
|
.BR makecontext (),
|
|
|
|
the caller must allocate a new stack
|
2007-12-22 16:40:46 +00:00
|
|
|
for this context and assign its address to \fIucp\->uc_stack\fP,
|
2006-07-27 05:53:22 +00:00
|
|
|
and define a successor context and
|
2007-12-22 16:40:46 +00:00
|
|
|
assign its address to \fIucp\->uc_link\fP.
|
2006-07-27 04:55:50 +00:00
|
|
|
|
2007-05-12 09:06:04 +00:00
|
|
|
When this context is later activated (using
|
2007-06-02 06:19:03 +00:00
|
|
|
.BR setcontext (2)
|
2007-05-12 09:06:04 +00:00
|
|
|
or
|
|
|
|
.BR swapcontext ())
|
|
|
|
the function \fIfunc\fP is called,
|
2007-04-12 22:42:49 +00:00
|
|
|
and passed the series of integer
|
2006-07-27 05:53:22 +00:00
|
|
|
.RI ( int )
|
2006-07-27 04:55:50 +00:00
|
|
|
arguments that follow
|
|
|
|
.IR argc ;
|
|
|
|
the caller must specify the number of these arguments in
|
|
|
|
.IR argc .
|
2007-04-12 22:42:49 +00:00
|
|
|
When this function returns, the successor context is activated.
|
2006-07-27 04:55:50 +00:00
|
|
|
If the successor context pointer is NULL, the thread exits.
|
2004-11-03 13:51:07 +00:00
|
|
|
.LP
|
2007-05-12 09:06:04 +00:00
|
|
|
The
|
|
|
|
.BR swapcontext ()
|
|
|
|
function saves the current context in
|
2004-11-03 13:51:07 +00:00
|
|
|
the structure pointed to by \fIoucp\fP, and then activates the
|
|
|
|
context pointed to by \fIucp\fP.
|
|
|
|
.SH "RETURN VALUE"
|
2007-05-12 09:06:04 +00:00
|
|
|
When successful,
|
|
|
|
.BR swapcontext ()
|
2007-04-12 22:42:49 +00:00
|
|
|
does not return.
|
|
|
|
(But we may return later, in case \fIoucp\fP is
|
2007-05-12 09:06:04 +00:00
|
|
|
activated, in which case it looks like
|
|
|
|
.BR swapcontext ()
|
|
|
|
returns 0.)
|
|
|
|
On error,
|
|
|
|
.BR swapcontext ()
|
|
|
|
returns \-1 and
|
2006-07-27 05:53:22 +00:00
|
|
|
sets \fIerrno\fP appropriately.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH ERRORS
|
|
|
|
.TP
|
|
|
|
.B ENOMEM
|
|
|
|
Insufficient stack space left.
|
2008-06-11 13:43:34 +00:00
|
|
|
.SH VERSIONS
|
|
|
|
.BR makecontext ()
|
|
|
|
and
|
|
|
|
.BR swapcontext ()
|
|
|
|
are provided in glibc since version 2.1.
|
2007-05-19 04:30:20 +00:00
|
|
|
.SH "CONFORMING TO"
|
|
|
|
SUSv2, POSIX.1-2001.
|
2008-08-06 12:24:39 +00:00
|
|
|
POSIX.1-2008 removes the specifications of
|
|
|
|
.BR makecontext ()
|
|
|
|
and
|
2009-03-14 18:47:36 +00:00
|
|
|
.BR swapcontext (),
|
|
|
|
citing portability issues, and
|
|
|
|
recommending that applications be rewritten to use POSIX threads instead.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH NOTES
|
2007-12-22 16:40:46 +00:00
|
|
|
The interpretation of \fIucp\->uc_stack\fP is just as in
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR sigaltstack (2),
|
2006-07-27 04:55:50 +00:00
|
|
|
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
|
2007-04-12 22:42:49 +00:00
|
|
|
the stack.
|
2006-07-27 04:55:50 +00:00
|
|
|
Thus, it is not necessary for the user program to
|
2004-11-03 13:51:07 +00:00
|
|
|
worry about this direction.
|
2008-11-08 01:31:15 +00:00
|
|
|
|
|
|
|
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.
|
2008-11-08 01:57:48 +00:00
|
|
|
Nevertheless, starting with version 2.8, glibc makes some changes to
|
2008-11-08 01:31:15 +00:00
|
|
|
.BR makecontext (3),
|
|
|
|
to permit this on some 64-bit architectures (e.g., x86-64).
|
2006-07-27 05:53:22 +00:00
|
|
|
.SH EXAMPLE
|
|
|
|
.PP
|
|
|
|
The example program below demonstrates the use of
|
2007-06-02 06:19:03 +00:00
|
|
|
.BR getcontext (2),
|
2006-07-27 05:53:22 +00:00
|
|
|
.BR makecontext (),
|
|
|
|
and
|
|
|
|
.BR swapcontext ().
|
|
|
|
Running the program produces the following output:
|
2007-12-19 05:53:30 +00:00
|
|
|
.in +4n
|
2006-07-27 05:53:22 +00:00
|
|
|
.nf
|
|
|
|
|
eventfd.2, execve.2, getdents.2, ioprio_set.2, mprotect.2, signalfd.2, timerfd_create.2, wait.2, backtrace.3, clock_getcpuclockid.3, end.3, fmemopen.3, fopencookie.3, frexp.3, getdate.3, getgrouplist.3, getprotoent_r.3, getservent_r.3, gnu_get_libc_version.3, inet.3, inet_pton.3, makecontext.3, malloc.3, matherr.3, offsetof.3, pthread_attr_init.3, pthread_create.3, pthread_getattr_np.3, sem_wait.3, strftime.3, strtok.3, strtol.3, core.5, proc.5, cpuset.7, mq_overview.7: Global fix: Format user input in shell sessions in boldface
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2008-10-28 15:37:22 +00:00
|
|
|
.RB "$" " ./a.out"
|
2006-07-27 05:53:22 +00:00
|
|
|
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
|
|
|
|
.fi
|
2007-12-19 05:53:30 +00:00
|
|
|
.in
|
eventfd.2, getdents.2, mprotect.2, signalfd.2, timerfd_create.2, wait.2, backtrace.3, clock_getcpuclockid.3, end.3, fmemopen.3, fopencookie.3, getdate.3, getgrouplist.3, getprotoent_r.3, getservent_r.3, gnu_get_libc_version.3, inet.3, inet_pton.3, makecontext.3, matherr.3, offsetof.3, pthread_attr_init.3, pthread_create.3, pthread_getattr_np.3, sem_wait.3, strtol.3, core.5: global fix: Add ".SS Program source" to EXAMPLE
Add ".SS Program source" to clearly distinguish shell session and
descriptive text from actual program code.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2008-10-28 15:51:05 +00:00
|
|
|
.SS Program source
|
eventfd.2, getdents.2, mprotect.2, signalfd.2, timerfd_create.2, wait.2, backtrace.3, clock_getcpuclockid.3, end.3, fmemopen.3, fopencookie.3, frexp.3, getaddrinfo.3, getdate.3, getgrouplist.3, getprotoent_r.3, getservent_r.3, gnu_get_libc_version.3, inet.3, inet_pton.3, makecontext.3, matherr.3, offsetof.3, pthread_attr_init.3, pthread_create.3, pthread_getattr_np.3, sem_wait.3, strftime.3, strtok.3, strtol.3, core.5: srcfix
s/\.R " "/\\\&/ as a way of getting a blank line after a .SS heading.
(Suggested by Sam Varshavchik <mrsam@courier-mta.com>)
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2008-11-05 01:42:45 +00:00
|
|
|
\&
|
2006-07-27 05:53:22 +00:00
|
|
|
.nf
|
|
|
|
#include <ucontext.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
static ucontext_t uctx_main, uctx_func1, uctx_func2;
|
|
|
|
|
2007-11-19 03:25:26 +00:00
|
|
|
#define handle_error(msg) \\
|
|
|
|
do { perror(msg); exit(EXIT_FAILURE); } while (0)
|
2006-07-27 05:53:22 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
func1(void)
|
|
|
|
{
|
|
|
|
printf("func1: started\\n");
|
|
|
|
printf("func1: swapcontext(&uctx_func1, &uctx_func2)\\n");
|
2007-06-20 21:39:45 +00:00
|
|
|
if (swapcontext(&uctx_func1, &uctx_func2) == \-1)
|
2007-11-19 03:25:26 +00:00
|
|
|
handle_error("swapcontext");
|
2006-07-27 05:53:22 +00:00
|
|
|
printf("func1: returning\\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
func2(void)
|
|
|
|
{
|
|
|
|
printf("func2: started\\n");
|
|
|
|
printf("func2: swapcontext(&uctx_func2, &uctx_func1)\\n");
|
2007-06-20 21:39:45 +00:00
|
|
|
if (swapcontext(&uctx_func2, &uctx_func1) == \-1)
|
2007-11-19 03:25:26 +00:00
|
|
|
handle_error("swapcontext");
|
2006-07-27 05:53:22 +00:00
|
|
|
printf("func2: returning\\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char func1_stack[16384];
|
|
|
|
char func2_stack[16384];
|
|
|
|
|
2007-06-20 21:39:45 +00:00
|
|
|
if (getcontext(&uctx_func1) == \-1)
|
2007-11-19 03:25:26 +00:00
|
|
|
handle_error("getcontext");
|
2006-07-27 05:53:22 +00:00
|
|
|
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);
|
|
|
|
|
2007-06-20 21:39:45 +00:00
|
|
|
if (getcontext(&uctx_func2) == \-1)
|
2007-11-19 03:25:26 +00:00
|
|
|
handle_error("getcontext");
|
2006-07-27 05:53:22 +00:00
|
|
|
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("main: swapcontext(&uctx_main, &uctx_func2)\\n");
|
2007-06-20 21:39:45 +00:00
|
|
|
if (swapcontext(&uctx_main, &uctx_func2) == \-1)
|
2007-11-19 03:25:26 +00:00
|
|
|
handle_error("swapcontext");
|
2006-07-27 05:53:22 +00:00
|
|
|
|
|
|
|
printf("main: exiting\\n");
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
.fi
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "SEE ALSO"
|
|
|
|
.BR getcontext (2),
|
|
|
|
.BR sigaction (2),
|
|
|
|
.BR sigaltstack (2),
|
|
|
|
.BR sigprocmask (2),
|
|
|
|
.BR sigsetjmp (3)
|