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.146 by root, Sat Mar 17 19:51:57 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
129}; 128};
130 129
131static size_t coro_stacksize = CORO_STACKSIZE; 130static size_t coro_stacksize = CORO_STACKSIZE;
132static struct CoroAPI coroapi; 131static struct CoroAPI coroapi;
133static AV *main_mainstack; /* used to differentiate between $main and others */ 132static AV *main_mainstack; /* used to differentiate between $main and others */
133static JMPENV *main_top_env;
134static HV *coro_state_stash, *coro_stash; 134static HV *coro_state_stash, *coro_stash;
135static SV *coro_mortal; /* will be freed after next transfer */ 135static SV *coro_mortal; /* will be freed after next transfer */
136 136
137/* async_pool helper stuff */
138static SV *sv_pool_rss;
139static SV *sv_pool_size;
140static AV *av_async_pool;
141
137static struct coro_cctx *cctx_first; 142static struct coro_cctx *cctx_first;
138static 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};
139 153
140/* this is a structure representing a c-level coroutine */ 154/* this is a structure representing a c-level coroutine */
141typedef struct coro_cctx { 155typedef struct coro_cctx {
142 struct coro_cctx *next; 156 struct coro_cctx *next;
143 157
152 coro_context cctx; 166 coro_context cctx;
153 167
154#if CORO_USE_VALGRIND 168#if CORO_USE_VALGRIND
155 int valgrind_id; 169 int valgrind_id;
156#endif 170#endif
157 char inuse, mapped; 171 unsigned char flags;
158} coro_cctx; 172} coro_cctx;
159 173
160enum { 174enum {
161 CF_RUNNING = 0x0001, /* coroutine is running */ 175 CF_RUNNING = 0x0001, /* coroutine is running */
162 CF_READY = 0x0002, /* coroutine is ready */ 176 CF_READY = 0x0002, /* coroutine is ready */
177 191
178 /* optionally saved, might be zero */ 192 /* optionally saved, might be zero */
179 AV *defav; /* @_ */ 193 AV *defav; /* @_ */
180 SV *defsv; /* $_ */ 194 SV *defsv; /* $_ */
181 SV *errsv; /* $@ */ 195 SV *errsv; /* $@ */
196 GV *deffh; /* default filehandle */
182 SV *irssv; /* $/ */ 197 SV *irssv; /* $/ */
183 SV *irssv_sv; /* real $/ cache */ 198 SV *irssv_sv; /* real $/ cache */
184 199
185#define VAR(name,type) type name; 200#define VAR(name,type) type name;
186# include "state.h" 201# include "state.h"
187#undef VAR 202#undef VAR
188 203
189 /* coro process data */ 204 /* coro process data */
190 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 */
191}; 210};
192 211
193typedef struct coro *Coro__State; 212typedef struct coro *Coro__State;
194typedef struct coro *Coro__State_or_hashref; 213typedef struct coro *Coro__State_or_hashref;
195 214
204 223
205/* for Coro.pm */ 224/* for Coro.pm */
206static SV *coro_current; 225static SV *coro_current;
207static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 226static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
208static int coro_nready; 227static int coro_nready;
228static struct coro *coro_first;
209 229
210/** lowlevel stuff **********************************************************/ 230/** lowlevel stuff **********************************************************/
211 231
212static AV * 232static AV *
213coro_clone_padlist (pTHX_ CV *cv) 233coro_clone_padlist (pTHX_ CV *cv)
341#undef VAR 361#undef VAR
342 362
343 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 363 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
344 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 364 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
345 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 365 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
366 if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh);
367
346 if (c->irssv) 368 if (c->irssv)
347 { 369 {
348 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) 370 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
371 {
349 SvREFCNT_dec (c->irssv); 372 SvREFCNT_dec (c->irssv);
373 c->irssv = 0;
374 }
350 else 375 else
351 { 376 {
352 REPLACE_SV (PL_rs, c->irssv); 377 REPLACE_SV (PL_rs, c->irssv);
353 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); 378 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
354 sv_setsv (c->irssv_sv, PL_rs); 379 sv_setsv (c->irssv_sv, PL_rs);
367 CvPADLIST (cv) = (AV *)POPs; 392 CvPADLIST (cv) = (AV *)POPs;
368 } 393 }
369 394
370 PUTBACK; 395 PUTBACK;
371 } 396 }
372 assert (!PL_comppad || AvARRAY (PL_comppad));//D
373} 397}
374 398
375static void 399static void
376save_perl (pTHX_ Coro__State c) 400save_perl (pTHX_ Coro__State c)
377{ 401{
378 assert (!PL_comppad || AvARRAY (PL_comppad));//D
379 { 402 {
380 dSP; 403 dSP;
381 I32 cxix = cxstack_ix; 404 I32 cxix = cxstack_ix;
382 PERL_CONTEXT *ccstk = cxstack; 405 PERL_CONTEXT *ccstk = cxstack;
383 PERL_SI *top_si = PL_curstackinfo; 406 PERL_SI *top_si = PL_curstackinfo;
385 /* 408 /*
386 * 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
387 * (and reinitialize) all cv's in the whole callchain :( 410 * (and reinitialize) all cv's in the whole callchain :(
388 */ 411 */
389 412
390 EXTEND (SP, 3 + 1);
391 PUSHs (Nullsv); 413 XPUSHs (Nullsv);
392 /* this loop was inspired by pp_caller */ 414 /* this loop was inspired by pp_caller */
393 for (;;) 415 for (;;)
394 { 416 {
395 while (cxix >= 0) 417 while (cxix >= 0)
396 { 418 {
397 PERL_CONTEXT *cx = &ccstk[cxix--]; 419 PERL_CONTEXT *cx = &ccstk[cxix--];
398 420
399 if (CxTYPE (cx) == CXt_SUB) 421 if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)
400 { 422 {
401 CV *cv = cx->blk_sub.cv; 423 CV *cv = cx->blk_sub.cv;
402 424
403 if (CvDEPTH (cv)) 425 if (CvDEPTH (cv))
404 { 426 {
415 437
416 if (top_si->si_type == PERLSI_MAIN) 438 if (top_si->si_type == PERLSI_MAIN)
417 break; 439 break;
418 440
419 top_si = top_si->si_prev; 441 top_si = top_si->si_prev;
420 ccstk = top_si->si_cxstack; 442 ccstk = top_si->si_cxstack;
421 cxix = top_si->si_cxix; 443 cxix = top_si->si_cxix;
422 } 444 }
423 445
424 PUTBACK; 446 PUTBACK;
425 } 447 }
426 448
427 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 449 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
428 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 450 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
429 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 451 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
452 c->deffh = c->save & CORO_SAVE_DEFFH ? (GV *)SvREFCNT_inc (PL_defoutgv) : 0;
430 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; 453 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0;
431 454
432#define VAR(name,type)c->name = PL_ ## name; 455#define VAR(name,type)c->name = PL_ ## name;
433# include "state.h" 456# include "state.h"
434#undef VAR 457#undef VAR
444# define coro_init_stacks init_stacks 467# define coro_init_stacks init_stacks
445#else 468#else
446static void 469static void
447coro_init_stacks (pTHX) 470coro_init_stacks (pTHX)
448{ 471{
449 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 472 PL_curstackinfo = new_stackinfo(64, 6);
450 PL_curstackinfo->si_type = PERLSI_MAIN; 473 PL_curstackinfo->si_type = PERLSI_MAIN;
451 PL_curstack = PL_curstackinfo->si_stack; 474 PL_curstack = PL_curstackinfo->si_stack;
452 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 475 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
453 476
454 PL_stack_base = AvARRAY(PL_curstack); 477 PL_stack_base = AvARRAY(PL_curstack);
455 PL_stack_sp = PL_stack_base; 478 PL_stack_sp = PL_stack_base;
456 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 479 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
457 480
458 New(50,PL_tmps_stack,128,SV*); 481 New(50,PL_tmps_stack,64,SV*);
459 PL_tmps_floor = -1; 482 PL_tmps_floor = -1;
460 PL_tmps_ix = -1; 483 PL_tmps_ix = -1;
461 PL_tmps_max = 128; 484 PL_tmps_max = 64;
462 485
463 New(54,PL_markstack,32,I32); 486 New(54,PL_markstack,16,I32);
464 PL_markstack_ptr = PL_markstack; 487 PL_markstack_ptr = PL_markstack;
465 PL_markstack_max = PL_markstack + 32; 488 PL_markstack_max = PL_markstack + 16;
466 489
467#ifdef SET_MARK_OFFSET 490#ifdef SET_MARK_OFFSET
468 SET_MARK_OFFSET; 491 SET_MARK_OFFSET;
469#endif 492#endif
470 493
471 New(54,PL_scopestack,32,I32); 494 New(54,PL_scopestack,16,I32);
472 PL_scopestack_ix = 0; 495 PL_scopestack_ix = 0;
473 PL_scopestack_max = 32; 496 PL_scopestack_max = 16;
474 497
475 New(54,PL_savestack,64,ANY); 498 New(54,PL_savestack,64,ANY);
476 PL_savestack_ix = 0; 499 PL_savestack_ix = 0;
477 PL_savestack_max = 64; 500 PL_savestack_max = 64;
478 501
479#if !PERL_VERSION_ATLEAST (5,9,0) 502#if !PERL_VERSION_ATLEAST (5,9,0)
480 New(54,PL_retstack,16,OP*); 503 New(54,PL_retstack,4,OP*);
481 PL_retstack_ix = 0; 504 PL_retstack_ix = 0;
482 PL_retstack_max = 16; 505 PL_retstack_max = 4;
483#endif 506#endif
484} 507}
485#endif 508#endif
486 509
487/* 510/*
527 Safefree (PL_scopestack); 550 Safefree (PL_scopestack);
528 Safefree (PL_savestack); 551 Safefree (PL_savestack);
529#if !PERL_VERSION_ATLEAST (5,9,0) 552#if !PERL_VERSION_ATLEAST (5,9,0)
530 Safefree (PL_retstack); 553 Safefree (PL_retstack);
531#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;
532} 586}
533 587
534/** coroutine stack handling ************************************************/ 588/** coroutine stack handling ************************************************/
535 589
536static void 590static void
559 Zero (&myop, 1, LOGOP); 613 Zero (&myop, 1, LOGOP);
560 myop.op_next = Nullop; 614 myop.op_next = Nullop;
561 myop.op_flags = OPf_WANT_VOID; 615 myop.op_flags = OPf_WANT_VOID;
562 616
563 PUSHMARK (SP); 617 PUSHMARK (SP);
564 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 618 XPUSHs (av_shift (GvAV (PL_defgv)));
565 PUTBACK; 619 PUTBACK;
566 PL_op = (OP *)&myop; 620 PL_op = (OP *)&myop;
567 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 621 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
568 SPAGAIN; 622 SPAGAIN;
569 } 623 }
579 SvREFCNT_dec (coro_mortal); 633 SvREFCNT_dec (coro_mortal);
580 coro_mortal = 0; 634 coro_mortal = 0;
581 } 635 }
582} 636}
583 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
584/* 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 */
743/* _cctx_init should be careful, as it could be called at almost any time */
744/* during execution of a perl program */
585static void NOINLINE 745static void NOINLINE
586prepare_cctx (pTHX_ coro_cctx *cctx) 746prepare_cctx (pTHX_ coro_cctx *cctx)
587{ 747{
588 dSP; 748 dSP;
589 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;
590 755
591 Zero (&myop, 1, LOGOP); 756 Zero (&myop, 1, LOGOP);
592 myop.op_next = PL_op; 757 myop.op_next = PL_op;
593 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 758 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
594 759
600 PL_op = (OP *)&myop; 765 PL_op = (OP *)&myop;
601 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 766 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
602 SPAGAIN; 767 SPAGAIN;
603} 768}
604 769
770/*
771 * this is a _very_ stripped down perl interpreter ;)
772 */
605static void 773static void
606coro_run (void *arg) 774coro_run (void *arg)
607{ 775{
608 dTHX; 776 dTHX;
609 777
610 /* coro_run is the alternative tail of transfer(), so unlock here. */ 778 /* coro_run is the alternative tail of transfer(), so unlock here. */
611 UNLOCK; 779 UNLOCK;
612 780
613 /* 781 /* we now skip the entersub that lead to transfer() */
614 * this is a _very_ stripped down perl interpreter ;) 782 PL_op = PL_op->op_next;
615 */
616 PL_top_env = &PL_start_env;
617 783
618 /* inject call to cctx_init */ 784 /* inject a fake subroutine call to cctx_init */
619 prepare_cctx (aTHX_ (coro_cctx *)arg); 785 prepare_cctx (aTHX_ (coro_cctx *)arg);
620 786
621 /* somebody will hit me for both perl_run and PL_restartop */ 787 /* somebody or something will hit me for both perl_run and PL_restartop */
622 PL_restartop = PL_op; 788 PL_restartop = PL_op;
623 perl_run (PL_curinterp); 789 perl_run (PL_curinterp);
624 790
625 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); 791 /*
626 abort (); 792 * If perl-run returns we assume exit() was being called or the coro
793 * fell off the end, which seems to be the only valid (non-bug)
794 * reason for perl_run to return. We try to exit by jumping to the
795 * bootstrap-time "top" top_env, as we cannot restore the "main"
796 * coroutine as Coro has no such concept
797 */
798 PL_top_env = main_top_env;
799 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
627} 800}
628 801
629static coro_cctx * 802static coro_cctx *
630cctx_new () 803cctx_new ()
631{ 804{
648# if CORO_STACKGUARD 821# if CORO_STACKGUARD
649 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 822 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
650# endif 823# endif
651 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 824 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
652 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 825 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
653 cctx->mapped = 1; 826 cctx->flags |= CC_MAPPED;
654 } 827 }
655 else 828 else
656#endif 829#endif
657 { 830 {
658 cctx->ssize = coro_stacksize * (long)sizeof (long); 831 cctx->ssize = coro_stacksize * (long)sizeof (long);
685#if CORO_USE_VALGRIND 858#if CORO_USE_VALGRIND
686 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 859 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
687#endif 860#endif
688 861
689#if HAVE_MMAP 862#if HAVE_MMAP
690 if (cctx->mapped) 863 if (cctx->flags & CC_MAPPED)
691 munmap (cctx->sptr, cctx->ssize); 864 munmap (cctx->sptr, cctx->ssize);
692 else 865 else
693#endif 866#endif
694 Safefree (cctx->sptr); 867 Safefree (cctx->sptr);
695 868
703 { 876 {
704 coro_cctx *cctx = cctx_first; 877 coro_cctx *cctx = cctx_first;
705 cctx_first = cctx->next; 878 cctx_first = cctx->next;
706 --cctx_idle; 879 --cctx_idle;
707 880
708 if (cctx->ssize >= coro_stacksize) 881 if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE))
709 return cctx; 882 return cctx;
710 883
711 cctx_destroy (cctx); 884 cctx_destroy (cctx);
712 } 885 }
713 886
714 PL_op = PL_op->op_next;
715 return cctx_new (); 887 return cctx_new ();
716} 888}
717 889
718static void 890static void
719cctx_put (coro_cctx *cctx) 891cctx_put (coro_cctx *cctx)
723 { 895 {
724 coro_cctx *first = cctx_first; 896 coro_cctx *first = cctx_first;
725 cctx_first = first->next; 897 cctx_first = first->next;
726 --cctx_idle; 898 --cctx_idle;
727 899
728 assert (!first->inuse);
729 cctx_destroy (first); 900 cctx_destroy (first);
730 } 901 }
731 902
732 ++cctx_idle; 903 ++cctx_idle;
733 cctx->next = cctx_first; 904 cctx->next = cctx_first;
754 925
755 if (prev->flags & CF_NEW) 926 if (prev->flags & CF_NEW)
756 { 927 {
757 /* create a new empty context */ 928 /* create a new empty context */
758 Newz (0, prev->cctx, 1, coro_cctx); 929 Newz (0, prev->cctx, 1, coro_cctx);
759 prev->cctx->inuse = 1;
760 prev->flags &= ~CF_NEW; 930 prev->flags &= ~CF_NEW;
761 prev->flags |= CF_RUNNING; 931 prev->flags |= CF_RUNNING;
762 } 932 }
763 933
764 /*TODO: must not croak here */ 934 /*TODO: must not croak here */
782 next->flags &= ~CF_NEW; 952 next->flags &= ~CF_NEW;
783 /* first get rid of the old state */ 953 /* first get rid of the old state */
784 save_perl (aTHX_ prev); 954 save_perl (aTHX_ prev);
785 /* setup coroutine call */ 955 /* setup coroutine call */
786 setup_coro (aTHX_ next); 956 setup_coro (aTHX_ next);
787 /* need a new stack */
788 assert (!next->cctx);
789 } 957 }
790 else 958 else
791 { 959 {
792 /* coroutine already started */ 960 /* coroutine already started */
793 save_perl (aTHX_ prev); 961 save_perl (aTHX_ prev);
795 } 963 }
796 964
797 prev__cctx = prev->cctx; 965 prev__cctx = prev->cctx;
798 966
799 /* possibly "free" the cctx */ 967 /* possibly "free" the cctx */
800 if (prev__cctx->idle_sp == STACKLEVEL) 968 if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))
801 { 969 {
802 /* 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 */
803 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));
804 972
805 prev->cctx = 0; 973 prev->cctx = 0;
806 974
807 cctx_put (prev__cctx); 975 cctx_put (prev__cctx);
808 prev__cctx->inuse = 0;
809 } 976 }
810 977
811 if (!next->cctx) 978 if (!next->cctx)
812 {
813 next->cctx = cctx_get (aTHX); 979 next->cctx = cctx_get (aTHX);
814 assert (!next->cctx->inuse);
815 next->cctx->inuse = 1;
816 }
817 980
818 if (prev__cctx != next->cctx) 981 if (prev__cctx != next->cctx)
819 { 982 {
820 prev__cctx->top_env = PL_top_env; 983 prev__cctx->top_env = PL_top_env;
821 PL_top_env = next->cctx->top_env; 984 PL_top_env = next->cctx->top_env;
878 } 1041 }
879 1042
880 cctx_destroy (coro->cctx); 1043 cctx_destroy (coro->cctx);
881 SvREFCNT_dec (coro->args); 1044 SvREFCNT_dec (coro->args);
882 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
883 return 1; 1050 return 1;
884} 1051}
885 1052
886static int 1053static int
887coro_state_free (pTHX_ SV *sv, MAGIC *mg) 1054coro_state_free (pTHX_ SV *sv, MAGIC *mg)
888{ 1055{
889 struct coro *coro = (struct coro *)mg->mg_ptr; 1056 struct coro *coro = (struct coro *)mg->mg_ptr;
890 mg->mg_ptr = 0; 1057 mg->mg_ptr = 0;
1058
1059 coro->hv = 0;
891 1060
892 if (--coro->refcnt < 0) 1061 if (--coro->refcnt < 0)
893 { 1062 {
894 coro_state_destroy (aTHX_ coro); 1063 coro_state_destroy (aTHX_ coro);
895 Safefree (coro); 1064 Safefree (coro);
1159#endif 1328#endif
1160 BOOT_PAGESIZE; 1329 BOOT_PAGESIZE;
1161 1330
1162 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1331 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1163 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
1164 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1338 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1165 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1339 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1166 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1340 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1167 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); 1341 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1342 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH));
1343 newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF));
1168 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); 1344 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1169 1345
1170 main_mainstack = PL_mainstack; 1346 main_mainstack = PL_mainstack;
1347 main_top_env = PL_top_env;
1348
1349 while (main_top_env->je_prev)
1350 main_top_env = main_top_env->je_prev;
1171 1351
1172 coroapi.ver = CORO_API_VERSION; 1352 coroapi.ver = CORO_API_VERSION;
1173 coroapi.transfer = api_transfer; 1353 coroapi.transfer = api_transfer;
1174 1354
1175 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1355 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1183 HV *hv; 1363 HV *hv;
1184 int i; 1364 int i;
1185 1365
1186 Newz (0, coro, 1, struct coro); 1366 Newz (0, coro, 1, struct coro);
1187 coro->args = newAV (); 1367 coro->args = newAV ();
1188 coro->save = CORO_SAVE_ALL; 1368 coro->save = CORO_SAVE_DEF;
1189 coro->flags = CF_NEW; 1369 coro->flags = CF_NEW;
1190 1370
1371 if (coro_first) coro_first->prev = coro;
1372 coro->next = coro_first;
1373 coro_first = coro;
1374
1191 hv = newHV (); 1375 coro->hv = hv = newHV ();
1192 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;
1193 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1377 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1194 1378
1195 for (i = 1; i < items; i++) 1379 for (i = 1; i < items; i++)
1196 av_push (coro->args, newSVsv (ST (i))); 1380 av_push (coro->args, newSVsv (ST (i)));
1200 1384
1201int 1385int
1202save (SV *coro, int new_save = -1) 1386save (SV *coro, int new_save = -1)
1203 CODE: 1387 CODE:
1204 RETVAL = api_save (coro, new_save); 1388 RETVAL = api_save (coro, new_save);
1389 OUTPUT:
1390 RETVAL
1391
1392int
1393save_also (SV *coro_sv, int save_also)
1394 CODE:
1395{
1396 struct coro *coro = SvSTATE (coro_sv);
1397 RETVAL = coro->save;
1398 coro->save |= save_also;
1399}
1205 OUTPUT: 1400 OUTPUT:
1206 RETVAL 1401 RETVAL
1207 1402
1208void 1403void
1209_set_stacklevel (...) 1404_set_stacklevel (...)
1287 CODE: 1482 CODE:
1288 RETVAL = cctx_idle; 1483 RETVAL = cctx_idle;
1289 OUTPUT: 1484 OUTPUT:
1290 RETVAL 1485 RETVAL
1291 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
1292MODULE = Coro::State PACKAGE = Coro 1601MODULE = Coro::State PACKAGE = Coro
1293 1602
1294BOOT: 1603BOOT:
1295{ 1604{
1296 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);
1297 1613
1298 coro_stash = gv_stashpv ("Coro", TRUE); 1614 coro_stash = gv_stashpv ("Coro", TRUE);
1299 1615
1300 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1616 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1301 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1617 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1302 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1618 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1303 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1619 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1304 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1620 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1305 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1621 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1306
1307 coro_current = get_sv ("Coro::current", FALSE);
1308 SvREADONLY_on (coro_current);
1309 1622
1310 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1623 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1311 coro_ready[i] = newAV (); 1624 coro_ready[i] = newAV ();
1312 1625
1313 { 1626 {
1363 CODE: 1676 CODE:
1364 RETVAL = boolSV (api_ready (self)); 1677 RETVAL = boolSV (api_ready (self));
1365 OUTPUT: 1678 OUTPUT:
1366 RETVAL 1679 RETVAL
1367 1680
1368SV *
1369is_ready (SV *self)
1370 PROTOTYPE: $
1371 CODE:
1372 RETVAL = boolSV (api_is_ready (self));
1373 OUTPUT:
1374 RETVAL
1375
1376int 1681int
1377nready (...) 1682nready (...)
1378 PROTOTYPE: 1683 PROTOTYPE:
1379 CODE: 1684 CODE:
1380 RETVAL = coro_nready; 1685 RETVAL = coro_nready;
1381 OUTPUT: 1686 OUTPUT:
1382 RETVAL 1687 RETVAL
1383 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
1384MODULE = Coro::State PACKAGE = Coro::AIO 1742MODULE = Coro::State PACKAGE = Coro::AIO
1385 1743
1386SV * 1744SV *
1387_get_state () 1745_get_state ()
1388 CODE: 1746 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines