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

Comparing libcoro/coro.c (file contents):
Revision 1.65 by root, Wed Dec 5 13:10:21 2012 UTC vs.
Revision 1.73 by root, Tue Aug 14 15:46:03 2018 UTC

38 * go to Ralf S. Engelschall <rse@engelschall.com>. 38 * go to Ralf S. Engelschall <rse@engelschall.com>.
39 */ 39 */
40 40
41#include "coro.h" 41#include "coro.h"
42 42
43#include <stddef.h>
43#include <string.h> 44#include <string.h>
44 45
45/*****************************************************************************/ 46/*****************************************************************************/
46/* ucontext/setjmp/asm backends */ 47/* ucontext/setjmp/asm backends */
47/*****************************************************************************/ 48/*****************************************************************************/
87 volatile void *arg = coro_init_arg; 88 volatile void *arg = coro_init_arg;
88 89
89 coro_transfer (new_coro, create_coro); 90 coro_transfer (new_coro, create_coro);
90 91
91#if __GCC_HAVE_DWARF2_CFI_ASM && __amd64 92#if __GCC_HAVE_DWARF2_CFI_ASM && __amd64
93 /*asm (".cfi_startproc");*/
92 asm (".cfi_undefined rip"); 94 /*asm (".cfi_undefined rip");*/
93#endif 95#endif
94 96
95 func ((void *)arg); 97 func ((void *)arg);
98
99#if __GCC_HAVE_DWARF2_CFI_ASM && __amd64
100 /*asm (".cfi_endproc");*/
101#endif
96 102
97 /* the new coro returned. bad. just abort() for now */ 103 /* the new coro returned. bad. just abort() for now */
98 abort (); 104 abort ();
99} 105}
100 106
113} 119}
114 120
115# endif 121# endif
116 122
117# if CORO_ASM 123# if CORO_ASM
124
125 #if __arm__ && \
126 (defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \
127 || defined __ARM_ARCH_7R__ || defined __ARM_ARCH_7M__ \
128 || __ARM_ARCH == 7)
129 #define CORO_ARM 1
130 #endif
118 131
119 #if _WIN32 || __CYGWIN__ 132 #if _WIN32 || __CYGWIN__
120 #define CORO_WIN_TIB 1 133 #define CORO_WIN_TIB 1
121 #endif 134 #endif
122 135
203 "\tpopq %rbp\n" 216 "\tpopq %rbp\n"
204 #endif 217 #endif
205 "\tpopq %rcx\n" 218 "\tpopq %rcx\n"
206 "\tjmpq *%rcx\n" 219 "\tjmpq *%rcx\n"
207 220
208 #elif __i386 221 #elif __i386__
209 222
210 #define NUM_SAVED 4 223 #define NUM_SAVED 4
211 "\tpushl %ebp\n" 224 "\tpushl %ebp\n"
212 "\tpushl %ebx\n" 225 "\tpushl %ebx\n"
213 "\tpushl %esi\n" 226 "\tpushl %esi\n"
231 "\tpopl %ebx\n" 244 "\tpopl %ebx\n"
232 "\tpopl %ebp\n" 245 "\tpopl %ebp\n"
233 "\tpopl %ecx\n" 246 "\tpopl %ecx\n"
234 "\tjmpl *%ecx\n" 247 "\tjmpl *%ecx\n"
235 248
249 #elif CORO_ARM /* untested, what about thumb, neon, iwmmxt? */
250
251 #if __ARM_PCS_VFP
252 "\tvpush {d8-d15}\n"
253 #define NUM_SAVED (9 + 8 * 2)
254 #else
255 #define NUM_SAVED 9
256 #endif
257 "\tpush {r4-r11,lr}\n"
258 "\tstr sp, [r0]\n"
259 "\tldr sp, [r1]\n"
260 "\tpop {r4-r11,lr}\n"
261 #if __ARM_PCS_VFP
262 "\tvpop {d8-d15}\n"
263 #endif
264 "\tmov r15, lr\n"
265
266 #elif __mips__ && 0 /* untested, 32 bit only */
267
268 #define NUM_SAVED (12 + 8 * 2)
269 /* TODO: n64/o64, lw=>ld */
270
271 "\t.set nomips16\n"
272 "\t.frame $sp,112,$31\n"
273 #if __mips_soft_float
274 "\taddiu $sp,$sp,-44\n"
275 #else
276 "\taddiu $sp,$sp,-112\n"
277 "\ts.d $f30,88($sp)\n"
278 "\ts.d $f28,80($sp)\n"
279 "\ts.d $f26,72($sp)\n"
280 "\ts.d $f24,64($sp)\n"
281 "\ts.d $f22,56($sp)\n"
282 "\ts.d $f20,48($sp)\n"
283 #endif
284 "\tsw $28,40($sp)\n"
285 "\tsw $31,36($sp)\n"
286 "\tsw $fp,32($sp)\n"
287 "\tsw $23,28($sp)\n"
288 "\tsw $22,24($sp)\n"
289 "\tsw $21,20($sp)\n"
290 "\tsw $20,16($sp)\n"
291 "\tsw $19,12($sp)\n"
292 "\tsw $18,8($sp)\n"
293 "\tsw $17,4($sp)\n"
294 "\tsw $16,0($sp)\n"
295 "\tsw $sp,0($4)\n"
296 "\tlw $sp,0($5)\n"
297 #if !__mips_soft_float
298 "\tl.d $f30,88($sp)\n"
299 "\tl.d $f28,80($sp)\n"
300 "\tl.d $f26,72($sp)\n"
301 "\tl.d $f24,64($sp)\n"
302 "\tl.d $f22,56($sp)\n"
303 "\tl.d $f20,48($sp)\n"
304 #endif
305 "\tlw $28,40($sp)\n"
306 "\tlw $31,36($sp)\n"
307 "\tlw $fp,32($sp)\n"
308 "\tlw $23,28($sp)\n"
309 "\tlw $22,24($sp)\n"
310 "\tlw $21,20($sp)\n"
311 "\tlw $20,16($sp)\n"
312 "\tlw $19,12($sp)\n"
313 "\tlw $18,8($sp)\n"
314 "\tlw $17,4($sp)\n"
315 "\tlw $16,0($sp)\n"
316 "\tj $31\n"
317 #if __mips_soft_float
318 "\taddiu $sp,$sp,44\n"
319 #else
320 "\taddiu $sp,$sp,112\n"
321 #endif
322
236 #else 323 #else
237 #error unsupported architecture 324 #error unsupported architecture
238 #endif 325 #endif
239 ); 326 );
240 327
241# endif 328# endif
242 329
243void 330void
244coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize) 331coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, size_t ssize)
245{ 332{
246 coro_context nctx; 333 coro_context nctx;
247# if CORO_SJLJ 334# if CORO_SJLJ
248 stack_t ostk, nstk; 335 stack_t ostk, nstk;
249 struct sigaction osa, nsa; 336 struct sigaction osa, nsa;
310 sigprocmask (SIG_SETMASK, &osig, 0); 397 sigprocmask (SIG_SETMASK, &osig, 0);
311 398
312# elif CORO_LOSER 399# elif CORO_LOSER
313 400
314 coro_setjmp (ctx->env); 401 coro_setjmp (ctx->env);
315 #if __CYGWIN__ && __i386 402 #if __CYGWIN__ && __i386__
316 ctx->env[8] = (long) coro_init; 403 ctx->env[8] = (long) coro_init;
317 ctx->env[7] = (long) ((char *)sptr + ssize) - sizeof (long); 404 ctx->env[7] = (long) ((char *)sptr + ssize) - sizeof (long);
318 #elif __CYGWIN__ && __x86_64 405 #elif __CYGWIN__ && __x86_64__
319 ctx->env[7] = (long) coro_init; 406 ctx->env[7] = (long) coro_init;
320 ctx->env[6] = (long) ((char *)sptr + ssize) - sizeof (long); 407 ctx->env[6] = (long) ((char *)sptr + ssize) - sizeof (long);
321 #elif defined __MINGW32__ 408 #elif defined __MINGW32__
322 ctx->env[5] = (long) coro_init; 409 ctx->env[5] = (long) coro_init;
323 ctx->env[4] = (long) ((char *)sptr + ssize) - sizeof (long); 410 ctx->env[4] = (long) ((char *)sptr + ssize) - sizeof (long);
344 ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; 431 ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init;
345 ctx->env[0].__jmpbuf[0].__sp = (int *) ((char *)sptr + ssize) - sizeof (long); 432 ctx->env[0].__jmpbuf[0].__sp = (int *) ((char *)sptr + ssize) - sizeof (long);
346 #elif defined (__GNU_LIBRARY__) && defined (__i386__) 433 #elif defined (__GNU_LIBRARY__) && defined (__i386__)
347 ctx->env[0].__jmpbuf[0].__pc = (char *) coro_init; 434 ctx->env[0].__jmpbuf[0].__pc = (char *) coro_init;
348 ctx->env[0].__jmpbuf[0].__sp = (void *) ((char *)sptr + ssize) - sizeof (long); 435 ctx->env[0].__jmpbuf[0].__sp = (void *) ((char *)sptr + ssize) - sizeof (long);
349 #elif defined (__GNU_LIBRARY__) && defined (__amd64__) 436 #elif defined (__GNU_LIBRARY__) && defined (__x86_64__)
350 ctx->env[0].__jmpbuf[JB_PC] = (long) coro_init; 437 ctx->env[0].__jmpbuf[JB_PC] = (long) coro_init;
351 ctx->env[0].__jmpbuf[0].__sp = (void *) ((char *)sptr + ssize) - sizeof (long); 438 ctx->env[0].__jmpbuf[0].__sp = (void *) ((char *)sptr + ssize) - sizeof (long);
352 #else 439 #else
353 #error "linux libc or architecture not supported" 440 #error "linux libc or architecture not supported"
354 #endif 441 #endif
359 ctx->env[JB_PC] = (__uint64_t)coro_init; 446 ctx->env[JB_PC] = (__uint64_t)coro_init;
360 ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); 447 ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
361 448
362# elif CORO_ASM 449# elif CORO_ASM
363 450
451 #if __i386__ || __x86_64__
364 ctx->sp = (void **)(ssize + (char *)sptr); 452 ctx->sp = (void **)(ssize + (char *)sptr);
365 *--ctx->sp = (void *)abort; /* needed for alignment only */ 453 *--ctx->sp = (void *)abort; /* needed for alignment only */
366 *--ctx->sp = (void *)coro_init; 454 *--ctx->sp = (void *)coro_init;
367
368 #if CORO_WIN_TIB 455 #if CORO_WIN_TIB
369 *--ctx->sp = 0; /* ExceptionList */ 456 *--ctx->sp = 0; /* ExceptionList */
370 *--ctx->sp = (char *)sptr + ssize; /* StackBase */ 457 *--ctx->sp = (char *)sptr + ssize; /* StackBase */
371 *--ctx->sp = sptr; /* StackLimit */ 458 *--ctx->sp = sptr; /* StackLimit */
459 #endif
460 #elif CORO_ARM
461 /* return address stored in lr register, don't push anything */
462 #else
463 #error unsupported architecture
372 #endif 464 #endif
373 465
374 ctx->sp -= NUM_SAVED; 466 ctx->sp -= NUM_SAVED;
375 memset (ctx->sp, 0, sizeof (*ctx->sp) * NUM_SAVED); 467 memset (ctx->sp, 0, sizeof (*ctx->sp) * NUM_SAVED);
468
469 #if __i386__ || __x86_64__
470 /* done already */
471 #elif CORO_ARM
472 ctx->sp[0] = coro; /* r4 */
473 ctx->sp[1] = arg; /* r5 */
474 ctx->sp[8] = (char *)coro_init; /* lr */
475 #else
476 #error unsupported architecture
477 #endif
376 478
377# elif CORO_UCONTEXT 479# elif CORO_UCONTEXT
378 480
379 getcontext (&(ctx->uc)); 481 getcontext (&(ctx->uc));
380 482
403 coro_func func; 505 coro_func func;
404 void *arg; 506 void *arg;
405 coro_context *self, *main; 507 coro_context *self, *main;
406}; 508};
407 509
408static pthread_t null_tid;
409
410/* I'd so love to cast pthread_mutex_unlock to void (*)(void *)... */
411static void
412mutex_unlock_wrapper (void *arg)
413{
414 pthread_mutex_unlock ((pthread_mutex_t *)arg);
415}
416
417static void * 510static void *
418coro_init (void *args_) 511coro_init (void *args_)
419{ 512{
420 struct coro_init_args *args = (struct coro_init_args *)args_; 513 struct coro_init_args *args = (struct coro_init_args *)args_;
421 coro_func func = args->func; 514 coro_func func = args->func;
422 void *arg = args->arg; 515 void *arg = args->arg;
423 516
424 pthread_mutex_lock (&coro_mutex);
425
426 /* we try to be good citizens and use deferred cancellation and cleanup handlers */
427 pthread_cleanup_push (mutex_unlock_wrapper, &coro_mutex);
428 coro_transfer (args->self, args->main); 517 coro_transfer (args->self, args->main);
429 func (arg); 518 func (arg);
430 pthread_cleanup_pop (1);
431 519
432 return 0; 520 return 0;
433} 521}
434 522
435void 523void
436coro_transfer (coro_context *prev, coro_context *next) 524coro_transfer (coro_context *prev, coro_context *next)
437{ 525{
526 pthread_mutex_lock (&coro_mutex);
527
528 next->flags = 1;
438 pthread_cond_signal (&next->cv); 529 pthread_cond_signal (&next->cv);
530
531 prev->flags = 0;
532
533 while (!prev->flags)
439 pthread_cond_wait (&prev->cv, &coro_mutex); 534 pthread_cond_wait (&prev->cv, &coro_mutex);
440#if __FreeBSD__ /* freebsd is of course broken and needs manual testcancel calls... yay... */ 535
441 pthread_testcancel (); 536 if (prev->flags == 2)
442#endif 537 {
538 pthread_mutex_unlock (&coro_mutex);
539 pthread_cond_destroy (&prev->cv);
540 pthread_detach (pthread_self ());
541 pthread_exit (0);
542 }
543
544 pthread_mutex_unlock (&coro_mutex);
443} 545}
444 546
445void 547void
446coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize) 548coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, size_t ssize)
447{ 549{
448 static coro_context nctx; 550 static coro_context nctx;
449 static int once; 551 static int once;
450 552
451 if (!once) 553 if (!once)
452 { 554 {
453 once = 1; 555 once = 1;
454 556
455 pthread_mutex_lock (&coro_mutex);
456 pthread_cond_init (&nctx.cv, 0); 557 pthread_cond_init (&nctx.cv, 0);
457 null_tid = pthread_self ();
458 } 558 }
459 559
460 pthread_cond_init (&ctx->cv, 0); 560 pthread_cond_init (&ctx->cv, 0);
461 561
462 if (coro) 562 if (coro)
463 { 563 {
464 pthread_attr_t attr; 564 pthread_attr_t attr;
465 struct coro_init_args args; 565 struct coro_init_args args;
566 pthread_t id;
466 567
467 args.func = coro; 568 args.func = coro;
468 args.arg = arg; 569 args.arg = arg;
469 args.self = ctx; 570 args.self = ctx;
470 args.main = &nctx; 571 args.main = &nctx;
478 pthread_attr_setstacksize (&attr, (size_t)ssize); 579 pthread_attr_setstacksize (&attr, (size_t)ssize);
479#else 580#else
480 pthread_attr_setstack (&attr, sptr, (size_t)ssize); 581 pthread_attr_setstack (&attr, sptr, (size_t)ssize);
481#endif 582#endif
482 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); 583 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
483 pthread_create (&ctx->id, &attr, coro_init, &args); 584 pthread_create (&id, &attr, coro_init, &args);
484 585
485 coro_transfer (args.main, args.self); 586 coro_transfer (args.main, args.self);
486 } 587 }
487 else
488 ctx->id = null_tid;
489} 588}
490 589
491void 590void
492coro_destroy (coro_context *ctx) 591coro_destroy (coro_context *ctx)
493{ 592{
494 if (!pthread_equal (ctx->id, null_tid)) 593 pthread_mutex_lock (&coro_mutex);
495 { 594 ctx->flags = 2;
496 pthread_cancel (ctx->id); 595 pthread_cond_signal (&ctx->cv);
497 pthread_mutex_unlock (&coro_mutex); 596 pthread_mutex_unlock (&coro_mutex);
498 pthread_join (ctx->id, 0);
499 pthread_mutex_lock (&coro_mutex);
500 }
501
502 pthread_cond_destroy (&ctx->cv);
503} 597}
504 598
505/*****************************************************************************/ 599/*****************************************************************************/
506/* fiber backend */ 600/* fiber backend */
507/*****************************************************************************/ 601/*****************************************************************************/
508#elif CORO_FIBER 602#elif CORO_FIBER
509 603
510#define WIN32_LEAN_AND_MEAN 604#define WIN32_LEAN_AND_MEAN
605#if _WIN32_WINNT < 0x0400
606 #undef _WIN32_WINNT
511#define _WIN32_WINNT 0x0400 607 #define _WIN32_WINNT 0x0400
608#endif
512#include <windows.h> 609#include <windows.h>
513 610
514VOID CALLBACK 611VOID CALLBACK
515coro_init (PVOID arg) 612coro_init (PVOID arg)
516{ 613{
532 629
533 SwitchToFiber (next->fiber); 630 SwitchToFiber (next->fiber);
534} 631}
535 632
536void 633void
537coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize) 634coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, size_t ssize)
538{ 635{
539 ctx->fiber = 0; 636 ctx->fiber = 0;
540 ctx->coro = coro; 637 ctx->coro = coro;
541 ctx->arg = arg; 638 ctx->arg = arg;
542 639
551{ 648{
552 DeleteFiber (ctx->fiber); 649 DeleteFiber (ctx->fiber);
553} 650}
554 651
555#else 652#else
556# error unsupported backend 653 #error unsupported backend
654#endif
655
656/*****************************************************************************/
657/* stack management */
658/*****************************************************************************/
659#if CORO_STACKALLOC
660
661#include <stdlib.h>
662
663#ifndef _WIN32
664# include <unistd.h>
665#endif
666
667#if CORO_USE_VALGRIND
668# include <valgrind/valgrind.h>
669#endif
670
671#if _POSIX_MAPPED_FILES
672# include <sys/mman.h>
673# define CORO_MMAP 1
674# ifndef MAP_ANONYMOUS
675# ifdef MAP_ANON
676# define MAP_ANONYMOUS MAP_ANON
677# else
678# undef CORO_MMAP
679# endif
557#endif 680# endif
681# include <limits.h>
682#else
683# undef CORO_MMAP
684#endif
558 685
686#if _POSIX_MEMORY_PROTECTION
687# ifndef CORO_GUARDPAGES
688# define CORO_GUARDPAGES 4
689# endif
690#else
691# undef CORO_GUARDPAGES
692#endif
693
694#if !CORO_MMAP
695# undef CORO_GUARDPAGES
696#endif
697
698#if !__i386__ && !__x86_64__ && !__powerpc__ && !__arm__ && !__aarch64__ && !__m68k__ && !__alpha__ && !__mips__ && !__sparc64__
699# undef CORO_GUARDPAGES
700#endif
701
702#ifndef CORO_GUARDPAGES
703# define CORO_GUARDPAGES 0
704#endif
705
706#if !PAGESIZE
707 #if !CORO_MMAP
708 #define PAGESIZE 4096
709 #else
710 static size_t
711 coro_pagesize (void)
712 {
713 static size_t pagesize;
714
715 if (!pagesize)
716 pagesize = sysconf (_SC_PAGESIZE);
717
718 return pagesize;
719 }
720
721 #define PAGESIZE coro_pagesize ()
722 #endif
723#endif
724
725int
726coro_stack_alloc (struct coro_stack *stack, unsigned int size)
727{
728 if (!size)
729 size = 256 * 1024;
730
731 stack->sptr = 0;
732 stack->ssze = ((size_t)size * sizeof (void *) + PAGESIZE - 1) / PAGESIZE * PAGESIZE;
733
734#if CORO_FIBER
735
736 stack->sptr = (void *)stack;
737 return 1;
738
739#else
740
741 size_t ssze = stack->ssze + CORO_GUARDPAGES * PAGESIZE;
742 void *base;
743
744 #if CORO_MMAP
745 /* mmap supposedly does allocate-on-write for us */
746 base = mmap (0, ssze, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
747
748 if (base == (void *)-1)
749 {
750 /* some systems don't let us have executable heap */
751 /* we assume they won't need executable stack in that case */
752 base = mmap (0, ssze, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
753
754 if (base == (void *)-1)
755 return 0;
756 }
757
758 #if CORO_GUARDPAGES
759 mprotect (base, CORO_GUARDPAGES * PAGESIZE, PROT_NONE);
760 #endif
761
762 base = (void*)((char *)base + CORO_GUARDPAGES * PAGESIZE);
763 #else
764 base = malloc (ssze);
765 if (!base)
766 return 0;
767 #endif
768
769 #if CORO_USE_VALGRIND
770 stack->valgrind_id = VALGRIND_STACK_REGISTER ((char *)base, ((char *)base) + ssze - CORO_GUARDPAGES * PAGESIZE);
771 #endif
772
773 stack->sptr = base;
774 return 1;
775
776#endif
777}
778
779void
780coro_stack_free (struct coro_stack *stack)
781{
782#if CORO_FIBER
783 /* nop */
784#else
785 #if CORO_USE_VALGRIND
786 VALGRIND_STACK_DEREGISTER (stack->valgrind_id);
787 #endif
788
789 #if CORO_MMAP
790 if (stack->sptr)
791 munmap ((void*)((char *)stack->sptr - CORO_GUARDPAGES * PAGESIZE),
792 stack->ssze + CORO_GUARDPAGES * PAGESIZE);
793 #else
794 free (stack->sptr);
795 #endif
796#endif
797}
798
799#endif
800

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines