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.166 by root, Wed Sep 26 21:11:28 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_TRACE = 0x02,
148 CC_NOREUSE = 0x04, /* throw this away after tracing */
149};
139 150
140/* this is a structure representing a c-level coroutine */ 151/* this is a structure representing a c-level coroutine */
141typedef struct coro_cctx { 152typedef struct coro_cctx {
142 struct coro_cctx *next; 153 struct coro_cctx *next;
143 154
152 coro_context cctx; 163 coro_context cctx;
153 164
154#if CORO_USE_VALGRIND 165#if CORO_USE_VALGRIND
155 int valgrind_id; 166 int valgrind_id;
156#endif 167#endif
157 char inuse, mapped; 168 unsigned char flags;
158} coro_cctx; 169} coro_cctx;
159 170
160enum { 171enum {
161 CF_RUNNING = 0x0001, /* coroutine is running */ 172 CF_RUNNING = 0x0001, /* coroutine is running */
162 CF_READY = 0x0002, /* coroutine is ready */ 173 CF_READY = 0x0002, /* coroutine is ready */
177 188
178 /* optionally saved, might be zero */ 189 /* optionally saved, might be zero */
179 AV *defav; /* @_ */ 190 AV *defav; /* @_ */
180 SV *defsv; /* $_ */ 191 SV *defsv; /* $_ */
181 SV *errsv; /* $@ */ 192 SV *errsv; /* $@ */
193 GV *deffh; /* default filehandle */
182 SV *irssv; /* $/ */ 194 SV *irssv; /* $/ */
183 SV *irssv_sv; /* real $/ cache */ 195 SV *irssv_sv; /* real $/ cache */
184 196
185#define VAR(name,type) type name; 197#define VAR(name,type) type name;
186# include "state.h" 198# include "state.h"
187#undef VAR 199#undef VAR
188 200
189 /* coro process data */ 201 /* coro process data */
190 int prio; 202 int prio;
203
204 /* linked list */
205 struct coro *next, *prev;
206 HV *hv; /* the perl hash associated with this coro, if any */
191}; 207};
192 208
193typedef struct coro *Coro__State; 209typedef struct coro *Coro__State;
194typedef struct coro *Coro__State_or_hashref; 210typedef struct coro *Coro__State_or_hashref;
195 211
204 220
205/* for Coro.pm */ 221/* for Coro.pm */
206static SV *coro_current; 222static SV *coro_current;
207static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 223static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
208static int coro_nready; 224static int coro_nready;
225static struct coro *coro_first;
209 226
210/** lowlevel stuff **********************************************************/ 227/** lowlevel stuff **********************************************************/
211 228
212static AV * 229static AV *
213coro_clone_padlist (pTHX_ CV *cv) 230coro_clone_padlist (pTHX_ CV *cv)
341#undef VAR 358#undef VAR
342 359
343 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 360 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
344 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 361 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
345 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 362 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
363 if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh);
364
346 if (c->irssv) 365 if (c->irssv)
347 { 366 {
348 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) 367 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
368 {
349 SvREFCNT_dec (c->irssv); 369 SvREFCNT_dec (c->irssv);
370 c->irssv = 0;
371 }
350 else 372 else
351 { 373 {
352 REPLACE_SV (PL_rs, c->irssv); 374 REPLACE_SV (PL_rs, c->irssv);
353 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); 375 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
354 sv_setsv (c->irssv_sv, PL_rs); 376 sv_setsv (c->irssv_sv, PL_rs);
367 CvPADLIST (cv) = (AV *)POPs; 389 CvPADLIST (cv) = (AV *)POPs;
368 } 390 }
369 391
370 PUTBACK; 392 PUTBACK;
371 } 393 }
372 assert (!PL_comppad || AvARRAY (PL_comppad));//D
373} 394}
374 395
375static void 396static void
376save_perl (pTHX_ Coro__State c) 397save_perl (pTHX_ Coro__State c)
377{ 398{
378 assert (!PL_comppad || AvARRAY (PL_comppad));//D
379 { 399 {
380 dSP; 400 dSP;
381 I32 cxix = cxstack_ix; 401 I32 cxix = cxstack_ix;
382 PERL_CONTEXT *ccstk = cxstack; 402 PERL_CONTEXT *ccstk = cxstack;
383 PERL_SI *top_si = PL_curstackinfo; 403 PERL_SI *top_si = PL_curstackinfo;
385 /* 405 /*
386 * the worst thing you can imagine happens first - we have to save 406 * the worst thing you can imagine happens first - we have to save
387 * (and reinitialize) all cv's in the whole callchain :( 407 * (and reinitialize) all cv's in the whole callchain :(
388 */ 408 */
389 409
390 EXTEND (SP, 3 + 1);
391 PUSHs (Nullsv); 410 XPUSHs (Nullsv);
392 /* this loop was inspired by pp_caller */ 411 /* this loop was inspired by pp_caller */
393 for (;;) 412 for (;;)
394 { 413 {
395 while (cxix >= 0) 414 while (cxix >= 0)
396 { 415 {
397 PERL_CONTEXT *cx = &ccstk[cxix--]; 416 PERL_CONTEXT *cx = &ccstk[cxix--];
398 417
399 if (CxTYPE (cx) == CXt_SUB) 418 if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)
400 { 419 {
401 CV *cv = cx->blk_sub.cv; 420 CV *cv = cx->blk_sub.cv;
402 421
403 if (CvDEPTH (cv)) 422 if (CvDEPTH (cv))
404 { 423 {
415 434
416 if (top_si->si_type == PERLSI_MAIN) 435 if (top_si->si_type == PERLSI_MAIN)
417 break; 436 break;
418 437
419 top_si = top_si->si_prev; 438 top_si = top_si->si_prev;
420 ccstk = top_si->si_cxstack; 439 ccstk = top_si->si_cxstack;
421 cxix = top_si->si_cxix; 440 cxix = top_si->si_cxix;
422 } 441 }
423 442
424 PUTBACK; 443 PUTBACK;
425 } 444 }
426 445
427 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 446 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; 447 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
429 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 448 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
449 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; 450 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0;
431 451
432#define VAR(name,type)c->name = PL_ ## name; 452#define VAR(name,type)c->name = PL_ ## name;
433# include "state.h" 453# include "state.h"
434#undef VAR 454#undef VAR
444# define coro_init_stacks init_stacks 464# define coro_init_stacks init_stacks
445#else 465#else
446static void 466static void
447coro_init_stacks (pTHX) 467coro_init_stacks (pTHX)
448{ 468{
449 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 469 PL_curstackinfo = new_stackinfo(64, 6);
450 PL_curstackinfo->si_type = PERLSI_MAIN; 470 PL_curstackinfo->si_type = PERLSI_MAIN;
451 PL_curstack = PL_curstackinfo->si_stack; 471 PL_curstack = PL_curstackinfo->si_stack;
452 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 472 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
453 473
454 PL_stack_base = AvARRAY(PL_curstack); 474 PL_stack_base = AvARRAY(PL_curstack);
455 PL_stack_sp = PL_stack_base; 475 PL_stack_sp = PL_stack_base;
456 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 476 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
457 477
458 New(50,PL_tmps_stack,128,SV*); 478 New(50,PL_tmps_stack,64,SV*);
459 PL_tmps_floor = -1; 479 PL_tmps_floor = -1;
460 PL_tmps_ix = -1; 480 PL_tmps_ix = -1;
461 PL_tmps_max = 128; 481 PL_tmps_max = 64;
462 482
463 New(54,PL_markstack,32,I32); 483 New(54,PL_markstack,16,I32);
464 PL_markstack_ptr = PL_markstack; 484 PL_markstack_ptr = PL_markstack;
465 PL_markstack_max = PL_markstack + 32; 485 PL_markstack_max = PL_markstack + 16;
466 486
467#ifdef SET_MARK_OFFSET 487#ifdef SET_MARK_OFFSET
468 SET_MARK_OFFSET; 488 SET_MARK_OFFSET;
469#endif 489#endif
470 490
471 New(54,PL_scopestack,32,I32); 491 New(54,PL_scopestack,16,I32);
472 PL_scopestack_ix = 0; 492 PL_scopestack_ix = 0;
473 PL_scopestack_max = 32; 493 PL_scopestack_max = 16;
474 494
475 New(54,PL_savestack,64,ANY); 495 New(54,PL_savestack,64,ANY);
476 PL_savestack_ix = 0; 496 PL_savestack_ix = 0;
477 PL_savestack_max = 64; 497 PL_savestack_max = 64;
478 498
479#if !PERL_VERSION_ATLEAST (5,9,0) 499#if !PERL_VERSION_ATLEAST (5,9,0)
480 New(54,PL_retstack,16,OP*); 500 New(54,PL_retstack,4,OP*);
481 PL_retstack_ix = 0; 501 PL_retstack_ix = 0;
482 PL_retstack_max = 16; 502 PL_retstack_max = 4;
483#endif 503#endif
484} 504}
485#endif 505#endif
486 506
487/* 507/*
527 Safefree (PL_scopestack); 547 Safefree (PL_scopestack);
528 Safefree (PL_savestack); 548 Safefree (PL_savestack);
529#if !PERL_VERSION_ATLEAST (5,9,0) 549#if !PERL_VERSION_ATLEAST (5,9,0)
530 Safefree (PL_retstack); 550 Safefree (PL_retstack);
531#endif 551#endif
552}
553
554static size_t
555coro_rss (struct coro *coro)
556{
557 size_t rss = sizeof (coro);
558
559 if (coro->mainstack)
560 {
561 if (coro->flags & CF_RUNNING)
562 {
563 #define VAR(name,type)coro->name = PL_ ## name;
564 # include "state.h"
565 #undef VAR
566 }
567
568 rss += sizeof (coro->curstackinfo);
569 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *);
570 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
571 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *);
572 rss += coro->tmps_max * sizeof (SV *);
573 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32);
574 rss += coro->scopestack_max * sizeof (I32);
575 rss += coro->savestack_max * sizeof (ANY);
576
577#if !PERL_VERSION_ATLEAST (5,9,0)
578 rss += coro->retstack_max * sizeof (OP *);
579#endif
580 }
581
582 return rss;
532} 583}
533 584
534/** coroutine stack handling ************************************************/ 585/** coroutine stack handling ************************************************/
535 586
536static void 587static void
559 Zero (&myop, 1, LOGOP); 610 Zero (&myop, 1, LOGOP);
560 myop.op_next = Nullop; 611 myop.op_next = Nullop;
561 myop.op_flags = OPf_WANT_VOID; 612 myop.op_flags = OPf_WANT_VOID;
562 613
563 PUSHMARK (SP); 614 PUSHMARK (SP);
564 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 615 XPUSHs (av_shift (GvAV (PL_defgv)));
565 PUTBACK; 616 PUTBACK;
566 PL_op = (OP *)&myop; 617 PL_op = (OP *)&myop;
567 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 618 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
568 SPAGAIN; 619 SPAGAIN;
569 } 620 }
579 SvREFCNT_dec (coro_mortal); 630 SvREFCNT_dec (coro_mortal);
580 coro_mortal = 0; 631 coro_mortal = 0;
581 } 632 }
582} 633}
583 634
635static void
636do_trace (pTHX)
637{
638 if (PL_curcop != &PL_compiling)
639 {
640 runops_proc_t old_runops = PL_runops;
641 dSP;
642 ENTER;
643 SAVETMPS;
644 PUSHMARK (SP);
645 PUTBACK;
646 PL_runops = RUNOPS_DEFAULT;
647 call_pv ("Coro::_do_trace", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD | G_NOARGS);
648 PL_runops = old_runops;
649 SPAGAIN;
650 FREETMPS;
651 LEAVE;
652 PUTBACK;
653 }
654}
655
656static int
657runops_coro (pTHX)
658{
659 COP *oldcop = 0;
660
661 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
662 {
663 PERL_ASYNC_CHECK ();
664
665 if (oldcop != PL_curcop)
666 {
667 oldcop = PL_curcop;
668 do_trace (aTHX);
669 }
670 }
671
672 TAINT_NOT;
673 return 0;
674}
675
584/* inject a fake call to Coro::State::_cctx_init into the execution */ 676/* inject a fake call to Coro::State::_cctx_init into the execution */
677/* _cctx_init should be careful, as it could be called at almost any time */
678/* during execution of a perl program */
585static void NOINLINE 679static void NOINLINE
586prepare_cctx (pTHX_ coro_cctx *cctx) 680prepare_cctx (pTHX_ coro_cctx *cctx)
587{ 681{
588 dSP; 682 dSP;
589 LOGOP myop; 683 LOGOP myop;
684
685 PL_top_env = &PL_start_env;
686
687 if (cctx->flags & CC_TRACE)
688 PL_runops = runops_coro;
590 689
591 Zero (&myop, 1, LOGOP); 690 Zero (&myop, 1, LOGOP);
592 myop.op_next = PL_op; 691 myop.op_next = PL_op;
593 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 692 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
594 693
600 PL_op = (OP *)&myop; 699 PL_op = (OP *)&myop;
601 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 700 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
602 SPAGAIN; 701 SPAGAIN;
603} 702}
604 703
704/*
705 * this is a _very_ stripped down perl interpreter ;)
706 */
605static void 707static void
606coro_run (void *arg) 708coro_run (void *arg)
607{ 709{
608 dTHX; 710 dTHX;
609 711
610 /* coro_run is the alternative tail of transfer(), so unlock here. */ 712 /* coro_run is the alternative tail of transfer(), so unlock here. */
611 UNLOCK; 713 UNLOCK;
612 714
613 /* 715 /* we now skip the entersub that lead to transfer() */
614 * this is a _very_ stripped down perl interpreter ;) 716 PL_op = PL_op->op_next;
615 */
616 PL_top_env = &PL_start_env;
617 717
618 /* inject call to cctx_init */ 718 /* inject a fake subroutine call to cctx_init */
619 prepare_cctx (aTHX_ (coro_cctx *)arg); 719 prepare_cctx (aTHX_ (coro_cctx *)arg);
620 720
621 /* somebody will hit me for both perl_run and PL_restartop */ 721 /* somebody or something will hit me for both perl_run and PL_restartop */
622 PL_restartop = PL_op; 722 PL_restartop = PL_op;
623 perl_run (PL_curinterp); 723 perl_run (PL_curinterp);
624 724
625 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); 725 /*
626 abort (); 726 * If perl-run returns we assume exit() was being called or the coro
727 * fell off the end, which seems to be the only valid (non-bug)
728 * reason for perl_run to return. We try to exit by jumping to the
729 * bootstrap-time "top" top_env, as we cannot restore the "main"
730 * coroutine as Coro has no such concept
731 */
732 PL_top_env = main_top_env;
733 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
627} 734}
628 735
629static coro_cctx * 736static coro_cctx *
630cctx_new () 737cctx_new ()
631{ 738{
648# if CORO_STACKGUARD 755# if CORO_STACKGUARD
649 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 756 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
650# endif 757# endif
651 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 758 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
652 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 759 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
653 cctx->mapped = 1; 760 cctx->flags |= CC_MAPPED;
654 } 761 }
655 else 762 else
656#endif 763#endif
657 { 764 {
658 cctx->ssize = coro_stacksize * (long)sizeof (long); 765 cctx->ssize = coro_stacksize * (long)sizeof (long);
685#if CORO_USE_VALGRIND 792#if CORO_USE_VALGRIND
686 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 793 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
687#endif 794#endif
688 795
689#if HAVE_MMAP 796#if HAVE_MMAP
690 if (cctx->mapped) 797 if (cctx->flags & CC_MAPPED)
691 munmap (cctx->sptr, cctx->ssize); 798 munmap (cctx->sptr, cctx->ssize);
692 else 799 else
693#endif 800#endif
694 Safefree (cctx->sptr); 801 Safefree (cctx->sptr);
695 802
703 { 810 {
704 coro_cctx *cctx = cctx_first; 811 coro_cctx *cctx = cctx_first;
705 cctx_first = cctx->next; 812 cctx_first = cctx->next;
706 --cctx_idle; 813 --cctx_idle;
707 814
708 if (cctx->ssize >= coro_stacksize) 815 if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE))
709 return cctx; 816 return cctx;
710 817
711 cctx_destroy (cctx); 818 cctx_destroy (cctx);
712 } 819 }
713 820
714 PL_op = PL_op->op_next;
715 return cctx_new (); 821 return cctx_new ();
716} 822}
717 823
718static void 824static void
719cctx_put (coro_cctx *cctx) 825cctx_put (coro_cctx *cctx)
723 { 829 {
724 coro_cctx *first = cctx_first; 830 coro_cctx *first = cctx_first;
725 cctx_first = first->next; 831 cctx_first = first->next;
726 --cctx_idle; 832 --cctx_idle;
727 833
728 assert (!first->inuse);
729 cctx_destroy (first); 834 cctx_destroy (first);
730 } 835 }
731 836
732 ++cctx_idle; 837 ++cctx_idle;
733 cctx->next = cctx_first; 838 cctx->next = cctx_first;
754 859
755 if (prev->flags & CF_NEW) 860 if (prev->flags & CF_NEW)
756 { 861 {
757 /* create a new empty context */ 862 /* create a new empty context */
758 Newz (0, prev->cctx, 1, coro_cctx); 863 Newz (0, prev->cctx, 1, coro_cctx);
759 prev->cctx->inuse = 1;
760 prev->flags &= ~CF_NEW; 864 prev->flags &= ~CF_NEW;
761 prev->flags |= CF_RUNNING; 865 prev->flags |= CF_RUNNING;
762 } 866 }
763 867
764 /*TODO: must not croak here */ 868 /*TODO: must not croak here */
782 next->flags &= ~CF_NEW; 886 next->flags &= ~CF_NEW;
783 /* first get rid of the old state */ 887 /* first get rid of the old state */
784 save_perl (aTHX_ prev); 888 save_perl (aTHX_ prev);
785 /* setup coroutine call */ 889 /* setup coroutine call */
786 setup_coro (aTHX_ next); 890 setup_coro (aTHX_ next);
787 /* need a new stack */
788 assert (!next->cctx);
789 } 891 }
790 else 892 else
791 { 893 {
792 /* coroutine already started */ 894 /* coroutine already started */
793 save_perl (aTHX_ prev); 895 save_perl (aTHX_ prev);
803 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 905 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
804 906
805 prev->cctx = 0; 907 prev->cctx = 0;
806 908
807 cctx_put (prev__cctx); 909 cctx_put (prev__cctx);
808 prev__cctx->inuse = 0;
809 } 910 }
810 911
811 if (!next->cctx) 912 if (!next->cctx)
812 {
813 next->cctx = cctx_get (aTHX); 913 next->cctx = cctx_get (aTHX);
814 assert (!next->cctx->inuse);
815 next->cctx->inuse = 1;
816 }
817 914
818 if (prev__cctx != next->cctx) 915 if (prev__cctx != next->cctx)
819 { 916 {
820 prev__cctx->top_env = PL_top_env; 917 prev__cctx->top_env = PL_top_env;
821 PL_top_env = next->cctx->top_env; 918 PL_top_env = next->cctx->top_env;
878 } 975 }
879 976
880 cctx_destroy (coro->cctx); 977 cctx_destroy (coro->cctx);
881 SvREFCNT_dec (coro->args); 978 SvREFCNT_dec (coro->args);
882 979
980 if (coro->next) coro->next->prev = coro->prev;
981 if (coro->prev) coro->prev->next = coro->next;
982 if (coro == coro_first) coro_first = coro->next;
983
883 return 1; 984 return 1;
884} 985}
885 986
886static int 987static int
887coro_state_free (pTHX_ SV *sv, MAGIC *mg) 988coro_state_free (pTHX_ SV *sv, MAGIC *mg)
888{ 989{
889 struct coro *coro = (struct coro *)mg->mg_ptr; 990 struct coro *coro = (struct coro *)mg->mg_ptr;
890 mg->mg_ptr = 0; 991 mg->mg_ptr = 0;
992
993 coro->hv = 0;
891 994
892 if (--coro->refcnt < 0) 995 if (--coro->refcnt < 0)
893 { 996 {
894 coro_state_destroy (aTHX_ coro); 997 coro_state_destroy (aTHX_ coro);
895 Safefree (coro); 998 Safefree (coro);
1163 1266
1164 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1267 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1165 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1268 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1166 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1269 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1167 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); 1270 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1271 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH));
1272 newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF));
1168 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); 1273 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1169 1274
1170 main_mainstack = PL_mainstack; 1275 main_mainstack = PL_mainstack;
1276 main_top_env = PL_top_env;
1277
1278 while (main_top_env->je_prev)
1279 main_top_env = main_top_env->je_prev;
1171 1280
1172 coroapi.ver = CORO_API_VERSION; 1281 coroapi.ver = CORO_API_VERSION;
1173 coroapi.transfer = api_transfer; 1282 coroapi.transfer = api_transfer;
1174 1283
1175 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1284 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1183 HV *hv; 1292 HV *hv;
1184 int i; 1293 int i;
1185 1294
1186 Newz (0, coro, 1, struct coro); 1295 Newz (0, coro, 1, struct coro);
1187 coro->args = newAV (); 1296 coro->args = newAV ();
1188 coro->save = CORO_SAVE_ALL; 1297 coro->save = CORO_SAVE_DEF;
1189 coro->flags = CF_NEW; 1298 coro->flags = CF_NEW;
1190 1299
1300 if (coro_first) coro_first->prev = coro;
1301 coro->next = coro_first;
1302 coro_first = coro;
1303
1191 hv = newHV (); 1304 coro->hv = hv = newHV ();
1192 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1305 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)); 1306 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1194 1307
1195 for (i = 1; i < items; i++) 1308 for (i = 1; i < items; i++)
1196 av_push (coro->args, newSVsv (ST (i))); 1309 av_push (coro->args, newSVsv (ST (i)));
1200 1313
1201int 1314int
1202save (SV *coro, int new_save = -1) 1315save (SV *coro, int new_save = -1)
1203 CODE: 1316 CODE:
1204 RETVAL = api_save (coro, new_save); 1317 RETVAL = api_save (coro, new_save);
1318 OUTPUT:
1319 RETVAL
1320
1321int
1322save_also (SV *coro_sv, int save_also)
1323 CODE:
1324{
1325 struct coro *coro = SvSTATE (coro_sv);
1326 RETVAL = coro->save;
1327 coro->save |= save_also;
1328}
1205 OUTPUT: 1329 OUTPUT:
1206 RETVAL 1330 RETVAL
1207 1331
1208void 1332void
1209_set_stacklevel (...) 1333_set_stacklevel (...)
1287 CODE: 1411 CODE:
1288 RETVAL = cctx_idle; 1412 RETVAL = cctx_idle;
1289 OUTPUT: 1413 OUTPUT:
1290 RETVAL 1414 RETVAL
1291 1415
1416void
1417list ()
1418 PPCODE:
1419{
1420 struct coro *coro;
1421 for (coro = coro_first; coro; coro = coro->next)
1422 if (coro->hv)
1423 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1424}
1425
1426void
1427call (Coro::State coro, SV *coderef)
1428 ALIAS:
1429 eval = 1
1430 CODE:
1431{
1432 if (coro->mainstack)
1433 {
1434 struct coro temp;
1435 Zero (&temp, 1, struct coro);
1436 temp.save = CORO_SAVE_ALL;
1437
1438 if (!(coro->flags & CF_RUNNING))
1439 {
1440 save_perl (aTHX_ &temp);
1441 load_perl (aTHX_ coro);
1442 }
1443
1444 {
1445 dSP;
1446 ENTER;
1447 SAVETMPS;
1448 PUSHMARK (SP);
1449 PUTBACK;
1450 if (ix)
1451 eval_sv (coderef, 0);
1452 else
1453 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1454 SPAGAIN;
1455 FREETMPS;
1456 LEAVE;
1457 PUTBACK;
1458 }
1459
1460 if (!(coro->flags & CF_RUNNING))
1461 {
1462 save_perl (aTHX_ coro);
1463 load_perl (aTHX_ &temp);
1464 }
1465 }
1466}
1467
1468SV *
1469is_ready (Coro::State coro)
1470 PROTOTYPE: $
1471 ALIAS:
1472 is_ready = CF_READY
1473 is_running = CF_RUNNING
1474 is_new = CF_NEW
1475 is_destroyed = CF_DESTROYED
1476 CODE:
1477 RETVAL = boolSV (coro->flags & ix);
1478 OUTPUT:
1479 RETVAL
1480
1481void
1482trace (Coro::State coro, int enable = 0)
1483 CODE:
1484 if (enable)
1485 {
1486 if (coro->cctx && coro->cctx->flags & CC_TRACE)
1487 XSRETURN_EMPTY;
1488
1489 if (coro->flags & CF_RUNNING)
1490 croak ("cannot enable tracing on running coroutine");
1491
1492 if (coro->cctx)
1493 croak ("cannot enable tracing on coroutine with custom stack");
1494
1495 coro->cctx = cctx_new ();
1496 coro->cctx->flags |= CC_TRACE;
1497 }
1498 else
1499 if (coro->cctx && coro->cctx->flags & CC_TRACE)
1500 {
1501 coro->cctx->flags &= ~CC_TRACE;
1502 coro->cctx->flags |= CC_NOREUSE;
1503 }
1504
1505SV *
1506has_stack (Coro::State coro)
1507 PROTOTYPE: $
1508 CODE:
1509 RETVAL = boolSV (!!coro->cctx);
1510 OUTPUT:
1511 RETVAL
1512
1513IV
1514rss (Coro::State coro)
1515 PROTOTYPE: $
1516 CODE:
1517 RETVAL = coro_rss (coro);
1518 OUTPUT:
1519 RETVAL
1520
1521
1292MODULE = Coro::State PACKAGE = Coro 1522MODULE = Coro::State PACKAGE = Coro
1293 1523
1294BOOT: 1524BOOT:
1295{ 1525{
1296 int i; 1526 int i;
1527
1528 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE);
1529 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE);
1530 av_async_pool = get_av ("Coro::async_pool", TRUE);
1531
1532 coro_current = get_sv ("Coro::current", FALSE);
1533 SvREADONLY_on (coro_current);
1297 1534
1298 coro_stash = gv_stashpv ("Coro", TRUE); 1535 coro_stash = gv_stashpv ("Coro", TRUE);
1299 1536
1300 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1537 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1301 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1538 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1302 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1539 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1303 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1540 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1304 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1541 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1305 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1542 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1306
1307 coro_current = get_sv ("Coro::current", FALSE);
1308 SvREADONLY_on (coro_current);
1309 1543
1310 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1544 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1311 coro_ready[i] = newAV (); 1545 coro_ready[i] = newAV ();
1312 1546
1313 { 1547 {
1363 CODE: 1597 CODE:
1364 RETVAL = boolSV (api_ready (self)); 1598 RETVAL = boolSV (api_ready (self));
1365 OUTPUT: 1599 OUTPUT:
1366 RETVAL 1600 RETVAL
1367 1601
1368SV *
1369is_ready (SV *self)
1370 PROTOTYPE: $
1371 CODE:
1372 RETVAL = boolSV (api_is_ready (self));
1373 OUTPUT:
1374 RETVAL
1375
1376int 1602int
1377nready (...) 1603nready (...)
1378 PROTOTYPE: 1604 PROTOTYPE:
1379 CODE: 1605 CODE:
1380 RETVAL = coro_nready; 1606 RETVAL = coro_nready;
1381 OUTPUT: 1607 OUTPUT:
1382 RETVAL 1608 RETVAL
1383 1609
1610# for async_pool speedup
1611void
1612_pool_1 (SV *cb)
1613 CODE:
1614{
1615 int i, len;
1616 HV *hv = (HV *)SvRV (coro_current);
1617 AV *defav = GvAV (PL_defgv);
1618 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1619 AV *invoke_av;
1620
1621 if (!invoke)
1622 croak ("\3terminate\2\n");
1623
1624 hv_store (hv, "desc", sizeof ("desc") - 1,
1625 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1626
1627 invoke_av = (AV *)SvRV (invoke);
1628 len = av_len (invoke_av);
1629
1630 sv_setsv (cb, AvARRAY (invoke_av)[0]);
1631
1632 if (len > 0)
1633 {
1634 av_fill (defav, len - 1);
1635 for (i = 0; i < len; ++i)
1636 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1]));
1637 }
1638
1639 SvREFCNT_dec (invoke);
1640}
1641
1642void
1643_pool_2 (SV *cb)
1644 CODE:
1645{
1646 struct coro *coro = SvSTATE (coro_current);
1647
1648 sv_setsv (cb, &PL_sv_undef);
1649
1650 if (coro_rss (coro) > SvIV (sv_pool_rss)
1651 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1652 croak ("\3terminate\2\n");
1653
1654 av_clear (GvAV (PL_defgv));
1655 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1656 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1657 coro->save = CORO_SAVE_DEF;
1658 coro->prio = 0;
1659 av_push (av_async_pool, newSVsv (coro_current));
1660}
1661
1662
1384MODULE = Coro::State PACKAGE = Coro::AIO 1663MODULE = Coro::State PACKAGE = Coro::AIO
1385 1664
1386SV * 1665SV *
1387_get_state () 1666_get_state ()
1388 CODE: 1667 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines