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.44 by root, Wed Nov 19 02:56:16 2008 UTC

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
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;
250 #elif defined(_M_IX86) 231 #elif defined(_M_IX86)
251 ((_JUMP_BUFFER *)&ctx->env)->Eip = (long)coro_init; 232 ((_JUMP_BUFFER *)&ctx->env)->Eip = (long)coro_init;
260 #error "microsoft libc or architecture not supported" 241 #error "microsoft libc or architecture not supported"
261 #endif 242 #endif
262 243
263# elif CORO_LINUX 244# elif CORO_LINUX
264 245
265 _setjmp (ctx->env); 246 coro_setjmp (ctx->env);
266 #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP) 247 #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP)
267 ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; 248 ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init;
268 ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); 249 ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
269 #elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__) 250 #elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__)
270 ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; 251 ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init;
279 #error "linux libc or architecture not supported" 260 #error "linux libc or architecture not supported"
280 #endif 261 #endif
281 262
282# elif CORO_IRIX 263# elif CORO_IRIX
283 264
284 setjmp (ctx->env); 265 coro_setjmp (ctx->env, 0);
285 ctx->env[JB_PC] = (__uint64_t)coro_init; 266 ctx->env[JB_PC] = (__uint64_t)coro_init;
286 ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); 267 ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
287 268
288# elif CORO_ASM 269# elif CORO_ASM
289 270
290 ctx->sp = (volatile void **)(ssize + (char *)sptr); 271 ctx->sp = (void **)(ssize + (char *)sptr);
291 *--ctx->sp = (void *)abort; /* needed for alignment only */ 272 *--ctx->sp = (void *)abort; /* needed for alignment only */
292 *--ctx->sp = (void *)coro_init; 273 *--ctx->sp = (void *)coro_init;
293 ctx->sp -= NUM_SAVED; 274 ctx->sp -= NUM_SAVED;
294 275
295# elif CORO_UCONTEXT 276# elif CORO_UCONTEXT
306# endif 287# endif
307 288
308 coro_transfer (create_coro, new_coro); 289 coro_transfer (create_coro, new_coro);
309} 290}
310 291
311#endif
312
313/*****************************************************************************/ 292/*****************************************************************************/
314/* pthread backend */ 293/* pthread backend */
315/*****************************************************************************/ 294/*****************************************************************************/
316
317#if CORO_PTHREAD 295#elif CORO_PTHREAD
318 296
319/* this mutex will be locked by the running coroutine */ 297/* this mutex will be locked by the running coroutine */
320pthread_mutex_t coro_mutex = PTHREAD_MUTEX_INITIALIZER; 298pthread_mutex_t coro_mutex = PTHREAD_MUTEX_INITIALIZER;
321 299
322struct coro_init_args 300struct coro_init_args
356void 334void
357coro_transfer (coro_context *prev, coro_context *next) 335coro_transfer (coro_context *prev, coro_context *next)
358{ 336{
359 pthread_cond_signal (&next->cv); 337 pthread_cond_signal (&next->cv);
360 pthread_cond_wait (&prev->cv, &coro_mutex); 338 pthread_cond_wait (&prev->cv, &coro_mutex);
339#if __FreeBSD__ /* freebsd is of course broken and needs manual testcancel calls... yay... */
340 pthread_testcancel ();
341#endif
361} 342}
362 343
363void 344void
364coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize) 345coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize)
365{ 346{
410 } 391 }
411 392
412 pthread_cond_destroy (&ctx->cv); 393 pthread_cond_destroy (&ctx->cv);
413} 394}
414 395
396#else
397# error unsupported backend
415#endif 398#endif
416 399

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines