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.240 by root, Sun Jun 29 00:28:17 2008 UTC vs.
Revision 1.251 by root, Thu Oct 30 09:44:31 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>
80# ifndef IS_PADCONST 81# ifndef IS_PADCONST
81# define IS_PADCONST(v) 0 82# define IS_PADCONST(v) 0
82# endif 83# endif
83#endif 84#endif
84 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
85/* 5.8.8 */ 96/* 5.8.8 */
86#ifndef GV_NOTQUAL 97#ifndef GV_NOTQUAL
87# define GV_NOTQUAL 0 98# define GV_NOTQUAL 0
88#endif 99#endif
89#ifndef newSV 100#ifndef newSV
90# define newSV(l) NEWSV(0,l) 101# define newSV(l) NEWSV(0,l)
91#endif
92
93/* 5.11 */
94#ifndef CxHASARGS
95# define CxHASARGS(cx) (cx)->blk_sub.hasargs
96#endif 102#endif
97 103
98/* 5.8.7 */ 104/* 5.8.7 */
99#ifndef SvRV_set 105#ifndef SvRV_set
100# define SvRV_set(s,v) SvRV(s) = (v) 106# define SvRV_set(s,v) SvRV(s) = (v)
147#endif 153#endif
148 154
149/* helper storage struct for Coro::AIO */ 155/* helper storage struct for Coro::AIO */
150struct io_state 156struct io_state
151{ 157{
158 AV *res;
152 int errorno; 159 int errorno;
153 I32 laststype; 160 I32 laststype;
154 int laststatval; 161 int laststatval;
155 Stat_t statcache; 162 Stat_t statcache;
156}; 163};
164
165static double (*nvtime)(); /* so why doesn't it take void? */
157 166
158static size_t coro_stacksize = CORO_STACKSIZE; 167static size_t coro_stacksize = CORO_STACKSIZE;
159static struct CoroAPI coroapi; 168static struct CoroAPI coroapi;
160static AV *main_mainstack; /* used to differentiate between $main and others */ 169static AV *main_mainstack; /* used to differentiate between $main and others */
161static JMPENV *main_top_env; 170static JMPENV *main_top_env;
321 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 330 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
322#endif 331#endif
323 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 332 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
324 --AvFILLp (padlist); 333 --AvFILLp (padlist);
325 334
326 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 335 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE)));
327 av_store (newpadlist, 1, (SV *)newpad); 336 av_store (newpadlist, 1, (SV *)newpad);
328 337
329 return newpadlist; 338 return newpadlist;
330} 339}
331 340
361 370
362 /* casting is fun. */ 371 /* casting is fun. */
363 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 372 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
364 free_padlist (aTHX_ padlist); 373 free_padlist (aTHX_ padlist);
365 374
375 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
376
366 return 0; 377 return 0;
367} 378}
368 379
369#define CORO_MAGIC_type_cv PERL_MAGIC_ext 380#define CORO_MAGIC_type_cv PERL_MAGIC_ext
370#define CORO_MAGIC_type_state PERL_MAGIC_ext 381#define CORO_MAGIC_type_state PERL_MAGIC_ext
420 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 431 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
421 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 432 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
422 else 433 else
423 { 434 {
424#if CORO_PREFER_PERL_FUNCTIONS 435#if CORO_PREFER_PERL_FUNCTIONS
425 /* this is probably cleaner, but also slower? */ 436 /* this is probably cleaner? but also slower! */
437 /* in practise, it seems to be less stable */
426 CV *cp = Perl_cv_clone (cv); 438 CV *cp = Perl_cv_clone (cv);
427 CvPADLIST (cv) = CvPADLIST (cp); 439 CvPADLIST (cv) = CvPADLIST (cp);
428 CvPADLIST (cp) = 0; 440 CvPADLIST (cp) = 0;
429 SvREFCNT_dec (cp); 441 SvREFCNT_dec (cp);
430#else 442#else
663 #undef VAR 675 #undef VAR
664 } 676 }
665 else 677 else
666 slot = coro->slot; 678 slot = coro->slot;
667 679
680 if (slot)
681 {
668 rss += sizeof (slot->curstackinfo); 682 rss += sizeof (slot->curstackinfo);
669 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 683 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT);
670 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); 684 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *);
671 rss += slot->tmps_max * sizeof (SV *); 685 rss += slot->tmps_max * sizeof (SV *);
672 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); 686 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
673 rss += slot->scopestack_max * sizeof (I32); 687 rss += slot->scopestack_max * sizeof (I32);
674 rss += slot->savestack_max * sizeof (ANY); 688 rss += slot->savestack_max * sizeof (ANY);
675 689
676#if !PERL_VERSION_ATLEAST (5,10,0) 690#if !PERL_VERSION_ATLEAST (5,10,0)
677 rss += slot->retstack_max * sizeof (OP *); 691 rss += slot->retstack_max * sizeof (OP *);
678#endif 692#endif
693 }
679 } 694 }
680 695
681 return rss; 696 return rss;
682} 697}
683 698
686static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); 701static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
687static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); 702static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg);
688static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg); 703static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg);
689 704
690/* apparently < 5.8.8 */ 705/* apparently < 5.8.8 */
691#undef MgPV_nolen_const
692#ifndef MgPV_nolen_const 706#ifndef MgPV_nolen_const
693#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ 707#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \
694 SvPV_nolen_const((SV*)((mg)->mg_ptr)) : \ 708 SvPV_nolen((SV*)((mg)->mg_ptr)) : \
695 (const char*)(mg)->mg_ptr) 709 (const char*)(mg)->mg_ptr)
696#endif 710#endif
697 711
698/* 712/*
699 * This overrides the default magic get method of %SIG elements. 713 * This overrides the default magic get method of %SIG elements.
802 GvSV (PL_defgv) = newSV (0); 816 GvSV (PL_defgv) = newSV (0);
803 GvAV (PL_defgv) = coro->args; coro->args = 0; 817 GvAV (PL_defgv) = coro->args; coro->args = 0;
804 GvSV (PL_errgv) = newSV (0); 818 GvSV (PL_errgv) = newSV (0);
805 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);
806 PL_rs = newSVsv (GvSV (irsgv)); 820 PL_rs = newSVsv (GvSV (irsgv));
807 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); 821 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
808 822
809 { 823 {
810 dSP; 824 dSP;
811 LOGOP myop; 825 LOGOP myop;
812 826
908 : cx->blk_gimme == G_SCALAR ? bot + 1 922 : cx->blk_gimme == G_SCALAR ? bot + 1
909 : bot; 923 : bot;
910 924
911 av_extend (av, top - bot); 925 av_extend (av, top - bot);
912 while (bot < top) 926 while (bot < top)
913 av_push (av, SvREFCNT_inc (*bot++)); 927 av_push (av, SvREFCNT_inc_NN (*bot++));
914 928
915 PL_runops = RUNOPS_DEFAULT; 929 PL_runops = RUNOPS_DEFAULT;
916 ENTER; 930 ENTER;
917 SAVETMPS; 931 SAVETMPS;
918 EXTEND (SP, 3); 932 EXTEND (SP, 3);
1442 LOCK; 1456 LOCK;
1443 1457
1444 sv_hook = coro_nready ? 0 : coro_readyhook; 1458 sv_hook = coro_nready ? 0 : coro_readyhook;
1445 xs_hook = coro_nready ? 0 : coroapi.readyhook; 1459 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1446 1460
1447 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1461 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1448 ++coro_nready; 1462 ++coro_nready;
1449 1463
1450 UNLOCK; 1464 UNLOCK;
1451 1465
1452 if (sv_hook) 1466 if (sv_hook)
1624 else 1638 else
1625 coro->slot->runops = RUNOPS_DEFAULT; 1639 coro->slot->runops = RUNOPS_DEFAULT;
1626 } 1640 }
1627} 1641}
1628 1642
1643static int
1644coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1645{
1646 AV *padlist;
1647 AV *av = (AV *)mg->mg_obj;
1648
1649 abort ();
1650
1651 return 0;
1652}
1653
1654static MGVTBL coro_gensub_vtbl = {
1655 0, 0, 0, 0,
1656 coro_gensub_free
1657};
1658
1659/*****************************************************************************/
1660/* PerlIO::cede */
1661
1662typedef struct
1663{
1664 PerlIOBuf base;
1665 NV next, every;
1666} PerlIOCede;
1667
1668static IV
1669PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1670{
1671 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1672
1673 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
1674 self->next = nvtime () + self->every;
1675
1676 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
1677}
1678
1679static SV *
1680PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
1681{
1682 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1683
1684 return newSVnv (self->every);
1685}
1686
1687static IV
1688PerlIOCede_flush (pTHX_ PerlIO *f)
1689{
1690 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1691 double now = nvtime ();
1692
1693 if (now >= self->next)
1694 {
1695 api_cede ();
1696 self->next = now + self->every;
1697 }
1698
1699 return PerlIOBuf_flush (aTHX_ f);
1700}
1701
1702static PerlIO_funcs PerlIO_cede =
1703{
1704 sizeof(PerlIO_funcs),
1705 "cede",
1706 sizeof(PerlIOCede),
1707 PERLIO_K_DESTRUCT | PERLIO_K_RAW,
1708 PerlIOCede_pushed,
1709 PerlIOBuf_popped,
1710 PerlIOBuf_open,
1711 PerlIOBase_binmode,
1712 PerlIOCede_getarg,
1713 PerlIOBase_fileno,
1714 PerlIOBuf_dup,
1715 PerlIOBuf_read,
1716 PerlIOBuf_unread,
1717 PerlIOBuf_write,
1718 PerlIOBuf_seek,
1719 PerlIOBuf_tell,
1720 PerlIOBuf_close,
1721 PerlIOCede_flush,
1722 PerlIOBuf_fill,
1723 PerlIOBase_eof,
1724 PerlIOBase_error,
1725 PerlIOBase_clearerr,
1726 PerlIOBase_setlinebuf,
1727 PerlIOBuf_get_base,
1728 PerlIOBuf_bufsiz,
1729 PerlIOBuf_get_ptr,
1730 PerlIOBuf_get_cnt,
1731 PerlIOBuf_set_ptrcnt,
1732};
1733
1734
1629MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1735MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1630 1736
1631PROTOTYPES: DISABLE 1737PROTOTYPES: DISABLE
1632 1738
1633BOOT: 1739BOOT:
1662 main_top_env = main_top_env->je_prev; 1768 main_top_env = main_top_env->je_prev;
1663 1769
1664 coroapi.ver = CORO_API_VERSION; 1770 coroapi.ver = CORO_API_VERSION;
1665 coroapi.rev = CORO_API_REVISION; 1771 coroapi.rev = CORO_API_REVISION;
1666 coroapi.transfer = api_transfer; 1772 coroapi.transfer = api_transfer;
1773
1774 {
1775 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1776
1777 if (!svp) croak ("Time::HiRes is required");
1778 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1779
1780 nvtime = INT2PTR (double (*)(), SvIV (*svp));
1781 }
1667 1782
1668 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1783 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1669} 1784}
1670 1785
1671SV * 1786SV *
1799call (Coro::State coro, SV *coderef) 1914call (Coro::State coro, SV *coderef)
1800 ALIAS: 1915 ALIAS:
1801 eval = 1 1916 eval = 1
1802 CODE: 1917 CODE:
1803{ 1918{
1804 if (coro->mainstack) 1919 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
1805 { 1920 {
1806 struct coro temp; 1921 struct coro temp;
1807 1922
1808 if (!(coro->flags & CF_RUNNING)) 1923 if (!(coro->flags & CF_RUNNING))
1809 { 1924 {
1891force_cctx () 2006force_cctx ()
1892 CODE: 2007 CODE:
1893 struct coro *coro = SvSTATE (coro_current); 2008 struct coro *coro = SvSTATE (coro_current);
1894 coro->cctx->idle_sp = 0; 2009 coro->cctx->idle_sp = 0;
1895 2010
1896MODULE = Coro::State PACKAGE = Coro
1897
1898BOOT:
1899{
1900 int i;
1901
1902 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
1903 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
1904 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
1905
1906 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
1907 SvREADONLY_on (coro_current);
1908
1909 coro_stash = gv_stashpv ("Coro", TRUE);
1910
1911 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1912 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1913 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1914 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1915 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1916 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1917
1918 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1919 coro_ready[i] = newAV ();
1920
1921 {
1922 SV *sv = perl_get_sv ("Coro::API", TRUE);
1923 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
1924
1925 coroapi.schedule = api_schedule;
1926 coroapi.cede = api_cede;
1927 coroapi.cede_notself = api_cede_notself;
1928 coroapi.ready = api_ready;
1929 coroapi.is_ready = api_is_ready;
1930 coroapi.nready = &coro_nready;
1931 coroapi.current = coro_current;
1932
1933 GCoroAPI = &coroapi;
1934 sv_setiv (sv, (IV)&coroapi);
1935 SvREADONLY_on (sv);
1936 }
1937}
1938
1939void
1940_set_current (SV *current)
1941 PROTOTYPE: $
1942 CODE:
1943 SvREFCNT_dec (SvRV (coro_current));
1944 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1945
1946void
1947_set_readyhook (SV *hook)
1948 PROTOTYPE: $
1949 CODE:
1950 LOCK;
1951 SvREFCNT_dec (coro_readyhook);
1952 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
1953 UNLOCK;
1954
1955int
1956prio (Coro::State coro, int newprio = 0)
1957 ALIAS:
1958 nice = 1
1959 CODE:
1960{
1961 RETVAL = coro->prio;
1962
1963 if (items > 1)
1964 {
1965 if (ix)
1966 newprio = coro->prio - newprio;
1967
1968 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1969 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1970
1971 coro->prio = newprio;
1972 }
1973}
1974 OUTPUT:
1975 RETVAL
1976
1977SV *
1978ready (SV *self)
1979 PROTOTYPE: $
1980 CODE:
1981 RETVAL = boolSV (api_ready (self));
1982 OUTPUT:
1983 RETVAL
1984
1985int
1986nready (...)
1987 PROTOTYPE:
1988 CODE:
1989 RETVAL = coro_nready;
1990 OUTPUT:
1991 RETVAL
1992
1993void 2011void
1994throw (Coro::State self, SV *throw = &PL_sv_undef) 2012throw (Coro::State self, SV *throw = &PL_sv_undef)
1995 PROTOTYPE: $;$ 2013 PROTOTYPE: $;$
1996 CODE: 2014 CODE:
1997 SvREFCNT_dec (self->throw); 2015 SvREFCNT_dec (self->throw);
2011 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 2029 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
2012 2030
2013 SV *tmp = *src; *src = *dst; *dst = tmp; 2031 SV *tmp = *src; *src = *dst; *dst = tmp;
2014 } 2032 }
2015 2033
2034MODULE = Coro::State PACKAGE = Coro
2035
2036BOOT:
2037{
2038 int i;
2039
2040 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2041 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2042 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2043
2044 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
2045 SvREADONLY_on (coro_current);
2046
2047 coro_stash = gv_stashpv ("Coro", TRUE);
2048
2049 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2050 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2051 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
2052 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
2053 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
2054 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
2055
2056 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
2057 coro_ready[i] = newAV ();
2058
2059 {
2060 SV *sv = perl_get_sv ("Coro::API", TRUE);
2061 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
2062
2063 coroapi.schedule = api_schedule;
2064 coroapi.cede = api_cede;
2065 coroapi.cede_notself = api_cede_notself;
2066 coroapi.ready = api_ready;
2067 coroapi.is_ready = api_is_ready;
2068 coroapi.nready = &coro_nready;
2069 coroapi.current = coro_current;
2070
2071 GCoroAPI = &coroapi;
2072 sv_setiv (sv, (IV)&coroapi);
2073 SvREADONLY_on (sv);
2074 }
2075}
2076
2077void
2078_set_current (SV *current)
2079 PROTOTYPE: $
2080 CODE:
2081 SvREFCNT_dec (SvRV (coro_current));
2082 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
2083
2084void
2085_set_readyhook (SV *hook)
2086 PROTOTYPE: $
2087 CODE:
2088 LOCK;
2089 SvREFCNT_dec (coro_readyhook);
2090 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
2091 UNLOCK;
2092
2093int
2094prio (Coro::State coro, int newprio = 0)
2095 ALIAS:
2096 nice = 1
2097 CODE:
2098{
2099 RETVAL = coro->prio;
2100
2101 if (items > 1)
2102 {
2103 if (ix)
2104 newprio = coro->prio - newprio;
2105
2106 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
2107 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
2108
2109 coro->prio = newprio;
2110 }
2111}
2112 OUTPUT:
2113 RETVAL
2114
2115SV *
2116ready (SV *self)
2117 PROTOTYPE: $
2118 CODE:
2119 RETVAL = boolSV (api_ready (self));
2120 OUTPUT:
2121 RETVAL
2122
2123int
2124nready (...)
2125 PROTOTYPE:
2126 CODE:
2127 RETVAL = coro_nready;
2128 OUTPUT:
2129 RETVAL
2130
2016# for async_pool speedup 2131# for async_pool speedup
2017void 2132void
2018_pool_1 (SV *cb) 2133_pool_1 (SV *cb)
2019 CODE: 2134 CODE:
2020{ 2135{
2032 SvREFCNT_dec (old); 2147 SvREFCNT_dec (old);
2033 croak ("\3async_pool terminate\2\n"); 2148 croak ("\3async_pool terminate\2\n");
2034 } 2149 }
2035 2150
2036 SvREFCNT_dec (coro->saved_deffh); 2151 SvREFCNT_dec (coro->saved_deffh);
2037 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2152 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2038 2153
2039 hv_store (hv, "desc", sizeof ("desc") - 1, 2154 hv_store (hv, "desc", sizeof ("desc") - 1,
2040 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 2155 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2041 2156
2042 invoke_av = (AV *)SvRV (invoke); 2157 invoke_av = (AV *)SvRV (invoke);
2046 2161
2047 if (len > 0) 2162 if (len > 0)
2048 { 2163 {
2049 av_fill (defav, len - 1); 2164 av_fill (defav, len - 1);
2050 for (i = 0; i < len; ++i) 2165 for (i = 0; i < len; ++i)
2051 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2166 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
2052 } 2167 }
2053 2168
2054 SvREFCNT_dec (invoke); 2169 SvREFCNT_dec (invoke);
2055} 2170}
2056 2171
2084 api_trace (coro_current, 0); 2199 api_trace (coro_current, 0);
2085 2200
2086 av_push (av_async_pool, newSVsv (coro_current)); 2201 av_push (av_async_pool, newSVsv (coro_current));
2087} 2202}
2088 2203
2204#if 0
2205
2206void
2207_generator_call (...)
2208 PROTOTYPE: @
2209 PPCODE:
2210 fprintf (stderr, "call %p\n", CvXSUBANY(cv).any_ptr);
2211 xxxx
2212 abort ();
2213
2214SV *
2215gensub (SV *sub, ...)
2216 PROTOTYPE: &;@
2217 CODE:
2218{
2219 struct coro *coro;
2220 MAGIC *mg;
2221 CV *xcv;
2222 CV *ncv = (CV *)newSV_type (SVt_PVCV);
2223 int i;
2224
2225 CvGV (ncv) = CvGV (cv);
2226 CvFILE (ncv) = CvFILE (cv);
2227
2228 Newz (0, coro, 1, struct coro);
2229 coro->args = newAV ();
2230 coro->flags = CF_NEW;
2231
2232 av_extend (coro->args, items - 1);
2233 for (i = 1; i < items; i++)
2234 av_push (coro->args, newSVsv (ST (i)));
2235
2236 CvISXSUB_on (ncv);
2237 CvXSUBANY (ncv).any_ptr = (void *)coro;
2238
2239 xcv = GvCV (gv_fetchpv ("Coro::_generator_call", 0, SVt_PVCV));
2240
2241 CvXSUB (ncv) = CvXSUB (xcv);
2242 CvANON_on (ncv);
2243
2244 mg = sv_magicext ((SV *)ncv, 0, CORO_MAGIC_type_state, &coro_gensub_vtbl, (char *)coro, 0);
2245 RETVAL = newRV_noinc ((SV *)ncv);
2246}
2247 OUTPUT:
2248 RETVAL
2249
2250#endif
2251
2089 2252
2090MODULE = Coro::State PACKAGE = Coro::AIO 2253MODULE = Coro::State PACKAGE = Coro::AIO
2091 2254
2092SV * 2255void
2093_get_state () 2256_get_state (SV *self)
2094 CODE: 2257 PPCODE:
2095{ 2258{
2096 struct io_state *data; 2259 AV *defav = GvAV (PL_defgv);
2097 2260 AV *av = newAV ();
2261 int i;
2098 RETVAL = newSV (sizeof (struct io_state)); 2262 SV *data_sv = newSV (sizeof (struct io_state));
2099 data = (struct io_state *)SvPVX (RETVAL); 2263 struct io_state *data = (struct io_state *)SvPVX (data_sv);
2100 SvCUR_set (RETVAL, sizeof (struct io_state)); 2264 SvCUR_set (data_sv, sizeof (struct io_state));
2101 SvPOK_only (RETVAL); 2265 SvPOK_only (data_sv);
2102 2266
2103 data->errorno = errno; 2267 data->errorno = errno;
2104 data->laststype = PL_laststype; 2268 data->laststype = PL_laststype;
2105 data->laststatval = PL_laststatval; 2269 data->laststatval = PL_laststatval;
2106 data->statcache = PL_statcache; 2270 data->statcache = PL_statcache;
2271
2272 av_extend (av, AvFILLp (defav) + 1 + 1);
2273
2274 for (i = 0; i <= AvFILLp (defav); ++i)
2275 av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i]));
2276
2277 av_push (av, data_sv);
2278
2279 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2280
2281 api_ready (self);
2107} 2282}
2108 OUTPUT:
2109 RETVAL
2110 2283
2111void 2284void
2112_set_state (char *data_) 2285_set_state (SV *state)
2113 PROTOTYPE: $ 2286 PROTOTYPE: $
2114 CODE: 2287 PPCODE:
2115{ 2288{
2116 struct io_state *data = (void *)data_; 2289 AV *av = (AV *)SvRV (state);
2290 struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]);
2291 int i;
2117 2292
2118 errno = data->errorno; 2293 errno = data->errorno;
2119 PL_laststype = data->laststype; 2294 PL_laststype = data->laststype;
2120 PL_laststatval = data->laststatval; 2295 PL_laststatval = data->laststatval;
2121 PL_statcache = data->statcache; 2296 PL_statcache = data->statcache;
2297
2298 EXTEND (SP, AvFILLp (av));
2299 for (i = 0; i < AvFILLp (av); ++i)
2300 PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i])));
2122} 2301}
2123 2302
2124 2303
2125MODULE = Coro::State PACKAGE = Coro::AnyEvent 2304MODULE = Coro::State PACKAGE = Coro::AnyEvent
2126 2305
2150 } 2329 }
2151 2330
2152 --incede; 2331 --incede;
2153} 2332}
2154 2333
2334
2335MODULE = Coro::State PACKAGE = PerlIO::cede
2336
2337BOOT:
2338 PerlIO_define_layer (aTHX_ &PerlIO_cede);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines