1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
| #include <string.h> #include <stddef.h> #include <stdio.h> #include <stdlib.h> #include <setjmp.h> #include <stdint.h> #include <ucontext.h>
#ifndef container_of #define container_of(ptr, type, member) ({ \ const typeof(((type *) 0)->member) *__mptr = (ptr); \ (type *) ((char *) __mptr - offsetof(type, member));}) #endif
#ifdef __GNUC__ #define DO_UPCAST(type, field, dev) ( __extension__ ( { \ char __attribute__((unused)) offset_must_be_zero[ \ -offsetof(type, field)]; \ container_of(dev, type, field);})) #else #define DO_UPCAST(type, field, dev) container_of(dev, type, field) #endif
#define coroutine_fn
typedef struct Coroutine Coroutine;
typedef void coroutine_fn CoroutineEntry(void *opaque);
struct Coroutine { CoroutineEntry *entry; void *entry_arg; Coroutine *caller; };
struct CoroutineUContext { Coroutine base; void *stack; sigjmp_buf env; };
typedef enum { COROUTINE_YIELD = 1, COROUTINE_TERMINATE = 2, COROUTINE_ENTER = 3, } CoroutineAction;
static __thread CoroutineUContext leader; static __thread Coroutine *current;
union cc_arg { void *p; int i[2]; };
CoroutineAction qemu_coroutine_switch(Coroutine *from, Coroutine *to, CoroutineAction action);
static void coroutine_trampoline(int i0, int i1) { union cc_arg arg; CoroutineUContext *self; Coroutine *co;
arg.i[0] = i0; arg.i[1] = i1; self = (CoroutineUContext *)arg.p; co = &self->base;
if (!sigsetjmp(self->env, 0)) { siglongjmp(*(sigjmp_buf *)co->entry_arg, 1); }
while (true) { co->entry(co->entry_arg);
qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE); } }
Coroutine *qemu_coroutine_new(void) { const size_t stack_size = 1 << 20; CoroutineUContext *co; ucontext_t old_uc, uc; sigjmp_buf old_env; union cc_arg arg = {0};
if (getcontext(&uc) == -1) { abort(); }
co = (CoroutineUContext *)malloc(sizeof(*co)); memset(co, 0, sizeof(*co)); co->stack = malloc(stack_size); co->base.entry_arg = &old_env;
uc.uc_link = &old_uc; uc.uc_stack.ss_sp = co->stack; uc.uc_stack.ss_size = stack_size; uc.uc_stack.ss_flags = 0;
arg.p = co;
makecontext(&uc, (void (*)(void))coroutine_trampoline, 2, arg.i[0], arg.i[1]);
if (!sigsetjmp(old_env, 0)) { swapcontext(&old_uc, &uc); } return &co->base; }
void qemu_coroutine_delete(Coroutine *co_) { CoroutineUContext *co = DO_UPCAST(CoroutineUContext, base, co_);
free(co->stack); free(co); }
CoroutineAction __attribute__((noinline)) qemu_coroutine_switch(Coroutine *from_, Coroutine *to_, CoroutineAction action) { CoroutineUContext *from = DO_UPCAST(CoroutineUContext, base, from_); CoroutineUContext *to = DO_UPCAST(CoroutineUContext, base, to_); int ret;
current = to_;
ret = sigsetjmp(from->env, 0); if (ret == 0) { siglongjmp(to->env, action); } return (CoroutineAction)ret; }
Coroutine *qemu_coroutine_self(void) { if (!current) { current = &leader.base; } return current; }
bool qemu_in_coroutine(void) { return current && current->caller; }
Coroutine *qemu_coroutine_create(CoroutineEntry *entry) { Coroutine *co = NULL; if (!co) { co = qemu_coroutine_new(); }
co->entry = entry; return co; }
static void coroutine_delete(Coroutine *co) { co->caller = NULL;
qemu_coroutine_delete(co); }
void qemu_coroutine_enter(Coroutine *co, void *opaque) { Coroutine *self = qemu_coroutine_self(); CoroutineAction ret;
if (co->caller) { fprintf(stderr, "Co-routine re-entered recursively\n"); abort(); }
co->caller = self; co->entry_arg = opaque;
ret = qemu_coroutine_switch(self, co, COROUTINE_ENTER);
switch (ret) { case COROUTINE_YIELD: return; case COROUTINE_TERMINATE: coroutine_delete(co); return; default: abort(); } }
void coroutine_fn qemu_coroutine_yield(void) { Coroutine *self = qemu_coroutine_self(); Coroutine *to = self->caller;
if (!to) { fprintf(stderr, "Co-routine is yielding to no one\n"); abort(); }
self->caller = NULL;
qemu_coroutine_switch(self, to, COROUTINE_YIELD); }
void coroutine_fn test(void *p) { printf("before yield\n"); qemu_coroutine_yield(); printf("after yield\n"); }
int main() { Coroutine *co = qemu_coroutine_create(test); qemu_coroutine_enter(co, NULL); qemu_coroutine_enter(co, NULL);
return 0; }
|