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.145 by root, Mon Mar 12 21:40:14 2007 UTC vs.
Revision 1.150 by root, Wed Jun 13 21:04:48 2007 UTC

1#include "libcoro/coro.c" 1#include "libcoro/coro.c"
2
3#define PERL_NO_GET_CONTEXT
2 4
3#include "EXTERN.h" 5#include "EXTERN.h"
4#include "perl.h" 6#include "perl.h"
5#include "XSUB.h" 7#include "XSUB.h"
6 8
127}; 129};
128 130
129static size_t coro_stacksize = CORO_STACKSIZE; 131static size_t coro_stacksize = CORO_STACKSIZE;
130static struct CoroAPI coroapi; 132static struct CoroAPI coroapi;
131static AV *main_mainstack; /* used to differentiate between $main and others */ 133static AV *main_mainstack; /* used to differentiate between $main and others */
134static JMPENV *main_top_env;
132static HV *coro_state_stash, *coro_stash; 135static HV *coro_state_stash, *coro_stash;
133static SV *coro_mortal; /* will be freed after next transfer */ 136static SV *coro_mortal; /* will be freed after next transfer */
134 137
135static struct coro_cctx *cctx_first; 138static struct coro_cctx *cctx_first;
136static int cctx_count, cctx_idle; 139static int cctx_count, cctx_idle;
175 178
176 /* optionally saved, might be zero */ 179 /* optionally saved, might be zero */
177 AV *defav; /* @_ */ 180 AV *defav; /* @_ */
178 SV *defsv; /* $_ */ 181 SV *defsv; /* $_ */
179 SV *errsv; /* $@ */ 182 SV *errsv; /* $@ */
183 GV *deffh; /* default filehandle */
180 SV *irssv; /* $/ */ 184 SV *irssv; /* $/ */
181 SV *irssv_sv; /* real $/ cache */ 185 SV *irssv_sv; /* real $/ cache */
182 186
183#define VAR(name,type) type name; 187#define VAR(name,type) type name;
184# include "state.h" 188# include "state.h"
206static int coro_nready; 210static int coro_nready;
207 211
208/** lowlevel stuff **********************************************************/ 212/** lowlevel stuff **********************************************************/
209 213
210static AV * 214static AV *
211coro_clone_padlist (CV *cv) 215coro_clone_padlist (pTHX_ CV *cv)
212{ 216{
213 AV *padlist = CvPADLIST (cv); 217 AV *padlist = CvPADLIST (cv);
214 AV *newpadlist, *newpad; 218 AV *newpadlist, *newpad;
215 219
216 newpadlist = newAV (); 220 newpadlist = newAV ();
228 232
229 return newpadlist; 233 return newpadlist;
230} 234}
231 235
232static void 236static void
233free_padlist (AV *padlist) 237free_padlist (pTHX_ AV *padlist)
234{ 238{
235 /* may be during global destruction */ 239 /* may be during global destruction */
236 if (SvREFCNT (padlist)) 240 if (SvREFCNT (padlist))
237 { 241 {
238 I32 i = AvFILLp (padlist); 242 I32 i = AvFILLp (padlist);
259 AV *padlist; 263 AV *padlist;
260 AV *av = (AV *)mg->mg_obj; 264 AV *av = (AV *)mg->mg_obj;
261 265
262 /* casting is fun. */ 266 /* casting is fun. */
263 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 267 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
264 free_padlist (padlist); 268 free_padlist (aTHX_ padlist);
265 269
266 SvREFCNT_dec (av); 270 SvREFCNT_dec (av);
267 271
268 return 0; 272 return 0;
269} 273}
279 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 283 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
280 : 0 284 : 0
281 285
282/* the next two functions merely cache the padlists */ 286/* the next two functions merely cache the padlists */
283static void 287static void
284get_padlist (CV *cv) 288get_padlist (pTHX_ CV *cv)
285{ 289{
286 MAGIC *mg = CORO_MAGIC (cv); 290 MAGIC *mg = CORO_MAGIC (cv);
287 AV *av; 291 AV *av;
288 292
289 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) 293 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
295 CV *cp = Perl_cv_clone (cv); 299 CV *cp = Perl_cv_clone (cv);
296 CvPADLIST (cv) = CvPADLIST (cp); 300 CvPADLIST (cv) = CvPADLIST (cp);
297 CvPADLIST (cp) = 0; 301 CvPADLIST (cp) = 0;
298 SvREFCNT_dec (cp); 302 SvREFCNT_dec (cp);
299#else 303#else
300 CvPADLIST (cv) = coro_clone_padlist (cv); 304 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv);
301#endif 305#endif
302 } 306 }
303} 307}
304 308
305static void 309static void
306put_padlist (CV *cv) 310put_padlist (pTHX_ CV *cv)
307{ 311{
308 MAGIC *mg = CORO_MAGIC (cv); 312 MAGIC *mg = CORO_MAGIC (cv);
309 AV *av; 313 AV *av;
310 314
311 if (!mg) 315 if (!mg)
330#define SE } while (0) 334#define SE } while (0)
331 335
332#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE 336#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
333 337
334static void 338static void
335load_perl (Coro__State c) 339load_perl (pTHX_ Coro__State c)
336{ 340{
337#define VAR(name,type) PL_ ## name = c->name; 341#define VAR(name,type) PL_ ## name = c->name;
338# include "state.h" 342# include "state.h"
339#undef VAR 343#undef VAR
340 344
341 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 345 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
342 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 346 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
343 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 347 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
348 if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh);
349
344 if (c->irssv) 350 if (c->irssv)
345 { 351 {
346 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) 352 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
353 {
347 SvREFCNT_dec (c->irssv); 354 SvREFCNT_dec (c->irssv);
355 c->irssv = 0;
356 }
348 else 357 else
349 { 358 {
350 REPLACE_SV (PL_rs, c->irssv); 359 REPLACE_SV (PL_rs, c->irssv);
351 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); 360 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
352 sv_setsv (c->irssv_sv, PL_rs); 361 sv_setsv (c->irssv_sv, PL_rs);
358 CV *cv; 367 CV *cv;
359 368
360 /* now do the ugly restore mess */ 369 /* now do the ugly restore mess */
361 while ((cv = (CV *)POPs)) 370 while ((cv = (CV *)POPs))
362 { 371 {
363 put_padlist (cv); /* mark this padlist as available */ 372 put_padlist (aTHX_ cv); /* mark this padlist as available */
364 CvDEPTH (cv) = PTR2IV (POPs); 373 CvDEPTH (cv) = PTR2IV (POPs);
365 CvPADLIST (cv) = (AV *)POPs; 374 CvPADLIST (cv) = (AV *)POPs;
366 } 375 }
367 376
368 PUTBACK; 377 PUTBACK;
369 } 378 }
370 assert (!PL_comppad || AvARRAY (PL_comppad));//D 379 assert (!PL_comppad || AvARRAY (PL_comppad));//D
371} 380}
372 381
373static void 382static void
374save_perl (Coro__State c) 383save_perl (pTHX_ Coro__State c)
375{ 384{
376 assert (!PL_comppad || AvARRAY (PL_comppad));//D 385 assert (!PL_comppad || AvARRAY (PL_comppad));//D
377 { 386 {
378 dSP; 387 dSP;
379 I32 cxix = cxstack_ix; 388 I32 cxix = cxstack_ix;
404 PUSHs ((SV *)CvPADLIST (cv)); 413 PUSHs ((SV *)CvPADLIST (cv));
405 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 414 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
406 PUSHs ((SV *)cv); 415 PUSHs ((SV *)cv);
407 416
408 CvDEPTH (cv) = 0; 417 CvDEPTH (cv) = 0;
409 get_padlist (cv); 418 get_padlist (aTHX_ cv);
410 } 419 }
411 } 420 }
412 } 421 }
413 422
414 if (top_si->si_type == PERLSI_MAIN) 423 if (top_si->si_type == PERLSI_MAIN)
423 } 432 }
424 433
425 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 434 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
426 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 435 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
427 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 436 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
437 c->deffh = c->save & CORO_SAVE_DEFFH ? (GV *)SvREFCNT_inc (PL_defoutgv) : 0;
428 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0; 438 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0;
429 439
430#define VAR(name,type)c->name = PL_ ## name; 440#define VAR(name,type)c->name = PL_ ## name;
431# include "state.h" 441# include "state.h"
432#undef VAR 442#undef VAR
440 */ 450 */
441#if CORO_PREFER_PERL_FUNCTIONS 451#if CORO_PREFER_PERL_FUNCTIONS
442# define coro_init_stacks init_stacks 452# define coro_init_stacks init_stacks
443#else 453#else
444static void 454static void
445coro_init_stacks () 455coro_init_stacks (pTHX)
446{ 456{
447 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 457 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
448 PL_curstackinfo->si_type = PERLSI_MAIN; 458 PL_curstackinfo->si_type = PERLSI_MAIN;
449 PL_curstack = PL_curstackinfo->si_stack; 459 PL_curstack = PL_curstackinfo->si_stack;
450 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 460 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
484 494
485/* 495/*
486 * destroy the stacks, the callchain etc... 496 * destroy the stacks, the callchain etc...
487 */ 497 */
488static void 498static void
489coro_destroy_stacks () 499coro_destroy_stacks (pTHX)
490{ 500{
491 if (!IN_DESTRUCT) 501 if (!IN_DESTRUCT)
492 { 502 {
493 /* restore all saved variables and stuff */ 503 /* restore all saved variables and stuff */
494 LEAVE_SCOPE (0); 504 LEAVE_SCOPE (0);
530} 540}
531 541
532/** coroutine stack handling ************************************************/ 542/** coroutine stack handling ************************************************/
533 543
534static void 544static void
535setup_coro (struct coro *coro) 545setup_coro (pTHX_ struct coro *coro)
536{ 546{
537 /* 547 /*
538 * emulate part of the perl startup here. 548 * emulate part of the perl startup here.
539 */ 549 */
540
541 coro_init_stacks (); 550 coro_init_stacks (aTHX);
542 551
543 PL_curcop = &PL_compiling; 552 PL_curcop = &PL_compiling;
544 PL_in_eval = EVAL_NULL; 553 PL_in_eval = EVAL_NULL;
545 PL_comppad = 0; 554 PL_comppad = 0;
546 PL_curpm = 0; 555 PL_curpm = 0;
558 Zero (&myop, 1, LOGOP); 567 Zero (&myop, 1, LOGOP);
559 myop.op_next = Nullop; 568 myop.op_next = Nullop;
560 myop.op_flags = OPf_WANT_VOID; 569 myop.op_flags = OPf_WANT_VOID;
561 570
562 PUSHMARK (SP); 571 PUSHMARK (SP);
563 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 572 XPUSHs (av_shift (GvAV (PL_defgv)));
564 PUTBACK; 573 PUTBACK;
565 PL_op = (OP *)&myop; 574 PL_op = (OP *)&myop;
566 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 575 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
567 SPAGAIN; 576 SPAGAIN;
568 } 577 }
569 578
570 ENTER; /* necessary e.g. for dounwind */ 579 ENTER; /* necessary e.g. for dounwind */
571} 580}
572 581
573static void 582static void
574free_coro_mortal () 583free_coro_mortal (pTHX)
575{ 584{
576 if (coro_mortal) 585 if (coro_mortal)
577 { 586 {
578 SvREFCNT_dec (coro_mortal); 587 SvREFCNT_dec (coro_mortal);
579 coro_mortal = 0; 588 coro_mortal = 0;
580 } 589 }
581} 590}
582 591
583/* inject a fake call to Coro::State::_cctx_init into the execution */ 592/* inject a fake call to Coro::State::_cctx_init into the execution */
593/* _cctx_init should be careful, as it could be called at almost any time */
594/* during execution of a perl program */
584static void NOINLINE 595static void NOINLINE
585prepare_cctx (coro_cctx *cctx) 596prepare_cctx (pTHX_ coro_cctx *cctx)
586{ 597{
587 dSP; 598 dSP;
588 LOGOP myop; 599 LOGOP myop;
589 600
590 Zero (&myop, 1, LOGOP); 601 Zero (&myop, 1, LOGOP);
599 PL_op = (OP *)&myop; 610 PL_op = (OP *)&myop;
600 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 611 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
601 SPAGAIN; 612 SPAGAIN;
602} 613}
603 614
615/*
616 * this is a _very_ stripped down perl interpreter ;)
617 */
604static void 618static void
605coro_run (void *arg) 619coro_run (void *arg)
606{ 620{
621 dTHX;
622
607 /* coro_run is the alternative tail of transfer(), so unlock here. */ 623 /* coro_run is the alternative tail of transfer(), so unlock here. */
608 UNLOCK; 624 UNLOCK;
609 625
610 /*
611 * this is a _very_ stripped down perl interpreter ;)
612 */
613 PL_top_env = &PL_start_env; 626 PL_top_env = &PL_start_env;
614 627
615 /* inject call to cctx_init */ 628 /* inject a fake subroutine call to cctx_init */
616 prepare_cctx ((coro_cctx *)arg); 629 prepare_cctx (aTHX_ (coro_cctx *)arg);
617 630
618 /* somebody will hit me for both perl_run and PL_restartop */ 631 /* somebody or something will hit me for both perl_run and PL_restartop */
619 PL_restartop = PL_op; 632 PL_restartop = PL_op;
620 perl_run (PL_curinterp); 633 perl_run (PL_curinterp);
621 634
622 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); 635 /*
623 abort (); 636 * If perl-run returns we assume exit() was being called or the coro
637 * fell off the end, which seems to be the only valid (non-bug)
638 * reason for perl_run to return. We try to exit by jumping to the
639 * bootstrap-time "top" top_env, as we cannot restore the "main"
640 * coroutine as Coro has no such concept
641 */
642 PL_top_env = main_top_env;
643 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
624} 644}
625 645
626static coro_cctx * 646static coro_cctx *
627cctx_new () 647cctx_new ()
628{ 648{
692 712
693 Safefree (cctx); 713 Safefree (cctx);
694} 714}
695 715
696static coro_cctx * 716static coro_cctx *
697cctx_get () 717cctx_get (pTHX)
698{ 718{
699 while (cctx_first) 719 while (cctx_first)
700 { 720 {
701 coro_cctx *cctx = cctx_first; 721 coro_cctx *cctx = cctx_first;
702 cctx_first = cctx->next; 722 cctx_first = cctx->next;
733 753
734/** coroutine switching *****************************************************/ 754/** coroutine switching *****************************************************/
735 755
736/* never call directly, always through the coro_state_transfer global variable */ 756/* never call directly, always through the coro_state_transfer global variable */
737static void NOINLINE 757static void NOINLINE
738transfer (struct coro *prev, struct coro *next) 758transfer (pTHX_ struct coro *prev, struct coro *next)
739{ 759{
740 dSTACKLEVEL; 760 dSTACKLEVEL;
741 761
742 /* sometimes transfer is only called to set idle_sp */ 762 /* sometimes transfer is only called to set idle_sp */
743 if (!next) 763 if (!next)
776 if (next->flags & CF_NEW) 796 if (next->flags & CF_NEW)
777 { 797 {
778 /* need to start coroutine */ 798 /* need to start coroutine */
779 next->flags &= ~CF_NEW; 799 next->flags &= ~CF_NEW;
780 /* first get rid of the old state */ 800 /* first get rid of the old state */
781 save_perl (prev); 801 save_perl (aTHX_ prev);
782 /* setup coroutine call */ 802 /* setup coroutine call */
783 setup_coro (next); 803 setup_coro (aTHX_ next);
784 /* need a new stack */ 804 /* need a new stack */
785 assert (!next->cctx); 805 assert (!next->cctx);
786 } 806 }
787 else 807 else
788 { 808 {
789 /* coroutine already started */ 809 /* coroutine already started */
790 save_perl (prev); 810 save_perl (aTHX_ prev);
791 load_perl (next); 811 load_perl (aTHX_ next);
792 } 812 }
793 813
794 prev__cctx = prev->cctx; 814 prev__cctx = prev->cctx;
795 815
796 /* possibly "free" the cctx */ 816 /* possibly "free" the cctx */
805 prev__cctx->inuse = 0; 825 prev__cctx->inuse = 0;
806 } 826 }
807 827
808 if (!next->cctx) 828 if (!next->cctx)
809 { 829 {
810 next->cctx = cctx_get (); 830 next->cctx = cctx_get (aTHX);
811 assert (!next->cctx->inuse); 831 assert (!next->cctx->inuse);
812 next->cctx->inuse = 1; 832 next->cctx->inuse = 1;
813 } 833 }
814 834
815 if (prev__cctx != next->cctx) 835 if (prev__cctx != next->cctx)
817 prev__cctx->top_env = PL_top_env; 837 prev__cctx->top_env = PL_top_env;
818 PL_top_env = next->cctx->top_env; 838 PL_top_env = next->cctx->top_env;
819 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 839 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
820 } 840 }
821 841
822 free_coro_mortal (); 842 free_coro_mortal (aTHX);
823 UNLOCK; 843 UNLOCK;
824 } 844 }
825} 845}
826 846
827struct transfer_args 847struct transfer_args
828{ 848{
829 struct coro *prev, *next; 849 struct coro *prev, *next;
830}; 850};
831 851
832#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 852#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next)
833 853
834/** high level stuff ********************************************************/ 854/** high level stuff ********************************************************/
835 855
836static int 856static int
837coro_state_destroy (struct coro *coro) 857coro_state_destroy (pTHX_ struct coro *coro)
838{ 858{
839 if (coro->flags & CF_DESTROYED) 859 if (coro->flags & CF_DESTROYED)
840 return 0; 860 return 0;
841 861
842 coro->flags |= CF_DESTROYED; 862 coro->flags |= CF_DESTROYED;
857 struct coro temp; 877 struct coro temp;
858 878
859 assert (!(coro->flags & CF_RUNNING)); 879 assert (!(coro->flags & CF_RUNNING));
860 880
861 Zero (&temp, 1, struct coro); 881 Zero (&temp, 1, struct coro);
862 temp.save = CORO_SAVE_ALL; 882 temp.save = CORO_SAVE_DEF;
863 883
864 if (coro->flags & CF_RUNNING) 884 if (coro->flags & CF_RUNNING)
865 croak ("FATAL: tried to destroy currently running coroutine"); 885 croak ("FATAL: tried to destroy currently running coroutine");
866 886
867 save_perl (&temp); 887 save_perl (aTHX_ &temp);
868 load_perl (coro); 888 load_perl (aTHX_ coro);
869 889
870 coro_destroy_stacks (); 890 coro_destroy_stacks (aTHX);
871 891
872 load_perl (&temp); /* this will get rid of defsv etc.. */ 892 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */
873 893
874 coro->mainstack = 0; 894 coro->mainstack = 0;
875 } 895 }
876 896
877 cctx_destroy (coro->cctx); 897 cctx_destroy (coro->cctx);
886 struct coro *coro = (struct coro *)mg->mg_ptr; 906 struct coro *coro = (struct coro *)mg->mg_ptr;
887 mg->mg_ptr = 0; 907 mg->mg_ptr = 0;
888 908
889 if (--coro->refcnt < 0) 909 if (--coro->refcnt < 0)
890 { 910 {
891 coro_state_destroy (coro); 911 coro_state_destroy (aTHX_ coro);
892 Safefree (coro); 912 Safefree (coro);
893 } 913 }
894 914
895 return 0; 915 return 0;
896} 916}
915# define MGf_DUP 0 935# define MGf_DUP 0
916#endif 936#endif
917}; 937};
918 938
919static struct coro * 939static struct coro *
920SvSTATE (SV *coro) 940SvSTATE_ (pTHX_ SV *coro)
921{ 941{
922 HV *stash; 942 HV *stash;
923 MAGIC *mg; 943 MAGIC *mg;
924 944
925 if (SvROK (coro)) 945 if (SvROK (coro))
935 955
936 mg = CORO_MAGIC (coro); 956 mg = CORO_MAGIC (coro);
937 return (struct coro *)mg->mg_ptr; 957 return (struct coro *)mg->mg_ptr;
938} 958}
939 959
960#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
961
940static void 962static void
941prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) 963prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv)
942{ 964{
943 ta->prev = SvSTATE (prev_sv); 965 ta->prev = SvSTATE (prev_sv);
944 ta->next = SvSTATE (next_sv); 966 ta->next = SvSTATE (next_sv);
945} 967}
946 968
947static void 969static void
948api_transfer (SV *prev_sv, SV *next_sv) 970api_transfer (SV *prev_sv, SV *next_sv)
949{ 971{
972 dTHX;
950 struct transfer_args ta; 973 struct transfer_args ta;
951 974
952 prepare_transfer (&ta, prev_sv, next_sv); 975 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
953 TRANSFER (ta); 976 TRANSFER (ta);
954} 977}
955 978
956static int 979static int
957api_save (SV *coro_sv, int new_save) 980api_save (SV *coro_sv, int new_save)
958{ 981{
982 dTHX;
959 struct coro *coro = SvSTATE (coro_sv); 983 struct coro *coro = SvSTATE (coro_sv);
960 int old_save = coro->save; 984 int old_save = coro->save;
961 985
962 if (new_save >= 0) 986 if (new_save >= 0)
963 coro->save = new_save; 987 coro->save = new_save;
966} 990}
967 991
968/** Coro ********************************************************************/ 992/** Coro ********************************************************************/
969 993
970static void 994static void
971coro_enq (SV *coro_sv) 995coro_enq (pTHX_ SV *coro_sv)
972{ 996{
973 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 997 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
974} 998}
975 999
976static SV * 1000static SV *
977coro_deq (int min_prio) 1001coro_deq (pTHX_ int min_prio)
978{ 1002{
979 int prio = PRIO_MAX - PRIO_MIN; 1003 int prio = PRIO_MAX - PRIO_MIN;
980 1004
981 min_prio -= PRIO_MIN; 1005 min_prio -= PRIO_MIN;
982 if (min_prio < 0) 1006 if (min_prio < 0)
990} 1014}
991 1015
992static int 1016static int
993api_ready (SV *coro_sv) 1017api_ready (SV *coro_sv)
994{ 1018{
1019 dTHX;
995 struct coro *coro; 1020 struct coro *coro;
996 1021
997 if (SvROK (coro_sv)) 1022 if (SvROK (coro_sv))
998 coro_sv = SvRV (coro_sv); 1023 coro_sv = SvRV (coro_sv);
999 1024
1003 return 0; 1028 return 0;
1004 1029
1005 coro->flags |= CF_READY; 1030 coro->flags |= CF_READY;
1006 1031
1007 LOCK; 1032 LOCK;
1008 coro_enq (SvREFCNT_inc (coro_sv)); 1033 coro_enq (aTHX_ SvREFCNT_inc (coro_sv));
1009 ++coro_nready; 1034 ++coro_nready;
1010 UNLOCK; 1035 UNLOCK;
1011 1036
1012 return 1; 1037 return 1;
1013} 1038}
1014 1039
1015static int 1040static int
1016api_is_ready (SV *coro_sv) 1041api_is_ready (SV *coro_sv)
1017{ 1042{
1043 dTHX;
1018 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1044 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1019} 1045}
1020 1046
1021static void 1047static void
1022prepare_schedule (struct transfer_args *ta) 1048prepare_schedule (pTHX_ struct transfer_args *ta)
1023{ 1049{
1024 SV *prev_sv, *next_sv; 1050 SV *prev_sv, *next_sv;
1025 1051
1026 for (;;) 1052 for (;;)
1027 { 1053 {
1028 LOCK; 1054 LOCK;
1029 next_sv = coro_deq (PRIO_MIN); 1055 next_sv = coro_deq (aTHX_ PRIO_MIN);
1030 1056
1031 /* nothing to schedule: call the idle handler */ 1057 /* nothing to schedule: call the idle handler */
1032 if (!next_sv) 1058 if (!next_sv)
1033 { 1059 {
1034 dSP; 1060 dSP;
1069 1095
1070 assert (ta->next->flags & CF_READY); 1096 assert (ta->next->flags & CF_READY);
1071 ta->next->flags &= ~CF_READY; 1097 ta->next->flags &= ~CF_READY;
1072 1098
1073 LOCK; 1099 LOCK;
1074 free_coro_mortal (); 1100 free_coro_mortal (aTHX);
1075 coro_mortal = prev_sv; 1101 coro_mortal = prev_sv;
1076 UNLOCK; 1102 UNLOCK;
1077} 1103}
1078 1104
1079static void 1105static void
1080prepare_cede (struct transfer_args *ta) 1106prepare_cede (pTHX_ struct transfer_args *ta)
1081{ 1107{
1082 api_ready (coro_current); 1108 api_ready (coro_current);
1083 prepare_schedule (ta); 1109 prepare_schedule (aTHX_ ta);
1084} 1110}
1085 1111
1086static int 1112static int
1087prepare_cede_notself (struct transfer_args *ta) 1113prepare_cede_notself (pTHX_ struct transfer_args *ta)
1088{ 1114{
1089 if (coro_nready) 1115 if (coro_nready)
1090 { 1116 {
1091 SV *prev = SvRV (coro_current); 1117 SV *prev = SvRV (coro_current);
1092 prepare_schedule (ta); 1118 prepare_schedule (aTHX_ ta);
1093 api_ready (prev); 1119 api_ready (prev);
1094 return 1; 1120 return 1;
1095 } 1121 }
1096 else 1122 else
1097 return 0; 1123 return 0;
1098} 1124}
1099 1125
1100static void 1126static void
1101api_schedule (void) 1127api_schedule (void)
1102{ 1128{
1129 dTHX;
1103 struct transfer_args ta; 1130 struct transfer_args ta;
1104 1131
1105 prepare_schedule (&ta); 1132 prepare_schedule (aTHX_ &ta);
1106 TRANSFER (ta); 1133 TRANSFER (ta);
1107} 1134}
1108 1135
1109static int 1136static int
1110api_cede (void) 1137api_cede (void)
1111{ 1138{
1139 dTHX;
1112 struct transfer_args ta; 1140 struct transfer_args ta;
1113 1141
1114 prepare_cede (&ta); 1142 prepare_cede (aTHX_ &ta);
1115 1143
1116 if (ta.prev != ta.next) 1144 if (ta.prev != ta.next)
1117 { 1145 {
1118 TRANSFER (ta); 1146 TRANSFER (ta);
1119 return 1; 1147 return 1;
1123} 1151}
1124 1152
1125static int 1153static int
1126api_cede_notself (void) 1154api_cede_notself (void)
1127{ 1155{
1156 dTHX;
1128 struct transfer_args ta; 1157 struct transfer_args ta;
1129 1158
1130 if (prepare_cede_notself (&ta)) 1159 if (prepare_cede_notself (aTHX_ &ta))
1131 { 1160 {
1132 TRANSFER (ta); 1161 TRANSFER (ta);
1133 return 1; 1162 return 1;
1134 } 1163 }
1135 else 1164 else
1151 1180
1152 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1181 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1153 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1182 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1154 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1183 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1155 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); 1184 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1185 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH));
1186 newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF));
1156 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); 1187 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1157 1188
1158 main_mainstack = PL_mainstack; 1189 main_mainstack = PL_mainstack;
1190 main_top_env = PL_top_env;
1191
1192 while (main_top_env->je_prev)
1193 main_top_env = main_top_env->je_prev;
1159 1194
1160 coroapi.ver = CORO_API_VERSION; 1195 coroapi.ver = CORO_API_VERSION;
1161 coroapi.transfer = api_transfer; 1196 coroapi.transfer = api_transfer;
1162 1197
1163 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1198 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1171 HV *hv; 1206 HV *hv;
1172 int i; 1207 int i;
1173 1208
1174 Newz (0, coro, 1, struct coro); 1209 Newz (0, coro, 1, struct coro);
1175 coro->args = newAV (); 1210 coro->args = newAV ();
1176 coro->save = CORO_SAVE_ALL; 1211 coro->save = CORO_SAVE_DEF;
1177 coro->flags = CF_NEW; 1212 coro->flags = CF_NEW;
1178 1213
1179 hv = newHV (); 1214 hv = newHV ();
1180 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1215 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1181 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1216 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1188 1223
1189int 1224int
1190save (SV *coro, int new_save = -1) 1225save (SV *coro, int new_save = -1)
1191 CODE: 1226 CODE:
1192 RETVAL = api_save (coro, new_save); 1227 RETVAL = api_save (coro, new_save);
1228 OUTPUT:
1229 RETVAL
1230
1231int
1232save_also (SV *coro_sv, int save_also)
1233 CODE:
1234{
1235 struct coro *coro = SvSTATE (coro_sv);
1236 RETVAL = coro->save;
1237 coro->save |= save_also;
1238}
1193 OUTPUT: 1239 OUTPUT:
1194 RETVAL 1240 RETVAL
1195 1241
1196void 1242void
1197_set_stacklevel (...) 1243_set_stacklevel (...)
1213 1259
1214 case 1: 1260 case 1:
1215 if (items != 2) 1261 if (items != 2)
1216 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); 1262 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1217 1263
1218 prepare_transfer (&ta, ST (0), ST (1)); 1264 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1219 break; 1265 break;
1220 1266
1221 case 2: 1267 case 2:
1222 prepare_schedule (&ta); 1268 prepare_schedule (aTHX_ &ta);
1223 break; 1269 break;
1224 1270
1225 case 3: 1271 case 3:
1226 prepare_cede (&ta); 1272 prepare_cede (aTHX_ &ta);
1227 break; 1273 break;
1228 1274
1229 case 4: 1275 case 4:
1230 if (!prepare_cede_notself (&ta)) 1276 if (!prepare_cede_notself (aTHX_ &ta))
1231 XSRETURN_EMPTY; 1277 XSRETURN_EMPTY;
1232 1278
1233 break; 1279 break;
1234 } 1280 }
1235 1281
1241} 1287}
1242 1288
1243bool 1289bool
1244_destroy (SV *coro_sv) 1290_destroy (SV *coro_sv)
1245 CODE: 1291 CODE:
1246 RETVAL = coro_state_destroy (SvSTATE (coro_sv)); 1292 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1247 OUTPUT: 1293 OUTPUT:
1248 RETVAL 1294 RETVAL
1249 1295
1250void 1296void
1251_exit (code) 1297_exit (code)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines