1 .\" Copyright (C) 2001 Andries Brouwer (aeb@cwi.nl)
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" Japanese Version Copyright (c) 2001 NAKANO Takeo all rights reserved.
24 .\" Translated Sat Dec 15 2001 by NAKANO Takeo <nakano@apm.seikei.ac.jp>
25 .\" Updated 2009-04-13, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v3.20
27 .TH GETCONTEXT 2 2009-03-15 "Linux" "Linux Programmer's Manual"
29 .\"O getcontext, setcontext \- get or set the user context
31 getcontext, setcontext \- ¥æ¡¼¥¶¥³¥ó¥Æ¥¥¹¥È¤ò¼èÆÀ/ÀßÄꤹ¤ë
34 .B #include <ucontext.h>
36 .BI "int getcontext(ucontext_t *" ucp );
38 .BI "int setcontext(const ucontext_t *" ucp );
41 .\"O In a System V-like environment, one has the two types
42 .\"O \fImcontext_t\fP and \fIucontext_t\fP defined in
44 .\"O and the four functions
45 .\"O .BR getcontext (),
46 .\"O .BR setcontext (),
47 .\"O .BR makecontext (3)
49 .\"O .BR swapcontext (3)
50 .\"O that allow user-level context switching between multiple
51 .\"O threads of control within a process.
52 System V Ū¤Ê´Ä¶¤Ç¤Ï¡¢
53 \fImcontext_t\fP ¤ª¤è¤Ó \fIucontext_t\fP ¤È¤¤¤¦ 2 ¤Ä¤Î·¿¤È¡¢
60 ¤ÇÄêµÁ¤µ¤ì¤Æ¤ª¤ê¡¢¤¢¤ë¥×¥í¥»¥¹ÆâÉô¤ÇÀ©¸æ²¼¤Ë¤¢¤ëÊ£¿ô¤Î¥¹¥ì¥Ã¥É´Ö¤Ç¡¢
61 ¥æ¡¼¥¶¥ì¥Ù¥ë¤Î¥³¥ó¥Æ¥¥¹¥ÈÀÚÂؤ¨¤¬¤Ç¤¤ë¤è¤¦¤Ë¤Ê¤Ã¤Æ¤¤¤ë¡£
63 .\"O The \fImcontext_t\fP type is machine-dependent and opaque.
64 .\"O The \fIucontext_t\fP type is a structure that has at least
65 .\"O the following fields:
66 \fImcontext_t\fP ·¿¤Ï¥Þ¥·¥ó°Í¸¤Ç¡¢³°Éô¤«¤é¤Ï±£Ê䵤ì¤Æ¤¤¤ë¡£
67 \fIucontext_t\fP ·¿¤Ï¹½Â¤ÂΤǡ¢¾¯¤Ê¤¯¤È¤â°Ê²¼¤Î 4 ¤Ä¤Î¥Õ¥£¡¼¥ë¥É¤ò»ý¤Ä¡£
71 typedef struct ucontext {
72 struct ucontext *uc_link;
75 mcontext_t uc_mcontext;
81 .\"O with \fIsigset_t\fP and \fIstack_t\fP defined in
83 .\"O Here \fIuc_link\fP points to the context that will be resumed
84 .\"O when the current context terminates (in case the current context
85 .\"O was created using
86 .\"O .BR makecontext (3)),
87 .\"O \fIuc_sigmask\fP is the
88 .\"O set of signals blocked in this context (see
89 .\"O .BR sigprocmask (2)),
90 .\"O \fIuc_stack\fP is the stack used by this context (see
91 .\"O .BR sigaltstack (2)),
92 .\"O and \fIuc_mcontext\fP is the
93 .\"O machine-specific representation of the saved context,
94 .\"O that includes the calling thread's machine registers.
95 \fIsigset_t\fP ¤È \fIstack_t\fP ¤Ï
98 ¤³¤³¤Ç \fIuc_link\fP ¤Ï¡¢
99 ¸½ºß¤Î¥³¥ó¥Æ¥¥¹¥È¤¬½ªÎ»¤·¤¿¤È¤¡¢
100 ³¤¤¤ÆÀÚ¤êÂؤï¤ë¥³¥ó¥Æ¥¥¹¥È¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë
101 (¸½ºß¤Î¥³¥ó¥Æ¥¥¹¥È¤¬
103 ¤ÇÀ¸À®¤µ¤ì¤¿¤â¤Î¤Î¾ì¹ç)¡£
104 \fIuc_sigmask\fP ¤Ï¤³¤Î¥³¥ó¥Æ¥¥¹¥È¤Ç¥Ö¥í¥Ã¥¯¤µ¤ì¤Æ¤¤¤ë
106 .RB ( sigprocmask (2)
108 \fIuc_stack\fP ¤Ï¤³¤Î¥³¥ó¥Æ¥¥¹¥È¤¬ÍѤ¤¤Æ¤¤¤ë¥¹¥¿¥Ã¥¯¤Ç¤¢¤ë
109 .RB ( signalstack (2)
111 \fIuc_mcontext\fP ¤ÏÊݸ¤µ¤ì¤Æ¤¤¤ë¥³¥ó¥Æ¥¥¹¥È¤Î
112 ¥Þ¥·¥óÆÃͤÎɽ¸½·Á¼°¤Ç¤¢¤ê¡¢
113 ¤³¤³¤Ë¤Ï¸Æ¤Ó½Ð¤·¤¿¥¹¥ì¥Ã¥É¤Î¥Þ¥·¥ó¥ì¥¸¥¹¥¿¤¬³ÊǼ¤µ¤ì¤ë¡£
116 .\"O .BR getcontext ()
117 .\"O initializes the structure
118 .\"O pointed at by \fIucp\fP to the currently active context.
121 ¥Ý¥¤¥ó¥¿ \fIucp\fP ¤¬»Ø¤¹¹½Â¤ÂΤò¡¢
122 ¸½ºß¥¢¥¯¥Æ¥£¥Ö¤Ê¥³¥ó¥Æ¥¥¹¥È¤Ë½é´ü²½¤¹¤ë¡£
125 .\"O .BR setcontext ()
126 .\"O restores the user context
127 .\"O pointed at by \fIucp\fP.
128 .\"O A successful call does not return.
129 .\"O The context should have been obtained by a call of
130 .\"O .BR getcontext (),
132 .\"O .BR makecontext (3),
133 .\"O or passed as third argument to a signal
137 \fIucp\fP ¤¬»Ø¤¹¥æ¡¼¥¶¥³¥ó¥Æ¥¥¹¥È¤ò¥ê¥¹¥È¥¢¤¹¤ë¡£
138 ¸Æ¤Ó½Ð¤·¤ËÀ®¸ù¤¹¤ë¤ÈÊÖ¤é¤Ê¤¤¡£
139 ¤³¤Î¥³¥ó¥Æ¥¥¹¥È¤Ï¡¢°ÊÁ°¤Ë
144 ¤¢¤ë¤¤¤Ï¥·¥°¥Ê¥ë¤ÎÂè»°°ú¿ô¤È¤·¤ÆÍ¿¤¨¤é¤ì¤¿¤â¤Î¤Ë¤Ê¤ë¡£
146 .\"O If the context was obtained by a call of
147 .\"O .BR getcontext (),
148 .\"O program execution continues as if this call just returned.
151 ¤Î¸Æ¤Ó½Ð¤·¤Ë¤è¤Ã¤ÆÆÀ¤é¤ì¤Æ¤¤¤¿¤â¤Î¤Î¾ì¹ç¤Ï¡¢
152 ¥×¥í¥°¥é¥à¤Ï¤³¤Î¸Æ¤Ó½Ð¤·¤«¤éÊ֤ä¿Ä¾¸å¤«¤é¤Î¤è¤¦¤Ë¼Â¹Ô¤ò·Ñ³¤¹¤ë¡£
154 .\"O If the context was obtained by a call of
155 .\"O .BR makecontext 3(),
156 .\"O program execution continues by a call to the function \fIfunc\fP
157 .\"O specified as the second argument of that call to
158 .\"O .BR makecontext (3).
159 .\"O When the function \fIfunc\fP returns, we continue with the
160 .\"O \fIuc_link\fP member of the structure \fIucp\fP specified as the
161 .\"O first argument of that call to
162 .\"O .BR makecontext (3).
163 .\"O When this member is NULL, the thread exits.
166 ¤Î¸Æ¤Ó½Ð¤·¤Ë¤è¤Ã¤ÆÆÀ¤é¤ì¤Æ¤¤¤¿¤â¤Î¤Î¾ì¹ç¤Ï¡¢
167 ¥×¥í¥°¥é¥à¤Î¼Â¹Ô¤Ï¤½¤Î
169 ¸Æ¤Ó½Ð¤·¤ÎÂèÆó°ú¿ô¤Ç»ØÄꤵ¤ì¤¿´Ø¿ô \fIfunc\fP
170 ¤ò¸Æ¤Ó½Ð¤¹¤«¤¿¤Á¤Ç·Ñ³¤¹¤ë¡£
171 \fIfunc\fP ¤«¤éÊÖ¤ë¤È¡¢
173 ¸Æ¤Ó½Ð¤·¤ÎÂè°ì°ú¿ô¤Ç»ØÄꤵ¤ì¤Æ¤¤¤¿
174 \fIucp\fP ¹½Â¤ÂΤΠ\fIuc_link\fP ¥á¥ó¥Ð¤Ç·Ñ³¤¹¤ë¡£
175 ¤³¤Î¥á¥ó¥Ð¤¬ NULL ¤À¤Ã¤¿¾ì¹ç¤Ï¡¢¤½¤Î¥¹¥ì¥Ã¥É¤Ï½ªÎ»¤¹¤ë¡£
177 .\"O If the context was obtained by a call to a signal handler,
178 .\"O then old standard text says that "program execution continues with the
179 .\"O program instruction following the instruction interrupted
181 .\"O However, this sentence was removed in SUSv2,
182 .\"O and the present verdict is "the result is unspecified".
183 ¥³¥ó¥Æ¥¥¹¥È¤¬¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤Î¸Æ¤Ó½Ð¤·¤Ë¤è¤Ã¤ÆÆÀ¤é¤ì¤Æ¤¤¤¿¤â¤Î¤Î¾ì¹ç¤Ï¡¢
185 ¡Ö¥×¥í¥°¥é¥à¤Î¼Â¹Ô¤Ï¥·¥°¥Ê¥ë¤Ë¤è¤Ã¤Æ³ä¤ê¹þ¤Þ¤ì¤¿Ì¿Îá¤Î¼¡¤ÎÌ¿Îᤫ¤é·Ñ³¤µ¤ì¤ë¡×¡£
186 ¤·¤«¤·¤³¤Îʸ¤Ï SUSv2 ¤Çºï½ü¤µ¤ì¤¿¤Î¤Ç¡¢
187 ¸½ºß¤ÎȽÃǤϡַë²Ì¤ÏÄêµÁ¤µ¤ì¤Æ¤¤¤Ê¤¤¡×¤Ç¤¢¤ë¡£
188 .\"O .SH "RETURN VALUE"
190 .\"O When successful,
191 .\"O .BR getcontext ()
193 .\"O .BR setcontext ()
194 .\"O does not return.
195 .\"O On error, both return \-1 and set \fIerrno\fP
202 ¼ºÇÔ¤¹¤ë¤È¡¢Î¾¼Ô¤È¤â \-1 ¤òÊÖ¤·¡¢\fIerrno\fP ¤ò¥¨¥é¡¼¤Ë±þ¤¸¤ÆÀßÄꤹ¤ë¡£
207 .\"O .SH "CONFORMING TO"
210 .\"O POSIX.1-2008 removes the specification of
211 .\"O .BR getcontext (),
212 .\"O citing portability issues, and
213 .\"O recommending that applications be rewritten to use POSIX threads instead.
214 POSIX.1-2008 ¤Ç¤Ï¡¢°Ü¿¢À¤ÎÌäÂ꤫¤é
217 Âå¤ï¤ê¤Ë¡¢¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤ò POSIX ¥¹¥ì¥Ã¥É¤ò»È¤Ã¤Æ½ñ¤Ä¾¤¹¤³¤È¤¬
221 .\"O The earliest incarnation of this mechanism was the
222 .\"O .BR setjmp (3)/ longjmp (3)
224 .\"O Since that does not define
225 .\"O the handling of the signal context, the next stage was the
226 .\"O .BR sigsetjmp (3)/ siglongjmp (3)
228 .\"O The present mechanism gives much more control.
229 .\"O On the other hand,
230 .\"O there is no easy way to detect whether a return from
231 .\"O .BR getcontext ()
232 .\"O is from the first call, or via a
233 .\"O .BR setcontext ()
235 .\"O The user has to invent her own bookkeeping device, and a register
236 .\"O variable won't do since registers are restored.
237 ¤³¤Î¥á¥«¥Ë¥º¥à¤ÎºÇ¸Å¤Î¼ÂÁõ¤Ï¡¢
238 .BR setjmp (3)/ longjmp (3)
240 ¤³¤ì¤é¤Ë¤Ï¥·¥°¥Ê¥ë¥³¥ó¥Æ¥¥¹¥È¤Î¼è¤ê°·¤¤¤¬ÄêµÁ¤µ¤ì¤Æ¤¤¤Ê¤«¤Ã¤¿¤Î¤Ç¡¢
242 .BR sigsetjmp (3)/ siglongjmp (3)
244 ¸½ºß¤Îµ¡¹½¤Ç¤Ï¤º¤Ã¤ÈºÙ¤«¤ÊÀ©¸æ¤¬¤Ç¤¤ë¡£
248 ¤³¤ì¤¬ºÇ½é¤Î¸Æ¤Ó½Ð¤·¤Ç¤¢¤Ã¤¿¤«¡¢
251 ¸Æ¤Ó½Ð¤·¤«¤é¤Î¤â¤Î¤Ç¤¢¤ë¤«¤ò
252 ¶èÊ̤¹¤ëÍưפÊÊýË¡¤¬¤Ê¤¯¤Ê¤Ã¤Æ¤·¤Þ¤Ã¤¿¡£
253 ¥æ¡¼¥¶¤Ï¡Ö¤·¤ª¤ê¡×µ¡¹½¤ò¼«Ê¬¤Çºî¤é¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
254 ¥ì¥¸¥¹¥¿ÊÑ¿ô¤Ï (¥ì¥¸¥¹¥¿¤Ï¥ê¥¹¥È¥¢¤µ¤ì¤Æ¤·¤Þ¤¦¤Î¤Ç) ¤³¤ì¤ò¤ä¤Ã¤Æ¤¯¤ì¤Ê¤¤¡£
256 .\"O When a signal occurs, the current user context is saved and
257 .\"O a new context is created by the kernel for the signal handler.
258 .\"O Do not leave the handler using
259 .\"O .BR longjmp (3):
260 .\"O it is undefined what would happen with contexts.
262 .\"O .BR siglongjmp (3)
264 .\"O .BR setcontext ()
266 ¥·¥°¥Ê¥ë¤¬È¯À¸¤¹¤ë¤È¡¢
267 ¸½ºß¤Î¥æ¡¼¥¶¥³¥ó¥Æ¥¥¹¥È¤ÏÊݸ¤µ¤ì¡¢
268 ¥·¥°¥Ê¥ë¥Ï¥ó¥É¥éÍѤΥ³¥ó¥Æ¥¥¹¥È¤¬¥«¡¼¥Í¥ë¤Ë¤è¤Ã¤ÆÀ¸À®¤µ¤ì¤ë¡£
272 ¤³¤Î´Ø¿ô¤Î¥³¥ó¥Æ¥¥¹¥È²¼¤Ç¤ÎÆ°ºî¤ÏÄêµÁ¤µ¤ì¤Æ¤¤¤Ê¤¤¡£