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.25 by root, Wed Aug 15 03:24:07 2001 UTC vs.
Revision 1.33 by root, Sun Aug 26 21:44:20 2001 UTC

16# undef HAVE_MMAP 16# undef HAVE_MMAP
17# endif 17# endif
18# endif 18# endif
19#endif 19#endif
20 20
21#define MAY_FLUSH /* increases codesize */ 21#define MAY_FLUSH /* increases codesize and is rarely used */
22 22
23#define SUB_INIT "Coro::State::initialize" 23#define SUB_INIT "Coro::State::initialize"
24#define UCORO_STATE "_coro_state" 24#define UCORO_STATE "_coro_state"
25 25
26/* The next macro should delcare a variable stacklevel that contains and approximation 26/* The next macro should declare a variable stacklevel that contains and approximation
27 * to the current C stack pointer. Its property is that it changes with each call 27 * to the current C stack pointer. Its property is that it changes with each call
28 * and should be unique. */ 28 * and should be unique. */
29#define dSTACKLEVEL void *stacklevel = &stacklevel 29#define dSTACKLEVEL void *stacklevel = &stacklevel
30 30
31#define labs(l) ((l) >= 0 ? (l) : -(l)) 31#define labs(l) ((l) >= 0 ? (l) : -(l))
195 195
196 return newpadlist; 196 return newpadlist;
197} 197}
198 198
199#ifdef MAY_FLUSH 199#ifdef MAY_FLUSH
200STATIC AV * 200STATIC void
201free_padlist (AV *padlist) 201free_padlist (AV *padlist)
202{ 202{
203 /* may be during global destruction */ 203 /* may be during global destruction */
204 if (SvREFCNT(padlist)) 204 if (SvREFCNT(padlist))
205 { 205 {
270#endif 270#endif
271 271
272#define SB do { 272#define SB do {
273#define SE } while (0) 273#define SE } while (0)
274 274
275#define LOAD(state) SB load_state(aTHX_ (state)); SPAGAIN; SE 275#define LOAD(state) load_state(aTHX_ (state));
276#define SAVE(state,flags) SB PUTBACK; save_state(aTHX_ (state),(flags)); SE 276#define SAVE(state,flags) save_state(aTHX_ (state),(flags));
277 277
278#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE 278#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE
279 279
280static void 280static void
281load_state(pTHX_ Coro__State c) 281load_state(pTHX_ Coro__State c)
355 355
356 PUSHs (Nullsv); 356 PUSHs (Nullsv);
357 /* this loop was inspired by pp_caller */ 357 /* this loop was inspired by pp_caller */
358 for (;;) 358 for (;;)
359 { 359 {
360 do 360 while (cxix >= 0)
361 { 361 {
362 PERL_CONTEXT *cx = &ccstk[cxix--]; 362 PERL_CONTEXT *cx = &ccstk[cxix--];
363 363
364 if (CxTYPE(cx) == CXt_SUB) 364 if (CxTYPE(cx) == CXt_SUB)
365 { 365 {
391 /* I never used formats, so how should I know how these are implemented? */ 391 /* I never used formats, so how should I know how these are implemented? */
392 /* my bold guess is as a simple, plain sub... */ 392 /* my bold guess is as a simple, plain sub... */
393 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); 393 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
394 } 394 }
395 } 395 }
396 while (cxix >= 0);
397 396
398 if (top_si->si_type == PERLSI_MAIN) 397 if (top_si->si_type == PERLSI_MAIN)
399 break; 398 break;
400 399
401 top_si = top_si->si_prev; 400 top_si = top_si->si_prev;
407 } 406 }
408 407
409 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 408 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
410 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 409 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
411 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 410 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
412
413 /* I have not the slightest idea of why av_reify is necessary */
414 /* but if it's missing the defav contents magically get replaced sometimes */
415 if (c->defav)
416 av_reify (c->defav);
417 411
418 c->dowarn = PL_dowarn; 412 c->dowarn = PL_dowarn;
419 c->in_eval = PL_in_eval; 413 c->in_eval = PL_in_eval;
420 414
421 c->curstackinfo = PL_curstackinfo; 415 c->curstackinfo = PL_curstackinfo;
493 * still there is a memleak of 128 bytes... 487 * still there is a memleak of 128 bytes...
494 */ 488 */
495STATIC void 489STATIC void
496destroy_stacks(pTHX) 490destroy_stacks(pTHX)
497{ 491{
492 int destruct = PL_main_cv != Nullcv;
493
494 if (destruct)
495 {
498 /* is this ugly, I ask? */ 496 /* is this ugly, I ask? */
499 while (PL_scopestack_ix) 497 while (PL_scopestack_ix)
500 LEAVE; 498 LEAVE;
501 499
502 /* sure it is, but more important: is it correct?? :/ */ 500 /* sure it is, but more important: is it correct?? :/ */
503 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ 501 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */
504 FREETMPS; 502 FREETMPS;
503 }
505 504
506 while (PL_curstackinfo->si_next) 505 while (PL_curstackinfo->si_next)
507 PL_curstackinfo = PL_curstackinfo->si_next; 506 PL_curstackinfo = PL_curstackinfo->si_next;
508 507
509 while (PL_curstackinfo) 508 while (PL_curstackinfo)
514 dSP; 513 dSP;
515 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 514 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
516 PUTBACK; /* possibly superfluous */ 515 PUTBACK; /* possibly superfluous */
517 } 516 }
518 517
519 if (PL_main_cv != Nullcv) /* don't during destruction. hack? */ 518 if (destruct)
519 {
520 dounwind(-1); 520 dounwind(-1);
521
522 SvREFCNT_dec(PL_curstackinfo->si_stack); 521 SvREFCNT_dec(PL_curstackinfo->si_stack);
522 }
523
523 Safefree(PL_curstackinfo->si_cxstack); 524 Safefree(PL_curstackinfo->si_cxstack);
524 Safefree(PL_curstackinfo); 525 Safefree(PL_curstackinfo);
525 PL_curstackinfo = p; 526 PL_curstackinfo = p;
526 } 527 }
527 528
649{ 650{
650 /* 651 /*
651 * this is a _very_ stripped down perl interpreter ;) 652 * this is a _very_ stripped down perl interpreter ;)
652 */ 653 */
653 Coro__State ctx = (Coro__State)arg; 654 Coro__State ctx = (Coro__State)arg;
655 JMPENV coro_start_env;
654 656
655 /*FIXME*//* must set up top_env here */ 657 /* same as JMPENV_BOOTSTRAP */
658 Zero(&coro_start_env, 1, JMPENV);
659 coro_start_env.je_ret = -1;
660 coro_start_env.je_mustcatch = TRUE;
661 PL_top_env = &coro_start_env;
662
656 ctx->cursp = 0; 663 ctx->cursp = 0;
657 PL_op = PL_op->op_next; 664 PL_op = PL_op->op_next;
658 CALLRUNOPS(aTHX); 665 CALLRUNOPS(aTHX);
659 666
660 abort (); 667 abort ();
661} 668}
662 669
663STATIC void 670STATIC void
664transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 671transfer(pTHX_ struct coro *prev, struct coro *next, int flags)
665{ 672{
666 dSP;
667 dSTACKLEVEL; 673 dSTACKLEVEL;
674 static struct coro *xnext;
668 675
669 if (prev != next) 676 if (prev != next)
670 { 677 {
678 xnext = next;
679
671 if (next->mainstack) 680 if (next->mainstack)
672 { 681 {
673 SAVE (prev, flags); 682 SAVE (prev, flags);
674 LOAD (next); 683 LOAD (next);
675 684
698 continue_coro, (void *)next, 707 continue_coro, (void *)next,
699 next->stack->sptr, labs (next->stack->ssize)); 708 next->stack->sptr, labs (next->stack->ssize));
700 } 709 }
701 710
702 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 711 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
712 /* don't add any code here */
703 } 713 }
704 714
705 } 715 }
706 else if (next->tmps_ix == -2) 716 else if (next->tmps_ix == -2)
707 croak ("tried to transfer to running coroutine"); 717 croak ("tried to transfer to running coroutine");
728 allocate_stack (next, 1); 738 allocate_stack (next, 1);
729 coro_create (&(next->stack->cctx), 739 coro_create (&(next->stack->cctx),
730 setup_coro, (void *)next, 740 setup_coro, (void *)next,
731 next->stack->sptr, labs (next->stack->ssize)); 741 next->stack->sptr, labs (next->stack->ssize));
732 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 742 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
743 /* don't add any code here */
733 } 744 }
734 } 745 }
735 else 746 else
736 setup_coro (next); 747 setup_coro (next);
737 } 748 }
738 } 749 }
739 750
751 /*
752 * xnext is now either prev or next, depending on wether
753 * we switched the c stack or not. that's why i use a global
754 * variable, that should become thread-specific at one point.
755 */
740 next->cursp = stacklevel; 756 xnext->cursp = stacklevel;
741} 757}
742 758
743static struct coro * 759static struct coro *
744sv_to_coro (SV *arg, const char *funcname, const char *varname) 760sv_to_coro (SV *arg, const char *funcname, const char *varname)
745{ 761{
752 768
753 arg = HeVAL(he); 769 arg = HeVAL(he);
754 } 770 }
755 771
756 /* must also be changed inside Coro::Cont::yield */ 772 /* must also be changed inside Coro::Cont::yield */
773 if (SvROK(arg) && SvOBJECT(SvRV(arg))
757 if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) 774 && SvSTASH(SvRV(arg)) == coro_state_stash)
758 return (struct coro *) SvIV((SV*)SvRV(arg)); 775 return (struct coro *) SvIV((SV*)SvRV(arg));
759 else 776
760 croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); 777 croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname);
778 /*NORETURN*/
761} 779}
762 780
763static void 781static void
764api_transfer(pTHX_ SV *prev, SV *next, int flags) 782api_transfer(pTHX_ SV *prev, SV *next, int flags)
765{ 783{
784 transfer(aTHX_
766 transfer(aTHX_ sv_to_coro (prev, "Coro::transfer", "prev"), 785 sv_to_coro (prev, "Coro::transfer", "prev"),
767 sv_to_coro (next, "Coro::transfer", "next"), 786 sv_to_coro (next, "Coro::transfer", "next"),
768 flags); 787 flags);
769} 788}
770 789
771/** Coro ********************************************************************/ 790/** Coro ********************************************************************/
772 791
773#define PRIO_MAX 3 792#define PRIO_MAX 3
846 865
847 if (!next) 866 if (!next)
848 next = SvREFCNT_inc (GvSV (coro_idle)); 867 next = SvREFCNT_inc (GvSV (coro_idle));
849 868
850 GvSV (coro_current) = SvREFCNT_inc (next); 869 GvSV (coro_current) = SvREFCNT_inc (next);
870 transfer (aTHX_
851 transfer (sv_to_coro (prev, "Coro::schedule", "current coroutine"), 871 sv_to_coro (prev, "Coro::schedule", "current coroutine"),
852 sv_to_coro (next, "Coro::schedule", "next coroutine"), 872 sv_to_coro (next, "Coro::schedule", "next coroutine"),
853 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 873 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
854 SvREFCNT_dec (next); 874 SvREFCNT_dec (next);
855 SvREFCNT_dec (prev); 875 SvREFCNT_dec (prev);
856} 876}
873 if (!padlist_cache) 893 if (!padlist_cache)
874 padlist_cache = newHV (); 894 padlist_cache = newHV ();
875 895
876 main_mainstack = PL_mainstack; 896 main_mainstack = PL_mainstack;
877 897
878 {
879 SV *sv = perl_get_sv("Coro::API", 1);
880
881 coroapi.ver = CORO_API_VERSION - 1; 898 coroapi.ver = CORO_API_VERSION;
882 coroapi.transfer = api_transfer; 899 coroapi.transfer = api_transfer;
883 coroapi.schedule = api_schedule;
884 coroapi.ready = api_ready;
885 coroapi.nready = &coro_nready;
886
887 GCoroAPI = &coroapi;
888 sv_setiv(sv, (IV)&coroapi);
889 SvREADONLY_on(sv);
890 }
891} 900}
892 901
893Coro::State 902Coro::State
894_newprocess(args) 903_newprocess(args)
895 SV * args 904 SV * args
928 937
929 if (coro->mainstack && coro->mainstack != main_mainstack) 938 if (coro->mainstack && coro->mainstack != main_mainstack)
930 { 939 {
931 struct coro temp; 940 struct coro temp;
932 941
942 PUTBACK;
933 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 943 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL);
934 LOAD(aTHX_ coro); 944 LOAD(aTHX_ coro);
945 SPAGAIN;
935 946
936 destroy_stacks (aTHX); 947 destroy_stacks (aTHX);
937 948
938 LOAD((&temp)); /* this will get rid of defsv etc.. */ 949 LOAD((&temp)); /* this will get rid of defsv etc.. */
950 SPAGAIN;
939 951
940 coro->mainstack = 0; 952 coro->mainstack = 0;
941 } 953 }
942 954
943 deallocate_stack (coro); 955 deallocate_stack (coro);
1013 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); 1025 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV);
1014 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); 1026 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV);
1015 1027
1016 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1028 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1017 coro_ready[i] = newAV (); 1029 coro_ready[i] = newAV ();
1030
1031 {
1032 SV *sv = perl_get_sv("Coro::API", 1);
1033
1034 coroapi.schedule = api_schedule;
1035 coroapi.ready = api_ready;
1036 coroapi.nready = &coro_nready;
1037 coroapi.current = coro_current;
1038
1039 GCoroAPI = &coroapi;
1040 sv_setiv(sv, (IV)&coroapi);
1041 SvREADONLY_on(sv);
1042 }
1018} 1043}
1019 1044
1020void 1045void
1021ready(self) 1046ready(self)
1022 SV * self 1047 SV * self

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines