1 MAKECONTEXT(3C) Standard C Library Functions MAKECONTEXT(3C)
2
3 NAME
4 makecontext, swapcontext - manipulate user contexts
5
6 SYNOPSIS
7 #include <ucontext.h>
8
9 void makecontext(ucontext_t *ucp, void (*func)(), int argc...);
10
11
12 int swapcontext(ucontext_t *restrict oucp,
13 const ucontext_t *restrict ucp);
14
15
16 DESCRIPTION
17 The makecontext() function modifies the context specified by ucp, which
18 has been initialized using getcontext(2). When this context is resumed
19 using swapcontext() or setcontext(2), execution continues by calling
20 the function func, passing it the arguments that follow argc in the
21 makecontext() call. The value of argc must match the number of pointer-
22 sized integer arguments passed to func, otherwise the behavior is
23 undefined.
24
25
26 Before a call is made to makecontext(), the context being modified
27 should have a stack allocated for it. The stack is assigned to the
28 context by initializing the uc_stack member.
29
30
31 The uc_link member is used to determine the context that will be
32 resumed when the context being modified by makecontext() returns. The
33 uc_link member should be initialized prior to the call to
34 makecontext(). If the uc_link member is initialized to NULL, the thread
35 executing func will exit when func returns. See pthread_exit(3C).
36
37
38 The swapcontext() function saves the current context in the context
39 structure pointed to by oucp and sets the context to the context
40 structure pointed to by ucp.
41
42
43 If the ucp or oucp argument points to an invalid address, the behavior
44 is undefined and errno may be set to EFAULT.
45
46 RETURN VALUES
47 On successful completion, swapcontext() returns 0. Otherwise, -1 is
48 returned and errno is set to indicate the error.
49
50 ERRORS
51 The swapcontext() function will fail if:
52
53 ENOMEM
54 The ucp argument does not have enough stack left to complete
55 the operation.
56
57
58
59 The swapcontext() function may fail if:
60
61 EFAULT
62 The ucp or oucp argument points to an invalid address.
63
64
65 EXAMPLES
66 Example 1 Alternate execution context on a stack whose memory was
67 allocated using mmap().
68
69 #include <stdio.h>
70 #include <ucontext.h>
71 #include <sys/mman.h>
72
73 void
74 assign(long a, int *b)
75 {
76 *b = (int)a;
77 }
78
79 int
80 main(int argc, char **argv)
81 {
82 ucontext_t uc, back;
83 size_t sz = 0x10000;
84 int value = 0;
85
86 getcontext(&uc);
87
88 uc.uc_stack.ss_sp = mmap(0, sz,
89 PROT_READ | PROT_WRITE | PROT_EXEC,
90 MAP_PRIVATE | MAP_ANON, -1, 0);
91 uc.uc_stack.ss_size = sz;
92 uc.uc_stack.ss_flags = 0;
93
94 uc.uc_link = &back;
95
96 makecontext(&uc, assign, 2, 100L, &value);
97 swapcontext(&back, &uc);
98
99 printf("done %d\n", value);
100
101 return (0);
102 }
103
104
105 USAGE
106 These functions are useful for implementing user-level context
107 switching between multiple threads of control within a process (co-
108 processing). More effective multiple threads of control can be obtained
109 by using native support for multithreading. See threads(7).
110
111 ATTRIBUTES
112 See attributes(7) for descriptions of the following attributes:
113
114
115
116
117 +--------------------+-----------------+
118 | ATTRIBUTE TYPE | ATTRIBUTE VALUE |
119 +--------------------+-----------------+
120 |Interface Stability | Standard |
121 +--------------------+-----------------+
122 |MT-Level | MT-Safe |
123 +--------------------+-----------------+
124
125 SEE ALSO
126 getcontext(2), mmap(2), sigaction(2), sigprocmask(2), pthread_exit(3C),
127 ucontext.h(3HEAD), attributes(7), standards(7), threads(7)
128
129 NOTES
130 The semantics of the uc_stack member of the ucontext_t structure have
131 changed as they apply to inputs to makecontext(). Prior to Solaris 10,
132 the ss_sp member of the uc_stack structure represented the high memory
133 address of the area reserved for the stack. The ss_sp member now
134 represents the base (low memory address), in keeping with other uses of
135 ss_sp.
136
137
138 This change in the meaning of ss_sp is now the default behavior. The
139 -D__MAKECONTEXT_V2_SOURCE compilation flag used in Solaris 9 update
140 releases to access this behavior is obsolete.
141
142
143 Binary compatibility has been preserved with releases prior to Solaris
144 10. Before recompiling, applications that use makecontext() must be
145 updated to reflect this behavior change. The example below demonstrates
146 a typical change that must be applied:
147
148 --- example1_s9.c Thu Oct 3 11:58:17 2002
149 +++ example1.c Thu Jun 27 13:28:16 2002
150 @@ -27,12 +27,9 @@
151 uc.uc_stack.ss_sp = mmap(0, sz,
152 PROT_READ | PROT_WRITE | PROT_EXEC,
153 MAP_PRIVATE | MAP_ANON, -1, 0);
154 - uc.uc_stack.ss_sp = (char *)uc.uc_stack.ss_sp + sz - 8;
155 uc.uc_stack.ss_size = sz;
156 uc.uc_stack.ss_flags = 0;
157
158 uc.uc_link = &back
159
160 makecontext(&uc, assign, 2, 100L, &value);
161
162
163 February 17, 2023 MAKECONTEXT(3C)
|
1 MAKECONTEXT(3C) Standard C Library Functions MAKECONTEXT(3C)
2
3 NAME
4 makecontext, swapcontext, swapcontext_extd - manipulate user contexts
5
6 SYNOPSIS
7 #include <ucontext.h>
8
9 void
10 makecontext(ucontext_t *ucp, void (*ifunc)(), int argc, ...);
11
12 int
13 swapcontext(ucontext_t *restrict oucp, const ucontext_t *restrict ucp);
14
15 int
16 swapcontext_extd(ucontext_t *restrict oucp, uint32_t flags,
17 const ucontext_t *restrict ucp);
18
19 DESCRIPTION
20 The makecontext() function modifies the context specified by ucp, which
21 has been initialized using getcontext(2) or getcontext_extd(2). When
22 this context is resumed using swapcontext(), swapcontext_extd(), or
23 setcontext(2), execution continues by calling the function func, passing
24 it the arguments that follow argc in the makecontext() call. The value
25 of argc must match the number of pointer-sized integer arguments passed
26 to func, otherwise the behavior is undefined.
27
28 Before a call is made to makecontext(), the context being modified should
29 have a stack allocated for it. The stack is assigned to the context by
30 initializing the uc_stack member.
31
32 The uc_link member is used to determine the context that will be resumed
33 when the context being modified by makecontext() returns. The uc_link
34 member should be initialized prior to the call to makecontext(). If the
35 uc_link member is initialized to NULL, the thread executing func will
36 exit when func returns. See pthread_exit(3C).
37
38 The swapcontext() function saves the current context in the context
39 structure pointed to by oucp and sets the context to the context
40 structure pointed to by ucp.
41
42 If the ucp or oucp argument points to an invalid address, the behavior is
43 undefined and errno may be set to EFAULT.
44
45 The swapcontext_extd() function is similar to swapcontext() except that
46 it performs a call to getcontext_extd(2) to get and save the current
47 context, passing the flags argument to getcontext_extd(2). Note, the
48 same constraints around the initialization of the ucontext_t that are
49 discussed in getcontext_extd(2) still apply. Mainly, the context must
50 either have originally come from ucontext_alloc(3C) or prior to its first
51 use been zeroed. See getcontext_extd(2) for more information.
52
53 RETURN VALUES
54 On successful completion, swapcontext() and swapcontext_extd() return 0.
55 Otherwise, -1 is returned and errno is set to indicate the error.
56
57 EXAMPLES
58 Example 1 Alternate execution context on a stack whose memory was
59 allocated using mmap().
60
61 #include <stdio.h>
62 #include <ucontext.h>
63 #include <sys/mman.h>
64
65 void
66 assign(long a, int *b)
67 {
68 *b = (int)a;
69 }
70
71 int
72 main(int argc, char **argv)
73 {
74 ucontext_t uc, back;
75 size_t sz = 0x10000;
76 int value = 0;
77
78 getcontext(&uc);
79
80 uc.uc_stack.ss_sp = mmap(0, sz,
81 PROT_READ | PROT_WRITE | PROT_EXEC,
82 MAP_PRIVATE | MAP_ANON, -1, 0);
83 uc.uc_stack.ss_size = sz;
84 uc.uc_stack.ss_flags = 0;
85
86 uc.uc_link = &back;
87
88 makecontext(&uc, assign, 2, 100L, &value);
89 swapcontext(&back, &uc);
90
91 printf("done %d\n", value);
92
93 return (0);
94 }
95
96 ERRORS
97 The swapcontext() and swapcontext_extd() function will fail if:
98
99 ENOMEM The ucp argument does not have enough stack left to
100 complete the operation.
101
102 The swapcontext() and swapcontext_extd() functions may fail if:
103
104 EFAULT The ucp or oucp argument points to an invalid address.
105
106 The swapcontext_extd() function may additionally fail if:
107
108 EINVAL The flags argument contains invalid values.
109
110 USAGE
111 These functions are useful for implementing user-level context switching
112 between multiple threads of control within a process (co-processing).
113 More effective multiple threads of control can be obtained by using
114 native support for multithreading. See threads(7).
115
116 INTERFACE STABILITY
117 Committed
118
119 MT-LEVEL
120 MT-Safe
121
122 SEE ALSO
123 getcontext(2), getcontext_extd(2), mmap(2), sigaction(2), sigprocmask(2),
124 pthread_exit(3C), ucontext_alloc(3C), ucontext.h(3HEAD), attributes(7),
125 standards(7), threads(7)
126
127 NOTES
128 The semantics of the uc_stack member of the ucontext_t structure have
129 changed as they apply to inputs to makecontext(). Prior to Solaris 10,
130 the ss_sp member of the uc_stack tructure represented the high memory
131 address of the area reserved for the stack. The ss_sp member now
132 represents the base (low memory address), in keeping with other uses of
133 ss_sp. This change in the meaning of ss_sp is the default behavior.
134
135 Binary compatibility has been preserved with releases prior to Solaris
136 10. Before recompiling, applications that use makecontext() must be
137 updated to reflect this behavior change. The example below demonstrates
138 a typical change that must be applied:
139
140 --- example1_s9.c Thu Oct 3 11:58:17 2002
141 +++ example1.c Thu Jun 27 13:28:16 2002
142 @@ -27,12 +27,9 @@
143 uc.uc_stack.ss_sp = mmap(0, sz,
144 PROT_READ | PROT_WRITE | PROT_EXEC,
145 MAP_PRIVATE | MAP_ANON, -1, 0);
146 - uc.uc_stack.ss_sp = (char *)uc.uc_stack.ss_sp + sz - 8;
147 uc.uc_stack.ss_size = sz;
148 uc.uc_stack.ss_flags = 0;
149
150 uc.uc_link = &back
151
152 makecontext(&uc, assign, 2, 100L, &value);
153
154 illumos March 20, 2023 illumos
|