ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libcoro/coro.c
(Generate patch)

Comparing libcoro/coro.c (file contents):
Revision 1.42 by root, Mon Nov 10 04:37:23 2008 UTC vs.
Revision 1.51 by root, Fri Apr 2 19:18:01 2010 UTC

1/* 1/*
2 * Copyright (c) 2001-2008 Marc Alexander Lehmann <schmorp@schmorp.de> 2 * Copyright (c) 2001-2009 Marc Alexander Lehmann <schmorp@schmorp.de>
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without modifica- 4 * Redistribution and use in source and binary forms, with or without modifica-
5 * tion, are permitted provided that the following conditions are met: 5 * tion, are permitted provided that the following conditions are met:
6 * 6 *
7 * 1. Redistributions of source code must retain the above copyright notice, 7 * 1. Redistributions of source code must retain the above copyright notice,
100 100
101/* trampoline signal handler */ 101/* trampoline signal handler */
102static void 102static void
103trampoline (int sig) 103trampoline (int sig)
104{ 104{
105 if (
106 #if _XOPEN_UNIX > 0
107 _setjmp (new_coro->env) 105 if (coro_setjmp (new_coro->env))
108 #else
109 sigsetjmp (new_coro->env, 0)
110 #endif
111 ) {
112 coro_init (); /* start it */ 106 coro_init (); /* start it */
113 }
114 else 107 else
115 trampoline_done = 1; 108 trampoline_done = 1;
116} 109}
117 110
118# endif 111# endif
130 "\tpush %rbx\n" 123 "\tpush %rbx\n"
131 "\tpush %r12\n" 124 "\tpush %r12\n"
132 "\tpush %r13\n" 125 "\tpush %r13\n"
133 "\tpush %r14\n" 126 "\tpush %r14\n"
134 "\tpush %r15\n" 127 "\tpush %r15\n"
128 #if CORO_WIN_TIB
129 "\tpush %fs:0x0\n"
130 "\tpush %fs:0x8\n"
131 "\tpush %fs:0xc\n"
132 #endif
135 "\tmov %rsp, (%rdi)\n" 133 "\tmov %rsp, (%rdi)\n"
136 "\tmov (%rsi), %rsp\n" 134 "\tmov (%rsi), %rsp\n"
135 #if CORO_WIN_TIB
136 "\tpop %fs:0xc\n"
137 "\tpop %fs:0x8\n"
138 "\tpop %fs:0x0\n"
139 #endif
137 "\tpop %r15\n" 140 "\tpop %r15\n"
138 "\tpop %r14\n" 141 "\tpop %r14\n"
139 "\tpop %r13\n" 142 "\tpop %r13\n"
140 "\tpop %r12\n" 143 "\tpop %r12\n"
141 "\tpop %rbx\n" 144 "\tpop %rbx\n"
144 #define NUM_SAVED 4 147 #define NUM_SAVED 4
145 "\tpush %ebp\n" 148 "\tpush %ebp\n"
146 "\tpush %ebx\n" 149 "\tpush %ebx\n"
147 "\tpush %esi\n" 150 "\tpush %esi\n"
148 "\tpush %edi\n" 151 "\tpush %edi\n"
152 #if CORO_WIN_TIB
153 "\tpush %fs:0\n"
154 "\tpush %fs:4\n"
155 "\tpush %fs:8\n"
156 #endif
149 "\tmov %esp, (%eax)\n" 157 "\tmov %esp, (%eax)\n"
150 "\tmov (%edx), %esp\n" 158 "\tmov (%edx), %esp\n"
159 #if CORO_WIN_TIB
160 "\tpop %fs:8\n"
161 "\tpop %fs:4\n"
162 "\tpop %fs:0\n"
163 #endif
151 "\tpop %edi\n" 164 "\tpop %edi\n"
152 "\tpop %esi\n" 165 "\tpop %esi\n"
153 "\tpop %ebx\n" 166 "\tpop %ebx\n"
154 "\tpop %ebp\n" 167 "\tpop %ebp\n"
155 #else 168 #else
229 sigaction (SIGUSR2, &osa, 0); 242 sigaction (SIGUSR2, &osa, 0);
230 sigprocmask (SIG_SETMASK, &osig, 0); 243 sigprocmask (SIG_SETMASK, &osig, 0);
231 244
232# elif CORO_LOSER 245# elif CORO_LOSER
233 246
234 setjmp (ctx->env); 247 coro_setjmp (ctx->env);
235 #if __CYGWIN__ 248 #if __CYGWIN__ && __i386
249 ctx->env[8] = (long) coro_init;
236 ctx->env[7] = (long)((char *)sptr + ssize) - sizeof (long); 250 ctx->env[7] = (long) ((char *)sptr + ssize) - sizeof (long);
237 ctx->env[8] = (long)coro_init; 251 #elif __CYGWIN__ && __x86_64
252 ctx->env[7] = (long) coro_init;
253 ctx->env[6] = (long) ((char *)sptr + ssize) - sizeof (long);
254 #elif defined(__MINGW32__)
255 ctx->env[5] = (long) coro_init;
256 ctx->env[4] = (long) ((char *)sptr + ssize) - sizeof (long);
238 #elif defined(_M_IX86) 257 #elif defined(_M_IX86)
239 ((_JUMP_BUFFER *)&ctx->env)->Eip = (long)coro_init; 258 ((_JUMP_BUFFER *)&ctx->env)->Eip = (long) coro_init;
240 ((_JUMP_BUFFER *)&ctx->env)->Esp = (long)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); 259 ((_JUMP_BUFFER *)&ctx->env)->Esp = (long) STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
241 #elif defined(_M_AMD64) 260 #elif defined(_M_AMD64)
242 ((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64)coro_init; 261 ((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64) coro_init;
243 ((_JUMP_BUFFER *)&ctx->env)->Rsp = (__int64)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); 262 ((_JUMP_BUFFER *)&ctx->env)->Rsp = (__int64) STACK_ADJUST_PTR (sptr, ssize) - sizeof (__int64);
244 #elif defined(_M_IA64) 263 #elif defined(_M_IA64)
245 ((_JUMP_BUFFER *)&ctx->env)->StIIP = (__int64)coro_init; 264 ((_JUMP_BUFFER *)&ctx->env)->StIIP = (__int64) coro_init;
246 ((_JUMP_BUFFER *)&ctx->env)->IntSp = (__int64)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); 265 ((_JUMP_BUFFER *)&ctx->env)->IntSp = (__int64) STACK_ADJUST_PTR (sptr, ssize) - sizeof (__int64);
247 #else 266 #else
248 #error "microsoft libc or architecture not supported" 267 #error "microsoft libc or architecture not supported"
249 #endif 268 #endif
250 269
251# elif CORO_LINUX 270# elif CORO_LINUX
252 271
253 _setjmp (ctx->env); 272 coro_setjmp (ctx->env);
254 #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP) 273 #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP)
255 ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; 274 ctx->env[0].__jmpbuf[JB_PC] = (long) coro_init;
256 ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); 275 ctx->env[0].__jmpbuf[JB_SP] = (long) STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
257 #elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__) 276 #elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__)
258 ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; 277 ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init;
259 ctx->env[0].__jmpbuf[0].__sp = (int *)((char *)sptr + ssize) - sizeof (long); 278 ctx->env[0].__jmpbuf[0].__sp = (int *) ((char *)sptr + ssize) - sizeof (long);
260 #elif defined (__GNU_LIBRARY__) && defined (__i386__) 279 #elif defined (__GNU_LIBRARY__) && defined (__i386__)
261 ctx->env[0].__jmpbuf[0].__pc = (char *)coro_init; 280 ctx->env[0].__jmpbuf[0].__pc = (char *) coro_init;
262 ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize) - sizeof (long); 281 ctx->env[0].__jmpbuf[0].__sp = (void *) ((char *)sptr + ssize) - sizeof (long);
263 #elif defined (__GNU_LIBRARY__) && defined (__amd64__) 282 #elif defined (__GNU_LIBRARY__) && defined (__amd64__)
264 ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; 283 ctx->env[0].__jmpbuf[JB_PC] = (long) coro_init;
265 ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize) - sizeof (long); 284 ctx->env[0].__jmpbuf[0].__sp = (void *) ((char *)sptr + ssize) - sizeof (long);
266 #else 285 #else
267 #error "linux libc or architecture not supported" 286 #error "linux libc or architecture not supported"
268 #endif 287 #endif
269 288
270# elif CORO_IRIX 289# elif CORO_IRIX
271 290
272 sigsetjmp (ctx->env, 0); 291 coro_setjmp (ctx->env, 0);
273 ctx->env[JB_PC] = (__uint64_t)coro_init; 292 ctx->env[JB_PC] = (__uint64_t)coro_init;
274 ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); 293 ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
275 294
276# elif CORO_ASM 295# elif CORO_ASM
277 296
278 ctx->sp = (void **)(ssize + (char *)sptr); 297 ctx->sp = (void **)(ssize + (char *)sptr);
279 *--ctx->sp = (void *)abort; /* needed for alignment only */ 298 *--ctx->sp = (void *)abort; /* needed for alignment only */
280 *--ctx->sp = (void *)coro_init; 299 *--ctx->sp = (void *)coro_init;
300
301 #if CORO_WIN_TIB
302 *--ctx->sp = 0; /* ExceptionList */
303 *--ctx->sp = (char *)sptr + ssize; /* StackBase */
304 *--ctx->sp = sptr; /* StackLimit */
305 #endif
306
281 ctx->sp -= NUM_SAVED; 307 ctx->sp -= NUM_SAVED;
282 308
283# elif CORO_UCONTEXT 309# elif CORO_UCONTEXT
284 310
285 getcontext (&(ctx->uc)); 311 getcontext (&(ctx->uc));
341void 367void
342coro_transfer (coro_context *prev, coro_context *next) 368coro_transfer (coro_context *prev, coro_context *next)
343{ 369{
344 pthread_cond_signal (&next->cv); 370 pthread_cond_signal (&next->cv);
345 pthread_cond_wait (&prev->cv, &coro_mutex); 371 pthread_cond_wait (&prev->cv, &coro_mutex);
372#if __FreeBSD__ /* freebsd is of course broken and needs manual testcancel calls... yay... */
373 pthread_testcancel ();
374#endif
346} 375}
347 376
348void 377void
349coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize) 378coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize)
350{ 379{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines