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.253 by root, Fri Nov 7 20:12:26 2008 UTC vs.
Revision 1.256 by root, Sat Nov 8 13:32:18 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) \
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? */
166 178
179static U32 cctx_gen;
167static size_t coro_stacksize = CORO_STACKSIZE; 180static size_t cctx_stacksize = CORO_STACKSIZE;
168static struct CoroAPI coroapi; 181static struct CoroAPI coroapi;
169static AV *main_mainstack; /* used to differentiate between $main and others */ 182static AV *main_mainstack; /* used to differentiate between $main and others */
170static JMPENV *main_top_env; 183static JMPENV *main_top_env;
171static HV *coro_state_stash, *coro_stash; 184static HV *coro_state_stash, *coro_stash;
172static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 185static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
209 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 222 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
210 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 223 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
211 JMPENV *top_env; 224 JMPENV *top_env;
212 coro_context cctx; 225 coro_context cctx;
213 226
227 U32 gen;
214#if CORO_USE_VALGRIND 228#if CORO_USE_VALGRIND
215 int valgrind_id; 229 int valgrind_id;
216#endif 230#endif
217 unsigned char flags; 231 unsigned char flags;
218} coro_cctx; 232} coro_cctx;
1046 * this is a _very_ stripped down perl interpreter ;) 1060 * this is a _very_ stripped down perl interpreter ;)
1047 */ 1061 */
1048static void 1062static void
1049cctx_run (void *arg) 1063cctx_run (void *arg)
1050{ 1064{
1065#ifdef USE_ITHREADS
1066# if CORO_PTHREAD
1067 PERL_SET_CONTEXT (coro_thx);
1068# endif
1069#endif
1070 {
1051 dTHX; 1071 dTHX;
1052 1072
1053 /* cctx_run is the alternative tail of transfer(), so unlock here. */ 1073 /* cctx_run is the alternative tail of transfer(), so unlock here. */
1054 UNLOCK; 1074 UNLOCK;
1055 1075
1056 /* we now skip the entersub that lead to transfer() */ 1076 /* we now skip the entersub that lead to transfer() */
1057 PL_op = PL_op->op_next; 1077 PL_op = PL_op->op_next;
1058 1078
1059 /* inject a fake subroutine call to cctx_init */ 1079 /* inject a fake subroutine call to cctx_init */
1060 cctx_prepare (aTHX_ (coro_cctx *)arg); 1080 cctx_prepare (aTHX_ (coro_cctx *)arg);
1061 1081
1062 /* somebody or something will hit me for both perl_run and PL_restartop */ 1082 /* somebody or something will hit me for both perl_run and PL_restartop */
1063 PL_restartop = PL_op; 1083 PL_restartop = PL_op;
1064 perl_run (PL_curinterp); 1084 perl_run (PL_curinterp);
1065 1085
1066 /* 1086 /*
1067 * If perl-run returns we assume exit() was being called or the coro 1087 * If perl-run returns we assume exit() was being called or the coro
1068 * fell off the end, which seems to be the only valid (non-bug) 1088 * fell off the end, which seems to be the only valid (non-bug)
1069 * reason for perl_run to return. We try to exit by jumping to the 1089 * reason for perl_run to return. We try to exit by jumping to the
1070 * bootstrap-time "top" top_env, as we cannot restore the "main" 1090 * bootstrap-time "top" top_env, as we cannot restore the "main"
1071 * coroutine as Coro has no such concept 1091 * coroutine as Coro has no such concept
1072 */ 1092 */
1073 PL_top_env = main_top_env; 1093 PL_top_env = main_top_env;
1074 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1094 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1095 }
1075} 1096}
1076 1097
1077static coro_cctx * 1098static coro_cctx *
1078cctx_new () 1099cctx_new ()
1079{ 1100{
1082 size_t stack_size; 1103 size_t stack_size;
1083 1104
1084 ++cctx_count; 1105 ++cctx_count;
1085 Newz (0, cctx, 1, coro_cctx); 1106 Newz (0, cctx, 1, coro_cctx);
1086 1107
1108 cctx->gen = cctx_gen;
1109
1087#if HAVE_MMAP 1110#if HAVE_MMAP
1088 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1111 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1089 /* mmap supposedly does allocate-on-write for us */ 1112 /* mmap supposedly does allocate-on-write for us */
1090 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1113 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
1091 1114
1092 if (cctx->sptr != (void *)-1) 1115 if (cctx->sptr != (void *)-1)
1093 { 1116 {
1099 cctx->flags |= CC_MAPPED; 1122 cctx->flags |= CC_MAPPED;
1100 } 1123 }
1101 else 1124 else
1102#endif 1125#endif
1103 { 1126 {
1104 cctx->ssize = coro_stacksize * (long)sizeof (long); 1127 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1105 New (0, cctx->sptr, coro_stacksize, long); 1128 New (0, cctx->sptr, cctx_stacksize, long);
1106 1129
1107 if (!cctx->sptr) 1130 if (!cctx->sptr)
1108 { 1131 {
1109 perror ("FATAL: unable to allocate stack for coroutine"); 1132 perror ("FATAL: unable to allocate stack for coroutine");
1110 _exit (EXIT_FAILURE); 1133 _exit (EXIT_FAILURE);
1146 1169
1147 Safefree (cctx); 1170 Safefree (cctx);
1148} 1171}
1149 1172
1150/* wether this cctx should be destructed */ 1173/* wether this cctx should be destructed */
1151#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) 1174#define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE))
1152 1175
1153static coro_cctx * 1176static coro_cctx *
1154cctx_get (pTHX) 1177cctx_get (pTHX)
1155{ 1178{
1156 while (expect_true (cctx_first)) 1179 while (expect_true (cctx_first))
1172cctx_put (coro_cctx *cctx) 1195cctx_put (coro_cctx *cctx)
1173{ 1196{
1174 assert (("cctx_put called on non-initialised cctx", cctx->sptr)); 1197 assert (("cctx_put called on non-initialised cctx", cctx->sptr));
1175 1198
1176 /* free another cctx if overlimit */ 1199 /* free another cctx if overlimit */
1177 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1200 if (expect_false (cctx_idle >= cctx_max_idle))
1178 { 1201 {
1179 coro_cctx *first = cctx_first; 1202 coro_cctx *first = cctx_first;
1180 cctx_first = first->next; 1203 cctx_first = first->next;
1181 --cctx_idle; 1204 --cctx_idle;
1182 1205
1648 else 1671 else
1649 coro->slot->runops = RUNOPS_DEFAULT; 1672 coro->slot->runops = RUNOPS_DEFAULT;
1650 } 1673 }
1651} 1674}
1652 1675
1676#if 0
1653static int 1677static int
1654coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) 1678coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1655{ 1679{
1656 AV *padlist; 1680 AV *padlist;
1657 AV *av = (AV *)mg->mg_obj; 1681 AV *av = (AV *)mg->mg_obj;
1663 1687
1664static MGVTBL coro_gensub_vtbl = { 1688static MGVTBL coro_gensub_vtbl = {
1665 0, 0, 0, 0, 1689 0, 0, 0, 0,
1666 coro_gensub_free 1690 coro_gensub_free
1667}; 1691};
1692#endif
1668 1693
1669/*****************************************************************************/ 1694/*****************************************************************************/
1670/* PerlIO::cede */ 1695/* PerlIO::cede */
1671 1696
1672typedef struct 1697typedef struct
1748 1773
1749BOOT: 1774BOOT:
1750{ 1775{
1751#ifdef USE_ITHREADS 1776#ifdef USE_ITHREADS
1752 MUTEX_INIT (&coro_lock); 1777 MUTEX_INIT (&coro_lock);
1778# if CORO_PTHREAD
1779 coro_thx = PERL_GET_CONTEXT;
1780# endif
1753#endif 1781#endif
1754 BOOT_PAGESIZE; 1782 BOOT_PAGESIZE;
1755 1783
1756 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1784 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1757 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1785 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1888 _exit (code); 1916 _exit (code);
1889 1917
1890int 1918int
1891cctx_stacksize (int new_stacksize = 0) 1919cctx_stacksize (int new_stacksize = 0)
1892 CODE: 1920 CODE:
1893 RETVAL = coro_stacksize; 1921 RETVAL = cctx_stacksize;
1894 if (new_stacksize) 1922 if (new_stacksize)
1923 {
1895 coro_stacksize = new_stacksize; 1924 cctx_stacksize = new_stacksize;
1925 ++cctx_gen;
1926 }
1927 OUTPUT:
1928 RETVAL
1929
1930int
1931cctx_max_idle (int max_idle = 0)
1932 CODE:
1933 RETVAL = cctx_max_idle;
1934 if (max_idle > 1)
1935 cctx_max_idle = max_idle;
1896 OUTPUT: 1936 OUTPUT:
1897 RETVAL 1937 RETVAL
1898 1938
1899int 1939int
1900cctx_count () 1940cctx_count ()
1996 CODE: 2036 CODE:
1997 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; 2037 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1998 OUTPUT: 2038 OUTPUT:
1999 RETVAL 2039 RETVAL
2000 2040
2001IV 2041UV
2002rss (Coro::State coro) 2042rss (Coro::State coro)
2003 PROTOTYPE: $ 2043 PROTOTYPE: $
2004 ALIAS: 2044 ALIAS:
2005 usecount = 1 2045 usecount = 1
2006 CODE: 2046 CODE:
2188 sv_setsv (cb, &PL_sv_undef); 2228 sv_setsv (cb, &PL_sv_undef);
2189 2229
2190 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2230 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2191 coro->saved_deffh = 0; 2231 coro->saved_deffh = 0;
2192 2232
2193 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2233 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2194 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2234 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2195 { 2235 {
2196 SV *old = PL_diehook; 2236 SV *old = PL_diehook;
2197 PL_diehook = 0; 2237 PL_diehook = 0;
2198 SvREFCNT_dec (old); 2238 SvREFCNT_dec (old);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines