… | |
… | |
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 */ |
… | |
… | |
190 | |
199 | |
191 | #define VAR(name,type) type name; |
200 | #define VAR(name,type) type name; |
192 | # include "state.h" |
201 | # include "state.h" |
193 | #undef VAR |
202 | #undef VAR |
194 | |
203 | |
|
|
204 | /* statistics */ |
|
|
205 | int usecount; /* number of switches to this coro */ |
|
|
206 | |
195 | /* coro process data */ |
207 | /* coro process data */ |
196 | int prio; |
208 | int prio; |
197 | |
209 | |
198 | /* linked list */ |
210 | /* linked list */ |
199 | struct coro *next, *prev; |
211 | struct coro *next, *prev; |
… | |
… | |
289 | SvMAGIC (cv) \ |
301 | SvMAGIC (cv) \ |
290 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
302 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
291 | ? SvMAGIC (cv) \ |
303 | ? SvMAGIC (cv) \ |
292 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
304 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
293 | : 0 |
305 | : 0 |
|
|
306 | |
|
|
307 | static struct coro * |
|
|
308 | SvSTATE_ (pTHX_ SV *coro) |
|
|
309 | { |
|
|
310 | HV *stash; |
|
|
311 | MAGIC *mg; |
|
|
312 | |
|
|
313 | if (SvROK (coro)) |
|
|
314 | coro = SvRV (coro); |
|
|
315 | |
|
|
316 | if (SvTYPE (coro) != SVt_PVHV) |
|
|
317 | croak ("Coro::State object required"); |
|
|
318 | |
|
|
319 | stash = SvSTASH (coro); |
|
|
320 | if (stash != coro_stash && stash != coro_state_stash) |
|
|
321 | { |
|
|
322 | /* very slow, but rare, check */ |
|
|
323 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
324 | croak ("Coro::State object required"); |
|
|
325 | } |
|
|
326 | |
|
|
327 | mg = CORO_MAGIC (coro); |
|
|
328 | return (struct coro *)mg->mg_ptr; |
|
|
329 | } |
|
|
330 | |
|
|
331 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
294 | |
332 | |
295 | /* the next two functions merely cache the padlists */ |
333 | /* the next two functions merely cache the padlists */ |
296 | static void |
334 | static void |
297 | get_padlist (pTHX_ CV *cv) |
335 | get_padlist (pTHX_ CV *cv) |
298 | { |
336 | { |
… | |
… | |
544 | Safefree (PL_retstack); |
582 | Safefree (PL_retstack); |
545 | #endif |
583 | #endif |
546 | } |
584 | } |
547 | |
585 | |
548 | static size_t |
586 | static size_t |
549 | coro_rss (struct coro *coro) |
587 | coro_rss (pTHX_ struct coro *coro) |
550 | { |
588 | { |
551 | size_t rss = sizeof (coro); |
589 | size_t rss = sizeof (coro); |
552 | |
590 | |
553 | if (coro->mainstack) |
591 | if (coro->mainstack) |
554 | { |
592 | { |
… | |
… | |
584 | /* |
622 | /* |
585 | * emulate part of the perl startup here. |
623 | * emulate part of the perl startup here. |
586 | */ |
624 | */ |
587 | coro_init_stacks (aTHX); |
625 | coro_init_stacks (aTHX); |
588 | |
626 | |
|
|
627 | PL_runops = RUNOPS_DEFAULT; |
589 | PL_curcop = &PL_compiling; |
628 | PL_curcop = &PL_compiling; |
590 | PL_in_eval = EVAL_NULL; |
629 | PL_in_eval = EVAL_NULL; |
591 | PL_comppad = 0; |
630 | PL_comppad = 0; |
592 | PL_curpm = 0; |
631 | PL_curpm = 0; |
593 | PL_localizing = 0; |
632 | PL_localizing = 0; |
… | |
… | |
624 | SvREFCNT_dec (coro_mortal); |
663 | SvREFCNT_dec (coro_mortal); |
625 | coro_mortal = 0; |
664 | coro_mortal = 0; |
626 | } |
665 | } |
627 | } |
666 | } |
628 | |
667 | |
|
|
668 | static int |
|
|
669 | runops_trace (pTHX) |
|
|
670 | { |
|
|
671 | COP *oldcop = 0; |
|
|
672 | int oldcxix = -2; |
|
|
673 | struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */ |
|
|
674 | coro_cctx *cctx = coro->cctx; |
|
|
675 | |
|
|
676 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
|
|
677 | { |
|
|
678 | PERL_ASYNC_CHECK (); |
|
|
679 | |
|
|
680 | if (cctx->flags & CC_TRACE_ALL) |
|
|
681 | { |
|
|
682 | if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) |
|
|
683 | { |
|
|
684 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
|
|
685 | SV **bot, **top; |
|
|
686 | AV *av = newAV (); /* return values */ |
|
|
687 | SV **cb; |
|
|
688 | dSP; |
|
|
689 | |
|
|
690 | GV *gv = CvGV (cx->blk_sub.cv); |
|
|
691 | SV *fullname = sv_2mortal (newSV (0)); |
|
|
692 | if (isGV (gv)) |
|
|
693 | gv_efullname3 (fullname, gv, 0); |
|
|
694 | |
|
|
695 | bot = PL_stack_base + cx->blk_oldsp + 1; |
|
|
696 | top = cx->blk_gimme == G_ARRAY ? SP + 1 |
|
|
697 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
|
|
698 | : bot; |
|
|
699 | |
|
|
700 | while (bot < top) |
|
|
701 | av_push (av, SvREFCNT_inc (*bot++)); |
|
|
702 | |
|
|
703 | PL_runops = RUNOPS_DEFAULT; |
|
|
704 | ENTER; |
|
|
705 | SAVETMPS; |
|
|
706 | EXTEND (SP, 3); |
|
|
707 | PUSHMARK (SP); |
|
|
708 | PUSHs (&PL_sv_no); |
|
|
709 | PUSHs (fullname); |
|
|
710 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
711 | PUTBACK; |
|
|
712 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
|
|
713 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
714 | SPAGAIN; |
|
|
715 | FREETMPS; |
|
|
716 | LEAVE; |
|
|
717 | PL_runops = runops_trace; |
|
|
718 | } |
|
|
719 | |
|
|
720 | if (oldcop != PL_curcop) |
|
|
721 | { |
|
|
722 | oldcop = PL_curcop; |
|
|
723 | |
|
|
724 | if (PL_curcop != &PL_compiling) |
|
|
725 | { |
|
|
726 | SV **cb; |
|
|
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 | runops_proc_t old_runops = PL_runops; |
|
|
735 | dSP; |
|
|
736 | GV *gv = CvGV (cx->blk_sub.cv); |
|
|
737 | SV *fullname = sv_2mortal (newSV (0)); |
|
|
738 | |
|
|
739 | if (isGV (gv)) |
|
|
740 | gv_efullname3 (fullname, gv, 0); |
|
|
741 | |
|
|
742 | PL_runops = RUNOPS_DEFAULT; |
|
|
743 | ENTER; |
|
|
744 | SAVETMPS; |
|
|
745 | EXTEND (SP, 3); |
|
|
746 | PUSHMARK (SP); |
|
|
747 | PUSHs (&PL_sv_yes); |
|
|
748 | PUSHs (fullname); |
|
|
749 | PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
|
|
750 | PUTBACK; |
|
|
751 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
|
|
752 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
753 | SPAGAIN; |
|
|
754 | FREETMPS; |
|
|
755 | LEAVE; |
|
|
756 | PL_runops = runops_trace; |
|
|
757 | } |
|
|
758 | |
|
|
759 | oldcxix = cxstack_ix; |
|
|
760 | } |
|
|
761 | |
|
|
762 | if (cctx->flags & CC_TRACE_LINE) |
|
|
763 | { |
|
|
764 | dSP; |
|
|
765 | |
|
|
766 | PL_runops = RUNOPS_DEFAULT; |
|
|
767 | ENTER; |
|
|
768 | SAVETMPS; |
|
|
769 | EXTEND (SP, 3); |
|
|
770 | PL_runops = RUNOPS_DEFAULT; |
|
|
771 | PUSHMARK (SP); |
|
|
772 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
|
|
773 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
|
|
774 | PUTBACK; |
|
|
775 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0); |
|
|
776 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
777 | SPAGAIN; |
|
|
778 | FREETMPS; |
|
|
779 | LEAVE; |
|
|
780 | PL_runops = runops_trace; |
|
|
781 | } |
|
|
782 | } |
|
|
783 | } |
|
|
784 | } |
|
|
785 | } |
|
|
786 | |
|
|
787 | TAINT_NOT; |
|
|
788 | return 0; |
|
|
789 | } |
|
|
790 | |
629 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
791 | /* 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 */ |
792 | /* _cctx_init should be careful, as it could be called at almost any time */ |
631 | /* during execution of a perl program */ |
793 | /* during execution of a perl program */ |
632 | static void NOINLINE |
794 | static void NOINLINE |
633 | prepare_cctx (pTHX_ coro_cctx *cctx) |
795 | prepare_cctx (pTHX_ coro_cctx *cctx) |
634 | { |
796 | { |
635 | dSP; |
797 | dSP; |
636 | LOGOP myop; |
798 | LOGOP myop; |
|
|
799 | |
|
|
800 | PL_top_env = &PL_start_env; |
|
|
801 | |
|
|
802 | if (cctx->flags & CC_TRACE) |
|
|
803 | PL_runops = runops_trace; |
637 | |
804 | |
638 | Zero (&myop, 1, LOGOP); |
805 | Zero (&myop, 1, LOGOP); |
639 | myop.op_next = PL_op; |
806 | myop.op_next = PL_op; |
640 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
807 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
641 | |
808 | |
… | |
… | |
658 | dTHX; |
825 | dTHX; |
659 | |
826 | |
660 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
827 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
661 | UNLOCK; |
828 | UNLOCK; |
662 | |
829 | |
663 | PL_top_env = &PL_start_env; |
830 | /* we now skip the entersub that lead to transfer() */ |
|
|
831 | PL_op = PL_op->op_next; |
664 | |
832 | |
665 | /* inject a fake subroutine call to cctx_init */ |
833 | /* inject a fake subroutine call to cctx_init */ |
666 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
834 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
667 | |
835 | |
668 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
836 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
… | |
… | |
702 | # if CORO_STACKGUARD |
870 | # if CORO_STACKGUARD |
703 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
871 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
704 | # endif |
872 | # endif |
705 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
873 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
706 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
874 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
707 | cctx->mapped = 1; |
875 | cctx->flags |= CC_MAPPED; |
708 | } |
876 | } |
709 | else |
877 | else |
710 | #endif |
878 | #endif |
711 | { |
879 | { |
712 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
880 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
… | |
… | |
739 | #if CORO_USE_VALGRIND |
907 | #if CORO_USE_VALGRIND |
740 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
908 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
741 | #endif |
909 | #endif |
742 | |
910 | |
743 | #if HAVE_MMAP |
911 | #if HAVE_MMAP |
744 | if (cctx->mapped) |
912 | if (cctx->flags & CC_MAPPED) |
745 | munmap (cctx->sptr, cctx->ssize); |
913 | munmap (cctx->sptr, cctx->ssize); |
746 | else |
914 | else |
747 | #endif |
915 | #endif |
748 | Safefree (cctx->sptr); |
916 | Safefree (cctx->sptr); |
749 | |
917 | |
… | |
… | |
757 | { |
925 | { |
758 | coro_cctx *cctx = cctx_first; |
926 | coro_cctx *cctx = cctx_first; |
759 | cctx_first = cctx->next; |
927 | cctx_first = cctx->next; |
760 | --cctx_idle; |
928 | --cctx_idle; |
761 | |
929 | |
762 | if (cctx->ssize >= coro_stacksize) |
930 | if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE)) |
763 | return cctx; |
931 | return cctx; |
764 | |
932 | |
765 | cctx_destroy (cctx); |
933 | cctx_destroy (cctx); |
766 | } |
934 | } |
767 | |
935 | |
768 | PL_op = PL_op->op_next; |
|
|
769 | return cctx_new (); |
936 | return cctx_new (); |
770 | } |
937 | } |
771 | |
938 | |
772 | static void |
939 | static void |
773 | cctx_put (coro_cctx *cctx) |
940 | cctx_put (coro_cctx *cctx) |
… | |
… | |
777 | { |
944 | { |
778 | coro_cctx *first = cctx_first; |
945 | coro_cctx *first = cctx_first; |
779 | cctx_first = first->next; |
946 | cctx_first = first->next; |
780 | --cctx_idle; |
947 | --cctx_idle; |
781 | |
948 | |
782 | assert (!first->inuse); |
|
|
783 | cctx_destroy (first); |
949 | cctx_destroy (first); |
784 | } |
950 | } |
785 | |
951 | |
786 | ++cctx_idle; |
952 | ++cctx_idle; |
787 | cctx->next = cctx_first; |
953 | cctx->next = cctx_first; |
788 | cctx_first = cctx; |
954 | cctx_first = cctx; |
789 | } |
955 | } |
790 | |
956 | |
791 | /** coroutine switching *****************************************************/ |
957 | /** coroutine switching *****************************************************/ |
792 | |
958 | |
793 | /* never call directly, always through the coro_state_transfer global variable */ |
959 | static void NOINLINE |
|
|
960 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
|
|
961 | { |
|
|
962 | if (prev != next) |
|
|
963 | { |
|
|
964 | if (!(prev->flags & (CF_RUNNING | CF_NEW))) |
|
|
965 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states"); |
|
|
966 | |
|
|
967 | if (next->flags & CF_RUNNING) |
|
|
968 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
969 | |
|
|
970 | if (next->flags & CF_DESTROYED) |
|
|
971 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
|
|
972 | |
|
|
973 | if (PL_lex_state != LEX_NOTPARSING) |
|
|
974 | croak ("Coro::State::transfer called while parsing, but this is not supported"); |
|
|
975 | } |
|
|
976 | } |
|
|
977 | |
|
|
978 | /* always use the TRANSFER macro */ |
794 | static void NOINLINE |
979 | static void NOINLINE |
795 | transfer (pTHX_ struct coro *prev, struct coro *next) |
980 | transfer (pTHX_ struct coro *prev, struct coro *next) |
796 | { |
981 | { |
797 | dSTACKLEVEL; |
982 | dSTACKLEVEL; |
798 | |
983 | |
… | |
… | |
808 | |
993 | |
809 | if (prev->flags & CF_NEW) |
994 | if (prev->flags & CF_NEW) |
810 | { |
995 | { |
811 | /* create a new empty context */ |
996 | /* create a new empty context */ |
812 | Newz (0, prev->cctx, 1, coro_cctx); |
997 | Newz (0, prev->cctx, 1, coro_cctx); |
813 | prev->cctx->inuse = 1; |
|
|
814 | prev->flags &= ~CF_NEW; |
998 | prev->flags &= ~CF_NEW; |
815 | prev->flags |= CF_RUNNING; |
999 | prev->flags |= CF_RUNNING; |
816 | } |
1000 | } |
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 | |
1001 | |
828 | prev->flags &= ~CF_RUNNING; |
1002 | prev->flags &= ~CF_RUNNING; |
829 | next->flags |= CF_RUNNING; |
1003 | next->flags |= CF_RUNNING; |
830 | |
1004 | |
831 | LOCK; |
1005 | LOCK; |
… | |
… | |
836 | next->flags &= ~CF_NEW; |
1010 | next->flags &= ~CF_NEW; |
837 | /* first get rid of the old state */ |
1011 | /* first get rid of the old state */ |
838 | save_perl (aTHX_ prev); |
1012 | save_perl (aTHX_ prev); |
839 | /* setup coroutine call */ |
1013 | /* setup coroutine call */ |
840 | setup_coro (aTHX_ next); |
1014 | setup_coro (aTHX_ next); |
841 | /* need a new stack */ |
|
|
842 | assert (!next->cctx); |
|
|
843 | } |
1015 | } |
844 | else |
1016 | else |
845 | { |
1017 | { |
846 | /* coroutine already started */ |
1018 | /* coroutine already started */ |
847 | save_perl (aTHX_ prev); |
1019 | save_perl (aTHX_ prev); |
… | |
… | |
849 | } |
1021 | } |
850 | |
1022 | |
851 | prev__cctx = prev->cctx; |
1023 | prev__cctx = prev->cctx; |
852 | |
1024 | |
853 | /* possibly "free" the cctx */ |
1025 | /* possibly "free" the cctx */ |
854 | if (prev__cctx->idle_sp == STACKLEVEL) |
1026 | if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE)) |
855 | { |
1027 | { |
856 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1028 | /* 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)); |
1029 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
858 | |
1030 | |
859 | prev->cctx = 0; |
1031 | prev->cctx = 0; |
860 | |
1032 | |
861 | cctx_put (prev__cctx); |
1033 | cctx_put (prev__cctx); |
862 | prev__cctx->inuse = 0; |
|
|
863 | } |
1034 | } |
864 | |
1035 | |
|
|
1036 | ++next->usecount; |
|
|
1037 | |
865 | if (!next->cctx) |
1038 | if (!next->cctx) |
866 | { |
|
|
867 | next->cctx = cctx_get (aTHX); |
1039 | next->cctx = cctx_get (aTHX); |
868 | assert (!next->cctx->inuse); |
|
|
869 | next->cctx->inuse = 1; |
|
|
870 | } |
|
|
871 | |
1040 | |
872 | if (prev__cctx != next->cctx) |
1041 | if (prev__cctx != next->cctx) |
873 | { |
1042 | { |
874 | prev__cctx->top_env = PL_top_env; |
1043 | prev__cctx->top_env = PL_top_env; |
875 | PL_top_env = next->cctx->top_env; |
1044 | PL_top_env = next->cctx->top_env; |
… | |
… | |
885 | { |
1054 | { |
886 | struct coro *prev, *next; |
1055 | struct coro *prev, *next; |
887 | }; |
1056 | }; |
888 | |
1057 | |
889 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
1058 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
|
|
1059 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
890 | |
1060 | |
891 | /** high level stuff ********************************************************/ |
1061 | /** high level stuff ********************************************************/ |
892 | |
1062 | |
893 | static int |
1063 | static int |
894 | coro_state_destroy (pTHX_ struct coro *coro) |
1064 | coro_state_destroy (pTHX_ struct coro *coro) |
… | |
… | |
977 | #else |
1147 | #else |
978 | # define MGf_DUP 0 |
1148 | # define MGf_DUP 0 |
979 | #endif |
1149 | #endif |
980 | }; |
1150 | }; |
981 | |
1151 | |
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 |
1152 | static void |
1006 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
1153 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
1007 | { |
1154 | { |
1008 | ta->prev = SvSTATE (prev_sv); |
1155 | ta->prev = SvSTATE (prev_sv); |
1009 | ta->next = SvSTATE (next_sv); |
1156 | ta->next = SvSTATE (next_sv); |
|
|
1157 | TRANSFER_CHECK (*ta); |
1010 | } |
1158 | } |
1011 | |
1159 | |
1012 | static void |
1160 | static void |
1013 | api_transfer (SV *prev_sv, SV *next_sv) |
1161 | api_transfer (SV *prev_sv, SV *next_sv) |
1014 | { |
1162 | { |
… | |
… | |
1131 | break; |
1279 | break; |
1132 | } |
1280 | } |
1133 | |
1281 | |
1134 | /* free this only after the transfer */ |
1282 | /* free this only after the transfer */ |
1135 | prev_sv = SvRV (coro_current); |
1283 | prev_sv = SvRV (coro_current); |
1136 | SvRV_set (coro_current, next_sv); |
|
|
1137 | ta->prev = SvSTATE (prev_sv); |
1284 | ta->prev = SvSTATE (prev_sv); |
1138 | |
1285 | TRANSFER_CHECK (*ta); |
1139 | assert (ta->next->flags & CF_READY); |
1286 | assert (ta->next->flags & CF_READY); |
1140 | ta->next->flags &= ~CF_READY; |
1287 | ta->next->flags &= ~CF_READY; |
|
|
1288 | SvRV_set (coro_current, next_sv); |
1141 | |
1289 | |
1142 | LOCK; |
1290 | LOCK; |
1143 | free_coro_mortal (aTHX); |
1291 | free_coro_mortal (aTHX); |
1144 | coro_mortal = prev_sv; |
1292 | coro_mortal = prev_sv; |
1145 | UNLOCK; |
1293 | UNLOCK; |
… | |
… | |
1206 | } |
1354 | } |
1207 | else |
1355 | else |
1208 | return 0; |
1356 | return 0; |
1209 | } |
1357 | } |
1210 | |
1358 | |
|
|
1359 | static void |
|
|
1360 | api_trace (SV *coro_sv, int flags) |
|
|
1361 | { |
|
|
1362 | dTHX; |
|
|
1363 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1364 | |
|
|
1365 | if (flags & CC_TRACE) |
|
|
1366 | { |
|
|
1367 | if (!coro->cctx) |
|
|
1368 | coro->cctx = cctx_new (); |
|
|
1369 | else if (!(coro->cctx->flags & CC_TRACE)) |
|
|
1370 | croak ("cannot enable tracing on coroutine with custom stack"); |
|
|
1371 | |
|
|
1372 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
|
|
1373 | } |
|
|
1374 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
|
|
1375 | { |
|
|
1376 | coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL); |
|
|
1377 | |
|
|
1378 | if (coro->flags & CF_RUNNING) |
|
|
1379 | PL_runops = RUNOPS_DEFAULT; |
|
|
1380 | else |
|
|
1381 | coro->runops = RUNOPS_DEFAULT; |
|
|
1382 | } |
|
|
1383 | } |
|
|
1384 | |
1211 | MODULE = Coro::State PACKAGE = Coro::State |
1385 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1212 | |
1386 | |
1213 | PROTOTYPES: DISABLE |
1387 | PROTOTYPES: DISABLE |
1214 | |
1388 | |
1215 | BOOT: |
1389 | BOOT: |
1216 | { |
1390 | { |
… | |
… | |
1218 | MUTEX_INIT (&coro_mutex); |
1392 | MUTEX_INIT (&coro_mutex); |
1219 | #endif |
1393 | #endif |
1220 | BOOT_PAGESIZE; |
1394 | BOOT_PAGESIZE; |
1221 | |
1395 | |
1222 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1396 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
|
|
1397 | |
|
|
1398 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
|
|
1399 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
|
|
1400 | newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); |
|
|
1401 | newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); |
1223 | |
1402 | |
1224 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1403 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1225 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1404 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1226 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1405 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1227 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
1406 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
… | |
… | |
1419 | save_perl (aTHX_ coro); |
1598 | save_perl (aTHX_ coro); |
1420 | load_perl (aTHX_ &temp); |
1599 | load_perl (aTHX_ &temp); |
1421 | } |
1600 | } |
1422 | } |
1601 | } |
1423 | } |
1602 | } |
1424 | |
1603 | |
1425 | SV * |
1604 | SV * |
1426 | is_ready (Coro::State coro) |
1605 | is_ready (Coro::State coro) |
1427 | PROTOTYPE: $ |
1606 | PROTOTYPE: $ |
1428 | ALIAS: |
1607 | ALIAS: |
1429 | is_ready = CF_READY |
1608 | is_ready = CF_READY |
… | |
… | |
1433 | CODE: |
1612 | CODE: |
1434 | RETVAL = boolSV (coro->flags & ix); |
1613 | RETVAL = boolSV (coro->flags & ix); |
1435 | OUTPUT: |
1614 | OUTPUT: |
1436 | RETVAL |
1615 | RETVAL |
1437 | |
1616 | |
|
|
1617 | void |
|
|
1618 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
|
|
1619 | |
1438 | SV * |
1620 | SV * |
1439 | has_stack (Coro::State coro) |
1621 | has_stack (Coro::State coro) |
1440 | PROTOTYPE: $ |
1622 | PROTOTYPE: $ |
1441 | CODE: |
1623 | CODE: |
1442 | RETVAL = boolSV (!!coro->cctx); |
1624 | RETVAL = boolSV (!!coro->cctx); |
1443 | OUTPUT: |
1625 | OUTPUT: |
1444 | RETVAL |
1626 | RETVAL |
1445 | |
1627 | |
|
|
1628 | int |
|
|
1629 | is_traced (Coro::State coro) |
|
|
1630 | PROTOTYPE: $ |
|
|
1631 | CODE: |
|
|
1632 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
|
|
1633 | OUTPUT: |
|
|
1634 | RETVAL |
|
|
1635 | |
1446 | IV |
1636 | IV |
1447 | rss (Coro::State coro) |
1637 | rss (Coro::State coro) |
1448 | PROTOTYPE: $ |
1638 | PROTOTYPE: $ |
|
|
1639 | ALIAS: |
|
|
1640 | usecount = 1 |
1449 | CODE: |
1641 | CODE: |
1450 | RETVAL = coro_rss (coro); |
1642 | switch (ix) |
|
|
1643 | { |
|
|
1644 | case 0: RETVAL = coro_rss (aTHX_ coro); break; |
|
|
1645 | case 1: RETVAL = coro->usecount; break; |
|
|
1646 | } |
1451 | OUTPUT: |
1647 | OUTPUT: |
1452 | RETVAL |
1648 | RETVAL |
1453 | |
1649 | |
1454 | |
1650 | |
1455 | MODULE = Coro::State PACKAGE = Coro |
1651 | MODULE = Coro::State PACKAGE = Coro |
… | |
… | |
1578 | { |
1774 | { |
1579 | struct coro *coro = SvSTATE (coro_current); |
1775 | struct coro *coro = SvSTATE (coro_current); |
1580 | |
1776 | |
1581 | sv_setsv (cb, &PL_sv_undef); |
1777 | sv_setsv (cb, &PL_sv_undef); |
1582 | |
1778 | |
1583 | if (coro_rss (coro) > SvIV (sv_pool_rss) |
1779 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1584 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1780 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1585 | croak ("\3terminate\2\n"); |
1781 | croak ("\3terminate\2\n"); |
1586 | |
1782 | |
1587 | av_clear (GvAV (PL_defgv)); |
1783 | av_clear (GvAV (PL_defgv)); |
1588 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1784 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1589 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
1785 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
|
|
1786 | |
1590 | coro->save = CORO_SAVE_DEF; |
1787 | coro->save = CORO_SAVE_DEF; |
1591 | coro->prio = 0; |
1788 | coro->prio = 0; |
|
|
1789 | |
|
|
1790 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
|
|
1791 | api_trace (coro_current, 0); |
|
|
1792 | |
1592 | av_push (av_async_pool, newSVsv (coro_current)); |
1793 | av_push (av_async_pool, newSVsv (coro_current)); |
1593 | } |
1794 | } |
1594 | |
1795 | |
1595 | |
1796 | |
1596 | MODULE = Coro::State PACKAGE = Coro::AIO |
1797 | MODULE = Coro::State PACKAGE = Coro::AIO |