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.58 by root, Fri Jun 10 12:27:02 2011 UTC

1/* 1/*
2 * Copyright (c) 2001-2008 Marc Alexander Lehmann <schmorp@schmorp.de> 2 * Copyright (c) 2001-2011 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,
86 volatile coro_func func = coro_init_func; 86 volatile coro_func func = coro_init_func;
87 volatile void *arg = coro_init_arg; 87 volatile void *arg = coro_init_arg;
88 88
89 coro_transfer (new_coro, create_coro); 89 coro_transfer (new_coro, create_coro);
90 90
91#if __linux && __amd64
92 /* we blindly assume on any __linux with __amd64 we have a new enough gas with .cfi_undefined support */
93 asm (".cfi_undefined rip");
94#endif
95
91 func ((void *)arg); 96 func ((void *)arg);
92 97
93 /* the new coro returned. bad. just abort() for now */ 98 /* the new coro returned. bad. just abort() for now */
94 abort (); 99 abort ();
95} 100}
100 105
101/* trampoline signal handler */ 106/* trampoline signal handler */
102static void 107static void
103trampoline (int sig) 108trampoline (int sig)
104{ 109{
105 if (
106 #if _XOPEN_UNIX > 0
107 _setjmp (new_coro->env) 110 if (coro_setjmp (new_coro->env))
108 #else
109 sigsetjmp (new_coro->env, 0)
110 #endif
111 ) {
112 coro_init (); /* start it */ 111 coro_init (); /* start it */
113 }
114 else 112 else
115 trampoline_done = 1; 113 trampoline_done = 1;
116} 114}
117 115
118# endif 116# endif
122 asm ( 120 asm (
123 ".text\n" 121 ".text\n"
124 ".globl coro_transfer\n" 122 ".globl coro_transfer\n"
125 ".type coro_transfer, @function\n" 123 ".type coro_transfer, @function\n"
126 "coro_transfer:\n" 124 "coro_transfer:\n"
125 /* windows, of course, gives a shit on the amd64 ABI and uses different registers */
126 /* http://blogs.msdn.com/freik/archive/2005/03/17/398200.aspx */
127 #if __amd64 127 #if __amd64
128 #ifdef _WIN32
129 #define NUM_SAVED 8
130 "\tpush %rsi\n"
131 "\tpush %rdi\n"
132 "\tpush %rbp\n"
133 "\tpush %rbx\n"
134 "\tpush %r12\n"
135 "\tpush %r13\n"
136 "\tpush %r14\n"
137 "\tpush %r15\n"
138 #if CORO_WIN_TIB
139 "\tpush %gs:0x0\n"
140 "\tpush %gs:0x8\n"
141 "\tpush %gs:0xc\n"
142 #endif
143 "\tmov %rsp, (%rcx)\n"
144 "\tmov (%rdx), %rsp\n"
145 #if CORO_WIN_TIB
146 "\tpop %gs:0xc\n"
147 "\tpop %gs:0x8\n"
148 "\tpop %gs:0x0\n"
149 #endif
150 "\tpop %r15\n"
151 "\tpop %r14\n"
152 "\tpop %r13\n"
153 "\tpop %r12\n"
154 "\tpop %rbx\n"
155 "\tpop %rbp\n"
156 "\tpop %rdi\n"
157 "\tpop %rsi\n"
158 #else
128 #define NUM_SAVED 6 159 #define NUM_SAVED 6
129 "\tpush %rbp\n" 160 "\tpush %rbp\n"
130 "\tpush %rbx\n" 161 "\tpush %rbx\n"
131 "\tpush %r12\n" 162 "\tpush %r12\n"
132 "\tpush %r13\n" 163 "\tpush %r13\n"
133 "\tpush %r14\n" 164 "\tpush %r14\n"
134 "\tpush %r15\n" 165 "\tpush %r15\n"
135 "\tmov %rsp, (%rdi)\n" 166 "\tmov %rsp, (%rdi)\n"
136 "\tmov (%rsi), %rsp\n" 167 "\tmov (%rsi), %rsp\n"
137 "\tpop %r15\n" 168 "\tpop %r15\n"
138 "\tpop %r14\n" 169 "\tpop %r14\n"
139 "\tpop %r13\n" 170 "\tpop %r13\n"
140 "\tpop %r12\n" 171 "\tpop %r12\n"
141 "\tpop %rbx\n" 172 "\tpop %rbx\n"
142 "\tpop %rbp\n" 173 "\tpop %rbp\n"
174 #endif
143 #elif __i386 175 #elif __i386
144 #define NUM_SAVED 4 176 #define NUM_SAVED 4
145 "\tpush %ebp\n" 177 "\tpush %ebp\n"
146 "\tpush %ebx\n" 178 "\tpush %ebx\n"
147 "\tpush %esi\n" 179 "\tpush %esi\n"
148 "\tpush %edi\n" 180 "\tpush %edi\n"
181 #if CORO_WIN_TIB
182 "\tpush %fs:0\n"
183 "\tpush %fs:4\n"
184 "\tpush %fs:8\n"
185 #endif
149 "\tmov %esp, (%eax)\n" 186 "\tmov %esp, (%eax)\n"
150 "\tmov (%edx), %esp\n" 187 "\tmov (%edx), %esp\n"
188 #if CORO_WIN_TIB
189 "\tpop %fs:8\n"
190 "\tpop %fs:4\n"
191 "\tpop %fs:0\n"
192 #endif
151 "\tpop %edi\n" 193 "\tpop %edi\n"
152 "\tpop %esi\n" 194 "\tpop %esi\n"
153 "\tpop %ebx\n" 195 "\tpop %ebx\n"
154 "\tpop %ebp\n" 196 "\tpop %ebp\n"
155 #else 197 #else
156 #error unsupported architecture 198 #error unsupported architecture
157 #endif 199 #endif
158 "\tret\n" 200 "\tret\n"
159 ); 201 );
195 perror ("sigaction"); 237 perror ("sigaction");
196 abort (); 238 abort ();
197 } 239 }
198 240
199 /* set the new stack */ 241 /* set the new stack */
200 nstk.ss_sp = STACK_ADJUST_PTR (sptr,ssize); /* yes, some platforms (IRIX) get this wrong. */ 242 nstk.ss_sp = STACK_ADJUST_PTR (sptr, ssize); /* yes, some platforms (IRIX) get this wrong. */
201 nstk.ss_size = STACK_ADJUST_SIZE (sptr,ssize); 243 nstk.ss_size = STACK_ADJUST_SIZE (sptr, ssize);
202 nstk.ss_flags = 0; 244 nstk.ss_flags = 0;
203 245
204 if (sigaltstack (&nstk, &ostk) < 0) 246 if (sigaltstack (&nstk, &ostk) < 0)
205 { 247 {
206 perror ("sigaltstack"); 248 perror ("sigaltstack");
229 sigaction (SIGUSR2, &osa, 0); 271 sigaction (SIGUSR2, &osa, 0);
230 sigprocmask (SIG_SETMASK, &osig, 0); 272 sigprocmask (SIG_SETMASK, &osig, 0);
231 273
232# elif CORO_LOSER 274# elif CORO_LOSER
233 275
234 setjmp (ctx->env); 276 coro_setjmp (ctx->env);
235 #if __CYGWIN__ 277 #if __CYGWIN__ && __i386
278 ctx->env[8] = (long) coro_init;
236 ctx->env[7] = (long)((char *)sptr + ssize) - sizeof (long); 279 ctx->env[7] = (long) ((char *)sptr + ssize) - sizeof (long);
237 ctx->env[8] = (long)coro_init; 280 #elif __CYGWIN__ && __x86_64
281 ctx->env[7] = (long) coro_init;
282 ctx->env[6] = (long) ((char *)sptr + ssize) - sizeof (long);
283 #elif defined(__MINGW32__)
284 ctx->env[5] = (long) coro_init;
285 ctx->env[4] = (long) ((char *)sptr + ssize) - sizeof (long);
238 #elif defined(_M_IX86) 286 #elif defined(_M_IX86)
239 ((_JUMP_BUFFER *)&ctx->env)->Eip = (long)coro_init; 287 ((_JUMP_BUFFER *)&ctx->env)->Eip = (long) coro_init;
240 ((_JUMP_BUFFER *)&ctx->env)->Esp = (long)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); 288 ((_JUMP_BUFFER *)&ctx->env)->Esp = (long) STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
241 #elif defined(_M_AMD64) 289 #elif defined(_M_AMD64)
242 ((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64)coro_init; 290 ((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64) coro_init;
243 ((_JUMP_BUFFER *)&ctx->env)->Rsp = (__int64)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); 291 ((_JUMP_BUFFER *)&ctx->env)->Rsp = (__int64) STACK_ADJUST_PTR (sptr, ssize) - sizeof (__int64);
244 #elif defined(_M_IA64) 292 #elif defined(_M_IA64)
245 ((_JUMP_BUFFER *)&ctx->env)->StIIP = (__int64)coro_init; 293 ((_JUMP_BUFFER *)&ctx->env)->StIIP = (__int64) coro_init;
246 ((_JUMP_BUFFER *)&ctx->env)->IntSp = (__int64)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); 294 ((_JUMP_BUFFER *)&ctx->env)->IntSp = (__int64) STACK_ADJUST_PTR (sptr, ssize) - sizeof (__int64);
247 #else 295 #else
248 #error "microsoft libc or architecture not supported" 296 #error "microsoft libc or architecture not supported"
249 #endif 297 #endif
250 298
251# elif CORO_LINUX 299# elif CORO_LINUX
252 300
253 _setjmp (ctx->env); 301 coro_setjmp (ctx->env);
254 #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP) 302 #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP)
255 ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; 303 ctx->env[0].__jmpbuf[JB_PC] = (long) coro_init;
256 ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); 304 ctx->env[0].__jmpbuf[JB_SP] = (long) STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
257 #elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__) 305 #elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__)
258 ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; 306 ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init;
259 ctx->env[0].__jmpbuf[0].__sp = (int *)((char *)sptr + ssize) - sizeof (long); 307 ctx->env[0].__jmpbuf[0].__sp = (int *) ((char *)sptr + ssize) - sizeof (long);
260 #elif defined (__GNU_LIBRARY__) && defined (__i386__) 308 #elif defined (__GNU_LIBRARY__) && defined (__i386__)
261 ctx->env[0].__jmpbuf[0].__pc = (char *)coro_init; 309 ctx->env[0].__jmpbuf[0].__pc = (char *) coro_init;
262 ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize) - sizeof (long); 310 ctx->env[0].__jmpbuf[0].__sp = (void *) ((char *)sptr + ssize) - sizeof (long);
263 #elif defined (__GNU_LIBRARY__) && defined (__amd64__) 311 #elif defined (__GNU_LIBRARY__) && defined (__amd64__)
264 ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; 312 ctx->env[0].__jmpbuf[JB_PC] = (long) coro_init;
265 ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize) - sizeof (long); 313 ctx->env[0].__jmpbuf[0].__sp = (void *) ((char *)sptr + ssize) - sizeof (long);
266 #else 314 #else
267 #error "linux libc or architecture not supported" 315 #error "linux libc or architecture not supported"
268 #endif 316 #endif
269 317
270# elif CORO_IRIX 318# elif CORO_IRIX
271 319
272 sigsetjmp (ctx->env, 0); 320 coro_setjmp (ctx->env, 0);
273 ctx->env[JB_PC] = (__uint64_t)coro_init; 321 ctx->env[JB_PC] = (__uint64_t)coro_init;
274 ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); 322 ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
275 323
276# elif CORO_ASM 324# elif CORO_ASM
277 325
278 ctx->sp = (void **)(ssize + (char *)sptr); 326 ctx->sp = (void **)(ssize + (char *)sptr);
279 *--ctx->sp = (void *)abort; /* needed for alignment only */ 327 *--ctx->sp = (void *)abort; /* needed for alignment only */
280 *--ctx->sp = (void *)coro_init; 328 *--ctx->sp = (void *)coro_init;
329
330 #if CORO_WIN_TIB
331 *--ctx->sp = 0; /* ExceptionList */
332 *--ctx->sp = (char *)sptr + ssize; /* StackBase */
333 *--ctx->sp = sptr; /* StackLimit */
334 #endif
335
281 ctx->sp -= NUM_SAVED; 336 ctx->sp -= NUM_SAVED;
337 memset (ctx->sp, 0, sizeof (*ctx->sp) * NUM_SAVED);
282 338
283# elif CORO_UCONTEXT 339# elif CORO_UCONTEXT
284 340
285 getcontext (&(ctx->uc)); 341 getcontext (&(ctx->uc));
286 342
341void 397void
342coro_transfer (coro_context *prev, coro_context *next) 398coro_transfer (coro_context *prev, coro_context *next)
343{ 399{
344 pthread_cond_signal (&next->cv); 400 pthread_cond_signal (&next->cv);
345 pthread_cond_wait (&prev->cv, &coro_mutex); 401 pthread_cond_wait (&prev->cv, &coro_mutex);
402#if __FreeBSD__ /* freebsd is of course broken and needs manual testcancel calls... yay... */
403 pthread_testcancel ();
404#endif
346} 405}
347 406
348void 407void
349coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize) 408coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize)
350{ 409{
371 args.arg = arg; 430 args.arg = arg;
372 args.self = ctx; 431 args.self = ctx;
373 args.main = &nctx; 432 args.main = &nctx;
374 433
375 pthread_attr_init (&attr); 434 pthread_attr_init (&attr);
435#if __UCLIBC__
436 /* exists, but is borked */
437 /*pthread_attr_setstacksize (&attr, (size_t)ssize);*/
438#else
376 pthread_attr_setstack (&attr, sptr, (size_t)ssize); 439 pthread_attr_setstack (&attr, sptr, (size_t)ssize);
440#endif
377 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); 441 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
378 pthread_create (&ctx->id, &attr, coro_init, &args); 442 pthread_create (&ctx->id, &attr, coro_init, &args);
379 443
380 coro_transfer (args.main, args.self); 444 coro_transfer (args.main, args.self);
381 } 445 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines