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