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.266 by root, Fri Nov 14 03:26:22 2008 UTC vs.
Revision 1.268 by root, Fri Nov 14 06:41:41 2008 UTC

116# define CORO_PREFER_PERL_FUNCTIONS 0 116# define CORO_PREFER_PERL_FUNCTIONS 0
117#endif 117#endif
118 118
119/* The next macros try to return the current stack pointer, in an as 119/* The next macros try to return the current stack pointer, in an as
120 * portable way as possible. */ 120 * portable way as possible. */
121#define dSTACKLEVEL volatile char stacklevel 121#if __GNUC__ >= 4
122#define STACKLEVEL ((void *)&stacklevel) 122# define dSTACKLEVEL void *stacklevel = __builtin_frame_address (0)
123#else
124# define dSTACKLEVEL volatile void *stacklevel = (volatile void *)&stacklevel
125#endif
123 126
124#define IN_DESTRUCT (PL_main_cv == Nullcv) 127#define IN_DESTRUCT (PL_main_cv == Nullcv)
125 128
126#if __GNUC__ >= 3 129#if __GNUC__ >= 3
127# define attribute(x) __attribute__(x) 130# define attribute(x) __attribute__(x)
715 718
716/** set stacklevel support **************************************************/ 719/** set stacklevel support **************************************************/
717 720
718/* we sometimes need to create the effect of pp_set_stacklevel calling us */ 721/* we sometimes need to create the effect of pp_set_stacklevel calling us */
719#define SSL_HEAD (void)0 722#define SSL_HEAD (void)0
720/* we somtimes need to create the effect of leaving via pp_set_stacklevel */ 723/* we sometimes need to create the effect of leaving via pp_set_stacklevel */
721#define SSL_TAIL set_stacklevel_tail (aTHX) 724#define SSL_TAIL set_stacklevel_tail (aTHX)
722 725
723INLINE void 726INLINE void
724set_stacklevel_tail (pTHX) 727set_stacklevel_tail (pTHX)
725{ 728{
1329 dSTACKLEVEL; 1332 dSTACKLEVEL;
1330 1333
1331 /* sometimes transfer is only called to set idle_sp */ 1334 /* sometimes transfer is only called to set idle_sp */
1332 if (expect_false (!next)) 1335 if (expect_false (!next))
1333 { 1336 {
1334 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1337 ((coro_cctx *)prev)->idle_sp = stacklevel;
1335 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1338 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1336 } 1339 }
1337 else if (expect_true (prev != next)) 1340 else if (expect_true (prev != next))
1338 { 1341 {
1339 coro_cctx *prev__cctx; 1342 coro_cctx *prev__cctx;
1366 1369
1367 prev__cctx = prev->cctx; 1370 prev__cctx = prev->cctx;
1368 1371
1369 /* possibly untie and reuse the cctx */ 1372 /* possibly untie and reuse the cctx */
1370 if (expect_true ( 1373 if (expect_true (
1371 prev__cctx->idle_sp == STACKLEVEL 1374 prev__cctx->idle_sp == stacklevel
1372 && !(prev__cctx->flags & CC_TRACE) 1375 && !(prev__cctx->flags & CC_TRACE)
1373 && !force_cctx 1376 && !force_cctx
1374 )) 1377 ))
1375 { 1378 {
1376 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1379 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1377 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); 1380 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te));
1378 1381
1379 prev->cctx = 0; 1382 prev->cctx = 0;
1380 1383
1381 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1384 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1655{ 1658{
1656 api_ready (coro_current); 1659 api_ready (coro_current);
1657 prepare_schedule (aTHX_ ta); 1660 prepare_schedule (aTHX_ ta);
1658} 1661}
1659 1662
1660static int 1663static void
1661prepare_cede_notself (pTHX_ struct transfer_args *ta) 1664prepare_cede_notself (pTHX_ struct transfer_args *ta)
1662{ 1665{
1666 SV *prev = SvRV (coro_current);
1667
1663 if (coro_nready) 1668 if (coro_nready)
1664 { 1669 {
1665 SV *prev = SvRV (coro_current);
1666 prepare_schedule (aTHX_ ta); 1670 prepare_schedule (aTHX_ ta);
1667 api_ready (prev); 1671 api_ready (prev);
1672 }
1673 else
1674 ta->prev = ta->next = SvSTATE (prev);
1675}
1676
1677static void
1678api_schedule (void)
1679{
1680 dTHX;
1681 struct transfer_args ta;
1682
1683 prepare_schedule (aTHX_ &ta);
1684 TRANSFER (ta, 1);
1685}
1686
1687static int
1688api_cede (void)
1689{
1690 dTHX;
1691 struct transfer_args ta;
1692
1693 prepare_cede (aTHX_ &ta);
1694
1695 if (expect_true (ta.prev != ta.next))
1696 {
1697 TRANSFER (ta, 1);
1668 return 1; 1698 return 1;
1669 } 1699 }
1670 else 1700 else
1671 return 0; 1701 return 0;
1672} 1702}
1673 1703
1674static void
1675api_schedule (void)
1676{
1677 dTHX;
1678 struct transfer_args ta;
1679
1680 prepare_schedule (aTHX_ &ta);
1681 TRANSFER (ta, 1);
1682}
1683
1684static int
1685api_cede (void)
1686{
1687 dTHX;
1688 struct transfer_args ta;
1689
1690 prepare_cede (aTHX_ &ta);
1691
1692 if (expect_true (ta.prev != ta.next))
1693 {
1694 TRANSFER (ta, 1);
1695 return 1;
1696 }
1697 else
1698 return 0;
1699}
1700
1701static int 1704static int
1702api_cede_notself (void) 1705api_cede_notself (void)
1703{ 1706{
1707 if (coro_nready)
1708 {
1704 dTHX; 1709 dTHX;
1705 struct transfer_args ta; 1710 struct transfer_args ta;
1706 1711
1707 if (prepare_cede_notself (aTHX_ &ta)) 1712 prepare_cede_notself (aTHX_ &ta);
1708 {
1709 TRANSFER (ta, 1); 1713 TRANSFER (ta, 1);
1710 return 1; 1714 return 1;
1711 } 1715 }
1712 else 1716 else
1713 return 0; 1717 return 0;
1856 PUSHs ((SV *)CvGV (ssl_cv)); 1860 PUSHs ((SV *)CvGV (ssl_cv));
1857 1861
1858 RETURNOP (ssl_restore.op_first); 1862 RETURNOP (ssl_restore.op_first);
1859} 1863}
1860 1864
1865#define OPpENTERSUB_SSL 15 /* the part of op_private entersub hopefully doesn't use */
1866
1861/* declare prototype */ 1867/* declare prototype */
1862XS(XS_Coro__State__set_stacklevel); 1868XS(XS_Coro__State__set_stacklevel);
1863 1869
1864#define OPpENTERSUB_SSL 15 1870/*
1865 1871 * these not obviously related functions are all rolled into one
1872 * function to increase chances that they all will call transfer with the same
1873 * stack offset
1874 */
1866static OP * 1875static OP *
1867pp_set_stacklevel (pTHX) 1876pp_set_stacklevel (pTHX)
1868{ 1877{
1869 dSP; 1878 dSP;
1870 struct transfer_args ta; 1879 struct transfer_args ta;
1908 case 3: 1917 case 3:
1909 prepare_cede (aTHX_ &ta); 1918 prepare_cede (aTHX_ &ta);
1910 break; 1919 break;
1911 1920
1912 case 4: 1921 case 4:
1913 if (!prepare_cede_notself (aTHX_ &ta)) 1922 prepare_cede_notself (aTHX_ &ta);
1914 goto skip;
1915
1916 break; 1923 break;
1917 } 1924 }
1918 1925
1919 TRANSFER (ta, 0); 1926 TRANSFER (ta, 0);
1920 SPAGAIN; 1927 SPAGAIN;
1924 SSL_TAIL; 1931 SSL_TAIL;
1925 SPAGAIN; 1932 SPAGAIN;
1926 RETURN; 1933 RETURN;
1927} 1934}
1928 1935
1936static void
1937coro_ssl_patch (pTHX_ CV *cv, int ix, SV **args, int items)
1938{
1939 assert (("FATAL: ssl call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_set_stacklevel));
1940
1941 assert (("FATAL: ssl call with illegal CV value", CvGV (cv)));
1942 ssl_cv = cv;
1943
1944 /* we patch the op, and then re-run the whole call */
1945 /* we have to put some dummy argument on the stack for this to work */
1946 ssl_restore.op_next = (OP *)&ssl_restore;
1947 ssl_restore.op_type = OP_NULL;
1948 ssl_restore.op_ppaddr = pp_restore;
1949 ssl_restore.op_first = PL_op;
1950
1951 ssl_arg0 = items > 0 ? SvREFCNT_inc (args [0]) : 0;
1952 ssl_arg1 = items > 1 ? SvREFCNT_inc (args [1]) : 0;
1953
1954 PL_op->op_ppaddr = pp_set_stacklevel;
1955 PL_op->op_private = PL_op->op_private & ~OPpENTERSUB_SSL | ix; /* we potentially share our private flags with entersub */
1956
1957 PL_op = (OP *)&ssl_restore;
1958}
1959
1929MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1960MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1930 1961
1931PROTOTYPES: DISABLE 1962PROTOTYPES: DISABLE
1932 1963
1933# these not obviously related functions are all rolled into the same xs 1964BOOT:
1934# function to increase chances that they all will call transfer with the same 1965{
1935# stack offset 1966#ifdef USE_ITHREADS
1967 MUTEX_INIT (&coro_lock);
1968# if CORO_PTHREAD
1969 coro_thx = PERL_GET_CONTEXT;
1970# endif
1971#endif
1972 BOOT_PAGESIZE;
1973
1974 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1975 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1976
1977 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1978 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1979 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr;
1980
1981 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1982 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1983 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1984
1985 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1986
1987 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1988 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1989 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1990 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1991
1992 main_mainstack = PL_mainstack;
1993 main_top_env = PL_top_env;
1994
1995 while (main_top_env->je_prev)
1996 main_top_env = main_top_env->je_prev;
1997
1998 coroapi.ver = CORO_API_VERSION;
1999 coroapi.rev = CORO_API_REVISION;
2000 coroapi.transfer = api_transfer;
2001
2002 {
2003 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
2004
2005 if (!svp) croak ("Time::HiRes is required");
2006 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
2007
2008 nvtime = INT2PTR (double (*)(), SvIV (*svp));
2009 }
2010
2011 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
2012}
2013
2014SV *
2015new (char *klass, ...)
2016 CODE:
2017{
2018 struct coro *coro;
2019 MAGIC *mg;
2020 HV *hv;
2021 int i;
2022
2023 Newz (0, coro, 1, struct coro);
2024 coro->args = newAV ();
2025 coro->flags = CF_NEW;
2026
2027 if (coro_first) coro_first->prev = coro;
2028 coro->next = coro_first;
2029 coro_first = coro;
2030
2031 coro->hv = hv = newHV ();
2032 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
2033 mg->mg_flags |= MGf_DUP;
2034 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2035
2036 av_extend (coro->args, items - 1);
2037 for (i = 1; i < items; i++)
2038 av_push (coro->args, newSVsv (ST (i)));
2039}
2040 OUTPUT:
2041 RETVAL
2042
1936void 2043void
1937_set_stacklevel (...) 2044_set_stacklevel (...)
1938 ALIAS: 2045 ALIAS:
1939 Coro::State::transfer = 1 2046 Coro::State::transfer = 1
1940 Coro::schedule = 2 2047 Coro::schedule = 2
1941 Coro::cede = 3 2048 Coro::cede = 3
1942 Coro::cede_notself = 4 2049 Coro::cede_notself = 4
1943 CODE: 2050 CODE:
1944{ 2051 coro_ssl_patch (aTHX_ cv, ix, &ST (0), items);
1945 assert (("FATAL: ssl call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_set_stacklevel));
1946
1947 assert (("FATAL: ssl call with illegal CV value", CvGV (cv)));
1948 ssl_cv = cv;
1949
1950 /* we patch the op, and then re-run the whole call */
1951 /* we have to put some dummy argument on the stack for this to work */
1952 ssl_restore.op_next = (OP *)&ssl_restore;
1953 ssl_restore.op_type = OP_NULL;
1954 ssl_restore.op_ppaddr = pp_restore;
1955 ssl_restore.op_first = PL_op;
1956
1957 ssl_arg0 = items > 0 ? SvREFCNT_inc (ST (0)) : 0;
1958 ssl_arg1 = items > 1 ? SvREFCNT_inc (ST (1)) : 0;
1959
1960 PL_op->op_ppaddr = pp_set_stacklevel;
1961 PL_op->op_private = PL_op->op_private & ~OPpENTERSUB_SSL | ix; /* we potentially share our private flags with entersub */
1962
1963 PL_op = (OP *)&ssl_restore;
1964}
1965
1966BOOT:
1967{
1968#ifdef USE_ITHREADS
1969 MUTEX_INIT (&coro_lock);
1970# if CORO_PTHREAD
1971 coro_thx = PERL_GET_CONTEXT;
1972# endif
1973#endif
1974 BOOT_PAGESIZE;
1975
1976 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1977 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1978
1979 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1980 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1981 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr;
1982
1983 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1984 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1985 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1986
1987 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1988
1989 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1990 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1991 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1992 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1993
1994 main_mainstack = PL_mainstack;
1995 main_top_env = PL_top_env;
1996
1997 while (main_top_env->je_prev)
1998 main_top_env = main_top_env->je_prev;
1999
2000 coroapi.ver = CORO_API_VERSION;
2001 coroapi.rev = CORO_API_REVISION;
2002 coroapi.transfer = api_transfer;
2003
2004 {
2005 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
2006
2007 if (!svp) croak ("Time::HiRes is required");
2008 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
2009
2010 nvtime = INT2PTR (double (*)(), SvIV (*svp));
2011 }
2012
2013 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
2014}
2015
2016SV *
2017new (char *klass, ...)
2018 CODE:
2019{
2020 struct coro *coro;
2021 MAGIC *mg;
2022 HV *hv;
2023 int i;
2024
2025 Newz (0, coro, 1, struct coro);
2026 coro->args = newAV ();
2027 coro->flags = CF_NEW;
2028
2029 if (coro_first) coro_first->prev = coro;
2030 coro->next = coro_first;
2031 coro_first = coro;
2032
2033 coro->hv = hv = newHV ();
2034 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
2035 mg->mg_flags |= MGf_DUP;
2036 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2037
2038 av_extend (coro->args, items - 1);
2039 for (i = 1; i < items; i++)
2040 av_push (coro->args, newSVsv (ST (i)));
2041}
2042 OUTPUT:
2043 RETVAL
2044 2052
2045bool 2053bool
2046_destroy (SV *coro_sv) 2054_destroy (SV *coro_sv)
2047 CODE: 2055 CODE:
2048 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 2056 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines