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.77 by root, Tue Oct 31 23:55:33 2006 UTC vs.
Revision 1.95 by root, Sun Nov 26 17:35:42 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>
34
35#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
36# undef STACKGUARD
37#endif
38
39#ifndef STACKGUARD
40# define STACKGUARD 0
41#endif
33 42
34#ifdef HAVE_MMAP 43#ifdef HAVE_MMAP
35# include <unistd.h> 44# include <unistd.h>
36# include <sys/mman.h> 45# include <sys/mman.h>
37# ifndef MAP_ANONYMOUS 46# ifndef MAP_ANONYMOUS
39# define MAP_ANONYMOUS MAP_ANON 48# define MAP_ANONYMOUS MAP_ANON
40# else 49# else
41# undef HAVE_MMAP 50# undef HAVE_MMAP
42# endif 51# endif
43# endif 52# endif
53# include <limits.h>
54# ifndef PAGESIZE
55# define PAGESIZE pagesize
56# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE)
57static long pagesize;
58# else
59# define BOOT_PAGESIZE
44#endif 60# endif
45 61#endif
46#define SUB_INIT "Coro::State::initialize"
47#define UCORO_STATE "_coro_state"
48 62
49/* 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
50 * 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
51 * and should be unique. */ 65 * and should be unique. */
66#define dSTACKLEVEL int stacklevel
52#define dSTACKLEVEL void *stacklevel = &stacklevel 67#define STACKLEVEL ((void *)&stacklevel)
53 68
54#define IN_DESTRUCT (PL_main_cv == Nullcv) 69#define IN_DESTRUCT (PL_main_cv == Nullcv)
55 70
56#define labs(l) ((l) >= 0 ? (l) : -(l))
57
58#include "CoroAPI.h" 71#include "CoroAPI.h"
72
73#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
59 74
60#ifdef USE_ITHREADS 75#ifdef USE_ITHREADS
61static perl_mutex coro_mutex; 76static perl_mutex coro_mutex;
62# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 77# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
63# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 78# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
66# define UNLOCK (void)0 81# define UNLOCK (void)0
67#endif 82#endif
68 83
69static struct CoroAPI coroapi; 84static struct CoroAPI coroapi;
70static AV *main_mainstack; /* used to differentiate between $main and others */ 85static AV *main_mainstack; /* used to differentiate between $main and others */
71static HV *coro_state_stash; 86static HV *coro_state_stash, *coro_stash;
72static SV *ucoro_state_sv;
73static U32 ucoro_state_hash;
74static SV *coro_mortal; /* will be freed after next transfer */ 87static SV *coro_mortal; /* will be freed after next transfer */
75 88
76/* this is actually not only the c stack but also c registers etc... */ 89/* this is a structure representing a c-level coroutine */
77typedef struct { 90typedef struct coro_stack {
78 int refcnt; /* pointer reference counter */ 91 struct coro_stack *next;
79 int usecnt; /* shared by how many coroutines */
80 int gencnt; /* generation counter */
81 92
82 coro_context cctx; 93 /* the stack */
83
84 void *sptr; 94 void *sptr;
85 long ssize; /* positive == mmap, otherwise malloc */ 95 long ssize; /* positive == mmap, otherwise malloc */
96
97 /* cpu state */
98 void *idle_sp; /* original stacklevel when coroutine was created */
99 JMPENV *top_env;
100 coro_context cctx;
86} coro_stack; 101} coro_stack;
87 102
103/* this is a structure representing a perl-level coroutine */
88struct coro { 104struct coro {
89 /* the top-level JMPENV for each coroutine, needed to catch dies. */ 105 /* the c coroutine allocated to this perl coroutine, if any */
90 JMPENV start_env;
91
92 /* the optional C context */
93 coro_stack *stack; 106 coro_stack *stack;
94 void *cursp; 107
108 /* data associated with this coroutine (initial args) */
109 AV *args;
95 int gencnt; 110 int refcnt;
96 111
97 /* optionally saved, might be zero */ 112 /* optionally saved, might be zero */
98 AV *defav; 113 AV *defav;
99 SV *defsv; 114 SV *defsv;
100 SV *errsv; 115 SV *errsv;
130 OP **retstack; 145 OP **retstack;
131 I32 retstack_ix; 146 I32 retstack_ix;
132 I32 retstack_max; 147 I32 retstack_max;
133 PMOP *curpm; 148 PMOP *curpm;
134 COP *curcop; 149 COP *curcop;
135 JMPENV *top_env;
136 150
137 /* data associated with this coroutine (initial args) */ 151 /* coro process data */
138 AV *args; 152 int prio;
139}; 153};
140 154
141typedef struct coro *Coro__State; 155typedef struct coro *Coro__State;
142typedef struct coro *Coro__State_or_hashref; 156typedef struct coro *Coro__State_or_hashref;
143 157
147 AV *padlist = CvPADLIST (cv); 161 AV *padlist = CvPADLIST (cv);
148 AV *newpadlist, *newpad; 162 AV *newpadlist, *newpad;
149 163
150 newpadlist = newAV (); 164 newpadlist = newAV ();
151 AvREAL_off (newpadlist); 165 AvREAL_off (newpadlist);
166#if PERL_VERSION < 9
152 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 167 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
168#else
169 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
170#endif
153 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 171 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
154 --AvFILLp (padlist); 172 --AvFILLp (padlist);
155 173
156 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 174 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE)));
157 av_store (newpadlist, 1, (SV *)newpad); 175 av_store (newpadlist, 1, (SV *)newpad);
282 PL_retstack_ix = c->retstack_ix; 300 PL_retstack_ix = c->retstack_ix;
283 PL_retstack_max = c->retstack_max; 301 PL_retstack_max = c->retstack_max;
284#endif 302#endif
285 PL_curpm = c->curpm; 303 PL_curpm = c->curpm;
286 PL_curcop = c->curcop; 304 PL_curcop = c->curcop;
287 PL_top_env = c->top_env;
288 305
289 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 306 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
290 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 307 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
291 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 308 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
292 309
410 c->retstack_ix = PL_retstack_ix; 427 c->retstack_ix = PL_retstack_ix;
411 c->retstack_max = PL_retstack_max; 428 c->retstack_max = PL_retstack_max;
412#endif 429#endif
413 c->curpm = PL_curpm; 430 c->curpm = PL_curpm;
414 c->curcop = PL_curcop; 431 c->curcop = PL_curcop;
415 c->top_env = PL_top_env;
416} 432}
417 433
418/* 434/*
419 * allocate various perl stacks. This is an exact copy 435 * allocate various perl stacks. This is an exact copy
420 * of perl.c:init_stacks, except that it uses less memory 436 * of perl.c:init_stacks, except that it uses less memory
514 Safefree (PL_retstack); 530 Safefree (PL_retstack);
515#endif 531#endif
516} 532}
517 533
518static void 534static void
519allocate_stack (Coro__State ctx, int alloc) 535setup_coro (struct coro *coro)
520{
521 coro_stack *stack;
522
523 New (0, stack, 1, coro_stack);
524
525 stack->refcnt = 1;
526 stack->usecnt = 1;
527 stack->gencnt = ctx->gencnt = 0;
528
529 if (alloc)
530 {
531#if HAVE_MMAP
532 stack->ssize = STACKSIZE * sizeof (long); /* mmap should do allocate-on-write for us */
533 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
534 if (stack->sptr == (void *)-1)
535#endif
536 {
537 stack->ssize = - (STACKSIZE * (long)sizeof (long));
538 New (0, stack->sptr, STACKSIZE, long);
539 }
540 }
541 else
542 stack->sptr = 0;
543
544 ctx->stack = stack;
545}
546
547static void
548deallocate_stack (Coro__State ctx)
549{
550 coro_stack *stack = ctx->stack;
551
552 ctx->stack = 0;
553
554 if (stack)
555 {
556 if (!--stack->refcnt)
557 {
558#ifdef HAVE_MMAP
559 if (stack->ssize > 0 && stack->sptr)
560 munmap (stack->sptr, stack->ssize);
561 else
562#endif
563 Safefree (stack->sptr);
564
565 Safefree (stack);
566 }
567 else if (ctx->gencnt == stack->gencnt)
568 --stack->usecnt;
569 }
570}
571
572static void
573setup_coro (void *arg)
574{ 536{
575 /* 537 /*
576 * emulate part of the perl startup here. 538 * emulate part of the perl startup here.
577 */ 539 */
578 dTHX; 540 dTHX;
579 dSP; 541 dSP;
580 Coro__State ctx = (Coro__State)arg; 542 UNOP myop;
581 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); 543 SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE);
582 544
583 coro_init_stacks (aTHX); 545 coro_init_stacks (aTHX);
584 /*PL_curcop = 0;*/ 546 /*PL_curcop = 0;*/
585 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 547 /*PL_in_eval = PL_in_eval;*/ /* inherit */
586 SvREFCNT_dec (GvAV (PL_defgv)); 548 SvREFCNT_dec (GvAV (PL_defgv));
587 GvAV (PL_defgv) = ctx->args; ctx->args = 0; 549 GvAV (PL_defgv) = coro->args; coro->args = 0;
588 550
589 SPAGAIN; 551 SPAGAIN;
590 552
591 if (ctx->stack)
592 {
593 ctx->cursp = 0;
594
595 PUSHMARK(SP);
596 PUTBACK;
597 (void) call_sv (sub_init, G_VOID|G_NOARGS|G_EVAL);
598
599 if (SvTRUE (ERRSV))
600 croak (NULL);
601 else
602 croak ("FATAL: CCTXT coroutine returned!");
603 }
604 else
605 {
606 UNOP myop;
607
608 PL_op = (OP *)&myop;
609
610 Zero(&myop, 1, UNOP); 553 Zero (&myop, 1, UNOP);
611 myop.op_next = Nullop; 554 myop.op_next = Nullop;
612 myop.op_flags = OPf_WANT_VOID; 555 myop.op_flags = OPf_WANT_VOID;
613 556
557 PL_op = (OP *)&myop;
558
614 PUSHMARK(SP); 559 PUSHMARK(SP);
615 XPUSHs (sub_init); 560 XPUSHs (sub_init);
616 /*
617 * the next line is slightly wrong, as PL_op->op_next
618 * is actually being executed so we skip the first op.
619 * that doesn't matter, though, since it is only
620 * pp_nextstate and we never return...
621 * ah yes, and I don't care anyways ;)
622 */
623 PUTBACK; 561 PUTBACK;
624 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 562 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
625 SPAGAIN; 563 SPAGAIN;
626 564
627 ENTER; /* necessary e.g. for dounwind */ 565 ENTER; /* necessary e.g. for dounwind */
628 }
629} 566}
630 567
631static void 568static void
632continue_coro (void *arg) 569free_coro_mortal ()
633{ 570{
634 /*
635 * this is a _very_ stripped down perl interpreter ;)
636 */
637 dTHX;
638 Coro__State ctx = (Coro__State)arg;
639
640 PL_top_env = &ctx->start_env;
641
642 ctx->cursp = 0;
643 PL_op = PL_op->op_next;
644 CALLRUNOPS(aTHX);
645
646 abort ();
647}
648
649static void
650transfer (pTHX_ struct coro *prev, struct coro *next, int flags)
651{
652 dSTACKLEVEL;
653
654 if (prev != next)
655 {
656 if (next->mainstack)
657 {
658 LOCK;
659 SAVE (prev, flags);
660 LOAD (next);
661 UNLOCK;
662
663 /* mark this state as in-use */
664 next->mainstack = 0;
665 next->tmps_ix = -2;
666
667 /* stacklevel changed? if yes, grab the stack for us! */
668 if (flags & TRANSFER_SAVE_CCTXT)
669 {
670 if (!prev->stack)
671 allocate_stack (prev, 0);
672 else if (prev->cursp != stacklevel
673 && prev->stack->usecnt > 1)
674 {
675 prev->gencnt = ++prev->stack->gencnt;
676 prev->stack->usecnt = 1;
677 }
678
679 /* has our stack been invalidated? */
680 if (next->stack && next->stack->gencnt != next->gencnt)
681 {
682 deallocate_stack (next);
683 allocate_stack (next, 1);
684 coro_create (&(next->stack->cctx),
685 continue_coro, (void *)next,
686 next->stack->sptr, labs (next->stack->ssize));
687 }
688
689 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
690 prev->cursp = stacklevel;
691 /* don't add any code here */
692 }
693 else
694 next->cursp = stacklevel;
695 }
696 else if (next->tmps_ix == -2)
697 croak ("tried to transfer to running coroutine");
698 else
699 {
700 LOCK;
701 SAVE (prev, -1); /* first get rid of the old state */
702 UNLOCK;
703
704 if (flags & TRANSFER_SAVE_CCTXT)
705 {
706 if (!prev->stack)
707 allocate_stack (prev, 0);
708
709 if (prev->stack->sptr && flags & TRANSFER_LAZY_STACK)
710 {
711 PL_top_env = &next->start_env;
712
713 setup_coro (next);
714 next->cursp = stacklevel;
715
716 prev->stack->refcnt++;
717 prev->stack->usecnt++;
718 next->stack = prev->stack;
719 next->gencnt = prev->gencnt;
720 }
721 else
722 {
723 assert (!next->stack);
724 allocate_stack (next, 1);
725 coro_create (&(next->stack->cctx),
726 setup_coro, (void *)next,
727 next->stack->sptr, labs (next->stack->ssize));
728 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
729 prev->cursp = stacklevel;
730 /* don't add any code here */
731 }
732 }
733 else
734 {
735 setup_coro (next);
736 next->cursp = stacklevel;
737 }
738 }
739 }
740
741 LOCK;
742 if (coro_mortal) 571 if (coro_mortal)
743 { 572 {
744 SvREFCNT_dec (coro_mortal); 573 SvREFCNT_dec (coro_mortal);
745 coro_mortal = 0; 574 coro_mortal = 0;
746 } 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
747 UNLOCK; 587 UNLOCK;
748}
749 588
750#define SV_CORO(sv,func) \ 589 PL_top_env = &PL_start_env;
751 do { \
752 if (SvROK (sv)) \
753 sv = SvRV (sv); \
754 \
755 if (SvTYPE (sv) == SVt_PVHV) \
756 { \
757 HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \
758 \
759 if (!he) \
760 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \
761 \
762 (sv) = SvRV (HeVAL(he)); \
763 } \
764 \
765 /* must also be changed inside Coro::Cont::yield */ \
766 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
767 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
768 \
769 } while(0)
770 590
771#define SvSTATE(sv) INT2PTR (struct coro *, SvIV (sv)) 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));
772 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 }
748}
749
750/* use this function pointer to call the above function */
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 */
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)
762
763static void
764coro_state_destroy (struct coro *coro)
765{
766 if (coro->refcnt--)
767 return;
768
769 if (coro->mainstack && coro->mainstack != main_mainstack)
770 {
771 struct coro temp;
772
773 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL);
774 LOAD (aTHX_ coro);
775
776 destroy_stacks (aTHX);
777
778 LOAD ((&temp)); /* this will get rid of defsv etc.. */
779
780 coro->mainstack = 0;
781 }
782
783 stack_free (coro->stack);
784 SvREFCNT_dec (coro->args);
785 Safefree (coro);
786}
787
773static void 788static int
789coro_state_clear (SV *sv, MAGIC *mg)
790{
791 struct coro *coro = (struct coro *)mg->mg_ptr;
792 mg->mg_ptr = 0;
793
794 coro_state_destroy (coro);
795
796 return 0;
797}
798
799static int
800coro_state_dup (MAGIC *mg, CLONE_PARAMS *params)
801{
802 struct coro *coro = (struct coro *)mg->mg_ptr;
803
804 ++coro->refcnt;
805
806 return 0;
807}
808
809static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 };
810
811static struct coro *
812SvSTATE (SV *coro)
813{
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);
829 assert (mg->mg_type == PERL_MAGIC_ext);
830 return (struct coro *)mg->mg_ptr;
831}
832
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
774api_transfer(pTHX_ SV *prev, SV *next, int flags) 842api_transfer (SV *prev, SV *next, int flags)
775{ 843{
776 SV_CORO (prev, "Coro::transfer"); 844 dTHX;
777 SV_CORO (next, "Coro::transfer"); 845 struct transfer_args ta;
778 846
779 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 847 prepare_transfer (aTHX_ &ta, prev, next, flags);
848 TRANSFER (ta);
780} 849}
781 850
782/** Coro ********************************************************************/ 851/** Coro ********************************************************************/
783 852
784#define PRIO_MAX 3 853#define PRIO_MAX 3
788#define PRIO_IDLE -3 857#define PRIO_IDLE -3
789#define PRIO_MIN -4 858#define PRIO_MIN -4
790 859
791/* for Coro.pm */ 860/* for Coro.pm */
792static GV *coro_current, *coro_idle; 861static GV *coro_current, *coro_idle;
793static AV *coro_ready[PRIO_MAX-PRIO_MIN+1]; 862static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
794static int coro_nready; 863static int coro_nready;
795 864
796static void 865static void
797coro_enq (pTHX_ SV *sv) 866coro_enq (pTHX_ SV *sv)
798{ 867{
799 SV **xprio;
800 int prio; 868 int prio;
801 869
802 if (SvTYPE (sv) != SVt_PVHV) 870 if (SvTYPE (sv) != SVt_PVHV)
803 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");
804 872
805 xprio = hv_fetch ((HV *)sv, "prio", 4, 0); 873 prio = SvSTATE (sv)->prio;
806 prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
807
808 prio = prio > PRIO_MAX ? PRIO_MAX
809 : prio < PRIO_MIN ? PRIO_MIN
810 : prio;
811 874
812 av_push (coro_ready [prio - PRIO_MIN], sv); 875 av_push (coro_ready [prio - PRIO_MIN], sv);
813 coro_nready++; 876 coro_nready++;
814} 877}
815 878
821 min_prio -= PRIO_MIN; 884 min_prio -= PRIO_MIN;
822 if (min_prio < 0) 885 if (min_prio < 0)
823 min_prio = 0; 886 min_prio = 0;
824 887
825 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 888 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
826 if (av_len (coro_ready[prio]) >= 0) 889 if (AvFILLp (coro_ready [prio]) >= 0)
827 { 890 {
828 coro_nready--; 891 coro_nready--;
829 return av_shift (coro_ready[prio]); 892 return av_shift (coro_ready [prio]);
830 } 893 }
831 894
832 return 0; 895 return 0;
833} 896}
834 897
844 coro_enq (aTHX_ SvREFCNT_inc (coro)); 907 coro_enq (aTHX_ SvREFCNT_inc (coro));
845 UNLOCK; 908 UNLOCK;
846} 909}
847 910
848static void 911static void
849api_schedule (void) 912prepare_schedule (aTHX_ struct transfer_args *ta)
850{ 913{
851 dTHX;
852
853 SV *prev, *next; 914 SV *current, *prev, *next;
854 915
855 LOCK; 916 LOCK;
856 917
857 prev = SvRV (GvSV (coro_current)); 918 current = GvSV (coro_current);
919
920 for (;;)
921 {
922 LOCK;
923
858 next = coro_deq (aTHX_ PRIO_MIN); 924 next = coro_deq (aTHX_ PRIO_MIN);
859 925
860 if (!next) 926 if (next)
861 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;
862 948
863 /* free this only after the transfer */ 949 /* free this only after the transfer */
950 free_coro_mortal ();
864 coro_mortal = prev; 951 coro_mortal = prev;
865 SV_CORO (prev, "Coro::schedule");
866 952
867 SvRV (GvSV (coro_current)) = next; 953 ta->prev = SvSTATE (prev);
868 954 ta->next = SvSTATE (next);
869 SV_CORO (next, "Coro::schedule"); 955 ta->flags = TRANSFER_SAVE_ALL;
870 956
871 UNLOCK; 957 UNLOCK;
872
873 transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
874 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
875} 958}
876 959
877static void 960static void
878api_cede (void) 961prepare_cede (aTHX_ struct transfer_args *ta)
879{ 962{
880 dTHX;
881
882 LOCK; 963 LOCK;
883 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); 964 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current))));
884 UNLOCK; 965 UNLOCK;
885 966
967 prepare_schedule (ta);
968}
969
970static void
886 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);
887} 988}
888 989
889MODULE = Coro::State PACKAGE = Coro::State 990MODULE = Coro::State PACKAGE = Coro::State
890 991
891PROTOTYPES: ENABLE 992PROTOTYPES: DISABLE
892 993
893BOOT: 994BOOT:
894{ /* {} necessary for stoopid perl-5.6.x */ 995{
895#ifdef USE_ITHREADS 996#ifdef USE_ITHREADS
896 MUTEX_INIT (&coro_mutex); 997 MUTEX_INIT (&coro_mutex);
897#endif 998#endif
999 BOOT_PAGESIZE;
898 1000
899 ucoro_state_sv = newSVpv (UCORO_STATE, sizeof(UCORO_STATE) - 1);
900 PERL_HASH(ucoro_state_hash, UCORO_STATE, sizeof(UCORO_STATE) - 1);
901 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1001 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
902 1002
903 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 1003 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV));
904 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 1004 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV));
905 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 1005 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV));
906 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT));
907 1006
908 main_mainstack = PL_mainstack; 1007 main_mainstack = PL_mainstack;
909 1008
910 coroapi.ver = CORO_API_VERSION; 1009 coroapi.ver = CORO_API_VERSION;
911 coroapi.transfer = api_transfer; 1010 coroapi.transfer = api_transfer;
912}
913 1011
914Coro::State 1012 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
915_newprocess(args) 1013}
916 SV * args 1014
917 PROTOTYPE: $ 1015SV *
1016new (char *klass, ...)
918 CODE: 1017 CODE:
919 Coro__State coro; 1018{
1019 struct coro *coro;
1020 HV *hv;
1021 int i;
920 1022
921 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV)
922 croak ("Coro::State::_newprocess expects an arrayref");
923
924 Newz (0, coro, 1, struct coro); 1023 Newz (0, coro, 1, struct coro);
1024 coro->args = newAV ();
925 1025
926 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 1026 hv = newHV ();
1027 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
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
927 /*coro->mainstack = 0; *//*actual work is done inside transfer */ 1033 /*coro->mainstack = 0; *//*actual work is done inside transfer */
928 /*coro->stack = 0;*/ 1034 /*coro->stack = 0;*/
929 1035}
930 /* same as JMPENV_BOOTSTRAP */
931 /* we might be able to recycle start_env, but safe is safe */
932 /*Zero(&coro->start_env, 1, JMPENV);*/
933 coro->start_env.je_ret = -1;
934 coro->start_env.je_mustcatch = TRUE;
935
936 RETVAL = coro;
937 OUTPUT: 1036 OUTPUT:
938 RETVAL 1037 RETVAL
939 1038
940void 1039void
941transfer(prev, next, flags) 1040_set_stacklevel (...)
942 SV *prev 1041 ALIAS:
943 SV *next 1042 Coro::State::transfer = 1
944 int flags 1043 Coro::schedule = 2
945 PROTOTYPE: @ 1044 Coro::cede = 3
1045 Coro::Cont::yield = 4
946 CODE: 1046 CODE:
947 PUTBACK; 1047{
948 SV_CORO (next, "Coro::transfer"); 1048 struct transfer_args ta;
949 SV_CORO (prev, "Coro::transfer"); 1049
950 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 1050 switch (ix)
951 SPAGAIN; 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:
1059 if (items != 3)
1060 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items);
1061
1062 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2)));
1063 break;
1064
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
1099 }
1100
1101 TRANSFER (ta);
1102}
952 1103
953void 1104void
954DESTROY(coro) 1105_clone_state_from (SV *dst, SV *src)
955 Coro::State coro 1106 CODE:
956 CODE: 1107{
1108 struct coro *coro_src = SvSTATE (src);
957 1109
958 if (coro->mainstack && coro->mainstack != main_mainstack) 1110 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
959 {
960 struct coro temp;
961 1111
962 PUTBACK; 1112 ++coro_src->refcnt;
963 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); 1113 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
964 LOAD (aTHX_ coro); 1114}
965 SPAGAIN;
966
967 destroy_stacks (aTHX);
968
969 LOAD ((&temp)); /* this will get rid of defsv etc.. */
970 SPAGAIN;
971
972 coro->mainstack = 0;
973 }
974
975 deallocate_stack (coro);
976 SvREFCNT_dec (coro->args);
977 Safefree (coro);
978 1115
979void 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);
1121
1122void
980_exit(code) 1123_exit (code)
981 int code 1124 int code
982 PROTOTYPE: $ 1125 PROTOTYPE: $
983 CODE: 1126 CODE:
984 _exit (code); 1127 _exit (code);
985 1128
986MODULE = Coro::State PACKAGE = Coro::Cont
987
988# this is slightly dirty (should expose a c-level api)
989
990void
991yield(...)
992 PROTOTYPE: @
993 CODE:
994 SV *yieldstack;
995 SV *sv;
996 AV *defav = GvAV (PL_defgv);
997 struct coro *prev, *next;
998
999 yieldstack = *hv_fetch (
1000 (HV *)SvRV (GvSV (coro_current)),
1001 "yieldstack", sizeof ("yieldstack") - 1,
1002 0
1003 );
1004
1005 /* set up @_ -- ugly */
1006 av_clear (defav);
1007 av_fill (defav, items - 1);
1008 while (items--)
1009 av_store (defav, items, SvREFCNT_inc (ST(items)));
1010
1011 sv = av_pop ((AV *)SvRV (yieldstack));
1012 prev = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 0, 0))));
1013 next = INT2PTR (struct coro *, SvIV ((SV*)SvRV (*av_fetch ((AV *)SvRV (sv), 1, 0))));
1014 SvREFCNT_dec (sv);
1015
1016 transfer (aTHX_ prev, next, 0);
1017
1018MODULE = Coro::State PACKAGE = Coro 1129MODULE = Coro::State PACKAGE = Coro
1019 1130
1020# this is slightly dirty (should expose a c-level api)
1021
1022BOOT: 1131BOOT:
1023{ 1132{
1024 int i; 1133 int i;
1134
1025 HV *stash = gv_stashpv ("Coro", TRUE); 1135 coro_stash = gv_stashpv ("Coro", TRUE);
1026 1136
1027 newCONSTSUB (stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1137 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1028 newCONSTSUB (stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1138 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1029 newCONSTSUB (stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1139 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1030 newCONSTSUB (stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1140 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1031 newCONSTSUB (stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1141 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1032 newCONSTSUB (stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1142 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1033 1143
1034 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); 1144 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV);
1035 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); 1145 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV);
1036 1146
1037 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1147 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1045 coroapi.ready = api_ready; 1155 coroapi.ready = api_ready;
1046 coroapi.nready = &coro_nready; 1156 coroapi.nready = &coro_nready;
1047 coroapi.current = coro_current; 1157 coroapi.current = coro_current;
1048 1158
1049 GCoroAPI = &coroapi; 1159 GCoroAPI = &coroapi;
1050 sv_setiv(sv, (IV)&coroapi); 1160 sv_setiv (sv, (IV)&coroapi);
1051 SvREADONLY_on(sv); 1161 SvREADONLY_on (sv);
1052 } 1162 }
1053} 1163}
1054 1164
1055#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}
1056 1184
1057void 1185void
1058ready(self) 1186ready (SV *self)
1059 SV * self
1060 PROTOTYPE: $ 1187 PROTOTYPE: $
1061 CODE: 1188 CODE:
1062 api_ready (self); 1189 api_ready (self);
1063 1190
1064#endif
1065
1066int 1191int
1067nready(...) 1192nready (...)
1068 PROTOTYPE: 1193 PROTOTYPE:
1069 CODE: 1194 CODE:
1070 RETVAL = coro_nready; 1195 RETVAL = coro_nready;
1071 OUTPUT: 1196 OUTPUT:
1072 RETVAL 1197 RETVAL
1073 1198
1074void 1199MODULE = Coro::State PACKAGE = Coro::AIO
1075schedule(...)
1076 PROTOTYPE:
1077 CODE:
1078 api_schedule ();
1079 1200
1080void
1081cede(...)
1082 PROTOTYPE:
1083 CODE:
1084 api_cede ();
1085
1086# and these are hacks
1087SV * 1201SV *
1088_aio_get_state () 1202_get_state ()
1089 CODE: 1203 CODE:
1090{ 1204{
1091 struct { 1205 struct {
1092 int errorno; 1206 int errorno;
1093 int laststype; 1207 int laststype;
1104} 1218}
1105 OUTPUT: 1219 OUTPUT:
1106 RETVAL 1220 RETVAL
1107 1221
1108void 1222void
1109_aio_set_state (char *data_) 1223_set_state (char *data_)
1110 PROTOTYPE: $ 1224 PROTOTYPE: $
1111 CODE: 1225 CODE:
1112{ 1226{
1113 struct { 1227 struct {
1114 int errorno; 1228 int errorno;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines