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.120 by root, Sat Dec 2 18:01:30 2006 UTC vs.
Revision 1.123 by root, Mon Dec 4 21:56:00 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 SV *irssv_sv; /* real $/ cache */
162 163
163#define VAR(name,type) type name; 164#define VAR(name,type) type name;
164# include "state.h" 165# include "state.h"
165#undef VAR 166#undef VAR
166 167
289} 290}
290 291
291#define SB do { 292#define SB do {
292#define SE } while (0) 293#define SE } while (0)
293 294
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 295#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
298 296
299static void 297static void
300load_state (Coro__State c) 298load_perl (Coro__State c)
301{ 299{
302#define VAR(name,type) PL_ ## name = c->name; 300#define VAR(name,type) PL_ ## name = c->name;
303# include "state.h" 301# include "state.h"
304#undef VAR 302#undef VAR
305 303
306 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 304 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
307 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 305 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
308 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 306 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
307 if (c->irssv)
308 {
309 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
310 SvREFCNT_dec (c->irssv);
311 else
312 {
313 REPLACE_SV (PL_rs, c->irssv);
314 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
315 sv_setsv (c->irssv_sv, PL_rs);
316 }
317 }
309 318
310 { 319 {
311 dSP; 320 dSP;
312 CV *cv; 321 CV *cv;
313 322
322 PUTBACK; 331 PUTBACK;
323 } 332 }
324} 333}
325 334
326static void 335static void
327save_state (Coro__State c, int flags) 336save_perl (Coro__State c)
328{ 337{
329 { 338 {
330 dSP; 339 dSP;
331 I32 cxix = cxstack_ix; 340 I32 cxix = cxstack_ix;
332 PERL_CONTEXT *ccstk = cxstack; 341 PERL_CONTEXT *ccstk = cxstack;
380 } 389 }
381 390
382 PUTBACK; 391 PUTBACK;
383 } 392 }
384 393
385 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 394 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; 395 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
387 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 396 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
397 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0;
388 398
389#define VAR(name,type)c->name = PL_ ## name; 399#define VAR(name,type)c->name = PL_ ## name;
390# include "state.h" 400# include "state.h"
391#undef VAR 401#undef VAR
392} 402}
688 cctx_first = cctx; 698 cctx_first = cctx;
689} 699}
690 700
691/* never call directly, always through the coro_state_transfer global variable */ 701/* never call directly, always through the coro_state_transfer global variable */
692static void NOINLINE 702static void NOINLINE
693transfer (struct coro *prev, struct coro *next, int flags) 703transfer (struct coro *prev, struct coro *next)
694{ 704{
695 dSTACKLEVEL; 705 dSTACKLEVEL;
696 706
697 /* sometimes transfer is only called to set idle_sp */ 707 /* sometimes transfer is only called to set idle_sp */
698 if (flags == TRANSFER_SET_STACKLEVEL) 708 if (!next)
699 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 709 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
700 else if (prev != next) 710 else if (prev != next)
701 { 711 {
702 coro_cctx *prev__cctx; 712 coro_cctx *prev__cctx;
703 713
725 if (next->flags & CF_NEW) 735 if (next->flags & CF_NEW)
726 { 736 {
727 /* need to start coroutine */ 737 /* need to start coroutine */
728 next->flags &= ~CF_NEW; 738 next->flags &= ~CF_NEW;
729 /* first get rid of the old state */ 739 /* first get rid of the old state */
730 SAVE (prev, -1); 740 save_perl (prev);
731 /* setup coroutine call */ 741 /* setup coroutine call */
732 setup_coro (next); 742 setup_coro (next);
733 /* need a new stack */ 743 /* need a new stack */
734 assert (!next->stack); 744 assert (!next->stack);
735 } 745 }
736 else 746 else
737 { 747 {
738 /* coroutine already started */ 748 /* coroutine already started */
739 SAVE (prev, flags); 749 save_perl (prev);
740 LOAD (next); 750 load_perl (next);
741 } 751 }
742 752
743 prev__cctx = prev->cctx; 753 prev__cctx = prev->cctx;
744 754
745 /* possibly "free" the cctx */ 755 /* possibly "free" the cctx */
775} 785}
776 786
777struct transfer_args 787struct transfer_args
778{ 788{
779 struct coro *prev, *next; 789 struct coro *prev, *next;
780 int flags;
781}; 790};
782 791
783#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) 792#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
784 793
785static void 794static void
786coro_state_destroy (struct coro *coro) 795coro_state_destroy (struct coro *coro)
787{ 796{
788 if (coro->refcnt--) 797 if (coro->refcnt--)
789 return; 798 return;
790 799
791 if (coro->mainstack && coro->mainstack != main_mainstack) 800 if (coro->mainstack && coro->mainstack != main_mainstack)
792 { 801 {
793 struct coro temp; 802 struct coro temp;
803 Zero (&temp, 1, struct coro);
804 temp.save = CORO_SAVE_ALL;
794 805
795 if (coro->flags & CF_RUNNING) 806 if (coro->flags & CF_RUNNING)
796 croak ("FATAL: tried to destroy currently running coroutine"); 807 croak ("FATAL: tried to destroy currently running coroutine");
797 808
798 SAVE ((&temp), TRANSFER_SAVE_ALL); 809 save_perl (&temp);
799 LOAD (coro); 810 load_perl (coro);
800 811
801 coro_destroy_stacks (); 812 coro_destroy_stacks ();
802 813
803 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 814 load_perl (&temp); /* this will get rid of defsv etc.. */
804 815
805 coro->mainstack = 0; 816 coro->mainstack = 0;
806 } 817 }
807 818
808 cctx_destroy (coro->cctx); 819 cctx_destroy (coro->cctx);
863 assert (mg->mg_type == PERL_MAGIC_ext); 874 assert (mg->mg_type == PERL_MAGIC_ext);
864 return (struct coro *)mg->mg_ptr; 875 return (struct coro *)mg->mg_ptr;
865} 876}
866 877
867static void 878static void
868prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) 879prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv)
869{ 880{
870 ta->prev = SvSTATE (prev); 881 ta->prev = SvSTATE (prev_sv);
871 ta->next = SvSTATE (next); 882 ta->next = SvSTATE (next_sv);
872 ta->flags = flags;
873} 883}
874 884
875static void 885static void
876api_transfer (SV *prev, SV *next, int flags) 886api_transfer (SV *prev_sv, SV *next_sv)
877{ 887{
878 struct transfer_args ta; 888 struct transfer_args ta;
879 889
880 prepare_transfer (&ta, prev, next, flags); 890 prepare_transfer (&ta, prev_sv, next_sv);
881 TRANSFER (ta); 891 TRANSFER (ta);
892}
893
894static int
895api_save (SV *coro_sv, int new_save)
896{
897 struct coro *coro = SvSTATE (coro_sv);
898 int old_save = coro->save;
899
900 if (new_save >= 0)
901 coro->save = new_save;
902
903 return old_save;
882} 904}
883 905
884/** Coro ********************************************************************/ 906/** Coro ********************************************************************/
885 907
886#define PRIO_MAX 3 908#define PRIO_MAX 3
995 assert (!SvROK(prev));//D 1017 assert (!SvROK(prev));//D
996 assert (!SvROK(next));//D 1018 assert (!SvROK(next));//D
997 1019
998 ta->prev = SvSTATE (prev); 1020 ta->prev = SvSTATE (prev);
999 ta->next = SvSTATE (next); 1021 ta->next = SvSTATE (next);
1000 ta->flags = TRANSFER_SAVE_ALL;
1001 1022
1002 assert (ta->flags & CF_READY); 1023 assert (ta->next->flags & CF_READY);
1003 ta->next->flags &= ~CF_READY; 1024 ta->next->flags &= ~CF_READY;
1004} 1025}
1005 1026
1006static void 1027static void
1007prepare_cede (struct transfer_args *ta) 1028prepare_cede (struct transfer_args *ta)
1040#endif 1061#endif
1041 BOOT_PAGESIZE; 1062 BOOT_PAGESIZE;
1042 1063
1043 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1064 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1044 1065
1045 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 1066 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1046 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 1067 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1047 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 1068 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1069 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1070 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1048 1071
1049 main_mainstack = PL_mainstack; 1072 main_mainstack = PL_mainstack;
1050 1073
1051 coroapi.ver = CORO_API_VERSION; 1074 coroapi.ver = CORO_API_VERSION;
1052 coroapi.transfer = api_transfer; 1075 coroapi.transfer = api_transfer;
1062 HV *hv; 1085 HV *hv;
1063 int i; 1086 int i;
1064 1087
1065 Newz (0, coro, 1, struct coro); 1088 Newz (0, coro, 1, struct coro);
1066 coro->args = newAV (); 1089 coro->args = newAV ();
1090 coro->save = CORO_SAVE_ALL;
1067 coro->flags = CF_NEW; 1091 coro->flags = CF_NEW;
1068 1092
1069 hv = newHV (); 1093 hv = newHV ();
1070 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1094 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)); 1095 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1072 1096
1073 for (i = 1; i < items; i++) 1097 for (i = 1; i < items; i++)
1074 av_push (coro->args, newSVsv (ST (i))); 1098 av_push (coro->args, newSVsv (ST (i)));
1075} 1099}
1076 OUTPUT: 1100 OUTPUT:
1101 RETVAL
1102
1103int
1104save (SV *coro, int new_save = -1)
1105 CODE:
1106 RETVAL = api_save (coro, new_save);
1107 OUTPUT:
1077 RETVAL 1108 RETVAL
1078 1109
1079void 1110void
1080_set_stacklevel (...) 1111_set_stacklevel (...)
1081 ALIAS: 1112 ALIAS:
1089 switch (ix) 1120 switch (ix)
1090 { 1121 {
1091 case 0: 1122 case 0:
1092 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); 1123 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1093 ta.next = 0; 1124 ta.next = 0;
1094 ta.flags = TRANSFER_SET_STACKLEVEL;
1095 break; 1125 break;
1096 1126
1097 case 1: 1127 case 1:
1098 if (items != 3) 1128 if (items != 2)
1099 croak ("Coro::State::transfer (prev,next,flags) expects three arguments, not %d", items); 1129 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1100 1130
1101 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); 1131 prepare_transfer (&ta, ST (0), ST (1));
1102 break; 1132 break;
1103 1133
1104 case 2: 1134 case 2:
1105 prepare_schedule (&ta); 1135 prepare_schedule (&ta);
1106 break; 1136 break;
1169 1199
1170 { 1200 {
1171 SV *sv = perl_get_sv("Coro::API", 1); 1201 SV *sv = perl_get_sv("Coro::API", 1);
1172 1202
1173 coroapi.schedule = api_schedule; 1203 coroapi.schedule = api_schedule;
1204 coroapi.save = api_save;
1174 coroapi.cede = api_cede; 1205 coroapi.cede = api_cede;
1175 coroapi.ready = api_ready; 1206 coroapi.ready = api_ready;
1176 coroapi.is_ready = api_is_ready; 1207 coroapi.is_ready = api_is_ready;
1177 coroapi.nready = &coro_nready; 1208 coroapi.nready = &coro_nready;
1178 coroapi.current = coro_current; 1209 coroapi.current = coro_current;
1180 GCoroAPI = &coroapi; 1211 GCoroAPI = &coroapi;
1181 sv_setiv (sv, (IV)&coroapi); 1212 sv_setiv (sv, (IV)&coroapi);
1182 SvREADONLY_on (sv); 1213 SvREADONLY_on (sv);
1183 } 1214 }
1184} 1215}
1216
1217void
1218_set_current (SV *current)
1219 PROTOTYPE: $
1220 CODE:
1221 SvREFCNT_dec (SvRV (coro_current));
1222 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1185 1223
1186int 1224int
1187prio (Coro::State coro, int newprio = 0) 1225prio (Coro::State coro, int newprio = 0)
1188 ALIAS: 1226 ALIAS:
1189 nice = 1 1227 nice = 1
1225 CODE: 1263 CODE:
1226 RETVAL = coro_nready; 1264 RETVAL = coro_nready;
1227 OUTPUT: 1265 OUTPUT:
1228 RETVAL 1266 RETVAL
1229 1267
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 1268MODULE = Coro::State PACKAGE = Coro::AIO
1238 1269
1239SV * 1270SV *
1240_get_state () 1271_get_state ()
1241 CODE: 1272 CODE:
1242{ 1273{
1274 struct io_state *data;
1275
1243 RETVAL = newSV (sizeof (struct io_state)); 1276 RETVAL = newSV (sizeof (struct io_state));
1244 struct io_state *data = (struct io_state *)SvPVX (RETVAL); 1277 data = (struct io_state *)SvPVX (RETVAL);
1245 SvCUR_set (RETVAL, sizeof (struct io_state)); 1278 SvCUR_set (RETVAL, sizeof (struct io_state));
1246 SvPOK_only (RETVAL); 1279 SvPOK_only (RETVAL);
1247 1280
1248 data->errorno = errno; 1281 data->errorno = errno;
1249 data->laststype = PL_laststype; 1282 data->laststype = PL_laststype;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines