… | |
… | |
140 | static AV *av_async_pool; |
140 | static AV *av_async_pool; |
141 | |
141 | |
142 | static struct coro_cctx *cctx_first; |
142 | static struct coro_cctx *cctx_first; |
143 | static int cctx_count, cctx_idle; |
143 | static int cctx_count, cctx_idle; |
144 | |
144 | |
|
|
145 | enum { |
|
|
146 | CC_MAPPED = 0x01, |
|
|
147 | CC_NOREUSE = 0x02, /* throw this away after tracing */ |
|
|
148 | CC_TRACE = 0x04, |
|
|
149 | CC_TRACE_SUB = 0x08, /* trace sub calls */ |
|
|
150 | CC_TRACE_LINE = 0x10, /* trace each statement */ |
|
|
151 | CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE, |
|
|
152 | }; |
|
|
153 | |
145 | /* this is a structure representing a c-level coroutine */ |
154 | /* this is a structure representing a c-level coroutine */ |
146 | typedef struct coro_cctx { |
155 | typedef struct coro_cctx { |
147 | struct coro_cctx *next; |
156 | struct coro_cctx *next; |
148 | |
157 | |
149 | /* the stack */ |
158 | /* the stack */ |
… | |
… | |
157 | coro_context cctx; |
166 | coro_context cctx; |
158 | |
167 | |
159 | #if CORO_USE_VALGRIND |
168 | #if CORO_USE_VALGRIND |
160 | int valgrind_id; |
169 | int valgrind_id; |
161 | #endif |
170 | #endif |
162 | char inuse, mapped; |
171 | unsigned char flags; |
163 | } coro_cctx; |
172 | } coro_cctx; |
164 | |
173 | |
165 | enum { |
174 | enum { |
166 | CF_RUNNING = 0x0001, /* coroutine is running */ |
175 | CF_RUNNING = 0x0001, /* coroutine is running */ |
167 | CF_READY = 0x0002, /* coroutine is ready */ |
176 | CF_READY = 0x0002, /* coroutine is ready */ |
… | |
… | |
289 | SvMAGIC (cv) \ |
298 | SvMAGIC (cv) \ |
290 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
299 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
291 | ? SvMAGIC (cv) \ |
300 | ? SvMAGIC (cv) \ |
292 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
301 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
293 | : 0 |
302 | : 0 |
|
|
303 | |
|
|
304 | static struct coro * |
|
|
305 | SvSTATE_ (pTHX_ SV *coro) |
|
|
306 | { |
|
|
307 | HV *stash; |
|
|
308 | MAGIC *mg; |
|
|
309 | |
|
|
310 | if (SvROK (coro)) |
|
|
311 | coro = SvRV (coro); |
|
|
312 | |
|
|
313 | stash = SvSTASH (coro); |
|
|
314 | if (stash != coro_stash && stash != coro_state_stash) |
|
|
315 | { |
|
|
316 | /* very slow, but rare, check */ |
|
|
317 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
318 | croak ("Coro::State object required"); |
|
|
319 | } |
|
|
320 | |
|
|
321 | mg = CORO_MAGIC (coro); |
|
|
322 | return (struct coro *)mg->mg_ptr; |
|
|
323 | } |
|
|
324 | |
|
|
325 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
294 | |
326 | |
295 | /* the next two functions merely cache the padlists */ |
327 | /* the next two functions merely cache the padlists */ |
296 | static void |
328 | static void |
297 | get_padlist (pTHX_ CV *cv) |
329 | get_padlist (pTHX_ CV *cv) |
298 | { |
330 | { |
… | |
… | |
584 | /* |
616 | /* |
585 | * emulate part of the perl startup here. |
617 | * emulate part of the perl startup here. |
586 | */ |
618 | */ |
587 | coro_init_stacks (aTHX); |
619 | coro_init_stacks (aTHX); |
588 | |
620 | |
|
|
621 | PL_runops = RUNOPS_DEFAULT; |
589 | PL_curcop = &PL_compiling; |
622 | PL_curcop = &PL_compiling; |
590 | PL_in_eval = EVAL_NULL; |
623 | PL_in_eval = EVAL_NULL; |
591 | PL_comppad = 0; |
624 | PL_comppad = 0; |
592 | PL_curpm = 0; |
625 | PL_curpm = 0; |
593 | PL_localizing = 0; |
626 | PL_localizing = 0; |
… | |
… | |
624 | SvREFCNT_dec (coro_mortal); |
657 | SvREFCNT_dec (coro_mortal); |
625 | coro_mortal = 0; |
658 | coro_mortal = 0; |
626 | } |
659 | } |
627 | } |
660 | } |
628 | |
661 | |
|
|
662 | static int |
|
|
663 | runops_trace (pTHX) |
|
|
664 | { |
|
|
665 | COP *oldcop = 0; |
|
|
666 | int oldcxix = -2; |
|
|
667 | struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */ |
|
|
668 | coro_cctx *cctx = coro->cctx; |
|
|
669 | |
|
|
670 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
|
|
671 | { |
|
|
672 | PERL_ASYNC_CHECK (); |
|
|
673 | |
|
|
674 | if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) |
|
|
675 | { |
|
|
676 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
|
|
677 | SV **bot, **top; |
|
|
678 | AV *av = newAV (); /* return values */ |
|
|
679 | SV **cb; |
|
|
680 | runops_proc_t old_runops = PL_runops; |
|
|
681 | dSP; |
|
|
682 | ENTER; |
|
|
683 | SAVETMPS; |
|
|
684 | EXTEND (SP, 3); |
|
|
685 | PL_runops = RUNOPS_DEFAULT; |
|
|
686 | |
|
|
687 | GV *gv = CvGV (cx->blk_sub.cv); |
|
|
688 | SV *fullname = sv_2mortal (newSV (0)); |
|
|
689 | if (isGV (gv)) |
|
|
690 | gv_efullname3 (fullname, gv, 0); |
|
|
691 | |
|
|
692 | bot = PL_stack_base + cx->blk_oldsp + 1; |
|
|
693 | top = cx->blk_gimme == G_ARRAY ? SP + 1 |
|
|
694 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
|
|
695 | : bot; |
|
|
696 | |
|
|
697 | while (bot < top) |
|
|
698 | av_push (av, SvREFCNT_inc (*bot++)); |
|
|
699 | |
|
|
700 | PUSHMARK (SP); |
|
|
701 | PUSHs (&PL_sv_no); |
|
|
702 | PUSHs (fullname); |
|
|
703 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
704 | PUTBACK; |
|
|
705 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
|
|
706 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
707 | SPAGAIN; |
|
|
708 | |
|
|
709 | FREETMPS; |
|
|
710 | LEAVE; |
|
|
711 | PL_runops = old_runops; |
|
|
712 | } |
|
|
713 | |
|
|
714 | if (oldcop != PL_curcop) |
|
|
715 | { |
|
|
716 | oldcop = PL_curcop; |
|
|
717 | |
|
|
718 | if (PL_curcop != &PL_compiling) |
|
|
719 | { |
|
|
720 | SV **cb; |
|
|
721 | runops_proc_t old_runops = PL_runops; |
|
|
722 | dSP; |
|
|
723 | ENTER; |
|
|
724 | SAVETMPS; |
|
|
725 | EXTEND (SP, 3); |
|
|
726 | PL_runops = RUNOPS_DEFAULT; |
|
|
727 | |
|
|
728 | if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) |
|
|
729 | { |
|
|
730 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
|
|
731 | |
|
|
732 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
|
|
733 | { |
|
|
734 | GV *gv = CvGV (cx->blk_sub.cv); |
|
|
735 | SV *fullname = sv_2mortal (newSV (0)); |
|
|
736 | if (isGV (gv)) |
|
|
737 | gv_efullname3 (fullname, gv, 0); |
|
|
738 | |
|
|
739 | PUSHMARK (SP); |
|
|
740 | PUSHs (&PL_sv_yes); |
|
|
741 | PUSHs (fullname); |
|
|
742 | PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
|
|
743 | PUTBACK; |
|
|
744 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
|
|
745 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
746 | SPAGAIN; |
|
|
747 | } |
|
|
748 | |
|
|
749 | oldcxix = cxstack_ix; |
|
|
750 | } |
|
|
751 | |
|
|
752 | if (cctx->flags & CC_TRACE_LINE) |
|
|
753 | { |
|
|
754 | PUSHMARK (SP); |
|
|
755 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
|
|
756 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
|
|
757 | PUTBACK; |
|
|
758 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0); |
|
|
759 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
760 | SPAGAIN; |
|
|
761 | } |
|
|
762 | |
|
|
763 | FREETMPS; |
|
|
764 | LEAVE; |
|
|
765 | PL_runops = old_runops; |
|
|
766 | } |
|
|
767 | } |
|
|
768 | } |
|
|
769 | |
|
|
770 | TAINT_NOT; |
|
|
771 | return 0; |
|
|
772 | } |
|
|
773 | |
629 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
774 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
630 | /* _cctx_init should be careful, as it could be called at almost any time */ |
775 | /* _cctx_init should be careful, as it could be called at almost any time */ |
631 | /* during execution of a perl program */ |
776 | /* during execution of a perl program */ |
632 | static void NOINLINE |
777 | static void NOINLINE |
633 | prepare_cctx (pTHX_ coro_cctx *cctx) |
778 | prepare_cctx (pTHX_ coro_cctx *cctx) |
634 | { |
779 | { |
635 | dSP; |
780 | dSP; |
636 | LOGOP myop; |
781 | LOGOP myop; |
|
|
782 | |
|
|
783 | PL_top_env = &PL_start_env; |
|
|
784 | |
|
|
785 | if (cctx->flags & CC_TRACE) |
|
|
786 | PL_runops = runops_trace; |
637 | |
787 | |
638 | Zero (&myop, 1, LOGOP); |
788 | Zero (&myop, 1, LOGOP); |
639 | myop.op_next = PL_op; |
789 | myop.op_next = PL_op; |
640 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
790 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
641 | |
791 | |
… | |
… | |
658 | dTHX; |
808 | dTHX; |
659 | |
809 | |
660 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
810 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
661 | UNLOCK; |
811 | UNLOCK; |
662 | |
812 | |
663 | PL_top_env = &PL_start_env; |
813 | /* we now skip the entersub that lead to transfer() */ |
|
|
814 | PL_op = PL_op->op_next; |
664 | |
815 | |
665 | /* inject a fake subroutine call to cctx_init */ |
816 | /* inject a fake subroutine call to cctx_init */ |
666 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
817 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
667 | |
818 | |
668 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
819 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
… | |
… | |
702 | # if CORO_STACKGUARD |
853 | # if CORO_STACKGUARD |
703 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
854 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
704 | # endif |
855 | # endif |
705 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
856 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
706 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
857 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
707 | cctx->mapped = 1; |
858 | cctx->flags |= CC_MAPPED; |
708 | } |
859 | } |
709 | else |
860 | else |
710 | #endif |
861 | #endif |
711 | { |
862 | { |
712 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
863 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
… | |
… | |
739 | #if CORO_USE_VALGRIND |
890 | #if CORO_USE_VALGRIND |
740 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
891 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
741 | #endif |
892 | #endif |
742 | |
893 | |
743 | #if HAVE_MMAP |
894 | #if HAVE_MMAP |
744 | if (cctx->mapped) |
895 | if (cctx->flags & CC_MAPPED) |
745 | munmap (cctx->sptr, cctx->ssize); |
896 | munmap (cctx->sptr, cctx->ssize); |
746 | else |
897 | else |
747 | #endif |
898 | #endif |
748 | Safefree (cctx->sptr); |
899 | Safefree (cctx->sptr); |
749 | |
900 | |
… | |
… | |
757 | { |
908 | { |
758 | coro_cctx *cctx = cctx_first; |
909 | coro_cctx *cctx = cctx_first; |
759 | cctx_first = cctx->next; |
910 | cctx_first = cctx->next; |
760 | --cctx_idle; |
911 | --cctx_idle; |
761 | |
912 | |
762 | if (cctx->ssize >= coro_stacksize) |
913 | if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE)) |
763 | return cctx; |
914 | return cctx; |
764 | |
915 | |
765 | cctx_destroy (cctx); |
916 | cctx_destroy (cctx); |
766 | } |
917 | } |
767 | |
918 | |
768 | PL_op = PL_op->op_next; |
|
|
769 | return cctx_new (); |
919 | return cctx_new (); |
770 | } |
920 | } |
771 | |
921 | |
772 | static void |
922 | static void |
773 | cctx_put (coro_cctx *cctx) |
923 | cctx_put (coro_cctx *cctx) |
… | |
… | |
777 | { |
927 | { |
778 | coro_cctx *first = cctx_first; |
928 | coro_cctx *first = cctx_first; |
779 | cctx_first = first->next; |
929 | cctx_first = first->next; |
780 | --cctx_idle; |
930 | --cctx_idle; |
781 | |
931 | |
782 | assert (!first->inuse); |
|
|
783 | cctx_destroy (first); |
932 | cctx_destroy (first); |
784 | } |
933 | } |
785 | |
934 | |
786 | ++cctx_idle; |
935 | ++cctx_idle; |
787 | cctx->next = cctx_first; |
936 | cctx->next = cctx_first; |
788 | cctx_first = cctx; |
937 | cctx_first = cctx; |
789 | } |
938 | } |
790 | |
939 | |
791 | /** coroutine switching *****************************************************/ |
940 | /** coroutine switching *****************************************************/ |
792 | |
941 | |
793 | /* never call directly, always through the coro_state_transfer global variable */ |
942 | static void NOINLINE |
|
|
943 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
|
|
944 | { |
|
|
945 | if (prev != next) |
|
|
946 | { |
|
|
947 | if (!(prev->flags & (CF_RUNNING | CF_NEW))) |
|
|
948 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states"); |
|
|
949 | |
|
|
950 | if (next->flags & CF_RUNNING) |
|
|
951 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
952 | |
|
|
953 | if (next->flags & CF_DESTROYED) |
|
|
954 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
|
|
955 | |
|
|
956 | if (PL_lex_state != LEX_NOTPARSING) |
|
|
957 | croak ("Coro::State::transfer called while parsing, but this is not supported"); |
|
|
958 | } |
|
|
959 | } |
|
|
960 | |
|
|
961 | /* always use the TRANSFER macro */ |
794 | static void NOINLINE |
962 | static void NOINLINE |
795 | transfer (pTHX_ struct coro *prev, struct coro *next) |
963 | transfer (pTHX_ struct coro *prev, struct coro *next) |
796 | { |
964 | { |
797 | dSTACKLEVEL; |
965 | dSTACKLEVEL; |
798 | |
966 | |
… | |
… | |
808 | |
976 | |
809 | if (prev->flags & CF_NEW) |
977 | if (prev->flags & CF_NEW) |
810 | { |
978 | { |
811 | /* create a new empty context */ |
979 | /* create a new empty context */ |
812 | Newz (0, prev->cctx, 1, coro_cctx); |
980 | Newz (0, prev->cctx, 1, coro_cctx); |
813 | prev->cctx->inuse = 1; |
|
|
814 | prev->flags &= ~CF_NEW; |
981 | prev->flags &= ~CF_NEW; |
815 | prev->flags |= CF_RUNNING; |
982 | prev->flags |= CF_RUNNING; |
816 | } |
983 | } |
817 | |
|
|
818 | /*TODO: must not croak here */ |
|
|
819 | if (!prev->flags & CF_RUNNING) |
|
|
820 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
|
|
821 | |
|
|
822 | if (next->flags & CF_RUNNING) |
|
|
823 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
824 | |
|
|
825 | if (next->flags & CF_DESTROYED) |
|
|
826 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
|
|
827 | |
984 | |
828 | prev->flags &= ~CF_RUNNING; |
985 | prev->flags &= ~CF_RUNNING; |
829 | next->flags |= CF_RUNNING; |
986 | next->flags |= CF_RUNNING; |
830 | |
987 | |
831 | LOCK; |
988 | LOCK; |
… | |
… | |
836 | next->flags &= ~CF_NEW; |
993 | next->flags &= ~CF_NEW; |
837 | /* first get rid of the old state */ |
994 | /* first get rid of the old state */ |
838 | save_perl (aTHX_ prev); |
995 | save_perl (aTHX_ prev); |
839 | /* setup coroutine call */ |
996 | /* setup coroutine call */ |
840 | setup_coro (aTHX_ next); |
997 | setup_coro (aTHX_ next); |
841 | /* need a new stack */ |
|
|
842 | assert (!next->cctx); |
|
|
843 | } |
998 | } |
844 | else |
999 | else |
845 | { |
1000 | { |
846 | /* coroutine already started */ |
1001 | /* coroutine already started */ |
847 | save_perl (aTHX_ prev); |
1002 | save_perl (aTHX_ prev); |
… | |
… | |
849 | } |
1004 | } |
850 | |
1005 | |
851 | prev__cctx = prev->cctx; |
1006 | prev__cctx = prev->cctx; |
852 | |
1007 | |
853 | /* possibly "free" the cctx */ |
1008 | /* possibly "free" the cctx */ |
854 | if (prev__cctx->idle_sp == STACKLEVEL) |
1009 | if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE)) |
855 | { |
1010 | { |
856 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1011 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
857 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
1012 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
858 | |
1013 | |
859 | prev->cctx = 0; |
1014 | prev->cctx = 0; |
860 | |
1015 | |
861 | cctx_put (prev__cctx); |
1016 | cctx_put (prev__cctx); |
862 | prev__cctx->inuse = 0; |
|
|
863 | } |
1017 | } |
864 | |
1018 | |
865 | if (!next->cctx) |
1019 | if (!next->cctx) |
866 | { |
|
|
867 | next->cctx = cctx_get (aTHX); |
1020 | next->cctx = cctx_get (aTHX); |
868 | assert (!next->cctx->inuse); |
|
|
869 | next->cctx->inuse = 1; |
|
|
870 | } |
|
|
871 | |
1021 | |
872 | if (prev__cctx != next->cctx) |
1022 | if (prev__cctx != next->cctx) |
873 | { |
1023 | { |
874 | prev__cctx->top_env = PL_top_env; |
1024 | prev__cctx->top_env = PL_top_env; |
875 | PL_top_env = next->cctx->top_env; |
1025 | PL_top_env = next->cctx->top_env; |
… | |
… | |
885 | { |
1035 | { |
886 | struct coro *prev, *next; |
1036 | struct coro *prev, *next; |
887 | }; |
1037 | }; |
888 | |
1038 | |
889 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
1039 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
|
|
1040 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
890 | |
1041 | |
891 | /** high level stuff ********************************************************/ |
1042 | /** high level stuff ********************************************************/ |
892 | |
1043 | |
893 | static int |
1044 | static int |
894 | coro_state_destroy (pTHX_ struct coro *coro) |
1045 | coro_state_destroy (pTHX_ struct coro *coro) |
… | |
… | |
977 | #else |
1128 | #else |
978 | # define MGf_DUP 0 |
1129 | # define MGf_DUP 0 |
979 | #endif |
1130 | #endif |
980 | }; |
1131 | }; |
981 | |
1132 | |
982 | static struct coro * |
|
|
983 | SvSTATE_ (pTHX_ SV *coro) |
|
|
984 | { |
|
|
985 | HV *stash; |
|
|
986 | MAGIC *mg; |
|
|
987 | |
|
|
988 | if (SvROK (coro)) |
|
|
989 | coro = SvRV (coro); |
|
|
990 | |
|
|
991 | stash = SvSTASH (coro); |
|
|
992 | if (stash != coro_stash && stash != coro_state_stash) |
|
|
993 | { |
|
|
994 | /* very slow, but rare, check */ |
|
|
995 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
996 | croak ("Coro::State object required"); |
|
|
997 | } |
|
|
998 | |
|
|
999 | mg = CORO_MAGIC (coro); |
|
|
1000 | return (struct coro *)mg->mg_ptr; |
|
|
1001 | } |
|
|
1002 | |
|
|
1003 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
1004 | |
|
|
1005 | static void |
1133 | static void |
1006 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
1134 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
1007 | { |
1135 | { |
1008 | ta->prev = SvSTATE (prev_sv); |
1136 | ta->prev = SvSTATE (prev_sv); |
1009 | ta->next = SvSTATE (next_sv); |
1137 | ta->next = SvSTATE (next_sv); |
|
|
1138 | TRANSFER_CHECK (*ta); |
1010 | } |
1139 | } |
1011 | |
1140 | |
1012 | static void |
1141 | static void |
1013 | api_transfer (SV *prev_sv, SV *next_sv) |
1142 | api_transfer (SV *prev_sv, SV *next_sv) |
1014 | { |
1143 | { |
… | |
… | |
1131 | break; |
1260 | break; |
1132 | } |
1261 | } |
1133 | |
1262 | |
1134 | /* free this only after the transfer */ |
1263 | /* free this only after the transfer */ |
1135 | prev_sv = SvRV (coro_current); |
1264 | prev_sv = SvRV (coro_current); |
1136 | SvRV_set (coro_current, next_sv); |
|
|
1137 | ta->prev = SvSTATE (prev_sv); |
1265 | ta->prev = SvSTATE (prev_sv); |
1138 | |
1266 | TRANSFER_CHECK (*ta); |
1139 | assert (ta->next->flags & CF_READY); |
1267 | assert (ta->next->flags & CF_READY); |
1140 | ta->next->flags &= ~CF_READY; |
1268 | ta->next->flags &= ~CF_READY; |
|
|
1269 | SvRV_set (coro_current, next_sv); |
1141 | |
1270 | |
1142 | LOCK; |
1271 | LOCK; |
1143 | free_coro_mortal (aTHX); |
1272 | free_coro_mortal (aTHX); |
1144 | coro_mortal = prev_sv; |
1273 | coro_mortal = prev_sv; |
1145 | UNLOCK; |
1274 | UNLOCK; |
… | |
… | |
1218 | MUTEX_INIT (&coro_mutex); |
1347 | MUTEX_INIT (&coro_mutex); |
1219 | #endif |
1348 | #endif |
1220 | BOOT_PAGESIZE; |
1349 | BOOT_PAGESIZE; |
1221 | |
1350 | |
1222 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1351 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
|
|
1352 | |
|
|
1353 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
|
|
1354 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
|
|
1355 | newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); |
|
|
1356 | newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); |
1223 | |
1357 | |
1224 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1358 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1225 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1359 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1226 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1360 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1227 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
1361 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
… | |
… | |
1379 | if (coro->hv) |
1513 | if (coro->hv) |
1380 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
1514 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
1381 | } |
1515 | } |
1382 | |
1516 | |
1383 | void |
1517 | void |
1384 | _eval (Coro::State coro, SV *coderef) |
1518 | call (Coro::State coro, SV *coderef) |
|
|
1519 | ALIAS: |
|
|
1520 | eval = 1 |
1385 | CODE: |
1521 | CODE: |
1386 | { |
1522 | { |
1387 | if (coro->mainstack) |
1523 | if (coro->mainstack) |
1388 | { |
1524 | { |
1389 | struct coro temp; |
1525 | struct coro temp; |
… | |
… | |
1400 | dSP; |
1536 | dSP; |
1401 | ENTER; |
1537 | ENTER; |
1402 | SAVETMPS; |
1538 | SAVETMPS; |
1403 | PUSHMARK (SP); |
1539 | PUSHMARK (SP); |
1404 | PUTBACK; |
1540 | PUTBACK; |
|
|
1541 | if (ix) |
|
|
1542 | eval_sv (coderef, 0); |
|
|
1543 | else |
1405 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1544 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1406 | SPAGAIN; |
1545 | SPAGAIN; |
1407 | FREETMPS; |
1546 | FREETMPS; |
1408 | LEAVE; |
1547 | LEAVE; |
1409 | PUTBACK; |
1548 | PUTBACK; |
1410 | } |
1549 | } |
… | |
… | |
1428 | CODE: |
1567 | CODE: |
1429 | RETVAL = boolSV (coro->flags & ix); |
1568 | RETVAL = boolSV (coro->flags & ix); |
1430 | OUTPUT: |
1569 | OUTPUT: |
1431 | RETVAL |
1570 | RETVAL |
1432 | |
1571 | |
|
|
1572 | void |
|
|
1573 | trace (Coro::State coro, int flags = CC_TRACE | CC_TRACE_SUB) |
|
|
1574 | CODE: |
|
|
1575 | if (flags & CC_TRACE) |
|
|
1576 | { |
|
|
1577 | if (!coro->cctx) |
|
|
1578 | coro->cctx = cctx_new (); |
|
|
1579 | else if (!(coro->cctx->flags & CC_TRACE)) |
|
|
1580 | croak ("cannot enable tracing on coroutine with custom stack"); |
|
|
1581 | |
|
|
1582 | coro->cctx->flags |= flags & (CC_TRACE | CC_TRACE_ALL); |
|
|
1583 | } |
|
|
1584 | else |
|
|
1585 | if (coro->cctx && coro->cctx->flags & CC_TRACE) |
|
|
1586 | { |
|
|
1587 | coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL); |
|
|
1588 | coro->cctx->flags |= CC_NOREUSE; |
|
|
1589 | |
|
|
1590 | if (coro->flags & CF_RUNNING) |
|
|
1591 | PL_runops = RUNOPS_DEFAULT; |
|
|
1592 | else |
|
|
1593 | coro->runops = RUNOPS_DEFAULT; |
|
|
1594 | } |
|
|
1595 | |
1433 | SV * |
1596 | SV * |
1434 | has_stack (Coro::State coro) |
1597 | has_stack (Coro::State coro) |
1435 | PROTOTYPE: $ |
1598 | PROTOTYPE: $ |
1436 | CODE: |
1599 | CODE: |
1437 | RETVAL = boolSV (!!coro->cctx); |
1600 | RETVAL = boolSV (!!coro->cctx); |
|
|
1601 | OUTPUT: |
|
|
1602 | RETVAL |
|
|
1603 | |
|
|
1604 | int |
|
|
1605 | is_traced (Coro::State coro) |
|
|
1606 | PROTOTYPE: $ |
|
|
1607 | CODE: |
|
|
1608 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
1438 | OUTPUT: |
1609 | OUTPUT: |
1439 | RETVAL |
1610 | RETVAL |
1440 | |
1611 | |
1441 | IV |
1612 | IV |
1442 | rss (Coro::State coro) |
1613 | rss (Coro::State coro) |
… | |
… | |
1578 | if (coro_rss (coro) > SvIV (sv_pool_rss) |
1749 | if (coro_rss (coro) > SvIV (sv_pool_rss) |
1579 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1750 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1580 | croak ("\3terminate\2\n"); |
1751 | croak ("\3terminate\2\n"); |
1581 | |
1752 | |
1582 | av_clear (GvAV (PL_defgv)); |
1753 | av_clear (GvAV (PL_defgv)); |
1583 | hv_store (SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1754 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1584 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
1755 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
1585 | coro->save = CORO_SAVE_DEF; |
1756 | coro->save = CORO_SAVE_DEF; |
1586 | coro->prio = 0; |
1757 | coro->prio = 0; |
1587 | av_push (av_async_pool, newSVsv (coro_current)); |
1758 | av_push (av_async_pool, newSVsv (coro_current)); |
1588 | } |
1759 | } |