… | |
… | |
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; |
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 |
… | |
… | |
683 | transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) |
686 | transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) |
684 | { |
687 | { |
685 | dSTACKLEVEL; |
688 | dSTACKLEVEL; |
686 | |
689 | |
687 | /* sometimes transfer is only called to set idle_sp */ |
690 | /* sometimes transfer is only called to set idle_sp */ |
688 | if (!prev->stack->idle_sp) |
691 | if (flags == TRANSFER_SET_STACKLEVEL) |
689 | prev->stack->idle_sp = stacklevel; |
692 | ((coro_stack *)prev)->idle_sp = STACKLEVEL; |
690 | |
|
|
691 | LOCK; |
|
|
692 | |
|
|
693 | if (prev != next) |
693 | else if (prev != next) |
694 | { |
694 | { |
695 | coro_stack *prev_stack = prev->stack; |
695 | coro_stack *prev__stack; |
696 | |
696 | |
697 | /* possibly "free" the stack */ |
697 | LOCK; |
698 | if (0 && prev_stack->idle_sp == stacklevel) |
|
|
699 | { |
|
|
700 | stack_put (prev_stack); |
|
|
701 | prev->stack = 0; |
|
|
702 | } |
|
|
703 | |
698 | |
704 | if (next->mainstack) |
699 | if (next->mainstack) |
705 | { |
700 | { |
706 | /* coroutine already started */ |
701 | /* coroutine already started */ |
707 | SAVE (prev, flags); |
702 | SAVE (prev, flags); |
… | |
… | |
712 | /* need to start coroutine */ |
707 | /* need to start coroutine */ |
713 | /* first get rid of the old state */ |
708 | /* first get rid of the old state */ |
714 | SAVE (prev, -1); |
709 | SAVE (prev, -1); |
715 | /* setup coroutine call */ |
710 | /* setup coroutine call */ |
716 | setup_coro (next); |
711 | setup_coro (next); |
717 | /* need to change stack from main_stack to real one */ |
712 | /* need a stack */ |
718 | next->stack = 0; |
713 | next->stack = 0; |
719 | } |
714 | } |
720 | |
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 | |
721 | if (!next->stack) |
725 | if (!next->stack) |
722 | next->stack = stack_get (); |
726 | next->stack = stack_get (); |
723 | |
727 | |
724 | 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; |
725 | 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; |
726 | } |
738 | } |
727 | |
|
|
728 | transfer_tail (); |
|
|
729 | } |
739 | } |
730 | |
740 | |
731 | /* use this function pointer to call the above function */ |
741 | /* use this function pointer to call the above function */ |
732 | /* 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 */ |
733 | 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) |
734 | |
753 | |
735 | static void |
754 | static void |
736 | coro_state_destroy (struct coro *coro) |
755 | coro_state_destroy (struct coro *coro) |
737 | { |
756 | { |
738 | if (coro->refcnt--) |
757 | if (coro->refcnt--) |
… | |
… | |
801 | assert (mg->mg_type == PERL_MAGIC_ext); |
820 | assert (mg->mg_type == PERL_MAGIC_ext); |
802 | return (struct coro *)mg->mg_ptr; |
821 | return (struct coro *)mg->mg_ptr; |
803 | } |
822 | } |
804 | |
823 | |
805 | 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 |
806 | api_transfer (pTHX_ SV *prev, SV *next, int flags) |
833 | api_transfer (SV *prev, SV *next, int flags) |
807 | { |
834 | { |
808 | 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); |
809 | } |
840 | } |
810 | |
841 | |
811 | /** Coro ********************************************************************/ |
842 | /** Coro ********************************************************************/ |
812 | |
843 | |
813 | #define PRIO_MAX 3 |
844 | #define PRIO_MAX 3 |
… | |
… | |
867 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
898 | coro_enq (aTHX_ SvREFCNT_inc (coro)); |
868 | UNLOCK; |
899 | UNLOCK; |
869 | } |
900 | } |
870 | |
901 | |
871 | static void |
902 | static void |
872 | api_schedule (void) |
903 | prepare_schedule (aTHX_ struct transfer_args *ta) |
873 | { |
904 | { |
874 | dTHX; |
|
|
875 | |
|
|
876 | SV *prev, *next; |
905 | SV *current, *prev, *next; |
|
|
906 | |
|
|
907 | LOCK; |
|
|
908 | |
877 | SV *current = GvSV (coro_current); |
909 | current = GvSV (coro_current); |
878 | |
910 | |
879 | for (;;) |
911 | for (;;) |
880 | { |
912 | { |
881 | LOCK; |
913 | LOCK; |
882 | |
914 | |
… | |
… | |
904 | |
936 | |
905 | prev = SvRV (current); |
937 | prev = SvRV (current); |
906 | SvRV (current) = next; |
938 | SvRV (current) = next; |
907 | |
939 | |
908 | /* free this only after the transfer */ |
940 | /* free this only after the transfer */ |
|
|
941 | free_coro_mortal (); |
909 | coro_mortal = prev; |
942 | coro_mortal = prev; |
910 | |
943 | |
|
|
944 | ta->prev = SvSTATE (prev); |
|
|
945 | ta->next = SvSTATE (next); |
|
|
946 | ta->flags = TRANSFER_SAVE_ALL; |
|
|
947 | |
911 | UNLOCK; |
948 | UNLOCK; |
912 | |
|
|
913 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), TRANSFER_SAVE_ALL); |
|
|
914 | } |
949 | } |
915 | |
950 | |
916 | 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 | } |
917 | |
970 | |
918 | static void |
971 | static void |
919 | api_cede (void) |
972 | api_cede (void) |
920 | { |
973 | { |
921 | dTHX; |
974 | dTHX; |
922 | SV *current = SvREFCNT_inc (SvRV (GvSV (coro_current))); |
975 | struct transfer_args ta; |
923 | |
976 | |
924 | LOCK; |
977 | prepare_cede (&ta); |
925 | |
978 | TRANSFER (ta); |
926 | if (coro_cede_self) |
|
|
927 | { |
|
|
928 | AV *runqueue = coro_ready [PRIO_MAX - PRIO_MIN]; |
|
|
929 | av_unshift (runqueue, 1); |
|
|
930 | av_store (runqueue, 0, current); |
|
|
931 | coro_nready++; |
|
|
932 | coro_cede_self = 0; |
|
|
933 | } |
|
|
934 | else |
|
|
935 | coro_enq (aTHX_ current); |
|
|
936 | |
|
|
937 | UNLOCK; |
|
|
938 | |
|
|
939 | api_schedule (); |
|
|
940 | } |
979 | } |
941 | |
980 | |
942 | MODULE = Coro::State PACKAGE = Coro::State |
981 | MODULE = Coro::State PACKAGE = Coro::State |
943 | |
982 | |
944 | PROTOTYPES: DISABLE |
983 | PROTOTYPES: DISABLE |
… | |
… | |
980 | |
1019 | |
981 | hv = newHV (); |
1020 | hv = newHV (); |
982 | 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; |
983 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1022 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
984 | |
1023 | |
985 | av_push (coro->args, newSVsv (RETVAL)); |
|
|
986 | for (i = 1; i < items; i++) |
1024 | for (i = 1; i < items; i++) |
987 | av_push (coro->args, newSVsv (ST (i))); |
1025 | av_push (coro->args, newSVsv (ST (i))); |
988 | |
1026 | |
989 | coro->stack = main_stack; |
1027 | coro->stack = main_stack; |
990 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
1028 | /*coro->mainstack = 0; *//*actual work is done inside transfer */ |
… | |
… | |
992 | } |
1030 | } |
993 | OUTPUT: |
1031 | OUTPUT: |
994 | RETVAL |
1032 | RETVAL |
995 | |
1033 | |
996 | void |
1034 | void |
997 | transfer (prev, next, flags) |
1035 | transfer (...) |
998 | SV *prev |
1036 | ALIAS: |
999 | SV *next |
1037 | Coro::schedule = 1 |
1000 | int flags |
1038 | Coro::cede = 2 |
|
|
1039 | _set_stacklevel = 3 |
1001 | CODE: |
1040 | CODE: |
1002 | PUTBACK; |
|
|
1003 | coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); |
|
|
1004 | SPAGAIN; |
|
|
1005 | |
|
|
1006 | int |
|
|
1007 | prio (Coro::State coro, int newprio = 0) |
|
|
1008 | ALIAS: |
|
|
1009 | nice = 1 |
|
|
1010 | CODE: |
|
|
1011 | { |
1041 | { |
1012 | RETVAL = coro->prio; |
1042 | struct transfer_args ta; |
1013 | |
1043 | |
1014 | if (items > 1) |
1044 | switch (ix) |
1015 | { |
1045 | { |
|
|
1046 | case 0: |
1016 | if (ix) |
1047 | if (items != 3) |
1017 | newprio += coro->prio; |
1048 | croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); |
1018 | |
1049 | |
1019 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
1050 | prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); |
1020 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
1051 | break; |
1021 | |
1052 | |
1022 | 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; |
1023 | } |
1066 | } |
1024 | } |
|
|
1025 | |
1067 | |
1026 | void |
1068 | TRANSFER (ta); |
1027 | _clear_idle_sp (Coro::State self) |
1069 | } |
1028 | CODE: |
|
|
1029 | self->stack->idle_sp = 0; |
|
|
1030 | |
1070 | |
1031 | void |
1071 | void |
1032 | _clone_state_from (SV *dst, SV *src) |
1072 | _clone_state_from (SV *dst, SV *src) |
1033 | CODE: |
1073 | CODE: |
1034 | { |
1074 | { |
… | |
… | |
1037 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
1077 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
1038 | |
1078 | |
1039 | ++coro_src->refcnt; |
1079 | ++coro_src->refcnt; |
1040 | 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; |
1041 | } |
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); |
1042 | |
1088 | |
1043 | void |
1089 | void |
1044 | _exit (code) |
1090 | _exit (code) |
1045 | int code |
1091 | int code |
1046 | PROTOTYPE: $ |
1092 | PROTOTYPE: $ |
… | |
… | |
1111 | |
1157 | |
1112 | GCoroAPI = &coroapi; |
1158 | GCoroAPI = &coroapi; |
1113 | sv_setiv (sv, (IV)&coroapi); |
1159 | sv_setiv (sv, (IV)&coroapi); |
1114 | SvREADONLY_on (sv); |
1160 | SvREADONLY_on (sv); |
1115 | } |
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 | } |
1116 | } |
1182 | } |
1117 | |
1183 | |
1118 | void |
1184 | void |
1119 | ready (SV *self) |
1185 | ready (SV *self) |
1120 | PROTOTYPE: $ |
1186 | PROTOTYPE: $ |
… | |
… | |
1127 | CODE: |
1193 | CODE: |
1128 | RETVAL = coro_nready; |
1194 | RETVAL = coro_nready; |
1129 | OUTPUT: |
1195 | OUTPUT: |
1130 | RETVAL |
1196 | RETVAL |
1131 | |
1197 | |
1132 | void |
|
|
1133 | schedule (...) |
|
|
1134 | PROTOTYPE: |
|
|
1135 | CODE: |
|
|
1136 | api_schedule (); |
|
|
1137 | |
|
|
1138 | void |
|
|
1139 | _set_cede_self () |
|
|
1140 | CODE: |
|
|
1141 | coro_cede_self = 1; |
|
|
1142 | |
|
|
1143 | void |
|
|
1144 | cede (...) |
|
|
1145 | PROTOTYPE: |
|
|
1146 | CODE: |
|
|
1147 | api_cede (); |
|
|
1148 | |
|
|
1149 | MODULE = Coro::State PACKAGE = Coro::AIO |
1198 | MODULE = Coro::State PACKAGE = Coro::AIO |
1150 | |
1199 | |
1151 | SV * |
1200 | SV * |
1152 | _get_state () |
1201 | _get_state () |
1153 | CODE: |
1202 | CODE: |