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.172 by root, Sun Sep 30 13:15:01 2007 UTC vs.
Revision 1.180 by root, Wed Oct 3 17:12:20 2007 UTC

132static AV *main_mainstack; /* used to differentiate between $main and others */ 132static AV *main_mainstack; /* used to differentiate between $main and others */
133static JMPENV *main_top_env; 133static JMPENV *main_top_env;
134static HV *coro_state_stash, *coro_stash; 134static HV *coro_state_stash, *coro_stash;
135static SV *coro_mortal; /* will be freed after next transfer */ 135static SV *coro_mortal; /* will be freed after next transfer */
136 136
137static GV *irsgv; /* $/ */
138
137/* async_pool helper stuff */ 139/* async_pool helper stuff */
138static SV *sv_pool_rss; 140static SV *sv_pool_rss;
139static SV *sv_pool_size; 141static SV *sv_pool_size;
140static AV *av_async_pool; 142static AV *av_async_pool;
141 143
184 coro_cctx *cctx; 186 coro_cctx *cctx;
185 187
186 /* data associated with this coroutine (initial args) */ 188 /* data associated with this coroutine (initial args) */
187 AV *args; 189 AV *args;
188 int refcnt; 190 int refcnt;
189 int save; /* CORO_SAVE flags */
190 int flags; /* CF_ flags */ 191 int flags; /* CF_ flags */
191 192
192 /* optionally saved, might be zero */ 193 /* optionally saved, might be zero */
193 AV *defav; /* @_ */ 194 AV *defav; /* @_ */
194 SV *defsv; /* $_ */ 195 SV *defsv; /* $_ */
195 SV *errsv; /* $@ */ 196 SV *errsv; /* $@ */
196 GV *deffh; /* default filehandle */ 197 IO *deffh; /* default filehandle */
197 SV *irssv; /* $/ */ 198 SV *irssv; /* $/ */
198 SV *irssv_sv; /* real $/ cache */ 199 SV *irssv_sv; /* real $/ cache */
199 200
200#define VAR(name,type) type name; 201#define VAR(name,type) type name;
201# include "state.h" 202# include "state.h"
375 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 376 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
376} 377}
377 378
378/** load & save, init *******************************************************/ 379/** load & save, init *******************************************************/
379 380
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
385static void 381static void
386load_perl (pTHX_ Coro__State c) 382load_perl (pTHX_ Coro__State c)
387{ 383{
388#define VAR(name,type) PL_ ## name = c->name; 384#define VAR(name,type) PL_ ## name = c->name;
389# include "state.h" 385# include "state.h"
390#undef VAR 386#undef VAR
391 387
392 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 388 GvSV (PL_defgv) = c->defsv;
393 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 389 GvAV (PL_defgv) = c->defav;
394 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 390 GvSV (PL_errgv) = c->errsv;
395 if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh); 391 PL_defoutgv = c->deffh;
396 392 PL_rs = c->irssv;
397 if (c->irssv) 393 GvSV (irsgv) = c->irssv_sv;
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 394
412 { 395 {
413 dSP; 396 dSP;
414 CV *cv; 397 CV *cv;
415 398
473 } 456 }
474 457
475 PUTBACK; 458 PUTBACK;
476 } 459 }
477 460
478 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 461 c->defav = GvAV (PL_defgv);
479 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 462 c->defsv = DEFSV;
480 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 463 c->errsv = ERRSV;
481 c->deffh = c->save & CORO_SAVE_DEFFH ? (GV *)SvREFCNT_inc (PL_defoutgv) : 0; 464 c->deffh = PL_defoutgv;
482 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; 465 c->irssv = PL_rs;
466 c->irssv_sv = GvSV (irsgv);
483 467
484#define VAR(name,type)c->name = PL_ ## name; 468#define VAR(name,type)c->name = PL_ ## name;
485# include "state.h" 469# include "state.h"
486#undef VAR 470#undef VAR
487} 471}
540 * destroy the stacks, the callchain etc... 524 * destroy the stacks, the callchain etc...
541 */ 525 */
542static void 526static void
543coro_destroy_stacks (pTHX) 527coro_destroy_stacks (pTHX)
544{ 528{
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) 529 while (PL_curstackinfo->si_next)
563 PL_curstackinfo = PL_curstackinfo->si_next; 530 PL_curstackinfo = PL_curstackinfo->si_next;
564 531
565 while (PL_curstackinfo) 532 while (PL_curstackinfo)
566 { 533 {
615} 582}
616 583
617/** coroutine stack handling ************************************************/ 584/** coroutine stack handling ************************************************/
618 585
619static void 586static void
620setup_coro (pTHX_ struct coro *coro) 587coro_setup (pTHX_ struct coro *coro)
621{ 588{
622 /* 589 /*
623 * emulate part of the perl startup here. 590 * emulate part of the perl startup here.
624 */ 591 */
625 coro_init_stacks (aTHX); 592 coro_init_stacks (aTHX);
630 PL_comppad = 0; 597 PL_comppad = 0;
631 PL_curpm = 0; 598 PL_curpm = 0;
632 PL_localizing = 0; 599 PL_localizing = 0;
633 PL_dirty = 0; 600 PL_dirty = 0;
634 PL_restartop = 0; 601 PL_restartop = 0;
602
603 GvSV (PL_defgv) = NEWSV (0, 0);
604 GvAV (PL_defgv) = coro->args; coro->args = 0;
605 GvSV (PL_errgv) = NEWSV (0, 0);
606 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
607 PL_rs = newSVsv (GvSV (irsgv));
608
609 {
610 IO *io = newIO ();
611 PL_defoutgv = newGVgen ("Coro");
612 GvIOp(PL_defoutgv) = io;
613 IoTYPE (io) = IoTYPE_WRONLY;
614 IoOFP (io) = IoIFP (io) = PerlIO_stdout ();
615 IoFLAGS (io) |= IOf_FLUSH;
616 }
635 617
636 { 618 {
637 dSP; 619 dSP;
638 LOGOP myop; 620 LOGOP myop;
639
640 SvREFCNT_dec (GvAV (PL_defgv));
641 GvAV (PL_defgv) = coro->args; coro->args = 0;
642 621
643 Zero (&myop, 1, LOGOP); 622 Zero (&myop, 1, LOGOP);
644 myop.op_next = Nullop; 623 myop.op_next = Nullop;
645 myop.op_flags = OPf_WANT_VOID; 624 myop.op_flags = OPf_WANT_VOID;
646 625
654 633
655 ENTER; /* necessary e.g. for dounwind */ 634 ENTER; /* necessary e.g. for dounwind */
656} 635}
657 636
658static void 637static void
638coro_destroy (pTHX_ struct coro *coro)
639{
640 if (!IN_DESTRUCT)
641 {
642 /* restore all saved variables and stuff */
643 LEAVE_SCOPE (0);
644 assert (PL_tmps_floor == -1);
645
646 /* free all temporaries */
647 FREETMPS;
648 assert (PL_tmps_ix == -1);
649
650 /* unwind all extra stacks */
651 POPSTACK_TO (PL_mainstack);
652
653 /* unwind main stack */
654 dounwind (-1);
655 }
656
657 SvREFCNT_dec (GvSV (PL_defgv));
658 SvREFCNT_dec (GvAV (PL_defgv));
659 SvREFCNT_dec (GvSV (PL_errgv));
660 SvREFCNT_dec (PL_defoutgv);
661 SvREFCNT_dec (PL_rs);
662 SvREFCNT_dec (GvSV (irsgv));
663
664 coro_destroy_stacks (aTHX);
665}
666
667static void
659free_coro_mortal (pTHX) 668free_coro_mortal (pTHX)
660{ 669{
661 if (coro_mortal) 670 if (coro_mortal)
662 { 671 {
663 SvREFCNT_dec (coro_mortal); 672 SvREFCNT_dec (coro_mortal);
675 684
676 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 685 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
677 { 686 {
678 PERL_ASYNC_CHECK (); 687 PERL_ASYNC_CHECK ();
679 688
680 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) 689 if (cctx->flags & CC_TRACE_ALL)
681 { 690 {
682 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 691 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB)
683 SV **bot, **top;
684 AV *av = newAV (); /* return values */
685 SV **cb;
686 runops_proc_t old_runops = PL_runops;
687 dSP;
688 ENTER;
689 SAVETMPS;
690 EXTEND (SP, 3);
691 PL_runops = RUNOPS_DEFAULT;
692
693 GV *gv = CvGV (cx->blk_sub.cv);
694 SV *fullname = sv_2mortal (newSV (0));
695 if (isGV (gv))
696 gv_efullname3 (fullname, gv, 0);
697
698 bot = PL_stack_base + cx->blk_oldsp + 1;
699 top = cx->blk_gimme == G_ARRAY ? SP + 1
700 : cx->blk_gimme == G_SCALAR ? bot + 1
701 : bot;
702
703 while (bot < top)
704 av_push (av, SvREFCNT_inc (*bot++));
705
706 PUSHMARK (SP);
707 PUSHs (&PL_sv_no);
708 PUSHs (fullname);
709 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
710 PUTBACK;
711 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
712 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
713 SPAGAIN;
714
715 FREETMPS;
716 LEAVE;
717 PL_runops = old_runops;
718 }
719
720 if (oldcop != PL_curcop)
721 {
722 oldcop = PL_curcop;
723
724 if (PL_curcop != &PL_compiling)
725 { 692 {
693 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
694 SV **bot, **top;
695 AV *av = newAV (); /* return values */
726 SV **cb; 696 SV **cb;
727 runops_proc_t old_runops = PL_runops;
728 dSP; 697 dSP;
698
699 GV *gv = CvGV (cx->blk_sub.cv);
700 SV *fullname = sv_2mortal (newSV (0));
701 if (isGV (gv))
702 gv_efullname3 (fullname, gv, 0);
703
704 bot = PL_stack_base + cx->blk_oldsp + 1;
705 top = cx->blk_gimme == G_ARRAY ? SP + 1
706 : cx->blk_gimme == G_SCALAR ? bot + 1
707 : bot;
708
709 while (bot < top)
710 av_push (av, SvREFCNT_inc (*bot++));
711
712 PL_runops = RUNOPS_DEFAULT;
729 ENTER; 713 ENTER;
730 SAVETMPS; 714 SAVETMPS;
731 EXTEND (SP, 3); 715 EXTEND (SP, 3);
732 PL_runops = RUNOPS_DEFAULT; 716 PUSHMARK (SP);
717 PUSHs (&PL_sv_no);
718 PUSHs (fullname);
719 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
720 PUTBACK;
721 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
722 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
723 SPAGAIN;
724 FREETMPS;
725 LEAVE;
726 PL_runops = runops_trace;
727 }
733 728
734 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 729 if (oldcop != PL_curcop)
730 {
731 oldcop = PL_curcop;
732
733 if (PL_curcop != &PL_compiling)
735 { 734 {
736 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 735 SV **cb;
737 736
738 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 737 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB)
739 { 738 {
739 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
740
741 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
742 {
743 runops_proc_t old_runops = PL_runops;
744 dSP;
740 GV *gv = CvGV (cx->blk_sub.cv); 745 GV *gv = CvGV (cx->blk_sub.cv);
741 SV *fullname = sv_2mortal (newSV (0)); 746 SV *fullname = sv_2mortal (newSV (0));
747
742 if (isGV (gv)) 748 if (isGV (gv))
743 gv_efullname3 (fullname, gv, 0); 749 gv_efullname3 (fullname, gv, 0);
744 750
751 PL_runops = RUNOPS_DEFAULT;
752 ENTER;
753 SAVETMPS;
754 EXTEND (SP, 3);
755 PUSHMARK (SP);
756 PUSHs (&PL_sv_yes);
757 PUSHs (fullname);
758 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
759 PUTBACK;
760 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
761 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
762 SPAGAIN;
763 FREETMPS;
764 LEAVE;
765 PL_runops = runops_trace;
766 }
767
768 oldcxix = cxstack_ix;
769 }
770
771 if (cctx->flags & CC_TRACE_LINE)
772 {
773 dSP;
774
775 PL_runops = RUNOPS_DEFAULT;
776 ENTER;
777 SAVETMPS;
778 EXTEND (SP, 3);
779 PL_runops = RUNOPS_DEFAULT;
745 PUSHMARK (SP); 780 PUSHMARK (SP);
746 PUSHs (&PL_sv_yes); 781 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
747 PUSHs (fullname); 782 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop))));
748 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
749 PUTBACK; 783 PUTBACK;
750 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); 784 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0);
751 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 785 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
752 SPAGAIN; 786 SPAGAIN;
787 FREETMPS;
788 LEAVE;
789 PL_runops = runops_trace;
753 } 790 }
754
755 oldcxix = cxstack_ix;
756 } 791 }
757
758 if (cctx->flags & CC_TRACE_LINE)
759 {
760 PUSHMARK (SP);
761 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
762 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop))));
763 PUTBACK;
764 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0);
765 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
766 SPAGAIN;
767 }
768
769 FREETMPS;
770 LEAVE;
771 PL_runops = old_runops;
772 } 792 }
773 } 793 }
774 } 794 }
775 795
776 TAINT_NOT; 796 TAINT_NOT;
998 /* need to start coroutine */ 1018 /* need to start coroutine */
999 next->flags &= ~CF_NEW; 1019 next->flags &= ~CF_NEW;
1000 /* first get rid of the old state */ 1020 /* first get rid of the old state */
1001 save_perl (aTHX_ prev); 1021 save_perl (aTHX_ prev);
1002 /* setup coroutine call */ 1022 /* setup coroutine call */
1003 setup_coro (aTHX_ next); 1023 coro_setup (aTHX_ next);
1004 } 1024 }
1005 else 1025 else
1006 { 1026 {
1007 /* coroutine already started */ 1027 /* coroutine already started */
1008 save_perl (aTHX_ prev); 1028 save_perl (aTHX_ prev);
1070 1090
1071 if (coro->mainstack && coro->mainstack != main_mainstack) 1091 if (coro->mainstack && coro->mainstack != main_mainstack)
1072 { 1092 {
1073 struct coro temp; 1093 struct coro temp;
1074 1094
1075 assert (!(coro->flags & CF_RUNNING));
1076
1077 Zero (&temp, 1, struct coro);
1078 temp.save = CORO_SAVE_ALL;
1079
1080 if (coro->flags & CF_RUNNING) 1095 if (coro->flags & CF_RUNNING)
1081 croak ("FATAL: tried to destroy currently running coroutine"); 1096 croak ("FATAL: tried to destroy currently running coroutine");
1082 1097
1083 save_perl (aTHX_ &temp); 1098 save_perl (aTHX_ &temp);
1084 load_perl (aTHX_ coro); 1099 load_perl (aTHX_ coro);
1085 1100
1086 coro_destroy_stacks (aTHX); 1101 coro_destroy (aTHX_ coro);
1087 1102
1088 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ 1103 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */
1089 1104
1090 coro->mainstack = 0; 1105 coro->mainstack = 0;
1091 } 1106 }
1154 1169
1155 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1170 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1156 TRANSFER (ta); 1171 TRANSFER (ta);
1157} 1172}
1158 1173
1159static int
1160api_save (SV *coro_sv, int new_save)
1161{
1162 dTHX;
1163 struct coro *coro = SvSTATE (coro_sv);
1164 int old_save = coro->save;
1165
1166 if (new_save >= 0)
1167 coro->save = new_save;
1168
1169 return old_save;
1170}
1171
1172/** Coro ********************************************************************/ 1174/** Coro ********************************************************************/
1173 1175
1174static void 1176static void
1175coro_enq (pTHX_ SV *coro_sv) 1177coro_enq (pTHX_ SV *coro_sv)
1176{ 1178{
1343 } 1345 }
1344 else 1346 else
1345 return 0; 1347 return 0;
1346} 1348}
1347 1349
1350static void
1351api_trace (SV *coro_sv, int flags)
1352{
1353 dTHX;
1354 struct coro *coro = SvSTATE (coro_sv);
1355
1356 if (flags & CC_TRACE)
1357 {
1358 if (!coro->cctx)
1359 coro->cctx = cctx_new ();
1360 else if (!(coro->cctx->flags & CC_TRACE))
1361 croak ("cannot enable tracing on coroutine with custom stack");
1362
1363 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1364 }
1365 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1366 {
1367 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL);
1368
1369 if (coro->flags & CF_RUNNING)
1370 PL_runops = RUNOPS_DEFAULT;
1371 else
1372 coro->runops = RUNOPS_DEFAULT;
1373 }
1374}
1375
1348MODULE = Coro::State PACKAGE = Coro::State 1376MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1349 1377
1350PROTOTYPES: DISABLE 1378PROTOTYPES: DISABLE
1351 1379
1352BOOT: 1380BOOT:
1353{ 1381{
1354#ifdef USE_ITHREADS 1382#ifdef USE_ITHREADS
1355 MUTEX_INIT (&coro_mutex); 1383 MUTEX_INIT (&coro_mutex);
1356#endif 1384#endif
1357 BOOT_PAGESIZE; 1385 BOOT_PAGESIZE;
1386
1387 irsgv = gv_fetchpv ("/", 1, SVt_PV);
1358 1388
1359 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1389 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1360 1390
1361 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); 1391 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1362 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); 1392 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1363 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); 1393 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1364 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); 1394 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1365 1395
1366 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1367 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1368 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1369 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1370 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH));
1371 newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF));
1372 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1373
1374 main_mainstack = PL_mainstack; 1396 main_mainstack = PL_mainstack;
1375 main_top_env = PL_top_env; 1397 main_top_env = PL_top_env;
1376 1398
1377 while (main_top_env->je_prev) 1399 while (main_top_env->je_prev)
1378 main_top_env = main_top_env->je_prev; 1400 main_top_env = main_top_env->je_prev;
1390 struct coro *coro; 1412 struct coro *coro;
1391 HV *hv; 1413 HV *hv;
1392 int i; 1414 int i;
1393 1415
1394 Newz (0, coro, 1, struct coro); 1416 Newz (0, coro, 1, struct coro);
1395 coro->args = newAV (); 1417 coro->args = newAV ();
1396 coro->save = CORO_SAVE_DEF;
1397 coro->flags = CF_NEW; 1418 coro->flags = CF_NEW;
1398 1419
1399 if (coro_first) coro_first->prev = coro; 1420 if (coro_first) coro_first->prev = coro;
1400 coro->next = coro_first; 1421 coro->next = coro_first;
1401 coro_first = coro; 1422 coro_first = coro;
1408 av_push (coro->args, newSVsv (ST (i))); 1429 av_push (coro->args, newSVsv (ST (i)));
1409} 1430}
1410 OUTPUT: 1431 OUTPUT:
1411 RETVAL 1432 RETVAL
1412 1433
1413int 1434# these not obviously related functions are all rolled into the same xs
1414save (SV *coro, int new_save = -1) 1435# function to increase chances that they all will call transfer with the same
1415 CODE: 1436# stack offset
1416 RETVAL = api_save (coro, new_save);
1417 OUTPUT:
1418 RETVAL
1419
1420int
1421save_also (SV *coro_sv, int save_also)
1422 CODE:
1423{
1424 struct coro *coro = SvSTATE (coro_sv);
1425 RETVAL = coro->save;
1426 coro->save |= save_also;
1427}
1428 OUTPUT:
1429 RETVAL
1430
1431void 1437void
1432_set_stacklevel (...) 1438_set_stacklevel (...)
1433 ALIAS: 1439 ALIAS:
1434 Coro::State::transfer = 1 1440 Coro::State::transfer = 1
1435 Coro::schedule = 2 1441 Coro::schedule = 2
1530{ 1536{
1531 if (coro->mainstack) 1537 if (coro->mainstack)
1532 { 1538 {
1533 struct coro temp; 1539 struct coro temp;
1534 Zero (&temp, 1, struct coro); 1540 Zero (&temp, 1, struct coro);
1535 temp.save = CORO_SAVE_ALL;
1536 1541
1537 if (!(coro->flags & CF_RUNNING)) 1542 if (!(coro->flags & CF_RUNNING))
1538 { 1543 {
1539 save_perl (aTHX_ &temp); 1544 save_perl (aTHX_ &temp);
1540 load_perl (aTHX_ coro); 1545 load_perl (aTHX_ coro);
1576 RETVAL = boolSV (coro->flags & ix); 1581 RETVAL = boolSV (coro->flags & ix);
1577 OUTPUT: 1582 OUTPUT:
1578 RETVAL 1583 RETVAL
1579 1584
1580void 1585void
1581trace (Coro::State coro, int flags = CC_TRACE | CC_TRACE_SUB) 1586api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1582 CODE:
1583 if (flags & CC_TRACE)
1584 {
1585 if (!coro->cctx)
1586 coro->cctx = cctx_new ();
1587 else if (!(coro->cctx->flags & CC_TRACE))
1588 croak ("cannot enable tracing on coroutine with custom stack");
1589
1590 coro->cctx->flags |= flags & (CC_TRACE | CC_TRACE_ALL);
1591 }
1592 else
1593 if (coro->cctx && coro->cctx->flags & CC_TRACE)
1594 {
1595 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL);
1596 coro->cctx->flags |= CC_NOREUSE;
1597
1598 if (coro->flags & CF_RUNNING)
1599 PL_runops = RUNOPS_DEFAULT;
1600 else
1601 coro->runops = RUNOPS_DEFAULT;
1602 }
1603 1587
1604SV * 1588SV *
1605has_stack (Coro::State coro) 1589has_stack (Coro::State coro)
1606 PROTOTYPE: $ 1590 PROTOTYPE: $
1607 CODE: 1591 CODE:
1659 1643
1660 { 1644 {
1661 SV *sv = perl_get_sv("Coro::API", 1); 1645 SV *sv = perl_get_sv("Coro::API", 1);
1662 1646
1663 coroapi.schedule = api_schedule; 1647 coroapi.schedule = api_schedule;
1664 coroapi.save = api_save;
1665 coroapi.cede = api_cede; 1648 coroapi.cede = api_cede;
1666 coroapi.cede_notself = api_cede_notself; 1649 coroapi.cede_notself = api_cede_notself;
1667 coroapi.ready = api_ready; 1650 coroapi.ready = api_ready;
1668 coroapi.is_ready = api_is_ready; 1651 coroapi.is_ready = api_is_ready;
1669 coroapi.nready = &coro_nready; 1652 coroapi.nready = &coro_nready;
1765 croak ("\3terminate\2\n"); 1748 croak ("\3terminate\2\n");
1766 1749
1767 av_clear (GvAV (PL_defgv)); 1750 av_clear (GvAV (PL_defgv));
1768 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, 1751 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1769 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); 1752 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1770 coro->save = CORO_SAVE_DEF; 1753
1771 coro->prio = 0; 1754 coro->prio = 0;
1755
1756 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1757 api_trace (coro_current, 0);
1758
1772 av_push (av_async_pool, newSVsv (coro_current)); 1759 av_push (av_async_pool, newSVsv (coro_current));
1773} 1760}
1774 1761
1775 1762
1776MODULE = Coro::State PACKAGE = Coro::AIO 1763MODULE = Coro::State PACKAGE = Coro::AIO

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines