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.256 by root, Sat Nov 8 13:32:18 2008 UTC vs.
Revision 1.261 by root, Mon Nov 10 05:09:30 2008 UTC

181static struct CoroAPI coroapi; 181static struct CoroAPI coroapi;
182static AV *main_mainstack; /* used to differentiate between $main and others */ 182static AV *main_mainstack; /* used to differentiate between $main and others */
183static JMPENV *main_top_env; 183static JMPENV *main_top_env;
184static HV *coro_state_stash, *coro_stash; 184static HV *coro_state_stash, *coro_stash;
185static volatile SV *coro_mortal; /* will be freed/thrown 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};
186 192
187static GV *irsgv; /* $/ */ 193static GV *irsgv; /* $/ */
188static GV *stdoutgv; /* *STDOUT */ 194static GV *stdoutgv; /* *STDOUT */
189static SV *rv_diehook; 195static SV *rv_diehook;
190static SV *rv_warnhook; 196static SV *rv_warnhook;
292#define PRIO_MIN -4 298#define PRIO_MIN -4
293 299
294/* for Coro.pm */ 300/* for Coro.pm */
295static SV *coro_current; 301static SV *coro_current;
296static SV *coro_readyhook; 302static SV *coro_readyhook;
297static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 303static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
298static int coro_nready; 304static int coro_nready;
299static struct coro *coro_first; 305static struct coro *coro_first;
300 306
301/** lowlevel stuff **********************************************************/ 307/** lowlevel stuff **********************************************************/
302 308
834 PL_rs = newSVsv (GvSV (irsgv)); 840 PL_rs = newSVsv (GvSV (irsgv));
835 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 841 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
836 842
837 { 843 {
838 dSP; 844 dSP;
839 LOGOP myop; 845 UNOP myop;
840 846
841 Zero (&myop, 1, LOGOP); 847 Zero (&myop, 1, UNOP);
842 myop.op_next = Nullop; 848 myop.op_next = Nullop;
843 myop.op_flags = OPf_WANT_VOID; 849 myop.op_flags = OPf_WANT_VOID;
844 850
845 PUSHMARK (SP); 851 PUSHMARK (SP);
846 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 852 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
850 SPAGAIN; 856 SPAGAIN;
851 } 857 }
852 858
853 /* 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
854 * likely was suspended in set_stacklevel, called from entersub. 860 * likely was suspended in set_stacklevel, called from entersub.
855 * 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,
856 * so we ENTER here for symmetry 862 * so we ENTER here for symmetry.
857 */ 863 */
858 ENTER; 864 ENTER;
859} 865}
860 866
861static void 867static void
1026 1032
1027 TAINT_NOT; 1033 TAINT_NOT;
1028 return 0; 1034 return 0;
1029} 1035}
1030 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
1031/* 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 */
1032/* _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 */
1033/* during execution of a perl program */ 1046/* during execution of a perl program */
1047/* also initialises PL_top_env */
1034static void NOINLINE 1048static void NOINLINE
1035cctx_prepare (pTHX_ coro_cctx *cctx) 1049cctx_prepare (pTHX_ coro_cctx *cctx)
1036{ 1050{
1037 dSP; 1051 dSP;
1038 LOGOP myop; 1052 UNOP myop;
1039 1053
1040 PL_top_env = &PL_start_env; 1054 PL_top_env = &PL_start_env;
1041 1055
1042 if (cctx->flags & CC_TRACE) 1056 if (cctx->flags & CC_TRACE)
1043 PL_runops = runops_trace; 1057 PL_runops = runops_trace;
1044 1058
1045 Zero (&myop, 1, LOGOP); 1059 Zero (&myop, 1, UNOP);
1046 myop.op_next = PL_op; 1060 myop.op_next = PL_op;
1047 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 1061 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
1048 1062
1049 PUSHMARK (SP); 1063 PUSHMARK (SP);
1050 EXTEND (SP, 2); 1064 EXTEND (SP, 2);
1051 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); 1065 PUSHs (sv_2mortal (newSViv ((IV)cctx)));
1052 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 1066 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
1053 PUTBACK; 1067 PUTBACK;
1054 PL_op = (OP *)&myop; 1068 PL_op = (OP *)&myop;
1055 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 1069 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1056 SPAGAIN; 1070 SPAGAIN;
1057} 1071}
1058 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
1059/* 1094/*
1060 * this is a _very_ stripped down perl interpreter ;) 1095 * this is a _very_ stripped down perl interpreter ;)
1061 */ 1096 */
1062static void 1097static void
1063cctx_run (void *arg) 1098cctx_run (void *arg)
1068# endif 1103# endif
1069#endif 1104#endif
1070 { 1105 {
1071 dTHX; 1106 dTHX;
1072 1107
1073 /* cctx_run is the alternative tail of transfer(), so unlock here. */ 1108 /* entersub called ENTER, but we never 'returned', undo that here */
1074 UNLOCK; 1109 LEAVE;
1075 1110
1076 /* we now skip the entersub that lead to transfer() */ 1111 /* we now skip the entersub that did lead to transfer() */
1077 PL_op = PL_op->op_next; 1112 PL_op = PL_op->op_next;
1078 1113
1079 /* inject a fake subroutine call to cctx_init */ 1114 /* inject a fake subroutine call to cctx_init */
1080 cctx_prepare (aTHX_ (coro_cctx *)arg); 1115 cctx_prepare (aTHX_ (coro_cctx *)arg);
1116
1117 /* cctx_run is the alternative tail of transfer() */
1118 transfer_tail ();
1081 1119
1082 /* 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 */
1083 PL_restartop = PL_op; 1121 PL_restartop = PL_op;
1084 perl_run (PL_curinterp); 1122 perl_run (PL_curinterp);
1085 1123
1097 1135
1098static coro_cctx * 1136static coro_cctx *
1099cctx_new () 1137cctx_new ()
1100{ 1138{
1101 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 ();
1102 void *stack_start; 1168 void *stack_start;
1103 size_t stack_size; 1169 size_t stack_size;
1104
1105 ++cctx_count;
1106 Newz (0, cctx, 1, coro_cctx);
1107
1108 cctx->gen = cctx_gen;
1109 1170
1110#if HAVE_MMAP 1171#if HAVE_MMAP
1111 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1172 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1112 /* mmap supposedly does allocate-on-write for us */ 1173 /* mmap supposedly does allocate-on-write for us */
1113 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);
1186 return cctx; 1247 return cctx;
1187 1248
1188 cctx_destroy (cctx); 1249 cctx_destroy (cctx);
1189 } 1250 }
1190 1251
1191 return cctx_new (); 1252 return cctx_new_run ();
1192} 1253}
1193 1254
1194static void 1255static void
1195cctx_put (coro_cctx *cctx) 1256cctx_put (coro_cctx *cctx)
1196{ 1257{
1246 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1307 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1247 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1308 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1248 } 1309 }
1249 else if (expect_true (prev != next)) 1310 else if (expect_true (prev != next))
1250 { 1311 {
1251 static volatile int has_throw;
1252 coro_cctx *prev__cctx; 1312 coro_cctx *prev__cctx;
1253 1313
1254 if (expect_false (prev->flags & CF_NEW)) 1314 if (expect_false (prev->flags & CF_NEW))
1255 { 1315 {
1256 /* create a new empty/source context */ 1316 /* create a new empty/source context */
1257 ++cctx_count; 1317 prev->cctx = cctx_new_empty ();
1258 New (0, prev->cctx, 1, coro_cctx);
1259 prev->cctx->sptr = 0;
1260 coro_create (&prev->cctx->cctx, 0, 0, 0, 0);
1261
1262 prev->flags &= ~CF_NEW; 1318 prev->flags &= ~CF_NEW;
1263 prev->flags |= CF_RUNNING; 1319 prev->flags |= CF_RUNNING;
1264 } 1320 }
1265 1321
1266 prev->flags &= ~CF_RUNNING; 1322 prev->flags &= ~CF_RUNNING;
1281 else 1337 else
1282 load_perl (aTHX_ next); 1338 load_perl (aTHX_ next);
1283 1339
1284 prev__cctx = prev->cctx; 1340 prev__cctx = prev->cctx;
1285 1341
1286 /* possibly "free" the cctx */ 1342 /* possibly untie and reuse the cctx */
1287 if (expect_true ( 1343 if (expect_true (
1288 prev__cctx->idle_sp == STACKLEVEL 1344 prev__cctx->idle_sp == STACKLEVEL
1289 && !(prev__cctx->flags & CC_TRACE) 1345 && !(prev__cctx->flags & CC_TRACE)
1290 && !force_cctx 1346 && !force_cctx
1291 )) 1347 ))
1307 ++next->usecount; 1363 ++next->usecount;
1308 1364
1309 if (expect_true (!next->cctx)) 1365 if (expect_true (!next->cctx))
1310 next->cctx = cctx_get (aTHX); 1366 next->cctx = cctx_get (aTHX);
1311 1367
1312 has_throw = !!next->throw; 1368 assert (("FATAL ERROR: internal error 1352 in Coro, please report", !transfer_next));//D
1369 transfer_next = next;
1313 1370
1314 if (expect_false (prev__cctx != next->cctx)) 1371 if (expect_false (prev__cctx != next->cctx))
1315 { 1372 {
1316 prev__cctx->top_env = PL_top_env; 1373 prev__cctx->top_env = PL_top_env;
1317 PL_top_env = next->cctx->top_env; 1374 PL_top_env = next->cctx->top_env;
1318 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1375 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
1319 } 1376 }
1320 1377
1321 free_coro_mortal (aTHX); 1378 transfer_tail ();
1322 UNLOCK;
1323
1324 if (expect_false (has_throw))
1325 {
1326 struct coro *coro = SvSTATE (coro_current);
1327
1328 if (coro->throw)
1329 {
1330 SV *exception = coro->throw;
1331 coro->throw = 0;
1332 sv_setsv (ERRSV, exception);
1333 croak (0);
1334 }
1335 }
1336 } 1379 }
1337} 1380}
1338
1339struct transfer_args
1340{
1341 struct coro *prev, *next;
1342};
1343 1381
1344#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))
1345#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) 1383#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1346 1384
1347/** high level stuff ********************************************************/ 1385/** high level stuff ********************************************************/
1559 /* cannot transfer to destroyed coros, skip and look for next */ 1597 /* cannot transfer to destroyed coros, skip and look for next */
1560 if (expect_false (ta->next->flags & CF_DESTROYED)) 1598 if (expect_false (ta->next->flags & CF_DESTROYED))
1561 { 1599 {
1562 UNLOCK; 1600 UNLOCK;
1563 SvREFCNT_dec (next_sv); 1601 SvREFCNT_dec (next_sv);
1564 /* coro_nready is already taken care of by destroy */ 1602 /* coro_nready has already been taken care of by destroy */
1565 continue; 1603 continue;
1566 } 1604 }
1567 1605
1568 --coro_nready; 1606 --coro_nready;
1569 UNLOCK; 1607 UNLOCK;
1654 struct coro *coro = SvSTATE (coro_sv); 1692 struct coro *coro = SvSTATE (coro_sv);
1655 1693
1656 if (flags & CC_TRACE) 1694 if (flags & CC_TRACE)
1657 { 1695 {
1658 if (!coro->cctx) 1696 if (!coro->cctx)
1659 coro->cctx = cctx_new (); 1697 coro->cctx = cctx_new_run ();
1660 else if (!(coro->cctx->flags & CC_TRACE)) 1698 else if (!(coro->cctx->flags & CC_TRACE))
1661 croak ("cannot enable tracing on coroutine with custom stack"); 1699 croak ("cannot enable tracing on coroutine with custom stack");
1662 1700
1663 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1701 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1664 } 1702 }
1866 1904
1867 PUTBACK; 1905 PUTBACK;
1868 switch (ix) 1906 switch (ix)
1869 { 1907 {
1870 case 0: 1908 case 0:
1871 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); 1909 prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (ST (0)));
1872 ta.next = 0;
1873 break; 1910 break;
1874 1911
1875 case 1: 1912 case 1:
1876 if (items != 2) 1913 if (items != 2)
1877 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);
2016 is_destroyed = CF_DESTROYED 2053 is_destroyed = CF_DESTROYED
2017 CODE: 2054 CODE:
2018 RETVAL = boolSV (coro->flags & ix); 2055 RETVAL = boolSV (coro->flags & ix);
2019 OUTPUT: 2056 OUTPUT:
2020 RETVAL 2057 RETVAL
2058
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;
2021 2065
2022void 2066void
2023api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 2067api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
2024 2068
2025SV * 2069SV *
2169 CODE: 2213 CODE:
2170 RETVAL = coro_nready; 2214 RETVAL = coro_nready;
2171 OUTPUT: 2215 OUTPUT:
2172 RETVAL 2216 RETVAL
2173 2217
2174void
2175throw (Coro::State self, SV *throw = &PL_sv_undef)
2176 PROTOTYPE: $;$
2177 CODE:
2178 SvREFCNT_dec (self->throw);
2179 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2180
2181# for async_pool speedup 2218# for async_pool speedup
2182void 2219void
2183_pool_1 (SV *cb) 2220_pool_1 (SV *cb)
2184 CODE: 2221 CODE:
2185{ 2222{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines