… | |
… | |
38 | * go to Ralf S. Engelschall <rse@engelschall.com>. |
38 | * go to Ralf S. Engelschall <rse@engelschall.com>. |
39 | */ |
39 | */ |
40 | |
40 | |
41 | #include "coro.h" |
41 | #include "coro.h" |
42 | |
42 | |
|
|
43 | #include <string.h> |
|
|
44 | |
43 | #if !defined(STACK_ADJUST_PTR) |
45 | #if !defined(STACK_ADJUST_PTR) |
44 | /* IRIX is decidedly NON-unix */ |
46 | /* IRIX is decidedly NON-unix */ |
45 | # if __sgi |
47 | # if __sgi |
46 | # define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss) - 8) |
48 | # define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss) - 8) |
47 | # define STACK_ADJUST_SIZE(sp,ss) ((ss) - 8) |
49 | # define STACK_ADJUST_SIZE(sp,ss) ((ss) - 8) |
… | |
… | |
61 | # include <stddef.h> |
63 | # include <stddef.h> |
62 | #endif |
64 | #endif |
63 | |
65 | |
64 | #if CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX || CORO_ASM |
66 | #if CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX || CORO_ASM |
65 | |
67 | |
66 | #include <stdlib.h> |
68 | # include <stdlib.h> |
67 | |
69 | |
68 | #if CORO_SJLJ |
70 | # if CORO_SJLJ |
69 | # include <stdio.h> |
71 | # include <stdio.h> |
70 | # include <signal.h> |
72 | # include <signal.h> |
71 | # include <unistd.h> |
73 | # include <unistd.h> |
72 | #endif |
74 | # endif |
73 | |
75 | |
74 | static volatile coro_func coro_init_func; |
76 | static volatile coro_func coro_init_func; |
75 | static volatile void *coro_init_arg; |
77 | static volatile void *coro_init_arg; |
76 | static volatile coro_context *new_coro, *create_coro; |
78 | static volatile coro_context *new_coro, *create_coro; |
77 | |
79 | |
78 | /* what we really want to detect here is wether we use a new-enough version of GAS */ |
80 | /* what we really want to detect here is wether we use a new-enough version of GAS */ |
79 | /* instead, check for gcc 3, ELF and GNU/Linux and hope for the best */ |
81 | /* instead, check for gcc 3, ELF and GNU/Linux and hope for the best */ |
80 | #if __GNUC__ >= 3 && __ELF__ && __linux__ |
82 | # if __GNUC__ >= 3 && __ELF__ && __linux__ |
81 | # define HAVE_CFI 1 |
83 | # define HAVE_CFI 1 |
82 | #endif |
84 | # endif |
83 | |
85 | |
84 | static void |
86 | static void |
85 | coro_init (void) |
87 | coro_init (void) |
86 | { |
88 | { |
87 | volatile coro_func func = coro_init_func; |
89 | volatile coro_func func = coro_init_func; |
… | |
… | |
103 | static void |
105 | static void |
104 | trampoline (int sig) |
106 | trampoline (int sig) |
105 | { |
107 | { |
106 | if (setjmp (((coro_context *)new_coro)->env)) |
108 | if (setjmp (((coro_context *)new_coro)->env)) |
107 | { |
109 | { |
108 | #if HAVE_CFI |
110 | # if HAVE_CFI |
109 | asm (".cfi_startproc"); |
111 | asm (".cfi_startproc"); |
110 | #endif |
112 | # endif |
111 | coro_init (); /* start it */ |
113 | coro_init (); /* start it */ |
112 | #if HAVE_CFI |
114 | # if HAVE_CFI |
113 | asm (".cfi_endproc"); |
115 | asm (".cfi_endproc"); |
114 | #endif |
116 | # endif |
115 | } |
117 | } |
116 | else |
118 | else |
117 | trampoline_count++; |
119 | trampoline_count++; |
118 | } |
120 | } |
119 | |
121 | |
120 | # endif |
122 | # endif |
121 | |
123 | |
122 | #endif |
124 | #endif |
123 | |
125 | |
124 | #if CORO_ASM |
126 | #if CORO_ASM |
|
|
127 | |
125 | asm ( |
128 | asm ( |
126 | ".text\n" |
129 | ".text\n" |
127 | ".globl coro_transfer\n" |
130 | ".globl coro_transfer\n" |
128 | ".type coro_transfer, @function\n" |
131 | ".type coro_transfer, @function\n" |
129 | "coro_transfer:\n" |
132 | "coro_transfer:\n" |
130 | #if __amd64 |
133 | # if __amd64 |
131 | # define NUM_ALIGN 1 |
|
|
132 | # define NUM_SAVED 5 |
134 | # define NUM_SAVED 6 |
|
|
135 | "\tpush %rbp\n" |
133 | "\tpush %rbx\n" |
136 | "\tpush %rbx\n" |
134 | "\tpush %r12\n" |
137 | "\tpush %r12\n" |
135 | "\tpush %r13\n" |
138 | "\tpush %r13\n" |
136 | "\tpush %r14\n" |
139 | "\tpush %r14\n" |
137 | "\tpush %r15\n" |
140 | "\tpush %r15\n" |
138 | "\tmov %rsp, (%rdi)\n" |
141 | "\tmov %rsp, (%rdi)\n" |
139 | "\tmov (%rsi), %rsp\n" |
142 | "\tmov (%rsi), %rsp\n" |
140 | "\tpop %r15\n" |
143 | "\tpop %r15\n" |
141 | "\tpop %r14\n" |
144 | "\tpop %r14\n" |
142 | "\tpop %r13\n" |
145 | "\tpop %r13\n" |
143 | "\tpop %r12\n" |
146 | "\tpop %r12\n" |
144 | "\tpop %rbx\n" |
147 | "\tpop %rbx\n" |
|
|
148 | "\tpop %rbp\n" |
145 | #elif __i386 |
149 | # elif __i386 |
146 | # define NUM_ALIGN 1 |
|
|
147 | # define NUM_SAVED 4 |
150 | # define NUM_SAVED 4 |
148 | "\tpush %ebx\n" |
|
|
149 | "\tpush %esi\n" |
|
|
150 | "\tpush %edi\n" |
|
|
151 | "\tpush %ebp\n" |
151 | "\tpush %ebp\n" |
|
|
152 | "\tpush %ebx\n" |
|
|
153 | "\tpush %esi\n" |
|
|
154 | "\tpush %edi\n" |
152 | "\tmov %esp, (%eax)\n" |
155 | "\tmov %esp, (%eax)\n" |
153 | "\tmov (%edx), %esp\n" |
156 | "\tmov (%edx), %esp\n" |
154 | "\tpop %ebp\n" |
|
|
155 | "\tpop %edi\n" |
157 | "\tpop %edi\n" |
156 | "\tpop %esi\n" |
158 | "\tpop %esi\n" |
157 | "\tpop %ebx\n" |
159 | "\tpop %ebx\n" |
|
|
160 | "\tpop %ebp\n" |
158 | #else |
161 | # else |
159 | # error unsupported architecture |
162 | # error unsupported architecture |
160 | #endif |
163 | # endif |
161 | "\tret\n" |
164 | "\tret\n" |
162 | ); |
165 | ); |
|
|
166 | |
163 | #endif |
167 | #endif |
164 | |
168 | |
165 | #if CORO_PTHREAD |
169 | #if CORO_PTHREAD |
166 | |
170 | |
|
|
171 | /* this mutex will be locked by the running coroutine */ |
|
|
172 | pthread_mutex_t coro_mutex = PTHREAD_MUTEX_INITIALIZER; |
|
|
173 | |
167 | struct coro_init_args { |
174 | struct coro_init_args |
|
|
175 | { |
168 | coro_func func; |
176 | coro_func func; |
169 | void *arg; |
177 | void *arg; |
170 | coro_context *self, *main; |
178 | coro_context *self, *main; |
171 | }; |
179 | }; |
172 | |
180 | |
173 | pthread_mutex_t coro_mutex = PTHREAD_MUTEX_INITIALIZER; |
181 | static pthread_t null_tid; |
|
|
182 | |
|
|
183 | /* I'd so love to cast pthread_mutex_unlock to void (*)(void *)... */ |
|
|
184 | static void |
|
|
185 | mutex_unlock_wrapper (void *arg) |
|
|
186 | { |
|
|
187 | pthread_mutex_unlock ((pthread_mutex_t *)arg); |
|
|
188 | } |
174 | |
189 | |
175 | static void * |
190 | static void * |
176 | trampoline (void *args_) |
191 | trampoline (void *args_) |
177 | { |
192 | { |
178 | struct coro_init_args *args = (struct coro_init_args *)args_; |
193 | struct coro_init_args *args = (struct coro_init_args *)args_; |
179 | coro_func func = args->func; |
194 | coro_func func = args->func; |
180 | void *arg = args->arg; |
195 | void *arg = args->arg; |
181 | |
196 | |
182 | pthread_mutex_lock (&coro_mutex); |
197 | pthread_mutex_lock (&coro_mutex); |
183 | pthread_cond_destroy (&args->self->c); |
198 | |
|
|
199 | /* we try to be good citizens and use deferred cancellation and cleanup handlers */ |
|
|
200 | pthread_cleanup_push (mutex_unlock_wrapper, &coro_mutex); |
184 | coro_transfer (args->self, args->main); |
201 | coro_transfer (args->self, args->main); |
185 | func (arg); |
202 | func (arg); |
186 | pthread_mutex_unlock (&coro_mutex); |
203 | pthread_cleanup_pop (1); |
187 | |
204 | |
188 | return 0; |
205 | return 0; |
189 | } |
206 | } |
190 | |
207 | |
191 | asm(""); |
208 | void |
192 | |
|
|
193 | void coro_transfer(coro_context *prev, coro_context *next) |
209 | coro_transfer (coro_context *prev, coro_context *next) |
194 | { |
210 | { |
195 | pthread_cond_init (&prev->c, 0); |
|
|
196 | pthread_cond_signal (&next->c); |
211 | pthread_cond_signal (&next->cv); |
197 | pthread_cond_wait (&prev->c, &coro_mutex); |
212 | pthread_cond_wait (&prev->cv, &coro_mutex); |
|
|
213 | } |
|
|
214 | |
|
|
215 | void |
|
|
216 | coro_destroy (coro_context *ctx) |
|
|
217 | { |
|
|
218 | if (!pthread_equal (ctx->id, null_tid)) |
|
|
219 | { |
|
|
220 | pthread_cancel (ctx->id); |
|
|
221 | pthread_mutex_unlock (&coro_mutex); |
|
|
222 | pthread_join (ctx->id, 0); |
|
|
223 | pthread_mutex_lock (&coro_mutex); |
|
|
224 | } |
|
|
225 | |
198 | pthread_cond_destroy (&prev->c); |
226 | pthread_cond_destroy (&ctx->cv); |
199 | } |
227 | } |
200 | |
228 | |
201 | #endif |
229 | #endif |
202 | |
230 | |
203 | /* initialize a machine state */ |
231 | /* initialize a machine state */ |
204 | void coro_create (coro_context *ctx, |
232 | void |
205 | coro_func coro, void *arg, |
233 | coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize) |
206 | void *sptr, long ssize) |
|
|
207 | { |
234 | { |
208 | #if CORO_UCONTEXT |
235 | #if CORO_UCONTEXT |
|
|
236 | |
|
|
237 | if (!coro) |
|
|
238 | return; |
209 | |
239 | |
210 | getcontext (&(ctx->uc)); |
240 | getcontext (&(ctx->uc)); |
211 | |
241 | |
212 | ctx->uc.uc_link = 0; |
242 | ctx->uc.uc_link = 0; |
213 | ctx->uc.uc_stack.ss_sp = STACK_ADJUST_PTR (sptr,ssize); |
243 | ctx->uc.uc_stack.ss_sp = STACK_ADJUST_PTR (sptr,ssize); |
214 | ctx->uc.uc_stack.ss_size = (size_t)STACK_ADJUST_SIZE (sptr,ssize); |
244 | ctx->uc.uc_stack.ss_size = (size_t)STACK_ADJUST_SIZE (sptr,ssize); |
215 | ctx->uc.uc_stack.ss_flags = 0; |
245 | ctx->uc.uc_stack.ss_flags = 0; |
216 | |
246 | |
217 | makecontext (&(ctx->uc), (void (*)()) coro, 1, arg); |
247 | makecontext (&(ctx->uc), (void (*)())coro, 1, arg); |
218 | |
248 | |
219 | #elif CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX || CORO_ASM |
249 | #elif CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX || CORO_ASM |
220 | |
250 | |
221 | # if CORO_SJLJ |
251 | # if CORO_SJLJ |
222 | stack_t ostk, nstk; |
252 | stack_t ostk, nstk; |
223 | struct sigaction osa, nsa; |
253 | struct sigaction osa, nsa; |
224 | sigset_t nsig, osig; |
254 | sigset_t nsig, osig; |
225 | # endif |
255 | # endif |
226 | coro_context nctx; |
256 | coro_context nctx; |
|
|
257 | |
|
|
258 | if (!coro) |
|
|
259 | return; |
227 | |
260 | |
228 | coro_init_func = coro; |
261 | coro_init_func = coro; |
229 | coro_init_arg = arg; |
262 | coro_init_arg = arg; |
230 | |
263 | |
231 | new_coro = ctx; |
264 | new_coro = ctx; |
… | |
… | |
327 | ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
360 | ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
328 | |
361 | |
329 | # elif CORO_ASM |
362 | # elif CORO_ASM |
330 | |
363 | |
331 | ctx->sp = (volatile void **)(ssize + (char *)sptr); |
364 | ctx->sp = (volatile void **)(ssize + (char *)sptr); |
332 | ctx->sp -= NUM_ALIGN; |
365 | *--ctx->sp = (void *)abort; /* needed for alignment only */ |
333 | *--ctx->sp = (void *)coro_init; |
366 | *--ctx->sp = (void *)coro_init; |
334 | ctx->sp -= NUM_SAVED; |
367 | ctx->sp -= NUM_SAVED; |
335 | |
368 | |
336 | # endif |
369 | # endif |
337 | |
370 | |
338 | coro_transfer ((coro_context *)create_coro, (coro_context *)new_coro); |
371 | coro_transfer ((coro_context *)create_coro, (coro_context *)new_coro); |
339 | |
372 | |
340 | # elif CORO_PTHREAD |
373 | # elif CORO_PTHREAD |
341 | |
374 | |
342 | pthread_t id; |
|
|
343 | pthread_attr_t attr; |
|
|
344 | coro_context nctx; |
375 | static coro_context nctx; |
345 | struct coro_init_args args; |
|
|
346 | static int once; |
376 | static int once; |
347 | |
377 | |
348 | if (!once) |
378 | if (!once) |
349 | { |
379 | { |
|
|
380 | once = 1; |
|
|
381 | |
350 | pthread_mutex_lock (&coro_mutex); |
382 | pthread_mutex_lock (&coro_mutex); |
351 | once = 1; |
383 | pthread_cond_init (&nctx.cv, 0); |
|
|
384 | null_tid = pthread_self (); |
|
|
385 | } |
|
|
386 | |
|
|
387 | pthread_cond_init (&ctx->cv, 0); |
|
|
388 | |
|
|
389 | if (coro) |
352 | } |
390 | { |
|
|
391 | pthread_attr_t attr; |
|
|
392 | struct coro_init_args args; |
353 | |
393 | |
354 | args.func = coro; |
394 | args.func = coro; |
355 | args.arg = arg; |
395 | args.arg = arg; |
356 | args.self = ctx; |
396 | args.self = ctx; |
357 | args.main = &nctx; |
397 | args.main = &nctx; |
358 | |
398 | |
359 | pthread_attr_init (&attr); |
399 | pthread_attr_init (&attr); |
360 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
|
|
361 | pthread_attr_setstack (&attr, sptr, (size_t)ssize); |
400 | pthread_attr_setstack (&attr, sptr, (size_t)ssize); |
|
|
401 | pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); |
362 | pthread_create (&id, &attr, trampoline, &args); |
402 | pthread_create (&ctx->id, &attr, trampoline, &args); |
363 | |
403 | |
364 | pthread_cond_init (&args.self->c, 0); |
|
|
365 | coro_transfer (args.main, args.self); |
404 | coro_transfer (args.main, args.self); |
|
|
405 | } |
|
|
406 | else |
|
|
407 | ctx->id = null_tid; |
366 | |
408 | |
367 | #else |
409 | #else |
368 | # error unsupported backend |
410 | # error unsupported backend |
369 | #endif |
411 | #endif |
370 | } |
412 | } |