ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.84 by root, Fri Nov 24 00:31:21 2006 UTC vs.
Revision 1.97 by root, Sun Nov 26 21:21:14 2006 UTC

27# ifndef IS_PADCONST 27# ifndef IS_PADCONST
28# define IS_PADCONST(v) 0 28# define IS_PADCONST(v) 0
29# endif 29# endif
30#endif 30#endif
31 31
32#include <stdio.h>
32#include <errno.h> 33#include <errno.h>
33 34
34#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 35#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
35# undef STACKGUARD 36# undef STACKGUARD
36#endif 37#endif
57# else 58# else
58# define BOOT_PAGESIZE 59# define BOOT_PAGESIZE
59# endif 60# endif
60#endif 61#endif
61 62
62#define SUB_INIT "Coro::State::initialize"
63#define UCORO_STATE "_coro_state"
64
65/* The next macro should declare a variable stacklevel that contains and approximation 63/* The next macro should declare a variable stacklevel that contains and approximation
66 * to the current C stack pointer. Its property is that it changes with each call 64 * to the current C stack pointer. Its property is that it changes with each call
67 * and should be unique. */ 65 * and should be unique. */
66#define dSTACKLEVEL int stacklevel
68#define dSTACKLEVEL void *stacklevel = &stacklevel 67#define STACKLEVEL ((void *)&stacklevel)
69 68
70#define IN_DESTRUCT (PL_main_cv == Nullcv) 69#define IN_DESTRUCT (PL_main_cv == Nullcv)
71 70
72#define labs(l) ((l) >= 0 ? (l) : -(l))
73
74#include "CoroAPI.h" 71#include "CoroAPI.h"
72
73#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
75 74
76#ifdef USE_ITHREADS 75#ifdef USE_ITHREADS
77static perl_mutex coro_mutex; 76static perl_mutex coro_mutex;
78# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 77# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
79# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 78# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
82# define UNLOCK (void)0 81# define UNLOCK (void)0
83#endif 82#endif
84 83
85static struct CoroAPI coroapi; 84static struct CoroAPI coroapi;
86static AV *main_mainstack; /* used to differentiate between $main and others */ 85static AV *main_mainstack; /* used to differentiate between $main and others */
87static HV *coro_state_stash; 86static HV *coro_state_stash, *coro_stash;
88static SV *ucoro_state_sv;
89static U32 ucoro_state_hash;
90static SV *coro_mortal; /* will be freed after next transfer */ 87static SV *coro_mortal; /* will be freed after next transfer */
91 88
92/* this is actually not only the c stack but also c registers etc... */ 89/* this is a structure representing a c-level coroutine */
93typedef struct { 90typedef struct coro_stack {
94 int refcnt; /* pointer reference counter */ 91 struct coro_stack *next;
95 int usecnt; /* shared by how many coroutines */
96 int gencnt; /* generation counter */
97 92
98 coro_context cctx; 93 /* the stack */
99
100 void *sptr; 94 void *sptr;
101 long ssize; /* positive == mmap, otherwise malloc */ 95 long ssize; /* positive == mmap, otherwise malloc */
96
97 /* cpu state */
98 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
99 JMPENV *top_env;
100 coro_context cctx;
102} coro_stack; 101} coro_stack;
103 102
103/* this is a structure representing a perl-level coroutine */
104struct coro { 104struct coro {
105 /* the optional C context */ 105 /* the c coroutine allocated to this perl coroutine, if any */
106 coro_stack *stack; 106 coro_stack *stack;
107 void *cursp; 107
108 /* data associated with this coroutine (initial args) */
109 AV *args;
108 int gencnt; 110 int refcnt;
109 111
110 /* optionally saved, might be zero */ 112 /* optionally saved, might be zero */
111 AV *defav; 113 AV *defav;
112 SV *defsv; 114 SV *defsv;
113 SV *errsv; 115 SV *errsv;
143 OP **retstack; 145 OP **retstack;
144 I32 retstack_ix; 146 I32 retstack_ix;
145 I32 retstack_max; 147 I32 retstack_max;
146 PMOP *curpm; 148 PMOP *curpm;
147 COP *curcop; 149 COP *curcop;
148 JMPENV *top_env;
149 150
150 /* data associated with this coroutine (initial args) */ 151 /* coro process data */
151 AV *args; 152 int prio;
152}; 153};
153 154
154typedef struct coro *Coro__State; 155typedef struct coro *Coro__State;
155typedef struct coro *Coro__State_or_hashref; 156typedef struct coro *Coro__State_or_hashref;
156 157
228 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 229 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
229 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 230 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
230 else 231 else
231 { 232 {
232#if 0 233#if 0
233 /* this should work - but it doesn't :( */ 234 /* this is probably cleaner, but also slower? */
234 CV *cp = Perl_cv_clone (aTHX_ cv); 235 CV *cp = Perl_cv_clone (aTHX_ cv);
235 CvPADLIST (cv) = CvPADLIST (cp); 236 CvPADLIST (cv) = CvPADLIST (cp);
236 CvPADLIST (cp) = 0; 237 CvPADLIST (cp) = 0;
237 SvREFCNT_dec (cp); 238 SvREFCNT_dec (cp);
238#else 239#else
299 PL_retstack_ix = c->retstack_ix; 300 PL_retstack_ix = c->retstack_ix;
300 PL_retstack_max = c->retstack_max; 301 PL_retstack_max = c->retstack_max;
301#endif 302#endif
302 PL_curpm = c->curpm; 303 PL_curpm = c->curpm;
303 PL_curcop = c->curcop; 304 PL_curcop = c->curcop;
304 PL_top_env = c->top_env;
305 305
306 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 306 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
307 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 307 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
308 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 308 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
309 309
427 c->retstack_ix = PL_retstack_ix; 427 c->retstack_ix = PL_retstack_ix;
428 c->retstack_max = PL_retstack_max; 428 c->retstack_max = PL_retstack_max;
429#endif 429#endif
430 c->curpm = PL_curpm; 430 c->curpm = PL_curpm;
431 c->curcop = PL_curcop; 431 c->curcop = PL_curcop;
432 c->top_env = PL_top_env;
433} 432}
434 433
435/* 434/*
436 * allocate various perl stacks. This is an exact copy 435 * allocate various perl stacks. This is an exact copy
437 * of perl.c:init_stacks, except that it uses less memory 436 * of perl.c:init_stacks, except that it uses less memory
531 Safefree (PL_retstack); 530 Safefree (PL_retstack);
532#endif 531#endif
533} 532}
534 533
535static void 534static void
536allocate_stack (Coro__State ctx, int alloc) 535setup_coro (struct coro *coro)
537{
538 coro_stack *stack;
539
540 New (0, stack, 1, coro_stack);
541
542 stack->refcnt = 1;
543 stack->usecnt = 1;
544 stack->gencnt = ctx->gencnt = 0;
545
546 if (alloc)
547 {
548#if HAVE_MMAP
549 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */
550 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
551 if (stack->sptr != (void *)-1)
552 {
553# if STACKGUARD
554 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
555# endif
556 }
557 else
558#endif
559 {
560 stack->ssize = - (STACKSIZE * (long)sizeof (long));
561 New (0, stack->sptr, STACKSIZE, long);
562 }
563 }
564 else
565 stack->sptr = 0;
566
567 ctx->stack = stack;
568}
569
570static void
571deallocate_stack (Coro__State ctx)
572{
573 coro_stack *stack = ctx->stack;
574
575 ctx->stack = 0;
576
577 if (stack)
578 {
579 if (!--stack->refcnt)
580 {
581#ifdef HAVE_MMAP
582 if (stack->ssize > 0 && stack->sptr)
583 munmap (stack->sptr, stack->ssize);
584 else
585#endif
586 Safefree (stack->sptr);
587
588 Safefree (stack);
589 }
590 else if (ctx->gencnt == stack->gencnt)
591 --stack->usecnt;
592 }
593}
594
595static void
596setup_coro (void *arg)
597{ 536{
598 /* 537 /*
599 * emulate part of the perl startup here. 538 * emulate part of the perl startup here.
600 */ 539 */
601 dTHX; 540 dTHX;
602 dSP; 541 dSP;
603 Coro__State ctx = (Coro__State)arg; 542 UNOP myop;
604 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); 543 SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE);
605 544
606 coro_init_stacks (aTHX); 545 coro_init_stacks (aTHX);
607 /*PL_curcop = 0;*/ 546 /*PL_curcop = 0;*/
608 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 547 /*PL_in_eval = PL_in_eval;*/ /* inherit */
609 SvREFCNT_dec (GvAV (PL_defgv)); 548 SvREFCNT_dec (GvAV (PL_defgv));
610 GvAV (PL_defgv) = ctx->args; ctx->args = 0; 549 GvAV (PL_defgv) = coro->args; coro->args = 0;
611 550
612 SPAGAIN; 551 SPAGAIN;
613 552
614 if (ctx->stack) 553 Zero (&myop, 1, UNOP);
554 myop.op_next = Nullop;
555 myop.op_flags = OPf_WANT_VOID;
556
557 PL_op = (OP *)&myop;
558
559 PUSHMARK(SP);
560 XPUSHs (sub_init);
561 PUTBACK;
562 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
563 SPAGAIN;
564
565 ENTER; /* necessary e.g. for dounwind */
566}
567
568static void
569free_coro_mortal ()
570{
571 if (coro_mortal)
615 { 572 {
616 ctx->cursp = 0; 573 dTHX;
617 574
618 PUSHMARK(SP); 575 SvREFCNT_dec (coro_mortal);
619 PUTBACK; 576 coro_mortal = 0;
620 (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL);
621
622 if (SvTRUE (ERRSV))
623 croak (NULL);
624 else
625 croak ("FATAL: CCTXT coroutine returned!");
626 } 577 }
627 else
628 {
629 UNOP myop;
630
631 Zero(&myop, 1, UNOP);
632 myop.op_next = Nullop;
633 myop.op_flags = OPf_WANT_VOID;
634
635 PL_op = (OP *)&myop;
636
637 PUSHMARK(SP);
638 XPUSHs (sub_init);
639 /*
640 * the next line is slightly wrong, as PL_op->op_next
641 * is actually being executed so we skip the first op.
642 * that doesn't matter, though, since it is only
643 * pp_nextstate and we never return...
644 * ah yes, and I don't care anyways ;)
645 */
646 PUTBACK;
647 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
648 SPAGAIN;
649
650 ENTER; /* necessary e.g. for dounwind */
651 }
652} 578}
653 579
654static void 580static void
655continue_coro (void *arg) 581coro_run (void *arg)
656{ 582{
657 /* 583 /*
658 * this is a _very_ stripped down perl interpreter ;) 584 * this is a _very_ stripped down perl interpreter ;)
659 */ 585 */
660 dTHX; 586 dTHX;
661 Coro__State ctx = (Coro__State)arg; 587 int ret;
588
589 UNLOCK;
662 590
663 PL_top_env = &PL_start_env; 591 PL_top_env = &PL_start_env;
664 592
665 ctx->cursp = 0; 593 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg));
666 PL_op = PL_op->op_next; 594 sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op));
667 CALLRUNOPS(aTHX);
668 595
596 /* continue at cctx_init, without entersub */
597 PL_restartop = CvSTART (get_cv ("Coro::State::cctx_init", FALSE));
598
599 /* somebody will hit me for both perl_run and PL_restartop */
600 ret = perl_run (aTHX_ PERL_GET_CONTEXT);
601 printf ("ret %d\n", ret);//D
602
603 fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr);
669 abort (); 604 abort ();
670} 605}
671 606
607static coro_stack *
608stack_new ()
609{
610 coro_stack *stack;
611
612 New (0, stack, 1, coro_stack);
613
614#if HAVE_MMAP
615
616 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
617 /* mmap suppsedly does allocate-on-write for us */
618 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
619
620 if (stack->sptr == (void *)-1)
621 {
622 perror ("FATAL: unable to mmap stack for coroutine");
623 _exit (EXIT_FAILURE);
624 }
625
626# if STACKGUARD
627 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
628# endif
629
630#else
631
632 stack->ssize = STACKSIZE * (long)sizeof (long);
633 New (0, stack->sptr, STACKSIZE, long);
634
635 if (!stack->sptr)
636 {
637 perror (stderr, "FATAL: unable to malloc stack for coroutine");
638 _exit (EXIT_FAILURE);
639 }
640
641#endif
642
643 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize);
644
645 return stack;
646}
647
648static void
649stack_free (coro_stack *stack)
650{
651 if (!stack)
652 return;
653
654#if HAVE_MMAP
655 munmap (stack->sptr, stack->ssize);
656#else
657 Safefree (stack->sptr);
658#endif
659
660 Safefree (stack);
661}
662
663static coro_stack *stack_first;
664
665static coro_stack *
666stack_get ()
667{
668 coro_stack *stack;
669
670 if (stack_first)
671 {
672 stack = stack_first;
673 stack_first = stack->next;
674 }
675 else
676 {
677 stack = stack_new ();
678 PL_op = PL_op->op_next;
679 }
680
681 return stack;
682}
683
684static void
685stack_put (coro_stack *stack)
686{
687 stack->next = stack_first;
688 stack_first = stack;
689}
690
672/* never call directly, always through the coro_state_transfer global variable */ 691/* never call directly, always through the coro_state_transfer global variable */
673static void 692static void
674transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) 693transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags)
675{ 694{
676 dSTACKLEVEL; 695 dSTACKLEVEL;
677 696
697 /* sometimes transfer is only called to set idle_sp */
698 if (flags == TRANSFER_SET_STACKLEVEL)
699 ((coro_stack *)prev)->idle_sp = STACKLEVEL;
678 if (prev != next) 700 else if (prev != next)
679 { 701 {
680 /* has this coro been created yet? */ 702 coro_stack *prev__stack;
703
704 LOCK;
705
681 if (next->mainstack) 706 if (next->mainstack)
682 { 707 {
683 LOCK; 708 /* coroutine already started */
684 SAVE (prev, flags); 709 SAVE (prev, flags);
685 LOAD (next); 710 LOAD (next);
686 UNLOCK;
687
688 /* mark this state as in-use */
689 next->mainstack = 0;
690 next->tmps_ix = -2;
691
692 /* stacklevel changed? if yes, grab the stack for us! */
693 if (flags & TRANSFER_SAVE_CCTXT)
694 {
695 if (!prev->stack)
696 allocate_stack (prev, 0);
697 else if (prev->cursp != stacklevel
698 && prev->stack->usecnt > 1)
699 {
700 prev->gencnt = ++prev->stack->gencnt;
701 prev->stack->usecnt = 1;
702 }
703
704 /* has our stack been invalidated? */
705 if (next->stack && next->stack->gencnt != next->gencnt)
706 {
707 deallocate_stack (next);
708 allocate_stack (next, 1);
709 coro_create (&(next->stack->cctx),
710 continue_coro, (void *)next,
711 next->stack->sptr, labs (next->stack->ssize));
712 }
713
714 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
715 prev->cursp = stacklevel;
716 /* don't add any code here */
717 }
718 else
719 next->cursp = stacklevel;
720 } 711 }
721 else if (next->tmps_ix == -2)
722 croak ("tried to transfer to running coroutine");
723 else 712 else
724 { 713 {
725 LOCK; 714 /* need to start coroutine */
726 SAVE (prev, -1); /* first get rid of the old state */ 715 /* first get rid of the old state */
727 UNLOCK; 716 SAVE (prev, -1);
728 717 /* setup coroutine call */
729 /* create the coroutine for the first time */
730 if (flags & TRANSFER_SAVE_CCTXT)
731 {
732 if (!prev->stack)
733 allocate_stack (prev, 0);
734
735 /* the new coroutine starts with start_env again */
736 PL_top_env = &PL_start_env;
737
738 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
739 {
740 setup_coro (next);
741 next->cursp = stacklevel;
742
743 prev->stack->refcnt++;
744 prev->stack->usecnt++;
745 next->stack = prev->stack;
746 next->gencnt = prev->gencnt;
747 }
748 else
749 {
750 assert (!next->stack);
751 allocate_stack (next, 1);
752 coro_create (&(next->stack->cctx),
753 setup_coro, (void *)next,
754 next->stack->sptr, labs (next->stack->ssize));
755 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
756 prev->cursp = stacklevel;
757 /* don't add any code here */
758 }
759 }
760 else
761 {
762 setup_coro (next); 718 setup_coro (next);
763 next->cursp = stacklevel; 719 /* need a stack */
764 } 720 next->stack = 0;
765 } 721 }
722
723 if (!prev->stack)
724 /* create a new empty context */
725 Newz (0, prev->stack, 1, coro_stack);
726
727 prev__stack = prev->stack;
728
729 /* possibly "free" the stack */
730 if (prev__stack->idle_sp == STACKLEVEL)
731 {
732 stack_put (prev__stack);
733 prev->stack = 0;
734 }
735
736 if (!next->stack)
737 next->stack = stack_get ();
738
739 if (prev__stack != next->stack)
740 {
741 prev__stack->top_env = PL_top_env;
742 PL_top_env = next->stack->top_env;
743 coro_transfer (&prev__stack->cctx, &next->stack->cctx);
744 }
745
746 free_coro_mortal ();
747
748 UNLOCK;
766 } 749 }
750}
767 751
768 LOCK; 752/* use this function pointer to call the above function */
769 if (coro_mortal) 753/* this is done to increase chances of the compiler not inlining the call */
754/* not static to make it even harder for the compiler (and theoretically impossible in most cases */
755void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl;
756
757struct transfer_args
758{
759 struct coro *prev, *next;
760 int flags;
761};
762
763#define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags)
764
765static void
766coro_state_destroy (struct coro *coro)
767{
768 if (coro->refcnt--)
769 return;
770
771 if (coro->mainstack && coro->mainstack != main_mainstack)
770 { 772 {
771 SvREFCNT_dec (coro_mortal); 773 struct coro temp;
774
775 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL);
776 LOAD (aTHX_ coro);
777
778 destroy_stacks (aTHX);
779
780 LOAD ((&temp)); /* this will get rid of defsv etc.. */
781
772 coro_mortal = 0; 782 coro->mainstack = 0;
773 } 783 }
774 UNLOCK;
775}
776 784
777void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; 785 stack_free (coro->stack);
786 SvREFCNT_dec (coro->args);
787 Safefree (coro);
788}
778 789
779#define SV_CORO(sv,func) \
780 do { \
781 if (SvROK (sv)) \
782 sv = SvRV (sv); \
783 \
784 if (SvTYPE (sv) == SVt_PVHV) \
785 { \
786 HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \
787 \
788 if (!he) \
789 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \
790 \
791 (sv) = SvRV (HeVAL(he)); \
792 } \
793 \
794 /* must also be changed inside Coro::Cont::yield */ \
795 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
796 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
797 \
798 } while(0)
799
800#define SvSTATE(sv) INT2PTR (struct coro *, SvIVX (sv))
801
802static void 790static int
791coro_state_clear (SV *sv, MAGIC *mg)
792{
793 struct coro *coro = (struct coro *)mg->mg_ptr;
794 mg->mg_ptr = 0;
795
796 coro_state_destroy (coro);
797
798 return 0;
799}
800
801static int
802coro_state_dup (MAGIC *mg, CLONE_PARAMS *params)
803{
804 struct coro *coro = (struct coro *)mg->mg_ptr;
805
806 ++coro->refcnt;
807
808 return 0;
809}
810
811static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 };
812
813static struct coro *
814SvSTATE (SV *coro)
815{
816 HV *stash;
817 MAGIC *mg;
818
819 if (SvROK (coro))
820 coro = SvRV (coro);
821
822 stash = SvSTASH (coro);
823 if (stash != coro_stash && stash != coro_state_stash)
824 {
825 /* very slow, but rare, check */
826 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
827 croak ("Coro::State object required");
828 }
829
830 mg = SvMAGIC (coro);
831 assert (mg->mg_type == PERL_MAGIC_ext);
832 return (struct coro *)mg->mg_ptr;
833}
834
835static void
836prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev, SV *next, int flags)
837{
838 ta->prev = SvSTATE (prev);
839 ta->next = SvSTATE (next);
840 ta->flags = flags;
841}
842
843static void
803api_transfer (pTHX_ SV *prev, SV *next, int flags) 844api_transfer (SV *prev, SV *next, int flags)
804{ 845{
805 SV_CORO (prev, "Coro::transfer"); 846 dTHX;
806 SV_CORO (next, "Coro::transfer"); 847 struct transfer_args ta;
807 848
808 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 849 prepare_transfer (aTHX_ &ta, prev, next, flags);
850 TRANSFER (ta);
809} 851}
810 852
811/** Coro ********************************************************************/ 853/** Coro ********************************************************************/
812 854
813#define PRIO_MAX 3 855#define PRIO_MAX 3
823static int coro_nready; 865static int coro_nready;
824 866
825static void 867static void
826coro_enq (pTHX_ SV *sv) 868coro_enq (pTHX_ SV *sv)
827{ 869{
828 SV **xprio;
829 int prio; 870 int prio;
830 871
831 if (SvTYPE (sv) != SVt_PVHV) 872 if (SvTYPE (sv) != SVt_PVHV)
832 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 873 croak ("Coro::ready tried to enqueue something that is not a coroutine");
833 874
834 xprio = hv_fetch ((HV *)sv, "prio", 4, 0); 875 prio = SvSTATE (sv)->prio;
835 prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
836
837 prio = prio > PRIO_MAX ? PRIO_MAX
838 : prio < PRIO_MIN ? PRIO_MIN
839 : prio;
840 876
841 av_push (coro_ready [prio - PRIO_MIN], sv); 877 av_push (coro_ready [prio - PRIO_MIN], sv);
842 coro_nready++; 878 coro_nready++;
843} 879}
844 880
873 coro_enq (aTHX_ SvREFCNT_inc (coro)); 909 coro_enq (aTHX_ SvREFCNT_inc (coro));
874 UNLOCK; 910 UNLOCK;
875} 911}
876 912
877static void 913static void
878api_schedule (void) 914prepare_schedule (aTHX_ struct transfer_args *ta)
879{ 915{
880 dTHX;
881
882 SV *prev, *next; 916 SV *current, *prev, *next;
883 917
884 LOCK; 918 LOCK;
885 919
886 prev = SvRV (GvSV (coro_current)); 920 current = GvSV (coro_current);
921
922 for (;;)
923 {
924 LOCK;
925
887 next = coro_deq (aTHX_ PRIO_MIN); 926 next = coro_deq (aTHX_ PRIO_MIN);
888 927
889 if (!next) 928 if (next)
890 next = SvREFCNT_inc (SvRV (GvSV (coro_idle))); 929 break;
930
931 UNLOCK;
932
933 {
934 dSP;
935
936 ENTER;
937 SAVETMPS;
938
939 PUSHMARK (SP);
940 PUTBACK;
941 call_sv (GvSV (coro_idle), G_DISCARD);
942
943 FREETMPS;
944 LEAVE;
945 }
946 }
947
948 prev = SvRV (current);
949 SvRV (current) = next;
891 950
892 /* free this only after the transfer */ 951 /* free this only after the transfer */
952 free_coro_mortal ();
893 coro_mortal = prev; 953 coro_mortal = prev;
894 SV_CORO (prev, "Coro::schedule");
895 954
896 SvRV (GvSV (coro_current)) = next; 955 ta->prev = SvSTATE (prev);
897 956 ta->next = SvSTATE (next);
898 SV_CORO (next, "Coro::schedule"); 957 ta->flags = TRANSFER_SAVE_ALL;
899 958
900 UNLOCK; 959 UNLOCK;
901
902 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
903 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
904} 960}
905 961
906static void 962static void
907api_cede (void) 963prepare_cede (aTHX_ struct transfer_args *ta)
908{ 964{
909 dTHX;
910
911 LOCK; 965 LOCK;
912 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); 966 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current))));
913 UNLOCK; 967 UNLOCK;
914 968
969 prepare_schedule (ta);
970}
971
972static void
915 api_schedule (); 973api_schedule (void)
974{
975 dTHX;
976 struct transfer_args ta;
977
978 prepare_schedule (&ta);
979 TRANSFER (ta);
980}
981
982static void
983api_cede (void)
984{
985 dTHX;
986 struct transfer_args ta;
987
988 prepare_cede (&ta);
989 TRANSFER (ta);
916} 990}
917 991
918MODULE = Coro::State PACKAGE = Coro::State 992MODULE = Coro::State PACKAGE = Coro::State
919 993
920PROTOTYPES: ENABLE 994PROTOTYPES: DISABLE
921 995
922BOOT: 996BOOT:
923{ /* {} necessary for stoopid perl-5.6.x */ 997{
924#ifdef USE_ITHREADS 998#ifdef USE_ITHREADS
925 MUTEX_INIT (&coro_mutex); 999 MUTEX_INIT (&coro_mutex);
926#endif 1000#endif
927 BOOT_PAGESIZE; 1001 BOOT_PAGESIZE;
928 1002
929 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1);
930 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1);
931 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1003 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
932 1004
933 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 1005 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV));
934 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 1006 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV));
935 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 1007 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV));
936 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT));
937 1008
938 main_mainstack = PL_mainstack; 1009 main_mainstack = PL_mainstack;
939 1010
940 coroapi.ver = CORO_API_VERSION; 1011 coroapi.ver = CORO_API_VERSION;
941 coroapi.transfer = api_transfer; 1012 coroapi.transfer = api_transfer;
942}
943 1013
944Coro::State 1014 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
945_newprocess(args) 1015}
946 SV * args 1016
947 PROTOTYPE: $ 1017SV *
1018new (char *klass, ...)
948 CODE: 1019 CODE:
949 Coro__State coro; 1020{
1021 struct coro *coro;
1022 HV *hv;
1023 int i;
950 1024
951 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV)
952 croak ("Coro::State::_newprocess expects an arrayref");
953
954 Newz (0, coro, 1, struct coro); 1025 Newz (0, coro, 1, struct coro);
1026 coro->args = newAV ();
955 1027
956 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 1028 hv = newHV ();
1029 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1030 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1031
1032 for (i = 1; i < items; i++)
1033 av_push (coro->args, newSVsv (ST (i)));
1034
957 /*coro->mainstack = 0; *//*actual work is done inside transfer */ 1035 /*coro->mainstack = 0; *//*actual work is done inside transfer */
958 /*coro->stack = 0;*/ 1036 /*coro->stack = 0;*/
959 1037}
960 RETVAL = coro;
961 OUTPUT: 1038 OUTPUT:
962 RETVAL 1039 RETVAL
963 1040
964void 1041void
965transfer(prev, next, flags) 1042_set_stacklevel (...)
966 SV *prev 1043 ALIAS:
967 SV *next 1044 Coro::State::transfer = 1
968 int flags 1045 Coro::schedule = 2
969 PROTOTYPE: @ 1046 Coro::cede = 3
1047 Coro::Cont::yield = 4
970 CODE: 1048 CODE:
971 PUTBACK; 1049{
972 SV_CORO (next, "Coro::transfer"); 1050 struct transfer_args ta;
973 SV_CORO (prev, "Coro::transfer"); 1051
974 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 1052 switch (ix)
975 SPAGAIN; 1053 {
1054 case 0:
1055 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0)));
1056 ta.next = 0;
1057 ta.flags = TRANSFER_SET_STACKLEVEL;
1058 break;
1059
1060 case 1:
1061 if (items != 3)
1062 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items);
1063
1064 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2)));
1065 break;
1066
1067 case 2:
1068 prepare_schedule (&ta);
1069 break;
1070
1071 case 3:
1072 prepare_cede (&ta);
1073 break;
1074
1075 case 4:
1076 {
1077 SV *yieldstack;
1078 SV *sv;
1079 AV *defav = GvAV (PL_defgv);
1080
1081 yieldstack = *hv_fetch (
1082 (HV *)SvRV (GvSV (coro_current)),
1083 "yieldstack", sizeof ("yieldstack") - 1,
1084 0
1085 );
1086
1087 /* set up @_ -- ugly */
1088 av_clear (defav);
1089 av_fill (defav, items - 1);
1090 while (items--)
1091 av_store (defav, items, SvREFCNT_inc (ST(items)));
1092
1093 sv = av_pop ((AV *)SvRV (yieldstack));
1094 ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1095 ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1096 ta.flags = 0;
1097 SvREFCNT_dec (sv);
1098 }
1099 break;
1100
1101 }
1102
1103 TRANSFER (ta);
1104}
976 1105
977void 1106void
978DESTROY(coro) 1107_clone_state_from (SV *dst, SV *src)
979 Coro::State coro 1108 CODE:
980 CODE: 1109{
1110 struct coro *coro_src = SvSTATE (src);
981 1111
982 if (coro->mainstack && coro->mainstack != main_mainstack) 1112 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
983 {
984 struct coro temp;
985 1113
986 PUTBACK; 1114 ++coro_src->refcnt;
987 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); 1115 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
988 LOAD (aTHX_ coro); 1116}
989 SPAGAIN;
990
991 destroy_stacks (aTHX);
992
993 LOAD ((&temp)); /* this will get rid of defsv etc.. */
994 SPAGAIN;
995
996 coro->mainstack = 0;
997 }
998
999 deallocate_stack (coro);
1000 SvREFCNT_dec (coro->args);
1001 Safefree (coro);
1002 1117
1003void 1118void
1119_nonlocal_goto (IV nextop)
1120 CODE:
1121 /* uuh, somebody will kill me again for this */
1122 PL_op->op_next = INT2PTR (OP *, nextop);
1123
1124void
1004_exit(code) 1125_exit (code)
1005 int code 1126 int code
1006 PROTOTYPE: $ 1127 PROTOTYPE: $
1007 CODE: 1128 CODE:
1008 _exit (code); 1129 _exit (code);
1009 1130
1010MODULE = Coro::State PACKAGE = Coro::Cont
1011
1012void
1013yield(...)
1014 PROTOTYPE: @
1015 CODE:
1016 SV *yieldstack;
1017 SV *sv;
1018 AV *defav = GvAV (PL_defgv);
1019 struct coro *prev, *next;
1020
1021 yieldstack = *hv_fetch (
1022 (HV *)SvRV (GvSV (coro_current)),
1023 "yieldstack", sizeof ("yieldstack") - 1,
1024 0
1025 );
1026
1027 /* set up @_ -- ugly */
1028 av_clear (defav);
1029 av_fill (defav, items - 1);
1030 while (items--)
1031 av_store (defav, items, SvREFCNT_inc (ST(items)));
1032
1033 sv = av_pop ((AV *)SvRV (yieldstack));
1034 prev = SvSTATE ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0)));
1035 next = SvSTATE ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0)));
1036 SvREFCNT_dec (sv);
1037
1038 coro_state_transfer (aTHX_ prev, next, 0);
1039
1040MODULE = Coro::State PACKAGE = Coro 1131MODULE = Coro::State PACKAGE = Coro
1041 1132
1042BOOT: 1133BOOT:
1043{ 1134{
1044 int i; 1135 int i;
1136
1045 HV *stash = gv_stashpv ("Coro", TRUE); 1137 coro_stash = gv_stashpv ("Coro", TRUE);
1046 1138
1047 newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1139 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1048 newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1140 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1049 newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1141 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1050 newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1142 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1051 newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1143 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1052 newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1144 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1053 1145
1054 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); 1146 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV);
1055 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); 1147 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV);
1056 1148
1057 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1149 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1070 sv_setiv (sv, (IV)&coroapi); 1162 sv_setiv (sv, (IV)&coroapi);
1071 SvREADONLY_on (sv); 1163 SvREADONLY_on (sv);
1072 } 1164 }
1073} 1165}
1074 1166
1075#if !PERL_MICRO 1167int
1168prio (Coro::State coro, int newprio = 0)
1169 ALIAS:
1170 nice = 1
1171 CODE:
1172{
1173 RETVAL = coro->prio;
1174
1175 if (items > 1)
1176 {
1177 if (ix)
1178 newprio += coro->prio;
1179
1180 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1181 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1182
1183 coro->prio = newprio;
1184 }
1185}
1076 1186
1077void 1187void
1078ready(self) 1188ready (SV *self)
1079 SV * self
1080 PROTOTYPE: $ 1189 PROTOTYPE: $
1081 CODE: 1190 CODE:
1082 api_ready (self); 1191 api_ready (self);
1083 1192
1084#endif
1085
1086int 1193int
1087nready(...) 1194nready (...)
1088 PROTOTYPE: 1195 PROTOTYPE:
1089 CODE: 1196 CODE:
1090 RETVAL = coro_nready; 1197 RETVAL = coro_nready;
1091 OUTPUT: 1198 OUTPUT:
1092 RETVAL 1199 RETVAL
1093 1200
1094void 1201MODULE = Coro::State PACKAGE = Coro::AIO
1095schedule(...)
1096 PROTOTYPE:
1097 CODE:
1098 api_schedule ();
1099 1202
1100void
1101cede(...)
1102 PROTOTYPE:
1103 CODE:
1104 api_cede ();
1105
1106# and these are hacks
1107SV * 1203SV *
1108_aio_get_state () 1204_get_state ()
1109 CODE: 1205 CODE:
1110{ 1206{
1111 struct { 1207 struct {
1112 int errorno; 1208 int errorno;
1113 int laststype; 1209 int laststype;
1124} 1220}
1125 OUTPUT: 1221 OUTPUT:
1126 RETVAL 1222 RETVAL
1127 1223
1128void 1224void
1129_aio_set_state (char *data_) 1225_set_state (char *data_)
1130 PROTOTYPE: $ 1226 PROTOTYPE: $
1131 CODE: 1227 CODE:
1132{ 1228{
1133 struct { 1229 struct {
1134 int errorno; 1230 int errorno;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines