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.130 by root, Mon Dec 25 13:25:55 2006 UTC vs.
Revision 1.133 by root, Thu Jan 4 20:14:19 2007 UTC

93 93
94#define IN_DESTRUCT (PL_main_cv == Nullcv) 94#define IN_DESTRUCT (PL_main_cv == Nullcv)
95 95
96#if __GNUC__ >= 3 96#if __GNUC__ >= 3
97# define attribute(x) __attribute__(x) 97# define attribute(x) __attribute__(x)
98# define BARRIER __asm__ __volatile__ ("" : : : "memory")
98#else 99#else
99# define attribute(x) 100# define attribute(x)
101# define BARRIER
100#endif 102#endif
101 103
102#define NOINLINE attribute ((noinline)) 104#define NOINLINE attribute ((noinline))
103 105
104#include "CoroAPI.h" 106#include "CoroAPI.h"
135 /* the stack */ 137 /* the stack */
136 void *sptr; 138 void *sptr;
137 long ssize; /* positive == mmap, otherwise malloc */ 139 long ssize; /* positive == mmap, otherwise malloc */
138 140
139 /* cpu state */ 141 /* cpu state */
140 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 142 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
143 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
141 JMPENV *top_env; 144 JMPENV *top_env;
142 coro_context cctx; 145 coro_context cctx;
143 146
144 int inuse; 147 int inuse;
145 148
147 int valgrind_id; 150 int valgrind_id;
148#endif 151#endif
149} coro_cctx; 152} coro_cctx;
150 153
151enum { 154enum {
152 CF_RUNNING = 0x0001, /* coroutine is running */ 155 CF_RUNNING = 0x0001, /* coroutine is running */
153 CF_READY = 0x0002, /* coroutine is ready */ 156 CF_READY = 0x0002, /* coroutine is ready */
154 CF_NEW = 0x0004, /* ahs never been switched to */ 157 CF_NEW = 0x0004, /* has never been switched to */
158 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
155}; 159};
156 160
157/* this is a structure representing a perl-level coroutine */ 161/* this is a structure representing a perl-level coroutine */
158struct coro { 162struct coro {
159 /* the c coroutine allocated to this perl coroutine, if any */ 163 /* the c coroutine allocated to this perl coroutine, if any */
706 710
707 /* sometimes transfer is only called to set idle_sp */ 711 /* sometimes transfer is only called to set idle_sp */
708 if (!next) 712 if (!next)
709 { 713 {
710 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 714 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
711 assert (((coro_cctx *)prev)->top_env = PL_top_env); /* just for the side effetc when assert is enabled */ 715 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
712 } 716 }
713 else if (prev != next) 717 else if (prev != next)
714 { 718 {
715 coro_cctx *prev__cctx; 719 coro_cctx *prev__cctx;
716 720
727 if (!prev->flags & CF_RUNNING) 731 if (!prev->flags & CF_RUNNING)
728 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); 732 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
729 733
730 if (next->flags & CF_RUNNING) 734 if (next->flags & CF_RUNNING)
731 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 735 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
736
737 if (next->flags & CF_DESTROYED)
738 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
732 739
733 prev->flags &= ~CF_RUNNING; 740 prev->flags &= ~CF_RUNNING;
734 next->flags |= CF_RUNNING; 741 next->flags |= CF_RUNNING;
735 742
736 LOCK; 743 LOCK;
757 764
758 /* possibly "free" the cctx */ 765 /* possibly "free" the cctx */
759 if (prev__cctx->idle_sp == STACKLEVEL) 766 if (prev__cctx->idle_sp == STACKLEVEL)
760 { 767 {
761 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 768 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
762 assert (PL_top_env == prev__cctx->top_env); 769 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
763 770
764 prev->cctx = 0; 771 prev->cctx = 0;
765 772
766 cctx_put (prev__cctx); 773 cctx_put (prev__cctx);
767 prev__cctx->inuse = 0; 774 prev__cctx->inuse = 0;
780 PL_top_env = next->cctx->top_env; 787 PL_top_env = next->cctx->top_env;
781 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 788 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
782 } 789 }
783 790
784 free_coro_mortal (); 791 free_coro_mortal ();
785
786 UNLOCK; 792 UNLOCK;
787 } 793 }
788} 794}
789 795
790struct transfer_args 796struct transfer_args
792 struct coro *prev, *next; 798 struct coro *prev, *next;
793}; 799};
794 800
795#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 801#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
796 802
797static void 803static int
798coro_state_destroy (struct coro *coro) 804coro_state_destroy (struct coro *coro)
799{ 805{
800 if (coro->refcnt--) 806 if (coro->refcnt--)
801 return; 807 return 0;
808
809 if (coro->flags & CF_DESTROYED)
810 return 0;
811
812 coro->flags |= CF_DESTROYED;
802 813
803 if (coro->mainstack && coro->mainstack != main_mainstack) 814 if (coro->mainstack && coro->mainstack != main_mainstack)
804 { 815 {
816 assert (!(coro->flags & CF_RUNNING));
817
805 struct coro temp; 818 struct coro temp;
806 Zero (&temp, 1, struct coro); 819 Zero (&temp, 1, struct coro);
807 temp.save = CORO_SAVE_ALL; 820 temp.save = CORO_SAVE_ALL;
808 821
809 if (coro->flags & CF_RUNNING) 822 if (coro->flags & CF_RUNNING)
819 coro->mainstack = 0; 832 coro->mainstack = 0;
820 } 833 }
821 834
822 cctx_destroy (coro->cctx); 835 cctx_destroy (coro->cctx);
823 SvREFCNT_dec (coro->args); 836 SvREFCNT_dec (coro->args);
824 Safefree (coro); 837
838 return 1;
825} 839}
826 840
827static int 841static int
828coro_state_clear (pTHX_ SV *sv, MAGIC *mg) 842coro_state_clear (pTHX_ SV *sv, MAGIC *mg)
829{ 843{
830 struct coro *coro = (struct coro *)mg->mg_ptr; 844 struct coro *coro = (struct coro *)mg->mg_ptr;
831 mg->mg_ptr = 0; 845 mg->mg_ptr = 0;
832 846
833 coro_state_destroy (coro); 847 coro_state_destroy (coro);
848
849 if (!coro->refcnt)
850 Safefree (coro);
834 851
835 return 0; 852 return 0;
836} 853}
837 854
838static int 855static int
975} 992}
976 993
977static void 994static void
978prepare_schedule (struct transfer_args *ta) 995prepare_schedule (struct transfer_args *ta)
979{ 996{
980 SV *prev, *next; 997 SV *prev_sv, *next_sv;
981 998
982 for (;;) 999 for (;;)
983 { 1000 {
984 LOCK; 1001 LOCK;
985 next = coro_deq (PRIO_MIN); 1002 next_sv = coro_deq (PRIO_MIN);
986 UNLOCK; 1003 UNLOCK;
987 1004
1005 /* nothing to schedule: call the idle handler */
988 if (next) 1006 if (!next_sv)
989 break;
990
991 { 1007 {
992 dSP; 1008 dSP;
993 1009
994 ENTER; 1010 ENTER;
995 SAVETMPS; 1011 SAVETMPS;
996 1012
997 PUSHMARK (SP); 1013 PUSHMARK (SP);
998 PUTBACK; 1014 PUTBACK;
999 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); 1015 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1000 1016
1001 FREETMPS; 1017 FREETMPS;
1002 LEAVE; 1018 LEAVE;
1019 continue;
1003 } 1020 }
1004 }
1005 1021
1006 prev = SvRV (coro_current); 1022 ta->next = SvSTATE (next_sv);
1007 SvRV_set (coro_current, next); 1023
1024 /* cannot transfer to destroyed coros, skip and look for next */
1025 if (ta->next->flags & CF_DESTROYED)
1026 {
1027 SvREFCNT_dec (next_sv);
1028 continue;
1029 }
1030
1031 break;
1032 }
1008 1033
1009 /* free this only after the transfer */ 1034 /* free this only after the transfer */
1035 prev_sv = SvRV (coro_current);
1036 SvRV_set (coro_current, next_sv);
1037 ta->prev = SvSTATE (prev_sv);
1038
1039 assert (ta->next->flags & CF_READY);
1040 ta->next->flags &= ~CF_READY;
1041
1010 LOCK; 1042 LOCK;
1011 free_coro_mortal (); 1043 free_coro_mortal ();
1044 coro_mortal = prev_sv;
1012 UNLOCK; 1045 UNLOCK;
1013 coro_mortal = prev;
1014
1015 assert (!SvROK(prev));//D
1016 assert (!SvROK(next));//D
1017
1018 ta->prev = SvSTATE (prev);
1019 ta->next = SvSTATE (next);
1020
1021 assert (ta->next->flags & CF_READY);
1022 ta->next->flags &= ~CF_READY;
1023} 1046}
1024 1047
1025static void 1048static void
1026prepare_cede (struct transfer_args *ta) 1049prepare_cede (struct transfer_args *ta)
1027{ 1050{
1028 api_ready (coro_current); 1051 api_ready (coro_current);
1029
1030 prepare_schedule (ta); 1052 prepare_schedule (ta);
1053}
1054
1055static int
1056prepare_cede_notself (struct transfer_args *ta)
1057{
1058 if (coro_nready)
1059 {
1060 SV *prev = SvRV (coro_current);
1061 prepare_schedule (ta);
1062 api_ready (prev);
1063 return 1;
1064 }
1065 else
1066 return 0;
1031} 1067}
1032 1068
1033static void 1069static void
1034api_schedule (void) 1070api_schedule (void)
1035{ 1071{
1037 1073
1038 prepare_schedule (&ta); 1074 prepare_schedule (&ta);
1039 TRANSFER (ta); 1075 TRANSFER (ta);
1040} 1076}
1041 1077
1042static void 1078static int
1043api_cede (void) 1079api_cede (void)
1044{ 1080{
1045 struct transfer_args ta; 1081 struct transfer_args ta;
1046 1082
1047 prepare_cede (&ta); 1083 prepare_cede (&ta);
1084
1085 if (ta.prev != ta.next)
1086 {
1048 TRANSFER (ta); 1087 TRANSFER (ta);
1088 return 1;
1089 }
1090 else
1091 return 0;
1092}
1093
1094static int
1095api_cede_notself (void)
1096{
1097 struct transfer_args ta;
1098
1099 if (prepare_cede_notself (&ta))
1100 {
1101 TRANSFER (ta);
1102 return 1;
1103 }
1104 else
1105 return 0;
1049} 1106}
1050 1107
1051MODULE = Coro::State PACKAGE = Coro::State 1108MODULE = Coro::State PACKAGE = Coro::State
1052 1109
1053PROTOTYPES: DISABLE 1110PROTOTYPES: DISABLE
1109_set_stacklevel (...) 1166_set_stacklevel (...)
1110 ALIAS: 1167 ALIAS:
1111 Coro::State::transfer = 1 1168 Coro::State::transfer = 1
1112 Coro::schedule = 2 1169 Coro::schedule = 2
1113 Coro::cede = 3 1170 Coro::cede = 3
1171 Coro::cede_notself = 4
1114 CODE: 1172 CODE:
1115{ 1173{
1116 struct transfer_args ta; 1174 struct transfer_args ta;
1117 1175
1118 switch (ix) 1176 switch (ix)
1134 break; 1192 break;
1135 1193
1136 case 3: 1194 case 3:
1137 prepare_cede (&ta); 1195 prepare_cede (&ta);
1138 break; 1196 break;
1197
1198 case 4:
1199 if (!prepare_cede_notself (&ta))
1200 XSRETURN_EMPTY;
1201
1202 break;
1139 } 1203 }
1140 1204
1205 BARRIER;
1141 TRANSFER (ta); 1206 TRANSFER (ta);
1142} 1207}
1143 1208
1144void 1209bool
1145_clone_state_from (SV *dst, SV *src) 1210_destroy (SV *coro_sv)
1146 CODE: 1211 CODE:
1147{ 1212 RETVAL = coro_state_destroy (SvSTATE (coro_sv));
1148 struct coro *coro_src = SvSTATE (src); 1213 OUTPUT:
1149 1214 RETVAL
1150 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1151
1152 ++coro_src->refcnt;
1153 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1154}
1155 1215
1156void 1216void
1157_exit (code) 1217_exit (code)
1158 int code 1218 int code
1159 PROTOTYPE: $ 1219 PROTOTYPE: $
1196 coro_ready[i] = newAV (); 1256 coro_ready[i] = newAV ();
1197 1257
1198 { 1258 {
1199 SV *sv = perl_get_sv("Coro::API", 1); 1259 SV *sv = perl_get_sv("Coro::API", 1);
1200 1260
1201 coroapi.schedule = api_schedule; 1261 coroapi.schedule = api_schedule;
1202 coroapi.save = api_save; 1262 coroapi.save = api_save;
1203 coroapi.cede = api_cede; 1263 coroapi.cede = api_cede;
1264 coroapi.cede_notself = api_cede_notself;
1204 coroapi.ready = api_ready; 1265 coroapi.ready = api_ready;
1205 coroapi.is_ready = api_is_ready; 1266 coroapi.is_ready = api_is_ready;
1206 coroapi.nready = &coro_nready; 1267 coroapi.nready = &coro_nready;
1207 coroapi.current = coro_current; 1268 coroapi.current = coro_current;
1208 1269
1209 GCoroAPI = &coroapi; 1270 GCoroAPI = &coroapi;
1210 sv_setiv (sv, (IV)&coroapi); 1271 sv_setiv (sv, (IV)&coroapi);
1211 SvREADONLY_on (sv); 1272 SvREADONLY_on (sv);
1212 } 1273 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines