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.249 by root, Tue Sep 30 17:12:35 2008 UTC vs.
Revision 1.255 by root, Sat Nov 8 04:52:01 2008 UTC

52#else 52#else
53# define REGISTER_STACK(cctx,start,end) 53# define REGISTER_STACK(cctx,start,end)
54#endif 54#endif
55 55
56/* the maximum number of idle cctx that will be pooled */ 56/* the maximum number of idle cctx that will be pooled */
57#define MAX_IDLE_CCTX 8 57static int cctx_max_idle = 4;
58 58
59#define PERL_VERSION_ATLEAST(a,b,c) \ 59#define PERL_VERSION_ATLEAST(a,b,c) \
60 (PERL_REVISION > (a) \ 60 (PERL_REVISION > (a) \
61 || (PERL_REVISION == (a) \ 61 || (PERL_REVISION == (a) \
62 && (PERL_VERSION > (b) \ 62 && (PERL_VERSION > (b) \
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{
153 AV *res; 158 AV *res;
154 int errorno; 159 int errorno;
155 I32 laststype; 160 I32 laststype; /* U16 in 5.10.0 */
156 int laststatval; 161 int laststatval;
157 Stat_t statcache; 162 Stat_t statcache;
158}; 163};
159 164
160static double (*nvtime)(); /* so why doesn't it take void? */ 165static double (*nvtime)(); /* so why doesn't it take void? */
161 166
167static U32 cctx_gen;
162static size_t coro_stacksize = CORO_STACKSIZE; 168static size_t cctx_stacksize = CORO_STACKSIZE;
163static struct CoroAPI coroapi; 169static struct CoroAPI coroapi;
164static AV *main_mainstack; /* used to differentiate between $main and others */ 170static AV *main_mainstack; /* used to differentiate between $main and others */
165static JMPENV *main_top_env; 171static JMPENV *main_top_env;
166static HV *coro_state_stash, *coro_stash; 172static HV *coro_state_stash, *coro_stash;
167static volatile SV *coro_mortal; /* will be freed after next transfer */ 173static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
168 174
169static GV *irsgv; /* $/ */ 175static GV *irsgv; /* $/ */
170static GV *stdoutgv; /* *STDOUT */ 176static GV *stdoutgv; /* *STDOUT */
171static SV *rv_diehook; 177static SV *rv_diehook;
172static SV *rv_warnhook; 178static SV *rv_warnhook;
204 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 210 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
205 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 211 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
206 JMPENV *top_env; 212 JMPENV *top_env;
207 coro_context cctx; 213 coro_context cctx;
208 214
215 U32 gen;
209#if CORO_USE_VALGRIND 216#if CORO_USE_VALGRIND
210 int valgrind_id; 217 int valgrind_id;
211#endif 218#endif
212 unsigned char flags; 219 unsigned char flags;
213} coro_cctx; 220} coro_cctx;
378static MGVTBL coro_cv_vtbl = { 385static MGVTBL coro_cv_vtbl = {
379 0, 0, 0, 0, 386 0, 0, 0, 0,
380 coro_cv_free 387 coro_cv_free
381}; 388};
382 389
383#define CORO_MAGIC(sv,type) \ 390#define CORO_MAGIC(sv, type) \
384 SvMAGIC (sv) \ 391 SvMAGIC (sv) \
385 ? SvMAGIC (sv)->mg_type == type \ 392 ? SvMAGIC (sv)->mg_type == type \
386 ? SvMAGIC (sv) \ 393 ? SvMAGIC (sv) \
387 : mg_find (sv, type) \ 394 : mg_find (sv, type) \
388 : 0 395 : 0
389 396
390#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 397#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
391#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) 398#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
392 399
393static struct coro * 400static struct coro *
623 630
624/* 631/*
625 * destroy the stacks, the callchain etc... 632 * destroy the stacks, the callchain etc...
626 */ 633 */
627static void 634static void
628coro_destroy_stacks (pTHX) 635coro_destruct_stacks (pTHX)
629{ 636{
630 while (PL_curstackinfo->si_next) 637 while (PL_curstackinfo->si_next)
631 PL_curstackinfo = PL_curstackinfo->si_next; 638 PL_curstackinfo = PL_curstackinfo->si_next;
632 639
633 while (PL_curstackinfo) 640 while (PL_curstackinfo)
838 */ 845 */
839 ENTER; 846 ENTER;
840} 847}
841 848
842static void 849static void
843coro_destroy (pTHX_ struct coro *coro) 850coro_destruct (pTHX_ struct coro *coro)
844{ 851{
845 if (!IN_DESTRUCT) 852 if (!IN_DESTRUCT)
846 { 853 {
847 /* restore all saved variables and stuff */ 854 /* restore all saved variables and stuff */
848 LEAVE_SCOPE (0); 855 LEAVE_SCOPE (0);
870 SvREFCNT_dec (PL_warnhook); 877 SvREFCNT_dec (PL_warnhook);
871 878
872 SvREFCNT_dec (coro->saved_deffh); 879 SvREFCNT_dec (coro->saved_deffh);
873 SvREFCNT_dec (coro->throw); 880 SvREFCNT_dec (coro->throw);
874 881
875 coro_destroy_stacks (aTHX); 882 coro_destruct_stacks (aTHX);
876} 883}
877 884
878static void 885static void
879free_coro_mortal (pTHX) 886free_coro_mortal (pTHX)
880{ 887{
1075 coro_cctx *cctx; 1082 coro_cctx *cctx;
1076 void *stack_start; 1083 void *stack_start;
1077 size_t stack_size; 1084 size_t stack_size;
1078 1085
1079 ++cctx_count; 1086 ++cctx_count;
1080
1081 Newz (0, cctx, 1, coro_cctx); 1087 Newz (0, cctx, 1, coro_cctx);
1082 1088
1089 cctx->gen = cctx_gen;
1090
1083#if HAVE_MMAP 1091#if HAVE_MMAP
1084 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1092 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1085 /* mmap supposedly does allocate-on-write for us */ 1093 /* mmap supposedly does allocate-on-write for us */
1086 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1094 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
1087 1095
1088 if (cctx->sptr != (void *)-1) 1096 if (cctx->sptr != (void *)-1)
1089 { 1097 {
1095 cctx->flags |= CC_MAPPED; 1103 cctx->flags |= CC_MAPPED;
1096 } 1104 }
1097 else 1105 else
1098#endif 1106#endif
1099 { 1107 {
1100 cctx->ssize = coro_stacksize * (long)sizeof (long); 1108 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1101 New (0, cctx->sptr, coro_stacksize, long); 1109 New (0, cctx->sptr, cctx_stacksize, long);
1102 1110
1103 if (!cctx->sptr) 1111 if (!cctx->sptr)
1104 { 1112 {
1105 perror ("FATAL: unable to allocate stack for coroutine"); 1113 perror ("FATAL: unable to allocate stack for coroutine");
1106 _exit (EXIT_FAILURE); 1114 _exit (EXIT_FAILURE);
1121{ 1129{
1122 if (!cctx) 1130 if (!cctx)
1123 return; 1131 return;
1124 1132
1125 --cctx_count; 1133 --cctx_count;
1134 coro_destroy (&cctx->cctx);
1126 1135
1136 /* coro_transfer creates new, empty cctx's */
1137 if (cctx->sptr)
1138 {
1127#if CORO_USE_VALGRIND 1139#if CORO_USE_VALGRIND
1128 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1140 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1129#endif 1141#endif
1130 1142
1131#if HAVE_MMAP 1143#if HAVE_MMAP
1132 if (cctx->flags & CC_MAPPED) 1144 if (cctx->flags & CC_MAPPED)
1133 munmap (cctx->sptr, cctx->ssize); 1145 munmap (cctx->sptr, cctx->ssize);
1134 else 1146 else
1135#endif 1147#endif
1136 Safefree (cctx->sptr); 1148 Safefree (cctx->sptr);
1149 }
1137 1150
1138 Safefree (cctx); 1151 Safefree (cctx);
1139} 1152}
1140 1153
1141/* wether this cctx should be destructed */ 1154/* wether this cctx should be destructed */
1142#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) 1155#define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE))
1143 1156
1144static coro_cctx * 1157static coro_cctx *
1145cctx_get (pTHX) 1158cctx_get (pTHX)
1146{ 1159{
1147 while (expect_true (cctx_first)) 1160 while (expect_true (cctx_first))
1160} 1173}
1161 1174
1162static void 1175static void
1163cctx_put (coro_cctx *cctx) 1176cctx_put (coro_cctx *cctx)
1164{ 1177{
1178 assert (("cctx_put called on non-initialised cctx", cctx->sptr));
1179
1165 /* free another cctx if overlimit */ 1180 /* free another cctx if overlimit */
1166 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1181 if (expect_false (cctx_idle >= cctx_max_idle))
1167 { 1182 {
1168 coro_cctx *first = cctx_first; 1183 coro_cctx *first = cctx_first;
1169 cctx_first = first->next; 1184 cctx_first = first->next;
1170 --cctx_idle; 1185 --cctx_idle;
1171 1186
1203/* always use the TRANSFER macro */ 1218/* always use the TRANSFER macro */
1204static void NOINLINE 1219static void NOINLINE
1205transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1220transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1206{ 1221{
1207 dSTACKLEVEL; 1222 dSTACKLEVEL;
1208 static volatile int has_throw;
1209 1223
1210 /* sometimes transfer is only called to set idle_sp */ 1224 /* sometimes transfer is only called to set idle_sp */
1211 if (expect_false (!next)) 1225 if (expect_false (!next))
1212 { 1226 {
1213 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1227 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1214 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1228 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1215 } 1229 }
1216 else if (expect_true (prev != next)) 1230 else if (expect_true (prev != next))
1217 { 1231 {
1232 static volatile int has_throw;
1218 coro_cctx *prev__cctx; 1233 coro_cctx *prev__cctx;
1219 1234
1220 if (expect_false (prev->flags & CF_NEW)) 1235 if (expect_false (prev->flags & CF_NEW))
1221 { 1236 {
1222 /* create a new empty context */ 1237 /* create a new empty/source context */
1238 ++cctx_count;
1223 Newz (0, prev->cctx, 1, coro_cctx); 1239 New (0, prev->cctx, 1, coro_cctx);
1240 prev->cctx->sptr = 0;
1241 coro_create (&prev->cctx->cctx, 0, 0, 0, 0);
1242
1224 prev->flags &= ~CF_NEW; 1243 prev->flags &= ~CF_NEW;
1225 prev->flags |= CF_RUNNING; 1244 prev->flags |= CF_RUNNING;
1226 } 1245 }
1227 1246
1228 prev->flags &= ~CF_RUNNING; 1247 prev->flags &= ~CF_RUNNING;
1335 croak ("FATAL: tried to destroy currently running coroutine"); 1354 croak ("FATAL: tried to destroy currently running coroutine");
1336 1355
1337 save_perl (aTHX_ &temp); 1356 save_perl (aTHX_ &temp);
1338 load_perl (aTHX_ coro); 1357 load_perl (aTHX_ coro);
1339 1358
1340 coro_destroy (aTHX_ coro); 1359 coro_destruct (aTHX_ coro);
1341 1360
1342 load_perl (aTHX_ &temp); 1361 load_perl (aTHX_ &temp);
1343 1362
1344 coro->slot = 0; 1363 coro->slot = 0;
1345 } 1364 }
1633 else 1652 else
1634 coro->slot->runops = RUNOPS_DEFAULT; 1653 coro->slot->runops = RUNOPS_DEFAULT;
1635 } 1654 }
1636} 1655}
1637 1656
1657#if 0
1638static int 1658static int
1639coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) 1659coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1640{ 1660{
1641 AV *padlist; 1661 AV *padlist;
1642 AV *av = (AV *)mg->mg_obj; 1662 AV *av = (AV *)mg->mg_obj;
1648 1668
1649static MGVTBL coro_gensub_vtbl = { 1669static MGVTBL coro_gensub_vtbl = {
1650 0, 0, 0, 0, 1670 0, 0, 0, 0,
1651 coro_gensub_free 1671 coro_gensub_free
1652}; 1672};
1673#endif
1653 1674
1654/*****************************************************************************/ 1675/*****************************************************************************/
1655/* PerlIO::cede */ 1676/* PerlIO::cede */
1656 1677
1657typedef struct 1678typedef struct
1732PROTOTYPES: DISABLE 1753PROTOTYPES: DISABLE
1733 1754
1734BOOT: 1755BOOT:
1735{ 1756{
1736#ifdef USE_ITHREADS 1757#ifdef USE_ITHREADS
1737 MUTEX_INIT (&coro_mutex); 1758 MUTEX_INIT (&coro_lock);
1738#endif 1759#endif
1739 BOOT_PAGESIZE; 1760 BOOT_PAGESIZE;
1740 1761
1741 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1762 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1742 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1763 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1829 ta.next = 0; 1850 ta.next = 0;
1830 break; 1851 break;
1831 1852
1832 case 1: 1853 case 1:
1833 if (items != 2) 1854 if (items != 2)
1834 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); 1855 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items);
1835 1856
1836 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 1857 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1837 break; 1858 break;
1838 1859
1839 case 2: 1860 case 2:
1873 _exit (code); 1894 _exit (code);
1874 1895
1875int 1896int
1876cctx_stacksize (int new_stacksize = 0) 1897cctx_stacksize (int new_stacksize = 0)
1877 CODE: 1898 CODE:
1878 RETVAL = coro_stacksize; 1899 RETVAL = cctx_stacksize;
1879 if (new_stacksize) 1900 if (new_stacksize)
1901 {
1880 coro_stacksize = new_stacksize; 1902 cctx_stacksize = new_stacksize;
1903 ++cctx_gen;
1904 }
1905 OUTPUT:
1906 RETVAL
1907
1908int
1909cctx_max_idle (int max_idle = 0)
1910 CODE:
1911 RETVAL = cctx_max_idle;
1912 if (max_idle > 1)
1913 cctx_max_idle = max_idle;
1881 OUTPUT: 1914 OUTPUT:
1882 RETVAL 1915 RETVAL
1883 1916
1884int 1917int
1885cctx_count () 1918cctx_count ()
1981 CODE: 2014 CODE:
1982 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; 2015 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1983 OUTPUT: 2016 OUTPUT:
1984 RETVAL 2017 RETVAL
1985 2018
1986IV 2019UV
1987rss (Coro::State coro) 2020rss (Coro::State coro)
1988 PROTOTYPE: $ 2021 PROTOTYPE: $
1989 ALIAS: 2022 ALIAS:
1990 usecount = 1 2023 usecount = 1
1991 CODE: 2024 CODE:
2002 CODE: 2035 CODE:
2003 struct coro *coro = SvSTATE (coro_current); 2036 struct coro *coro = SvSTATE (coro_current);
2004 coro->cctx->idle_sp = 0; 2037 coro->cctx->idle_sp = 0;
2005 2038
2006void 2039void
2007throw (Coro::State self, SV *throw = &PL_sv_undef)
2008 PROTOTYPE: $;$
2009 CODE:
2010 SvREFCNT_dec (self->throw);
2011 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2012
2013void
2014swap_defsv (Coro::State self) 2040swap_defsv (Coro::State self)
2015 PROTOTYPE: $ 2041 PROTOTYPE: $
2016 ALIAS: 2042 ALIAS:
2017 swap_defav = 1 2043 swap_defav = 1
2018 CODE: 2044 CODE:
2121 CODE: 2147 CODE:
2122 RETVAL = coro_nready; 2148 RETVAL = coro_nready;
2123 OUTPUT: 2149 OUTPUT:
2124 RETVAL 2150 RETVAL
2125 2151
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
2126# for async_pool speedup 2159# for async_pool speedup
2127void 2160void
2128_pool_1 (SV *cb) 2161_pool_1 (SV *cb)
2129 CODE: 2162 CODE:
2130{ 2163{
2173 sv_setsv (cb, &PL_sv_undef); 2206 sv_setsv (cb, &PL_sv_undef);
2174 2207
2175 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2208 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2176 coro->saved_deffh = 0; 2209 coro->saved_deffh = 0;
2177 2210
2178 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2211 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2179 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2212 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2180 { 2213 {
2181 SV *old = PL_diehook; 2214 SV *old = PL_diehook;
2182 PL_diehook = 0; 2215 PL_diehook = 0;
2183 SvREFCNT_dec (old); 2216 SvREFCNT_dec (old);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines