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.267 by root, Fri Nov 14 06:29:52 2008 UTC

715 715
716/** set stacklevel support **************************************************/ 716/** set stacklevel support **************************************************/
717 717
718/* we sometimes need to create the effect of pp_set_stacklevel calling us */ 718/* we sometimes need to create the effect of pp_set_stacklevel calling us */
719#define SSL_HEAD (void)0 719#define SSL_HEAD (void)0
720/* we somtimes need to create the effect of leaving via pp_set_stacklevel */ 720/* we sometimes need to create the effect of leaving via pp_set_stacklevel */
721#define SSL_TAIL set_stacklevel_tail (aTHX) 721#define SSL_TAIL set_stacklevel_tail (aTHX)
722 722
723INLINE void 723INLINE void
724set_stacklevel_tail (pTHX) 724set_stacklevel_tail (pTHX)
725{ 725{
1655{ 1655{
1656 api_ready (coro_current); 1656 api_ready (coro_current);
1657 prepare_schedule (aTHX_ ta); 1657 prepare_schedule (aTHX_ ta);
1658} 1658}
1659 1659
1660static int 1660static void
1661prepare_cede_notself (pTHX_ struct transfer_args *ta) 1661prepare_cede_notself (pTHX_ struct transfer_args *ta)
1662{ 1662{
1663 SV *prev = SvRV (coro_current);
1664
1663 if (coro_nready) 1665 if (coro_nready)
1664 { 1666 {
1665 SV *prev = SvRV (coro_current);
1666 prepare_schedule (aTHX_ ta); 1667 prepare_schedule (aTHX_ ta);
1667 api_ready (prev); 1668 api_ready (prev);
1669 }
1670 else
1671 ta->prev = ta->next = SvSTATE (prev);
1672}
1673
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);
1668 return 1; 1695 return 1;
1669 } 1696 }
1670 else 1697 else
1671 return 0; 1698 return 0;
1672} 1699}
1673 1700
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 1701static int
1702api_cede_notself (void) 1702api_cede_notself (void)
1703{ 1703{
1704 if (coro_nready)
1705 {
1704 dTHX; 1706 dTHX;
1705 struct transfer_args ta; 1707 struct transfer_args ta;
1706 1708
1707 if (prepare_cede_notself (aTHX_ &ta)) 1709 prepare_cede_notself (aTHX_ &ta);
1708 {
1709 TRANSFER (ta, 1); 1710 TRANSFER (ta, 1);
1710 return 1; 1711 return 1;
1711 } 1712 }
1712 else 1713 else
1713 return 0; 1714 return 0;
1856 PUSHs ((SV *)CvGV (ssl_cv)); 1857 PUSHs ((SV *)CvGV (ssl_cv));
1857 1858
1858 RETURNOP (ssl_restore.op_first); 1859 RETURNOP (ssl_restore.op_first);
1859} 1860}
1860 1861
1862#define OPpENTERSUB_SSL 15 /* the part of op_private entersub hopefully doesn't use */
1863
1861/* declare prototype */ 1864/* declare prototype */
1862XS(XS_Coro__State__set_stacklevel); 1865XS(XS_Coro__State__set_stacklevel);
1863 1866
1864#define OPpENTERSUB_SSL 15 1867/*
1865 1868 * these not obviously related functions are all rolled into one
1869 * function to increase chances that they all will call transfer with the same
1870 * stack offset
1871 */
1866static OP * 1872static OP *
1867pp_set_stacklevel (pTHX) 1873pp_set_stacklevel (pTHX)
1868{ 1874{
1869 dSP; 1875 dSP;
1870 struct transfer_args ta; 1876 struct transfer_args ta;
1908 case 3: 1914 case 3:
1909 prepare_cede (aTHX_ &ta); 1915 prepare_cede (aTHX_ &ta);
1910 break; 1916 break;
1911 1917
1912 case 4: 1918 case 4:
1913 if (!prepare_cede_notself (aTHX_ &ta)) 1919 prepare_cede_notself (aTHX_ &ta);
1914 goto skip;
1915
1916 break; 1920 break;
1917 } 1921 }
1918 1922
1919 TRANSFER (ta, 0); 1923 TRANSFER (ta, 0);
1920 SPAGAIN; 1924 SPAGAIN;
1924 SSL_TAIL; 1928 SSL_TAIL;
1925 SPAGAIN; 1929 SPAGAIN;
1926 RETURN; 1930 RETURN;
1927} 1931}
1928 1932
1933static void
1934coro_ssl_patch (pTHX_ CV *cv, int ix, SV **args, int items)
1935{
1936 assert (("FATAL: ssl call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_set_stacklevel));
1937
1938 assert (("FATAL: ssl call with illegal CV value", CvGV (cv)));
1939 ssl_cv = cv;
1940
1941 /* we patch the op, and then re-run the whole call */
1942 /* we have to put some dummy argument on the stack for this to work */
1943 ssl_restore.op_next = (OP *)&ssl_restore;
1944 ssl_restore.op_type = OP_NULL;
1945 ssl_restore.op_ppaddr = pp_restore;
1946 ssl_restore.op_first = PL_op;
1947
1948 ssl_arg0 = items > 0 ? SvREFCNT_inc (args [0]) : 0;
1949 ssl_arg1 = items > 1 ? SvREFCNT_inc (args [1]) : 0;
1950
1951 PL_op->op_ppaddr = pp_set_stacklevel;
1952 PL_op->op_private = PL_op->op_private & ~OPpENTERSUB_SSL | ix; /* we potentially share our private flags with entersub */
1953
1954 PL_op = (OP *)&ssl_restore;
1955}
1956
1929MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1957MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1930 1958
1931PROTOTYPES: DISABLE 1959PROTOTYPES: DISABLE
1932 1960
1933# these not obviously related functions are all rolled into the same xs 1961BOOT:
1934# function to increase chances that they all will call transfer with the same 1962{
1935# stack offset 1963#ifdef USE_ITHREADS
1964 MUTEX_INIT (&coro_lock);
1965# if CORO_PTHREAD
1966 coro_thx = PERL_GET_CONTEXT;
1967# endif
1968#endif
1969 BOOT_PAGESIZE;
1970
1971 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1972 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1973
1974 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1975 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1976 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr;
1977
1978 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1979 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1980 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1981
1982 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1983
1984 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1985 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1986 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1987 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1988
1989 main_mainstack = PL_mainstack;
1990 main_top_env = PL_top_env;
1991
1992 while (main_top_env->je_prev)
1993 main_top_env = main_top_env->je_prev;
1994
1995 coroapi.ver = CORO_API_VERSION;
1996 coroapi.rev = CORO_API_REVISION;
1997 coroapi.transfer = api_transfer;
1998
1999 {
2000 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
2001
2002 if (!svp) croak ("Time::HiRes is required");
2003 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
2004
2005 nvtime = INT2PTR (double (*)(), SvIV (*svp));
2006 }
2007
2008 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
2009}
2010
2011SV *
2012new (char *klass, ...)
2013 CODE:
2014{
2015 struct coro *coro;
2016 MAGIC *mg;
2017 HV *hv;
2018 int i;
2019
2020 Newz (0, coro, 1, struct coro);
2021 coro->args = newAV ();
2022 coro->flags = CF_NEW;
2023
2024 if (coro_first) coro_first->prev = coro;
2025 coro->next = coro_first;
2026 coro_first = coro;
2027
2028 coro->hv = hv = newHV ();
2029 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
2030 mg->mg_flags |= MGf_DUP;
2031 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2032
2033 av_extend (coro->args, items - 1);
2034 for (i = 1; i < items; i++)
2035 av_push (coro->args, newSVsv (ST (i)));
2036}
2037 OUTPUT:
2038 RETVAL
2039
1936void 2040void
1937_set_stacklevel (...) 2041_set_stacklevel (...)
1938 ALIAS: 2042 ALIAS:
1939 Coro::State::transfer = 1 2043 Coro::State::transfer = 1
1940 Coro::schedule = 2 2044 Coro::schedule = 2
1941 Coro::cede = 3 2045 Coro::cede = 3
1942 Coro::cede_notself = 4 2046 Coro::cede_notself = 4
1943 CODE: 2047 CODE:
1944{ 2048 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 2049
2045bool 2050bool
2046_destroy (SV *coro_sv) 2051_destroy (SV *coro_sv)
2047 CODE: 2052 CODE:
2048 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 2053 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines