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.168 by root, Thu Sep 27 16:10:48 2007 UTC vs.
Revision 1.196 by root, Sat Oct 6 01:11:01 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
219 /* statistics */
220 int usecount; /* number of transfers to this coro */
221
204 /* coro process data */ 222 /* coro process data */
205 int prio; 223 int prio;
224 SV *throw;
225
226 /* async_pool */
227 SV *saved_deffh;
206 228
207 /* linked list */ 229 /* linked list */
208 struct coro *next, *prev; 230 struct coro *next, *prev;
209 HV *hv; /* the perl hash associated with this coro, if any */ 231 HV *hv; /* the perl hash associated with this coro, if any */
210}; 232};
299 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ 321 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
300 ? SvMAGIC (cv) \ 322 ? SvMAGIC (cv) \
301 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 323 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
302 : 0 324 : 0
303 325
326static struct coro *
327SvSTATE_ (pTHX_ SV *coro)
328{
329 HV *stash;
330 MAGIC *mg;
331
332 if (SvROK (coro))
333 coro = SvRV (coro);
334
335 if (expect_false (SvTYPE (coro) != SVt_PVHV))
336 croak ("Coro::State object required");
337
338 stash = SvSTASH (coro);
339 if (expect_false (stash != coro_stash && stash != coro_state_stash))
340 {
341 /* very slow, but rare, check */
342 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
343 croak ("Coro::State object required");
344 }
345
346 mg = CORO_MAGIC (coro);
347 return (struct coro *)mg->mg_ptr;
348}
349
350#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
351
304/* the next two functions merely cache the padlists */ 352/* the next two functions merely cache the padlists */
305static void 353static void
306get_padlist (pTHX_ CV *cv) 354get_padlist (pTHX_ CV *cv)
307{ 355{
308 MAGIC *mg = CORO_MAGIC (cv); 356 MAGIC *mg = CORO_MAGIC (cv);
309 AV *av; 357 AV *av;
310 358
311 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) 359 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
312 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 360 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
313 else 361 else
314 { 362 {
315#if CORO_PREFER_PERL_FUNCTIONS 363#if CORO_PREFER_PERL_FUNCTIONS
316 /* this is probably cleaner, but also slower? */ 364 /* this is probably cleaner, but also slower? */
328put_padlist (pTHX_ CV *cv) 376put_padlist (pTHX_ CV *cv)
329{ 377{
330 MAGIC *mg = CORO_MAGIC (cv); 378 MAGIC *mg = CORO_MAGIC (cv);
331 AV *av; 379 AV *av;
332 380
333 if (!mg) 381 if (expect_false (!mg))
334 { 382 {
335 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); 383 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
336 mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 384 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
337 mg->mg_virtual = &vtbl_coro; 385 mg->mg_virtual = &vtbl_coro;
338 mg->mg_obj = (SV *)newAV (); 386 mg->mg_obj = (SV *)newAV ();
339 } 387 }
340 388
341 av = (AV *)mg->mg_obj; 389 av = (AV *)mg->mg_obj;
342 390
343 if (AvFILLp (av) >= AvMAX (av)) 391 if (expect_false (AvFILLp (av) >= AvMAX (av)))
344 av_extend (av, AvMAX (av) + 1); 392 av_extend (av, AvMAX (av) + 1);
345 393
346 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 394 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
347} 395}
348 396
349/** load & save, init *******************************************************/ 397/** load & save, init *******************************************************/
350
351#define SB do {
352#define SE } while (0)
353
354#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
355 398
356static void 399static void
357load_perl (pTHX_ Coro__State c) 400load_perl (pTHX_ Coro__State c)
358{ 401{
359#define VAR(name,type) PL_ ## name = c->name; 402#define VAR(name,type) PL_ ## name = c->name;
360# include "state.h" 403# include "state.h"
361#undef VAR 404#undef VAR
362 405
363 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 406 GvSV (PL_defgv) = c->defsv;
364 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 407 GvAV (PL_defgv) = c->defav;
365 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 408 GvSV (PL_errgv) = c->errsv;
366 if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh); 409 GvSV (irsgv) = c->irssv_sv;
367
368 if (c->irssv)
369 {
370 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
371 {
372 SvREFCNT_dec (c->irssv);
373 c->irssv = 0;
374 }
375 else
376 {
377 REPLACE_SV (PL_rs, c->irssv);
378 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
379 sv_setsv (c->irssv_sv, PL_rs);
380 }
381 }
382 410
383 { 411 {
384 dSP; 412 dSP;
385 CV *cv; 413 CV *cv;
386 414
387 /* now do the ugly restore mess */ 415 /* now do the ugly restore mess */
388 while ((cv = (CV *)POPs)) 416 while (expect_true (cv = (CV *)POPs))
389 { 417 {
390 put_padlist (aTHX_ cv); /* mark this padlist as available */ 418 put_padlist (aTHX_ cv); /* mark this padlist as available */
391 CvDEPTH (cv) = PTR2IV (POPs); 419 CvDEPTH (cv) = PTR2IV (POPs);
392 CvPADLIST (cv) = (AV *)POPs; 420 CvPADLIST (cv) = (AV *)POPs;
393 } 421 }
412 440
413 XPUSHs (Nullsv); 441 XPUSHs (Nullsv);
414 /* this loop was inspired by pp_caller */ 442 /* this loop was inspired by pp_caller */
415 for (;;) 443 for (;;)
416 { 444 {
417 while (cxix >= 0) 445 while (expect_true (cxix >= 0))
418 { 446 {
419 PERL_CONTEXT *cx = &ccstk[cxix--]; 447 PERL_CONTEXT *cx = &ccstk[cxix--];
420 448
421 if (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT) 449 if (expect_true (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT))
422 { 450 {
423 CV *cv = cx->blk_sub.cv; 451 CV *cv = cx->blk_sub.cv;
424 452
425 if (CvDEPTH (cv)) 453 if (expect_true (CvDEPTH (cv)))
426 { 454 {
427 EXTEND (SP, 3); 455 EXTEND (SP, 3);
428 PUSHs ((SV *)CvPADLIST (cv)); 456 PUSHs ((SV *)CvPADLIST (cv));
429 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 457 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
430 PUSHs ((SV *)cv); 458 PUSHs ((SV *)cv);
433 get_padlist (aTHX_ cv); 461 get_padlist (aTHX_ cv);
434 } 462 }
435 } 463 }
436 } 464 }
437 465
438 if (top_si->si_type == PERLSI_MAIN) 466 if (expect_true (top_si->si_type == PERLSI_MAIN))
439 break; 467 break;
440 468
441 top_si = top_si->si_prev; 469 top_si = top_si->si_prev;
442 ccstk = top_si->si_cxstack; 470 ccstk = top_si->si_cxstack;
443 cxix = top_si->si_cxix; 471 cxix = top_si->si_cxix;
444 } 472 }
445 473
446 PUTBACK; 474 PUTBACK;
447 } 475 }
448 476
449 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 477 c->defav = GvAV (PL_defgv);
450 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 478 c->defsv = DEFSV;
451 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 479 c->errsv = ERRSV;
452 c->deffh = c->save & CORO_SAVE_DEFFH ? (GV *)SvREFCNT_inc (PL_defoutgv) : 0; 480 c->irssv_sv = GvSV (irsgv);
453 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0;
454 481
455#define VAR(name,type)c->name = PL_ ## name; 482#define VAR(name,type)c->name = PL_ ## name;
456# include "state.h" 483# include "state.h"
457#undef VAR 484#undef VAR
458} 485}
511 * destroy the stacks, the callchain etc... 538 * destroy the stacks, the callchain etc...
512 */ 539 */
513static void 540static void
514coro_destroy_stacks (pTHX) 541coro_destroy_stacks (pTHX)
515{ 542{
516 if (!IN_DESTRUCT)
517 {
518 /* restore all saved variables and stuff */
519 LEAVE_SCOPE (0);
520 assert (PL_tmps_floor == -1);
521
522 /* free all temporaries */
523 FREETMPS;
524 assert (PL_tmps_ix == -1);
525
526 /* unwind all extra stacks */
527 POPSTACK_TO (PL_mainstack);
528
529 /* unwind main stack */
530 dounwind (-1);
531 }
532
533 while (PL_curstackinfo->si_next) 543 while (PL_curstackinfo->si_next)
534 PL_curstackinfo = PL_curstackinfo->si_next; 544 PL_curstackinfo = PL_curstackinfo->si_next;
535 545
536 while (PL_curstackinfo) 546 while (PL_curstackinfo)
537 { 547 {
553 Safefree (PL_retstack); 563 Safefree (PL_retstack);
554#endif 564#endif
555} 565}
556 566
557static size_t 567static size_t
558coro_rss (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
586} 596}
587 597
588/** coroutine stack handling ************************************************/ 598/** coroutine stack handling ************************************************/
589 599
590static void 600static void
591setup_coro (pTHX_ struct coro *coro) 601coro_setup (pTHX_ struct coro *coro)
592{ 602{
593 /* 603 /*
594 * emulate part of the perl startup here. 604 * emulate part of the perl startup here.
595 */ 605 */
596 coro_init_stacks (aTHX); 606 coro_init_stacks (aTHX);
597 607
608 PL_runops = RUNOPS_DEFAULT;
598 PL_curcop = &PL_compiling; 609 PL_curcop = &PL_compiling;
599 PL_in_eval = EVAL_NULL; 610 PL_in_eval = EVAL_NULL;
600 PL_comppad = 0; 611 PL_comppad = 0;
601 PL_curpm = 0; 612 PL_curpm = 0;
613 PL_curpad = 0;
602 PL_localizing = 0; 614 PL_localizing = 0;
603 PL_dirty = 0; 615 PL_dirty = 0;
604 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);
605 624
606 { 625 {
607 dSP; 626 dSP;
608 LOGOP myop; 627 LOGOP myop;
609 628
610 SvREFCNT_dec (GvAV (PL_defgv));
611 GvAV (PL_defgv) = coro->args; coro->args = 0;
612
613 Zero (&myop, 1, LOGOP); 629 Zero (&myop, 1, LOGOP);
614 myop.op_next = Nullop; 630 myop.op_next = Nullop;
615 myop.op_flags = OPf_WANT_VOID; 631 myop.op_flags = OPf_WANT_VOID;
616 632
617 PUSHMARK (SP); 633 PUSHMARK (SP);
618 XPUSHs (av_shift (GvAV (PL_defgv))); 634 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
619 PUTBACK; 635 PUTBACK;
620 PL_op = (OP *)&myop; 636 PL_op = (OP *)&myop;
621 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 637 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
622 SPAGAIN; 638 SPAGAIN;
623 } 639 }
624 640
625 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);
626} 675}
627 676
628static void 677static void
629free_coro_mortal (pTHX) 678free_coro_mortal (pTHX)
630{ 679{
631 if (coro_mortal) 680 if (expect_true (coro_mortal))
632 { 681 {
633 SvREFCNT_dec (coro_mortal); 682 SvREFCNT_dec (coro_mortal);
634 coro_mortal = 0; 683 coro_mortal = 0;
635 } 684 }
636} 685}
637 686
638static int 687static int
639runops_coro (pTHX) 688runops_trace (pTHX)
640{ 689{
641 COP *oldcop = 0; 690 COP *oldcop = 0;
642 int oldcxix = -2; 691 int oldcxix = -2;
692 struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */
693 coro_cctx *cctx = coro->cctx;
643 694
644 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 695 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
645 { 696 {
646 PERL_ASYNC_CHECK (); 697 PERL_ASYNC_CHECK ();
647 698
648 if (PL_op->op_type == OP_LEAVESUB) 699 if (cctx->flags & CC_TRACE_ALL)
649 { 700 {
650 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 701 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB)
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 { 702 {
692 runops_proc_t old_runops = PL_runops; 703 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
704 SV **bot, **top;
705 AV *av = newAV (); /* return values */
706 SV **cb;
693 dSP; 707 dSP;
708
709 GV *gv = CvGV (cx->blk_sub.cv);
710 SV *fullname = sv_2mortal (newSV (0));
711 if (isGV (gv))
712 gv_efullname3 (fullname, gv, 0);
713
714 bot = PL_stack_base + cx->blk_oldsp + 1;
715 top = cx->blk_gimme == G_ARRAY ? SP + 1
716 : cx->blk_gimme == G_SCALAR ? bot + 1
717 : bot;
718
719 av_extend (av, top - bot);
720 while (bot < top)
721 av_push (av, SvREFCNT_inc (*bot++));
722
723 PL_runops = RUNOPS_DEFAULT;
694 ENTER; 724 ENTER;
695 SAVETMPS; 725 SAVETMPS;
696 EXTEND (SP, 3); 726 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); 727 PUSHMARK (SP);
724 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); 728 PUSHs (&PL_sv_no);
725 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); 729 PUSHs (fullname);
730 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
726 PUTBACK; 731 PUTBACK;
732 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
727 call_pv ("Coro::_do_trace_line", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 733 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
728 SPAGAIN; 734 SPAGAIN;
729 }
730
731 FREETMPS; 735 FREETMPS;
732 LEAVE; 736 LEAVE;
733 PL_runops = old_runops; 737 PL_runops = runops_trace;
738 }
739
740 if (oldcop != PL_curcop)
741 {
742 oldcop = PL_curcop;
743
744 if (PL_curcop != &PL_compiling)
745 {
746 SV **cb;
747
748 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB)
749 {
750 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
751
752 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
753 {
754 runops_proc_t old_runops = PL_runops;
755 dSP;
756 GV *gv = CvGV (cx->blk_sub.cv);
757 SV *fullname = sv_2mortal (newSV (0));
758
759 if (isGV (gv))
760 gv_efullname3 (fullname, gv, 0);
761
762 PL_runops = RUNOPS_DEFAULT;
763 ENTER;
764 SAVETMPS;
765 EXTEND (SP, 3);
766 PUSHMARK (SP);
767 PUSHs (&PL_sv_yes);
768 PUSHs (fullname);
769 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
770 PUTBACK;
771 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
772 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
773 SPAGAIN;
774 FREETMPS;
775 LEAVE;
776 PL_runops = runops_trace;
777 }
778
779 oldcxix = cxstack_ix;
780 }
781
782 if (cctx->flags & CC_TRACE_LINE)
783 {
784 dSP;
785
786 PL_runops = RUNOPS_DEFAULT;
787 ENTER;
788 SAVETMPS;
789 EXTEND (SP, 3);
790 PL_runops = RUNOPS_DEFAULT;
791 PUSHMARK (SP);
792 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
793 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop))));
794 PUTBACK;
795 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0);
796 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
797 SPAGAIN;
798 FREETMPS;
799 LEAVE;
800 PL_runops = runops_trace;
801 }
802 }
734 } 803 }
735 } 804 }
736 } 805 }
737 806
738 TAINT_NOT; 807 TAINT_NOT;
749 LOGOP myop; 818 LOGOP myop;
750 819
751 PL_top_env = &PL_start_env; 820 PL_top_env = &PL_start_env;
752 821
753 if (cctx->flags & CC_TRACE) 822 if (cctx->flags & CC_TRACE)
754 PL_runops = runops_coro; 823 PL_runops = runops_trace;
755 824
756 Zero (&myop, 1, LOGOP); 825 Zero (&myop, 1, LOGOP);
757 myop.op_next = PL_op; 826 myop.op_next = PL_op;
758 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 827 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
759 828
867 Safefree (cctx->sptr); 936 Safefree (cctx->sptr);
868 937
869 Safefree (cctx); 938 Safefree (cctx);
870} 939}
871 940
941/* wether this cctx should be destructed */
942#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE))
943
872static coro_cctx * 944static coro_cctx *
873cctx_get (pTHX) 945cctx_get (pTHX)
874{ 946{
875 while (cctx_first) 947 while (expect_true (cctx_first))
876 { 948 {
877 coro_cctx *cctx = cctx_first; 949 coro_cctx *cctx = cctx_first;
878 cctx_first = cctx->next; 950 cctx_first = cctx->next;
879 --cctx_idle; 951 --cctx_idle;
880 952
881 if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE)) 953 if (expect_true (!CCTX_EXPIRED (cctx)))
882 return cctx; 954 return cctx;
883 955
884 cctx_destroy (cctx); 956 cctx_destroy (cctx);
885 } 957 }
886 958
889 961
890static void 962static void
891cctx_put (coro_cctx *cctx) 963cctx_put (coro_cctx *cctx)
892{ 964{
893 /* free another cctx if overlimit */ 965 /* free another cctx if overlimit */
894 if (cctx_idle >= MAX_IDLE_CCTX) 966 if (expect_false (cctx_idle >= MAX_IDLE_CCTX))
895 { 967 {
896 coro_cctx *first = cctx_first; 968 coro_cctx *first = cctx_first;
897 cctx_first = first->next; 969 cctx_first = first->next;
898 --cctx_idle; 970 --cctx_idle;
899 971
905 cctx_first = cctx; 977 cctx_first = cctx;
906} 978}
907 979
908/** coroutine switching *****************************************************/ 980/** coroutine switching *****************************************************/
909 981
910/* never call directly, always through the coro_state_transfer global variable */ 982static void
983transfer_check (pTHX_ struct coro *prev, struct coro *next)
984{
985 if (expect_true (prev != next))
986 {
987 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
988 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states");
989
990 if (expect_false (next->flags & CF_RUNNING))
991 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
992
993 if (expect_false (next->flags & CF_DESTROYED))
994 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
995
996 if (expect_false (PL_lex_state != LEX_NOTPARSING))
997 croak ("Coro::State::transfer called while parsing, but this is not supported");
998 }
999}
1000
1001/* always use the TRANSFER macro */
911static void NOINLINE 1002static void NOINLINE
912transfer (pTHX_ struct coro *prev, struct coro *next) 1003transfer (pTHX_ struct coro *prev, struct coro *next)
913{ 1004{
914 dSTACKLEVEL; 1005 dSTACKLEVEL;
915 1006
916 /* sometimes transfer is only called to set idle_sp */ 1007 /* sometimes transfer is only called to set idle_sp */
917 if (!next) 1008 if (expect_false (!next))
918 { 1009 {
919 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1010 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
920 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 */
921 } 1012 }
922 else if (prev != next) 1013 else if (expect_true (prev != next))
923 { 1014 {
924 coro_cctx *prev__cctx; 1015 coro_cctx *prev__cctx;
925 1016
926 if (prev->flags & CF_NEW) 1017 if (expect_false (prev->flags & CF_NEW))
927 { 1018 {
928 /* create a new empty context */ 1019 /* create a new empty context */
929 Newz (0, prev->cctx, 1, coro_cctx); 1020 Newz (0, prev->cctx, 1, coro_cctx);
930 prev->flags &= ~CF_NEW; 1021 prev->flags &= ~CF_NEW;
931 prev->flags |= CF_RUNNING; 1022 prev->flags |= CF_RUNNING;
932 } 1023 }
933 1024
934 /*TODO: must not croak here */
935 if (!prev->flags & CF_RUNNING)
936 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
937
938 if (next->flags & CF_RUNNING)
939 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
940
941 if (next->flags & CF_DESTROYED)
942 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
943
944 prev->flags &= ~CF_RUNNING; 1025 prev->flags &= ~CF_RUNNING;
945 next->flags |= CF_RUNNING; 1026 next->flags |= CF_RUNNING;
946 1027
947 LOCK; 1028 LOCK;
948 1029
949 if (next->flags & CF_NEW) 1030 if (expect_false (next->flags & CF_NEW))
950 { 1031 {
951 /* need to start coroutine */ 1032 /* need to start coroutine */
952 next->flags &= ~CF_NEW; 1033 next->flags &= ~CF_NEW;
953 /* first get rid of the old state */ 1034 /* first get rid of the old state */
954 save_perl (aTHX_ prev); 1035 save_perl (aTHX_ prev);
955 /* setup coroutine call */ 1036 /* setup coroutine call */
956 setup_coro (aTHX_ next); 1037 coro_setup (aTHX_ next);
957 } 1038 }
958 else 1039 else
959 { 1040 {
960 /* coroutine already started */ 1041 /* coroutine already started */
961 save_perl (aTHX_ prev); 1042 save_perl (aTHX_ prev);
963 } 1044 }
964 1045
965 prev__cctx = prev->cctx; 1046 prev__cctx = prev->cctx;
966 1047
967 /* possibly "free" the cctx */ 1048 /* possibly "free" the cctx */
968 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)))
969 { 1050 {
970 /* 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 */
971 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));
972 1053
973 prev->cctx = 0; 1054 prev->cctx = 0;
974 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
975 cctx_put (prev__cctx); 1061 cctx_put (prev__cctx);
976 } 1062 }
977 1063
1064 ++next->usecount;
1065
978 if (!next->cctx) 1066 if (expect_true (!next->cctx))
979 next->cctx = cctx_get (aTHX); 1067 next->cctx = cctx_get (aTHX);
980 1068
981 if (prev__cctx != next->cctx) 1069 if (expect_false (prev__cctx != next->cctx))
982 { 1070 {
983 prev__cctx->top_env = PL_top_env; 1071 prev__cctx->top_env = PL_top_env;
984 PL_top_env = next->cctx->top_env; 1072 PL_top_env = next->cctx->top_env;
985 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1073 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
986 } 1074 }
987 1075
988 free_coro_mortal (aTHX); 1076 free_coro_mortal (aTHX);
989 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 }
990 } 1091 }
991} 1092}
992 1093
993struct transfer_args 1094struct transfer_args
994{ 1095{
995 struct coro *prev, *next; 1096 struct coro *prev, *next;
996}; 1097};
997 1098
998#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next) 1099#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next)
1100#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
999 1101
1000/** high level stuff ********************************************************/ 1102/** high level stuff ********************************************************/
1001 1103
1002static int 1104static int
1003coro_state_destroy (pTHX_ struct coro *coro) 1105coro_state_destroy (pTHX_ struct coro *coro)
1020 1122
1021 if (coro->mainstack && coro->mainstack != main_mainstack) 1123 if (coro->mainstack && coro->mainstack != main_mainstack)
1022 { 1124 {
1023 struct coro temp; 1125 struct coro temp;
1024 1126
1025 assert (!(coro->flags & CF_RUNNING));
1026
1027 Zero (&temp, 1, struct coro);
1028 temp.save = CORO_SAVE_ALL;
1029
1030 if (coro->flags & CF_RUNNING) 1127 if (coro->flags & CF_RUNNING)
1031 croak ("FATAL: tried to destroy currently running coroutine"); 1128 croak ("FATAL: tried to destroy currently running coroutine");
1032 1129
1033 save_perl (aTHX_ &temp); 1130 save_perl (aTHX_ &temp);
1034 load_perl (aTHX_ coro); 1131 load_perl (aTHX_ coro);
1035 1132
1036 coro_destroy_stacks (aTHX); 1133 coro_destroy (aTHX_ coro);
1037 1134
1038 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ 1135 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */
1039 1136
1040 coro->mainstack = 0; 1137 coro->mainstack = 0;
1041 } 1138 }
1086#else 1183#else
1087# define MGf_DUP 0 1184# define MGf_DUP 0
1088#endif 1185#endif
1089}; 1186};
1090 1187
1091static struct coro *
1092SvSTATE_ (pTHX_ SV *coro)
1093{
1094 HV *stash;
1095 MAGIC *mg;
1096
1097 if (SvROK (coro))
1098 coro = SvRV (coro);
1099
1100 stash = SvSTASH (coro);
1101 if (stash != coro_stash && stash != coro_state_stash)
1102 {
1103 /* very slow, but rare, check */
1104 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
1105 croak ("Coro::State object required");
1106 }
1107
1108 mg = CORO_MAGIC (coro);
1109 return (struct coro *)mg->mg_ptr;
1110}
1111
1112#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
1113
1114static void 1188static void
1115prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) 1189prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv)
1116{ 1190{
1117 ta->prev = SvSTATE (prev_sv); 1191 ta->prev = SvSTATE (prev_sv);
1118 ta->next = SvSTATE (next_sv); 1192 ta->next = SvSTATE (next_sv);
1193 TRANSFER_CHECK (*ta);
1119} 1194}
1120 1195
1121static void 1196static void
1122api_transfer (SV *prev_sv, SV *next_sv) 1197api_transfer (SV *prev_sv, SV *next_sv)
1123{ 1198{
1124 dTHX; 1199 dTHX;
1125 struct transfer_args ta; 1200 struct transfer_args ta;
1126 1201
1127 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1202 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1128 TRANSFER (ta); 1203 TRANSFER (ta);
1129}
1130
1131static int
1132api_save (SV *coro_sv, int new_save)
1133{
1134 dTHX;
1135 struct coro *coro = SvSTATE (coro_sv);
1136 int old_save = coro->save;
1137
1138 if (new_save >= 0)
1139 coro->save = new_save;
1140
1141 return old_save;
1142} 1204}
1143 1205
1144/** Coro ********************************************************************/ 1206/** Coro ********************************************************************/
1145 1207
1146static void 1208static void
1205 { 1267 {
1206 LOCK; 1268 LOCK;
1207 next_sv = coro_deq (aTHX_ PRIO_MIN); 1269 next_sv = coro_deq (aTHX_ PRIO_MIN);
1208 1270
1209 /* nothing to schedule: call the idle handler */ 1271 /* nothing to schedule: call the idle handler */
1210 if (!next_sv) 1272 if (expect_false (!next_sv))
1211 { 1273 {
1212 dSP; 1274 dSP;
1213 UNLOCK; 1275 UNLOCK;
1214 1276
1215 ENTER; 1277 ENTER;
1225 } 1287 }
1226 1288
1227 ta->next = SvSTATE (next_sv); 1289 ta->next = SvSTATE (next_sv);
1228 1290
1229 /* cannot transfer to destroyed coros, skip and look for next */ 1291 /* cannot transfer to destroyed coros, skip and look for next */
1230 if (ta->next->flags & CF_DESTROYED) 1292 if (expect_false (ta->next->flags & CF_DESTROYED))
1231 { 1293 {
1232 UNLOCK; 1294 UNLOCK;
1233 SvREFCNT_dec (next_sv); 1295 SvREFCNT_dec (next_sv);
1234 /* coro_nready is already taken care of by destroy */ 1296 /* coro_nready is already taken care of by destroy */
1235 continue; 1297 continue;
1240 break; 1302 break;
1241 } 1303 }
1242 1304
1243 /* free this only after the transfer */ 1305 /* free this only after the transfer */
1244 prev_sv = SvRV (coro_current); 1306 prev_sv = SvRV (coro_current);
1245 SvRV_set (coro_current, next_sv);
1246 ta->prev = SvSTATE (prev_sv); 1307 ta->prev = SvSTATE (prev_sv);
1247 1308 TRANSFER_CHECK (*ta);
1248 assert (ta->next->flags & CF_READY); 1309 assert (ta->next->flags & CF_READY);
1249 ta->next->flags &= ~CF_READY; 1310 ta->next->flags &= ~CF_READY;
1311 SvRV_set (coro_current, next_sv);
1250 1312
1251 LOCK; 1313 LOCK;
1252 free_coro_mortal (aTHX); 1314 free_coro_mortal (aTHX);
1253 coro_mortal = prev_sv; 1315 coro_mortal = prev_sv;
1254 UNLOCK; 1316 UNLOCK;
1291 dTHX; 1353 dTHX;
1292 struct transfer_args ta; 1354 struct transfer_args ta;
1293 1355
1294 prepare_cede (aTHX_ &ta); 1356 prepare_cede (aTHX_ &ta);
1295 1357
1296 if (ta.prev != ta.next) 1358 if (expect_true (ta.prev != ta.next))
1297 { 1359 {
1298 TRANSFER (ta); 1360 TRANSFER (ta);
1299 return 1; 1361 return 1;
1300 } 1362 }
1301 else 1363 else
1315 } 1377 }
1316 else 1378 else
1317 return 0; 1379 return 0;
1318} 1380}
1319 1381
1382static void
1383api_trace (SV *coro_sv, int flags)
1384{
1385 dTHX;
1386 struct coro *coro = SvSTATE (coro_sv);
1387
1388 if (flags & CC_TRACE)
1389 {
1390 if (!coro->cctx)
1391 coro->cctx = cctx_new ();
1392 else if (!(coro->cctx->flags & CC_TRACE))
1393 croak ("cannot enable tracing on coroutine with custom stack");
1394
1395 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1396 }
1397 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1398 {
1399 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL);
1400
1401 if (coro->flags & CF_RUNNING)
1402 PL_runops = RUNOPS_DEFAULT;
1403 else
1404 coro->runops = RUNOPS_DEFAULT;
1405 }
1406}
1407
1320MODULE = Coro::State PACKAGE = Coro::State 1408MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1321 1409
1322PROTOTYPES: DISABLE 1410PROTOTYPES: DISABLE
1323 1411
1324BOOT: 1412BOOT:
1325{ 1413{
1326#ifdef USE_ITHREADS 1414#ifdef USE_ITHREADS
1327 MUTEX_INIT (&coro_mutex); 1415 MUTEX_INIT (&coro_mutex);
1328#endif 1416#endif
1329 BOOT_PAGESIZE; 1417 BOOT_PAGESIZE;
1418
1419 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1420 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1330 1421
1331 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1422 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1332 1423
1333 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); 1424 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1334 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); 1425 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1335 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); 1426 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1336 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); 1427 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1337 1428
1338 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1339 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1340 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
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));
1344 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1345
1346 main_mainstack = PL_mainstack; 1429 main_mainstack = PL_mainstack;
1347 main_top_env = PL_top_env; 1430 main_top_env = PL_top_env;
1348 1431
1349 while (main_top_env->je_prev) 1432 while (main_top_env->je_prev)
1350 main_top_env = main_top_env->je_prev; 1433 main_top_env = main_top_env->je_prev;
1362 struct coro *coro; 1445 struct coro *coro;
1363 HV *hv; 1446 HV *hv;
1364 int i; 1447 int i;
1365 1448
1366 Newz (0, coro, 1, struct coro); 1449 Newz (0, coro, 1, struct coro);
1367 coro->args = newAV (); 1450 coro->args = newAV ();
1368 coro->save = CORO_SAVE_DEF;
1369 coro->flags = CF_NEW; 1451 coro->flags = CF_NEW;
1370 1452
1371 if (coro_first) coro_first->prev = coro; 1453 if (coro_first) coro_first->prev = coro;
1372 coro->next = coro_first; 1454 coro->next = coro_first;
1373 coro_first = coro; 1455 coro_first = coro;
1374 1456
1375 coro->hv = hv = newHV (); 1457 coro->hv = hv = newHV ();
1376 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;
1377 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1459 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1378 1460
1461 av_extend (coro->args, items - 1);
1379 for (i = 1; i < items; i++) 1462 for (i = 1; i < items; i++)
1380 av_push (coro->args, newSVsv (ST (i))); 1463 av_push (coro->args, newSVsv (ST (i)));
1381} 1464}
1382 OUTPUT: 1465 OUTPUT:
1383 RETVAL 1466 RETVAL
1384 1467
1385int 1468# these not obviously related functions are all rolled into the same xs
1386save (SV *coro, int new_save = -1) 1469# function to increase chances that they all will call transfer with the same
1387 CODE: 1470# stack offset
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}
1400 OUTPUT:
1401 RETVAL
1402
1403void 1471void
1404_set_stacklevel (...) 1472_set_stacklevel (...)
1405 ALIAS: 1473 ALIAS:
1406 Coro::State::transfer = 1 1474 Coro::State::transfer = 1
1407 Coro::schedule = 2 1475 Coro::schedule = 2
1441 } 1509 }
1442 1510
1443 BARRIER; 1511 BARRIER;
1444 TRANSFER (ta); 1512 TRANSFER (ta);
1445 1513
1446 if (GIMME_V != G_VOID && ta.next != ta.prev) 1514 if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev))
1447 XSRETURN_YES; 1515 XSRETURN_YES;
1448} 1516}
1449 1517
1450bool 1518bool
1451_destroy (SV *coro_sv) 1519_destroy (SV *coro_sv)
1502{ 1570{
1503 if (coro->mainstack) 1571 if (coro->mainstack)
1504 { 1572 {
1505 struct coro temp; 1573 struct coro temp;
1506 Zero (&temp, 1, struct coro); 1574 Zero (&temp, 1, struct coro);
1507 temp.save = CORO_SAVE_ALL;
1508 1575
1509 if (!(coro->flags & CF_RUNNING)) 1576 if (!(coro->flags & CF_RUNNING))
1510 { 1577 {
1511 save_perl (aTHX_ &temp); 1578 save_perl (aTHX_ &temp);
1512 load_perl (aTHX_ coro); 1579 load_perl (aTHX_ coro);
1533 save_perl (aTHX_ coro); 1600 save_perl (aTHX_ coro);
1534 load_perl (aTHX_ &temp); 1601 load_perl (aTHX_ &temp);
1535 } 1602 }
1536 } 1603 }
1537} 1604}
1538 1605
1539SV * 1606SV *
1540is_ready (Coro::State coro) 1607is_ready (Coro::State coro)
1541 PROTOTYPE: $ 1608 PROTOTYPE: $
1542 ALIAS: 1609 ALIAS:
1543 is_ready = CF_READY 1610 is_ready = CF_READY
1548 RETVAL = boolSV (coro->flags & ix); 1615 RETVAL = boolSV (coro->flags & ix);
1549 OUTPUT: 1616 OUTPUT:
1550 RETVAL 1617 RETVAL
1551 1618
1552void 1619void
1553trace (Coro::State coro, int flags = CC_TRACE | CC_TRACE_SUB) 1620api_trace (SV *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 1621
1576SV * 1622SV *
1577has_stack (Coro::State coro) 1623has_stack (Coro::State coro)
1578 PROTOTYPE: $ 1624 PROTOTYPE: $
1579 CODE: 1625 CODE:
1590 RETVAL 1636 RETVAL
1591 1637
1592IV 1638IV
1593rss (Coro::State coro) 1639rss (Coro::State coro)
1594 PROTOTYPE: $ 1640 PROTOTYPE: $
1641 ALIAS:
1642 usecount = 1
1595 CODE: 1643 CODE:
1596 RETVAL = coro_rss (coro); 1644 switch (ix)
1645 {
1646 case 0: RETVAL = coro_rss (aTHX_ coro); break;
1647 case 1: RETVAL = coro->usecount; break;
1648 }
1597 OUTPUT: 1649 OUTPUT:
1598 RETVAL 1650 RETVAL
1599 1651
1600 1652
1601MODULE = Coro::State PACKAGE = Coro 1653MODULE = Coro::State PACKAGE = Coro
1606 1658
1607 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); 1659 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE);
1608 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); 1660 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE);
1609 av_async_pool = get_av ("Coro::async_pool", TRUE); 1661 av_async_pool = get_av ("Coro::async_pool", TRUE);
1610 1662
1611 coro_current = get_sv ("Coro::current", FALSE); 1663 coro_current = get_sv ("Coro::current", FALSE);
1612 SvREADONLY_on (coro_current); 1664 SvREADONLY_on (coro_current);
1613 1665
1614 coro_stash = gv_stashpv ("Coro", TRUE); 1666 coro_stash = gv_stashpv ("Coro", TRUE);
1615 1667
1616 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1668 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1625 1677
1626 { 1678 {
1627 SV *sv = perl_get_sv("Coro::API", 1); 1679 SV *sv = perl_get_sv("Coro::API", 1);
1628 1680
1629 coroapi.schedule = api_schedule; 1681 coroapi.schedule = api_schedule;
1630 coroapi.save = api_save;
1631 coroapi.cede = api_cede; 1682 coroapi.cede = api_cede;
1632 coroapi.cede_notself = api_cede_notself; 1683 coroapi.cede_notself = api_cede_notself;
1633 coroapi.ready = api_ready; 1684 coroapi.ready = api_ready;
1634 coroapi.is_ready = api_is_ready; 1685 coroapi.is_ready = api_is_ready;
1635 coroapi.nready = &coro_nready; 1686 coroapi.nready = &coro_nready;
1684 CODE: 1735 CODE:
1685 RETVAL = coro_nready; 1736 RETVAL = coro_nready;
1686 OUTPUT: 1737 OUTPUT:
1687 RETVAL 1738 RETVAL
1688 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
1689# for async_pool speedup 1747# for async_pool speedup
1690void 1748void
1691_pool_1 (SV *cb) 1749_pool_1 (SV *cb)
1692 CODE: 1750 CODE:
1693{ 1751{
1694 int i, len; 1752 struct coro *coro = SvSTATE (coro_current);
1695 HV *hv = (HV *)SvRV (coro_current); 1753 HV *hv = (HV *)SvRV (coro_current);
1696 AV *defav = GvAV (PL_defgv); 1754 AV *defav = GvAV (PL_defgv);
1697 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); 1755 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1698 AV *invoke_av; 1756 AV *invoke_av;
1757 int i, len;
1699 1758
1700 if (!invoke) 1759 if (!invoke)
1701 croak ("\3terminate\2\n"); 1760 croak ("\3terminate\2\n");
1761
1762 SvREFCNT_dec (coro->saved_deffh);
1763 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv);
1702 1764
1703 hv_store (hv, "desc", sizeof ("desc") - 1, 1765 hv_store (hv, "desc", sizeof ("desc") - 1,
1704 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 1766 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1705 1767
1706 invoke_av = (AV *)SvRV (invoke); 1768 invoke_av = (AV *)SvRV (invoke);
1724{ 1786{
1725 struct coro *coro = SvSTATE (coro_current); 1787 struct coro *coro = SvSTATE (coro_current);
1726 1788
1727 sv_setsv (cb, &PL_sv_undef); 1789 sv_setsv (cb, &PL_sv_undef);
1728 1790
1791 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1792 coro->saved_deffh = 0;
1793
1729 if (coro_rss (coro) > SvIV (sv_pool_rss) 1794 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1730 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 1795 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1731 croak ("\3terminate\2\n"); 1796 croak ("\3terminate\2\n");
1732 1797
1733 av_clear (GvAV (PL_defgv)); 1798 av_clear (GvAV (PL_defgv));
1734 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, 1799 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1735 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); 1800 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1736 coro->save = CORO_SAVE_DEF; 1801
1737 coro->prio = 0; 1802 coro->prio = 0;
1803
1804 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1805 api_trace (coro_current, 0);
1806
1738 av_push (av_async_pool, newSVsv (coro_current)); 1807 av_push (av_async_pool, newSVsv (coro_current));
1739} 1808}
1740 1809
1741 1810
1742MODULE = Coro::State PACKAGE = Coro::AIO 1811MODULE = Coro::State PACKAGE = Coro::AIO

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines