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.258 by root, Sun Nov 9 23:08:49 2008 UTC vs.
Revision 1.266 by root, Fri Nov 14 03:26:22 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
181static struct CoroAPI coroapi; 178static struct CoroAPI coroapi;
182static AV *main_mainstack; /* used to differentiate between $main and others */ 179static AV *main_mainstack; /* used to differentiate between $main and others */
183static JMPENV *main_top_env; 180static JMPENV *main_top_env;
184static HV *coro_state_stash, *coro_stash; 181static HV *coro_state_stash, *coro_stash;
185static 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};
186 189
187static GV *irsgv; /* $/ */ 190static GV *irsgv; /* $/ */
188static GV *stdoutgv; /* *STDOUT */ 191static GV *stdoutgv; /* *STDOUT */
189static SV *rv_diehook; 192static SV *rv_diehook;
190static SV *rv_warnhook; 193static SV *rv_warnhook;
407 : 0 410 : 0
408 411
409#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)
410#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)
411 414
412static struct coro * 415INLINE struct coro *
413SvSTATE_ (pTHX_ SV *coro) 416SvSTATE_ (pTHX_ SV *coro)
414{ 417{
415 HV *stash; 418 HV *stash;
416 MAGIC *mg; 419 MAGIC *mg;
417 420
708 } 711 }
709 712
710 return rss; 713 return rss;
711} 714}
712 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 somtimes 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
713/** coroutine stack handling ************************************************/ 743/** coroutine stack handling ************************************************/
714 744
715static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); 745static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
716static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); 746static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg);
717static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg); 747static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg);
834 PL_rs = newSVsv (GvSV (irsgv)); 864 PL_rs = newSVsv (GvSV (irsgv));
835 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 865 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
836 866
837 { 867 {
838 dSP; 868 dSP;
839 LOGOP myop; 869 UNOP myop;
840 870
841 Zero (&myop, 1, LOGOP); 871 Zero (&myop, 1, UNOP);
842 myop.op_next = Nullop; 872 myop.op_next = Nullop;
843 myop.op_flags = OPf_WANT_VOID; 873 myop.op_flags = OPf_WANT_VOID;
844 874
845 PUSHMARK (SP); 875 PUSHMARK (SP);
846 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 876 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
849 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 879 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
850 SPAGAIN; 880 SPAGAIN;
851 } 881 }
852 882
853 /* 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
854 * likely was suspended in set_stacklevel, called from entersub. 884 * likely was suspended in set_stacklevel, called from pp_set_stacklevel,
855 * set_stacklevl doesn't do anything on return, but entersub does LEAVE, 885 * so we have to emulate entering pp_set_stacklevel here.
856 * so we ENTER here for symmetry
857 */ 886 */
858 ENTER; 887 SSL_HEAD;
859} 888}
860 889
861static void 890static void
862coro_destruct (pTHX_ struct coro *coro) 891coro_destruct (pTHX_ struct coro *coro)
863{ 892{
892 SvREFCNT_dec (coro->throw); 921 SvREFCNT_dec (coro->throw);
893 922
894 coro_destruct_stacks (aTHX); 923 coro_destruct_stacks (aTHX);
895} 924}
896 925
897static void 926INLINE void
898free_coro_mortal (pTHX) 927free_coro_mortal (pTHX)
899{ 928{
900 if (expect_true (coro_mortal)) 929 if (expect_true (coro_mortal))
901 { 930 {
902 SvREFCNT_dec (coro_mortal); 931 SvREFCNT_dec (coro_mortal);
1026 1055
1027 TAINT_NOT; 1056 TAINT_NOT;
1028 return 0; 1057 return 0;
1029} 1058}
1030 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
1031/* 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 */
1032/* _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 */
1033/* during execution of a perl program */ 1069/* during execution of a perl program */
1070/* also initialises PL_top_env */
1034static void NOINLINE 1071static void NOINLINE
1035cctx_prepare (pTHX_ coro_cctx *cctx) 1072cctx_prepare (pTHX_ coro_cctx *cctx)
1036{ 1073{
1037 dSP; 1074 dSP;
1038 LOGOP myop; 1075 UNOP myop;
1039 1076
1040 PL_top_env = &PL_start_env; 1077 PL_top_env = &PL_start_env;
1041 1078
1042 if (cctx->flags & CC_TRACE) 1079 if (cctx->flags & CC_TRACE)
1043 PL_runops = runops_trace; 1080 PL_runops = runops_trace;
1044 1081
1045 Zero (&myop, 1, LOGOP); 1082 Zero (&myop, 1, UNOP);
1046 myop.op_next = PL_op; 1083 myop.op_next = PL_op;
1047 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 1084 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
1048 1085
1049 PUSHMARK (SP); 1086 PUSHMARK (SP);
1050 EXTEND (SP, 2); 1087 EXTEND (SP, 2);
1051 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); 1088 PUSHs (sv_2mortal (newSViv ((IV)cctx)));
1052 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 1089 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
1053 PUTBACK; 1090 PUTBACK;
1054 PL_op = (OP *)&myop; 1091 PL_op = (OP *)&myop;
1055 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 1092 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1056 SPAGAIN; 1093 SPAGAIN;
1057} 1094}
1058 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
1059/* 1117/*
1060 * this is a _very_ stripped down perl interpreter ;) 1118 * this is a _very_ stripped down perl interpreter ;)
1061 */ 1119 */
1062static void 1120static void
1063cctx_run (void *arg) 1121cctx_run (void *arg)
1068# endif 1126# endif
1069#endif 1127#endif
1070 { 1128 {
1071 dTHX; 1129 dTHX;
1072 1130
1073 /* cctx_run is the alternative tail of transfer(), so unlock here. */ 1131 /* we are the alternative tail to pp_set_stacklevel */
1074 UNLOCK; 1132 /* so do the same things here */
1133 SSL_TAIL;
1075 1134
1076 /* we now skip the entersub that lead to transfer() */ 1135 /* we now skip the op that did lead to transfer() */
1077 PL_op = PL_op->op_next; 1136 PL_op = PL_op->op_next;
1078 1137
1079 /* inject a fake subroutine call to cctx_init */ 1138 /* inject a fake subroutine call to cctx_init */
1080 cctx_prepare (aTHX_ (coro_cctx *)arg); 1139 cctx_prepare (aTHX_ (coro_cctx *)arg);
1140
1141 /* cctx_run is the alternative tail of transfer() */
1142 transfer_tail (aTHX);
1081 1143
1082 /* 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 */
1083 PL_restartop = PL_op; 1145 PL_restartop = PL_op;
1084 perl_run (PL_curinterp); 1146 perl_run (PL_curinterp);
1085 1147
1101 coro_cctx *cctx; 1163 coro_cctx *cctx;
1102 1164
1103 ++cctx_count; 1165 ++cctx_count;
1104 New (0, cctx, 1, coro_cctx); 1166 New (0, cctx, 1, coro_cctx);
1105 1167
1106 cctx->gen = cctx_gen; 1168 cctx->gen = cctx_gen;
1107 cctx->flags = 0; 1169 cctx->flags = 0;
1170 cctx->idle_sp = 0; /* can be accessed by transfer between cctx_run and set_stacklevel, on throw */
1108 1171
1109 return cctx; 1172 return cctx;
1110} 1173}
1111 1174
1112/* create a new cctx only suitable as source */ 1175/* create a new cctx only suitable as source */
1113static coro_cctx * 1176static coro_cctx *
1114cctx_new_empty () 1177cctx_new_empty ()
1115{ 1178{
1116 coro_cctx *cctx = cctx_new (); 1179 coro_cctx *cctx = cctx_new ();
1117 1180
1118 cctx->sptr = 0; 1181 cctx->sptr = 0;
1119 cctx->idle_sp = 0; /* should never be a valid address */
1120 coro_create (&cctx->cctx, 0, 0, 0, 0); 1182 coro_create (&cctx->cctx, 0, 0, 0, 0);
1121 1183
1122 return cctx; 1184 return cctx;
1123} 1185}
1124 1186
1135 /* mmap supposedly does allocate-on-write for us */ 1197 /* mmap supposedly does allocate-on-write for us */
1136 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);
1137 1199
1138 if (cctx->sptr != (void *)-1) 1200 if (cctx->sptr != (void *)-1)
1139 { 1201 {
1140# if CORO_STACKGUARD 1202 #if CORO_STACKGUARD
1141 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1203 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1142# endif 1204 #endif
1143 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 1205 stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE;
1144 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 1206 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
1145 cctx->flags |= CC_MAPPED; 1207 cctx->flags |= CC_MAPPED;
1146 } 1208 }
1147 else 1209 else
1148#endif 1210#endif
1149 { 1211 {
1150 cctx->ssize = cctx_stacksize * (long)sizeof (long); 1212 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1151 New (0, cctx->sptr, cctx_stacksize, long); 1213 New (0, cctx->sptr, cctx_stacksize, long);
1152 1214
1153 if (!cctx->sptr) 1215 if (!cctx->sptr)
1154 { 1216 {
1155 perror ("FATAL: unable to allocate stack for coroutine"); 1217 perror ("FATAL: unable to allocate stack for coroutine, exiting.");
1156 _exit (EXIT_FAILURE); 1218 _exit (EXIT_FAILURE);
1157 } 1219 }
1158 1220
1159 stack_start = cctx->sptr; 1221 stack_start = cctx->sptr;
1160 stack_size = cctx->ssize; 1222 stack_size = cctx->ssize;
1161 } 1223 }
1162 1224
1163 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
1164 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);
1165 1230
1166 return cctx; 1231 return cctx;
1167} 1232}
1168 1233
1176 coro_destroy (&cctx->cctx); 1241 coro_destroy (&cctx->cctx);
1177 1242
1178 /* coro_transfer creates new, empty cctx's */ 1243 /* coro_transfer creates new, empty cctx's */
1179 if (cctx->sptr) 1244 if (cctx->sptr)
1180 { 1245 {
1181#if CORO_USE_VALGRIND 1246 #if CORO_USE_VALGRIND
1182 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1247 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1183#endif 1248 #endif
1184 1249
1185#if HAVE_MMAP 1250#if HAVE_MMAP
1186 if (cctx->flags & CC_MAPPED) 1251 if (cctx->flags & CC_MAPPED)
1187 munmap (cctx->sptr, cctx->ssize); 1252 munmap (cctx->sptr, cctx->ssize);
1188 else 1253 else
1215} 1280}
1216 1281
1217static void 1282static void
1218cctx_put (coro_cctx *cctx) 1283cctx_put (coro_cctx *cctx)
1219{ 1284{
1220 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));
1221 1286
1222 /* free another cctx if overlimit */ 1287 /* free another cctx if overlimit */
1223 if (expect_false (cctx_idle >= cctx_max_idle)) 1288 if (expect_false (cctx_idle >= cctx_max_idle))
1224 { 1289 {
1225 coro_cctx *first = cctx_first; 1290 coro_cctx *first = cctx_first;
1269 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1334 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1270 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 */
1271 } 1336 }
1272 else if (expect_true (prev != next)) 1337 else if (expect_true (prev != next))
1273 { 1338 {
1274 static volatile int has_throw;
1275 coro_cctx *prev__cctx; 1339 coro_cctx *prev__cctx;
1276 1340
1277 if (expect_false (prev->flags & CF_NEW)) 1341 if (expect_false (prev->flags & CF_NEW))
1278 { 1342 {
1279 /* create a new empty/source context */ 1343 /* create a new empty/source context */
1300 else 1364 else
1301 load_perl (aTHX_ next); 1365 load_perl (aTHX_ next);
1302 1366
1303 prev__cctx = prev->cctx; 1367 prev__cctx = prev->cctx;
1304 1368
1305 /* possibly "free" the cctx */ 1369 /* possibly untie and reuse the cctx */
1306 if (expect_true ( 1370 if (expect_true (
1307 prev__cctx->idle_sp == STACKLEVEL 1371 prev__cctx->idle_sp == STACKLEVEL
1308 && !(prev__cctx->flags & CC_TRACE) 1372 && !(prev__cctx->flags & CC_TRACE)
1309 && !force_cctx 1373 && !force_cctx
1310 )) 1374 ))
1311 { 1375 {
1312 /* 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 */
1313 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));
1314 1378
1315 prev->cctx = 0; 1379 prev->cctx = 0;
1316 1380
1317 /* 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 */
1318 /* 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 */
1326 ++next->usecount; 1390 ++next->usecount;
1327 1391
1328 if (expect_true (!next->cctx)) 1392 if (expect_true (!next->cctx))
1329 next->cctx = cctx_get (aTHX); 1393 next->cctx = cctx_get (aTHX);
1330 1394
1331 has_throw = !!next->throw; 1395 assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next));
1396 transfer_next = next;
1332 1397
1333 if (expect_false (prev__cctx != next->cctx)) 1398 if (expect_false (prev__cctx != next->cctx))
1334 { 1399 {
1335 prev__cctx->top_env = PL_top_env; 1400 prev__cctx->top_env = PL_top_env;
1336 PL_top_env = next->cctx->top_env; 1401 PL_top_env = next->cctx->top_env;
1337 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1402 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
1338 } 1403 }
1339 1404
1340 free_coro_mortal (aTHX); 1405 transfer_tail (aTHX);
1341 UNLOCK;
1342
1343 if (expect_false (has_throw))
1344 {
1345 struct coro *coro = SvSTATE (coro_current);
1346
1347 if (coro->throw)
1348 {
1349 SV *exception = coro->throw;
1350 coro->throw = 0;
1351 sv_setsv (ERRSV, exception);
1352 croak (0);
1353 }
1354 }
1355 } 1406 }
1356} 1407}
1357
1358struct transfer_args
1359{
1360 struct coro *prev, *next;
1361};
1362 1408
1363#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))
1364#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) 1410#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1365 1411
1366/** high level stuff ********************************************************/ 1412/** high level stuff ********************************************************/
1539 1585
1540static int 1586static int
1541api_is_ready (SV *coro_sv) 1587api_is_ready (SV *coro_sv)
1542{ 1588{
1543 dTHX; 1589 dTHX;
1590
1544 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1591 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1545} 1592}
1546 1593
1547static void 1594INLINE void
1548prepare_schedule (pTHX_ struct transfer_args *ta) 1595prepare_schedule (pTHX_ struct transfer_args *ta)
1549{ 1596{
1550 SV *prev_sv, *next_sv; 1597 SV *prev_sv, *next_sv;
1551 1598
1552 for (;;) 1599 for (;;)
1578 /* cannot transfer to destroyed coros, skip and look for next */ 1625 /* cannot transfer to destroyed coros, skip and look for next */
1579 if (expect_false (ta->next->flags & CF_DESTROYED)) 1626 if (expect_false (ta->next->flags & CF_DESTROYED))
1580 { 1627 {
1581 UNLOCK; 1628 UNLOCK;
1582 SvREFCNT_dec (next_sv); 1629 SvREFCNT_dec (next_sv);
1583 /* coro_nready is already taken care of by destroy */ 1630 /* coro_nready has already been taken care of by destroy */
1584 continue; 1631 continue;
1585 } 1632 }
1586 1633
1587 --coro_nready; 1634 --coro_nready;
1588 UNLOCK; 1635 UNLOCK;
1591 1638
1592 /* free this only after the transfer */ 1639 /* free this only after the transfer */
1593 prev_sv = SvRV (coro_current); 1640 prev_sv = SvRV (coro_current);
1594 ta->prev = SvSTATE (prev_sv); 1641 ta->prev = SvSTATE (prev_sv);
1595 TRANSFER_CHECK (*ta); 1642 TRANSFER_CHECK (*ta);
1596 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));
1597 ta->next->flags &= ~CF_READY; 1644 ta->next->flags &= ~CF_READY;
1598 SvRV_set (coro_current, next_sv); 1645 SvRV_set (coro_current, next_sv);
1599 1646
1600 LOCK; 1647 LOCK;
1601 free_coro_mortal (aTHX); 1648 free_coro_mortal (aTHX);
1602 coro_mortal = prev_sv; 1649 coro_mortal = prev_sv;
1603 UNLOCK; 1650 UNLOCK;
1604} 1651}
1605 1652
1606static void 1653INLINE void
1607prepare_cede (pTHX_ struct transfer_args *ta) 1654prepare_cede (pTHX_ struct transfer_args *ta)
1608{ 1655{
1609 api_ready (coro_current); 1656 api_ready (coro_current);
1610 prepare_schedule (aTHX_ ta); 1657 prepare_schedule (aTHX_ ta);
1611} 1658}
1783 PerlIOBuf_get_ptr, 1830 PerlIOBuf_get_ptr,
1784 PerlIOBuf_get_cnt, 1831 PerlIOBuf_get_cnt,
1785 PerlIOBuf_set_ptrcnt, 1832 PerlIOBuf_set_ptrcnt,
1786}; 1833};
1787 1834
1835/*****************************************************************************/
1836
1837static const CV *ssl_cv; /* for quick consistency check */
1838
1839static UNOP ssl_restore; /* restore stack as entersub did, for first-re-run */
1840static SV *ssl_arg0;
1841static SV *ssl_arg1;
1842
1843/* this restores the stack in the case we patched the entersub, to */
1844/* recreate the stack frame as perl will on following calls */
1845/* since entersub cleared the stack */
1846static OP *
1847pp_restore (pTHX)
1848{
1849 dSP;
1850
1851 PUSHMARK (SP);
1852
1853 EXTEND (SP, 3);
1854 if (ssl_arg0) PUSHs (sv_2mortal (ssl_arg0)), ssl_arg0 = 0;
1855 if (ssl_arg1) PUSHs (sv_2mortal (ssl_arg1)), ssl_arg1 = 0;
1856 PUSHs ((SV *)CvGV (ssl_cv));
1857
1858 RETURNOP (ssl_restore.op_first);
1859}
1860
1861/* declare prototype */
1862XS(XS_Coro__State__set_stacklevel);
1863
1864#define OPpENTERSUB_SSL 15
1865
1866static OP *
1867pp_set_stacklevel (pTHX)
1868{
1869 dSP;
1870 struct transfer_args ta;
1871 SV **arg = PL_stack_base + TOPMARK + 1;
1872 int items = SP - arg; /* args without function object */
1873
1874 /* do a quick consistency check on the "function" object, and if it isn't */
1875 /* for us, divert to the real entersub */
1876 if (SvTYPE (*sp) != SVt_PVGV || CvXSUB (GvCV (*sp)) != XS_Coro__State__set_stacklevel)
1877 return PL_ppaddr[OP_ENTERSUB](aTHX);
1878
1879 /* pop args */
1880 SP = PL_stack_base + POPMARK;
1881
1882 if (!(PL_op->op_flags & OPf_STACKED))
1883 {
1884 /* ampersand-form of call, use @_ instead of stack */
1885 AV *av = GvAV (PL_defgv);
1886 arg = AvARRAY (av);
1887 items = AvFILLp (av) + 1;
1888 }
1889
1890 PUTBACK;
1891 switch (PL_op->op_private & OPpENTERSUB_SSL)
1892 {
1893 case 0:
1894 prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (arg [0]));
1895 break;
1896
1897 case 1:
1898 if (items != 2)
1899 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d.", items);
1900
1901 prepare_transfer (aTHX_ &ta, arg [0], arg [1]);
1902 break;
1903
1904 case 2:
1905 prepare_schedule (aTHX_ &ta);
1906 break;
1907
1908 case 3:
1909 prepare_cede (aTHX_ &ta);
1910 break;
1911
1912 case 4:
1913 if (!prepare_cede_notself (aTHX_ &ta))
1914 goto skip;
1915
1916 break;
1917 }
1918
1919 TRANSFER (ta, 0);
1920 SPAGAIN;
1921
1922skip:
1923 PUTBACK;
1924 SSL_TAIL;
1925 SPAGAIN;
1926 RETURN;
1927}
1788 1928
1789MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1929MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1790 1930
1791PROTOTYPES: DISABLE 1931PROTOTYPES: DISABLE
1792
1793BOOT:
1794{
1795#ifdef USE_ITHREADS
1796 MUTEX_INIT (&coro_lock);
1797# if CORO_PTHREAD
1798 coro_thx = PERL_GET_CONTEXT;
1799# endif
1800#endif
1801 BOOT_PAGESIZE;
1802
1803 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1804 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1805
1806 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1807 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1808 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr;
1809
1810 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1811 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1812 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1813
1814 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1815
1816 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1817 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1818 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1819 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1820
1821 main_mainstack = PL_mainstack;
1822 main_top_env = PL_top_env;
1823
1824 while (main_top_env->je_prev)
1825 main_top_env = main_top_env->je_prev;
1826
1827 coroapi.ver = CORO_API_VERSION;
1828 coroapi.rev = CORO_API_REVISION;
1829 coroapi.transfer = api_transfer;
1830
1831 {
1832 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1833
1834 if (!svp) croak ("Time::HiRes is required");
1835 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1836
1837 nvtime = INT2PTR (double (*)(), SvIV (*svp));
1838 }
1839
1840 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1841}
1842
1843SV *
1844new (char *klass, ...)
1845 CODE:
1846{
1847 struct coro *coro;
1848 MAGIC *mg;
1849 HV *hv;
1850 int i;
1851
1852 Newz (0, coro, 1, struct coro);
1853 coro->args = newAV ();
1854 coro->flags = CF_NEW;
1855
1856 if (coro_first) coro_first->prev = coro;
1857 coro->next = coro_first;
1858 coro_first = coro;
1859
1860 coro->hv = hv = newHV ();
1861 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
1862 mg->mg_flags |= MGf_DUP;
1863 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1864
1865 av_extend (coro->args, items - 1);
1866 for (i = 1; i < items; i++)
1867 av_push (coro->args, newSVsv (ST (i)));
1868}
1869 OUTPUT:
1870 RETVAL
1871 1932
1872# these not obviously related functions are all rolled into the same xs 1933# these not obviously related functions are all rolled into the same xs
1873# function to increase chances that they all will call transfer with the same 1934# function to increase chances that they all will call transfer with the same
1874# stack offset 1935# stack offset
1875void 1936void
1879 Coro::schedule = 2 1940 Coro::schedule = 2
1880 Coro::cede = 3 1941 Coro::cede = 3
1881 Coro::cede_notself = 4 1942 Coro::cede_notself = 4
1882 CODE: 1943 CODE:
1883{ 1944{
1884 struct transfer_args ta; 1945 assert (("FATAL: ssl call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_set_stacklevel));
1885 1946
1886 PUTBACK; 1947 assert (("FATAL: ssl call with illegal CV value", CvGV (cv)));
1887 switch (ix) 1948 ssl_cv = cv;
1949
1950 /* we patch the op, and then re-run the whole call */
1951 /* we have to put some dummy argument on the stack for this to work */
1952 ssl_restore.op_next = (OP *)&ssl_restore;
1953 ssl_restore.op_type = OP_NULL;
1954 ssl_restore.op_ppaddr = pp_restore;
1955 ssl_restore.op_first = PL_op;
1956
1957 ssl_arg0 = items > 0 ? SvREFCNT_inc (ST (0)) : 0;
1958 ssl_arg1 = items > 1 ? SvREFCNT_inc (ST (1)) : 0;
1959
1960 PL_op->op_ppaddr = pp_set_stacklevel;
1961 PL_op->op_private = PL_op->op_private & ~OPpENTERSUB_SSL | ix; /* we potentially share our private flags with entersub */
1962
1963 PL_op = (OP *)&ssl_restore;
1964}
1965
1966BOOT:
1967{
1968#ifdef USE_ITHREADS
1969 MUTEX_INIT (&coro_lock);
1970# if CORO_PTHREAD
1971 coro_thx = PERL_GET_CONTEXT;
1972# endif
1973#endif
1974 BOOT_PAGESIZE;
1975
1976 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1977 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1978
1979 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1980 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1981 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr;
1982
1983 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1984 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1985 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1986
1987 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1988
1989 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1990 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1991 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1992 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1993
1994 main_mainstack = PL_mainstack;
1995 main_top_env = PL_top_env;
1996
1997 while (main_top_env->je_prev)
1998 main_top_env = main_top_env->je_prev;
1999
2000 coroapi.ver = CORO_API_VERSION;
2001 coroapi.rev = CORO_API_REVISION;
2002 coroapi.transfer = api_transfer;
2003
1888 { 2004 {
1889 case 0: 2005 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1890 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1891 ta.next = 0;
1892 break;
1893 2006
1894 case 1: 2007 if (!svp) croak ("Time::HiRes is required");
1895 if (items != 2) 2008 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1896 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items);
1897 2009
1898 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 2010 nvtime = INT2PTR (double (*)(), SvIV (*svp));
1899 break;
1900
1901 case 2:
1902 prepare_schedule (aTHX_ &ta);
1903 break;
1904
1905 case 3:
1906 prepare_cede (aTHX_ &ta);
1907 break;
1908
1909 case 4:
1910 if (!prepare_cede_notself (aTHX_ &ta))
1911 XSRETURN_EMPTY;
1912
1913 break;
1914 } 2011 }
1915 SPAGAIN;
1916 2012
1917 BARRIER; 2013 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1918 PUTBACK;
1919 TRANSFER (ta, 0);
1920 SPAGAIN; /* might be the sp of a different coroutine now */
1921 /* be extra careful not to ever do anything after TRANSFER */
1922} 2014}
2015
2016SV *
2017new (char *klass, ...)
2018 CODE:
2019{
2020 struct coro *coro;
2021 MAGIC *mg;
2022 HV *hv;
2023 int i;
2024
2025 Newz (0, coro, 1, struct coro);
2026 coro->args = newAV ();
2027 coro->flags = CF_NEW;
2028
2029 if (coro_first) coro_first->prev = coro;
2030 coro->next = coro_first;
2031 coro_first = coro;
2032
2033 coro->hv = hv = newHV ();
2034 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
2035 mg->mg_flags |= MGf_DUP;
2036 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2037
2038 av_extend (coro->args, items - 1);
2039 for (i = 1; i < items; i++)
2040 av_push (coro->args, newSVsv (ST (i)));
2041}
2042 OUTPUT:
2043 RETVAL
1923 2044
1924bool 2045bool
1925_destroy (SV *coro_sv) 2046_destroy (SV *coro_sv)
1926 CODE: 2047 CODE:
1927 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 2048 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
2035 is_destroyed = CF_DESTROYED 2156 is_destroyed = CF_DESTROYED
2036 CODE: 2157 CODE:
2037 RETVAL = boolSV (coro->flags & ix); 2158 RETVAL = boolSV (coro->flags & ix);
2038 OUTPUT: 2159 OUTPUT:
2039 RETVAL 2160 RETVAL
2161
2162void
2163throw (Coro::State self, SV *throw = &PL_sv_undef)
2164 PROTOTYPE: $;$
2165 CODE:
2166 SvREFCNT_dec (self->throw);
2167 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2040 2168
2041void 2169void
2042api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 2170api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
2043 2171
2044SV * 2172SV *
2117 2245
2118 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 2246 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
2119 coro_ready[i] = newAV (); 2247 coro_ready[i] = newAV ();
2120 2248
2121 { 2249 {
2122 SV *sv = perl_get_sv ("Coro::API", TRUE); 2250 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2123 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
2124 2251
2125 coroapi.schedule = api_schedule; 2252 coroapi.schedule = api_schedule;
2126 coroapi.cede = api_cede; 2253 coroapi.cede = api_cede;
2127 coroapi.cede_notself = api_cede_notself; 2254 coroapi.cede_notself = api_cede_notself;
2128 coroapi.ready = api_ready; 2255 coroapi.ready = api_ready;
2188 CODE: 2315 CODE:
2189 RETVAL = coro_nready; 2316 RETVAL = coro_nready;
2190 OUTPUT: 2317 OUTPUT:
2191 RETVAL 2318 RETVAL
2192 2319
2193void
2194throw (Coro::State self, SV *throw = &PL_sv_undef)
2195 PROTOTYPE: $;$
2196 CODE:
2197 SvREFCNT_dec (self->throw);
2198 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2199
2200# for async_pool speedup 2320# for async_pool speedup
2201void 2321void
2202_pool_1 (SV *cb) 2322_pool_1 (SV *cb)
2203 CODE: 2323 CODE:
2204{ 2324{
2403 2523
2404MODULE = Coro::State PACKAGE = PerlIO::cede 2524MODULE = Coro::State PACKAGE = PerlIO::cede
2405 2525
2406BOOT: 2526BOOT:
2407 PerlIO_define_layer (aTHX_ &PerlIO_cede); 2527 PerlIO_define_layer (aTHX_ &PerlIO_cede);
2528

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines