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.255 by root, Sat Nov 8 04:52:01 2008 UTC vs.
Revision 1.263 by root, Wed Nov 12 04:49:06 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
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") 128# define BARRIER __asm__ __volatile__ ("" : : : "memory")
132# define expect(expr,value) __builtin_expect ((expr),(value)) 129# define expect(expr,value) __builtin_expect ((expr),(value))
130# define INLINE static inline
133#else 131#else
134# define attribute(x) 132# define attribute(x)
135# define BARRIER 133# define BARRIER
136# define expect(expr,value) (expr) 134# define expect(expr,value) (expr)
135# define INLINE static
137#endif 136#endif
138 137
139#define expect_false(expr) expect ((expr) != 0, 0) 138#define expect_false(expr) expect ((expr) != 0, 0)
140#define expect_true(expr) expect ((expr) != 0, 1) 139#define expect_true(expr) expect ((expr) != 0, 1)
141 140
142#define NOINLINE attribute ((noinline)) 141#define NOINLINE attribute ((noinline))
143 142
144#include "CoroAPI.h" 143#include "CoroAPI.h"
145 144
146#ifdef USE_ITHREADS 145#ifdef USE_ITHREADS
146
147static perl_mutex coro_lock; 147static perl_mutex coro_lock;
148# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) 148# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0)
149# define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) 149# define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0)
150# if CORO_PTHREAD
151static void *coro_thx;
152# endif
153
150#else 154#else
155
151# define LOCK (void)0 156# define LOCK (void)0
152# define UNLOCK (void)0 157# define UNLOCK (void)0
158
153#endif 159#endif
160
161# undef LOCK
162# define LOCK (void)0
163# undef UNLOCK
164# define UNLOCK (void)0
154 165
155/* helper storage struct for Coro::AIO */ 166/* helper storage struct for Coro::AIO */
156struct io_state 167struct io_state
157{ 168{
158 AV *res; 169 AV *res;
169static struct CoroAPI coroapi; 180static struct CoroAPI coroapi;
170static AV *main_mainstack; /* used to differentiate between $main and others */ 181static AV *main_mainstack; /* used to differentiate between $main and others */
171static JMPENV *main_top_env; 182static JMPENV *main_top_env;
172static HV *coro_state_stash, *coro_stash; 183static HV *coro_state_stash, *coro_stash;
173static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 184static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
185static volatile struct coro *transfer_next;
186
187struct transfer_args
188{
189 struct coro *prev, *next;
190};
174 191
175static GV *irsgv; /* $/ */ 192static GV *irsgv; /* $/ */
176static GV *stdoutgv; /* *STDOUT */ 193static GV *stdoutgv; /* *STDOUT */
177static SV *rv_diehook; 194static SV *rv_diehook;
178static SV *rv_warnhook; 195static SV *rv_warnhook;
280#define PRIO_MIN -4 297#define PRIO_MIN -4
281 298
282/* for Coro.pm */ 299/* for Coro.pm */
283static SV *coro_current; 300static SV *coro_current;
284static SV *coro_readyhook; 301static SV *coro_readyhook;
285static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 302static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
286static int coro_nready; 303static int coro_nready;
287static struct coro *coro_first; 304static struct coro *coro_first;
288 305
289/** lowlevel stuff **********************************************************/ 306/** lowlevel stuff **********************************************************/
290 307
395 : 0 412 : 0
396 413
397#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 414#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) 415#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
399 416
400static struct coro * 417INLINE struct coro *
401SvSTATE_ (pTHX_ SV *coro) 418SvSTATE_ (pTHX_ SV *coro)
402{ 419{
403 HV *stash; 420 HV *stash;
404 MAGIC *mg; 421 MAGIC *mg;
405 422
709#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ 726#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \
710 SvPV_nolen((SV*)((mg)->mg_ptr)) : \ 727 SvPV_nolen((SV*)((mg)->mg_ptr)) : \
711 (const char*)(mg)->mg_ptr) 728 (const char*)(mg)->mg_ptr)
712#endif 729#endif
713 730
731/* we sometimes need to create the effect of entersub calling us */
732#define ENTERSUB_HEAD ENTER; SAVETMPS
733/* we somtimes need to create the effect of leaving via entersub */
734#define ENTERSUB_TAIL LEAVE
735
714/* 736/*
715 * This overrides the default magic get method of %SIG elements. 737 * This overrides the default magic get method of %SIG elements.
716 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 738 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
717 * and instead of tryign to save and restore the hash elements, we just provide 739 * and instead of tryign to save and restore the hash elements, we just provide
718 * readback here. 740 * readback here.
822 PL_rs = newSVsv (GvSV (irsgv)); 844 PL_rs = newSVsv (GvSV (irsgv));
823 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 845 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
824 846
825 { 847 {
826 dSP; 848 dSP;
827 LOGOP myop; 849 UNOP myop;
828 850
829 Zero (&myop, 1, LOGOP); 851 Zero (&myop, 1, UNOP);
830 myop.op_next = Nullop; 852 myop.op_next = Nullop;
831 myop.op_flags = OPf_WANT_VOID; 853 myop.op_flags = OPf_WANT_VOID;
832 854
833 PUSHMARK (SP); 855 PUSHMARK (SP);
834 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 856 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
838 SPAGAIN; 860 SPAGAIN;
839 } 861 }
840 862
841 /* this newly created coroutine might be run on an existing cctx which most 863 /* this newly created coroutine might be run on an existing cctx which most
842 * likely was suspended in set_stacklevel, called from entersub. 864 * likely was suspended in set_stacklevel, called from entersub.
843 * set_stacklevl doesn't do anything on return, but entersub does LEAVE, 865 * set_stacklevel doesn't do anything on return, but entersub does LEAVE,
844 * so we ENTER here for symmetry 866 * so we ENTER here for symmetry.
845 */ 867 */
846 ENTER; 868 ENTERSUB_HEAD;
847} 869}
848 870
849static void 871static void
850coro_destruct (pTHX_ struct coro *coro) 872coro_destruct (pTHX_ struct coro *coro)
851{ 873{
880 SvREFCNT_dec (coro->throw); 902 SvREFCNT_dec (coro->throw);
881 903
882 coro_destruct_stacks (aTHX); 904 coro_destruct_stacks (aTHX);
883} 905}
884 906
885static void 907INLINE void
886free_coro_mortal (pTHX) 908free_coro_mortal (pTHX)
887{ 909{
888 if (expect_true (coro_mortal)) 910 if (expect_true (coro_mortal))
889 { 911 {
890 SvREFCNT_dec (coro_mortal); 912 SvREFCNT_dec (coro_mortal);
1014 1036
1015 TAINT_NOT; 1037 TAINT_NOT;
1016 return 0; 1038 return 0;
1017} 1039}
1018 1040
1041static void
1042prepare_set_stacklevel (struct transfer_args *ta, struct coro_cctx *cctx)
1043{
1044 ta->prev = (struct coro *)cctx;
1045 ta->next = 0;
1046}
1047
1019/* inject a fake call to Coro::State::_cctx_init into the execution */ 1048/* 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 */ 1049/* _cctx_init should be careful, as it could be called at almost any time */
1021/* during execution of a perl program */ 1050/* during execution of a perl program */
1051/* also initialises PL_top_env */
1022static void NOINLINE 1052static void NOINLINE
1023cctx_prepare (pTHX_ coro_cctx *cctx) 1053cctx_prepare (pTHX_ coro_cctx *cctx)
1024{ 1054{
1025 dSP; 1055 dSP;
1026 LOGOP myop; 1056 UNOP myop;
1027 1057
1028 PL_top_env = &PL_start_env; 1058 PL_top_env = &PL_start_env;
1029 1059
1030 if (cctx->flags & CC_TRACE) 1060 if (cctx->flags & CC_TRACE)
1031 PL_runops = runops_trace; 1061 PL_runops = runops_trace;
1032 1062
1033 Zero (&myop, 1, LOGOP); 1063 Zero (&myop, 1, UNOP);
1034 myop.op_next = PL_op; 1064 myop.op_next = PL_op;
1035 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 1065 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
1036 1066
1037 PUSHMARK (SP); 1067 PUSHMARK (SP);
1038 EXTEND (SP, 2); 1068 EXTEND (SP, 2);
1039 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); 1069 PUSHs (sv_2mortal (newSViv ((IV)cctx)));
1040 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 1070 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
1041 PUTBACK; 1071 PUTBACK;
1042 PL_op = (OP *)&myop; 1072 PL_op = (OP *)&myop;
1043 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 1073 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1044 SPAGAIN; 1074 SPAGAIN;
1045} 1075}
1046 1076
1077/* the tail of transfer: execute stuff we can only do after a transfer */
1078INLINE void
1079transfer_tail (pTHX)
1080{
1081 struct coro *next = (struct coro *)transfer_next;
1082 assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */
1083 assert (("FATAL: transfer_next was zero in transfer_tail (please report)", next));
1084
1085 free_coro_mortal (aTHX);
1086 UNLOCK;
1087
1088 if (expect_false (next->throw))
1089 {
1090 SV *exception = sv_2mortal (next->throw);
1091
1092 next->throw = 0;
1093 sv_setsv (ERRSV, exception);
1094 croak (0);
1095 }
1096}
1097
1047/* 1098/*
1048 * this is a _very_ stripped down perl interpreter ;) 1099 * this is a _very_ stripped down perl interpreter ;)
1049 */ 1100 */
1050static void 1101static void
1051cctx_run (void *arg) 1102cctx_run (void *arg)
1052{ 1103{
1104#ifdef USE_ITHREADS
1105# if CORO_PTHREAD
1106 PERL_SET_CONTEXT (coro_thx);
1107# endif
1108#endif
1109 {
1053 dTHX; 1110 dTHX;
1054 1111
1055 /* cctx_run is the alternative tail of transfer(), so unlock here. */ 1112 /* entersub called ENTER, but we never 'returned', undo that here */
1056 UNLOCK; 1113 ENTERSUB_TAIL;
1057 1114
1058 /* we now skip the entersub that lead to transfer() */ 1115 /* we now skip the entersub that did lead to transfer() */
1059 PL_op = PL_op->op_next; 1116 PL_op = PL_op->op_next;
1060 1117
1061 /* inject a fake subroutine call to cctx_init */ 1118 /* inject a fake subroutine call to cctx_init */
1062 cctx_prepare (aTHX_ (coro_cctx *)arg); 1119 cctx_prepare (aTHX_ (coro_cctx *)arg);
1063 1120
1121 /* cctx_run is the alternative tail of transfer() */
1122 transfer_tail (aTHX);
1123
1064 /* somebody or something will hit me for both perl_run and PL_restartop */ 1124 /* somebody or something will hit me for both perl_run and PL_restartop */
1065 PL_restartop = PL_op; 1125 PL_restartop = PL_op;
1066 perl_run (PL_curinterp); 1126 perl_run (PL_curinterp);
1067 1127
1068 /* 1128 /*
1069 * If perl-run returns we assume exit() was being called or the coro 1129 * 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) 1130 * 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 1131 * 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" 1132 * bootstrap-time "top" top_env, as we cannot restore the "main"
1073 * coroutine as Coro has no such concept 1133 * coroutine as Coro has no such concept
1074 */ 1134 */
1075 PL_top_env = main_top_env; 1135 PL_top_env = main_top_env;
1076 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1136 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1137 }
1077} 1138}
1078 1139
1079static coro_cctx * 1140static coro_cctx *
1080cctx_new () 1141cctx_new ()
1081{ 1142{
1082 coro_cctx *cctx; 1143 coro_cctx *cctx;
1144
1145 ++cctx_count;
1146 New (0, cctx, 1, coro_cctx);
1147
1148 cctx->gen = cctx_gen;
1149 cctx->flags = 0;
1150 cctx->idle_sp = 0; /* can be accessed by transfer between cctx_run and set_stacklevel, on throw */
1151
1152 return cctx;
1153}
1154
1155/* create a new cctx only suitable as source */
1156static coro_cctx *
1157cctx_new_empty ()
1158{
1159 coro_cctx *cctx = cctx_new ();
1160
1161 cctx->sptr = 0;
1162 coro_create (&cctx->cctx, 0, 0, 0, 0);
1163
1164 return cctx;
1165}
1166
1167/* create a new cctx suitable as destination/running a perl interpreter */
1168static coro_cctx *
1169cctx_new_run ()
1170{
1171 coro_cctx *cctx = cctx_new ();
1083 void *stack_start; 1172 void *stack_start;
1084 size_t stack_size; 1173 size_t stack_size;
1085
1086 ++cctx_count;
1087 Newz (0, cctx, 1, coro_cctx);
1088
1089 cctx->gen = cctx_gen;
1090 1174
1091#if HAVE_MMAP 1175#if HAVE_MMAP
1092 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1176 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1093 /* mmap supposedly does allocate-on-write for us */ 1177 /* 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); 1178 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
1095 1179
1096 if (cctx->sptr != (void *)-1) 1180 if (cctx->sptr != (void *)-1)
1097 { 1181 {
1098# if CORO_STACKGUARD 1182 #if CORO_STACKGUARD
1099 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1183 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1100# endif 1184 #endif
1101 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 1185 stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE;
1102 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 1186 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
1103 cctx->flags |= CC_MAPPED; 1187 cctx->flags |= CC_MAPPED;
1104 } 1188 }
1105 else 1189 else
1106#endif 1190#endif
1107 { 1191 {
1116 1200
1117 stack_start = cctx->sptr; 1201 stack_start = cctx->sptr;
1118 stack_size = cctx->ssize; 1202 stack_size = cctx->ssize;
1119 } 1203 }
1120 1204
1121 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); 1205 #if CORO_USE_VALGRIND
1206 cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size);
1207 #endif
1208
1122 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); 1209 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size);
1123 1210
1124 return cctx; 1211 return cctx;
1125} 1212}
1126 1213
1134 coro_destroy (&cctx->cctx); 1221 coro_destroy (&cctx->cctx);
1135 1222
1136 /* coro_transfer creates new, empty cctx's */ 1223 /* coro_transfer creates new, empty cctx's */
1137 if (cctx->sptr) 1224 if (cctx->sptr)
1138 { 1225 {
1139#if CORO_USE_VALGRIND 1226 #if CORO_USE_VALGRIND
1140 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1227 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1141#endif 1228 #endif
1142 1229
1143#if HAVE_MMAP 1230#if HAVE_MMAP
1144 if (cctx->flags & CC_MAPPED) 1231 if (cctx->flags & CC_MAPPED)
1145 munmap (cctx->sptr, cctx->ssize); 1232 munmap (cctx->sptr, cctx->ssize);
1146 else 1233 else
1167 return cctx; 1254 return cctx;
1168 1255
1169 cctx_destroy (cctx); 1256 cctx_destroy (cctx);
1170 } 1257 }
1171 1258
1172 return cctx_new (); 1259 return cctx_new_run ();
1173} 1260}
1174 1261
1175static void 1262static void
1176cctx_put (coro_cctx *cctx) 1263cctx_put (coro_cctx *cctx)
1177{ 1264{
1178 assert (("cctx_put called on non-initialised cctx", cctx->sptr)); 1265 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr));
1179 1266
1180 /* free another cctx if overlimit */ 1267 /* free another cctx if overlimit */
1181 if (expect_false (cctx_idle >= cctx_max_idle)) 1268 if (expect_false (cctx_idle >= cctx_max_idle))
1182 { 1269 {
1183 coro_cctx *first = cctx_first; 1270 coro_cctx *first = cctx_first;
1227 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1314 ((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 */ 1315 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1229 } 1316 }
1230 else if (expect_true (prev != next)) 1317 else if (expect_true (prev != next))
1231 { 1318 {
1232 static volatile int has_throw;
1233 coro_cctx *prev__cctx; 1319 coro_cctx *prev__cctx;
1234 1320
1235 if (expect_false (prev->flags & CF_NEW)) 1321 if (expect_false (prev->flags & CF_NEW))
1236 { 1322 {
1237 /* create a new empty/source context */ 1323 /* create a new empty/source context */
1238 ++cctx_count; 1324 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; 1325 prev->flags &= ~CF_NEW;
1244 prev->flags |= CF_RUNNING; 1326 prev->flags |= CF_RUNNING;
1245 } 1327 }
1246 1328
1247 prev->flags &= ~CF_RUNNING; 1329 prev->flags &= ~CF_RUNNING;
1262 else 1344 else
1263 load_perl (aTHX_ next); 1345 load_perl (aTHX_ next);
1264 1346
1265 prev__cctx = prev->cctx; 1347 prev__cctx = prev->cctx;
1266 1348
1267 /* possibly "free" the cctx */ 1349 /* possibly untie and reuse the cctx */
1268 if (expect_true ( 1350 if (expect_true (
1269 prev__cctx->idle_sp == STACKLEVEL 1351 prev__cctx->idle_sp == STACKLEVEL
1270 && !(prev__cctx->flags & CC_TRACE) 1352 && !(prev__cctx->flags & CC_TRACE)
1271 && !force_cctx 1353 && !force_cctx
1272 )) 1354 ))
1273 { 1355 {
1274 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1356 /* 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)); 1357 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te));
1276 1358
1277 prev->cctx = 0; 1359 prev->cctx = 0;
1278 1360
1279 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1361 /* 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 */ 1362 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1288 ++next->usecount; 1370 ++next->usecount;
1289 1371
1290 if (expect_true (!next->cctx)) 1372 if (expect_true (!next->cctx))
1291 next->cctx = cctx_get (aTHX); 1373 next->cctx = cctx_get (aTHX);
1292 1374
1293 has_throw = !!next->throw; 1375 assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next));
1376 transfer_next = next;
1294 1377
1295 if (expect_false (prev__cctx != next->cctx)) 1378 if (expect_false (prev__cctx != next->cctx))
1296 { 1379 {
1297 prev__cctx->top_env = PL_top_env; 1380 prev__cctx->top_env = PL_top_env;
1298 PL_top_env = next->cctx->top_env; 1381 PL_top_env = next->cctx->top_env;
1299 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1382 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
1300 } 1383 }
1301 1384
1302 free_coro_mortal (aTHX); 1385 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 } 1386 }
1318} 1387}
1319
1320struct transfer_args
1321{
1322 struct coro *prev, *next;
1323};
1324 1388
1325#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) 1389#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) 1390#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1327 1391
1328/** high level stuff ********************************************************/ 1392/** high level stuff ********************************************************/
1501 1565
1502static int 1566static int
1503api_is_ready (SV *coro_sv) 1567api_is_ready (SV *coro_sv)
1504{ 1568{
1505 dTHX; 1569 dTHX;
1570
1506 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1571 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1507} 1572}
1508 1573
1509static void 1574INLINE void
1510prepare_schedule (pTHX_ struct transfer_args *ta) 1575prepare_schedule (pTHX_ struct transfer_args *ta)
1511{ 1576{
1512 SV *prev_sv, *next_sv; 1577 SV *prev_sv, *next_sv;
1513 1578
1514 for (;;) 1579 for (;;)
1540 /* cannot transfer to destroyed coros, skip and look for next */ 1605 /* cannot transfer to destroyed coros, skip and look for next */
1541 if (expect_false (ta->next->flags & CF_DESTROYED)) 1606 if (expect_false (ta->next->flags & CF_DESTROYED))
1542 { 1607 {
1543 UNLOCK; 1608 UNLOCK;
1544 SvREFCNT_dec (next_sv); 1609 SvREFCNT_dec (next_sv);
1545 /* coro_nready is already taken care of by destroy */ 1610 /* coro_nready has already been taken care of by destroy */
1546 continue; 1611 continue;
1547 } 1612 }
1548 1613
1549 --coro_nready; 1614 --coro_nready;
1550 UNLOCK; 1615 UNLOCK;
1553 1618
1554 /* free this only after the transfer */ 1619 /* free this only after the transfer */
1555 prev_sv = SvRV (coro_current); 1620 prev_sv = SvRV (coro_current);
1556 ta->prev = SvSTATE (prev_sv); 1621 ta->prev = SvSTATE (prev_sv);
1557 TRANSFER_CHECK (*ta); 1622 TRANSFER_CHECK (*ta);
1558 assert (ta->next->flags & CF_READY); 1623 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY));
1559 ta->next->flags &= ~CF_READY; 1624 ta->next->flags &= ~CF_READY;
1560 SvRV_set (coro_current, next_sv); 1625 SvRV_set (coro_current, next_sv);
1561 1626
1562 LOCK; 1627 LOCK;
1563 free_coro_mortal (aTHX); 1628 free_coro_mortal (aTHX);
1564 coro_mortal = prev_sv; 1629 coro_mortal = prev_sv;
1565 UNLOCK; 1630 UNLOCK;
1566} 1631}
1567 1632
1568static void 1633INLINE void
1569prepare_cede (pTHX_ struct transfer_args *ta) 1634prepare_cede (pTHX_ struct transfer_args *ta)
1570{ 1635{
1571 api_ready (coro_current); 1636 api_ready (coro_current);
1572 prepare_schedule (aTHX_ ta); 1637 prepare_schedule (aTHX_ ta);
1573} 1638}
1635 struct coro *coro = SvSTATE (coro_sv); 1700 struct coro *coro = SvSTATE (coro_sv);
1636 1701
1637 if (flags & CC_TRACE) 1702 if (flags & CC_TRACE)
1638 { 1703 {
1639 if (!coro->cctx) 1704 if (!coro->cctx)
1640 coro->cctx = cctx_new (); 1705 coro->cctx = cctx_new_run ();
1641 else if (!(coro->cctx->flags & CC_TRACE)) 1706 else if (!(coro->cctx->flags & CC_TRACE))
1642 croak ("cannot enable tracing on coroutine with custom stack"); 1707 croak ("cannot enable tracing on coroutine with custom stack");
1643 1708
1644 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1709 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1645 } 1710 }
1754 1819
1755BOOT: 1820BOOT:
1756{ 1821{
1757#ifdef USE_ITHREADS 1822#ifdef USE_ITHREADS
1758 MUTEX_INIT (&coro_lock); 1823 MUTEX_INIT (&coro_lock);
1824# if CORO_PTHREAD
1825 coro_thx = PERL_GET_CONTEXT;
1826# endif
1759#endif 1827#endif
1760 BOOT_PAGESIZE; 1828 BOOT_PAGESIZE;
1761 1829
1762 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1830 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1763 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1831 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1844 1912
1845 PUTBACK; 1913 PUTBACK;
1846 switch (ix) 1914 switch (ix)
1847 { 1915 {
1848 case 0: 1916 case 0:
1849 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); 1917 prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (ST (0)));
1850 ta.next = 0;
1851 break; 1918 break;
1852 1919
1853 case 1: 1920 case 1:
1854 if (items != 2) 1921 if (items != 2)
1855 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items); 1922 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items);
1994 is_destroyed = CF_DESTROYED 2061 is_destroyed = CF_DESTROYED
1995 CODE: 2062 CODE:
1996 RETVAL = boolSV (coro->flags & ix); 2063 RETVAL = boolSV (coro->flags & ix);
1997 OUTPUT: 2064 OUTPUT:
1998 RETVAL 2065 RETVAL
2066
2067void
2068throw (Coro::State self, SV *throw = &PL_sv_undef)
2069 PROTOTYPE: $;$
2070 CODE:
2071 SvREFCNT_dec (self->throw);
2072 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
1999 2073
2000void 2074void
2001api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 2075api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
2002 2076
2003SV * 2077SV *
2076 2150
2077 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 2151 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
2078 coro_ready[i] = newAV (); 2152 coro_ready[i] = newAV ();
2079 2153
2080 { 2154 {
2081 SV *sv = perl_get_sv ("Coro::API", TRUE); 2155 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2082 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
2083 2156
2084 coroapi.schedule = api_schedule; 2157 coroapi.schedule = api_schedule;
2085 coroapi.cede = api_cede; 2158 coroapi.cede = api_cede;
2086 coroapi.cede_notself = api_cede_notself; 2159 coroapi.cede_notself = api_cede_notself;
2087 coroapi.ready = api_ready; 2160 coroapi.ready = api_ready;
2147 CODE: 2220 CODE:
2148 RETVAL = coro_nready; 2221 RETVAL = coro_nready;
2149 OUTPUT: 2222 OUTPUT:
2150 RETVAL 2223 RETVAL
2151 2224
2152void
2153throw (Coro::State self, SV *throw = &PL_sv_undef)
2154 PROTOTYPE: $;$
2155 CODE:
2156 SvREFCNT_dec (self->throw);
2157 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2158
2159# for async_pool speedup 2225# for async_pool speedup
2160void 2226void
2161_pool_1 (SV *cb) 2227_pool_1 (SV *cb)
2162 CODE: 2228 CODE:
2163{ 2229{
2362 2428
2363MODULE = Coro::State PACKAGE = PerlIO::cede 2429MODULE = Coro::State PACKAGE = PerlIO::cede
2364 2430
2365BOOT: 2431BOOT:
2366 PerlIO_define_layer (aTHX_ &PerlIO_cede); 2432 PerlIO_define_layer (aTHX_ &PerlIO_cede);
2433

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines