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

Comparing libcoro/coro.c (file contents):
Revision 1.39 by root, Sat Nov 8 04:31:28 2008 UTC vs.
Revision 1.52 by root, Fri Apr 2 20:21:21 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,
74# include <stdio.h> 74# include <stdio.h>
75# include <signal.h> 75# include <signal.h>
76# include <unistd.h> 76# include <unistd.h>
77# endif 77# endif
78 78
79static volatile coro_func coro_init_func; 79static coro_func coro_init_func;
80static volatile void *coro_init_arg; 80static void *coro_init_arg;
81static volatile coro_context *new_coro, *create_coro; 81static coro_context *new_coro, *create_coro;
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 82
89static void 83static void
90coro_init (void) 84coro_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;
94 88
95 coro_transfer ((coro_context *)new_coro, (coro_context *)create_coro); 89 coro_transfer (new_coro, create_coro);
96 90
97 func ((void *)arg); 91 func ((void *)arg);
98 92
99 /* the new coro returned. bad. just abort() for now */ 93 /* the new coro returned. bad. just abort() for now */
100 abort (); 94 abort ();
106 100
107/* trampoline signal handler */ 101/* trampoline signal handler */
108static void 102static void
109trampoline (int sig) 103trampoline (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 setjmp (new_coro->env)
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
134 asm ( 115 asm (
135 ".text\n" 116 ".text\n"
136 ".globl coro_transfer\n" 117 ".globl coro_transfer\n"
137 ".type coro_transfer, @function\n" 118 ".type coro_transfer, @function\n"
138 "coro_transfer:\n" 119 "coro_transfer:\n"
120 /* windows, of course, gives a shit on the amd64 ABI and uses different registers */
121 /* http://blogs.msdn.com/freik/archive/2005/03/17/398200.aspx */
139 #if __amd64 122 #if __amd64
140 #define NUM_SAVED 6 123 #define NUM_SAVED 6
141 "\tpush %rbp\n" 124 "\tpush %rbp\n"
142 "\tpush %rbx\n" 125 "\tpush %rbx\n"
143 "\tpush %r12\n" 126 "\tpush %r12\n"
144 "\tpush %r13\n" 127 "\tpush %r13\n"
145 "\tpush %r14\n" 128 "\tpush %r14\n"
146 "\tpush %r15\n" 129 "\tpush %r15\n"
130 #if CORO_WIN_TIB
131 "\tpush %gs:0x0\n"
132 "\tpush %gs:0x8\n"
133 "\tpush %gs:0xc\n"
134 #endif
147 "\tmov %rsp, (%rdi)\n" 135 "\tmov %rsp, (%rdi)\n"
148 "\tmov (%rsi), %rsp\n" 136 "\tmov (%rsi), %rsp\n"
137 #if CORO_WIN_TIB
138 "\tpop %gs:0xc\n"
139 "\tpop %gs:0x8\n"
140 "\tpop %gs:0x0\n"
141 #endif
149 "\tpop %r15\n" 142 "\tpop %r15\n"
150 "\tpop %r14\n" 143 "\tpop %r14\n"
151 "\tpop %r13\n" 144 "\tpop %r13\n"
152 "\tpop %r12\n" 145 "\tpop %r12\n"
153 "\tpop %rbx\n" 146 "\tpop %rbx\n"
156 #define NUM_SAVED 4 149 #define NUM_SAVED 4
157 "\tpush %ebp\n" 150 "\tpush %ebp\n"
158 "\tpush %ebx\n" 151 "\tpush %ebx\n"
159 "\tpush %esi\n" 152 "\tpush %esi\n"
160 "\tpush %edi\n" 153 "\tpush %edi\n"
154 #if CORO_WIN_TIB
155 "\tpush %fs:0\n"
156 "\tpush %fs:4\n"
157 "\tpush %fs:8\n"
158 #endif
161 "\tmov %esp, (%eax)\n" 159 "\tmov %esp, (%eax)\n"
162 "\tmov (%edx), %esp\n" 160 "\tmov (%edx), %esp\n"
161 #if CORO_WIN_TIB
162 "\tpop %fs:8\n"
163 "\tpop %fs:4\n"
164 "\tpop %fs:0\n"
165 #endif
163 "\tpop %edi\n" 166 "\tpop %edi\n"
164 "\tpop %esi\n" 167 "\tpop %esi\n"
165 "\tpop %ebx\n" 168 "\tpop %ebx\n"
166 "\tpop %ebp\n" 169 "\tpop %ebp\n"
167 #else 170 #else
241 sigaction (SIGUSR2, &osa, 0); 244 sigaction (SIGUSR2, &osa, 0);
242 sigprocmask (SIG_SETMASK, &osig, 0); 245 sigprocmask (SIG_SETMASK, &osig, 0);
243 246
244# elif CORO_LOSER 247# elif CORO_LOSER
245 248
246 setjmp (ctx->env); 249 coro_setjmp (ctx->env);
247 #if __CYGWIN__ 250 #if __CYGWIN__ && __i386
251 ctx->env[8] = (long) coro_init;
248 ctx->env[7] = (long)((char *)sptr + ssize) - sizeof (long); 252 ctx->env[7] = (long) ((char *)sptr + ssize) - sizeof (long);
249 ctx->env[8] = (long)coro_init; 253 #elif __CYGWIN__ && __x86_64
254 ctx->env[7] = (long) coro_init;
255 ctx->env[6] = (long) ((char *)sptr + ssize) - sizeof (long);
256 #elif defined(__MINGW32__)
257 ctx->env[5] = (long) coro_init;
258 ctx->env[4] = (long) ((char *)sptr + ssize) - sizeof (long);
250 #elif defined(_M_IX86) 259 #elif defined(_M_IX86)
251 ((_JUMP_BUFFER *)&ctx->env)->Eip = (long)coro_init; 260 ((_JUMP_BUFFER *)&ctx->env)->Eip = (long) coro_init;
252 ((_JUMP_BUFFER *)&ctx->env)->Esp = (long)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); 261 ((_JUMP_BUFFER *)&ctx->env)->Esp = (long) STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
253 #elif defined(_M_AMD64) 262 #elif defined(_M_AMD64)
254 ((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64)coro_init; 263 ((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64) coro_init;
255 ((_JUMP_BUFFER *)&ctx->env)->Rsp = (__int64)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); 264 ((_JUMP_BUFFER *)&ctx->env)->Rsp = (__int64) STACK_ADJUST_PTR (sptr, ssize) - sizeof (__int64);
256 #elif defined(_M_IA64) 265 #elif defined(_M_IA64)
257 ((_JUMP_BUFFER *)&ctx->env)->StIIP = (__int64)coro_init; 266 ((_JUMP_BUFFER *)&ctx->env)->StIIP = (__int64) coro_init;
258 ((_JUMP_BUFFER *)&ctx->env)->IntSp = (__int64)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); 267 ((_JUMP_BUFFER *)&ctx->env)->IntSp = (__int64) STACK_ADJUST_PTR (sptr, ssize) - sizeof (__int64);
259 #else 268 #else
260 #error "microsoft libc or architecture not supported" 269 #error "microsoft libc or architecture not supported"
261 #endif 270 #endif
262 271
263# elif CORO_LINUX 272# elif CORO_LINUX
264 273
265 _setjmp (ctx->env); 274 coro_setjmp (ctx->env);
266 #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP) 275 #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP)
267 ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; 276 ctx->env[0].__jmpbuf[JB_PC] = (long) coro_init;
268 ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); 277 ctx->env[0].__jmpbuf[JB_SP] = (long) STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
269 #elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__) 278 #elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__)
270 ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; 279 ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init;
271 ctx->env[0].__jmpbuf[0].__sp = (int *)((char *)sptr + ssize) - sizeof (long); 280 ctx->env[0].__jmpbuf[0].__sp = (int *) ((char *)sptr + ssize) - sizeof (long);
272 #elif defined (__GNU_LIBRARY__) && defined (__i386__) 281 #elif defined (__GNU_LIBRARY__) && defined (__i386__)
273 ctx->env[0].__jmpbuf[0].__pc = (char *)coro_init; 282 ctx->env[0].__jmpbuf[0].__pc = (char *) coro_init;
274 ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize) - sizeof (long); 283 ctx->env[0].__jmpbuf[0].__sp = (void *) ((char *)sptr + ssize) - sizeof (long);
275 #elif defined (__GNU_LIBRARY__) && defined (__amd64__) 284 #elif defined (__GNU_LIBRARY__) && defined (__amd64__)
276 ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; 285 ctx->env[0].__jmpbuf[JB_PC] = (long) coro_init;
277 ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize) - sizeof (long); 286 ctx->env[0].__jmpbuf[0].__sp = (void *) ((char *)sptr + ssize) - sizeof (long);
278 #else 287 #else
279 #error "linux libc or architecture not supported" 288 #error "linux libc or architecture not supported"
280 #endif 289 #endif
281 290
282# elif CORO_IRIX 291# elif CORO_IRIX
283 292
284 setjmp (ctx->env); 293 coro_setjmp (ctx->env, 0);
285 ctx->env[JB_PC] = (__uint64_t)coro_init; 294 ctx->env[JB_PC] = (__uint64_t)coro_init;
286 ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); 295 ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
287 296
288# elif CORO_ASM 297# elif CORO_ASM
289 298
290 ctx->sp = (volatile void **)(ssize + (char *)sptr); 299 ctx->sp = (void **)(ssize + (char *)sptr);
291 *--ctx->sp = (void *)abort; /* needed for alignment only */ 300 *--ctx->sp = (void *)abort; /* needed for alignment only */
292 *--ctx->sp = (void *)coro_init; 301 *--ctx->sp = (void *)coro_init;
302
303 #if CORO_WIN_TIB
304 *--ctx->sp = 0; /* ExceptionList */
305 *--ctx->sp = (char *)sptr + ssize; /* StackBase */
306 *--ctx->sp = sptr; /* StackLimit */
307 #endif
308
293 ctx->sp -= NUM_SAVED; 309 ctx->sp -= NUM_SAVED;
294 310
295# elif CORO_UCONTEXT 311# elif CORO_UCONTEXT
296 312
297 getcontext (&(ctx->uc)); 313 getcontext (&(ctx->uc));
306# endif 322# endif
307 323
308 coro_transfer (create_coro, new_coro); 324 coro_transfer (create_coro, new_coro);
309} 325}
310 326
311#endif
312
313/*****************************************************************************/ 327/*****************************************************************************/
314/* pthread backend */ 328/* pthread backend */
315/*****************************************************************************/ 329/*****************************************************************************/
316
317#if CORO_PTHREAD 330#elif CORO_PTHREAD
318 331
319/* this mutex will be locked by the running coroutine */ 332/* this mutex will be locked by the running coroutine */
320pthread_mutex_t coro_mutex = PTHREAD_MUTEX_INITIALIZER; 333pthread_mutex_t coro_mutex = PTHREAD_MUTEX_INITIALIZER;
321 334
322struct coro_init_args 335struct coro_init_args
356void 369void
357coro_transfer (coro_context *prev, coro_context *next) 370coro_transfer (coro_context *prev, coro_context *next)
358{ 371{
359 pthread_cond_signal (&next->cv); 372 pthread_cond_signal (&next->cv);
360 pthread_cond_wait (&prev->cv, &coro_mutex); 373 pthread_cond_wait (&prev->cv, &coro_mutex);
374#if __FreeBSD__ /* freebsd is of course broken and needs manual testcancel calls... yay... */
375 pthread_testcancel ();
376#endif
361} 377}
362 378
363void 379void
364coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize) 380coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize)
365{ 381{
410 } 426 }
411 427
412 pthread_cond_destroy (&ctx->cv); 428 pthread_cond_destroy (&ctx->cv);
413} 429}
414 430
431#else
432# error unsupported backend
415#endif 433#endif
416 434

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines