man-pages/man3/getcontext.3.html
2021-03-31 01:06:50 +01:00

308 lines
7.4 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of GETCONTEXT</TITLE>
</HEAD><BODY>
<H1>GETCONTEXT</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
getcontext, setcontext - get or set the user context
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/ucontext.h">ucontext.h</A>&gt;</B>
<P>
<B>int getcontext(ucontext_t *</B><I>ucp</I><B>);</B>
<BR>
<B>int setcontext(const ucontext_t *</B><I>ucp</I><B>);</B>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
In a System V-like environment, one has the two types
<I>mcontext_t</I>
and
<I>ucontext_t</I>
defined in
<I>&lt;<A HREF="file:///usr/include/ucontext.h">ucontext.h</A>&gt;</I>
and the four functions
<B>getcontext</B>(),
<B>setcontext</B>(),
<B><A HREF="/cgi-bin/man/man2html?3+makecontext">makecontext</A></B>(3),
and
<B><A HREF="/cgi-bin/man/man2html?3+swapcontext">swapcontext</A></B>(3)
that allow user-level context switching between multiple
threads of control within a process.
<P>
The
<I>mcontext_t</I>
type is machine-dependent and opaque.
The
<I>ucontext_t</I>
type is a structure that has at least
the following fields:
<P>
typedef struct ucontext_t {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;ucontext_t&nbsp;*uc_link;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;sigset_t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;uc_sigmask;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;stack_t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;uc_stack;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;mcontext_t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;uc_mcontext;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;...
} ucontext_t;
<P>
with
<I>sigset_t</I>
and
<I>stack_t</I>
defined in
<I>&lt;<A HREF="file:///usr/include/signal.h">signal.h</A>&gt;</I>.
Here
<I>uc_link</I>
points to the context that will be resumed
when the current context terminates (in case the current context
was created using
<B><A HREF="/cgi-bin/man/man2html?3+makecontext">makecontext</A></B>(3)),
<I>uc_sigmask</I>
is the
set of signals blocked in this context (see
<B><A HREF="/cgi-bin/man/man2html?2+sigprocmask">sigprocmask</A></B>(2)),
<I>uc_stack</I>
is the stack used by this context (see
<B><A HREF="/cgi-bin/man/man2html?2+sigaltstack">sigaltstack</A></B>(2)),
and
<I>uc_mcontext</I>
is the
machine-specific representation of the saved context,
that includes the calling thread's machine registers.
<P>
The function
<B>getcontext</B>()
initializes the structure
pointed at by
<I>ucp</I>
to the currently active context.
<P>
The function
<B>setcontext</B>()
restores the user context
pointed at by
<I>ucp</I>.
A successful call does not return.
The context should have been obtained by a call of
<B>getcontext</B>(),
or
<B><A HREF="/cgi-bin/man/man2html?3+makecontext">makecontext</A></B>(3),
or passed as third argument to a signal
handler.
<P>
If the context was obtained by a call of
<B>getcontext</B>(),
program execution continues as if this call just returned.
<P>
If the context was obtained by a call of
<B><A HREF="/cgi-bin/man/man2html?3+makecontext">makecontext</A></B>(3),
program execution continues by a call to the function
<I>func</I>
specified as the second argument of that call to
<B><A HREF="/cgi-bin/man/man2html?3+makecontext">makecontext</A></B>(3).
When the function
<I>func</I>
returns, we continue with the
<I>uc_link</I>
member of the structure
<I>ucp</I>
specified as the
first argument of that call to
<B><A HREF="/cgi-bin/man/man2html?3+makecontext">makecontext</A></B>(3).
When this member is NULL, the thread exits.
<P>
If the context was obtained by a call to a signal handler,
then old standard text says that &quot;program execution continues with the
program instruction following the instruction interrupted
by the signal&quot;.
However, this sentence was removed in SUSv2,
and the present verdict is &quot;the result is unspecified&quot;.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
When successful,
<B>getcontext</B>()
returns 0 and
<B>setcontext</B>()
does not return.
On error, both return -1 and set
<I>errno</I>
appropriately.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
None defined.
<A NAME="lbAG">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>getcontext</B>(),
<B>setcontext</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe race:ucp<BR></TD></TR>
</TABLE>
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
SUSv2, POSIX.1-2001.
POSIX.1-2008 removes the specification of
<B>getcontext</B>(),
citing portability issues, and
recommending that applications be rewritten to use POSIX threads instead.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
The earliest incarnation of this mechanism was the
<B><A HREF="/cgi-bin/man/man2html?3+setjmp">setjmp</A></B>(3)/<B><A HREF="/cgi-bin/man/man2html?3+longjmp">longjmp</A></B>(3)
mechanism.
Since that does not define
the handling of the signal context, the next stage was the
<B><A HREF="/cgi-bin/man/man2html?3+sigsetjmp">sigsetjmp</A></B>(3)/<B><A HREF="/cgi-bin/man/man2html?3+siglongjmp">siglongjmp</A></B>(3)
pair.
The present mechanism gives much more control.
On the other hand,
there is no easy way to detect whether a return from
<B>getcontext</B>()
is from the first call, or via a
<B>setcontext</B>()
call.
The user has to invent their own bookkeeping device, and a register
variable won't do since registers are restored.
<P>
When a signal occurs, the current user context is saved and
a new context is created by the kernel for the signal handler.
Do not leave the handler using
<B><A HREF="/cgi-bin/man/man2html?3+longjmp">longjmp</A></B>(3):
it is undefined what would happen with contexts.
Use
<B><A HREF="/cgi-bin/man/man2html?3+siglongjmp">siglongjmp</A></B>(3)
or
<B>setcontext</B>()
instead.
<A NAME="lbAJ">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+sigaction">sigaction</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+sigaltstack">sigaltstack</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+sigprocmask">sigprocmask</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+longjmp">longjmp</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+makecontext">makecontext</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+sigsetjmp">sigsetjmp</A></B>(3)
<A NAME="lbAK">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="4"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="5"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="6"><A HREF="#lbAG">ATTRIBUTES</A><DD>
<DT id="7"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="8"><A HREF="#lbAI">NOTES</A><DD>
<DT id="9"><A HREF="#lbAJ">SEE ALSO</A><DD>
<DT id="10"><A HREF="#lbAK">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:44 GMT, March 31, 2021
</BODY>
</HTML>