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_t {
24               struct ucontext_t *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

ATTRIBUTES

72       For   an   explanation   of   the  terms  used  in  this  section,  see
73       attributes(7).
74
75       ┌───────────────────────────┬───────────────┬──────────────────┐
76Interface                  Attribute     Value            
77       ├───────────────────────────┼───────────────┼──────────────────┤
78getcontext(), setcontext() │ Thread safety │ MT-Safe race:ucp │
79       └───────────────────────────┴───────────────┴──────────────────┘

CONFORMING TO

81       SUSv2, POSIX.1-2001.  POSIX.1-2008 removes the specification of getcon‐
82       text(),  citing  portability issues, and recommending that applications
83       be rewritten to use POSIX threads instead.
84

NOTES

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

SEE ALSO

101       sigaction(2),  sigaltstack(2),  sigprocmask(2),  longjmp(3),   makecon‐
102       text(3), sigsetjmp(3)
103

COLOPHON

105       This  page  is  part of release 5.04 of the Linux man-pages project.  A
106       description of the project, information about reporting bugs,  and  the
107       latest     version     of     this    page,    can    be    found    at
108       https://www.kernel.org/doc/man-pages/.
109
110
111
112Linux                             2017-09-15                     GETCONTEXT(3)
Impressum