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.168 by root, Thu Sep 27 16:10:48 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)
374 CvPADLIST (cv) = (AV *)POPs; 392 CvPADLIST (cv) = (AV *)POPs;
375 } 393 }
376 394
377 PUTBACK; 395 PUTBACK;
378 } 396 }
379 assert (!PL_comppad || AvARRAY (PL_comppad));//D
380} 397}
381 398
382static void 399static void
383save_perl (pTHX_ Coro__State c) 400save_perl (pTHX_ Coro__State c)
384{ 401{
385 assert (!PL_comppad || AvARRAY (PL_comppad));//D
386 { 402 {
387 dSP; 403 dSP;
388 I32 cxix = cxstack_ix; 404 I32 cxix = cxstack_ix;
389 PERL_CONTEXT *ccstk = cxstack; 405 PERL_CONTEXT *ccstk = cxstack;
390 PERL_SI *top_si = PL_curstackinfo; 406 PERL_SI *top_si = PL_curstackinfo;
392 /* 408 /*
393 * the worst thing you can imagine happens first - we have to save 409 * the worst thing you can imagine happens first - we have to save
394 * (and reinitialize) all cv's in the whole callchain :( 410 * (and reinitialize) all cv's in the whole callchain :(
395 */ 411 */
396 412
397 EXTEND (SP, 3 + 1);
398 PUSHs (Nullsv); 413 XPUSHs (Nullsv);
399 /* this loop was inspired by pp_caller */ 414 /* this loop was inspired by pp_caller */
400 for (;;) 415 for (;;)
401 { 416 {
402 while (cxix >= 0) 417 while (cxix >= 0)
403 { 418 {
404 PERL_CONTEXT *cx = &ccstk[cxix--]; 419 PERL_CONTEXT *cx = &ccstk[cxix--];
405 420
406 if (CxTYPE (cx) == CXt_SUB) 421 if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)
407 { 422 {
408 CV *cv = cx->blk_sub.cv; 423 CV *cv = cx->blk_sub.cv;
409 424
410 if (CvDEPTH (cv)) 425 if (CvDEPTH (cv))
411 { 426 {
422 437
423 if (top_si->si_type == PERLSI_MAIN) 438 if (top_si->si_type == PERLSI_MAIN)
424 break; 439 break;
425 440
426 top_si = top_si->si_prev; 441 top_si = top_si->si_prev;
427 ccstk = top_si->si_cxstack; 442 ccstk = top_si->si_cxstack;
428 cxix = top_si->si_cxix; 443 cxix = top_si->si_cxix;
429 } 444 }
430 445
431 PUTBACK; 446 PUTBACK;
432 } 447 }
433 448
452# define coro_init_stacks init_stacks 467# define coro_init_stacks init_stacks
453#else 468#else
454static void 469static void
455coro_init_stacks (pTHX) 470coro_init_stacks (pTHX)
456{ 471{
457 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 472 PL_curstackinfo = new_stackinfo(64, 6);
458 PL_curstackinfo->si_type = PERLSI_MAIN; 473 PL_curstackinfo->si_type = PERLSI_MAIN;
459 PL_curstack = PL_curstackinfo->si_stack; 474 PL_curstack = PL_curstackinfo->si_stack;
460 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 475 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
461 476
462 PL_stack_base = AvARRAY(PL_curstack); 477 PL_stack_base = AvARRAY(PL_curstack);
463 PL_stack_sp = PL_stack_base; 478 PL_stack_sp = PL_stack_base;
464 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 479 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
465 480
466 New(50,PL_tmps_stack,128,SV*); 481 New(50,PL_tmps_stack,64,SV*);
467 PL_tmps_floor = -1; 482 PL_tmps_floor = -1;
468 PL_tmps_ix = -1; 483 PL_tmps_ix = -1;
469 PL_tmps_max = 128; 484 PL_tmps_max = 64;
470 485
471 New(54,PL_markstack,32,I32); 486 New(54,PL_markstack,16,I32);
472 PL_markstack_ptr = PL_markstack; 487 PL_markstack_ptr = PL_markstack;
473 PL_markstack_max = PL_markstack + 32; 488 PL_markstack_max = PL_markstack + 16;
474 489
475#ifdef SET_MARK_OFFSET 490#ifdef SET_MARK_OFFSET
476 SET_MARK_OFFSET; 491 SET_MARK_OFFSET;
477#endif 492#endif
478 493
479 New(54,PL_scopestack,32,I32); 494 New(54,PL_scopestack,16,I32);
480 PL_scopestack_ix = 0; 495 PL_scopestack_ix = 0;
481 PL_scopestack_max = 32; 496 PL_scopestack_max = 16;
482 497
483 New(54,PL_savestack,64,ANY); 498 New(54,PL_savestack,64,ANY);
484 PL_savestack_ix = 0; 499 PL_savestack_ix = 0;
485 PL_savestack_max = 64; 500 PL_savestack_max = 64;
486 501
487#if !PERL_VERSION_ATLEAST (5,9,0) 502#if !PERL_VERSION_ATLEAST (5,9,0)
488 New(54,PL_retstack,16,OP*); 503 New(54,PL_retstack,4,OP*);
489 PL_retstack_ix = 0; 504 PL_retstack_ix = 0;
490 PL_retstack_max = 16; 505 PL_retstack_max = 4;
491#endif 506#endif
492} 507}
493#endif 508#endif
494 509
495/* 510/*
535 Safefree (PL_scopestack); 550 Safefree (PL_scopestack);
536 Safefree (PL_savestack); 551 Safefree (PL_savestack);
537#if !PERL_VERSION_ATLEAST (5,9,0) 552#if !PERL_VERSION_ATLEAST (5,9,0)
538 Safefree (PL_retstack); 553 Safefree (PL_retstack);
539#endif 554#endif
555}
556
557static size_t
558coro_rss (struct coro *coro)
559{
560 size_t rss = sizeof (coro);
561
562 if (coro->mainstack)
563 {
564 if (coro->flags & CF_RUNNING)
565 {
566 #define VAR(name,type)coro->name = PL_ ## name;
567 # include "state.h"
568 #undef VAR
569 }
570
571 rss += sizeof (coro->curstackinfo);
572 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *);
573 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
574 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *);
575 rss += coro->tmps_max * sizeof (SV *);
576 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32);
577 rss += coro->scopestack_max * sizeof (I32);
578 rss += coro->savestack_max * sizeof (ANY);
579
580#if !PERL_VERSION_ATLEAST (5,9,0)
581 rss += coro->retstack_max * sizeof (OP *);
582#endif
583 }
584
585 return rss;
540} 586}
541 587
542/** coroutine stack handling ************************************************/ 588/** coroutine stack handling ************************************************/
543 589
544static void 590static void
567 Zero (&myop, 1, LOGOP); 613 Zero (&myop, 1, LOGOP);
568 myop.op_next = Nullop; 614 myop.op_next = Nullop;
569 myop.op_flags = OPf_WANT_VOID; 615 myop.op_flags = OPf_WANT_VOID;
570 616
571 PUSHMARK (SP); 617 PUSHMARK (SP);
572 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 618 XPUSHs (av_shift (GvAV (PL_defgv)));
573 PUTBACK; 619 PUTBACK;
574 PL_op = (OP *)&myop; 620 PL_op = (OP *)&myop;
575 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 621 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
576 SPAGAIN; 622 SPAGAIN;
577 } 623 }
587 SvREFCNT_dec (coro_mortal); 633 SvREFCNT_dec (coro_mortal);
588 coro_mortal = 0; 634 coro_mortal = 0;
589 } 635 }
590} 636}
591 637
638static int
639runops_coro (pTHX)
640{
641 COP *oldcop = 0;
642 int oldcxix = -2;
643
644 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
645 {
646 PERL_ASYNC_CHECK ();
647
648 if (PL_op->op_type == OP_LEAVESUB)
649 {
650 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
651 SV **bot, **top;
652 AV *av = newAV (); /* return values */
653 runops_proc_t old_runops = PL_runops;
654 dSP;
655 ENTER;
656 SAVETMPS;
657 EXTEND (SP, 3);
658 PL_runops = RUNOPS_DEFAULT;
659
660 GV *gv = CvGV (cx->blk_sub.cv);
661 SV *fullname = sv_2mortal (newSV (0));
662 if (isGV (gv))
663 gv_efullname3 (fullname, gv, 0);
664
665 bot = PL_stack_base + cx->blk_oldsp + 1;
666 top = cx->blk_gimme == G_ARRAY ? SP + 1
667 : cx->blk_gimme == G_SCALAR ? bot + 1
668 : bot;
669
670 while (bot < top)
671 av_push (av, SvREFCNT_inc (*bot++));
672
673 PUSHMARK (SP);
674 PUSHs (&PL_sv_no);
675 PUSHs (fullname);
676 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
677 PUTBACK;
678 call_pv ("Coro::_do_trace_sub", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
679 SPAGAIN;
680
681 FREETMPS;
682 LEAVE;
683 PL_runops = old_runops;
684 }
685
686 if (oldcop != PL_curcop)
687 {
688 oldcop = PL_curcop;
689
690 if (PL_curcop != &PL_compiling)
691 {
692 runops_proc_t old_runops = PL_runops;
693 dSP;
694 ENTER;
695 SAVETMPS;
696 EXTEND (SP, 3);
697 PL_runops = RUNOPS_DEFAULT;
698
699 if (oldcxix != cxstack_ix)
700 {
701 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
702
703 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
704 {
705 GV *gv = CvGV (cx->blk_sub.cv);
706 SV *fullname = sv_2mortal (newSV (0));
707 if (isGV (gv))
708 gv_efullname3 (fullname, gv, 0);
709
710 PUSHMARK (SP);
711 PUSHs (&PL_sv_yes);
712 PUSHs (fullname);
713 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
714 PUTBACK;
715 call_pv ("Coro::_do_trace_sub", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
716 SPAGAIN;
717 }
718
719 oldcxix = cxstack_ix;
720 }
721
722 if (0) {
723 PUSHMARK (SP);
724 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
725 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop))));
726 PUTBACK;
727 call_pv ("Coro::_do_trace_line", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
728 SPAGAIN;
729 }
730
731 FREETMPS;
732 LEAVE;
733 PL_runops = old_runops;
734 }
735 }
736 }
737
738 TAINT_NOT;
739 return 0;
740}
741
592/* inject a fake call to Coro::State::_cctx_init into the execution */ 742/* 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 */ 743/* _cctx_init should be careful, as it could be called at almost any time */
594/* during execution of a pelr program */ 744/* during execution of a perl program */
595static void NOINLINE 745static void NOINLINE
596prepare_cctx (pTHX_ coro_cctx *cctx) 746prepare_cctx (pTHX_ coro_cctx *cctx)
597{ 747{
598 dSP; 748 dSP;
599 LOGOP myop; 749 LOGOP myop;
750
751 PL_top_env = &PL_start_env;
752
753 if (cctx->flags & CC_TRACE)
754 PL_runops = runops_coro;
600 755
601 Zero (&myop, 1, LOGOP); 756 Zero (&myop, 1, LOGOP);
602 myop.op_next = PL_op; 757 myop.op_next = PL_op;
603 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 758 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
604 759
621 dTHX; 776 dTHX;
622 777
623 /* coro_run is the alternative tail of transfer(), so unlock here. */ 778 /* coro_run is the alternative tail of transfer(), so unlock here. */
624 UNLOCK; 779 UNLOCK;
625 780
626 PL_top_env = &PL_start_env; 781 /* we now skip the entersub that lead to transfer() */
782 PL_op = PL_op->op_next;
627 783
628 /* inject a fake subroutine call to cctx_init */ 784 /* inject a fake subroutine call to cctx_init */
629 prepare_cctx (aTHX_ (coro_cctx *)arg); 785 prepare_cctx (aTHX_ (coro_cctx *)arg);
630 786
631 /* somebody or something will hit me for both perl_run and PL_restartop */ 787 /* somebody or something will hit me for both perl_run and PL_restartop */
632 PL_restartop = PL_op; 788 PL_restartop = PL_op;
633 perl_run (PL_curinterp); 789 perl_run (PL_curinterp);
634 790
791 /*
635 /* If perl-run returns we assume exit() was being called, which */ 792 * 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. */ 793 * 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 */ 794 * 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 */ 795 * bootstrap-time "top" top_env, as we cannot restore the "main"
796 * coroutine as Coro has no such concept
797 */
639 PL_top_env = main_top_env; 798 PL_top_env = main_top_env;
640 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 799 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
641} 800}
642 801
643static coro_cctx * 802static coro_cctx *
662# if CORO_STACKGUARD 821# if CORO_STACKGUARD
663 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 822 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
664# endif 823# endif
665 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 824 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
666 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 825 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
667 cctx->mapped = 1; 826 cctx->flags |= CC_MAPPED;
668 } 827 }
669 else 828 else
670#endif 829#endif
671 { 830 {
672 cctx->ssize = coro_stacksize * (long)sizeof (long); 831 cctx->ssize = coro_stacksize * (long)sizeof (long);
699#if CORO_USE_VALGRIND 858#if CORO_USE_VALGRIND
700 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 859 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
701#endif 860#endif
702 861
703#if HAVE_MMAP 862#if HAVE_MMAP
704 if (cctx->mapped) 863 if (cctx->flags & CC_MAPPED)
705 munmap (cctx->sptr, cctx->ssize); 864 munmap (cctx->sptr, cctx->ssize);
706 else 865 else
707#endif 866#endif
708 Safefree (cctx->sptr); 867 Safefree (cctx->sptr);
709 868
717 { 876 {
718 coro_cctx *cctx = cctx_first; 877 coro_cctx *cctx = cctx_first;
719 cctx_first = cctx->next; 878 cctx_first = cctx->next;
720 --cctx_idle; 879 --cctx_idle;
721 880
722 if (cctx->ssize >= coro_stacksize) 881 if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE))
723 return cctx; 882 return cctx;
724 883
725 cctx_destroy (cctx); 884 cctx_destroy (cctx);
726 } 885 }
727 886
728 PL_op = PL_op->op_next;
729 return cctx_new (); 887 return cctx_new ();
730} 888}
731 889
732static void 890static void
733cctx_put (coro_cctx *cctx) 891cctx_put (coro_cctx *cctx)
737 { 895 {
738 coro_cctx *first = cctx_first; 896 coro_cctx *first = cctx_first;
739 cctx_first = first->next; 897 cctx_first = first->next;
740 --cctx_idle; 898 --cctx_idle;
741 899
742 assert (!first->inuse);
743 cctx_destroy (first); 900 cctx_destroy (first);
744 } 901 }
745 902
746 ++cctx_idle; 903 ++cctx_idle;
747 cctx->next = cctx_first; 904 cctx->next = cctx_first;
768 925
769 if (prev->flags & CF_NEW) 926 if (prev->flags & CF_NEW)
770 { 927 {
771 /* create a new empty context */ 928 /* create a new empty context */
772 Newz (0, prev->cctx, 1, coro_cctx); 929 Newz (0, prev->cctx, 1, coro_cctx);
773 prev->cctx->inuse = 1;
774 prev->flags &= ~CF_NEW; 930 prev->flags &= ~CF_NEW;
775 prev->flags |= CF_RUNNING; 931 prev->flags |= CF_RUNNING;
776 } 932 }
777 933
778 /*TODO: must not croak here */ 934 /*TODO: must not croak here */
796 next->flags &= ~CF_NEW; 952 next->flags &= ~CF_NEW;
797 /* first get rid of the old state */ 953 /* first get rid of the old state */
798 save_perl (aTHX_ prev); 954 save_perl (aTHX_ prev);
799 /* setup coroutine call */ 955 /* setup coroutine call */
800 setup_coro (aTHX_ next); 956 setup_coro (aTHX_ next);
801 /* need a new stack */
802 assert (!next->cctx);
803 } 957 }
804 else 958 else
805 { 959 {
806 /* coroutine already started */ 960 /* coroutine already started */
807 save_perl (aTHX_ prev); 961 save_perl (aTHX_ prev);
809 } 963 }
810 964
811 prev__cctx = prev->cctx; 965 prev__cctx = prev->cctx;
812 966
813 /* possibly "free" the cctx */ 967 /* possibly "free" the cctx */
814 if (prev__cctx->idle_sp == STACKLEVEL) 968 if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))
815 { 969 {
816 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 970 /* 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)); 971 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
818 972
819 prev->cctx = 0; 973 prev->cctx = 0;
820 974
821 cctx_put (prev__cctx); 975 cctx_put (prev__cctx);
822 prev__cctx->inuse = 0;
823 } 976 }
824 977
825 if (!next->cctx) 978 if (!next->cctx)
826 {
827 next->cctx = cctx_get (aTHX); 979 next->cctx = cctx_get (aTHX);
828 assert (!next->cctx->inuse);
829 next->cctx->inuse = 1;
830 }
831 980
832 if (prev__cctx != next->cctx) 981 if (prev__cctx != next->cctx)
833 { 982 {
834 prev__cctx->top_env = PL_top_env; 983 prev__cctx->top_env = PL_top_env;
835 PL_top_env = next->cctx->top_env; 984 PL_top_env = next->cctx->top_env;
874 struct coro temp; 1023 struct coro temp;
875 1024
876 assert (!(coro->flags & CF_RUNNING)); 1025 assert (!(coro->flags & CF_RUNNING));
877 1026
878 Zero (&temp, 1, struct coro); 1027 Zero (&temp, 1, struct coro);
879 temp.save = CORO_SAVE_DEF; 1028 temp.save = CORO_SAVE_ALL;
880 1029
881 if (coro->flags & CF_RUNNING) 1030 if (coro->flags & CF_RUNNING)
882 croak ("FATAL: tried to destroy currently running coroutine"); 1031 croak ("FATAL: tried to destroy currently running coroutine");
883 1032
884 save_perl (aTHX_ &temp); 1033 save_perl (aTHX_ &temp);
892 } 1041 }
893 1042
894 cctx_destroy (coro->cctx); 1043 cctx_destroy (coro->cctx);
895 SvREFCNT_dec (coro->args); 1044 SvREFCNT_dec (coro->args);
896 1045
1046 if (coro->next) coro->next->prev = coro->prev;
1047 if (coro->prev) coro->prev->next = coro->next;
1048 if (coro == coro_first) coro_first = coro->next;
1049
897 return 1; 1050 return 1;
898} 1051}
899 1052
900static int 1053static int
901coro_state_free (pTHX_ SV *sv, MAGIC *mg) 1054coro_state_free (pTHX_ SV *sv, MAGIC *mg)
902{ 1055{
903 struct coro *coro = (struct coro *)mg->mg_ptr; 1056 struct coro *coro = (struct coro *)mg->mg_ptr;
904 mg->mg_ptr = 0; 1057 mg->mg_ptr = 0;
1058
1059 coro->hv = 0;
905 1060
906 if (--coro->refcnt < 0) 1061 if (--coro->refcnt < 0)
907 { 1062 {
908 coro_state_destroy (aTHX_ coro); 1063 coro_state_destroy (aTHX_ coro);
909 Safefree (coro); 1064 Safefree (coro);
1173#endif 1328#endif
1174 BOOT_PAGESIZE; 1329 BOOT_PAGESIZE;
1175 1330
1176 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1331 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1177 1332
1333 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1334 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1335 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1336 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1337
1178 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1338 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1179 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1339 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1180 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1340 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1181 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); 1341 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1182 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); 1342 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH));
1206 Newz (0, coro, 1, struct coro); 1366 Newz (0, coro, 1, struct coro);
1207 coro->args = newAV (); 1367 coro->args = newAV ();
1208 coro->save = CORO_SAVE_DEF; 1368 coro->save = CORO_SAVE_DEF;
1209 coro->flags = CF_NEW; 1369 coro->flags = CF_NEW;
1210 1370
1371 if (coro_first) coro_first->prev = coro;
1372 coro->next = coro_first;
1373 coro_first = coro;
1374
1211 hv = newHV (); 1375 coro->hv = hv = newHV ();
1212 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1376 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)); 1377 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1214 1378
1215 for (i = 1; i < items; i++) 1379 for (i = 1; i < items; i++)
1216 av_push (coro->args, newSVsv (ST (i))); 1380 av_push (coro->args, newSVsv (ST (i)));
1318 CODE: 1482 CODE:
1319 RETVAL = cctx_idle; 1483 RETVAL = cctx_idle;
1320 OUTPUT: 1484 OUTPUT:
1321 RETVAL 1485 RETVAL
1322 1486
1487void
1488list ()
1489 PPCODE:
1490{
1491 struct coro *coro;
1492 for (coro = coro_first; coro; coro = coro->next)
1493 if (coro->hv)
1494 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1495}
1496
1497void
1498call (Coro::State coro, SV *coderef)
1499 ALIAS:
1500 eval = 1
1501 CODE:
1502{
1503 if (coro->mainstack)
1504 {
1505 struct coro temp;
1506 Zero (&temp, 1, struct coro);
1507 temp.save = CORO_SAVE_ALL;
1508
1509 if (!(coro->flags & CF_RUNNING))
1510 {
1511 save_perl (aTHX_ &temp);
1512 load_perl (aTHX_ coro);
1513 }
1514
1515 {
1516 dSP;
1517 ENTER;
1518 SAVETMPS;
1519 PUSHMARK (SP);
1520 PUTBACK;
1521 if (ix)
1522 eval_sv (coderef, 0);
1523 else
1524 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1525 SPAGAIN;
1526 FREETMPS;
1527 LEAVE;
1528 PUTBACK;
1529 }
1530
1531 if (!(coro->flags & CF_RUNNING))
1532 {
1533 save_perl (aTHX_ coro);
1534 load_perl (aTHX_ &temp);
1535 }
1536 }
1537}
1538
1539SV *
1540is_ready (Coro::State coro)
1541 PROTOTYPE: $
1542 ALIAS:
1543 is_ready = CF_READY
1544 is_running = CF_RUNNING
1545 is_new = CF_NEW
1546 is_destroyed = CF_DESTROYED
1547 CODE:
1548 RETVAL = boolSV (coro->flags & ix);
1549 OUTPUT:
1550 RETVAL
1551
1552void
1553trace (Coro::State coro, int flags = CC_TRACE | CC_TRACE_SUB)
1554 CODE:
1555 if (flags & CC_TRACE)
1556 {
1557 if (!coro->cctx)
1558 coro->cctx = cctx_new ();
1559 else if (!(coro->cctx->flags & CC_TRACE))
1560 croak ("cannot enable tracing on coroutine with custom stack");
1561
1562 coro->cctx->flags |= flags & (CC_TRACE | CC_TRACE_ALL);
1563 }
1564 else
1565 if (coro->cctx && coro->cctx->flags & CC_TRACE)
1566 {
1567 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL);
1568 coro->cctx->flags |= CC_NOREUSE;
1569
1570 if (coro->flags & CF_RUNNING)
1571 PL_runops = RUNOPS_DEFAULT;
1572 else
1573 coro->runops = RUNOPS_DEFAULT;
1574 }
1575
1576SV *
1577has_stack (Coro::State coro)
1578 PROTOTYPE: $
1579 CODE:
1580 RETVAL = boolSV (!!coro->cctx);
1581 OUTPUT:
1582 RETVAL
1583
1584int
1585is_traced (Coro::State coro)
1586 PROTOTYPE: $
1587 CODE:
1588 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1589 OUTPUT:
1590 RETVAL
1591
1592IV
1593rss (Coro::State coro)
1594 PROTOTYPE: $
1595 CODE:
1596 RETVAL = coro_rss (coro);
1597 OUTPUT:
1598 RETVAL
1599
1600
1323MODULE = Coro::State PACKAGE = Coro 1601MODULE = Coro::State PACKAGE = Coro
1324 1602
1325BOOT: 1603BOOT:
1326{ 1604{
1327 int i; 1605 int i;
1606
1607 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE);
1608 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE);
1609 av_async_pool = get_av ("Coro::async_pool", TRUE);
1610
1611 coro_current = get_sv ("Coro::current", FALSE);
1612 SvREADONLY_on (coro_current);
1328 1613
1329 coro_stash = gv_stashpv ("Coro", TRUE); 1614 coro_stash = gv_stashpv ("Coro", TRUE);
1330 1615
1331 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1616 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1332 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1617 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1333 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1618 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1334 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1619 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1335 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1620 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1336 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1621 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1337
1338 coro_current = get_sv ("Coro::current", FALSE);
1339 SvREADONLY_on (coro_current);
1340 1622
1341 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1623 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1342 coro_ready[i] = newAV (); 1624 coro_ready[i] = newAV ();
1343 1625
1344 { 1626 {
1394 CODE: 1676 CODE:
1395 RETVAL = boolSV (api_ready (self)); 1677 RETVAL = boolSV (api_ready (self));
1396 OUTPUT: 1678 OUTPUT:
1397 RETVAL 1679 RETVAL
1398 1680
1399SV *
1400is_ready (SV *self)
1401 PROTOTYPE: $
1402 CODE:
1403 RETVAL = boolSV (api_is_ready (self));
1404 OUTPUT:
1405 RETVAL
1406
1407int 1681int
1408nready (...) 1682nready (...)
1409 PROTOTYPE: 1683 PROTOTYPE:
1410 CODE: 1684 CODE:
1411 RETVAL = coro_nready; 1685 RETVAL = coro_nready;
1412 OUTPUT: 1686 OUTPUT:
1413 RETVAL 1687 RETVAL
1414 1688
1689# for async_pool speedup
1690void
1691_pool_1 (SV *cb)
1692 CODE:
1693{
1694 int i, len;
1695 HV *hv = (HV *)SvRV (coro_current);
1696 AV *defav = GvAV (PL_defgv);
1697 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1698 AV *invoke_av;
1699
1700 if (!invoke)
1701 croak ("\3terminate\2\n");
1702
1703 hv_store (hv, "desc", sizeof ("desc") - 1,
1704 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1705
1706 invoke_av = (AV *)SvRV (invoke);
1707 len = av_len (invoke_av);
1708
1709 sv_setsv (cb, AvARRAY (invoke_av)[0]);
1710
1711 if (len > 0)
1712 {
1713 av_fill (defav, len - 1);
1714 for (i = 0; i < len; ++i)
1715 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1]));
1716 }
1717
1718 SvREFCNT_dec (invoke);
1719}
1720
1721void
1722_pool_2 (SV *cb)
1723 CODE:
1724{
1725 struct coro *coro = SvSTATE (coro_current);
1726
1727 sv_setsv (cb, &PL_sv_undef);
1728
1729 if (coro_rss (coro) > SvIV (sv_pool_rss)
1730 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1731 croak ("\3terminate\2\n");
1732
1733 av_clear (GvAV (PL_defgv));
1734 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1735 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1736 coro->save = CORO_SAVE_DEF;
1737 coro->prio = 0;
1738 av_push (av_async_pool, newSVsv (coro_current));
1739}
1740
1741
1415MODULE = Coro::State PACKAGE = Coro::AIO 1742MODULE = Coro::State PACKAGE = Coro::AIO
1416 1743
1417SV * 1744SV *
1418_get_state () 1745_get_state ()
1419 CODE: 1746 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines