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.271 by root, Fri Nov 14 07:35:32 2008 UTC vs.
Revision 1.272 by root, Fri Nov 14 20:35:49 2008 UTC

183static JMPENV *main_top_env; 183static JMPENV *main_top_env;
184static HV *coro_state_stash, *coro_stash; 184static HV *coro_state_stash, *coro_stash;
185static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 185static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
186static volatile struct coro *transfer_next; 186static volatile struct coro *transfer_next;
187 187
188struct transfer_args
189{
190 struct coro *prev, *next;
191};
192
193static GV *irsgv; /* $/ */ 188static GV *irsgv; /* $/ */
194static GV *stdoutgv; /* *STDOUT */ 189static GV *stdoutgv; /* *STDOUT */
195static SV *rv_diehook; 190static SV *rv_diehook;
196static SV *rv_warnhook; 191static SV *rv_warnhook;
197static HV *hv_sig; /* %SIG */ 192static HV *hv_sig; /* %SIG */
215 CC_TRACE_LINE = 0x10, /* trace each statement */ 210 CC_TRACE_LINE = 0x10, /* trace each statement */
216 CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE, 211 CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE,
217}; 212};
218 213
219/* this is a structure representing a c-level coroutine */ 214/* this is a structure representing a c-level coroutine */
220typedef struct coro_cctx { 215typedef struct coro_cctx
216{
221 struct coro_cctx *next; 217 struct coro_cctx *next;
222 218
223 /* the stack */ 219 /* the stack */
224 void *sptr; 220 void *sptr;
225 size_t ssize; 221 size_t ssize;
243 CF_NEW = 0x0004, /* has never been switched to */ 239 CF_NEW = 0x0004, /* has never been switched to */
244 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 240 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
245}; 241};
246 242
247/* the structure where most of the perl state is stored, overlaid on the cxstack */ 243/* the structure where most of the perl state is stored, overlaid on the cxstack */
248typedef struct { 244typedef struct
245{
249 SV *defsv; 246 SV *defsv;
250 AV *defav; 247 AV *defav;
251 SV *errsv; 248 SV *errsv;
252 SV *irsgv; 249 SV *irsgv;
253#define VAR(name,type) type name; 250#define VAR(name,type) type name;
255#undef VAR 252#undef VAR
256} perl_slots; 253} perl_slots;
257 254
258#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) 255#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT))
259 256
257/* this is the per-perl-coro slf frame info */
258/* it is treated like other "global" interpreter data */
259/* and unfortunately is copied around, so kepe it small */
260struct slf_frame
261{
262 void (*prepare) (struct coro_transfer_args *ta); /* 0 means not yet initialised */
263 int (*check) (pTHX);
264};
265
260/* this is a structure representing a perl-level coroutine */ 266/* this is a structure representing a perl-level coroutine */
261struct coro { 267struct coro {
262 /* the c coroutine allocated to this perl coroutine, if any */ 268 /* the C coroutine allocated to this perl coroutine, if any */
263 coro_cctx *cctx; 269 coro_cctx *cctx;
264 270
265 /* process data */ 271 /* process data */
272 struct slf_frame slf_frame; /* saved slf frame */
273 void *slf_data;
266 AV *mainstack; 274 AV *mainstack;
267 perl_slots *slot; /* basically the saved sp */ 275 perl_slots *slot; /* basically the saved sp */
268 276
269 AV *args; /* data associated with this coroutine (initial args) */ 277 AV *args; /* data associated with this coroutine (initial args) */
270 int refcnt; /* coroutines are refcounted, yes */ 278 int refcnt; /* coroutines are refcounted, yes */
285 struct coro *next, *prev; 293 struct coro *next, *prev;
286}; 294};
287 295
288typedef struct coro *Coro__State; 296typedef struct coro *Coro__State;
289typedef struct coro *Coro__State_or_hashref; 297typedef struct coro *Coro__State_or_hashref;
298
299static struct slf_frame slf_frame; /* the current slf frame */
290 300
291/** Coro ********************************************************************/ 301/** Coro ********************************************************************/
292 302
293#define PRIO_MAX 3 303#define PRIO_MAX 3
294#define PRIO_HIGH 1 304#define PRIO_HIGH 1
514 CvPADLIST (cv) = (AV *)POPs; 524 CvPADLIST (cv) = (AV *)POPs;
515 } 525 }
516 526
517 PUTBACK; 527 PUTBACK;
518 } 528 }
529
530 slf_frame = c->slf_frame;
531 coroapi.slf_data = c->slf_data;
519} 532}
520 533
521static void 534static void
522save_perl (pTHX_ Coro__State c) 535save_perl (pTHX_ Coro__State c)
523{ 536{
537 c->slf_data = coroapi.slf_data;
538 c->slf_frame = slf_frame;
539
524 { 540 {
525 dSP; 541 dSP;
526 I32 cxix = cxstack_ix; 542 I32 cxix = cxstack_ix;
527 PERL_CONTEXT *ccstk = cxstack; 543 PERL_CONTEXT *ccstk = cxstack;
528 PERL_SI *top_si = PL_curstackinfo; 544 PERL_SI *top_si = PL_curstackinfo;
595 #undef VAR 611 #undef VAR
596 } 612 }
597} 613}
598 614
599/* 615/*
600 * allocate various perl stacks. This is an exact copy 616 * allocate various perl stacks. This is almost an exact copy
601 * of perl.c:init_stacks, except that it uses less memory 617 * of perl.c:init_stacks, except that it uses less memory
602 * on the (sometimes correct) assumption that coroutines do 618 * on the (sometimes correct) assumption that coroutines do
603 * not usually need a lot of stackspace. 619 * not usually need a lot of stackspace.
604 */ 620 */
605#if CORO_PREFER_PERL_FUNCTIONS 621#if CORO_PREFER_PERL_FUNCTIONS
712#endif 728#endif
713 } 729 }
714 } 730 }
715 731
716 return rss; 732 return rss;
717}
718
719/** set stacklevel support **************************************************/
720
721/* we sometimes need to create the effect of pp_slf calling us */
722#define SLF_HEAD (void)0
723/* we sometimes need to create the effect of leaving via pp_slf */
724#define SLF_TAIL slf_tail (aTHX)
725
726INLINE void
727slf_tail (pTHX)
728{
729 dSP;
730 SV **bot = SP;
731
732 int gimme = GIMME_V;
733
734 /* make sure we put something on the stack in scalar context */
735 if (gimme == G_SCALAR)
736 {
737 if (sp == bot)
738 XPUSHs (&PL_sv_undef);
739
740 SP = bot + 1;
741 }
742
743 PUTBACK;
744} 733}
745 734
746/** coroutine stack handling ************************************************/ 735/** coroutine stack handling ************************************************/
747 736
748static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); 737static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
834 823
835 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; 824 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0;
836} 825}
837 826
838static void 827static void
828prepare_nop (aTHX_ struct coro_transfer_args *ta)
829{
830 /* kind of mega-hacky, but works */
831 ta->next = ta->prev = (struct coro *)ta;
832}
833
834static int
835slf_check_nop (aTHX)
836{
837 return 0;
838}
839
840static void
839coro_setup (pTHX_ struct coro *coro) 841coro_setup (pTHX_ struct coro *coro)
840{ 842{
841 /* 843 /*
842 * emulate part of the perl startup here. 844 * emulate part of the perl startup here.
843 */ 845 */
882 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 884 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
883 SPAGAIN; 885 SPAGAIN;
884 } 886 }
885 887
886 /* this newly created coroutine might be run on an existing cctx which most 888 /* this newly created coroutine might be run on an existing cctx which most
887 * likely was suspended in set_stacklevel, called from pp_set_stacklevel, 889 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here.
888 * so we have to emulate entering pp_set_stacklevel here.
889 */ 890 */
890 SLF_HEAD; 891 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
892 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
891} 893}
892 894
893static void 895static void
894coro_destruct (pTHX_ struct coro *coro) 896coro_destruct (pTHX_ struct coro *coro)
895{ 897{
1059 TAINT_NOT; 1061 TAINT_NOT;
1060 return 0; 1062 return 0;
1061} 1063}
1062 1064
1063static void 1065static void
1064prepare_set_stacklevel (struct transfer_args *ta, struct coro_cctx *cctx) 1066prepare_set_stacklevel (struct coro_transfer_args *ta, struct coro_cctx *cctx)
1065{ 1067{
1066 ta->prev = (struct coro *)cctx; 1068 ta->prev = (struct coro *)cctx;
1067 ta->next = 0; 1069 ta->next = 0;
1068} 1070}
1069 1071
1129# endif 1131# endif
1130#endif 1132#endif
1131 { 1133 {
1132 dTHX; 1134 dTHX;
1133 1135
1134 /* we are the alternative tail to pp_set_stacklevel */ 1136 /* normally we would need to skip the entersub here */
1135 /* so do the same things here */ 1137 /* not doing so will re-execute it, which is exactly what we want */
1136 SLF_TAIL;
1137
1138 /* we now skip the op that did lead to transfer() */
1139 PL_op = PL_op->op_next; 1138 /* PL_nop = PL_nop->op_next */
1140 1139
1141 /* inject a fake subroutine call to cctx_init */ 1140 /* inject a fake subroutine call to cctx_init */
1142 cctx_prepare (aTHX_ (coro_cctx *)arg); 1141 cctx_prepare (aTHX_ (coro_cctx *)arg);
1143 1142
1144 /* cctx_run is the alternative tail of transfer() */ 1143 /* cctx_run is the alternative tail of transfer() */
1144 /* TODO: throwing an exception here might be deadly, VERIFY */
1145 transfer_tail (aTHX); 1145 transfer_tail (aTHX);
1146 1146
1147 /* somebody or something will hit me for both perl_run and PL_restartop */ 1147 /* somebody or something will hit me for both perl_run and PL_restartop */
1148 PL_restartop = PL_op; 1148 PL_restartop = PL_op;
1149 perl_run (PL_curinterp); 1149 perl_run (PL_curinterp);
1497# define MGf_DUP 0 1497# define MGf_DUP 0
1498#endif 1498#endif
1499}; 1499};
1500 1500
1501static void 1501static void
1502prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) 1502prepare_transfer (pTHX_ struct coro_transfer_args *ta, SV *prev_sv, SV *next_sv)
1503{ 1503{
1504 ta->prev = SvSTATE (prev_sv); 1504 ta->prev = SvSTATE (prev_sv);
1505 ta->next = SvSTATE (next_sv); 1505 ta->next = SvSTATE (next_sv);
1506 TRANSFER_CHECK (*ta); 1506 TRANSFER_CHECK (*ta);
1507} 1507}
1508 1508
1509static void 1509static void
1510api_transfer (pTHX_ SV *prev_sv, SV *next_sv) 1510api_transfer (pTHX_ SV *prev_sv, SV *next_sv)
1511{ 1511{
1512 struct transfer_args ta; 1512 struct coro_transfer_args ta;
1513 1513
1514 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1514 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1515 TRANSFER (ta, 1); 1515 TRANSFER (ta, 1);
1516} 1516}
1517 1517
1589{ 1589{
1590 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1590 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1591} 1591}
1592 1592
1593INLINE void 1593INLINE void
1594prepare_schedule (pTHX_ struct transfer_args *ta) 1594prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1595{ 1595{
1596 SV *prev_sv, *next_sv; 1596 SV *prev_sv, *next_sv;
1597 1597
1598 for (;;) 1598 for (;;)
1599 { 1599 {
1648 coro_mortal = prev_sv; 1648 coro_mortal = prev_sv;
1649 UNLOCK; 1649 UNLOCK;
1650} 1650}
1651 1651
1652INLINE void 1652INLINE void
1653prepare_cede (pTHX_ struct transfer_args *ta) 1653prepare_cede (pTHX_ struct coro_transfer_args *ta)
1654{ 1654{
1655 api_ready (aTHX_ coro_current); 1655 api_ready (aTHX_ coro_current);
1656 prepare_schedule (aTHX_ ta); 1656 prepare_schedule (aTHX_ ta);
1657} 1657}
1658 1658
1659static void 1659INLINE void
1660prepare_cede_notself (pTHX_ struct transfer_args *ta) 1660prepare_cede_notself (pTHX_ struct coro_transfer_args *ta)
1661{ 1661{
1662 SV *prev = SvRV (coro_current); 1662 SV *prev = SvRV (coro_current);
1663 1663
1664 if (coro_nready) 1664 if (coro_nready)
1665 { 1665 {
1666 prepare_schedule (aTHX_ ta); 1666 prepare_schedule (aTHX_ ta);
1667 api_ready (aTHX_ prev); 1667 api_ready (aTHX_ prev);
1668 } 1668 }
1669 else 1669 else
1670 ta->prev = ta->next = SvSTATE (prev); 1670 prepare_nop (aTHX_ ta);
1671} 1671}
1672 1672
1673static void 1673static void
1674api_schedule (pTHX) 1674api_schedule (pTHX)
1675{ 1675{
1676 struct transfer_args ta; 1676 struct coro_transfer_args ta;
1677 1677
1678 prepare_schedule (aTHX_ &ta); 1678 prepare_schedule (aTHX_ &ta);
1679 TRANSFER (ta, 1); 1679 TRANSFER (ta, 1);
1680} 1680}
1681 1681
1682static int 1682static int
1683api_cede (pTHX) 1683api_cede (pTHX)
1684{ 1684{
1685 struct transfer_args ta; 1685 struct coro_transfer_args ta;
1686 1686
1687 prepare_cede (aTHX_ &ta); 1687 prepare_cede (aTHX_ &ta);
1688 1688
1689 if (expect_true (ta.prev != ta.next)) 1689 if (expect_true (ta.prev != ta.next))
1690 { 1690 {
1698static int 1698static int
1699api_cede_notself (pTHX) 1699api_cede_notself (pTHX)
1700{ 1700{
1701 if (coro_nready) 1701 if (coro_nready)
1702 { 1702 {
1703 struct transfer_args ta; 1703 struct coro_transfer_args ta;
1704 1704
1705 prepare_cede_notself (aTHX_ &ta); 1705 prepare_cede_notself (aTHX_ &ta);
1706 TRANSFER (ta, 1); 1706 TRANSFER (ta, 1);
1707 return 1; 1707 return 1;
1708 } 1708 }
1852 PUSHs ((SV *)CvGV (slf_cv)); 1852 PUSHs ((SV *)CvGV (slf_cv));
1853 1853
1854 RETURNOP (slf_restore.op_first); 1854 RETURNOP (slf_restore.op_first);
1855} 1855}
1856 1856
1857#define OPpENTERSUB_SLF 15 /* the part of op_private entersub hopefully doesn't use */ 1857static void
1858slf_init_set_stacklevel (pTHX_ SV **arg, int items)
1859{
1860 assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1));
1861 CORO_SLF_DATA = (void *)SvIV (arg [0]);
1862}
1858 1863
1859enum { 1864static void
1860 CORO_SLF_CUSTOM = 0, 1865slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1861 CORO_SLF_SET_STACKLEVEL = 1, 1866{
1862 CORO_SLF_TRANSFER = 2 1867 prepare_set_stacklevel (ta, (struct coro_cctx *)CORO_SLF_DATA);
1863}; 1868}
1864 1869
1865/* declare prototype */ 1870static void
1866XS(XS_Coro__State__set_stacklevel); 1871slf_init_transfer (pTHX_ SV **arg, int items)
1872{
1873 if (items != 2)
1874 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d.", items);
1875
1876 CORO_SLF_DATA = (void *)arg; /* let's hope it will stay valid */
1877}
1878
1879static void
1880slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta)
1881{
1882 SV **arg = (SV **)CORO_SLF_DATA;
1883
1884 prepare_transfer (ta, arg [0], arg [1]);
1885}
1886
1887static void
1888slf_init_nop (pTHX_ SV **arg, int items)
1889{
1890}
1891
1892/* slf_prepare_schedule == prepare_schedule */
1893/* slf_prepare_cede == prepare_cede */
1894/* slf_prepare_notself == prepare_notself */
1895
1896/* we hijack an hopefully unused CV flag for our purposes */
1897#define CVf_SLF 0x4000
1867 1898
1868/* 1899/*
1869 * these not obviously related functions are all rolled into one 1900 * these not obviously related functions are all rolled into one
1870 * function to increase chances that they all will call transfer with the same 1901 * function to increase chances that they all will call transfer with the same
1871 * stack offset 1902 * stack offset
1872 * SLF stands for "schedule-like-function". 1903 * SLF stands for "schedule-like-function".
1873 */ 1904 */
1874static OP * 1905static OP *
1875pp_slf (pTHX) 1906pp_slf (pTHX)
1876{ 1907{
1908 I32 checkmark; /* mark SP to see how many elements check has pushed */
1909
1910 if (expect_true (!slf_frame.prepare))
1911 {
1912 /* first iteration */
1877 dSP; 1913 dSP;
1878 struct transfer_args ta;
1879 SV **arg = PL_stack_base + TOPMARK + 1; 1914 SV **arg = PL_stack_base + TOPMARK + 1;
1880 int items = SP - arg; /* args without function object */ 1915 int items = SP - arg; /* args without function object */
1881 int ix = PL_op->op_private & OPpENTERSUB_SLF;
1882 struct CoroSLF *slf = 0;
1883 SV *gv = *sp; 1916 SV *gv = *sp;
1917 struct CoroSLF *slf;
1884 1918
1885 /* do a quick consistency check on the "function" object, and if it isn't */ 1919 /* do a quick consistency check on the "function" object, and if it isn't */
1886 /* for us, divert to the real entersub */ 1920 /* for us, divert to the real entersub */
1887 if (SvTYPE (gv) != SVt_PVGV || CvXSUB (GvCV (gv)) != XS_Coro__State__set_stacklevel) 1921 if (SvTYPE (gv) != SVt_PVGV || !(CvFLAGS (GvCV (gv)) & CVf_SLF))
1888 return PL_ppaddr[OP_ENTERSUB](aTHX); 1922 return PL_ppaddr[OP_ENTERSUB](aTHX);
1889 1923
1890 /* pop args */ 1924 /* pop args */
1891 SP = PL_stack_base + POPMARK; 1925 SP = PL_stack_base + POPMARK;
1892 1926
1893 if (!(PL_op->op_flags & OPf_STACKED)) 1927 if (!(PL_op->op_flags & OPf_STACKED))
1894 { 1928 {
1895 /* ampersand-form of call, use @_ instead of stack */ 1929 /* ampersand-form of call, use @_ instead of stack */
1896 AV *av = GvAV (PL_defgv); 1930 AV *av = GvAV (PL_defgv);
1897 arg = AvARRAY (av); 1931 arg = AvARRAY (av);
1898 items = AvFILLp (av) + 1; 1932 items = AvFILLp (av) + 1;
1899 } 1933 }
1900 1934
1901 PUTBACK; 1935 PUTBACK;
1902 1936
1903 if (!ix)
1904 {
1905 slf = (struct CoroSLF *)CvXSUBANY (GvCV (gv)).any_ptr; 1937 slf = (struct CoroSLF *)CvXSUBANY (GvCV (gv)).any_ptr;
1938 slf_frame.prepare = slf->prepare;
1939 slf_frame.check = slf->check;
1906 ix = slf->prepare (aTHX_ arg, items); 1940 slf->init (aTHX_ arg, items);
1907 }
1908
1909 switch (ix)
1910 { 1941 }
1911 case CORO_SLF_SET_STACKLEVEL:
1912 prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (arg [0]));
1913 break;
1914 1942
1915 case CORO_SLF_TRANSFER: 1943 /* now interpret the slf_frame */
1916 if (items != 2) 1944 /* we use a callback system not to make the code needlessly */
1917 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d.", items); 1945 /* complicated, but so we can run multiple perl coros from one cctx */
1918
1919 prepare_transfer (aTHX_ &ta, arg [0], arg [1]);
1920 break;
1921
1922 case CORO_SLF_SCHEDULE:
1923 prepare_schedule (aTHX_ &ta);
1924 break;
1925
1926 case CORO_SLF_CEDE:
1927 prepare_cede (aTHX_ &ta);
1928 break;
1929
1930 case CORO_SLF_CEDE_NOTSELF:
1931 prepare_cede_notself (aTHX_ &ta);
1932 break;
1933
1934 default:
1935 abort ();
1936 }
1937 1946
1938 do 1947 do
1948 {
1949 struct coro_transfer_args ta;
1950
1951 slf_frame.prepare (aTHX_ &ta);
1939 TRANSFER (ta, 0); 1952 TRANSFER (ta, 0);
1953
1954 checkmark = PL_stack_sp - PL_stack_base;
1955 }
1940 while (slf && slf->check (aTHX)); 1956 while (slf_frame.check (aTHX));
1941 1957
1942 SPAGAIN; 1958 {
1959 dSP;
1960 SV **bot = PL_stack_base + checkmark;
1961 int gimme = GIMME_V;
1943 1962
1963 slf_frame.prepare = 0; /* signal pp_slf that we need a new frame */
1964
1965 /* make sure we put something on the stack in scalar context */
1966 if (gimme == G_SCALAR)
1967 {
1968 if (sp == bot)
1969 XPUSHs (&PL_sv_undef);
1970
1971 SP = bot + 1;
1972 }
1973
1944 PUTBACK; 1974 PUTBACK;
1945 SLF_TAIL; 1975 }
1946 SPAGAIN; 1976
1947 RETURN; 1977 return NORMAL;
1948} 1978}
1949 1979
1950static void 1980static void
1951coro_slf_patch (pTHX_ CV *cv, int ix, SV **args, int items) 1981api_execute_slf (pTHX_ CV *cv, const struct CoroSLF *slf, SV **arg, int items)
1952{ 1982{
1953 assert (("FATAL: SLF call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_slf)); 1983 assert (("FATAL: SLF call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_slf));
1954
1955 assert (("FATAL: SLF call with illegal CV value", CvGV (cv))); 1984 assert (("FATAL: SLF call with illegal CV value", !CvANON (cv)));
1985
1986 if (items > 2)
1987 croak ("Coro only supports a max of two arguments to SLF functions.");
1988
1989 CvFLAGS (cv) |= CVf_SLF;
1990 CvXSUBANY (cv).any_ptr = (void *)slf;
1956 slf_cv = cv; 1991 slf_cv = cv;
1957 1992
1958 /* we patch the op, and then re-run the whole call */ 1993 /* we patch the op, and then re-run the whole call */
1959 /* we have to put the same argument on the stack for this to work */ 1994 /* we have to put the same argument on the stack for this to work */
1960 /* and this will be done by pp_restore */ 1995 /* and this will be done by pp_restore */
1961 slf_restore.op_next = (OP *)&slf_restore; 1996 slf_restore.op_next = (OP *)&slf_restore;
1962 slf_restore.op_type = OP_NULL; 1997 slf_restore.op_type = OP_NULL;
1963 slf_restore.op_ppaddr = pp_restore; 1998 slf_restore.op_ppaddr = pp_restore;
1964 slf_restore.op_first = PL_op; 1999 slf_restore.op_first = PL_op;
1965 2000
1966 slf_arg0 = items > 0 ? SvREFCNT_inc (args [0]) : 0; 2001 slf_arg0 = items > 0 ? SvREFCNT_inc (arg [0]) : 0;
1967 slf_arg1 = items > 1 ? SvREFCNT_inc (args [1]) : 0; 2002 slf_arg1 = items > 1 ? SvREFCNT_inc (arg [1]) : 0;
1968 2003
1969 PL_op->op_ppaddr = pp_slf; 2004 PL_op->op_ppaddr = pp_slf;
1970 PL_op->op_private = PL_op->op_private & ~OPpENTERSUB_SLF | ix; /* we potentially share our private flags with entersub */
1971 2005
1972 PL_op = (OP *)&slf_restore; 2006 PL_op = (OP *)&slf_restore;
1973}
1974
1975static void
1976api_execute_slf (pTHX_ CV *cv, const struct CoroSLF *slf, SV **arg, int items)
1977{
1978 CvXSUBANY (cv).any_ptr = (void *)slf;
1979 coro_slf_patch (aTHX_ cv, CORO_SLF_CUSTOM, arg, items);
1980} 2007}
1981 2008
1982MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2009MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1983 2010
1984PROTOTYPES: DISABLE 2011PROTOTYPES: DISABLE
2019 2046
2020 coroapi.ver = CORO_API_VERSION; 2047 coroapi.ver = CORO_API_VERSION;
2021 coroapi.rev = CORO_API_REVISION; 2048 coroapi.rev = CORO_API_REVISION;
2022 coroapi.transfer = api_transfer; 2049 coroapi.transfer = api_transfer;
2023 coroapi.execute_slf = api_execute_slf; 2050 coroapi.execute_slf = api_execute_slf;
2051 coroapi.sv_state = SvSTATE_;
2024 2052
2025 { 2053 {
2026 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); 2054 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
2027 2055
2028 if (!svp) croak ("Time::HiRes is required"); 2056 if (!svp) croak ("Time::HiRes is required");
2063 OUTPUT: 2091 OUTPUT:
2064 RETVAL 2092 RETVAL
2065 2093
2066void 2094void
2067_set_stacklevel (...) 2095_set_stacklevel (...)
2068 ALIAS: 2096 CODE:
2069 _set_stacklevel = CORO_SLF_SET_STACKLEVEL 2097{
2070 Coro::State::transfer = CORO_SLF_TRANSFER 2098 static struct CoroSLF slf = { slf_init_set_stacklevel, slf_prepare_set_stacklevel, slf_check_nop };
2071 Coro::schedule = CORO_SLF_SCHEDULE 2099 api_execute_slf (aTHX_ cv, &slf, &ST (0), items);
2072 Coro::cede = CORO_SLF_CEDE 2100}
2073 Coro::cede_notself = CORO_SLF_CEDE_NOTSELF 2101
2074 CODE: 2102void
2075 coro_slf_patch (aTHX_ cv, ix, &ST (0), items); 2103transfer (...)
2104 CODE:
2105{
2106 static struct CoroSLF slf = { slf_init_transfer, slf_prepare_transfer, slf_check_nop };
2107 api_execute_slf (aTHX_ cv, &slf, &ST (0), items);
2108}
2076 2109
2077bool 2110bool
2078_destroy (SV *coro_sv) 2111_destroy (SV *coro_sv)
2079 CODE: 2112 CODE:
2080 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 2113 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
2295 SvREADONLY_on (sv); 2328 SvREADONLY_on (sv);
2296 } 2329 }
2297} 2330}
2298 2331
2299void 2332void
2333schedule (...)
2334 CODE:
2335{
2336 static struct CoroSLF slf = { slf_init_nop, prepare_schedule, slf_check_nop };
2337 api_execute_slf (aTHX_ cv, &slf, &ST (0), items);
2338}
2339
2340void
2341cede (...)
2342 CODE:
2343{
2344 static struct CoroSLF slf = { slf_init_nop, prepare_cede, slf_check_nop };
2345 api_execute_slf (aTHX_ cv, &slf, &ST (0), items);
2346}
2347
2348void
2349cede_notself (...)
2350 CODE:
2351{
2352 static struct CoroSLF slf = { slf_init_nop, prepare_cede_notself, slf_check_nop };
2353 api_execute_slf (aTHX_ cv, &slf, &ST (0), items);
2354}
2355
2356void
2300_set_current (SV *current) 2357_set_current (SV *current)
2301 PROTOTYPE: $ 2358 PROTOTYPE: $
2302 CODE: 2359 CODE:
2303 SvREFCNT_dec (SvRV (coro_current)); 2360 SvREFCNT_dec (SvRV (coro_current));
2304 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); 2361 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines