1 .\" Copyright (C) 2001 Andries Brouwer (aeb@cwi.nl)
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH GETCONTEXT 3 2020-12-21 "Linux" "Linux Programmer's Manual"
27 getcontext, setcontext \- get or set the user context
30 .B #include <ucontext.h>
32 .BI "int getcontext(ucontext_t *" ucp );
33 .BI "int setcontext(const ucontext_t *" ucp );
36 In a System V-like environment, one has the two types
42 and the four functions
48 that allow user-level context switching between multiple
49 threads of control within a process.
53 type is machine-dependent and opaque.
56 type is a structure that has at least
61 typedef struct ucontext_t {
62 struct ucontext_t *uc_link;
65 mcontext_t uc_mcontext;
79 points to the context that will be resumed
80 when the current context terminates (in case the current context
85 set of signals blocked in this context (see
88 is the stack used by this context (see
93 machine-specific representation of the saved context,
94 that includes the calling thread's machine registers.
98 initializes the structure
101 to the currently active context.
105 restores the user context
108 A successful call does not return.
109 The context should have been obtained by a call of
113 or received as the third argument to a signal
114 handler (see the discussion of the
119 If the context was obtained by a call of
121 program execution continues as if this call just returned.
123 If the context was obtained by a call of
125 program execution continues by a call to the function
127 specified as the second argument of that call to
131 returns, we continue with the
133 member of the structure
136 first argument of that call to
138 When this member is NULL, the thread exits.
140 If the context was obtained by a call to a signal handler,
141 then old standard text says that "program execution continues with the
142 program instruction following the instruction interrupted
144 However, this sentence was removed in SUSv2,
145 and the present verdict is "the result is unspecified".
152 On error, both return \-1 and set
154 to indicate the error.
158 For an explanation of the terms used in this section, see
166 Interface Attribute Value
170 T} Thread safety MT-Safe race:ucp
177 POSIX.1-2008 removes the specification of
179 citing portability issues, and
180 recommending that applications be rewritten to use POSIX threads instead.
182 The earliest incarnation of this mechanism was the
183 .BR setjmp (3)/ longjmp (3)
185 Since that does not define
186 the handling of the signal context, the next stage was the
187 .BR sigsetjmp (3)/ siglongjmp (3)
189 The present mechanism gives much more control.
191 there is no easy way to detect whether a return from
193 is from the first call, or via a
196 The user has to invent their own bookkeeping device, and a register
197 variable won't do since registers are restored.
199 When a signal occurs, the current user context is saved and
200 a new context is created by the kernel for the signal handler.
201 Do not leave the handler using
203 it is undefined what would happen with contexts.