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.84 by root, Fri Nov 24 00:31:21 2006 UTC vs.
Revision 1.87 by root, Fri Nov 24 13:40:36 2006 UTC

145 I32 retstack_max; 145 I32 retstack_max;
146 PMOP *curpm; 146 PMOP *curpm;
147 COP *curcop; 147 COP *curcop;
148 JMPENV *top_env; 148 JMPENV *top_env;
149 149
150 /* coro process data */
151 int prio;
152
150 /* data associated with this coroutine (initial args) */ 153 /* data associated with this coroutine (initial args) */
151 AV *args; 154 AV *args;
155 int refcnt;
152}; 156};
153 157
154typedef struct coro *Coro__State; 158typedef struct coro *Coro__State;
155typedef struct coro *Coro__State_or_hashref; 159typedef struct coro *Coro__State_or_hashref;
156 160
772 coro_mortal = 0; 776 coro_mortal = 0;
773 } 777 }
774 UNLOCK; 778 UNLOCK;
775} 779}
776 780
781/* use this function pointer to call the above function */
782/* this is done to increase chances of the compiler not inlining the call */
777void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl; 783void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl;
778 784
779#define SV_CORO(sv,func) \ 785#define SV_CORO(sv,func) \
780 do { \ 786 do { \
781 if (SvROK (sv)) \ 787 if (SvROK (sv)) \
782 sv = SvRV (sv); \ 788 sv = SvRV (sv); \
783 \ 789 \
784 if (SvTYPE (sv) == SVt_PVHV) \ 790 if (SvTYPE (sv) == SVt_PVHV && SvSTASH (sv) != coro_state_stash) \
785 { \ 791 { \
786 HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \ 792 HE *he = hv_fetch_ent ((HV *)sv, ucoro_state_sv, 0, ucoro_state_hash); \
787 \ 793 \
788 if (!he) \ 794 if (!he) \
789 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \ 795 croak ("%s() -- %s is a hashref but lacks the " UCORO_STATE " key", func, # sv); \
795 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \ 801 if (!SvOBJECT (sv) || SvSTASH (sv) != coro_state_stash) \
796 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \ 802 croak ("%s() -- %s is not (and contains not) a Coro::State object", func, # sv); \
797 \ 803 \
798 } while(0) 804 } while(0)
799 805
800#define SvSTATE(sv) INT2PTR (struct coro *, SvIVX (sv)) 806static void
807coro_state_destroy (struct coro *coro)
808{
809 if (coro->refcnt--)
810 return;
811
812 if (coro->mainstack && coro->mainstack != main_mainstack)
813 {
814 struct coro temp;
815
816 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL);
817 LOAD (aTHX_ coro);
818
819 destroy_stacks (aTHX);
820
821 LOAD ((&temp)); /* this will get rid of defsv etc.. */
822
823 coro->mainstack = 0;
824 }
825
826 deallocate_stack (coro);
827 SvREFCNT_dec (coro->args);
828 Safefree (coro);
829}
830
831static int
832coro_state_clear (SV *sv, MAGIC *mg)
833{
834 struct coro *coro = (struct coro *)mg->mg_ptr;
835 mg->mg_ptr = 0;
836
837 coro_state_destroy (coro);
838
839 return 0;
840}
841
842static int
843coro_state_dup (MAGIC *mg, CLONE_PARAMS *params)
844{
845 struct coro *coro = (struct coro *)mg->mg_ptr;
846
847 ++coro->refcnt;
848
849 return 0;
850}
851
852static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 };
853
854static struct coro *
855SvSTATE (SV *coro)
856{
857 MAGIC *mg = SvMAGIC (SvROK (coro) ? SvRV (coro) : coro);
858 assert (mg->mg_type == PERL_MAGIC_ext);
859 return (struct coro *)mg->mg_ptr;
860}
801 861
802static void 862static void
803api_transfer (pTHX_ SV *prev, SV *next, int flags) 863api_transfer (pTHX_ SV *prev, SV *next, int flags)
804{ 864{
805 SV_CORO (prev, "Coro::transfer"); 865 SV_CORO (prev, "Coro::transfer");
823static int coro_nready; 883static int coro_nready;
824 884
825static void 885static void
826coro_enq (pTHX_ SV *sv) 886coro_enq (pTHX_ SV *sv)
827{ 887{
828 SV **xprio;
829 int prio; 888 int prio;
830 889
831 if (SvTYPE (sv) != SVt_PVHV) 890 if (SvTYPE (sv) != SVt_PVHV)
832 croak ("Coro::ready tried to enqueue something that is not a coroutine"); 891 croak ("Coro::ready tried to enqueue something that is not a coroutine");
833 892
834 xprio = hv_fetch ((HV *)sv, "prio", 4, 0); 893 {
835 prio = xprio ? SvIV (*xprio) : PRIO_NORMAL; 894 SV *coro = sv;
836 895 SV_CORO (coro, "omg");
837 prio = prio > PRIO_MAX ? PRIO_MAX 896 prio = SvSTATE (coro)->prio;
838 : prio < PRIO_MIN ? PRIO_MIN 897 }
839 : prio;
840 898
841 av_push (coro_ready [prio - PRIO_MIN], sv); 899 av_push (coro_ready [prio - PRIO_MIN], sv);
842 coro_nready++; 900 coro_nready++;
843} 901}
844 902
915 api_schedule (); 973 api_schedule ();
916} 974}
917 975
918MODULE = Coro::State PACKAGE = Coro::State 976MODULE = Coro::State PACKAGE = Coro::State
919 977
920PROTOTYPES: ENABLE 978PROTOTYPES: DISABLE
921 979
922BOOT: 980BOOT:
923{ /* {} necessary for stoopid perl-5.6.x */ 981{ /* {} necessary for stoopid perl-5.6.x */
924#ifdef USE_ITHREADS 982#ifdef USE_ITHREADS
925 MUTEX_INIT (&coro_mutex); 983 MUTEX_INIT (&coro_mutex);
937 995
938 main_mainstack = PL_mainstack; 996 main_mainstack = PL_mainstack;
939 997
940 coroapi.ver = CORO_API_VERSION; 998 coroapi.ver = CORO_API_VERSION;
941 coroapi.transfer = api_transfer; 999 coroapi.transfer = api_transfer;
942}
943 1000
944Coro::State 1001 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
945_newprocess(args) 1002}
946 SV * args 1003
947 PROTOTYPE: $ 1004SV *
1005new (char *klass, ...)
948 CODE: 1006 CODE:
949 Coro__State coro; 1007{
1008 struct coro *coro;
1009 HV *hv;
1010 int i;
950 1011
951 if (!SvROK (args) || SvTYPE (SvRV (args)) != SVt_PVAV)
952 croak ("Coro::State::_newprocess expects an arrayref");
953
954 Newz (0, coro, 1, struct coro); 1012 Newz (0, coro, 1, struct coro);
1013 coro->args = newAV ();
955 1014
956 coro->args = (AV *)SvREFCNT_inc (SvRV (args)); 1015 for (i = 1; i < items; i++)
1016 av_push (coro->args, newSVsv (ST (i)));
1017
957 /*coro->mainstack = 0; *//*actual work is done inside transfer */ 1018 /*coro->mainstack = 0; *//*actual work is done inside transfer */
958 /*coro->stack = 0;*/ 1019 /*coro->stack = 0;*/
959 1020
960 RETVAL = coro; 1021 hv = newHV ();
1022 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1023 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1024}
961 OUTPUT: 1025 OUTPUT:
962 RETVAL 1026 RETVAL
963 1027
964void 1028void
965transfer(prev, next, flags) 1029transfer(prev, next, flags)
966 SV *prev 1030 SV *prev
967 SV *next 1031 SV *next
968 int flags 1032 int flags
969 PROTOTYPE: @
970 CODE: 1033 CODE:
971 PUTBACK; 1034 PUTBACK;
972 SV_CORO (next, "Coro::transfer"); 1035 SV_CORO (next, "Coro::transfer");
973 SV_CORO (prev, "Coro::transfer"); 1036 SV_CORO (prev, "Coro::transfer");
974 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags); 1037 coro_state_transfer (aTHX_ SvSTATE (prev), SvSTATE (next), flags);
975 SPAGAIN; 1038 SPAGAIN;
976 1039
1040int
1041prio (Coro::State coro, int newprio = 0)
1042 ALIAS:
1043 nice = 1
1044 CODE:
1045{
1046 RETVAL = coro->prio;
1047
1048 if (items > 1)
1049 {
1050 if (ix)
1051 newprio += coro->prio;
1052
1053 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1054 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1055
1056 coro->prio = newprio;
1057 }
1058}
1059
977void 1060void
978DESTROY(coro) 1061_clone_state_from (SV *dst, SV *src)
979 Coro::State coro 1062 CODE:
980 CODE: 1063{
1064 struct coro *coro_src = SvSTATE (src);
981 1065
982 if (coro->mainstack && coro->mainstack != main_mainstack) 1066 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
983 {
984 struct coro temp;
985 1067
986 PUTBACK; 1068 ++coro_src->refcnt;
987 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); 1069 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
988 LOAD (aTHX_ coro); 1070}
989 SPAGAIN;
990
991 destroy_stacks (aTHX);
992
993 LOAD ((&temp)); /* this will get rid of defsv etc.. */
994 SPAGAIN;
995
996 coro->mainstack = 0;
997 }
998
999 deallocate_stack (coro);
1000 SvREFCNT_dec (coro->args);
1001 Safefree (coro);
1002 1071
1003void 1072void
1004_exit(code) 1073_exit (code)
1005 int code 1074 int code
1006 PROTOTYPE: $ 1075 PROTOTYPE: $
1007 CODE: 1076 CODE:
1008 _exit (code); 1077 _exit (code);
1009 1078
1010MODULE = Coro::State PACKAGE = Coro::Cont 1079MODULE = Coro::State PACKAGE = Coro::Cont
1011 1080
1012void 1081void
1013yield(...) 1082yield (...)
1014 PROTOTYPE: @ 1083 PROTOTYPE: @
1015 CODE: 1084 CODE:
1016 SV *yieldstack; 1085 SV *yieldstack;
1017 SV *sv; 1086 SV *sv;
1018 AV *defav = GvAV (PL_defgv); 1087 AV *defav = GvAV (PL_defgv);
1073} 1142}
1074 1143
1075#if !PERL_MICRO 1144#if !PERL_MICRO
1076 1145
1077void 1146void
1078ready(self) 1147ready (self)
1079 SV * self 1148 SV * self
1080 PROTOTYPE: $ 1149 PROTOTYPE: $
1081 CODE: 1150 CODE:
1082 api_ready (self); 1151 api_ready (self);
1083 1152
1084#endif 1153#endif
1085 1154
1086int 1155int
1087nready(...) 1156nready (...)
1088 PROTOTYPE: 1157 PROTOTYPE:
1089 CODE: 1158 CODE:
1090 RETVAL = coro_nready; 1159 RETVAL = coro_nready;
1091 OUTPUT: 1160 OUTPUT:
1092 RETVAL 1161 RETVAL
1093 1162
1094void 1163void
1095schedule(...) 1164schedule (...)
1096 PROTOTYPE: 1165 PROTOTYPE:
1097 CODE: 1166 CODE:
1098 api_schedule (); 1167 api_schedule ();
1099 1168
1100void 1169void
1101cede(...) 1170cede (...)
1102 PROTOTYPE: 1171 PROTOTYPE:
1103 CODE: 1172 CODE:
1104 api_cede (); 1173 api_cede ();
1105 1174
1106# and these are hacks 1175# and these are hacks

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines