… | |
… | |
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 */ |
… | |
… | |
186 | |
199 | |
187 | #define VAR(name,type) type name; |
200 | #define VAR(name,type) type name; |
188 | # include "state.h" |
201 | # include "state.h" |
189 | #undef VAR |
202 | #undef VAR |
190 | |
203 | |
|
|
204 | /* statistics */ |
|
|
205 | int usecount; /* number of switches to this coro */ |
|
|
206 | |
191 | /* coro process data */ |
207 | /* coro process data */ |
192 | int prio; |
208 | int prio; |
193 | |
209 | |
194 | /* linked list */ |
210 | /* linked list */ |
195 | struct coro *next, *prev; |
211 | struct coro *next, *prev; |
… | |
… | |
210 | |
226 | |
211 | /* for Coro.pm */ |
227 | /* for Coro.pm */ |
212 | static SV *coro_current; |
228 | static SV *coro_current; |
213 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
229 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
214 | static int coro_nready; |
230 | static int coro_nready; |
215 | static struct coro *first; |
231 | static struct coro *coro_first; |
216 | |
232 | |
217 | /** lowlevel stuff **********************************************************/ |
233 | /** lowlevel stuff **********************************************************/ |
218 | |
234 | |
219 | static AV * |
235 | static AV * |
220 | coro_clone_padlist (pTHX_ CV *cv) |
236 | coro_clone_padlist (pTHX_ CV *cv) |
… | |
… | |
285 | SvMAGIC (cv) \ |
301 | SvMAGIC (cv) \ |
286 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
302 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
287 | ? SvMAGIC (cv) \ |
303 | ? SvMAGIC (cv) \ |
288 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
304 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
289 | : 0 |
305 | : 0 |
|
|
306 | |
|
|
307 | static struct coro * |
|
|
308 | SvSTATE_ (pTHX_ SV *coro) |
|
|
309 | { |
|
|
310 | HV *stash; |
|
|
311 | MAGIC *mg; |
|
|
312 | |
|
|
313 | if (SvROK (coro)) |
|
|
314 | coro = SvRV (coro); |
|
|
315 | |
|
|
316 | if (SvTYPE (coro) != SVt_PVHV) |
|
|
317 | croak ("Coro::State object required"); |
|
|
318 | |
|
|
319 | stash = SvSTASH (coro); |
|
|
320 | if (stash != coro_stash && stash != coro_state_stash) |
|
|
321 | { |
|
|
322 | /* very slow, but rare, check */ |
|
|
323 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
|
|
324 | croak ("Coro::State object required"); |
|
|
325 | } |
|
|
326 | |
|
|
327 | mg = CORO_MAGIC (coro); |
|
|
328 | return (struct coro *)mg->mg_ptr; |
|
|
329 | } |
|
|
330 | |
|
|
331 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
290 | |
332 | |
291 | /* the next two functions merely cache the padlists */ |
333 | /* the next two functions merely cache the padlists */ |
292 | static void |
334 | static void |
293 | get_padlist (pTHX_ CV *cv) |
335 | get_padlist (pTHX_ CV *cv) |
294 | { |
336 | { |
… | |
… | |
395 | /* |
437 | /* |
396 | * the worst thing you can imagine happens first - we have to save |
438 | * the worst thing you can imagine happens first - we have to save |
397 | * (and reinitialize) all cv's in the whole callchain :( |
439 | * (and reinitialize) all cv's in the whole callchain :( |
398 | */ |
440 | */ |
399 | |
441 | |
400 | EXTEND (SP, 3 + 1); |
|
|
401 | PUSHs (Nullsv); |
442 | XPUSHs (Nullsv); |
402 | /* this loop was inspired by pp_caller */ |
443 | /* this loop was inspired by pp_caller */ |
403 | for (;;) |
444 | for (;;) |
404 | { |
445 | { |
405 | while (cxix >= 0) |
446 | while (cxix >= 0) |
406 | { |
447 | { |
… | |
… | |
425 | |
466 | |
426 | if (top_si->si_type == PERLSI_MAIN) |
467 | if (top_si->si_type == PERLSI_MAIN) |
427 | break; |
468 | break; |
428 | |
469 | |
429 | top_si = top_si->si_prev; |
470 | top_si = top_si->si_prev; |
430 | ccstk = top_si->si_cxstack; |
471 | ccstk = top_si->si_cxstack; |
431 | cxix = top_si->si_cxix; |
472 | cxix = top_si->si_cxix; |
432 | } |
473 | } |
433 | |
474 | |
434 | PUTBACK; |
475 | PUTBACK; |
435 | } |
476 | } |
436 | |
477 | |
… | |
… | |
455 | # define coro_init_stacks init_stacks |
496 | # define coro_init_stacks init_stacks |
456 | #else |
497 | #else |
457 | static void |
498 | static void |
458 | coro_init_stacks (pTHX) |
499 | coro_init_stacks (pTHX) |
459 | { |
500 | { |
460 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
501 | PL_curstackinfo = new_stackinfo(64, 6); |
461 | PL_curstackinfo->si_type = PERLSI_MAIN; |
502 | PL_curstackinfo->si_type = PERLSI_MAIN; |
462 | PL_curstack = PL_curstackinfo->si_stack; |
503 | PL_curstack = PL_curstackinfo->si_stack; |
463 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
504 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
464 | |
505 | |
465 | PL_stack_base = AvARRAY(PL_curstack); |
506 | PL_stack_base = AvARRAY(PL_curstack); |
466 | PL_stack_sp = PL_stack_base; |
507 | PL_stack_sp = PL_stack_base; |
467 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
508 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
468 | |
509 | |
469 | New(50,PL_tmps_stack,128,SV*); |
510 | New(50,PL_tmps_stack,64,SV*); |
470 | PL_tmps_floor = -1; |
511 | PL_tmps_floor = -1; |
471 | PL_tmps_ix = -1; |
512 | PL_tmps_ix = -1; |
472 | PL_tmps_max = 128; |
513 | PL_tmps_max = 64; |
473 | |
514 | |
474 | New(54,PL_markstack,32,I32); |
515 | New(54,PL_markstack,16,I32); |
475 | PL_markstack_ptr = PL_markstack; |
516 | PL_markstack_ptr = PL_markstack; |
476 | PL_markstack_max = PL_markstack + 32; |
517 | PL_markstack_max = PL_markstack + 16; |
477 | |
518 | |
478 | #ifdef SET_MARK_OFFSET |
519 | #ifdef SET_MARK_OFFSET |
479 | SET_MARK_OFFSET; |
520 | SET_MARK_OFFSET; |
480 | #endif |
521 | #endif |
481 | |
522 | |
482 | New(54,PL_scopestack,32,I32); |
523 | New(54,PL_scopestack,16,I32); |
483 | PL_scopestack_ix = 0; |
524 | PL_scopestack_ix = 0; |
484 | PL_scopestack_max = 32; |
525 | PL_scopestack_max = 16; |
485 | |
526 | |
486 | New(54,PL_savestack,64,ANY); |
527 | New(54,PL_savestack,64,ANY); |
487 | PL_savestack_ix = 0; |
528 | PL_savestack_ix = 0; |
488 | PL_savestack_max = 64; |
529 | PL_savestack_max = 64; |
489 | |
530 | |
490 | #if !PERL_VERSION_ATLEAST (5,9,0) |
531 | #if !PERL_VERSION_ATLEAST (5,9,0) |
491 | New(54,PL_retstack,16,OP*); |
532 | New(54,PL_retstack,4,OP*); |
492 | PL_retstack_ix = 0; |
533 | PL_retstack_ix = 0; |
493 | PL_retstack_max = 16; |
534 | PL_retstack_max = 4; |
494 | #endif |
535 | #endif |
495 | } |
536 | } |
496 | #endif |
537 | #endif |
497 | |
538 | |
498 | /* |
539 | /* |
… | |
… | |
540 | #if !PERL_VERSION_ATLEAST (5,9,0) |
581 | #if !PERL_VERSION_ATLEAST (5,9,0) |
541 | Safefree (PL_retstack); |
582 | Safefree (PL_retstack); |
542 | #endif |
583 | #endif |
543 | } |
584 | } |
544 | |
585 | |
|
|
586 | static size_t |
|
|
587 | coro_rss (pTHX_ struct coro *coro) |
|
|
588 | { |
|
|
589 | size_t rss = sizeof (coro); |
|
|
590 | |
|
|
591 | if (coro->mainstack) |
|
|
592 | { |
|
|
593 | if (coro->flags & CF_RUNNING) |
|
|
594 | { |
|
|
595 | #define VAR(name,type)coro->name = PL_ ## name; |
|
|
596 | # include "state.h" |
|
|
597 | #undef VAR |
|
|
598 | } |
|
|
599 | |
|
|
600 | rss += sizeof (coro->curstackinfo); |
|
|
601 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); |
|
|
602 | rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
|
|
603 | rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); |
|
|
604 | rss += coro->tmps_max * sizeof (SV *); |
|
|
605 | rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); |
|
|
606 | rss += coro->scopestack_max * sizeof (I32); |
|
|
607 | rss += coro->savestack_max * sizeof (ANY); |
|
|
608 | |
|
|
609 | #if !PERL_VERSION_ATLEAST (5,9,0) |
|
|
610 | rss += coro->retstack_max * sizeof (OP *); |
|
|
611 | #endif |
|
|
612 | } |
|
|
613 | |
|
|
614 | return rss; |
|
|
615 | } |
|
|
616 | |
545 | /** coroutine stack handling ************************************************/ |
617 | /** coroutine stack handling ************************************************/ |
546 | |
618 | |
547 | static void |
619 | static void |
548 | setup_coro (pTHX_ struct coro *coro) |
620 | setup_coro (pTHX_ struct coro *coro) |
549 | { |
621 | { |
550 | /* |
622 | /* |
551 | * emulate part of the perl startup here. |
623 | * emulate part of the perl startup here. |
552 | */ |
624 | */ |
553 | coro_init_stacks (aTHX); |
625 | coro_init_stacks (aTHX); |
554 | |
626 | |
|
|
627 | PL_runops = RUNOPS_DEFAULT; |
555 | PL_curcop = &PL_compiling; |
628 | PL_curcop = &PL_compiling; |
556 | PL_in_eval = EVAL_NULL; |
629 | PL_in_eval = EVAL_NULL; |
557 | PL_comppad = 0; |
630 | PL_comppad = 0; |
558 | PL_curpm = 0; |
631 | PL_curpm = 0; |
559 | PL_localizing = 0; |
632 | PL_localizing = 0; |
… | |
… | |
590 | SvREFCNT_dec (coro_mortal); |
663 | SvREFCNT_dec (coro_mortal); |
591 | coro_mortal = 0; |
664 | coro_mortal = 0; |
592 | } |
665 | } |
593 | } |
666 | } |
594 | |
667 | |
|
|
668 | static int |
|
|
669 | runops_trace (pTHX) |
|
|
670 | { |
|
|
671 | COP *oldcop = 0; |
|
|
672 | int oldcxix = -2; |
|
|
673 | struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */ |
|
|
674 | coro_cctx *cctx = coro->cctx; |
|
|
675 | |
|
|
676 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
|
|
677 | { |
|
|
678 | PERL_ASYNC_CHECK (); |
|
|
679 | |
|
|
680 | if (cctx->flags & CC_TRACE_ALL) |
|
|
681 | { |
|
|
682 | if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) |
|
|
683 | { |
|
|
684 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
|
|
685 | SV **bot, **top; |
|
|
686 | AV *av = newAV (); /* return values */ |
|
|
687 | SV **cb; |
|
|
688 | dSP; |
|
|
689 | |
|
|
690 | GV *gv = CvGV (cx->blk_sub.cv); |
|
|
691 | SV *fullname = sv_2mortal (newSV (0)); |
|
|
692 | if (isGV (gv)) |
|
|
693 | gv_efullname3 (fullname, gv, 0); |
|
|
694 | |
|
|
695 | bot = PL_stack_base + cx->blk_oldsp + 1; |
|
|
696 | top = cx->blk_gimme == G_ARRAY ? SP + 1 |
|
|
697 | : cx->blk_gimme == G_SCALAR ? bot + 1 |
|
|
698 | : bot; |
|
|
699 | |
|
|
700 | while (bot < top) |
|
|
701 | av_push (av, SvREFCNT_inc (*bot++)); |
|
|
702 | |
|
|
703 | PL_runops = RUNOPS_DEFAULT; |
|
|
704 | ENTER; |
|
|
705 | SAVETMPS; |
|
|
706 | EXTEND (SP, 3); |
|
|
707 | PUSHMARK (SP); |
|
|
708 | PUSHs (&PL_sv_no); |
|
|
709 | PUSHs (fullname); |
|
|
710 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
|
|
711 | PUTBACK; |
|
|
712 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
|
|
713 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
714 | SPAGAIN; |
|
|
715 | FREETMPS; |
|
|
716 | LEAVE; |
|
|
717 | PL_runops = runops_trace; |
|
|
718 | } |
|
|
719 | |
|
|
720 | if (oldcop != PL_curcop) |
|
|
721 | { |
|
|
722 | oldcop = PL_curcop; |
|
|
723 | |
|
|
724 | if (PL_curcop != &PL_compiling) |
|
|
725 | { |
|
|
726 | SV **cb; |
|
|
727 | |
|
|
728 | if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) |
|
|
729 | { |
|
|
730 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
|
|
731 | |
|
|
732 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
|
|
733 | { |
|
|
734 | runops_proc_t old_runops = PL_runops; |
|
|
735 | dSP; |
|
|
736 | GV *gv = CvGV (cx->blk_sub.cv); |
|
|
737 | SV *fullname = sv_2mortal (newSV (0)); |
|
|
738 | |
|
|
739 | if (isGV (gv)) |
|
|
740 | gv_efullname3 (fullname, gv, 0); |
|
|
741 | |
|
|
742 | PL_runops = RUNOPS_DEFAULT; |
|
|
743 | ENTER; |
|
|
744 | SAVETMPS; |
|
|
745 | EXTEND (SP, 3); |
|
|
746 | PUSHMARK (SP); |
|
|
747 | PUSHs (&PL_sv_yes); |
|
|
748 | PUSHs (fullname); |
|
|
749 | PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
|
|
750 | PUTBACK; |
|
|
751 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
|
|
752 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
753 | SPAGAIN; |
|
|
754 | FREETMPS; |
|
|
755 | LEAVE; |
|
|
756 | PL_runops = runops_trace; |
|
|
757 | } |
|
|
758 | |
|
|
759 | oldcxix = cxstack_ix; |
|
|
760 | } |
|
|
761 | |
|
|
762 | if (cctx->flags & CC_TRACE_LINE) |
|
|
763 | { |
|
|
764 | dSP; |
|
|
765 | |
|
|
766 | PL_runops = RUNOPS_DEFAULT; |
|
|
767 | ENTER; |
|
|
768 | SAVETMPS; |
|
|
769 | EXTEND (SP, 3); |
|
|
770 | PL_runops = RUNOPS_DEFAULT; |
|
|
771 | PUSHMARK (SP); |
|
|
772 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
|
|
773 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
|
|
774 | PUTBACK; |
|
|
775 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0); |
|
|
776 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
|
|
777 | SPAGAIN; |
|
|
778 | FREETMPS; |
|
|
779 | LEAVE; |
|
|
780 | PL_runops = runops_trace; |
|
|
781 | } |
|
|
782 | } |
|
|
783 | } |
|
|
784 | } |
|
|
785 | } |
|
|
786 | |
|
|
787 | TAINT_NOT; |
|
|
788 | return 0; |
|
|
789 | } |
|
|
790 | |
595 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
791 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
596 | /* _cctx_init should be careful, as it could be called at almost any time */ |
792 | /* _cctx_init should be careful, as it could be called at almost any time */ |
597 | /* during execution of a perl program */ |
793 | /* during execution of a perl program */ |
598 | static void NOINLINE |
794 | static void NOINLINE |
599 | prepare_cctx (pTHX_ coro_cctx *cctx) |
795 | prepare_cctx (pTHX_ coro_cctx *cctx) |
600 | { |
796 | { |
601 | dSP; |
797 | dSP; |
602 | LOGOP myop; |
798 | LOGOP myop; |
|
|
799 | |
|
|
800 | PL_top_env = &PL_start_env; |
|
|
801 | |
|
|
802 | if (cctx->flags & CC_TRACE) |
|
|
803 | PL_runops = runops_trace; |
603 | |
804 | |
604 | Zero (&myop, 1, LOGOP); |
805 | Zero (&myop, 1, LOGOP); |
605 | myop.op_next = PL_op; |
806 | myop.op_next = PL_op; |
606 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
807 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
607 | |
808 | |
… | |
… | |
624 | dTHX; |
825 | dTHX; |
625 | |
826 | |
626 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
827 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
627 | UNLOCK; |
828 | UNLOCK; |
628 | |
829 | |
629 | PL_top_env = &PL_start_env; |
830 | /* we now skip the entersub that lead to transfer() */ |
|
|
831 | PL_op = PL_op->op_next; |
630 | |
832 | |
631 | /* inject a fake subroutine call to cctx_init */ |
833 | /* inject a fake subroutine call to cctx_init */ |
632 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
834 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
633 | |
835 | |
634 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
836 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
… | |
… | |
668 | # if CORO_STACKGUARD |
870 | # if CORO_STACKGUARD |
669 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
871 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
670 | # endif |
872 | # endif |
671 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
873 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
672 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
874 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
673 | cctx->mapped = 1; |
875 | cctx->flags |= CC_MAPPED; |
674 | } |
876 | } |
675 | else |
877 | else |
676 | #endif |
878 | #endif |
677 | { |
879 | { |
678 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
880 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
… | |
… | |
705 | #if CORO_USE_VALGRIND |
907 | #if CORO_USE_VALGRIND |
706 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
908 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
707 | #endif |
909 | #endif |
708 | |
910 | |
709 | #if HAVE_MMAP |
911 | #if HAVE_MMAP |
710 | if (cctx->mapped) |
912 | if (cctx->flags & CC_MAPPED) |
711 | munmap (cctx->sptr, cctx->ssize); |
913 | munmap (cctx->sptr, cctx->ssize); |
712 | else |
914 | else |
713 | #endif |
915 | #endif |
714 | Safefree (cctx->sptr); |
916 | Safefree (cctx->sptr); |
715 | |
917 | |
… | |
… | |
723 | { |
925 | { |
724 | coro_cctx *cctx = cctx_first; |
926 | coro_cctx *cctx = cctx_first; |
725 | cctx_first = cctx->next; |
927 | cctx_first = cctx->next; |
726 | --cctx_idle; |
928 | --cctx_idle; |
727 | |
929 | |
728 | if (cctx->ssize >= coro_stacksize) |
930 | if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE)) |
729 | return cctx; |
931 | return cctx; |
730 | |
932 | |
731 | cctx_destroy (cctx); |
933 | cctx_destroy (cctx); |
732 | } |
934 | } |
733 | |
935 | |
734 | PL_op = PL_op->op_next; |
|
|
735 | return cctx_new (); |
936 | return cctx_new (); |
736 | } |
937 | } |
737 | |
938 | |
738 | static void |
939 | static void |
739 | cctx_put (coro_cctx *cctx) |
940 | cctx_put (coro_cctx *cctx) |
… | |
… | |
743 | { |
944 | { |
744 | coro_cctx *first = cctx_first; |
945 | coro_cctx *first = cctx_first; |
745 | cctx_first = first->next; |
946 | cctx_first = first->next; |
746 | --cctx_idle; |
947 | --cctx_idle; |
747 | |
948 | |
748 | assert (!first->inuse); |
|
|
749 | cctx_destroy (first); |
949 | cctx_destroy (first); |
750 | } |
950 | } |
751 | |
951 | |
752 | ++cctx_idle; |
952 | ++cctx_idle; |
753 | cctx->next = cctx_first; |
953 | cctx->next = cctx_first; |
754 | cctx_first = cctx; |
954 | cctx_first = cctx; |
755 | } |
955 | } |
756 | |
956 | |
757 | /** coroutine switching *****************************************************/ |
957 | /** coroutine switching *****************************************************/ |
758 | |
958 | |
759 | /* never call directly, always through the coro_state_transfer global variable */ |
959 | static void NOINLINE |
|
|
960 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
|
|
961 | { |
|
|
962 | if (prev != next) |
|
|
963 | { |
|
|
964 | if (!(prev->flags & (CF_RUNNING | CF_NEW))) |
|
|
965 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states"); |
|
|
966 | |
|
|
967 | if (next->flags & CF_RUNNING) |
|
|
968 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
969 | |
|
|
970 | if (next->flags & CF_DESTROYED) |
|
|
971 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
|
|
972 | |
|
|
973 | if (PL_lex_state != LEX_NOTPARSING) |
|
|
974 | croak ("Coro::State::transfer called while parsing, but this is not supported"); |
|
|
975 | } |
|
|
976 | } |
|
|
977 | |
|
|
978 | /* always use the TRANSFER macro */ |
760 | static void NOINLINE |
979 | static void NOINLINE |
761 | transfer (pTHX_ struct coro *prev, struct coro *next) |
980 | transfer (pTHX_ struct coro *prev, struct coro *next) |
762 | { |
981 | { |
763 | dSTACKLEVEL; |
982 | dSTACKLEVEL; |
764 | |
983 | |
… | |
… | |
774 | |
993 | |
775 | if (prev->flags & CF_NEW) |
994 | if (prev->flags & CF_NEW) |
776 | { |
995 | { |
777 | /* create a new empty context */ |
996 | /* create a new empty context */ |
778 | Newz (0, prev->cctx, 1, coro_cctx); |
997 | Newz (0, prev->cctx, 1, coro_cctx); |
779 | prev->cctx->inuse = 1; |
|
|
780 | prev->flags &= ~CF_NEW; |
998 | prev->flags &= ~CF_NEW; |
781 | prev->flags |= CF_RUNNING; |
999 | prev->flags |= CF_RUNNING; |
782 | } |
1000 | } |
783 | |
|
|
784 | /*TODO: must not croak here */ |
|
|
785 | if (!prev->flags & CF_RUNNING) |
|
|
786 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
|
|
787 | |
|
|
788 | if (next->flags & CF_RUNNING) |
|
|
789 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
790 | |
|
|
791 | if (next->flags & CF_DESTROYED) |
|
|
792 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
|
|
793 | |
1001 | |
794 | prev->flags &= ~CF_RUNNING; |
1002 | prev->flags &= ~CF_RUNNING; |
795 | next->flags |= CF_RUNNING; |
1003 | next->flags |= CF_RUNNING; |
796 | |
1004 | |
797 | LOCK; |
1005 | LOCK; |
… | |
… | |
802 | next->flags &= ~CF_NEW; |
1010 | next->flags &= ~CF_NEW; |
803 | /* first get rid of the old state */ |
1011 | /* first get rid of the old state */ |
804 | save_perl (aTHX_ prev); |
1012 | save_perl (aTHX_ prev); |
805 | /* setup coroutine call */ |
1013 | /* setup coroutine call */ |
806 | setup_coro (aTHX_ next); |
1014 | setup_coro (aTHX_ next); |
807 | /* need a new stack */ |
|
|
808 | assert (!next->cctx); |
|
|
809 | } |
1015 | } |
810 | else |
1016 | else |
811 | { |
1017 | { |
812 | /* coroutine already started */ |
1018 | /* coroutine already started */ |
813 | save_perl (aTHX_ prev); |
1019 | save_perl (aTHX_ prev); |
… | |
… | |
815 | } |
1021 | } |
816 | |
1022 | |
817 | prev__cctx = prev->cctx; |
1023 | prev__cctx = prev->cctx; |
818 | |
1024 | |
819 | /* possibly "free" the cctx */ |
1025 | /* possibly "free" the cctx */ |
820 | if (prev__cctx->idle_sp == STACKLEVEL) |
1026 | if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE)) |
821 | { |
1027 | { |
822 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
1028 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
823 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
1029 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
824 | |
1030 | |
825 | prev->cctx = 0; |
1031 | prev->cctx = 0; |
826 | |
1032 | |
827 | cctx_put (prev__cctx); |
1033 | cctx_put (prev__cctx); |
828 | prev__cctx->inuse = 0; |
|
|
829 | } |
1034 | } |
830 | |
1035 | |
|
|
1036 | ++next->usecount; |
|
|
1037 | |
831 | if (!next->cctx) |
1038 | if (!next->cctx) |
832 | { |
|
|
833 | next->cctx = cctx_get (aTHX); |
1039 | next->cctx = cctx_get (aTHX); |
834 | assert (!next->cctx->inuse); |
|
|
835 | next->cctx->inuse = 1; |
|
|
836 | } |
|
|
837 | |
1040 | |
838 | if (prev__cctx != next->cctx) |
1041 | if (prev__cctx != next->cctx) |
839 | { |
1042 | { |
840 | prev__cctx->top_env = PL_top_env; |
1043 | prev__cctx->top_env = PL_top_env; |
841 | PL_top_env = next->cctx->top_env; |
1044 | PL_top_env = next->cctx->top_env; |
… | |
… | |
851 | { |
1054 | { |
852 | struct coro *prev, *next; |
1055 | struct coro *prev, *next; |
853 | }; |
1056 | }; |
854 | |
1057 | |
855 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
1058 | #define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) |
|
|
1059 | #define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) |
856 | |
1060 | |
857 | /** high level stuff ********************************************************/ |
1061 | /** high level stuff ********************************************************/ |
858 | |
1062 | |
859 | static int |
1063 | static int |
860 | coro_state_destroy (pTHX_ struct coro *coro) |
1064 | coro_state_destroy (pTHX_ struct coro *coro) |
… | |
… | |
900 | cctx_destroy (coro->cctx); |
1104 | cctx_destroy (coro->cctx); |
901 | SvREFCNT_dec (coro->args); |
1105 | SvREFCNT_dec (coro->args); |
902 | |
1106 | |
903 | if (coro->next) coro->next->prev = coro->prev; |
1107 | if (coro->next) coro->next->prev = coro->prev; |
904 | if (coro->prev) coro->prev->next = coro->next; |
1108 | if (coro->prev) coro->prev->next = coro->next; |
905 | if (coro == first) first = coro->next; |
1109 | if (coro == coro_first) coro_first = coro->next; |
906 | |
1110 | |
907 | return 1; |
1111 | return 1; |
908 | } |
1112 | } |
909 | |
1113 | |
910 | static int |
1114 | static int |
… | |
… | |
943 | #else |
1147 | #else |
944 | # define MGf_DUP 0 |
1148 | # define MGf_DUP 0 |
945 | #endif |
1149 | #endif |
946 | }; |
1150 | }; |
947 | |
1151 | |
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 |
1152 | static void |
972 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
1153 | prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) |
973 | { |
1154 | { |
974 | ta->prev = SvSTATE (prev_sv); |
1155 | ta->prev = SvSTATE (prev_sv); |
975 | ta->next = SvSTATE (next_sv); |
1156 | ta->next = SvSTATE (next_sv); |
|
|
1157 | TRANSFER_CHECK (*ta); |
976 | } |
1158 | } |
977 | |
1159 | |
978 | static void |
1160 | static void |
979 | api_transfer (SV *prev_sv, SV *next_sv) |
1161 | api_transfer (SV *prev_sv, SV *next_sv) |
980 | { |
1162 | { |
… | |
… | |
1097 | break; |
1279 | break; |
1098 | } |
1280 | } |
1099 | |
1281 | |
1100 | /* free this only after the transfer */ |
1282 | /* free this only after the transfer */ |
1101 | prev_sv = SvRV (coro_current); |
1283 | prev_sv = SvRV (coro_current); |
1102 | SvRV_set (coro_current, next_sv); |
|
|
1103 | ta->prev = SvSTATE (prev_sv); |
1284 | ta->prev = SvSTATE (prev_sv); |
1104 | |
1285 | TRANSFER_CHECK (*ta); |
1105 | assert (ta->next->flags & CF_READY); |
1286 | assert (ta->next->flags & CF_READY); |
1106 | ta->next->flags &= ~CF_READY; |
1287 | ta->next->flags &= ~CF_READY; |
|
|
1288 | SvRV_set (coro_current, next_sv); |
1107 | |
1289 | |
1108 | LOCK; |
1290 | LOCK; |
1109 | free_coro_mortal (aTHX); |
1291 | free_coro_mortal (aTHX); |
1110 | coro_mortal = prev_sv; |
1292 | coro_mortal = prev_sv; |
1111 | UNLOCK; |
1293 | UNLOCK; |
… | |
… | |
1172 | } |
1354 | } |
1173 | else |
1355 | else |
1174 | return 0; |
1356 | return 0; |
1175 | } |
1357 | } |
1176 | |
1358 | |
|
|
1359 | static void |
|
|
1360 | api_trace (SV *coro_sv, int flags) |
|
|
1361 | { |
|
|
1362 | dTHX; |
|
|
1363 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1364 | |
|
|
1365 | if (flags & CC_TRACE) |
|
|
1366 | { |
|
|
1367 | if (!coro->cctx) |
|
|
1368 | coro->cctx = cctx_new (); |
|
|
1369 | else if (!(coro->cctx->flags & CC_TRACE)) |
|
|
1370 | croak ("cannot enable tracing on coroutine with custom stack"); |
|
|
1371 | |
|
|
1372 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
|
|
1373 | } |
|
|
1374 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
|
|
1375 | { |
|
|
1376 | coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL); |
|
|
1377 | |
|
|
1378 | if (coro->flags & CF_RUNNING) |
|
|
1379 | PL_runops = RUNOPS_DEFAULT; |
|
|
1380 | else |
|
|
1381 | coro->runops = RUNOPS_DEFAULT; |
|
|
1382 | } |
|
|
1383 | } |
|
|
1384 | |
1177 | MODULE = Coro::State PACKAGE = Coro::State |
1385 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
1178 | |
1386 | |
1179 | PROTOTYPES: DISABLE |
1387 | PROTOTYPES: DISABLE |
1180 | |
1388 | |
1181 | BOOT: |
1389 | BOOT: |
1182 | { |
1390 | { |
… | |
… | |
1184 | MUTEX_INIT (&coro_mutex); |
1392 | MUTEX_INIT (&coro_mutex); |
1185 | #endif |
1393 | #endif |
1186 | BOOT_PAGESIZE; |
1394 | BOOT_PAGESIZE; |
1187 | |
1395 | |
1188 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1396 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
|
|
1397 | |
|
|
1398 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
|
|
1399 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
|
|
1400 | newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); |
|
|
1401 | newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); |
1189 | |
1402 | |
1190 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1403 | newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); |
1191 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1404 | newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); |
1192 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1405 | newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); |
1193 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
1406 | newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); |
… | |
… | |
1218 | Newz (0, coro, 1, struct coro); |
1431 | Newz (0, coro, 1, struct coro); |
1219 | coro->args = newAV (); |
1432 | coro->args = newAV (); |
1220 | coro->save = CORO_SAVE_DEF; |
1433 | coro->save = CORO_SAVE_DEF; |
1221 | coro->flags = CF_NEW; |
1434 | coro->flags = CF_NEW; |
1222 | |
1435 | |
1223 | if (first) first->prev = coro; |
1436 | if (coro_first) coro_first->prev = coro; |
1224 | coro->next = first; |
1437 | coro->next = coro_first; |
1225 | first = coro; |
1438 | coro_first = coro; |
1226 | |
1439 | |
1227 | coro->hv = hv = newHV (); |
1440 | coro->hv = hv = newHV (); |
1228 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1441 | 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)); |
1442 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1230 | |
1443 | |
… | |
… | |
1250 | coro->save |= save_also; |
1463 | coro->save |= save_also; |
1251 | } |
1464 | } |
1252 | OUTPUT: |
1465 | OUTPUT: |
1253 | RETVAL |
1466 | RETVAL |
1254 | |
1467 | |
|
|
1468 | # these not obviously related functions are all rolled into the same xs |
|
|
1469 | # function to increase chances that they all will call transfer with the same |
|
|
1470 | # stack offset |
1255 | void |
1471 | void |
1256 | _set_stacklevel (...) |
1472 | _set_stacklevel (...) |
1257 | ALIAS: |
1473 | ALIAS: |
1258 | Coro::State::transfer = 1 |
1474 | Coro::State::transfer = 1 |
1259 | Coro::schedule = 2 |
1475 | Coro::schedule = 2 |
… | |
… | |
1339 | void |
1555 | void |
1340 | list () |
1556 | list () |
1341 | PPCODE: |
1557 | PPCODE: |
1342 | { |
1558 | { |
1343 | struct coro *coro; |
1559 | struct coro *coro; |
1344 | for (coro = first; coro; coro = coro->next) |
1560 | for (coro = coro_first; coro; coro = coro->next) |
1345 | if (coro->hv) |
1561 | if (coro->hv) |
1346 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
1562 | XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); |
1347 | } |
1563 | } |
1348 | |
1564 | |
1349 | void |
1565 | void |
1350 | _eval (SV *coro_sv, SV *coderef) |
1566 | call (Coro::State coro, SV *coderef) |
|
|
1567 | ALIAS: |
|
|
1568 | eval = 1 |
1351 | CODE: |
1569 | CODE: |
1352 | { |
1570 | { |
1353 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1354 | if (coro->mainstack) |
1571 | if (coro->mainstack) |
1355 | { |
1572 | { |
1356 | struct coro temp; |
1573 | struct coro temp; |
1357 | Zero (&temp, 1, struct coro); |
1574 | Zero (&temp, 1, struct coro); |
1358 | temp.save = CORO_SAVE_ALL; |
1575 | temp.save = CORO_SAVE_ALL; |
… | |
… | |
1367 | dSP; |
1584 | dSP; |
1368 | ENTER; |
1585 | ENTER; |
1369 | SAVETMPS; |
1586 | SAVETMPS; |
1370 | PUSHMARK (SP); |
1587 | PUSHMARK (SP); |
1371 | PUTBACK; |
1588 | PUTBACK; |
|
|
1589 | if (ix) |
|
|
1590 | eval_sv (coderef, 0); |
|
|
1591 | else |
1372 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1592 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1373 | SPAGAIN; |
1593 | SPAGAIN; |
1374 | FREETMPS; |
1594 | FREETMPS; |
1375 | LEAVE; |
1595 | LEAVE; |
1376 | PUTBACK; |
1596 | PUTBACK; |
1377 | } |
1597 | } |
… | |
… | |
1381 | save_perl (aTHX_ coro); |
1601 | save_perl (aTHX_ coro); |
1382 | load_perl (aTHX_ &temp); |
1602 | load_perl (aTHX_ &temp); |
1383 | } |
1603 | } |
1384 | } |
1604 | } |
1385 | } |
1605 | } |
1386 | |
1606 | |
1387 | SV * |
1607 | SV * |
1388 | is_ready (SV *coro_sv) |
1608 | is_ready (Coro::State coro) |
1389 | PROTOTYPE: $ |
1609 | PROTOTYPE: $ |
1390 | ALIAS: |
1610 | ALIAS: |
1391 | is_ready = CF_READY |
1611 | is_ready = CF_READY |
1392 | is_running = CF_RUNNING |
1612 | is_running = CF_RUNNING |
1393 | is_new = CF_NEW |
1613 | is_new = CF_NEW |
1394 | is_destroyed = CF_DESTROYED |
1614 | is_destroyed = CF_DESTROYED |
1395 | CODE: |
1615 | CODE: |
1396 | struct coro *coro = SvSTATE (coro_sv); |
|
|
1397 | RETVAL = boolSV (coro->flags & ix); |
1616 | RETVAL = boolSV (coro->flags & ix); |
1398 | OUTPUT: |
1617 | OUTPUT: |
1399 | RETVAL |
1618 | RETVAL |
1400 | |
1619 | |
|
|
1620 | void |
|
|
1621 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
|
|
1622 | |
|
|
1623 | SV * |
|
|
1624 | has_stack (Coro::State coro) |
|
|
1625 | PROTOTYPE: $ |
|
|
1626 | CODE: |
|
|
1627 | RETVAL = boolSV (!!coro->cctx); |
|
|
1628 | OUTPUT: |
|
|
1629 | RETVAL |
|
|
1630 | |
|
|
1631 | int |
|
|
1632 | is_traced (Coro::State coro) |
|
|
1633 | PROTOTYPE: $ |
|
|
1634 | CODE: |
|
|
1635 | RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; |
|
|
1636 | OUTPUT: |
|
|
1637 | RETVAL |
|
|
1638 | |
|
|
1639 | IV |
|
|
1640 | rss (Coro::State coro) |
|
|
1641 | PROTOTYPE: $ |
|
|
1642 | ALIAS: |
|
|
1643 | usecount = 1 |
|
|
1644 | CODE: |
|
|
1645 | switch (ix) |
|
|
1646 | { |
|
|
1647 | case 0: RETVAL = coro_rss (aTHX_ coro); break; |
|
|
1648 | case 1: RETVAL = coro->usecount; break; |
|
|
1649 | } |
|
|
1650 | OUTPUT: |
|
|
1651 | RETVAL |
|
|
1652 | |
1401 | |
1653 | |
1402 | MODULE = Coro::State PACKAGE = Coro |
1654 | MODULE = Coro::State PACKAGE = Coro |
1403 | |
1655 | |
1404 | BOOT: |
1656 | BOOT: |
1405 | { |
1657 | { |
1406 | int i; |
1658 | int i; |
|
|
1659 | |
|
|
1660 | sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); |
|
|
1661 | sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); |
|
|
1662 | av_async_pool = get_av ("Coro::async_pool", TRUE); |
|
|
1663 | |
|
|
1664 | coro_current = get_sv ("Coro::current", FALSE); |
|
|
1665 | SvREADONLY_on (coro_current); |
1407 | |
1666 | |
1408 | coro_stash = gv_stashpv ("Coro", TRUE); |
1667 | coro_stash = gv_stashpv ("Coro", TRUE); |
1409 | |
1668 | |
1410 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1669 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1411 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1670 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1412 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1671 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1413 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1672 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1414 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1673 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1415 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1674 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1416 | |
|
|
1417 | coro_current = get_sv ("Coro::current", FALSE); |
|
|
1418 | SvREADONLY_on (coro_current); |
|
|
1419 | |
1675 | |
1420 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1676 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1421 | coro_ready[i] = newAV (); |
1677 | coro_ready[i] = newAV (); |
1422 | |
1678 | |
1423 | { |
1679 | { |
… | |
… | |
1481 | CODE: |
1737 | CODE: |
1482 | RETVAL = coro_nready; |
1738 | RETVAL = coro_nready; |
1483 | OUTPUT: |
1739 | OUTPUT: |
1484 | RETVAL |
1740 | RETVAL |
1485 | |
1741 | |
|
|
1742 | # for async_pool speedup |
|
|
1743 | void |
|
|
1744 | _pool_1 (SV *cb) |
|
|
1745 | CODE: |
|
|
1746 | { |
|
|
1747 | int i, len; |
|
|
1748 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1749 | AV *defav = GvAV (PL_defgv); |
|
|
1750 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
|
|
1751 | AV *invoke_av; |
|
|
1752 | |
|
|
1753 | if (!invoke) |
|
|
1754 | croak ("\3terminate\2\n"); |
|
|
1755 | |
|
|
1756 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
1757 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
1758 | |
|
|
1759 | invoke_av = (AV *)SvRV (invoke); |
|
|
1760 | len = av_len (invoke_av); |
|
|
1761 | |
|
|
1762 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
|
|
1763 | |
|
|
1764 | if (len > 0) |
|
|
1765 | { |
|
|
1766 | av_fill (defav, len - 1); |
|
|
1767 | for (i = 0; i < len; ++i) |
|
|
1768 | av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); |
|
|
1769 | } |
|
|
1770 | |
|
|
1771 | SvREFCNT_dec (invoke); |
|
|
1772 | } |
|
|
1773 | |
|
|
1774 | void |
|
|
1775 | _pool_2 (SV *cb) |
|
|
1776 | CODE: |
|
|
1777 | { |
|
|
1778 | struct coro *coro = SvSTATE (coro_current); |
|
|
1779 | |
|
|
1780 | sv_setsv (cb, &PL_sv_undef); |
|
|
1781 | |
|
|
1782 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
|
|
1783 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
1784 | croak ("\3terminate\2\n"); |
|
|
1785 | |
|
|
1786 | av_clear (GvAV (PL_defgv)); |
|
|
1787 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
|
|
1788 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
|
|
1789 | |
|
|
1790 | coro->save = CORO_SAVE_DEF; |
|
|
1791 | coro->prio = 0; |
|
|
1792 | |
|
|
1793 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
|
|
1794 | api_trace (coro_current, 0); |
|
|
1795 | |
|
|
1796 | av_push (av_async_pool, newSVsv (coro_current)); |
|
|
1797 | } |
|
|
1798 | |
|
|
1799 | |
1486 | MODULE = Coro::State PACKAGE = Coro::AIO |
1800 | MODULE = Coro::State PACKAGE = Coro::AIO |
1487 | |
1801 | |
1488 | SV * |
1802 | SV * |
1489 | _get_state () |
1803 | _get_state () |
1490 | CODE: |
1804 | CODE: |