OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / man3 / getcontext.3
1 .\" Copyright (C) 2001 Andries Brouwer (aeb@cwi.nl)
2 .\"
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.
7 .\"
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.
12 .\"
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
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .TH GETCONTEXT 3 2014-04-08 "Linux" "Linux Programmer's Manual"
26 .SH NAME
27 getcontext, setcontext \- get or set the user context
28 .SH SYNOPSIS
29 .B #include <ucontext.h>
30 .sp
31 .BI "int getcontext(ucontext_t *" ucp );
32 .br
33 .BI "int setcontext(const ucontext_t *" ucp );
34 .SH DESCRIPTION
35 In a System V-like environment, one has the two types
36 .I mcontext_t
37 and
38 .I ucontext_t
39 defined in
40 .I <ucontext.h>
41 and the four functions
42 .BR getcontext (),
43 .BR setcontext (),
44 .BR makecontext (3),
45 and
46 .BR swapcontext (3)
47 that allow user-level context switching between multiple
48 threads of control within a process.
49 .LP
50 The
51 .I mcontext_t
52 type is machine-dependent and opaque.
53 The
54 .I ucontext_t
55 type is a structure that has at least
56 the following fields:
57 .in +4
58 .nf
59
60 typedef struct ucontext {
61     struct ucontext *uc_link;
62     sigset_t         uc_sigmask;
63     stack_t          uc_stack;
64     mcontext_t       uc_mcontext;
65     ...
66 } ucontext_t;
67
68 .fi
69 .in
70 with
71 .IR sigset_t
72 and
73 .I stack_t
74 defined in
75 .IR <signal.h> .
76 Here
77 .I uc_link
78 points to the context that will be resumed
79 when the current context terminates (in case the current context
80 was created using
81 .BR makecontext (3)),
82 .I uc_sigmask
83 is the
84 set of signals blocked in this context (see
85 .BR sigprocmask (2)),
86 .I uc_stack
87 is the stack used by this context (see
88 .BR sigaltstack (2)),
89 and
90 .I uc_mcontext
91 is the
92 machine-specific representation of the saved context,
93 that includes the calling thread's machine registers.
94 .LP
95 The function
96 .BR getcontext ()
97 initializes the structure
98 pointed at by
99 .I ucp
100 to the currently active context.
101 .LP
102 The function
103 .BR setcontext ()
104 restores the user context
105 pointed at by
106 .IR ucp .
107 A successful call does not return.
108 The context should have been obtained by a call of
109 .BR getcontext (),
110 or
111 .BR makecontext (3),
112 or passed as third argument to a signal
113 handler.
114 .LP
115 If the context was obtained by a call of
116 .BR getcontext (),
117 program execution continues as if this call just returned.
118 .LP
119 If the context was obtained by a call of
120 .BR makecontext (3),
121 program execution continues by a call to the function
122 .I func
123 specified as the second argument of that call to
124 .BR makecontext (3).
125 When the function
126 .I func
127 returns, we continue with the
128 .I uc_link
129 member of the structure
130 .I ucp
131 specified as the
132 first argument of that call to
133 .BR makecontext (3).
134 When this member is NULL, the thread exits.
135 .LP
136 If the context was obtained by a call to a signal handler,
137 then old standard text says that "program execution continues with the
138 program instruction following the instruction interrupted
139 by the signal".
140 However, this sentence was removed in SUSv2,
141 and the present verdict is "the result is unspecified".
142 .SH RETURN VALUE
143 When successful,
144 .BR getcontext ()
145 returns 0 and
146 .BR setcontext ()
147 does not return.
148 On error, both return \-1 and set
149 .I errno
150 appropriately.
151 .SH ERRORS
152 None defined.
153 .SH ATTRIBUTES
154 .SS Multithreading (see pthreads(7))
155 The
156 .BR getcontext ()
157 and
158 .BR setcontext ()
159 functions are thread-safe.
160 .SH CONFORMING TO
161 SUSv2, POSIX.1-2001.
162 POSIX.1-2008 removes the specification of
163 .BR getcontext (),
164 citing portability issues, and
165 recommending that applications be rewritten to use POSIX threads instead.
166 .SH NOTES
167 The earliest incarnation of this mechanism was the
168 .BR setjmp (3)/ longjmp (3)
169 mechanism.
170 Since that does not define
171 the handling of the signal context, the next stage was the
172 .BR sigsetjmp (3)/ siglongjmp (3)
173 pair.
174 The present mechanism gives much more control.
175 On the other hand,
176 there is no easy way to detect whether a return from
177 .BR getcontext ()
178 is from the first call, or via a
179 .BR setcontext ()
180 call.
181 The user has to invent her own bookkeeping device, and a register
182 variable won't do since registers are restored.
183 .LP
184 When a signal occurs, the current user context is saved and
185 a new context is created by the kernel for the signal handler.
186 Do not leave the handler using
187 .BR longjmp (3):
188 it is undefined what would happen with contexts.
189 Use
190 .BR siglongjmp (3)
191 or
192 .BR setcontext ()
193 instead.
194 .SH SEE ALSO
195 .BR sigaction (2),
196 .BR sigaltstack (2),
197 .BR sigprocmask (2),
198 .BR longjmp (3),
199 .BR makecontext (3),
200 .BR sigsetjmp (3)
201 .SH COLOPHON
202 This page is part of release 3.79 of the Linux
203 .I man-pages
204 project.
205 A description of the project,
206 information about reporting bugs,
207 and the latest version of this page,
208 can be found at
209 \%http://www.kernel.org/doc/man\-pages/.