1GETCONTEXT(3) Linux Programmer's Manual GETCONTEXT(3)
2
3
4
6 getcontext, setcontext - get or set the user context
7
9 #include <ucontext.h>
10
11 int getcontext(ucontext_t *ucp);
12 int setcontext(const ucontext_t *ucp);
13
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 to by ucp
41 to the currently active context.
42
43 The function setcontext() restores the user context pointed to by ucp.
44 A successful call does not return. The context should have been ob‐
45 tained by a call of getcontext(), or makecontext(3), or received as the
46 third argument to a signal handler (see the discussion of the SA_SIG‐
47 INFO flag in sigaction(2)).
48
49 If the context was obtained by a call of getcontext(), program execu‐
50 tion continues as if this call just returned.
51
52 If the context was obtained by a call of makecontext(3), program execu‐
53 tion continues by a call to the function func specified as the second
54 argument of that call to makecontext(3). When the function func re‐
55 turns, we continue with the uc_link member of the structure ucp speci‐
56 fied as the first argument of that call to makecontext(3). When this
57 member is NULL, the thread exits.
58
59 If the context was obtained by a call to a signal handler, then old
60 standard text says that "program execution continues with the program
61 instruction following the instruction interrupted by the signal". How‐
62 ever, this sentence was removed in SUSv2, and the present verdict is
63 "the result is unspecified".
64
66 When successful, getcontext() returns 0 and setcontext() does not re‐
67 turn. On error, both return -1 and set errno appropriately.
68
70 None defined.
71
73 For an explanation of the terms used in this section, see at‐
74 tributes(7).
75
76 ┌───────────────────────────┬───────────────┬──────────────────┐
77 │Interface │ Attribute │ Value │
78 ├───────────────────────────┼───────────────┼──────────────────┤
79 │getcontext(), setcontext() │ Thread safety │ MT-Safe race:ucp │
80 └───────────────────────────┴───────────────┴──────────────────┘
82 SUSv2, POSIX.1-2001. POSIX.1-2008 removes the specification of getcon‐
83 text(), citing portability issues, and recommending that applications
84 be rewritten to use POSIX threads instead.
85
87 The earliest incarnation of this mechanism was the setjmp(3)/longjmp(3)
88 mechanism. Since that does not define the handling of the signal con‐
89 text, the next stage was the sigsetjmp(3)/siglongjmp(3) pair. The
90 present mechanism gives much more control. On the other hand, there is
91 no easy way to detect whether a return from getcontext() is from the
92 first call, or via a setcontext() call. The user has to invent their
93 own bookkeeping device, and a register variable won't do since regis‐
94 ters are restored.
95
96 When a signal occurs, the current user context is saved and a new con‐
97 text is created by the kernel for the signal handler. Do not leave the
98 handler using longjmp(3): it is undefined what would happen with con‐
99 texts. Use siglongjmp(3) or setcontext() instead.
100
102 sigaction(2), sigaltstack(2), sigprocmask(2), longjmp(3), makecon‐
103 text(3), sigsetjmp(3), signal(7)
104
106 This page is part of release 5.10 of the Linux man-pages project. A
107 description of the project, information about reporting bugs, and the
108 latest version of this page, can be found at
109 https://www.kernel.org/doc/man-pages/.
110
111
112
113Linux 2020-12-21 GETCONTEXT(3)