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.287 by root, Mon Nov 17 07:03:12 2008 UTC vs.
Revision 1.303 by root, Wed Nov 19 05:52:42 2008 UTC

16 16
17#ifdef WIN32 17#ifdef WIN32
18# undef setjmp 18# undef setjmp
19# undef longjmp 19# undef longjmp
20# undef _exit 20# undef _exit
21# define setjmp _setjmp // deep magic, don't ask 21# define setjmp _setjmp /* deep magic */
22#else 22#else
23# include <inttypes.h> /* most portable stdint.h */ 23# include <inttypes.h> /* most portable stdint.h */
24#endif 24#endif
25 25
26#ifdef HAVE_MMAP 26#ifdef HAVE_MMAP
95# define GV_NOTQUAL 0 95# define GV_NOTQUAL 0
96#endif 96#endif
97#ifndef newSV 97#ifndef newSV
98# define newSV(l) NEWSV(0,l) 98# define newSV(l) NEWSV(0,l)
99#endif 99#endif
100#ifndef CvISXSUB_on
101# define CvISXSUB_on(cv) (void)cv
102#endif
100 103
101/* 5.8.7 */ 104/* 5.8.7 */
102#ifndef SvRV_set 105#ifndef SvRV_set
103# define SvRV_set(s,v) SvRV(s) = (v) 106# define SvRV_set(s,v) SvRV(s) = (v)
104#endif 107#endif
117#endif 120#endif
118 121
119/* The next macros try to return the current stack pointer, in an as 122/* The next macros try to return the current stack pointer, in an as
120 * portable way as possible. */ 123 * portable way as possible. */
121#if __GNUC__ >= 4 124#if __GNUC__ >= 4
125# define dSTACKLEVEL int stacklevel_dummy
122# define dSTACKLEVEL void *stacklevel = __builtin_frame_address (0) 126# define STACKLEVEL __builtin_frame_address (0)
123#else 127#else
124# define dSTACKLEVEL volatile void *stacklevel = (volatile void *)&stacklevel 128# define dSTACKLEVEL volatile void *stacklevel
129# define STACKLEVEL ((void *)&stacklevel)
125#endif 130#endif
126 131
127#define IN_DESTRUCT (PL_main_cv == Nullcv) 132#define IN_DESTRUCT (PL_main_cv == Nullcv)
128 133
129#if __GNUC__ >= 3 134#if __GNUC__ >= 3
140#define expect_true(expr) expect ((expr) != 0, 1) 145#define expect_true(expr) expect ((expr) != 0, 1)
141 146
142#define NOINLINE attribute ((noinline)) 147#define NOINLINE attribute ((noinline))
143 148
144#include "CoroAPI.h" 149#include "CoroAPI.h"
150#define GCoroAPI (&coroapi) /* very sneaky */
145 151
146#ifdef USE_ITHREADS 152#ifdef USE_ITHREADS
147# if CORO_PTHREAD 153# if CORO_PTHREAD
148static void *coro_thx; 154static void *coro_thx;
149# endif 155# endif
150#endif 156#endif
151 157
152static double (*nvtime)(); /* so why doesn't it take void? */ 158static double (*nvtime)(); /* so why doesn't it take void? */
159
160/* we hijack an hopefully unused CV flag for our purposes */
161#define CVf_SLF 0x4000
162static OP *pp_slf (pTHX);
153 163
154static U32 cctx_gen; 164static U32 cctx_gen;
155static size_t cctx_stacksize = CORO_STACKSIZE; 165static size_t cctx_stacksize = CORO_STACKSIZE;
156static struct CoroAPI coroapi; 166static struct CoroAPI coroapi;
157static AV *main_mainstack; /* used to differentiate between $main and others */ 167static AV *main_mainstack; /* used to differentiate between $main and others */
247 /* statistics */ 257 /* statistics */
248 int usecount; /* number of transfers to this coro */ 258 int usecount; /* number of transfers to this coro */
249 259
250 /* coro process data */ 260 /* coro process data */
251 int prio; 261 int prio;
252 SV *throw; /* exception to be thrown */ 262 SV *except; /* exception to be thrown */
263 SV *rouse_cb;
253 264
254 /* async_pool */ 265 /* async_pool */
255 SV *saved_deffh; 266 SV *saved_deffh;
256 267
257 /* linked list */ 268 /* linked list */
263 274
264/* the following variables are effectively part of the perl context */ 275/* the following variables are effectively part of the perl context */
265/* and get copied between struct coro and these variables */ 276/* and get copied between struct coro and these variables */
266/* the mainr easonw e don't support windows process emulation */ 277/* the mainr easonw e don't support windows process emulation */
267static struct CoroSLF slf_frame; /* the current slf frame */ 278static struct CoroSLF slf_frame; /* the current slf frame */
268static SV *coro_throw;
269 279
270/** Coro ********************************************************************/ 280/** Coro ********************************************************************/
271 281
272#define PRIO_MAX 3 282#define PRIO_MAX 3
273#define PRIO_HIGH 1 283#define PRIO_HIGH 1
502 512
503 PUTBACK; 513 PUTBACK;
504 } 514 }
505 515
506 slf_frame = c->slf_frame; 516 slf_frame = c->slf_frame;
507 coro_throw = c->throw; 517 CORO_THROW = c->except;
508} 518}
509 519
510static void 520static void
511save_perl (pTHX_ Coro__State c) 521save_perl (pTHX_ Coro__State c)
512{ 522{
513 c->throw = coro_throw; 523 c->except = CORO_THROW;
514 c->slf_frame = slf_frame; 524 c->slf_frame = slf_frame;
515 525
516 { 526 {
517 dSP; 527 dSP;
518 I32 cxix = cxstack_ix; 528 I32 cxix = cxstack_ix;
811slf_check_nop (pTHX_ struct CoroSLF *frame) 821slf_check_nop (pTHX_ struct CoroSLF *frame)
812{ 822{
813 return 0; 823 return 0;
814} 824}
815 825
826static UNOP coro_setup_op;
827
816static void NOINLINE /* noinline to keep it out of the transfer fast path */ 828static void NOINLINE /* noinline to keep it out of the transfer fast path */
817coro_setup (pTHX_ struct coro *coro) 829coro_setup (pTHX_ struct coro *coro)
818{ 830{
819 /* 831 /*
820 * emulate part of the perl startup here. 832 * emulate part of the perl startup here.
848 { 860 {
849 dSP; 861 dSP;
850 UNOP myop; 862 UNOP myop;
851 863
852 Zero (&myop, 1, UNOP); 864 Zero (&myop, 1, UNOP);
853 myop.op_next = Nullop; 865 myop.op_next = Nullop;
854 myop.op_flags = OPf_WANT_VOID; 866 myop.op_flags = OPf_WANT_VOID;
855 867
856 PUSHMARK (SP); 868 PUSHMARK (SP);
857 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 869 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
858 PUTBACK; 870 PUTBACK;
865 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. 877 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here.
866 */ 878 */
867 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 879 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
868 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ 880 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
869 881
870 coro_throw = coro->throw; 882 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */
883 coro_setup_op.op_next = PL_op;
884 coro_setup_op.op_type = OP_CUSTOM;
885 coro_setup_op.op_ppaddr = pp_slf;
886 /* no flags required, as an init function won't be called */
887
888 PL_op = (OP *)&coro_setup_op;
889
890 /* copy throw, in case it was set before coro_setup */
891 CORO_THROW = coro->except;
871} 892}
872 893
873static void 894static void
874coro_destruct (pTHX_ struct coro *coro) 895coro_destruct (pTHX_ struct coro *coro)
875{ 896{
898 SvREFCNT_dec (GvSV (irsgv)); 919 SvREFCNT_dec (GvSV (irsgv));
899 920
900 SvREFCNT_dec (PL_diehook); 921 SvREFCNT_dec (PL_diehook);
901 SvREFCNT_dec (PL_warnhook); 922 SvREFCNT_dec (PL_warnhook);
902 923
924 SvREFCNT_dec (CORO_THROW);
903 SvREFCNT_dec (coro->saved_deffh); 925 SvREFCNT_dec (coro->saved_deffh);
904 SvREFCNT_dec (coro_throw); 926 SvREFCNT_dec (coro->rouse_cb);
905 927
906 coro_destruct_stacks (aTHX); 928 coro_destruct_stacks (aTHX);
907} 929}
908 930
909INLINE void 931INLINE void
1038 1060
1039 TAINT_NOT; 1061 TAINT_NOT;
1040 return 0; 1062 return 0;
1041} 1063}
1042 1064
1065static struct coro_cctx *cctx_ssl_cctx;
1066static struct CoroSLF cctx_ssl_frame;
1067
1043static void 1068static void
1044prepare_set_stacklevel (struct coro_transfer_args *ta, struct coro_cctx *cctx) 1069slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1045{ 1070{
1046 ta->prev = (struct coro *)cctx; 1071 ta->prev = (struct coro *)cctx_ssl_cctx;
1047 ta->next = 0; 1072 ta->next = 0;
1048} 1073}
1049 1074
1050/* inject a fake call to Coro::State::_cctx_init into the execution */ 1075static int
1051/* _cctx_init should be careful, as it could be called at almost any time */ 1076slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1052/* during execution of a perl program */ 1077{
1053/* also initialises PL_top_env */ 1078 *frame = cctx_ssl_frame;
1079
1080 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1081}
1082
1083/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1054static void NOINLINE 1084static void NOINLINE
1055cctx_prepare (pTHX_ coro_cctx *cctx) 1085cctx_prepare (pTHX_ coro_cctx *cctx)
1056{ 1086{
1057 dSP;
1058 UNOP myop;
1059
1060 PL_top_env = &PL_start_env; 1087 PL_top_env = &PL_start_env;
1061 1088
1062 if (cctx->flags & CC_TRACE) 1089 if (cctx->flags & CC_TRACE)
1063 PL_runops = runops_trace; 1090 PL_runops = runops_trace;
1064 1091
1065 Zero (&myop, 1, UNOP); 1092 /* we already must be executing an SLF op, there is no other valid way
1066 myop.op_next = PL_op; 1093 * that can lead to creation of a new cctx */
1067 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 1094 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1095 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1068 1096
1069 PUSHMARK (SP); 1097 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */
1070 EXTEND (SP, 2); 1098 cctx_ssl_cctx = cctx;
1071 PUSHs (sv_2mortal (newSViv ((IV)cctx))); 1099 cctx_ssl_frame = slf_frame;
1072 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 1100
1073 PUTBACK; 1101 slf_frame.prepare = slf_prepare_set_stacklevel;
1074 PL_op = (OP *)&myop; 1102 slf_frame.check = slf_check_set_stacklevel;
1075 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1076 SPAGAIN;
1077} 1103}
1078 1104
1079/* the tail of transfer: execute stuff we can only do after a transfer */ 1105/* the tail of transfer: execute stuff we can only do after a transfer */
1080INLINE void 1106INLINE void
1081transfer_tail (pTHX) 1107transfer_tail (pTHX)
1297 dSTACKLEVEL; 1323 dSTACKLEVEL;
1298 1324
1299 /* sometimes transfer is only called to set idle_sp */ 1325 /* sometimes transfer is only called to set idle_sp */
1300 if (expect_false (!next)) 1326 if (expect_false (!next))
1301 { 1327 {
1302 ((coro_cctx *)prev)->idle_sp = (void *)stacklevel; 1328 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1303 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1329 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1304 } 1330 }
1305 else if (expect_true (prev != next)) 1331 else if (expect_true (prev != next))
1306 { 1332 {
1307 coro_cctx *prev__cctx; 1333 coro_cctx *prev__cctx;
1332 1358
1333 prev__cctx = prev->cctx; 1359 prev__cctx = prev->cctx;
1334 1360
1335 /* possibly untie and reuse the cctx */ 1361 /* possibly untie and reuse the cctx */
1336 if (expect_true ( 1362 if (expect_true (
1337 prev__cctx->idle_sp == (void *)stacklevel 1363 prev__cctx->idle_sp == STACKLEVEL
1338 && !(prev__cctx->flags & CC_TRACE) 1364 && !(prev__cctx->flags & CC_TRACE)
1339 && !force_cctx 1365 && !force_cctx
1340 )) 1366 ))
1341 { 1367 {
1342 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1368 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1473 1499
1474 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1500 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1475 TRANSFER (ta, 1); 1501 TRANSFER (ta, 1);
1476} 1502}
1477 1503
1504/*****************************************************************************/
1505/* gensub: simple closure generation utility */
1506
1507#define GENSUB_ARG CvXSUBANY (cv).any_ptr
1508
1509/* create a closure from XS, returns a code reference */
1510/* the arg can be accessed via GENSUB_ARG from the callback */
1511/* the callback must use dXSARGS/XSRETURN */
1512static SV *
1513gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg)
1514{
1515 CV *cv = (CV *)newSV (0);
1516
1517 sv_upgrade ((SV *)cv, SVt_PVCV);
1518
1519 CvANON_on (cv);
1520 CvISXSUB_on (cv);
1521 CvXSUB (cv) = xsub;
1522 GENSUB_ARG = arg;
1523
1524 return newRV_noinc ((SV *)cv);
1525}
1526
1478/** Coro ********************************************************************/ 1527/** Coro ********************************************************************/
1479 1528
1480INLINE void 1529INLINE void
1481coro_enq (pTHX_ struct coro *coro) 1530coro_enq (pTHX_ struct coro *coro)
1482{ 1531{
1525 ENTER; 1574 ENTER;
1526 SAVETMPS; 1575 SAVETMPS;
1527 1576
1528 PUSHMARK (SP); 1577 PUSHMARK (SP);
1529 PUTBACK; 1578 PUTBACK;
1530 call_sv (sv_hook, G_DISCARD); 1579 call_sv (sv_hook, G_VOID | G_DISCARD);
1531 SPAGAIN;
1532 1580
1533 FREETMPS; 1581 FREETMPS;
1534 LEAVE; 1582 LEAVE;
1535 } 1583 }
1536 1584
1563 ENTER; 1611 ENTER;
1564 SAVETMPS; 1612 SAVETMPS;
1565 1613
1566 PUSHMARK (SP); 1614 PUSHMARK (SP);
1567 PUTBACK; 1615 PUTBACK;
1568 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); 1616 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD);
1569 SPAGAIN;
1570 1617
1571 FREETMPS; 1618 FREETMPS;
1572 LEAVE; 1619 LEAVE;
1573 continue; 1620 continue;
1574 } 1621 }
1684 coro->slot->runops = RUNOPS_DEFAULT; 1731 coro->slot->runops = RUNOPS_DEFAULT;
1685 } 1732 }
1686} 1733}
1687 1734
1688/*****************************************************************************/ 1735/*****************************************************************************/
1736/* rouse callback */
1737
1738#define CORO_MAGIC_type_rouse PERL_MAGIC_ext
1739
1740static void
1741coro_rouse_callback (pTHX_ CV *cv)
1742{
1743 dXSARGS;
1744 SV *data = (SV *)GENSUB_ARG;
1745
1746 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1747 {
1748 /* first call, set args */
1749 int i;
1750 AV *av = newAV ();
1751 SV *coro = SvRV (data);
1752
1753 SvRV_set (data, (SV *)av);
1754 api_ready (aTHX_ coro);
1755 SvREFCNT_dec (coro);
1756
1757 /* better take a full copy of the arguments */
1758 while (items--)
1759 av_store (av, items, newSVsv (ST (items)));
1760 }
1761
1762 XSRETURN_EMPTY;
1763}
1764
1765static int
1766slf_check_rouse_wait (pTHX_ struct CoroSLF *frame)
1767{
1768 SV *data = (SV *)frame->data;
1769
1770 if (CORO_THROW)
1771 return 0;
1772
1773 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1774 return 1;
1775
1776 /* now push all results on the stack */
1777 {
1778 dSP;
1779 AV *av = (AV *)SvRV (data);
1780 int i;
1781
1782 EXTEND (SP, AvFILLp (av) + 1);
1783 for (i = 0; i <= AvFILLp (av); ++i)
1784 PUSHs (sv_2mortal (AvARRAY (av)[i]));
1785
1786 /* we have stolen the elements, so ste length to zero and free */
1787 AvFILLp (av) = -1;
1788 av_undef (av);
1789
1790 PUTBACK;
1791 }
1792
1793 return 0;
1794}
1795
1796static void
1797slf_init_rouse_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1798{
1799 SV *cb;
1800
1801 if (items)
1802 cb = arg [0];
1803 else
1804 {
1805 struct coro *coro = SvSTATE_current;
1806
1807 if (!coro->rouse_cb)
1808 croak ("Coro::rouse_wait called without rouse callback, and no default rouse callback found either,");
1809
1810 cb = sv_2mortal (coro->rouse_cb);
1811 coro->rouse_cb = 0;
1812 }
1813
1814 if (!SvROK (cb)
1815 || SvTYPE (SvRV (cb)) != SVt_PVCV
1816 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback)
1817 croak ("Coro::rouse_wait called with illegal callback argument,");
1818
1819 {
1820 CV *cv = (CV *)SvRV (cb); /* for GENSUB_ARG */
1821 SV *data = (SV *)GENSUB_ARG;
1822
1823 frame->data = (void *)data;
1824 frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule;
1825 frame->check = slf_check_rouse_wait;
1826 }
1827}
1828
1829static SV *
1830coro_new_rouse_cb (pTHX)
1831{
1832 HV *hv = (HV *)SvRV (coro_current);
1833 struct coro *coro = SvSTATE_hv (hv);
1834 SV *data = newRV_inc ((SV *)hv);
1835 SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data);
1836
1837 sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0);
1838 SvREFCNT_dec (data); /* magicext increases the refcount */
1839
1840 SvREFCNT_dec (coro->rouse_cb);
1841 coro->rouse_cb = SvREFCNT_inc_NN (cb);
1842
1843 return cb;
1844}
1845
1846/*****************************************************************************/
1689/* schedule-like-function opcode (SLF) */ 1847/* schedule-like-function opcode (SLF) */
1690 1848
1691static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */ 1849static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */
1692static const CV *slf_cv; 1850static const CV *slf_cv;
1693static SV **slf_argv; 1851static SV **slf_argv;
1714 1872
1715 RETURNOP (slf_restore.op_first); 1873 RETURNOP (slf_restore.op_first);
1716} 1874}
1717 1875
1718static void 1876static void
1719slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1720{
1721 prepare_set_stacklevel (ta, (struct coro_cctx *)slf_frame.data);
1722}
1723
1724static void
1725slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1726{
1727 assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1));
1728
1729 frame->prepare = slf_prepare_set_stacklevel;
1730 frame->check = slf_check_nop;
1731 frame->data = (void *)SvIV (arg [0]);
1732}
1733
1734static void
1735slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta) 1877slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta)
1736{ 1878{
1737 SV **arg = (SV **)slf_frame.data; 1879 SV **arg = (SV **)slf_frame.data;
1738 1880
1739 prepare_transfer (aTHX_ ta, arg [0], arg [1]); 1881 prepare_transfer (aTHX_ ta, arg [0], arg [1]);
1768slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 1910slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1769{ 1911{
1770 frame->prepare = prepare_cede_notself; 1912 frame->prepare = prepare_cede_notself;
1771 frame->check = slf_check_nop; 1913 frame->check = slf_check_nop;
1772} 1914}
1773
1774/* we hijack an hopefully unused CV flag for our purposes */
1775#define CVf_SLF 0x4000
1776 1915
1777/* 1916/*
1778 * these not obviously related functions are all rolled into one 1917 * these not obviously related functions are all rolled into one
1779 * function to increase chances that they all will call transfer with the same 1918 * function to increase chances that they all will call transfer with the same
1780 * stack offset 1919 * stack offset
1836 } 1975 }
1837 while (slf_frame.check (aTHX_ &slf_frame)); 1976 while (slf_frame.check (aTHX_ &slf_frame));
1838 1977
1839 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ 1978 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */
1840 1979
1841 /* return value handling - mostly like entersub */
1842 {
1843 dSP;
1844 SV **bot = PL_stack_base + checkmark;
1845 int gimme = GIMME_V;
1846
1847 /* make sure we put something on the stack in scalar context */
1848 if (gimme == G_SCALAR)
1849 {
1850 if (sp == bot)
1851 XPUSHs (&PL_sv_undef);
1852
1853 SP = bot + 1;
1854 }
1855
1856 PUTBACK;
1857 }
1858
1859 /* exception handling */ 1980 /* exception handling */
1860 if (expect_false (coro_throw)) 1981 if (expect_false (CORO_THROW))
1861 { 1982 {
1862 SV *exception = sv_2mortal (coro_throw); 1983 SV *exception = sv_2mortal (CORO_THROW);
1863 1984
1864 coro_throw = 0; 1985 CORO_THROW = 0;
1865 sv_setsv (ERRSV, exception); 1986 sv_setsv (ERRSV, exception);
1866 croak (0); 1987 croak (0);
1988 }
1989
1990 /* return value handling - mostly like entersub */
1991 /* make sure we put something on the stack in scalar context */
1992 if (GIMME_V == G_SCALAR)
1993 {
1994 dSP;
1995 SV **bot = PL_stack_base + checkmark;
1996
1997 if (sp == bot) /* too few, push undef */
1998 bot [1] = &PL_sv_undef;
1999 else if (sp != bot + 1) /* too many, take last one */
2000 bot [1] = *sp;
2001
2002 SP = bot + 1;
2003
2004 PUTBACK;
1867 } 2005 }
1868 2006
1869 return NORMAL; 2007 return NORMAL;
1870} 2008}
1871 2009
1993 PerlIOBuf_get_cnt, 2131 PerlIOBuf_get_cnt,
1994 PerlIOBuf_set_ptrcnt, 2132 PerlIOBuf_set_ptrcnt,
1995}; 2133};
1996 2134
1997/*****************************************************************************/ 2135/*****************************************************************************/
2136/* Coro::Semaphore & Coro::Signal */
2137
2138static SV *
2139coro_waitarray_new (pTHX_ int count)
2140{
2141 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */
2142 AV *av = newAV ();
2143 SV **ary;
2144
2145 /* unfortunately, building manually saves memory */
2146 Newx (ary, 2, SV *);
2147 AvALLOC (av) = ary;
2148 AvARRAY (av) = ary;
2149 AvMAX (av) = 1;
2150 AvFILLp (av) = 0;
2151 ary [0] = newSViv (count);
2152
2153 return newRV_noinc ((SV *)av);
2154}
2155
1998/* Coro::Semaphore */ 2156/* semaphore */
1999 2157
2000static void 2158static void
2001coro_semaphore_adjust (pTHX_ AV *av, IV adjust) 2159coro_semaphore_adjust (pTHX_ AV *av, IV adjust)
2002{ 2160{
2003 SV *count_sv = AvARRAY (av)[0]; 2161 SV *count_sv = AvARRAY (av)[0];
2033 /* call $sem->adjust (0) to possibly wake up some other waiters */ 2191 /* call $sem->adjust (0) to possibly wake up some other waiters */
2034 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); 2192 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0);
2035} 2193}
2036 2194
2037static int 2195static int
2038slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) 2196slf_check_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, int acquire)
2039{ 2197{
2040 AV *av = (AV *)frame->data; 2198 AV *av = (AV *)frame->data;
2041 SV *count_sv = AvARRAY (av)[0]; 2199 SV *count_sv = AvARRAY (av)[0];
2042 2200
2201 /* if we are about to throw, don't actually acquire the lock, just throw */
2202 if (CORO_THROW)
2203 return 0;
2043 if (SvIVX (count_sv) > 0) 2204 else if (SvIVX (count_sv) > 0)
2044 { 2205 {
2045 SvSTATE_current->on_destroy = 0; 2206 SvSTATE_current->on_destroy = 0;
2207
2208 if (acquire)
2046 SvIVX (count_sv) = SvIVX (count_sv) - 1; 2209 SvIVX (count_sv) = SvIVX (count_sv) - 1;
2210 else
2211 coro_semaphore_adjust (aTHX_ av, 0);
2212
2047 return 0; 2213 return 0;
2048 } 2214 }
2049 else 2215 else
2050 { 2216 {
2051 int i; 2217 int i;
2060 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2226 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2061 return 1; 2227 return 1;
2062 } 2228 }
2063} 2229}
2064 2230
2065static void 2231static int
2232slf_check_semaphore_down (pTHX_ struct CoroSLF *frame)
2233{
2234 return slf_check_semaphore_down_or_wait (aTHX_ frame, 1);
2235}
2236
2237static int
2238slf_check_semaphore_wait (pTHX_ struct CoroSLF *frame)
2239{
2240 return slf_check_semaphore_down_or_wait (aTHX_ frame, 0);
2241}
2242
2243static void
2066slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2244slf_init_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2067{ 2245{
2068 AV *av = (AV *)SvRV (arg [0]); 2246 AV *av = (AV *)SvRV (arg [0]);
2069 2247
2070 if (SvIVX (AvARRAY (av)[0]) > 0) 2248 if (SvIVX (AvARRAY (av)[0]) > 0)
2071 { 2249 {
2072 frame->data = (void *)av; 2250 frame->data = (void *)av;
2073 frame->prepare = prepare_nop; 2251 frame->prepare = prepare_nop;
2074 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2075 } 2252 }
2076 else 2253 else
2077 { 2254 {
2078 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2255 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2079 2256
2080 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); 2257 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av));
2081 frame->prepare = prepare_schedule; 2258 frame->prepare = prepare_schedule;
2082 2259
2083 /* to avoid race conditions when a woken-up coro gets terminated */ 2260 /* to avoid race conditions when a woken-up coro gets terminated */
2084 /* we arrange for a temporary on_destroy that calls adjust (0) */ 2261 /* we arrange for a temporary on_destroy that calls adjust (0) */
2085 assert (!SvSTATE_current->on_destroy);//D
2086 SvSTATE_current->on_destroy = coro_semaphore_on_destroy; 2262 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2087 } 2263 }
2264}
2088 2265
2266static void
2267slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2268{
2269 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items);
2089 frame->check = slf_check_semaphore_down; 2270 frame->check = slf_check_semaphore_down;
2090
2091} 2271}
2092 2272
2093/*****************************************************************************/ 2273static void
2094/* gensub: simple closure generation utility */ 2274slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2095
2096#define GENSUB_ARG CvXSUBANY (cv).any_ptr
2097
2098/* create a closure from XS, returns a code reference */
2099/* the arg can be accessed via GENSUB_ARG from the callback */
2100/* the callback must use dXSARGS/XSRETURN */
2101static SV *
2102gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg)
2103{ 2275{
2104 CV *cv = (CV *)newSV (0); 2276 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items);
2277 frame->check = slf_check_semaphore_wait;
2278}
2105 2279
2106 sv_upgrade ((SV *)cv, SVt_PVCV); 2280/* signal */
2107 2281
2108 CvANON_on (cv); 2282static void
2109 CvISXSUB_on (cv); 2283coro_signal_wake (pTHX_ AV *av, int count)
2110 CvXSUB (cv) = xsub; 2284{
2111 GENSUB_ARG = arg; 2285 SvIVX (AvARRAY (av)[0]) = 0;
2112 2286
2113 return newRV_noinc ((SV *)cv); 2287 /* now signal count waiters */
2288 while (count > 0 && AvFILLp (av) > 0)
2289 {
2290 SV *cb;
2291
2292 /* swap first two elements so we can shift a waiter */
2293 cb = AvARRAY (av)[0];
2294 AvARRAY (av)[0] = AvARRAY (av)[1];
2295 AvARRAY (av)[1] = cb;
2296
2297 cb = av_shift (av);
2298
2299 api_ready (aTHX_ cb);
2300 sv_setiv (cb, 0); /* signal waiter */
2301 SvREFCNT_dec (cb);
2302
2303 --count;
2304 }
2305}
2306
2307static int
2308slf_check_signal_wait (pTHX_ struct CoroSLF *frame)
2309{
2310 /* if we are about to throw, also stop waiting */
2311 return SvROK ((SV *)frame->data) && !CORO_THROW;
2312}
2313
2314static void
2315slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2316{
2317 AV *av = (AV *)SvRV (arg [0]);
2318
2319 if (SvIVX (AvARRAY (av)[0]))
2320 {
2321 SvIVX (AvARRAY (av)[0]) = 0;
2322 frame->prepare = prepare_nop;
2323 frame->check = slf_check_nop;
2324 }
2325 else
2326 {
2327 SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */
2328
2329 av_push (av, waiter);
2330
2331 frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */
2332 frame->prepare = prepare_schedule;
2333 frame->check = slf_check_signal_wait;
2334 }
2114} 2335}
2115 2336
2116/*****************************************************************************/ 2337/*****************************************************************************/
2117/* Coro::AIO */ 2338/* Coro::AIO */
2118 2339
2168static int 2389static int
2169slf_check_aio_req (pTHX_ struct CoroSLF *frame) 2390slf_check_aio_req (pTHX_ struct CoroSLF *frame)
2170{ 2391{
2171 AV *state = (AV *)frame->data; 2392 AV *state = (AV *)frame->data;
2172 2393
2394 /* if we are about to throw, return early */
2395 /* this does not cancel the aio request, but at least */
2396 /* it quickly returns */
2397 if (CORO_THROW)
2398 return 0;
2399
2173 /* one element that is an RV? repeat! */ 2400 /* one element that is an RV? repeat! */
2174 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) 2401 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0]))
2175 return 1; 2402 return 1;
2176 2403
2177 /* restore status */ 2404 /* restore status */
2247 2474
2248 for (i = 0; i < items; ++i) 2475 for (i = 0; i < items; ++i)
2249 PUSHs (arg [i]); 2476 PUSHs (arg [i]);
2250 2477
2251 /* now push the callback closure */ 2478 /* now push the callback closure */
2252 PUSHs (sv_2mortal (gensub (coro_aio_callback, (void *)SvREFCNT_inc_NN ((SV *)state)))); 2479 PUSHs (sv_2mortal (gensub (aTHX_ coro_aio_callback, (void *)SvREFCNT_inc_NN ((SV *)state))));
2253 2480
2254 /* now call the AIO function - we assume our request is uncancelable */ 2481 /* now call the AIO function - we assume our request is uncancelable */
2255 PUTBACK; 2482 PUTBACK;
2256 call_sv ((SV *)req, G_VOID | G_DISCARD); 2483 call_sv ((SV *)req, G_VOID | G_DISCARD);
2257 } 2484 }
2263} 2490}
2264 2491
2265static void 2492static void
2266coro_aio_req_xs (pTHX_ CV *cv) 2493coro_aio_req_xs (pTHX_ CV *cv)
2267{ 2494{
2268 dVAR;
2269 dXSARGS; 2495 dXSARGS;
2270 2496
2271 CORO_EXECUTE_SLF_XS (slf_init_aio_req); 2497 CORO_EXECUTE_SLF_XS (slf_init_aio_req);
2272 2498
2273 XSRETURN_EMPTY; 2499 XSRETURN_EMPTY;
2374 for (i = 1; i < items; i++) 2600 for (i = 1; i < items; i++)
2375 av_push (coro->args, newSVsv (ST (i))); 2601 av_push (coro->args, newSVsv (ST (i)));
2376} 2602}
2377 OUTPUT: 2603 OUTPUT:
2378 RETVAL 2604 RETVAL
2379
2380void
2381_set_stacklevel (...)
2382 CODE:
2383 CORO_EXECUTE_SLF_XS (slf_init_set_stacklevel);
2384 2605
2385void 2606void
2386transfer (...) 2607transfer (...)
2387 PROTOTYPE: $$ 2608 PROTOTYPE: $$
2388 CODE: 2609 CODE:
2514throw (Coro::State self, SV *throw = &PL_sv_undef) 2735throw (Coro::State self, SV *throw = &PL_sv_undef)
2515 PROTOTYPE: $;$ 2736 PROTOTYPE: $;$
2516 CODE: 2737 CODE:
2517{ 2738{
2518 struct coro *current = SvSTATE_current; 2739 struct coro *current = SvSTATE_current;
2519 SV **throwp = self == current ? &coro_throw : &self->throw; 2740 SV **throwp = self == current ? &CORO_THROW : &self->except;
2520 SvREFCNT_dec (*throwp); 2741 SvREFCNT_dec (*throwp);
2521 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 2742 *throwp = SvOK (throw) ? newSVsv (throw) : 0;
2522} 2743}
2523 2744
2524void 2745void
2613 coroapi.ready = api_ready; 2834 coroapi.ready = api_ready;
2614 coroapi.is_ready = api_is_ready; 2835 coroapi.is_ready = api_is_ready;
2615 coroapi.nready = coro_nready; 2836 coroapi.nready = coro_nready;
2616 coroapi.current = coro_current; 2837 coroapi.current = coro_current;
2617 2838
2618 GCoroAPI = &coroapi; 2839 /*GCoroAPI = &coroapi;*/
2619 sv_setiv (sv, (IV)&coroapi); 2840 sv_setiv (sv, (IV)&coroapi);
2620 SvREADONLY_on (sv); 2841 SvREADONLY_on (sv);
2621 } 2842 }
2622} 2843}
2623 2844
2759 api_trace (aTHX_ coro_current, 0); 2980 api_trace (aTHX_ coro_current, 0);
2760 2981
2761 av_push (av_async_pool, newSVsv (coro_current)); 2982 av_push (av_async_pool, newSVsv (coro_current));
2762} 2983}
2763 2984
2985SV *
2986rouse_cb ()
2987 PROTOTYPE:
2988 CODE:
2989 RETVAL = coro_new_rouse_cb (aTHX);
2990 OUTPUT:
2991 RETVAL
2992
2993void
2994rouse_wait (SV *cb = 0)
2995 PROTOTYPE: ;$
2996 PPCODE:
2997 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
2998
2764 2999
2765MODULE = Coro::State PACKAGE = PerlIO::cede 3000MODULE = Coro::State PACKAGE = PerlIO::cede
2766 3001
2767BOOT: 3002BOOT:
2768 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3003 PerlIO_define_layer (aTHX_ &PerlIO_cede);
2769 3004
2770 3005
2771MODULE = Coro::State PACKAGE = Coro::Semaphore 3006MODULE = Coro::State PACKAGE = Coro::Semaphore
2772 3007
2773SV * 3008SV *
2774new (SV *klass, SV *count_ = 0) 3009new (SV *klass, SV *count = 0)
2775 CODE: 3010 CODE:
2776{ 3011 RETVAL = sv_bless (
2777 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ 3012 coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1),
2778 AV *av = newAV (); 3013 GvSTASH (CvGV (cv))
2779 av_push (av, newSViv (count_ && SvOK (count_) ? SvIV (count_) : 1)); 3014 );
2780 RETVAL = sv_bless (newRV_noinc ((SV *)av), GvSTASH (CvGV (cv))); 3015 OUTPUT:
2781} 3016 RETVAL
3017
3018# helper for Coro::Channel
3019SV *
3020_alloc (int count)
3021 CODE:
3022 RETVAL = coro_waitarray_new (aTHX_ count);
2782 OUTPUT: 3023 OUTPUT:
2783 RETVAL 3024 RETVAL
2784 3025
2785SV * 3026SV *
2786count (SV *self) 3027count (SV *self)
2798 3039
2799void 3040void
2800down (SV *self) 3041down (SV *self)
2801 CODE: 3042 CODE:
2802 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); 3043 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down);
3044
3045void
3046wait (SV *self)
3047 CODE:
3048 CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait);
2803 3049
2804void 3050void
2805try (SV *self) 3051try (SV *self)
2806 PPCODE: 3052 PPCODE:
2807{ 3053{
2819 XSRETURN_NO; 3065 XSRETURN_NO;
2820} 3066}
2821 3067
2822void 3068void
2823waiters (SV *self) 3069waiters (SV *self)
2824 CODE: 3070 PPCODE:
2825{ 3071{
2826 AV *av = (AV *)SvRV (self); 3072 AV *av = (AV *)SvRV (self);
3073 int wcount = AvFILLp (av) + 1 - 1;
2827 3074
2828 if (GIMME_V == G_SCALAR) 3075 if (GIMME_V == G_SCALAR)
2829 XPUSHs (sv_2mortal (newSVsv (AvARRAY (av)[0]))); 3076 XPUSHs (sv_2mortal (newSViv (wcount)));
2830 else 3077 else
2831 { 3078 {
2832 int i; 3079 int i;
2833 EXTEND (SP, AvFILLp (av) + 1 - 1); 3080 EXTEND (SP, wcount);
2834 for (i = 1; i <= AvFILLp (av); ++i) 3081 for (i = 1; i <= wcount; ++i)
2835 PUSHs (newSVsv (AvARRAY (av)[i])); 3082 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
2836 } 3083 }
2837} 3084}
3085
3086MODULE = Coro::State PACKAGE = Coro::Signal
3087
3088SV *
3089new (SV *klass)
3090 CODE:
3091 RETVAL = sv_bless (
3092 coro_waitarray_new (aTHX_ 0),
3093 GvSTASH (CvGV (cv))
3094 );
3095 OUTPUT:
3096 RETVAL
3097
3098void
3099wait (SV *self)
3100 CODE:
3101 CORO_EXECUTE_SLF_XS (slf_init_signal_wait);
3102
3103void
3104broadcast (SV *self)
3105 CODE:
3106{
3107 AV *av = (AV *)SvRV (self);
3108 coro_signal_wake (aTHX_ av, AvFILLp (av));
3109}
3110
3111void
3112send (SV *self)
3113 CODE:
3114{
3115 AV *av = (AV *)SvRV (self);
3116
3117 if (AvFILLp (av))
3118 coro_signal_wake (aTHX_ av, 1);
3119 else
3120 SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */
3121}
3122
3123IV
3124awaited (SV *self)
3125 CODE:
3126 RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1;
3127 OUTPUT:
3128 RETVAL
2838 3129
2839 3130
2840MODULE = Coro::State PACKAGE = Coro::AnyEvent 3131MODULE = Coro::State PACKAGE = Coro::AnyEvent
2841 3132
2842BOOT: 3133BOOT:
2857 sv_setsv (sv_activity, &PL_sv_undef); 3148 sv_setsv (sv_activity, &PL_sv_undef);
2858 if (coro_nready >= incede) 3149 if (coro_nready >= incede)
2859 { 3150 {
2860 PUSHMARK (SP); 3151 PUSHMARK (SP);
2861 PUTBACK; 3152 PUTBACK;
2862 call_pv ("Coro::AnyEvent::_activity", G_DISCARD | G_EVAL); 3153 call_pv ("Coro::AnyEvent::_activity", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2863 SPAGAIN;
2864 } 3154 }
2865 3155
2866 --incede; 3156 --incede;
2867} 3157}
2868 3158
2874 CODE: 3164 CODE:
2875{ 3165{
2876 HV *st; 3166 HV *st;
2877 GV *gvp; 3167 GV *gvp;
2878 CV *req_cv = sv_2cv (req, &st, &gvp, 0); 3168 CV *req_cv = sv_2cv (req, &st, &gvp, 0);
3169 /* newXSproto doesn't return the CV on 5.8 */
2879 CV *slf_cv = newXSproto (target, coro_aio_req_xs, __FILE__, proto); 3170 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
3171 sv_setpv ((SV *)slf_cv, proto);
2880 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 3172 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
2881} 3173}
2882 3174

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines