root / coroutine-ucontext.c @ c48c6522
History | View | Annotate | Download (5.7 kB)
1 | 00dccaf1 | Kevin Wolf | /*
|
---|---|---|---|
2 | 00dccaf1 | Kevin Wolf | * ucontext coroutine initialization code
|
3 | 00dccaf1 | Kevin Wolf | *
|
4 | 00dccaf1 | Kevin Wolf | * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
|
5 | 00dccaf1 | Kevin Wolf | * Copyright (C) 2011 Kevin Wolf <kwolf@redhat.com>
|
6 | 00dccaf1 | Kevin Wolf | *
|
7 | 00dccaf1 | Kevin Wolf | * This library is free software; you can redistribute it and/or
|
8 | 00dccaf1 | Kevin Wolf | * modify it under the terms of the GNU Lesser General Public
|
9 | 00dccaf1 | Kevin Wolf | * License as published by the Free Software Foundation; either
|
10 | 00dccaf1 | Kevin Wolf | * version 2.0 of the License, or (at your option) any later version.
|
11 | 00dccaf1 | Kevin Wolf | *
|
12 | 00dccaf1 | Kevin Wolf | * This library is distributed in the hope that it will be useful,
|
13 | 00dccaf1 | Kevin Wolf | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 | 00dccaf1 | Kevin Wolf | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
15 | 00dccaf1 | Kevin Wolf | * Lesser General Public License for more details.
|
16 | 00dccaf1 | Kevin Wolf | *
|
17 | 00dccaf1 | Kevin Wolf | * You should have received a copy of the GNU Lesser General Public
|
18 | 00dccaf1 | Kevin Wolf | * License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
19 | 00dccaf1 | Kevin Wolf | */
|
20 | 00dccaf1 | Kevin Wolf | |
21 | 00dccaf1 | Kevin Wolf | /* XXX Is there a nicer way to disable glibc's stack check for longjmp? */
|
22 | 00dccaf1 | Kevin Wolf | #ifdef _FORTIFY_SOURCE
|
23 | 00dccaf1 | Kevin Wolf | #undef _FORTIFY_SOURCE
|
24 | 00dccaf1 | Kevin Wolf | #endif
|
25 | 00dccaf1 | Kevin Wolf | #include <stdlib.h> |
26 | 00dccaf1 | Kevin Wolf | #include <setjmp.h> |
27 | 00dccaf1 | Kevin Wolf | #include <stdint.h> |
28 | 00dccaf1 | Kevin Wolf | #include <pthread.h> |
29 | 00dccaf1 | Kevin Wolf | #include <ucontext.h> |
30 | 00dccaf1 | Kevin Wolf | #include "qemu-common.h" |
31 | 00dccaf1 | Kevin Wolf | #include "qemu-coroutine-int.h" |
32 | 00dccaf1 | Kevin Wolf | |
33 | 00dccaf1 | Kevin Wolf | enum {
|
34 | 00dccaf1 | Kevin Wolf | /* Maximum free pool size prevents holding too many freed coroutines */
|
35 | 00dccaf1 | Kevin Wolf | POOL_MAX_SIZE = 64,
|
36 | 00dccaf1 | Kevin Wolf | }; |
37 | 00dccaf1 | Kevin Wolf | |
38 | 39a7a362 | Avi Kivity | /** Free list to speed up creation */
|
39 | 1bbbdabd | Paolo Bonzini | static QSLIST_HEAD(, Coroutine) pool = QSLIST_HEAD_INITIALIZER(pool);
|
40 | 39a7a362 | Avi Kivity | static unsigned int pool_size; |
41 | 39a7a362 | Avi Kivity | |
42 | 00dccaf1 | Kevin Wolf | typedef struct { |
43 | 00dccaf1 | Kevin Wolf | Coroutine base; |
44 | 00dccaf1 | Kevin Wolf | void *stack;
|
45 | 00dccaf1 | Kevin Wolf | jmp_buf env; |
46 | 00dccaf1 | Kevin Wolf | } CoroutineUContext; |
47 | 00dccaf1 | Kevin Wolf | |
48 | 00dccaf1 | Kevin Wolf | /**
|
49 | 00dccaf1 | Kevin Wolf | * Per-thread coroutine bookkeeping
|
50 | 00dccaf1 | Kevin Wolf | */
|
51 | 00dccaf1 | Kevin Wolf | typedef struct { |
52 | 00dccaf1 | Kevin Wolf | /** Currently executing coroutine */
|
53 | 00dccaf1 | Kevin Wolf | Coroutine *current; |
54 | 00dccaf1 | Kevin Wolf | |
55 | 00dccaf1 | Kevin Wolf | /** The default coroutine */
|
56 | 00dccaf1 | Kevin Wolf | CoroutineUContext leader; |
57 | 00dccaf1 | Kevin Wolf | } CoroutineThreadState; |
58 | 00dccaf1 | Kevin Wolf | |
59 | 00dccaf1 | Kevin Wolf | static pthread_key_t thread_state_key;
|
60 | 00dccaf1 | Kevin Wolf | |
61 | 00dccaf1 | Kevin Wolf | /*
|
62 | 00dccaf1 | Kevin Wolf | * va_args to makecontext() must be type 'int', so passing
|
63 | 00dccaf1 | Kevin Wolf | * the pointer we need may require several int args. This
|
64 | 00dccaf1 | Kevin Wolf | * union is a quick hack to let us do that
|
65 | 00dccaf1 | Kevin Wolf | */
|
66 | 00dccaf1 | Kevin Wolf | union cc_arg {
|
67 | 00dccaf1 | Kevin Wolf | void *p;
|
68 | 00dccaf1 | Kevin Wolf | int i[2]; |
69 | 00dccaf1 | Kevin Wolf | }; |
70 | 00dccaf1 | Kevin Wolf | |
71 | 00dccaf1 | Kevin Wolf | static CoroutineThreadState *coroutine_get_thread_state(void) |
72 | 00dccaf1 | Kevin Wolf | { |
73 | 00dccaf1 | Kevin Wolf | CoroutineThreadState *s = pthread_getspecific(thread_state_key); |
74 | 00dccaf1 | Kevin Wolf | |
75 | 00dccaf1 | Kevin Wolf | if (!s) {
|
76 | 7267c094 | Anthony Liguori | s = g_malloc0(sizeof(*s));
|
77 | 00dccaf1 | Kevin Wolf | s->current = &s->leader.base; |
78 | 00dccaf1 | Kevin Wolf | pthread_setspecific(thread_state_key, s); |
79 | 00dccaf1 | Kevin Wolf | } |
80 | 00dccaf1 | Kevin Wolf | return s;
|
81 | 00dccaf1 | Kevin Wolf | } |
82 | 00dccaf1 | Kevin Wolf | |
83 | 00dccaf1 | Kevin Wolf | static void qemu_coroutine_thread_cleanup(void *opaque) |
84 | 00dccaf1 | Kevin Wolf | { |
85 | 00dccaf1 | Kevin Wolf | CoroutineThreadState *s = opaque; |
86 | 39a7a362 | Avi Kivity | |
87 | 39a7a362 | Avi Kivity | g_free(s); |
88 | 39a7a362 | Avi Kivity | } |
89 | 39a7a362 | Avi Kivity | |
90 | 39a7a362 | Avi Kivity | static void __attribute__((destructor)) coroutine_cleanup(void) |
91 | 39a7a362 | Avi Kivity | { |
92 | 00dccaf1 | Kevin Wolf | Coroutine *co; |
93 | 00dccaf1 | Kevin Wolf | Coroutine *tmp; |
94 | 00dccaf1 | Kevin Wolf | |
95 | 1bbbdabd | Paolo Bonzini | QSLIST_FOREACH_SAFE(co, &pool, pool_next, tmp) { |
96 | 7267c094 | Anthony Liguori | g_free(DO_UPCAST(CoroutineUContext, base, co)->stack); |
97 | 7267c094 | Anthony Liguori | g_free(co); |
98 | 00dccaf1 | Kevin Wolf | } |
99 | 00dccaf1 | Kevin Wolf | } |
100 | 00dccaf1 | Kevin Wolf | |
101 | 00dccaf1 | Kevin Wolf | static void __attribute__((constructor)) coroutine_init(void) |
102 | 00dccaf1 | Kevin Wolf | { |
103 | 00dccaf1 | Kevin Wolf | int ret;
|
104 | 00dccaf1 | Kevin Wolf | |
105 | 00dccaf1 | Kevin Wolf | ret = pthread_key_create(&thread_state_key, qemu_coroutine_thread_cleanup); |
106 | 00dccaf1 | Kevin Wolf | if (ret != 0) { |
107 | 00dccaf1 | Kevin Wolf | fprintf(stderr, "unable to create leader key: %s\n", strerror(errno));
|
108 | 00dccaf1 | Kevin Wolf | abort(); |
109 | 00dccaf1 | Kevin Wolf | } |
110 | 00dccaf1 | Kevin Wolf | } |
111 | 00dccaf1 | Kevin Wolf | |
112 | 00dccaf1 | Kevin Wolf | static void coroutine_trampoline(int i0, int i1) |
113 | 00dccaf1 | Kevin Wolf | { |
114 | 00dccaf1 | Kevin Wolf | union cc_arg arg;
|
115 | 00dccaf1 | Kevin Wolf | CoroutineUContext *self; |
116 | 00dccaf1 | Kevin Wolf | Coroutine *co; |
117 | 00dccaf1 | Kevin Wolf | |
118 | 00dccaf1 | Kevin Wolf | arg.i[0] = i0;
|
119 | 00dccaf1 | Kevin Wolf | arg.i[1] = i1;
|
120 | 00dccaf1 | Kevin Wolf | self = arg.p; |
121 | 00dccaf1 | Kevin Wolf | co = &self->base; |
122 | 00dccaf1 | Kevin Wolf | |
123 | 00dccaf1 | Kevin Wolf | /* Initialize longjmp environment and switch back the caller */
|
124 | 00dccaf1 | Kevin Wolf | if (!setjmp(self->env)) {
|
125 | 00dccaf1 | Kevin Wolf | longjmp(*(jmp_buf *)co->entry_arg, 1);
|
126 | 00dccaf1 | Kevin Wolf | } |
127 | 00dccaf1 | Kevin Wolf | |
128 | 00dccaf1 | Kevin Wolf | while (true) { |
129 | 00dccaf1 | Kevin Wolf | co->entry(co->entry_arg); |
130 | 00dccaf1 | Kevin Wolf | qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE); |
131 | 00dccaf1 | Kevin Wolf | } |
132 | 00dccaf1 | Kevin Wolf | } |
133 | 00dccaf1 | Kevin Wolf | |
134 | 00dccaf1 | Kevin Wolf | static Coroutine *coroutine_new(void) |
135 | 00dccaf1 | Kevin Wolf | { |
136 | 00dccaf1 | Kevin Wolf | const size_t stack_size = 1 << 20; |
137 | 00dccaf1 | Kevin Wolf | CoroutineUContext *co; |
138 | 00dccaf1 | Kevin Wolf | ucontext_t old_uc, uc; |
139 | 00dccaf1 | Kevin Wolf | jmp_buf old_env; |
140 | 32b74677 | malc | union cc_arg arg = {0}; |
141 | 00dccaf1 | Kevin Wolf | |
142 | 00dccaf1 | Kevin Wolf | /* The ucontext functions preserve signal masks which incurs a system call
|
143 | 00dccaf1 | Kevin Wolf | * overhead. setjmp()/longjmp() does not preserve signal masks but only
|
144 | 00dccaf1 | Kevin Wolf | * works on the current stack. Since we need a way to create and switch to
|
145 | 00dccaf1 | Kevin Wolf | * a new stack, use the ucontext functions for that but setjmp()/longjmp()
|
146 | 00dccaf1 | Kevin Wolf | * for everything else.
|
147 | 00dccaf1 | Kevin Wolf | */
|
148 | 00dccaf1 | Kevin Wolf | |
149 | 00dccaf1 | Kevin Wolf | if (getcontext(&uc) == -1) { |
150 | 00dccaf1 | Kevin Wolf | abort(); |
151 | 00dccaf1 | Kevin Wolf | } |
152 | 00dccaf1 | Kevin Wolf | |
153 | 7267c094 | Anthony Liguori | co = g_malloc0(sizeof(*co));
|
154 | 7267c094 | Anthony Liguori | co->stack = g_malloc(stack_size); |
155 | 00dccaf1 | Kevin Wolf | co->base.entry_arg = &old_env; /* stash away our jmp_buf */
|
156 | 00dccaf1 | Kevin Wolf | |
157 | 00dccaf1 | Kevin Wolf | uc.uc_link = &old_uc; |
158 | 00dccaf1 | Kevin Wolf | uc.uc_stack.ss_sp = co->stack; |
159 | 00dccaf1 | Kevin Wolf | uc.uc_stack.ss_size = stack_size; |
160 | 00dccaf1 | Kevin Wolf | uc.uc_stack.ss_flags = 0;
|
161 | 00dccaf1 | Kevin Wolf | |
162 | 00dccaf1 | Kevin Wolf | arg.p = co; |
163 | 00dccaf1 | Kevin Wolf | |
164 | 00dccaf1 | Kevin Wolf | makecontext(&uc, (void (*)(void))coroutine_trampoline, |
165 | 00dccaf1 | Kevin Wolf | 2, arg.i[0], arg.i[1]); |
166 | 00dccaf1 | Kevin Wolf | |
167 | 00dccaf1 | Kevin Wolf | /* swapcontext() in, longjmp() back out */
|
168 | 00dccaf1 | Kevin Wolf | if (!setjmp(old_env)) {
|
169 | 00dccaf1 | Kevin Wolf | swapcontext(&old_uc, &uc); |
170 | 00dccaf1 | Kevin Wolf | } |
171 | 00dccaf1 | Kevin Wolf | return &co->base;
|
172 | 00dccaf1 | Kevin Wolf | } |
173 | 00dccaf1 | Kevin Wolf | |
174 | 00dccaf1 | Kevin Wolf | Coroutine *qemu_coroutine_new(void)
|
175 | 00dccaf1 | Kevin Wolf | { |
176 | 00dccaf1 | Kevin Wolf | Coroutine *co; |
177 | 00dccaf1 | Kevin Wolf | |
178 | 1bbbdabd | Paolo Bonzini | co = QSLIST_FIRST(&pool); |
179 | 00dccaf1 | Kevin Wolf | if (co) {
|
180 | 1bbbdabd | Paolo Bonzini | QSLIST_REMOVE_HEAD(&pool, pool_next); |
181 | 39a7a362 | Avi Kivity | pool_size--; |
182 | 00dccaf1 | Kevin Wolf | } else {
|
183 | 00dccaf1 | Kevin Wolf | co = coroutine_new(); |
184 | 00dccaf1 | Kevin Wolf | } |
185 | 00dccaf1 | Kevin Wolf | return co;
|
186 | 00dccaf1 | Kevin Wolf | } |
187 | 00dccaf1 | Kevin Wolf | |
188 | 00dccaf1 | Kevin Wolf | void qemu_coroutine_delete(Coroutine *co_)
|
189 | 00dccaf1 | Kevin Wolf | { |
190 | 00dccaf1 | Kevin Wolf | CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_); |
191 | 00dccaf1 | Kevin Wolf | |
192 | 39a7a362 | Avi Kivity | if (pool_size < POOL_MAX_SIZE) {
|
193 | 1bbbdabd | Paolo Bonzini | QSLIST_INSERT_HEAD(&pool, &co->base, pool_next); |
194 | 00dccaf1 | Kevin Wolf | co->base.caller = NULL;
|
195 | 39a7a362 | Avi Kivity | pool_size++; |
196 | 00dccaf1 | Kevin Wolf | return;
|
197 | 00dccaf1 | Kevin Wolf | } |
198 | 00dccaf1 | Kevin Wolf | |
199 | 7267c094 | Anthony Liguori | g_free(co->stack); |
200 | 7267c094 | Anthony Liguori | g_free(co); |
201 | 00dccaf1 | Kevin Wolf | } |
202 | 00dccaf1 | Kevin Wolf | |
203 | 00dccaf1 | Kevin Wolf | CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_, |
204 | 00dccaf1 | Kevin Wolf | CoroutineAction action) |
205 | 00dccaf1 | Kevin Wolf | { |
206 | 00dccaf1 | Kevin Wolf | CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_); |
207 | 00dccaf1 | Kevin Wolf | CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_); |
208 | 00dccaf1 | Kevin Wolf | CoroutineThreadState *s = coroutine_get_thread_state(); |
209 | 00dccaf1 | Kevin Wolf | int ret;
|
210 | 00dccaf1 | Kevin Wolf | |
211 | 00dccaf1 | Kevin Wolf | s->current = to_; |
212 | 00dccaf1 | Kevin Wolf | |
213 | 00dccaf1 | Kevin Wolf | ret = setjmp(from->env); |
214 | 00dccaf1 | Kevin Wolf | if (ret == 0) { |
215 | 00dccaf1 | Kevin Wolf | longjmp(to->env, action); |
216 | 00dccaf1 | Kevin Wolf | } |
217 | 00dccaf1 | Kevin Wolf | return ret;
|
218 | 00dccaf1 | Kevin Wolf | } |
219 | 00dccaf1 | Kevin Wolf | |
220 | 00dccaf1 | Kevin Wolf | Coroutine *qemu_coroutine_self(void)
|
221 | 00dccaf1 | Kevin Wolf | { |
222 | 00dccaf1 | Kevin Wolf | CoroutineThreadState *s = coroutine_get_thread_state(); |
223 | 00dccaf1 | Kevin Wolf | |
224 | 00dccaf1 | Kevin Wolf | return s->current;
|
225 | 00dccaf1 | Kevin Wolf | } |
226 | 00dccaf1 | Kevin Wolf | |
227 | 00dccaf1 | Kevin Wolf | bool qemu_in_coroutine(void) |
228 | 00dccaf1 | Kevin Wolf | { |
229 | 00dccaf1 | Kevin Wolf | CoroutineThreadState *s = pthread_getspecific(thread_state_key); |
230 | 00dccaf1 | Kevin Wolf | |
231 | 00dccaf1 | Kevin Wolf | return s && s->current->caller;
|
232 | 00dccaf1 | Kevin Wolf | } |