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.27 by root, Mon Aug 20 16:58:19 2001 UTC vs.
Revision 1.42 by root, Mon Dec 31 20:41:45 2001 UTC

7#include <signal.h> 7#include <signal.h>
8 8
9#ifdef HAVE_MMAP 9#ifdef HAVE_MMAP
10# include <unistd.h> 10# include <unistd.h>
11# include <sys/mman.h> 11# include <sys/mman.h>
12# ifndef MAP_ANON
13# ifdef MAP_ANONYMOUS 12# ifndef MAP_ANONYMOUS
14# define MAP_ANON MAP_ANONYMOUS 13# ifdef MAP_ANON
14# define MAP_ANONYMOUS MAP_ANON
15# else 15# else
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
31#define IN_DESTRUCT (PL_main_cv == Nullcv)
30 32
31#define labs(l) ((l) >= 0 ? (l) : -(l)) 33#define labs(l) ((l) >= 0 ? (l) : -(l))
32 34
33#include "CoroAPI.h" 35#include "CoroAPI.h"
34 36
99static AV *main_mainstack; /* used to differentiate between $main and others */ 101static AV *main_mainstack; /* used to differentiate between $main and others */
100static HV *coro_state_stash; 102static HV *coro_state_stash;
101static SV *ucoro_state_sv; 103static SV *ucoro_state_sv;
102static U32 ucoro_state_hash; 104static U32 ucoro_state_hash;
103static HV *padlist_cache; 105static HV *padlist_cache;
106static SV *coro_mortal; /* will be freed after next transfer */
104 107
105/* mostly copied from op.c:cv_clone2 */ 108/* mostly copied from op.c:cv_clone2 */
106STATIC AV * 109STATIC AV *
107clone_padlist (AV *protopadlist) 110clone_padlist (AV *protopadlist)
108{ 111{
270#endif 273#endif
271 274
272#define SB do { 275#define SB do {
273#define SE } while (0) 276#define SE } while (0)
274 277
275#define LOAD(state) SB load_state(aTHX_ (state)); SPAGAIN; SE 278#define LOAD(state) load_state(aTHX_ (state));
276#define SAVE(state,flags) SB PUTBACK; save_state(aTHX_ (state),(flags)); SE 279#define SAVE(state,flags) save_state(aTHX_ (state),(flags));
277 280
278#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE 281#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); SE
279 282
280static void 283static void
281load_state(pTHX_ Coro__State c) 284load_state(pTHX_ Coro__State c)
355 358
356 PUSHs (Nullsv); 359 PUSHs (Nullsv);
357 /* this loop was inspired by pp_caller */ 360 /* this loop was inspired by pp_caller */
358 for (;;) 361 for (;;)
359 { 362 {
360 do 363 while (cxix >= 0)
361 { 364 {
362 PERL_CONTEXT *cx = &ccstk[cxix--]; 365 PERL_CONTEXT *cx = &ccstk[cxix--];
363 366
364 if (CxTYPE(cx) == CXt_SUB) 367 if (CxTYPE(cx) == CXt_SUB)
365 { 368 {
391 /* I never used formats, so how should I know how these are implemented? */ 394 /* I never used formats, so how should I know how these are implemented? */
392 /* my bold guess is as a simple, plain sub... */ 395 /* my bold guess is as a simple, plain sub... */
393 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); 396 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
394 } 397 }
395 } 398 }
396 while (cxix >= 0);
397 399
398 if (top_si->si_type == PERLSI_MAIN) 400 if (top_si->si_type == PERLSI_MAIN)
399 break; 401 break;
400 402
401 top_si = top_si->si_prev; 403 top_si = top_si->si_prev;
407 } 409 }
408 410
409 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 411 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
410 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 412 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
411 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 413 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 414
418 c->dowarn = PL_dowarn; 415 c->dowarn = PL_dowarn;
419 c->in_eval = PL_in_eval; 416 c->in_eval = PL_in_eval;
420 417
421 c->curstackinfo = PL_curstackinfo; 418 c->curstackinfo = PL_curstackinfo;
493 * still there is a memleak of 128 bytes... 490 * still there is a memleak of 128 bytes...
494 */ 491 */
495STATIC void 492STATIC void
496destroy_stacks(pTHX) 493destroy_stacks(pTHX)
497{ 494{
495 if (!IN_DESTRUCT)
496 {
498 /* is this ugly, I ask? */ 497 /* is this ugly, I ask? */
499 while (PL_scopestack_ix) 498 while (PL_scopestack_ix)
500 LEAVE; 499 LEAVE;
501 500
502 /* sure it is, but more important: is it correct?? :/ */ 501 /* sure it is, but more important: is it correct?? :/ */
503 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */ 502 while (PL_tmps_ix > PL_tmps_floor) /* should only ever be one iteration */
504 FREETMPS; 503 FREETMPS;
504 }
505 505
506 while (PL_curstackinfo->si_next) 506 while (PL_curstackinfo->si_next)
507 PL_curstackinfo = PL_curstackinfo->si_next; 507 PL_curstackinfo = PL_curstackinfo->si_next;
508 508
509 while (PL_curstackinfo) 509 while (PL_curstackinfo)
514 dSP; 514 dSP;
515 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack); 515 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
516 PUTBACK; /* possibly superfluous */ 516 PUTBACK; /* possibly superfluous */
517 } 517 }
518 518
519 if (PL_main_cv != Nullcv) /* don't during destruction. hack? */ 519 if (!IN_DESTRUCT)
520 {
520 dounwind(-1); 521 dounwind(-1);
521
522 SvREFCNT_dec(PL_curstackinfo->si_stack); 522 SvREFCNT_dec(PL_curstackinfo->si_stack);
523 }
524
523 Safefree(PL_curstackinfo->si_cxstack); 525 Safefree(PL_curstackinfo->si_cxstack);
524 Safefree(PL_curstackinfo); 526 Safefree(PL_curstackinfo);
525 PL_curstackinfo = p; 527 PL_curstackinfo = p;
526 } 528 }
527 529
542 stack->refcnt = 1; 544 stack->refcnt = 1;
543 stack->usecnt = 1; 545 stack->usecnt = 1;
544 stack->gencnt = ctx->gencnt = 0; 546 stack->gencnt = ctx->gencnt = 0;
545 if (alloc) 547 if (alloc)
546 { 548 {
547#ifdef HAVE_MMAP 549#if HAVE_MMAP
548 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ 550 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */
549 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0); 551 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
550 if (stack->sptr == (void *)-1) 552 if (stack->sptr == (void *)-1)
551#endif 553#endif
552 { 554 {
553 /*FIXME*//*D*//* reasonable stack size! */ 555 /*FIXME*//*D*//* reasonable stack size! */
554 stack->ssize = -4096 * sizeof (long); 556 stack->ssize = -4096 * sizeof (long);
635 * that doesn't matter, though, since it is only 637 * that doesn't matter, though, since it is only
636 * pp_nextstate and we never return... 638 * pp_nextstate and we never return...
637 * ah yes, and I don't care anyways ;) 639 * ah yes, and I don't care anyways ;)
638 */ 640 */
639 PUTBACK; 641 PUTBACK;
640 PL_op = pp_entersub(); 642 PL_op = PL_ppaddr[OP_ENTERSUB]();
641 SPAGAIN; 643 SPAGAIN;
642 644
643 ENTER; /* necessary e.g. for dounwind */ 645 ENTER; /* necessary e.g. for dounwind */
644 } 646 }
645} 647}
649{ 651{
650 /* 652 /*
651 * this is a _very_ stripped down perl interpreter ;) 653 * this is a _very_ stripped down perl interpreter ;)
652 */ 654 */
653 Coro__State ctx = (Coro__State)arg; 655 Coro__State ctx = (Coro__State)arg;
656 JMPENV coro_start_env;
654 657
655 /*FIXME*//* must set up top_env here */ 658 /* same as JMPENV_BOOTSTRAP */
659 Zero(&coro_start_env, 1, JMPENV);
660 coro_start_env.je_ret = -1;
661 coro_start_env.je_mustcatch = TRUE;
662 PL_top_env = &coro_start_env;
663
656 ctx->cursp = 0; 664 ctx->cursp = 0;
657 PL_op = PL_op->op_next; 665 PL_op = PL_op->op_next;
658 CALLRUNOPS(aTHX); 666 CALLRUNOPS(aTHX);
659 667
660 abort (); 668 abort ();
661} 669}
662 670
663STATIC void 671STATIC void
664transfer(pTHX_ struct coro *prev, struct coro *next, int flags) 672transfer(pTHX_ struct coro *prev, struct coro *next, int flags)
665{ 673{
666 dSP;
667 dSTACKLEVEL; 674 dSTACKLEVEL;
675 static struct coro *xnext;
668 676
669 if (prev != next) 677 if (prev != next)
670 { 678 {
679 xnext = next;
680
671 if (next->mainstack) 681 if (next->mainstack)
672 { 682 {
673 SAVE (prev, flags); 683 SAVE (prev, flags);
674 LOAD (next); 684 LOAD (next);
675 685
698 continue_coro, (void *)next, 708 continue_coro, (void *)next,
699 next->stack->sptr, labs (next->stack->ssize)); 709 next->stack->sptr, labs (next->stack->ssize));
700 } 710 }
701 711
702 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 712 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
713 /* don't add any code here */
703 } 714 }
704 715
705 } 716 }
706 else if (next->tmps_ix == -2) 717 else if (next->tmps_ix == -2)
707 croak ("tried to transfer to running coroutine"); 718 croak ("tried to transfer to running coroutine");
728 allocate_stack (next, 1); 739 allocate_stack (next, 1);
729 coro_create (&(next->stack->cctx), 740 coro_create (&(next->stack->cctx),
730 setup_coro, (void *)next, 741 setup_coro, (void *)next,
731 next->stack->sptr, labs (next->stack->ssize)); 742 next->stack->sptr, labs (next->stack->ssize));
732 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx)); 743 coro_transfer (&(prev->stack->cctx), &(next->stack->cctx));
744 /* don't add any code here */
733 } 745 }
734 } 746 }
735 else 747 else
736 setup_coro (next); 748 setup_coro (next);
737 } 749 }
738 }
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}
742
743static struct coro *
744sv_to_coro (SV *arg, const char *funcname, const char *varname)
745{
746 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV)
747 { 757 }
758
759 if (coro_mortal)
760 {
761 SvREFCNT_dec (coro_mortal);
762 coro_mortal = 0;
763 }
764}
765
766#define SV_CORO(sv,func) \
767 do { \
768 if (SvROK (sv)) \
769 sv = SvRV (sv); \
770 \
771 if (SvTYPE(sv) == SVt_PVHV) \
772 { \
748 HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); 773 HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \
749 774 \
750 if (!he) 775 if (!he) \
751 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); 776 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \
752 777 \
753 arg = HeVAL(he); 778 (sv) = SvRV (HeVAL(he)); \
754 } 779 } \
755 780 \
756 /* must also be changed inside Coro::Cont::yield */ 781 /* must also be changed inside Coro::Cont::yield */ \
757 if (SvROK(arg) && SvSTASH(SvRV(arg)) == coro_state_stash) 782 if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \
758 return (struct coro *) SvIV((SV*)SvRV(arg));
759
760 croak ("%s() -- %s is not (and contains not) a Coro::State object", funcname, varname); 783 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
761 /*NORETURN*/ 784 \
762} 785 } while(0)
786
787#define SvSTATE(sv) (struct coro *)SvIV (sv)
763 788
764static void 789static void
765api_transfer(pTHX_ SV *prev, SV *next, int flags) 790api_transfer(pTHX_ SV *prev, SV *next, int flags)
766{ 791{
767 transfer(aTHX_ 792 SV_CORO (prev, "Coro::transfer");
768 sv_to_coro (prev, "Coro::transfer", "prev"), 793 SV_CORO (next, "Coro::transfer");
769 sv_to_coro (next, "Coro::transfer", "next"), 794
770 flags); 795 transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags);
771} 796}
772 797
773/** Coro ********************************************************************/ 798/** Coro ********************************************************************/
774 799
775#define PRIO_MAX 3 800#define PRIO_MAX 3
785static int coro_nready; 810static int coro_nready;
786 811
787static void 812static void
788coro_enq (SV *sv) 813coro_enq (SV *sv)
789{ 814{
790 if (SvROK (sv))
791 {
792 SV *hv = SvRV (sv);
793 if (SvTYPE (hv) == SVt_PVHV) 815 if (SvTYPE (sv) == SVt_PVHV)
794 { 816 {
795 SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0); 817 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
796 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; 818 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
797 819
798 prio = prio > PRIO_MAX ? PRIO_MAX 820 prio = prio > PRIO_MAX ? PRIO_MAX
799 : prio < PRIO_MIN ? PRIO_MIN 821 : prio < PRIO_MIN ? PRIO_MIN
800 : prio; 822 : prio;
801 823
802 av_push (coro_ready [prio - PRIO_MIN], sv); 824 av_push (coro_ready [prio - PRIO_MIN], sv);
803 coro_nready++; 825 coro_nready++;
804 826
805 return; 827 return;
806 }
807 } 828 }
808 829
809 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 830 croak ("Coro::ready tried to enqueue something that is not a coroutine");
810} 831}
811 832
829} 850}
830 851
831static void 852static void
832api_ready (SV *coro) 853api_ready (SV *coro)
833{ 854{
855 if (SvROK (coro))
856 coro = SvRV (coro);
857
834 coro_enq (SvREFCNT_inc (coro)); 858 coro_enq (SvREFCNT_inc (coro));
835} 859}
836 860
837static void 861static void
838api_schedule (int cede) 862api_schedule (void)
839{ 863{
840 SV *prev, *next; 864 SV *prev, *next;
841 865
842 prev = GvSV (coro_current); 866 prev = SvRV (GvSV (coro_current));
843
844 if (cede)
845 coro_enq (SvREFCNT_inc (prev));
846
847 next = coro_deq (PRIO_MIN); 867 next = coro_deq (PRIO_MIN);
848 868
849 if (!next) 869 if (!next)
850 next = SvREFCNT_inc (GvSV (coro_idle)); 870 next = SvREFCNT_inc (SvRV (GvSV (coro_idle)));
851 871
852 GvSV (coro_current) = SvREFCNT_inc (next); 872 /* free this only after the transfer */
853 transfer (aTHX_ 873 coro_mortal = prev;
854 sv_to_coro (prev, "Coro::schedule", "current coroutine"), 874 SV_CORO (prev, "Coro::schedule");
855 sv_to_coro (next, "Coro::schedule", "next coroutine"), 875
876 SvRV (GvSV (coro_current)) = next;
877
878 SV_CORO (next, "Coro::schedule");
879
880 transfer (aTHX_ SvSTATE (prev), SvSTATE (next),
856 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 881 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
857 SvREFCNT_dec (next); 882}
858 SvREFCNT_dec (prev); 883
884static void
885api_cede (void)
886{
887 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
888
889 api_schedule ();
859} 890}
860 891
861MODULE = Coro::State PACKAGE = Coro::State 892MODULE = Coro::State PACKAGE = Coro::State
862 893
863PROTOTYPES: ENABLE 894PROTOTYPES: ENABLE
902 OUTPUT: 933 OUTPUT:
903 RETVAL 934 RETVAL
904 935
905void 936void
906transfer(prev, next, flags) 937transfer(prev, next, flags)
907 Coro::State_or_hashref prev 938 SV *prev
908 Coro::State_or_hashref next 939 SV *next
909 int flags 940 int flags
910 PROTOTYPE: @ 941 PROTOTYPE: @
911 CODE: 942 CODE:
912 PUTBACK; 943 PUTBACK;
944 SV_CORO (next, "Coro::transfer");
945 SV_CORO (prev, "Coro::transfer");
913 transfer (aTHX_ prev, next, flags); 946 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
914 SPAGAIN; 947 SPAGAIN;
915 948
916void 949void
917DESTROY(coro) 950DESTROY(coro)
918 Coro::State coro 951 Coro::State coro
920 953
921 if (coro->mainstack && coro->mainstack != main_mainstack) 954 if (coro->mainstack && coro->mainstack != main_mainstack)
922 { 955 {
923 struct coro temp; 956 struct coro temp;
924 957
958 PUTBACK;
925 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL); 959 SAVE(aTHX_ (&temp), TRANSFER_SAVE_ALL);
926 LOAD(aTHX_ coro); 960 LOAD(aTHX_ coro);
961 SPAGAIN;
927 962
928 destroy_stacks (aTHX); 963 destroy_stacks (aTHX);
929 964
930 LOAD((&temp)); /* this will get rid of defsv etc.. */ 965 LOAD((&temp)); /* this will get rid of defsv etc.. */
966 SPAGAIN;
931 967
932 coro->mainstack = 0; 968 coro->mainstack = 0;
933 } 969 }
934 970
935 deallocate_stack (coro); 971 deallocate_stack (coro);
1010 1046
1011 { 1047 {
1012 SV *sv = perl_get_sv("Coro::API", 1); 1048 SV *sv = perl_get_sv("Coro::API", 1);
1013 1049
1014 coroapi.schedule = api_schedule; 1050 coroapi.schedule = api_schedule;
1051 coroapi.cede = api_cede;
1015 coroapi.ready = api_ready; 1052 coroapi.ready = api_ready;
1016 coroapi.nready = &coro_nready; 1053 coroapi.nready = &coro_nready;
1017 coroapi.current = coro_current; 1054 coroapi.current = coro_current;
1018 1055
1019 GCoroAPI = &coroapi; 1056 GCoroAPI = &coroapi;
1023} 1060}
1024 1061
1025void 1062void
1026ready(self) 1063ready(self)
1027 SV * self 1064 SV * self
1065 PROTOTYPE: $
1028 CODE: 1066 CODE:
1029 api_ready (self); 1067 api_ready (self);
1030 1068
1031int 1069int
1032nready(...) 1070nready(...)
1037 RETVAL 1075 RETVAL
1038 1076
1039void 1077void
1040schedule(...) 1078schedule(...)
1041 PROTOTYPE: 1079 PROTOTYPE:
1042 ALIAS:
1043 cede = 1
1044 CODE: 1080 CODE:
1045 api_schedule (ix); 1081 api_schedule ();
1046 1082
1083void
1084cede(...)
1085 PROTOTYPE:
1086 CODE:
1087 api_cede ();
1088

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines