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.251 by root, Thu Oct 30 09:44:31 2008 UTC vs.
Revision 1.262 by root, Mon Nov 10 20:38:19 2008 UTC

52#else 52#else
53# define REGISTER_STACK(cctx,start,end) 53# define REGISTER_STACK(cctx,start,end)
54#endif 54#endif
55 55
56/* the maximum number of idle cctx that will be pooled */ 56/* the maximum number of idle cctx that will be pooled */
57#define MAX_IDLE_CCTX 8 57static int cctx_max_idle = 4;
58 58
59#define PERL_VERSION_ATLEAST(a,b,c) \ 59#define PERL_VERSION_ATLEAST(a,b,c) \
60 (PERL_REVISION > (a) \ 60 (PERL_REVISION > (a) \
61 || (PERL_REVISION == (a) \ 61 || (PERL_REVISION == (a) \
62 && (PERL_VERSION > (b) \ 62 && (PERL_VERSION > (b) \
142#define NOINLINE attribute ((noinline)) 142#define NOINLINE attribute ((noinline))
143 143
144#include "CoroAPI.h" 144#include "CoroAPI.h"
145 145
146#ifdef USE_ITHREADS 146#ifdef USE_ITHREADS
147
147static perl_mutex coro_mutex; 148static perl_mutex coro_lock;
148# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 149# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0)
149# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 150# define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0)
151# if CORO_PTHREAD
152static void *coro_thx;
153# endif
154
150#else 155#else
156
151# define LOCK (void)0 157# define LOCK (void)0
152# define UNLOCK (void)0 158# define UNLOCK (void)0
159
153#endif 160#endif
161
162# undef LOCK
163# define LOCK (void)0
164# undef UNLOCK
165# define UNLOCK (void)0
154 166
155/* helper storage struct for Coro::AIO */ 167/* helper storage struct for Coro::AIO */
156struct io_state 168struct io_state
157{ 169{
158 AV *res; 170 AV *res;
159 int errorno; 171 int errorno;
160 I32 laststype; 172 I32 laststype; /* U16 in 5.10.0 */
161 int laststatval; 173 int laststatval;
162 Stat_t statcache; 174 Stat_t statcache;
163}; 175};
164 176
165static double (*nvtime)(); /* so why doesn't it take void? */ 177static double (*nvtime)(); /* so why doesn't it take void? */
166 178
179static U32 cctx_gen;
167static size_t coro_stacksize = CORO_STACKSIZE; 180static size_t cctx_stacksize = CORO_STACKSIZE;
168static struct CoroAPI coroapi; 181static struct CoroAPI coroapi;
169static AV *main_mainstack; /* used to differentiate between $main and others */ 182static AV *main_mainstack; /* used to differentiate between $main and others */
170static JMPENV *main_top_env; 183static JMPENV *main_top_env;
171static HV *coro_state_stash, *coro_stash; 184static HV *coro_state_stash, *coro_stash;
172static volatile SV *coro_mortal; /* will be freed after next transfer */ 185static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
186static volatile struct coro *transfer_next;
187
188struct transfer_args
189{
190 struct coro *prev, *next;
191};
173 192
174static GV *irsgv; /* $/ */ 193static GV *irsgv; /* $/ */
175static GV *stdoutgv; /* *STDOUT */ 194static GV *stdoutgv; /* *STDOUT */
176static SV *rv_diehook; 195static SV *rv_diehook;
177static SV *rv_warnhook; 196static SV *rv_warnhook;
209 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 228 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
210 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 229 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
211 JMPENV *top_env; 230 JMPENV *top_env;
212 coro_context cctx; 231 coro_context cctx;
213 232
233 U32 gen;
214#if CORO_USE_VALGRIND 234#if CORO_USE_VALGRIND
215 int valgrind_id; 235 int valgrind_id;
216#endif 236#endif
217 unsigned char flags; 237 unsigned char flags;
218} coro_cctx; 238} coro_cctx;
278#define PRIO_MIN -4 298#define PRIO_MIN -4
279 299
280/* for Coro.pm */ 300/* for Coro.pm */
281static SV *coro_current; 301static SV *coro_current;
282static SV *coro_readyhook; 302static SV *coro_readyhook;
283static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 303static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
284static int coro_nready; 304static int coro_nready;
285static struct coro *coro_first; 305static struct coro *coro_first;
286 306
287/** lowlevel stuff **********************************************************/ 307/** lowlevel stuff **********************************************************/
288 308
383static MGVTBL coro_cv_vtbl = { 403static MGVTBL coro_cv_vtbl = {
384 0, 0, 0, 0, 404 0, 0, 0, 0,
385 coro_cv_free 405 coro_cv_free
386}; 406};
387 407
388#define CORO_MAGIC(sv,type) \ 408#define CORO_MAGIC(sv, type) \
389 SvMAGIC (sv) \ 409 SvMAGIC (sv) \
390 ? SvMAGIC (sv)->mg_type == type \ 410 ? SvMAGIC (sv)->mg_type == type \
391 ? SvMAGIC (sv) \ 411 ? SvMAGIC (sv) \
392 : mg_find (sv, type) \ 412 : mg_find (sv, type) \
393 : 0 413 : 0
394 414
395#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 415#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
396#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) 416#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
397 417
398static struct coro * 418static struct coro *
628 648
629/* 649/*
630 * destroy the stacks, the callchain etc... 650 * destroy the stacks, the callchain etc...
631 */ 651 */
632static void 652static void
633coro_destroy_stacks (pTHX) 653coro_destruct_stacks (pTHX)
634{ 654{
635 while (PL_curstackinfo->si_next) 655 while (PL_curstackinfo->si_next)
636 PL_curstackinfo = PL_curstackinfo->si_next; 656 PL_curstackinfo = PL_curstackinfo->si_next;
637 657
638 while (PL_curstackinfo) 658 while (PL_curstackinfo)
820 PL_rs = newSVsv (GvSV (irsgv)); 840 PL_rs = newSVsv (GvSV (irsgv));
821 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 841 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
822 842
823 { 843 {
824 dSP; 844 dSP;
825 LOGOP myop; 845 UNOP myop;
826 846
827 Zero (&myop, 1, LOGOP); 847 Zero (&myop, 1, UNOP);
828 myop.op_next = Nullop; 848 myop.op_next = Nullop;
829 myop.op_flags = OPf_WANT_VOID; 849 myop.op_flags = OPf_WANT_VOID;
830 850
831 PUSHMARK (SP); 851 PUSHMARK (SP);
832 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 852 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
836 SPAGAIN; 856 SPAGAIN;
837 } 857 }
838 858
839 /* this newly created coroutine might be run on an existing cctx which most 859 /* this newly created coroutine might be run on an existing cctx which most
840 * likely was suspended in set_stacklevel, called from entersub. 860 * likely was suspended in set_stacklevel, called from entersub.
841 * set_stacklevl doesn't do anything on return, but entersub does LEAVE, 861 * set_stacklevel doesn't do anything on return, but entersub does LEAVE,
842 * so we ENTER here for symmetry 862 * so we ENTER here for symmetry.
843 */ 863 */
844 ENTER; 864 ENTER;
845} 865}
846 866
847static void 867static void
848coro_destroy (pTHX_ struct coro *coro) 868coro_destruct (pTHX_ struct coro *coro)
849{ 869{
850 if (!IN_DESTRUCT) 870 if (!IN_DESTRUCT)
851 { 871 {
852 /* restore all saved variables and stuff */ 872 /* restore all saved variables and stuff */
853 LEAVE_SCOPE (0); 873 LEAVE_SCOPE (0);
875 SvREFCNT_dec (PL_warnhook); 895 SvREFCNT_dec (PL_warnhook);
876 896
877 SvREFCNT_dec (coro->saved_deffh); 897 SvREFCNT_dec (coro->saved_deffh);
878 SvREFCNT_dec (coro->throw); 898 SvREFCNT_dec (coro->throw);
879 899
880 coro_destroy_stacks (aTHX); 900 coro_destruct_stacks (aTHX);
881} 901}
882 902
883static void 903static void
884free_coro_mortal (pTHX) 904free_coro_mortal (pTHX)
885{ 905{
1012 1032
1013 TAINT_NOT; 1033 TAINT_NOT;
1014 return 0; 1034 return 0;
1015} 1035}
1016 1036
1037static void
1038prepare_set_stacklevel (struct transfer_args *ta, struct coro_cctx *cctx)
1039{
1040 ta->prev = (struct coro *)cctx;
1041 ta->next = 0;
1042}
1043
1017/* inject a fake call to Coro::State::_cctx_init into the execution */ 1044/* inject a fake call to Coro::State::_cctx_init into the execution */
1018/* _cctx_init should be careful, as it could be called at almost any time */ 1045/* _cctx_init should be careful, as it could be called at almost any time */
1019/* during execution of a perl program */ 1046/* during execution of a perl program */
1047/* also initialises PL_top_env */
1020static void NOINLINE 1048static void NOINLINE
1021cctx_prepare (pTHX_ coro_cctx *cctx) 1049cctx_prepare (pTHX_ coro_cctx *cctx)
1022{ 1050{
1023 dSP; 1051 dSP;
1024 LOGOP myop; 1052 UNOP myop;
1025 1053
1026 PL_top_env = &PL_start_env; 1054 PL_top_env = &PL_start_env;
1027 1055
1028 if (cctx->flags & CC_TRACE) 1056 if (cctx->flags & CC_TRACE)
1029 PL_runops = runops_trace; 1057 PL_runops = runops_trace;
1030 1058
1031 Zero (&myop, 1, LOGOP); 1059 Zero (&myop, 1, UNOP);
1032 myop.op_next = PL_op; 1060 myop.op_next = PL_op;
1033 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 1061 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
1034 1062
1035 PUSHMARK (SP); 1063 PUSHMARK (SP);
1036 EXTEND (SP, 2); 1064 EXTEND (SP, 2);
1037 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); 1065 PUSHs (sv_2mortal (newSViv ((IV)cctx)));
1038 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 1066 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
1039 PUTBACK; 1067 PUTBACK;
1040 PL_op = (OP *)&myop; 1068 PL_op = (OP *)&myop;
1041 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 1069 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1042 SPAGAIN; 1070 SPAGAIN;
1043} 1071}
1044 1072
1073/* the tail of transfer: execute stuff we can only do after a transfer */
1074static void
1075transfer_tail (pTHX)
1076{
1077 struct coro *next = (struct coro *)transfer_next;
1078 transfer_next = 0; //D for temporary assertion in transfer
1079 assert (("FATAL ERROR: internal error 1067 in Coro module, please report", next));//D
1080
1081 free_coro_mortal (aTHX);
1082 UNLOCK;
1083
1084 if (expect_false (next->throw))
1085 {
1086 SV *exception = sv_2mortal (next->throw);
1087
1088 next->throw = 0;
1089 sv_setsv (ERRSV, exception);
1090 croak (0);
1091 }
1092}
1093
1045/* 1094/*
1046 * this is a _very_ stripped down perl interpreter ;) 1095 * this is a _very_ stripped down perl interpreter ;)
1047 */ 1096 */
1048static void 1097static void
1049cctx_run (void *arg) 1098cctx_run (void *arg)
1050{ 1099{
1100#ifdef USE_ITHREADS
1101# if CORO_PTHREAD
1102 PERL_SET_CONTEXT (coro_thx);
1103# endif
1104#endif
1105 {
1051 dTHX; 1106 dTHX;
1052 1107
1053 /* cctx_run is the alternative tail of transfer(), so unlock here. */ 1108 /* entersub called ENTER, but we never 'returned', undo that here */
1054 UNLOCK; 1109 LEAVE;
1055 1110
1056 /* we now skip the entersub that lead to transfer() */ 1111 /* we now skip the entersub that did lead to transfer() */
1057 PL_op = PL_op->op_next; 1112 PL_op = PL_op->op_next;
1058 1113
1059 /* inject a fake subroutine call to cctx_init */ 1114 /* inject a fake subroutine call to cctx_init */
1060 cctx_prepare (aTHX_ (coro_cctx *)arg); 1115 cctx_prepare (aTHX_ (coro_cctx *)arg);
1061 1116
1117 /* cctx_run is the alternative tail of transfer() */
1118 transfer_tail (aTHX);
1119
1062 /* somebody or something will hit me for both perl_run and PL_restartop */ 1120 /* somebody or something will hit me for both perl_run and PL_restartop */
1063 PL_restartop = PL_op; 1121 PL_restartop = PL_op;
1064 perl_run (PL_curinterp); 1122 perl_run (PL_curinterp);
1065 1123
1066 /* 1124 /*
1067 * If perl-run returns we assume exit() was being called or the coro 1125 * If perl-run returns we assume exit() was being called or the coro
1068 * fell off the end, which seems to be the only valid (non-bug) 1126 * fell off the end, which seems to be the only valid (non-bug)
1069 * reason for perl_run to return. We try to exit by jumping to the 1127 * reason for perl_run to return. We try to exit by jumping to the
1070 * bootstrap-time "top" top_env, as we cannot restore the "main" 1128 * bootstrap-time "top" top_env, as we cannot restore the "main"
1071 * coroutine as Coro has no such concept 1129 * coroutine as Coro has no such concept
1072 */ 1130 */
1073 PL_top_env = main_top_env; 1131 PL_top_env = main_top_env;
1074 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1132 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1133 }
1075} 1134}
1076 1135
1077static coro_cctx * 1136static coro_cctx *
1078cctx_new () 1137cctx_new ()
1079{ 1138{
1080 coro_cctx *cctx; 1139 coro_cctx *cctx;
1140
1141 ++cctx_count;
1142 New (0, cctx, 1, coro_cctx);
1143
1144 cctx->gen = cctx_gen;
1145 cctx->flags = 0;
1146 cctx->idle_sp = 0; /* can be accessed by transfer between cctx_run and set_stacklevel, on throw */
1147
1148 return cctx;
1149}
1150
1151/* create a new cctx only suitable as source */
1152static coro_cctx *
1153cctx_new_empty ()
1154{
1155 coro_cctx *cctx = cctx_new ();
1156
1157 cctx->sptr = 0;
1158 coro_create (&cctx->cctx, 0, 0, 0, 0);
1159
1160 return cctx;
1161}
1162
1163/* create a new cctx suitable as destination/running a perl interpreter */
1164static coro_cctx *
1165cctx_new_run ()
1166{
1167 coro_cctx *cctx = cctx_new ();
1081 void *stack_start; 1168 void *stack_start;
1082 size_t stack_size; 1169 size_t stack_size;
1083 1170
1084 ++cctx_count;
1085
1086 Newz (0, cctx, 1, coro_cctx);
1087
1088#if HAVE_MMAP 1171#if HAVE_MMAP
1089 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1172 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1090 /* mmap supposedly does allocate-on-write for us */ 1173 /* mmap supposedly does allocate-on-write for us */
1091 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1174 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
1092 1175
1093 if (cctx->sptr != (void *)-1) 1176 if (cctx->sptr != (void *)-1)
1094 { 1177 {
1100 cctx->flags |= CC_MAPPED; 1183 cctx->flags |= CC_MAPPED;
1101 } 1184 }
1102 else 1185 else
1103#endif 1186#endif
1104 { 1187 {
1105 cctx->ssize = coro_stacksize * (long)sizeof (long); 1188 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1106 New (0, cctx->sptr, coro_stacksize, long); 1189 New (0, cctx->sptr, cctx_stacksize, long);
1107 1190
1108 if (!cctx->sptr) 1191 if (!cctx->sptr)
1109 { 1192 {
1110 perror ("FATAL: unable to allocate stack for coroutine"); 1193 perror ("FATAL: unable to allocate stack for coroutine");
1111 _exit (EXIT_FAILURE); 1194 _exit (EXIT_FAILURE);
1126{ 1209{
1127 if (!cctx) 1210 if (!cctx)
1128 return; 1211 return;
1129 1212
1130 --cctx_count; 1213 --cctx_count;
1214 coro_destroy (&cctx->cctx);
1131 1215
1216 /* coro_transfer creates new, empty cctx's */
1217 if (cctx->sptr)
1218 {
1132#if CORO_USE_VALGRIND 1219#if CORO_USE_VALGRIND
1133 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1220 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1134#endif 1221#endif
1135 1222
1136#if HAVE_MMAP 1223#if HAVE_MMAP
1137 if (cctx->flags & CC_MAPPED) 1224 if (cctx->flags & CC_MAPPED)
1138 munmap (cctx->sptr, cctx->ssize); 1225 munmap (cctx->sptr, cctx->ssize);
1139 else 1226 else
1140#endif 1227#endif
1141 Safefree (cctx->sptr); 1228 Safefree (cctx->sptr);
1229 }
1142 1230
1143 Safefree (cctx); 1231 Safefree (cctx);
1144} 1232}
1145 1233
1146/* wether this cctx should be destructed */ 1234/* wether this cctx should be destructed */
1147#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) 1235#define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE))
1148 1236
1149static coro_cctx * 1237static coro_cctx *
1150cctx_get (pTHX) 1238cctx_get (pTHX)
1151{ 1239{
1152 while (expect_true (cctx_first)) 1240 while (expect_true (cctx_first))
1159 return cctx; 1247 return cctx;
1160 1248
1161 cctx_destroy (cctx); 1249 cctx_destroy (cctx);
1162 } 1250 }
1163 1251
1164 return cctx_new (); 1252 return cctx_new_run ();
1165} 1253}
1166 1254
1167static void 1255static void
1168cctx_put (coro_cctx *cctx) 1256cctx_put (coro_cctx *cctx)
1169{ 1257{
1258 assert (("cctx_put called on non-initialised cctx", cctx->sptr));
1259
1170 /* free another cctx if overlimit */ 1260 /* free another cctx if overlimit */
1171 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1261 if (expect_false (cctx_idle >= cctx_max_idle))
1172 { 1262 {
1173 coro_cctx *first = cctx_first; 1263 coro_cctx *first = cctx_first;
1174 cctx_first = first->next; 1264 cctx_first = first->next;
1175 --cctx_idle; 1265 --cctx_idle;
1176 1266
1208/* always use the TRANSFER macro */ 1298/* always use the TRANSFER macro */
1209static void NOINLINE 1299static void NOINLINE
1210transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1300transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1211{ 1301{
1212 dSTACKLEVEL; 1302 dSTACKLEVEL;
1213 static volatile int has_throw;
1214 1303
1215 /* sometimes transfer is only called to set idle_sp */ 1304 /* sometimes transfer is only called to set idle_sp */
1216 if (expect_false (!next)) 1305 if (expect_false (!next))
1217 { 1306 {
1218 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1307 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1222 { 1311 {
1223 coro_cctx *prev__cctx; 1312 coro_cctx *prev__cctx;
1224 1313
1225 if (expect_false (prev->flags & CF_NEW)) 1314 if (expect_false (prev->flags & CF_NEW))
1226 { 1315 {
1227 /* create a new empty context */ 1316 /* create a new empty/source context */
1228 Newz (0, prev->cctx, 1, coro_cctx); 1317 prev->cctx = cctx_new_empty ();
1229 prev->flags &= ~CF_NEW; 1318 prev->flags &= ~CF_NEW;
1230 prev->flags |= CF_RUNNING; 1319 prev->flags |= CF_RUNNING;
1231 } 1320 }
1232 1321
1233 prev->flags &= ~CF_RUNNING; 1322 prev->flags &= ~CF_RUNNING;
1248 else 1337 else
1249 load_perl (aTHX_ next); 1338 load_perl (aTHX_ next);
1250 1339
1251 prev__cctx = prev->cctx; 1340 prev__cctx = prev->cctx;
1252 1341
1253 /* possibly "free" the cctx */ 1342 /* possibly untie and reuse the cctx */
1254 if (expect_true ( 1343 if (expect_true (
1255 prev__cctx->idle_sp == STACKLEVEL 1344 prev__cctx->idle_sp == STACKLEVEL
1256 && !(prev__cctx->flags & CC_TRACE) 1345 && !(prev__cctx->flags & CC_TRACE)
1257 && !force_cctx 1346 && !force_cctx
1258 )) 1347 ))
1274 ++next->usecount; 1363 ++next->usecount;
1275 1364
1276 if (expect_true (!next->cctx)) 1365 if (expect_true (!next->cctx))
1277 next->cctx = cctx_get (aTHX); 1366 next->cctx = cctx_get (aTHX);
1278 1367
1279 has_throw = !!next->throw; 1368 assert (("FATAL ERROR: internal error 1352 in Coro, please report", !transfer_next));//D
1369 transfer_next = next;
1280 1370
1281 if (expect_false (prev__cctx != next->cctx)) 1371 if (expect_false (prev__cctx != next->cctx))
1282 { 1372 {
1283 prev__cctx->top_env = PL_top_env; 1373 prev__cctx->top_env = PL_top_env;
1284 PL_top_env = next->cctx->top_env; 1374 PL_top_env = next->cctx->top_env;
1285 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1375 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
1286 } 1376 }
1287 1377
1288 free_coro_mortal (aTHX); 1378 transfer_tail (aTHX);
1289 UNLOCK;
1290
1291 if (expect_false (has_throw))
1292 {
1293 struct coro *coro = SvSTATE (coro_current);
1294
1295 if (coro->throw)
1296 {
1297 SV *exception = coro->throw;
1298 coro->throw = 0;
1299 sv_setsv (ERRSV, exception);
1300 croak (0);
1301 }
1302 }
1303 } 1379 }
1304} 1380}
1305
1306struct transfer_args
1307{
1308 struct coro *prev, *next;
1309};
1310 1381
1311#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) 1382#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx))
1312#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) 1383#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1313 1384
1314/** high level stuff ********************************************************/ 1385/** high level stuff ********************************************************/
1340 croak ("FATAL: tried to destroy currently running coroutine"); 1411 croak ("FATAL: tried to destroy currently running coroutine");
1341 1412
1342 save_perl (aTHX_ &temp); 1413 save_perl (aTHX_ &temp);
1343 load_perl (aTHX_ coro); 1414 load_perl (aTHX_ coro);
1344 1415
1345 coro_destroy (aTHX_ coro); 1416 coro_destruct (aTHX_ coro);
1346 1417
1347 load_perl (aTHX_ &temp); 1418 load_perl (aTHX_ &temp);
1348 1419
1349 coro->slot = 0; 1420 coro->slot = 0;
1350 } 1421 }
1526 /* cannot transfer to destroyed coros, skip and look for next */ 1597 /* cannot transfer to destroyed coros, skip and look for next */
1527 if (expect_false (ta->next->flags & CF_DESTROYED)) 1598 if (expect_false (ta->next->flags & CF_DESTROYED))
1528 { 1599 {
1529 UNLOCK; 1600 UNLOCK;
1530 SvREFCNT_dec (next_sv); 1601 SvREFCNT_dec (next_sv);
1531 /* coro_nready is already taken care of by destroy */ 1602 /* coro_nready has already been taken care of by destroy */
1532 continue; 1603 continue;
1533 } 1604 }
1534 1605
1535 --coro_nready; 1606 --coro_nready;
1536 UNLOCK; 1607 UNLOCK;
1621 struct coro *coro = SvSTATE (coro_sv); 1692 struct coro *coro = SvSTATE (coro_sv);
1622 1693
1623 if (flags & CC_TRACE) 1694 if (flags & CC_TRACE)
1624 { 1695 {
1625 if (!coro->cctx) 1696 if (!coro->cctx)
1626 coro->cctx = cctx_new (); 1697 coro->cctx = cctx_new_run ();
1627 else if (!(coro->cctx->flags & CC_TRACE)) 1698 else if (!(coro->cctx->flags & CC_TRACE))
1628 croak ("cannot enable tracing on coroutine with custom stack"); 1699 croak ("cannot enable tracing on coroutine with custom stack");
1629 1700
1630 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1701 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1631 } 1702 }
1638 else 1709 else
1639 coro->slot->runops = RUNOPS_DEFAULT; 1710 coro->slot->runops = RUNOPS_DEFAULT;
1640 } 1711 }
1641} 1712}
1642 1713
1714#if 0
1643static int 1715static int
1644coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) 1716coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1645{ 1717{
1646 AV *padlist; 1718 AV *padlist;
1647 AV *av = (AV *)mg->mg_obj; 1719 AV *av = (AV *)mg->mg_obj;
1653 1725
1654static MGVTBL coro_gensub_vtbl = { 1726static MGVTBL coro_gensub_vtbl = {
1655 0, 0, 0, 0, 1727 0, 0, 0, 0,
1656 coro_gensub_free 1728 coro_gensub_free
1657}; 1729};
1730#endif
1658 1731
1659/*****************************************************************************/ 1732/*****************************************************************************/
1660/* PerlIO::cede */ 1733/* PerlIO::cede */
1661 1734
1662typedef struct 1735typedef struct
1737PROTOTYPES: DISABLE 1810PROTOTYPES: DISABLE
1738 1811
1739BOOT: 1812BOOT:
1740{ 1813{
1741#ifdef USE_ITHREADS 1814#ifdef USE_ITHREADS
1742 MUTEX_INIT (&coro_mutex); 1815 MUTEX_INIT (&coro_lock);
1816# if CORO_PTHREAD
1817 coro_thx = PERL_GET_CONTEXT;
1818# endif
1743#endif 1819#endif
1744 BOOT_PAGESIZE; 1820 BOOT_PAGESIZE;
1745 1821
1746 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1822 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1747 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1823 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1828 1904
1829 PUTBACK; 1905 PUTBACK;
1830 switch (ix) 1906 switch (ix)
1831 { 1907 {
1832 case 0: 1908 case 0:
1833 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); 1909 prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (ST (0)));
1834 ta.next = 0;
1835 break; 1910 break;
1836 1911
1837 case 1: 1912 case 1:
1838 if (items != 2) 1913 if (items != 2)
1839 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); 1914 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items);
1840 1915
1841 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 1916 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1842 break; 1917 break;
1843 1918
1844 case 2: 1919 case 2:
1878 _exit (code); 1953 _exit (code);
1879 1954
1880int 1955int
1881cctx_stacksize (int new_stacksize = 0) 1956cctx_stacksize (int new_stacksize = 0)
1882 CODE: 1957 CODE:
1883 RETVAL = coro_stacksize; 1958 RETVAL = cctx_stacksize;
1884 if (new_stacksize) 1959 if (new_stacksize)
1960 {
1885 coro_stacksize = new_stacksize; 1961 cctx_stacksize = new_stacksize;
1962 ++cctx_gen;
1963 }
1964 OUTPUT:
1965 RETVAL
1966
1967int
1968cctx_max_idle (int max_idle = 0)
1969 CODE:
1970 RETVAL = cctx_max_idle;
1971 if (max_idle > 1)
1972 cctx_max_idle = max_idle;
1886 OUTPUT: 1973 OUTPUT:
1887 RETVAL 1974 RETVAL
1888 1975
1889int 1976int
1890cctx_count () 1977cctx_count ()
1968 RETVAL = boolSV (coro->flags & ix); 2055 RETVAL = boolSV (coro->flags & ix);
1969 OUTPUT: 2056 OUTPUT:
1970 RETVAL 2057 RETVAL
1971 2058
1972void 2059void
2060throw (Coro::State self, SV *throw = &PL_sv_undef)
2061 PROTOTYPE: $;$
2062 CODE:
2063 SvREFCNT_dec (self->throw);
2064 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2065
2066void
1973api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 2067api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1974 2068
1975SV * 2069SV *
1976has_cctx (Coro::State coro) 2070has_cctx (Coro::State coro)
1977 PROTOTYPE: $ 2071 PROTOTYPE: $
1986 CODE: 2080 CODE:
1987 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; 2081 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1988 OUTPUT: 2082 OUTPUT:
1989 RETVAL 2083 RETVAL
1990 2084
1991IV 2085UV
1992rss (Coro::State coro) 2086rss (Coro::State coro)
1993 PROTOTYPE: $ 2087 PROTOTYPE: $
1994 ALIAS: 2088 ALIAS:
1995 usecount = 1 2089 usecount = 1
1996 CODE: 2090 CODE:
2007 CODE: 2101 CODE:
2008 struct coro *coro = SvSTATE (coro_current); 2102 struct coro *coro = SvSTATE (coro_current);
2009 coro->cctx->idle_sp = 0; 2103 coro->cctx->idle_sp = 0;
2010 2104
2011void 2105void
2012throw (Coro::State self, SV *throw = &PL_sv_undef)
2013 PROTOTYPE: $;$
2014 CODE:
2015 SvREFCNT_dec (self->throw);
2016 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2017
2018void
2019swap_defsv (Coro::State self) 2106swap_defsv (Coro::State self)
2020 PROTOTYPE: $ 2107 PROTOTYPE: $
2021 ALIAS: 2108 ALIAS:
2022 swap_defav = 1 2109 swap_defav = 1
2023 CODE: 2110 CODE:
2178 sv_setsv (cb, &PL_sv_undef); 2265 sv_setsv (cb, &PL_sv_undef);
2179 2266
2180 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2267 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2181 coro->saved_deffh = 0; 2268 coro->saved_deffh = 0;
2182 2269
2183 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2270 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2184 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2271 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2185 { 2272 {
2186 SV *old = PL_diehook; 2273 SV *old = PL_diehook;
2187 PL_diehook = 0; 2274 PL_diehook = 0;
2188 SvREFCNT_dec (old); 2275 SvREFCNT_dec (old);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines