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.121 by root, Sat Dec 2 20:40:25 2006 UTC vs.
Revision 1.122 by root, Mon Dec 4 13:47:56 2006 UTC

90 90
91#define NOINLINE attribute ((noinline)) 91#define NOINLINE attribute ((noinline))
92 92
93#include "CoroAPI.h" 93#include "CoroAPI.h"
94 94
95#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
96
97#ifdef USE_ITHREADS 95#ifdef USE_ITHREADS
98static perl_mutex coro_mutex; 96static perl_mutex coro_mutex;
99# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 97# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
100# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 98# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
101#else 99#else
151 coro_cctx *cctx; 149 coro_cctx *cctx;
152 150
153 /* data associated with this coroutine (initial args) */ 151 /* data associated with this coroutine (initial args) */
154 AV *args; 152 AV *args;
155 int refcnt; 153 int refcnt;
156 int flags; 154 int save; /* CORO_SAVE flags */
155 int flags; /* CF_ flags */
157 156
158 /* optionally saved, might be zero */ 157 /* optionally saved, might be zero */
159 AV *defav; 158 AV *defav; /* @_ */
160 SV *defsv; 159 SV *defsv; /* $_ */
161 SV *errsv; 160 SV *errsv; /* $@ */
161 SV *irssv; /* $/ */
162 162
163#define VAR(name,type) type name; 163#define VAR(name,type) type name;
164# include "state.h" 164# include "state.h"
165#undef VAR 165#undef VAR
166 166
289} 289}
290 290
291#define SB do { 291#define SB do {
292#define SE } while (0) 292#define SE } while (0)
293 293
294#define LOAD(state) load_state ((state))
295#define SAVE(state,flags) save_state ((state),(flags))
296
297#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE 294#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
298 295
299static void 296static void
300load_state (Coro__State c) 297load_perl (Coro__State c)
301{ 298{
302#define VAR(name,type) PL_ ## name = c->name; 299#define VAR(name,type) PL_ ## name = c->name;
303# include "state.h" 300# include "state.h"
304#undef VAR 301#undef VAR
305 302
306 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 303 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
307 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 304 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
308 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 305 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
306 if (c->irssv) REPLACE_SV (PL_rs , c->irssv);
309 307
310 { 308 {
311 dSP; 309 dSP;
312 CV *cv; 310 CV *cv;
313 311
322 PUTBACK; 320 PUTBACK;
323 } 321 }
324} 322}
325 323
326static void 324static void
327save_state (Coro__State c, int flags) 325save_perl (Coro__State c)
328{ 326{
329 { 327 {
330 dSP; 328 dSP;
331 I32 cxix = cxstack_ix; 329 I32 cxix = cxstack_ix;
332 PERL_CONTEXT *ccstk = cxstack; 330 PERL_CONTEXT *ccstk = cxstack;
380 } 378 }
381 379
382 PUTBACK; 380 PUTBACK;
383 } 381 }
384 382
385 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 383 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
386 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 384 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
387 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 385 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
386 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0;
388 387
389#define VAR(name,type)c->name = PL_ ## name; 388#define VAR(name,type)c->name = PL_ ## name;
390# include "state.h" 389# include "state.h"
391#undef VAR 390#undef VAR
392} 391}
688 cctx_first = cctx; 687 cctx_first = cctx;
689} 688}
690 689
691/* never call directly, always through the coro_state_transfer global variable */ 690/* never call directly, always through the coro_state_transfer global variable */
692static void NOINLINE 691static void NOINLINE
693transfer (struct coro *prev, struct coro *next, int flags) 692transfer (struct coro *prev, struct coro *next)
694{ 693{
695 dSTACKLEVEL; 694 dSTACKLEVEL;
696 695
697 /* sometimes transfer is only called to set idle_sp */ 696 /* sometimes transfer is only called to set idle_sp */
698 if (flags == TRANSFER_SET_STACKLEVEL) 697 if (!next)
699 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 698 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
700 else if (prev != next) 699 else if (prev != next)
701 { 700 {
702 coro_cctx *prev__cctx; 701 coro_cctx *prev__cctx;
703 702
725 if (next->flags & CF_NEW) 724 if (next->flags & CF_NEW)
726 { 725 {
727 /* need to start coroutine */ 726 /* need to start coroutine */
728 next->flags &= ~CF_NEW; 727 next->flags &= ~CF_NEW;
729 /* first get rid of the old state */ 728 /* first get rid of the old state */
730 SAVE (prev, -1); 729 save_perl (prev);
731 /* setup coroutine call */ 730 /* setup coroutine call */
732 setup_coro (next); 731 setup_coro (next);
733 /* need a new stack */ 732 /* need a new stack */
734 assert (!next->stack); 733 assert (!next->stack);
735 } 734 }
736 else 735 else
737 { 736 {
738 /* coroutine already started */ 737 /* coroutine already started */
739 SAVE (prev, flags); 738 save_perl (prev);
740 LOAD (next); 739 load_perl (next);
741 } 740 }
742 741
743 prev__cctx = prev->cctx; 742 prev__cctx = prev->cctx;
744 743
745 /* possibly "free" the cctx */ 744 /* possibly "free" the cctx */
775} 774}
776 775
777struct transfer_args 776struct transfer_args
778{ 777{
779 struct coro *prev, *next; 778 struct coro *prev, *next;
780 int flags;
781}; 779};
782 780
783#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) 781#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
784 782
785static void 783static void
786coro_state_destroy (struct coro *coro) 784coro_state_destroy (struct coro *coro)
787{ 785{
788 if (coro->refcnt--) 786 if (coro->refcnt--)
789 return; 787 return;
790 788
791 if (coro->mainstack && coro->mainstack != main_mainstack) 789 if (coro->mainstack && coro->mainstack != main_mainstack)
792 { 790 {
793 struct coro temp; 791 struct coro temp;
792 Zero (&temp, 1, struct coro);
793 temp.save = CORO_SAVE_ALL;
794 794
795 if (coro->flags & CF_RUNNING) 795 if (coro->flags & CF_RUNNING)
796 croak ("FATAL: tried to destroy currently running coroutine"); 796 croak ("FATAL: tried to destroy currently running coroutine");
797 797
798 SAVE ((&temp), TRANSFER_SAVE_ALL); 798 save_perl (&temp);
799 LOAD (coro); 799 load_perl (coro);
800 800
801 coro_destroy_stacks (); 801 coro_destroy_stacks ();
802 802
803 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 803 load_perl (&temp); /* this will get rid of defsv etc.. */
804 804
805 coro->mainstack = 0; 805 coro->mainstack = 0;
806 } 806 }
807 807
808 cctx_destroy (coro->cctx); 808 cctx_destroy (coro->cctx);
863 assert (mg->mg_type == PERL_MAGIC_ext); 863 assert (mg->mg_type == PERL_MAGIC_ext);
864 return (struct coro *)mg->mg_ptr; 864 return (struct coro *)mg->mg_ptr;
865} 865}
866 866
867static void 867static void
868prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) 868prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv)
869{ 869{
870 ta->prev = SvSTATE (prev); 870 ta->prev = SvSTATE (prev_sv);
871 ta->next = SvSTATE (next); 871 ta->next = SvSTATE (next_sv);
872 ta->flags = flags;
873} 872}
874 873
875static void 874static void
876api_transfer (SV *prev, SV *next, int flags) 875api_transfer (SV *prev_sv, SV *next_sv)
877{ 876{
878 struct transfer_args ta; 877 struct transfer_args ta;
879 878
880 prepare_transfer (&ta, prev, next, flags); 879 prepare_transfer (&ta, prev_sv, next_sv);
881 TRANSFER (ta); 880 TRANSFER (ta);
881}
882
883static int
884api_save (SV *coro_sv, int new_save)
885{
886 struct coro *coro = SvSTATE (coro_sv);
887 int old_save = coro->save;
888
889 if (new_save >= 0)
890 coro->save = new_save;
891
892 return old_save;
882} 893}
883 894
884/** Coro ********************************************************************/ 895/** Coro ********************************************************************/
885 896
886#define PRIO_MAX 3 897#define PRIO_MAX 3
995 assert (!SvROK(prev));//D 1006 assert (!SvROK(prev));//D
996 assert (!SvROK(next));//D 1007 assert (!SvROK(next));//D
997 1008
998 ta->prev = SvSTATE (prev); 1009 ta->prev = SvSTATE (prev);
999 ta->next = SvSTATE (next); 1010 ta->next = SvSTATE (next);
1000 ta->flags = TRANSFER_SAVE_ALL;
1001 1011
1002 assert (ta->flags & CF_READY); 1012 assert (ta->next->flags & CF_READY);
1003 ta->next->flags &= ~CF_READY; 1013 ta->next->flags &= ~CF_READY;
1004} 1014}
1005 1015
1006static void 1016static void
1007prepare_cede (struct transfer_args *ta) 1017prepare_cede (struct transfer_args *ta)
1040#endif 1050#endif
1041 BOOT_PAGESIZE; 1051 BOOT_PAGESIZE;
1042 1052
1043 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1053 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1044 1054
1045 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 1055 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1046 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 1056 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1047 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 1057 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1058 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1059 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1048 1060
1049 main_mainstack = PL_mainstack; 1061 main_mainstack = PL_mainstack;
1050 1062
1051 coroapi.ver = CORO_API_VERSION; 1063 coroapi.ver = CORO_API_VERSION;
1052 coroapi.transfer = api_transfer; 1064 coroapi.transfer = api_transfer;
1062 HV *hv; 1074 HV *hv;
1063 int i; 1075 int i;
1064 1076
1065 Newz (0, coro, 1, struct coro); 1077 Newz (0, coro, 1, struct coro);
1066 coro->args = newAV (); 1078 coro->args = newAV ();
1079 coro->save = CORO_SAVE_ALL;
1067 coro->flags = CF_NEW; 1080 coro->flags = CF_NEW;
1068 1081
1069 hv = newHV (); 1082 hv = newHV ();
1070 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1083 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1071 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1084 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1072 1085
1073 for (i = 1; i < items; i++) 1086 for (i = 1; i < items; i++)
1074 av_push (coro->args, newSVsv (ST (i))); 1087 av_push (coro->args, newSVsv (ST (i)));
1075} 1088}
1076 OUTPUT: 1089 OUTPUT:
1090 RETVAL
1091
1092int
1093save (SV *coro, int new_save = -1)
1094 CODE:
1095 RETVAL = api_save (coro, new_save);
1096 OUTPUT:
1077 RETVAL 1097 RETVAL
1078 1098
1079void 1099void
1080_set_stacklevel (...) 1100_set_stacklevel (...)
1081 ALIAS: 1101 ALIAS:
1089 switch (ix) 1109 switch (ix)
1090 { 1110 {
1091 case 0: 1111 case 0:
1092 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); 1112 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1093 ta.next = 0; 1113 ta.next = 0;
1094 ta.flags = TRANSFER_SET_STACKLEVEL;
1095 break; 1114 break;
1096 1115
1097 case 1: 1116 case 1:
1098 if (items != 3) 1117 if (items != 2)
1099 croak ("Coro::State::transfer (prev,next,flags) expects three arguments, not %d", items); 1118 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1100 1119
1101 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); 1120 prepare_transfer (&ta, ST (0), ST (1));
1102 break; 1121 break;
1103 1122
1104 case 2: 1123 case 2:
1105 prepare_schedule (&ta); 1124 prepare_schedule (&ta);
1106 break; 1125 break;
1169 1188
1170 { 1189 {
1171 SV *sv = perl_get_sv("Coro::API", 1); 1190 SV *sv = perl_get_sv("Coro::API", 1);
1172 1191
1173 coroapi.schedule = api_schedule; 1192 coroapi.schedule = api_schedule;
1193 coroapi.save = api_save;
1174 coroapi.cede = api_cede; 1194 coroapi.cede = api_cede;
1175 coroapi.ready = api_ready; 1195 coroapi.ready = api_ready;
1176 coroapi.is_ready = api_is_ready; 1196 coroapi.is_ready = api_is_ready;
1177 coroapi.nready = &coro_nready; 1197 coroapi.nready = &coro_nready;
1178 coroapi.current = coro_current; 1198 coroapi.current = coro_current;
1180 GCoroAPI = &coroapi; 1200 GCoroAPI = &coroapi;
1181 sv_setiv (sv, (IV)&coroapi); 1201 sv_setiv (sv, (IV)&coroapi);
1182 SvREADONLY_on (sv); 1202 SvREADONLY_on (sv);
1183 } 1203 }
1184} 1204}
1205
1206void
1207_set_current (SV *current)
1208 PROTOTYPE: $
1209 CODE:
1210 SvREFCNT_dec (SvRV (coro_current));
1211 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1185 1212
1186int 1213int
1187prio (Coro::State coro, int newprio = 0) 1214prio (Coro::State coro, int newprio = 0)
1188 ALIAS: 1215 ALIAS:
1189 nice = 1 1216 nice = 1
1225 CODE: 1252 CODE:
1226 RETVAL = coro_nready; 1253 RETVAL = coro_nready;
1227 OUTPUT: 1254 OUTPUT:
1228 RETVAL 1255 RETVAL
1229 1256
1230void
1231_set_current (SV *current)
1232 PROTOTYPE: $
1233 CODE:
1234 SvREFCNT_dec (SvRV (coro_current));
1235 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1236
1237MODULE = Coro::State PACKAGE = Coro::AIO 1257MODULE = Coro::State PACKAGE = Coro::AIO
1238 1258
1239SV * 1259SV *
1240_get_state () 1260_get_state ()
1241 CODE: 1261 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines