Statistics
| Branch: | Revision:

root / coroutine-ucontext.c @ a8a00822

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 00dccaf1 Kevin Wolf
typedef struct {
39 00dccaf1 Kevin Wolf
    Coroutine base;
40 00dccaf1 Kevin Wolf
    void *stack;
41 00dccaf1 Kevin Wolf
    jmp_buf env;
42 00dccaf1 Kevin Wolf
} CoroutineUContext;
43 00dccaf1 Kevin Wolf
44 00dccaf1 Kevin Wolf
/**
45 00dccaf1 Kevin Wolf
 * Per-thread coroutine bookkeeping
46 00dccaf1 Kevin Wolf
 */
47 00dccaf1 Kevin Wolf
typedef struct {
48 00dccaf1 Kevin Wolf
    /** Currently executing coroutine */
49 00dccaf1 Kevin Wolf
    Coroutine *current;
50 00dccaf1 Kevin Wolf
51 00dccaf1 Kevin Wolf
    /** Free list to speed up creation */
52 00dccaf1 Kevin Wolf
    QLIST_HEAD(, Coroutine) pool;
53 00dccaf1 Kevin Wolf
    unsigned int pool_size;
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
        QLIST_INIT(&s->pool);
79 00dccaf1 Kevin Wolf
        pthread_setspecific(thread_state_key, s);
80 00dccaf1 Kevin Wolf
    }
81 00dccaf1 Kevin Wolf
    return s;
82 00dccaf1 Kevin Wolf
}
83 00dccaf1 Kevin Wolf
84 00dccaf1 Kevin Wolf
static void qemu_coroutine_thread_cleanup(void *opaque)
85 00dccaf1 Kevin Wolf
{
86 00dccaf1 Kevin Wolf
    CoroutineThreadState *s = opaque;
87 00dccaf1 Kevin Wolf
    Coroutine *co;
88 00dccaf1 Kevin Wolf
    Coroutine *tmp;
89 00dccaf1 Kevin Wolf
90 00dccaf1 Kevin Wolf
    QLIST_FOREACH_SAFE(co, &s->pool, pool_next, tmp) {
91 7267c094 Anthony Liguori
        g_free(DO_UPCAST(CoroutineUContext, base, co)->stack);
92 7267c094 Anthony Liguori
        g_free(co);
93 00dccaf1 Kevin Wolf
    }
94 7267c094 Anthony Liguori
    g_free(s);
95 00dccaf1 Kevin Wolf
}
96 00dccaf1 Kevin Wolf
97 00dccaf1 Kevin Wolf
static void __attribute__((constructor)) coroutine_init(void)
98 00dccaf1 Kevin Wolf
{
99 00dccaf1 Kevin Wolf
    int ret;
100 00dccaf1 Kevin Wolf
101 00dccaf1 Kevin Wolf
    ret = pthread_key_create(&thread_state_key, qemu_coroutine_thread_cleanup);
102 00dccaf1 Kevin Wolf
    if (ret != 0) {
103 00dccaf1 Kevin Wolf
        fprintf(stderr, "unable to create leader key: %s\n", strerror(errno));
104 00dccaf1 Kevin Wolf
        abort();
105 00dccaf1 Kevin Wolf
    }
106 00dccaf1 Kevin Wolf
}
107 00dccaf1 Kevin Wolf
108 00dccaf1 Kevin Wolf
static void coroutine_trampoline(int i0, int i1)
109 00dccaf1 Kevin Wolf
{
110 00dccaf1 Kevin Wolf
    union cc_arg arg;
111 00dccaf1 Kevin Wolf
    CoroutineUContext *self;
112 00dccaf1 Kevin Wolf
    Coroutine *co;
113 00dccaf1 Kevin Wolf
114 00dccaf1 Kevin Wolf
    arg.i[0] = i0;
115 00dccaf1 Kevin Wolf
    arg.i[1] = i1;
116 00dccaf1 Kevin Wolf
    self = arg.p;
117 00dccaf1 Kevin Wolf
    co = &self->base;
118 00dccaf1 Kevin Wolf
119 00dccaf1 Kevin Wolf
    /* Initialize longjmp environment and switch back the caller */
120 00dccaf1 Kevin Wolf
    if (!setjmp(self->env)) {
121 00dccaf1 Kevin Wolf
        longjmp(*(jmp_buf *)co->entry_arg, 1);
122 00dccaf1 Kevin Wolf
    }
123 00dccaf1 Kevin Wolf
124 00dccaf1 Kevin Wolf
    while (true) {
125 00dccaf1 Kevin Wolf
        co->entry(co->entry_arg);
126 00dccaf1 Kevin Wolf
        qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE);
127 00dccaf1 Kevin Wolf
    }
128 00dccaf1 Kevin Wolf
}
129 00dccaf1 Kevin Wolf
130 00dccaf1 Kevin Wolf
static Coroutine *coroutine_new(void)
131 00dccaf1 Kevin Wolf
{
132 00dccaf1 Kevin Wolf
    const size_t stack_size = 1 << 20;
133 00dccaf1 Kevin Wolf
    CoroutineUContext *co;
134 00dccaf1 Kevin Wolf
    ucontext_t old_uc, uc;
135 00dccaf1 Kevin Wolf
    jmp_buf old_env;
136 32b74677 malc
    union cc_arg arg = {0};
137 00dccaf1 Kevin Wolf
138 00dccaf1 Kevin Wolf
    /* The ucontext functions preserve signal masks which incurs a system call
139 00dccaf1 Kevin Wolf
     * overhead.  setjmp()/longjmp() does not preserve signal masks but only
140 00dccaf1 Kevin Wolf
     * works on the current stack.  Since we need a way to create and switch to
141 00dccaf1 Kevin Wolf
     * a new stack, use the ucontext functions for that but setjmp()/longjmp()
142 00dccaf1 Kevin Wolf
     * for everything else.
143 00dccaf1 Kevin Wolf
     */
144 00dccaf1 Kevin Wolf
145 00dccaf1 Kevin Wolf
    if (getcontext(&uc) == -1) {
146 00dccaf1 Kevin Wolf
        abort();
147 00dccaf1 Kevin Wolf
    }
148 00dccaf1 Kevin Wolf
149 7267c094 Anthony Liguori
    co = g_malloc0(sizeof(*co));
150 7267c094 Anthony Liguori
    co->stack = g_malloc(stack_size);
151 00dccaf1 Kevin Wolf
    co->base.entry_arg = &old_env; /* stash away our jmp_buf */
152 00dccaf1 Kevin Wolf
153 00dccaf1 Kevin Wolf
    uc.uc_link = &old_uc;
154 00dccaf1 Kevin Wolf
    uc.uc_stack.ss_sp = co->stack;
155 00dccaf1 Kevin Wolf
    uc.uc_stack.ss_size = stack_size;
156 00dccaf1 Kevin Wolf
    uc.uc_stack.ss_flags = 0;
157 00dccaf1 Kevin Wolf
158 00dccaf1 Kevin Wolf
    arg.p = co;
159 00dccaf1 Kevin Wolf
160 00dccaf1 Kevin Wolf
    makecontext(&uc, (void (*)(void))coroutine_trampoline,
161 00dccaf1 Kevin Wolf
                2, arg.i[0], arg.i[1]);
162 00dccaf1 Kevin Wolf
163 00dccaf1 Kevin Wolf
    /* swapcontext() in, longjmp() back out */
164 00dccaf1 Kevin Wolf
    if (!setjmp(old_env)) {
165 00dccaf1 Kevin Wolf
        swapcontext(&old_uc, &uc);
166 00dccaf1 Kevin Wolf
    }
167 00dccaf1 Kevin Wolf
    return &co->base;
168 00dccaf1 Kevin Wolf
}
169 00dccaf1 Kevin Wolf
170 00dccaf1 Kevin Wolf
Coroutine *qemu_coroutine_new(void)
171 00dccaf1 Kevin Wolf
{
172 00dccaf1 Kevin Wolf
    CoroutineThreadState *s = coroutine_get_thread_state();
173 00dccaf1 Kevin Wolf
    Coroutine *co;
174 00dccaf1 Kevin Wolf
175 00dccaf1 Kevin Wolf
    co = QLIST_FIRST(&s->pool);
176 00dccaf1 Kevin Wolf
    if (co) {
177 00dccaf1 Kevin Wolf
        QLIST_REMOVE(co, pool_next);
178 00dccaf1 Kevin Wolf
        s->pool_size--;
179 00dccaf1 Kevin Wolf
    } else {
180 00dccaf1 Kevin Wolf
        co = coroutine_new();
181 00dccaf1 Kevin Wolf
    }
182 00dccaf1 Kevin Wolf
    return co;
183 00dccaf1 Kevin Wolf
}
184 00dccaf1 Kevin Wolf
185 00dccaf1 Kevin Wolf
void qemu_coroutine_delete(Coroutine *co_)
186 00dccaf1 Kevin Wolf
{
187 00dccaf1 Kevin Wolf
    CoroutineThreadState *s = coroutine_get_thread_state();
188 00dccaf1 Kevin Wolf
    CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_);
189 00dccaf1 Kevin Wolf
190 00dccaf1 Kevin Wolf
    if (s->pool_size < POOL_MAX_SIZE) {
191 00dccaf1 Kevin Wolf
        QLIST_INSERT_HEAD(&s->pool, &co->base, pool_next);
192 00dccaf1 Kevin Wolf
        co->base.caller = NULL;
193 00dccaf1 Kevin Wolf
        s->pool_size++;
194 00dccaf1 Kevin Wolf
        return;
195 00dccaf1 Kevin Wolf
    }
196 00dccaf1 Kevin Wolf
197 7267c094 Anthony Liguori
    g_free(co->stack);
198 7267c094 Anthony Liguori
    g_free(co);
199 00dccaf1 Kevin Wolf
}
200 00dccaf1 Kevin Wolf
201 00dccaf1 Kevin Wolf
CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
202 00dccaf1 Kevin Wolf
                                      CoroutineAction action)
203 00dccaf1 Kevin Wolf
{
204 00dccaf1 Kevin Wolf
    CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_);
205 00dccaf1 Kevin Wolf
    CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_);
206 00dccaf1 Kevin Wolf
    CoroutineThreadState *s = coroutine_get_thread_state();
207 00dccaf1 Kevin Wolf
    int ret;
208 00dccaf1 Kevin Wolf
209 00dccaf1 Kevin Wolf
    s->current = to_;
210 00dccaf1 Kevin Wolf
211 00dccaf1 Kevin Wolf
    ret = setjmp(from->env);
212 00dccaf1 Kevin Wolf
    if (ret == 0) {
213 00dccaf1 Kevin Wolf
        longjmp(to->env, action);
214 00dccaf1 Kevin Wolf
    }
215 00dccaf1 Kevin Wolf
    return ret;
216 00dccaf1 Kevin Wolf
}
217 00dccaf1 Kevin Wolf
218 00dccaf1 Kevin Wolf
Coroutine *qemu_coroutine_self(void)
219 00dccaf1 Kevin Wolf
{
220 00dccaf1 Kevin Wolf
    CoroutineThreadState *s = coroutine_get_thread_state();
221 00dccaf1 Kevin Wolf
222 00dccaf1 Kevin Wolf
    return s->current;
223 00dccaf1 Kevin Wolf
}
224 00dccaf1 Kevin Wolf
225 00dccaf1 Kevin Wolf
bool qemu_in_coroutine(void)
226 00dccaf1 Kevin Wolf
{
227 00dccaf1 Kevin Wolf
    CoroutineThreadState *s = pthread_getspecific(thread_state_key);
228 00dccaf1 Kevin Wolf
229 00dccaf1 Kevin Wolf
    return s && s->current->caller;
230 00dccaf1 Kevin Wolf
}