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.249 by root, Tue Sep 30 17:12:35 2008 UTC vs.
Revision 1.260 by root, Mon Nov 10 04:37:23 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) \
81# ifndef IS_PADCONST 81# ifndef IS_PADCONST
82# define IS_PADCONST(v) 0 82# define IS_PADCONST(v) 0
83# endif 83# endif
84#endif 84#endif
85 85
86/* 5.11 */
87#ifndef CxHASARGS
88# define CxHASARGS(cx) (cx)->blk_sub.hasargs
89#endif
90
91/* 5.10.0 */
92#ifndef SvREFCNT_inc_NN
93# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv)
94#endif
95
86/* 5.8.8 */ 96/* 5.8.8 */
87#ifndef GV_NOTQUAL 97#ifndef GV_NOTQUAL
88# define GV_NOTQUAL 0 98# define GV_NOTQUAL 0
89#endif 99#endif
90#ifndef newSV 100#ifndef newSV
91# define newSV(l) NEWSV(0,l) 101# define newSV(l) NEWSV(0,l)
92#endif
93
94/* 5.11 */
95#ifndef CxHASARGS
96# define CxHASARGS(cx) (cx)->blk_sub.hasargs
97#endif 102#endif
98 103
99/* 5.8.7 */ 104/* 5.8.7 */
100#ifndef SvRV_set 105#ifndef SvRV_set
101# define SvRV_set(s,v) SvRV(s) = (v) 106# define SvRV_set(s,v) SvRV(s) = (v)
137#define NOINLINE attribute ((noinline)) 142#define NOINLINE attribute ((noinline))
138 143
139#include "CoroAPI.h" 144#include "CoroAPI.h"
140 145
141#ifdef USE_ITHREADS 146#ifdef USE_ITHREADS
147
142static perl_mutex coro_mutex; 148static perl_mutex coro_lock;
143# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 149# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0)
144# 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
145#else 155#else
156
146# define LOCK (void)0 157# define LOCK (void)0
147# define UNLOCK (void)0 158# define UNLOCK (void)0
159
148#endif 160#endif
161
162# undef LOCK
163# define LOCK (void)0
164# undef UNLOCK
165# define UNLOCK (void)0
149 166
150/* helper storage struct for Coro::AIO */ 167/* helper storage struct for Coro::AIO */
151struct io_state 168struct io_state
152{ 169{
153 AV *res; 170 AV *res;
154 int errorno; 171 int errorno;
155 I32 laststype; 172 I32 laststype; /* U16 in 5.10.0 */
156 int laststatval; 173 int laststatval;
157 Stat_t statcache; 174 Stat_t statcache;
158}; 175};
159 176
160static double (*nvtime)(); /* so why doesn't it take void? */ 177static double (*nvtime)(); /* so why doesn't it take void? */
161 178
179static U32 cctx_gen;
162static size_t coro_stacksize = CORO_STACKSIZE; 180static size_t cctx_stacksize = CORO_STACKSIZE;
163static struct CoroAPI coroapi; 181static struct CoroAPI coroapi;
164static AV *main_mainstack; /* used to differentiate between $main and others */ 182static AV *main_mainstack; /* used to differentiate between $main and others */
165static JMPENV *main_top_env; 183static JMPENV *main_top_env;
166static HV *coro_state_stash, *coro_stash; 184static HV *coro_state_stash, *coro_stash;
167static 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};
168 192
169static GV *irsgv; /* $/ */ 193static GV *irsgv; /* $/ */
170static GV *stdoutgv; /* *STDOUT */ 194static GV *stdoutgv; /* *STDOUT */
171static SV *rv_diehook; 195static SV *rv_diehook;
172static SV *rv_warnhook; 196static SV *rv_warnhook;
204 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 228 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
205 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 */
206 JMPENV *top_env; 230 JMPENV *top_env;
207 coro_context cctx; 231 coro_context cctx;
208 232
233 U32 gen;
209#if CORO_USE_VALGRIND 234#if CORO_USE_VALGRIND
210 int valgrind_id; 235 int valgrind_id;
211#endif 236#endif
212 unsigned char flags; 237 unsigned char flags;
213} coro_cctx; 238} coro_cctx;
273#define PRIO_MIN -4 298#define PRIO_MIN -4
274 299
275/* for Coro.pm */ 300/* for Coro.pm */
276static SV *coro_current; 301static SV *coro_current;
277static SV *coro_readyhook; 302static SV *coro_readyhook;
278static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 303static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
279static int coro_nready; 304static int coro_nready;
280static struct coro *coro_first; 305static struct coro *coro_first;
281 306
282/** lowlevel stuff **********************************************************/ 307/** lowlevel stuff **********************************************************/
283 308
378static MGVTBL coro_cv_vtbl = { 403static MGVTBL coro_cv_vtbl = {
379 0, 0, 0, 0, 404 0, 0, 0, 0,
380 coro_cv_free 405 coro_cv_free
381}; 406};
382 407
383#define CORO_MAGIC(sv,type) \ 408#define CORO_MAGIC(sv, type) \
384 SvMAGIC (sv) \ 409 SvMAGIC (sv) \
385 ? SvMAGIC (sv)->mg_type == type \ 410 ? SvMAGIC (sv)->mg_type == type \
386 ? SvMAGIC (sv) \ 411 ? SvMAGIC (sv) \
387 : mg_find (sv, type) \ 412 : mg_find (sv, type) \
388 : 0 413 : 0
389 414
390#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)
391#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)
392 417
393static struct coro * 418static struct coro *
623 648
624/* 649/*
625 * destroy the stacks, the callchain etc... 650 * destroy the stacks, the callchain etc...
626 */ 651 */
627static void 652static void
628coro_destroy_stacks (pTHX) 653coro_destruct_stacks (pTHX)
629{ 654{
630 while (PL_curstackinfo->si_next) 655 while (PL_curstackinfo->si_next)
631 PL_curstackinfo = PL_curstackinfo->si_next; 656 PL_curstackinfo = PL_curstackinfo->si_next;
632 657
633 while (PL_curstackinfo) 658 while (PL_curstackinfo)
815 PL_rs = newSVsv (GvSV (irsgv)); 840 PL_rs = newSVsv (GvSV (irsgv));
816 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 841 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
817 842
818 { 843 {
819 dSP; 844 dSP;
820 LOGOP myop; 845 UNOP myop;
821 846
822 Zero (&myop, 1, LOGOP); 847 Zero (&myop, 1, UNOP);
823 myop.op_next = Nullop; 848 myop.op_next = Nullop;
824 myop.op_flags = OPf_WANT_VOID; 849 myop.op_flags = OPf_WANT_VOID;
825 850
826 PUSHMARK (SP); 851 PUSHMARK (SP);
827 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 852 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
832 } 857 }
833 858
834 /* 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
835 * likely was suspended in set_stacklevel, called from entersub. 860 * likely was suspended in set_stacklevel, called from entersub.
836 * set_stacklevl doesn't do anything on return, but entersub does LEAVE, 861 * set_stacklevl doesn't do anything on return, but entersub does LEAVE,
837 * so we ENTER here for symmetry 862 * so we ENTER here for symmetry.
838 */ 863 */
839 ENTER; 864 ENTER;
840} 865}
841 866
842static void 867static void
843coro_destroy (pTHX_ struct coro *coro) 868coro_destruct (pTHX_ struct coro *coro)
844{ 869{
845 if (!IN_DESTRUCT) 870 if (!IN_DESTRUCT)
846 { 871 {
847 /* restore all saved variables and stuff */ 872 /* restore all saved variables and stuff */
848 LEAVE_SCOPE (0); 873 LEAVE_SCOPE (0);
870 SvREFCNT_dec (PL_warnhook); 895 SvREFCNT_dec (PL_warnhook);
871 896
872 SvREFCNT_dec (coro->saved_deffh); 897 SvREFCNT_dec (coro->saved_deffh);
873 SvREFCNT_dec (coro->throw); 898 SvREFCNT_dec (coro->throw);
874 899
875 coro_destroy_stacks (aTHX); 900 coro_destruct_stacks (aTHX);
876} 901}
877 902
878static void 903static void
879free_coro_mortal (pTHX) 904free_coro_mortal (pTHX)
880{ 905{
1007 1032
1008 TAINT_NOT; 1033 TAINT_NOT;
1009 return 0; 1034 return 0;
1010} 1035}
1011 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
1012/* 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 */
1013/* _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 */
1014/* during execution of a perl program */ 1046/* during execution of a perl program */
1047/* also initialises PL_top_env */
1015static void NOINLINE 1048static void NOINLINE
1016cctx_prepare (pTHX_ coro_cctx *cctx) 1049cctx_prepare (pTHX_ coro_cctx *cctx)
1017{ 1050{
1018 dSP; 1051 dSP;
1019 LOGOP myop; 1052 UNOP myop;
1020 1053
1021 PL_top_env = &PL_start_env; 1054 PL_top_env = &PL_start_env;
1022 1055
1023 if (cctx->flags & CC_TRACE) 1056 if (cctx->flags & CC_TRACE)
1024 PL_runops = runops_trace; 1057 PL_runops = runops_trace;
1025 1058
1026 Zero (&myop, 1, LOGOP); 1059 Zero (&myop, 1, UNOP);
1027 myop.op_next = PL_op; 1060 myop.op_next = PL_op;
1028 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 1061 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
1029 1062
1030 PUSHMARK (SP); 1063 PUSHMARK (SP);
1031 EXTEND (SP, 2); 1064 EXTEND (SP, 2);
1032 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); 1065 PUSHs (sv_2mortal (newSViv ((IV)cctx)));
1033 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 1066 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
1034 PUTBACK; 1067 PUTBACK;
1035 PL_op = (OP *)&myop; 1068 PL_op = (OP *)&myop;
1036 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 1069 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1037 SPAGAIN; 1070 SPAGAIN;
1038} 1071}
1039 1072
1073/* the tail of transfer: execute stuff we can onyl do afetr a transfer */
1074static void
1075transfer_tail (void)
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
1040/* 1094/*
1041 * this is a _very_ stripped down perl interpreter ;) 1095 * this is a _very_ stripped down perl interpreter ;)
1042 */ 1096 */
1043static void 1097static void
1044cctx_run (void *arg) 1098cctx_run (void *arg)
1045{ 1099{
1100#ifdef USE_ITHREADS
1101# if CORO_PTHREAD
1102 PERL_SET_CONTEXT (coro_thx);
1103# endif
1104#endif
1105 {
1046 dTHX; 1106 dTHX;
1047 1107
1048 /* cctx_run is the alternative tail of transfer(), so unlock here. */ 1108 /* entersub called ENTER, but we never 'returned', undo that here */
1049 UNLOCK; 1109 LEAVE;
1050 1110
1051 /* we now skip the entersub that lead to transfer() */ 1111 /* we now skip the entersub that did lead to transfer() */
1052 PL_op = PL_op->op_next; 1112 PL_op = PL_op->op_next;
1053 1113
1054 /* inject a fake subroutine call to cctx_init */ 1114 /* inject a fake subroutine call to cctx_init */
1055 cctx_prepare (aTHX_ (coro_cctx *)arg); 1115 cctx_prepare (aTHX_ (coro_cctx *)arg);
1056 1116
1117 /* cctx_run is the alternative tail of transfer() */
1118 transfer_tail ();
1119
1057 /* 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 */
1058 PL_restartop = PL_op; 1121 PL_restartop = PL_op;
1059 perl_run (PL_curinterp); 1122 perl_run (PL_curinterp);
1060 1123
1061 /* 1124 /*
1062 * 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
1063 * 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)
1064 * 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
1065 * bootstrap-time "top" top_env, as we cannot restore the "main" 1128 * bootstrap-time "top" top_env, as we cannot restore the "main"
1066 * coroutine as Coro has no such concept 1129 * coroutine as Coro has no such concept
1067 */ 1130 */
1068 PL_top_env = main_top_env; 1131 PL_top_env = main_top_env;
1069 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 }
1070} 1134}
1071 1135
1072static coro_cctx * 1136static coro_cctx *
1073cctx_new () 1137cctx_new ()
1074{ 1138{
1075 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 ();
1076 void *stack_start; 1168 void *stack_start;
1077 size_t stack_size; 1169 size_t stack_size;
1078 1170
1079 ++cctx_count;
1080
1081 Newz (0, cctx, 1, coro_cctx);
1082
1083#if HAVE_MMAP 1171#if HAVE_MMAP
1084 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;
1085 /* mmap supposedly does allocate-on-write for us */ 1173 /* mmap supposedly does allocate-on-write for us */
1086 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);
1087 1175
1088 if (cctx->sptr != (void *)-1) 1176 if (cctx->sptr != (void *)-1)
1089 { 1177 {
1095 cctx->flags |= CC_MAPPED; 1183 cctx->flags |= CC_MAPPED;
1096 } 1184 }
1097 else 1185 else
1098#endif 1186#endif
1099 { 1187 {
1100 cctx->ssize = coro_stacksize * (long)sizeof (long); 1188 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1101 New (0, cctx->sptr, coro_stacksize, long); 1189 New (0, cctx->sptr, cctx_stacksize, long);
1102 1190
1103 if (!cctx->sptr) 1191 if (!cctx->sptr)
1104 { 1192 {
1105 perror ("FATAL: unable to allocate stack for coroutine"); 1193 perror ("FATAL: unable to allocate stack for coroutine");
1106 _exit (EXIT_FAILURE); 1194 _exit (EXIT_FAILURE);
1121{ 1209{
1122 if (!cctx) 1210 if (!cctx)
1123 return; 1211 return;
1124 1212
1125 --cctx_count; 1213 --cctx_count;
1214 coro_destroy (&cctx->cctx);
1126 1215
1216 /* coro_transfer creates new, empty cctx's */
1217 if (cctx->sptr)
1218 {
1127#if CORO_USE_VALGRIND 1219#if CORO_USE_VALGRIND
1128 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1220 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1129#endif 1221#endif
1130 1222
1131#if HAVE_MMAP 1223#if HAVE_MMAP
1132 if (cctx->flags & CC_MAPPED) 1224 if (cctx->flags & CC_MAPPED)
1133 munmap (cctx->sptr, cctx->ssize); 1225 munmap (cctx->sptr, cctx->ssize);
1134 else 1226 else
1135#endif 1227#endif
1136 Safefree (cctx->sptr); 1228 Safefree (cctx->sptr);
1229 }
1137 1230
1138 Safefree (cctx); 1231 Safefree (cctx);
1139} 1232}
1140 1233
1141/* wether this cctx should be destructed */ 1234/* wether this cctx should be destructed */
1142#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))
1143 1236
1144static coro_cctx * 1237static coro_cctx *
1145cctx_get (pTHX) 1238cctx_get (pTHX)
1146{ 1239{
1147 while (expect_true (cctx_first)) 1240 while (expect_true (cctx_first))
1154 return cctx; 1247 return cctx;
1155 1248
1156 cctx_destroy (cctx); 1249 cctx_destroy (cctx);
1157 } 1250 }
1158 1251
1159 return cctx_new (); 1252 return cctx_new_run ();
1160} 1253}
1161 1254
1162static void 1255static void
1163cctx_put (coro_cctx *cctx) 1256cctx_put (coro_cctx *cctx)
1164{ 1257{
1258 assert (("cctx_put called on non-initialised cctx", cctx->sptr));
1259
1165 /* free another cctx if overlimit */ 1260 /* free another cctx if overlimit */
1166 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1261 if (expect_false (cctx_idle >= cctx_max_idle))
1167 { 1262 {
1168 coro_cctx *first = cctx_first; 1263 coro_cctx *first = cctx_first;
1169 cctx_first = first->next; 1264 cctx_first = first->next;
1170 --cctx_idle; 1265 --cctx_idle;
1171 1266
1203/* always use the TRANSFER macro */ 1298/* always use the TRANSFER macro */
1204static void NOINLINE 1299static void NOINLINE
1205transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1300transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1206{ 1301{
1207 dSTACKLEVEL; 1302 dSTACKLEVEL;
1208 static volatile int has_throw;
1209 1303
1210 /* sometimes transfer is only called to set idle_sp */ 1304 /* sometimes transfer is only called to set idle_sp */
1211 if (expect_false (!next)) 1305 if (expect_false (!next))
1212 { 1306 {
1213 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1307 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1217 { 1311 {
1218 coro_cctx *prev__cctx; 1312 coro_cctx *prev__cctx;
1219 1313
1220 if (expect_false (prev->flags & CF_NEW)) 1314 if (expect_false (prev->flags & CF_NEW))
1221 { 1315 {
1222 /* create a new empty context */ 1316 /* create a new empty/source context */
1223 Newz (0, prev->cctx, 1, coro_cctx); 1317 prev->cctx = cctx_new_empty ();
1224 prev->flags &= ~CF_NEW; 1318 prev->flags &= ~CF_NEW;
1225 prev->flags |= CF_RUNNING; 1319 prev->flags |= CF_RUNNING;
1226 } 1320 }
1227 1321
1228 prev->flags &= ~CF_RUNNING; 1322 prev->flags &= ~CF_RUNNING;
1269 ++next->usecount; 1363 ++next->usecount;
1270 1364
1271 if (expect_true (!next->cctx)) 1365 if (expect_true (!next->cctx))
1272 next->cctx = cctx_get (aTHX); 1366 next->cctx = cctx_get (aTHX);
1273 1367
1274 has_throw = !!next->throw; 1368 assert (("FATAL ERROR: internal error 1352 in Coro, please report", !transfer_next));//D
1369 transfer_next = next;
1275 1370
1276 if (expect_false (prev__cctx != next->cctx)) 1371 if (expect_false (prev__cctx != next->cctx))
1277 { 1372 {
1278 prev__cctx->top_env = PL_top_env; 1373 prev__cctx->top_env = PL_top_env;
1279 PL_top_env = next->cctx->top_env; 1374 PL_top_env = next->cctx->top_env;
1280 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1375 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
1281 } 1376 }
1282 1377
1283 free_coro_mortal (aTHX); 1378 transfer_tail ();
1284 UNLOCK;
1285
1286 if (expect_false (has_throw))
1287 {
1288 struct coro *coro = SvSTATE (coro_current);
1289
1290 if (coro->throw)
1291 {
1292 SV *exception = coro->throw;
1293 coro->throw = 0;
1294 sv_setsv (ERRSV, exception);
1295 croak (0);
1296 }
1297 }
1298 } 1379 }
1299} 1380}
1300
1301struct transfer_args
1302{
1303 struct coro *prev, *next;
1304};
1305 1381
1306#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))
1307#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) 1383#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1308 1384
1309/** high level stuff ********************************************************/ 1385/** high level stuff ********************************************************/
1335 croak ("FATAL: tried to destroy currently running coroutine"); 1411 croak ("FATAL: tried to destroy currently running coroutine");
1336 1412
1337 save_perl (aTHX_ &temp); 1413 save_perl (aTHX_ &temp);
1338 load_perl (aTHX_ coro); 1414 load_perl (aTHX_ coro);
1339 1415
1340 coro_destroy (aTHX_ coro); 1416 coro_destruct (aTHX_ coro);
1341 1417
1342 load_perl (aTHX_ &temp); 1418 load_perl (aTHX_ &temp);
1343 1419
1344 coro->slot = 0; 1420 coro->slot = 0;
1345 } 1421 }
1521 /* cannot transfer to destroyed coros, skip and look for next */ 1597 /* cannot transfer to destroyed coros, skip and look for next */
1522 if (expect_false (ta->next->flags & CF_DESTROYED)) 1598 if (expect_false (ta->next->flags & CF_DESTROYED))
1523 { 1599 {
1524 UNLOCK; 1600 UNLOCK;
1525 SvREFCNT_dec (next_sv); 1601 SvREFCNT_dec (next_sv);
1526 /* coro_nready is already taken care of by destroy */ 1602 /* coro_nready has already been taken care of by destroy */
1527 continue; 1603 continue;
1528 } 1604 }
1529 1605
1530 --coro_nready; 1606 --coro_nready;
1531 UNLOCK; 1607 UNLOCK;
1616 struct coro *coro = SvSTATE (coro_sv); 1692 struct coro *coro = SvSTATE (coro_sv);
1617 1693
1618 if (flags & CC_TRACE) 1694 if (flags & CC_TRACE)
1619 { 1695 {
1620 if (!coro->cctx) 1696 if (!coro->cctx)
1621 coro->cctx = cctx_new (); 1697 coro->cctx = cctx_new_run ();
1622 else if (!(coro->cctx->flags & CC_TRACE)) 1698 else if (!(coro->cctx->flags & CC_TRACE))
1623 croak ("cannot enable tracing on coroutine with custom stack"); 1699 croak ("cannot enable tracing on coroutine with custom stack");
1624 1700
1625 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1701 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1626 } 1702 }
1633 else 1709 else
1634 coro->slot->runops = RUNOPS_DEFAULT; 1710 coro->slot->runops = RUNOPS_DEFAULT;
1635 } 1711 }
1636} 1712}
1637 1713
1714#if 0
1638static int 1715static int
1639coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) 1716coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1640{ 1717{
1641 AV *padlist; 1718 AV *padlist;
1642 AV *av = (AV *)mg->mg_obj; 1719 AV *av = (AV *)mg->mg_obj;
1648 1725
1649static MGVTBL coro_gensub_vtbl = { 1726static MGVTBL coro_gensub_vtbl = {
1650 0, 0, 0, 0, 1727 0, 0, 0, 0,
1651 coro_gensub_free 1728 coro_gensub_free
1652}; 1729};
1730#endif
1653 1731
1654/*****************************************************************************/ 1732/*****************************************************************************/
1655/* PerlIO::cede */ 1733/* PerlIO::cede */
1656 1734
1657typedef struct 1735typedef struct
1732PROTOTYPES: DISABLE 1810PROTOTYPES: DISABLE
1733 1811
1734BOOT: 1812BOOT:
1735{ 1813{
1736#ifdef USE_ITHREADS 1814#ifdef USE_ITHREADS
1737 MUTEX_INIT (&coro_mutex); 1815 MUTEX_INIT (&coro_lock);
1816# if CORO_PTHREAD
1817 coro_thx = PERL_GET_CONTEXT;
1818# endif
1738#endif 1819#endif
1739 BOOT_PAGESIZE; 1820 BOOT_PAGESIZE;
1740 1821
1741 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1822 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1742 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1823 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1823 1904
1824 PUTBACK; 1905 PUTBACK;
1825 switch (ix) 1906 switch (ix)
1826 { 1907 {
1827 case 0: 1908 case 0:
1828 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); 1909 prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (ST (0)));
1829 ta.next = 0;
1830 break; 1910 break;
1831 1911
1832 case 1: 1912 case 1:
1833 if (items != 2) 1913 if (items != 2)
1834 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);
1835 1915
1836 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 1916 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1837 break; 1917 break;
1838 1918
1839 case 2: 1919 case 2:
1873 _exit (code); 1953 _exit (code);
1874 1954
1875int 1955int
1876cctx_stacksize (int new_stacksize = 0) 1956cctx_stacksize (int new_stacksize = 0)
1877 CODE: 1957 CODE:
1878 RETVAL = coro_stacksize; 1958 RETVAL = cctx_stacksize;
1879 if (new_stacksize) 1959 if (new_stacksize)
1960 {
1880 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;
1881 OUTPUT: 1973 OUTPUT:
1882 RETVAL 1974 RETVAL
1883 1975
1884int 1976int
1885cctx_count () 1977cctx_count ()
1963 RETVAL = boolSV (coro->flags & ix); 2055 RETVAL = boolSV (coro->flags & ix);
1964 OUTPUT: 2056 OUTPUT:
1965 RETVAL 2057 RETVAL
1966 2058
1967void 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
1968api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 2067api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1969 2068
1970SV * 2069SV *
1971has_cctx (Coro::State coro) 2070has_cctx (Coro::State coro)
1972 PROTOTYPE: $ 2071 PROTOTYPE: $
1981 CODE: 2080 CODE:
1982 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; 2081 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1983 OUTPUT: 2082 OUTPUT:
1984 RETVAL 2083 RETVAL
1985 2084
1986IV 2085UV
1987rss (Coro::State coro) 2086rss (Coro::State coro)
1988 PROTOTYPE: $ 2087 PROTOTYPE: $
1989 ALIAS: 2088 ALIAS:
1990 usecount = 1 2089 usecount = 1
1991 CODE: 2090 CODE:
2002 CODE: 2101 CODE:
2003 struct coro *coro = SvSTATE (coro_current); 2102 struct coro *coro = SvSTATE (coro_current);
2004 coro->cctx->idle_sp = 0; 2103 coro->cctx->idle_sp = 0;
2005 2104
2006void 2105void
2007throw (Coro::State self, SV *throw = &PL_sv_undef)
2008 PROTOTYPE: $;$
2009 CODE:
2010 SvREFCNT_dec (self->throw);
2011 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2012
2013void
2014swap_defsv (Coro::State self) 2106swap_defsv (Coro::State self)
2015 PROTOTYPE: $ 2107 PROTOTYPE: $
2016 ALIAS: 2108 ALIAS:
2017 swap_defav = 1 2109 swap_defav = 1
2018 CODE: 2110 CODE:
2173 sv_setsv (cb, &PL_sv_undef); 2265 sv_setsv (cb, &PL_sv_undef);
2174 2266
2175 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2267 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2176 coro->saved_deffh = 0; 2268 coro->saved_deffh = 0;
2177 2269
2178 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2270 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2179 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2271 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2180 { 2272 {
2181 SV *old = PL_diehook; 2273 SV *old = PL_diehook;
2182 PL_diehook = 0; 2274 PL_diehook = 0;
2183 SvREFCNT_dec (old); 2275 SvREFCNT_dec (old);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines