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.264 by root, Fri Nov 14 02:29:09 2008 UTC vs.
Revision 1.267 by root, Fri Nov 14 06:29:52 2008 UTC

711 } 711 }
712 712
713 return rss; 713 return rss;
714} 714}
715 715
716/** set stacklevel support **************************************************/
717
718/* we sometimes need to create the effect of pp_set_stacklevel calling us */
719#define SSL_HEAD (void)0
720/* we sometimes need to create the effect of leaving via pp_set_stacklevel */
721#define SSL_TAIL set_stacklevel_tail (aTHX)
722
723INLINE void
724set_stacklevel_tail (pTHX)
725{
726 dSP;
727 SV **bot = SP;
728
729 int gimme = GIMME_V;
730
731 /* make sure we put something on the stack in scalar context */
732 if (gimme == G_SCALAR)
733 {
734 if (sp == bot)
735 XPUSHs (&PL_sv_undef);
736
737 SP = bot + 1;
738 }
739
740 PUTBACK;
741}
742
716/** coroutine stack handling ************************************************/ 743/** coroutine stack handling ************************************************/
717 744
718static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); 745static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
719static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); 746static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg);
720static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg); 747static int (*orig_sigelem_clr) (pTHX_ SV *sv, MAGIC *mg);
723#ifndef MgPV_nolen_const 750#ifndef MgPV_nolen_const
724#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ 751#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \
725 SvPV_nolen((SV*)((mg)->mg_ptr)) : \ 752 SvPV_nolen((SV*)((mg)->mg_ptr)) : \
726 (const char*)(mg)->mg_ptr) 753 (const char*)(mg)->mg_ptr)
727#endif 754#endif
728
729/* we sometimes need to create the effect of entersub calling us */
730#define SSL_HEAD (void)0
731/* we somtimes need to create the effect of leaving via entersub */
732#define SSL_TAIL (void)0
733 755
734/* 756/*
735 * This overrides the default magic get method of %SIG elements. 757 * This overrides the default magic get method of %SIG elements.
736 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 758 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
737 * and instead of tryign to save and restore the hash elements, we just provide 759 * and instead of tryign to save and restore the hash elements, we just provide
857 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 879 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
858 SPAGAIN; 880 SPAGAIN;
859 } 881 }
860 882
861 /* this newly created coroutine might be run on an existing cctx which most 883 /* this newly created coroutine might be run on an existing cctx which most
862 * likely was suspended in set_stacklevel, called from entersub. 884 * likely was suspended in set_stacklevel, called from pp_set_stacklevel,
863 * set_stacklevel doesn't do anything on return, but entersub does LEAVE, 885 * so we have to emulate entering pp_set_stacklevel here.
864 * so we ENTER here for symmetry.
865 */ 886 */
866 SSL_HEAD; 887 SSL_HEAD;
867} 888}
868 889
869static void 890static void
1105# endif 1126# endif
1106#endif 1127#endif
1107 { 1128 {
1108 dTHX; 1129 dTHX;
1109 1130
1110 /* entersub called ENTER, but we never 'returned', undo that here */ 1131 /* we are the alternative tail to pp_set_stacklevel */
1132 /* so do the same things here */
1111 SSL_TAIL; 1133 SSL_TAIL;
1112 1134
1113 /* we now skip the op that did lead to transfer() */ 1135 /* we now skip the op that did lead to transfer() */
1114 PL_op = PL_op->op_next; 1136 PL_op = PL_op->op_next;
1115 1137
1633{ 1655{
1634 api_ready (coro_current); 1656 api_ready (coro_current);
1635 prepare_schedule (aTHX_ ta); 1657 prepare_schedule (aTHX_ ta);
1636} 1658}
1637 1659
1638static int 1660static void
1639prepare_cede_notself (pTHX_ struct transfer_args *ta) 1661prepare_cede_notself (pTHX_ struct transfer_args *ta)
1640{ 1662{
1663 SV *prev = SvRV (coro_current);
1664
1641 if (coro_nready) 1665 if (coro_nready)
1642 { 1666 {
1643 SV *prev = SvRV (coro_current);
1644 prepare_schedule (aTHX_ ta); 1667 prepare_schedule (aTHX_ ta);
1645 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);
1646 return 1; 1695 return 1;
1647 } 1696 }
1648 else 1697 else
1649 return 0; 1698 return 0;
1650} 1699}
1651 1700
1652static void
1653api_schedule (void)
1654{
1655 dTHX;
1656 struct transfer_args ta;
1657
1658 prepare_schedule (aTHX_ &ta);
1659 TRANSFER (ta, 1);
1660}
1661
1662static int
1663api_cede (void)
1664{
1665 dTHX;
1666 struct transfer_args ta;
1667
1668 prepare_cede (aTHX_ &ta);
1669
1670 if (expect_true (ta.prev != ta.next))
1671 {
1672 TRANSFER (ta, 1);
1673 return 1;
1674 }
1675 else
1676 return 0;
1677}
1678
1679static int 1701static int
1680api_cede_notself (void) 1702api_cede_notself (void)
1681{ 1703{
1704 if (coro_nready)
1705 {
1682 dTHX; 1706 dTHX;
1683 struct transfer_args ta; 1707 struct transfer_args ta;
1684 1708
1685 if (prepare_cede_notself (aTHX_ &ta)) 1709 prepare_cede_notself (aTHX_ &ta);
1686 {
1687 TRANSFER (ta, 1); 1710 TRANSFER (ta, 1);
1688 return 1; 1711 return 1;
1689 } 1712 }
1690 else 1713 else
1691 return 0; 1714 return 0;
1834 PUSHs ((SV *)CvGV (ssl_cv)); 1857 PUSHs ((SV *)CvGV (ssl_cv));
1835 1858
1836 RETURNOP (ssl_restore.op_first); 1859 RETURNOP (ssl_restore.op_first);
1837} 1860}
1838 1861
1862#define OPpENTERSUB_SSL 15 /* the part of op_private entersub hopefully doesn't use */
1863
1839/* declare prototype */ 1864/* declare prototype */
1840XS(XS_Coro__State__set_stacklevel); 1865XS(XS_Coro__State__set_stacklevel);
1841 1866
1867/*
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 */
1842static OP * 1872static OP *
1843pp_set_stacklevel (pTHX) 1873pp_set_stacklevel (pTHX)
1844{ 1874{
1845 dSP; 1875 dSP;
1846 struct transfer_args ta; 1876 struct transfer_args ta;
1862 arg = AvARRAY (av); 1892 arg = AvARRAY (av);
1863 items = AvFILLp (av) + 1; 1893 items = AvFILLp (av) + 1;
1864 } 1894 }
1865 1895
1866 PUTBACK; 1896 PUTBACK;
1867 switch (PL_op->op_private & 7) 1897 switch (PL_op->op_private & OPpENTERSUB_SSL)
1868 { 1898 {
1869 case 0: 1899 case 0:
1870 prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (arg [0])); 1900 prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (arg [0]));
1871 break; 1901 break;
1872 1902
1884 case 3: 1914 case 3:
1885 prepare_cede (aTHX_ &ta); 1915 prepare_cede (aTHX_ &ta);
1886 break; 1916 break;
1887 1917
1888 case 4: 1918 case 4:
1889 if (!prepare_cede_notself (aTHX_ &ta)) 1919 prepare_cede_notself (aTHX_ &ta);
1890 RETURN;
1891
1892 break; 1920 break;
1893 } 1921 }
1894 1922
1895 TRANSFER (ta, 0); 1923 TRANSFER (ta, 0);
1896 SPAGAIN; 1924 SPAGAIN;
1897 1925
1898skip: 1926skip:
1899 1927 PUTBACK;
1928 SSL_TAIL;
1929 SPAGAIN;
1900 RETURN; 1930 RETURN;
1901} 1931}
1902 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
1903MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1957MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1904 1958
1905PROTOTYPES: DISABLE 1959PROTOTYPES: DISABLE
1906 1960
1907# these not obviously related functions are all rolled into the same xs 1961BOOT:
1908# function to increase chances that they all will call transfer with the same 1962{
1909# 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
1910void 2040void
1911_set_stacklevel (...) 2041_set_stacklevel (...)
1912 ALIAS: 2042 ALIAS:
1913 Coro::State::transfer = 1 2043 Coro::State::transfer = 1
1914 Coro::schedule = 2 2044 Coro::schedule = 2
1915 Coro::cede = 3 2045 Coro::cede = 3
1916 Coro::cede_notself = 4 2046 Coro::cede_notself = 4
1917 CODE: 2047 CODE:
1918{ 2048 coro_ssl_patch (aTHX_ cv, ix, &ST (0), items);
1919 assert (("FATAL: ssl call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_set_stacklevel));
1920
1921 /* we patch the op, and then re-run the whole call */
1922 /* we have to put some dummy argument on the stack for this to work */
1923 /* TODO: walk back the opcode chain (but how?), nuke the pp_gv etc. */
1924 ssl_restore.op_next = (OP *)&ssl_restore;
1925 ssl_restore.op_type = OP_NULL;
1926 ssl_restore.op_ppaddr = pp_restore;
1927 ssl_restore.op_first = PL_op;
1928
1929 ssl_arg0 = items > 0 ? SvREFCNT_inc (ST (0)) : 0;
1930 ssl_arg1 = items > 1 ? SvREFCNT_inc (ST (1)) : 0;
1931
1932 PL_op->op_ppaddr = pp_set_stacklevel;
1933 PL_op->op_private = PL_op->op_private & ~7 | ix; /* we potentially share our private flags with entersub */
1934
1935 PL_op = (OP *)&ssl_restore;
1936}
1937
1938BOOT:
1939{
1940#ifdef USE_ITHREADS
1941 MUTEX_INIT (&coro_lock);
1942# if CORO_PTHREAD
1943 coro_thx = PERL_GET_CONTEXT;
1944# endif
1945#endif
1946 BOOT_PAGESIZE;
1947
1948 ssl_cv = get_cv ("Coro::State::_set_stacklevel", 0);
1949
1950 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1951 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1952
1953 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1954 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1955 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr;
1956
1957 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1958 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1959 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1960
1961 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1962
1963 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1964 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1965 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1966 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1967
1968 main_mainstack = PL_mainstack;
1969 main_top_env = PL_top_env;
1970
1971 while (main_top_env->je_prev)
1972 main_top_env = main_top_env->je_prev;
1973
1974 coroapi.ver = CORO_API_VERSION;
1975 coroapi.rev = CORO_API_REVISION;
1976 coroapi.transfer = api_transfer;
1977
1978 {
1979 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1980
1981 if (!svp) croak ("Time::HiRes is required");
1982 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1983
1984 nvtime = INT2PTR (double (*)(), SvIV (*svp));
1985 }
1986
1987 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1988}
1989
1990SV *
1991new (char *klass, ...)
1992 CODE:
1993{
1994 struct coro *coro;
1995 MAGIC *mg;
1996 HV *hv;
1997 int i;
1998
1999 Newz (0, coro, 1, struct coro);
2000 coro->args = newAV ();
2001 coro->flags = CF_NEW;
2002
2003 if (coro_first) coro_first->prev = coro;
2004 coro->next = coro_first;
2005 coro_first = coro;
2006
2007 coro->hv = hv = newHV ();
2008 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
2009 mg->mg_flags |= MGf_DUP;
2010 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2011
2012 av_extend (coro->args, items - 1);
2013 for (i = 1; i < items; i++)
2014 av_push (coro->args, newSVsv (ST (i)));
2015}
2016 OUTPUT:
2017 RETVAL
2018 2049
2019bool 2050bool
2020_destroy (SV *coro_sv) 2051_destroy (SV *coro_sv)
2021 CODE: 2052 CODE:
2022 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