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.92 by root, Sun Nov 26 02:16:19 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)) 71#define labs(l) ((l) >= 0 ? (l) : -(l))
72 72
73#include "CoroAPI.h" 73#include "CoroAPI.h"
74
75#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
74 76
75#ifdef USE_ITHREADS 77#ifdef USE_ITHREADS
76static perl_mutex coro_mutex; 78static perl_mutex coro_mutex;
77# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 79# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
78# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 80# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
84static struct CoroAPI coroapi; 86static struct CoroAPI coroapi;
85static AV *main_mainstack; /* used to differentiate between $main and others */ 87static AV *main_mainstack; /* used to differentiate between $main and others */
86static HV *coro_state_stash, *coro_stash; 88static HV *coro_state_stash, *coro_stash;
87static SV *coro_mortal; /* will be freed after next transfer */ 89static SV *coro_mortal; /* will be freed after next transfer */
88 90
89/* this is actually not only the c stack but also c registers etc... */ 91/* this is a structure representing a c-level coroutine */
90typedef struct { 92typedef struct coro_stack {
91 int refcnt; /* pointer reference counter */ 93 struct coro_stack *next;
92 int usecnt; /* shared by how many coroutines */
93 int gencnt; /* generation counter */
94 94
95 coro_context cctx; 95 void *idle_sp; /* original stacklevel when coroutine was created */
96
97 void *sptr; 96 void *sptr;
98 long ssize; /* positive == mmap, otherwise malloc */ 97 long ssize; /* positive == mmap, otherwise malloc */
98
99 /* cpu state */
100 coro_context cctx;
101 JMPENV *top_env;
99} coro_stack; 102} coro_stack;
100 103
104/* the (fake) coro_stack representing the main program */
105static coro_stack *main_stack;
106
107/* this is a structure representing a perl-level coroutine */
101struct coro { 108struct coro {
102 /* the optional C context */ 109 /* the c coroutine allocated to this perl coroutine, if any */
103 coro_stack *stack; 110 coro_stack *stack;
104 void *cursp; 111
112 /* data associated with this coroutine (initial args) */
113 AV *args;
105 int gencnt; 114 int refcnt;
106 115
107 /* optionally saved, might be zero */ 116 /* optionally saved, might be zero */
108 AV *defav; 117 AV *defav;
109 SV *defsv; 118 SV *defsv;
110 SV *errsv; 119 SV *errsv;
140 OP **retstack; 149 OP **retstack;
141 I32 retstack_ix; 150 I32 retstack_ix;
142 I32 retstack_max; 151 I32 retstack_max;
143 PMOP *curpm; 152 PMOP *curpm;
144 COP *curcop; 153 COP *curcop;
145 JMPENV *top_env;
146 154
147 /* coro process data */ 155 /* coro process data */
148 int prio; 156 int prio;
149
150 /* data associated with this coroutine (initial args) */
151 AV *args;
152 int refcnt;
153}; 157};
154 158
155typedef struct coro *Coro__State; 159typedef struct coro *Coro__State;
156typedef struct coro *Coro__State_or_hashref; 160typedef struct coro *Coro__State_or_hashref;
157 161
300 PL_retstack_ix = c->retstack_ix; 304 PL_retstack_ix = c->retstack_ix;
301 PL_retstack_max = c->retstack_max; 305 PL_retstack_max = c->retstack_max;
302#endif 306#endif
303 PL_curpm = c->curpm; 307 PL_curpm = c->curpm;
304 PL_curcop = c->curcop; 308 PL_curcop = c->curcop;
305 PL_top_env = c->top_env;
306 309
307 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 310 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
308 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 311 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
309 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 312 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
310 313
428 c->retstack_ix = PL_retstack_ix; 431 c->retstack_ix = PL_retstack_ix;
429 c->retstack_max = PL_retstack_max; 432 c->retstack_max = PL_retstack_max;
430#endif 433#endif
431 c->curpm = PL_curpm; 434 c->curpm = PL_curpm;
432 c->curcop = PL_curcop; 435 c->curcop = PL_curcop;
433 c->top_env = PL_top_env;
434} 436}
435 437
436/* 438/*
437 * allocate various perl stacks. This is an exact copy 439 * allocate various perl stacks. This is an exact copy
438 * of perl.c:init_stacks, except that it uses less memory 440 * of perl.c:init_stacks, except that it uses less memory
532 Safefree (PL_retstack); 534 Safefree (PL_retstack);
533#endif 535#endif
534} 536}
535 537
536static void 538static void
537allocate_stack (Coro__State ctx, int alloc) 539setup_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{ 540{
599 /* 541 /*
600 * emulate part of the perl startup here. 542 * emulate part of the perl startup here.
601 */ 543 */
602 dTHX; 544 dTHX;
603 dSP; 545 dSP;
604 Coro__State ctx = (Coro__State)arg; 546 UNOP myop;
605 SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); 547 SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE);
606 548
607 coro_init_stacks (aTHX); 549 coro_init_stacks (aTHX);
608 /*PL_curcop = 0;*/ 550 /*PL_curcop = 0;*/
609 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 551 /*PL_in_eval = PL_in_eval;*/ /* inherit */
610 SvREFCNT_dec (GvAV (PL_defgv)); 552 SvREFCNT_dec (GvAV (PL_defgv));
611 GvAV (PL_defgv) = ctx->args; ctx->args = 0; 553 GvAV (PL_defgv) = coro->args; coro->args = 0;
612 554
613 SPAGAIN; 555 SPAGAIN;
614 556
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); 557 Zero (&myop, 1, UNOP);
633 myop.op_next = Nullop; 558 myop.op_next = Nullop;
634 myop.op_flags = OPf_WANT_VOID; 559 myop.op_flags = OPf_WANT_VOID;
635 560
636 PL_op = (OP *)&myop; 561 PL_op = (OP *)&myop;
637 562
638 PUSHMARK(SP); 563 PUSHMARK(SP);
639 XPUSHs (sub_init); 564 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; 565 PUTBACK;
648 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 566 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
649 SPAGAIN; 567 SPAGAIN;
650 568
651 ENTER; /* necessary e.g. for dounwind */ 569 ENTER; /* necessary e.g. for dounwind */
652 }
653} 570}
654 571
655static void 572static void
656continue_coro (void *arg) 573free_coro_mortal ()
657{ 574{
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) 575 if (coro_mortal)
771 { 576 {
772 SvREFCNT_dec (coro_mortal); 577 SvREFCNT_dec (coro_mortal);
773 coro_mortal = 0; 578 coro_mortal = 0;
774 } 579 }
580}
581
582static void
583coro_run (void *arg)
584{
585 /*
586 * this is a _very_ stripped down perl interpreter ;)
587 */
588 dTHX;
589 PL_top_env = &PL_start_env;
590
775 UNLOCK; 591 UNLOCK;
592
593 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg));
594 sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op));
595 PL_restartop = CvSTART (get_cv ("Coro::State::cctx_init", FALSE));
596
597 /* somebody will hit me for both perl_run and PL_restart_op */
598 perl_run (aTHX_ PERL_GET_CONTEXT);
599
600 abort ();
601}
602
603static coro_stack *
604stack_new ()
605{
606 coro_stack *stack;
607
608 New (0, stack, 1, coro_stack);
609
610#if HAVE_MMAP
611
612 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */
613 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
614
615 if (stack->sptr == (void *)-1)
616 {
617 perror ("FATAL: unable to mmap stack for coroutine");
618 _exit (EXIT_FAILURE);
619 }
620
621 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
622
623#else
624
625 stack->ssize = STACKSIZE * (long)sizeof (long);
626 New (0, stack->sptr, STACKSIZE, long);
627
628 if (!stack->sptr)
629 {
630 perror (stderr, "FATAL: unable to malloc stack for coroutine");
631 _exit (EXIT_FAILURE);
632 }
633
634#endif
635
636 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize);
637
638 return stack;
639}
640
641static void
642stack_free (coro_stack *stack)
643{
644 if (!stack || stack == main_stack)
645 return;
646
647#if HAVE_MMAP
648 munmap (stack->sptr, stack->ssize);
649#else
650 Safefree (stack->sptr);
651#endif
652
653 Safefree (stack);
654}
655
656static coro_stack *stack_first;
657
658static coro_stack *
659stack_get ()
660{
661 coro_stack *stack;
662
663 if (stack_first)
664 {
665 stack = stack_first;
666 stack_first = stack->next;
667 }
668 else
669 {
670 stack = stack_new ();
671 PL_op = PL_op->op_next;
672 }
673
674 return stack;
675}
676
677static void
678stack_put (coro_stack *stack)
679{
680 stack->next = stack_first;
681 stack_first = stack;
682}
683
684/* never call directly, always through the coro_state_transfer global variable */
685static void
686transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags)
687{
688 dSTACKLEVEL;
689
690 /* sometimes transfer is only called to set idle_sp */
691 if (flags == TRANSFER_SET_STACKLEVEL)
692 ((coro_stack *)prev)->idle_sp = STACKLEVEL;
693 else if (prev != next)
694 {
695 coro_stack *prev__stack;
696
697 LOCK;
698
699 if (next->mainstack)
700 {
701 /* coroutine already started */
702 SAVE (prev, flags);
703 LOAD (next);
704 }
705 else
706 {
707 /* need to start coroutine */
708 /* first get rid of the old state */
709 SAVE (prev, -1);
710 /* setup coroutine call */
711 setup_coro (next);
712 /* need a stack */
713 next->stack = 0;
714 }
715
716 prev__stack = prev->stack;
717
718 /* possibly "free" the stack */
719 if (prev__stack->idle_sp == STACKLEVEL)
720 {
721 stack_put (prev__stack);
722 prev->stack = 0;
723 }
724
725 if (!next->stack)
726 next->stack = stack_get ();
727
728 if (prev__stack != next->stack)
729 {
730 prev__stack->top_env = PL_top_env;
731 PL_top_env = next->stack->top_env;
732 coro_transfer (&prev__stack->cctx, &next->stack->cctx);
733 }
734
735 free_coro_mortal ();
736
737 UNLOCK;
738 }
776} 739}
777 740
778/* use this function pointer to call the above function */ 741/* use this function pointer to call the above function */
779/* this is done to increase chances of the compiler not inlining the call */ 742/* this is done to increase chances of the compiler not inlining the call */
743/* 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; 744void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl;
745
746struct transfer_args
747{
748 struct coro *prev, *next;
749 int flags;
750};
751
752#define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags)
781 753
782static void 754static void
783coro_state_destroy (struct coro *coro) 755coro_state_destroy (struct coro *coro)
784{ 756{
785 if (coro->refcnt--) 757 if (coro->refcnt--)
797 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 769 LOAD ((&temp)); /* this will get rid of defsv etc.. */
798 770
799 coro->mainstack = 0; 771 coro->mainstack = 0;
800 } 772 }
801 773
802 deallocate_stack (coro); 774 stack_free (coro->stack);
803 SvREFCNT_dec (coro->args); 775 SvREFCNT_dec (coro->args);
804 Safefree (coro); 776 Safefree (coro);
805} 777}
806 778
807static int 779static int
848 assert (mg->mg_type == PERL_MAGIC_ext); 820 assert (mg->mg_type == PERL_MAGIC_ext);
849 return (struct coro *)mg->mg_ptr; 821 return (struct coro *)mg->mg_ptr;
850} 822}
851 823
852static void 824static void
825prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev, SV *next, int flags)
826{
827 ta->prev = SvSTATE (prev);
828 ta->next = SvSTATE (next);
829 ta->flags = flags;
830}
831
832static void
853api_transfer (pTHX_ SV *prev, SV *next, int flags) 833api_transfer (SV *prev, SV *next, int flags)
854{ 834{
855 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 835 dTHX;
836 struct transfer_args ta;
837
838 prepare_transfer (aTHX_ &ta, prev, next, flags);
839 TRANSFER (ta);
856} 840}
857 841
858/** Coro ********************************************************************/ 842/** Coro ********************************************************************/
859 843
860#define PRIO_MAX 3 844#define PRIO_MAX 3
914 coro_enq (aTHX_ SvREFCNT_inc (coro)); 898 coro_enq (aTHX_ SvREFCNT_inc (coro));
915 UNLOCK; 899 UNLOCK;
916} 900}
917 901
918static void 902static void
919api_schedule (void) 903prepare_schedule (aTHX_ struct transfer_args *ta)
920{ 904{
921 dTHX;
922
923 SV *prev, *next; 905 SV *current, *prev, *next;
906
907 LOCK;
908
924 SV *current = GvSV (coro_current); 909 current = GvSV (coro_current);
925 910
926 for (;;) 911 for (;;)
927 { 912 {
928 LOCK; 913 LOCK;
929 914
951 936
952 prev = SvRV (current); 937 prev = SvRV (current);
953 SvRV (current) = next; 938 SvRV (current) = next;
954 939
955 /* free this only after the transfer */ 940 /* free this only after the transfer */
941 free_coro_mortal ();
956 coro_mortal = prev; 942 coro_mortal = prev;
957 943
944 ta->prev = SvSTATE (prev);
945 ta->next = SvSTATE (next);
946 ta->flags = TRANSFER_SAVE_ALL;
947
958 UNLOCK; 948 UNLOCK;
959
960 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
961 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
962} 949}
963 950
964static void 951static void
965api_cede (void) 952prepare_cede (aTHX_ struct transfer_args *ta)
966{ 953{
967 dTHX;
968
969 LOCK; 954 LOCK;
970 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); 955 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current))));
971 UNLOCK; 956 UNLOCK;
972 957
958 prepare_schedule (ta);
959}
960
961static void
973 api_schedule (); 962api_schedule (void)
963{
964 dTHX;
965 struct transfer_args ta;
966
967 prepare_schedule (&ta);
968 TRANSFER (ta);
969}
970
971static void
972api_cede (void)
973{
974 dTHX;
975 struct transfer_args ta;
976
977 prepare_cede (&ta);
978 TRANSFER (ta);
974} 979}
975 980
976MODULE = Coro::State PACKAGE = Coro::State 981MODULE = Coro::State PACKAGE = Coro::State
977 982
978PROTOTYPES: DISABLE 983PROTOTYPES: DISABLE
987 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 992 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
988 993
989 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 994 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV));
990 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 995 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV));
991 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 996 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV));
992 newCONSTSUB (coro_state_stash, "SAVE_CCTXT", newSViv (TRANSFER_SAVE_CCTXT));
993 997
994 main_mainstack = PL_mainstack; 998 main_mainstack = PL_mainstack;
995 999
996 coroapi.ver = CORO_API_VERSION; 1000 coroapi.ver = CORO_API_VERSION;
997 coroapi.transfer = api_transfer; 1001 coroapi.transfer = api_transfer;
1002
1003 Newz (0, main_stack, 1, coro_stack);
1004 main_stack->idle_sp = (void *)-1;
998 1005
999 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1006 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1000} 1007}
1001 1008
1002SV * 1009SV *
1008 int i; 1015 int i;
1009 1016
1010 Newz (0, coro, 1, struct coro); 1017 Newz (0, coro, 1, struct coro);
1011 coro->args = newAV (); 1018 coro->args = newAV ();
1012 1019
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 (); 1020 hv = newHV ();
1020 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1021 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)); 1022 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1023
1024 for (i = 1; i < items; i++)
1025 av_push (coro->args, newSVsv (ST (i)));
1026
1027 coro->stack = main_stack;
1028 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1029 /*coro->stack = 0;*/
1022} 1030}
1023 OUTPUT: 1031 OUTPUT:
1024 RETVAL 1032 RETVAL
1025 1033
1026void 1034void
1027transfer(prev, next, flags) 1035transfer (...)
1028 SV *prev 1036 ALIAS:
1029 SV *next 1037 Coro::schedule = 1
1030 int flags 1038 Coro::cede = 2
1039 _set_stacklevel = 3
1031 CODE: 1040 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{ 1041{
1042 RETVAL = coro->prio; 1042 struct transfer_args ta;
1043 1043
1044 if (items > 1) 1044 switch (ix)
1045 { 1045 {
1046 case 0:
1046 if (ix) 1047 if (items != 3)
1047 newprio += coro->prio; 1048 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items);
1048 1049
1049 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 1050 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2)));
1050 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 1051 break;
1051 1052
1052 coro->prio = newprio; 1053 case 1:
1054 prepare_schedule (&ta);
1055 break;
1056
1057 case 2:
1058 prepare_cede (&ta);
1059 break;
1060
1061 case 3:
1062 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0)));
1063 ta.next = 0;
1064 ta.flags = TRANSFER_SET_STACKLEVEL;
1065 break;
1053 } 1066 }
1067
1068 TRANSFER (ta);
1054} 1069}
1055 1070
1056void 1071void
1057_clone_state_from (SV *dst, SV *src) 1072_clone_state_from (SV *dst, SV *src)
1058 CODE: 1073 CODE:
1062 sv_unmagic (SvRV (dst), PERL_MAGIC_ext); 1077 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1063 1078
1064 ++coro_src->refcnt; 1079 ++coro_src->refcnt;
1065 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; 1080 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1066} 1081}
1082
1083void
1084_nonlocal_goto (IV nextop)
1085 CODE:
1086 /* uuh, somebody will kill me again for this */
1087 PL_op->op_next = INT2PTR (OP *, nextop);
1067 1088
1068void 1089void
1069_exit (code) 1090_exit (code)
1070 int code 1091 int code
1071 PROTOTYPE: $ 1092 PROTOTYPE: $
1138 sv_setiv (sv, (IV)&coroapi); 1159 sv_setiv (sv, (IV)&coroapi);
1139 SvREADONLY_on (sv); 1160 SvREADONLY_on (sv);
1140 } 1161 }
1141} 1162}
1142 1163
1143#if !PERL_MICRO 1164int
1165prio (Coro::State coro, int newprio = 0)
1166 ALIAS:
1167 nice = 1
1168 CODE:
1169{
1170 RETVAL = coro->prio;
1171
1172 if (items > 1)
1173 {
1174 if (ix)
1175 newprio += coro->prio;
1176
1177 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1178 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1179
1180 coro->prio = newprio;
1181 }
1182}
1144 1183
1145void 1184void
1146ready (self) 1185ready (SV *self)
1147 SV * self
1148 PROTOTYPE: $ 1186 PROTOTYPE: $
1149 CODE: 1187 CODE:
1150 api_ready (self); 1188 api_ready (self);
1151
1152#endif
1153 1189
1154int 1190int
1155nready (...) 1191nready (...)
1156 PROTOTYPE: 1192 PROTOTYPE:
1157 CODE: 1193 CODE:
1158 RETVAL = coro_nready; 1194 RETVAL = coro_nready;
1159 OUTPUT: 1195 OUTPUT:
1160 RETVAL 1196 RETVAL
1161 1197
1162void 1198MODULE = Coro::State PACKAGE = Coro::AIO
1163schedule (...)
1164 PROTOTYPE:
1165 CODE:
1166 api_schedule ();
1167 1199
1168void
1169cede (...)
1170 PROTOTYPE:
1171 CODE:
1172 api_cede ();
1173
1174# and these are hacks
1175SV * 1200SV *
1176_aio_get_state () 1201_get_state ()
1177 CODE: 1202 CODE:
1178{ 1203{
1179 struct { 1204 struct {
1180 int errorno; 1205 int errorno;
1181 int laststype; 1206 int laststype;
1192} 1217}
1193 OUTPUT: 1218 OUTPUT:
1194 RETVAL 1219 RETVAL
1195 1220
1196void 1221void
1197_aio_set_state (char *data_) 1222_set_state (char *data_)
1198 PROTOTYPE: $ 1223 PROTOTYPE: $
1199 CODE: 1224 CODE:
1200{ 1225{
1201 struct { 1226 struct {
1202 int errorno; 1227 int errorno;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines