… | |
… | |
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 | /* the stack */ |
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 */ |
|
|
100 | void *idle_sp; /* original stacklevel when coroutine was created */ |
|
|
101 | JMPENV *top_env; |
98 | coro_context cctx; |
102 | coro_context cctx; |
99 | } coro_stack; |
103 | } coro_stack; |
100 | |
104 | |
|
|
105 | /* the (fake) coro_stack representing the main program */ |
101 | static coro_stack *main_stack; |
106 | static coro_stack *main_stack; |
102 | |
107 | |
|
|
108 | /* this is a structure representing a perl-level coroutine */ |
103 | struct coro { |
109 | struct coro { |
|
|
110 | /* the c coroutine allocated to this perl coroutine, if any */ |
|
|
111 | coro_stack *stack; |
|
|
112 | |
|
|
113 | /* data associated with this coroutine (initial args) */ |
|
|
114 | AV *args; |
|
|
115 | int refcnt; |
|
|
116 | |
104 | /* optionally saved, might be zero */ |
117 | /* optionally saved, might be zero */ |
105 | AV *defav; |
118 | AV *defav; |
106 | SV *defsv; |
119 | SV *defsv; |
107 | SV *errsv; |
120 | SV *errsv; |
108 | |
121 | |
109 | /* saved global state not related to stacks */ |
122 | /* saved global state not related to stacks */ |
110 | U8 dowarn; |
123 | U8 dowarn; |
111 | I32 in_eval; |
124 | I32 in_eval; |
112 | |
|
|
113 | /* the c stack, if any */ |
|
|
114 | coro_stack *stack; |
|
|
115 | |
125 | |
116 | /* the stacks and related info (callchain etc..) */ |
126 | /* the stacks and related info (callchain etc..) */ |
117 | PERL_SI *curstackinfo; |
127 | PERL_SI *curstackinfo; |
118 | AV *curstack; |
128 | AV *curstack; |
119 | AV *mainstack; |
129 | AV *mainstack; |
… | |
… | |
140 | OP **retstack; |
150 | OP **retstack; |
141 | I32 retstack_ix; |
151 | I32 retstack_ix; |
142 | I32 retstack_max; |
152 | I32 retstack_max; |
143 | PMOP *curpm; |
153 | PMOP *curpm; |
144 | COP *curcop; |
154 | COP *curcop; |
145 | JMPENV *top_env; |
|
|
146 | |
155 | |
147 | /* coro process data */ |
156 | /* coro process data */ |
148 | int prio; |
157 | int prio; |
149 | |
|
|
150 | /* data associated with this coroutine (initial args) */ |
|
|
151 | AV *args; |
|
|
152 | int refcnt; |
|
|
153 | }; |
158 | }; |
154 | |
159 | |
155 | typedef struct coro *Coro__State; |
160 | typedef struct coro *Coro__State; |
156 | typedef struct coro *Coro__State_or_hashref; |
161 | typedef struct coro *Coro__State_or_hashref; |
157 | |
162 | |
… | |
… | |
300 | PL_retstack_ix = c->retstack_ix; |
305 | PL_retstack_ix = c->retstack_ix; |
301 | PL_retstack_max = c->retstack_max; |
306 | PL_retstack_max = c->retstack_max; |
302 | #endif |
307 | #endif |
303 | PL_curpm = c->curpm; |
308 | PL_curpm = c->curpm; |
304 | PL_curcop = c->curcop; |
309 | PL_curcop = c->curcop; |
305 | PL_top_env = c->top_env; |
|
|
306 | |
310 | |
307 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
311 | if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); |
308 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
312 | if (c->defsv) REPLACE_SV (DEFSV , c->defsv); |
309 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
313 | if (c->errsv) REPLACE_SV (ERRSV , c->errsv); |
310 | |
314 | |
… | |
… | |
428 | c->retstack_ix = PL_retstack_ix; |
432 | c->retstack_ix = PL_retstack_ix; |
429 | c->retstack_max = PL_retstack_max; |
433 | c->retstack_max = PL_retstack_max; |
430 | #endif |
434 | #endif |
431 | c->curpm = PL_curpm; |
435 | c->curpm = PL_curpm; |
432 | c->curcop = PL_curcop; |
436 | c->curcop = PL_curcop; |
433 | c->top_env = PL_top_env; |
|
|
434 | } |
437 | } |
435 | |
438 | |
436 | /* |
439 | /* |
437 | * allocate various perl stacks. This is an exact copy |
440 | * allocate various perl stacks. This is an exact copy |
438 | * of perl.c:init_stacks, except that it uses less memory |
441 | * of perl.c:init_stacks, except that it uses less memory |
… | |
… | |
540 | * emulate part of the perl startup here. |
543 | * emulate part of the perl startup here. |
541 | */ |
544 | */ |
542 | dTHX; |
545 | dTHX; |
543 | dSP; |
546 | dSP; |
544 | UNOP myop; |
547 | UNOP myop; |
545 | SV *sub_init = (SV *)get_cv (SUB_INIT, FALSE); |
548 | SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE); |
546 | |
549 | |
547 | coro_init_stacks (aTHX); |
550 | coro_init_stacks (aTHX); |
548 | /*PL_curcop = 0;*/ |
551 | /*PL_curcop = 0;*/ |
549 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
552 | /*PL_in_eval = PL_in_eval;*/ /* inherit */ |
550 | SvREFCNT_dec (GvAV (PL_defgv)); |
553 | SvREFCNT_dec (GvAV (PL_defgv)); |
… | |
… | |
566 | |
569 | |
567 | ENTER; /* necessary e.g. for dounwind */ |
570 | ENTER; /* necessary e.g. for dounwind */ |
568 | } |
571 | } |
569 | |
572 | |
570 | static void |
573 | static void |
571 | transfer_tail () |
574 | free_coro_mortal () |
572 | { |
575 | { |
573 | if (coro_mortal) |
576 | if (coro_mortal) |
574 | { |
577 | { |
575 | SvREFCNT_dec (coro_mortal); |
578 | SvREFCNT_dec (coro_mortal); |
576 | coro_mortal = 0; |
579 | coro_mortal = 0; |
577 | } |
580 | } |
578 | |
|
|
579 | UNLOCK; |
|
|
580 | } |
581 | } |
581 | |
582 | |
582 | static void |
583 | static void |
583 | coro_run (void *arg) |
584 | coro_run (void *arg) |
584 | { |
585 | { |
585 | /* |
586 | /* |
586 | * this is a _very_ stripped down perl interpreter ;) |
587 | * this is a _very_ stripped down perl interpreter ;) |
587 | */ |
588 | */ |
588 | dTHX; |
589 | dTHX; |
589 | |
590 | |
590 | transfer_tail (); |
591 | UNLOCK; |
591 | |
592 | |
592 | PL_top_env = &PL_start_env; |
593 | PL_top_env = &PL_start_env; |
593 | PL_restartop = PL_op->op_next; |
594 | |
|
|
595 | sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg)); |
|
|
596 | sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op)); |
|
|
597 | |
|
|
598 | /* continue at cctx_init, without entersub */ |
|
|
599 | PL_restartop = CvSTART (get_cv ("Coro::State::cctx_init", FALSE)); |
|
|
600 | |
594 | /* somebody will hit me for both perl_run and PL_restart_top */ |
601 | /* somebody will hit me for both perl_run and PL_restart_op */ |
595 | perl_run (aTHX_ PERL_GET_CONTEXT); |
602 | perl_run (aTHX_ PERL_GET_CONTEXT); |
596 | |
603 | |
597 | abort (); |
604 | abort (); |
598 | } |
605 | } |
599 | |
606 | |
… | |
… | |
609 | stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; /* mmap should do allocate-on-write for us */ |
616 | 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); |
617 | stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
611 | |
618 | |
612 | if (stack->sptr == (void *)-1) |
619 | if (stack->sptr == (void *)-1) |
613 | { |
620 | { |
614 | fprintf (stderr, "FATAL: unable to mmap stack for coroutine\n"); |
621 | perror ("FATAL: unable to mmap stack for coroutine"); |
615 | _exit (EXIT_FAILURE); |
622 | _exit (EXIT_FAILURE); |
616 | } |
623 | } |
617 | |
624 | |
618 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
625 | mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); |
619 | |
626 | |
… | |
… | |
622 | stack->ssize = STACKSIZE * (long)sizeof (long); |
629 | stack->ssize = STACKSIZE * (long)sizeof (long); |
623 | New (0, stack->sptr, STACKSIZE, long); |
630 | New (0, stack->sptr, STACKSIZE, long); |
624 | |
631 | |
625 | if (!stack->sptr) |
632 | if (!stack->sptr) |
626 | { |
633 | { |
627 | fprintf (stderr, "FATAL: unable to malloc stack for coroutine\n"); |
634 | perror (stderr, "FATAL: unable to malloc stack for coroutine"); |
628 | _exit (EXIT_FAILURE); |
635 | _exit (EXIT_FAILURE); |
629 | } |
636 | } |
630 | |
637 | |
631 | #endif |
638 | #endif |
632 | |
639 | |
633 | coro_create (&stack->cctx, coro_run, 0, stack->sptr, stack->ssize); |
640 | coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); |
634 | |
641 | |
635 | return stack; |
642 | return stack; |
636 | } |
643 | } |
637 | |
644 | |
638 | static void |
645 | static void |
… | |
… | |
650 | Safefree (stack); |
657 | Safefree (stack); |
651 | } |
658 | } |
652 | |
659 | |
653 | static coro_stack *stack_first; |
660 | static coro_stack *stack_first; |
654 | |
661 | |
655 | static void |
662 | static coro_stack * |
656 | stack_get (struct coro *coro) |
663 | stack_get () |
657 | { |
664 | { |
|
|
665 | coro_stack *stack; |
|
|
666 | |
658 | if (stack_first) |
667 | if (stack_first) |
659 | { |
668 | { |
660 | coro->stack = stack_first; |
669 | stack = stack_first; |
661 | stack_first = stack_first->next; |
670 | stack_first = stack->next; |
662 | } |
671 | } |
663 | else |
672 | else |
|
|
673 | { |
664 | coro->stack = stack_new (); |
674 | stack = stack_new (); |
|
|
675 | PL_op = PL_op->op_next; |
|
|
676 | } |
|
|
677 | |
|
|
678 | return stack; |
665 | } |
679 | } |
666 | |
680 | |
667 | static void |
681 | static void |
668 | stack_put (coro_stack *stack) |
682 | stack_put (coro_stack *stack) |
669 | { |
683 | { |
… | |
… | |
676 | transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) |
690 | transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) |
677 | { |
691 | { |
678 | dSTACKLEVEL; |
692 | dSTACKLEVEL; |
679 | |
693 | |
680 | /* sometimes transfer is only called to set idle_sp */ |
694 | /* sometimes transfer is only called to set idle_sp */ |
681 | if (!prev->stack->idle_sp) |
695 | if (flags == TRANSFER_SET_STACKLEVEL) |
682 | prev->stack->idle_sp = stacklevel; |
696 | ((coro_stack *)prev)->idle_sp = STACKLEVEL; |
683 | |
|
|
684 | LOCK; |
|
|
685 | |
|
|
686 | if (prev != next) |
697 | else if (prev != next) |
687 | { |
698 | { |
688 | coro_stack *prev_stack = prev->stack; |
699 | coro_stack *prev__stack; |
689 | |
700 | |
690 | /* possibly "free" the stack */ |
701 | LOCK; |
691 | if (prev_stack->idle_sp == stacklevel) |
702 | |
|
|
703 | if (next->mainstack) |
692 | { |
704 | { |
693 | stack_put (prev_stack); |
705 | /* coroutine already started */ |
694 | prev->stack = 0; |
706 | SAVE (prev, flags); |
|
|
707 | LOAD (next); |
695 | } |
708 | } |
696 | |
709 | else |
697 | if (!next->mainstack) |
|
|
698 | { |
710 | { |
|
|
711 | /* need to start coroutine */ |
699 | /* first get rid of the old state */ |
712 | /* first get rid of the old state */ |
700 | SAVE (prev, -1); |
713 | SAVE (prev, -1); |
701 | /* setup coroutine call */ |
714 | /* setup coroutine call */ |
702 | setup_coro (next); |
715 | setup_coro (next); |
|
|
716 | /* need a stack */ |
703 | next->stack = 0; |
717 | next->stack = 0; |
704 | } |
718 | } |
705 | else |
719 | |
|
|
720 | prev__stack = prev->stack; |
|
|
721 | |
|
|
722 | /* possibly "free" the stack */ |
|
|
723 | if (prev__stack->idle_sp == STACKLEVEL) |
706 | { |
724 | { |
707 | SAVE (prev, flags); |
725 | stack_put (prev__stack); |
708 | LOAD (next); |
726 | prev->stack = 0; |
709 | } |
727 | } |
710 | |
728 | |
711 | if (!next->stack) |
729 | if (!next->stack) |
712 | stack_get (next); |
730 | next->stack = stack_get (); |
713 | |
731 | |
714 | if (prev_stack != next->stack) |
732 | if (prev__stack != next->stack) |
|
|
733 | { |
|
|
734 | prev__stack->top_env = PL_top_env; |
|
|
735 | PL_top_env = next->stack->top_env; |
715 | coro_transfer (&prev_stack->cctx, &next->stack->cctx); |
736 | coro_transfer (&prev__stack->cctx, &next->stack->cctx); |
|
|
737 | } |
|
|
738 | |
|
|
739 | free_coro_mortal (); |
|
|
740 | |
|
|
741 | UNLOCK; |
716 | } |
742 | } |
717 | |
|
|
718 | transfer_tail (); |
|
|
719 | } |
743 | } |
720 | |
744 | |
721 | /* use this function pointer to call the above function */ |
745 | /* use this function pointer to call the above function */ |
722 | /* this is done to increase chances of the compiler not inlining the call */ |
746 | /* this is done to increase chances of the compiler not inlining the call */ |
|
|
747 | /* 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; |
748 | void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; |
|
|
749 | |
|
|
750 | struct transfer_args |
|
|
751 | { |
|
|
752 | struct coro *prev, *next; |
|
|
753 | int flags; |
|
|
754 | }; |
|
|
755 | |
|
|
756 | #define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) |
724 | |
757 | |
725 | static void |
758 | static void |
726 | coro_state_destroy (struct coro *coro) |
759 | coro_state_destroy (struct coro *coro) |
727 | { |
760 | { |
728 | if (coro->refcnt--) |
761 | if (coro->refcnt--) |
… | |
… | |
791 | assert (mg->mg_type == PERL_MAGIC_ext); |
824 | assert (mg->mg_type == PERL_MAGIC_ext); |
792 | return (struct coro *)mg->mg_ptr; |
825 | return (struct coro *)mg->mg_ptr; |
793 | } |
826 | } |
794 | |
827 | |
795 | static void |
828 | static void |
|
|
829 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev, SV *next, int flags) |
|
|
830 | { |
|
|
831 | ta->prev = SvSTATE (prev); |
|
|
832 | ta->next = SvSTATE (next); |
|
|
833 | ta->flags = flags; |
|
|
834 | } |
|
|
835 | |
|
|
836 | static void |
796 | api_transfer (pTHX_ SV *prev, SV *next, int flags) |
837 | api_transfer (SV *prev, SV *next, int flags) |
797 | { |
838 | { |
798 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
839 | dTHX; |
|
|
840 | struct transfer_args ta; |
|
|
841 | |
|
|
842 | prepare_transfer (aTHX_ &ta, prev, next, flags); |
|
|
843 | TRANSFER (ta); |
799 | } |
844 | } |
800 | |
845 | |
801 | /** Coro ********************************************************************/ |
846 | /** Coro ********************************************************************/ |
802 | |
847 | |
803 | #define PRIO_MAX 3 |
848 | #define PRIO_MAX 3 |
… | |
… | |
857 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
902 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
858 | UNLOCK; |
903 | UNLOCK; |
859 | } |
904 | } |
860 | |
905 | |
861 | static void |
906 | static void |
862 | api_schedule (void) |
907 | prepare_schedule (aTHX_ struct transfer_args *ta) |
863 | { |
908 | { |
864 | dTHX; |
|
|
865 | |
|
|
866 | SV *prev, *next; |
909 | SV *current, *prev, *next; |
|
|
910 | |
|
|
911 | LOCK; |
|
|
912 | |
867 | SV *current = GvSV (coro_current); |
913 | current = GvSV (coro_current); |
868 | |
914 | |
869 | for (;;) |
915 | for (;;) |
870 | { |
916 | { |
871 | LOCK; |
917 | LOCK; |
872 | |
918 | |
… | |
… | |
894 | |
940 | |
895 | prev = SvRV (current); |
941 | prev = SvRV (current); |
896 | SvRV (current) = next; |
942 | SvRV (current) = next; |
897 | |
943 | |
898 | /* free this only after the transfer */ |
944 | /* free this only after the transfer */ |
|
|
945 | free_coro_mortal (); |
899 | coro_mortal = prev; |
946 | coro_mortal = prev; |
900 | |
947 | |
|
|
948 | ta->prev = SvSTATE (prev); |
|
|
949 | ta->next = SvSTATE (next); |
|
|
950 | ta->flags = TRANSFER_SAVE_ALL; |
|
|
951 | |
901 | UNLOCK; |
952 | UNLOCK; |
902 | |
|
|
903 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), TRANSFER_SAVE_ALL); |
|
|
904 | } |
953 | } |
905 | |
954 | |
906 | static int coro_cede_self; |
955 | static void |
|
|
956 | prepare_cede (aTHX_ struct transfer_args *ta) |
|
|
957 | { |
|
|
958 | LOCK; |
|
|
959 | coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
960 | UNLOCK; |
|
|
961 | |
|
|
962 | prepare_schedule (ta); |
|
|
963 | } |
|
|
964 | |
|
|
965 | static void |
|
|
966 | api_schedule (void) |
|
|
967 | { |
|
|
968 | dTHX; |
|
|
969 | struct transfer_args ta; |
|
|
970 | |
|
|
971 | prepare_schedule (&ta); |
|
|
972 | TRANSFER (ta); |
|
|
973 | } |
907 | |
974 | |
908 | static void |
975 | static void |
909 | api_cede (void) |
976 | api_cede (void) |
910 | { |
977 | { |
911 | dTHX; |
978 | dTHX; |
912 | SV *current = SvREFCNT_inc (SvRV (GvSV (coro_current))); |
979 | struct transfer_args ta; |
913 | |
980 | |
914 | LOCK; |
981 | prepare_cede (&ta); |
915 | |
982 | 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 | } |
983 | } |
931 | |
984 | |
932 | MODULE = Coro::State PACKAGE = Coro::State |
985 | MODULE = Coro::State PACKAGE = Coro::State |
933 | |
986 | |
934 | PROTOTYPES: DISABLE |
987 | PROTOTYPES: DISABLE |
… | |
… | |
970 | |
1023 | |
971 | hv = newHV (); |
1024 | hv = newHV (); |
972 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1025 | 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)); |
1026 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
974 | |
1027 | |
975 | av_push (coro->args, newSVsv (RETVAL)); |
|
|
976 | for (i = 1; i < items; i++) |
1028 | for (i = 1; i < items; i++) |
977 | av_push (coro->args, newSVsv (ST (i))); |
1029 | av_push (coro->args, newSVsv (ST (i))); |
978 | |
1030 | |
979 | coro->stack = main_stack; |
1031 | coro->stack = main_stack; |
980 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
1032 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
… | |
… | |
982 | } |
1034 | } |
983 | OUTPUT: |
1035 | OUTPUT: |
984 | RETVAL |
1036 | RETVAL |
985 | |
1037 | |
986 | void |
1038 | void |
987 | transfer (prev, next, flags) |
1039 | transfer (...) |
988 | SV *prev |
1040 | ALIAS: |
989 | SV *next |
1041 | Coro::schedule = 1 |
990 | int flags |
1042 | Coro::cede = 2 |
|
|
1043 | _set_stacklevel = 3 |
991 | CODE: |
1044 | 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 | { |
1045 | { |
1002 | RETVAL = coro->prio; |
1046 | struct transfer_args ta; |
1003 | |
1047 | |
1004 | if (items > 1) |
1048 | switch (ix) |
1005 | { |
1049 | { |
|
|
1050 | case 0: |
1006 | if (ix) |
1051 | if (items != 3) |
1007 | newprio += coro->prio; |
1052 | croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); |
1008 | |
1053 | |
1009 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1054 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1010 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1055 | break; |
1011 | |
1056 | |
1012 | coro->prio = newprio; |
1057 | case 1: |
|
|
1058 | prepare_schedule (&ta); |
|
|
1059 | break; |
|
|
1060 | |
|
|
1061 | case 2: |
|
|
1062 | prepare_cede (&ta); |
|
|
1063 | break; |
|
|
1064 | |
|
|
1065 | case 3: |
|
|
1066 | ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); |
|
|
1067 | ta.next = 0; |
|
|
1068 | ta.flags = TRANSFER_SET_STACKLEVEL; |
|
|
1069 | break; |
1013 | } |
1070 | } |
1014 | } |
|
|
1015 | |
1071 | |
1016 | void |
1072 | TRANSFER (ta); |
1017 | _clear_idle_sp (Coro::State self) |
1073 | } |
1018 | CODE: |
|
|
1019 | self->stack->idle_sp = 0; |
|
|
1020 | |
1074 | |
1021 | void |
1075 | void |
1022 | _clone_state_from (SV *dst, SV *src) |
1076 | _clone_state_from (SV *dst, SV *src) |
1023 | CODE: |
1077 | CODE: |
1024 | { |
1078 | { |
… | |
… | |
1027 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
1081 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
1028 | |
1082 | |
1029 | ++coro_src->refcnt; |
1083 | ++coro_src->refcnt; |
1030 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
1084 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
1031 | } |
1085 | } |
|
|
1086 | |
|
|
1087 | void |
|
|
1088 | _nonlocal_goto (IV nextop) |
|
|
1089 | CODE: |
|
|
1090 | /* uuh, somebody will kill me again for this */ |
|
|
1091 | PL_op->op_next = INT2PTR (OP *, nextop); |
1032 | |
1092 | |
1033 | void |
1093 | void |
1034 | _exit (code) |
1094 | _exit (code) |
1035 | int code |
1095 | int code |
1036 | PROTOTYPE: $ |
1096 | PROTOTYPE: $ |
… | |
… | |
1101 | |
1161 | |
1102 | GCoroAPI = &coroapi; |
1162 | GCoroAPI = &coroapi; |
1103 | sv_setiv (sv, (IV)&coroapi); |
1163 | sv_setiv (sv, (IV)&coroapi); |
1104 | SvREADONLY_on (sv); |
1164 | SvREADONLY_on (sv); |
1105 | } |
1165 | } |
|
|
1166 | } |
|
|
1167 | |
|
|
1168 | int |
|
|
1169 | prio (Coro::State coro, int newprio = 0) |
|
|
1170 | ALIAS: |
|
|
1171 | nice = 1 |
|
|
1172 | CODE: |
|
|
1173 | { |
|
|
1174 | RETVAL = coro->prio; |
|
|
1175 | |
|
|
1176 | if (items > 1) |
|
|
1177 | { |
|
|
1178 | if (ix) |
|
|
1179 | newprio += coro->prio; |
|
|
1180 | |
|
|
1181 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
|
|
1182 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
|
|
1183 | |
|
|
1184 | coro->prio = newprio; |
|
|
1185 | } |
1106 | } |
1186 | } |
1107 | |
1187 | |
1108 | void |
1188 | void |
1109 | ready (SV *self) |
1189 | ready (SV *self) |
1110 | PROTOTYPE: $ |
1190 | PROTOTYPE: $ |
… | |
… | |
1117 | CODE: |
1197 | CODE: |
1118 | RETVAL = coro_nready; |
1198 | RETVAL = coro_nready; |
1119 | OUTPUT: |
1199 | OUTPUT: |
1120 | RETVAL |
1200 | RETVAL |
1121 | |
1201 | |
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 |
1202 | MODULE = Coro::State PACKAGE = Coro::AIO |
1140 | |
1203 | |
1141 | SV * |
1204 | SV * |
1142 | _get_state () |
1205 | _get_state () |
1143 | CODE: |
1206 | CODE: |