… | |
… | |
88 | /* prefer perl internal functions over our own? */ |
88 | /* prefer perl internal functions over our own? */ |
89 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
89 | #ifndef CORO_PREFER_PERL_FUNCTIONS |
90 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
90 | # define CORO_PREFER_PERL_FUNCTIONS 0 |
91 | #endif |
91 | #endif |
92 | |
92 | |
93 | /* The next macro should declare a variable stacklevel that contains and approximation |
93 | /* The next macros try to return the current stack pointer, in an as |
94 | * to the current C stack pointer. Its property is that it changes with each call |
94 | * portable way as possible. */ |
95 | * and should be unique. */ |
|
|
96 | #define dSTACKLEVEL int stacklevel |
95 | #define dSTACKLEVEL volatile char stacklevel |
97 | #define STACKLEVEL ((void *)&stacklevel) |
96 | #define STACKLEVEL ((void *)&stacklevel) |
98 | |
97 | |
99 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
98 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
100 | |
99 | |
101 | #if __GNUC__ >= 3 |
100 | #if __GNUC__ >= 3 |
… | |
… | |
133 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
132 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
134 | static JMPENV *main_top_env; |
133 | static JMPENV *main_top_env; |
135 | static HV *coro_state_stash, *coro_stash; |
134 | static HV *coro_state_stash, *coro_stash; |
136 | static SV *coro_mortal; /* will be freed after next transfer */ |
135 | static SV *coro_mortal; /* will be freed after next transfer */ |
137 | |
136 | |
|
|
137 | /* async_pool helper stuff */ |
|
|
138 | static SV *sv_pool_rss; |
|
|
139 | static SV *sv_pool_size; |
|
|
140 | static AV *av_async_pool; |
|
|
141 | |
138 | static struct coro_cctx *cctx_first; |
142 | static struct coro_cctx *cctx_first; |
139 | static int cctx_count, cctx_idle; |
143 | static int cctx_count, cctx_idle; |
|
|
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 | }; |
140 | |
153 | |
141 | /* this is a structure representing a c-level coroutine */ |
154 | /* this is a structure representing a c-level coroutine */ |
142 | typedef struct coro_cctx { |
155 | typedef struct coro_cctx { |
143 | struct coro_cctx *next; |
156 | struct coro_cctx *next; |
144 | |
157 | |
… | |
… | |
153 | coro_context cctx; |
166 | coro_context cctx; |
154 | |
167 | |
155 | #if CORO_USE_VALGRIND |
168 | #if CORO_USE_VALGRIND |
156 | int valgrind_id; |
169 | int valgrind_id; |
157 | #endif |
170 | #endif |
158 | char inuse, mapped; |
171 | unsigned char flags; |
159 | } coro_cctx; |
172 | } coro_cctx; |
160 | |
173 | |
161 | enum { |
174 | enum { |
162 | CF_RUNNING = 0x0001, /* coroutine is running */ |
175 | CF_RUNNING = 0x0001, /* coroutine is running */ |
163 | CF_READY = 0x0002, /* coroutine is ready */ |
176 | CF_READY = 0x0002, /* coroutine is ready */ |
… | |
… | |
395 | /* |
408 | /* |
396 | * the worst thing you can imagine happens first - we have to save |
409 | * the worst thing you can imagine happens first - we have to save |
397 | * (and reinitialize) all cv's in the whole callchain :( |
410 | * (and reinitialize) all cv's in the whole callchain :( |
398 | */ |
411 | */ |
399 | |
412 | |
400 | EXTEND (SP, 3 + 1); |
|
|
401 | PUSHs (Nullsv); |
413 | XPUSHs (Nullsv); |
402 | /* this loop was inspired by pp_caller */ |
414 | /* this loop was inspired by pp_caller */ |
403 | for (;;) |
415 | for (;;) |
404 | { |
416 | { |
405 | while (cxix >= 0) |
417 | while (cxix >= 0) |
406 | { |
418 | { |
… | |
… | |
425 | |
437 | |
426 | if (top_si->si_type == PERLSI_MAIN) |
438 | if (top_si->si_type == PERLSI_MAIN) |
427 | break; |
439 | break; |
428 | |
440 | |
429 | top_si = top_si->si_prev; |
441 | top_si = top_si->si_prev; |
430 | ccstk = top_si->si_cxstack; |
442 | ccstk = top_si->si_cxstack; |
431 | cxix = top_si->si_cxix; |
443 | cxix = top_si->si_cxix; |
432 | } |
444 | } |
433 | |
445 | |
434 | PUTBACK; |
446 | PUTBACK; |
435 | } |
447 | } |
436 | |
448 | |
… | |
… | |
455 | # define coro_init_stacks init_stacks |
467 | # define coro_init_stacks init_stacks |
456 | #else |
468 | #else |
457 | static void |
469 | static void |
458 | coro_init_stacks (pTHX) |
470 | coro_init_stacks (pTHX) |
459 | { |
471 | { |
460 | PL_curstackinfo = new_stackinfo(64, 4); |
472 | PL_curstackinfo = new_stackinfo(64, 6); |
461 | PL_curstackinfo->si_type = PERLSI_MAIN; |
473 | PL_curstackinfo->si_type = PERLSI_MAIN; |
462 | PL_curstack = PL_curstackinfo->si_stack; |
474 | PL_curstack = PL_curstackinfo->si_stack; |
463 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
475 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
464 | |
476 | |
465 | PL_stack_base = AvARRAY(PL_curstack); |
477 | PL_stack_base = AvARRAY(PL_curstack); |
… | |
… | |
481 | |
493 | |
482 | New(54,PL_scopestack,16,I32); |
494 | New(54,PL_scopestack,16,I32); |
483 | PL_scopestack_ix = 0; |
495 | PL_scopestack_ix = 0; |
484 | PL_scopestack_max = 16; |
496 | PL_scopestack_max = 16; |
485 | |
497 | |
486 | New(54,PL_savestack,32,ANY); |
498 | New(54,PL_savestack,64,ANY); |
487 | PL_savestack_ix = 0; |
499 | PL_savestack_ix = 0; |
488 | PL_savestack_max = 32; |
500 | PL_savestack_max = 64; |
489 | |
501 | |
490 | #if !PERL_VERSION_ATLEAST (5,9,0) |
502 | #if !PERL_VERSION_ATLEAST (5,9,0) |
491 | New(54,PL_retstack,8,OP*); |
503 | New(54,PL_retstack,4,OP*); |
492 | PL_retstack_ix = 0; |
504 | PL_retstack_ix = 0; |
493 | PL_retstack_max = 8; |
505 | PL_retstack_max = 4; |
494 | #endif |
506 | #endif |
495 | } |
507 | } |
496 | #endif |
508 | #endif |
497 | |
509 | |
498 | /* |
510 | /* |
… | |
… | |
621 | SvREFCNT_dec (coro_mortal); |
633 | SvREFCNT_dec (coro_mortal); |
622 | coro_mortal = 0; |
634 | coro_mortal = 0; |
623 | } |
635 | } |
624 | } |
636 | } |
625 | |
637 | |
|
|
638 | static int |
|
|
639 | runops_coro (pTHX) |
|
|
640 | { |
|
|
641 | COP *oldcop = 0; |
|
|
642 | int oldcxix = -2; |
|
|
643 | |
|
|
644 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
|
|
645 | { |
|
|
646 | PERL_ASYNC_CHECK (); |
|
|
647 | |
|
|
648 | if (PL_op->op_type == OP_LEAVESUB) |
|
|
649 | { |
|
|
650 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
|
|
651 | SV **bot, **top; |
|
|
652 | AV *av = newAV (); /* return values */ |
|
|
653 | runops_proc_t old_runops = PL_runops; |
|
|
654 | dSP; |
|
|
655 | ENTER; |
|
|
656 | SAVETMPS; |
|
|
657 | EXTEND (SP, 3); |
|
|
658 | PL_runops = RUNOPS_DEFAULT; |
|
|
659 | |
|
|
660 | GV *gv = CvGV (cx->blk_sub.cv); |
|
|
661 | SV *fullname = sv_2mortal (newSV (0)); |
|
|
662 | if (isGV (gv)) |
|
|
663 | gv_efullname3 (fullname, gv, 0); |
|
|
664 | |
|
|
665 | bot = PL_stack_base + cx->blk_oldsp + 1; |
|
|
666 | top = cx->blk_gimme == G_ARRAY ? SP + 1 |
|
|
667 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
|
|
668 | : bot; |
|
|
669 | |
|
|
670 | while (bot < top) |
|
|
671 | av_push (av, SvREFCNT_inc (*bot++)); |
|
|
672 | |
|
|
673 | PUSHMARK (SP); |
|
|
674 | PUSHs (&PL_sv_no); |
|
|
675 | PUSHs (fullname); |
|
|
676 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
677 | PUTBACK; |
|
|
678 | call_pv ("Coro::_do_trace_sub", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
679 | SPAGAIN; |
|
|
680 | |
|
|
681 | FREETMPS; |
|
|
682 | LEAVE; |
|
|
683 | PL_runops = old_runops; |
|
|
684 | } |
|
|
685 | |
|
|
686 | if (oldcop != PL_curcop) |
|
|
687 | { |
|
|
688 | oldcop = PL_curcop; |
|
|
689 | |
|
|
690 | if (PL_curcop != &PL_compiling) |
|
|
691 | { |
|
|
692 | runops_proc_t old_runops = PL_runops; |
|
|
693 | dSP; |
|
|
694 | ENTER; |
|
|
695 | SAVETMPS; |
|
|
696 | EXTEND (SP, 3); |
|
|
697 | PL_runops = RUNOPS_DEFAULT; |
|
|
698 | |
|
|
699 | if (oldcxix != cxstack_ix) |
|
|
700 | { |
|
|
701 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
|
|
702 | |
|
|
703 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
|
|
704 | { |
|
|
705 | GV *gv = CvGV (cx->blk_sub.cv); |
|
|
706 | SV *fullname = sv_2mortal (newSV (0)); |
|
|
707 | if (isGV (gv)) |
|
|
708 | gv_efullname3 (fullname, gv, 0); |
|
|
709 | |
|
|
710 | PUSHMARK (SP); |
|
|
711 | PUSHs (&PL_sv_yes); |
|
|
712 | PUSHs (fullname); |
|
|
713 | PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
|
|
714 | PUTBACK; |
|
|
715 | call_pv ("Coro::_do_trace_sub", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
716 | SPAGAIN; |
|
|
717 | } |
|
|
718 | |
|
|
719 | oldcxix = cxstack_ix; |
|
|
720 | } |
|
|
721 | |
|
|
722 | if (0) { |
|
|
723 | PUSHMARK (SP); |
|
|
724 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
|
|
725 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
|
|
726 | PUTBACK; |
|
|
727 | call_pv ("Coro::_do_trace_line", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
728 | SPAGAIN; |
|
|
729 | } |
|
|
730 | |
|
|
731 | FREETMPS; |
|
|
732 | LEAVE; |
|
|
733 | PL_runops = old_runops; |
|
|
734 | } |
|
|
735 | } |
|
|
736 | } |
|
|
737 | |
|
|
738 | TAINT_NOT; |
|
|
739 | return 0; |
|
|
740 | } |
|
|
741 | |
626 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
742 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
627 | /* _cctx_init should be careful, as it could be called at almost any time */ |
743 | /* _cctx_init should be careful, as it could be called at almost any time */ |
628 | /* during execution of a perl program */ |
744 | /* during execution of a perl program */ |
629 | static void NOINLINE |
745 | static void NOINLINE |
630 | prepare_cctx (pTHX_ coro_cctx *cctx) |
746 | prepare_cctx (pTHX_ coro_cctx *cctx) |
631 | { |
747 | { |
632 | dSP; |
748 | dSP; |
633 | LOGOP myop; |
749 | LOGOP myop; |
|
|
750 | |
|
|
751 | PL_top_env = &PL_start_env; |
|
|
752 | |
|
|
753 | if (cctx->flags & CC_TRACE) |
|
|
754 | PL_runops = runops_coro; |
634 | |
755 | |
635 | Zero (&myop, 1, LOGOP); |
756 | Zero (&myop, 1, LOGOP); |
636 | myop.op_next = PL_op; |
757 | myop.op_next = PL_op; |
637 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
758 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
638 | |
759 | |
… | |
… | |
655 | dTHX; |
776 | dTHX; |
656 | |
777 | |
657 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
778 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
658 | UNLOCK; |
779 | UNLOCK; |
659 | |
780 | |
660 | PL_top_env = &PL_start_env; |
781 | /* we now skip the entersub that lead to transfer() */ |
|
|
782 | PL_op = PL_op->op_next; |
661 | |
783 | |
662 | /* inject a fake subroutine call to cctx_init */ |
784 | /* inject a fake subroutine call to cctx_init */ |
663 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
785 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
664 | |
786 | |
665 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
787 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
… | |
… | |
699 | # if CORO_STACKGUARD |
821 | # if CORO_STACKGUARD |
700 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
822 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
701 | # endif |
823 | # endif |
702 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
824 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
703 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
825 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
704 | cctx->mapped = 1; |
826 | cctx->flags |= CC_MAPPED; |
705 | } |
827 | } |
706 | else |
828 | else |
707 | #endif |
829 | #endif |
708 | { |
830 | { |
709 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
831 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
… | |
… | |
736 | #if CORO_USE_VALGRIND |
858 | #if CORO_USE_VALGRIND |
737 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
859 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
738 | #endif |
860 | #endif |
739 | |
861 | |
740 | #if HAVE_MMAP |
862 | #if HAVE_MMAP |
741 | if (cctx->mapped) |
863 | if (cctx->flags & CC_MAPPED) |
742 | munmap (cctx->sptr, cctx->ssize); |
864 | munmap (cctx->sptr, cctx->ssize); |
743 | else |
865 | else |
744 | #endif |
866 | #endif |
745 | Safefree (cctx->sptr); |
867 | Safefree (cctx->sptr); |
746 | |
868 | |
… | |
… | |
754 | { |
876 | { |
755 | coro_cctx *cctx = cctx_first; |
877 | coro_cctx *cctx = cctx_first; |
756 | cctx_first = cctx->next; |
878 | cctx_first = cctx->next; |
757 | --cctx_idle; |
879 | --cctx_idle; |
758 | |
880 | |
759 | if (cctx->ssize >= coro_stacksize) |
881 | if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE)) |
760 | return cctx; |
882 | return cctx; |
761 | |
883 | |
762 | cctx_destroy (cctx); |
884 | cctx_destroy (cctx); |
763 | } |
885 | } |
764 | |
886 | |
765 | PL_op = PL_op->op_next; |
|
|
766 | return cctx_new (); |
887 | return cctx_new (); |
767 | } |
888 | } |
768 | |
889 | |
769 | static void |
890 | static void |
770 | cctx_put (coro_cctx *cctx) |
891 | cctx_put (coro_cctx *cctx) |
… | |
… | |
774 | { |
895 | { |
775 | coro_cctx *first = cctx_first; |
896 | coro_cctx *first = cctx_first; |
776 | cctx_first = first->next; |
897 | cctx_first = first->next; |
777 | --cctx_idle; |
898 | --cctx_idle; |
778 | |
899 | |
779 | assert (!first->inuse); |
|
|
780 | cctx_destroy (first); |
900 | cctx_destroy (first); |
781 | } |
901 | } |
782 | |
902 | |
783 | ++cctx_idle; |
903 | ++cctx_idle; |
784 | cctx->next = cctx_first; |
904 | cctx->next = cctx_first; |
… | |
… | |
805 | |
925 | |
806 | if (prev->flags & CF_NEW) |
926 | if (prev->flags & CF_NEW) |
807 | { |
927 | { |
808 | /* create a new empty context */ |
928 | /* create a new empty context */ |
809 | Newz (0, prev->cctx, 1, coro_cctx); |
929 | Newz (0, prev->cctx, 1, coro_cctx); |
810 | prev->cctx->inuse = 1; |
|
|
811 | prev->flags &= ~CF_NEW; |
930 | prev->flags &= ~CF_NEW; |
812 | prev->flags |= CF_RUNNING; |
931 | prev->flags |= CF_RUNNING; |
813 | } |
932 | } |
814 | |
933 | |
815 | /*TODO: must not croak here */ |
934 | /*TODO: must not croak here */ |
… | |
… | |
833 | next->flags &= ~CF_NEW; |
952 | next->flags &= ~CF_NEW; |
834 | /* first get rid of the old state */ |
953 | /* first get rid of the old state */ |
835 | save_perl (aTHX_ prev); |
954 | save_perl (aTHX_ prev); |
836 | /* setup coroutine call */ |
955 | /* setup coroutine call */ |
837 | setup_coro (aTHX_ next); |
956 | setup_coro (aTHX_ next); |
838 | /* need a new stack */ |
|
|
839 | assert (!next->cctx); |
|
|
840 | } |
957 | } |
841 | else |
958 | else |
842 | { |
959 | { |
843 | /* coroutine already started */ |
960 | /* coroutine already started */ |
844 | save_perl (aTHX_ prev); |
961 | save_perl (aTHX_ prev); |
… | |
… | |
846 | } |
963 | } |
847 | |
964 | |
848 | prev__cctx = prev->cctx; |
965 | prev__cctx = prev->cctx; |
849 | |
966 | |
850 | /* possibly "free" the cctx */ |
967 | /* possibly "free" the cctx */ |
851 | if (prev__cctx->idle_sp == STACKLEVEL) |
968 | if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE)) |
852 | { |
969 | { |
853 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
970 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
854 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
971 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
855 | |
972 | |
856 | prev->cctx = 0; |
973 | prev->cctx = 0; |
857 | |
974 | |
858 | cctx_put (prev__cctx); |
975 | cctx_put (prev__cctx); |
859 | prev__cctx->inuse = 0; |
|
|
860 | } |
976 | } |
861 | |
977 | |
862 | if (!next->cctx) |
978 | if (!next->cctx) |
863 | { |
|
|
864 | next->cctx = cctx_get (aTHX); |
979 | next->cctx = cctx_get (aTHX); |
865 | assert (!next->cctx->inuse); |
|
|
866 | next->cctx->inuse = 1; |
|
|
867 | } |
|
|
868 | |
980 | |
869 | if (prev__cctx != next->cctx) |
981 | if (prev__cctx != next->cctx) |
870 | { |
982 | { |
871 | prev__cctx->top_env = PL_top_env; |
983 | prev__cctx->top_env = PL_top_env; |
872 | PL_top_env = next->cctx->top_env; |
984 | PL_top_env = next->cctx->top_env; |
… | |
… | |
1216 | #endif |
1328 | #endif |
1217 | BOOT_PAGESIZE; |
1329 | BOOT_PAGESIZE; |
1218 | |
1330 | |
1219 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1331 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1220 | |
1332 | |
|
|
1333 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
|
|
1334 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
|
|
1335 | newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); |
|
|
1336 | newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); |
|
|
1337 | |
1221 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1338 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1222 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1339 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1223 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1340 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1224 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
1341 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
1225 | newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); |
1342 | newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); |
… | |
… | |
1376 | if (coro->hv) |
1493 | if (coro->hv) |
1377 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
1494 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
1378 | } |
1495 | } |
1379 | |
1496 | |
1380 | void |
1497 | void |
1381 | _eval (Coro::State coro, SV *coderef) |
1498 | call (Coro::State coro, SV *coderef) |
|
|
1499 | ALIAS: |
|
|
1500 | eval = 1 |
1382 | CODE: |
1501 | CODE: |
1383 | { |
1502 | { |
1384 | if (coro->mainstack) |
1503 | if (coro->mainstack) |
1385 | { |
1504 | { |
1386 | struct coro temp; |
1505 | struct coro temp; |
… | |
… | |
1397 | dSP; |
1516 | dSP; |
1398 | ENTER; |
1517 | ENTER; |
1399 | SAVETMPS; |
1518 | SAVETMPS; |
1400 | PUSHMARK (SP); |
1519 | PUSHMARK (SP); |
1401 | PUTBACK; |
1520 | PUTBACK; |
|
|
1521 | if (ix) |
|
|
1522 | eval_sv (coderef, 0); |
|
|
1523 | else |
1402 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1524 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1403 | SPAGAIN; |
1525 | SPAGAIN; |
1404 | FREETMPS; |
1526 | FREETMPS; |
1405 | LEAVE; |
1527 | LEAVE; |
1406 | PUTBACK; |
1528 | PUTBACK; |
1407 | } |
1529 | } |
… | |
… | |
1425 | CODE: |
1547 | CODE: |
1426 | RETVAL = boolSV (coro->flags & ix); |
1548 | RETVAL = boolSV (coro->flags & ix); |
1427 | OUTPUT: |
1549 | OUTPUT: |
1428 | RETVAL |
1550 | RETVAL |
1429 | |
1551 | |
|
|
1552 | void |
|
|
1553 | trace (Coro::State coro, int flags = CC_TRACE | CC_TRACE_SUB) |
|
|
1554 | CODE: |
|
|
1555 | if (flags & CC_TRACE) |
|
|
1556 | { |
|
|
1557 | if (!coro->cctx) |
|
|
1558 | coro->cctx = cctx_new (); |
|
|
1559 | else if (!(coro->cctx->flags & CC_TRACE)) |
|
|
1560 | croak ("cannot enable tracing on coroutine with custom stack"); |
|
|
1561 | |
|
|
1562 | coro->cctx->flags |= flags & (CC_TRACE | CC_TRACE_ALL); |
|
|
1563 | } |
|
|
1564 | else |
|
|
1565 | if (coro->cctx && coro->cctx->flags & CC_TRACE) |
|
|
1566 | { |
|
|
1567 | coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL); |
|
|
1568 | coro->cctx->flags |= CC_NOREUSE; |
|
|
1569 | |
|
|
1570 | if (coro->flags & CF_RUNNING) |
|
|
1571 | PL_runops = RUNOPS_DEFAULT; |
|
|
1572 | else |
|
|
1573 | coro->runops = RUNOPS_DEFAULT; |
|
|
1574 | } |
|
|
1575 | |
|
|
1576 | SV * |
|
|
1577 | has_stack (Coro::State coro) |
|
|
1578 | PROTOTYPE: $ |
|
|
1579 | CODE: |
|
|
1580 | RETVAL = boolSV (!!coro->cctx); |
|
|
1581 | OUTPUT: |
|
|
1582 | RETVAL |
|
|
1583 | |
|
|
1584 | int |
|
|
1585 | is_traced (Coro::State coro) |
|
|
1586 | PROTOTYPE: $ |
|
|
1587 | CODE: |
|
|
1588 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
|
|
1589 | OUTPUT: |
|
|
1590 | RETVAL |
|
|
1591 | |
1430 | IV |
1592 | IV |
1431 | rss (Coro::State coro) |
1593 | rss (Coro::State coro) |
1432 | PROTOTYPE: $ |
1594 | PROTOTYPE: $ |
1433 | CODE: |
1595 | CODE: |
1434 | RETVAL = coro_rss (coro); |
1596 | RETVAL = coro_rss (coro); |
… | |
… | |
1440 | |
1602 | |
1441 | BOOT: |
1603 | BOOT: |
1442 | { |
1604 | { |
1443 | int i; |
1605 | int i; |
1444 | |
1606 | |
|
|
1607 | sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); |
|
|
1608 | sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); |
|
|
1609 | av_async_pool = get_av ("Coro::async_pool", TRUE); |
|
|
1610 | |
|
|
1611 | coro_current = get_sv ("Coro::current", FALSE); |
|
|
1612 | SvREADONLY_on (coro_current); |
|
|
1613 | |
1445 | coro_stash = gv_stashpv ("Coro", TRUE); |
1614 | coro_stash = gv_stashpv ("Coro", TRUE); |
1446 | |
1615 | |
1447 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1616 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1448 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1617 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1449 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1618 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1450 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1619 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1451 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1620 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1452 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1621 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1453 | |
|
|
1454 | coro_current = get_sv ("Coro::current", FALSE); |
|
|
1455 | SvREADONLY_on (coro_current); |
|
|
1456 | |
1622 | |
1457 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1623 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1458 | coro_ready[i] = newAV (); |
1624 | coro_ready[i] = newAV (); |
1459 | |
1625 | |
1460 | { |
1626 | { |
… | |
… | |
1518 | CODE: |
1684 | CODE: |
1519 | RETVAL = coro_nready; |
1685 | RETVAL = coro_nready; |
1520 | OUTPUT: |
1686 | OUTPUT: |
1521 | RETVAL |
1687 | RETVAL |
1522 | |
1688 | |
|
|
1689 | # for async_pool speedup |
|
|
1690 | void |
|
|
1691 | _pool_1 (SV *cb) |
|
|
1692 | CODE: |
|
|
1693 | { |
|
|
1694 | int i, len; |
|
|
1695 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1696 | AV *defav = GvAV (PL_defgv); |
|
|
1697 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
|
|
1698 | AV *invoke_av; |
|
|
1699 | |
|
|
1700 | if (!invoke) |
|
|
1701 | croak ("\3terminate\2\n"); |
|
|
1702 | |
|
|
1703 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
1704 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
1705 | |
|
|
1706 | invoke_av = (AV *)SvRV (invoke); |
|
|
1707 | len = av_len (invoke_av); |
|
|
1708 | |
|
|
1709 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
|
|
1710 | |
|
|
1711 | if (len > 0) |
|
|
1712 | { |
|
|
1713 | av_fill (defav, len - 1); |
|
|
1714 | for (i = 0; i < len; ++i) |
|
|
1715 | av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); |
|
|
1716 | } |
|
|
1717 | |
|
|
1718 | SvREFCNT_dec (invoke); |
|
|
1719 | } |
|
|
1720 | |
|
|
1721 | void |
|
|
1722 | _pool_2 (SV *cb) |
|
|
1723 | CODE: |
|
|
1724 | { |
|
|
1725 | struct coro *coro = SvSTATE (coro_current); |
|
|
1726 | |
|
|
1727 | sv_setsv (cb, &PL_sv_undef); |
|
|
1728 | |
|
|
1729 | if (coro_rss (coro) > SvIV (sv_pool_rss) |
|
|
1730 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
1731 | croak ("\3terminate\2\n"); |
|
|
1732 | |
|
|
1733 | av_clear (GvAV (PL_defgv)); |
|
|
1734 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
|
|
1735 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
|
|
1736 | coro->save = CORO_SAVE_DEF; |
|
|
1737 | coro->prio = 0; |
|
|
1738 | av_push (av_async_pool, newSVsv (coro_current)); |
|
|
1739 | } |
|
|
1740 | |
|
|
1741 | |
1523 | MODULE = Coro::State PACKAGE = Coro::AIO |
1742 | MODULE = Coro::State PACKAGE = Coro::AIO |
1524 | |
1743 | |
1525 | SV * |
1744 | SV * |
1526 | _get_state () |
1745 | _get_state () |
1527 | CODE: |
1746 | CODE: |