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.148 by root, Fri Apr 13 12:56:55 2007 UTC vs.
Revision 1.173 by root, Sun Sep 30 13:41:45 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;
209
210 /* linked list */
211 struct coro *next, *prev;
212 HV *hv; /* the perl hash associated with this coro, if any */
193}; 213};
194 214
195typedef struct coro *Coro__State; 215typedef struct coro *Coro__State;
196typedef struct coro *Coro__State_or_hashref; 216typedef struct coro *Coro__State_or_hashref;
197 217
206 226
207/* for Coro.pm */ 227/* for Coro.pm */
208static SV *coro_current; 228static SV *coro_current;
209static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 229static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
210static int coro_nready; 230static int coro_nready;
231static struct coro *coro_first;
211 232
212/** lowlevel stuff **********************************************************/ 233/** lowlevel stuff **********************************************************/
213 234
214static AV * 235static AV *
215coro_clone_padlist (pTHX_ CV *cv) 236coro_clone_padlist (pTHX_ CV *cv)
280 SvMAGIC (cv) \ 301 SvMAGIC (cv) \
281 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ 302 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
282 ? SvMAGIC (cv) \ 303 ? SvMAGIC (cv) \
283 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 304 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
284 : 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))
285 332
286/* the next two functions merely cache the padlists */ 333/* the next two functions merely cache the padlists */
287static void 334static void
288get_padlist (pTHX_ CV *cv) 335get_padlist (pTHX_ CV *cv)
289{ 336{
374 CvPADLIST (cv) = (AV *)POPs; 421 CvPADLIST (cv) = (AV *)POPs;
375 } 422 }
376 423
377 PUTBACK; 424 PUTBACK;
378 } 425 }
379 assert (!PL_comppad || AvARRAY (PL_comppad));//D
380} 426}
381 427
382static void 428static void
383save_perl (pTHX_ Coro__State c) 429save_perl (pTHX_ Coro__State c)
384{ 430{
385 assert (!PL_comppad || AvARRAY (PL_comppad));//D
386 { 431 {
387 dSP; 432 dSP;
388 I32 cxix = cxstack_ix; 433 I32 cxix = cxstack_ix;
389 PERL_CONTEXT *ccstk = cxstack; 434 PERL_CONTEXT *ccstk = cxstack;
390 PERL_SI *top_si = PL_curstackinfo; 435 PERL_SI *top_si = PL_curstackinfo;
392 /* 437 /*
393 * 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
394 * (and reinitialize) all cv's in the whole callchain :( 439 * (and reinitialize) all cv's in the whole callchain :(
395 */ 440 */
396 441
397 EXTEND (SP, 3 + 1);
398 PUSHs (Nullsv); 442 XPUSHs (Nullsv);
399 /* this loop was inspired by pp_caller */ 443 /* this loop was inspired by pp_caller */
400 for (;;) 444 for (;;)
401 { 445 {
402 while (cxix >= 0) 446 while (cxix >= 0)
403 { 447 {
404 PERL_CONTEXT *cx = &ccstk[cxix--]; 448 PERL_CONTEXT *cx = &ccstk[cxix--];
405 449
406 if (CxTYPE (cx) == CXt_SUB) 450 if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)
407 { 451 {
408 CV *cv = cx->blk_sub.cv; 452 CV *cv = cx->blk_sub.cv;
409 453
410 if (CvDEPTH (cv)) 454 if (CvDEPTH (cv))
411 { 455 {
422 466
423 if (top_si->si_type == PERLSI_MAIN) 467 if (top_si->si_type == PERLSI_MAIN)
424 break; 468 break;
425 469
426 top_si = top_si->si_prev; 470 top_si = top_si->si_prev;
427 ccstk = top_si->si_cxstack; 471 ccstk = top_si->si_cxstack;
428 cxix = top_si->si_cxix; 472 cxix = top_si->si_cxix;
429 } 473 }
430 474
431 PUTBACK; 475 PUTBACK;
432 } 476 }
433 477
452# define coro_init_stacks init_stacks 496# define coro_init_stacks init_stacks
453#else 497#else
454static void 498static void
455coro_init_stacks (pTHX) 499coro_init_stacks (pTHX)
456{ 500{
457 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 501 PL_curstackinfo = new_stackinfo(64, 6);
458 PL_curstackinfo->si_type = PERLSI_MAIN; 502 PL_curstackinfo->si_type = PERLSI_MAIN;
459 PL_curstack = PL_curstackinfo->si_stack; 503 PL_curstack = PL_curstackinfo->si_stack;
460 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 504 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
461 505
462 PL_stack_base = AvARRAY(PL_curstack); 506 PL_stack_base = AvARRAY(PL_curstack);
463 PL_stack_sp = PL_stack_base; 507 PL_stack_sp = PL_stack_base;
464 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 508 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
465 509
466 New(50,PL_tmps_stack,128,SV*); 510 New(50,PL_tmps_stack,64,SV*);
467 PL_tmps_floor = -1; 511 PL_tmps_floor = -1;
468 PL_tmps_ix = -1; 512 PL_tmps_ix = -1;
469 PL_tmps_max = 128; 513 PL_tmps_max = 64;
470 514
471 New(54,PL_markstack,32,I32); 515 New(54,PL_markstack,16,I32);
472 PL_markstack_ptr = PL_markstack; 516 PL_markstack_ptr = PL_markstack;
473 PL_markstack_max = PL_markstack + 32; 517 PL_markstack_max = PL_markstack + 16;
474 518
475#ifdef SET_MARK_OFFSET 519#ifdef SET_MARK_OFFSET
476 SET_MARK_OFFSET; 520 SET_MARK_OFFSET;
477#endif 521#endif
478 522
479 New(54,PL_scopestack,32,I32); 523 New(54,PL_scopestack,16,I32);
480 PL_scopestack_ix = 0; 524 PL_scopestack_ix = 0;
481 PL_scopestack_max = 32; 525 PL_scopestack_max = 16;
482 526
483 New(54,PL_savestack,64,ANY); 527 New(54,PL_savestack,64,ANY);
484 PL_savestack_ix = 0; 528 PL_savestack_ix = 0;
485 PL_savestack_max = 64; 529 PL_savestack_max = 64;
486 530
487#if !PERL_VERSION_ATLEAST (5,9,0) 531#if !PERL_VERSION_ATLEAST (5,9,0)
488 New(54,PL_retstack,16,OP*); 532 New(54,PL_retstack,4,OP*);
489 PL_retstack_ix = 0; 533 PL_retstack_ix = 0;
490 PL_retstack_max = 16; 534 PL_retstack_max = 4;
491#endif 535#endif
492} 536}
493#endif 537#endif
494 538
495/* 539/*
537#if !PERL_VERSION_ATLEAST (5,9,0) 581#if !PERL_VERSION_ATLEAST (5,9,0)
538 Safefree (PL_retstack); 582 Safefree (PL_retstack);
539#endif 583#endif
540} 584}
541 585
586static size_t
587coro_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
542/** coroutine stack handling ************************************************/ 617/** coroutine stack handling ************************************************/
543 618
544static void 619static void
545setup_coro (pTHX_ struct coro *coro) 620setup_coro (pTHX_ struct coro *coro)
546{ 621{
547 /* 622 /*
548 * emulate part of the perl startup here. 623 * emulate part of the perl startup here.
549 */ 624 */
550 coro_init_stacks (aTHX); 625 coro_init_stacks (aTHX);
551 626
627 PL_runops = RUNOPS_DEFAULT;
552 PL_curcop = &PL_compiling; 628 PL_curcop = &PL_compiling;
553 PL_in_eval = EVAL_NULL; 629 PL_in_eval = EVAL_NULL;
554 PL_comppad = 0; 630 PL_comppad = 0;
555 PL_curpm = 0; 631 PL_curpm = 0;
556 PL_localizing = 0; 632 PL_localizing = 0;
567 Zero (&myop, 1, LOGOP); 643 Zero (&myop, 1, LOGOP);
568 myop.op_next = Nullop; 644 myop.op_next = Nullop;
569 myop.op_flags = OPf_WANT_VOID; 645 myop.op_flags = OPf_WANT_VOID;
570 646
571 PUSHMARK (SP); 647 PUSHMARK (SP);
572 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 648 XPUSHs (av_shift (GvAV (PL_defgv)));
573 PUTBACK; 649 PUTBACK;
574 PL_op = (OP *)&myop; 650 PL_op = (OP *)&myop;
575 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 651 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
576 SPAGAIN; 652 SPAGAIN;
577 } 653 }
587 SvREFCNT_dec (coro_mortal); 663 SvREFCNT_dec (coro_mortal);
588 coro_mortal = 0; 664 coro_mortal = 0;
589 } 665 }
590} 666}
591 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 (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
592/* 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 */
593/* _cctx_init shoukld 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 */
594/* during execution of a pelr program */ 793/* during execution of a perl program */
595static void NOINLINE 794static void NOINLINE
596prepare_cctx (pTHX_ coro_cctx *cctx) 795prepare_cctx (pTHX_ coro_cctx *cctx)
597{ 796{
598 dSP; 797 dSP;
599 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;
600 804
601 Zero (&myop, 1, LOGOP); 805 Zero (&myop, 1, LOGOP);
602 myop.op_next = PL_op; 806 myop.op_next = PL_op;
603 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 807 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
604 808
621 dTHX; 825 dTHX;
622 826
623 /* coro_run is the alternative tail of transfer(), so unlock here. */ 827 /* coro_run is the alternative tail of transfer(), so unlock here. */
624 UNLOCK; 828 UNLOCK;
625 829
626 PL_top_env = &PL_start_env; 830 /* we now skip the entersub that lead to transfer() */
831 PL_op = PL_op->op_next;
627 832
628 /* inject a fake subroutine call to cctx_init */ 833 /* inject a fake subroutine call to cctx_init */
629 prepare_cctx (aTHX_ (coro_cctx *)arg); 834 prepare_cctx (aTHX_ (coro_cctx *)arg);
630 835
631 /* 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 */
632 PL_restartop = PL_op; 837 PL_restartop = PL_op;
633 perl_run (PL_curinterp); 838 perl_run (PL_curinterp);
634 839
840 /*
635 /* If perl-run returns we assume exit() was being called, which */ 841 * If perl-run returns we assume exit() was being called or the coro
636 /* seems to be the only valid (non-bug) reason for perl_run to return. */ 842 * fell off the end, which seems to be the only valid (non-bug)
637 /* We try to exit by jumping to the bootstrap-time "top" top_env, as */ 843 * reason for perl_run to return. We try to exit by jumping to the
638 /* we cannot restore the "main" coroutine as Coro has no such concept */ 844 * bootstrap-time "top" top_env, as we cannot restore the "main"
845 * coroutine as Coro has no such concept
846 */
639 PL_top_env = main_top_env; 847 PL_top_env = main_top_env;
640 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 848 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
641} 849}
642 850
643static coro_cctx * 851static coro_cctx *
662# if CORO_STACKGUARD 870# if CORO_STACKGUARD
663 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 871 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
664# endif 872# endif
665 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 873 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
666 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 874 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
667 cctx->mapped = 1; 875 cctx->flags |= CC_MAPPED;
668 } 876 }
669 else 877 else
670#endif 878#endif
671 { 879 {
672 cctx->ssize = coro_stacksize * (long)sizeof (long); 880 cctx->ssize = coro_stacksize * (long)sizeof (long);
699#if CORO_USE_VALGRIND 907#if CORO_USE_VALGRIND
700 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 908 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
701#endif 909#endif
702 910
703#if HAVE_MMAP 911#if HAVE_MMAP
704 if (cctx->mapped) 912 if (cctx->flags & CC_MAPPED)
705 munmap (cctx->sptr, cctx->ssize); 913 munmap (cctx->sptr, cctx->ssize);
706 else 914 else
707#endif 915#endif
708 Safefree (cctx->sptr); 916 Safefree (cctx->sptr);
709 917
717 { 925 {
718 coro_cctx *cctx = cctx_first; 926 coro_cctx *cctx = cctx_first;
719 cctx_first = cctx->next; 927 cctx_first = cctx->next;
720 --cctx_idle; 928 --cctx_idle;
721 929
722 if (cctx->ssize >= coro_stacksize) 930 if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE))
723 return cctx; 931 return cctx;
724 932
725 cctx_destroy (cctx); 933 cctx_destroy (cctx);
726 } 934 }
727 935
728 PL_op = PL_op->op_next;
729 return cctx_new (); 936 return cctx_new ();
730} 937}
731 938
732static void 939static void
733cctx_put (coro_cctx *cctx) 940cctx_put (coro_cctx *cctx)
737 { 944 {
738 coro_cctx *first = cctx_first; 945 coro_cctx *first = cctx_first;
739 cctx_first = first->next; 946 cctx_first = first->next;
740 --cctx_idle; 947 --cctx_idle;
741 948
742 assert (!first->inuse);
743 cctx_destroy (first); 949 cctx_destroy (first);
744 } 950 }
745 951
746 ++cctx_idle; 952 ++cctx_idle;
747 cctx->next = cctx_first; 953 cctx->next = cctx_first;
748 cctx_first = cctx; 954 cctx_first = cctx;
749} 955}
750 956
751/** coroutine switching *****************************************************/ 957/** coroutine switching *****************************************************/
752 958
753/* never call directly, always through the coro_state_transfer global variable */ 959static void NOINLINE
960transfer_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 */
754static void NOINLINE 979static void NOINLINE
755transfer (pTHX_ struct coro *prev, struct coro *next) 980transfer (pTHX_ struct coro *prev, struct coro *next)
756{ 981{
757 dSTACKLEVEL; 982 dSTACKLEVEL;
758 983
768 993
769 if (prev->flags & CF_NEW) 994 if (prev->flags & CF_NEW)
770 { 995 {
771 /* create a new empty context */ 996 /* create a new empty context */
772 Newz (0, prev->cctx, 1, coro_cctx); 997 Newz (0, prev->cctx, 1, coro_cctx);
773 prev->cctx->inuse = 1;
774 prev->flags &= ~CF_NEW; 998 prev->flags &= ~CF_NEW;
775 prev->flags |= CF_RUNNING; 999 prev->flags |= CF_RUNNING;
776 } 1000 }
777
778 /*TODO: must not croak here */
779 if (!prev->flags & CF_RUNNING)
780 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
781
782 if (next->flags & CF_RUNNING)
783 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
784
785 if (next->flags & CF_DESTROYED)
786 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
787 1001
788 prev->flags &= ~CF_RUNNING; 1002 prev->flags &= ~CF_RUNNING;
789 next->flags |= CF_RUNNING; 1003 next->flags |= CF_RUNNING;
790 1004
791 LOCK; 1005 LOCK;
796 next->flags &= ~CF_NEW; 1010 next->flags &= ~CF_NEW;
797 /* first get rid of the old state */ 1011 /* first get rid of the old state */
798 save_perl (aTHX_ prev); 1012 save_perl (aTHX_ prev);
799 /* setup coroutine call */ 1013 /* setup coroutine call */
800 setup_coro (aTHX_ next); 1014 setup_coro (aTHX_ next);
801 /* need a new stack */
802 assert (!next->cctx);
803 } 1015 }
804 else 1016 else
805 { 1017 {
806 /* coroutine already started */ 1018 /* coroutine already started */
807 save_perl (aTHX_ prev); 1019 save_perl (aTHX_ prev);
809 } 1021 }
810 1022
811 prev__cctx = prev->cctx; 1023 prev__cctx = prev->cctx;
812 1024
813 /* possibly "free" the cctx */ 1025 /* possibly "free" the cctx */
814 if (prev__cctx->idle_sp == STACKLEVEL) 1026 if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))
815 { 1027 {
816 /* 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 */
817 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));
818 1030
819 prev->cctx = 0; 1031 prev->cctx = 0;
820 1032
821 cctx_put (prev__cctx); 1033 cctx_put (prev__cctx);
822 prev__cctx->inuse = 0;
823 } 1034 }
824 1035
1036 ++next->usecount;
1037
825 if (!next->cctx) 1038 if (!next->cctx)
826 {
827 next->cctx = cctx_get (aTHX); 1039 next->cctx = cctx_get (aTHX);
828 assert (!next->cctx->inuse);
829 next->cctx->inuse = 1;
830 }
831 1040
832 if (prev__cctx != next->cctx) 1041 if (prev__cctx != next->cctx)
833 { 1042 {
834 prev__cctx->top_env = PL_top_env; 1043 prev__cctx->top_env = PL_top_env;
835 PL_top_env = next->cctx->top_env; 1044 PL_top_env = next->cctx->top_env;
845{ 1054{
846 struct coro *prev, *next; 1055 struct coro *prev, *next;
847}; 1056};
848 1057
849#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)
850 1060
851/** high level stuff ********************************************************/ 1061/** high level stuff ********************************************************/
852 1062
853static int 1063static int
854coro_state_destroy (pTHX_ struct coro *coro) 1064coro_state_destroy (pTHX_ struct coro *coro)
874 struct coro temp; 1084 struct coro temp;
875 1085
876 assert (!(coro->flags & CF_RUNNING)); 1086 assert (!(coro->flags & CF_RUNNING));
877 1087
878 Zero (&temp, 1, struct coro); 1088 Zero (&temp, 1, struct coro);
879 temp.save = CORO_SAVE_DEF; 1089 temp.save = CORO_SAVE_ALL;
880 1090
881 if (coro->flags & CF_RUNNING) 1091 if (coro->flags & CF_RUNNING)
882 croak ("FATAL: tried to destroy currently running coroutine"); 1092 croak ("FATAL: tried to destroy currently running coroutine");
883 1093
884 save_perl (aTHX_ &temp); 1094 save_perl (aTHX_ &temp);
892 } 1102 }
893 1103
894 cctx_destroy (coro->cctx); 1104 cctx_destroy (coro->cctx);
895 SvREFCNT_dec (coro->args); 1105 SvREFCNT_dec (coro->args);
896 1106
1107 if (coro->next) coro->next->prev = coro->prev;
1108 if (coro->prev) coro->prev->next = coro->next;
1109 if (coro == coro_first) coro_first = coro->next;
1110
897 return 1; 1111 return 1;
898} 1112}
899 1113
900static int 1114static int
901coro_state_free (pTHX_ SV *sv, MAGIC *mg) 1115coro_state_free (pTHX_ SV *sv, MAGIC *mg)
902{ 1116{
903 struct coro *coro = (struct coro *)mg->mg_ptr; 1117 struct coro *coro = (struct coro *)mg->mg_ptr;
904 mg->mg_ptr = 0; 1118 mg->mg_ptr = 0;
1119
1120 coro->hv = 0;
905 1121
906 if (--coro->refcnt < 0) 1122 if (--coro->refcnt < 0)
907 { 1123 {
908 coro_state_destroy (aTHX_ coro); 1124 coro_state_destroy (aTHX_ coro);
909 Safefree (coro); 1125 Safefree (coro);
931#else 1147#else
932# define MGf_DUP 0 1148# define MGf_DUP 0
933#endif 1149#endif
934}; 1150};
935 1151
936static struct coro *
937SvSTATE_ (pTHX_ SV *coro)
938{
939 HV *stash;
940 MAGIC *mg;
941
942 if (SvROK (coro))
943 coro = SvRV (coro);
944
945 stash = SvSTASH (coro);
946 if (stash != coro_stash && stash != coro_state_stash)
947 {
948 /* very slow, but rare, check */
949 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
950 croak ("Coro::State object required");
951 }
952
953 mg = CORO_MAGIC (coro);
954 return (struct coro *)mg->mg_ptr;
955}
956
957#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
958
959static void 1152static void
960prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) 1153prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv)
961{ 1154{
962 ta->prev = SvSTATE (prev_sv); 1155 ta->prev = SvSTATE (prev_sv);
963 ta->next = SvSTATE (next_sv); 1156 ta->next = SvSTATE (next_sv);
1157 TRANSFER_CHECK (*ta);
964} 1158}
965 1159
966static void 1160static void
967api_transfer (SV *prev_sv, SV *next_sv) 1161api_transfer (SV *prev_sv, SV *next_sv)
968{ 1162{
1085 break; 1279 break;
1086 } 1280 }
1087 1281
1088 /* free this only after the transfer */ 1282 /* free this only after the transfer */
1089 prev_sv = SvRV (coro_current); 1283 prev_sv = SvRV (coro_current);
1090 SvRV_set (coro_current, next_sv);
1091 ta->prev = SvSTATE (prev_sv); 1284 ta->prev = SvSTATE (prev_sv);
1092 1285 TRANSFER_CHECK (*ta);
1093 assert (ta->next->flags & CF_READY); 1286 assert (ta->next->flags & CF_READY);
1094 ta->next->flags &= ~CF_READY; 1287 ta->next->flags &= ~CF_READY;
1288 SvRV_set (coro_current, next_sv);
1095 1289
1096 LOCK; 1290 LOCK;
1097 free_coro_mortal (aTHX); 1291 free_coro_mortal (aTHX);
1098 coro_mortal = prev_sv; 1292 coro_mortal = prev_sv;
1099 UNLOCK; 1293 UNLOCK;
1160 } 1354 }
1161 else 1355 else
1162 return 0; 1356 return 0;
1163} 1357}
1164 1358
1359static void
1360api_trace (struct coro *coro, int flags)
1361{
1362 dTHX;
1363
1364 if (flags & CC_TRACE)
1365 {
1366 if (!coro->cctx)
1367 coro->cctx = cctx_new ();
1368 else if (!(coro->cctx->flags & CC_TRACE))
1369 croak ("cannot enable tracing on coroutine with custom stack");
1370
1371 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1372 }
1373 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1374 {
1375 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL);
1376
1377 if (coro->flags & CF_RUNNING)
1378 PL_runops = RUNOPS_DEFAULT;
1379 else
1380 coro->runops = RUNOPS_DEFAULT;
1381 }
1382}
1383
1165MODULE = Coro::State PACKAGE = Coro::State 1384MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1166 1385
1167PROTOTYPES: DISABLE 1386PROTOTYPES: DISABLE
1168 1387
1169BOOT: 1388BOOT:
1170{ 1389{
1172 MUTEX_INIT (&coro_mutex); 1391 MUTEX_INIT (&coro_mutex);
1173#endif 1392#endif
1174 BOOT_PAGESIZE; 1393 BOOT_PAGESIZE;
1175 1394
1176 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1395 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1396
1397 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1398 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1399 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1400 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1177 1401
1178 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1402 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1179 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1403 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1180 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1404 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1181 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); 1405 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1206 Newz (0, coro, 1, struct coro); 1430 Newz (0, coro, 1, struct coro);
1207 coro->args = newAV (); 1431 coro->args = newAV ();
1208 coro->save = CORO_SAVE_DEF; 1432 coro->save = CORO_SAVE_DEF;
1209 coro->flags = CF_NEW; 1433 coro->flags = CF_NEW;
1210 1434
1435 if (coro_first) coro_first->prev = coro;
1436 coro->next = coro_first;
1437 coro_first = coro;
1438
1211 hv = newHV (); 1439 coro->hv = hv = newHV ();
1212 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1440 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1213 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1441 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1214 1442
1215 for (i = 1; i < items; i++) 1443 for (i = 1; i < items; i++)
1216 av_push (coro->args, newSVsv (ST (i))); 1444 av_push (coro->args, newSVsv (ST (i)));
1318 CODE: 1546 CODE:
1319 RETVAL = cctx_idle; 1547 RETVAL = cctx_idle;
1320 OUTPUT: 1548 OUTPUT:
1321 RETVAL 1549 RETVAL
1322 1550
1551void
1552list ()
1553 PPCODE:
1554{
1555 struct coro *coro;
1556 for (coro = coro_first; coro; coro = coro->next)
1557 if (coro->hv)
1558 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1559}
1560
1561void
1562call (Coro::State coro, SV *coderef)
1563 ALIAS:
1564 eval = 1
1565 CODE:
1566{
1567 if (coro->mainstack)
1568 {
1569 struct coro temp;
1570 Zero (&temp, 1, struct coro);
1571 temp.save = CORO_SAVE_ALL;
1572
1573 if (!(coro->flags & CF_RUNNING))
1574 {
1575 save_perl (aTHX_ &temp);
1576 load_perl (aTHX_ coro);
1577 }
1578
1579 {
1580 dSP;
1581 ENTER;
1582 SAVETMPS;
1583 PUSHMARK (SP);
1584 PUTBACK;
1585 if (ix)
1586 eval_sv (coderef, 0);
1587 else
1588 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1589 SPAGAIN;
1590 FREETMPS;
1591 LEAVE;
1592 PUTBACK;
1593 }
1594
1595 if (!(coro->flags & CF_RUNNING))
1596 {
1597 save_perl (aTHX_ coro);
1598 load_perl (aTHX_ &temp);
1599 }
1600 }
1601}
1602
1603SV *
1604is_ready (Coro::State coro)
1605 PROTOTYPE: $
1606 ALIAS:
1607 is_ready = CF_READY
1608 is_running = CF_RUNNING
1609 is_new = CF_NEW
1610 is_destroyed = CF_DESTROYED
1611 CODE:
1612 RETVAL = boolSV (coro->flags & ix);
1613 OUTPUT:
1614 RETVAL
1615
1616void
1617api_trace (Coro::State coro, int flags = CC_TRACE | CC_TRACE_SUB)
1618
1619SV *
1620has_stack (Coro::State coro)
1621 PROTOTYPE: $
1622 CODE:
1623 RETVAL = boolSV (!!coro->cctx);
1624 OUTPUT:
1625 RETVAL
1626
1627int
1628is_traced (Coro::State coro)
1629 PROTOTYPE: $
1630 CODE:
1631 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1632 OUTPUT:
1633 RETVAL
1634
1635IV
1636rss (Coro::State coro)
1637 PROTOTYPE: $
1638 ALIAS:
1639 usecount = 1
1640 CODE:
1641 switch (ix)
1642 {
1643 case 0: RETVAL = coro_rss (aTHX_ coro); break;
1644 case 1: RETVAL = coro->usecount; break;
1645 }
1646 OUTPUT:
1647 RETVAL
1648
1649
1323MODULE = Coro::State PACKAGE = Coro 1650MODULE = Coro::State PACKAGE = Coro
1324 1651
1325BOOT: 1652BOOT:
1326{ 1653{
1327 int i; 1654 int i;
1655
1656 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE);
1657 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE);
1658 av_async_pool = get_av ("Coro::async_pool", TRUE);
1659
1660 coro_current = get_sv ("Coro::current", FALSE);
1661 SvREADONLY_on (coro_current);
1328 1662
1329 coro_stash = gv_stashpv ("Coro", TRUE); 1663 coro_stash = gv_stashpv ("Coro", TRUE);
1330 1664
1331 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1665 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1332 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1666 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1333 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1667 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1334 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1668 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1335 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1669 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1336 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1670 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1337
1338 coro_current = get_sv ("Coro::current", FALSE);
1339 SvREADONLY_on (coro_current);
1340 1671
1341 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1672 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1342 coro_ready[i] = newAV (); 1673 coro_ready[i] = newAV ();
1343 1674
1344 { 1675 {
1394 CODE: 1725 CODE:
1395 RETVAL = boolSV (api_ready (self)); 1726 RETVAL = boolSV (api_ready (self));
1396 OUTPUT: 1727 OUTPUT:
1397 RETVAL 1728 RETVAL
1398 1729
1399SV *
1400is_ready (SV *self)
1401 PROTOTYPE: $
1402 CODE:
1403 RETVAL = boolSV (api_is_ready (self));
1404 OUTPUT:
1405 RETVAL
1406
1407int 1730int
1408nready (...) 1731nready (...)
1409 PROTOTYPE: 1732 PROTOTYPE:
1410 CODE: 1733 CODE:
1411 RETVAL = coro_nready; 1734 RETVAL = coro_nready;
1412 OUTPUT: 1735 OUTPUT:
1413 RETVAL 1736 RETVAL
1414 1737
1738# for async_pool speedup
1739void
1740_pool_1 (SV *cb)
1741 CODE:
1742{
1743 int i, len;
1744 HV *hv = (HV *)SvRV (coro_current);
1745 AV *defav = GvAV (PL_defgv);
1746 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1747 AV *invoke_av;
1748
1749 if (!invoke)
1750 croak ("\3terminate\2\n");
1751
1752 hv_store (hv, "desc", sizeof ("desc") - 1,
1753 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1754
1755 invoke_av = (AV *)SvRV (invoke);
1756 len = av_len (invoke_av);
1757
1758 sv_setsv (cb, AvARRAY (invoke_av)[0]);
1759
1760 if (len > 0)
1761 {
1762 av_fill (defav, len - 1);
1763 for (i = 0; i < len; ++i)
1764 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1]));
1765 }
1766
1767 SvREFCNT_dec (invoke);
1768}
1769
1770void
1771_pool_2 (SV *cb)
1772 CODE:
1773{
1774 struct coro *coro = SvSTATE (coro_current);
1775
1776 sv_setsv (cb, &PL_sv_undef);
1777
1778 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1779 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1780 croak ("\3terminate\2\n");
1781
1782 av_clear (GvAV (PL_defgv));
1783 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1784 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1785 coro->save = CORO_SAVE_DEF;
1786 coro->prio = 0;
1787
1788 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1789 api_trace (coro, 0);
1790
1791 av_push (av_async_pool, newSVsv (coro_current));
1792}
1793
1794
1415MODULE = Coro::State PACKAGE = Coro::AIO 1795MODULE = Coro::State PACKAGE = Coro::AIO
1416 1796
1417SV * 1797SV *
1418_get_state () 1798_get_state ()
1419 CODE: 1799 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines