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

Comparing libcoro/coro.c (file contents):
Revision 1.38 by root, Fri Nov 7 20:12:26 2008 UTC vs.
Revision 1.39 by root, Sat Nov 8 04:31:28 2008 UTC

40 40
41#include "coro.h" 41#include "coro.h"
42 42
43#include <string.h> 43#include <string.h>
44 44
45/*****************************************************************************/
46/* ucontext/setjmp/asm backends */
47/*****************************************************************************/
48#if CORO_UCONTEXT || CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX || CORO_ASM
49
50# if CORO_UCONTEXT
51# include <stddef.h>
52# endif
53
45#if !defined(STACK_ADJUST_PTR) 54# if !defined(STACK_ADJUST_PTR)
55# if __sgi
46/* IRIX is decidedly NON-unix */ 56/* IRIX is decidedly NON-unix */
47# if __sgi
48# define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss) - 8) 57# define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss) - 8)
49# define STACK_ADJUST_SIZE(sp,ss) ((ss) - 8) 58# define STACK_ADJUST_SIZE(sp,ss) ((ss) - 8)
50# elif (__i386__ && CORO_LINUX) || (_M_IX86 && CORO_LOSER) 59# elif (__i386__ && CORO_LINUX) || (_M_IX86 && CORO_LOSER)
51# define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss)) 60# define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss))
52# define STACK_ADJUST_SIZE(sp,ss) (ss) 61# define STACK_ADJUST_SIZE(sp,ss) (ss)
53# elif (__amd64__ && CORO_LINUX) || ((_M_AMD64 || _M_IA64) && CORO_LOSER) 62# elif (__amd64__ && CORO_LINUX) || ((_M_AMD64 || _M_IA64) && CORO_LOSER)
54# define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss) - 8) 63# define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss) - 8)
55# define STACK_ADJUST_SIZE(sp,ss) (ss) 64# define STACK_ADJUST_SIZE(sp,ss) (ss)
56# else 65# else
57# define STACK_ADJUST_PTR(sp,ss) (sp) 66# define STACK_ADJUST_PTR(sp,ss) (sp)
58# define STACK_ADJUST_SIZE(sp,ss) (ss) 67# define STACK_ADJUST_SIZE(sp,ss) (ss)
59# endif 68# endif
60#endif 69# endif
61
62#if CORO_UCONTEXT
63# include <stddef.h>
64#endif
65
66#if CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX || CORO_ASM
67 70
68# include <stdlib.h> 71# include <stdlib.h>
69 72
70# if CORO_SJLJ 73# if CORO_SJLJ
71# include <stdio.h> 74# include <stdio.h>
76static volatile coro_func coro_init_func; 79static volatile coro_func coro_init_func;
77static volatile void *coro_init_arg; 80static volatile void *coro_init_arg;
78static volatile coro_context *new_coro, *create_coro; 81static volatile coro_context *new_coro, *create_coro;
79 82
80/* what we really want to detect here is wether we use a new-enough version of GAS */ 83/* what we really want to detect here is wether we use a new-enough version of GAS */
81/* instead, check for gcc 3, ELF and GNU/Linux and hope for the best */ 84/* with dwarf debug info. instead, check for gcc 3, ELF and GNU/Linux and hope for the best */
82# if __GNUC__ >= 3 && __ELF__ && __linux__ 85# if __GNUC__ >= 3 && __ELF__ && __linux__
83# define HAVE_CFI 1 86# define HAVE_CFI 1
84# endif 87# endif
85 88
86static void 89static void
97 abort (); 100 abort ();
98} 101}
99 102
100# if CORO_SJLJ 103# if CORO_SJLJ
101 104
102static volatile int trampoline_count; 105static volatile int trampoline_done;
103 106
104/* trampoline signal handler */ 107/* trampoline signal handler */
105static void 108static void
106trampoline (int sig) 109trampoline (int sig)
107{ 110{
108 if (setjmp (((coro_context *)new_coro)->env)) 111 if (
112 #if _XOPEN_UNIX > 0
113 _setjmp (new_coro->env)
114 #else
115 setjmp (new_coro->env)
116 #endif
109 { 117 ) {
110# if HAVE_CFI 118 #if HAVE_CFI
111 asm (".cfi_startproc"); 119 asm (".cfi_startproc");
112# endif 120 #endif
113 coro_init (); /* start it */ 121 coro_init (); /* start it */
114# if HAVE_CFI 122 #if HAVE_CFI
115 asm (".cfi_endproc"); 123 asm (".cfi_endproc");
116# endif 124 #endif
117 } 125 }
118 else 126 else
119 trampoline_count++; 127 trampoline_done = 1;
120} 128}
121 129
122# endif
123
124#endif 130# endif
125 131
126#if CORO_ASM 132# if CORO_ASM
127 133
128 asm ( 134 asm (
129 ".text\n" 135 ".text\n"
130 ".globl coro_transfer\n" 136 ".globl coro_transfer\n"
131 ".type coro_transfer, @function\n" 137 ".type coro_transfer, @function\n"
132 "coro_transfer:\n" 138 "coro_transfer:\n"
133# if __amd64 139 #if __amd64
134# define NUM_SAVED 6 140 #define NUM_SAVED 6
135 "\tpush %rbp\n" 141 "\tpush %rbp\n"
136 "\tpush %rbx\n" 142 "\tpush %rbx\n"
137 "\tpush %r12\n" 143 "\tpush %r12\n"
138 "\tpush %r13\n" 144 "\tpush %r13\n"
139 "\tpush %r14\n" 145 "\tpush %r14\n"
140 "\tpush %r15\n" 146 "\tpush %r15\n"
141 "\tmov %rsp, (%rdi)\n" 147 "\tmov %rsp, (%rdi)\n"
142 "\tmov (%rsi), %rsp\n" 148 "\tmov (%rsi), %rsp\n"
143 "\tpop %r15\n" 149 "\tpop %r15\n"
144 "\tpop %r14\n" 150 "\tpop %r14\n"
145 "\tpop %r13\n" 151 "\tpop %r13\n"
146 "\tpop %r12\n" 152 "\tpop %r12\n"
147 "\tpop %rbx\n" 153 "\tpop %rbx\n"
148 "\tpop %rbp\n" 154 "\tpop %rbp\n"
149# elif __i386 155 #elif __i386
150# define NUM_SAVED 4 156 #define NUM_SAVED 4
151 "\tpush %ebp\n" 157 "\tpush %ebp\n"
152 "\tpush %ebx\n" 158 "\tpush %ebx\n"
153 "\tpush %esi\n" 159 "\tpush %esi\n"
154 "\tpush %edi\n" 160 "\tpush %edi\n"
155 "\tmov %esp, (%eax)\n" 161 "\tmov %esp, (%eax)\n"
156 "\tmov (%edx), %esp\n" 162 "\tmov (%edx), %esp\n"
157 "\tpop %edi\n" 163 "\tpop %edi\n"
158 "\tpop %esi\n" 164 "\tpop %esi\n"
159 "\tpop %ebx\n" 165 "\tpop %ebx\n"
160 "\tpop %ebp\n" 166 "\tpop %ebp\n"
161# else 167 #else
162# error unsupported architecture 168 #error unsupported architecture
163# endif 169 #endif
164 "\tret\n" 170 "\tret\n"
165 ); 171 );
166 172
173# endif
174
175void
176coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize)
177{
178 coro_context nctx;
179# if CORO_SJLJ
180 stack_t ostk, nstk;
181 struct sigaction osa, nsa;
182 sigset_t nsig, osig;
183# endif
184
185 if (!coro)
186 return;
187
188 coro_init_func = coro;
189 coro_init_arg = arg;
190
191 new_coro = ctx;
192 create_coro = &nctx;
193
194# if CORO_SJLJ
195 /* we use SIGUSR2. first block it, then fiddle with it. */
196
197 sigemptyset (&nsig);
198 sigaddset (&nsig, SIGUSR2);
199 sigprocmask (SIG_BLOCK, &nsig, &osig);
200
201 nsa.sa_handler = trampoline;
202 sigemptyset (&nsa.sa_mask);
203 nsa.sa_flags = SA_ONSTACK;
204
205 if (sigaction (SIGUSR2, &nsa, &osa))
206 {
207 perror ("sigaction");
208 abort ();
209 }
210
211 /* set the new stack */
212 nstk.ss_sp = STACK_ADJUST_PTR (sptr,ssize); /* yes, some platforms (IRIX) get this wrong. */
213 nstk.ss_size = STACK_ADJUST_SIZE (sptr,ssize);
214 nstk.ss_flags = 0;
215
216 if (sigaltstack (&nstk, &ostk) < 0)
217 {
218 perror ("sigaltstack");
219 abort ();
220 }
221
222 trampoline_done = 0;
223 kill (getpid (), SIGUSR2);
224 sigfillset (&nsig); sigdelset (&nsig, SIGUSR2);
225
226 while (!trampoline_done)
227 sigsuspend (&nsig);
228
229 sigaltstack (0, &nstk);
230 nstk.ss_flags = SS_DISABLE;
231 if (sigaltstack (&nstk, 0) < 0)
232 perror ("sigaltstack");
233
234 sigaltstack (0, &nstk);
235 if (~nstk.ss_flags & SS_DISABLE)
236 abort ();
237
238 if (~ostk.ss_flags & SS_DISABLE)
239 sigaltstack (&ostk, 0);
240
241 sigaction (SIGUSR2, &osa, 0);
242 sigprocmask (SIG_SETMASK, &osig, 0);
243
244# elif CORO_LOSER
245
246 setjmp (ctx->env);
247 #if __CYGWIN__
248 ctx->env[7] = (long)((char *)sptr + ssize) - sizeof (long);
249 ctx->env[8] = (long)coro_init;
250 #elif defined(_M_IX86)
251 ((_JUMP_BUFFER *)&ctx->env)->Eip = (long)coro_init;
252 ((_JUMP_BUFFER *)&ctx->env)->Esp = (long)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
253 #elif defined(_M_AMD64)
254 ((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64)coro_init;
255 ((_JUMP_BUFFER *)&ctx->env)->Rsp = (__int64)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
256 #elif defined(_M_IA64)
257 ((_JUMP_BUFFER *)&ctx->env)->StIIP = (__int64)coro_init;
258 ((_JUMP_BUFFER *)&ctx->env)->IntSp = (__int64)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
259 #else
260 #error "microsoft libc or architecture not supported"
261 #endif
262
263# elif CORO_LINUX
264
265 _setjmp (ctx->env);
266 #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP)
267 ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init;
268 ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
269 #elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__)
270 ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init;
271 ctx->env[0].__jmpbuf[0].__sp = (int *)((char *)sptr + ssize) - sizeof (long);
272 #elif defined (__GNU_LIBRARY__) && defined (__i386__)
273 ctx->env[0].__jmpbuf[0].__pc = (char *)coro_init;
274 ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize) - sizeof (long);
275 #elif defined (__GNU_LIBRARY__) && defined (__amd64__)
276 ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init;
277 ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize) - sizeof (long);
278 #else
279 #error "linux libc or architecture not supported"
280 #endif
281
282# elif CORO_IRIX
283
284 setjmp (ctx->env);
285 ctx->env[JB_PC] = (__uint64_t)coro_init;
286 ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
287
288# elif CORO_ASM
289
290 ctx->sp = (volatile void **)(ssize + (char *)sptr);
291 *--ctx->sp = (void *)abort; /* needed for alignment only */
292 *--ctx->sp = (void *)coro_init;
293 ctx->sp -= NUM_SAVED;
294
295# elif CORO_UCONTEXT
296
297 getcontext (&(ctx->uc));
298
299 ctx->uc.uc_link = 0;
300 ctx->uc.uc_stack.ss_sp = sptr;
301 ctx->uc.uc_stack.ss_size = (size_t)ssize;
302 ctx->uc.uc_stack.ss_flags = 0;
303
304 makecontext (&(ctx->uc), (void (*)())coro_init, 0);
305
306# endif
307
308 coro_transfer (create_coro, new_coro);
309}
310
167#endif 311#endif
312
313/*****************************************************************************/
314/* pthread backend */
315/*****************************************************************************/
168 316
169#if CORO_PTHREAD 317#if CORO_PTHREAD
170 318
171/* this mutex will be locked by the running coroutine */ 319/* this mutex will be locked by the running coroutine */
172pthread_mutex_t coro_mutex = PTHREAD_MUTEX_INITIALIZER; 320pthread_mutex_t coro_mutex = PTHREAD_MUTEX_INITIALIZER;
186{ 334{
187 pthread_mutex_unlock ((pthread_mutex_t *)arg); 335 pthread_mutex_unlock ((pthread_mutex_t *)arg);
188} 336}
189 337
190static void * 338static void *
191trampoline (void *args_) 339coro_init (void *args_)
192{ 340{
193 struct coro_init_args *args = (struct coro_init_args *)args_; 341 struct coro_init_args *args = (struct coro_init_args *)args_;
194 coro_func func = args->func; 342 coro_func func = args->func;
195 void *arg = args->arg; 343 void *arg = args->arg;
196 344
208void 356void
209coro_transfer (coro_context *prev, coro_context *next) 357coro_transfer (coro_context *prev, coro_context *next)
210{ 358{
211 pthread_cond_signal (&next->cv); 359 pthread_cond_signal (&next->cv);
212 pthread_cond_wait (&prev->cv, &coro_mutex); 360 pthread_cond_wait (&prev->cv, &coro_mutex);
361}
362
363void
364coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize)
365{
366 static coro_context nctx;
367 static int once;
368
369 if (!once)
370 {
371 once = 1;
372
373 pthread_mutex_lock (&coro_mutex);
374 pthread_cond_init (&nctx.cv, 0);
375 null_tid = pthread_self ();
376 }
377
378 pthread_cond_init (&ctx->cv, 0);
379
380 if (coro)
381 {
382 pthread_attr_t attr;
383 struct coro_init_args args;
384
385 args.func = coro;
386 args.arg = arg;
387 args.self = ctx;
388 args.main = &nctx;
389
390 pthread_attr_init (&attr);
391 pthread_attr_setstack (&attr, sptr, (size_t)ssize);
392 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
393 pthread_create (&ctx->id, &attr, coro_init, &args);
394
395 coro_transfer (args.main, args.self);
396 }
397 else
398 ctx->id = null_tid;
213} 399}
214 400
215void 401void
216coro_destroy (coro_context *ctx) 402coro_destroy (coro_context *ctx)
217{ 403{
226 pthread_cond_destroy (&ctx->cv); 412 pthread_cond_destroy (&ctx->cv);
227} 413}
228 414
229#endif 415#endif
230 416
231/* initialize a machine state */
232void
233coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize)
234{
235#if CORO_UCONTEXT
236
237 if (!coro)
238 return;
239
240 getcontext (&(ctx->uc));
241
242 ctx->uc.uc_link = 0;
243 ctx->uc.uc_stack.ss_sp = STACK_ADJUST_PTR (sptr,ssize);
244 ctx->uc.uc_stack.ss_size = (size_t)STACK_ADJUST_SIZE (sptr,ssize);
245 ctx->uc.uc_stack.ss_flags = 0;
246
247 makecontext (&(ctx->uc), (void (*)())coro, 1, arg);
248
249#elif CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX || CORO_ASM
250
251# if CORO_SJLJ
252 stack_t ostk, nstk;
253 struct sigaction osa, nsa;
254 sigset_t nsig, osig;
255# endif
256 coro_context nctx;
257
258 if (!coro)
259 return;
260
261 coro_init_func = coro;
262 coro_init_arg = arg;
263
264 new_coro = ctx;
265 create_coro = &nctx;
266
267# if CORO_SJLJ
268 /* we use SIGUSR2. first block it, then fiddle with it. */
269
270 sigemptyset (&nsig);
271 sigaddset (&nsig, SIGUSR2);
272 sigprocmask (SIG_BLOCK, &nsig, &osig);
273
274 nsa.sa_handler = trampoline;
275 sigemptyset (&nsa.sa_mask);
276 nsa.sa_flags = SA_ONSTACK;
277
278 if (sigaction (SIGUSR2, &nsa, &osa))
279 {
280 perror ("sigaction");
281 abort ();
282 }
283
284 /* set the new stack */
285 nstk.ss_sp = STACK_ADJUST_PTR (sptr,ssize); /* yes, some platforms (IRIX) get this wrong. */
286 nstk.ss_size = STACK_ADJUST_SIZE (sptr,ssize);
287 nstk.ss_flags = 0;
288
289 if (sigaltstack (&nstk, &ostk) < 0)
290 {
291 perror ("sigaltstack");
292 abort ();
293 }
294
295 trampoline_count = 0;
296 kill (getpid (), SIGUSR2);
297 sigfillset (&nsig); sigdelset (&nsig, SIGUSR2);
298
299 while (!trampoline_count)
300 sigsuspend (&nsig);
301
302 sigaltstack (0, &nstk);
303 nstk.ss_flags = SS_DISABLE;
304 if (sigaltstack (&nstk, 0) < 0)
305 perror ("sigaltstack");
306
307 sigaltstack (0, &nstk);
308 if (~nstk.ss_flags & SS_DISABLE)
309 abort ();
310
311 if (~ostk.ss_flags & SS_DISABLE)
312 sigaltstack (&ostk, 0);
313
314 sigaction (SIGUSR2, &osa, 0);
315
316 sigprocmask (SIG_SETMASK, &osig, 0);
317
318# elif CORO_LOSER
319
320 setjmp (ctx->env);
321#if __CYGWIN__
322 ctx->env[7] = (long)((char *)sptr + ssize) - sizeof (long);
323 ctx->env[8] = (long)coro_init;
324#elif defined(_M_IX86)
325 ((_JUMP_BUFFER *)&ctx->env)->Eip = (long)coro_init;
326 ((_JUMP_BUFFER *)&ctx->env)->Esp = (long)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
327#elif defined(_M_AMD64)
328 ((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64)coro_init;
329 ((_JUMP_BUFFER *)&ctx->env)->Rsp = (__int64)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
330#elif defined(_M_IA64)
331 ((_JUMP_BUFFER *)&ctx->env)->StIIP = (__int64)coro_init;
332 ((_JUMP_BUFFER *)&ctx->env)->IntSp = (__int64)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
333#else
334# error "microsoft libc or architecture not supported"
335#endif
336
337# elif CORO_LINUX
338
339 _setjmp (ctx->env);
340#if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP)
341 ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init;
342 ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
343#elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__)
344 ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init;
345 ctx->env[0].__jmpbuf[0].__sp = (int *)((char *)sptr + ssize) - sizeof (long);
346#elif defined (__GNU_LIBRARY__) && defined (__i386__)
347 ctx->env[0].__jmpbuf[0].__pc = (char *)coro_init;
348 ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize) - sizeof (long);
349#elif defined (__GNU_LIBRARY__) && defined (__amd64__)
350 ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init;
351 ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize) - sizeof (long);
352#else
353# error "linux libc or architecture not supported"
354#endif
355
356# elif CORO_IRIX
357
358 setjmp (ctx->env);
359 ctx->env[JB_PC] = (__uint64_t)coro_init;
360 ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
361
362# elif CORO_ASM
363
364 ctx->sp = (volatile void **)(ssize + (char *)sptr);
365 *--ctx->sp = (void *)abort; /* needed for alignment only */
366 *--ctx->sp = (void *)coro_init;
367 ctx->sp -= NUM_SAVED;
368
369# endif
370
371 coro_transfer ((coro_context *)create_coro, (coro_context *)new_coro);
372
373# elif CORO_PTHREAD
374
375 static coro_context nctx;
376 static int once;
377
378 if (!once)
379 {
380 once = 1;
381
382 pthread_mutex_lock (&coro_mutex);
383 pthread_cond_init (&nctx.cv, 0);
384 null_tid = pthread_self ();
385 }
386
387 pthread_cond_init (&ctx->cv, 0);
388
389 if (coro)
390 {
391 pthread_attr_t attr;
392 struct coro_init_args args;
393
394 args.func = coro;
395 args.arg = arg;
396 args.self = ctx;
397 args.main = &nctx;
398
399 pthread_attr_init (&attr);
400 pthread_attr_setstack (&attr, sptr, (size_t)ssize);
401 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
402 pthread_create (&ctx->id, &attr, trampoline, &args);
403
404 coro_transfer (args.main, args.self);
405 }
406 else
407 ctx->id = null_tid;
408
409#else
410# error unsupported backend
411#endif
412}
413

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines