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.242 by root, Thu Aug 14 12:48:20 2008 UTC vs.
Revision 1.249 by root, Tue Sep 30 17:12:35 2008 UTC

4#define PERL_EXT 4#define PERL_EXT
5 5
6#include "EXTERN.h" 6#include "EXTERN.h"
7#include "perl.h" 7#include "perl.h"
8#include "XSUB.h" 8#include "XSUB.h"
9#include "perliol.h"
9 10
10#include "patchlevel.h" 11#include "patchlevel.h"
11 12
12#include <stdio.h> 13#include <stdio.h>
13#include <errno.h> 14#include <errno.h>
147#endif 148#endif
148 149
149/* helper storage struct for Coro::AIO */ 150/* helper storage struct for Coro::AIO */
150struct io_state 151struct io_state
151{ 152{
153 AV *res;
152 int errorno; 154 int errorno;
153 I32 laststype; 155 I32 laststype;
154 int laststatval; 156 int laststatval;
155 Stat_t statcache; 157 Stat_t statcache;
156}; 158};
159
160static double (*nvtime)(); /* so why doesn't it take void? */
157 161
158static size_t coro_stacksize = CORO_STACKSIZE; 162static size_t coro_stacksize = CORO_STACKSIZE;
159static struct CoroAPI coroapi; 163static struct CoroAPI coroapi;
160static AV *main_mainstack; /* used to differentiate between $main and others */ 164static AV *main_mainstack; /* used to differentiate between $main and others */
161static JMPENV *main_top_env; 165static JMPENV *main_top_env;
321 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 325 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
322#endif 326#endif
323 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 327 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
324 --AvFILLp (padlist); 328 --AvFILLp (padlist);
325 329
326 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 330 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE)));
327 av_store (newpadlist, 1, (SV *)newpad); 331 av_store (newpadlist, 1, (SV *)newpad);
328 332
329 return newpadlist; 333 return newpadlist;
330} 334}
331 335
361 365
362 /* casting is fun. */ 366 /* casting is fun. */
363 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 367 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
364 free_padlist (aTHX_ padlist); 368 free_padlist (aTHX_ padlist);
365 369
370 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
371
366 return 0; 372 return 0;
367} 373}
368 374
369#define CORO_MAGIC_type_cv PERL_MAGIC_ext 375#define CORO_MAGIC_type_cv PERL_MAGIC_ext
370#define CORO_MAGIC_type_state PERL_MAGIC_ext 376#define CORO_MAGIC_type_state PERL_MAGIC_ext
420 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 426 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
421 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 427 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
422 else 428 else
423 { 429 {
424#if CORO_PREFER_PERL_FUNCTIONS 430#if CORO_PREFER_PERL_FUNCTIONS
425 /* this is probably cleaner, but also slower? */ 431 /* this is probably cleaner? but also slower! */
432 /* in practise, it seems to be less stable */
426 CV *cp = Perl_cv_clone (cv); 433 CV *cp = Perl_cv_clone (cv);
427 CvPADLIST (cv) = CvPADLIST (cp); 434 CvPADLIST (cv) = CvPADLIST (cp);
428 CvPADLIST (cp) = 0; 435 CvPADLIST (cp) = 0;
429 SvREFCNT_dec (cp); 436 SvREFCNT_dec (cp);
430#else 437#else
663 #undef VAR 670 #undef VAR
664 } 671 }
665 else 672 else
666 slot = coro->slot; 673 slot = coro->slot;
667 674
675 if (slot)
676 {
668 rss += sizeof (slot->curstackinfo); 677 rss += sizeof (slot->curstackinfo);
669 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 678 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
670 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); 679 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *);
671 rss += slot->tmps_max * sizeof (SV *); 680 rss += slot->tmps_max * sizeof (SV *);
672 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); 681 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
673 rss += slot->scopestack_max * sizeof (I32); 682 rss += slot->scopestack_max * sizeof (I32);
674 rss += slot->savestack_max * sizeof (ANY); 683 rss += slot->savestack_max * sizeof (ANY);
675 684
676#if !PERL_VERSION_ATLEAST (5,10,0) 685#if !PERL_VERSION_ATLEAST (5,10,0)
677 rss += slot->retstack_max * sizeof (OP *); 686 rss += slot->retstack_max * sizeof (OP *);
678#endif 687#endif
688 }
679 } 689 }
680 690
681 return rss; 691 return rss;
682} 692}
683 693
801 GvSV (PL_defgv) = newSV (0); 811 GvSV (PL_defgv) = newSV (0);
802 GvAV (PL_defgv) = coro->args; coro->args = 0; 812 GvAV (PL_defgv) = coro->args; coro->args = 0;
803 GvSV (PL_errgv) = newSV (0); 813 GvSV (PL_errgv) = newSV (0);
804 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 814 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
805 PL_rs = newSVsv (GvSV (irsgv)); 815 PL_rs = newSVsv (GvSV (irsgv));
806 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); 816 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
807 817
808 { 818 {
809 dSP; 819 dSP;
810 LOGOP myop; 820 LOGOP myop;
811 821
907 : cx->blk_gimme == G_SCALAR ? bot + 1 917 : cx->blk_gimme == G_SCALAR ? bot + 1
908 : bot; 918 : bot;
909 919
910 av_extend (av, top - bot); 920 av_extend (av, top - bot);
911 while (bot < top) 921 while (bot < top)
912 av_push (av, SvREFCNT_inc (*bot++)); 922 av_push (av, SvREFCNT_inc_NN (*bot++));
913 923
914 PL_runops = RUNOPS_DEFAULT; 924 PL_runops = RUNOPS_DEFAULT;
915 ENTER; 925 ENTER;
916 SAVETMPS; 926 SAVETMPS;
917 EXTEND (SP, 3); 927 EXTEND (SP, 3);
1441 LOCK; 1451 LOCK;
1442 1452
1443 sv_hook = coro_nready ? 0 : coro_readyhook; 1453 sv_hook = coro_nready ? 0 : coro_readyhook;
1444 xs_hook = coro_nready ? 0 : coroapi.readyhook; 1454 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1445 1455
1446 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1456 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1447 ++coro_nready; 1457 ++coro_nready;
1448 1458
1449 UNLOCK; 1459 UNLOCK;
1450 1460
1451 if (sv_hook) 1461 if (sv_hook)
1623 else 1633 else
1624 coro->slot->runops = RUNOPS_DEFAULT; 1634 coro->slot->runops = RUNOPS_DEFAULT;
1625 } 1635 }
1626} 1636}
1627 1637
1638static int
1639coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1640{
1641 AV *padlist;
1642 AV *av = (AV *)mg->mg_obj;
1643
1644 abort ();
1645
1646 return 0;
1647}
1648
1649static MGVTBL coro_gensub_vtbl = {
1650 0, 0, 0, 0,
1651 coro_gensub_free
1652};
1653
1654/*****************************************************************************/
1655/* PerlIO::cede */
1656
1657typedef struct
1658{
1659 PerlIOBuf base;
1660 NV next, every;
1661} PerlIOCede;
1662
1663static IV
1664PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1665{
1666 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1667
1668 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
1669 self->next = nvtime () + self->every;
1670
1671 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
1672}
1673
1674static SV *
1675PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
1676{
1677 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1678
1679 return newSVnv (self->every);
1680}
1681
1682static IV
1683PerlIOCede_flush (pTHX_ PerlIO *f)
1684{
1685 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1686 double now = nvtime ();
1687
1688 if (now >= self->next)
1689 {
1690 api_cede ();
1691 self->next = now + self->every;
1692 }
1693
1694 return PerlIOBuf_flush (aTHX_ f);
1695}
1696
1697static PerlIO_funcs PerlIO_cede =
1698{
1699 sizeof(PerlIO_funcs),
1700 "cede",
1701 sizeof(PerlIOCede),
1702 PERLIO_K_DESTRUCT | PERLIO_K_RAW,
1703 PerlIOCede_pushed,
1704 PerlIOBuf_popped,
1705 PerlIOBuf_open,
1706 PerlIOBase_binmode,
1707 PerlIOCede_getarg,
1708 PerlIOBase_fileno,
1709 PerlIOBuf_dup,
1710 PerlIOBuf_read,
1711 PerlIOBuf_unread,
1712 PerlIOBuf_write,
1713 PerlIOBuf_seek,
1714 PerlIOBuf_tell,
1715 PerlIOBuf_close,
1716 PerlIOCede_flush,
1717 PerlIOBuf_fill,
1718 PerlIOBase_eof,
1719 PerlIOBase_error,
1720 PerlIOBase_clearerr,
1721 PerlIOBase_setlinebuf,
1722 PerlIOBuf_get_base,
1723 PerlIOBuf_bufsiz,
1724 PerlIOBuf_get_ptr,
1725 PerlIOBuf_get_cnt,
1726 PerlIOBuf_set_ptrcnt,
1727};
1728
1729
1628MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1730MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1629 1731
1630PROTOTYPES: DISABLE 1732PROTOTYPES: DISABLE
1631 1733
1632BOOT: 1734BOOT:
1661 main_top_env = main_top_env->je_prev; 1763 main_top_env = main_top_env->je_prev;
1662 1764
1663 coroapi.ver = CORO_API_VERSION; 1765 coroapi.ver = CORO_API_VERSION;
1664 coroapi.rev = CORO_API_REVISION; 1766 coroapi.rev = CORO_API_REVISION;
1665 coroapi.transfer = api_transfer; 1767 coroapi.transfer = api_transfer;
1768
1769 {
1770 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1771
1772 if (!svp) croak ("Time::HiRes is required");
1773 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1774
1775 nvtime = INT2PTR (double (*)(), SvIV (*svp));
1776 }
1666 1777
1667 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1778 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1668} 1779}
1669 1780
1670SV * 1781SV *
1798call (Coro::State coro, SV *coderef) 1909call (Coro::State coro, SV *coderef)
1799 ALIAS: 1910 ALIAS:
1800 eval = 1 1911 eval = 1
1801 CODE: 1912 CODE:
1802{ 1913{
1803 if (coro->mainstack) 1914 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
1804 { 1915 {
1805 struct coro temp; 1916 struct coro temp;
1806 1917
1807 if (!(coro->flags & CF_RUNNING)) 1918 if (!(coro->flags & CF_RUNNING))
1808 { 1919 {
1961void 2072void
1962_set_current (SV *current) 2073_set_current (SV *current)
1963 PROTOTYPE: $ 2074 PROTOTYPE: $
1964 CODE: 2075 CODE:
1965 SvREFCNT_dec (SvRV (coro_current)); 2076 SvREFCNT_dec (SvRV (coro_current));
1966 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); 2077 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
1967 2078
1968void 2079void
1969_set_readyhook (SV *hook) 2080_set_readyhook (SV *hook)
1970 PROTOTYPE: $ 2081 PROTOTYPE: $
1971 CODE: 2082 CODE:
2031 SvREFCNT_dec (old); 2142 SvREFCNT_dec (old);
2032 croak ("\3async_pool terminate\2\n"); 2143 croak ("\3async_pool terminate\2\n");
2033 } 2144 }
2034 2145
2035 SvREFCNT_dec (coro->saved_deffh); 2146 SvREFCNT_dec (coro->saved_deffh);
2036 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2147 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2037 2148
2038 hv_store (hv, "desc", sizeof ("desc") - 1, 2149 hv_store (hv, "desc", sizeof ("desc") - 1,
2039 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 2150 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2040 2151
2041 invoke_av = (AV *)SvRV (invoke); 2152 invoke_av = (AV *)SvRV (invoke);
2045 2156
2046 if (len > 0) 2157 if (len > 0)
2047 { 2158 {
2048 av_fill (defav, len - 1); 2159 av_fill (defav, len - 1);
2049 for (i = 0; i < len; ++i) 2160 for (i = 0; i < len; ++i)
2050 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2161 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
2051 } 2162 }
2052 2163
2053 SvREFCNT_dec (invoke); 2164 SvREFCNT_dec (invoke);
2054} 2165}
2055 2166
2083 api_trace (coro_current, 0); 2194 api_trace (coro_current, 0);
2084 2195
2085 av_push (av_async_pool, newSVsv (coro_current)); 2196 av_push (av_async_pool, newSVsv (coro_current));
2086} 2197}
2087 2198
2199#if 0
2200
2201void
2202_generator_call (...)
2203 PROTOTYPE: @
2204 PPCODE:
2205 fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr);
2206 xxxx
2207 abort ();
2208
2209SV *
2210gensub (SV *sub, ...)
2211 PROTOTYPE: &;@
2212 CODE:
2213{
2214 struct coro *coro;
2215 MAGIC *mg;
2216 CV *xcv;
2217 CV *ncv = (CV *)newSV_type (SVt_PVCV);
2218 int i;
2219
2220 CvGV (ncv) = CvGV (cv);
2221 CvFILE (ncv) = CvFILE (cv);
2222
2223 Newz (0, coro, 1, struct coro);
2224 coro->args = newAV ();
2225 coro->flags = CF_NEW;
2226
2227 av_extend (coro->args, items - 1);
2228 for (i = 1; i < items; i++)
2229 av_push (coro->args, newSVsv (ST (i)));
2230
2231 CvISXSUB_on (ncv);
2232 CvXSUBANY (ncv).any_ptr = (void *)coro;
2233
2234 xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV));
2235
2236 CvXSUB (ncv) = CvXSUB (xcv);
2237 CvANON_on (ncv);
2238
2239 mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0);
2240 RETVAL = newRV_noinc ((SV *)ncv);
2241}
2242 OUTPUT:
2243 RETVAL
2244
2245#endif
2246
2088 2247
2089MODULE = Coro::State PACKAGE = Coro::AIO 2248MODULE = Coro::State PACKAGE = Coro::AIO
2090 2249
2091SV * 2250void
2092_get_state () 2251_get_state (SV *self)
2093 CODE: 2252 PPCODE:
2094{ 2253{
2095 struct io_state *data; 2254 AV *defav = GvAV (PL_defgv);
2096 2255 AV *av = newAV ();
2256 int i;
2097 RETVAL = newSV (sizeof (struct io_state)); 2257 SV *data_sv = newSV (sizeof (struct io_state));
2098 data = (struct io_state *)SvPVX (RETVAL); 2258 struct io_state *data = (struct io_state *)SvPVX (data_sv);
2099 SvCUR_set (RETVAL, sizeof (struct io_state)); 2259 SvCUR_set (data_sv, sizeof (struct io_state));
2100 SvPOK_only (RETVAL); 2260 SvPOK_only (data_sv);
2101 2261
2102 data->errorno = errno; 2262 data->errorno = errno;
2103 data->laststype = PL_laststype; 2263 data->laststype = PL_laststype;
2104 data->laststatval = PL_laststatval; 2264 data->laststatval = PL_laststatval;
2105 data->statcache = PL_statcache; 2265 data->statcache = PL_statcache;
2266
2267 av_extend (av, AvFILLp (defav) + 1 + 1);
2268
2269 for (i = 0; i <= AvFILLp (defav); ++i)
2270 av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i]));
2271
2272 av_push (av, data_sv);
2273
2274 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2275
2276 api_ready (self);
2106} 2277}
2107 OUTPUT:
2108 RETVAL
2109 2278
2110void 2279void
2111_set_state (char *data_) 2280_set_state (SV *state)
2112 PROTOTYPE: $ 2281 PROTOTYPE: $
2113 CODE: 2282 PPCODE:
2114{ 2283{
2115 struct io_state *data = (void *)data_; 2284 AV *av = (AV *)SvRV (state);
2285 struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]);
2286 int i;
2116 2287
2117 errno = data->errorno; 2288 errno = data->errorno;
2118 PL_laststype = data->laststype; 2289 PL_laststype = data->laststype;
2119 PL_laststatval = data->laststatval; 2290 PL_laststatval = data->laststatval;
2120 PL_statcache = data->statcache; 2291 PL_statcache = data->statcache;
2292
2293 EXTEND (SP, AvFILLp (av));
2294 for (i = 0; i < AvFILLp (av); ++i)
2295 PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i])));
2121} 2296}
2122 2297
2123 2298
2124MODULE = Coro::State PACKAGE = Coro::AnyEvent 2299MODULE = Coro::State PACKAGE = Coro::AnyEvent
2125 2300
2149 } 2324 }
2150 2325
2151 --incede; 2326 --incede;
2152} 2327}
2153 2328
2329
2330MODULE = Coro::State PACKAGE = PerlIO::cede
2331
2332BOOT:
2333 PerlIO_define_layer (aTHX_ &PerlIO_cede);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines