… | |
… | |
78 | |
78 | |
79 | static coro_func coro_init_func; |
79 | static coro_func coro_init_func; |
80 | static void *coro_init_arg; |
80 | static void *coro_init_arg; |
81 | static coro_context *new_coro, *create_coro; |
81 | static coro_context *new_coro, *create_coro; |
82 | |
82 | |
83 | /* what we really want to detect here is wether we use a new-enough version of GAS */ |
|
|
84 | /* with dwarf debug info. instead, check for gcc 3, ELF and GNU/Linux and hope for the best */ |
|
|
85 | # if __GNUC__ >= 3 && __ELF__ && __linux__ |
|
|
86 | # define HAVE_CFI 1 |
|
|
87 | # endif |
|
|
88 | |
|
|
89 | static void |
83 | static void |
90 | coro_init (void) |
84 | coro_init (void) |
91 | { |
85 | { |
92 | volatile coro_func func = coro_init_func; |
86 | volatile coro_func func = coro_init_func; |
93 | volatile void *arg = coro_init_arg; |
87 | volatile void *arg = coro_init_arg; |
… | |
… | |
106 | |
100 | |
107 | /* trampoline signal handler */ |
101 | /* trampoline signal handler */ |
108 | static void |
102 | static void |
109 | trampoline (int sig) |
103 | trampoline (int sig) |
110 | { |
104 | { |
111 | if ( |
|
|
112 | #if _XOPEN_UNIX > 0 |
|
|
113 | _setjmp (new_coro->env) |
105 | if (coro_setjmp (new_coro->env)) |
114 | #else |
|
|
115 | sigsetjmp (new_coro->env, 0) |
|
|
116 | #endif |
|
|
117 | ) { |
|
|
118 | #if HAVE_CFI |
|
|
119 | asm (".cfi_startproc"); |
|
|
120 | #endif |
|
|
121 | coro_init (); /* start it */ |
106 | coro_init (); /* start it */ |
122 | #if HAVE_CFI |
|
|
123 | asm (".cfi_endproc"); |
|
|
124 | #endif |
|
|
125 | } |
|
|
126 | else |
107 | else |
127 | trampoline_done = 1; |
108 | trampoline_done = 1; |
128 | } |
109 | } |
129 | |
110 | |
130 | # endif |
111 | # endif |
… | |
… | |
241 | sigaction (SIGUSR2, &osa, 0); |
222 | sigaction (SIGUSR2, &osa, 0); |
242 | sigprocmask (SIG_SETMASK, &osig, 0); |
223 | sigprocmask (SIG_SETMASK, &osig, 0); |
243 | |
224 | |
244 | # elif CORO_LOSER |
225 | # elif CORO_LOSER |
245 | |
226 | |
246 | setjmp (ctx->env); |
227 | coro_setjmp (ctx->env); |
247 | #if __CYGWIN__ |
228 | #if __CYGWIN__ |
248 | ctx->env[7] = (long)((char *)sptr + ssize) - sizeof (long); |
229 | ctx->env[7] = (long)((char *)sptr + ssize) - sizeof (long); |
249 | ctx->env[8] = (long)coro_init; |
230 | ctx->env[8] = (long)coro_init; |
|
|
231 | #elif defined(__MINGW32__) |
|
|
232 | ctx->env[4] = (long)((char *)sptr + ssize) - sizeof (long); |
|
|
233 | ctx->env[5] = (long)coro_init; |
250 | #elif defined(_M_IX86) |
234 | #elif defined(_M_IX86) |
251 | ((_JUMP_BUFFER *)&ctx->env)->Eip = (long)coro_init; |
235 | ((_JUMP_BUFFER *)&ctx->env)->Eip = (long)coro_init; |
252 | ((_JUMP_BUFFER *)&ctx->env)->Esp = (long)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
236 | ((_JUMP_BUFFER *)&ctx->env)->Esp = (long)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
253 | #elif defined(_M_AMD64) |
237 | #elif defined(_M_AMD64) |
254 | ((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64)coro_init; |
238 | ((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64)coro_init; |
… | |
… | |
260 | #error "microsoft libc or architecture not supported" |
244 | #error "microsoft libc or architecture not supported" |
261 | #endif |
245 | #endif |
262 | |
246 | |
263 | # elif CORO_LINUX |
247 | # elif CORO_LINUX |
264 | |
248 | |
265 | _setjmp (ctx->env); |
249 | coro_setjmp (ctx->env); |
266 | #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP) |
250 | #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP) |
267 | ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; |
251 | ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; |
268 | ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
252 | ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
269 | #elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__) |
253 | #elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__) |
270 | ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; |
254 | ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; |
… | |
… | |
279 | #error "linux libc or architecture not supported" |
263 | #error "linux libc or architecture not supported" |
280 | #endif |
264 | #endif |
281 | |
265 | |
282 | # elif CORO_IRIX |
266 | # elif CORO_IRIX |
283 | |
267 | |
284 | sigsetjmp (ctx->env, 0); |
268 | coro_setjmp (ctx->env, 0); |
285 | ctx->env[JB_PC] = (__uint64_t)coro_init; |
269 | ctx->env[JB_PC] = (__uint64_t)coro_init; |
286 | ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
270 | ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); |
287 | |
271 | |
288 | # elif CORO_ASM |
272 | # elif CORO_ASM |
289 | |
273 | |
… | |
… | |
353 | void |
337 | void |
354 | coro_transfer (coro_context *prev, coro_context *next) |
338 | coro_transfer (coro_context *prev, coro_context *next) |
355 | { |
339 | { |
356 | pthread_cond_signal (&next->cv); |
340 | pthread_cond_signal (&next->cv); |
357 | pthread_cond_wait (&prev->cv, &coro_mutex); |
341 | pthread_cond_wait (&prev->cv, &coro_mutex); |
|
|
342 | #if __FreeBSD__ /* freebsd is of course broken and needs manual testcancel calls... yay... */ |
|
|
343 | pthread_testcancel (); |
|
|
344 | #endif |
358 | } |
345 | } |
359 | |
346 | |
360 | void |
347 | void |
361 | coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize) |
348 | coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize) |
362 | { |
349 | { |