… | |
… | |
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 */ |
… | |
… | |
188 | # include "state.h" |
198 | # include "state.h" |
189 | #undef VAR |
199 | #undef VAR |
190 | |
200 | |
191 | /* coro process data */ |
201 | /* coro process data */ |
192 | int prio; |
202 | int prio; |
|
|
203 | |
|
|
204 | /* linked list */ |
|
|
205 | struct coro *next, *prev; |
|
|
206 | HV *hv; /* the perl hash associated with this coro, if any */ |
193 | }; |
207 | }; |
194 | |
208 | |
195 | typedef struct coro *Coro__State; |
209 | typedef struct coro *Coro__State; |
196 | typedef struct coro *Coro__State_or_hashref; |
210 | typedef struct coro *Coro__State_or_hashref; |
197 | |
211 | |
… | |
… | |
206 | |
220 | |
207 | /* for Coro.pm */ |
221 | /* for Coro.pm */ |
208 | static SV *coro_current; |
222 | static SV *coro_current; |
209 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
223 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
210 | static int coro_nready; |
224 | static int coro_nready; |
|
|
225 | static struct coro *coro_first; |
211 | |
226 | |
212 | /** lowlevel stuff **********************************************************/ |
227 | /** lowlevel stuff **********************************************************/ |
213 | |
228 | |
214 | static AV * |
229 | static AV * |
215 | coro_clone_padlist (pTHX_ CV *cv) |
230 | coro_clone_padlist (pTHX_ CV *cv) |
… | |
… | |
374 | CvPADLIST (cv) = (AV *)POPs; |
389 | CvPADLIST (cv) = (AV *)POPs; |
375 | } |
390 | } |
376 | |
391 | |
377 | PUTBACK; |
392 | PUTBACK; |
378 | } |
393 | } |
379 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
|
|
380 | } |
394 | } |
381 | |
395 | |
382 | static void |
396 | static void |
383 | save_perl (pTHX_ Coro__State c) |
397 | save_perl (pTHX_ Coro__State c) |
384 | { |
398 | { |
385 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
|
|
386 | { |
399 | { |
387 | dSP; |
400 | dSP; |
388 | I32 cxix = cxstack_ix; |
401 | I32 cxix = cxstack_ix; |
389 | PERL_CONTEXT *ccstk = cxstack; |
402 | PERL_CONTEXT *ccstk = cxstack; |
390 | PERL_SI *top_si = PL_curstackinfo; |
403 | PERL_SI *top_si = PL_curstackinfo; |
… | |
… | |
392 | /* |
405 | /* |
393 | * 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 |
394 | * (and reinitialize) all cv's in the whole callchain :( |
407 | * (and reinitialize) all cv's in the whole callchain :( |
395 | */ |
408 | */ |
396 | |
409 | |
397 | EXTEND (SP, 3 + 1); |
|
|
398 | PUSHs (Nullsv); |
410 | XPUSHs (Nullsv); |
399 | /* this loop was inspired by pp_caller */ |
411 | /* this loop was inspired by pp_caller */ |
400 | for (;;) |
412 | for (;;) |
401 | { |
413 | { |
402 | while (cxix >= 0) |
414 | while (cxix >= 0) |
403 | { |
415 | { |
404 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
416 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
405 | |
417 | |
406 | if (CxTYPE (cx) == CXt_SUB) |
418 | if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT) |
407 | { |
419 | { |
408 | CV *cv = cx->blk_sub.cv; |
420 | CV *cv = cx->blk_sub.cv; |
409 | |
421 | |
410 | if (CvDEPTH (cv)) |
422 | if (CvDEPTH (cv)) |
411 | { |
423 | { |
… | |
… | |
422 | |
434 | |
423 | if (top_si->si_type == PERLSI_MAIN) |
435 | if (top_si->si_type == PERLSI_MAIN) |
424 | break; |
436 | break; |
425 | |
437 | |
426 | top_si = top_si->si_prev; |
438 | top_si = top_si->si_prev; |
427 | ccstk = top_si->si_cxstack; |
439 | ccstk = top_si->si_cxstack; |
428 | cxix = top_si->si_cxix; |
440 | cxix = top_si->si_cxix; |
429 | } |
441 | } |
430 | |
442 | |
431 | PUTBACK; |
443 | PUTBACK; |
432 | } |
444 | } |
433 | |
445 | |
… | |
… | |
452 | # define coro_init_stacks init_stacks |
464 | # define coro_init_stacks init_stacks |
453 | #else |
465 | #else |
454 | static void |
466 | static void |
455 | coro_init_stacks (pTHX) |
467 | coro_init_stacks (pTHX) |
456 | { |
468 | { |
457 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
469 | PL_curstackinfo = new_stackinfo(64, 6); |
458 | PL_curstackinfo->si_type = PERLSI_MAIN; |
470 | PL_curstackinfo->si_type = PERLSI_MAIN; |
459 | PL_curstack = PL_curstackinfo->si_stack; |
471 | PL_curstack = PL_curstackinfo->si_stack; |
460 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
472 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
461 | |
473 | |
462 | PL_stack_base = AvARRAY(PL_curstack); |
474 | PL_stack_base = AvARRAY(PL_curstack); |
463 | PL_stack_sp = PL_stack_base; |
475 | PL_stack_sp = PL_stack_base; |
464 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
476 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
465 | |
477 | |
466 | New(50,PL_tmps_stack,128,SV*); |
478 | New(50,PL_tmps_stack,64,SV*); |
467 | PL_tmps_floor = -1; |
479 | PL_tmps_floor = -1; |
468 | PL_tmps_ix = -1; |
480 | PL_tmps_ix = -1; |
469 | PL_tmps_max = 128; |
481 | PL_tmps_max = 64; |
470 | |
482 | |
471 | New(54,PL_markstack,32,I32); |
483 | New(54,PL_markstack,16,I32); |
472 | PL_markstack_ptr = PL_markstack; |
484 | PL_markstack_ptr = PL_markstack; |
473 | PL_markstack_max = PL_markstack + 32; |
485 | PL_markstack_max = PL_markstack + 16; |
474 | |
486 | |
475 | #ifdef SET_MARK_OFFSET |
487 | #ifdef SET_MARK_OFFSET |
476 | SET_MARK_OFFSET; |
488 | SET_MARK_OFFSET; |
477 | #endif |
489 | #endif |
478 | |
490 | |
479 | New(54,PL_scopestack,32,I32); |
491 | New(54,PL_scopestack,16,I32); |
480 | PL_scopestack_ix = 0; |
492 | PL_scopestack_ix = 0; |
481 | PL_scopestack_max = 32; |
493 | PL_scopestack_max = 16; |
482 | |
494 | |
483 | New(54,PL_savestack,64,ANY); |
495 | New(54,PL_savestack,64,ANY); |
484 | PL_savestack_ix = 0; |
496 | PL_savestack_ix = 0; |
485 | PL_savestack_max = 64; |
497 | PL_savestack_max = 64; |
486 | |
498 | |
487 | #if !PERL_VERSION_ATLEAST (5,9,0) |
499 | #if !PERL_VERSION_ATLEAST (5,9,0) |
488 | New(54,PL_retstack,16,OP*); |
500 | New(54,PL_retstack,4,OP*); |
489 | PL_retstack_ix = 0; |
501 | PL_retstack_ix = 0; |
490 | PL_retstack_max = 16; |
502 | PL_retstack_max = 4; |
491 | #endif |
503 | #endif |
492 | } |
504 | } |
493 | #endif |
505 | #endif |
494 | |
506 | |
495 | /* |
507 | /* |
… | |
… | |
535 | Safefree (PL_scopestack); |
547 | Safefree (PL_scopestack); |
536 | Safefree (PL_savestack); |
548 | Safefree (PL_savestack); |
537 | #if !PERL_VERSION_ATLEAST (5,9,0) |
549 | #if !PERL_VERSION_ATLEAST (5,9,0) |
538 | Safefree (PL_retstack); |
550 | Safefree (PL_retstack); |
539 | #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; |
540 | } |
583 | } |
541 | |
584 | |
542 | /** coroutine stack handling ************************************************/ |
585 | /** coroutine stack handling ************************************************/ |
543 | |
586 | |
544 | static void |
587 | static void |
… | |
… | |
587 | SvREFCNT_dec (coro_mortal); |
630 | SvREFCNT_dec (coro_mortal); |
588 | coro_mortal = 0; |
631 | coro_mortal = 0; |
589 | } |
632 | } |
590 | } |
633 | } |
591 | |
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 | |
592 | /* 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 */ |
593 | /* _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 */ |
594 | /* during execution of a perl program */ |
678 | /* during execution of a perl program */ |
595 | static void NOINLINE |
679 | static void NOINLINE |
596 | prepare_cctx (pTHX_ coro_cctx *cctx) |
680 | prepare_cctx (pTHX_ coro_cctx *cctx) |
597 | { |
681 | { |
598 | dSP; |
682 | dSP; |
599 | 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; |
600 | |
689 | |
601 | Zero (&myop, 1, LOGOP); |
690 | Zero (&myop, 1, LOGOP); |
602 | myop.op_next = PL_op; |
691 | myop.op_next = PL_op; |
603 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
692 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
604 | |
693 | |
… | |
… | |
621 | dTHX; |
710 | dTHX; |
622 | |
711 | |
623 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
712 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
624 | UNLOCK; |
713 | UNLOCK; |
625 | |
714 | |
626 | PL_top_env = &PL_start_env; |
715 | /* we now skip the entersub that lead to transfer() */ |
|
|
716 | PL_op = PL_op->op_next; |
627 | |
717 | |
628 | /* inject a fake subroutine call to cctx_init */ |
718 | /* inject a fake subroutine call to cctx_init */ |
629 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
719 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
630 | |
720 | |
631 | /* 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 */ |
… | |
… | |
665 | # if CORO_STACKGUARD |
755 | # if CORO_STACKGUARD |
666 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
756 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
667 | # endif |
757 | # endif |
668 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
758 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
669 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
759 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
670 | cctx->mapped = 1; |
760 | cctx->flags |= CC_MAPPED; |
671 | } |
761 | } |
672 | else |
762 | else |
673 | #endif |
763 | #endif |
674 | { |
764 | { |
675 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
765 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
… | |
… | |
702 | #if CORO_USE_VALGRIND |
792 | #if CORO_USE_VALGRIND |
703 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
793 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
704 | #endif |
794 | #endif |
705 | |
795 | |
706 | #if HAVE_MMAP |
796 | #if HAVE_MMAP |
707 | if (cctx->mapped) |
797 | if (cctx->flags & CC_MAPPED) |
708 | munmap (cctx->sptr, cctx->ssize); |
798 | munmap (cctx->sptr, cctx->ssize); |
709 | else |
799 | else |
710 | #endif |
800 | #endif |
711 | Safefree (cctx->sptr); |
801 | Safefree (cctx->sptr); |
712 | |
802 | |
… | |
… | |
720 | { |
810 | { |
721 | coro_cctx *cctx = cctx_first; |
811 | coro_cctx *cctx = cctx_first; |
722 | cctx_first = cctx->next; |
812 | cctx_first = cctx->next; |
723 | --cctx_idle; |
813 | --cctx_idle; |
724 | |
814 | |
725 | if (cctx->ssize >= coro_stacksize) |
815 | if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE)) |
726 | return cctx; |
816 | return cctx; |
727 | |
817 | |
728 | cctx_destroy (cctx); |
818 | cctx_destroy (cctx); |
729 | } |
819 | } |
730 | |
820 | |
731 | PL_op = PL_op->op_next; |
|
|
732 | return cctx_new (); |
821 | return cctx_new (); |
733 | } |
822 | } |
734 | |
823 | |
735 | static void |
824 | static void |
736 | cctx_put (coro_cctx *cctx) |
825 | cctx_put (coro_cctx *cctx) |
… | |
… | |
740 | { |
829 | { |
741 | coro_cctx *first = cctx_first; |
830 | coro_cctx *first = cctx_first; |
742 | cctx_first = first->next; |
831 | cctx_first = first->next; |
743 | --cctx_idle; |
832 | --cctx_idle; |
744 | |
833 | |
745 | assert (!first->inuse); |
|
|
746 | cctx_destroy (first); |
834 | cctx_destroy (first); |
747 | } |
835 | } |
748 | |
836 | |
749 | ++cctx_idle; |
837 | ++cctx_idle; |
750 | cctx->next = cctx_first; |
838 | cctx->next = cctx_first; |
… | |
… | |
771 | |
859 | |
772 | if (prev->flags & CF_NEW) |
860 | if (prev->flags & CF_NEW) |
773 | { |
861 | { |
774 | /* create a new empty context */ |
862 | /* create a new empty context */ |
775 | Newz (0, prev->cctx, 1, coro_cctx); |
863 | Newz (0, prev->cctx, 1, coro_cctx); |
776 | prev->cctx->inuse = 1; |
|
|
777 | prev->flags &= ~CF_NEW; |
864 | prev->flags &= ~CF_NEW; |
778 | prev->flags |= CF_RUNNING; |
865 | prev->flags |= CF_RUNNING; |
779 | } |
866 | } |
780 | |
867 | |
781 | /*TODO: must not croak here */ |
868 | /*TODO: must not croak here */ |
… | |
… | |
799 | next->flags &= ~CF_NEW; |
886 | next->flags &= ~CF_NEW; |
800 | /* first get rid of the old state */ |
887 | /* first get rid of the old state */ |
801 | save_perl (aTHX_ prev); |
888 | save_perl (aTHX_ prev); |
802 | /* setup coroutine call */ |
889 | /* setup coroutine call */ |
803 | setup_coro (aTHX_ next); |
890 | setup_coro (aTHX_ next); |
804 | /* need a new stack */ |
|
|
805 | assert (!next->cctx); |
|
|
806 | } |
891 | } |
807 | else |
892 | else |
808 | { |
893 | { |
809 | /* coroutine already started */ |
894 | /* coroutine already started */ |
810 | save_perl (aTHX_ prev); |
895 | save_perl (aTHX_ prev); |
… | |
… | |
820 | 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)); |
821 | |
906 | |
822 | prev->cctx = 0; |
907 | prev->cctx = 0; |
823 | |
908 | |
824 | cctx_put (prev__cctx); |
909 | cctx_put (prev__cctx); |
825 | prev__cctx->inuse = 0; |
|
|
826 | } |
910 | } |
827 | |
911 | |
828 | if (!next->cctx) |
912 | if (!next->cctx) |
829 | { |
|
|
830 | next->cctx = cctx_get (aTHX); |
913 | next->cctx = cctx_get (aTHX); |
831 | assert (!next->cctx->inuse); |
|
|
832 | next->cctx->inuse = 1; |
|
|
833 | } |
|
|
834 | |
914 | |
835 | if (prev__cctx != next->cctx) |
915 | if (prev__cctx != next->cctx) |
836 | { |
916 | { |
837 | prev__cctx->top_env = PL_top_env; |
917 | prev__cctx->top_env = PL_top_env; |
838 | PL_top_env = next->cctx->top_env; |
918 | PL_top_env = next->cctx->top_env; |
… | |
… | |
877 | struct coro temp; |
957 | struct coro temp; |
878 | |
958 | |
879 | assert (!(coro->flags & CF_RUNNING)); |
959 | assert (!(coro->flags & CF_RUNNING)); |
880 | |
960 | |
881 | Zero (&temp, 1, struct coro); |
961 | Zero (&temp, 1, struct coro); |
882 | temp.save = CORO_SAVE_DEF; |
962 | temp.save = CORO_SAVE_ALL; |
883 | |
963 | |
884 | if (coro->flags & CF_RUNNING) |
964 | if (coro->flags & CF_RUNNING) |
885 | croak ("FATAL: tried to destroy currently running coroutine"); |
965 | croak ("FATAL: tried to destroy currently running coroutine"); |
886 | |
966 | |
887 | save_perl (aTHX_ &temp); |
967 | save_perl (aTHX_ &temp); |
… | |
… | |
895 | } |
975 | } |
896 | |
976 | |
897 | cctx_destroy (coro->cctx); |
977 | cctx_destroy (coro->cctx); |
898 | SvREFCNT_dec (coro->args); |
978 | SvREFCNT_dec (coro->args); |
899 | |
979 | |
|
|
980 | if (coro->next) coro->next->prev = coro->prev; |
|
|
981 | if (coro->prev) coro->prev->next = coro->next; |
|
|
982 | if (coro == coro_first) coro_first = coro->next; |
|
|
983 | |
900 | return 1; |
984 | return 1; |
901 | } |
985 | } |
902 | |
986 | |
903 | static int |
987 | static int |
904 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
988 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
905 | { |
989 | { |
906 | struct coro *coro = (struct coro *)mg->mg_ptr; |
990 | struct coro *coro = (struct coro *)mg->mg_ptr; |
907 | mg->mg_ptr = 0; |
991 | mg->mg_ptr = 0; |
|
|
992 | |
|
|
993 | coro->hv = 0; |
908 | |
994 | |
909 | if (--coro->refcnt < 0) |
995 | if (--coro->refcnt < 0) |
910 | { |
996 | { |
911 | coro_state_destroy (aTHX_ coro); |
997 | coro_state_destroy (aTHX_ coro); |
912 | Safefree (coro); |
998 | Safefree (coro); |
… | |
… | |
1209 | Newz (0, coro, 1, struct coro); |
1295 | Newz (0, coro, 1, struct coro); |
1210 | coro->args = newAV (); |
1296 | coro->args = newAV (); |
1211 | coro->save = CORO_SAVE_DEF; |
1297 | coro->save = CORO_SAVE_DEF; |
1212 | coro->flags = CF_NEW; |
1298 | coro->flags = CF_NEW; |
1213 | |
1299 | |
|
|
1300 | if (coro_first) coro_first->prev = coro; |
|
|
1301 | coro->next = coro_first; |
|
|
1302 | coro_first = coro; |
|
|
1303 | |
1214 | hv = newHV (); |
1304 | coro->hv = hv = newHV (); |
1215 | 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; |
1216 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1306 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1217 | |
1307 | |
1218 | for (i = 1; i < items; i++) |
1308 | for (i = 1; i < items; i++) |
1219 | av_push (coro->args, newSVsv (ST (i))); |
1309 | av_push (coro->args, newSVsv (ST (i))); |
… | |
… | |
1321 | CODE: |
1411 | CODE: |
1322 | RETVAL = cctx_idle; |
1412 | RETVAL = cctx_idle; |
1323 | OUTPUT: |
1413 | OUTPUT: |
1324 | RETVAL |
1414 | RETVAL |
1325 | |
1415 | |
|
|
1416 | void |
|
|
1417 | list () |
|
|
1418 | PPCODE: |
|
|
1419 | { |
|
|
1420 | struct coro *coro; |
|
|
1421 | for (coro = coro_first; coro; coro = coro->next) |
|
|
1422 | if (coro->hv) |
|
|
1423 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
|
|
1424 | } |
|
|
1425 | |
|
|
1426 | void |
|
|
1427 | call (Coro::State coro, SV *coderef) |
|
|
1428 | ALIAS: |
|
|
1429 | eval = 1 |
|
|
1430 | CODE: |
|
|
1431 | { |
|
|
1432 | if (coro->mainstack) |
|
|
1433 | { |
|
|
1434 | struct coro temp; |
|
|
1435 | Zero (&temp, 1, struct coro); |
|
|
1436 | temp.save = CORO_SAVE_ALL; |
|
|
1437 | |
|
|
1438 | if (!(coro->flags & CF_RUNNING)) |
|
|
1439 | { |
|
|
1440 | save_perl (aTHX_ &temp); |
|
|
1441 | load_perl (aTHX_ coro); |
|
|
1442 | } |
|
|
1443 | |
|
|
1444 | { |
|
|
1445 | dSP; |
|
|
1446 | ENTER; |
|
|
1447 | SAVETMPS; |
|
|
1448 | PUSHMARK (SP); |
|
|
1449 | PUTBACK; |
|
|
1450 | if (ix) |
|
|
1451 | eval_sv (coderef, 0); |
|
|
1452 | else |
|
|
1453 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
1454 | SPAGAIN; |
|
|
1455 | FREETMPS; |
|
|
1456 | LEAVE; |
|
|
1457 | PUTBACK; |
|
|
1458 | } |
|
|
1459 | |
|
|
1460 | if (!(coro->flags & CF_RUNNING)) |
|
|
1461 | { |
|
|
1462 | save_perl (aTHX_ coro); |
|
|
1463 | load_perl (aTHX_ &temp); |
|
|
1464 | } |
|
|
1465 | } |
|
|
1466 | } |
|
|
1467 | |
|
|
1468 | SV * |
|
|
1469 | is_ready (Coro::State coro) |
|
|
1470 | PROTOTYPE: $ |
|
|
1471 | ALIAS: |
|
|
1472 | is_ready = CF_READY |
|
|
1473 | is_running = CF_RUNNING |
|
|
1474 | is_new = CF_NEW |
|
|
1475 | is_destroyed = CF_DESTROYED |
|
|
1476 | CODE: |
|
|
1477 | RETVAL = boolSV (coro->flags & ix); |
|
|
1478 | OUTPUT: |
|
|
1479 | RETVAL |
|
|
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 | |
|
|
1518 | |
1326 | MODULE = Coro::State PACKAGE = Coro |
1519 | MODULE = Coro::State PACKAGE = Coro |
1327 | |
1520 | |
1328 | BOOT: |
1521 | BOOT: |
1329 | { |
1522 | { |
1330 | 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); |
1331 | |
1531 | |
1332 | coro_stash = gv_stashpv ("Coro", TRUE); |
1532 | coro_stash = gv_stashpv ("Coro", TRUE); |
1333 | |
1533 | |
1334 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1534 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1335 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1535 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1336 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1536 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1337 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1537 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1338 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1538 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1339 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1539 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1340 | |
|
|
1341 | coro_current = get_sv ("Coro::current", FALSE); |
|
|
1342 | SvREADONLY_on (coro_current); |
|
|
1343 | |
1540 | |
1344 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1541 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1345 | coro_ready[i] = newAV (); |
1542 | coro_ready[i] = newAV (); |
1346 | |
1543 | |
1347 | { |
1544 | { |
… | |
… | |
1397 | CODE: |
1594 | CODE: |
1398 | RETVAL = boolSV (api_ready (self)); |
1595 | RETVAL = boolSV (api_ready (self)); |
1399 | OUTPUT: |
1596 | OUTPUT: |
1400 | RETVAL |
1597 | RETVAL |
1401 | |
1598 | |
1402 | SV * |
|
|
1403 | is_ready (SV *self) |
|
|
1404 | PROTOTYPE: $ |
|
|
1405 | CODE: |
|
|
1406 | RETVAL = boolSV (api_is_ready (self)); |
|
|
1407 | OUTPUT: |
|
|
1408 | RETVAL |
|
|
1409 | |
|
|
1410 | int |
1599 | int |
1411 | nready (...) |
1600 | nready (...) |
1412 | PROTOTYPE: |
1601 | PROTOTYPE: |
1413 | CODE: |
1602 | CODE: |
1414 | RETVAL = coro_nready; |
1603 | RETVAL = coro_nready; |
1415 | OUTPUT: |
1604 | OUTPUT: |
1416 | RETVAL |
1605 | RETVAL |
1417 | |
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 | |
1418 | MODULE = Coro::State PACKAGE = Coro::AIO |
1660 | MODULE = Coro::State PACKAGE = Coro::AIO |
1419 | |
1661 | |
1420 | SV * |
1662 | SV * |
1421 | _get_state () |
1663 | _get_state () |
1422 | CODE: |
1664 | CODE: |