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.147 by root, Mon Mar 19 15:50: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
175 177
176 /* optionally saved, might be zero */ 178 /* optionally saved, might be zero */
177 AV *defav; /* @_ */ 179 AV *defav; /* @_ */
178 SV *defsv; /* $_ */ 180 SV *defsv; /* $_ */
179 SV *errsv; /* $@ */ 181 SV *errsv; /* $@ */
182 GV *deffh; /* default filehandle */
180 SV *irssv; /* $/ */ 183 SV *irssv; /* $/ */
181 SV *irssv_sv; /* real $/ cache */ 184 SV *irssv_sv; /* real $/ cache */
182 185
183#define VAR(name,type) type name; 186#define VAR(name,type) type name;
184# include "state.h" 187# include "state.h"
206static int coro_nready; 209static int coro_nready;
207 210
208/** lowlevel stuff **********************************************************/ 211/** lowlevel stuff **********************************************************/
209 212
210static AV * 213static AV *
211coro_clone_padlist (CV *cv) 214coro_clone_padlist (pTHX_ CV *cv)
212{ 215{
213 AV *padlist = CvPADLIST (cv); 216 AV *padlist = CvPADLIST (cv);
214 AV *newpadlist, *newpad; 217 AV *newpadlist, *newpad;
215 218
216 newpadlist = newAV (); 219 newpadlist = newAV ();
228 231
229 return newpadlist; 232 return newpadlist;
230} 233}
231 234
232static void 235static void
233free_padlist (AV *padlist) 236free_padlist (pTHX_ AV *padlist)
234{ 237{
235 /* may be during global destruction */ 238 /* may be during global destruction */
236 if (SvREFCNT (padlist)) 239 if (SvREFCNT (padlist))
237 { 240 {
238 I32 i = AvFILLp (padlist); 241 I32 i = AvFILLp (padlist);
259 AV *padlist; 262 AV *padlist;
260 AV *av = (AV *)mg->mg_obj; 263 AV *av = (AV *)mg->mg_obj;
261 264
262 /* casting is fun. */ 265 /* casting is fun. */
263 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 266 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
264 free_padlist (padlist); 267 free_padlist (aTHX_ padlist);
265 268
266 SvREFCNT_dec (av); 269 SvREFCNT_dec (av);
267 270
268 return 0; 271 return 0;
269} 272}
279 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 282 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
280 : 0 283 : 0
281 284
282/* the next two functions merely cache the padlists */ 285/* the next two functions merely cache the padlists */
283static void 286static void
284get_padlist (CV *cv) 287get_padlist (pTHX_ CV *cv)
285{ 288{
286 MAGIC *mg = CORO_MAGIC (cv); 289 MAGIC *mg = CORO_MAGIC (cv);
287 AV *av; 290 AV *av;
288 291
289 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) 292 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
295 CV *cp = Perl_cv_clone (cv); 298 CV *cp = Perl_cv_clone (cv);
296 CvPADLIST (cv) = CvPADLIST (cp); 299 CvPADLIST (cv) = CvPADLIST (cp);
297 CvPADLIST (cp) = 0; 300 CvPADLIST (cp) = 0;
298 SvREFCNT_dec (cp); 301 SvREFCNT_dec (cp);
299#else 302#else
300 CvPADLIST (cv) = coro_clone_padlist (cv); 303 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv);
301#endif 304#endif
302 } 305 }
303} 306}
304 307
305static void 308static void
306put_padlist (CV *cv) 309put_padlist (pTHX_ CV *cv)
307{ 310{
308 MAGIC *mg = CORO_MAGIC (cv); 311 MAGIC *mg = CORO_MAGIC (cv);
309 AV *av; 312 AV *av;
310 313
311 if (!mg) 314 if (!mg)
330#define SE } while (0) 333#define SE } while (0)
331 334
332#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE 335#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
333 336
334static void 337static void
335load_perl (Coro__State c) 338load_perl (pTHX_ Coro__State c)
336{ 339{
337#define VAR(name,type) PL_ ## name = c->name; 340#define VAR(name,type) PL_ ## name = c->name;
338# include "state.h" 341# include "state.h"
339#undef VAR 342#undef VAR
340 343
341 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 344 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
342 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 345 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
343 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 346 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
347 if (c->deffh) REPLACE_SV (PL_defoutgv , c->deffh);
348
344 if (c->irssv) 349 if (c->irssv)
345 { 350 {
346 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv)) 351 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
352 {
347 SvREFCNT_dec (c->irssv); 353 SvREFCNT_dec (c->irssv);
354 c->irssv = 0;
355 }
348 else 356 else
349 { 357 {
350 REPLACE_SV (PL_rs, c->irssv); 358 REPLACE_SV (PL_rs, c->irssv);
351 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0); 359 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
352 sv_setsv (c->irssv_sv, PL_rs); 360 sv_setsv (c->irssv_sv, PL_rs);
358 CV *cv; 366 CV *cv;
359 367
360 /* now do the ugly restore mess */ 368 /* now do the ugly restore mess */
361 while ((cv = (CV *)POPs)) 369 while ((cv = (CV *)POPs))
362 { 370 {
363 put_padlist (cv); /* mark this padlist as available */ 371 put_padlist (aTHX_ cv); /* mark this padlist as available */
364 CvDEPTH (cv) = PTR2IV (POPs); 372 CvDEPTH (cv) = PTR2IV (POPs);
365 CvPADLIST (cv) = (AV *)POPs; 373 CvPADLIST (cv) = (AV *)POPs;
366 } 374 }
367 375
368 PUTBACK; 376 PUTBACK;
369 } 377 }
370 assert (!PL_comppad || AvARRAY (PL_comppad));//D 378 assert (!PL_comppad || AvARRAY (PL_comppad));//D
371} 379}
372 380
373static void 381static void
374save_perl (Coro__State c) 382save_perl (pTHX_ Coro__State c)
375{ 383{
376 assert (!PL_comppad || AvARRAY (PL_comppad));//D 384 assert (!PL_comppad || AvARRAY (PL_comppad));//D
377 { 385 {
378 dSP; 386 dSP;
379 I32 cxix = cxstack_ix; 387 I32 cxix = cxstack_ix;
404 PUSHs ((SV *)CvPADLIST (cv)); 412 PUSHs ((SV *)CvPADLIST (cv));
405 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 413 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
406 PUSHs ((SV *)cv); 414 PUSHs ((SV *)cv);
407 415
408 CvDEPTH (cv) = 0; 416 CvDEPTH (cv) = 0;
409 get_padlist (cv); 417 get_padlist (aTHX_ cv);
410 } 418 }
411 } 419 }
412 } 420 }
413 421
414 if (top_si->si_type == PERLSI_MAIN) 422 if (top_si->si_type == PERLSI_MAIN)
423 } 431 }
424 432
425 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 433 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; 434 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
427 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 435 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
436 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; 437 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0;
429 438
430#define VAR(name,type)c->name = PL_ ## name; 439#define VAR(name,type)c->name = PL_ ## name;
431# include "state.h" 440# include "state.h"
432#undef VAR 441#undef VAR
440 */ 449 */
441#if CORO_PREFER_PERL_FUNCTIONS 450#if CORO_PREFER_PERL_FUNCTIONS
442# define coro_init_stacks init_stacks 451# define coro_init_stacks init_stacks
443#else 452#else
444static void 453static void
445coro_init_stacks () 454coro_init_stacks (pTHX)
446{ 455{
447 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 456 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
448 PL_curstackinfo->si_type = PERLSI_MAIN; 457 PL_curstackinfo->si_type = PERLSI_MAIN;
449 PL_curstack = PL_curstackinfo->si_stack; 458 PL_curstack = PL_curstackinfo->si_stack;
450 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 459 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
484 493
485/* 494/*
486 * destroy the stacks, the callchain etc... 495 * destroy the stacks, the callchain etc...
487 */ 496 */
488static void 497static void
489coro_destroy_stacks () 498coro_destroy_stacks (pTHX)
490{ 499{
491 if (!IN_DESTRUCT) 500 if (!IN_DESTRUCT)
492 { 501 {
493 /* restore all saved variables and stuff */ 502 /* restore all saved variables and stuff */
494 LEAVE_SCOPE (0); 503 LEAVE_SCOPE (0);
530} 539}
531 540
532/** coroutine stack handling ************************************************/ 541/** coroutine stack handling ************************************************/
533 542
534static void 543static void
535setup_coro (struct coro *coro) 544setup_coro (pTHX_ struct coro *coro)
536{ 545{
537 /* 546 /*
538 * emulate part of the perl startup here. 547 * emulate part of the perl startup here.
539 */ 548 */
540
541 coro_init_stacks (); 549 coro_init_stacks (aTHX);
542 550
543 PL_curcop = &PL_compiling; 551 PL_curcop = &PL_compiling;
544 PL_in_eval = EVAL_NULL; 552 PL_in_eval = EVAL_NULL;
545 PL_comppad = 0; 553 PL_comppad = 0;
546 PL_curpm = 0; 554 PL_curpm = 0;
569 577
570 ENTER; /* necessary e.g. for dounwind */ 578 ENTER; /* necessary e.g. for dounwind */
571} 579}
572 580
573static void 581static void
574free_coro_mortal () 582free_coro_mortal (pTHX)
575{ 583{
576 if (coro_mortal) 584 if (coro_mortal)
577 { 585 {
578 SvREFCNT_dec (coro_mortal); 586 SvREFCNT_dec (coro_mortal);
579 coro_mortal = 0; 587 coro_mortal = 0;
580 } 588 }
581} 589}
582 590
583/* inject a fake call to Coro::State::_cctx_init into the execution */ 591/* inject a fake call to Coro::State::_cctx_init into the execution */
584static void NOINLINE 592static void NOINLINE
585prepare_cctx (coro_cctx *cctx) 593prepare_cctx (pTHX_ coro_cctx *cctx)
586{ 594{
587 dSP; 595 dSP;
588 LOGOP myop; 596 LOGOP myop;
589 597
590 Zero (&myop, 1, LOGOP); 598 Zero (&myop, 1, LOGOP);
602} 610}
603 611
604static void 612static void
605coro_run (void *arg) 613coro_run (void *arg)
606{ 614{
615 dTHX;
616
607 /* coro_run is the alternative tail of transfer(), so unlock here. */ 617 /* coro_run is the alternative tail of transfer(), so unlock here. */
608 UNLOCK; 618 UNLOCK;
609 619
610 /* 620 /*
611 * this is a _very_ stripped down perl interpreter ;) 621 * this is a _very_ stripped down perl interpreter ;)
612 */ 622 */
613 PL_top_env = &PL_start_env; 623 PL_top_env = &PL_start_env;
614 624
615 /* inject call to cctx_init */ 625 /* inject call to cctx_init */
616 prepare_cctx ((coro_cctx *)arg); 626 prepare_cctx (aTHX_ (coro_cctx *)arg);
617 627
618 /* somebody will hit me for both perl_run and PL_restartop */ 628 /* somebody will hit me for both perl_run and PL_restartop */
619 PL_restartop = PL_op; 629 PL_restartop = PL_op;
620 perl_run (PL_curinterp); 630 perl_run (PL_curinterp);
621 631
692 702
693 Safefree (cctx); 703 Safefree (cctx);
694} 704}
695 705
696static coro_cctx * 706static coro_cctx *
697cctx_get () 707cctx_get (pTHX)
698{ 708{
699 while (cctx_first) 709 while (cctx_first)
700 { 710 {
701 coro_cctx *cctx = cctx_first; 711 coro_cctx *cctx = cctx_first;
702 cctx_first = cctx->next; 712 cctx_first = cctx->next;
733 743
734/** coroutine switching *****************************************************/ 744/** coroutine switching *****************************************************/
735 745
736/* never call directly, always through the coro_state_transfer global variable */ 746/* never call directly, always through the coro_state_transfer global variable */
737static void NOINLINE 747static void NOINLINE
738transfer (struct coro *prev, struct coro *next) 748transfer (pTHX_ struct coro *prev, struct coro *next)
739{ 749{
740 dSTACKLEVEL; 750 dSTACKLEVEL;
741 751
742 /* sometimes transfer is only called to set idle_sp */ 752 /* sometimes transfer is only called to set idle_sp */
743 if (!next) 753 if (!next)
776 if (next->flags & CF_NEW) 786 if (next->flags & CF_NEW)
777 { 787 {
778 /* need to start coroutine */ 788 /* need to start coroutine */
779 next->flags &= ~CF_NEW; 789 next->flags &= ~CF_NEW;
780 /* first get rid of the old state */ 790 /* first get rid of the old state */
781 save_perl (prev); 791 save_perl (aTHX_ prev);
782 /* setup coroutine call */ 792 /* setup coroutine call */
783 setup_coro (next); 793 setup_coro (aTHX_ next);
784 /* need a new stack */ 794 /* need a new stack */
785 assert (!next->cctx); 795 assert (!next->cctx);
786 } 796 }
787 else 797 else
788 { 798 {
789 /* coroutine already started */ 799 /* coroutine already started */
790 save_perl (prev); 800 save_perl (aTHX_ prev);
791 load_perl (next); 801 load_perl (aTHX_ next);
792 } 802 }
793 803
794 prev__cctx = prev->cctx; 804 prev__cctx = prev->cctx;
795 805
796 /* possibly "free" the cctx */ 806 /* possibly "free" the cctx */
805 prev__cctx->inuse = 0; 815 prev__cctx->inuse = 0;
806 } 816 }
807 817
808 if (!next->cctx) 818 if (!next->cctx)
809 { 819 {
810 next->cctx = cctx_get (); 820 next->cctx = cctx_get (aTHX);
811 assert (!next->cctx->inuse); 821 assert (!next->cctx->inuse);
812 next->cctx->inuse = 1; 822 next->cctx->inuse = 1;
813 } 823 }
814 824
815 if (prev__cctx != next->cctx) 825 if (prev__cctx != next->cctx)
817 prev__cctx->top_env = PL_top_env; 827 prev__cctx->top_env = PL_top_env;
818 PL_top_env = next->cctx->top_env; 828 PL_top_env = next->cctx->top_env;
819 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 829 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
820 } 830 }
821 831
822 free_coro_mortal (); 832 free_coro_mortal (aTHX);
823 UNLOCK; 833 UNLOCK;
824 } 834 }
825} 835}
826 836
827struct transfer_args 837struct transfer_args
828{ 838{
829 struct coro *prev, *next; 839 struct coro *prev, *next;
830}; 840};
831 841
832#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 842#define TRANSFER(ta) transfer (aTHX_ (ta).prev, (ta).next)
833 843
834/** high level stuff ********************************************************/ 844/** high level stuff ********************************************************/
835 845
836static int 846static int
837coro_state_destroy (struct coro *coro) 847coro_state_destroy (pTHX_ struct coro *coro)
838{ 848{
839 if (coro->flags & CF_DESTROYED) 849 if (coro->flags & CF_DESTROYED)
840 return 0; 850 return 0;
841 851
842 coro->flags |= CF_DESTROYED; 852 coro->flags |= CF_DESTROYED;
857 struct coro temp; 867 struct coro temp;
858 868
859 assert (!(coro->flags & CF_RUNNING)); 869 assert (!(coro->flags & CF_RUNNING));
860 870
861 Zero (&temp, 1, struct coro); 871 Zero (&temp, 1, struct coro);
862 temp.save = CORO_SAVE_ALL; 872 temp.save = CORO_SAVE_DEF;
863 873
864 if (coro->flags & CF_RUNNING) 874 if (coro->flags & CF_RUNNING)
865 croak ("FATAL: tried to destroy currently running coroutine"); 875 croak ("FATAL: tried to destroy currently running coroutine");
866 876
867 save_perl (&temp); 877 save_perl (aTHX_ &temp);
868 load_perl (coro); 878 load_perl (aTHX_ coro);
869 879
870 coro_destroy_stacks (); 880 coro_destroy_stacks (aTHX);
871 881
872 load_perl (&temp); /* this will get rid of defsv etc.. */ 882 load_perl (aTHX_ &temp); /* this will get rid of defsv etc.. */
873 883
874 coro->mainstack = 0; 884 coro->mainstack = 0;
875 } 885 }
876 886
877 cctx_destroy (coro->cctx); 887 cctx_destroy (coro->cctx);
886 struct coro *coro = (struct coro *)mg->mg_ptr; 896 struct coro *coro = (struct coro *)mg->mg_ptr;
887 mg->mg_ptr = 0; 897 mg->mg_ptr = 0;
888 898
889 if (--coro->refcnt < 0) 899 if (--coro->refcnt < 0)
890 { 900 {
891 coro_state_destroy (coro); 901 coro_state_destroy (aTHX_ coro);
892 Safefree (coro); 902 Safefree (coro);
893 } 903 }
894 904
895 return 0; 905 return 0;
896} 906}
915# define MGf_DUP 0 925# define MGf_DUP 0
916#endif 926#endif
917}; 927};
918 928
919static struct coro * 929static struct coro *
920SvSTATE (SV *coro) 930SvSTATE_ (pTHX_ SV *coro)
921{ 931{
922 HV *stash; 932 HV *stash;
923 MAGIC *mg; 933 MAGIC *mg;
924 934
925 if (SvROK (coro)) 935 if (SvROK (coro))
935 945
936 mg = CORO_MAGIC (coro); 946 mg = CORO_MAGIC (coro);
937 return (struct coro *)mg->mg_ptr; 947 return (struct coro *)mg->mg_ptr;
938} 948}
939 949
950#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
951
940static void 952static void
941prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) 953prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv)
942{ 954{
943 ta->prev = SvSTATE (prev_sv); 955 ta->prev = SvSTATE (prev_sv);
944 ta->next = SvSTATE (next_sv); 956 ta->next = SvSTATE (next_sv);
945} 957}
946 958
947static void 959static void
948api_transfer (SV *prev_sv, SV *next_sv) 960api_transfer (SV *prev_sv, SV *next_sv)
949{ 961{
962 dTHX;
950 struct transfer_args ta; 963 struct transfer_args ta;
951 964
952 prepare_transfer (&ta, prev_sv, next_sv); 965 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
953 TRANSFER (ta); 966 TRANSFER (ta);
954} 967}
955 968
956static int 969static int
957api_save (SV *coro_sv, int new_save) 970api_save (SV *coro_sv, int new_save)
958{ 971{
972 dTHX;
959 struct coro *coro = SvSTATE (coro_sv); 973 struct coro *coro = SvSTATE (coro_sv);
960 int old_save = coro->save; 974 int old_save = coro->save;
961 975
962 if (new_save >= 0) 976 if (new_save >= 0)
963 coro->save = new_save; 977 coro->save = new_save;
966} 980}
967 981
968/** Coro ********************************************************************/ 982/** Coro ********************************************************************/
969 983
970static void 984static void
971coro_enq (SV *coro_sv) 985coro_enq (pTHX_ SV *coro_sv)
972{ 986{
973 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 987 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
974} 988}
975 989
976static SV * 990static SV *
977coro_deq (int min_prio) 991coro_deq (pTHX_ int min_prio)
978{ 992{
979 int prio = PRIO_MAX - PRIO_MIN; 993 int prio = PRIO_MAX - PRIO_MIN;
980 994
981 min_prio -= PRIO_MIN; 995 min_prio -= PRIO_MIN;
982 if (min_prio < 0) 996 if (min_prio < 0)
990} 1004}
991 1005
992static int 1006static int
993api_ready (SV *coro_sv) 1007api_ready (SV *coro_sv)
994{ 1008{
1009 dTHX;
995 struct coro *coro; 1010 struct coro *coro;
996 1011
997 if (SvROK (coro_sv)) 1012 if (SvROK (coro_sv))
998 coro_sv = SvRV (coro_sv); 1013 coro_sv = SvRV (coro_sv);
999 1014
1003 return 0; 1018 return 0;
1004 1019
1005 coro->flags |= CF_READY; 1020 coro->flags |= CF_READY;
1006 1021
1007 LOCK; 1022 LOCK;
1008 coro_enq (SvREFCNT_inc (coro_sv)); 1023 coro_enq (aTHX_ SvREFCNT_inc (coro_sv));
1009 ++coro_nready; 1024 ++coro_nready;
1010 UNLOCK; 1025 UNLOCK;
1011 1026
1012 return 1; 1027 return 1;
1013} 1028}
1014 1029
1015static int 1030static int
1016api_is_ready (SV *coro_sv) 1031api_is_ready (SV *coro_sv)
1017{ 1032{
1033 dTHX;
1018 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1034 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1019} 1035}
1020 1036
1021static void 1037static void
1022prepare_schedule (struct transfer_args *ta) 1038prepare_schedule (pTHX_ struct transfer_args *ta)
1023{ 1039{
1024 SV *prev_sv, *next_sv; 1040 SV *prev_sv, *next_sv;
1025 1041
1026 for (;;) 1042 for (;;)
1027 { 1043 {
1028 LOCK; 1044 LOCK;
1029 next_sv = coro_deq (PRIO_MIN); 1045 next_sv = coro_deq (aTHX_ PRIO_MIN);
1030 1046
1031 /* nothing to schedule: call the idle handler */ 1047 /* nothing to schedule: call the idle handler */
1032 if (!next_sv) 1048 if (!next_sv)
1033 { 1049 {
1034 dSP; 1050 dSP;
1069 1085
1070 assert (ta->next->flags & CF_READY); 1086 assert (ta->next->flags & CF_READY);
1071 ta->next->flags &= ~CF_READY; 1087 ta->next->flags &= ~CF_READY;
1072 1088
1073 LOCK; 1089 LOCK;
1074 free_coro_mortal (); 1090 free_coro_mortal (aTHX);
1075 coro_mortal = prev_sv; 1091 coro_mortal = prev_sv;
1076 UNLOCK; 1092 UNLOCK;
1077} 1093}
1078 1094
1079static void 1095static void
1080prepare_cede (struct transfer_args *ta) 1096prepare_cede (pTHX_ struct transfer_args *ta)
1081{ 1097{
1082 api_ready (coro_current); 1098 api_ready (coro_current);
1083 prepare_schedule (ta); 1099 prepare_schedule (aTHX_ ta);
1084} 1100}
1085 1101
1086static int 1102static int
1087prepare_cede_notself (struct transfer_args *ta) 1103prepare_cede_notself (pTHX_ struct transfer_args *ta)
1088{ 1104{
1089 if (coro_nready) 1105 if (coro_nready)
1090 { 1106 {
1091 SV *prev = SvRV (coro_current); 1107 SV *prev = SvRV (coro_current);
1092 prepare_schedule (ta); 1108 prepare_schedule (aTHX_ ta);
1093 api_ready (prev); 1109 api_ready (prev);
1094 return 1; 1110 return 1;
1095 } 1111 }
1096 else 1112 else
1097 return 0; 1113 return 0;
1098} 1114}
1099 1115
1100static void 1116static void
1101api_schedule (void) 1117api_schedule (void)
1102{ 1118{
1119 dTHX;
1103 struct transfer_args ta; 1120 struct transfer_args ta;
1104 1121
1105 prepare_schedule (&ta); 1122 prepare_schedule (aTHX_ &ta);
1106 TRANSFER (ta); 1123 TRANSFER (ta);
1107} 1124}
1108 1125
1109static int 1126static int
1110api_cede (void) 1127api_cede (void)
1111{ 1128{
1129 dTHX;
1112 struct transfer_args ta; 1130 struct transfer_args ta;
1113 1131
1114 prepare_cede (&ta); 1132 prepare_cede (aTHX_ &ta);
1115 1133
1116 if (ta.prev != ta.next) 1134 if (ta.prev != ta.next)
1117 { 1135 {
1118 TRANSFER (ta); 1136 TRANSFER (ta);
1119 return 1; 1137 return 1;
1123} 1141}
1124 1142
1125static int 1143static int
1126api_cede_notself (void) 1144api_cede_notself (void)
1127{ 1145{
1146 dTHX;
1128 struct transfer_args ta; 1147 struct transfer_args ta;
1129 1148
1130 if (prepare_cede_notself (&ta)) 1149 if (prepare_cede_notself (aTHX_ &ta))
1131 { 1150 {
1132 TRANSFER (ta); 1151 TRANSFER (ta);
1133 return 1; 1152 return 1;
1134 } 1153 }
1135 else 1154 else
1151 1170
1152 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1171 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1153 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1172 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1154 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1173 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1155 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); 1174 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1175 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH));
1176 newCONSTSUB (coro_state_stash, "SAVE_DEF", newSViv (CORO_SAVE_DEF));
1156 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL)); 1177 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1157 1178
1158 main_mainstack = PL_mainstack; 1179 main_mainstack = PL_mainstack;
1159 1180
1160 coroapi.ver = CORO_API_VERSION; 1181 coroapi.ver = CORO_API_VERSION;
1171 HV *hv; 1192 HV *hv;
1172 int i; 1193 int i;
1173 1194
1174 Newz (0, coro, 1, struct coro); 1195 Newz (0, coro, 1, struct coro);
1175 coro->args = newAV (); 1196 coro->args = newAV ();
1176 coro->save = CORO_SAVE_ALL; 1197 coro->save = CORO_SAVE_DEF;
1177 coro->flags = CF_NEW; 1198 coro->flags = CF_NEW;
1178 1199
1179 hv = newHV (); 1200 hv = newHV ();
1180 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1201 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)); 1202 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1188 1209
1189int 1210int
1190save (SV *coro, int new_save = -1) 1211save (SV *coro, int new_save = -1)
1191 CODE: 1212 CODE:
1192 RETVAL = api_save (coro, new_save); 1213 RETVAL = api_save (coro, new_save);
1214 OUTPUT:
1215 RETVAL
1216
1217int
1218save_also (SV *coro_sv, int save_also)
1219 CODE:
1220{
1221 struct coro *coro = SvSTATE (coro_sv);
1222 RETVAL = coro->save;
1223 coro->save |= save_also;
1224}
1193 OUTPUT: 1225 OUTPUT:
1194 RETVAL 1226 RETVAL
1195 1227
1196void 1228void
1197_set_stacklevel (...) 1229_set_stacklevel (...)
1213 1245
1214 case 1: 1246 case 1:
1215 if (items != 2) 1247 if (items != 2)
1216 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); 1248 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1217 1249
1218 prepare_transfer (&ta, ST (0), ST (1)); 1250 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1219 break; 1251 break;
1220 1252
1221 case 2: 1253 case 2:
1222 prepare_schedule (&ta); 1254 prepare_schedule (aTHX_ &ta);
1223 break; 1255 break;
1224 1256
1225 case 3: 1257 case 3:
1226 prepare_cede (&ta); 1258 prepare_cede (aTHX_ &ta);
1227 break; 1259 break;
1228 1260
1229 case 4: 1261 case 4:
1230 if (!prepare_cede_notself (&ta)) 1262 if (!prepare_cede_notself (aTHX_ &ta))
1231 XSRETURN_EMPTY; 1263 XSRETURN_EMPTY;
1232 1264
1233 break; 1265 break;
1234 } 1266 }
1235 1267
1241} 1273}
1242 1274
1243bool 1275bool
1244_destroy (SV *coro_sv) 1276_destroy (SV *coro_sv)
1245 CODE: 1277 CODE:
1246 RETVAL = coro_state_destroy (SvSTATE (coro_sv)); 1278 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1247 OUTPUT: 1279 OUTPUT:
1248 RETVAL 1280 RETVAL
1249 1281
1250void 1282void
1251_exit (code) 1283_exit (code)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines