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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines