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.254 by root, Fri Nov 7 20:27:47 2008 UTC vs.
Revision 1.259 by root, Mon Nov 10 00:02:29 2008 UTC

142#define NOINLINE attribute ((noinline)) 142#define NOINLINE attribute ((noinline))
143 143
144#include "CoroAPI.h" 144#include "CoroAPI.h"
145 145
146#ifdef USE_ITHREADS 146#ifdef USE_ITHREADS
147
147static perl_mutex coro_lock; 148static perl_mutex coro_lock;
148# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0) 149# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0)
149# define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0) 150# define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0)
151# if CORO_PTHREAD
152static void *coro_thx;
153# endif
154
150#else 155#else
156
151# define LOCK (void)0 157# define LOCK (void)0
152# define UNLOCK (void)0 158# define UNLOCK (void)0
159
153#endif 160#endif
161
162# undef LOCK
163# define LOCK (void)0
164# undef UNLOCK
165# define UNLOCK (void)0
154 166
155/* helper storage struct for Coro::AIO */ 167/* helper storage struct for Coro::AIO */
156struct io_state 168struct io_state
157{ 169{
158 AV *res; 170 AV *res;
159 int errorno; 171 int errorno;
160 I32 laststype; 172 I32 laststype; /* U16 in 5.10.0 */
161 int laststatval; 173 int laststatval;
162 Stat_t statcache; 174 Stat_t statcache;
163}; 175};
164 176
165static double (*nvtime)(); /* so why doesn't it take void? */ 177static double (*nvtime)(); /* so why doesn't it take void? */
169static struct CoroAPI coroapi; 181static struct CoroAPI coroapi;
170static AV *main_mainstack; /* used to differentiate between $main and others */ 182static AV *main_mainstack; /* used to differentiate between $main and others */
171static JMPENV *main_top_env; 183static JMPENV *main_top_env;
172static HV *coro_state_stash, *coro_stash; 184static HV *coro_state_stash, *coro_stash;
173static 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 char next_has_throw; /* speedup flag for next->throw check */
174 187
175static GV *irsgv; /* $/ */ 188static GV *irsgv; /* $/ */
176static GV *stdoutgv; /* *STDOUT */ 189static GV *stdoutgv; /* *STDOUT */
177static SV *rv_diehook; 190static SV *rv_diehook;
178static SV *rv_warnhook; 191static SV *rv_warnhook;
280#define PRIO_MIN -4 293#define PRIO_MIN -4
281 294
282/* for Coro.pm */ 295/* for Coro.pm */
283static SV *coro_current; 296static SV *coro_current;
284static SV *coro_readyhook; 297static SV *coro_readyhook;
285static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 298static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
286static int coro_nready; 299static int coro_nready;
287static struct coro *coro_first; 300static struct coro *coro_first;
288 301
289/** lowlevel stuff **********************************************************/ 302/** lowlevel stuff **********************************************************/
290 303
1017} 1030}
1018 1031
1019/* inject a fake call to Coro::State::_cctx_init into the execution */ 1032/* 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 */ 1033/* _cctx_init should be careful, as it could be called at almost any time */
1021/* during execution of a perl program */ 1034/* during execution of a perl program */
1035/* also initialises PL_top_env */
1022static void NOINLINE 1036static void NOINLINE
1023cctx_prepare (pTHX_ coro_cctx *cctx) 1037cctx_prepare (pTHX_ coro_cctx *cctx)
1024{ 1038{
1025 dSP; 1039 dSP;
1026 LOGOP myop; 1040 LOGOP myop;
1042 PL_op = (OP *)&myop; 1056 PL_op = (OP *)&myop;
1043 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 1057 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1044 SPAGAIN; 1058 SPAGAIN;
1045} 1059}
1046 1060
1061/* the tail of transfer: execute stuff we can onyl do afetr a transfer */
1062static void
1063transfer_tail (void)
1064{
1065 UNLOCK;
1066
1067 if (expect_false (next_has_throw))
1068 {
1069 struct coro *coro = SvSTATE (coro_current);
1070
1071 if (coro->throw)
1072 {
1073 SV *exception = coro->throw;
1074 coro->throw = 0;
1075 sv_setsv (ERRSV, exception);
1076 croak (0);
1077 }
1078 }
1079}
1080
1047/* 1081/*
1048 * this is a _very_ stripped down perl interpreter ;) 1082 * this is a _very_ stripped down perl interpreter ;)
1049 */ 1083 */
1050static void 1084static void
1051cctx_run (void *arg) 1085cctx_run (void *arg)
1052{ 1086{
1087#ifdef USE_ITHREADS
1088# if CORO_PTHREAD
1089 PERL_SET_CONTEXT (coro_thx);
1090# endif
1091#endif
1092 {
1053 dTHX; 1093 dTHX;
1054 1094
1055 /* cctx_run is the alternative tail of transfer(), so unlock here. */
1056 UNLOCK;
1057
1058 /* we now skip the entersub that lead to transfer() */ 1095 /* we now skip the entersub that lead to transfer () */
1059 PL_op = PL_op->op_next; 1096 PL_op = PL_op->op_next;
1060 1097
1061 /* inject a fake subroutine call to cctx_init */ 1098 /* inject a fake subroutine call to cctx_init */
1062 cctx_prepare (aTHX_ (coro_cctx *)arg); 1099 cctx_prepare (aTHX_ (coro_cctx *)arg);
1063 1100
1101 /* cctx_run is the alternative tail of transfer () */
1102 transfer_tail ();
1103
1064 /* somebody or something will hit me for both perl_run and PL_restartop */ 1104 /* somebody or something will hit me for both perl_run and PL_restartop */
1065 PL_restartop = PL_op; 1105 PL_restartop = PL_op;
1066 perl_run (PL_curinterp); 1106 perl_run (PL_curinterp);
1067 1107
1068 /* 1108 /*
1069 * If perl-run returns we assume exit() was being called or the coro 1109 * 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) 1110 * 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 1111 * 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" 1112 * bootstrap-time "top" top_env, as we cannot restore the "main"
1073 * coroutine as Coro has no such concept 1113 * coroutine as Coro has no such concept
1074 */ 1114 */
1075 PL_top_env = main_top_env; 1115 PL_top_env = main_top_env;
1076 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1116 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1117 }
1077} 1118}
1078 1119
1079static coro_cctx * 1120static coro_cctx *
1080cctx_new () 1121cctx_new ()
1081{ 1122{
1082 coro_cctx *cctx; 1123 coro_cctx *cctx;
1124
1125 ++cctx_count;
1126 New (0, cctx, 1, coro_cctx);
1127
1128 cctx->gen = cctx_gen;
1129 cctx->flags = 0;
1130 cctx->idle_sp = 0; /* can be accessed by transfer between cctx_run and set_stacklevel */
1131
1132 return cctx;
1133}
1134
1135/* create a new cctx only suitable as source */
1136static coro_cctx *
1137cctx_new_empty ()
1138{
1139 coro_cctx *cctx = cctx_new ();
1140
1141 cctx->sptr = 0;
1142 coro_create (&cctx->cctx, 0, 0, 0, 0);
1143
1144 return cctx;
1145}
1146
1147/* create a new cctx suitable as destination/running a perl interpreter */
1148static coro_cctx *
1149cctx_new_run ()
1150{
1151 coro_cctx *cctx = cctx_new ();
1083 void *stack_start; 1152 void *stack_start;
1084 size_t stack_size; 1153 size_t stack_size;
1085
1086 ++cctx_count;
1087 Newz (0, cctx, 1, coro_cctx);
1088
1089 cctx->gen = cctx_gen;
1090 1154
1091#if HAVE_MMAP 1155#if HAVE_MMAP
1092 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1156 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1093 /* mmap supposedly does allocate-on-write for us */ 1157 /* 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); 1158 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
1167 return cctx; 1231 return cctx;
1168 1232
1169 cctx_destroy (cctx); 1233 cctx_destroy (cctx);
1170 } 1234 }
1171 1235
1172 return cctx_new (); 1236 return cctx_new_run ();
1173} 1237}
1174 1238
1175static void 1239static void
1176cctx_put (coro_cctx *cctx) 1240cctx_put (coro_cctx *cctx)
1177{ 1241{
1227 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1291 ((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 */ 1292 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1229 } 1293 }
1230 else if (expect_true (prev != next)) 1294 else if (expect_true (prev != next))
1231 { 1295 {
1232 static volatile int has_throw;
1233 coro_cctx *prev__cctx; 1296 coro_cctx *prev__cctx;
1234 1297
1235 if (expect_false (prev->flags & CF_NEW)) 1298 if (expect_false (prev->flags & CF_NEW))
1236 { 1299 {
1237 /* create a new empty/source context */ 1300 /* create a new empty/source context */
1238 ++cctx_count; 1301 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; 1302 prev->flags &= ~CF_NEW;
1244 prev->flags |= CF_RUNNING; 1303 prev->flags |= CF_RUNNING;
1245 } 1304 }
1246 1305
1247 prev->flags &= ~CF_RUNNING; 1306 prev->flags &= ~CF_RUNNING;
1262 else 1321 else
1263 load_perl (aTHX_ next); 1322 load_perl (aTHX_ next);
1264 1323
1265 prev__cctx = prev->cctx; 1324 prev__cctx = prev->cctx;
1266 1325
1326 if (prev__cctx->idle_sp == STACKLEVEL) asm volatile("");//D
1327
1267 /* possibly "free" the cctx */ 1328 /* possibly "free" the cctx */
1268 if (expect_true ( 1329 if (expect_true (
1269 prev__cctx->idle_sp == STACKLEVEL 1330 prev__cctx->idle_sp == STACKLEVEL
1270 && !(prev__cctx->flags & CC_TRACE) 1331 && !(prev__cctx->flags & CC_TRACE)
1271 && !force_cctx 1332 && !force_cctx
1288 ++next->usecount; 1349 ++next->usecount;
1289 1350
1290 if (expect_true (!next->cctx)) 1351 if (expect_true (!next->cctx))
1291 next->cctx = cctx_get (aTHX); 1352 next->cctx = cctx_get (aTHX);
1292 1353
1293 has_throw = !!next->throw; 1354 next_has_throw = !!next->throw;
1294 1355
1295 if (expect_false (prev__cctx != next->cctx)) 1356 if (expect_false (prev__cctx != next->cctx))
1296 { 1357 {
1297 prev__cctx->top_env = PL_top_env; 1358 prev__cctx->top_env = PL_top_env;
1298 PL_top_env = next->cctx->top_env; 1359 PL_top_env = next->cctx->top_env;
1300 } 1361 }
1301 1362
1302 free_coro_mortal (aTHX); 1363 free_coro_mortal (aTHX);
1303 UNLOCK; 1364 UNLOCK;
1304 1365
1305 if (expect_false (has_throw)) 1366 transfer_tail ();
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 } 1367 }
1318} 1368}
1319 1369
1320struct transfer_args 1370struct transfer_args
1321{ 1371{
1635 struct coro *coro = SvSTATE (coro_sv); 1685 struct coro *coro = SvSTATE (coro_sv);
1636 1686
1637 if (flags & CC_TRACE) 1687 if (flags & CC_TRACE)
1638 { 1688 {
1639 if (!coro->cctx) 1689 if (!coro->cctx)
1640 coro->cctx = cctx_new (); 1690 coro->cctx = cctx_new_run ();
1641 else if (!(coro->cctx->flags & CC_TRACE)) 1691 else if (!(coro->cctx->flags & CC_TRACE))
1642 croak ("cannot enable tracing on coroutine with custom stack"); 1692 croak ("cannot enable tracing on coroutine with custom stack");
1643 1693
1644 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1694 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1645 } 1695 }
1652 else 1702 else
1653 coro->slot->runops = RUNOPS_DEFAULT; 1703 coro->slot->runops = RUNOPS_DEFAULT;
1654 } 1704 }
1655} 1705}
1656 1706
1707#if 0
1657static int 1708static int
1658coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) 1709coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1659{ 1710{
1660 AV *padlist; 1711 AV *padlist;
1661 AV *av = (AV *)mg->mg_obj; 1712 AV *av = (AV *)mg->mg_obj;
1667 1718
1668static MGVTBL coro_gensub_vtbl = { 1719static MGVTBL coro_gensub_vtbl = {
1669 0, 0, 0, 0, 1720 0, 0, 0, 0,
1670 coro_gensub_free 1721 coro_gensub_free
1671}; 1722};
1723#endif
1672 1724
1673/*****************************************************************************/ 1725/*****************************************************************************/
1674/* PerlIO::cede */ 1726/* PerlIO::cede */
1675 1727
1676typedef struct 1728typedef struct
1752 1804
1753BOOT: 1805BOOT:
1754{ 1806{
1755#ifdef USE_ITHREADS 1807#ifdef USE_ITHREADS
1756 MUTEX_INIT (&coro_lock); 1808 MUTEX_INIT (&coro_lock);
1809# if CORO_PTHREAD
1810 coro_thx = PERL_GET_CONTEXT;
1811# endif
1757#endif 1812#endif
1758 BOOT_PAGESIZE; 1813 BOOT_PAGESIZE;
1759 1814
1760 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1815 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1761 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1816 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2012 CODE: 2067 CODE:
2013 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; 2068 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
2014 OUTPUT: 2069 OUTPUT:
2015 RETVAL 2070 RETVAL
2016 2071
2017IV 2072UV
2018rss (Coro::State coro) 2073rss (Coro::State coro)
2019 PROTOTYPE: $ 2074 PROTOTYPE: $
2020 ALIAS: 2075 ALIAS:
2021 usecount = 1 2076 usecount = 1
2022 CODE: 2077 CODE:
2204 sv_setsv (cb, &PL_sv_undef); 2259 sv_setsv (cb, &PL_sv_undef);
2205 2260
2206 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2261 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2207 coro->saved_deffh = 0; 2262 coro->saved_deffh = 0;
2208 2263
2209 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2264 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2210 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2265 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2211 { 2266 {
2212 SV *old = PL_diehook; 2267 SV *old = PL_diehook;
2213 PL_diehook = 0; 2268 PL_diehook = 0;
2214 SvREFCNT_dec (old); 2269 SvREFCNT_dec (old);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines