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.254 by root, Fri Nov 7 20:27:47 2008 UTC vs.
Revision 1.267 by root, Fri Nov 14 06:29:52 2008 UTC

46# define BOOT_PAGESIZE (void)0 46# define BOOT_PAGESIZE (void)0
47#endif 47#endif
48 48
49#if CORO_USE_VALGRIND 49#if CORO_USE_VALGRIND
50# include <valgrind/valgrind.h> 50# include <valgrind/valgrind.h>
51# define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end))
52#else
53# define REGISTER_STACK(cctx,start,end)
54#endif 51#endif
55 52
56/* the maximum number of idle cctx that will be pooled */ 53/* the maximum number of idle cctx that will be pooled */
57static int cctx_max_idle = 4; 54static int cctx_max_idle = 4;
58 55
126 123
127#define IN_DESTRUCT (PL_main_cv == Nullcv) 124#define IN_DESTRUCT (PL_main_cv == Nullcv)
128 125
129#if __GNUC__ >= 3 126#if __GNUC__ >= 3
130# define attribute(x) __attribute__(x) 127# define attribute(x) __attribute__(x)
131# define BARRIER __asm__ __volatile__ ("" : : : "memory")
132# define expect(expr,value) __builtin_expect ((expr),(value)) 128# define expect(expr,value) __builtin_expect ((expr),(value))
129# define INLINE static inline
133#else 130#else
134# define attribute(x) 131# define attribute(x)
135# define BARRIER
136# define expect(expr,value) (expr) 132# define expect(expr,value) (expr)
133# define INLINE static
137#endif 134#endif
138 135
139#define expect_false(expr) expect ((expr) != 0, 0) 136#define expect_false(expr) expect ((expr) != 0, 0)
140#define expect_true(expr) expect ((expr) != 0, 1) 137#define expect_true(expr) expect ((expr) != 0, 1)
141 138
142#define NOINLINE attribute ((noinline)) 139#define NOINLINE attribute ((noinline))
143 140
144#include "CoroAPI.h" 141#include "CoroAPI.h"
145 142
146#ifdef USE_ITHREADS 143#ifdef USE_ITHREADS
144
147static perl_mutex coro_lock; 145static perl_mutex coro_lock;
148# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) 146# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0)
149# define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) 147# define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0)
148# if CORO_PTHREAD
149static void *coro_thx;
150# endif
151
150#else 152#else
153
151# define LOCK (void)0 154# define LOCK (void)0
152# define UNLOCK (void)0 155# define UNLOCK (void)0
156
153#endif 157#endif
158
159# undef LOCK
160# define LOCK (void)0
161# undef UNLOCK
162# define UNLOCK (void)0
154 163
155/* helper storage struct for Coro::AIO */ 164/* helper storage struct for Coro::AIO */
156struct io_state 165struct io_state
157{ 166{
158 AV *res; 167 AV *res;
159 int errorno; 168 int errorno;
160 I32 laststype; 169 I32 laststype; /* U16 in 5.10.0 */
161 int laststatval; 170 int laststatval;
162 Stat_t statcache; 171 Stat_t statcache;
163}; 172};
164 173
165static double (*nvtime)(); /* so why doesn't it take void? */ 174static double (*nvtime)(); /* so why doesn't it take void? */
169static struct CoroAPI coroapi; 178static struct CoroAPI coroapi;
170static AV *main_mainstack; /* used to differentiate between $main and others */ 179static AV *main_mainstack; /* used to differentiate between $main and others */
171static JMPENV *main_top_env; 180static JMPENV *main_top_env;
172static HV *coro_state_stash, *coro_stash; 181static HV *coro_state_stash, *coro_stash;
173static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 182static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
183static volatile struct coro *transfer_next;
184
185struct transfer_args
186{
187 struct coro *prev, *next;
188};
174 189
175static GV *irsgv; /* $/ */ 190static GV *irsgv; /* $/ */
176static GV *stdoutgv; /* *STDOUT */ 191static GV *stdoutgv; /* *STDOUT */
177static SV *rv_diehook; 192static SV *rv_diehook;
178static SV *rv_warnhook; 193static SV *rv_warnhook;
280#define PRIO_MIN -4 295#define PRIO_MIN -4
281 296
282/* for Coro.pm */ 297/* for Coro.pm */
283static SV *coro_current; 298static SV *coro_current;
284static SV *coro_readyhook; 299static SV *coro_readyhook;
285static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 300static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
286static int coro_nready; 301static int coro_nready;
287static struct coro *coro_first; 302static struct coro *coro_first;
288 303
289/** lowlevel stuff **********************************************************/ 304/** lowlevel stuff **********************************************************/
290 305
395 : 0 410 : 0
396 411
397#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 412#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
398#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) 413#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
399 414
400static struct coro * 415INLINE struct coro *
401SvSTATE_ (pTHX_ SV *coro) 416SvSTATE_ (pTHX_ SV *coro)
402{ 417{
403 HV *stash; 418 HV *stash;
404 MAGIC *mg; 419 MAGIC *mg;
405 420
696 } 711 }
697 712
698 return rss; 713 return rss;
699} 714}
700 715
716/** set stacklevel support **************************************************/
717
718/* we sometimes need to create the effect of pp_set_stacklevel calling us */
719#define SSL_HEAD (void)0
720/* we sometimes need to create the effect of leaving via pp_set_stacklevel */
721#define SSL_TAIL set_stacklevel_tail (aTHX)
722
723INLINE void
724set_stacklevel_tail (pTHX)
725{
726 dSP;
727 SV **bot = SP;
728
729 int gimme = GIMME_V;
730
731 /* make sure we put something on the stack in scalar context */
732 if (gimme == G_SCALAR)
733 {
734 if (sp == bot)
735 XPUSHs (&PL_sv_undef);
736
737 SP = bot + 1;
738 }
739
740 PUTBACK;
741}
742
701/** coroutine stack handling ************************************************/ 743/** coroutine stack handling ************************************************/
702 744
703static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); 745static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
704static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); 746static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg);
705static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg); 747static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg);
822 PL_rs = newSVsv (GvSV (irsgv)); 864 PL_rs = newSVsv (GvSV (irsgv));
823 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 865 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
824 866
825 { 867 {
826 dSP; 868 dSP;
827 LOGOP myop; 869 UNOP myop;
828 870
829 Zero (&myop, 1, LOGOP); 871 Zero (&myop, 1, UNOP);
830 myop.op_next = Nullop; 872 myop.op_next = Nullop;
831 myop.op_flags = OPf_WANT_VOID; 873 myop.op_flags = OPf_WANT_VOID;
832 874
833 PUSHMARK (SP); 875 PUSHMARK (SP);
834 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 876 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
837 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 879 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
838 SPAGAIN; 880 SPAGAIN;
839 } 881 }
840 882
841 /* this newly created coroutine might be run on an existing cctx which most 883 /* this newly created coroutine might be run on an existing cctx which most
842 * likely was suspended in set_stacklevel, called from entersub. 884 * likely was suspended in set_stacklevel, called from pp_set_stacklevel,
843 * set_stacklevl doesn't do anything on return, but entersub does LEAVE, 885 * so we have to emulate entering pp_set_stacklevel here.
844 * so we ENTER here for symmetry
845 */ 886 */
846 ENTER; 887 SSL_HEAD;
847} 888}
848 889
849static void 890static void
850coro_destruct (pTHX_ struct coro *coro) 891coro_destruct (pTHX_ struct coro *coro)
851{ 892{
880 SvREFCNT_dec (coro->throw); 921 SvREFCNT_dec (coro->throw);
881 922
882 coro_destruct_stacks (aTHX); 923 coro_destruct_stacks (aTHX);
883} 924}
884 925
885static void 926INLINE void
886free_coro_mortal (pTHX) 927free_coro_mortal (pTHX)
887{ 928{
888 if (expect_true (coro_mortal)) 929 if (expect_true (coro_mortal))
889 { 930 {
890 SvREFCNT_dec (coro_mortal); 931 SvREFCNT_dec (coro_mortal);
1014 1055
1015 TAINT_NOT; 1056 TAINT_NOT;
1016 return 0; 1057 return 0;
1017} 1058}
1018 1059
1060static void
1061prepare_set_stacklevel (struct transfer_args *ta, struct coro_cctx *cctx)
1062{
1063 ta->prev = (struct coro *)cctx;
1064 ta->next = 0;
1065}
1066
1019/* inject a fake call to Coro::State::_cctx_init into the execution */ 1067/* inject a fake call to Coro::State::_cctx_init into the execution */
1020/* _cctx_init should be careful, as it could be called at almost any time */ 1068/* _cctx_init should be careful, as it could be called at almost any time */
1021/* during execution of a perl program */ 1069/* during execution of a perl program */
1070/* also initialises PL_top_env */
1022static void NOINLINE 1071static void NOINLINE
1023cctx_prepare (pTHX_ coro_cctx *cctx) 1072cctx_prepare (pTHX_ coro_cctx *cctx)
1024{ 1073{
1025 dSP; 1074 dSP;
1026 LOGOP myop; 1075 UNOP myop;
1027 1076
1028 PL_top_env = &PL_start_env; 1077 PL_top_env = &PL_start_env;
1029 1078
1030 if (cctx->flags & CC_TRACE) 1079 if (cctx->flags & CC_TRACE)
1031 PL_runops = runops_trace; 1080 PL_runops = runops_trace;
1032 1081
1033 Zero (&myop, 1, LOGOP); 1082 Zero (&myop, 1, UNOP);
1034 myop.op_next = PL_op; 1083 myop.op_next = PL_op;
1035 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 1084 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
1036 1085
1037 PUSHMARK (SP); 1086 PUSHMARK (SP);
1038 EXTEND (SP, 2); 1087 EXTEND (SP, 2);
1039 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); 1088 PUSHs (sv_2mortal (newSViv ((IV)cctx)));
1040 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 1089 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
1041 PUTBACK; 1090 PUTBACK;
1042 PL_op = (OP *)&myop; 1091 PL_op = (OP *)&myop;
1043 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 1092 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1044 SPAGAIN; 1093 SPAGAIN;
1045} 1094}
1046 1095
1096/* the tail of transfer: execute stuff we can only do after a transfer */
1097INLINE void
1098transfer_tail (pTHX)
1099{
1100 struct coro *next = (struct coro *)transfer_next;
1101 assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */
1102 assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next));
1103
1104 free_coro_mortal (aTHX);
1105 UNLOCK;
1106
1107 if (expect_false (next->throw))
1108 {
1109 SV *exception = sv_2mortal (next->throw);
1110
1111 next->throw = 0;
1112 sv_setsv (ERRSV, exception);
1113 croak (0);
1114 }
1115}
1116
1047/* 1117/*
1048 * this is a _very_ stripped down perl interpreter ;) 1118 * this is a _very_ stripped down perl interpreter ;)
1049 */ 1119 */
1050static void 1120static void
1051cctx_run (void *arg) 1121cctx_run (void *arg)
1052{ 1122{
1123#ifdef USE_ITHREADS
1124# if CORO_PTHREAD
1125 PERL_SET_CONTEXT (coro_thx);
1126# endif
1127#endif
1128 {
1053 dTHX; 1129 dTHX;
1054 1130
1055 /* cctx_run is the alternative tail of transfer(), so unlock here. */ 1131 /* we are the alternative tail to pp_set_stacklevel */
1056 UNLOCK; 1132 /* so do the same things here */
1133 SSL_TAIL;
1057 1134
1058 /* we now skip the entersub that lead to transfer() */ 1135 /* we now skip the op that did lead to transfer() */
1059 PL_op = PL_op->op_next; 1136 PL_op = PL_op->op_next;
1060 1137
1061 /* inject a fake subroutine call to cctx_init */ 1138 /* inject a fake subroutine call to cctx_init */
1062 cctx_prepare (aTHX_ (coro_cctx *)arg); 1139 cctx_prepare (aTHX_ (coro_cctx *)arg);
1063 1140
1141 /* cctx_run is the alternative tail of transfer() */
1142 transfer_tail (aTHX);
1143
1064 /* somebody or something will hit me for both perl_run and PL_restartop */ 1144 /* somebody or something will hit me for both perl_run and PL_restartop */
1065 PL_restartop = PL_op; 1145 PL_restartop = PL_op;
1066 perl_run (PL_curinterp); 1146 perl_run (PL_curinterp);
1067 1147
1068 /* 1148 /*
1069 * If perl-run returns we assume exit() was being called or the coro 1149 * If perl-run returns we assume exit() was being called or the coro
1070 * fell off the end, which seems to be the only valid (non-bug) 1150 * fell off the end, which seems to be the only valid (non-bug)
1071 * reason for perl_run to return. We try to exit by jumping to the 1151 * reason for perl_run to return. We try to exit by jumping to the
1072 * bootstrap-time "top" top_env, as we cannot restore the "main" 1152 * bootstrap-time "top" top_env, as we cannot restore the "main"
1073 * coroutine as Coro has no such concept 1153 * coroutine as Coro has no such concept
1074 */ 1154 */
1075 PL_top_env = main_top_env; 1155 PL_top_env = main_top_env;
1076 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1156 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1157 }
1077} 1158}
1078 1159
1079static coro_cctx * 1160static coro_cctx *
1080cctx_new () 1161cctx_new ()
1081{ 1162{
1082 coro_cctx *cctx; 1163 coro_cctx *cctx;
1164
1165 ++cctx_count;
1166 New (0, cctx, 1, coro_cctx);
1167
1168 cctx->gen = cctx_gen;
1169 cctx->flags = 0;
1170 cctx->idle_sp = 0; /* can be accessed by transfer between cctx_run and set_stacklevel, on throw */
1171
1172 return cctx;
1173}
1174
1175/* create a new cctx only suitable as source */
1176static coro_cctx *
1177cctx_new_empty ()
1178{
1179 coro_cctx *cctx = cctx_new ();
1180
1181 cctx->sptr = 0;
1182 coro_create (&cctx->cctx, 0, 0, 0, 0);
1183
1184 return cctx;
1185}
1186
1187/* create a new cctx suitable as destination/running a perl interpreter */
1188static coro_cctx *
1189cctx_new_run ()
1190{
1191 coro_cctx *cctx = cctx_new ();
1083 void *stack_start; 1192 void *stack_start;
1084 size_t stack_size; 1193 size_t stack_size;
1085
1086 ++cctx_count;
1087 Newz (0, cctx, 1, coro_cctx);
1088
1089 cctx->gen = cctx_gen;
1090 1194
1091#if HAVE_MMAP 1195#if HAVE_MMAP
1092 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1196 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1093 /* mmap supposedly does allocate-on-write for us */ 1197 /* mmap supposedly does allocate-on-write for us */
1094 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1198 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
1095 1199
1096 if (cctx->sptr != (void *)-1) 1200 if (cctx->sptr != (void *)-1)
1097 { 1201 {
1098# if CORO_STACKGUARD 1202 #if CORO_STACKGUARD
1099 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1203 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1100# endif 1204 #endif
1101 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 1205 stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE;
1102 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 1206 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
1103 cctx->flags |= CC_MAPPED; 1207 cctx->flags |= CC_MAPPED;
1104 } 1208 }
1105 else 1209 else
1106#endif 1210#endif
1107 { 1211 {
1108 cctx->ssize = cctx_stacksize * (long)sizeof (long); 1212 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1109 New (0, cctx->sptr, cctx_stacksize, long); 1213 New (0, cctx->sptr, cctx_stacksize, long);
1110 1214
1111 if (!cctx->sptr) 1215 if (!cctx->sptr)
1112 { 1216 {
1113 perror ("FATAL: unable to allocate stack for coroutine"); 1217 perror ("FATAL: unable to allocate stack for coroutine, exiting.");
1114 _exit (EXIT_FAILURE); 1218 _exit (EXIT_FAILURE);
1115 } 1219 }
1116 1220
1117 stack_start = cctx->sptr; 1221 stack_start = cctx->sptr;
1118 stack_size = cctx->ssize; 1222 stack_size = cctx->ssize;
1119 } 1223 }
1120 1224
1121 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); 1225 #if CORO_USE_VALGRIND
1226 cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size);
1227 #endif
1228
1122 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); 1229 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size);
1123 1230
1124 return cctx; 1231 return cctx;
1125} 1232}
1126 1233
1134 coro_destroy (&cctx->cctx); 1241 coro_destroy (&cctx->cctx);
1135 1242
1136 /* coro_transfer creates new, empty cctx's */ 1243 /* coro_transfer creates new, empty cctx's */
1137 if (cctx->sptr) 1244 if (cctx->sptr)
1138 { 1245 {
1139#if CORO_USE_VALGRIND 1246 #if CORO_USE_VALGRIND
1140 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1247 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1141#endif 1248 #endif
1142 1249
1143#if HAVE_MMAP 1250#if HAVE_MMAP
1144 if (cctx->flags & CC_MAPPED) 1251 if (cctx->flags & CC_MAPPED)
1145 munmap (cctx->sptr, cctx->ssize); 1252 munmap (cctx->sptr, cctx->ssize);
1146 else 1253 else
1167 return cctx; 1274 return cctx;
1168 1275
1169 cctx_destroy (cctx); 1276 cctx_destroy (cctx);
1170 } 1277 }
1171 1278
1172 return cctx_new (); 1279 return cctx_new_run ();
1173} 1280}
1174 1281
1175static void 1282static void
1176cctx_put (coro_cctx *cctx) 1283cctx_put (coro_cctx *cctx)
1177{ 1284{
1178 assert (("cctx_put called on non-initialised cctx", cctx->sptr)); 1285 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr));
1179 1286
1180 /* free another cctx if overlimit */ 1287 /* free another cctx if overlimit */
1181 if (expect_false (cctx_idle >= cctx_max_idle)) 1288 if (expect_false (cctx_idle >= cctx_max_idle))
1182 { 1289 {
1183 coro_cctx *first = cctx_first; 1290 coro_cctx *first = cctx_first;
1227 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1334 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1228 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1335 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1229 } 1336 }
1230 else if (expect_true (prev != next)) 1337 else if (expect_true (prev != next))
1231 { 1338 {
1232 static volatile int has_throw;
1233 coro_cctx *prev__cctx; 1339 coro_cctx *prev__cctx;
1234 1340
1235 if (expect_false (prev->flags & CF_NEW)) 1341 if (expect_false (prev->flags & CF_NEW))
1236 { 1342 {
1237 /* create a new empty/source context */ 1343 /* create a new empty/source context */
1238 ++cctx_count; 1344 prev->cctx = cctx_new_empty ();
1239 New (0, prev->cctx, 1, coro_cctx);
1240 prev->cctx->sptr = 0;
1241 coro_create (&prev->cctx->cctx, 0, 0, 0, 0);
1242
1243 prev->flags &= ~CF_NEW; 1345 prev->flags &= ~CF_NEW;
1244 prev->flags |= CF_RUNNING; 1346 prev->flags |= CF_RUNNING;
1245 } 1347 }
1246 1348
1247 prev->flags &= ~CF_RUNNING; 1349 prev->flags &= ~CF_RUNNING;
1262 else 1364 else
1263 load_perl (aTHX_ next); 1365 load_perl (aTHX_ next);
1264 1366
1265 prev__cctx = prev->cctx; 1367 prev__cctx = prev->cctx;
1266 1368
1267 /* possibly "free" the cctx */ 1369 /* possibly untie and reuse the cctx */
1268 if (expect_true ( 1370 if (expect_true (
1269 prev__cctx->idle_sp == STACKLEVEL 1371 prev__cctx->idle_sp == STACKLEVEL
1270 && !(prev__cctx->flags & CC_TRACE) 1372 && !(prev__cctx->flags & CC_TRACE)
1271 && !force_cctx 1373 && !force_cctx
1272 )) 1374 ))
1273 { 1375 {
1274 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1376 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
1275 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 1377 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te));
1276 1378
1277 prev->cctx = 0; 1379 prev->cctx = 0;
1278 1380
1279 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1381 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1280 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1382 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1288 ++next->usecount; 1390 ++next->usecount;
1289 1391
1290 if (expect_true (!next->cctx)) 1392 if (expect_true (!next->cctx))
1291 next->cctx = cctx_get (aTHX); 1393 next->cctx = cctx_get (aTHX);
1292 1394
1293 has_throw = !!next->throw; 1395 assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next));
1396 transfer_next = next;
1294 1397
1295 if (expect_false (prev__cctx != next->cctx)) 1398 if (expect_false (prev__cctx != next->cctx))
1296 { 1399 {
1297 prev__cctx->top_env = PL_top_env; 1400 prev__cctx->top_env = PL_top_env;
1298 PL_top_env = next->cctx->top_env; 1401 PL_top_env = next->cctx->top_env;
1299 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1402 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
1300 } 1403 }
1301 1404
1302 free_coro_mortal (aTHX); 1405 transfer_tail (aTHX);
1303 UNLOCK;
1304
1305 if (expect_false (has_throw))
1306 {
1307 struct coro *coro = SvSTATE (coro_current);
1308
1309 if (coro->throw)
1310 {
1311 SV *exception = coro->throw;
1312 coro->throw = 0;
1313 sv_setsv (ERRSV, exception);
1314 croak (0);
1315 }
1316 }
1317 } 1406 }
1318} 1407}
1319
1320struct transfer_args
1321{
1322 struct coro *prev, *next;
1323};
1324 1408
1325#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) 1409#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx))
1326#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) 1410#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1327 1411
1328/** high level stuff ********************************************************/ 1412/** high level stuff ********************************************************/
1501 1585
1502static int 1586static int
1503api_is_ready (SV *coro_sv) 1587api_is_ready (SV *coro_sv)
1504{ 1588{
1505 dTHX; 1589 dTHX;
1590
1506 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1591 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1507} 1592}
1508 1593
1509static void 1594INLINE void
1510prepare_schedule (pTHX_ struct transfer_args *ta) 1595prepare_schedule (pTHX_ struct transfer_args *ta)
1511{ 1596{
1512 SV *prev_sv, *next_sv; 1597 SV *prev_sv, *next_sv;
1513 1598
1514 for (;;) 1599 for (;;)
1540 /* cannot transfer to destroyed coros, skip and look for next */ 1625 /* cannot transfer to destroyed coros, skip and look for next */
1541 if (expect_false (ta->next->flags & CF_DESTROYED)) 1626 if (expect_false (ta->next->flags & CF_DESTROYED))
1542 { 1627 {
1543 UNLOCK; 1628 UNLOCK;
1544 SvREFCNT_dec (next_sv); 1629 SvREFCNT_dec (next_sv);
1545 /* coro_nready is already taken care of by destroy */ 1630 /* coro_nready has already been taken care of by destroy */
1546 continue; 1631 continue;
1547 } 1632 }
1548 1633
1549 --coro_nready; 1634 --coro_nready;
1550 UNLOCK; 1635 UNLOCK;
1553 1638
1554 /* free this only after the transfer */ 1639 /* free this only after the transfer */
1555 prev_sv = SvRV (coro_current); 1640 prev_sv = SvRV (coro_current);
1556 ta->prev = SvSTATE (prev_sv); 1641 ta->prev = SvSTATE (prev_sv);
1557 TRANSFER_CHECK (*ta); 1642 TRANSFER_CHECK (*ta);
1558 assert (ta->next->flags & CF_READY); 1643 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY));
1559 ta->next->flags &= ~CF_READY; 1644 ta->next->flags &= ~CF_READY;
1560 SvRV_set (coro_current, next_sv); 1645 SvRV_set (coro_current, next_sv);
1561 1646
1562 LOCK; 1647 LOCK;
1563 free_coro_mortal (aTHX); 1648 free_coro_mortal (aTHX);
1564 coro_mortal = prev_sv; 1649 coro_mortal = prev_sv;
1565 UNLOCK; 1650 UNLOCK;
1566} 1651}
1567 1652
1568static void 1653INLINE void
1569prepare_cede (pTHX_ struct transfer_args *ta) 1654prepare_cede (pTHX_ struct transfer_args *ta)
1570{ 1655{
1571 api_ready (coro_current); 1656 api_ready (coro_current);
1572 prepare_schedule (aTHX_ ta); 1657 prepare_schedule (aTHX_ ta);
1573} 1658}
1574 1659
1575static int 1660static void
1576prepare_cede_notself (pTHX_ struct transfer_args *ta) 1661prepare_cede_notself (pTHX_ struct transfer_args *ta)
1577{ 1662{
1663 SV *prev = SvRV (coro_current);
1664
1578 if (coro_nready) 1665 if (coro_nready)
1579 { 1666 {
1580 SV *prev = SvRV (coro_current);
1581 prepare_schedule (aTHX_ ta); 1667 prepare_schedule (aTHX_ ta);
1582 api_ready (prev); 1668 api_ready (prev);
1669 }
1670 else
1671 ta->prev = ta->next = SvSTATE (prev);
1672}
1673
1674static void
1675api_schedule (void)
1676{
1677 dTHX;
1678 struct transfer_args ta;
1679
1680 prepare_schedule (aTHX_ &ta);
1681 TRANSFER (ta, 1);
1682}
1683
1684static int
1685api_cede (void)
1686{
1687 dTHX;
1688 struct transfer_args ta;
1689
1690 prepare_cede (aTHX_ &ta);
1691
1692 if (expect_true (ta.prev != ta.next))
1693 {
1694 TRANSFER (ta, 1);
1583 return 1; 1695 return 1;
1584 } 1696 }
1585 else 1697 else
1586 return 0; 1698 return 0;
1587} 1699}
1588 1700
1589static void
1590api_schedule (void)
1591{
1592 dTHX;
1593 struct transfer_args ta;
1594
1595 prepare_schedule (aTHX_ &ta);
1596 TRANSFER (ta, 1);
1597}
1598
1599static int 1701static int
1600api_cede (void) 1702api_cede_notself (void)
1601{ 1703{
1704 if (coro_nready)
1705 {
1602 dTHX; 1706 dTHX;
1603 struct transfer_args ta; 1707 struct transfer_args ta;
1604 1708
1605 prepare_cede (aTHX_ &ta); 1709 prepare_cede_notself (aTHX_ &ta);
1606
1607 if (expect_true (ta.prev != ta.next))
1608 {
1609 TRANSFER (ta, 1); 1710 TRANSFER (ta, 1);
1610 return 1; 1711 return 1;
1611 } 1712 }
1612 else 1713 else
1613 return 0; 1714 return 0;
1614} 1715}
1615 1716
1616static int
1617api_cede_notself (void)
1618{
1619 dTHX;
1620 struct transfer_args ta;
1621
1622 if (prepare_cede_notself (aTHX_ &ta))
1623 {
1624 TRANSFER (ta, 1);
1625 return 1;
1626 }
1627 else
1628 return 0;
1629}
1630
1631static void 1717static void
1632api_trace (SV *coro_sv, int flags) 1718api_trace (SV *coro_sv, int flags)
1633{ 1719{
1634 dTHX; 1720 dTHX;
1635 struct coro *coro = SvSTATE (coro_sv); 1721 struct coro *coro = SvSTATE (coro_sv);
1636 1722
1637 if (flags & CC_TRACE) 1723 if (flags & CC_TRACE)
1638 { 1724 {
1639 if (!coro->cctx) 1725 if (!coro->cctx)
1640 coro->cctx = cctx_new (); 1726 coro->cctx = cctx_new_run ();
1641 else if (!(coro->cctx->flags & CC_TRACE)) 1727 else if (!(coro->cctx->flags & CC_TRACE))
1642 croak ("cannot enable tracing on coroutine with custom stack"); 1728 croak ("cannot enable tracing on coroutine with custom stack");
1643 1729
1644 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1730 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1645 } 1731 }
1652 else 1738 else
1653 coro->slot->runops = RUNOPS_DEFAULT; 1739 coro->slot->runops = RUNOPS_DEFAULT;
1654 } 1740 }
1655} 1741}
1656 1742
1743#if 0
1657static int 1744static int
1658coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) 1745coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1659{ 1746{
1660 AV *padlist; 1747 AV *padlist;
1661 AV *av = (AV *)mg->mg_obj; 1748 AV *av = (AV *)mg->mg_obj;
1667 1754
1668static MGVTBL coro_gensub_vtbl = { 1755static MGVTBL coro_gensub_vtbl = {
1669 0, 0, 0, 0, 1756 0, 0, 0, 0,
1670 coro_gensub_free 1757 coro_gensub_free
1671}; 1758};
1759#endif
1672 1760
1673/*****************************************************************************/ 1761/*****************************************************************************/
1674/* PerlIO::cede */ 1762/* PerlIO::cede */
1675 1763
1676typedef struct 1764typedef struct
1743 PerlIOBuf_get_ptr, 1831 PerlIOBuf_get_ptr,
1744 PerlIOBuf_get_cnt, 1832 PerlIOBuf_get_cnt,
1745 PerlIOBuf_set_ptrcnt, 1833 PerlIOBuf_set_ptrcnt,
1746}; 1834};
1747 1835
1836/*****************************************************************************/
1837
1838static const CV *ssl_cv; /* for quick consistency check */
1839
1840static UNOP ssl_restore; /* restore stack as entersub did, for first-re-run */
1841static SV *ssl_arg0;
1842static SV *ssl_arg1;
1843
1844/* this restores the stack in the case we patched the entersub, to */
1845/* recreate the stack frame as perl will on following calls */
1846/* since entersub cleared the stack */
1847static OP *
1848pp_restore (pTHX)
1849{
1850 dSP;
1851
1852 PUSHMARK (SP);
1853
1854 EXTEND (SP, 3);
1855 if (ssl_arg0) PUSHs (sv_2mortal (ssl_arg0)), ssl_arg0 = 0;
1856 if (ssl_arg1) PUSHs (sv_2mortal (ssl_arg1)), ssl_arg1 = 0;
1857 PUSHs ((SV *)CvGV (ssl_cv));
1858
1859 RETURNOP (ssl_restore.op_first);
1860}
1861
1862#define OPpENTERSUB_SSL 15 /* the part of op_private entersub hopefully doesn't use */
1863
1864/* declare prototype */
1865XS(XS_Coro__State__set_stacklevel);
1866
1867/*
1868 * these not obviously related functions are all rolled into one
1869 * function to increase chances that they all will call transfer with the same
1870 * stack offset
1871 */
1872static OP *
1873pp_set_stacklevel (pTHX)
1874{
1875 dSP;
1876 struct transfer_args ta;
1877 SV **arg = PL_stack_base + TOPMARK + 1;
1878 int items = SP - arg; /* args without function object */
1879
1880 /* do a quick consistency check on the "function" object, and if it isn't */
1881 /* for us, divert to the real entersub */
1882 if (SvTYPE (*sp) != SVt_PVGV || CvXSUB (GvCV (*sp)) != XS_Coro__State__set_stacklevel)
1883 return PL_ppaddr[OP_ENTERSUB](aTHX);
1884
1885 /* pop args */
1886 SP = PL_stack_base + POPMARK;
1887
1888 if (!(PL_op->op_flags & OPf_STACKED))
1889 {
1890 /* ampersand-form of call, use @_ instead of stack */
1891 AV *av = GvAV (PL_defgv);
1892 arg = AvARRAY (av);
1893 items = AvFILLp (av) + 1;
1894 }
1895
1896 PUTBACK;
1897 switch (PL_op->op_private & OPpENTERSUB_SSL)
1898 {
1899 case 0:
1900 prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (arg [0]));
1901 break;
1902
1903 case 1:
1904 if (items != 2)
1905 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d.", items);
1906
1907 prepare_transfer (aTHX_ &ta, arg [0], arg [1]);
1908 break;
1909
1910 case 2:
1911 prepare_schedule (aTHX_ &ta);
1912 break;
1913
1914 case 3:
1915 prepare_cede (aTHX_ &ta);
1916 break;
1917
1918 case 4:
1919 prepare_cede_notself (aTHX_ &ta);
1920 break;
1921 }
1922
1923 TRANSFER (ta, 0);
1924 SPAGAIN;
1925
1926skip:
1927 PUTBACK;
1928 SSL_TAIL;
1929 SPAGAIN;
1930 RETURN;
1931}
1932
1933static void
1934coro_ssl_patch (pTHX_ CV *cv, int ix, SV **args, int items)
1935{
1936 assert (("FATAL: ssl call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_set_stacklevel));
1937
1938 assert (("FATAL: ssl call with illegal CV value", CvGV (cv)));
1939 ssl_cv = cv;
1940
1941 /* we patch the op, and then re-run the whole call */
1942 /* we have to put some dummy argument on the stack for this to work */
1943 ssl_restore.op_next = (OP *)&ssl_restore;
1944 ssl_restore.op_type = OP_NULL;
1945 ssl_restore.op_ppaddr = pp_restore;
1946 ssl_restore.op_first = PL_op;
1947
1948 ssl_arg0 = items > 0 ? SvREFCNT_inc (args [0]) : 0;
1949 ssl_arg1 = items > 1 ? SvREFCNT_inc (args [1]) : 0;
1950
1951 PL_op->op_ppaddr = pp_set_stacklevel;
1952 PL_op->op_private = PL_op->op_private & ~OPpENTERSUB_SSL | ix; /* we potentially share our private flags with entersub */
1953
1954 PL_op = (OP *)&ssl_restore;
1955}
1748 1956
1749MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1957MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1750 1958
1751PROTOTYPES: DISABLE 1959PROTOTYPES: DISABLE
1752 1960
1753BOOT: 1961BOOT:
1754{ 1962{
1755#ifdef USE_ITHREADS 1963#ifdef USE_ITHREADS
1756 MUTEX_INIT (&coro_lock); 1964 MUTEX_INIT (&coro_lock);
1965# if CORO_PTHREAD
1966 coro_thx = PERL_GET_CONTEXT;
1967# endif
1757#endif 1968#endif
1758 BOOT_PAGESIZE; 1969 BOOT_PAGESIZE;
1759 1970
1760 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1971 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1761 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1972 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1824 av_push (coro->args, newSVsv (ST (i))); 2035 av_push (coro->args, newSVsv (ST (i)));
1825} 2036}
1826 OUTPUT: 2037 OUTPUT:
1827 RETVAL 2038 RETVAL
1828 2039
1829# these not obviously related functions are all rolled into the same xs
1830# function to increase chances that they all will call transfer with the same
1831# stack offset
1832void 2040void
1833_set_stacklevel (...) 2041_set_stacklevel (...)
1834 ALIAS: 2042 ALIAS:
1835 Coro::State::transfer = 1 2043 Coro::State::transfer = 1
1836 Coro::schedule = 2 2044 Coro::schedule = 2
1837 Coro::cede = 3 2045 Coro::cede = 3
1838 Coro::cede_notself = 4 2046 Coro::cede_notself = 4
1839 CODE: 2047 CODE:
1840{ 2048 coro_ssl_patch (aTHX_ cv, ix, &ST (0), items);
1841 struct transfer_args ta;
1842
1843 PUTBACK;
1844 switch (ix)
1845 {
1846 case 0:
1847 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1848 ta.next = 0;
1849 break;
1850
1851 case 1:
1852 if (items != 2)
1853 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items);
1854
1855 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1856 break;
1857
1858 case 2:
1859 prepare_schedule (aTHX_ &ta);
1860 break;
1861
1862 case 3:
1863 prepare_cede (aTHX_ &ta);
1864 break;
1865
1866 case 4:
1867 if (!prepare_cede_notself (aTHX_ &ta))
1868 XSRETURN_EMPTY;
1869
1870 break;
1871 }
1872 SPAGAIN;
1873
1874 BARRIER;
1875 PUTBACK;
1876 TRANSFER (ta, 0);
1877 SPAGAIN; /* might be the sp of a different coroutine now */
1878 /* be extra careful not to ever do anything after TRANSFER */
1879}
1880 2049
1881bool 2050bool
1882_destroy (SV *coro_sv) 2051_destroy (SV *coro_sv)
1883 CODE: 2052 CODE:
1884 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 2053 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1994 RETVAL = boolSV (coro->flags & ix); 2163 RETVAL = boolSV (coro->flags & ix);
1995 OUTPUT: 2164 OUTPUT:
1996 RETVAL 2165 RETVAL
1997 2166
1998void 2167void
2168throw (Coro::State self, SV *throw = &PL_sv_undef)
2169 PROTOTYPE: $;$
2170 CODE:
2171 SvREFCNT_dec (self->throw);
2172 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2173
2174void
1999api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 2175api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
2000 2176
2001SV * 2177SV *
2002has_cctx (Coro::State coro) 2178has_cctx (Coro::State coro)
2003 PROTOTYPE: $ 2179 PROTOTYPE: $
2012 CODE: 2188 CODE:
2013 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; 2189 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
2014 OUTPUT: 2190 OUTPUT:
2015 RETVAL 2191 RETVAL
2016 2192
2017IV 2193UV
2018rss (Coro::State coro) 2194rss (Coro::State coro)
2019 PROTOTYPE: $ 2195 PROTOTYPE: $
2020 ALIAS: 2196 ALIAS:
2021 usecount = 1 2197 usecount = 1
2022 CODE: 2198 CODE:
2074 2250
2075 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 2251 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
2076 coro_ready[i] = newAV (); 2252 coro_ready[i] = newAV ();
2077 2253
2078 { 2254 {
2079 SV *sv = perl_get_sv ("Coro::API", TRUE); 2255 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2080 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
2081 2256
2082 coroapi.schedule = api_schedule; 2257 coroapi.schedule = api_schedule;
2083 coroapi.cede = api_cede; 2258 coroapi.cede = api_cede;
2084 coroapi.cede_notself = api_cede_notself; 2259 coroapi.cede_notself = api_cede_notself;
2085 coroapi.ready = api_ready; 2260 coroapi.ready = api_ready;
2145 CODE: 2320 CODE:
2146 RETVAL = coro_nready; 2321 RETVAL = coro_nready;
2147 OUTPUT: 2322 OUTPUT:
2148 RETVAL 2323 RETVAL
2149 2324
2150void
2151throw (Coro::State self, SV *throw = &PL_sv_undef)
2152 PROTOTYPE: $;$
2153 CODE:
2154 SvREFCNT_dec (self->throw);
2155 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2156
2157# for async_pool speedup 2325# for async_pool speedup
2158void 2326void
2159_pool_1 (SV *cb) 2327_pool_1 (SV *cb)
2160 CODE: 2328 CODE:
2161{ 2329{
2204 sv_setsv (cb, &PL_sv_undef); 2372 sv_setsv (cb, &PL_sv_undef);
2205 2373
2206 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2374 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2207 coro->saved_deffh = 0; 2375 coro->saved_deffh = 0;
2208 2376
2209 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2377 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2210 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2378 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2211 { 2379 {
2212 SV *old = PL_diehook; 2380 SV *old = PL_diehook;
2213 PL_diehook = 0; 2381 PL_diehook = 0;
2214 SvREFCNT_dec (old); 2382 SvREFCNT_dec (old);
2360 2528
2361MODULE = Coro::State PACKAGE = PerlIO::cede 2529MODULE = Coro::State PACKAGE = PerlIO::cede
2362 2530
2363BOOT: 2531BOOT:
2364 PerlIO_define_layer (aTHX_ &PerlIO_cede); 2532 PerlIO_define_layer (aTHX_ &PerlIO_cede);
2533

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines