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.174 by root, Sun Sep 30 13:43:18 2007 UTC vs.
Revision 1.197 by root, Sat Oct 6 19:25:00 2007 UTC

75/* 5.8.7 */ 75/* 5.8.7 */
76#ifndef SvRV_set 76#ifndef SvRV_set
77# define SvRV_set(s,v) SvRV(s) = (v) 77# define SvRV_set(s,v) SvRV(s) = (v)
78#endif 78#endif
79 79
80/* 5.8.8 */
81#ifndef GV_NOTQUAL
82# define GV_NOTQUAL 0
83#endif
84#ifndef newSV
85# define newSV(l) NEWSV(0,l)
86#endif
87
80#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 88#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
81# undef CORO_STACKGUARD 89# undef CORO_STACKGUARD
82#endif 90#endif
83 91
84#ifndef CORO_STACKGUARD 92#ifndef CORO_STACKGUARD
98#define IN_DESTRUCT (PL_main_cv == Nullcv) 106#define IN_DESTRUCT (PL_main_cv == Nullcv)
99 107
100#if __GNUC__ >= 3 108#if __GNUC__ >= 3
101# define attribute(x) __attribute__(x) 109# define attribute(x) __attribute__(x)
102# define BARRIER __asm__ __volatile__ ("" : : : "memory") 110# define BARRIER __asm__ __volatile__ ("" : : : "memory")
111# define expect(expr,value) __builtin_expect ((expr),(value))
103#else 112#else
104# define attribute(x) 113# define attribute(x)
105# define BARRIER 114# define BARRIER
115# define expect(expr,value) (expr)
106#endif 116#endif
117
118#define expect_false(expr) expect ((expr) != 0, 0)
119#define expect_true(expr) expect ((expr) != 0, 1)
107 120
108#define NOINLINE attribute ((noinline)) 121#define NOINLINE attribute ((noinline))
109 122
110#include "CoroAPI.h" 123#include "CoroAPI.h"
111 124
131static struct CoroAPI coroapi; 144static struct CoroAPI coroapi;
132static AV *main_mainstack; /* used to differentiate between $main and others */ 145static AV *main_mainstack; /* used to differentiate between $main and others */
133static JMPENV *main_top_env; 146static JMPENV *main_top_env;
134static HV *coro_state_stash, *coro_stash; 147static HV *coro_state_stash, *coro_stash;
135static SV *coro_mortal; /* will be freed after next transfer */ 148static SV *coro_mortal; /* will be freed after next transfer */
149
150static GV *irsgv; /* $/ */
151static GV *stdoutgv; /* *STDOUT */
136 152
137/* async_pool helper stuff */ 153/* async_pool helper stuff */
138static SV *sv_pool_rss; 154static SV *sv_pool_rss;
139static SV *sv_pool_size; 155static SV *sv_pool_size;
140static AV *av_async_pool; 156static AV *av_async_pool;
184 coro_cctx *cctx; 200 coro_cctx *cctx;
185 201
186 /* data associated with this coroutine (initial args) */ 202 /* data associated with this coroutine (initial args) */
187 AV *args; 203 AV *args;
188 int refcnt; 204 int refcnt;
189 int save; /* CORO_SAVE flags */
190 int flags; /* CF_ flags */ 205 int flags; /* CF_ flags */
191 206
192 /* optionally saved, might be zero */ 207 /* optionally saved, might be zero */
193 AV *defav; /* @_ */ 208 AV *defav; /* @_ */
194 SV *defsv; /* $_ */ 209 SV *defsv; /* $_ */
195 SV *errsv; /* $@ */ 210 SV *errsv; /* $@ */
196 GV *deffh; /* default filehandle */ 211 SV *deffh; /* default filehandle */
197 SV *irssv; /* $/ */ 212 SV *irssv; /* $/ */
198 SV *irssv_sv; /* real $/ cache */ 213 SV *irssv_sv; /* real $/ cache */
199 214
200#define VAR(name,type) type name; 215#define VAR(name,type) type name;
201# include "state.h" 216# include "state.h"
202#undef VAR 217#undef VAR
203 218
204 /* statistics */ 219 /* statistics */
205 int usecount; /* number of switches to this coro */ 220 int usecount; /* number of transfers to this coro */
206 221
207 /* coro process data */ 222 /* coro process data */
208 int prio; 223 int prio;
224 SV *throw;
225
226 /* async_pool */
227 SV *saved_deffh;
209 228
210 /* linked list */ 229 /* linked list */
211 struct coro *next, *prev; 230 struct coro *next, *prev;
212 HV *hv; /* the perl hash associated with this coro, if any */ 231 HV *hv; /* the perl hash associated with this coro, if any */
213}; 232};
311 MAGIC *mg; 330 MAGIC *mg;
312 331
313 if (SvROK (coro)) 332 if (SvROK (coro))
314 coro = SvRV (coro); 333 coro = SvRV (coro);
315 334
316 if (SvTYPE (coro) != SVt_PVHV) 335 if (expect_false (SvTYPE (coro) != SVt_PVHV))
317 croak ("Coro::State object required"); 336 croak ("Coro::State object required");
318 337
319 stash = SvSTASH (coro); 338 stash = SvSTASH (coro);
320 if (stash != coro_stash && stash != coro_state_stash) 339 if (expect_false (stash != coro_stash && stash != coro_state_stash))
321 { 340 {
322 /* very slow, but rare, check */ 341 /* very slow, but rare, check */
323 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) 342 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
324 croak ("Coro::State object required"); 343 croak ("Coro::State object required");
325 } 344 }
335get_padlist (pTHX_ CV *cv) 354get_padlist (pTHX_ CV *cv)
336{ 355{
337 MAGIC *mg = CORO_MAGIC (cv); 356 MAGIC *mg = CORO_MAGIC (cv);
338 AV *av; 357 AV *av;
339 358
340 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) 359 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
341 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 360 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
342 else 361 else
343 { 362 {
344#if CORO_PREFER_PERL_FUNCTIONS 363#if CORO_PREFER_PERL_FUNCTIONS
345 /* this is probably cleaner, but also slower? */ 364 /* this is probably cleaner, but also slower? */
357put_padlist (pTHX_ CV *cv) 376put_padlist (pTHX_ CV *cv)
358{ 377{
359 MAGIC *mg = CORO_MAGIC (cv); 378 MAGIC *mg = CORO_MAGIC (cv);
360 AV *av; 379 AV *av;
361 380
362 if (!mg) 381 if (expect_false (!mg))
363 { 382 {
364 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); 383 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
365 mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 384 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
366 mg->mg_virtual = &vtbl_coro; 385 mg->mg_virtual = &vtbl_coro;
367 mg->mg_obj = (SV *)newAV (); 386 mg->mg_obj = (SV *)newAV ();
368 } 387 }
369 388
370 av = (AV *)mg->mg_obj; 389 av = (AV *)mg->mg_obj;
371 390
372 if (AvFILLp (av) >= AvMAX (av)) 391 if (expect_false (AvFILLp (av) >= AvMAX (av)))
373 av_extend (av, AvMAX (av) + 1); 392 av_extend (av, AvMAX (av) + 1);
374 393
375 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 394 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
376} 395}
377 396
378/** load & save, init *******************************************************/ 397/** load & save, init *******************************************************/
379
380#define SB do {
381#define SE } while (0)
382
383#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
384 398
385static void 399static void
386load_perl (pTHX_ Coro__State c) 400load_perl (pTHX_ Coro__State c)
387{ 401{
388#define VAR(name,type) PL_ ## name = c->name; 402#define VAR(name,type) PL_ ## name = c->name;
389# include "state.h" 403# include "state.h"
390#undef VAR 404#undef VAR
391 405
392 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 406 GvSV (PL_defgv) = c->defsv;
393 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 407 GvAV (PL_defgv) = c->defav;
394 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 408 GvSV (PL_errgv) = c->errsv;
395 if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh); 409 GvSV (irsgv) = c->irssv_sv;
396
397 if (c->irssv)
398 {
399 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
400 {
401 SvREFCNT_dec (c->irssv);
402 c->irssv = 0;
403 }
404 else
405 {
406 REPLACE_SV (PL_rs, c->irssv);
407 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
408 sv_setsv (c->irssv_sv, PL_rs);
409 }
410 }
411 410
412 { 411 {
413 dSP; 412 dSP;
414 CV *cv; 413 CV *cv;
415 414
416 /* now do the ugly restore mess */ 415 /* now do the ugly restore mess */
417 while ((cv = (CV *)POPs)) 416 while (expect_true (cv = (CV *)POPs))
418 { 417 {
419 put_padlist (aTHX_ cv); /* mark this padlist as available */ 418 put_padlist (aTHX_ cv); /* mark this padlist as available */
420 CvDEPTH (cv) = PTR2IV (POPs); 419 CvDEPTH (cv) = PTR2IV (POPs);
421 CvPADLIST (cv) = (AV *)POPs; 420 CvPADLIST (cv) = (AV *)POPs;
422 } 421 }
441 440
442 XPUSHs (Nullsv); 441 XPUSHs (Nullsv);
443 /* this loop was inspired by pp_caller */ 442 /* this loop was inspired by pp_caller */
444 for (;;) 443 for (;;)
445 { 444 {
446 while (cxix >= 0) 445 while (expect_true (cxix >= 0))
447 { 446 {
448 PERL_CONTEXT *cx = &ccstk[cxix--]; 447 PERL_CONTEXT *cx = &ccstk[cxix--];
449 448
450 if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT) 449 if (expect_true (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT))
451 { 450 {
452 CV *cv = cx->blk_sub.cv; 451 CV *cv = cx->blk_sub.cv;
453 452
454 if (CvDEPTH (cv)) 453 if (expect_true (CvDEPTH (cv)))
455 { 454 {
456 EXTEND (SP, 3); 455 EXTEND (SP, 3);
457 PUSHs ((SV *)CvPADLIST (cv)); 456 PUSHs ((SV *)CvPADLIST (cv));
458 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 457 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
459 PUSHs ((SV *)cv); 458 PUSHs ((SV *)cv);
462 get_padlist (aTHX_ cv); 461 get_padlist (aTHX_ cv);
463 } 462 }
464 } 463 }
465 } 464 }
466 465
467 if (top_si->si_type == PERLSI_MAIN) 466 if (expect_true (top_si->si_type == PERLSI_MAIN))
468 break; 467 break;
469 468
470 top_si = top_si->si_prev; 469 top_si = top_si->si_prev;
471 ccstk = top_si->si_cxstack; 470 ccstk = top_si->si_cxstack;
472 cxix = top_si->si_cxix; 471 cxix = top_si->si_cxix;
473 } 472 }
474 473
475 PUTBACK; 474 PUTBACK;
476 } 475 }
477 476
478 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 477 c->defav = GvAV (PL_defgv);
479 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 478 c->defsv = DEFSV;
480 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 479 c->errsv = ERRSV;
481 c->deffh = c->save & CORO_SAVE_DEFFH ? (GV *)SvREFCNT_inc (PL_defoutgv) : 0; 480 c->irssv_sv = GvSV (irsgv);
482 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0;
483 481
484#define VAR(name,type)c->name = PL_ ## name; 482#define VAR(name,type)c->name = PL_ ## name;
485# include "state.h" 483# include "state.h"
486#undef VAR 484#undef VAR
487} 485}
540 * destroy the stacks, the callchain etc... 538 * destroy the stacks, the callchain etc...
541 */ 539 */
542static void 540static void
543coro_destroy_stacks (pTHX) 541coro_destroy_stacks (pTHX)
544{ 542{
545 if (!IN_DESTRUCT)
546 {
547 /* restore all saved variables and stuff */
548 LEAVE_SCOPE (0);
549 assert (PL_tmps_floor == -1);
550
551 /* free all temporaries */
552 FREETMPS;
553 assert (PL_tmps_ix == -1);
554
555 /* unwind all extra stacks */
556 POPSTACK_TO (PL_mainstack);
557
558 /* unwind main stack */
559 dounwind (-1);
560 }
561
562 while (PL_curstackinfo->si_next) 543 while (PL_curstackinfo->si_next)
563 PL_curstackinfo = PL_curstackinfo->si_next; 544 PL_curstackinfo = PL_curstackinfo->si_next;
564 545
565 while (PL_curstackinfo) 546 while (PL_curstackinfo)
566 { 547 {
584} 565}
585 566
586static size_t 567static size_t
587coro_rss (pTHX_ struct coro *coro) 568coro_rss (pTHX_ struct coro *coro)
588{ 569{
589 size_t rss = sizeof (coro); 570 size_t rss = sizeof (*coro);
590 571
591 if (coro->mainstack) 572 if (coro->mainstack)
592 { 573 {
593 if (coro->flags & CF_RUNNING) 574 if (coro->flags & CF_RUNNING)
594 { 575 {
596 # include "state.h" 577 # include "state.h"
597 #undef VAR 578 #undef VAR
598 } 579 }
599 580
600 rss += sizeof (coro->curstackinfo); 581 rss += sizeof (coro->curstackinfo);
601 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *); 582 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstackinfo->si_stack)) * sizeof (SV *);
602 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 583 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
603 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); 584 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *);
604 rss += coro->tmps_max * sizeof (SV *); 585 rss += coro->tmps_max * sizeof (SV *);
605 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); 586 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32);
606 rss += coro->scopestack_max * sizeof (I32); 587 rss += coro->scopestack_max * sizeof (I32);
607 rss += coro->savestack_max * sizeof (ANY); 588 rss += coro->savestack_max * sizeof (ANY);
608 589
615} 596}
616 597
617/** coroutine stack handling ************************************************/ 598/** coroutine stack handling ************************************************/
618 599
619static void 600static void
620setup_coro (pTHX_ struct coro *coro) 601coro_setup (pTHX_ struct coro *coro)
621{ 602{
622 /* 603 /*
623 * emulate part of the perl startup here. 604 * emulate part of the perl startup here.
624 */ 605 */
625 coro_init_stacks (aTHX); 606 coro_init_stacks (aTHX);
627 PL_runops = RUNOPS_DEFAULT; 608 PL_runops = RUNOPS_DEFAULT;
628 PL_curcop = &PL_compiling; 609 PL_curcop = &PL_compiling;
629 PL_in_eval = EVAL_NULL; 610 PL_in_eval = EVAL_NULL;
630 PL_comppad = 0; 611 PL_comppad = 0;
631 PL_curpm = 0; 612 PL_curpm = 0;
613 PL_curpad = 0;
632 PL_localizing = 0; 614 PL_localizing = 0;
633 PL_dirty = 0; 615 PL_dirty = 0;
634 PL_restartop = 0; 616 PL_restartop = 0;
617
618 GvSV (PL_defgv) = newSV (0);
619 GvAV (PL_defgv) = coro->args; coro->args = 0;
620 GvSV (PL_errgv) = newSV (0);
621 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
622 PL_rs = newSVsv (GvSV (irsgv));
623 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv);
635 624
636 { 625 {
637 dSP; 626 dSP;
638 LOGOP myop; 627 LOGOP myop;
639 628
640 SvREFCNT_dec (GvAV (PL_defgv));
641 GvAV (PL_defgv) = coro->args; coro->args = 0;
642
643 Zero (&myop, 1, LOGOP); 629 Zero (&myop, 1, LOGOP);
644 myop.op_next = Nullop; 630 myop.op_next = Nullop;
645 myop.op_flags = OPf_WANT_VOID; 631 myop.op_flags = OPf_WANT_VOID;
646 632
647 PUSHMARK (SP); 633 PUSHMARK (SP);
648 XPUSHs (av_shift (GvAV (PL_defgv))); 634 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
649 PUTBACK; 635 PUTBACK;
650 PL_op = (OP *)&myop; 636 PL_op = (OP *)&myop;
651 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 637 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
652 SPAGAIN; 638 SPAGAIN;
653 } 639 }
654 640
655 ENTER; /* necessary e.g. for dounwind */ 641 ENTER; /* necessary e.g. for dounwind and to balance the xsub-entersub */
642}
643
644static void
645coro_destroy (pTHX_ struct coro *coro)
646{
647 if (!IN_DESTRUCT)
648 {
649 /* restore all saved variables and stuff */
650 LEAVE_SCOPE (0);
651 assert (PL_tmps_floor == -1);
652
653 /* free all temporaries */
654 FREETMPS;
655 assert (PL_tmps_ix == -1);
656
657 /* unwind all extra stacks */
658 POPSTACK_TO (PL_mainstack);
659
660 /* unwind main stack */
661 dounwind (-1);
662 }
663
664 SvREFCNT_dec (GvSV (PL_defgv));
665 SvREFCNT_dec (GvAV (PL_defgv));
666 SvREFCNT_dec (GvSV (PL_errgv));
667 SvREFCNT_dec (PL_defoutgv);
668 SvREFCNT_dec (PL_rs);
669 SvREFCNT_dec (GvSV (irsgv));
670
671 SvREFCNT_dec (coro->saved_deffh);
672 SvREFCNT_dec (coro->throw);
673
674 coro_destroy_stacks (aTHX);
656} 675}
657 676
658static void 677static void
659free_coro_mortal (pTHX) 678free_coro_mortal (pTHX)
660{ 679{
661 if (coro_mortal) 680 if (expect_true (coro_mortal))
662 { 681 {
663 SvREFCNT_dec (coro_mortal); 682 SvREFCNT_dec (coro_mortal);
664 coro_mortal = 0; 683 coro_mortal = 0;
665 } 684 }
666} 685}
695 bot = PL_stack_base + cx->blk_oldsp + 1; 714 bot = PL_stack_base + cx->blk_oldsp + 1;
696 top = cx->blk_gimme == G_ARRAY ? SP + 1 715 top = cx->blk_gimme == G_ARRAY ? SP + 1
697 : cx->blk_gimme == G_SCALAR ? bot + 1 716 : cx->blk_gimme == G_SCALAR ? bot + 1
698 : bot; 717 : bot;
699 718
719 av_extend (av, top - bot);
700 while (bot < top) 720 while (bot < top)
701 av_push (av, SvREFCNT_inc (*bot++)); 721 av_push (av, SvREFCNT_inc (*bot++));
702 722
703 PL_runops = RUNOPS_DEFAULT; 723 PL_runops = RUNOPS_DEFAULT;
704 ENTER; 724 ENTER;
916 Safefree (cctx->sptr); 936 Safefree (cctx->sptr);
917 937
918 Safefree (cctx); 938 Safefree (cctx);
919} 939}
920 940
941/* wether this cctx should be destructed */
942#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE))
943
921static coro_cctx * 944static coro_cctx *
922cctx_get (pTHX) 945cctx_get (pTHX)
923{ 946{
924 while (cctx_first) 947 while (expect_true (cctx_first))
925 { 948 {
926 coro_cctx *cctx = cctx_first; 949 coro_cctx *cctx = cctx_first;
927 cctx_first = cctx->next; 950 cctx_first = cctx->next;
928 --cctx_idle; 951 --cctx_idle;
929 952
930 if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE)) 953 if (expect_true (!CCTX_EXPIRED (cctx)))
931 return cctx; 954 return cctx;
932 955
933 cctx_destroy (cctx); 956 cctx_destroy (cctx);
934 } 957 }
935 958
938 961
939static void 962static void
940cctx_put (coro_cctx *cctx) 963cctx_put (coro_cctx *cctx)
941{ 964{
942 /* free another cctx if overlimit */ 965 /* free another cctx if overlimit */
943 if (cctx_idle >= MAX_IDLE_CCTX) 966 if (expect_false (cctx_idle >= MAX_IDLE_CCTX))
944 { 967 {
945 coro_cctx *first = cctx_first; 968 coro_cctx *first = cctx_first;
946 cctx_first = first->next; 969 cctx_first = first->next;
947 --cctx_idle; 970 --cctx_idle;
948 971
954 cctx_first = cctx; 977 cctx_first = cctx;
955} 978}
956 979
957/** coroutine switching *****************************************************/ 980/** coroutine switching *****************************************************/
958 981
959static void NOINLINE 982static void
960transfer_check (pTHX_ struct coro *prev, struct coro *next) 983transfer_check (pTHX_ struct coro *prev, struct coro *next)
961{ 984{
962 if (prev != next) 985 if (expect_true (prev != next))
963 { 986 {
964 if (!(prev->flags & (CF_RUNNING | CF_NEW))) 987 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
965 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states"); 988 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states");
966 989
967 if (next->flags & CF_RUNNING) 990 if (expect_false (next->flags & CF_RUNNING))
968 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 991 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
969 992
970 if (next->flags & CF_DESTROYED) 993 if (expect_false (next->flags & CF_DESTROYED))
971 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); 994 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
972 995
973 if (PL_lex_state != LEX_NOTPARSING) 996 if (expect_false (PL_lex_state != LEX_NOTPARSING))
974 croak ("Coro::State::transfer called while parsing, but this is not supported"); 997 croak ("Coro::State::transfer called while parsing, but this is not supported");
975 } 998 }
976} 999}
977 1000
978/* always use the TRANSFER macro */ 1001/* always use the TRANSFER macro */
980transfer (pTHX_ struct coro *prev, struct coro *next) 1003transfer (pTHX_ struct coro *prev, struct coro *next)
981{ 1004{
982 dSTACKLEVEL; 1005 dSTACKLEVEL;
983 1006
984 /* sometimes transfer is only called to set idle_sp */ 1007 /* sometimes transfer is only called to set idle_sp */
985 if (!next) 1008 if (expect_false (!next))
986 { 1009 {
987 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1010 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
988 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1011 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
989 } 1012 }
990 else if (prev != next) 1013 else if (expect_true (prev != next))
991 { 1014 {
992 coro_cctx *prev__cctx; 1015 coro_cctx *prev__cctx;
993 1016
994 if (prev->flags & CF_NEW) 1017 if (expect_false (prev->flags & CF_NEW))
995 { 1018 {
996 /* create a new empty context */ 1019 /* create a new empty context */
997 Newz (0, prev->cctx, 1, coro_cctx); 1020 Newz (0, prev->cctx, 1, coro_cctx);
998 prev->flags &= ~CF_NEW; 1021 prev->flags &= ~CF_NEW;
999 prev->flags |= CF_RUNNING; 1022 prev->flags |= CF_RUNNING;
1002 prev->flags &= ~CF_RUNNING; 1025 prev->flags &= ~CF_RUNNING;
1003 next->flags |= CF_RUNNING; 1026 next->flags |= CF_RUNNING;
1004 1027
1005 LOCK; 1028 LOCK;
1006 1029
1007 if (next->flags & CF_NEW) 1030 if (expect_false (next->flags & CF_NEW))
1008 { 1031 {
1009 /* need to start coroutine */ 1032 /* need to start coroutine */
1010 next->flags &= ~CF_NEW; 1033 next->flags &= ~CF_NEW;
1011 /* first get rid of the old state */ 1034 /* first get rid of the old state */
1012 save_perl (aTHX_ prev); 1035 save_perl (aTHX_ prev);
1013 /* setup coroutine call */ 1036 /* setup coroutine call */
1014 setup_coro (aTHX_ next); 1037 coro_setup (aTHX_ next);
1015 } 1038 }
1016 else 1039 else
1017 { 1040 {
1018 /* coroutine already started */ 1041 /* coroutine already started */
1019 save_perl (aTHX_ prev); 1042 save_perl (aTHX_ prev);
1021 } 1044 }
1022 1045
1023 prev__cctx = prev->cctx; 1046 prev__cctx = prev->cctx;
1024 1047
1025 /* possibly "free" the cctx */ 1048 /* possibly "free" the cctx */
1026 if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE)) 1049 if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE)))
1027 { 1050 {
1028 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1051 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
1029 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 1052 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
1030 1053
1031 prev->cctx = 0; 1054 prev->cctx = 0;
1032 1055
1056 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1057 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1058 if (expect_false (CCTX_EXPIRED (prev__cctx)))
1059 next->cctx = cctx_get (aTHX);
1060
1033 cctx_put (prev__cctx); 1061 cctx_put (prev__cctx);
1034 } 1062 }
1035 1063
1036 ++next->usecount; 1064 ++next->usecount;
1037 1065
1038 if (!next->cctx) 1066 if (expect_true (!next->cctx))
1039 next->cctx = cctx_get (aTHX); 1067 next->cctx = cctx_get (aTHX);
1040 1068
1041 if (prev__cctx != next->cctx) 1069 if (expect_false (prev__cctx != next->cctx))
1042 { 1070 {
1043 prev__cctx->top_env = PL_top_env; 1071 prev__cctx->top_env = PL_top_env;
1044 PL_top_env = next->cctx->top_env; 1072 PL_top_env = next->cctx->top_env;
1045 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1073 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
1046 } 1074 }
1047 1075
1048 free_coro_mortal (aTHX); 1076 free_coro_mortal (aTHX);
1049 UNLOCK; 1077 UNLOCK;
1078
1079 if (expect_false (prev->throw || next->throw))
1080 {
1081 struct coro *coro = SvSTATE (coro_current);
1082
1083 if (coro->throw)
1084 {
1085 SV *exception = coro->throw;
1086 coro->throw = 0;
1087 sv_setsv (ERRSV, exception);
1088 croak (0);
1089 }
1090 }
1050 } 1091 }
1051} 1092}
1052 1093
1053struct transfer_args 1094struct transfer_args
1054{ 1095{
1081 1122
1082 if (coro->mainstack && coro->mainstack != main_mainstack) 1123 if (coro->mainstack && coro->mainstack != main_mainstack)
1083 { 1124 {
1084 struct coro temp; 1125 struct coro temp;
1085 1126
1086 assert (!(coro->flags & CF_RUNNING));
1087
1088 Zero (&temp, 1, struct coro);
1089 temp.save = CORO_SAVE_ALL;
1090
1091 if (coro->flags & CF_RUNNING) 1127 if (coro->flags & CF_RUNNING)
1092 croak ("FATAL: tried to destroy currently running coroutine"); 1128 croak ("FATAL: tried to destroy currently running coroutine");
1093 1129
1094 save_perl (aTHX_ &temp); 1130 save_perl (aTHX_ &temp);
1095 load_perl (aTHX_ coro); 1131 load_perl (aTHX_ coro);
1096 1132
1097 coro_destroy_stacks (aTHX); 1133 coro_destroy (aTHX_ coro);
1098 1134
1099 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ 1135 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */
1100 1136
1101 coro->mainstack = 0; 1137 coro->mainstack = 0;
1102 } 1138 }
1165 1201
1166 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1202 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1167 TRANSFER (ta); 1203 TRANSFER (ta);
1168} 1204}
1169 1205
1170static int
1171api_save (SV *coro_sv, int new_save)
1172{
1173 dTHX;
1174 struct coro *coro = SvSTATE (coro_sv);
1175 int old_save = coro->save;
1176
1177 if (new_save >= 0)
1178 coro->save = new_save;
1179
1180 return old_save;
1181}
1182
1183/** Coro ********************************************************************/ 1206/** Coro ********************************************************************/
1184 1207
1185static void 1208static void
1186coro_enq (pTHX_ SV *coro_sv) 1209coro_enq (pTHX_ SV *coro_sv)
1187{ 1210{
1244 { 1267 {
1245 LOCK; 1268 LOCK;
1246 next_sv = coro_deq (aTHX_ PRIO_MIN); 1269 next_sv = coro_deq (aTHX_ PRIO_MIN);
1247 1270
1248 /* nothing to schedule: call the idle handler */ 1271 /* nothing to schedule: call the idle handler */
1249 if (!next_sv) 1272 if (expect_false (!next_sv))
1250 { 1273 {
1251 dSP; 1274 dSP;
1252 UNLOCK; 1275 UNLOCK;
1253 1276
1254 ENTER; 1277 ENTER;
1264 } 1287 }
1265 1288
1266 ta->next = SvSTATE (next_sv); 1289 ta->next = SvSTATE (next_sv);
1267 1290
1268 /* cannot transfer to destroyed coros, skip and look for next */ 1291 /* cannot transfer to destroyed coros, skip and look for next */
1269 if (ta->next->flags & CF_DESTROYED) 1292 if (expect_false (ta->next->flags & CF_DESTROYED))
1270 { 1293 {
1271 UNLOCK; 1294 UNLOCK;
1272 SvREFCNT_dec (next_sv); 1295 SvREFCNT_dec (next_sv);
1273 /* coro_nready is already taken care of by destroy */ 1296 /* coro_nready is already taken care of by destroy */
1274 continue; 1297 continue;
1330 dTHX; 1353 dTHX;
1331 struct transfer_args ta; 1354 struct transfer_args ta;
1332 1355
1333 prepare_cede (aTHX_ &ta); 1356 prepare_cede (aTHX_ &ta);
1334 1357
1335 if (ta.prev != ta.next) 1358 if (expect_true (ta.prev != ta.next))
1336 { 1359 {
1337 TRANSFER (ta); 1360 TRANSFER (ta);
1338 return 1; 1361 return 1;
1339 } 1362 }
1340 else 1363 else
1391#ifdef USE_ITHREADS 1414#ifdef USE_ITHREADS
1392 MUTEX_INIT (&coro_mutex); 1415 MUTEX_INIT (&coro_mutex);
1393#endif 1416#endif
1394 BOOT_PAGESIZE; 1417 BOOT_PAGESIZE;
1395 1418
1419 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1420 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1421
1396 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1422 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1397 1423
1398 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); 1424 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1399 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); 1425 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1400 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); 1426 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1401 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); 1427 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1402 1428
1403 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1404 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1405 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1406 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1407 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH));
1408 newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF));
1409 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1410
1411 main_mainstack = PL_mainstack; 1429 main_mainstack = PL_mainstack;
1412 main_top_env = PL_top_env; 1430 main_top_env = PL_top_env;
1413 1431
1414 while (main_top_env->je_prev) 1432 while (main_top_env->je_prev)
1415 main_top_env = main_top_env->je_prev; 1433 main_top_env = main_top_env->je_prev;
1427 struct coro *coro; 1445 struct coro *coro;
1428 HV *hv; 1446 HV *hv;
1429 int i; 1447 int i;
1430 1448
1431 Newz (0, coro, 1, struct coro); 1449 Newz (0, coro, 1, struct coro);
1432 coro->args = newAV (); 1450 coro->args = newAV ();
1433 coro->save = CORO_SAVE_DEF;
1434 coro->flags = CF_NEW; 1451 coro->flags = CF_NEW;
1435 1452
1436 if (coro_first) coro_first->prev = coro; 1453 if (coro_first) coro_first->prev = coro;
1437 coro->next = coro_first; 1454 coro->next = coro_first;
1438 coro_first = coro; 1455 coro_first = coro;
1439 1456
1440 coro->hv = hv = newHV (); 1457 coro->hv = hv = newHV ();
1441 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1458 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1442 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1459 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1443 1460
1461 av_extend (coro->args, items - 1);
1444 for (i = 1; i < items; i++) 1462 for (i = 1; i < items; i++)
1445 av_push (coro->args, newSVsv (ST (i))); 1463 av_push (coro->args, newSVsv (ST (i)));
1446} 1464}
1447 OUTPUT: 1465 OUTPUT:
1448 RETVAL 1466 RETVAL
1449 1467
1450int 1468# these not obviously related functions are all rolled into the same xs
1451save (SV *coro, int new_save = -1) 1469# function to increase chances that they all will call transfer with the same
1452 CODE: 1470# stack offset
1453 RETVAL = api_save (coro, new_save);
1454 OUTPUT:
1455 RETVAL
1456
1457int
1458save_also (SV *coro_sv, int save_also)
1459 CODE:
1460{
1461 struct coro *coro = SvSTATE (coro_sv);
1462 RETVAL = coro->save;
1463 coro->save |= save_also;
1464}
1465 OUTPUT:
1466 RETVAL
1467
1468void 1471void
1469_set_stacklevel (...) 1472_set_stacklevel (...)
1470 ALIAS: 1473 ALIAS:
1471 Coro::State::transfer = 1 1474 Coro::State::transfer = 1
1472 Coro::schedule = 2 1475 Coro::schedule = 2
1506 } 1509 }
1507 1510
1508 BARRIER; 1511 BARRIER;
1509 TRANSFER (ta); 1512 TRANSFER (ta);
1510 1513
1511 if (GIMME_V != G_VOID && ta.next != ta.prev) 1514 if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev))
1512 XSRETURN_YES; 1515 XSRETURN_YES;
1513} 1516}
1514 1517
1515bool 1518bool
1516_destroy (SV *coro_sv) 1519_destroy (SV *coro_sv)
1567{ 1570{
1568 if (coro->mainstack) 1571 if (coro->mainstack)
1569 { 1572 {
1570 struct coro temp; 1573 struct coro temp;
1571 Zero (&temp, 1, struct coro); 1574 Zero (&temp, 1, struct coro);
1572 temp.save = CORO_SAVE_ALL;
1573 1575
1574 if (!(coro->flags & CF_RUNNING)) 1576 if (!(coro->flags & CF_RUNNING))
1575 { 1577 {
1576 save_perl (aTHX_ &temp); 1578 save_perl (aTHX_ &temp);
1577 load_perl (aTHX_ coro); 1579 load_perl (aTHX_ coro);
1656 1658
1657 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); 1659 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE);
1658 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); 1660 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE);
1659 av_async_pool = get_av ("Coro::async_pool", TRUE); 1661 av_async_pool = get_av ("Coro::async_pool", TRUE);
1660 1662
1661 coro_current = get_sv ("Coro::current", FALSE); 1663 coro_current = get_sv ("Coro::current", FALSE);
1662 SvREADONLY_on (coro_current); 1664 SvREADONLY_on (coro_current);
1663 1665
1664 coro_stash = gv_stashpv ("Coro", TRUE); 1666 coro_stash = gv_stashpv ("Coro", TRUE);
1665 1667
1666 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1668 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1675 1677
1676 { 1678 {
1677 SV *sv = perl_get_sv("Coro::API", 1); 1679 SV *sv = perl_get_sv("Coro::API", 1);
1678 1680
1679 coroapi.schedule = api_schedule; 1681 coroapi.schedule = api_schedule;
1680 coroapi.save = api_save;
1681 coroapi.cede = api_cede; 1682 coroapi.cede = api_cede;
1682 coroapi.cede_notself = api_cede_notself; 1683 coroapi.cede_notself = api_cede_notself;
1683 coroapi.ready = api_ready; 1684 coroapi.ready = api_ready;
1684 coroapi.is_ready = api_is_ready; 1685 coroapi.is_ready = api_is_ready;
1685 coroapi.nready = &coro_nready; 1686 coroapi.nready = &coro_nready;
1734 CODE: 1735 CODE:
1735 RETVAL = coro_nready; 1736 RETVAL = coro_nready;
1736 OUTPUT: 1737 OUTPUT:
1737 RETVAL 1738 RETVAL
1738 1739
1740void
1741throw (Coro::State self, SV *throw = &PL_sv_undef)
1742 PROTOTYPE: $;$
1743 CODE:
1744 SvREFCNT_dec (self->throw);
1745 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
1746
1739# for async_pool speedup 1747# for async_pool speedup
1740void 1748void
1741_pool_1 (SV *cb) 1749_pool_1 (SV *cb)
1742 CODE: 1750 CODE:
1743{ 1751{
1744 int i, len; 1752 struct coro *coro = SvSTATE (coro_current);
1745 HV *hv = (HV *)SvRV (coro_current); 1753 HV *hv = (HV *)SvRV (coro_current);
1746 AV *defav = GvAV (PL_defgv); 1754 AV *defav = GvAV (PL_defgv);
1747 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); 1755 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1748 AV *invoke_av; 1756 AV *invoke_av;
1757 int i, len;
1749 1758
1750 if (!invoke) 1759 if (!invoke)
1751 croak ("\3terminate\2\n"); 1760 croak ("\3async_pool terminate\2\n");
1761
1762 SvREFCNT_dec (coro->saved_deffh);
1763 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv);
1752 1764
1753 hv_store (hv, "desc", sizeof ("desc") - 1, 1765 hv_store (hv, "desc", sizeof ("desc") - 1,
1754 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 1766 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1755 1767
1756 invoke_av = (AV *)SvRV (invoke); 1768 invoke_av = (AV *)SvRV (invoke);
1774{ 1786{
1775 struct coro *coro = SvSTATE (coro_current); 1787 struct coro *coro = SvSTATE (coro_current);
1776 1788
1777 sv_setsv (cb, &PL_sv_undef); 1789 sv_setsv (cb, &PL_sv_undef);
1778 1790
1791 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1792 coro->saved_deffh = 0;
1793
1779 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 1794 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1780 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 1795 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1781 croak ("\3terminate\2\n"); 1796 croak ("\3async_pool terminate\2\n");
1782 1797
1783 av_clear (GvAV (PL_defgv)); 1798 av_clear (GvAV (PL_defgv));
1784 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, 1799 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1785 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); 1800 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1786 coro->save = CORO_SAVE_DEF; 1801
1787 coro->prio = 0; 1802 coro->prio = 0;
1788 1803
1789 if (coro->cctx && (coro->cctx->flags & CC_TRACE)) 1804 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1790 api_trace (coro_current, 0); 1805 api_trace (coro_current, 0);
1791 1806

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines