2004-11-03 13:51:07 +00:00
|
|
|
.\" Copyright (C) 2001 Andries Brouwer (aeb@cwi.nl)
|
|
|
|
.\"
|
|
|
|
.\" 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.
|
|
|
|
.\"
|
2009-03-14 18:46:56 +00:00
|
|
|
.TH GETCONTEXT 2 2009-03-15 "Linux" "Linux Programmer's Manual"
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH NAME
|
|
|
|
getcontext, setcontext \- get or set the user context
|
|
|
|
.SH SYNOPSIS
|
|
|
|
.B #include <ucontext.h>
|
|
|
|
.sp
|
|
|
|
.BI "int getcontext(ucontext_t *" ucp );
|
|
|
|
.br
|
|
|
|
.BI "int setcontext(const ucontext_t *" ucp );
|
|
|
|
.SH DESCRIPTION
|
2006-01-13 09:39:55 +00:00
|
|
|
In a System V-like environment, one has the two types
|
2006-09-04 08:57:04 +00:00
|
|
|
\fImcontext_t\fP and \fIucontext_t\fP defined in
|
2004-11-03 13:51:07 +00:00
|
|
|
.I <ucontext.h>
|
|
|
|
and the four functions
|
2007-05-12 09:06:04 +00:00
|
|
|
.BR getcontext (),
|
|
|
|
.BR setcontext (),
|
|
|
|
.BR makecontext (3)
|
|
|
|
and
|
|
|
|
.BR swapcontext (3)
|
2004-11-03 13:51:07 +00:00
|
|
|
that allow user-level context switching between multiple
|
|
|
|
threads of control within a process.
|
|
|
|
.LP
|
2006-07-27 04:29:02 +00:00
|
|
|
The \fImcontext_t\fP type is machine-dependent and opaque.
|
|
|
|
The \fIucontext_t\fP type is a structure that has at least
|
2004-11-03 13:51:07 +00:00
|
|
|
the following fields:
|
2007-12-23 22:04:06 +00:00
|
|
|
.in +4
|
2004-11-03 13:51:07 +00:00
|
|
|
.nf
|
2006-07-27 05:03:27 +00:00
|
|
|
|
2004-11-03 13:51:07 +00:00
|
|
|
typedef struct ucontext {
|
2006-07-27 05:03:27 +00:00
|
|
|
struct ucontext *uc_link;
|
|
|
|
sigset_t uc_sigmask;
|
|
|
|
stack_t uc_stack;
|
|
|
|
mcontext_t uc_mcontext;
|
|
|
|
...
|
2004-11-03 13:51:07 +00:00
|
|
|
} ucontext_t;
|
2006-07-27 05:03:27 +00:00
|
|
|
|
2004-11-03 13:51:07 +00:00
|
|
|
.fi
|
2007-12-23 22:04:06 +00:00
|
|
|
.in
|
2006-07-27 05:03:27 +00:00
|
|
|
with \fIsigset_t\fP and \fIstack_t\fP defined in
|
2004-11-03 13:51:07 +00:00
|
|
|
.IR <signal.h> .
|
|
|
|
Here \fIuc_link\fP points to the context that will be resumed
|
|
|
|
when the current context terminates (in case the current context
|
2007-05-12 09:06:04 +00:00
|
|
|
was created using
|
|
|
|
.BR makecontext (3)),
|
|
|
|
\fIuc_sigmask\fP is the
|
2004-11-03 13:51:07 +00:00
|
|
|
set of signals blocked in this context (see
|
|
|
|
.BR sigprocmask (2)),
|
|
|
|
\fIuc_stack\fP is the stack used by this context (see
|
|
|
|
.BR sigaltstack (2)),
|
|
|
|
and \fIuc_mcontext\fP is the
|
|
|
|
machine-specific representation of the saved context,
|
|
|
|
that includes the calling thread's machine registers.
|
|
|
|
.LP
|
2007-05-12 09:06:04 +00:00
|
|
|
The function
|
|
|
|
.BR getcontext ()
|
|
|
|
initializes the structure
|
2004-11-03 13:51:07 +00:00
|
|
|
pointed at by \fIucp\fP to the currently active context.
|
|
|
|
.LP
|
2007-05-12 09:06:04 +00:00
|
|
|
The function
|
|
|
|
.BR setcontext ()
|
|
|
|
restores the user context
|
2007-04-12 22:42:49 +00:00
|
|
|
pointed at by \fIucp\fP.
|
|
|
|
A successful call does not return.
|
2007-05-12 09:06:04 +00:00
|
|
|
The context should have been obtained by a call of
|
|
|
|
.BR getcontext (),
|
|
|
|
or
|
|
|
|
.BR makecontext (3),
|
|
|
|
or passed as third argument to a signal
|
2004-11-03 13:51:07 +00:00
|
|
|
handler.
|
|
|
|
.LP
|
2007-05-12 09:06:04 +00:00
|
|
|
If the context was obtained by a call of
|
|
|
|
.BR getcontext (),
|
2004-11-03 13:51:07 +00:00
|
|
|
program execution continues as if this call just returned.
|
|
|
|
.LP
|
2007-05-12 09:06:04 +00:00
|
|
|
If the context was obtained by a call of
|
|
|
|
.BR makecontext (3),
|
2004-11-03 13:51:07 +00:00
|
|
|
program execution continues by a call to the function \fIfunc\fP
|
2007-05-12 09:06:04 +00:00
|
|
|
specified as the second argument of that call to
|
|
|
|
.BR makecontext (3).
|
2004-11-03 13:51:07 +00:00
|
|
|
When the function \fIfunc\fP returns, we continue with the
|
|
|
|
\fIuc_link\fP member of the structure \fIucp\fP specified as the
|
2007-05-12 09:06:04 +00:00
|
|
|
first argument of that call to
|
|
|
|
.BR makecontext (3).
|
2004-11-03 13:51:07 +00:00
|
|
|
When this member is NULL, the thread exits.
|
|
|
|
.LP
|
|
|
|
If the context was obtained by a call to a signal handler,
|
|
|
|
then old standard text says that "program execution continues with the
|
|
|
|
program instruction following the instruction interrupted
|
2007-04-12 22:42:49 +00:00
|
|
|
by the signal".
|
|
|
|
However, this sentence was removed in SUSv2,
|
2004-11-03 13:51:07 +00:00
|
|
|
and the present verdict is "the result is unspecified".
|
|
|
|
.SH "RETURN VALUE"
|
2007-05-12 09:06:04 +00:00
|
|
|
When successful,
|
|
|
|
.BR getcontext ()
|
|
|
|
returns 0 and
|
|
|
|
.BR setcontext ()
|
2007-04-12 22:42:49 +00:00
|
|
|
does not return.
|
|
|
|
On error, both return \-1 and set \fIerrno\fP
|
2004-11-03 13:51:07 +00:00
|
|
|
appropriately.
|
|
|
|
.SH ERRORS
|
|
|
|
None defined.
|
2007-05-18 16:06:42 +00:00
|
|
|
.SH "CONFORMING TO"
|
|
|
|
SUSv2, POSIX.1-2001.
|
2009-03-14 18:46:56 +00:00
|
|
|
POSIX.1-2008 removes the specification of
|
|
|
|
.BR getcontext (),
|
|
|
|
citing portability issues, and
|
|
|
|
recommending that applications be rewritten to use POSIX threads instead.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH NOTES
|
|
|
|
The earliest incarnation of this mechanism was the
|
2007-06-21 22:55:04 +00:00
|
|
|
.BR setjmp (3)/ longjmp (3)
|
2007-05-12 13:12:02 +00:00
|
|
|
mechanism.
|
2007-04-12 22:42:49 +00:00
|
|
|
Since that does not define
|
2004-11-03 13:51:07 +00:00
|
|
|
the handling of the signal context, the next stage was the
|
2007-06-21 22:55:04 +00:00
|
|
|
.BR sigsetjmp (3)/ siglongjmp (3)
|
2007-05-12 13:12:02 +00:00
|
|
|
pair.
|
2007-04-12 22:42:49 +00:00
|
|
|
The present mechanism gives much more control.
|
|
|
|
On the other hand,
|
2007-05-12 09:06:04 +00:00
|
|
|
there is no easy way to detect whether a return from
|
|
|
|
.BR getcontext ()
|
|
|
|
is from the first call, or via a
|
|
|
|
.BR setcontext ()
|
|
|
|
call.
|
2004-11-03 13:51:07 +00:00
|
|
|
The user has to invent her own bookkeeping device, and a register
|
|
|
|
variable won't do since registers are restored.
|
|
|
|
.LP
|
|
|
|
When a signal occurs, the current user context is saved and
|
|
|
|
a new context is created by the kernel for the signal handler.
|
2007-05-12 13:12:02 +00:00
|
|
|
Do not leave the handler using
|
|
|
|
.BR longjmp (3):
|
|
|
|
it is undefined what would happen with contexts.
|
|
|
|
Use
|
|
|
|
.BR siglongjmp (3)
|
|
|
|
or
|
|
|
|
.BR setcontext ()
|
|
|
|
instead.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "SEE ALSO"
|
|
|
|
.BR sigaction (2),
|
|
|
|
.BR sigaltstack (2),
|
|
|
|
.BR sigprocmask (2),
|
|
|
|
.BR longjmp (3),
|
|
|
|
.BR makecontext (3),
|
|
|
|
.BR sigsetjmp (3)
|