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.125 by root, Mon Dec 11 20:54:45 2006 UTC

38# ifndef IS_PADCONST 38# ifndef IS_PADCONST
39# define IS_PADCONST(v) 0 39# define IS_PADCONST(v) 0
40# endif 40# endif
41#endif 41#endif
42 42
43/* 5.8.7 */
44#ifndef SvRV_set
45# define SvRV_set(s,v) SvRV(s) = (v)
46#endif
47
43#include <stdio.h> 48#include <stdio.h>
44#include <errno.h> 49#include <errno.h>
50#include <assert.h>
45 51
46#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 52#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
47# undef STACKGUARD 53# undef STACKGUARD
48#endif 54#endif
49 55
90 96
91#define NOINLINE attribute ((noinline)) 97#define NOINLINE attribute ((noinline))
92 98
93#include "CoroAPI.h" 99#include "CoroAPI.h"
94 100
95#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
96
97#ifdef USE_ITHREADS 101#ifdef USE_ITHREADS
98static perl_mutex coro_mutex; 102static perl_mutex coro_mutex;
99# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 103# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
100# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 104# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
101#else 105#else
151 coro_cctx *cctx; 155 coro_cctx *cctx;
152 156
153 /* data associated with this coroutine (initial args) */ 157 /* data associated with this coroutine (initial args) */
154 AV *args; 158 AV *args;
155 int refcnt; 159 int refcnt;
156 int flags; 160 int save; /* CORO_SAVE flags */
161 int flags; /* CF_ flags */
157 162
158 /* optionally saved, might be zero */ 163 /* optionally saved, might be zero */
159 AV *defav; 164 AV *defav; /* @_ */
160 SV *defsv; 165 SV *defsv; /* $_ */
161 SV *errsv; 166 SV *errsv; /* $@ */
167 SV *irssv; /* $/ */
168 SV *irssv_sv; /* real $/ cache */
162 169
163#define VAR(name,type) type name; 170#define VAR(name,type) type name;
164# include "state.h" 171# include "state.h"
165#undef VAR 172#undef VAR
166 173
289} 296}
290 297
291#define SB do { 298#define SB do {
292#define SE } while (0) 299#define SE } while (0)
293 300
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 301#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
298 302
299static void 303static void
300load_state (Coro__State c) 304load_perl (Coro__State c)
301{ 305{
302#define VAR(name,type) PL_ ## name = c->name; 306#define VAR(name,type) PL_ ## name = c->name;
303# include "state.h" 307# include "state.h"
304#undef VAR 308#undef VAR
305 309
306 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 310 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
307 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 311 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
308 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 312 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
313 if (c->irssv)
314 {
315 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
316 SvREFCNT_dec (c->irssv);
317 else
318 {
319 REPLACE_SV (PL_rs, c->irssv);
320 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
321 sv_setsv (c->irssv_sv, PL_rs);
322 }
323 }
309 324
310 { 325 {
311 dSP; 326 dSP;
312 CV *cv; 327 CV *cv;
313 328
322 PUTBACK; 337 PUTBACK;
323 } 338 }
324} 339}
325 340
326static void 341static void
327save_state (Coro__State c, int flags) 342save_perl (Coro__State c)
328{ 343{
329 { 344 {
330 dSP; 345 dSP;
331 I32 cxix = cxstack_ix; 346 I32 cxix = cxstack_ix;
332 PERL_CONTEXT *ccstk = cxstack; 347 PERL_CONTEXT *ccstk = cxstack;
380 } 395 }
381 396
382 PUTBACK; 397 PUTBACK;
383 } 398 }
384 399
385 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 400 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; 401 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
387 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 402 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
403 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0;
388 404
389#define VAR(name,type)c->name = PL_ ## name; 405#define VAR(name,type)c->name = PL_ ## name;
390# include "state.h" 406# include "state.h"
391#undef VAR 407#undef VAR
392} 408}
583{ 599{
584 coro_cctx *cctx; 600 coro_cctx *cctx;
585 601
586 ++cctx_count; 602 ++cctx_count;
587 603
588 New (0, cctx, 1, coro_cctx); 604 Newz (0, cctx, 1, coro_cctx);
589 605
590#if HAVE_MMAP 606#if HAVE_MMAP
591 607
592 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 608 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
593 /* mmap supposedly does allocate-on-write for us */ 609 /* mmap supposedly does allocate-on-write for us */
688 cctx_first = cctx; 704 cctx_first = cctx;
689} 705}
690 706
691/* never call directly, always through the coro_state_transfer global variable */ 707/* never call directly, always through the coro_state_transfer global variable */
692static void NOINLINE 708static void NOINLINE
693transfer (struct coro *prev, struct coro *next, int flags) 709transfer (struct coro *prev, struct coro *next)
694{ 710{
695 dSTACKLEVEL; 711 dSTACKLEVEL;
696 712
697 /* sometimes transfer is only called to set idle_sp */ 713 /* sometimes transfer is only called to set idle_sp */
698 if (flags == TRANSFER_SET_STACKLEVEL) 714 if (!next)
715 {
699 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 716 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
717 assert (((coro_cctx *)prev)->top_env = PL_top_env); /* just for the side effetc when assert is enabled */
718 }
700 else if (prev != next) 719 else if (prev != next)
701 { 720 {
702 coro_cctx *prev__cctx; 721 coro_cctx *prev__cctx;
703 722
704 if (prev->flags & CF_NEW) 723 if (prev->flags & CF_NEW)
725 if (next->flags & CF_NEW) 744 if (next->flags & CF_NEW)
726 { 745 {
727 /* need to start coroutine */ 746 /* need to start coroutine */
728 next->flags &= ~CF_NEW; 747 next->flags &= ~CF_NEW;
729 /* first get rid of the old state */ 748 /* first get rid of the old state */
730 SAVE (prev, -1); 749 save_perl (prev);
731 /* setup coroutine call */ 750 /* setup coroutine call */
732 setup_coro (next); 751 setup_coro (next);
733 /* need a new stack */ 752 /* need a new stack */
734 assert (!next->stack); 753 assert (!next->cctx);
735 } 754 }
736 else 755 else
737 { 756 {
738 /* coroutine already started */ 757 /* coroutine already started */
739 SAVE (prev, flags); 758 save_perl (prev);
740 LOAD (next); 759 load_perl (next);
741 } 760 }
742 761
743 prev__cctx = prev->cctx; 762 prev__cctx = prev->cctx;
744 763
745 /* possibly "free" the cctx */ 764 /* possibly "free" the cctx */
775} 794}
776 795
777struct transfer_args 796struct transfer_args
778{ 797{
779 struct coro *prev, *next; 798 struct coro *prev, *next;
780 int flags;
781}; 799};
782 800
783#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) 801#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
784 802
785static void 803static void
786coro_state_destroy (struct coro *coro) 804coro_state_destroy (struct coro *coro)
787{ 805{
788 if (coro->refcnt--) 806 if (coro->refcnt--)
789 return; 807 return;
790 808
791 if (coro->mainstack && coro->mainstack != main_mainstack) 809 if (coro->mainstack && coro->mainstack != main_mainstack)
792 { 810 {
793 struct coro temp; 811 struct coro temp;
812 Zero (&temp, 1, struct coro);
813 temp.save = CORO_SAVE_ALL;
794 814
795 if (coro->flags & CF_RUNNING) 815 if (coro->flags & CF_RUNNING)
796 croak ("FATAL: tried to destroy currently running coroutine"); 816 croak ("FATAL: tried to destroy currently running coroutine");
797 817
798 SAVE ((&temp), TRANSFER_SAVE_ALL); 818 save_perl (&temp);
799 LOAD (coro); 819 load_perl (coro);
800 820
801 coro_destroy_stacks (); 821 coro_destroy_stacks ();
802 822
803 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 823 load_perl (&temp); /* this will get rid of defsv etc.. */
804 824
805 coro->mainstack = 0; 825 coro->mainstack = 0;
806 } 826 }
807 827
808 cctx_destroy (coro->cctx); 828 cctx_destroy (coro->cctx);
863 assert (mg->mg_type == PERL_MAGIC_ext); 883 assert (mg->mg_type == PERL_MAGIC_ext);
864 return (struct coro *)mg->mg_ptr; 884 return (struct coro *)mg->mg_ptr;
865} 885}
866 886
867static void 887static void
868prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) 888prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv)
869{ 889{
870 ta->prev = SvSTATE (prev); 890 ta->prev = SvSTATE (prev_sv);
871 ta->next = SvSTATE (next); 891 ta->next = SvSTATE (next_sv);
872 ta->flags = flags;
873} 892}
874 893
875static void 894static void
876api_transfer (SV *prev, SV *next, int flags) 895api_transfer (SV *prev_sv, SV *next_sv)
877{ 896{
878 struct transfer_args ta; 897 struct transfer_args ta;
879 898
880 prepare_transfer (&ta, prev, next, flags); 899 prepare_transfer (&ta, prev_sv, next_sv);
881 TRANSFER (ta); 900 TRANSFER (ta);
901}
902
903static int
904api_save (SV *coro_sv, int new_save)
905{
906 struct coro *coro = SvSTATE (coro_sv);
907 int old_save = coro->save;
908
909 if (new_save >= 0)
910 coro->save = new_save;
911
912 return old_save;
882} 913}
883 914
884/** Coro ********************************************************************/ 915/** Coro ********************************************************************/
885 916
886#define PRIO_MAX 3 917#define PRIO_MAX 3
995 assert (!SvROK(prev));//D 1026 assert (!SvROK(prev));//D
996 assert (!SvROK(next));//D 1027 assert (!SvROK(next));//D
997 1028
998 ta->prev = SvSTATE (prev); 1029 ta->prev = SvSTATE (prev);
999 ta->next = SvSTATE (next); 1030 ta->next = SvSTATE (next);
1000 ta->flags = TRANSFER_SAVE_ALL;
1001 1031
1002 assert (ta->flags & CF_READY); 1032 assert (ta->next->flags & CF_READY);
1003 ta->next->flags &= ~CF_READY; 1033 ta->next->flags &= ~CF_READY;
1004} 1034}
1005 1035
1006static void 1036static void
1007prepare_cede (struct transfer_args *ta) 1037prepare_cede (struct transfer_args *ta)
1040#endif 1070#endif
1041 BOOT_PAGESIZE; 1071 BOOT_PAGESIZE;
1042 1072
1043 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1073 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1044 1074
1045 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 1075 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1046 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 1076 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1047 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 1077 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1078 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1079 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1048 1080
1049 main_mainstack = PL_mainstack; 1081 main_mainstack = PL_mainstack;
1050 1082
1051 coroapi.ver = CORO_API_VERSION; 1083 coroapi.ver = CORO_API_VERSION;
1052 coroapi.transfer = api_transfer; 1084 coroapi.transfer = api_transfer;
1062 HV *hv; 1094 HV *hv;
1063 int i; 1095 int i;
1064 1096
1065 Newz (0, coro, 1, struct coro); 1097 Newz (0, coro, 1, struct coro);
1066 coro->args = newAV (); 1098 coro->args = newAV ();
1099 coro->save = CORO_SAVE_ALL;
1067 coro->flags = CF_NEW; 1100 coro->flags = CF_NEW;
1068 1101
1069 hv = newHV (); 1102 hv = newHV ();
1070 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1103 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)); 1104 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1072 1105
1073 for (i = 1; i < items; i++) 1106 for (i = 1; i < items; i++)
1074 av_push (coro->args, newSVsv (ST (i))); 1107 av_push (coro->args, newSVsv (ST (i)));
1075} 1108}
1076 OUTPUT: 1109 OUTPUT:
1110 RETVAL
1111
1112int
1113save (SV *coro, int new_save = -1)
1114 CODE:
1115 RETVAL = api_save (coro, new_save);
1116 OUTPUT:
1077 RETVAL 1117 RETVAL
1078 1118
1079void 1119void
1080_set_stacklevel (...) 1120_set_stacklevel (...)
1081 ALIAS: 1121 ALIAS:
1089 switch (ix) 1129 switch (ix)
1090 { 1130 {
1091 case 0: 1131 case 0:
1092 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); 1132 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1093 ta.next = 0; 1133 ta.next = 0;
1094 ta.flags = TRANSFER_SET_STACKLEVEL;
1095 break; 1134 break;
1096 1135
1097 case 1: 1136 case 1:
1098 if (items != 3) 1137 if (items != 2)
1099 croak ("Coro::State::transfer (prev,next,flags) expects three arguments, not %d", items); 1138 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1100 1139
1101 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); 1140 prepare_transfer (&ta, ST (0), ST (1));
1102 break; 1141 break;
1103 1142
1104 case 2: 1143 case 2:
1105 prepare_schedule (&ta); 1144 prepare_schedule (&ta);
1106 break; 1145 break;
1169 1208
1170 { 1209 {
1171 SV *sv = perl_get_sv("Coro::API", 1); 1210 SV *sv = perl_get_sv("Coro::API", 1);
1172 1211
1173 coroapi.schedule = api_schedule; 1212 coroapi.schedule = api_schedule;
1213 coroapi.save = api_save;
1174 coroapi.cede = api_cede; 1214 coroapi.cede = api_cede;
1175 coroapi.ready = api_ready; 1215 coroapi.ready = api_ready;
1176 coroapi.is_ready = api_is_ready; 1216 coroapi.is_ready = api_is_ready;
1177 coroapi.nready = &coro_nready; 1217 coroapi.nready = &coro_nready;
1178 coroapi.current = coro_current; 1218 coroapi.current = coro_current;
1180 GCoroAPI = &coroapi; 1220 GCoroAPI = &coroapi;
1181 sv_setiv (sv, (IV)&coroapi); 1221 sv_setiv (sv, (IV)&coroapi);
1182 SvREADONLY_on (sv); 1222 SvREADONLY_on (sv);
1183 } 1223 }
1184} 1224}
1225
1226void
1227_set_current (SV *current)
1228 PROTOTYPE: $
1229 CODE:
1230 SvREFCNT_dec (SvRV (coro_current));
1231 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1185 1232
1186int 1233int
1187prio (Coro::State coro, int newprio = 0) 1234prio (Coro::State coro, int newprio = 0)
1188 ALIAS: 1235 ALIAS:
1189 nice = 1 1236 nice = 1
1225 CODE: 1272 CODE:
1226 RETVAL = coro_nready; 1273 RETVAL = coro_nready;
1227 OUTPUT: 1274 OUTPUT:
1228 RETVAL 1275 RETVAL
1229 1276
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 1277MODULE = Coro::State PACKAGE = Coro::AIO
1238 1278
1239SV * 1279SV *
1240_get_state () 1280_get_state ()
1241 CODE: 1281 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines