1GETCONTEXT(3)              Linux Programmer's Manual             GETCONTEXT(3)
2
3
4

NAME

6       getcontext, setcontext - get or set the user context
7

SYNOPSIS

9       #include <ucontext.h>
10
11       int getcontext(ucontext_t *ucp);
12       int setcontext(const ucontext_t *ucp);
13

DESCRIPTION

15       In  a  System  V-like environment, one has the two types mcontext_t and
16       ucontext_t defined in <ucontext.h> and the four functions getcontext(),
17       setcontext(),  makecontext(3)  and swapcontext(3) that allow user-level
18       context switching between multiple threads of control within a process.
19
20       The mcontext_t type is machine-dependent and  opaque.   The  ucontext_t
21       type is a structure that has at least the following fields:
22
23           typedef struct ucontext {
24               struct ucontext *uc_link;
25               sigset_t         uc_sigmask;
26               stack_t          uc_stack;
27               mcontext_t       uc_mcontext;
28               ...
29           } ucontext_t;
30
31       with  sigset_t  and stack_t defined in <signal.h>.  Here uc_link points
32       to the context that will be resumed when the current context terminates
33       (in case the current context was created using makecontext(3)), uc_sig‐
34       mask is the set of  signals  blocked  in  this  context  (see  sigproc‐
35       mask(2)),  uc_stack  is  the  stack  used  by this context (see sigalt‐
36       stack(2)), and uc_mcontext is the  machine-specific  representation  of
37       the  saved  context,  that includes the calling thread's machine regis‐
38       ters.
39
40       The function getcontext() initializes the structure pointed at  by  ucp
41       to the currently active context.
42
43       The  function setcontext() restores the user context pointed at by ucp.
44       A successful call does  not  return.   The  context  should  have  been
45       obtained  by  a  call  of getcontext(), or makecontext(3), or passed as
46       third argument to a signal handler.
47
48       If the context was obtained by a call of getcontext(),  program  execu‐
49       tion continues as if this call just returned.
50
51       If the context was obtained by a call of makecontext(3), program execu‐
52       tion continues by a call to the function func specified as  the  second
53       argument  of  that  call  to  makecontext(3).   When  the function func
54       returns, we continue with the uc_link member of the structure ucp spec‐
55       ified  as the first argument of that call to makecontext(3).  When this
56       member is NULL, the thread exits.
57
58       If the context was obtained by a call to a  signal  handler,  then  old
59       standard  text  says that "program execution continues with the program
60       instruction following the instruction interrupted by the signal".  How‐
61       ever,  this  sentence  was removed in SUSv2, and the present verdict is
62       "the result is unspecified".
63

RETURN VALUE

65       When successful, getcontext()  returns  0  and  setcontext()  does  not
66       return.  On error, both return -1 and set errno appropriately.
67

ERRORS

69       None defined.
70

CONFORMING TO

72       SUSv2, POSIX.1-2001.  POSIX.1-2008 removes the specification of getcon‐
73       text(), citing portability issues, and recommending  that  applications
74       be rewritten to use POSIX threads instead.
75

NOTES

77       The earliest incarnation of this mechanism was the setjmp(3)/longjmp(3)
78       mechanism.  Since that does not define the handling of the signal  con‐
79       text,  the  next  stage  was  the sigsetjmp(3)/siglongjmp(3) pair.  The
80       present mechanism gives much more control.  On the other hand, there is
81       no  easy  way  to detect whether a return from getcontext() is from the
82       first call, or via a setcontext() call.  The user has to invent her own
83       bookkeeping  device,  and  a register variable won't do since registers
84       are restored.
85
86       When a signal occurs, the current user context is saved and a new  con‐
87       text is created by the kernel for the signal handler.  Do not leave the
88       handler using longjmp(3): it is undefined what would happen  with  con‐
89       texts.  Use siglongjmp(3) or setcontext() instead.
90

SEE ALSO

92       sigaction(2),   sigaltstack(2),  sigprocmask(2),  longjmp(3),  makecon‐
93       text(3), sigsetjmp(3)
94

COLOPHON

96       This page is part of release 3.53 of the Linux  man-pages  project.   A
97       description  of  the project, and information about reporting bugs, can
98       be found at http://www.kernel.org/doc/man-pages/.
99
100
101
102Linux                             2009-03-15                     GETCONTEXT(3)
Impressum