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.248 by root, Mon Sep 29 12:40:50 2008 UTC vs.
Revision 1.253 by root, Fri Nov 7 20:12:26 2008 UTC

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
142static perl_mutex coro_mutex; 147static perl_mutex coro_lock;
143# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 148# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0)
144# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 149# define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0)
145#else 150#else
146# define LOCK (void)0 151# define LOCK (void)0
147# define UNLOCK (void)0 152# define UNLOCK (void)0
148#endif 153#endif
149 154
150/* helper storage struct for Coro::AIO */ 155/* helper storage struct for Coro::AIO */
151struct io_state 156struct io_state
152{ 157{
158 AV *res;
153 int errorno; 159 int errorno;
154 I32 laststype; 160 I32 laststype;
155 int laststatval; 161 int laststatval;
156 Stat_t statcache; 162 Stat_t statcache;
157}; 163};
161static size_t coro_stacksize = CORO_STACKSIZE; 167static size_t coro_stacksize = CORO_STACKSIZE;
162static struct CoroAPI coroapi; 168static struct CoroAPI coroapi;
163static AV *main_mainstack; /* used to differentiate between $main and others */ 169static AV *main_mainstack; /* used to differentiate between $main and others */
164static JMPENV *main_top_env; 170static JMPENV *main_top_env;
165static HV *coro_state_stash, *coro_stash; 171static HV *coro_state_stash, *coro_stash;
166static volatile SV *coro_mortal; /* will be freed after next transfer */ 172static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
167 173
168static GV *irsgv; /* $/ */ 174static GV *irsgv; /* $/ */
169static GV *stdoutgv; /* *STDOUT */ 175static GV *stdoutgv; /* *STDOUT */
170static SV *rv_diehook; 176static SV *rv_diehook;
171static SV *rv_warnhook; 177static SV *rv_warnhook;
324 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 330 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
325#endif 331#endif
326 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 332 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
327 --AvFILLp (padlist); 333 --AvFILLp (padlist);
328 334
329 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 335 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE)));
330 av_store (newpadlist, 1, (SV *)newpad); 336 av_store (newpadlist, 1, (SV *)newpad);
331 337
332 return newpadlist; 338 return newpadlist;
333} 339}
334 340
377static MGVTBL coro_cv_vtbl = { 383static MGVTBL coro_cv_vtbl = {
378 0, 0, 0, 0, 384 0, 0, 0, 0,
379 coro_cv_free 385 coro_cv_free
380}; 386};
381 387
382#define CORO_MAGIC(sv,type) \ 388#define CORO_MAGIC(sv, type) \
383 SvMAGIC (sv) \ 389 SvMAGIC (sv) \
384 ? SvMAGIC (sv)->mg_type == type \ 390 ? SvMAGIC (sv)->mg_type == type \
385 ? SvMAGIC (sv) \ 391 ? SvMAGIC (sv) \
386 : mg_find (sv, type) \ 392 : mg_find (sv, type) \
387 : 0 393 : 0
388 394
389#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 395#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
390#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) 396#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
391 397
392static struct coro * 398static struct coro *
622 628
623/* 629/*
624 * destroy the stacks, the callchain etc... 630 * destroy the stacks, the callchain etc...
625 */ 631 */
626static void 632static void
627coro_destroy_stacks (pTHX) 633coro_destruct_stacks (pTHX)
628{ 634{
629 while (PL_curstackinfo->si_next) 635 while (PL_curstackinfo->si_next)
630 PL_curstackinfo = PL_curstackinfo->si_next; 636 PL_curstackinfo = PL_curstackinfo->si_next;
631 637
632 while (PL_curstackinfo) 638 while (PL_curstackinfo)
810 GvSV (PL_defgv) = newSV (0); 816 GvSV (PL_defgv) = newSV (0);
811 GvAV (PL_defgv) = coro->args; coro->args = 0; 817 GvAV (PL_defgv) = coro->args; coro->args = 0;
812 GvSV (PL_errgv) = newSV (0); 818 GvSV (PL_errgv) = newSV (0);
813 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 819 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
814 PL_rs = newSVsv (GvSV (irsgv)); 820 PL_rs = newSVsv (GvSV (irsgv));
815 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); 821 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
816 822
817 { 823 {
818 dSP; 824 dSP;
819 LOGOP myop; 825 LOGOP myop;
820 826
837 */ 843 */
838 ENTER; 844 ENTER;
839} 845}
840 846
841static void 847static void
842coro_destroy (pTHX_ struct coro *coro) 848coro_destruct (pTHX_ struct coro *coro)
843{ 849{
844 if (!IN_DESTRUCT) 850 if (!IN_DESTRUCT)
845 { 851 {
846 /* restore all saved variables and stuff */ 852 /* restore all saved variables and stuff */
847 LEAVE_SCOPE (0); 853 LEAVE_SCOPE (0);
869 SvREFCNT_dec (PL_warnhook); 875 SvREFCNT_dec (PL_warnhook);
870 876
871 SvREFCNT_dec (coro->saved_deffh); 877 SvREFCNT_dec (coro->saved_deffh);
872 SvREFCNT_dec (coro->throw); 878 SvREFCNT_dec (coro->throw);
873 879
874 coro_destroy_stacks (aTHX); 880 coro_destruct_stacks (aTHX);
875} 881}
876 882
877static void 883static void
878free_coro_mortal (pTHX) 884free_coro_mortal (pTHX)
879{ 885{
916 : cx->blk_gimme == G_SCALAR ? bot + 1 922 : cx->blk_gimme == G_SCALAR ? bot + 1
917 : bot; 923 : bot;
918 924
919 av_extend (av, top - bot); 925 av_extend (av, top - bot);
920 while (bot < top) 926 while (bot < top)
921 av_push (av, SvREFCNT_inc (*bot++)); 927 av_push (av, SvREFCNT_inc_NN (*bot++));
922 928
923 PL_runops = RUNOPS_DEFAULT; 929 PL_runops = RUNOPS_DEFAULT;
924 ENTER; 930 ENTER;
925 SAVETMPS; 931 SAVETMPS;
926 EXTEND (SP, 3); 932 EXTEND (SP, 3);
1074 coro_cctx *cctx; 1080 coro_cctx *cctx;
1075 void *stack_start; 1081 void *stack_start;
1076 size_t stack_size; 1082 size_t stack_size;
1077 1083
1078 ++cctx_count; 1084 ++cctx_count;
1079
1080 Newz (0, cctx, 1, coro_cctx); 1085 Newz (0, cctx, 1, coro_cctx);
1081 1086
1082#if HAVE_MMAP 1087#if HAVE_MMAP
1083 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1088 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1084 /* mmap supposedly does allocate-on-write for us */ 1089 /* mmap supposedly does allocate-on-write for us */
1120{ 1125{
1121 if (!cctx) 1126 if (!cctx)
1122 return; 1127 return;
1123 1128
1124 --cctx_count; 1129 --cctx_count;
1130 coro_destroy (&cctx->cctx);
1125 1131
1132 /* coro_transfer creates new, empty cctx's */
1133 if (cctx->sptr)
1134 {
1126#if CORO_USE_VALGRIND 1135#if CORO_USE_VALGRIND
1127 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1136 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1128#endif 1137#endif
1129 1138
1130#if HAVE_MMAP 1139#if HAVE_MMAP
1131 if (cctx->flags & CC_MAPPED) 1140 if (cctx->flags & CC_MAPPED)
1132 munmap (cctx->sptr, cctx->ssize); 1141 munmap (cctx->sptr, cctx->ssize);
1133 else 1142 else
1134#endif 1143#endif
1135 Safefree (cctx->sptr); 1144 Safefree (cctx->sptr);
1145 }
1136 1146
1137 Safefree (cctx); 1147 Safefree (cctx);
1138} 1148}
1139 1149
1140/* wether this cctx should be destructed */ 1150/* wether this cctx should be destructed */
1159} 1169}
1160 1170
1161static void 1171static void
1162cctx_put (coro_cctx *cctx) 1172cctx_put (coro_cctx *cctx)
1163{ 1173{
1174 assert (("cctx_put called on non-initialised cctx", cctx->sptr));
1175
1164 /* free another cctx if overlimit */ 1176 /* free another cctx if overlimit */
1165 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1177 if (expect_false (cctx_idle >= MAX_IDLE_CCTX))
1166 { 1178 {
1167 coro_cctx *first = cctx_first; 1179 coro_cctx *first = cctx_first;
1168 cctx_first = first->next; 1180 cctx_first = first->next;
1202/* always use the TRANSFER macro */ 1214/* always use the TRANSFER macro */
1203static void NOINLINE 1215static void NOINLINE
1204transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1216transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1205{ 1217{
1206 dSTACKLEVEL; 1218 dSTACKLEVEL;
1207 static volatile int has_throw;
1208 1219
1209 /* sometimes transfer is only called to set idle_sp */ 1220 /* sometimes transfer is only called to set idle_sp */
1210 if (expect_false (!next)) 1221 if (expect_false (!next))
1211 { 1222 {
1212 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1223 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1213 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1224 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1214 } 1225 }
1215 else if (expect_true (prev != next)) 1226 else if (expect_true (prev != next))
1216 { 1227 {
1228 static volatile int has_throw;
1217 coro_cctx *prev__cctx; 1229 coro_cctx *prev__cctx;
1218 1230
1219 if (expect_false (prev->flags & CF_NEW)) 1231 if (expect_false (prev->flags & CF_NEW))
1220 { 1232 {
1221 /* create a new empty context */ 1233 /* create a new empty/source context */
1234 ++cctx_count;
1222 Newz (0, prev->cctx, 1, coro_cctx); 1235 New (0, prev->cctx, 1, coro_cctx);
1236 prev->cctx->sptr = 0;
1237 coro_create (&prev->cctx->cctx, 0, 0, 0, 0);
1238
1223 prev->flags &= ~CF_NEW; 1239 prev->flags &= ~CF_NEW;
1224 prev->flags |= CF_RUNNING; 1240 prev->flags |= CF_RUNNING;
1225 } 1241 }
1226 1242
1227 prev->flags &= ~CF_RUNNING; 1243 prev->flags &= ~CF_RUNNING;
1334 croak ("FATAL: tried to destroy currently running coroutine"); 1350 croak ("FATAL: tried to destroy currently running coroutine");
1335 1351
1336 save_perl (aTHX_ &temp); 1352 save_perl (aTHX_ &temp);
1337 load_perl (aTHX_ coro); 1353 load_perl (aTHX_ coro);
1338 1354
1339 coro_destroy (aTHX_ coro); 1355 coro_destruct (aTHX_ coro);
1340 1356
1341 load_perl (aTHX_ &temp); 1357 load_perl (aTHX_ &temp);
1342 1358
1343 coro->slot = 0; 1359 coro->slot = 0;
1344 } 1360 }
1450 LOCK; 1466 LOCK;
1451 1467
1452 sv_hook = coro_nready ? 0 : coro_readyhook; 1468 sv_hook = coro_nready ? 0 : coro_readyhook;
1453 xs_hook = coro_nready ? 0 : coroapi.readyhook; 1469 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1454 1470
1455 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1471 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1456 ++coro_nready; 1472 ++coro_nready;
1457 1473
1458 UNLOCK; 1474 UNLOCK;
1459 1475
1460 if (sv_hook) 1476 if (sv_hook)
1731PROTOTYPES: DISABLE 1747PROTOTYPES: DISABLE
1732 1748
1733BOOT: 1749BOOT:
1734{ 1750{
1735#ifdef USE_ITHREADS 1751#ifdef USE_ITHREADS
1736 MUTEX_INIT (&coro_mutex); 1752 MUTEX_INIT (&coro_lock);
1737#endif 1753#endif
1738 BOOT_PAGESIZE; 1754 BOOT_PAGESIZE;
1739 1755
1740 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1756 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1741 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1757 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1828 ta.next = 0; 1844 ta.next = 0;
1829 break; 1845 break;
1830 1846
1831 case 1: 1847 case 1:
1832 if (items != 2) 1848 if (items != 2)
1833 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); 1849 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items);
1834 1850
1835 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 1851 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1836 break; 1852 break;
1837 1853
1838 case 2: 1854 case 2:
2001 CODE: 2017 CODE:
2002 struct coro *coro = SvSTATE (coro_current); 2018 struct coro *coro = SvSTATE (coro_current);
2003 coro->cctx->idle_sp = 0; 2019 coro->cctx->idle_sp = 0;
2004 2020
2005void 2021void
2006throw (Coro::State self, SV *throw = &PL_sv_undef)
2007 PROTOTYPE: $;$
2008 CODE:
2009 SvREFCNT_dec (self->throw);
2010 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2011
2012void
2013swap_defsv (Coro::State self) 2022swap_defsv (Coro::State self)
2014 PROTOTYPE: $ 2023 PROTOTYPE: $
2015 ALIAS: 2024 ALIAS:
2016 swap_defav = 1 2025 swap_defav = 1
2017 CODE: 2026 CODE:
2071void 2080void
2072_set_current (SV *current) 2081_set_current (SV *current)
2073 PROTOTYPE: $ 2082 PROTOTYPE: $
2074 CODE: 2083 CODE:
2075 SvREFCNT_dec (SvRV (coro_current)); 2084 SvREFCNT_dec (SvRV (coro_current));
2076 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); 2085 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
2077 2086
2078void 2087void
2079_set_readyhook (SV *hook) 2088_set_readyhook (SV *hook)
2080 PROTOTYPE: $ 2089 PROTOTYPE: $
2081 CODE: 2090 CODE:
2120 CODE: 2129 CODE:
2121 RETVAL = coro_nready; 2130 RETVAL = coro_nready;
2122 OUTPUT: 2131 OUTPUT:
2123 RETVAL 2132 RETVAL
2124 2133
2134void
2135throw (Coro::State self, SV *throw = &PL_sv_undef)
2136 PROTOTYPE: $;$
2137 CODE:
2138 SvREFCNT_dec (self->throw);
2139 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2140
2125# for async_pool speedup 2141# for async_pool speedup
2126void 2142void
2127_pool_1 (SV *cb) 2143_pool_1 (SV *cb)
2128 CODE: 2144 CODE:
2129{ 2145{
2141 SvREFCNT_dec (old); 2157 SvREFCNT_dec (old);
2142 croak ("\3async_pool terminate\2\n"); 2158 croak ("\3async_pool terminate\2\n");
2143 } 2159 }
2144 2160
2145 SvREFCNT_dec (coro->saved_deffh); 2161 SvREFCNT_dec (coro->saved_deffh);
2146 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2162 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2147 2163
2148 hv_store (hv, "desc", sizeof ("desc") - 1, 2164 hv_store (hv, "desc", sizeof ("desc") - 1,
2149 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 2165 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2150 2166
2151 invoke_av = (AV *)SvRV (invoke); 2167 invoke_av = (AV *)SvRV (invoke);
2155 2171
2156 if (len > 0) 2172 if (len > 0)
2157 { 2173 {
2158 av_fill (defav, len - 1); 2174 av_fill (defav, len - 1);
2159 for (i = 0; i < len; ++i) 2175 for (i = 0; i < len; ++i)
2160 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2176 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
2161 } 2177 }
2162 2178
2163 SvREFCNT_dec (invoke); 2179 SvREFCNT_dec (invoke);
2164} 2180}
2165 2181
2244#endif 2260#endif
2245 2261
2246 2262
2247MODULE = Coro::State PACKAGE = Coro::AIO 2263MODULE = Coro::State PACKAGE = Coro::AIO
2248 2264
2249SV * 2265void
2250_get_state () 2266_get_state (SV *self)
2251 CODE: 2267 PPCODE:
2252{ 2268{
2253 struct io_state *data; 2269 AV *defav = GvAV (PL_defgv);
2254 2270 AV *av = newAV ();
2271 int i;
2255 RETVAL = newSV (sizeof (struct io_state)); 2272 SV *data_sv = newSV (sizeof (struct io_state));
2256 data = (struct io_state *)SvPVX (RETVAL); 2273 struct io_state *data = (struct io_state *)SvPVX (data_sv);
2257 SvCUR_set (RETVAL, sizeof (struct io_state)); 2274 SvCUR_set (data_sv, sizeof (struct io_state));
2258 SvPOK_only (RETVAL); 2275 SvPOK_only (data_sv);
2259 2276
2260 data->errorno = errno; 2277 data->errorno = errno;
2261 data->laststype = PL_laststype; 2278 data->laststype = PL_laststype;
2262 data->laststatval = PL_laststatval; 2279 data->laststatval = PL_laststatval;
2263 data->statcache = PL_statcache; 2280 data->statcache = PL_statcache;
2281
2282 av_extend (av, AvFILLp (defav) + 1 + 1);
2283
2284 for (i = 0; i <= AvFILLp (defav); ++i)
2285 av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i]));
2286
2287 av_push (av, data_sv);
2288
2289 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2290
2291 api_ready (self);
2264} 2292}
2265 OUTPUT:
2266 RETVAL
2267 2293
2268void 2294void
2269_set_state (char *data_) 2295_set_state (SV *state)
2270 PROTOTYPE: $ 2296 PROTOTYPE: $
2271 CODE: 2297 PPCODE:
2272{ 2298{
2273 struct io_state *data = (void *)data_; 2299 AV *av = (AV *)SvRV (state);
2300 struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]);
2301 int i;
2274 2302
2275 errno = data->errorno; 2303 errno = data->errorno;
2276 PL_laststype = data->laststype; 2304 PL_laststype = data->laststype;
2277 PL_laststatval = data->laststatval; 2305 PL_laststatval = data->laststatval;
2278 PL_statcache = data->statcache; 2306 PL_statcache = data->statcache;
2307
2308 EXTEND (SP, AvFILLp (av));
2309 for (i = 0; i < AvFILLp (av); ++i)
2310 PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i])));
2279} 2311}
2280 2312
2281 2313
2282MODULE = Coro::State PACKAGE = Coro::AnyEvent 2314MODULE = Coro::State PACKAGE = Coro::AnyEvent
2283 2315

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines