… | |
… | |
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 |
… | |
… | |
129 | }; |
128 | }; |
130 | |
129 | |
131 | static size_t coro_stacksize = CORO_STACKSIZE; |
130 | static size_t coro_stacksize = CORO_STACKSIZE; |
132 | static struct CoroAPI coroapi; |
131 | static struct CoroAPI coroapi; |
133 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
132 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
|
|
133 | static JMPENV *main_top_env; |
134 | static HV *coro_state_stash, *coro_stash; |
134 | static HV *coro_state_stash, *coro_stash; |
135 | static SV *coro_mortal; /* will be freed after next transfer */ |
135 | static SV *coro_mortal; /* will be freed after next transfer */ |
136 | |
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 | |
137 | static struct coro_cctx *cctx_first; |
142 | static struct coro_cctx *cctx_first; |
138 | 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 | }; |
139 | |
150 | |
140 | /* this is a structure representing a c-level coroutine */ |
151 | /* this is a structure representing a c-level coroutine */ |
141 | typedef struct coro_cctx { |
152 | typedef struct coro_cctx { |
142 | struct coro_cctx *next; |
153 | struct coro_cctx *next; |
143 | |
154 | |
… | |
… | |
152 | coro_context cctx; |
163 | coro_context cctx; |
153 | |
164 | |
154 | #if CORO_USE_VALGRIND |
165 | #if CORO_USE_VALGRIND |
155 | int valgrind_id; |
166 | int valgrind_id; |
156 | #endif |
167 | #endif |
157 | char inuse, mapped; |
168 | unsigned char flags; |
158 | } coro_cctx; |
169 | } coro_cctx; |
159 | |
170 | |
160 | enum { |
171 | enum { |
161 | CF_RUNNING = 0x0001, /* coroutine is running */ |
172 | CF_RUNNING = 0x0001, /* coroutine is running */ |
162 | CF_READY = 0x0002, /* coroutine is ready */ |
173 | CF_READY = 0x0002, /* coroutine is ready */ |
… | |
… | |
187 | # include "state.h" |
198 | # include "state.h" |
188 | #undef VAR |
199 | #undef VAR |
189 | |
200 | |
190 | /* coro process data */ |
201 | /* coro process data */ |
191 | 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 */ |
192 | }; |
207 | }; |
193 | |
208 | |
194 | typedef struct coro *Coro__State; |
209 | typedef struct coro *Coro__State; |
195 | typedef struct coro *Coro__State_or_hashref; |
210 | typedef struct coro *Coro__State_or_hashref; |
196 | |
211 | |
… | |
… | |
205 | |
220 | |
206 | /* for Coro.pm */ |
221 | /* for Coro.pm */ |
207 | static SV *coro_current; |
222 | static SV *coro_current; |
208 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
223 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
209 | static int coro_nready; |
224 | static int coro_nready; |
|
|
225 | static struct coro *coro_first; |
210 | |
226 | |
211 | /** lowlevel stuff **********************************************************/ |
227 | /** lowlevel stuff **********************************************************/ |
212 | |
228 | |
213 | static AV * |
229 | static AV * |
214 | coro_clone_padlist (pTHX_ CV *cv) |
230 | coro_clone_padlist (pTHX_ CV *cv) |
… | |
… | |
373 | CvPADLIST (cv) = (AV *)POPs; |
389 | CvPADLIST (cv) = (AV *)POPs; |
374 | } |
390 | } |
375 | |
391 | |
376 | PUTBACK; |
392 | PUTBACK; |
377 | } |
393 | } |
378 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
|
|
379 | } |
394 | } |
380 | |
395 | |
381 | static void |
396 | static void |
382 | save_perl (pTHX_ Coro__State c) |
397 | save_perl (pTHX_ Coro__State c) |
383 | { |
398 | { |
384 | assert (!PL_comppad || AvARRAY (PL_comppad));//D |
|
|
385 | { |
399 | { |
386 | dSP; |
400 | dSP; |
387 | I32 cxix = cxstack_ix; |
401 | I32 cxix = cxstack_ix; |
388 | PERL_CONTEXT *ccstk = cxstack; |
402 | PERL_CONTEXT *ccstk = cxstack; |
389 | PERL_SI *top_si = PL_curstackinfo; |
403 | PERL_SI *top_si = PL_curstackinfo; |
… | |
… | |
391 | /* |
405 | /* |
392 | * 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 |
393 | * (and reinitialize) all cv's in the whole callchain :( |
407 | * (and reinitialize) all cv's in the whole callchain :( |
394 | */ |
408 | */ |
395 | |
409 | |
396 | EXTEND (SP, 3 + 1); |
|
|
397 | PUSHs (Nullsv); |
410 | XPUSHs (Nullsv); |
398 | /* this loop was inspired by pp_caller */ |
411 | /* this loop was inspired by pp_caller */ |
399 | for (;;) |
412 | for (;;) |
400 | { |
413 | { |
401 | while (cxix >= 0) |
414 | while (cxix >= 0) |
402 | { |
415 | { |
403 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
416 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
404 | |
417 | |
405 | if (CxTYPE (cx) == CXt_SUB) |
418 | if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT) |
406 | { |
419 | { |
407 | CV *cv = cx->blk_sub.cv; |
420 | CV *cv = cx->blk_sub.cv; |
408 | |
421 | |
409 | if (CvDEPTH (cv)) |
422 | if (CvDEPTH (cv)) |
410 | { |
423 | { |
… | |
… | |
421 | |
434 | |
422 | if (top_si->si_type == PERLSI_MAIN) |
435 | if (top_si->si_type == PERLSI_MAIN) |
423 | break; |
436 | break; |
424 | |
437 | |
425 | top_si = top_si->si_prev; |
438 | top_si = top_si->si_prev; |
426 | ccstk = top_si->si_cxstack; |
439 | ccstk = top_si->si_cxstack; |
427 | cxix = top_si->si_cxix; |
440 | cxix = top_si->si_cxix; |
428 | } |
441 | } |
429 | |
442 | |
430 | PUTBACK; |
443 | PUTBACK; |
431 | } |
444 | } |
432 | |
445 | |
… | |
… | |
451 | # define coro_init_stacks init_stacks |
464 | # define coro_init_stacks init_stacks |
452 | #else |
465 | #else |
453 | static void |
466 | static void |
454 | coro_init_stacks (pTHX) |
467 | coro_init_stacks (pTHX) |
455 | { |
468 | { |
456 | PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); |
469 | PL_curstackinfo = new_stackinfo(64, 6); |
457 | PL_curstackinfo->si_type = PERLSI_MAIN; |
470 | PL_curstackinfo->si_type = PERLSI_MAIN; |
458 | PL_curstack = PL_curstackinfo->si_stack; |
471 | PL_curstack = PL_curstackinfo->si_stack; |
459 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
472 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ |
460 | |
473 | |
461 | PL_stack_base = AvARRAY(PL_curstack); |
474 | PL_stack_base = AvARRAY(PL_curstack); |
462 | PL_stack_sp = PL_stack_base; |
475 | PL_stack_sp = PL_stack_base; |
463 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
476 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); |
464 | |
477 | |
465 | New(50,PL_tmps_stack,128,SV*); |
478 | New(50,PL_tmps_stack,64,SV*); |
466 | PL_tmps_floor = -1; |
479 | PL_tmps_floor = -1; |
467 | PL_tmps_ix = -1; |
480 | PL_tmps_ix = -1; |
468 | PL_tmps_max = 128; |
481 | PL_tmps_max = 64; |
469 | |
482 | |
470 | New(54,PL_markstack,32,I32); |
483 | New(54,PL_markstack,16,I32); |
471 | PL_markstack_ptr = PL_markstack; |
484 | PL_markstack_ptr = PL_markstack; |
472 | PL_markstack_max = PL_markstack + 32; |
485 | PL_markstack_max = PL_markstack + 16; |
473 | |
486 | |
474 | #ifdef SET_MARK_OFFSET |
487 | #ifdef SET_MARK_OFFSET |
475 | SET_MARK_OFFSET; |
488 | SET_MARK_OFFSET; |
476 | #endif |
489 | #endif |
477 | |
490 | |
478 | New(54,PL_scopestack,32,I32); |
491 | New(54,PL_scopestack,16,I32); |
479 | PL_scopestack_ix = 0; |
492 | PL_scopestack_ix = 0; |
480 | PL_scopestack_max = 32; |
493 | PL_scopestack_max = 16; |
481 | |
494 | |
482 | New(54,PL_savestack,64,ANY); |
495 | New(54,PL_savestack,64,ANY); |
483 | PL_savestack_ix = 0; |
496 | PL_savestack_ix = 0; |
484 | PL_savestack_max = 64; |
497 | PL_savestack_max = 64; |
485 | |
498 | |
486 | #if !PERL_VERSION_ATLEAST (5,9,0) |
499 | #if !PERL_VERSION_ATLEAST (5,9,0) |
487 | New(54,PL_retstack,16,OP*); |
500 | New(54,PL_retstack,4,OP*); |
488 | PL_retstack_ix = 0; |
501 | PL_retstack_ix = 0; |
489 | PL_retstack_max = 16; |
502 | PL_retstack_max = 4; |
490 | #endif |
503 | #endif |
491 | } |
504 | } |
492 | #endif |
505 | #endif |
493 | |
506 | |
494 | /* |
507 | /* |
… | |
… | |
534 | Safefree (PL_scopestack); |
547 | Safefree (PL_scopestack); |
535 | Safefree (PL_savestack); |
548 | Safefree (PL_savestack); |
536 | #if !PERL_VERSION_ATLEAST (5,9,0) |
549 | #if !PERL_VERSION_ATLEAST (5,9,0) |
537 | Safefree (PL_retstack); |
550 | Safefree (PL_retstack); |
538 | #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; |
539 | } |
583 | } |
540 | |
584 | |
541 | /** coroutine stack handling ************************************************/ |
585 | /** coroutine stack handling ************************************************/ |
542 | |
586 | |
543 | static void |
587 | static void |
… | |
… | |
566 | Zero (&myop, 1, LOGOP); |
610 | Zero (&myop, 1, LOGOP); |
567 | myop.op_next = Nullop; |
611 | myop.op_next = Nullop; |
568 | myop.op_flags = OPf_WANT_VOID; |
612 | myop.op_flags = OPf_WANT_VOID; |
569 | |
613 | |
570 | PUSHMARK (SP); |
614 | PUSHMARK (SP); |
571 | XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); |
615 | XPUSHs (av_shift (GvAV (PL_defgv))); |
572 | PUTBACK; |
616 | PUTBACK; |
573 | PL_op = (OP *)&myop; |
617 | PL_op = (OP *)&myop; |
574 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
618 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
575 | SPAGAIN; |
619 | SPAGAIN; |
576 | } |
620 | } |
… | |
… | |
586 | SvREFCNT_dec (coro_mortal); |
630 | SvREFCNT_dec (coro_mortal); |
587 | coro_mortal = 0; |
631 | coro_mortal = 0; |
588 | } |
632 | } |
589 | } |
633 | } |
590 | |
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 | |
591 | /* 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 */ |
|
|
677 | /* _cctx_init should be careful, as it could be called at almost any time */ |
|
|
678 | /* during execution of a perl program */ |
592 | static void NOINLINE |
679 | static void NOINLINE |
593 | prepare_cctx (pTHX_ coro_cctx *cctx) |
680 | prepare_cctx (pTHX_ coro_cctx *cctx) |
594 | { |
681 | { |
595 | dSP; |
682 | dSP; |
596 | 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; |
597 | |
689 | |
598 | Zero (&myop, 1, LOGOP); |
690 | Zero (&myop, 1, LOGOP); |
599 | myop.op_next = PL_op; |
691 | myop.op_next = PL_op; |
600 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
692 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
601 | |
693 | |
… | |
… | |
607 | PL_op = (OP *)&myop; |
699 | PL_op = (OP *)&myop; |
608 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
700 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
609 | SPAGAIN; |
701 | SPAGAIN; |
610 | } |
702 | } |
611 | |
703 | |
|
|
704 | /* |
|
|
705 | * this is a _very_ stripped down perl interpreter ;) |
|
|
706 | */ |
612 | static void |
707 | static void |
613 | coro_run (void *arg) |
708 | coro_run (void *arg) |
614 | { |
709 | { |
615 | dTHX; |
710 | dTHX; |
616 | |
711 | |
617 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
712 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
618 | UNLOCK; |
713 | UNLOCK; |
619 | |
714 | |
620 | /* |
715 | /* we now skip the entersub that lead to transfer() */ |
621 | * this is a _very_ stripped down perl interpreter ;) |
716 | PL_op = PL_op->op_next; |
622 | */ |
|
|
623 | PL_top_env = &PL_start_env; |
|
|
624 | |
717 | |
625 | /* inject call to cctx_init */ |
718 | /* inject a fake subroutine call to cctx_init */ |
626 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
719 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
627 | |
720 | |
628 | /* somebody will hit me for both perl_run and PL_restartop */ |
721 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
629 | PL_restartop = PL_op; |
722 | PL_restartop = PL_op; |
630 | perl_run (PL_curinterp); |
723 | perl_run (PL_curinterp); |
631 | |
724 | |
632 | fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); |
725 | /* |
633 | abort (); |
726 | * If perl-run returns we assume exit() was being called or the coro |
|
|
727 | * fell off the end, which seems to be the only valid (non-bug) |
|
|
728 | * reason for perl_run to return. We try to exit by jumping to the |
|
|
729 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
|
|
730 | * coroutine as Coro has no such concept |
|
|
731 | */ |
|
|
732 | PL_top_env = main_top_env; |
|
|
733 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
634 | } |
734 | } |
635 | |
735 | |
636 | static coro_cctx * |
736 | static coro_cctx * |
637 | cctx_new () |
737 | cctx_new () |
638 | { |
738 | { |
… | |
… | |
655 | # if CORO_STACKGUARD |
755 | # if CORO_STACKGUARD |
656 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
756 | mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); |
657 | # endif |
757 | # endif |
658 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
758 | stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; |
659 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
759 | stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; |
660 | cctx->mapped = 1; |
760 | cctx->flags |= CC_MAPPED; |
661 | } |
761 | } |
662 | else |
762 | else |
663 | #endif |
763 | #endif |
664 | { |
764 | { |
665 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
765 | cctx->ssize = coro_stacksize * (long)sizeof (long); |
… | |
… | |
692 | #if CORO_USE_VALGRIND |
792 | #if CORO_USE_VALGRIND |
693 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
793 | VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); |
694 | #endif |
794 | #endif |
695 | |
795 | |
696 | #if HAVE_MMAP |
796 | #if HAVE_MMAP |
697 | if (cctx->mapped) |
797 | if (cctx->flags & CC_MAPPED) |
698 | munmap (cctx->sptr, cctx->ssize); |
798 | munmap (cctx->sptr, cctx->ssize); |
699 | else |
799 | else |
700 | #endif |
800 | #endif |
701 | Safefree (cctx->sptr); |
801 | Safefree (cctx->sptr); |
702 | |
802 | |
… | |
… | |
710 | { |
810 | { |
711 | coro_cctx *cctx = cctx_first; |
811 | coro_cctx *cctx = cctx_first; |
712 | cctx_first = cctx->next; |
812 | cctx_first = cctx->next; |
713 | --cctx_idle; |
813 | --cctx_idle; |
714 | |
814 | |
715 | if (cctx->ssize >= coro_stacksize) |
815 | if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE)) |
716 | return cctx; |
816 | return cctx; |
717 | |
817 | |
718 | cctx_destroy (cctx); |
818 | cctx_destroy (cctx); |
719 | } |
819 | } |
720 | |
820 | |
721 | PL_op = PL_op->op_next; |
|
|
722 | return cctx_new (); |
821 | return cctx_new (); |
723 | } |
822 | } |
724 | |
823 | |
725 | static void |
824 | static void |
726 | cctx_put (coro_cctx *cctx) |
825 | cctx_put (coro_cctx *cctx) |
… | |
… | |
730 | { |
829 | { |
731 | coro_cctx *first = cctx_first; |
830 | coro_cctx *first = cctx_first; |
732 | cctx_first = first->next; |
831 | cctx_first = first->next; |
733 | --cctx_idle; |
832 | --cctx_idle; |
734 | |
833 | |
735 | assert (!first->inuse); |
|
|
736 | cctx_destroy (first); |
834 | cctx_destroy (first); |
737 | } |
835 | } |
738 | |
836 | |
739 | ++cctx_idle; |
837 | ++cctx_idle; |
740 | cctx->next = cctx_first; |
838 | cctx->next = cctx_first; |
… | |
… | |
761 | |
859 | |
762 | if (prev->flags & CF_NEW) |
860 | if (prev->flags & CF_NEW) |
763 | { |
861 | { |
764 | /* create a new empty context */ |
862 | /* create a new empty context */ |
765 | Newz (0, prev->cctx, 1, coro_cctx); |
863 | Newz (0, prev->cctx, 1, coro_cctx); |
766 | prev->cctx->inuse = 1; |
|
|
767 | prev->flags &= ~CF_NEW; |
864 | prev->flags &= ~CF_NEW; |
768 | prev->flags |= CF_RUNNING; |
865 | prev->flags |= CF_RUNNING; |
769 | } |
866 | } |
770 | |
867 | |
771 | /*TODO: must not croak here */ |
868 | /*TODO: must not croak here */ |
… | |
… | |
789 | next->flags &= ~CF_NEW; |
886 | next->flags &= ~CF_NEW; |
790 | /* first get rid of the old state */ |
887 | /* first get rid of the old state */ |
791 | save_perl (aTHX_ prev); |
888 | save_perl (aTHX_ prev); |
792 | /* setup coroutine call */ |
889 | /* setup coroutine call */ |
793 | setup_coro (aTHX_ next); |
890 | setup_coro (aTHX_ next); |
794 | /* need a new stack */ |
|
|
795 | assert (!next->cctx); |
|
|
796 | } |
891 | } |
797 | else |
892 | else |
798 | { |
893 | { |
799 | /* coroutine already started */ |
894 | /* coroutine already started */ |
800 | save_perl (aTHX_ prev); |
895 | save_perl (aTHX_ prev); |
… | |
… | |
810 | 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)); |
811 | |
906 | |
812 | prev->cctx = 0; |
907 | prev->cctx = 0; |
813 | |
908 | |
814 | cctx_put (prev__cctx); |
909 | cctx_put (prev__cctx); |
815 | prev__cctx->inuse = 0; |
|
|
816 | } |
910 | } |
817 | |
911 | |
818 | if (!next->cctx) |
912 | if (!next->cctx) |
819 | { |
|
|
820 | next->cctx = cctx_get (aTHX); |
913 | next->cctx = cctx_get (aTHX); |
821 | assert (!next->cctx->inuse); |
|
|
822 | next->cctx->inuse = 1; |
|
|
823 | } |
|
|
824 | |
914 | |
825 | if (prev__cctx != next->cctx) |
915 | if (prev__cctx != next->cctx) |
826 | { |
916 | { |
827 | prev__cctx->top_env = PL_top_env; |
917 | prev__cctx->top_env = PL_top_env; |
828 | PL_top_env = next->cctx->top_env; |
918 | PL_top_env = next->cctx->top_env; |
… | |
… | |
867 | struct coro temp; |
957 | struct coro temp; |
868 | |
958 | |
869 | assert (!(coro->flags & CF_RUNNING)); |
959 | assert (!(coro->flags & CF_RUNNING)); |
870 | |
960 | |
871 | Zero (&temp, 1, struct coro); |
961 | Zero (&temp, 1, struct coro); |
872 | temp.save = CORO_SAVE_DEF; |
962 | temp.save = CORO_SAVE_ALL; |
873 | |
963 | |
874 | if (coro->flags & CF_RUNNING) |
964 | if (coro->flags & CF_RUNNING) |
875 | croak ("FATAL: tried to destroy currently running coroutine"); |
965 | croak ("FATAL: tried to destroy currently running coroutine"); |
876 | |
966 | |
877 | save_perl (aTHX_ &temp); |
967 | save_perl (aTHX_ &temp); |
… | |
… | |
885 | } |
975 | } |
886 | |
976 | |
887 | cctx_destroy (coro->cctx); |
977 | cctx_destroy (coro->cctx); |
888 | SvREFCNT_dec (coro->args); |
978 | SvREFCNT_dec (coro->args); |
889 | |
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 | |
890 | return 1; |
984 | return 1; |
891 | } |
985 | } |
892 | |
986 | |
893 | static int |
987 | static int |
894 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
988 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
895 | { |
989 | { |
896 | struct coro *coro = (struct coro *)mg->mg_ptr; |
990 | struct coro *coro = (struct coro *)mg->mg_ptr; |
897 | mg->mg_ptr = 0; |
991 | mg->mg_ptr = 0; |
|
|
992 | |
|
|
993 | coro->hv = 0; |
898 | |
994 | |
899 | if (--coro->refcnt < 0) |
995 | if (--coro->refcnt < 0) |
900 | { |
996 | { |
901 | coro_state_destroy (aTHX_ coro); |
997 | coro_state_destroy (aTHX_ coro); |
902 | Safefree (coro); |
998 | Safefree (coro); |
… | |
… | |
1175 | newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); |
1271 | newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); |
1176 | newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF)); |
1272 | newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF)); |
1177 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1273 | newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); |
1178 | |
1274 | |
1179 | main_mainstack = PL_mainstack; |
1275 | main_mainstack = PL_mainstack; |
|
|
1276 | main_top_env = PL_top_env; |
|
|
1277 | |
|
|
1278 | while (main_top_env->je_prev) |
|
|
1279 | main_top_env = main_top_env->je_prev; |
1180 | |
1280 | |
1181 | coroapi.ver = CORO_API_VERSION; |
1281 | coroapi.ver = CORO_API_VERSION; |
1182 | coroapi.transfer = api_transfer; |
1282 | coroapi.transfer = api_transfer; |
1183 | |
1283 | |
1184 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1284 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
… | |
… | |
1195 | Newz (0, coro, 1, struct coro); |
1295 | Newz (0, coro, 1, struct coro); |
1196 | coro->args = newAV (); |
1296 | coro->args = newAV (); |
1197 | coro->save = CORO_SAVE_DEF; |
1297 | coro->save = CORO_SAVE_DEF; |
1198 | coro->flags = CF_NEW; |
1298 | coro->flags = CF_NEW; |
1199 | |
1299 | |
|
|
1300 | if (coro_first) coro_first->prev = coro; |
|
|
1301 | coro->next = coro_first; |
|
|
1302 | coro_first = coro; |
|
|
1303 | |
1200 | hv = newHV (); |
1304 | coro->hv = hv = newHV (); |
1201 | 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; |
1202 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1306 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1203 | |
1307 | |
1204 | for (i = 1; i < items; i++) |
1308 | for (i = 1; i < items; i++) |
1205 | av_push (coro->args, newSVsv (ST (i))); |
1309 | av_push (coro->args, newSVsv (ST (i))); |
… | |
… | |
1307 | CODE: |
1411 | CODE: |
1308 | RETVAL = cctx_idle; |
1412 | RETVAL = cctx_idle; |
1309 | OUTPUT: |
1413 | OUTPUT: |
1310 | RETVAL |
1414 | RETVAL |
1311 | |
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->cctx && coro->cctx->flags & CC_TRACE) |
|
|
1487 | XSRETURN_EMPTY; |
|
|
1488 | |
|
|
1489 | if (coro->flags & CF_RUNNING) |
|
|
1490 | croak ("cannot enable tracing on running coroutine"); |
|
|
1491 | |
|
|
1492 | if (coro->cctx) |
|
|
1493 | croak ("cannot enable tracing on coroutine with custom stack"); |
|
|
1494 | |
|
|
1495 | coro->cctx = cctx_new (); |
|
|
1496 | coro->cctx->flags |= CC_TRACE; |
|
|
1497 | } |
|
|
1498 | else |
|
|
1499 | if (coro->cctx && coro->cctx->flags & CC_TRACE) |
|
|
1500 | { |
|
|
1501 | coro->cctx->flags &= ~CC_TRACE; |
|
|
1502 | coro->cctx->flags |= CC_NOREUSE; |
|
|
1503 | } |
|
|
1504 | |
|
|
1505 | SV * |
|
|
1506 | has_stack (Coro::State coro) |
|
|
1507 | PROTOTYPE: $ |
|
|
1508 | CODE: |
|
|
1509 | RETVAL = boolSV (!!coro->cctx); |
|
|
1510 | OUTPUT: |
|
|
1511 | RETVAL |
|
|
1512 | |
|
|
1513 | IV |
|
|
1514 | rss (Coro::State coro) |
|
|
1515 | PROTOTYPE: $ |
|
|
1516 | CODE: |
|
|
1517 | RETVAL = coro_rss (coro); |
|
|
1518 | OUTPUT: |
|
|
1519 | RETVAL |
|
|
1520 | |
|
|
1521 | |
1312 | MODULE = Coro::State PACKAGE = Coro |
1522 | MODULE = Coro::State PACKAGE = Coro |
1313 | |
1523 | |
1314 | BOOT: |
1524 | BOOT: |
1315 | { |
1525 | { |
1316 | int i; |
1526 | int i; |
|
|
1527 | |
|
|
1528 | sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); |
|
|
1529 | sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); |
|
|
1530 | av_async_pool = get_av ("Coro::async_pool", TRUE); |
|
|
1531 | |
|
|
1532 | coro_current = get_sv ("Coro::current", FALSE); |
|
|
1533 | SvREADONLY_on (coro_current); |
1317 | |
1534 | |
1318 | coro_stash = gv_stashpv ("Coro", TRUE); |
1535 | coro_stash = gv_stashpv ("Coro", TRUE); |
1319 | |
1536 | |
1320 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1537 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1321 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1538 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
1322 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1539 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); |
1323 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1540 | newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); |
1324 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1541 | newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); |
1325 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1542 | newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); |
1326 | |
|
|
1327 | coro_current = get_sv ("Coro::current", FALSE); |
|
|
1328 | SvREADONLY_on (coro_current); |
|
|
1329 | |
1543 | |
1330 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1544 | for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) |
1331 | coro_ready[i] = newAV (); |
1545 | coro_ready[i] = newAV (); |
1332 | |
1546 | |
1333 | { |
1547 | { |
… | |
… | |
1383 | CODE: |
1597 | CODE: |
1384 | RETVAL = boolSV (api_ready (self)); |
1598 | RETVAL = boolSV (api_ready (self)); |
1385 | OUTPUT: |
1599 | OUTPUT: |
1386 | RETVAL |
1600 | RETVAL |
1387 | |
1601 | |
1388 | SV * |
|
|
1389 | is_ready (SV *self) |
|
|
1390 | PROTOTYPE: $ |
|
|
1391 | CODE: |
|
|
1392 | RETVAL = boolSV (api_is_ready (self)); |
|
|
1393 | OUTPUT: |
|
|
1394 | RETVAL |
|
|
1395 | |
|
|
1396 | int |
1602 | int |
1397 | nready (...) |
1603 | nready (...) |
1398 | PROTOTYPE: |
1604 | PROTOTYPE: |
1399 | CODE: |
1605 | CODE: |
1400 | RETVAL = coro_nready; |
1606 | RETVAL = coro_nready; |
1401 | OUTPUT: |
1607 | OUTPUT: |
1402 | RETVAL |
1608 | RETVAL |
1403 | |
1609 | |
|
|
1610 | # for async_pool speedup |
|
|
1611 | void |
|
|
1612 | _pool_1 (SV *cb) |
|
|
1613 | CODE: |
|
|
1614 | { |
|
|
1615 | int i, len; |
|
|
1616 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1617 | AV *defav = GvAV (PL_defgv); |
|
|
1618 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
|
|
1619 | AV *invoke_av; |
|
|
1620 | |
|
|
1621 | if (!invoke) |
|
|
1622 | croak ("\3terminate\2\n"); |
|
|
1623 | |
|
|
1624 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
1625 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
1626 | |
|
|
1627 | invoke_av = (AV *)SvRV (invoke); |
|
|
1628 | len = av_len (invoke_av); |
|
|
1629 | |
|
|
1630 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
|
|
1631 | |
|
|
1632 | if (len > 0) |
|
|
1633 | { |
|
|
1634 | av_fill (defav, len - 1); |
|
|
1635 | for (i = 0; i < len; ++i) |
|
|
1636 | av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); |
|
|
1637 | } |
|
|
1638 | |
|
|
1639 | SvREFCNT_dec (invoke); |
|
|
1640 | } |
|
|
1641 | |
|
|
1642 | void |
|
|
1643 | _pool_2 (SV *cb) |
|
|
1644 | CODE: |
|
|
1645 | { |
|
|
1646 | struct coro *coro = SvSTATE (coro_current); |
|
|
1647 | |
|
|
1648 | sv_setsv (cb, &PL_sv_undef); |
|
|
1649 | |
|
|
1650 | if (coro_rss (coro) > SvIV (sv_pool_rss) |
|
|
1651 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
1652 | croak ("\3terminate\2\n"); |
|
|
1653 | |
|
|
1654 | av_clear (GvAV (PL_defgv)); |
|
|
1655 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
|
|
1656 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
|
|
1657 | coro->save = CORO_SAVE_DEF; |
|
|
1658 | coro->prio = 0; |
|
|
1659 | av_push (av_async_pool, newSVsv (coro_current)); |
|
|
1660 | } |
|
|
1661 | |
|
|
1662 | |
1404 | MODULE = Coro::State PACKAGE = Coro::AIO |
1663 | MODULE = Coro::State PACKAGE = Coro::AIO |
1405 | |
1664 | |
1406 | SV * |
1665 | SV * |
1407 | _get_state () |
1666 | _get_state () |
1408 | CODE: |
1667 | CODE: |