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.36 by root, Fri Oct 5 14:20:10 2001 UTC vs.
Revision 1.43 by root, Fri Jan 4 02:47:38 2002 UTC

101static AV *main_mainstack; /* used to differentiate between $main and others */ 101static AV *main_mainstack; /* used to differentiate between $main and others */
102static HV *coro_state_stash; 102static HV *coro_state_stash;
103static SV *ucoro_state_sv; 103static SV *ucoro_state_sv;
104static U32 ucoro_state_hash; 104static U32 ucoro_state_hash;
105static HV *padlist_cache; 105static HV *padlist_cache;
106static SV *coro_mortal; /* will be freed after next transfer */
106 107
107/* mostly copied from op.c:cv_clone2 */ 108/* mostly copied from op.c:cv_clone2 */
108STATIC AV * 109STATIC AV *
109clone_padlist (AV *protopadlist) 110clone_padlist (AV *protopadlist)
110{ 111{
543 stack->refcnt = 1; 544 stack->refcnt = 1;
544 stack->usecnt = 1; 545 stack->usecnt = 1;
545 stack->gencnt = ctx->gencnt = 0; 546 stack->gencnt = ctx->gencnt = 0;
546 if (alloc) 547 if (alloc)
547 { 548 {
548#ifdef HAVE_MMAP 549#if HAVE_MMAP
549 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 */
550 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 551 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
551 if (stack->sptr == (void *)-1) 552 if (stack->sptr == (void *)-1)
552#endif 553#endif
553 { 554 {
636 * that doesn't matter, though, since it is only 637 * that doesn't matter, though, since it is only
637 * pp_nextstate and we never return... 638 * pp_nextstate and we never return...
638 * ah yes, and I don't care anyways ;) 639 * ah yes, and I don't care anyways ;)
639 */ 640 */
640 PUTBACK; 641 PUTBACK;
641 PL_op = pp_entersub(); 642 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
642 SPAGAIN; 643 SPAGAIN;
643 644
644 ENTER; /* necessary e.g. for dounwind */ 645 ENTER; /* necessary e.g. for dounwind */
645 } 646 }
646} 647}
747 setup_coro (next); 748 setup_coro (next);
748 } 749 }
749 750
750 /* 751 /*
751 * xnext is now either prev or next, depending on wether 752 * xnext is now either prev or next, depending on wether
752 * we switched the c stack or not. that's why i use a global 753 * we switched the c stack or not. that's why I use a global
753 * variable, that should become thread-specific at one point. 754 * variable, that should become thread-specific at one point.
754 */ 755 */
755 xnext->cursp = stacklevel; 756 xnext->cursp = stacklevel;
756 } 757 }
757}
758 758
759static struct coro * 759 if (coro_mortal)
760sv_to_coro (SV *arg, const char *funcname, const char *varname) 760 {
761{ 761 SvREFCNT_dec (coro_mortal);
762 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV) 762 coro_mortal = 0;
763 { 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 { \
764 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); \
765 774 \
766 if (!he) 775 if (!he) \
767 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); \
768 777 \
769 arg = HeVAL(he); 778 (sv) = SvRV (HeVAL(he)); \
770 } 779 } \
771 780 \
772 /* must also be changed inside Coro::Cont::yield */ 781 /* must also be changed inside Coro::Cont::yield */ \
773 if (SvROK(arg) && SvOBJECT(SvRV(arg)) 782 if (!SvOBJECT(sv) || SvSTASH(sv) != coro_state_stash) \
774 && SvSTASH(SvRV(arg)) == coro_state_stash)
775 return (struct coro *) SvIV((SV*)SvRV(arg));
776
777 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); \
778 /*NORETURN*/ 784 \
779} 785 } while(0)
786
787#define SvSTATE(sv) (struct coro *)SvIV (sv)
780 788
781static void 789static void
782api_transfer(pTHX_ SV *prev, SV *next, int flags) 790api_transfer(pTHX_ SV *prev, SV *next, int flags)
783{ 791{
784 transfer(aTHX_ 792 SV_CORO (prev, "Coro::transfer");
785 sv_to_coro (prev, "Coro::transfer", "prev"), 793 SV_CORO (next, "Coro::transfer");
786 sv_to_coro (next, "Coro::transfer", "next"), 794
787 flags); 795 transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags);
788} 796}
789 797
790/** Coro ********************************************************************/ 798/** Coro ********************************************************************/
791 799
792#define PRIO_MAX 3 800#define PRIO_MAX 3
802static int coro_nready; 810static int coro_nready;
803 811
804static void 812static void
805coro_enq (SV *sv) 813coro_enq (SV *sv)
806{ 814{
807 if (SvROK (sv))
808 {
809 SV *hv = SvRV (sv);
810 if (SvTYPE (hv) == SVt_PVHV) 815 if (SvTYPE (sv) == SVt_PVHV)
811 { 816 {
812 SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0); 817 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
813 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; 818 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
814 819
815 prio = prio > PRIO_MAX ? PRIO_MAX 820 prio = prio > PRIO_MAX ? PRIO_MAX
816 : prio < PRIO_MIN ? PRIO_MIN 821 : prio < PRIO_MIN ? PRIO_MIN
817 : prio; 822 : prio;
818 823
819 av_push (coro_ready [prio - PRIO_MIN], sv); 824 av_push (coro_ready [prio - PRIO_MIN], sv);
820 coro_nready++; 825 coro_nready++;
821 826
822 return; 827 return;
823 }
824 } 828 }
825 829
826 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");
827} 831}
828 832
846} 850}
847 851
848static void 852static void
849api_ready (SV *coro) 853api_ready (SV *coro)
850{ 854{
855 if (SvROK (coro))
856 coro = SvRV (coro);
857
851 coro_enq (SvREFCNT_inc (coro)); 858 coro_enq (SvREFCNT_inc (coro));
852} 859}
853 860
854static void 861static void
855api_schedule (int cede) 862api_schedule (void)
856{ 863{
857 SV *prev, *next; 864 SV *prev, *next;
858 865
859 prev = GvSV (coro_current); 866 prev = SvRV (GvSV (coro_current));
860
861 if (cede)
862 coro_enq (SvREFCNT_inc (prev));
863
864 next = coro_deq (PRIO_MIN); 867 next = coro_deq (PRIO_MIN);
865 868
866 if (!next) 869 if (!next)
867 next = SvREFCNT_inc (GvSV (coro_idle)); 870 next = SvREFCNT_inc (SvRV (GvSV (coro_idle)));
868 871
869 GvSV (coro_current) = SvREFCNT_inc (next); 872 /* free this only after the transfer */
870 transfer (aTHX_ 873 coro_mortal = prev;
871 sv_to_coro (prev, "Coro::schedule", "current coroutine"), 874 SV_CORO (prev, "Coro::schedule");
872 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),
873 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK); 881 TRANSFER_SAVE_ALL | TRANSFER_LAZY_STACK);
874 SvREFCNT_dec (next); 882}
875 SvREFCNT_dec (prev); 883
884static void
885api_cede (void)
886{
887 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
888
889 api_schedule ();
876} 890}
877 891
878MODULE = Coro::State PACKAGE = Coro::State 892MODULE = Coro::State PACKAGE = Coro::State
879 893
880PROTOTYPES: ENABLE 894PROTOTYPES: ENABLE
919 OUTPUT: 933 OUTPUT:
920 RETVAL 934 RETVAL
921 935
922void 936void
923transfer(prev, next, flags) 937transfer(prev, next, flags)
924 Coro::State_or_hashref prev 938 SV *prev
925 Coro::State_or_hashref next 939 SV *next
926 int flags 940 int flags
927 PROTOTYPE: @ 941 PROTOTYPE: @
928 CODE: 942 CODE:
929 PUTBACK; 943 PUTBACK;
944 SV_CORO (next, "Coro::transfer");
945 SV_CORO (prev, "Coro::transfer");
930 transfer (aTHX_ prev, next, flags); 946 transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
931 SPAGAIN; 947 SPAGAIN;
932 948
933void 949void
934DESTROY(coro) 950DESTROY(coro)
935 Coro::State coro 951 Coro::State coro
1030 1046
1031 { 1047 {
1032 SV *sv = perl_get_sv("Coro::API", 1); 1048 SV *sv = perl_get_sv("Coro::API", 1);
1033 1049
1034 coroapi.schedule = api_schedule; 1050 coroapi.schedule = api_schedule;
1051 coroapi.cede = api_cede;
1035 coroapi.ready = api_ready; 1052 coroapi.ready = api_ready;
1036 coroapi.nready = &coro_nready; 1053 coroapi.nready = &coro_nready;
1037 coroapi.current = coro_current; 1054 coroapi.current = coro_current;
1038 1055
1039 GCoroAPI = &coroapi; 1056 GCoroAPI = &coroapi;
1058 RETVAL 1075 RETVAL
1059 1076
1060void 1077void
1061schedule(...) 1078schedule(...)
1062 PROTOTYPE: 1079 PROTOTYPE:
1063 ALIAS:
1064 cede = 1
1065 CODE: 1080 CODE:
1066 api_schedule (ix); 1081 api_schedule ();
1067 1082
1083void
1084cede(...)
1085 PROTOTYPE:
1086 CODE:
1087 api_cede ();
1088

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines