1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
'\" t
.\" Copyright (C) 2001 Andries Brouwer (aeb@cwi.nl)
.\"
.\" SPDX-License-Identifier: Linux-man-pages-copyleft
.\"
.TH getcontext 3 2024-05-02 "Linux man-pages 6.8"
.SH NAME
getcontext, setcontext \- get or set the user context
.SH LIBRARY
Standard C library
.RI ( libc ", " \-lc )
.SH SYNOPSIS
.nf
.B #include <ucontext.h>
.P
.BI "int getcontext(ucontext_t *" ucp );
.BI "int setcontext(const ucontext_t *" ucp );
.fi
.SH DESCRIPTION
In a System V-like environment, one has the two types
.I mcontext_t
and
.I ucontext_t
defined in
.I <ucontext.h>
and the four functions
.BR getcontext (),
.BR setcontext (),
.BR makecontext (3),
and
.BR swapcontext (3)
that allow user-level context switching between multiple
threads of control within a process.
.P
The
.I mcontext_t
type is machine-dependent and opaque.
The
.I ucontext_t
type is a structure that has at least
the following fields:
.P
.in +4n
.EX
typedef struct ucontext_t {
struct ucontext_t *uc_link;
sigset_t uc_sigmask;
stack_t uc_stack;
mcontext_t uc_mcontext;
...
} ucontext_t;
.EE
.in
.P
with
.I sigset_t
and
.I stack_t
defined in
.IR <signal.h> .
Here
.I uc_link
points to the context that will be resumed
when the current context terminates (in case the current context
was created using
.BR makecontext (3)),
.I uc_sigmask
is the
set of signals blocked in this context (see
.BR sigprocmask (2)),
.I uc_stack
is the stack used by this context (see
.BR sigaltstack (2)),
and
.I uc_mcontext
is the
machine-specific representation of the saved context,
that includes the calling thread's machine registers.
.P
The function
.BR getcontext ()
initializes the structure
pointed to by
.I ucp
to the currently active context.
.P
The function
.BR setcontext ()
restores the user context
pointed to by
.IR ucp .
A successful call does not return.
The context should have been obtained by a call of
.BR getcontext (),
or
.BR makecontext (3),
or received as the third argument to a signal
handler (see the discussion of the
.B SA_SIGINFO
flag in
.BR sigaction (2)).
.P
If the context was obtained by a call of
.BR getcontext (),
program execution continues as if this call just returned.
.P
If the context was obtained by a call of
.BR makecontext (3),
program execution continues by a call to the function
.I func
specified as the second argument of that call to
.BR makecontext (3).
When the function
.I func
returns, we continue with the
.I uc_link
member of the structure
.I ucp
specified as the
first argument of that call to
.BR makecontext (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 "program execution continues with the
program instruction following the instruction interrupted
by the signal".
However, this sentence was removed in SUSv2,
and the present verdict is "the result is unspecified".
.SH RETURN VALUE
When successful,
.BR getcontext ()
returns 0 and
.BR setcontext ()
does not return.
On error, both return \-1 and set
.I errno
to indicate the error.
.SH ERRORS
None defined.
.SH ATTRIBUTES
For an explanation of the terms used in this section, see
.BR attributes (7).
.TS
allbox;
lbx lb lb
l l l.
Interface Attribute Value
T{
.na
.nh
.BR getcontext (),
.BR setcontext ()
T} Thread safety MT-Safe race:ucp
.TE
.SH STANDARDS
None.
.SH HISTORY
SUSv2, POSIX.1-2001.
.P
POSIX.1-2008 removes these functions,
citing portability issues, and
recommending that applications be rewritten to use POSIX threads instead.
.SH NOTES
The earliest incarnation of this mechanism was the
.BR setjmp (3)/\c
.BR longjmp (3)
mechanism.
Since that does not define
the handling of the signal context, the next stage was the
.BR sigsetjmp (3)/\c
.BR siglongjmp (3)
pair.
The present mechanism gives much more control.
On the other hand,
there is no easy way to detect whether a return from
.BR getcontext ()
is from the first call, or via a
.BR setcontext ()
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
.BR longjmp (3):
it is undefined what would happen with contexts.
Use
.BR siglongjmp (3)
or
.BR setcontext ()
instead.
.SH SEE ALSO
.BR sigaction (2),
.BR sigaltstack (2),
.BR sigprocmask (2),
.BR longjmp (3),
.BR makecontext (3),
.BR sigsetjmp (3),
.BR signal (7)
|