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.34 by root, Fri Sep 14 15:40:56 2001 UTC vs.
Revision 1.39 by root, Sun Nov 25 20:04:05 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
56 56
57 /* optionally saved, might be zero */ 57 /* optionally saved, might be zero */
58 AV *defav; 58 AV *defav;
59 SV *defsv; 59 SV *defsv;
60 SV *errsv; 60 SV *errsv;
61 /* TODO: PL_pmop might be nice && effortless to save. */
61 62
62 /* saved global state not related to stacks */ 63 /* saved global state not related to stacks */
63 U8 dowarn; 64 U8 dowarn;
64 I32 in_eval; 65 I32 in_eval;
65 66
101static AV *main_mainstack; /* used to differentiate between $main and others */ 102static AV *main_mainstack; /* used to differentiate between $main and others */
102static HV *coro_state_stash; 103static HV *coro_state_stash;
103static SV *ucoro_state_sv; 104static SV *ucoro_state_sv;
104static U32 ucoro_state_hash; 105static U32 ucoro_state_hash;
105static HV *padlist_cache; 106static HV *padlist_cache;
107static SV *coro_mortal; /* will be freed after next transfer */
106 108
107/* mostly copied from op.c:cv_clone2 */ 109/* mostly copied from op.c:cv_clone2 */
108STATIC AV * 110STATIC AV *
109clone_padlist (AV *protopadlist) 111clone_padlist (AV *protopadlist)
110{ 112{
543 stack->refcnt = 1; 545 stack->refcnt = 1;
544 stack->usecnt = 1; 546 stack->usecnt = 1;
545 stack->gencnt = ctx->gencnt = 0; 547 stack->gencnt = ctx->gencnt = 0;
546 if (alloc) 548 if (alloc)
547 { 549 {
548#ifdef HAVE_MMAP 550#if HAVE_MMAP
549 stack->ssize = 128 * 1024 * sizeof (long); /* mmap should do allocate-on-write for us */ 551 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_ANON, 0, 0); 552 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
551 if (stack->sptr == (void *)-1) 553 if (stack->sptr == (void *)-1)
552#endif 554#endif
553 { 555 {
554 /*FIXME*//*D*//* reasonable stack size! */ 556 /*FIXME*//*D*//* reasonable stack size! */
555 stack->ssize = -4096 * sizeof (long); 557 stack->ssize = -4096 * sizeof (long);
752 * we switched the c stack or not. that's why i use a global 754 * we switched the c stack or not. that's why i use a global
753 * variable, that should become thread-specific at one point. 755 * variable, that should become thread-specific at one point.
754 */ 756 */
755 xnext->cursp = stacklevel; 757 xnext->cursp = stacklevel;
756 } 758 }
757}
758 759
759static struct coro * 760 if (coro_mortal)
760sv_to_coro (SV *arg, const char *funcname, const char *varname) 761 {
761{ 762 SvREFCNT_dec (coro_mortal);
762 if (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVHV) 763 coro_mortal = 0;
763 { 764 }
765}
766
767#define SV_CORO(sv,func) \
768 do { \
769 if (SvROK (sv)) \
770 sv = SvRV (sv); \
771 \
772 if (SvTYPE(sv) == SVt_PVHV) \
773 { \
764 HE *he = hv_fetch_ent((HV *)SvRV(arg), ucoro_state_sv, 0, ucoro_state_hash); 774 HE *he = hv_fetch_ent((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \
765 775 \
766 if (!he) 776 if (!he) \
767 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", funcname, varname); 777 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \
768 778 \
769 arg = HeVAL(he); 779 (sv) = SvRV (HeVAL(he)); \
770 } 780 } \
771 781 \
772 /* must also be changed inside Coro::Cont::yield */ 782 /* must also be changed inside Coro::Cont::yield */ \
773 if (SvROK(arg) && SvOBJECT(SvRV(arg)) 783 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); 784 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
778 /*NORETURN*/ 785 \
779} 786 } while(0)
787
788#define SvSTATE(sv) (struct coro *)SvIV (sv)
780 789
781static void 790static void
782api_transfer(pTHX_ SV *prev, SV *next, int flags) 791api_transfer(pTHX_ SV *prev, SV *next, int flags)
783{ 792{
784 transfer(aTHX_ 793 SV_CORO (prev, "Coro::transfer");
785 sv_to_coro (prev, "Coro::transfer", "prev"), 794 SV_CORO (next, "Coro::transfer");
786 sv_to_coro (next, "Coro::transfer", "next"), 795
787 flags); 796 transfer(aTHX_ SvSTATE(prev), SvSTATE(next), flags);
788} 797}
789 798
790/** Coro ********************************************************************/ 799/** Coro ********************************************************************/
791 800
792#define PRIO_MAX 3 801#define PRIO_MAX 3
802static int coro_nready; 811static int coro_nready;
803 812
804static void 813static void
805coro_enq (SV *sv) 814coro_enq (SV *sv)
806{ 815{
807 if (SvROK (sv))
808 {
809 SV *hv = SvRV (sv);
810 if (SvTYPE (hv) == SVt_PVHV) 816 if (SvTYPE (sv) == SVt_PVHV)
811 { 817 {
812 SV **xprio = hv_fetch ((HV *)hv, "prio", 4, 0); 818 SV **xprio = hv_fetch ((HV *)sv, "prio", 4, 0);
813 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; 819 int prio = xprio ? SvIV (*xprio) : PRIO_NORMAL;
814 820
815 prio = prio > PRIO_MAX ? PRIO_MAX 821 prio = prio > PRIO_MAX ? PRIO_MAX
816 : prio < PRIO_MIN ? PRIO_MIN 822 : prio < PRIO_MIN ? PRIO_MIN
817 : prio; 823 : prio;
818 824
819 av_push (coro_ready [prio - PRIO_MIN], sv); 825 av_push (coro_ready [prio - PRIO_MIN], sv);
820 coro_nready++; 826 coro_nready++;
821 827
822 return; 828 return;
823 }
824 } 829 }
825 830
826 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 831 croak ("Coro::ready tried to enqueue something that is not a coroutine");
827} 832}
828 833
846} 851}
847 852
848static void 853static void
849api_ready (SV *coro) 854api_ready (SV *coro)
850{ 855{
856 if (SvROK (coro))
857 coro = SvRV (coro);
858
851 coro_enq (SvREFCNT_inc (coro)); 859 coro_enq (SvREFCNT_inc (coro));
852} 860}
853 861
854static void 862static void
855api_schedule (int cede) 863api_schedule (void)
856{ 864{
857 SV *prev, *next; 865 SV *prev, *next;
858 866
859 prev = GvSV (coro_current); 867 prev = SvRV (GvSV (coro_current));
860
861 if (cede)
862 coro_enq (SvREFCNT_inc (prev));
863
864 next = coro_deq (PRIO_MIN); 868 next = coro_deq (PRIO_MIN);
865 869
866 if (!next) 870 if (!next)
867 next = SvREFCNT_inc (GvSV (coro_idle)); 871 next = SvREFCNT_inc (SvRV (GvSV (coro_idle)));
868 872
869 GvSV (coro_current) = SvREFCNT_inc (next); 873 coro_mortal = prev;
870 transfer (aTHX_ 874 SV_CORO (prev, "Coro::schedule");
871 sv_to_coro (prev, "Coro::schedule", "current coroutine"), 875
872 sv_to_coro (next, "Coro::schedule", "next coroutine"), 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;
1043} 1060}
1044 1061
1045void 1062void
1046ready(self) 1063ready(self)
1047 SV * self 1064 SV * self
1065 PROTOTYPE: $
1048 CODE: 1066 CODE:
1049 api_ready (self); 1067 api_ready (self);
1050 1068
1051int 1069int
1052nready(...) 1070nready(...)
1057 RETVAL 1075 RETVAL
1058 1076
1059void 1077void
1060schedule(...) 1078schedule(...)
1061 PROTOTYPE: 1079 PROTOTYPE:
1062 ALIAS:
1063 cede = 1
1064 CODE: 1080 CODE:
1065 api_schedule (ix); 1081 api_schedule ();
1066 1082
1083void
1084cede(...)
1085 PROTOTYPE:
1086 CODE:
1087 api_cede ();
1088

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines