… | |
… | |
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 */ |
… | |
… | |
210 | |
223 | |
211 | /* for Coro.pm */ |
224 | /* for Coro.pm */ |
212 | static SV *coro_current; |
225 | static SV *coro_current; |
213 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
226 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
214 | static int coro_nready; |
227 | static int coro_nready; |
215 | static struct coro *first; |
228 | static struct coro *coro_first; |
216 | |
229 | |
217 | /** lowlevel stuff **********************************************************/ |
230 | /** lowlevel stuff **********************************************************/ |
218 | |
231 | |
219 | static AV * |
232 | static AV * |
220 | coro_clone_padlist (pTHX_ CV *cv) |
233 | coro_clone_padlist (pTHX_ CV *cv) |
… | |
… | |
285 | SvMAGIC (cv) \ |
298 | SvMAGIC (cv) \ |
286 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
299 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
287 | ? SvMAGIC (cv) \ |
300 | ? SvMAGIC (cv) \ |
288 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
301 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
289 | : 0 |
302 | : 0 |
|
|
303 | |
|
|
304 | static struct coro * |
|
|
305 | SvSTATE_ (pTHX_ SV *coro) |
|
|
306 | { |
|
|
307 | HV *stash; |
|
|
308 | MAGIC *mg; |
|
|
309 | |
|
|
310 | if (SvROK (coro)) |
|
|
311 | coro = SvRV (coro); |
|
|
312 | |
|
|
313 | stash = SvSTASH (coro); |
|
|
314 | if (stash != coro_stash && stash != coro_state_stash) |
|
|
315 | { |
|
|
316 | /* very slow, but rare, check */ |
|
|
317 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
318 | croak ("Coro::State object required"); |
|
|
319 | } |
|
|
320 | |
|
|
321 | mg = CORO_MAGIC (coro); |
|
|
322 | return (struct coro *)mg->mg_ptr; |
|
|
323 | } |
|
|
324 | |
|
|
325 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
290 | |
326 | |
291 | /* the next two functions merely cache the padlists */ |
327 | /* the next two functions merely cache the padlists */ |
292 | static void |
328 | static void |
293 | get_padlist (pTHX_ CV *cv) |
329 | get_padlist (pTHX_ CV *cv) |
294 | { |
330 | { |
… | |
… | |
395 | /* |
431 | /* |
396 | * the worst thing you can imagine happens first - we have to save |
432 | * the worst thing you can imagine happens first - we have to save |
397 | * (and reinitialize) all cv's in the whole callchain :( |
433 | * (and reinitialize) all cv's in the whole callchain :( |
398 | */ |
434 | */ |
399 | |
435 | |
400 | EXTEND (SP, 3 + 1); |
|
|
401 | PUSHs (Nullsv); |
436 | XPUSHs (Nullsv); |
402 | /* this loop was inspired by pp_caller */ |
437 | /* this loop was inspired by pp_caller */ |
403 | for (;;) |
438 | for (;;) |
404 | { |
439 | { |
405 | while (cxix >= 0) |
440 | while (cxix >= 0) |
406 | { |
441 | { |
… | |
… | |
425 | |
460 | |
426 | if (top_si->si_type == PERLSI_MAIN) |
461 | if (top_si->si_type == PERLSI_MAIN) |
427 | break; |
462 | break; |
428 | |
463 | |
429 | top_si = top_si->si_prev; |
464 | top_si = top_si->si_prev; |
430 | ccstk = top_si->si_cxstack; |
465 | ccstk = top_si->si_cxstack; |
431 | cxix = top_si->si_cxix; |
466 | cxix = top_si->si_cxix; |
432 | } |
467 | } |
433 | |
468 | |
434 | PUTBACK; |
469 | PUTBACK; |
435 | } |
470 | } |
436 | |
471 | |
… | |
… | |
455 | # define coro_init_stacks init_stacks |
490 | # define coro_init_stacks init_stacks |
456 | #else |
491 | #else |
457 | static void |
492 | static void |
458 | coro_init_stacks (pTHX) |
493 | coro_init_stacks (pTHX) |
459 | { |
494 | { |
460 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
495 | PL_curstackinfo = new_stackinfo(64, 6); |
461 | PL_curstackinfo->si_type = PERLSI_MAIN; |
496 | PL_curstackinfo->si_type = PERLSI_MAIN; |
462 | PL_curstack = PL_curstackinfo->si_stack; |
497 | PL_curstack = PL_curstackinfo->si_stack; |
463 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
498 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
464 | |
499 | |
465 | PL_stack_base = AvARRAY(PL_curstack); |
500 | PL_stack_base = AvARRAY(PL_curstack); |
466 | PL_stack_sp = PL_stack_base; |
501 | PL_stack_sp = PL_stack_base; |
467 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
502 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
468 | |
503 | |
469 | New(50,PL_tmps_stack,128,SV*); |
504 | New(50,PL_tmps_stack,64,SV*); |
470 | PL_tmps_floor = -1; |
505 | PL_tmps_floor = -1; |
471 | PL_tmps_ix = -1; |
506 | PL_tmps_ix = -1; |
472 | PL_tmps_max = 128; |
507 | PL_tmps_max = 64; |
473 | |
508 | |
474 | New(54,PL_markstack,32,I32); |
509 | New(54,PL_markstack,16,I32); |
475 | PL_markstack_ptr = PL_markstack; |
510 | PL_markstack_ptr = PL_markstack; |
476 | PL_markstack_max = PL_markstack + 32; |
511 | PL_markstack_max = PL_markstack + 16; |
477 | |
512 | |
478 | #ifdef SET_MARK_OFFSET |
513 | #ifdef SET_MARK_OFFSET |
479 | SET_MARK_OFFSET; |
514 | SET_MARK_OFFSET; |
480 | #endif |
515 | #endif |
481 | |
516 | |
482 | New(54,PL_scopestack,32,I32); |
517 | New(54,PL_scopestack,16,I32); |
483 | PL_scopestack_ix = 0; |
518 | PL_scopestack_ix = 0; |
484 | PL_scopestack_max = 32; |
519 | PL_scopestack_max = 16; |
485 | |
520 | |
486 | New(54,PL_savestack,64,ANY); |
521 | New(54,PL_savestack,64,ANY); |
487 | PL_savestack_ix = 0; |
522 | PL_savestack_ix = 0; |
488 | PL_savestack_max = 64; |
523 | PL_savestack_max = 64; |
489 | |
524 | |
490 | #if !PERL_VERSION_ATLEAST (5,9,0) |
525 | #if !PERL_VERSION_ATLEAST (5,9,0) |
491 | New(54,PL_retstack,16,OP*); |
526 | New(54,PL_retstack,4,OP*); |
492 | PL_retstack_ix = 0; |
527 | PL_retstack_ix = 0; |
493 | PL_retstack_max = 16; |
528 | PL_retstack_max = 4; |
494 | #endif |
529 | #endif |
495 | } |
530 | } |
496 | #endif |
531 | #endif |
497 | |
532 | |
498 | /* |
533 | /* |
… | |
… | |
540 | #if !PERL_VERSION_ATLEAST (5,9,0) |
575 | #if !PERL_VERSION_ATLEAST (5,9,0) |
541 | Safefree (PL_retstack); |
576 | Safefree (PL_retstack); |
542 | #endif |
577 | #endif |
543 | } |
578 | } |
544 | |
579 | |
|
|
580 | static size_t |
|
|
581 | coro_rss (struct coro *coro) |
|
|
582 | { |
|
|
583 | size_t rss = sizeof (coro); |
|
|
584 | |
|
|
585 | if (coro->mainstack) |
|
|
586 | { |
|
|
587 | if (coro->flags & CF_RUNNING) |
|
|
588 | { |
|
|
589 | #define VAR(name,type)coro->name = PL_ ## name; |
|
|
590 | # include "state.h" |
|
|
591 | #undef VAR |
|
|
592 | } |
|
|
593 | |
|
|
594 | rss += sizeof (coro->curstackinfo); |
|
|
595 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); |
|
|
596 | rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
|
|
597 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); |
|
|
598 | rss += coro->tmps_max * sizeof (SV *); |
|
|
599 | rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); |
|
|
600 | rss += coro->scopestack_max * sizeof (I32); |
|
|
601 | rss += coro->savestack_max * sizeof (ANY); |
|
|
602 | |
|
|
603 | #if !PERL_VERSION_ATLEAST (5,9,0) |
|
|
604 | rss += coro->retstack_max * sizeof (OP *); |
|
|
605 | #endif |
|
|
606 | } |
|
|
607 | |
|
|
608 | return rss; |
|
|
609 | } |
|
|
610 | |
545 | /** coroutine stack handling ************************************************/ |
611 | /** coroutine stack handling ************************************************/ |
546 | |
612 | |
547 | static void |
613 | static void |
548 | setup_coro (pTHX_ struct coro *coro) |
614 | setup_coro (pTHX_ struct coro *coro) |
549 | { |
615 | { |
550 | /* |
616 | /* |
551 | * emulate part of the perl startup here. |
617 | * emulate part of the perl startup here. |
552 | */ |
618 | */ |
553 | coro_init_stacks (aTHX); |
619 | coro_init_stacks (aTHX); |
554 | |
620 | |
|
|
621 | PL_runops = RUNOPS_DEFAULT; |
555 | PL_curcop = &PL_compiling; |
622 | PL_curcop = &PL_compiling; |
556 | PL_in_eval = EVAL_NULL; |
623 | PL_in_eval = EVAL_NULL; |
557 | PL_comppad = 0; |
624 | PL_comppad = 0; |
558 | PL_curpm = 0; |
625 | PL_curpm = 0; |
559 | PL_localizing = 0; |
626 | PL_localizing = 0; |
… | |
… | |
590 | SvREFCNT_dec (coro_mortal); |
657 | SvREFCNT_dec (coro_mortal); |
591 | coro_mortal = 0; |
658 | coro_mortal = 0; |
592 | } |
659 | } |
593 | } |
660 | } |
594 | |
661 | |
|
|
662 | static int |
|
|
663 | runops_trace (pTHX) |
|
|
664 | { |
|
|
665 | COP *oldcop = 0; |
|
|
666 | int oldcxix = -2; |
|
|
667 | struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */ |
|
|
668 | coro_cctx *cctx = coro->cctx; |
|
|
669 | |
|
|
670 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
|
|
671 | { |
|
|
672 | PERL_ASYNC_CHECK (); |
|
|
673 | |
|
|
674 | if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) |
|
|
675 | { |
|
|
676 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
|
|
677 | SV **bot, **top; |
|
|
678 | AV *av = newAV (); /* return values */ |
|
|
679 | SV **cb; |
|
|
680 | runops_proc_t old_runops = PL_runops; |
|
|
681 | dSP; |
|
|
682 | ENTER; |
|
|
683 | SAVETMPS; |
|
|
684 | EXTEND (SP, 3); |
|
|
685 | PL_runops = RUNOPS_DEFAULT; |
|
|
686 | |
|
|
687 | GV *gv = CvGV (cx->blk_sub.cv); |
|
|
688 | SV *fullname = sv_2mortal (newSV (0)); |
|
|
689 | if (isGV (gv)) |
|
|
690 | gv_efullname3 (fullname, gv, 0); |
|
|
691 | |
|
|
692 | bot = PL_stack_base + cx->blk_oldsp + 1; |
|
|
693 | top = cx->blk_gimme == G_ARRAY ? SP + 1 |
|
|
694 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
|
|
695 | : bot; |
|
|
696 | |
|
|
697 | while (bot < top) |
|
|
698 | av_push (av, SvREFCNT_inc (*bot++)); |
|
|
699 | |
|
|
700 | PUSHMARK (SP); |
|
|
701 | PUSHs (&PL_sv_no); |
|
|
702 | PUSHs (fullname); |
|
|
703 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
704 | PUTBACK; |
|
|
705 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
|
|
706 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
707 | SPAGAIN; |
|
|
708 | |
|
|
709 | FREETMPS; |
|
|
710 | LEAVE; |
|
|
711 | PL_runops = old_runops; |
|
|
712 | } |
|
|
713 | |
|
|
714 | if (oldcop != PL_curcop) |
|
|
715 | { |
|
|
716 | oldcop = PL_curcop; |
|
|
717 | |
|
|
718 | if (PL_curcop != &PL_compiling) |
|
|
719 | { |
|
|
720 | SV **cb; |
|
|
721 | runops_proc_t old_runops = PL_runops; |
|
|
722 | dSP; |
|
|
723 | ENTER; |
|
|
724 | SAVETMPS; |
|
|
725 | EXTEND (SP, 3); |
|
|
726 | PL_runops = RUNOPS_DEFAULT; |
|
|
727 | |
|
|
728 | if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) |
|
|
729 | { |
|
|
730 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
|
|
731 | |
|
|
732 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
|
|
733 | { |
|
|
734 | GV *gv = CvGV (cx->blk_sub.cv); |
|
|
735 | SV *fullname = sv_2mortal (newSV (0)); |
|
|
736 | if (isGV (gv)) |
|
|
737 | gv_efullname3 (fullname, gv, 0); |
|
|
738 | |
|
|
739 | PUSHMARK (SP); |
|
|
740 | PUSHs (&PL_sv_yes); |
|
|
741 | PUSHs (fullname); |
|
|
742 | PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
|
|
743 | PUTBACK; |
|
|
744 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
|
|
745 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
746 | SPAGAIN; |
|
|
747 | } |
|
|
748 | |
|
|
749 | oldcxix = cxstack_ix; |
|
|
750 | } |
|
|
751 | |
|
|
752 | if (cctx->flags & CC_TRACE_LINE) |
|
|
753 | { |
|
|
754 | PUSHMARK (SP); |
|
|
755 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
|
|
756 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
|
|
757 | PUTBACK; |
|
|
758 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0); |
|
|
759 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
760 | SPAGAIN; |
|
|
761 | } |
|
|
762 | |
|
|
763 | FREETMPS; |
|
|
764 | LEAVE; |
|
|
765 | PL_runops = old_runops; |
|
|
766 | } |
|
|
767 | } |
|
|
768 | } |
|
|
769 | |
|
|
770 | TAINT_NOT; |
|
|
771 | return 0; |
|
|
772 | } |
|
|
773 | |
595 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
774 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
596 | /* _cctx_init should be careful, as it could be called at almost any time */ |
775 | /* _cctx_init should be careful, as it could be called at almost any time */ |
597 | /* during execution of a perl program */ |
776 | /* during execution of a perl program */ |
598 | static void NOINLINE |
777 | static void NOINLINE |
599 | prepare_cctx (pTHX_ coro_cctx *cctx) |
778 | prepare_cctx (pTHX_ coro_cctx *cctx) |
600 | { |
779 | { |
601 | dSP; |
780 | dSP; |
602 | LOGOP myop; |
781 | LOGOP myop; |
|
|
782 | |
|
|
783 | PL_top_env = &PL_start_env; |
|
|
784 | |
|
|
785 | if (cctx->flags & CC_TRACE) |
|
|
786 | PL_runops = runops_trace; |
603 | |
787 | |
604 | Zero (&myop, 1, LOGOP); |
788 | Zero (&myop, 1, LOGOP); |
605 | myop.op_next = PL_op; |
789 | myop.op_next = PL_op; |
606 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
790 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
607 | |
791 | |
… | |
… | |
624 | dTHX; |
808 | dTHX; |
625 | |
809 | |
626 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
810 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
627 | UNLOCK; |
811 | UNLOCK; |
628 | |
812 | |
629 | PL_top_env = &PL_start_env; |
813 | /* we now skip the entersub that lead to transfer() */ |
|
|
814 | PL_op = PL_op->op_next; |
630 | |
815 | |
631 | /* inject a fake subroutine call to cctx_init */ |
816 | /* inject a fake subroutine call to cctx_init */ |
632 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
817 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
633 | |
818 | |
634 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
819 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
… | |
… | |
668 | # if CORO_STACKGUARD |
853 | # if CORO_STACKGUARD |
669 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
854 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
670 | # endif |
855 | # endif |
671 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
856 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
672 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
857 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
673 | cctx->mapped = 1; |
858 | cctx->flags |= CC_MAPPED; |
674 | } |
859 | } |
675 | else |
860 | else |
676 | #endif |
861 | #endif |
677 | { |
862 | { |
678 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
863 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
… | |
… | |
705 | #if CORO_USE_VALGRIND |
890 | #if CORO_USE_VALGRIND |
706 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
891 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
707 | #endif |
892 | #endif |
708 | |
893 | |
709 | #if HAVE_MMAP |
894 | #if HAVE_MMAP |
710 | if (cctx->mapped) |
895 | if (cctx->flags & CC_MAPPED) |
711 | munmap (cctx->sptr, cctx->ssize); |
896 | munmap (cctx->sptr, cctx->ssize); |
712 | else |
897 | else |
713 | #endif |
898 | #endif |
714 | Safefree (cctx->sptr); |
899 | Safefree (cctx->sptr); |
715 | |
900 | |
… | |
… | |
723 | { |
908 | { |
724 | coro_cctx *cctx = cctx_first; |
909 | coro_cctx *cctx = cctx_first; |
725 | cctx_first = cctx->next; |
910 | cctx_first = cctx->next; |
726 | --cctx_idle; |
911 | --cctx_idle; |
727 | |
912 | |
728 | if (cctx->ssize >= coro_stacksize) |
913 | if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE)) |
729 | return cctx; |
914 | return cctx; |
730 | |
915 | |
731 | cctx_destroy (cctx); |
916 | cctx_destroy (cctx); |
732 | } |
917 | } |
733 | |
918 | |
734 | PL_op = PL_op->op_next; |
|
|
735 | return cctx_new (); |
919 | return cctx_new (); |
736 | } |
920 | } |
737 | |
921 | |
738 | static void |
922 | static void |
739 | cctx_put (coro_cctx *cctx) |
923 | cctx_put (coro_cctx *cctx) |
… | |
… | |
743 | { |
927 | { |
744 | coro_cctx *first = cctx_first; |
928 | coro_cctx *first = cctx_first; |
745 | cctx_first = first->next; |
929 | cctx_first = first->next; |
746 | --cctx_idle; |
930 | --cctx_idle; |
747 | |
931 | |
748 | assert (!first->inuse); |
|
|
749 | cctx_destroy (first); |
932 | cctx_destroy (first); |
750 | } |
933 | } |
751 | |
934 | |
752 | ++cctx_idle; |
935 | ++cctx_idle; |
753 | cctx->next = cctx_first; |
936 | cctx->next = cctx_first; |
… | |
… | |
774 | |
957 | |
775 | if (prev->flags & CF_NEW) |
958 | if (prev->flags & CF_NEW) |
776 | { |
959 | { |
777 | /* create a new empty context */ |
960 | /* create a new empty context */ |
778 | Newz (0, prev->cctx, 1, coro_cctx); |
961 | Newz (0, prev->cctx, 1, coro_cctx); |
779 | prev->cctx->inuse = 1; |
|
|
780 | prev->flags &= ~CF_NEW; |
962 | prev->flags &= ~CF_NEW; |
781 | prev->flags |= CF_RUNNING; |
963 | prev->flags |= CF_RUNNING; |
782 | } |
964 | } |
783 | |
965 | |
784 | /*TODO: must not croak here */ |
966 | /*TODO: must not croak here */ |
… | |
… | |
802 | next->flags &= ~CF_NEW; |
984 | next->flags &= ~CF_NEW; |
803 | /* first get rid of the old state */ |
985 | /* first get rid of the old state */ |
804 | save_perl (aTHX_ prev); |
986 | save_perl (aTHX_ prev); |
805 | /* setup coroutine call */ |
987 | /* setup coroutine call */ |
806 | setup_coro (aTHX_ next); |
988 | setup_coro (aTHX_ next); |
807 | /* need a new stack */ |
|
|
808 | assert (!next->cctx); |
|
|
809 | } |
989 | } |
810 | else |
990 | else |
811 | { |
991 | { |
812 | /* coroutine already started */ |
992 | /* coroutine already started */ |
813 | save_perl (aTHX_ prev); |
993 | save_perl (aTHX_ prev); |
… | |
… | |
815 | } |
995 | } |
816 | |
996 | |
817 | prev__cctx = prev->cctx; |
997 | prev__cctx = prev->cctx; |
818 | |
998 | |
819 | /* possibly "free" the cctx */ |
999 | /* possibly "free" the cctx */ |
820 | if (prev__cctx->idle_sp == STACKLEVEL) |
1000 | if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE)) |
821 | { |
1001 | { |
822 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1002 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
823 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
1003 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
824 | |
1004 | |
825 | prev->cctx = 0; |
1005 | prev->cctx = 0; |
826 | |
1006 | |
827 | cctx_put (prev__cctx); |
1007 | cctx_put (prev__cctx); |
828 | prev__cctx->inuse = 0; |
|
|
829 | } |
1008 | } |
830 | |
1009 | |
831 | if (!next->cctx) |
1010 | if (!next->cctx) |
832 | { |
|
|
833 | next->cctx = cctx_get (aTHX); |
1011 | next->cctx = cctx_get (aTHX); |
834 | assert (!next->cctx->inuse); |
|
|
835 | next->cctx->inuse = 1; |
|
|
836 | } |
|
|
837 | |
1012 | |
838 | if (prev__cctx != next->cctx) |
1013 | if (prev__cctx != next->cctx) |
839 | { |
1014 | { |
840 | prev__cctx->top_env = PL_top_env; |
1015 | prev__cctx->top_env = PL_top_env; |
841 | PL_top_env = next->cctx->top_env; |
1016 | PL_top_env = next->cctx->top_env; |
… | |
… | |
900 | cctx_destroy (coro->cctx); |
1075 | cctx_destroy (coro->cctx); |
901 | SvREFCNT_dec (coro->args); |
1076 | SvREFCNT_dec (coro->args); |
902 | |
1077 | |
903 | if (coro->next) coro->next->prev = coro->prev; |
1078 | if (coro->next) coro->next->prev = coro->prev; |
904 | if (coro->prev) coro->prev->next = coro->next; |
1079 | if (coro->prev) coro->prev->next = coro->next; |
905 | if (coro == first) first = coro->next; |
1080 | if (coro == coro_first) coro_first = coro->next; |
906 | |
1081 | |
907 | return 1; |
1082 | return 1; |
908 | } |
1083 | } |
909 | |
1084 | |
910 | static int |
1085 | static int |
… | |
… | |
943 | #else |
1118 | #else |
944 | # define MGf_DUP 0 |
1119 | # define MGf_DUP 0 |
945 | #endif |
1120 | #endif |
946 | }; |
1121 | }; |
947 | |
1122 | |
948 | static struct coro * |
|
|
949 | SvSTATE_ (pTHX_ SV *coro) |
|
|
950 | { |
|
|
951 | HV *stash; |
|
|
952 | MAGIC *mg; |
|
|
953 | |
|
|
954 | if (SvROK (coro)) |
|
|
955 | coro = SvRV (coro); |
|
|
956 | |
|
|
957 | stash = SvSTASH (coro); |
|
|
958 | if (stash != coro_stash && stash != coro_state_stash) |
|
|
959 | { |
|
|
960 | /* very slow, but rare, check */ |
|
|
961 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
962 | croak ("Coro::State object required"); |
|
|
963 | } |
|
|
964 | |
|
|
965 | mg = CORO_MAGIC (coro); |
|
|
966 | return (struct coro *)mg->mg_ptr; |
|
|
967 | } |
|
|
968 | |
|
|
969 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
|
|
970 | |
|
|
971 | static void |
1123 | static void |
972 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
1124 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
973 | { |
1125 | { |
974 | ta->prev = SvSTATE (prev_sv); |
1126 | ta->prev = SvSTATE (prev_sv); |
975 | ta->next = SvSTATE (next_sv); |
1127 | ta->next = SvSTATE (next_sv); |
… | |
… | |
1185 | #endif |
1337 | #endif |
1186 | BOOT_PAGESIZE; |
1338 | BOOT_PAGESIZE; |
1187 | |
1339 | |
1188 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1340 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1189 | |
1341 | |
|
|
1342 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
|
|
1343 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
|
|
1344 | newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); |
|
|
1345 | newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); |
|
|
1346 | |
1190 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1347 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1191 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1348 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1192 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1349 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1193 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
1350 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
1194 | newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); |
1351 | newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); |
… | |
… | |
1218 | Newz (0, coro, 1, struct coro); |
1375 | Newz (0, coro, 1, struct coro); |
1219 | coro->args = newAV (); |
1376 | coro->args = newAV (); |
1220 | coro->save = CORO_SAVE_DEF; |
1377 | coro->save = CORO_SAVE_DEF; |
1221 | coro->flags = CF_NEW; |
1378 | coro->flags = CF_NEW; |
1222 | |
1379 | |
1223 | if (first) first->prev = coro; |
1380 | if (coro_first) coro_first->prev = coro; |
1224 | coro->next = first; |
1381 | coro->next = coro_first; |
1225 | first = coro; |
1382 | coro_first = coro; |
1226 | |
1383 | |
1227 | coro->hv = hv = newHV (); |
1384 | coro->hv = hv = newHV (); |
1228 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1385 | 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)); |
1386 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1230 | |
1387 | |
… | |
… | |
1339 | void |
1496 | void |
1340 | list () |
1497 | list () |
1341 | PPCODE: |
1498 | PPCODE: |
1342 | { |
1499 | { |
1343 | struct coro *coro; |
1500 | struct coro *coro; |
1344 | for (coro = first; coro; coro = coro->next) |
1501 | for (coro = coro_first; coro; coro = coro->next) |
1345 | if (coro->hv) |
1502 | if (coro->hv) |
1346 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
1503 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
1347 | } |
1504 | } |
1348 | |
1505 | |
1349 | void |
1506 | void |
1350 | _eval (SV *coro_sv, SV *coderef) |
1507 | call (Coro::State coro, SV *coderef) |
|
|
1508 | ALIAS: |
|
|
1509 | eval = 1 |
1351 | CODE: |
1510 | CODE: |
1352 | { |
1511 | { |
1353 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1354 | if (coro->mainstack) |
1512 | if (coro->mainstack) |
1355 | { |
1513 | { |
1356 | struct coro temp; |
1514 | struct coro temp; |
1357 | Zero (&temp, 1, struct coro); |
1515 | Zero (&temp, 1, struct coro); |
1358 | temp.save = CORO_SAVE_ALL; |
1516 | temp.save = CORO_SAVE_ALL; |
… | |
… | |
1367 | dSP; |
1525 | dSP; |
1368 | ENTER; |
1526 | ENTER; |
1369 | SAVETMPS; |
1527 | SAVETMPS; |
1370 | PUSHMARK (SP); |
1528 | PUSHMARK (SP); |
1371 | PUTBACK; |
1529 | PUTBACK; |
|
|
1530 | if (ix) |
|
|
1531 | eval_sv (coderef, 0); |
|
|
1532 | else |
1372 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1533 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1373 | SPAGAIN; |
1534 | SPAGAIN; |
1374 | FREETMPS; |
1535 | FREETMPS; |
1375 | LEAVE; |
1536 | LEAVE; |
1376 | PUTBACK; |
1537 | PUTBACK; |
1377 | } |
1538 | } |
… | |
… | |
1383 | } |
1544 | } |
1384 | } |
1545 | } |
1385 | } |
1546 | } |
1386 | |
1547 | |
1387 | SV * |
1548 | SV * |
1388 | is_ready (SV *coro_sv) |
1549 | is_ready (Coro::State coro) |
1389 | PROTOTYPE: $ |
1550 | PROTOTYPE: $ |
1390 | ALIAS: |
1551 | ALIAS: |
1391 | is_ready = CF_READY |
1552 | is_ready = CF_READY |
1392 | is_running = CF_RUNNING |
1553 | is_running = CF_RUNNING |
1393 | is_new = CF_NEW |
1554 | is_new = CF_NEW |
1394 | is_destroyed = CF_DESTROYED |
1555 | is_destroyed = CF_DESTROYED |
1395 | CODE: |
1556 | CODE: |
1396 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1397 | RETVAL = boolSV (coro->flags & ix); |
1557 | RETVAL = boolSV (coro->flags & ix); |
1398 | OUTPUT: |
1558 | OUTPUT: |
1399 | RETVAL |
1559 | RETVAL |
1400 | |
1560 | |
|
|
1561 | void |
|
|
1562 | trace (Coro::State coro, int flags = CC_TRACE | CC_TRACE_SUB) |
|
|
1563 | CODE: |
|
|
1564 | if (flags & CC_TRACE) |
|
|
1565 | { |
|
|
1566 | if (!coro->cctx) |
|
|
1567 | coro->cctx = cctx_new (); |
|
|
1568 | else if (!(coro->cctx->flags & CC_TRACE)) |
|
|
1569 | croak ("cannot enable tracing on coroutine with custom stack"); |
|
|
1570 | |
|
|
1571 | coro->cctx->flags |= flags & (CC_TRACE | CC_TRACE_ALL); |
|
|
1572 | } |
|
|
1573 | else |
|
|
1574 | if (coro->cctx && coro->cctx->flags & CC_TRACE) |
|
|
1575 | { |
|
|
1576 | coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL); |
|
|
1577 | coro->cctx->flags |= CC_NOREUSE; |
|
|
1578 | |
|
|
1579 | if (coro->flags & CF_RUNNING) |
|
|
1580 | PL_runops = RUNOPS_DEFAULT; |
|
|
1581 | else |
|
|
1582 | coro->runops = RUNOPS_DEFAULT; |
|
|
1583 | } |
|
|
1584 | |
|
|
1585 | SV * |
|
|
1586 | has_stack (Coro::State coro) |
|
|
1587 | PROTOTYPE: $ |
|
|
1588 | CODE: |
|
|
1589 | RETVAL = boolSV (!!coro->cctx); |
|
|
1590 | OUTPUT: |
|
|
1591 | RETVAL |
|
|
1592 | |
|
|
1593 | int |
|
|
1594 | is_traced (Coro::State coro) |
|
|
1595 | PROTOTYPE: $ |
|
|
1596 | CODE: |
|
|
1597 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
|
|
1598 | OUTPUT: |
|
|
1599 | RETVAL |
|
|
1600 | |
|
|
1601 | IV |
|
|
1602 | rss (Coro::State coro) |
|
|
1603 | PROTOTYPE: $ |
|
|
1604 | CODE: |
|
|
1605 | RETVAL = coro_rss (coro); |
|
|
1606 | OUTPUT: |
|
|
1607 | RETVAL |
|
|
1608 | |
1401 | |
1609 | |
1402 | MODULE = Coro::State PACKAGE = Coro |
1610 | MODULE = Coro::State PACKAGE = Coro |
1403 | |
1611 | |
1404 | BOOT: |
1612 | BOOT: |
1405 | { |
1613 | { |
1406 | int i; |
1614 | int i; |
|
|
1615 | |
|
|
1616 | sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); |
|
|
1617 | sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); |
|
|
1618 | av_async_pool = get_av ("Coro::async_pool", TRUE); |
|
|
1619 | |
|
|
1620 | coro_current = get_sv ("Coro::current", FALSE); |
|
|
1621 | SvREADONLY_on (coro_current); |
1407 | |
1622 | |
1408 | coro_stash = gv_stashpv ("Coro", TRUE); |
1623 | coro_stash = gv_stashpv ("Coro", TRUE); |
1409 | |
1624 | |
1410 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1625 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1411 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1626 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1412 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1627 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1413 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1628 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1414 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1629 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1415 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1630 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1416 | |
|
|
1417 | coro_current = get_sv ("Coro::current", FALSE); |
|
|
1418 | SvREADONLY_on (coro_current); |
|
|
1419 | |
1631 | |
1420 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1632 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1421 | coro_ready[i] = newAV (); |
1633 | coro_ready[i] = newAV (); |
1422 | |
1634 | |
1423 | { |
1635 | { |
… | |
… | |
1481 | CODE: |
1693 | CODE: |
1482 | RETVAL = coro_nready; |
1694 | RETVAL = coro_nready; |
1483 | OUTPUT: |
1695 | OUTPUT: |
1484 | RETVAL |
1696 | RETVAL |
1485 | |
1697 | |
|
|
1698 | # for async_pool speedup |
|
|
1699 | void |
|
|
1700 | _pool_1 (SV *cb) |
|
|
1701 | CODE: |
|
|
1702 | { |
|
|
1703 | int i, len; |
|
|
1704 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1705 | AV *defav = GvAV (PL_defgv); |
|
|
1706 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
|
|
1707 | AV *invoke_av; |
|
|
1708 | |
|
|
1709 | if (!invoke) |
|
|
1710 | croak ("\3terminate\2\n"); |
|
|
1711 | |
|
|
1712 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
1713 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
1714 | |
|
|
1715 | invoke_av = (AV *)SvRV (invoke); |
|
|
1716 | len = av_len (invoke_av); |
|
|
1717 | |
|
|
1718 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
|
|
1719 | |
|
|
1720 | if (len > 0) |
|
|
1721 | { |
|
|
1722 | av_fill (defav, len - 1); |
|
|
1723 | for (i = 0; i < len; ++i) |
|
|
1724 | av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); |
|
|
1725 | } |
|
|
1726 | |
|
|
1727 | SvREFCNT_dec (invoke); |
|
|
1728 | } |
|
|
1729 | |
|
|
1730 | void |
|
|
1731 | _pool_2 (SV *cb) |
|
|
1732 | CODE: |
|
|
1733 | { |
|
|
1734 | struct coro *coro = SvSTATE (coro_current); |
|
|
1735 | |
|
|
1736 | sv_setsv (cb, &PL_sv_undef); |
|
|
1737 | |
|
|
1738 | if (coro_rss (coro) > SvIV (sv_pool_rss) |
|
|
1739 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
1740 | croak ("\3terminate\2\n"); |
|
|
1741 | |
|
|
1742 | av_clear (GvAV (PL_defgv)); |
|
|
1743 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
|
|
1744 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
|
|
1745 | coro->save = CORO_SAVE_DEF; |
|
|
1746 | coro->prio = 0; |
|
|
1747 | av_push (av_async_pool, newSVsv (coro_current)); |
|
|
1748 | } |
|
|
1749 | |
|
|
1750 | |
1486 | MODULE = Coro::State PACKAGE = Coro::AIO |
1751 | MODULE = Coro::State PACKAGE = Coro::AIO |
1487 | |
1752 | |
1488 | SV * |
1753 | SV * |
1489 | _get_state () |
1754 | _get_state () |
1490 | CODE: |
1755 | CODE: |