ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.152 by root, Thu Sep 20 12:02:25 2007 UTC vs.
Revision 1.172 by root, Sun Sep 30 13:15:01 2007 UTC

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
133static AV *main_mainstack; /* used to differentiate between $main and others */ 132static AV *main_mainstack; /* used to differentiate between $main and others */
134static JMPENV *main_top_env; 133static JMPENV *main_top_env;
135static HV *coro_state_stash, *coro_stash; 134static HV *coro_state_stash, *coro_stash;
136static SV *coro_mortal; /* will be freed after next transfer */ 135static SV *coro_mortal; /* will be freed after next transfer */
137 136
137/* async_pool helper stuff */
138static SV *sv_pool_rss;
139static SV *sv_pool_size;
140static AV *av_async_pool;
141
138static struct coro_cctx *cctx_first; 142static struct coro_cctx *cctx_first;
139static int cctx_count, cctx_idle; 143static int cctx_count, cctx_idle;
144
145enum {
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 */
142typedef struct coro_cctx { 155typedef 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
161enum { 174enum {
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 */
212static SV *coro_current; 228static SV *coro_current;
213static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 229static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
214static int coro_nready; 230static int coro_nready;
215static struct coro *first; 231static struct coro *coro_first;
216 232
217/** lowlevel stuff **********************************************************/ 233/** lowlevel stuff **********************************************************/
218 234
219static AV * 235static AV *
220coro_clone_padlist (pTHX_ CV *cv) 236coro_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
307static struct coro *
308SvSTATE_ (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 */
292static void 334static void
293get_padlist (pTHX_ CV *cv) 335get_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
457static void 498static void
458coro_init_stacks (pTHX) 499coro_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/*
541 Safefree (PL_retstack); 582 Safefree (PL_retstack);
542#endif 583#endif
543} 584}
544 585
545static size_t 586static size_t
546coro_rss (struct coro *coro) 587coro_rss (pTHX_ struct coro *coro)
547{ 588{
548 size_t rss = sizeof (coro); 589 size_t rss = sizeof (coro);
549 590
550 if (coro->mainstack) 591 if (coro->mainstack)
551 { 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
552 rss += sizeof (coro->curstackinfo); 600 rss += sizeof (coro->curstackinfo);
553 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); 601 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *);
554 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 602 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
555
556 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); 603 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *);
557
558 rss += coro->tmps_max * sizeof (SV *); 604 rss += coro->tmps_max * sizeof (SV *);
559
560 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); 605 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32);
561
562 rss += coro->scopestack_max * sizeof (I32); 606 rss += coro->scopestack_max * sizeof (I32);
563
564 rss += coro->savestack_max * sizeof (ANY); 607 rss += coro->savestack_max * sizeof (ANY);
565 608
566#if !PERL_VERSION_ATLEAST (5,9,0) 609#if !PERL_VERSION_ATLEAST (5,9,0)
567 rss += coro->retstack_max * sizeof (OP *); 610 rss += coro->retstack_max * sizeof (OP *);
568#endif 611#endif
579 /* 622 /*
580 * emulate part of the perl startup here. 623 * emulate part of the perl startup here.
581 */ 624 */
582 coro_init_stacks (aTHX); 625 coro_init_stacks (aTHX);
583 626
627 PL_runops = RUNOPS_DEFAULT;
584 PL_curcop = &PL_compiling; 628 PL_curcop = &PL_compiling;
585 PL_in_eval = EVAL_NULL; 629 PL_in_eval = EVAL_NULL;
586 PL_comppad = 0; 630 PL_comppad = 0;
587 PL_curpm = 0; 631 PL_curpm = 0;
588 PL_localizing = 0; 632 PL_localizing = 0;
619 SvREFCNT_dec (coro_mortal); 663 SvREFCNT_dec (coro_mortal);
620 coro_mortal = 0; 664 coro_mortal = 0;
621 } 665 }
622} 666}
623 667
668static int
669runops_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 (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB)
681 {
682 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
683 SV **bot, **top;
684 AV *av = newAV (); /* return values */
685 SV **cb;
686 runops_proc_t old_runops = PL_runops;
687 dSP;
688 ENTER;
689 SAVETMPS;
690 EXTEND (SP, 3);
691 PL_runops = RUNOPS_DEFAULT;
692
693 GV *gv = CvGV (cx->blk_sub.cv);
694 SV *fullname = sv_2mortal (newSV (0));
695 if (isGV (gv))
696 gv_efullname3 (fullname, gv, 0);
697
698 bot = PL_stack_base + cx->blk_oldsp + 1;
699 top = cx->blk_gimme == G_ARRAY ? SP + 1
700 : cx->blk_gimme == G_SCALAR ? bot + 1
701 : bot;
702
703 while (bot < top)
704 av_push (av, SvREFCNT_inc (*bot++));
705
706 PUSHMARK (SP);
707 PUSHs (&PL_sv_no);
708 PUSHs (fullname);
709 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
710 PUTBACK;
711 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
712 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
713 SPAGAIN;
714
715 FREETMPS;
716 LEAVE;
717 PL_runops = old_runops;
718 }
719
720 if (oldcop != PL_curcop)
721 {
722 oldcop = PL_curcop;
723
724 if (PL_curcop != &PL_compiling)
725 {
726 SV **cb;
727 runops_proc_t old_runops = PL_runops;
728 dSP;
729 ENTER;
730 SAVETMPS;
731 EXTEND (SP, 3);
732 PL_runops = RUNOPS_DEFAULT;
733
734 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB)
735 {
736 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
737
738 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
739 {
740 GV *gv = CvGV (cx->blk_sub.cv);
741 SV *fullname = sv_2mortal (newSV (0));
742 if (isGV (gv))
743 gv_efullname3 (fullname, gv, 0);
744
745 PUSHMARK (SP);
746 PUSHs (&PL_sv_yes);
747 PUSHs (fullname);
748 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
749 PUTBACK;
750 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
751 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
752 SPAGAIN;
753 }
754
755 oldcxix = cxstack_ix;
756 }
757
758 if (cctx->flags & CC_TRACE_LINE)
759 {
760 PUSHMARK (SP);
761 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
762 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop))));
763 PUTBACK;
764 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0);
765 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
766 SPAGAIN;
767 }
768
769 FREETMPS;
770 LEAVE;
771 PL_runops = old_runops;
772 }
773 }
774 }
775
776 TAINT_NOT;
777 return 0;
778}
779
624/* inject a fake call to Coro::State::_cctx_init into the execution */ 780/* inject a fake call to Coro::State::_cctx_init into the execution */
625/* _cctx_init should be careful, as it could be called at almost any time */ 781/* _cctx_init should be careful, as it could be called at almost any time */
626/* during execution of a perl program */ 782/* during execution of a perl program */
627static void NOINLINE 783static void NOINLINE
628prepare_cctx (pTHX_ coro_cctx *cctx) 784prepare_cctx (pTHX_ coro_cctx *cctx)
629{ 785{
630 dSP; 786 dSP;
631 LOGOP myop; 787 LOGOP myop;
788
789 PL_top_env = &PL_start_env;
790
791 if (cctx->flags & CC_TRACE)
792 PL_runops = runops_trace;
632 793
633 Zero (&myop, 1, LOGOP); 794 Zero (&myop, 1, LOGOP);
634 myop.op_next = PL_op; 795 myop.op_next = PL_op;
635 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 796 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
636 797
653 dTHX; 814 dTHX;
654 815
655 /* coro_run is the alternative tail of transfer(), so unlock here. */ 816 /* coro_run is the alternative tail of transfer(), so unlock here. */
656 UNLOCK; 817 UNLOCK;
657 818
658 PL_top_env = &PL_start_env; 819 /* we now skip the entersub that lead to transfer() */
820 PL_op = PL_op->op_next;
659 821
660 /* inject a fake subroutine call to cctx_init */ 822 /* inject a fake subroutine call to cctx_init */
661 prepare_cctx (aTHX_ (coro_cctx *)arg); 823 prepare_cctx (aTHX_ (coro_cctx *)arg);
662 824
663 /* somebody or something will hit me for both perl_run and PL_restartop */ 825 /* somebody or something will hit me for both perl_run and PL_restartop */
697# if CORO_STACKGUARD 859# if CORO_STACKGUARD
698 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 860 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
699# endif 861# endif
700 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 862 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
701 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 863 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
702 cctx->mapped = 1; 864 cctx->flags |= CC_MAPPED;
703 } 865 }
704 else 866 else
705#endif 867#endif
706 { 868 {
707 cctx->ssize = coro_stacksize * (long)sizeof (long); 869 cctx->ssize = coro_stacksize * (long)sizeof (long);
734#if CORO_USE_VALGRIND 896#if CORO_USE_VALGRIND
735 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 897 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
736#endif 898#endif
737 899
738#if HAVE_MMAP 900#if HAVE_MMAP
739 if (cctx->mapped) 901 if (cctx->flags & CC_MAPPED)
740 munmap (cctx->sptr, cctx->ssize); 902 munmap (cctx->sptr, cctx->ssize);
741 else 903 else
742#endif 904#endif
743 Safefree (cctx->sptr); 905 Safefree (cctx->sptr);
744 906
752 { 914 {
753 coro_cctx *cctx = cctx_first; 915 coro_cctx *cctx = cctx_first;
754 cctx_first = cctx->next; 916 cctx_first = cctx->next;
755 --cctx_idle; 917 --cctx_idle;
756 918
757 if (cctx->ssize >= coro_stacksize) 919 if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE))
758 return cctx; 920 return cctx;
759 921
760 cctx_destroy (cctx); 922 cctx_destroy (cctx);
761 } 923 }
762 924
763 PL_op = PL_op->op_next;
764 return cctx_new (); 925 return cctx_new ();
765} 926}
766 927
767static void 928static void
768cctx_put (coro_cctx *cctx) 929cctx_put (coro_cctx *cctx)
772 { 933 {
773 coro_cctx *first = cctx_first; 934 coro_cctx *first = cctx_first;
774 cctx_first = first->next; 935 cctx_first = first->next;
775 --cctx_idle; 936 --cctx_idle;
776 937
777 assert (!first->inuse);
778 cctx_destroy (first); 938 cctx_destroy (first);
779 } 939 }
780 940
781 ++cctx_idle; 941 ++cctx_idle;
782 cctx->next = cctx_first; 942 cctx->next = cctx_first;
783 cctx_first = cctx; 943 cctx_first = cctx;
784} 944}
785 945
786/** coroutine switching *****************************************************/ 946/** coroutine switching *****************************************************/
787 947
788/* never call directly, always through the coro_state_transfer global variable */ 948static void NOINLINE
949transfer_check (pTHX_ struct coro *prev, struct coro *next)
950{
951 if (prev != next)
952 {
953 if (!(prev->flags & (CF_RUNNING | CF_NEW)))
954 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states");
955
956 if (next->flags & CF_RUNNING)
957 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
958
959 if (next->flags & CF_DESTROYED)
960 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
961
962 if (PL_lex_state != LEX_NOTPARSING)
963 croak ("Coro::State::transfer called while parsing, but this is not supported");
964 }
965}
966
967/* always use the TRANSFER macro */
789static void NOINLINE 968static void NOINLINE
790transfer (pTHX_ struct coro *prev, struct coro *next) 969transfer (pTHX_ struct coro *prev, struct coro *next)
791{ 970{
792 dSTACKLEVEL; 971 dSTACKLEVEL;
793 972
803 982
804 if (prev->flags & CF_NEW) 983 if (prev->flags & CF_NEW)
805 { 984 {
806 /* create a new empty context */ 985 /* create a new empty context */
807 Newz (0, prev->cctx, 1, coro_cctx); 986 Newz (0, prev->cctx, 1, coro_cctx);
808 prev->cctx->inuse = 1;
809 prev->flags &= ~CF_NEW; 987 prev->flags &= ~CF_NEW;
810 prev->flags |= CF_RUNNING; 988 prev->flags |= CF_RUNNING;
811 } 989 }
812
813 /*TODO: must not croak here */
814 if (!prev->flags & CF_RUNNING)
815 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
816
817 if (next->flags & CF_RUNNING)
818 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
819
820 if (next->flags & CF_DESTROYED)
821 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
822 990
823 prev->flags &= ~CF_RUNNING; 991 prev->flags &= ~CF_RUNNING;
824 next->flags |= CF_RUNNING; 992 next->flags |= CF_RUNNING;
825 993
826 LOCK; 994 LOCK;
831 next->flags &= ~CF_NEW; 999 next->flags &= ~CF_NEW;
832 /* first get rid of the old state */ 1000 /* first get rid of the old state */
833 save_perl (aTHX_ prev); 1001 save_perl (aTHX_ prev);
834 /* setup coroutine call */ 1002 /* setup coroutine call */
835 setup_coro (aTHX_ next); 1003 setup_coro (aTHX_ next);
836 /* need a new stack */
837 assert (!next->cctx);
838 } 1004 }
839 else 1005 else
840 { 1006 {
841 /* coroutine already started */ 1007 /* coroutine already started */
842 save_perl (aTHX_ prev); 1008 save_perl (aTHX_ prev);
844 } 1010 }
845 1011
846 prev__cctx = prev->cctx; 1012 prev__cctx = prev->cctx;
847 1013
848 /* possibly "free" the cctx */ 1014 /* possibly "free" the cctx */
849 if (prev__cctx->idle_sp == STACKLEVEL) 1015 if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))
850 { 1016 {
851 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1017 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
852 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 1018 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
853 1019
854 prev->cctx = 0; 1020 prev->cctx = 0;
855 1021
856 cctx_put (prev__cctx); 1022 cctx_put (prev__cctx);
857 prev__cctx->inuse = 0;
858 } 1023 }
859 1024
1025 ++next->usecount;
1026
860 if (!next->cctx) 1027 if (!next->cctx)
861 {
862 next->cctx = cctx_get (aTHX); 1028 next->cctx = cctx_get (aTHX);
863 assert (!next->cctx->inuse);
864 next->cctx->inuse = 1;
865 }
866 1029
867 if (prev__cctx != next->cctx) 1030 if (prev__cctx != next->cctx)
868 { 1031 {
869 prev__cctx->top_env = PL_top_env; 1032 prev__cctx->top_env = PL_top_env;
870 PL_top_env = next->cctx->top_env; 1033 PL_top_env = next->cctx->top_env;
880{ 1043{
881 struct coro *prev, *next; 1044 struct coro *prev, *next;
882}; 1045};
883 1046
884#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) 1047#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next)
1048#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
885 1049
886/** high level stuff ********************************************************/ 1050/** high level stuff ********************************************************/
887 1051
888static int 1052static int
889coro_state_destroy (pTHX_ struct coro *coro) 1053coro_state_destroy (pTHX_ struct coro *coro)
929 cctx_destroy (coro->cctx); 1093 cctx_destroy (coro->cctx);
930 SvREFCNT_dec (coro->args); 1094 SvREFCNT_dec (coro->args);
931 1095
932 if (coro->next) coro->next->prev = coro->prev; 1096 if (coro->next) coro->next->prev = coro->prev;
933 if (coro->prev) coro->prev->next = coro->next; 1097 if (coro->prev) coro->prev->next = coro->next;
934 if (coro == first) first = coro->next; 1098 if (coro == coro_first) coro_first = coro->next;
935 1099
936 return 1; 1100 return 1;
937} 1101}
938 1102
939static int 1103static int
972#else 1136#else
973# define MGf_DUP 0 1137# define MGf_DUP 0
974#endif 1138#endif
975}; 1139};
976 1140
977static struct coro *
978SvSTATE_ (pTHX_ SV *coro)
979{
980 HV *stash;
981 MAGIC *mg;
982
983 if (SvROK (coro))
984 coro = SvRV (coro);
985
986 stash = SvSTASH (coro);
987 if (stash != coro_stash && stash != coro_state_stash)
988 {
989 /* very slow, but rare, check */
990 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
991 croak ("Coro::State object required");
992 }
993
994 mg = CORO_MAGIC (coro);
995 return (struct coro *)mg->mg_ptr;
996}
997
998#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
999
1000static void 1141static void
1001prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) 1142prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv)
1002{ 1143{
1003 ta->prev = SvSTATE (prev_sv); 1144 ta->prev = SvSTATE (prev_sv);
1004 ta->next = SvSTATE (next_sv); 1145 ta->next = SvSTATE (next_sv);
1146 TRANSFER_CHECK (*ta);
1005} 1147}
1006 1148
1007static void 1149static void
1008api_transfer (SV *prev_sv, SV *next_sv) 1150api_transfer (SV *prev_sv, SV *next_sv)
1009{ 1151{
1126 break; 1268 break;
1127 } 1269 }
1128 1270
1129 /* free this only after the transfer */ 1271 /* free this only after the transfer */
1130 prev_sv = SvRV (coro_current); 1272 prev_sv = SvRV (coro_current);
1131 SvRV_set (coro_current, next_sv);
1132 ta->prev = SvSTATE (prev_sv); 1273 ta->prev = SvSTATE (prev_sv);
1133 1274 TRANSFER_CHECK (*ta);
1134 assert (ta->next->flags & CF_READY); 1275 assert (ta->next->flags & CF_READY);
1135 ta->next->flags &= ~CF_READY; 1276 ta->next->flags &= ~CF_READY;
1277 SvRV_set (coro_current, next_sv);
1136 1278
1137 LOCK; 1279 LOCK;
1138 free_coro_mortal (aTHX); 1280 free_coro_mortal (aTHX);
1139 coro_mortal = prev_sv; 1281 coro_mortal = prev_sv;
1140 UNLOCK; 1282 UNLOCK;
1213 MUTEX_INIT (&coro_mutex); 1355 MUTEX_INIT (&coro_mutex);
1214#endif 1356#endif
1215 BOOT_PAGESIZE; 1357 BOOT_PAGESIZE;
1216 1358
1217 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1359 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1360
1361 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1362 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1363 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1364 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1218 1365
1219 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1366 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1220 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1367 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1221 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1368 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1222 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); 1369 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1247 Newz (0, coro, 1, struct coro); 1394 Newz (0, coro, 1, struct coro);
1248 coro->args = newAV (); 1395 coro->args = newAV ();
1249 coro->save = CORO_SAVE_DEF; 1396 coro->save = CORO_SAVE_DEF;
1250 coro->flags = CF_NEW; 1397 coro->flags = CF_NEW;
1251 1398
1252 if (first) first->prev = coro; 1399 if (coro_first) coro_first->prev = coro;
1253 coro->next = first; 1400 coro->next = coro_first;
1254 first = coro; 1401 coro_first = coro;
1255 1402
1256 coro->hv = hv = newHV (); 1403 coro->hv = hv = newHV ();
1257 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1404 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1258 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1405 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1259 1406
1368void 1515void
1369list () 1516list ()
1370 PPCODE: 1517 PPCODE:
1371{ 1518{
1372 struct coro *coro; 1519 struct coro *coro;
1373 for (coro = first; coro; coro = coro->next) 1520 for (coro = coro_first; coro; coro = coro->next)
1374 if (coro->hv) 1521 if (coro->hv)
1375 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); 1522 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1376} 1523}
1377 1524
1378void 1525void
1379_eval (Coro::State coro, SV *coderef) 1526call (Coro::State coro, SV *coderef)
1527 ALIAS:
1528 eval = 1
1380 CODE: 1529 CODE:
1381{ 1530{
1382 if (coro->mainstack) 1531 if (coro->mainstack)
1383 { 1532 {
1384 struct coro temp; 1533 struct coro temp;
1395 dSP; 1544 dSP;
1396 ENTER; 1545 ENTER;
1397 SAVETMPS; 1546 SAVETMPS;
1398 PUSHMARK (SP); 1547 PUSHMARK (SP);
1399 PUTBACK; 1548 PUTBACK;
1549 if (ix)
1550 eval_sv (coderef, 0);
1551 else
1400 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1552 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1401 SPAGAIN; 1553 SPAGAIN;
1402 FREETMPS; 1554 FREETMPS;
1403 LEAVE; 1555 LEAVE;
1404 PUTBACK; 1556 PUTBACK;
1405 } 1557 }
1409 save_perl (aTHX_ coro); 1561 save_perl (aTHX_ coro);
1410 load_perl (aTHX_ &temp); 1562 load_perl (aTHX_ &temp);
1411 } 1563 }
1412 } 1564 }
1413} 1565}
1414 1566
1415SV * 1567SV *
1416is_ready (Coro::State coro) 1568is_ready (Coro::State coro)
1417 PROTOTYPE: $ 1569 PROTOTYPE: $
1418 ALIAS: 1570 ALIAS:
1419 is_ready = CF_READY 1571 is_ready = CF_READY
1423 CODE: 1575 CODE:
1424 RETVAL = boolSV (coro->flags & ix); 1576 RETVAL = boolSV (coro->flags & ix);
1425 OUTPUT: 1577 OUTPUT:
1426 RETVAL 1578 RETVAL
1427 1579
1580void
1581trace (Coro::State coro, int flags = CC_TRACE | CC_TRACE_SUB)
1582 CODE:
1583 if (flags & CC_TRACE)
1584 {
1585 if (!coro->cctx)
1586 coro->cctx = cctx_new ();
1587 else if (!(coro->cctx->flags & CC_TRACE))
1588 croak ("cannot enable tracing on coroutine with custom stack");
1589
1590 coro->cctx->flags |= flags & (CC_TRACE | CC_TRACE_ALL);
1591 }
1592 else
1593 if (coro->cctx && coro->cctx->flags & CC_TRACE)
1594 {
1595 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL);
1596 coro->cctx->flags |= CC_NOREUSE;
1597
1598 if (coro->flags & CF_RUNNING)
1599 PL_runops = RUNOPS_DEFAULT;
1600 else
1601 coro->runops = RUNOPS_DEFAULT;
1602 }
1603
1604SV *
1605has_stack (Coro::State coro)
1606 PROTOTYPE: $
1607 CODE:
1608 RETVAL = boolSV (!!coro->cctx);
1609 OUTPUT:
1610 RETVAL
1611
1612int
1613is_traced (Coro::State coro)
1614 PROTOTYPE: $
1615 CODE:
1616 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1617 OUTPUT:
1618 RETVAL
1619
1428IV 1620IV
1429rss (Coro::State coro) 1621rss (Coro::State coro)
1430 PROTOTYPE: $ 1622 PROTOTYPE: $
1623 ALIAS:
1624 usecount = 1
1431 CODE: 1625 CODE:
1432 RETVAL = coro_rss (coro); 1626 switch (ix)
1627 {
1628 case 0: RETVAL = coro_rss (aTHX_ coro); break;
1629 case 1: RETVAL = coro->usecount; break;
1630 }
1433 OUTPUT: 1631 OUTPUT:
1434 RETVAL 1632 RETVAL
1435 1633
1436 1634
1437MODULE = Coro::State PACKAGE = Coro 1635MODULE = Coro::State PACKAGE = Coro
1438 1636
1439BOOT: 1637BOOT:
1440{ 1638{
1441 int i; 1639 int i;
1640
1641 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE);
1642 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE);
1643 av_async_pool = get_av ("Coro::async_pool", TRUE);
1644
1645 coro_current = get_sv ("Coro::current", FALSE);
1646 SvREADONLY_on (coro_current);
1442 1647
1443 coro_stash = gv_stashpv ("Coro", TRUE); 1648 coro_stash = gv_stashpv ("Coro", TRUE);
1444 1649
1445 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1650 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1446 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1651 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1447 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1652 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1448 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1653 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1449 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1654 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1450 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1655 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1451
1452 coro_current = get_sv ("Coro::current", FALSE);
1453 SvREADONLY_on (coro_current);
1454 1656
1455 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1657 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1456 coro_ready[i] = newAV (); 1658 coro_ready[i] = newAV ();
1457 1659
1458 { 1660 {
1516 CODE: 1718 CODE:
1517 RETVAL = coro_nready; 1719 RETVAL = coro_nready;
1518 OUTPUT: 1720 OUTPUT:
1519 RETVAL 1721 RETVAL
1520 1722
1723# for async_pool speedup
1724void
1725_pool_1 (SV *cb)
1726 CODE:
1727{
1728 int i, len;
1729 HV *hv = (HV *)SvRV (coro_current);
1730 AV *defav = GvAV (PL_defgv);
1731 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1732 AV *invoke_av;
1733
1734 if (!invoke)
1735 croak ("\3terminate\2\n");
1736
1737 hv_store (hv, "desc", sizeof ("desc") - 1,
1738 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1739
1740 invoke_av = (AV *)SvRV (invoke);
1741 len = av_len (invoke_av);
1742
1743 sv_setsv (cb, AvARRAY (invoke_av)[0]);
1744
1745 if (len > 0)
1746 {
1747 av_fill (defav, len - 1);
1748 for (i = 0; i < len; ++i)
1749 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1]));
1750 }
1751
1752 SvREFCNT_dec (invoke);
1753}
1754
1755void
1756_pool_2 (SV *cb)
1757 CODE:
1758{
1759 struct coro *coro = SvSTATE (coro_current);
1760
1761 sv_setsv (cb, &PL_sv_undef);
1762
1763 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1764 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1765 croak ("\3terminate\2\n");
1766
1767 av_clear (GvAV (PL_defgv));
1768 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1769 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1770 coro->save = CORO_SAVE_DEF;
1771 coro->prio = 0;
1772 av_push (av_async_pool, newSVsv (coro_current));
1773}
1774
1775
1521MODULE = Coro::State PACKAGE = Coro::AIO 1776MODULE = Coro::State PACKAGE = Coro::AIO
1522 1777
1523SV * 1778SV *
1524_get_state () 1779_get_state ()
1525 CODE: 1780 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines