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.181 by root, Wed Oct 3 17:37:33 2007 UTC vs.
Revision 1.194 by root, Sat Oct 6 00:08:04 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
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 */
136 149
137static GV *irsgv; /* $/ */ 150static GV *irsgv; /* $/ */
151static GV *stdoutgv; /* *STDOUT */
138 152
139/* async_pool helper stuff */ 153/* async_pool helper stuff */
140static SV *sv_pool_rss; 154static SV *sv_pool_rss;
141static SV *sv_pool_size; 155static SV *sv_pool_size;
142static AV *av_async_pool; 156static AV *av_async_pool;
192 206
193 /* optionally saved, might be zero */ 207 /* optionally saved, might be zero */
194 AV *defav; /* @_ */ 208 AV *defav; /* @_ */
195 SV *defsv; /* $_ */ 209 SV *defsv; /* $_ */
196 SV *errsv; /* $@ */ 210 SV *errsv; /* $@ */
197 GV *deffh; /* default filehandle */ 211 SV *deffh; /* default filehandle */
198 SV *irssv; /* $/ */ 212 SV *irssv; /* $/ */
199 SV *irssv_sv; /* real $/ cache */ 213 SV *irssv_sv; /* real $/ cache */
200 214
201#define VAR(name,type) type name; 215#define VAR(name,type) type name;
202# include "state.h" 216# include "state.h"
208 /* coro process data */ 222 /* coro process data */
209 int prio; 223 int prio;
210 //SV *throw; 224 //SV *throw;
211 225
212 /* async_pool */ 226 /* async_pool */
213 GV *defoutgv; 227 SV *saved_deffh;
214 228
215 /* linked list */ 229 /* linked list */
216 struct coro *next, *prev; 230 struct coro *next, *prev;
217 HV *hv; /* the perl hash associated with this coro, if any */ 231 HV *hv; /* the perl hash associated with this coro, if any */
218}; 232};
316 MAGIC *mg; 330 MAGIC *mg;
317 331
318 if (SvROK (coro)) 332 if (SvROK (coro))
319 coro = SvRV (coro); 333 coro = SvRV (coro);
320 334
321 if (SvTYPE (coro) != SVt_PVHV) 335 if (expect_false (SvTYPE (coro) != SVt_PVHV))
322 croak ("Coro::State object required"); 336 croak ("Coro::State object required");
323 337
324 stash = SvSTASH (coro); 338 stash = SvSTASH (coro);
325 if (stash != coro_stash && stash != coro_state_stash) 339 if (expect_false (stash != coro_stash && stash != coro_state_stash))
326 { 340 {
327 /* very slow, but rare, check */ 341 /* very slow, but rare, check */
328 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) 342 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
329 croak ("Coro::State object required"); 343 croak ("Coro::State object required");
330 } 344 }
340get_padlist (pTHX_ CV *cv) 354get_padlist (pTHX_ CV *cv)
341{ 355{
342 MAGIC *mg = CORO_MAGIC (cv); 356 MAGIC *mg = CORO_MAGIC (cv);
343 AV *av; 357 AV *av;
344 358
345 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) 359 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
346 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 360 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
347 else 361 else
348 { 362 {
349#if CORO_PREFER_PERL_FUNCTIONS 363#if CORO_PREFER_PERL_FUNCTIONS
350 /* this is probably cleaner, but also slower? */ 364 /* this is probably cleaner, but also slower? */
362put_padlist (pTHX_ CV *cv) 376put_padlist (pTHX_ CV *cv)
363{ 377{
364 MAGIC *mg = CORO_MAGIC (cv); 378 MAGIC *mg = CORO_MAGIC (cv);
365 AV *av; 379 AV *av;
366 380
367 if (!mg) 381 if (expect_false (!mg))
368 { 382 {
369 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); 383 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
370 mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 384 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
371 mg->mg_virtual = &vtbl_coro; 385 mg->mg_virtual = &vtbl_coro;
372 mg->mg_obj = (SV *)newAV (); 386 mg->mg_obj = (SV *)newAV ();
373 } 387 }
374 388
375 av = (AV *)mg->mg_obj; 389 av = (AV *)mg->mg_obj;
376 390
377 if (AvFILLp (av) >= AvMAX (av)) 391 if (expect_false (AvFILLp (av) >= AvMAX (av)))
378 av_extend (av, AvMAX (av) + 1); 392 av_extend (av, AvMAX (av) + 1);
379 393
380 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 394 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
381} 395}
382 396
390#undef VAR 404#undef VAR
391 405
392 GvSV (PL_defgv) = c->defsv; 406 GvSV (PL_defgv) = c->defsv;
393 GvAV (PL_defgv) = c->defav; 407 GvAV (PL_defgv) = c->defav;
394 GvSV (PL_errgv) = c->errsv; 408 GvSV (PL_errgv) = c->errsv;
395 PL_defoutgv = c->deffh;
396 PL_rs = c->irssv;
397 GvSV (irsgv) = c->irssv_sv; 409 GvSV (irsgv) = c->irssv_sv;
398 410
399 { 411 {
400 dSP; 412 dSP;
401 CV *cv; 413 CV *cv;
402 414
403 /* now do the ugly restore mess */ 415 /* now do the ugly restore mess */
404 while ((cv = (CV *)POPs)) 416 while (expect_true (cv = (CV *)POPs))
405 { 417 {
406 put_padlist (aTHX_ cv); /* mark this padlist as available */ 418 put_padlist (aTHX_ cv); /* mark this padlist as available */
407 CvDEPTH (cv) = PTR2IV (POPs); 419 CvDEPTH (cv) = PTR2IV (POPs);
408 CvPADLIST (cv) = (AV *)POPs; 420 CvPADLIST (cv) = (AV *)POPs;
409 } 421 }
428 440
429 XPUSHs (Nullsv); 441 XPUSHs (Nullsv);
430 /* this loop was inspired by pp_caller */ 442 /* this loop was inspired by pp_caller */
431 for (;;) 443 for (;;)
432 { 444 {
433 while (cxix >= 0) 445 while (expect_true (cxix >= 0))
434 { 446 {
435 PERL_CONTEXT *cx = &ccstk[cxix--]; 447 PERL_CONTEXT *cx = &ccstk[cxix--];
436 448
437 if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT) 449 if (expect_true (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT))
438 { 450 {
439 CV *cv = cx->blk_sub.cv; 451 CV *cv = cx->blk_sub.cv;
440 452
441 if (CvDEPTH (cv)) 453 if (expect_true (CvDEPTH (cv)))
442 { 454 {
443 EXTEND (SP, 3); 455 EXTEND (SP, 3);
444 PUSHs ((SV *)CvPADLIST (cv)); 456 PUSHs ((SV *)CvPADLIST (cv));
445 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 457 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
446 PUSHs ((SV *)cv); 458 PUSHs ((SV *)cv);
449 get_padlist (aTHX_ cv); 461 get_padlist (aTHX_ cv);
450 } 462 }
451 } 463 }
452 } 464 }
453 465
454 if (top_si->si_type == PERLSI_MAIN) 466 if (expect_true (top_si->si_type == PERLSI_MAIN))
455 break; 467 break;
456 468
457 top_si = top_si->si_prev; 469 top_si = top_si->si_prev;
458 ccstk = top_si->si_cxstack; 470 ccstk = top_si->si_cxstack;
459 cxix = top_si->si_cxix; 471 cxix = top_si->si_cxix;
463 } 475 }
464 476
465 c->defav = GvAV (PL_defgv); 477 c->defav = GvAV (PL_defgv);
466 c->defsv = DEFSV; 478 c->defsv = DEFSV;
467 c->errsv = ERRSV; 479 c->errsv = ERRSV;
468 c->deffh = PL_defoutgv;
469 c->irssv = PL_rs;
470 c->irssv_sv = GvSV (irsgv); 480 c->irssv_sv = GvSV (irsgv);
471 481
472#define VAR(name,type)c->name = PL_ ## name; 482#define VAR(name,type)c->name = PL_ ## name;
473# include "state.h" 483# include "state.h"
474#undef VAR 484#undef VAR
555} 565}
556 566
557static size_t 567static size_t
558coro_rss (pTHX_ struct coro *coro) 568coro_rss (pTHX_ struct coro *coro)
559{ 569{
560 size_t rss = sizeof (coro); 570 size_t rss = sizeof (*coro);
561 571
562 if (coro->mainstack) 572 if (coro->mainstack)
563 { 573 {
564 if (coro->flags & CF_RUNNING) 574 if (coro->flags & CF_RUNNING)
565 { 575 {
567 # include "state.h" 577 # include "state.h"
568 #undef VAR 578 #undef VAR
569 } 579 }
570 580
571 rss += sizeof (coro->curstackinfo); 581 rss += sizeof (coro->curstackinfo);
572 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 *);
573 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 583 rss += (coro->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
574 rss += sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *); 584 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvFILL (coro->curstack)) * sizeof (SV *);
575 rss += coro->tmps_max * sizeof (SV *); 585 rss += coro->tmps_max * sizeof (SV *);
576 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32); 586 rss += (coro->markstack_max - coro->markstack_ptr) * sizeof (I32);
577 rss += coro->scopestack_max * sizeof (I32); 587 rss += coro->scopestack_max * sizeof (I32);
578 rss += coro->savestack_max * sizeof (ANY); 588 rss += coro->savestack_max * sizeof (ANY);
579 589
602 PL_curpm = 0; 612 PL_curpm = 0;
603 PL_localizing = 0; 613 PL_localizing = 0;
604 PL_dirty = 0; 614 PL_dirty = 0;
605 PL_restartop = 0; 615 PL_restartop = 0;
606 616
607 GvSV (PL_defgv) = NEWSV (0, 0); 617 GvSV (PL_defgv) = newSV (0);
608 GvAV (PL_defgv) = coro->args; coro->args = 0; 618 GvAV (PL_defgv) = coro->args; coro->args = 0;
609 GvSV (PL_errgv) = NEWSV (0, 0); 619 GvSV (PL_errgv) = newSV (0);
610 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 620 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
611 PL_rs = newSVsv (GvSV (irsgv)); 621 PL_rs = newSVsv (GvSV (irsgv));
622 PL_defoutgv = SvREFCNT_inc (stdoutgv);
612 623
613 { 624 ENTER; /* necessary e.g. for dounwind */
614 IO *io = newIO ();
615 PL_defoutgv = newGVgen ("Coro");
616 GvIOp(PL_defoutgv) = io;
617 IoTYPE (io) = IoTYPE_WRONLY;
618 IoOFP (io) = IoIFP (io) = PerlIO_stdout ();
619 IoFLAGS (io) |= IOf_FLUSH;
620 }
621 625
622 { 626 {
623 dSP; 627 dSP;
624 LOGOP myop; 628 LOGOP myop;
625 629
626 Zero (&myop, 1, LOGOP); 630 Zero (&myop, 1, LOGOP);
627 myop.op_next = Nullop; 631 myop.op_next = Nullop;
628 myop.op_flags = OPf_WANT_VOID; 632 myop.op_flags = OPf_WANT_VOID;
629 633
630 PUSHMARK (SP); 634 PUSHMARK (SP);
631 XPUSHs (av_shift (GvAV (PL_defgv))); 635 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
632 PUTBACK; 636 PUTBACK;
633 PL_op = (OP *)&myop; 637 PL_op = (OP *)&myop;
634 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 638 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
635 SPAGAIN; 639 SPAGAIN;
636 } 640 }
637
638 ENTER; /* necessary e.g. for dounwind */
639} 641}
640 642
641static void 643static void
642coro_destroy (pTHX_ struct coro *coro) 644coro_destroy (pTHX_ struct coro *coro)
643{ 645{
663 SvREFCNT_dec (GvSV (PL_errgv)); 665 SvREFCNT_dec (GvSV (PL_errgv));
664 SvREFCNT_dec (PL_defoutgv); 666 SvREFCNT_dec (PL_defoutgv);
665 SvREFCNT_dec (PL_rs); 667 SvREFCNT_dec (PL_rs);
666 SvREFCNT_dec (GvSV (irsgv)); 668 SvREFCNT_dec (GvSV (irsgv));
667 669
668 SvREFCNT_dec (coro->defoutgv); 670 SvREFCNT_dec (coro->saved_deffh);
669 //SvREFCNT_dec (coro->throw); 671 //SvREFCNT_dec (coro->throw);
670 672
671 coro_destroy_stacks (aTHX); 673 coro_destroy_stacks (aTHX);
672} 674}
673 675
674static void 676static void
675free_coro_mortal (pTHX) 677free_coro_mortal (pTHX)
676{ 678{
677 if (coro_mortal) 679 if (expect_true (coro_mortal))
678 { 680 {
679 SvREFCNT_dec (coro_mortal); 681 SvREFCNT_dec (coro_mortal);
680 coro_mortal = 0; 682 coro_mortal = 0;
681 } 683 }
682} 684}
711 bot = PL_stack_base + cx->blk_oldsp + 1; 713 bot = PL_stack_base + cx->blk_oldsp + 1;
712 top = cx->blk_gimme == G_ARRAY ? SP + 1 714 top = cx->blk_gimme == G_ARRAY ? SP + 1
713 : cx->blk_gimme == G_SCALAR ? bot + 1 715 : cx->blk_gimme == G_SCALAR ? bot + 1
714 : bot; 716 : bot;
715 717
718 av_extend (av, top - bot);
716 while (bot < top) 719 while (bot < top)
717 av_push (av, SvREFCNT_inc (*bot++)); 720 av_push (av, SvREFCNT_inc (*bot++));
718 721
719 PL_runops = RUNOPS_DEFAULT; 722 PL_runops = RUNOPS_DEFAULT;
720 ENTER; 723 ENTER;
932 Safefree (cctx->sptr); 935 Safefree (cctx->sptr);
933 936
934 Safefree (cctx); 937 Safefree (cctx);
935} 938}
936 939
940/* wether this cctx should be destructed */
941#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE))
942
937static coro_cctx * 943static coro_cctx *
938cctx_get (pTHX) 944cctx_get (pTHX)
939{ 945{
940 while (cctx_first) 946 while (expect_true (cctx_first))
941 { 947 {
942 coro_cctx *cctx = cctx_first; 948 coro_cctx *cctx = cctx_first;
943 cctx_first = cctx->next; 949 cctx_first = cctx->next;
944 --cctx_idle; 950 --cctx_idle;
945 951
946 if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE)) 952 if (expect_true (!CCTX_EXPIRED (cctx)))
947 return cctx; 953 return cctx;
948 954
949 cctx_destroy (cctx); 955 cctx_destroy (cctx);
950 } 956 }
951 957
954 960
955static void 961static void
956cctx_put (coro_cctx *cctx) 962cctx_put (coro_cctx *cctx)
957{ 963{
958 /* free another cctx if overlimit */ 964 /* free another cctx if overlimit */
959 if (cctx_idle >= MAX_IDLE_CCTX) 965 if (expect_false (cctx_idle >= MAX_IDLE_CCTX))
960 { 966 {
961 coro_cctx *first = cctx_first; 967 coro_cctx *first = cctx_first;
962 cctx_first = first->next; 968 cctx_first = first->next;
963 --cctx_idle; 969 --cctx_idle;
964 970
970 cctx_first = cctx; 976 cctx_first = cctx;
971} 977}
972 978
973/** coroutine switching *****************************************************/ 979/** coroutine switching *****************************************************/
974 980
975static void NOINLINE 981static void
976transfer_check (pTHX_ struct coro *prev, struct coro *next) 982transfer_check (pTHX_ struct coro *prev, struct coro *next)
977{ 983{
978 if (prev != next) 984 if (expect_true (prev != next))
979 { 985 {
980 if (!(prev->flags & (CF_RUNNING | CF_NEW))) 986 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
981 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states"); 987 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states");
982 988
983 if (next->flags & CF_RUNNING) 989 if (expect_false (next->flags & CF_RUNNING))
984 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 990 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
985 991
986 if (next->flags & CF_DESTROYED) 992 if (expect_false (next->flags & CF_DESTROYED))
987 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); 993 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
988 994
989 if (PL_lex_state != LEX_NOTPARSING) 995 if (expect_false (PL_lex_state != LEX_NOTPARSING))
990 croak ("Coro::State::transfer called while parsing, but this is not supported"); 996 croak ("Coro::State::transfer called while parsing, but this is not supported");
991 } 997 }
992} 998}
993 999
994/* always use the TRANSFER macro */ 1000/* always use the TRANSFER macro */
996transfer (pTHX_ struct coro *prev, struct coro *next) 1002transfer (pTHX_ struct coro *prev, struct coro *next)
997{ 1003{
998 dSTACKLEVEL; 1004 dSTACKLEVEL;
999 1005
1000 /* sometimes transfer is only called to set idle_sp */ 1006 /* sometimes transfer is only called to set idle_sp */
1001 if (!next) 1007 if (expect_false (!next))
1002 { 1008 {
1003 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1009 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1004 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1010 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1005 } 1011 }
1006 else if (prev != next) 1012 else if (expect_true (prev != next))
1007 { 1013 {
1008 coro_cctx *prev__cctx; 1014 coro_cctx *prev__cctx;
1009 1015
1010 if (prev->flags & CF_NEW) 1016 if (expect_false (prev->flags & CF_NEW))
1011 { 1017 {
1012 /* create a new empty context */ 1018 /* create a new empty context */
1013 Newz (0, prev->cctx, 1, coro_cctx); 1019 Newz (0, prev->cctx, 1, coro_cctx);
1014 prev->flags &= ~CF_NEW; 1020 prev->flags &= ~CF_NEW;
1015 prev->flags |= CF_RUNNING; 1021 prev->flags |= CF_RUNNING;
1018 prev->flags &= ~CF_RUNNING; 1024 prev->flags &= ~CF_RUNNING;
1019 next->flags |= CF_RUNNING; 1025 next->flags |= CF_RUNNING;
1020 1026
1021 LOCK; 1027 LOCK;
1022 1028
1023 if (next->flags & CF_NEW) 1029 if (expect_false (next->flags & CF_NEW))
1024 { 1030 {
1025 /* need to start coroutine */ 1031 /* need to start coroutine */
1026 next->flags &= ~CF_NEW; 1032 next->flags &= ~CF_NEW;
1027 /* first get rid of the old state */ 1033 /* first get rid of the old state */
1028 save_perl (aTHX_ prev); 1034 save_perl (aTHX_ prev);
1037 } 1043 }
1038 1044
1039 prev__cctx = prev->cctx; 1045 prev__cctx = prev->cctx;
1040 1046
1041 /* possibly "free" the cctx */ 1047 /* possibly "free" the cctx */
1042 if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE)) 1048 if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE)))
1043 { 1049 {
1044 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1050 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
1045 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 1051 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
1046 1052
1047 prev->cctx = 0; 1053 prev->cctx = 0;
1048 1054
1055 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1056 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1057 if (expect_false (CCTX_EXPIRED (prev__cctx)))
1058 next->cctx = cctx_get (aTHX);
1059
1049 cctx_put (prev__cctx); 1060 cctx_put (prev__cctx);
1050 } 1061 }
1051 1062
1052 ++next->usecount; 1063 ++next->usecount;
1053 1064
1054 if (!next->cctx) 1065 if (expect_true (!next->cctx))
1055 next->cctx = cctx_get (aTHX); 1066 next->cctx = cctx_get (aTHX);
1056 1067
1057 if (prev__cctx != next->cctx) 1068 if (expect_false (prev__cctx != next->cctx))
1058 { 1069 {
1059 prev__cctx->top_env = PL_top_env; 1070 prev__cctx->top_env = PL_top_env;
1060 PL_top_env = next->cctx->top_env; 1071 PL_top_env = next->cctx->top_env;
1061 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1072 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
1062 } 1073 }
1242 { 1253 {
1243 LOCK; 1254 LOCK;
1244 next_sv = coro_deq (aTHX_ PRIO_MIN); 1255 next_sv = coro_deq (aTHX_ PRIO_MIN);
1245 1256
1246 /* nothing to schedule: call the idle handler */ 1257 /* nothing to schedule: call the idle handler */
1247 if (!next_sv) 1258 if (expect_false (!next_sv))
1248 { 1259 {
1249 dSP; 1260 dSP;
1250 UNLOCK; 1261 UNLOCK;
1251 1262
1252 ENTER; 1263 ENTER;
1262 } 1273 }
1263 1274
1264 ta->next = SvSTATE (next_sv); 1275 ta->next = SvSTATE (next_sv);
1265 1276
1266 /* cannot transfer to destroyed coros, skip and look for next */ 1277 /* cannot transfer to destroyed coros, skip and look for next */
1267 if (ta->next->flags & CF_DESTROYED) 1278 if (expect_false (ta->next->flags & CF_DESTROYED))
1268 { 1279 {
1269 UNLOCK; 1280 UNLOCK;
1270 SvREFCNT_dec (next_sv); 1281 SvREFCNT_dec (next_sv);
1271 /* coro_nready is already taken care of by destroy */ 1282 /* coro_nready is already taken care of by destroy */
1272 continue; 1283 continue;
1328 dTHX; 1339 dTHX;
1329 struct transfer_args ta; 1340 struct transfer_args ta;
1330 1341
1331 prepare_cede (aTHX_ &ta); 1342 prepare_cede (aTHX_ &ta);
1332 1343
1333 if (ta.prev != ta.next) 1344 if (expect_true (ta.prev != ta.next))
1334 { 1345 {
1335 TRANSFER (ta); 1346 TRANSFER (ta);
1336 return 1; 1347 return 1;
1337 } 1348 }
1338 else 1349 else
1389#ifdef USE_ITHREADS 1400#ifdef USE_ITHREADS
1390 MUTEX_INIT (&coro_mutex); 1401 MUTEX_INIT (&coro_mutex);
1391#endif 1402#endif
1392 BOOT_PAGESIZE; 1403 BOOT_PAGESIZE;
1393 1404
1394 irsgv = gv_fetchpv ("/", 1, SVt_PV); 1405 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1406 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1395 1407
1396 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1408 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1397 1409
1398 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); 1410 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1399 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); 1411 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1430 1442
1431 coro->hv = hv = newHV (); 1443 coro->hv = hv = newHV ();
1432 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1444 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1433 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1445 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1434 1446
1447 av_extend (coro->args, items - 1);
1435 for (i = 1; i < items; i++) 1448 for (i = 1; i < items; i++)
1436 av_push (coro->args, newSVsv (ST (i))); 1449 av_push (coro->args, newSVsv (ST (i)));
1437} 1450}
1438 OUTPUT: 1451 OUTPUT:
1439 RETVAL 1452 RETVAL
1482 } 1495 }
1483 1496
1484 BARRIER; 1497 BARRIER;
1485 TRANSFER (ta); 1498 TRANSFER (ta);
1486 1499
1487 if (GIMME_V != G_VOID && ta.next != ta.prev) 1500 if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev))
1488 XSRETURN_YES; 1501 XSRETURN_YES;
1489} 1502}
1490 1503
1491bool 1504bool
1492_destroy (SV *coro_sv) 1505_destroy (SV *coro_sv)
1631 1644
1632 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); 1645 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE);
1633 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); 1646 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE);
1634 av_async_pool = get_av ("Coro::async_pool", TRUE); 1647 av_async_pool = get_av ("Coro::async_pool", TRUE);
1635 1648
1636 coro_current = get_sv ("Coro::current", FALSE); 1649 coro_current = get_sv ("Coro::current", FALSE);
1637 SvREADONLY_on (coro_current); 1650 SvREADONLY_on (coro_current);
1638 1651
1639 coro_stash = gv_stashpv ("Coro", TRUE); 1652 coro_stash = gv_stashpv ("Coro", TRUE);
1640 1653
1641 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1654 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1720 AV *defav = GvAV (PL_defgv); 1733 AV *defav = GvAV (PL_defgv);
1721 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); 1734 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1722 AV *invoke_av; 1735 AV *invoke_av;
1723 int i, len; 1736 int i, len;
1724 1737
1725 coro->defoutgv = SvREFCNT_inc (PL_defoutgv);
1726
1727 if (!invoke) 1738 if (!invoke)
1728 croak ("\3terminate\2\n"); 1739 croak ("\3terminate\2\n");
1740
1741 SvREFCNT_dec (coro->saved_deffh);
1742 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv);
1729 1743
1730 hv_store (hv, "desc", sizeof ("desc") - 1, 1744 hv_store (hv, "desc", sizeof ("desc") - 1,
1731 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 1745 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1732 1746
1733 invoke_av = (AV *)SvRV (invoke); 1747 invoke_av = (AV *)SvRV (invoke);
1740 av_fill (defav, len - 1); 1754 av_fill (defav, len - 1);
1741 for (i = 0; i < len; ++i) 1755 for (i = 0; i < len; ++i)
1742 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 1756 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1]));
1743 } 1757 }
1744 1758
1745
1746
1747 SvREFCNT_dec (invoke); 1759 SvREFCNT_dec (invoke);
1748} 1760}
1749 1761
1750void 1762void
1751_pool_2 (SV *cb) 1763_pool_2 (SV *cb)
1753{ 1765{
1754 struct coro *coro = SvSTATE (coro_current); 1766 struct coro *coro = SvSTATE (coro_current);
1755 1767
1756 sv_setsv (cb, &PL_sv_undef); 1768 sv_setsv (cb, &PL_sv_undef);
1757 1769
1758 SvREFCNT_dec (PL_defoutgv); PL_defoutgv = coro->defoutgv; 1770 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1771 coro->saved_deffh = 0;
1759 1772
1760 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 1773 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1761 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 1774 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1762 croak ("\3terminate\2\n"); 1775 croak ("\3terminate\2\n");
1763 1776

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines