… | |
… | |
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_TRACE = 0x02, |
|
|
148 | CC_NOREUSE = 0x04, /* throw this away after tracing */ |
|
|
149 | }; |
140 | |
150 | |
141 | /* this is a structure representing a c-level coroutine */ |
151 | /* this is a structure representing a c-level coroutine */ |
142 | typedef struct coro_cctx { |
152 | typedef struct coro_cctx { |
143 | struct coro_cctx *next; |
153 | struct coro_cctx *next; |
144 | |
154 | |
… | |
… | |
153 | coro_context cctx; |
163 | coro_context cctx; |
154 | |
164 | |
155 | #if CORO_USE_VALGRIND |
165 | #if CORO_USE_VALGRIND |
156 | int valgrind_id; |
166 | int valgrind_id; |
157 | #endif |
167 | #endif |
158 | char inuse, mapped; |
168 | unsigned char flags; |
159 | } coro_cctx; |
169 | } coro_cctx; |
160 | |
170 | |
161 | enum { |
171 | enum { |
162 | CF_RUNNING = 0x0001, /* coroutine is running */ |
172 | CF_RUNNING = 0x0001, /* coroutine is running */ |
163 | CF_READY = 0x0002, /* coroutine is ready */ |
173 | CF_READY = 0x0002, /* coroutine is ready */ |
… | |
… | |
210 | |
220 | |
211 | /* for Coro.pm */ |
221 | /* for Coro.pm */ |
212 | static SV *coro_current; |
222 | static SV *coro_current; |
213 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
223 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
214 | static int coro_nready; |
224 | static int coro_nready; |
215 | static struct coro *first; |
225 | static struct coro *coro_first; |
216 | |
226 | |
217 | /** lowlevel stuff **********************************************************/ |
227 | /** lowlevel stuff **********************************************************/ |
218 | |
228 | |
219 | static AV * |
229 | static AV * |
220 | coro_clone_padlist (pTHX_ CV *cv) |
230 | coro_clone_padlist (pTHX_ CV *cv) |
… | |
… | |
395 | /* |
405 | /* |
396 | * the worst thing you can imagine happens first - we have to save |
406 | * the worst thing you can imagine happens first - we have to save |
397 | * (and reinitialize) all cv's in the whole callchain :( |
407 | * (and reinitialize) all cv's in the whole callchain :( |
398 | */ |
408 | */ |
399 | |
409 | |
400 | EXTEND (SP, 3 + 1); |
|
|
401 | PUSHs (Nullsv); |
410 | XPUSHs (Nullsv); |
402 | /* this loop was inspired by pp_caller */ |
411 | /* this loop was inspired by pp_caller */ |
403 | for (;;) |
412 | for (;;) |
404 | { |
413 | { |
405 | while (cxix >= 0) |
414 | while (cxix >= 0) |
406 | { |
415 | { |
… | |
… | |
425 | |
434 | |
426 | if (top_si->si_type == PERLSI_MAIN) |
435 | if (top_si->si_type == PERLSI_MAIN) |
427 | break; |
436 | break; |
428 | |
437 | |
429 | top_si = top_si->si_prev; |
438 | top_si = top_si->si_prev; |
430 | ccstk = top_si->si_cxstack; |
439 | ccstk = top_si->si_cxstack; |
431 | cxix = top_si->si_cxix; |
440 | cxix = top_si->si_cxix; |
432 | } |
441 | } |
433 | |
442 | |
434 | PUTBACK; |
443 | PUTBACK; |
435 | } |
444 | } |
436 | |
445 | |
… | |
… | |
455 | # define coro_init_stacks init_stacks |
464 | # define coro_init_stacks init_stacks |
456 | #else |
465 | #else |
457 | static void |
466 | static void |
458 | coro_init_stacks (pTHX) |
467 | coro_init_stacks (pTHX) |
459 | { |
468 | { |
460 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
469 | PL_curstackinfo = new_stackinfo(64, 6); |
461 | PL_curstackinfo->si_type = PERLSI_MAIN; |
470 | PL_curstackinfo->si_type = PERLSI_MAIN; |
462 | PL_curstack = PL_curstackinfo->si_stack; |
471 | PL_curstack = PL_curstackinfo->si_stack; |
463 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
472 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
464 | |
473 | |
465 | PL_stack_base = AvARRAY(PL_curstack); |
474 | PL_stack_base = AvARRAY(PL_curstack); |
466 | PL_stack_sp = PL_stack_base; |
475 | PL_stack_sp = PL_stack_base; |
467 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
476 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
468 | |
477 | |
469 | New(50,PL_tmps_stack,128,SV*); |
478 | New(50,PL_tmps_stack,64,SV*); |
470 | PL_tmps_floor = -1; |
479 | PL_tmps_floor = -1; |
471 | PL_tmps_ix = -1; |
480 | PL_tmps_ix = -1; |
472 | PL_tmps_max = 128; |
481 | PL_tmps_max = 64; |
473 | |
482 | |
474 | New(54,PL_markstack,32,I32); |
483 | New(54,PL_markstack,16,I32); |
475 | PL_markstack_ptr = PL_markstack; |
484 | PL_markstack_ptr = PL_markstack; |
476 | PL_markstack_max = PL_markstack + 32; |
485 | PL_markstack_max = PL_markstack + 16; |
477 | |
486 | |
478 | #ifdef SET_MARK_OFFSET |
487 | #ifdef SET_MARK_OFFSET |
479 | SET_MARK_OFFSET; |
488 | SET_MARK_OFFSET; |
480 | #endif |
489 | #endif |
481 | |
490 | |
482 | New(54,PL_scopestack,32,I32); |
491 | New(54,PL_scopestack,16,I32); |
483 | PL_scopestack_ix = 0; |
492 | PL_scopestack_ix = 0; |
484 | PL_scopestack_max = 32; |
493 | PL_scopestack_max = 16; |
485 | |
494 | |
486 | New(54,PL_savestack,64,ANY); |
495 | New(54,PL_savestack,64,ANY); |
487 | PL_savestack_ix = 0; |
496 | PL_savestack_ix = 0; |
488 | PL_savestack_max = 64; |
497 | PL_savestack_max = 64; |
489 | |
498 | |
490 | #if !PERL_VERSION_ATLEAST (5,9,0) |
499 | #if !PERL_VERSION_ATLEAST (5,9,0) |
491 | New(54,PL_retstack,16,OP*); |
500 | New(54,PL_retstack,4,OP*); |
492 | PL_retstack_ix = 0; |
501 | PL_retstack_ix = 0; |
493 | PL_retstack_max = 16; |
502 | PL_retstack_max = 4; |
494 | #endif |
503 | #endif |
495 | } |
504 | } |
496 | #endif |
505 | #endif |
497 | |
506 | |
498 | /* |
507 | /* |
… | |
… | |
538 | Safefree (PL_scopestack); |
547 | Safefree (PL_scopestack); |
539 | Safefree (PL_savestack); |
548 | Safefree (PL_savestack); |
540 | #if !PERL_VERSION_ATLEAST (5,9,0) |
549 | #if !PERL_VERSION_ATLEAST (5,9,0) |
541 | Safefree (PL_retstack); |
550 | Safefree (PL_retstack); |
542 | #endif |
551 | #endif |
|
|
552 | } |
|
|
553 | |
|
|
554 | static size_t |
|
|
555 | coro_rss (struct coro *coro) |
|
|
556 | { |
|
|
557 | size_t rss = sizeof (coro); |
|
|
558 | |
|
|
559 | if (coro->mainstack) |
|
|
560 | { |
|
|
561 | if (coro->flags & CF_RUNNING) |
|
|
562 | { |
|
|
563 | #define VAR(name,type)coro->name = PL_ ## name; |
|
|
564 | # include "state.h" |
|
|
565 | #undef VAR |
|
|
566 | } |
|
|
567 | |
|
|
568 | rss += sizeof (coro->curstackinfo); |
|
|
569 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); |
|
|
570 | rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
|
|
571 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); |
|
|
572 | rss += coro->tmps_max * sizeof (SV *); |
|
|
573 | rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); |
|
|
574 | rss += coro->scopestack_max * sizeof (I32); |
|
|
575 | rss += coro->savestack_max * sizeof (ANY); |
|
|
576 | |
|
|
577 | #if !PERL_VERSION_ATLEAST (5,9,0) |
|
|
578 | rss += coro->retstack_max * sizeof (OP *); |
|
|
579 | #endif |
|
|
580 | } |
|
|
581 | |
|
|
582 | return rss; |
543 | } |
583 | } |
544 | |
584 | |
545 | /** coroutine stack handling ************************************************/ |
585 | /** coroutine stack handling ************************************************/ |
546 | |
586 | |
547 | static void |
587 | static void |
… | |
… | |
590 | SvREFCNT_dec (coro_mortal); |
630 | SvREFCNT_dec (coro_mortal); |
591 | coro_mortal = 0; |
631 | coro_mortal = 0; |
592 | } |
632 | } |
593 | } |
633 | } |
594 | |
634 | |
|
|
635 | static void |
|
|
636 | do_trace (pTHX) |
|
|
637 | { |
|
|
638 | if (PL_curcop != &PL_compiling) |
|
|
639 | { |
|
|
640 | runops_proc_t old_runops = PL_runops; |
|
|
641 | dSP; |
|
|
642 | ENTER; |
|
|
643 | SAVETMPS; |
|
|
644 | PUSHMARK (SP); |
|
|
645 | PUTBACK; |
|
|
646 | PL_runops = RUNOPS_DEFAULT; |
|
|
647 | call_pv ("Coro::_do_trace", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD | G_NOARGS); |
|
|
648 | PL_runops = old_runops; |
|
|
649 | SPAGAIN; |
|
|
650 | FREETMPS; |
|
|
651 | LEAVE; |
|
|
652 | PUTBACK; |
|
|
653 | } |
|
|
654 | } |
|
|
655 | |
|
|
656 | static int |
|
|
657 | runops_coro (pTHX) |
|
|
658 | { |
|
|
659 | COP *oldcop = 0; |
|
|
660 | |
|
|
661 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
|
|
662 | { |
|
|
663 | PERL_ASYNC_CHECK (); |
|
|
664 | |
|
|
665 | if (oldcop != PL_curcop) |
|
|
666 | { |
|
|
667 | oldcop = PL_curcop; |
|
|
668 | do_trace (aTHX); |
|
|
669 | } |
|
|
670 | } |
|
|
671 | |
|
|
672 | TAINT_NOT; |
|
|
673 | return 0; |
|
|
674 | } |
|
|
675 | |
595 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
676 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
596 | /* _cctx_init should be careful, as it could be called at almost any time */ |
677 | /* _cctx_init should be careful, as it could be called at almost any time */ |
597 | /* during execution of a perl program */ |
678 | /* during execution of a perl program */ |
598 | static void NOINLINE |
679 | static void NOINLINE |
599 | prepare_cctx (pTHX_ coro_cctx *cctx) |
680 | prepare_cctx (pTHX_ coro_cctx *cctx) |
600 | { |
681 | { |
601 | dSP; |
682 | dSP; |
602 | LOGOP myop; |
683 | LOGOP myop; |
|
|
684 | |
|
|
685 | PL_top_env = &PL_start_env; |
|
|
686 | |
|
|
687 | if (cctx->flags & CC_TRACE) |
|
|
688 | PL_runops = runops_coro; |
603 | |
689 | |
604 | Zero (&myop, 1, LOGOP); |
690 | Zero (&myop, 1, LOGOP); |
605 | myop.op_next = PL_op; |
691 | myop.op_next = PL_op; |
606 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
692 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
607 | |
693 | |
… | |
… | |
624 | dTHX; |
710 | dTHX; |
625 | |
711 | |
626 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
712 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
627 | UNLOCK; |
713 | UNLOCK; |
628 | |
714 | |
629 | PL_top_env = &PL_start_env; |
715 | /* we now skip the entersub that lead to transfer() */ |
|
|
716 | PL_op = PL_op->op_next; |
630 | |
717 | |
631 | /* inject a fake subroutine call to cctx_init */ |
718 | /* inject a fake subroutine call to cctx_init */ |
632 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
719 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
633 | |
720 | |
634 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
721 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
… | |
… | |
668 | # if CORO_STACKGUARD |
755 | # if CORO_STACKGUARD |
669 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
756 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
670 | # endif |
757 | # endif |
671 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
758 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
672 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
759 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
673 | cctx->mapped = 1; |
760 | cctx->flags |= CC_MAPPED; |
674 | } |
761 | } |
675 | else |
762 | else |
676 | #endif |
763 | #endif |
677 | { |
764 | { |
678 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
765 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
… | |
… | |
705 | #if CORO_USE_VALGRIND |
792 | #if CORO_USE_VALGRIND |
706 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
793 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
707 | #endif |
794 | #endif |
708 | |
795 | |
709 | #if HAVE_MMAP |
796 | #if HAVE_MMAP |
710 | if (cctx->mapped) |
797 | if (cctx->flags & CC_MAPPED) |
711 | munmap (cctx->sptr, cctx->ssize); |
798 | munmap (cctx->sptr, cctx->ssize); |
712 | else |
799 | else |
713 | #endif |
800 | #endif |
714 | Safefree (cctx->sptr); |
801 | Safefree (cctx->sptr); |
715 | |
802 | |
… | |
… | |
723 | { |
810 | { |
724 | coro_cctx *cctx = cctx_first; |
811 | coro_cctx *cctx = cctx_first; |
725 | cctx_first = cctx->next; |
812 | cctx_first = cctx->next; |
726 | --cctx_idle; |
813 | --cctx_idle; |
727 | |
814 | |
728 | if (cctx->ssize >= coro_stacksize) |
815 | if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE)) |
729 | return cctx; |
816 | return cctx; |
730 | |
817 | |
731 | cctx_destroy (cctx); |
818 | cctx_destroy (cctx); |
732 | } |
819 | } |
733 | |
820 | |
734 | PL_op = PL_op->op_next; |
|
|
735 | return cctx_new (); |
821 | return cctx_new (); |
736 | } |
822 | } |
737 | |
823 | |
738 | static void |
824 | static void |
739 | cctx_put (coro_cctx *cctx) |
825 | cctx_put (coro_cctx *cctx) |
… | |
… | |
743 | { |
829 | { |
744 | coro_cctx *first = cctx_first; |
830 | coro_cctx *first = cctx_first; |
745 | cctx_first = first->next; |
831 | cctx_first = first->next; |
746 | --cctx_idle; |
832 | --cctx_idle; |
747 | |
833 | |
748 | assert (!first->inuse); |
|
|
749 | cctx_destroy (first); |
834 | cctx_destroy (first); |
750 | } |
835 | } |
751 | |
836 | |
752 | ++cctx_idle; |
837 | ++cctx_idle; |
753 | cctx->next = cctx_first; |
838 | cctx->next = cctx_first; |
… | |
… | |
774 | |
859 | |
775 | if (prev->flags & CF_NEW) |
860 | if (prev->flags & CF_NEW) |
776 | { |
861 | { |
777 | /* create a new empty context */ |
862 | /* create a new empty context */ |
778 | Newz (0, prev->cctx, 1, coro_cctx); |
863 | Newz (0, prev->cctx, 1, coro_cctx); |
779 | prev->cctx->inuse = 1; |
|
|
780 | prev->flags &= ~CF_NEW; |
864 | prev->flags &= ~CF_NEW; |
781 | prev->flags |= CF_RUNNING; |
865 | prev->flags |= CF_RUNNING; |
782 | } |
866 | } |
783 | |
867 | |
784 | /*TODO: must not croak here */ |
868 | /*TODO: must not croak here */ |
… | |
… | |
802 | next->flags &= ~CF_NEW; |
886 | next->flags &= ~CF_NEW; |
803 | /* first get rid of the old state */ |
887 | /* first get rid of the old state */ |
804 | save_perl (aTHX_ prev); |
888 | save_perl (aTHX_ prev); |
805 | /* setup coroutine call */ |
889 | /* setup coroutine call */ |
806 | setup_coro (aTHX_ next); |
890 | setup_coro (aTHX_ next); |
807 | /* need a new stack */ |
|
|
808 | assert (!next->cctx); |
|
|
809 | } |
891 | } |
810 | else |
892 | else |
811 | { |
893 | { |
812 | /* coroutine already started */ |
894 | /* coroutine already started */ |
813 | save_perl (aTHX_ prev); |
895 | save_perl (aTHX_ prev); |
… | |
… | |
823 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
905 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
824 | |
906 | |
825 | prev->cctx = 0; |
907 | prev->cctx = 0; |
826 | |
908 | |
827 | cctx_put (prev__cctx); |
909 | cctx_put (prev__cctx); |
828 | prev__cctx->inuse = 0; |
|
|
829 | } |
910 | } |
830 | |
911 | |
831 | if (!next->cctx) |
912 | if (!next->cctx) |
832 | { |
|
|
833 | next->cctx = cctx_get (aTHX); |
913 | next->cctx = cctx_get (aTHX); |
834 | assert (!next->cctx->inuse); |
|
|
835 | next->cctx->inuse = 1; |
|
|
836 | } |
|
|
837 | |
914 | |
838 | if (prev__cctx != next->cctx) |
915 | if (prev__cctx != next->cctx) |
839 | { |
916 | { |
840 | prev__cctx->top_env = PL_top_env; |
917 | prev__cctx->top_env = PL_top_env; |
841 | PL_top_env = next->cctx->top_env; |
918 | PL_top_env = next->cctx->top_env; |
… | |
… | |
900 | cctx_destroy (coro->cctx); |
977 | cctx_destroy (coro->cctx); |
901 | SvREFCNT_dec (coro->args); |
978 | SvREFCNT_dec (coro->args); |
902 | |
979 | |
903 | if (coro->next) coro->next->prev = coro->prev; |
980 | if (coro->next) coro->next->prev = coro->prev; |
904 | if (coro->prev) coro->prev->next = coro->next; |
981 | if (coro->prev) coro->prev->next = coro->next; |
905 | if (coro == first) first = coro->next; |
982 | if (coro == coro_first) coro_first = coro->next; |
906 | |
983 | |
907 | return 1; |
984 | return 1; |
908 | } |
985 | } |
909 | |
986 | |
910 | static int |
987 | static int |
… | |
… | |
1218 | Newz (0, coro, 1, struct coro); |
1295 | Newz (0, coro, 1, struct coro); |
1219 | coro->args = newAV (); |
1296 | coro->args = newAV (); |
1220 | coro->save = CORO_SAVE_DEF; |
1297 | coro->save = CORO_SAVE_DEF; |
1221 | coro->flags = CF_NEW; |
1298 | coro->flags = CF_NEW; |
1222 | |
1299 | |
1223 | if (first) first->prev = coro; |
1300 | if (coro_first) coro_first->prev = coro; |
1224 | coro->next = first; |
1301 | coro->next = coro_first; |
1225 | first = coro; |
1302 | coro_first = coro; |
1226 | |
1303 | |
1227 | coro->hv = hv = newHV (); |
1304 | coro->hv = hv = newHV (); |
1228 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1305 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1229 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1306 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1230 | |
1307 | |
… | |
… | |
1339 | void |
1416 | void |
1340 | list () |
1417 | list () |
1341 | PPCODE: |
1418 | PPCODE: |
1342 | { |
1419 | { |
1343 | struct coro *coro; |
1420 | struct coro *coro; |
1344 | for (coro = first; coro; coro = coro->next) |
1421 | for (coro = coro_first; coro; coro = coro->next) |
1345 | if (coro->hv) |
1422 | if (coro->hv) |
1346 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
1423 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
1347 | } |
1424 | } |
1348 | |
1425 | |
1349 | void |
1426 | void |
1350 | _eval (SV *coro_sv, SV *coderef) |
1427 | call (Coro::State coro, SV *coderef) |
|
|
1428 | ALIAS: |
|
|
1429 | eval = 1 |
1351 | CODE: |
1430 | CODE: |
1352 | { |
1431 | { |
1353 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1354 | if (coro->mainstack) |
1432 | if (coro->mainstack) |
1355 | { |
1433 | { |
1356 | struct coro temp; |
1434 | struct coro temp; |
1357 | Zero (&temp, 1, struct coro); |
1435 | Zero (&temp, 1, struct coro); |
1358 | temp.save = CORO_SAVE_ALL; |
1436 | temp.save = CORO_SAVE_ALL; |
… | |
… | |
1367 | dSP; |
1445 | dSP; |
1368 | ENTER; |
1446 | ENTER; |
1369 | SAVETMPS; |
1447 | SAVETMPS; |
1370 | PUSHMARK (SP); |
1448 | PUSHMARK (SP); |
1371 | PUTBACK; |
1449 | PUTBACK; |
|
|
1450 | if (ix) |
|
|
1451 | eval_sv (coderef, 0); |
|
|
1452 | else |
1372 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1453 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1373 | SPAGAIN; |
1454 | SPAGAIN; |
1374 | FREETMPS; |
1455 | FREETMPS; |
1375 | LEAVE; |
1456 | LEAVE; |
1376 | PUTBACK; |
1457 | PUTBACK; |
1377 | } |
1458 | } |
… | |
… | |
1383 | } |
1464 | } |
1384 | } |
1465 | } |
1385 | } |
1466 | } |
1386 | |
1467 | |
1387 | SV * |
1468 | SV * |
1388 | is_ready (SV *coro_sv) |
1469 | is_ready (Coro::State coro) |
1389 | PROTOTYPE: $ |
1470 | PROTOTYPE: $ |
1390 | ALIAS: |
1471 | ALIAS: |
1391 | is_ready = CF_READY |
1472 | is_ready = CF_READY |
1392 | is_running = CF_RUNNING |
1473 | is_running = CF_RUNNING |
1393 | is_new = CF_NEW |
1474 | is_new = CF_NEW |
1394 | is_destroyed = CF_DESTROYED |
1475 | is_destroyed = CF_DESTROYED |
1395 | CODE: |
1476 | CODE: |
1396 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1397 | RETVAL = boolSV (coro->flags & ix); |
1477 | RETVAL = boolSV (coro->flags & ix); |
1398 | OUTPUT: |
1478 | OUTPUT: |
1399 | RETVAL |
1479 | RETVAL |
1400 | |
1480 | |
|
|
1481 | void |
|
|
1482 | trace (Coro::State coro, int enable = 0) |
|
|
1483 | CODE: |
|
|
1484 | if (enable) |
|
|
1485 | { |
|
|
1486 | if (coro->flags & CF_RUNNING) |
|
|
1487 | croak ("cannot enable tracing on running coroutine"); |
|
|
1488 | |
|
|
1489 | if (coro->cctx) |
|
|
1490 | croak ("cannot enable tracing on coroutine with custom stack"); |
|
|
1491 | |
|
|
1492 | coro->cctx = cctx_new (); |
|
|
1493 | coro->cctx->flags |= CC_TRACE; |
|
|
1494 | } |
|
|
1495 | else |
|
|
1496 | if (coro->cctx && coro->cctx->flags & CC_TRACE) |
|
|
1497 | { |
|
|
1498 | coro->cctx->flags &= ~CC_TRACE; |
|
|
1499 | coro->cctx->flags |= CC_NOREUSE; |
|
|
1500 | } |
|
|
1501 | |
|
|
1502 | SV * |
|
|
1503 | has_stack (Coro::State coro) |
|
|
1504 | PROTOTYPE: $ |
|
|
1505 | CODE: |
|
|
1506 | RETVAL = boolSV (!!coro->cctx); |
|
|
1507 | OUTPUT: |
|
|
1508 | RETVAL |
|
|
1509 | |
|
|
1510 | IV |
|
|
1511 | rss (Coro::State coro) |
|
|
1512 | PROTOTYPE: $ |
|
|
1513 | CODE: |
|
|
1514 | RETVAL = coro_rss (coro); |
|
|
1515 | OUTPUT: |
|
|
1516 | RETVAL |
|
|
1517 | |
1401 | |
1518 | |
1402 | MODULE = Coro::State PACKAGE = Coro |
1519 | MODULE = Coro::State PACKAGE = Coro |
1403 | |
1520 | |
1404 | BOOT: |
1521 | BOOT: |
1405 | { |
1522 | { |
1406 | int i; |
1523 | int i; |
|
|
1524 | |
|
|
1525 | sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); |
|
|
1526 | sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); |
|
|
1527 | av_async_pool = get_av ("Coro::async_pool", TRUE); |
|
|
1528 | |
|
|
1529 | coro_current = get_sv ("Coro::current", FALSE); |
|
|
1530 | SvREADONLY_on (coro_current); |
1407 | |
1531 | |
1408 | coro_stash = gv_stashpv ("Coro", TRUE); |
1532 | coro_stash = gv_stashpv ("Coro", TRUE); |
1409 | |
1533 | |
1410 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1534 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1411 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1535 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1412 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1536 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1413 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1537 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1414 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1538 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1415 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1539 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1416 | |
|
|
1417 | coro_current = get_sv ("Coro::current", FALSE); |
|
|
1418 | SvREADONLY_on (coro_current); |
|
|
1419 | |
1540 | |
1420 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1541 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1421 | coro_ready[i] = newAV (); |
1542 | coro_ready[i] = newAV (); |
1422 | |
1543 | |
1423 | { |
1544 | { |
… | |
… | |
1481 | CODE: |
1602 | CODE: |
1482 | RETVAL = coro_nready; |
1603 | RETVAL = coro_nready; |
1483 | OUTPUT: |
1604 | OUTPUT: |
1484 | RETVAL |
1605 | RETVAL |
1485 | |
1606 | |
|
|
1607 | # for async_pool speedup |
|
|
1608 | void |
|
|
1609 | _pool_1 (SV *cb) |
|
|
1610 | CODE: |
|
|
1611 | { |
|
|
1612 | int i, len; |
|
|
1613 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1614 | AV *defav = GvAV (PL_defgv); |
|
|
1615 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
|
|
1616 | AV *invoke_av; |
|
|
1617 | |
|
|
1618 | if (!invoke) |
|
|
1619 | croak ("\3terminate\2\n"); |
|
|
1620 | |
|
|
1621 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
1622 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
1623 | |
|
|
1624 | invoke_av = (AV *)SvRV (invoke); |
|
|
1625 | len = av_len (invoke_av); |
|
|
1626 | |
|
|
1627 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
|
|
1628 | |
|
|
1629 | if (len > 0) |
|
|
1630 | { |
|
|
1631 | av_fill (defav, len - 1); |
|
|
1632 | for (i = 0; i < len; ++i) |
|
|
1633 | av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); |
|
|
1634 | } |
|
|
1635 | |
|
|
1636 | SvREFCNT_dec (invoke); |
|
|
1637 | } |
|
|
1638 | |
|
|
1639 | void |
|
|
1640 | _pool_2 (SV *cb) |
|
|
1641 | CODE: |
|
|
1642 | { |
|
|
1643 | struct coro *coro = SvSTATE (coro_current); |
|
|
1644 | |
|
|
1645 | sv_setsv (cb, &PL_sv_undef); |
|
|
1646 | |
|
|
1647 | if (coro_rss (coro) > SvIV (sv_pool_rss) |
|
|
1648 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
1649 | croak ("\3terminate\2\n"); |
|
|
1650 | |
|
|
1651 | av_clear (GvAV (PL_defgv)); |
|
|
1652 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
|
|
1653 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
|
|
1654 | coro->save = CORO_SAVE_DEF; |
|
|
1655 | coro->prio = 0; |
|
|
1656 | av_push (av_async_pool, newSVsv (coro_current)); |
|
|
1657 | } |
|
|
1658 | |
|
|
1659 | |
1486 | MODULE = Coro::State PACKAGE = Coro::AIO |
1660 | MODULE = Coro::State PACKAGE = Coro::AIO |
1487 | |
1661 | |
1488 | SV * |
1662 | SV * |
1489 | _get_state () |
1663 | _get_state () |
1490 | CODE: |
1664 | CODE: |