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