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.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;
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 (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
592/* 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 */
593/* _cctx_init shoukld 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 */
594/* during execution of a pelr program */ 782/* during execution of a perl program */
595static void NOINLINE 783static void NOINLINE
596prepare_cctx (pTHX_ coro_cctx *cctx) 784prepare_cctx (pTHX_ coro_cctx *cctx)
597{ 785{
598 dSP; 786 dSP;
599 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;
600 793
601 Zero (&myop, 1, LOGOP); 794 Zero (&myop, 1, LOGOP);
602 myop.op_next = PL_op; 795 myop.op_next = PL_op;
603 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 796 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
604 797
621 dTHX; 814 dTHX;
622 815
623 /* coro_run is the alternative tail of transfer(), so unlock here. */ 816 /* coro_run is the alternative tail of transfer(), so unlock here. */
624 UNLOCK; 817 UNLOCK;
625 818
626 PL_top_env = &PL_start_env; 819 /* we now skip the entersub that lead to transfer() */
820 PL_op = PL_op->op_next;
627 821
628 /* inject a fake subroutine call to cctx_init */ 822 /* inject a fake subroutine call to cctx_init */
629 prepare_cctx (aTHX_ (coro_cctx *)arg); 823 prepare_cctx (aTHX_ (coro_cctx *)arg);
630 824
631 /* 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 */
632 PL_restartop = PL_op; 826 PL_restartop = PL_op;
633 perl_run (PL_curinterp); 827 perl_run (PL_curinterp);
634 828
829 /*
635 /* If perl-run returns we assume exit() was being called, which */ 830 * 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. */ 831 * 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 */ 832 * 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 */ 833 * bootstrap-time "top" top_env, as we cannot restore the "main"
834 * coroutine as Coro has no such concept
835 */
639 PL_top_env = main_top_env; 836 PL_top_env = main_top_env;
640 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 837 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
641} 838}
642 839
643static coro_cctx * 840static coro_cctx *
662# if CORO_STACKGUARD 859# if CORO_STACKGUARD
663 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 860 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
664# endif 861# endif
665 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 862 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
666 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 863 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
667 cctx->mapped = 1; 864 cctx->flags |= CC_MAPPED;
668 } 865 }
669 else 866 else
670#endif 867#endif
671 { 868 {
672 cctx->ssize = coro_stacksize * (long)sizeof (long); 869 cctx->ssize = coro_stacksize * (long)sizeof (long);
699#if CORO_USE_VALGRIND 896#if CORO_USE_VALGRIND
700 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 897 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
701#endif 898#endif
702 899
703#if HAVE_MMAP 900#if HAVE_MMAP
704 if (cctx->mapped) 901 if (cctx->flags & CC_MAPPED)
705 munmap (cctx->sptr, cctx->ssize); 902 munmap (cctx->sptr, cctx->ssize);
706 else 903 else
707#endif 904#endif
708 Safefree (cctx->sptr); 905 Safefree (cctx->sptr);
709 906
717 { 914 {
718 coro_cctx *cctx = cctx_first; 915 coro_cctx *cctx = cctx_first;
719 cctx_first = cctx->next; 916 cctx_first = cctx->next;
720 --cctx_idle; 917 --cctx_idle;
721 918
722 if (cctx->ssize >= coro_stacksize) 919 if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE))
723 return cctx; 920 return cctx;
724 921
725 cctx_destroy (cctx); 922 cctx_destroy (cctx);
726 } 923 }
727 924
728 PL_op = PL_op->op_next;
729 return cctx_new (); 925 return cctx_new ();
730} 926}
731 927
732static void 928static void
733cctx_put (coro_cctx *cctx) 929cctx_put (coro_cctx *cctx)
737 { 933 {
738 coro_cctx *first = cctx_first; 934 coro_cctx *first = cctx_first;
739 cctx_first = first->next; 935 cctx_first = first->next;
740 --cctx_idle; 936 --cctx_idle;
741 937
742 assert (!first->inuse);
743 cctx_destroy (first); 938 cctx_destroy (first);
744 } 939 }
745 940
746 ++cctx_idle; 941 ++cctx_idle;
747 cctx->next = cctx_first; 942 cctx->next = cctx_first;
748 cctx_first = cctx; 943 cctx_first = cctx;
749} 944}
750 945
751/** coroutine switching *****************************************************/ 946/** coroutine switching *****************************************************/
752 947
753/* 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 */
754static void NOINLINE 968static void NOINLINE
755transfer (pTHX_ struct coro *prev, struct coro *next) 969transfer (pTHX_ struct coro *prev, struct coro *next)
756{ 970{
757 dSTACKLEVEL; 971 dSTACKLEVEL;
758 972
768 982
769 if (prev->flags & CF_NEW) 983 if (prev->flags & CF_NEW)
770 { 984 {
771 /* create a new empty context */ 985 /* create a new empty context */
772 Newz (0, prev->cctx, 1, coro_cctx); 986 Newz (0, prev->cctx, 1, coro_cctx);
773 prev->cctx->inuse = 1;
774 prev->flags &= ~CF_NEW; 987 prev->flags &= ~CF_NEW;
775 prev->flags |= CF_RUNNING; 988 prev->flags |= CF_RUNNING;
776 } 989 }
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 990
788 prev->flags &= ~CF_RUNNING; 991 prev->flags &= ~CF_RUNNING;
789 next->flags |= CF_RUNNING; 992 next->flags |= CF_RUNNING;
790 993
791 LOCK; 994 LOCK;
796 next->flags &= ~CF_NEW; 999 next->flags &= ~CF_NEW;
797 /* first get rid of the old state */ 1000 /* first get rid of the old state */
798 save_perl (aTHX_ prev); 1001 save_perl (aTHX_ prev);
799 /* setup coroutine call */ 1002 /* setup coroutine call */
800 setup_coro (aTHX_ next); 1003 setup_coro (aTHX_ next);
801 /* need a new stack */
802 assert (!next->cctx);
803 } 1004 }
804 else 1005 else
805 { 1006 {
806 /* coroutine already started */ 1007 /* coroutine already started */
807 save_perl (aTHX_ prev); 1008 save_perl (aTHX_ prev);
809 } 1010 }
810 1011
811 prev__cctx = prev->cctx; 1012 prev__cctx = prev->cctx;
812 1013
813 /* possibly "free" the cctx */ 1014 /* possibly "free" the cctx */
814 if (prev__cctx->idle_sp == STACKLEVEL) 1015 if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))
815 { 1016 {
816 /* 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 */
817 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));
818 1019
819 prev->cctx = 0; 1020 prev->cctx = 0;
820 1021
821 cctx_put (prev__cctx); 1022 cctx_put (prev__cctx);
822 prev__cctx->inuse = 0;
823 } 1023 }
824 1024
1025 ++next->usecount;
1026
825 if (!next->cctx) 1027 if (!next->cctx)
826 {
827 next->cctx = cctx_get (aTHX); 1028 next->cctx = cctx_get (aTHX);
828 assert (!next->cctx->inuse);
829 next->cctx->inuse = 1;
830 }
831 1029
832 if (prev__cctx != next->cctx) 1030 if (prev__cctx != next->cctx)
833 { 1031 {
834 prev__cctx->top_env = PL_top_env; 1032 prev__cctx->top_env = PL_top_env;
835 PL_top_env = next->cctx->top_env; 1033 PL_top_env = next->cctx->top_env;
845{ 1043{
846 struct coro *prev, *next; 1044 struct coro *prev, *next;
847}; 1045};
848 1046
849#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)
850 1049
851/** high level stuff ********************************************************/ 1050/** high level stuff ********************************************************/
852 1051
853static int 1052static int
854coro_state_destroy (pTHX_ struct coro *coro) 1053coro_state_destroy (pTHX_ struct coro *coro)
874 struct coro temp; 1073 struct coro temp;
875 1074
876 assert (!(coro->flags & CF_RUNNING)); 1075 assert (!(coro->flags & CF_RUNNING));
877 1076
878 Zero (&temp, 1, struct coro); 1077 Zero (&temp, 1, struct coro);
879 temp.save = CORO_SAVE_DEF; 1078 temp.save = CORO_SAVE_ALL;
880 1079
881 if (coro->flags & CF_RUNNING) 1080 if (coro->flags & CF_RUNNING)
882 croak ("FATAL: tried to destroy currently running coroutine"); 1081 croak ("FATAL: tried to destroy currently running coroutine");
883 1082
884 save_perl (aTHX_ &temp); 1083 save_perl (aTHX_ &temp);
892 } 1091 }
893 1092
894 cctx_destroy (coro->cctx); 1093 cctx_destroy (coro->cctx);
895 SvREFCNT_dec (coro->args); 1094 SvREFCNT_dec (coro->args);
896 1095
1096 if (coro->next) coro->next->prev = coro->prev;
1097 if (coro->prev) coro->prev->next = coro->next;
1098 if (coro == coro_first) coro_first = coro->next;
1099
897 return 1; 1100 return 1;
898} 1101}
899 1102
900static int 1103static int
901coro_state_free (pTHX_ SV *sv, MAGIC *mg) 1104coro_state_free (pTHX_ SV *sv, MAGIC *mg)
902{ 1105{
903 struct coro *coro = (struct coro *)mg->mg_ptr; 1106 struct coro *coro = (struct coro *)mg->mg_ptr;
904 mg->mg_ptr = 0; 1107 mg->mg_ptr = 0;
1108
1109 coro->hv = 0;
905 1110
906 if (--coro->refcnt < 0) 1111 if (--coro->refcnt < 0)
907 { 1112 {
908 coro_state_destroy (aTHX_ coro); 1113 coro_state_destroy (aTHX_ coro);
909 Safefree (coro); 1114 Safefree (coro);
931#else 1136#else
932# define MGf_DUP 0 1137# define MGf_DUP 0
933#endif 1138#endif
934}; 1139};
935 1140
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 1141static void
960prepare_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)
961{ 1143{
962 ta->prev = SvSTATE (prev_sv); 1144 ta->prev = SvSTATE (prev_sv);
963 ta->next = SvSTATE (next_sv); 1145 ta->next = SvSTATE (next_sv);
1146 TRANSFER_CHECK (*ta);
964} 1147}
965 1148
966static void 1149static void
967api_transfer (SV *prev_sv, SV *next_sv) 1150api_transfer (SV *prev_sv, SV *next_sv)
968{ 1151{
1085 break; 1268 break;
1086 } 1269 }
1087 1270
1088 /* free this only after the transfer */ 1271 /* free this only after the transfer */
1089 prev_sv = SvRV (coro_current); 1272 prev_sv = SvRV (coro_current);
1090 SvRV_set (coro_current, next_sv);
1091 ta->prev = SvSTATE (prev_sv); 1273 ta->prev = SvSTATE (prev_sv);
1092 1274 TRANSFER_CHECK (*ta);
1093 assert (ta->next->flags & CF_READY); 1275 assert (ta->next->flags & CF_READY);
1094 ta->next->flags &= ~CF_READY; 1276 ta->next->flags &= ~CF_READY;
1277 SvRV_set (coro_current, next_sv);
1095 1278
1096 LOCK; 1279 LOCK;
1097 free_coro_mortal (aTHX); 1280 free_coro_mortal (aTHX);
1098 coro_mortal = prev_sv; 1281 coro_mortal = prev_sv;
1099 UNLOCK; 1282 UNLOCK;
1172 MUTEX_INIT (&coro_mutex); 1355 MUTEX_INIT (&coro_mutex);
1173#endif 1356#endif
1174 BOOT_PAGESIZE; 1357 BOOT_PAGESIZE;
1175 1358
1176 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));
1177 1365
1178 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1366 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1179 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1367 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1180 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1368 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1181 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); 1369 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1206 Newz (0, coro, 1, struct coro); 1394 Newz (0, coro, 1, struct coro);
1207 coro->args = newAV (); 1395 coro->args = newAV ();
1208 coro->save = CORO_SAVE_DEF; 1396 coro->save = CORO_SAVE_DEF;
1209 coro->flags = CF_NEW; 1397 coro->flags = CF_NEW;
1210 1398
1399 if (coro_first) coro_first->prev = coro;
1400 coro->next = coro_first;
1401 coro_first = coro;
1402
1211 hv = newHV (); 1403 coro->hv = hv = newHV ();
1212 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;
1213 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1405 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1214 1406
1215 for (i = 1; i < items; i++) 1407 for (i = 1; i < items; i++)
1216 av_push (coro->args, newSVsv (ST (i))); 1408 av_push (coro->args, newSVsv (ST (i)));
1318 CODE: 1510 CODE:
1319 RETVAL = cctx_idle; 1511 RETVAL = cctx_idle;
1320 OUTPUT: 1512 OUTPUT:
1321 RETVAL 1513 RETVAL
1322 1514
1515void
1516list ()
1517 PPCODE:
1518{
1519 struct coro *coro;
1520 for (coro = coro_first; coro; coro = coro->next)
1521 if (coro->hv)
1522 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1523}
1524
1525void
1526call (Coro::State coro, SV *coderef)
1527 ALIAS:
1528 eval = 1
1529 CODE:
1530{
1531 if (coro->mainstack)
1532 {
1533 struct coro temp;
1534 Zero (&temp, 1, struct coro);
1535 temp.save = CORO_SAVE_ALL;
1536
1537 if (!(coro->flags & CF_RUNNING))
1538 {
1539 save_perl (aTHX_ &temp);
1540 load_perl (aTHX_ coro);
1541 }
1542
1543 {
1544 dSP;
1545 ENTER;
1546 SAVETMPS;
1547 PUSHMARK (SP);
1548 PUTBACK;
1549 if (ix)
1550 eval_sv (coderef, 0);
1551 else
1552 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1553 SPAGAIN;
1554 FREETMPS;
1555 LEAVE;
1556 PUTBACK;
1557 }
1558
1559 if (!(coro->flags & CF_RUNNING))
1560 {
1561 save_perl (aTHX_ coro);
1562 load_perl (aTHX_ &temp);
1563 }
1564 }
1565}
1566
1567SV *
1568is_ready (Coro::State coro)
1569 PROTOTYPE: $
1570 ALIAS:
1571 is_ready = CF_READY
1572 is_running = CF_RUNNING
1573 is_new = CF_NEW
1574 is_destroyed = CF_DESTROYED
1575 CODE:
1576 RETVAL = boolSV (coro->flags & ix);
1577 OUTPUT:
1578 RETVAL
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
1620IV
1621rss (Coro::State coro)
1622 PROTOTYPE: $
1623 ALIAS:
1624 usecount = 1
1625 CODE:
1626 switch (ix)
1627 {
1628 case 0: RETVAL = coro_rss (aTHX_ coro); break;
1629 case 1: RETVAL = coro->usecount; break;
1630 }
1631 OUTPUT:
1632 RETVAL
1633
1634
1323MODULE = Coro::State PACKAGE = Coro 1635MODULE = Coro::State PACKAGE = Coro
1324 1636
1325BOOT: 1637BOOT:
1326{ 1638{
1327 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);
1328 1647
1329 coro_stash = gv_stashpv ("Coro", TRUE); 1648 coro_stash = gv_stashpv ("Coro", TRUE);
1330 1649
1331 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1650 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1332 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1651 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1333 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1652 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1334 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1653 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1335 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1654 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1336 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1655 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1337
1338 coro_current = get_sv ("Coro::current", FALSE);
1339 SvREADONLY_on (coro_current);
1340 1656
1341 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1657 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1342 coro_ready[i] = newAV (); 1658 coro_ready[i] = newAV ();
1343 1659
1344 { 1660 {
1394 CODE: 1710 CODE:
1395 RETVAL = boolSV (api_ready (self)); 1711 RETVAL = boolSV (api_ready (self));
1396 OUTPUT: 1712 OUTPUT:
1397 RETVAL 1713 RETVAL
1398 1714
1399SV *
1400is_ready (SV *self)
1401 PROTOTYPE: $
1402 CODE:
1403 RETVAL = boolSV (api_is_ready (self));
1404 OUTPUT:
1405 RETVAL
1406
1407int 1715int
1408nready (...) 1716nready (...)
1409 PROTOTYPE: 1717 PROTOTYPE:
1410 CODE: 1718 CODE:
1411 RETVAL = coro_nready; 1719 RETVAL = coro_nready;
1412 OUTPUT: 1720 OUTPUT:
1413 RETVAL 1721 RETVAL
1414 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
1415MODULE = Coro::State PACKAGE = Coro::AIO 1776MODULE = Coro::State PACKAGE = Coro::AIO
1416 1777
1417SV * 1778SV *
1418_get_state () 1779_get_state ()
1419 CODE: 1780 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines