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.178 by root, Wed Oct 3 01:48:06 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 GvIOp(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 = GvIOp (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 {
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) = newSVsv (PL_rs);
607 PL_rs = newSVpvn ("\n", 1);
608
609 {
610 IO *io = newIO ();
611 GvIOp(PL_defoutgv) = io;
612 IoTYPE (io) = IoTYPE_WRONLY;
613 IoOFP (io) = IoIFP (io) = PerlIO_stdout ();
614 IoFLAGS (io) |= IOf_FLUSH;
615 }
635 616
636 { 617 {
637 dSP; 618 dSP;
638 LOGOP myop; 619 LOGOP myop;
639
640 SvREFCNT_dec (GvAV (PL_defgv));
641 GvAV (PL_defgv) = coro->args; coro->args = 0;
642 620
643 Zero (&myop, 1, LOGOP); 621 Zero (&myop, 1, LOGOP);
644 myop.op_next = Nullop; 622 myop.op_next = Nullop;
645 myop.op_flags = OPf_WANT_VOID; 623 myop.op_flags = OPf_WANT_VOID;
646 624
654 632
655 ENTER; /* necessary e.g. for dounwind */ 633 ENTER; /* necessary e.g. for dounwind */
656} 634}
657 635
658static void 636static void
637destroy_coro (pTHX_ struct coro *coro)
638{
639 if (!IN_DESTRUCT)
640 {
641 /* restore all saved variables and stuff */
642 LEAVE_SCOPE (0);
643 assert (PL_tmps_floor == -1);
644
645 /* free all temporaries */
646 FREETMPS;
647 assert (PL_tmps_ix == -1);
648
649 /* unwind all extra stacks */
650 POPSTACK_TO (PL_mainstack);
651
652 /* unwind main stack */
653 dounwind (-1);
654 }
655
656 SvREFCNT_dec (GvSV (PL_defgv));
657 SvREFCNT_dec (GvAV (PL_defgv));
658 SvREFCNT_dec (GvSV (PL_errgv));
659 SvREFCNT_dec (GvIOp(PL_defoutgv));
660 SvREFCNT_dec (PL_rs);
661 SvREFCNT_dec (GvSV (irsgv));
662
663 coro_destroy_stacks (aTHX);
664}
665
666static void
659free_coro_mortal (pTHX) 667free_coro_mortal (pTHX)
660{ 668{
661 if (coro_mortal) 669 if (coro_mortal)
662 { 670 {
663 SvREFCNT_dec (coro_mortal); 671 SvREFCNT_dec (coro_mortal);
675 683
676 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 684 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
677 { 685 {
678 PERL_ASYNC_CHECK (); 686 PERL_ASYNC_CHECK ();
679 687
680 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) 688 if (cctx->flags & CC_TRACE_ALL)
681 { 689 {
682 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 690 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 { 691 {
692 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
693 SV **bot, **top;
694 AV *av = newAV (); /* return values */
726 SV **cb; 695 SV **cb;
727 runops_proc_t old_runops = PL_runops;
728 dSP; 696 dSP;
697
698 GV *gv = CvGV (cx->blk_sub.cv);
699 SV *fullname = sv_2mortal (newSV (0));
700 if (isGV (gv))
701 gv_efullname3 (fullname, gv, 0);
702
703 bot = PL_stack_base + cx->blk_oldsp + 1;
704 top = cx->blk_gimme == G_ARRAY ? SP + 1
705 : cx->blk_gimme == G_SCALAR ? bot + 1
706 : bot;
707
708 while (bot < top)
709 av_push (av, SvREFCNT_inc (*bot++));
710
711 PL_runops = RUNOPS_DEFAULT;
729 ENTER; 712 ENTER;
730 SAVETMPS; 713 SAVETMPS;
731 EXTEND (SP, 3); 714 EXTEND (SP, 3);
732 PL_runops = RUNOPS_DEFAULT; 715 PUSHMARK (SP);
716 PUSHs (&PL_sv_no);
717 PUSHs (fullname);
718 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
719 PUTBACK;
720 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
721 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
722 SPAGAIN;
723 FREETMPS;
724 LEAVE;
725 PL_runops = runops_trace;
726 }
733 727
734 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 728 if (oldcop != PL_curcop)
729 {
730 oldcop = PL_curcop;
731
732 if (PL_curcop != &PL_compiling)
735 { 733 {
736 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 734 SV **cb;
737 735
738 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 736 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB)
739 { 737 {
738 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
739
740 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
741 {
742 runops_proc_t old_runops = PL_runops;
743 dSP;
740 GV *gv = CvGV (cx->blk_sub.cv); 744 GV *gv = CvGV (cx->blk_sub.cv);
741 SV *fullname = sv_2mortal (newSV (0)); 745 SV *fullname = sv_2mortal (newSV (0));
746
742 if (isGV (gv)) 747 if (isGV (gv))
743 gv_efullname3 (fullname, gv, 0); 748 gv_efullname3 (fullname, gv, 0);
744 749
750 PL_runops = RUNOPS_DEFAULT;
751 ENTER;
752 SAVETMPS;
753 EXTEND (SP, 3);
754 PUSHMARK (SP);
755 PUSHs (&PL_sv_yes);
756 PUSHs (fullname);
757 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
758 PUTBACK;
759 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
760 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
761 SPAGAIN;
762 FREETMPS;
763 LEAVE;
764 PL_runops = runops_trace;
765 }
766
767 oldcxix = cxstack_ix;
768 }
769
770 if (cctx->flags & CC_TRACE_LINE)
771 {
772 dSP;
773
774 PL_runops = RUNOPS_DEFAULT;
775 ENTER;
776 SAVETMPS;
777 EXTEND (SP, 3);
778 PL_runops = RUNOPS_DEFAULT;
745 PUSHMARK (SP); 779 PUSHMARK (SP);
746 PUSHs (&PL_sv_yes); 780 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
747 PUSHs (fullname); 781 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; 782 PUTBACK;
750 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); 783 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); 784 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
752 SPAGAIN; 785 SPAGAIN;
786 FREETMPS;
787 LEAVE;
788 PL_runops = runops_trace;
753 } 789 }
754
755 oldcxix = cxstack_ix;
756 } 790 }
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 } 791 }
773 } 792 }
774 } 793 }
775 794
776 TAINT_NOT; 795 TAINT_NOT;
1070 1089
1071 if (coro->mainstack && coro->mainstack != main_mainstack) 1090 if (coro->mainstack && coro->mainstack != main_mainstack)
1072 { 1091 {
1073 struct coro temp; 1092 struct coro temp;
1074 1093
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) 1094 if (coro->flags & CF_RUNNING)
1081 croak ("FATAL: tried to destroy currently running coroutine"); 1095 croak ("FATAL: tried to destroy currently running coroutine");
1082 1096
1083 save_perl (aTHX_ &temp); 1097 save_perl (aTHX_ &temp);
1084 load_perl (aTHX_ coro); 1098 load_perl (aTHX_ coro);
1085 1099
1086 coro_destroy_stacks (aTHX); 1100 destroy_coro (aTHX_ coro);
1087 1101
1088 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */ 1102 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */
1089 1103
1090 coro->mainstack = 0; 1104 coro->mainstack = 0;
1091 } 1105 }
1154 1168
1155 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1169 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1156 TRANSFER (ta); 1170 TRANSFER (ta);
1157} 1171}
1158 1172
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 ********************************************************************/ 1173/** Coro ********************************************************************/
1173 1174
1174static void 1175static void
1175coro_enq (pTHX_ SV *coro_sv) 1176coro_enq (pTHX_ SV *coro_sv)
1176{ 1177{
1343 } 1344 }
1344 else 1345 else
1345 return 0; 1346 return 0;
1346} 1347}
1347 1348
1349static void
1350api_trace (SV *coro_sv, int flags)
1351{
1352 dTHX;
1353 struct coro *coro = SvSTATE (coro_sv);
1354
1355 if (flags & CC_TRACE)
1356 {
1357 if (!coro->cctx)
1358 coro->cctx = cctx_new ();
1359 else if (!(coro->cctx->flags & CC_TRACE))
1360 croak ("cannot enable tracing on coroutine with custom stack");
1361
1362 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1363 }
1364 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1365 {
1366 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL);
1367
1368 if (coro->flags & CF_RUNNING)
1369 PL_runops = RUNOPS_DEFAULT;
1370 else
1371 coro->runops = RUNOPS_DEFAULT;
1372 }
1373}
1374
1348MODULE = Coro::State PACKAGE = Coro::State 1375MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1349 1376
1350PROTOTYPES: DISABLE 1377PROTOTYPES: DISABLE
1351 1378
1352BOOT: 1379BOOT:
1353{ 1380{
1354#ifdef USE_ITHREADS 1381#ifdef USE_ITHREADS
1355 MUTEX_INIT (&coro_mutex); 1382 MUTEX_INIT (&coro_mutex);
1356#endif 1383#endif
1357 BOOT_PAGESIZE; 1384 BOOT_PAGESIZE;
1385
1386 irsgv = gv_fetchpv ("/", 1, SVt_PV);
1358 1387
1359 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1388 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1360 1389
1361 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); 1390 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1362 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); 1391 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1363 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); 1392 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1364 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL)); 1393 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1365 1394
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; 1395 main_mainstack = PL_mainstack;
1375 main_top_env = PL_top_env; 1396 main_top_env = PL_top_env;
1376 1397
1377 while (main_top_env->je_prev) 1398 while (main_top_env->je_prev)
1378 main_top_env = main_top_env->je_prev; 1399 main_top_env = main_top_env->je_prev;
1390 struct coro *coro; 1411 struct coro *coro;
1391 HV *hv; 1412 HV *hv;
1392 int i; 1413 int i;
1393 1414
1394 Newz (0, coro, 1, struct coro); 1415 Newz (0, coro, 1, struct coro);
1395 coro->args = newAV (); 1416 coro->args = newAV ();
1396 coro->save = CORO_SAVE_DEF;
1397 coro->flags = CF_NEW; 1417 coro->flags = CF_NEW;
1398 1418
1399 if (coro_first) coro_first->prev = coro; 1419 if (coro_first) coro_first->prev = coro;
1400 coro->next = coro_first; 1420 coro->next = coro_first;
1401 coro_first = coro; 1421 coro_first = coro;
1408 av_push (coro->args, newSVsv (ST (i))); 1428 av_push (coro->args, newSVsv (ST (i)));
1409} 1429}
1410 OUTPUT: 1430 OUTPUT:
1411 RETVAL 1431 RETVAL
1412 1432
1413int 1433# these not obviously related functions are all rolled into the same xs
1414save (SV *coro, int new_save = -1) 1434# function to increase chances that they all will call transfer with the same
1415 CODE: 1435# 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 1436void
1432_set_stacklevel (...) 1437_set_stacklevel (...)
1433 ALIAS: 1438 ALIAS:
1434 Coro::State::transfer = 1 1439 Coro::State::transfer = 1
1435 Coro::schedule = 2 1440 Coro::schedule = 2
1530{ 1535{
1531 if (coro->mainstack) 1536 if (coro->mainstack)
1532 { 1537 {
1533 struct coro temp; 1538 struct coro temp;
1534 Zero (&temp, 1, struct coro); 1539 Zero (&temp, 1, struct coro);
1535 temp.save = CORO_SAVE_ALL;
1536 1540
1537 if (!(coro->flags & CF_RUNNING)) 1541 if (!(coro->flags & CF_RUNNING))
1538 { 1542 {
1539 save_perl (aTHX_ &temp); 1543 save_perl (aTHX_ &temp);
1540 load_perl (aTHX_ coro); 1544 load_perl (aTHX_ coro);
1576 RETVAL = boolSV (coro->flags & ix); 1580 RETVAL = boolSV (coro->flags & ix);
1577 OUTPUT: 1581 OUTPUT:
1578 RETVAL 1582 RETVAL
1579 1583
1580void 1584void
1581trace (Coro::State coro, int flags = CC_TRACE | CC_TRACE_SUB) 1585api_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 1586
1604SV * 1587SV *
1605has_stack (Coro::State coro) 1588has_stack (Coro::State coro)
1606 PROTOTYPE: $ 1589 PROTOTYPE: $
1607 CODE: 1590 CODE:
1659 1642
1660 { 1643 {
1661 SV *sv = perl_get_sv("Coro::API", 1); 1644 SV *sv = perl_get_sv("Coro::API", 1);
1662 1645
1663 coroapi.schedule = api_schedule; 1646 coroapi.schedule = api_schedule;
1664 coroapi.save = api_save;
1665 coroapi.cede = api_cede; 1647 coroapi.cede = api_cede;
1666 coroapi.cede_notself = api_cede_notself; 1648 coroapi.cede_notself = api_cede_notself;
1667 coroapi.ready = api_ready; 1649 coroapi.ready = api_ready;
1668 coroapi.is_ready = api_is_ready; 1650 coroapi.is_ready = api_is_ready;
1669 coroapi.nready = &coro_nready; 1651 coroapi.nready = &coro_nready;
1765 croak ("\3terminate\2\n"); 1747 croak ("\3terminate\2\n");
1766 1748
1767 av_clear (GvAV (PL_defgv)); 1749 av_clear (GvAV (PL_defgv));
1768 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, 1750 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1769 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); 1751 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1770 coro->save = CORO_SAVE_DEF; 1752
1771 coro->prio = 0; 1753 coro->prio = 0;
1754
1755 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1756 api_trace (coro_current, 0);
1757
1772 av_push (av_async_pool, newSVsv (coro_current)); 1758 av_push (av_async_pool, newSVsv (coro_current));
1773} 1759}
1774 1760
1775 1761
1776MODULE = Coro::State PACKAGE = Coro::AIO 1762MODULE = Coro::State PACKAGE = Coro::AIO

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines