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.267 by root, Fri Nov 14 06:29:52 2008 UTC vs.
Revision 1.272 by root, Fri Nov 14 20:35:49 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)
180static JMPENV *main_top_env; 183static JMPENV *main_top_env;
181static HV *coro_state_stash, *coro_stash; 184static HV *coro_state_stash, *coro_stash;
182static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 185static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
183static volatile struct coro *transfer_next; 186static volatile struct coro *transfer_next;
184 187
185struct transfer_args
186{
187 struct coro *prev, *next;
188};
189
190static GV *irsgv; /* $/ */ 188static GV *irsgv; /* $/ */
191static GV *stdoutgv; /* *STDOUT */ 189static GV *stdoutgv; /* *STDOUT */
192static SV *rv_diehook; 190static SV *rv_diehook;
193static SV *rv_warnhook; 191static SV *rv_warnhook;
194static HV *hv_sig; /* %SIG */ 192static HV *hv_sig; /* %SIG */
212 CC_TRACE_LINE = 0x10, /* trace each statement */ 210 CC_TRACE_LINE = 0x10, /* trace each statement */
213 CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE, 211 CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE,
214}; 212};
215 213
216/* this is a structure representing a c-level coroutine */ 214/* this is a structure representing a c-level coroutine */
217typedef struct coro_cctx { 215typedef struct coro_cctx
216{
218 struct coro_cctx *next; 217 struct coro_cctx *next;
219 218
220 /* the stack */ 219 /* the stack */
221 void *sptr; 220 void *sptr;
222 size_t ssize; 221 size_t ssize;
240 CF_NEW = 0x0004, /* has never been switched to */ 239 CF_NEW = 0x0004, /* has never been switched to */
241 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 240 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
242}; 241};
243 242
244/* 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 */
245typedef struct { 244typedef struct
245{
246 SV *defsv; 246 SV *defsv;
247 AV *defav; 247 AV *defav;
248 SV *errsv; 248 SV *errsv;
249 SV *irsgv; 249 SV *irsgv;
250#define VAR(name,type) type name; 250#define VAR(name,type) type name;
252#undef VAR 252#undef VAR
253} perl_slots; 253} perl_slots;
254 254
255#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))
256 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
257/* this is a structure representing a perl-level coroutine */ 266/* this is a structure representing a perl-level coroutine */
258struct coro { 267struct coro {
259 /* the c coroutine allocated to this perl coroutine, if any */ 268 /* the C coroutine allocated to this perl coroutine, if any */
260 coro_cctx *cctx; 269 coro_cctx *cctx;
261 270
262 /* process data */ 271 /* process data */
272 struct slf_frame slf_frame; /* saved slf frame */
273 void *slf_data;
263 AV *mainstack; 274 AV *mainstack;
264 perl_slots *slot; /* basically the saved sp */ 275 perl_slots *slot; /* basically the saved sp */
265 276
266 AV *args; /* data associated with this coroutine (initial args) */ 277 AV *args; /* data associated with this coroutine (initial args) */
267 int refcnt; /* coroutines are refcounted, yes */ 278 int refcnt; /* coroutines are refcounted, yes */
282 struct coro *next, *prev; 293 struct coro *next, *prev;
283}; 294};
284 295
285typedef struct coro *Coro__State; 296typedef struct coro *Coro__State;
286typedef struct coro *Coro__State_or_hashref; 297typedef struct coro *Coro__State_or_hashref;
298
299static struct slf_frame slf_frame; /* the current slf frame */
287 300
288/** Coro ********************************************************************/ 301/** Coro ********************************************************************/
289 302
290#define PRIO_MAX 3 303#define PRIO_MAX 3
291#define PRIO_HIGH 1 304#define PRIO_HIGH 1
296 309
297/* for Coro.pm */ 310/* for Coro.pm */
298static SV *coro_current; 311static SV *coro_current;
299static SV *coro_readyhook; 312static SV *coro_readyhook;
300static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 313static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
301static int coro_nready;
302static struct coro *coro_first; 314static struct coro *coro_first;
315#define coro_nready coroapi.nready
303 316
304/** lowlevel stuff **********************************************************/ 317/** lowlevel stuff **********************************************************/
305 318
306static SV * 319static SV *
307coro_get_sv (pTHX_ const char *name, int create) 320coro_get_sv (pTHX_ const char *name, int create)
511 CvPADLIST (cv) = (AV *)POPs; 524 CvPADLIST (cv) = (AV *)POPs;
512 } 525 }
513 526
514 PUTBACK; 527 PUTBACK;
515 } 528 }
529
530 slf_frame = c->slf_frame;
531 coroapi.slf_data = c->slf_data;
516} 532}
517 533
518static void 534static void
519save_perl (pTHX_ Coro__State c) 535save_perl (pTHX_ Coro__State c)
520{ 536{
537 c->slf_data = coroapi.slf_data;
538 c->slf_frame = slf_frame;
539
521 { 540 {
522 dSP; 541 dSP;
523 I32 cxix = cxstack_ix; 542 I32 cxix = cxstack_ix;
524 PERL_CONTEXT *ccstk = cxstack; 543 PERL_CONTEXT *ccstk = cxstack;
525 PERL_SI *top_si = PL_curstackinfo; 544 PERL_SI *top_si = PL_curstackinfo;
592 #undef VAR 611 #undef VAR
593 } 612 }
594} 613}
595 614
596/* 615/*
597 * allocate various perl stacks. This is an exact copy 616 * allocate various perl stacks. This is almost an exact copy
598 * of perl.c:init_stacks, except that it uses less memory 617 * of perl.c:init_stacks, except that it uses less memory
599 * on the (sometimes correct) assumption that coroutines do 618 * on the (sometimes correct) assumption that coroutines do
600 * not usually need a lot of stackspace. 619 * not usually need a lot of stackspace.
601 */ 620 */
602#if CORO_PREFER_PERL_FUNCTIONS 621#if CORO_PREFER_PERL_FUNCTIONS
709#endif 728#endif
710 } 729 }
711 } 730 }
712 731
713 return rss; 732 return rss;
714}
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} 733}
742 734
743/** coroutine stack handling ************************************************/ 735/** coroutine stack handling ************************************************/
744 736
745static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); 737static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
831 823
832 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; 824 return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0;
833} 825}
834 826
835static 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
836coro_setup (pTHX_ struct coro *coro) 841coro_setup (pTHX_ struct coro *coro)
837{ 842{
838 /* 843 /*
839 * emulate part of the perl startup here. 844 * emulate part of the perl startup here.
840 */ 845 */
879 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 884 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
880 SPAGAIN; 885 SPAGAIN;
881 } 886 }
882 887
883 /* 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
884 * 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.
885 * so we have to emulate entering pp_set_stacklevel here.
886 */ 890 */
887 SSL_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 */
888} 893}
889 894
890static void 895static void
891coro_destruct (pTHX_ struct coro *coro) 896coro_destruct (pTHX_ struct coro *coro)
892{ 897{
1056 TAINT_NOT; 1061 TAINT_NOT;
1057 return 0; 1062 return 0;
1058} 1063}
1059 1064
1060static void 1065static void
1061prepare_set_stacklevel (struct transfer_args *ta, struct coro_cctx *cctx) 1066prepare_set_stacklevel (struct coro_transfer_args *ta, struct coro_cctx *cctx)
1062{ 1067{
1063 ta->prev = (struct coro *)cctx; 1068 ta->prev = (struct coro *)cctx;
1064 ta->next = 0; 1069 ta->next = 0;
1065} 1070}
1066 1071
1126# endif 1131# endif
1127#endif 1132#endif
1128 { 1133 {
1129 dTHX; 1134 dTHX;
1130 1135
1131 /* we are the alternative tail to pp_set_stacklevel */ 1136 /* normally we would need to skip the entersub here */
1132 /* so do the same things here */ 1137 /* not doing so will re-execute it, which is exactly what we want */
1133 SSL_TAIL;
1134
1135 /* we now skip the op that did lead to transfer() */
1136 PL_op = PL_op->op_next; 1138 /* PL_nop = PL_nop->op_next */
1137 1139
1138 /* inject a fake subroutine call to cctx_init */ 1140 /* inject a fake subroutine call to cctx_init */
1139 cctx_prepare (aTHX_ (coro_cctx *)arg); 1141 cctx_prepare (aTHX_ (coro_cctx *)arg);
1140 1142
1141 /* 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 */
1142 transfer_tail (aTHX); 1145 transfer_tail (aTHX);
1143 1146
1144 /* 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 */
1145 PL_restartop = PL_op; 1148 PL_restartop = PL_op;
1146 perl_run (PL_curinterp); 1149 perl_run (PL_curinterp);
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 */
1494# define MGf_DUP 0 1497# define MGf_DUP 0
1495#endif 1498#endif
1496}; 1499};
1497 1500
1498static void 1501static void
1499prepare_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)
1500{ 1503{
1501 ta->prev = SvSTATE (prev_sv); 1504 ta->prev = SvSTATE (prev_sv);
1502 ta->next = SvSTATE (next_sv); 1505 ta->next = SvSTATE (next_sv);
1503 TRANSFER_CHECK (*ta); 1506 TRANSFER_CHECK (*ta);
1504} 1507}
1505 1508
1506static void 1509static void
1507api_transfer (SV *prev_sv, SV *next_sv) 1510api_transfer (pTHX_ SV *prev_sv, SV *next_sv)
1508{ 1511{
1509 dTHX;
1510 struct transfer_args ta; 1512 struct coro_transfer_args ta;
1511 1513
1512 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1514 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1513 TRANSFER (ta, 1); 1515 TRANSFER (ta, 1);
1514} 1516}
1515 1517
1532 1534
1533 return 0; 1535 return 0;
1534} 1536}
1535 1537
1536static int 1538static int
1537api_ready (SV *coro_sv) 1539api_ready (pTHX_ SV *coro_sv)
1538{ 1540{
1539 dTHX;
1540 struct coro *coro; 1541 struct coro *coro;
1541 SV *sv_hook; 1542 SV *sv_hook;
1542 void (*xs_hook)(void); 1543 void (*xs_hook)(void);
1543 1544
1544 if (SvROK (coro_sv)) 1545 if (SvROK (coro_sv))
1582 1583
1583 return 1; 1584 return 1;
1584} 1585}
1585 1586
1586static int 1587static int
1587api_is_ready (SV *coro_sv) 1588api_is_ready (pTHX_ SV *coro_sv)
1588{ 1589{
1589 dTHX;
1590
1591 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1590 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1592} 1591}
1593 1592
1594INLINE void 1593INLINE void
1595prepare_schedule (pTHX_ struct transfer_args *ta) 1594prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1596{ 1595{
1597 SV *prev_sv, *next_sv; 1596 SV *prev_sv, *next_sv;
1598 1597
1599 for (;;) 1598 for (;;)
1600 { 1599 {
1649 coro_mortal = prev_sv; 1648 coro_mortal = prev_sv;
1650 UNLOCK; 1649 UNLOCK;
1651} 1650}
1652 1651
1653INLINE void 1652INLINE void
1654prepare_cede (pTHX_ struct transfer_args *ta) 1653prepare_cede (pTHX_ struct coro_transfer_args *ta)
1655{ 1654{
1656 api_ready (coro_current); 1655 api_ready (aTHX_ coro_current);
1657 prepare_schedule (aTHX_ ta); 1656 prepare_schedule (aTHX_ ta);
1658} 1657}
1659 1658
1659INLINE void
1660prepare_cede_notself (pTHX_ struct coro_transfer_args *ta)
1661{
1662 SV *prev = SvRV (coro_current);
1663
1664 if (coro_nready)
1665 {
1666 prepare_schedule (aTHX_ ta);
1667 api_ready (aTHX_ prev);
1668 }
1669 else
1670 prepare_nop (aTHX_ ta);
1671}
1672
1660static void 1673static void
1661prepare_cede_notself (pTHX_ struct transfer_args *ta)
1662{
1663 SV *prev = SvRV (coro_current);
1664
1665 if (coro_nready)
1666 {
1667 prepare_schedule (aTHX_ ta);
1668 api_ready (prev);
1669 }
1670 else
1671 ta->prev = ta->next = SvSTATE (prev);
1672}
1673
1674static void
1675api_schedule (void) 1674api_schedule (pTHX)
1676{ 1675{
1677 dTHX;
1678 struct transfer_args ta; 1676 struct coro_transfer_args ta;
1679 1677
1680 prepare_schedule (aTHX_ &ta); 1678 prepare_schedule (aTHX_ &ta);
1681 TRANSFER (ta, 1); 1679 TRANSFER (ta, 1);
1682} 1680}
1683 1681
1684static int 1682static int
1685api_cede (void) 1683api_cede (pTHX)
1686{ 1684{
1687 dTHX;
1688 struct transfer_args ta; 1685 struct coro_transfer_args ta;
1689 1686
1690 prepare_cede (aTHX_ &ta); 1687 prepare_cede (aTHX_ &ta);
1691 1688
1692 if (expect_true (ta.prev != ta.next)) 1689 if (expect_true (ta.prev != ta.next))
1693 { 1690 {
1697 else 1694 else
1698 return 0; 1695 return 0;
1699} 1696}
1700 1697
1701static int 1698static int
1702api_cede_notself (void) 1699api_cede_notself (pTHX)
1703{ 1700{
1704 if (coro_nready) 1701 if (coro_nready)
1705 { 1702 {
1706 dTHX;
1707 struct transfer_args ta; 1703 struct coro_transfer_args ta;
1708 1704
1709 prepare_cede_notself (aTHX_ &ta); 1705 prepare_cede_notself (aTHX_ &ta);
1710 TRANSFER (ta, 1); 1706 TRANSFER (ta, 1);
1711 return 1; 1707 return 1;
1712 } 1708 }
1713 else 1709 else
1714 return 0; 1710 return 0;
1715} 1711}
1716 1712
1717static void 1713static void
1718api_trace (SV *coro_sv, int flags) 1714api_trace (pTHX_ SV *coro_sv, int flags)
1719{ 1715{
1720 dTHX;
1721 struct coro *coro = SvSTATE (coro_sv); 1716 struct coro *coro = SvSTATE (coro_sv);
1722 1717
1723 if (flags & CC_TRACE) 1718 if (flags & CC_TRACE)
1724 { 1719 {
1725 if (!coro->cctx) 1720 if (!coro->cctx)
1792 PerlIOCede *self = PerlIOSelf (f, PerlIOCede); 1787 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1793 double now = nvtime (); 1788 double now = nvtime ();
1794 1789
1795 if (now >= self->next) 1790 if (now >= self->next)
1796 { 1791 {
1797 api_cede (); 1792 api_cede (aTHX);
1798 self->next = now + self->every; 1793 self->next = now + self->every;
1799 } 1794 }
1800 1795
1801 return PerlIOBuf_flush (aTHX_ f); 1796 return PerlIOBuf_flush (aTHX_ f);
1802} 1797}
1833 PerlIOBuf_set_ptrcnt, 1828 PerlIOBuf_set_ptrcnt,
1834}; 1829};
1835 1830
1836/*****************************************************************************/ 1831/*****************************************************************************/
1837 1832
1838static const CV *ssl_cv; /* for quick consistency check */ 1833static const CV *slf_cv; /* for quick consistency check */
1839 1834
1840static UNOP ssl_restore; /* restore stack as entersub did, for first-re-run */ 1835static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */
1841static SV *ssl_arg0; 1836static SV *slf_arg0;
1842static SV *ssl_arg1; 1837static SV *slf_arg1;
1843 1838
1844/* this restores the stack in the case we patched the entersub, to */ 1839/* this restores the stack in the case we patched the entersub, to */
1845/* recreate the stack frame as perl will on following calls */ 1840/* recreate the stack frame as perl will on following calls */
1846/* since entersub cleared the stack */ 1841/* since entersub cleared the stack */
1847static OP * 1842static OP *
1850 dSP; 1845 dSP;
1851 1846
1852 PUSHMARK (SP); 1847 PUSHMARK (SP);
1853 1848
1854 EXTEND (SP, 3); 1849 EXTEND (SP, 3);
1855 if (ssl_arg0) PUSHs (sv_2mortal (ssl_arg0)), ssl_arg0 = 0; 1850 if (slf_arg0) PUSHs (sv_2mortal (slf_arg0));
1856 if (ssl_arg1) PUSHs (sv_2mortal (ssl_arg1)), ssl_arg1 = 0; 1851 if (slf_arg1) PUSHs (sv_2mortal (slf_arg1));
1857 PUSHs ((SV *)CvGV (ssl_cv)); 1852 PUSHs ((SV *)CvGV (slf_cv));
1858 1853
1859 RETURNOP (ssl_restore.op_first); 1854 RETURNOP (slf_restore.op_first);
1860} 1855}
1861 1856
1862#define OPpENTERSUB_SSL 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}
1863 1863
1864/* declare prototype */ 1864static void
1865XS(XS_Coro__State__set_stacklevel); 1865slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1866{
1867 prepare_set_stacklevel (ta, (struct coro_cctx *)CORO_SLF_DATA);
1868}
1869
1870static void
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
1866 1898
1867/* 1899/*
1868 * these not obviously related functions are all rolled into one 1900 * these not obviously related functions are all rolled into one
1869 * 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
1870 * stack offset 1902 * stack offset
1903 * SLF stands for "schedule-like-function".
1871 */ 1904 */
1872static OP * 1905static OP *
1873pp_set_stacklevel (pTHX) 1906pp_slf (pTHX)
1874{ 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 */
1875 dSP; 1913 dSP;
1876 struct transfer_args ta;
1877 SV **arg = PL_stack_base + TOPMARK + 1; 1914 SV **arg = PL_stack_base + TOPMARK + 1;
1878 int items = SP - arg; /* args without function object */ 1915 int items = SP - arg; /* args without function object */
1916 SV *gv = *sp;
1917 struct CoroSLF *slf;
1879 1918
1880 /* 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 */
1881 /* for us, divert to the real entersub */ 1920 /* for us, divert to the real entersub */
1882 if (SvTYPE (*sp) != SVt_PVGV || CvXSUB (GvCV (*sp)) != XS_Coro__State__set_stacklevel) 1921 if (SvTYPE (gv) != SVt_PVGV || !(CvFLAGS (GvCV (gv)) & CVf_SLF))
1883 return PL_ppaddr[OP_ENTERSUB](aTHX); 1922 return PL_ppaddr[OP_ENTERSUB](aTHX);
1884 1923
1885 /* pop args */ 1924 /* pop args */
1886 SP = PL_stack_base + POPMARK; 1925 SP = PL_stack_base + POPMARK;
1887 1926
1888 if (!(PL_op->op_flags & OPf_STACKED)) 1927 if (!(PL_op->op_flags & OPf_STACKED))
1889 { 1928 {
1890 /* ampersand-form of call, use @_ instead of stack */ 1929 /* ampersand-form of call, use @_ instead of stack */
1891 AV *av = GvAV (PL_defgv); 1930 AV *av = GvAV (PL_defgv);
1892 arg = AvARRAY (av); 1931 arg = AvARRAY (av);
1893 items = AvFILLp (av) + 1; 1932 items = AvFILLp (av) + 1;
1933 }
1934
1935 PUTBACK;
1936
1937 slf = (struct CoroSLF *)CvXSUBANY (GvCV (gv)).any_ptr;
1938 slf_frame.prepare = slf->prepare;
1939 slf_frame.check = slf->check;
1940 slf->init (aTHX_ arg, items);
1941 }
1942
1943 /* now interpret the slf_frame */
1944 /* we use a callback system not to make the code needlessly */
1945 /* complicated, but so we can run multiple perl coros from one cctx */
1946
1947 do
1948 {
1949 struct coro_transfer_args ta;
1950
1951 slf_frame.prepare (aTHX_ &ta);
1952 TRANSFER (ta, 0);
1953
1954 checkmark = PL_stack_sp - PL_stack_base;
1955 }
1956 while (slf_frame.check (aTHX));
1957
1958 {
1959 dSP;
1960 SV **bot = PL_stack_base + checkmark;
1961 int gimme = GIMME_V;
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;
1894 } 1972 }
1895 1973
1896 PUTBACK; 1974 PUTBACK;
1897 switch (PL_op->op_private & OPpENTERSUB_SSL)
1898 {
1899 case 0:
1900 prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (arg [0]));
1901 break;
1902
1903 case 1:
1904 if (items != 2)
1905 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d.", items);
1906
1907 prepare_transfer (aTHX_ &ta, arg [0], arg [1]);
1908 break;
1909
1910 case 2:
1911 prepare_schedule (aTHX_ &ta);
1912 break;
1913
1914 case 3:
1915 prepare_cede (aTHX_ &ta);
1916 break;
1917
1918 case 4:
1919 prepare_cede_notself (aTHX_ &ta);
1920 break;
1921 } 1975 }
1922 1976
1923 TRANSFER (ta, 0); 1977 return NORMAL;
1924 SPAGAIN;
1925
1926skip:
1927 PUTBACK;
1928 SSL_TAIL;
1929 SPAGAIN;
1930 RETURN;
1931} 1978}
1932 1979
1933static void 1980static void
1934coro_ssl_patch (pTHX_ CV *cv, int ix, SV **args, int items) 1981api_execute_slf (pTHX_ CV *cv, const struct CoroSLF *slf, SV **arg, int items)
1935{ 1982{
1936 assert (("FATAL: ssl call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_set_stacklevel)); 1983 assert (("FATAL: SLF call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_slf));
1937
1938 assert (("FATAL: ssl 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;
1939 ssl_cv = cv; 1991 slf_cv = cv;
1940 1992
1941 /* we patch the op, and then re-run the whole call */ 1993 /* 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 */ 1994 /* we have to put the same argument on the stack for this to work */
1995 /* and this will be done by pp_restore */
1943 ssl_restore.op_next = (OP *)&ssl_restore; 1996 slf_restore.op_next = (OP *)&slf_restore;
1944 ssl_restore.op_type = OP_NULL; 1997 slf_restore.op_type = OP_NULL;
1945 ssl_restore.op_ppaddr = pp_restore; 1998 slf_restore.op_ppaddr = pp_restore;
1946 ssl_restore.op_first = PL_op; 1999 slf_restore.op_first = PL_op;
1947 2000
1948 ssl_arg0 = items > 0 ? SvREFCNT_inc (args [0]) : 0; 2001 slf_arg0 = items > 0 ? SvREFCNT_inc (arg [0]) : 0;
1949 ssl_arg1 = items > 1 ? SvREFCNT_inc (args [1]) : 0; 2002 slf_arg1 = items > 1 ? SvREFCNT_inc (arg [1]) : 0;
1950 2003
1951 PL_op->op_ppaddr = pp_set_stacklevel; 2004 PL_op->op_ppaddr = pp_slf;
1952 PL_op->op_private = PL_op->op_private & ~OPpENTERSUB_SSL | ix; /* we potentially share our private flags with entersub */
1953 2005
1954 PL_op = (OP *)&ssl_restore; 2006 PL_op = (OP *)&slf_restore;
1955} 2007}
1956 2008
1957MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2009MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1958 2010
1959PROTOTYPES: DISABLE 2011PROTOTYPES: DISABLE
1990 main_top_env = PL_top_env; 2042 main_top_env = PL_top_env;
1991 2043
1992 while (main_top_env->je_prev) 2044 while (main_top_env->je_prev)
1993 main_top_env = main_top_env->je_prev; 2045 main_top_env = main_top_env->je_prev;
1994 2046
1995 coroapi.ver = CORO_API_VERSION; 2047 coroapi.ver = CORO_API_VERSION;
1996 coroapi.rev = CORO_API_REVISION; 2048 coroapi.rev = CORO_API_REVISION;
1997 coroapi.transfer = api_transfer; 2049 coroapi.transfer = api_transfer;
2050 coroapi.execute_slf = api_execute_slf;
2051 coroapi.sv_state = SvSTATE_;
1998 2052
1999 { 2053 {
2000 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); 2054 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
2001 2055
2002 if (!svp) croak ("Time::HiRes is required"); 2056 if (!svp) croak ("Time::HiRes is required");
2037 OUTPUT: 2091 OUTPUT:
2038 RETVAL 2092 RETVAL
2039 2093
2040void 2094void
2041_set_stacklevel (...) 2095_set_stacklevel (...)
2042 ALIAS: 2096 CODE:
2043 Coro::State::transfer = 1 2097{
2044 Coro::schedule = 2 2098 static struct CoroSLF slf = { slf_init_set_stacklevel, slf_prepare_set_stacklevel, slf_check_nop };
2045 Coro::cede = 3 2099 api_execute_slf (aTHX_ cv, &slf, &ST (0), items);
2046 Coro::cede_notself = 4 2100}
2047 CODE: 2101
2048 coro_ssl_patch (aTHX_ cv, ix, &ST (0), items); 2102void
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}
2049 2109
2050bool 2110bool
2051_destroy (SV *coro_sv) 2111_destroy (SV *coro_sv)
2052 CODE: 2112 CODE:
2053 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 2113 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
2171 SvREFCNT_dec (self->throw); 2231 SvREFCNT_dec (self->throw);
2172 self->throw = SvOK (throw) ? newSVsv (throw) : 0; 2232 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2173 2233
2174void 2234void
2175api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 2235api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
2236 C_ARGS: aTHX_ coro, flags
2176 2237
2177SV * 2238SV *
2178has_cctx (Coro::State coro) 2239has_cctx (Coro::State coro)
2179 PROTOTYPE: $ 2240 PROTOTYPE: $
2180 CODE: 2241 CODE:
2257 coroapi.schedule = api_schedule; 2318 coroapi.schedule = api_schedule;
2258 coroapi.cede = api_cede; 2319 coroapi.cede = api_cede;
2259 coroapi.cede_notself = api_cede_notself; 2320 coroapi.cede_notself = api_cede_notself;
2260 coroapi.ready = api_ready; 2321 coroapi.ready = api_ready;
2261 coroapi.is_ready = api_is_ready; 2322 coroapi.is_ready = api_is_ready;
2262 coroapi.nready = &coro_nready; 2323 coroapi.nready = coro_nready;
2263 coroapi.current = coro_current; 2324 coroapi.current = coro_current;
2264 2325
2265 GCoroAPI = &coroapi; 2326 GCoroAPI = &coroapi;
2266 sv_setiv (sv, (IV)&coroapi); 2327 sv_setiv (sv, (IV)&coroapi);
2267 SvREADONLY_on (sv); 2328 SvREADONLY_on (sv);
2268 } 2329 }
2330}
2331
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);
2269} 2354}
2270 2355
2271void 2356void
2272_set_current (SV *current) 2357_set_current (SV *current)
2273 PROTOTYPE: $ 2358 PROTOTYPE: $
2308 2393
2309SV * 2394SV *
2310ready (SV *self) 2395ready (SV *self)
2311 PROTOTYPE: $ 2396 PROTOTYPE: $
2312 CODE: 2397 CODE:
2313 RETVAL = boolSV (api_ready (self)); 2398 RETVAL = boolSV (api_ready (aTHX_ self));
2314 OUTPUT: 2399 OUTPUT:
2315 RETVAL 2400 RETVAL
2316 2401
2317int 2402int
2318nready (...) 2403nready (...)
2388 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); 2473 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
2389 2474
2390 coro->prio = 0; 2475 coro->prio = 0;
2391 2476
2392 if (coro->cctx && (coro->cctx->flags & CC_TRACE)) 2477 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
2393 api_trace (coro_current, 0); 2478 api_trace (aTHX_ coro_current, 0);
2394 2479
2395 av_push (av_async_pool, newSVsv (coro_current)); 2480 av_push (av_async_pool, newSVsv (coro_current));
2396} 2481}
2397 2482
2398#if 0 2483#if 0
2470 2555
2471 av_push (av, data_sv); 2556 av_push (av, data_sv);
2472 2557
2473 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); 2558 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2474 2559
2475 api_ready (self); 2560 api_ready (aTHX_ self);
2476} 2561}
2477 2562
2478void 2563void
2479_set_state (SV *state) 2564_set_state (SV *state)
2480 PROTOTYPE: $ 2565 PROTOTYPE: $
2505 PROTOTYPE: @ 2590 PROTOTYPE: @
2506 CODE: 2591 CODE:
2507{ 2592{
2508 static int incede; 2593 static int incede;
2509 2594
2510 api_cede_notself (); 2595 api_cede_notself (aTHX);
2511 2596
2512 ++incede; 2597 ++incede;
2513 while (coro_nready >= incede && api_cede ()) 2598 while (coro_nready >= incede && api_cede (aTHX))
2514 ; 2599 ;
2515 2600
2516 sv_setsv (sv_activity, &PL_sv_undef); 2601 sv_setsv (sv_activity, &PL_sv_undef);
2517 if (coro_nready >= incede) 2602 if (coro_nready >= incede)
2518 { 2603 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines