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.150 by root, Wed Jun 13 21:04:48 2007 UTC vs.
Revision 1.171 by root, Sat Sep 29 19:42:10 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 */
188# include "state.h" 201# include "state.h"
189#undef VAR 202#undef VAR
190 203
191 /* coro process data */ 204 /* coro process data */
192 int prio; 205 int prio;
206
207 /* linked list */
208 struct coro *next, *prev;
209 HV *hv; /* the perl hash associated with this coro, if any */
193}; 210};
194 211
195typedef struct coro *Coro__State; 212typedef struct coro *Coro__State;
196typedef struct coro *Coro__State_or_hashref; 213typedef struct coro *Coro__State_or_hashref;
197 214
206 223
207/* for Coro.pm */ 224/* for Coro.pm */
208static SV *coro_current; 225static SV *coro_current;
209static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 226static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
210static int coro_nready; 227static int coro_nready;
228static struct coro *coro_first;
211 229
212/** lowlevel stuff **********************************************************/ 230/** lowlevel stuff **********************************************************/
213 231
214static AV * 232static AV *
215coro_clone_padlist (pTHX_ CV *cv) 233coro_clone_padlist (pTHX_ CV *cv)
280 SvMAGIC (cv) \ 298 SvMAGIC (cv) \
281 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ 299 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
282 ? SvMAGIC (cv) \ 300 ? SvMAGIC (cv) \
283 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 301 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
284 : 0 302 : 0
303
304static struct coro *
305SvSTATE_ (pTHX_ SV *coro)
306{
307 HV *stash;
308 MAGIC *mg;
309
310 if (SvROK (coro))
311 coro = SvRV (coro);
312
313 stash = SvSTASH (coro);
314 if (stash != coro_stash && stash != coro_state_stash)
315 {
316 /* very slow, but rare, check */
317 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
318 croak ("Coro::State object required");
319 }
320
321 mg = CORO_MAGIC (coro);
322 return (struct coro *)mg->mg_ptr;
323}
324
325#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
285 326
286/* the next two functions merely cache the padlists */ 327/* the next two functions merely cache the padlists */
287static void 328static void
288get_padlist (pTHX_ CV *cv) 329get_padlist (pTHX_ CV *cv)
289{ 330{
374 CvPADLIST (cv) = (AV *)POPs; 415 CvPADLIST (cv) = (AV *)POPs;
375 } 416 }
376 417
377 PUTBACK; 418 PUTBACK;
378 } 419 }
379 assert (!PL_comppad || AvARRAY (PL_comppad));//D
380} 420}
381 421
382static void 422static void
383save_perl (pTHX_ Coro__State c) 423save_perl (pTHX_ Coro__State c)
384{ 424{
385 assert (!PL_comppad || AvARRAY (PL_comppad));//D
386 { 425 {
387 dSP; 426 dSP;
388 I32 cxix = cxstack_ix; 427 I32 cxix = cxstack_ix;
389 PERL_CONTEXT *ccstk = cxstack; 428 PERL_CONTEXT *ccstk = cxstack;
390 PERL_SI *top_si = PL_curstackinfo; 429 PERL_SI *top_si = PL_curstackinfo;
392 /* 431 /*
393 * the worst thing you can imagine happens first - we have to save 432 * the worst thing you can imagine happens first - we have to save
394 * (and reinitialize) all cv's in the whole callchain :( 433 * (and reinitialize) all cv's in the whole callchain :(
395 */ 434 */
396 435
397 EXTEND (SP, 3 + 1);
398 PUSHs (Nullsv); 436 XPUSHs (Nullsv);
399 /* this loop was inspired by pp_caller */ 437 /* this loop was inspired by pp_caller */
400 for (;;) 438 for (;;)
401 { 439 {
402 while (cxix >= 0) 440 while (cxix >= 0)
403 { 441 {
404 PERL_CONTEXT *cx = &ccstk[cxix--]; 442 PERL_CONTEXT *cx = &ccstk[cxix--];
405 443
406 if (CxTYPE (cx) == CXt_SUB) 444 if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)
407 { 445 {
408 CV *cv = cx->blk_sub.cv; 446 CV *cv = cx->blk_sub.cv;
409 447
410 if (CvDEPTH (cv)) 448 if (CvDEPTH (cv))
411 { 449 {
422 460
423 if (top_si->si_type == PERLSI_MAIN) 461 if (top_si->si_type == PERLSI_MAIN)
424 break; 462 break;
425 463
426 top_si = top_si->si_prev; 464 top_si = top_si->si_prev;
427 ccstk = top_si->si_cxstack; 465 ccstk = top_si->si_cxstack;
428 cxix = top_si->si_cxix; 466 cxix = top_si->si_cxix;
429 } 467 }
430 468
431 PUTBACK; 469 PUTBACK;
432 } 470 }
433 471
452# define coro_init_stacks init_stacks 490# define coro_init_stacks init_stacks
453#else 491#else
454static void 492static void
455coro_init_stacks (pTHX) 493coro_init_stacks (pTHX)
456{ 494{
457 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 495 PL_curstackinfo = new_stackinfo(64, 6);
458 PL_curstackinfo->si_type = PERLSI_MAIN; 496 PL_curstackinfo->si_type = PERLSI_MAIN;
459 PL_curstack = PL_curstackinfo->si_stack; 497 PL_curstack = PL_curstackinfo->si_stack;
460 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 498 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
461 499
462 PL_stack_base = AvARRAY(PL_curstack); 500 PL_stack_base = AvARRAY(PL_curstack);
463 PL_stack_sp = PL_stack_base; 501 PL_stack_sp = PL_stack_base;
464 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 502 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
465 503
466 New(50,PL_tmps_stack,128,SV*); 504 New(50,PL_tmps_stack,64,SV*);
467 PL_tmps_floor = -1; 505 PL_tmps_floor = -1;
468 PL_tmps_ix = -1; 506 PL_tmps_ix = -1;
469 PL_tmps_max = 128; 507 PL_tmps_max = 64;
470 508
471 New(54,PL_markstack,32,I32); 509 New(54,PL_markstack,16,I32);
472 PL_markstack_ptr = PL_markstack; 510 PL_markstack_ptr = PL_markstack;
473 PL_markstack_max = PL_markstack + 32; 511 PL_markstack_max = PL_markstack + 16;
474 512
475#ifdef SET_MARK_OFFSET 513#ifdef SET_MARK_OFFSET
476 SET_MARK_OFFSET; 514 SET_MARK_OFFSET;
477#endif 515#endif
478 516
479 New(54,PL_scopestack,32,I32); 517 New(54,PL_scopestack,16,I32);
480 PL_scopestack_ix = 0; 518 PL_scopestack_ix = 0;
481 PL_scopestack_max = 32; 519 PL_scopestack_max = 16;
482 520
483 New(54,PL_savestack,64,ANY); 521 New(54,PL_savestack,64,ANY);
484 PL_savestack_ix = 0; 522 PL_savestack_ix = 0;
485 PL_savestack_max = 64; 523 PL_savestack_max = 64;
486 524
487#if !PERL_VERSION_ATLEAST (5,9,0) 525#if !PERL_VERSION_ATLEAST (5,9,0)
488 New(54,PL_retstack,16,OP*); 526 New(54,PL_retstack,4,OP*);
489 PL_retstack_ix = 0; 527 PL_retstack_ix = 0;
490 PL_retstack_max = 16; 528 PL_retstack_max = 4;
491#endif 529#endif
492} 530}
493#endif 531#endif
494 532
495/* 533/*
537#if !PERL_VERSION_ATLEAST (5,9,0) 575#if !PERL_VERSION_ATLEAST (5,9,0)
538 Safefree (PL_retstack); 576 Safefree (PL_retstack);
539#endif 577#endif
540} 578}
541 579
580static size_t
581coro_rss (pTHX_ struct coro *coro)
582{
583 size_t rss = sizeof (coro);
584
585 if (coro->mainstack)
586 {
587 if (coro->flags & CF_RUNNING)
588 {
589 #define VAR(name,type)coro->name = PL_ ## name;
590 # include "state.h"
591 #undef VAR
592 }
593
594 rss += sizeof (coro->curstackinfo);
595 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *);
596 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
597 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *);
598 rss += coro->tmps_max * sizeof (SV *);
599 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32);
600 rss += coro->scopestack_max * sizeof (I32);
601 rss += coro->savestack_max * sizeof (ANY);
602
603#if !PERL_VERSION_ATLEAST (5,9,0)
604 rss += coro->retstack_max * sizeof (OP *);
605#endif
606 }
607
608 return rss;
609}
610
542/** coroutine stack handling ************************************************/ 611/** coroutine stack handling ************************************************/
543 612
544static void 613static void
545setup_coro (pTHX_ struct coro *coro) 614setup_coro (pTHX_ struct coro *coro)
546{ 615{
547 /* 616 /*
548 * emulate part of the perl startup here. 617 * emulate part of the perl startup here.
549 */ 618 */
550 coro_init_stacks (aTHX); 619 coro_init_stacks (aTHX);
551 620
621 PL_runops = RUNOPS_DEFAULT;
552 PL_curcop = &PL_compiling; 622 PL_curcop = &PL_compiling;
553 PL_in_eval = EVAL_NULL; 623 PL_in_eval = EVAL_NULL;
554 PL_comppad = 0; 624 PL_comppad = 0;
555 PL_curpm = 0; 625 PL_curpm = 0;
556 PL_localizing = 0; 626 PL_localizing = 0;
587 SvREFCNT_dec (coro_mortal); 657 SvREFCNT_dec (coro_mortal);
588 coro_mortal = 0; 658 coro_mortal = 0;
589 } 659 }
590} 660}
591 661
662static int
663runops_trace (pTHX)
664{
665 COP *oldcop = 0;
666 int oldcxix = -2;
667 struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */
668 coro_cctx *cctx = coro->cctx;
669
670 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
671 {
672 PERL_ASYNC_CHECK ();
673
674 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB)
675 {
676 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
677 SV **bot, **top;
678 AV *av = newAV (); /* return values */
679 SV **cb;
680 runops_proc_t old_runops = PL_runops;
681 dSP;
682 ENTER;
683 SAVETMPS;
684 EXTEND (SP, 3);
685 PL_runops = RUNOPS_DEFAULT;
686
687 GV *gv = CvGV (cx->blk_sub.cv);
688 SV *fullname = sv_2mortal (newSV (0));
689 if (isGV (gv))
690 gv_efullname3 (fullname, gv, 0);
691
692 bot = PL_stack_base + cx->blk_oldsp + 1;
693 top = cx->blk_gimme == G_ARRAY ? SP + 1
694 : cx->blk_gimme == G_SCALAR ? bot + 1
695 : bot;
696
697 while (bot < top)
698 av_push (av, SvREFCNT_inc (*bot++));
699
700 PUSHMARK (SP);
701 PUSHs (&PL_sv_no);
702 PUSHs (fullname);
703 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
704 PUTBACK;
705 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
706 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
707 SPAGAIN;
708
709 FREETMPS;
710 LEAVE;
711 PL_runops = old_runops;
712 }
713
714 if (oldcop != PL_curcop)
715 {
716 oldcop = PL_curcop;
717
718 if (PL_curcop != &PL_compiling)
719 {
720 SV **cb;
721 runops_proc_t old_runops = PL_runops;
722 dSP;
723 ENTER;
724 SAVETMPS;
725 EXTEND (SP, 3);
726 PL_runops = RUNOPS_DEFAULT;
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 GV *gv = CvGV (cx->blk_sub.cv);
735 SV *fullname = sv_2mortal (newSV (0));
736 if (isGV (gv))
737 gv_efullname3 (fullname, gv, 0);
738
739 PUSHMARK (SP);
740 PUSHs (&PL_sv_yes);
741 PUSHs (fullname);
742 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
743 PUTBACK;
744 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
745 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
746 SPAGAIN;
747 }
748
749 oldcxix = cxstack_ix;
750 }
751
752 if (cctx->flags & CC_TRACE_LINE)
753 {
754 PUSHMARK (SP);
755 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
756 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop))));
757 PUTBACK;
758 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0);
759 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
760 SPAGAIN;
761 }
762
763 FREETMPS;
764 LEAVE;
765 PL_runops = old_runops;
766 }
767 }
768 }
769
770 TAINT_NOT;
771 return 0;
772}
773
592/* inject a fake call to Coro::State::_cctx_init into the execution */ 774/* inject a fake call to Coro::State::_cctx_init into the execution */
593/* _cctx_init should be careful, as it could be called at almost any time */ 775/* _cctx_init should be careful, as it could be called at almost any time */
594/* during execution of a perl program */ 776/* during execution of a perl program */
595static void NOINLINE 777static void NOINLINE
596prepare_cctx (pTHX_ coro_cctx *cctx) 778prepare_cctx (pTHX_ coro_cctx *cctx)
597{ 779{
598 dSP; 780 dSP;
599 LOGOP myop; 781 LOGOP myop;
782
783 PL_top_env = &PL_start_env;
784
785 if (cctx->flags & CC_TRACE)
786 PL_runops = runops_trace;
600 787
601 Zero (&myop, 1, LOGOP); 788 Zero (&myop, 1, LOGOP);
602 myop.op_next = PL_op; 789 myop.op_next = PL_op;
603 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 790 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
604 791
621 dTHX; 808 dTHX;
622 809
623 /* coro_run is the alternative tail of transfer(), so unlock here. */ 810 /* coro_run is the alternative tail of transfer(), so unlock here. */
624 UNLOCK; 811 UNLOCK;
625 812
626 PL_top_env = &PL_start_env; 813 /* we now skip the entersub that lead to transfer() */
814 PL_op = PL_op->op_next;
627 815
628 /* inject a fake subroutine call to cctx_init */ 816 /* inject a fake subroutine call to cctx_init */
629 prepare_cctx (aTHX_ (coro_cctx *)arg); 817 prepare_cctx (aTHX_ (coro_cctx *)arg);
630 818
631 /* somebody or something will hit me for both perl_run and PL_restartop */ 819 /* somebody or something will hit me for both perl_run and PL_restartop */
665# if CORO_STACKGUARD 853# if CORO_STACKGUARD
666 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 854 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
667# endif 855# endif
668 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 856 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
669 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 857 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
670 cctx->mapped = 1; 858 cctx->flags |= CC_MAPPED;
671 } 859 }
672 else 860 else
673#endif 861#endif
674 { 862 {
675 cctx->ssize = coro_stacksize * (long)sizeof (long); 863 cctx->ssize = coro_stacksize * (long)sizeof (long);
702#if CORO_USE_VALGRIND 890#if CORO_USE_VALGRIND
703 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 891 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
704#endif 892#endif
705 893
706#if HAVE_MMAP 894#if HAVE_MMAP
707 if (cctx->mapped) 895 if (cctx->flags & CC_MAPPED)
708 munmap (cctx->sptr, cctx->ssize); 896 munmap (cctx->sptr, cctx->ssize);
709 else 897 else
710#endif 898#endif
711 Safefree (cctx->sptr); 899 Safefree (cctx->sptr);
712 900
720 { 908 {
721 coro_cctx *cctx = cctx_first; 909 coro_cctx *cctx = cctx_first;
722 cctx_first = cctx->next; 910 cctx_first = cctx->next;
723 --cctx_idle; 911 --cctx_idle;
724 912
725 if (cctx->ssize >= coro_stacksize) 913 if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE))
726 return cctx; 914 return cctx;
727 915
728 cctx_destroy (cctx); 916 cctx_destroy (cctx);
729 } 917 }
730 918
731 PL_op = PL_op->op_next;
732 return cctx_new (); 919 return cctx_new ();
733} 920}
734 921
735static void 922static void
736cctx_put (coro_cctx *cctx) 923cctx_put (coro_cctx *cctx)
740 { 927 {
741 coro_cctx *first = cctx_first; 928 coro_cctx *first = cctx_first;
742 cctx_first = first->next; 929 cctx_first = first->next;
743 --cctx_idle; 930 --cctx_idle;
744 931
745 assert (!first->inuse);
746 cctx_destroy (first); 932 cctx_destroy (first);
747 } 933 }
748 934
749 ++cctx_idle; 935 ++cctx_idle;
750 cctx->next = cctx_first; 936 cctx->next = cctx_first;
751 cctx_first = cctx; 937 cctx_first = cctx;
752} 938}
753 939
754/** coroutine switching *****************************************************/ 940/** coroutine switching *****************************************************/
755 941
756/* never call directly, always through the coro_state_transfer global variable */ 942static void NOINLINE
943transfer_check (pTHX_ struct coro *prev, struct coro *next)
944{
945 if (prev != next)
946 {
947 if (!(prev->flags & (CF_RUNNING | CF_NEW)))
948 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states");
949
950 if (next->flags & CF_RUNNING)
951 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
952
953 if (next->flags & CF_DESTROYED)
954 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
955
956 if (PL_lex_state != LEX_NOTPARSING)
957 croak ("Coro::State::transfer called while parsing, but this is not supported");
958 }
959}
960
961/* always use the TRANSFER macro */
757static void NOINLINE 962static void NOINLINE
758transfer (pTHX_ struct coro *prev, struct coro *next) 963transfer (pTHX_ struct coro *prev, struct coro *next)
759{ 964{
760 dSTACKLEVEL; 965 dSTACKLEVEL;
761 966
771 976
772 if (prev->flags & CF_NEW) 977 if (prev->flags & CF_NEW)
773 { 978 {
774 /* create a new empty context */ 979 /* create a new empty context */
775 Newz (0, prev->cctx, 1, coro_cctx); 980 Newz (0, prev->cctx, 1, coro_cctx);
776 prev->cctx->inuse = 1;
777 prev->flags &= ~CF_NEW; 981 prev->flags &= ~CF_NEW;
778 prev->flags |= CF_RUNNING; 982 prev->flags |= CF_RUNNING;
779 } 983 }
780
781 /*TODO: must not croak here */
782 if (!prev->flags & CF_RUNNING)
783 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
784
785 if (next->flags & CF_RUNNING)
786 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
787
788 if (next->flags & CF_DESTROYED)
789 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
790 984
791 prev->flags &= ~CF_RUNNING; 985 prev->flags &= ~CF_RUNNING;
792 next->flags |= CF_RUNNING; 986 next->flags |= CF_RUNNING;
793 987
794 LOCK; 988 LOCK;
799 next->flags &= ~CF_NEW; 993 next->flags &= ~CF_NEW;
800 /* first get rid of the old state */ 994 /* first get rid of the old state */
801 save_perl (aTHX_ prev); 995 save_perl (aTHX_ prev);
802 /* setup coroutine call */ 996 /* setup coroutine call */
803 setup_coro (aTHX_ next); 997 setup_coro (aTHX_ next);
804 /* need a new stack */
805 assert (!next->cctx);
806 } 998 }
807 else 999 else
808 { 1000 {
809 /* coroutine already started */ 1001 /* coroutine already started */
810 save_perl (aTHX_ prev); 1002 save_perl (aTHX_ prev);
812 } 1004 }
813 1005
814 prev__cctx = prev->cctx; 1006 prev__cctx = prev->cctx;
815 1007
816 /* possibly "free" the cctx */ 1008 /* possibly "free" the cctx */
817 if (prev__cctx->idle_sp == STACKLEVEL) 1009 if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))
818 { 1010 {
819 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1011 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
820 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 1012 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
821 1013
822 prev->cctx = 0; 1014 prev->cctx = 0;
823 1015
824 cctx_put (prev__cctx); 1016 cctx_put (prev__cctx);
825 prev__cctx->inuse = 0;
826 } 1017 }
827 1018
828 if (!next->cctx) 1019 if (!next->cctx)
829 {
830 next->cctx = cctx_get (aTHX); 1020 next->cctx = cctx_get (aTHX);
831 assert (!next->cctx->inuse);
832 next->cctx->inuse = 1;
833 }
834 1021
835 if (prev__cctx != next->cctx) 1022 if (prev__cctx != next->cctx)
836 { 1023 {
837 prev__cctx->top_env = PL_top_env; 1024 prev__cctx->top_env = PL_top_env;
838 PL_top_env = next->cctx->top_env; 1025 PL_top_env = next->cctx->top_env;
848{ 1035{
849 struct coro *prev, *next; 1036 struct coro *prev, *next;
850}; 1037};
851 1038
852#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) 1039#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next)
1040#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
853 1041
854/** high level stuff ********************************************************/ 1042/** high level stuff ********************************************************/
855 1043
856static int 1044static int
857coro_state_destroy (pTHX_ struct coro *coro) 1045coro_state_destroy (pTHX_ struct coro *coro)
877 struct coro temp; 1065 struct coro temp;
878 1066
879 assert (!(coro->flags & CF_RUNNING)); 1067 assert (!(coro->flags & CF_RUNNING));
880 1068
881 Zero (&temp, 1, struct coro); 1069 Zero (&temp, 1, struct coro);
882 temp.save = CORO_SAVE_DEF; 1070 temp.save = CORO_SAVE_ALL;
883 1071
884 if (coro->flags & CF_RUNNING) 1072 if (coro->flags & CF_RUNNING)
885 croak ("FATAL: tried to destroy currently running coroutine"); 1073 croak ("FATAL: tried to destroy currently running coroutine");
886 1074
887 save_perl (aTHX_ &temp); 1075 save_perl (aTHX_ &temp);
895 } 1083 }
896 1084
897 cctx_destroy (coro->cctx); 1085 cctx_destroy (coro->cctx);
898 SvREFCNT_dec (coro->args); 1086 SvREFCNT_dec (coro->args);
899 1087
1088 if (coro->next) coro->next->prev = coro->prev;
1089 if (coro->prev) coro->prev->next = coro->next;
1090 if (coro == coro_first) coro_first = coro->next;
1091
900 return 1; 1092 return 1;
901} 1093}
902 1094
903static int 1095static int
904coro_state_free (pTHX_ SV *sv, MAGIC *mg) 1096coro_state_free (pTHX_ SV *sv, MAGIC *mg)
905{ 1097{
906 struct coro *coro = (struct coro *)mg->mg_ptr; 1098 struct coro *coro = (struct coro *)mg->mg_ptr;
907 mg->mg_ptr = 0; 1099 mg->mg_ptr = 0;
1100
1101 coro->hv = 0;
908 1102
909 if (--coro->refcnt < 0) 1103 if (--coro->refcnt < 0)
910 { 1104 {
911 coro_state_destroy (aTHX_ coro); 1105 coro_state_destroy (aTHX_ coro);
912 Safefree (coro); 1106 Safefree (coro);
934#else 1128#else
935# define MGf_DUP 0 1129# define MGf_DUP 0
936#endif 1130#endif
937}; 1131};
938 1132
939static struct coro *
940SvSTATE_ (pTHX_ SV *coro)
941{
942 HV *stash;
943 MAGIC *mg;
944
945 if (SvROK (coro))
946 coro = SvRV (coro);
947
948 stash = SvSTASH (coro);
949 if (stash != coro_stash && stash != coro_state_stash)
950 {
951 /* very slow, but rare, check */
952 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
953 croak ("Coro::State object required");
954 }
955
956 mg = CORO_MAGIC (coro);
957 return (struct coro *)mg->mg_ptr;
958}
959
960#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
961
962static void 1133static void
963prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) 1134prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv)
964{ 1135{
965 ta->prev = SvSTATE (prev_sv); 1136 ta->prev = SvSTATE (prev_sv);
966 ta->next = SvSTATE (next_sv); 1137 ta->next = SvSTATE (next_sv);
1138 TRANSFER_CHECK (*ta);
967} 1139}
968 1140
969static void 1141static void
970api_transfer (SV *prev_sv, SV *next_sv) 1142api_transfer (SV *prev_sv, SV *next_sv)
971{ 1143{
1088 break; 1260 break;
1089 } 1261 }
1090 1262
1091 /* free this only after the transfer */ 1263 /* free this only after the transfer */
1092 prev_sv = SvRV (coro_current); 1264 prev_sv = SvRV (coro_current);
1093 SvRV_set (coro_current, next_sv);
1094 ta->prev = SvSTATE (prev_sv); 1265 ta->prev = SvSTATE (prev_sv);
1095 1266 TRANSFER_CHECK (*ta);
1096 assert (ta->next->flags & CF_READY); 1267 assert (ta->next->flags & CF_READY);
1097 ta->next->flags &= ~CF_READY; 1268 ta->next->flags &= ~CF_READY;
1269 SvRV_set (coro_current, next_sv);
1098 1270
1099 LOCK; 1271 LOCK;
1100 free_coro_mortal (aTHX); 1272 free_coro_mortal (aTHX);
1101 coro_mortal = prev_sv; 1273 coro_mortal = prev_sv;
1102 UNLOCK; 1274 UNLOCK;
1175 MUTEX_INIT (&coro_mutex); 1347 MUTEX_INIT (&coro_mutex);
1176#endif 1348#endif
1177 BOOT_PAGESIZE; 1349 BOOT_PAGESIZE;
1178 1350
1179 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1351 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1352
1353 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1354 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1355 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1356 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1180 1357
1181 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1358 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1182 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1359 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1183 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1360 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1184 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); 1361 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1209 Newz (0, coro, 1, struct coro); 1386 Newz (0, coro, 1, struct coro);
1210 coro->args = newAV (); 1387 coro->args = newAV ();
1211 coro->save = CORO_SAVE_DEF; 1388 coro->save = CORO_SAVE_DEF;
1212 coro->flags = CF_NEW; 1389 coro->flags = CF_NEW;
1213 1390
1391 if (coro_first) coro_first->prev = coro;
1392 coro->next = coro_first;
1393 coro_first = coro;
1394
1214 hv = newHV (); 1395 coro->hv = hv = newHV ();
1215 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1396 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1216 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1397 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1217 1398
1218 for (i = 1; i < items; i++) 1399 for (i = 1; i < items; i++)
1219 av_push (coro->args, newSVsv (ST (i))); 1400 av_push (coro->args, newSVsv (ST (i)));
1321 CODE: 1502 CODE:
1322 RETVAL = cctx_idle; 1503 RETVAL = cctx_idle;
1323 OUTPUT: 1504 OUTPUT:
1324 RETVAL 1505 RETVAL
1325 1506
1507void
1508list ()
1509 PPCODE:
1510{
1511 struct coro *coro;
1512 for (coro = coro_first; coro; coro = coro->next)
1513 if (coro->hv)
1514 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1515}
1516
1517void
1518call (Coro::State coro, SV *coderef)
1519 ALIAS:
1520 eval = 1
1521 CODE:
1522{
1523 if (coro->mainstack)
1524 {
1525 struct coro temp;
1526 Zero (&temp, 1, struct coro);
1527 temp.save = CORO_SAVE_ALL;
1528
1529 if (!(coro->flags & CF_RUNNING))
1530 {
1531 save_perl (aTHX_ &temp);
1532 load_perl (aTHX_ coro);
1533 }
1534
1535 {
1536 dSP;
1537 ENTER;
1538 SAVETMPS;
1539 PUSHMARK (SP);
1540 PUTBACK;
1541 if (ix)
1542 eval_sv (coderef, 0);
1543 else
1544 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1545 SPAGAIN;
1546 FREETMPS;
1547 LEAVE;
1548 PUTBACK;
1549 }
1550
1551 if (!(coro->flags & CF_RUNNING))
1552 {
1553 save_perl (aTHX_ coro);
1554 load_perl (aTHX_ &temp);
1555 }
1556 }
1557}
1558
1559SV *
1560is_ready (Coro::State coro)
1561 PROTOTYPE: $
1562 ALIAS:
1563 is_ready = CF_READY
1564 is_running = CF_RUNNING
1565 is_new = CF_NEW
1566 is_destroyed = CF_DESTROYED
1567 CODE:
1568 RETVAL = boolSV (coro->flags & ix);
1569 OUTPUT:
1570 RETVAL
1571
1572void
1573trace (Coro::State coro, int flags = CC_TRACE | CC_TRACE_SUB)
1574 CODE:
1575 if (flags & CC_TRACE)
1576 {
1577 if (!coro->cctx)
1578 coro->cctx = cctx_new ();
1579 else if (!(coro->cctx->flags & CC_TRACE))
1580 croak ("cannot enable tracing on coroutine with custom stack");
1581
1582 coro->cctx->flags |= flags & (CC_TRACE | CC_TRACE_ALL);
1583 }
1584 else
1585 if (coro->cctx && coro->cctx->flags & CC_TRACE)
1586 {
1587 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL);
1588 coro->cctx->flags |= CC_NOREUSE;
1589
1590 if (coro->flags & CF_RUNNING)
1591 PL_runops = RUNOPS_DEFAULT;
1592 else
1593 coro->runops = RUNOPS_DEFAULT;
1594 }
1595
1596SV *
1597has_stack (Coro::State coro)
1598 PROTOTYPE: $
1599 CODE:
1600 RETVAL = boolSV (!!coro->cctx);
1601 OUTPUT:
1602 RETVAL
1603
1604int
1605is_traced (Coro::State coro)
1606 PROTOTYPE: $
1607 CODE:
1608 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1609 OUTPUT:
1610 RETVAL
1611
1612IV
1613rss (Coro::State coro)
1614 PROTOTYPE: $
1615 CODE:
1616 RETVAL = coro_rss (aTHX_ coro);
1617 OUTPUT:
1618 RETVAL
1619
1620
1326MODULE = Coro::State PACKAGE = Coro 1621MODULE = Coro::State PACKAGE = Coro
1327 1622
1328BOOT: 1623BOOT:
1329{ 1624{
1330 int i; 1625 int i;
1626
1627 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE);
1628 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE);
1629 av_async_pool = get_av ("Coro::async_pool", TRUE);
1630
1631 coro_current = get_sv ("Coro::current", FALSE);
1632 SvREADONLY_on (coro_current);
1331 1633
1332 coro_stash = gv_stashpv ("Coro", TRUE); 1634 coro_stash = gv_stashpv ("Coro", TRUE);
1333 1635
1334 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1636 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1335 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1637 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1336 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1638 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1337 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1639 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1338 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1640 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1339 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1641 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1340
1341 coro_current = get_sv ("Coro::current", FALSE);
1342 SvREADONLY_on (coro_current);
1343 1642
1344 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1643 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1345 coro_ready[i] = newAV (); 1644 coro_ready[i] = newAV ();
1346 1645
1347 { 1646 {
1397 CODE: 1696 CODE:
1398 RETVAL = boolSV (api_ready (self)); 1697 RETVAL = boolSV (api_ready (self));
1399 OUTPUT: 1698 OUTPUT:
1400 RETVAL 1699 RETVAL
1401 1700
1402SV *
1403is_ready (SV *self)
1404 PROTOTYPE: $
1405 CODE:
1406 RETVAL = boolSV (api_is_ready (self));
1407 OUTPUT:
1408 RETVAL
1409
1410int 1701int
1411nready (...) 1702nready (...)
1412 PROTOTYPE: 1703 PROTOTYPE:
1413 CODE: 1704 CODE:
1414 RETVAL = coro_nready; 1705 RETVAL = coro_nready;
1415 OUTPUT: 1706 OUTPUT:
1416 RETVAL 1707 RETVAL
1417 1708
1709# for async_pool speedup
1710void
1711_pool_1 (SV *cb)
1712 CODE:
1713{
1714 int i, len;
1715 HV *hv = (HV *)SvRV (coro_current);
1716 AV *defav = GvAV (PL_defgv);
1717 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1718 AV *invoke_av;
1719
1720 if (!invoke)
1721 croak ("\3terminate\2\n");
1722
1723 hv_store (hv, "desc", sizeof ("desc") - 1,
1724 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1725
1726 invoke_av = (AV *)SvRV (invoke);
1727 len = av_len (invoke_av);
1728
1729 sv_setsv (cb, AvARRAY (invoke_av)[0]);
1730
1731 if (len > 0)
1732 {
1733 av_fill (defav, len - 1);
1734 for (i = 0; i < len; ++i)
1735 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1]));
1736 }
1737
1738 SvREFCNT_dec (invoke);
1739}
1740
1741void
1742_pool_2 (SV *cb)
1743 CODE:
1744{
1745 struct coro *coro = SvSTATE (coro_current);
1746
1747 sv_setsv (cb, &PL_sv_undef);
1748
1749 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1750 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1751 croak ("\3terminate\2\n");
1752
1753 av_clear (GvAV (PL_defgv));
1754 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1755 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1756 coro->save = CORO_SAVE_DEF;
1757 coro->prio = 0;
1758 av_push (av_async_pool, newSVsv (coro_current));
1759}
1760
1761
1418MODULE = Coro::State PACKAGE = Coro::AIO 1762MODULE = Coro::State PACKAGE = Coro::AIO
1419 1763
1420SV * 1764SV *
1421_get_state () 1765_get_state ()
1422 CODE: 1766 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines