… | |
… | |
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 |
76 | static perl_mutex coro_mutex; |
78 | static 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) |
… | |
… | |
84 | static struct CoroAPI coroapi; |
86 | static struct CoroAPI coroapi; |
85 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
87 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
86 | static HV *coro_state_stash, *coro_stash; |
88 | static HV *coro_state_stash, *coro_stash; |
87 | static SV *coro_mortal; /* will be freed after next transfer */ |
89 | static SV *coro_mortal; /* will be freed after next transfer */ |
88 | |
90 | |
89 | /* this is actually not just the c stack but also c registers etc... */ |
91 | /* this is a structure representing a c-level coroutine */ |
90 | typedef struct coro_stack { |
92 | typedef struct coro_stack { |
91 | struct coro_stack *next; |
93 | struct coro_stack *next; |
92 | |
94 | |
93 | void *idle_sp; |
95 | void *idle_sp; /* original stacklevel when coroutine was created */ |
94 | void *sptr; |
96 | void *sptr; |
95 | long ssize; /* positive == mmap, otherwise malloc */ |
97 | long ssize; /* positive == mmap, otherwise malloc */ |
96 | |
98 | |
97 | /* cpu state */ |
99 | /* cpu state */ |
98 | coro_context cctx; |
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 */ |
101 | static coro_stack *main_stack; |
105 | static coro_stack *main_stack; |
102 | |
106 | |
|
|
107 | /* this is a structure representing a perl-level coroutine */ |
103 | struct coro { |
108 | struct coro { |
|
|
109 | /* the c coroutine allocated to this perl coroutine, if any */ |
|
|
110 | coro_stack *stack; |
|
|
111 | |
|
|
112 | /* data associated with this coroutine (initial args) */ |
|
|
113 | AV *args; |
|
|
114 | int refcnt; |
|
|
115 | |
104 | /* optionally saved, might be zero */ |
116 | /* optionally saved, might be zero */ |
105 | AV *defav; |
117 | AV *defav; |
106 | SV *defsv; |
118 | SV *defsv; |
107 | SV *errsv; |
119 | SV *errsv; |
108 | |
120 | |
109 | /* saved global state not related to stacks */ |
121 | /* saved global state not related to stacks */ |
110 | U8 dowarn; |
122 | U8 dowarn; |
111 | I32 in_eval; |
123 | I32 in_eval; |
112 | |
|
|
113 | /* the c stack, if any */ |
|
|
114 | coro_stack *stack; |
|
|
115 | |
124 | |
116 | /* the stacks and related info (callchain etc..) */ |
125 | /* the stacks and related info (callchain etc..) */ |
117 | PERL_SI *curstackinfo; |
126 | PERL_SI *curstackinfo; |
118 | AV *curstack; |
127 | AV *curstack; |
119 | AV *mainstack; |
128 | AV *mainstack; |
… | |
… | |
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 | |
155 | typedef struct coro *Coro__State; |
159 | typedef struct coro *Coro__State; |
156 | typedef struct coro *Coro__State_or_hashref; |
160 | typedef 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 |
… | |
… | |
540 | * emulate part of the perl startup here. |
542 | * emulate part of the perl startup here. |
541 | */ |
543 | */ |
542 | dTHX; |
544 | dTHX; |
543 | dSP; |
545 | dSP; |
544 | UNOP myop; |
546 | UNOP myop; |
545 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
547 | SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE); |
546 | |
548 | |
547 | coro_init_stacks (aTHX); |
549 | coro_init_stacks (aTHX); |
548 | /*PL_curcop = 0;*/ |
550 | /*PL_curcop = 0;*/ |
549 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
551 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
550 | SvREFCNT_dec (GvAV (PL_defgv)); |
552 | SvREFCNT_dec (GvAV (PL_defgv)); |
… | |
… | |
566 | |
568 | |
567 | ENTER; /* necessary e.g. for dounwind */ |
569 | ENTER; /* necessary e.g. for dounwind */ |
568 | } |
570 | } |
569 | |
571 | |
570 | static void |
572 | static void |
571 | transfer_tail () |
573 | free_coro_mortal () |
572 | { |
574 | { |
573 | if (coro_mortal) |
575 | if (coro_mortal) |
574 | { |
576 | { |
575 | SvREFCNT_dec (coro_mortal); |
577 | SvREFCNT_dec (coro_mortal); |
576 | coro_mortal = 0; |
578 | coro_mortal = 0; |
577 | } |
579 | } |
578 | |
|
|
579 | UNLOCK; |
|
|
580 | } |
580 | } |
581 | |
581 | |
582 | static void |
582 | static void |
583 | coro_run (void *arg) |
583 | coro_run (void *arg) |
584 | { |
584 | { |
585 | /* |
585 | /* |
586 | * this is a _very_ stripped down perl interpreter ;) |
586 | * this is a _very_ stripped down perl interpreter ;) |
587 | */ |
587 | */ |
588 | dTHX; |
588 | dTHX; |
589 | |
|
|
590 | transfer_tail (); |
|
|
591 | |
|
|
592 | PL_top_env = &PL_start_env; |
589 | PL_top_env = &PL_start_env; |
593 | PL_restartop = PL_op->op_next; |
590 | |
|
|
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 | |
594 | /* somebody will hit me for both perl_run and PL_restart_top */ |
597 | /* somebody will hit me for both perl_run and PL_restart_op */ |
595 | perl_run (aTHX_ PERL_GET_CONTEXT); |
598 | perl_run (aTHX_ PERL_GET_CONTEXT); |
596 | |
599 | |
597 | abort (); |
600 | abort (); |
598 | } |
601 | } |
599 | |
602 | |
… | |
… | |
609 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */ |
612 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */ |
610 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
613 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
611 | |
614 | |
612 | if (stack->sptr == (void *)-1) |
615 | if (stack->sptr == (void *)-1) |
613 | { |
616 | { |
614 | fprintf (stderr, "FATAL: unable to mmap stack for coroutine\n"); |
617 | perror ("FATAL: unable to mmap stack for coroutine"); |
615 | _exit (EXIT_FAILURE); |
618 | _exit (EXIT_FAILURE); |
616 | } |
619 | } |
617 | |
620 | |
618 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
621 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
619 | |
622 | |
… | |
… | |
622 | stack->ssize = STACKSIZE * (long)sizeof (long); |
625 | stack->ssize = STACKSIZE * (long)sizeof (long); |
623 | New (0, stack->sptr, STACKSIZE, long); |
626 | New (0, stack->sptr, STACKSIZE, long); |
624 | |
627 | |
625 | if (!stack->sptr) |
628 | if (!stack->sptr) |
626 | { |
629 | { |
627 | fprintf (stderr, "FATAL: unable to malloc stack for coroutine\n"); |
630 | perror (stderr, "FATAL: unable to malloc stack for coroutine"); |
628 | _exit (EXIT_FAILURE); |
631 | _exit (EXIT_FAILURE); |
629 | } |
632 | } |
630 | |
633 | |
631 | #endif |
634 | #endif |
632 | |
635 | |
633 | coro_create (&stack->cctx, coro_run, 0, stack->sptr, stack->ssize); |
636 | coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); |
634 | |
637 | |
635 | return stack; |
638 | return stack; |
636 | } |
639 | } |
637 | |
640 | |
638 | static void |
641 | static void |
… | |
… | |
650 | Safefree (stack); |
653 | Safefree (stack); |
651 | } |
654 | } |
652 | |
655 | |
653 | static coro_stack *stack_first; |
656 | static coro_stack *stack_first; |
654 | |
657 | |
655 | static void |
658 | static coro_stack * |
656 | stack_get (struct coro *coro) |
659 | stack_get () |
657 | { |
660 | { |
|
|
661 | coro_stack *stack; |
|
|
662 | |
658 | if (stack_first) |
663 | if (stack_first) |
659 | { |
664 | { |
660 | coro->stack = stack_first; |
665 | stack = stack_first; |
661 | stack_first = stack_first->next; |
666 | stack_first = stack->next; |
662 | } |
667 | } |
663 | else |
668 | else |
|
|
669 | { |
664 | coro->stack = stack_new (); |
670 | stack = stack_new (); |
|
|
671 | PL_op = PL_op->op_next; |
|
|
672 | } |
|
|
673 | |
|
|
674 | return stack; |
665 | } |
675 | } |
666 | |
676 | |
667 | static void |
677 | static void |
668 | stack_put (coro_stack *stack) |
678 | stack_put (coro_stack *stack) |
669 | { |
679 | { |
… | |
… | |
676 | transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) |
686 | transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) |
677 | { |
687 | { |
678 | dSTACKLEVEL; |
688 | dSTACKLEVEL; |
679 | |
689 | |
680 | /* sometimes transfer is only called to set idle_sp */ |
690 | /* sometimes transfer is only called to set idle_sp */ |
681 | if (!prev->stack->idle_sp) |
691 | if (flags == TRANSFER_SET_STACKLEVEL) |
682 | prev->stack->idle_sp = stacklevel; |
692 | ((coro_stack *)prev)->idle_sp = STACKLEVEL; |
683 | |
|
|
684 | LOCK; |
|
|
685 | |
|
|
686 | if (prev != next) |
693 | else if (prev != next) |
687 | { |
694 | { |
688 | coro_stack *prev_stack = prev->stack; |
695 | coro_stack *prev__stack; |
689 | |
696 | |
690 | /* possibly "free" the stack */ |
697 | LOCK; |
691 | if (prev_stack->idle_sp == stacklevel) |
698 | |
|
|
699 | if (next->mainstack) |
692 | { |
700 | { |
693 | stack_put (prev_stack); |
701 | /* coroutine already started */ |
694 | prev->stack = 0; |
702 | SAVE (prev, flags); |
|
|
703 | LOAD (next); |
695 | } |
704 | } |
696 | |
705 | else |
697 | if (!next->mainstack) |
|
|
698 | { |
706 | { |
|
|
707 | /* need to start coroutine */ |
699 | /* first get rid of the old state */ |
708 | /* first get rid of the old state */ |
700 | SAVE (prev, -1); |
709 | SAVE (prev, -1); |
701 | /* setup coroutine call */ |
710 | /* setup coroutine call */ |
702 | setup_coro (next); |
711 | setup_coro (next); |
|
|
712 | /* need a stack */ |
703 | next->stack = 0; |
713 | next->stack = 0; |
704 | } |
714 | } |
705 | else |
715 | |
|
|
716 | prev__stack = prev->stack; |
|
|
717 | |
|
|
718 | /* possibly "free" the stack */ |
|
|
719 | if (prev__stack->idle_sp == STACKLEVEL) |
706 | { |
720 | { |
707 | SAVE (prev, flags); |
721 | stack_put (prev__stack); |
708 | LOAD (next); |
722 | prev->stack = 0; |
709 | } |
723 | } |
710 | |
724 | |
711 | if (!next->stack) |
725 | if (!next->stack) |
712 | stack_get (next); |
726 | next->stack = stack_get (); |
713 | |
727 | |
714 | if (prev_stack != next->stack) |
728 | if (prev__stack != next->stack) |
|
|
729 | { |
|
|
730 | prev__stack->top_env = PL_top_env; |
|
|
731 | PL_top_env = next->stack->top_env; |
715 | coro_transfer (&prev_stack->cctx, &next->stack->cctx); |
732 | coro_transfer (&prev__stack->cctx, &next->stack->cctx); |
|
|
733 | } |
|
|
734 | |
|
|
735 | free_coro_mortal (); |
|
|
736 | |
|
|
737 | UNLOCK; |
716 | } |
738 | } |
717 | |
|
|
718 | transfer_tail (); |
|
|
719 | } |
739 | } |
720 | |
740 | |
721 | /* use this function pointer to call the above function */ |
741 | /* use this function pointer to call the above function */ |
722 | /* 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 */ |
723 | void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; |
744 | void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; |
|
|
745 | |
|
|
746 | struct 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) |
724 | |
753 | |
725 | static void |
754 | static void |
726 | coro_state_destroy (struct coro *coro) |
755 | coro_state_destroy (struct coro *coro) |
727 | { |
756 | { |
728 | if (coro->refcnt--) |
757 | if (coro->refcnt--) |
… | |
… | |
791 | assert (mg->mg_type == PERL_MAGIC_ext); |
820 | assert (mg->mg_type == PERL_MAGIC_ext); |
792 | return (struct coro *)mg->mg_ptr; |
821 | return (struct coro *)mg->mg_ptr; |
793 | } |
822 | } |
794 | |
823 | |
795 | static void |
824 | static void |
|
|
825 | prepare_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 | |
|
|
832 | static void |
796 | api_transfer (pTHX_ SV *prev, SV *next, int flags) |
833 | api_transfer (SV *prev, SV *next, int flags) |
797 | { |
834 | { |
798 | 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); |
799 | } |
840 | } |
800 | |
841 | |
801 | /** Coro ********************************************************************/ |
842 | /** Coro ********************************************************************/ |
802 | |
843 | |
803 | #define PRIO_MAX 3 |
844 | #define PRIO_MAX 3 |
… | |
… | |
857 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
898 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
858 | UNLOCK; |
899 | UNLOCK; |
859 | } |
900 | } |
860 | |
901 | |
861 | static void |
902 | static void |
862 | api_schedule (void) |
903 | prepare_schedule (aTHX_ struct transfer_args *ta) |
863 | { |
904 | { |
864 | dTHX; |
|
|
865 | |
|
|
866 | SV *prev, *next; |
905 | SV *current, *prev, *next; |
|
|
906 | |
|
|
907 | LOCK; |
|
|
908 | |
867 | SV *current = GvSV (coro_current); |
909 | current = GvSV (coro_current); |
868 | |
910 | |
869 | for (;;) |
911 | for (;;) |
870 | { |
912 | { |
871 | LOCK; |
913 | LOCK; |
872 | |
914 | |
… | |
… | |
894 | |
936 | |
895 | prev = SvRV (current); |
937 | prev = SvRV (current); |
896 | SvRV (current) = next; |
938 | SvRV (current) = next; |
897 | |
939 | |
898 | /* free this only after the transfer */ |
940 | /* free this only after the transfer */ |
|
|
941 | free_coro_mortal (); |
899 | coro_mortal = prev; |
942 | coro_mortal = prev; |
900 | |
943 | |
|
|
944 | ta->prev = SvSTATE (prev); |
|
|
945 | ta->next = SvSTATE (next); |
|
|
946 | ta->flags = TRANSFER_SAVE_ALL; |
|
|
947 | |
901 | UNLOCK; |
948 | UNLOCK; |
902 | |
|
|
903 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), TRANSFER_SAVE_ALL); |
|
|
904 | } |
949 | } |
905 | |
950 | |
906 | static int coro_cede_self; |
951 | static void |
|
|
952 | prepare_cede (aTHX_ struct transfer_args *ta) |
|
|
953 | { |
|
|
954 | LOCK; |
|
|
955 | coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
956 | UNLOCK; |
|
|
957 | |
|
|
958 | prepare_schedule (ta); |
|
|
959 | } |
|
|
960 | |
|
|
961 | static void |
|
|
962 | api_schedule (void) |
|
|
963 | { |
|
|
964 | dTHX; |
|
|
965 | struct transfer_args ta; |
|
|
966 | |
|
|
967 | prepare_schedule (&ta); |
|
|
968 | TRANSFER (ta); |
|
|
969 | } |
907 | |
970 | |
908 | static void |
971 | static void |
909 | api_cede (void) |
972 | api_cede (void) |
910 | { |
973 | { |
911 | dTHX; |
974 | dTHX; |
912 | SV *current = SvREFCNT_inc (SvRV (GvSV (coro_current))); |
975 | struct transfer_args ta; |
913 | |
976 | |
914 | LOCK; |
977 | prepare_cede (&ta); |
915 | |
978 | TRANSFER (ta); |
916 | if (coro_cede_self) |
|
|
917 | { |
|
|
918 | AV *runqueue = coro_ready [PRIO_MAX - PRIO_MIN]; |
|
|
919 | av_unshift (runqueue, 1); |
|
|
920 | av_store (runqueue, 0, current); |
|
|
921 | coro_nready++; |
|
|
922 | coro_cede_self = 0; |
|
|
923 | } |
|
|
924 | else |
|
|
925 | coro_enq (aTHX_ current); |
|
|
926 | |
|
|
927 | UNLOCK; |
|
|
928 | |
|
|
929 | api_schedule (); |
|
|
930 | } |
979 | } |
931 | |
980 | |
932 | MODULE = Coro::State PACKAGE = Coro::State |
981 | MODULE = Coro::State PACKAGE = Coro::State |
933 | |
982 | |
934 | PROTOTYPES: DISABLE |
983 | PROTOTYPES: DISABLE |
… | |
… | |
970 | |
1019 | |
971 | hv = newHV (); |
1020 | hv = newHV (); |
972 | 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; |
973 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1022 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
974 | |
1023 | |
975 | av_push (coro->args, newSVsv (RETVAL)); |
|
|
976 | for (i = 1; i < items; i++) |
1024 | for (i = 1; i < items; i++) |
977 | av_push (coro->args, newSVsv (ST (i))); |
1025 | av_push (coro->args, newSVsv (ST (i))); |
978 | |
1026 | |
979 | coro->stack = main_stack; |
1027 | coro->stack = main_stack; |
980 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
1028 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
… | |
… | |
982 | } |
1030 | } |
983 | OUTPUT: |
1031 | OUTPUT: |
984 | RETVAL |
1032 | RETVAL |
985 | |
1033 | |
986 | void |
1034 | void |
987 | transfer (prev, next, flags) |
1035 | transfer (...) |
988 | SV *prev |
1036 | ALIAS: |
989 | SV *next |
1037 | Coro::schedule = 1 |
990 | int flags |
1038 | Coro::cede = 2 |
|
|
1039 | _set_stacklevel = 3 |
991 | CODE: |
1040 | CODE: |
992 | PUTBACK; |
|
|
993 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
|
|
994 | SPAGAIN; |
|
|
995 | |
|
|
996 | int |
|
|
997 | prio (Coro::State coro, int newprio = 0) |
|
|
998 | ALIAS: |
|
|
999 | nice = 1 |
|
|
1000 | CODE: |
|
|
1001 | { |
1041 | { |
1002 | RETVAL = coro->prio; |
1042 | struct transfer_args ta; |
1003 | |
1043 | |
1004 | if (items > 1) |
1044 | switch (ix) |
1005 | { |
1045 | { |
|
|
1046 | case 0: |
1006 | if (ix) |
1047 | if (items != 3) |
1007 | newprio += coro->prio; |
1048 | croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); |
1008 | |
1049 | |
1009 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1050 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1010 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1051 | break; |
1011 | |
1052 | |
1012 | 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; |
1013 | } |
1066 | } |
1014 | } |
|
|
1015 | |
1067 | |
1016 | void |
1068 | TRANSFER (ta); |
1017 | _clear_idle_sp (Coro::State self) |
1069 | } |
1018 | CODE: |
|
|
1019 | self->stack->idle_sp = 0; |
|
|
1020 | |
1070 | |
1021 | void |
1071 | void |
1022 | _clone_state_from (SV *dst, SV *src) |
1072 | _clone_state_from (SV *dst, SV *src) |
1023 | CODE: |
1073 | CODE: |
1024 | { |
1074 | { |
… | |
… | |
1027 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
1077 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
1028 | |
1078 | |
1029 | ++coro_src->refcnt; |
1079 | ++coro_src->refcnt; |
1030 | 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; |
1031 | } |
1081 | } |
|
|
1082 | |
|
|
1083 | void |
|
|
1084 | _nonlocal_goto (IV nextop) |
|
|
1085 | CODE: |
|
|
1086 | /* uuh, somebody will kill me again for this */ |
|
|
1087 | PL_op->op_next = INT2PTR (OP *, nextop); |
1032 | |
1088 | |
1033 | void |
1089 | void |
1034 | _exit (code) |
1090 | _exit (code) |
1035 | int code |
1091 | int code |
1036 | PROTOTYPE: $ |
1092 | PROTOTYPE: $ |
… | |
… | |
1101 | |
1157 | |
1102 | GCoroAPI = &coroapi; |
1158 | GCoroAPI = &coroapi; |
1103 | sv_setiv (sv, (IV)&coroapi); |
1159 | sv_setiv (sv, (IV)&coroapi); |
1104 | SvREADONLY_on (sv); |
1160 | SvREADONLY_on (sv); |
1105 | } |
1161 | } |
|
|
1162 | } |
|
|
1163 | |
|
|
1164 | int |
|
|
1165 | prio (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 | } |
1106 | } |
1182 | } |
1107 | |
1183 | |
1108 | void |
1184 | void |
1109 | ready (SV *self) |
1185 | ready (SV *self) |
1110 | PROTOTYPE: $ |
1186 | PROTOTYPE: $ |
… | |
… | |
1117 | CODE: |
1193 | CODE: |
1118 | RETVAL = coro_nready; |
1194 | RETVAL = coro_nready; |
1119 | OUTPUT: |
1195 | OUTPUT: |
1120 | RETVAL |
1196 | RETVAL |
1121 | |
1197 | |
1122 | void |
|
|
1123 | schedule (...) |
|
|
1124 | PROTOTYPE: |
|
|
1125 | CODE: |
|
|
1126 | api_schedule (); |
|
|
1127 | |
|
|
1128 | void |
|
|
1129 | _set_cede_self () |
|
|
1130 | CODE: |
|
|
1131 | coro_cede_self = 1; |
|
|
1132 | |
|
|
1133 | void |
|
|
1134 | cede (...) |
|
|
1135 | PROTOTYPE: |
|
|
1136 | CODE: |
|
|
1137 | api_cede (); |
|
|
1138 | |
|
|
1139 | MODULE = Coro::State PACKAGE = Coro::AIO |
1198 | MODULE = Coro::State PACKAGE = Coro::AIO |
1140 | |
1199 | |
1141 | SV * |
1200 | SV * |
1142 | _get_state () |
1201 | _get_state () |
1143 | CODE: |
1202 | CODE: |