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.88 by root, Fri Nov 24 15:34:33 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
64/* 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
65 * 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
66 * and should be unique. */ 65 * and should be unique. */
66#define dSTACKLEVEL int stacklevel
67#define dSTACKLEVEL void *stacklevel = &stacklevel 67#define STACKLEVEL ((void *)&stacklevel)
68 68
69#define IN_DESTRUCT (PL_main_cv == Nullcv) 69#define IN_DESTRUCT (PL_main_cv == Nullcv)
70 70
71#define labs(l) ((l) >= 0 ? (l) : -(l))
72
73#include "CoroAPI.h" 71#include "CoroAPI.h"
72
73#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
74 74
75#ifdef USE_ITHREADS 75#ifdef USE_ITHREADS
76static perl_mutex coro_mutex; 76static perl_mutex coro_mutex;
77# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 77# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
78# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 78# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
84static struct CoroAPI coroapi; 84static struct CoroAPI coroapi;
85static AV *main_mainstack; /* used to differentiate between $main and others */ 85static AV *main_mainstack; /* used to differentiate between $main and others */
86static HV *coro_state_stash, *coro_stash; 86static HV *coro_state_stash, *coro_stash;
87static SV *coro_mortal; /* will be freed after next transfer */ 87static SV *coro_mortal; /* will be freed after next transfer */
88 88
89/* this is actually not only the c stack but also c registers etc... */ 89/* this is a structure representing a c-level coroutine */
90typedef struct { 90typedef struct coro_stack {
91 int refcnt; /* pointer reference counter */ 91 struct coro_stack *next;
92 int usecnt; /* shared by how many coroutines */
93 int gencnt; /* generation counter */
94 92
95 coro_context cctx; 93 /* the stack */
96
97 void *sptr; 94 void *sptr;
98 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;
99} coro_stack; 101} coro_stack;
100 102
103/* this is a structure representing a perl-level coroutine */
101struct coro { 104struct coro {
102 /* the optional C context */ 105 /* the c coroutine allocated to this perl coroutine, if any */
103 coro_stack *stack; 106 coro_stack *stack;
104 void *cursp; 107
108 /* data associated with this coroutine (initial args) */
109 AV *args;
105 int gencnt; 110 int refcnt;
106 111
107 /* optionally saved, might be zero */ 112 /* optionally saved, might be zero */
108 AV *defav; 113 AV *defav;
109 SV *defsv; 114 SV *defsv;
110 SV *errsv; 115 SV *errsv;
140 OP **retstack; 145 OP **retstack;
141 I32 retstack_ix; 146 I32 retstack_ix;
142 I32 retstack_max; 147 I32 retstack_max;
143 PMOP *curpm; 148 PMOP *curpm;
144 COP *curcop; 149 COP *curcop;
145 JMPENV *top_env;
146 150
147 /* coro process data */ 151 /* coro process data */
148 int prio; 152 int prio;
149
150 /* data associated with this coroutine (initial args) */
151 AV *args;
152 int refcnt;
153}; 153};
154 154
155typedef struct coro *Coro__State; 155typedef struct coro *Coro__State;
156typedef struct coro *Coro__State_or_hashref; 156typedef struct coro *Coro__State_or_hashref;
157 157
229 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 229 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0)
230 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 230 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj);
231 else 231 else
232 { 232 {
233#if 0 233#if 0
234 /* this should work - but it doesn't :( */ 234 /* this is probably cleaner, but also slower? */
235 CV *cp = Perl_cv_clone (aTHX_ cv); 235 CV *cp = Perl_cv_clone (aTHX_ cv);
236 CvPADLIST (cv) = CvPADLIST (cp); 236 CvPADLIST (cv) = CvPADLIST (cp);
237 CvPADLIST (cp) = 0; 237 CvPADLIST (cp) = 0;
238 SvREFCNT_dec (cp); 238 SvREFCNT_dec (cp);
239#else 239#else
300 PL_retstack_ix = c->retstack_ix; 300 PL_retstack_ix = c->retstack_ix;
301 PL_retstack_max = c->retstack_max; 301 PL_retstack_max = c->retstack_max;
302#endif 302#endif
303 PL_curpm = c->curpm; 303 PL_curpm = c->curpm;
304 PL_curcop = c->curcop; 304 PL_curcop = c->curcop;
305 PL_top_env = c->top_env;
306 305
307 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 306 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
308 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 307 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
309 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 308 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
310 309
428 c->retstack_ix = PL_retstack_ix; 427 c->retstack_ix = PL_retstack_ix;
429 c->retstack_max = PL_retstack_max; 428 c->retstack_max = PL_retstack_max;
430#endif 429#endif
431 c->curpm = PL_curpm; 430 c->curpm = PL_curpm;
432 c->curcop = PL_curcop; 431 c->curcop = PL_curcop;
433 c->top_env = PL_top_env;
434} 432}
435 433
436/* 434/*
437 * allocate various perl stacks. This is an exact copy 435 * allocate various perl stacks. This is an exact copy
438 * of perl.c:init_stacks, except that it uses less memory 436 * of perl.c:init_stacks, except that it uses less memory
532 Safefree (PL_retstack); 530 Safefree (PL_retstack);
533#endif 531#endif
534} 532}
535 533
536static void 534static void
537allocate_stack (Coro__State ctx, int alloc) 535setup_coro (struct coro *coro)
538{
539 coro_stack *stack;
540
541 New (0, stack, 1, coro_stack);
542
543 stack->refcnt = 1;
544 stack->usecnt = 1;
545 stack->gencnt = ctx->gencnt = 0;
546
547 if (alloc)
548 {
549#if HAVE_MMAP
550 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */
551 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
552 if (stack->sptr != (void *)-1)
553 {
554# if STACKGUARD
555 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
556# endif
557 }
558 else
559#endif
560 {
561 stack->ssize = - (STACKSIZE * (long)sizeof (long));
562 New (0, stack->sptr, STACKSIZE, long);
563 }
564 }
565 else
566 stack->sptr = 0;
567
568 ctx->stack = stack;
569}
570
571static void
572deallocate_stack (Coro__State ctx)
573{
574 coro_stack *stack = ctx->stack;
575
576 ctx->stack = 0;
577
578 if (stack)
579 {
580 if (!--stack->refcnt)
581 {
582#ifdef HAVE_MMAP
583 if (stack->ssize > 0 && stack->sptr)
584 munmap (stack->sptr, stack->ssize);
585 else
586#endif
587 Safefree (stack->sptr);
588
589 Safefree (stack);
590 }
591 else if (ctx->gencnt == stack->gencnt)
592 --stack->usecnt;
593 }
594}
595
596static void
597setup_coro (void *arg)
598{ 536{
599 /* 537 /*
600 * emulate part of the perl startup here. 538 * emulate part of the perl startup here.
601 */ 539 */
602 dTHX; 540 dTHX;
603 dSP; 541 dSP;
604 Coro__State ctx = (Coro__State)arg; 542 UNOP myop;
605 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); 543 SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE);
606 544
607 coro_init_stacks (aTHX); 545 coro_init_stacks (aTHX);
608 /*PL_curcop = 0;*/ 546 /*PL_curcop = 0;*/
609 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 547 /*PL_in_eval = PL_in_eval;*/ /* inherit */
610 SvREFCNT_dec (GvAV (PL_defgv)); 548 SvREFCNT_dec (GvAV (PL_defgv));
611 GvAV (PL_defgv) = ctx->args; ctx->args = 0; 549 GvAV (PL_defgv) = coro->args; coro->args = 0;
612 550
613 SPAGAIN; 551 SPAGAIN;
614 552
615 if (ctx->stack)
616 {
617 ctx->cursp = 0;
618
619 PUSHMARK(SP);
620 PUTBACK;
621 (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL);
622
623 if (SvTRUE (ERRSV))
624 croak (NULL);
625 else
626 croak ("FATAL: CCTXT coroutine returned!");
627 }
628 else
629 {
630 UNOP myop;
631
632 Zero(&myop, 1, UNOP); 553 Zero (&myop, 1, UNOP);
633 myop.op_next = Nullop; 554 myop.op_next = Nullop;
634 myop.op_flags = OPf_WANT_VOID; 555 myop.op_flags = OPf_WANT_VOID;
635 556
636 PL_op = (OP *)&myop; 557 PL_op = (OP *)&myop;
637 558
638 PUSHMARK(SP); 559 PUSHMARK(SP);
639 XPUSHs (sub_init); 560 XPUSHs (sub_init);
640 /*
641 * the next line is slightly wrong, as PL_op->op_next
642 * is actually being executed so we skip the first op.
643 * that doesn't matter, though, since it is only
644 * pp_nextstate and we never return...
645 * ah yes, and I don't care anyways ;)
646 */
647 PUTBACK; 561 PUTBACK;
648 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 562 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
649 SPAGAIN; 563 SPAGAIN;
650 564
651 ENTER; /* necessary e.g. for dounwind */ 565 ENTER; /* necessary e.g. for dounwind */
652 }
653} 566}
654 567
655static void 568static void
656continue_coro (void *arg) 569free_coro_mortal ()
657{ 570{
658 /*
659 * this is a _very_ stripped down perl interpreter ;)
660 */
661 dTHX;
662 Coro__State ctx = (Coro__State)arg;
663
664 PL_top_env = &PL_start_env;
665
666 ctx->cursp = 0;
667 PL_op = PL_op->op_next;
668 CALLRUNOPS(aTHX);
669
670 abort ();
671}
672
673/* never call directly, always through the coro_state_transfer global variable */
674static void
675transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags)
676{
677 dSTACKLEVEL;
678
679 if (prev != next)
680 {
681 /* has this coro been created yet? */
682 if (next->mainstack)
683 {
684 LOCK;
685 SAVE (prev, flags);
686 LOAD (next);
687 UNLOCK;
688
689 /* mark this state as in-use */
690 next->mainstack = 0;
691 next->tmps_ix = -2;
692
693 /* stacklevel changed? if yes, grab the stack for us! */
694 if (flags & TRANSFER_SAVE_CCTXT)
695 {
696 if (!prev->stack)
697 allocate_stack (prev, 0);
698 else if (prev->cursp != stacklevel
699 && prev->stack->usecnt > 1)
700 {
701 prev->gencnt = ++prev->stack->gencnt;
702 prev->stack->usecnt = 1;
703 }
704
705 /* has our stack been invalidated? */
706 if (next->stack && next->stack->gencnt != next->gencnt)
707 {
708 deallocate_stack (next);
709 allocate_stack (next, 1);
710 coro_create (&(next->stack->cctx),
711 continue_coro, (void *)next,
712 next->stack->sptr, labs (next->stack->ssize));
713 }
714
715 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
716 prev->cursp = stacklevel;
717 /* don't add any code here */
718 }
719 else
720 next->cursp = stacklevel;
721 }
722 else if (next->tmps_ix == -2)
723 croak ("tried to transfer to running coroutine");
724 else
725 {
726 LOCK;
727 SAVE (prev, -1); /* first get rid of the old state */
728 UNLOCK;
729
730 /* create the coroutine for the first time */
731 if (flags & TRANSFER_SAVE_CCTXT)
732 {
733 if (!prev->stack)
734 allocate_stack (prev, 0);
735
736 /* the new coroutine starts with start_env again */
737 PL_top_env = &PL_start_env;
738
739 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
740 {
741 setup_coro (next);
742 next->cursp = stacklevel;
743
744 prev->stack->refcnt++;
745 prev->stack->usecnt++;
746 next->stack = prev->stack;
747 next->gencnt = prev->gencnt;
748 }
749 else
750 {
751 assert (!next->stack);
752 allocate_stack (next, 1);
753 coro_create (&(next->stack->cctx),
754 setup_coro, (void *)next,
755 next->stack->sptr, labs (next->stack->ssize));
756 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
757 prev->cursp = stacklevel;
758 /* don't add any code here */
759 }
760 }
761 else
762 {
763 setup_coro (next);
764 next->cursp = stacklevel;
765 }
766 }
767 }
768
769 LOCK;
770 if (coro_mortal) 571 if (coro_mortal)
771 { 572 {
772 SvREFCNT_dec (coro_mortal); 573 SvREFCNT_dec (coro_mortal);
773 coro_mortal = 0; 574 coro_mortal = 0;
774 } 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
775 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 }
776} 748}
777 749
778/* use this function pointer to call the above function */ 750/* use this function pointer to call the above function */
779/* 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 */
780void (*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;
754
755struct transfer_args
756{
757 struct coro *prev, *next;
758 int flags;
759};
760
761#define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags)
781 762
782static void 763static void
783coro_state_destroy (struct coro *coro) 764coro_state_destroy (struct coro *coro)
784{ 765{
785 if (coro->refcnt--) 766 if (coro->refcnt--)
797 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 778 LOAD ((&temp)); /* this will get rid of defsv etc.. */
798 779
799 coro->mainstack = 0; 780 coro->mainstack = 0;
800 } 781 }
801 782
802 deallocate_stack (coro); 783 stack_free (coro->stack);
803 SvREFCNT_dec (coro->args); 784 SvREFCNT_dec (coro->args);
804 Safefree (coro); 785 Safefree (coro);
805} 786}
806 787
807static int 788static int
848 assert (mg->mg_type == PERL_MAGIC_ext); 829 assert (mg->mg_type == PERL_MAGIC_ext);
849 return (struct coro *)mg->mg_ptr; 830 return (struct coro *)mg->mg_ptr;
850} 831}
851 832
852static 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
853api_transfer (pTHX_ SV *prev, SV *next, int flags) 842api_transfer (SV *prev, SV *next, int flags)
854{ 843{
855 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 844 dTHX;
845 struct transfer_args ta;
846
847 prepare_transfer (aTHX_ &ta, prev, next, flags);
848 TRANSFER (ta);
856} 849}
857 850
858/** Coro ********************************************************************/ 851/** Coro ********************************************************************/
859 852
860#define PRIO_MAX 3 853#define PRIO_MAX 3
914 coro_enq (aTHX_ SvREFCNT_inc (coro)); 907 coro_enq (aTHX_ SvREFCNT_inc (coro));
915 UNLOCK; 908 UNLOCK;
916} 909}
917 910
918static void 911static void
919api_schedule (void) 912prepare_schedule (aTHX_ struct transfer_args *ta)
920{ 913{
921 dTHX;
922
923 SV *prev, *next; 914 SV *current, *prev, *next;
915
916 LOCK;
917
924 SV *current = GvSV (coro_current); 918 current = GvSV (coro_current);
925 919
926 for (;;) 920 for (;;)
927 { 921 {
928 LOCK; 922 LOCK;
929 923
951 945
952 prev = SvRV (current); 946 prev = SvRV (current);
953 SvRV (current) = next; 947 SvRV (current) = next;
954 948
955 /* free this only after the transfer */ 949 /* free this only after the transfer */
950 free_coro_mortal ();
956 coro_mortal = prev; 951 coro_mortal = prev;
957 952
953 ta->prev = SvSTATE (prev);
954 ta->next = SvSTATE (next);
955 ta->flags = TRANSFER_SAVE_ALL;
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
987 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1001 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
988 1002
989 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 1003 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV));
990 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 1004 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV));
991 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 1005 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV));
992 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT));
993 1006
994 main_mainstack = PL_mainstack; 1007 main_mainstack = PL_mainstack;
995 1008
996 coroapi.ver = CORO_API_VERSION; 1009 coroapi.ver = CORO_API_VERSION;
997 coroapi.transfer = api_transfer; 1010 coroapi.transfer = api_transfer;
1008 int i; 1021 int i;
1009 1022
1010 Newz (0, coro, 1, struct coro); 1023 Newz (0, coro, 1, struct coro);
1011 coro->args = newAV (); 1024 coro->args = newAV ();
1012 1025
1013 for (i = 1; i < items; i++)
1014 av_push (coro->args, newSVsv (ST (i)));
1015
1016 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1017 /*coro->stack = 0;*/
1018
1019 hv = newHV (); 1026 hv = newHV ();
1020 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;
1021 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;*/
1022} 1035}
1023 OUTPUT: 1036 OUTPUT:
1024 RETVAL 1037 RETVAL
1025 1038
1026void 1039void
1027transfer(prev, next, flags) 1040_set_stacklevel (...)
1028 SV *prev 1041 ALIAS:
1029 SV *next 1042 Coro::State::transfer = 1
1030 int flags 1043 Coro::schedule = 2
1044 Coro::cede = 3
1045 Coro::Cont::yield = 4
1031 CODE: 1046 CODE:
1032 PUTBACK;
1033 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
1034 SPAGAIN;
1035
1036int
1037prio (Coro::State coro, int newprio = 0)
1038 ALIAS:
1039 nice = 1
1040 CODE:
1041{ 1047{
1042 RETVAL = coro->prio; 1048 struct transfer_args ta;
1043 1049
1044 if (items > 1) 1050 switch (ix)
1045 { 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:
1046 if (ix) 1059 if (items != 3)
1047 newprio += coro->prio; 1060 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items);
1048 1061
1049 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 1062 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2)));
1050 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 1063 break;
1051 1064
1052 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
1053 } 1099 }
1100
1101 TRANSFER (ta);
1054} 1102}
1055 1103
1056void 1104void
1057_clone_state_from (SV *dst, SV *src) 1105_clone_state_from (SV *dst, SV *src)
1058 CODE: 1106 CODE:
1062 sv_unmagic (SvRV (dst), PERL_MAGIC_ext); 1110 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1063 1111
1064 ++coro_src->refcnt; 1112 ++coro_src->refcnt;
1065 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;
1066} 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);
1067 1121
1068void 1122void
1069_exit (code) 1123_exit (code)
1070 int code 1124 int code
1071 PROTOTYPE: $ 1125 PROTOTYPE: $
1072 CODE: 1126 CODE:
1073 _exit (code); 1127 _exit (code);
1074 1128
1075MODULE = Coro::State PACKAGE = Coro::Cont
1076
1077void
1078yield (...)
1079 PROTOTYPE: @
1080 CODE:
1081{
1082 SV *yieldstack;
1083 SV *sv;
1084 AV *defav = GvAV (PL_defgv);
1085 struct coro *prev, *next;
1086
1087 yieldstack = *hv_fetch (
1088 (HV *)SvRV (GvSV (coro_current)),
1089 "yieldstack", sizeof ("yieldstack") - 1,
1090 0
1091 );
1092
1093 /* set up @_ -- ugly */
1094 av_clear (defav);
1095 av_fill (defav, items - 1);
1096 while (items--)
1097 av_store (defav, items, SvREFCNT_inc (ST(items)));
1098
1099 sv = av_pop ((AV *)SvRV (yieldstack));
1100 prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1101 next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1102 SvREFCNT_dec (sv);
1103
1104 coro_state_transfer (aTHX_ prev, next, 0);
1105}
1106
1107MODULE = Coro::State PACKAGE = Coro 1129MODULE = Coro::State PACKAGE = Coro
1108 1130
1109BOOT: 1131BOOT:
1110{ 1132{
1111 int i; 1133 int i;
1138 sv_setiv (sv, (IV)&coroapi); 1160 sv_setiv (sv, (IV)&coroapi);
1139 SvREADONLY_on (sv); 1161 SvREADONLY_on (sv);
1140 } 1162 }
1141} 1163}
1142 1164
1143#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}
1144 1184
1145void 1185void
1146ready (self) 1186ready (SV *self)
1147 SV * self
1148 PROTOTYPE: $ 1187 PROTOTYPE: $
1149 CODE: 1188 CODE:
1150 api_ready (self); 1189 api_ready (self);
1151
1152#endif
1153 1190
1154int 1191int
1155nready (...) 1192nready (...)
1156 PROTOTYPE: 1193 PROTOTYPE:
1157 CODE: 1194 CODE:
1158 RETVAL = coro_nready; 1195 RETVAL = coro_nready;
1159 OUTPUT: 1196 OUTPUT:
1160 RETVAL 1197 RETVAL
1161 1198
1162void 1199MODULE = Coro::State PACKAGE = Coro::AIO
1163schedule (...)
1164 PROTOTYPE:
1165 CODE:
1166 api_schedule ();
1167 1200
1168void
1169cede (...)
1170 PROTOTYPE:
1171 CODE:
1172 api_cede ();
1173
1174# and these are hacks
1175SV * 1201SV *
1176_aio_get_state () 1202_get_state ()
1177 CODE: 1203 CODE:
1178{ 1204{
1179 struct { 1205 struct {
1180 int errorno; 1206 int errorno;
1181 int laststype; 1207 int laststype;
1192} 1218}
1193 OUTPUT: 1219 OUTPUT:
1194 RETVAL 1220 RETVAL
1195 1221
1196void 1222void
1197_aio_set_state (char *data_) 1223_set_state (char *data_)
1198 PROTOTYPE: $ 1224 PROTOTYPE: $
1199 CODE: 1225 CODE:
1200{ 1226{
1201 struct { 1227 struct {
1202 int errorno; 1228 int errorno;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines