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.288 by root, Mon Nov 17 07:14:50 2008 UTC vs.
Revision 1.306 by root, Wed Nov 19 11:11:10 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
120#endif 120#endif
121 121
122/* 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
123 * portable way as possible. */ 123 * portable way as possible. */
124#if __GNUC__ >= 4 124#if __GNUC__ >= 4
125# define dSTACKLEVEL int stacklevel_dummy
125# define dSTACKLEVEL void *stacklevel = __builtin_frame_address (0) 126# define STACKLEVEL __builtin_frame_address (0)
126#else 127#else
127# define dSTACKLEVEL volatile void *stacklevel = (volatile void *)&stacklevel 128# define dSTACKLEVEL volatile void *stacklevel
129# define STACKLEVEL ((void *)&stacklevel)
128#endif 130#endif
129 131
130#define IN_DESTRUCT (PL_main_cv == Nullcv) 132#define IN_DESTRUCT (PL_main_cv == Nullcv)
131 133
132#if __GNUC__ >= 3 134#if __GNUC__ >= 3
143#define expect_true(expr) expect ((expr) != 0, 1) 145#define expect_true(expr) expect ((expr) != 0, 1)
144 146
145#define NOINLINE attribute ((noinline)) 147#define NOINLINE attribute ((noinline))
146 148
147#include "CoroAPI.h" 149#include "CoroAPI.h"
150#define GCoroAPI (&coroapi) /* very sneaky */
148 151
149#ifdef USE_ITHREADS 152#ifdef USE_ITHREADS
150# if CORO_PTHREAD 153# if CORO_PTHREAD
151static void *coro_thx; 154static void *coro_thx;
152# endif 155# endif
153#endif 156#endif
154 157
155static 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);
156 163
157static U32 cctx_gen; 164static U32 cctx_gen;
158static size_t cctx_stacksize = CORO_STACKSIZE; 165static size_t cctx_stacksize = CORO_STACKSIZE;
159static struct CoroAPI coroapi; 166static struct CoroAPI coroapi;
160static AV *main_mainstack; /* used to differentiate between $main and others */ 167static AV *main_mainstack; /* used to differentiate between $main and others */
250 /* statistics */ 257 /* statistics */
251 int usecount; /* number of transfers to this coro */ 258 int usecount; /* number of transfers to this coro */
252 259
253 /* coro process data */ 260 /* coro process data */
254 int prio; 261 int prio;
255 SV *throw; /* exception to be thrown */ 262 SV *except; /* exception to be thrown */
263 SV *rouse_cb;
256 264
257 /* async_pool */ 265 /* async_pool */
258 SV *saved_deffh; 266 SV *saved_deffh;
259 267
260 /* linked list */ 268 /* linked list */
266 274
267/* the following variables are effectively part of the perl context */ 275/* the following variables are effectively part of the perl context */
268/* and get copied between struct coro and these variables */ 276/* and get copied between struct coro and these variables */
269/* the mainr easonw e don't support windows process emulation */ 277/* the mainr easonw e don't support windows process emulation */
270static struct CoroSLF slf_frame; /* the current slf frame */ 278static struct CoroSLF slf_frame; /* the current slf frame */
271static SV *coro_throw;
272 279
273/** Coro ********************************************************************/ 280/** Coro ********************************************************************/
274 281
275#define PRIO_MAX 3 282#define PRIO_MAX 3
276#define PRIO_HIGH 1 283#define PRIO_HIGH 1
442 else 449 else
443 { 450 {
444#if CORO_PREFER_PERL_FUNCTIONS 451#if CORO_PREFER_PERL_FUNCTIONS
445 /* this is probably cleaner? but also slower! */ 452 /* this is probably cleaner? but also slower! */
446 /* in practise, it seems to be less stable */ 453 /* in practise, it seems to be less stable */
447 CV *cp = Perl_cv_clone (cv); 454 CV *cp = Perl_cv_clone (aTHX_ cv);
448 CvPADLIST (cv) = CvPADLIST (cp); 455 CvPADLIST (cv) = CvPADLIST (cp);
449 CvPADLIST (cp) = 0; 456 CvPADLIST (cp) = 0;
450 SvREFCNT_dec (cp); 457 SvREFCNT_dec (cp);
451#else 458#else
452 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 459 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv);
505 512
506 PUTBACK; 513 PUTBACK;
507 } 514 }
508 515
509 slf_frame = c->slf_frame; 516 slf_frame = c->slf_frame;
510 coro_throw = c->throw; 517 CORO_THROW = c->except;
511} 518}
512 519
513static void 520static void
514save_perl (pTHX_ Coro__State c) 521save_perl (pTHX_ Coro__State c)
515{ 522{
516 c->throw = coro_throw; 523 c->except = CORO_THROW;
517 c->slf_frame = slf_frame; 524 c->slf_frame = slf_frame;
518 525
519 { 526 {
520 dSP; 527 dSP;
521 I32 cxix = cxstack_ix; 528 I32 cxix = cxstack_ix;
596 * of perl.c:init_stacks, except that it uses less memory 603 * of perl.c:init_stacks, except that it uses less memory
597 * on the (sometimes correct) assumption that coroutines do 604 * on the (sometimes correct) assumption that coroutines do
598 * not usually need a lot of stackspace. 605 * not usually need a lot of stackspace.
599 */ 606 */
600#if CORO_PREFER_PERL_FUNCTIONS 607#if CORO_PREFER_PERL_FUNCTIONS
601# define coro_init_stacks init_stacks 608# define coro_init_stacks(thx) init_stacks ()
602#else 609#else
603static void 610static void
604coro_init_stacks (pTHX) 611coro_init_stacks (pTHX)
605{ 612{
606 PL_curstackinfo = new_stackinfo(32, 8); 613 PL_curstackinfo = new_stackinfo(32, 8);
814slf_check_nop (pTHX_ struct CoroSLF *frame) 821slf_check_nop (pTHX_ struct CoroSLF *frame)
815{ 822{
816 return 0; 823 return 0;
817} 824}
818 825
826static UNOP coro_setup_op;
827
819static 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 */
820coro_setup (pTHX_ struct coro *coro) 829coro_setup (pTHX_ struct coro *coro)
821{ 830{
822 /* 831 /*
823 * emulate part of the perl startup here. 832 * emulate part of the perl startup here.
851 { 860 {
852 dSP; 861 dSP;
853 UNOP myop; 862 UNOP myop;
854 863
855 Zero (&myop, 1, UNOP); 864 Zero (&myop, 1, UNOP);
856 myop.op_next = Nullop; 865 myop.op_next = Nullop;
857 myop.op_flags = OPf_WANT_VOID; 866 myop.op_flags = OPf_WANT_VOID;
858 867
859 PUSHMARK (SP); 868 PUSHMARK (SP);
860 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 869 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
861 PUTBACK; 870 PUTBACK;
868 * 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.
869 */ 878 */
870 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 */
871 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 */
872 881
873 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;
874} 892}
875 893
876static void 894static void
877coro_destruct (pTHX_ struct coro *coro) 895coro_destruct (pTHX_ struct coro *coro)
878{ 896{
901 SvREFCNT_dec (GvSV (irsgv)); 919 SvREFCNT_dec (GvSV (irsgv));
902 920
903 SvREFCNT_dec (PL_diehook); 921 SvREFCNT_dec (PL_diehook);
904 SvREFCNT_dec (PL_warnhook); 922 SvREFCNT_dec (PL_warnhook);
905 923
924 SvREFCNT_dec (CORO_THROW);
906 SvREFCNT_dec (coro->saved_deffh); 925 SvREFCNT_dec (coro->saved_deffh);
907 SvREFCNT_dec (coro_throw); 926 SvREFCNT_dec (coro->rouse_cb);
908 927
909 coro_destruct_stacks (aTHX); 928 coro_destruct_stacks (aTHX);
910} 929}
911 930
912INLINE void 931INLINE void
1041 1060
1042 TAINT_NOT; 1061 TAINT_NOT;
1043 return 0; 1062 return 0;
1044} 1063}
1045 1064
1065static struct coro_cctx *cctx_ssl_cctx;
1066static struct CoroSLF cctx_ssl_frame;
1067
1046static void 1068static void
1047prepare_set_stacklevel (struct coro_transfer_args *ta, struct coro_cctx *cctx) 1069slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1048{ 1070{
1049 ta->prev = (struct coro *)cctx; 1071 ta->prev = (struct coro *)cctx_ssl_cctx;
1050 ta->next = 0; 1072 ta->next = 0;
1051} 1073}
1052 1074
1053/* inject a fake call to Coro::State::_cctx_init into the execution */ 1075static int
1054/* _cctx_init should be careful, as it could be called at almost any time */ 1076slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1055/* during execution of a perl program */ 1077{
1056/* 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 */
1057static void NOINLINE 1084static void NOINLINE
1058cctx_prepare (pTHX_ coro_cctx *cctx) 1085cctx_prepare (pTHX_ coro_cctx *cctx)
1059{ 1086{
1060 dSP;
1061 UNOP myop;
1062
1063 PL_top_env = &PL_start_env; 1087 PL_top_env = &PL_start_env;
1064 1088
1065 if (cctx->flags & CC_TRACE) 1089 if (cctx->flags & CC_TRACE)
1066 PL_runops = runops_trace; 1090 PL_runops = runops_trace;
1067 1091
1068 Zero (&myop, 1, UNOP); 1092 /* we already must be executing an SLF op, there is no other valid way
1069 myop.op_next = PL_op; 1093 * that can lead to creation of a new cctx */
1070 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));
1071 1096
1072 PUSHMARK (SP); 1097 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */
1073 EXTEND (SP, 2); 1098 cctx_ssl_cctx = cctx;
1074 PUSHs (sv_2mortal (newSViv ((IV)cctx))); 1099 cctx_ssl_frame = slf_frame;
1075 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 1100
1076 PUTBACK; 1101 slf_frame.prepare = slf_prepare_set_stacklevel;
1077 PL_op = (OP *)&myop; 1102 slf_frame.check = slf_check_set_stacklevel;
1078 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1079 SPAGAIN;
1080} 1103}
1081 1104
1082/* 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 */
1083INLINE void 1106INLINE void
1084transfer_tail (pTHX) 1107transfer_tail (pTHX)
1300 dSTACKLEVEL; 1323 dSTACKLEVEL;
1301 1324
1302 /* sometimes transfer is only called to set idle_sp */ 1325 /* sometimes transfer is only called to set idle_sp */
1303 if (expect_false (!next)) 1326 if (expect_false (!next))
1304 { 1327 {
1305 ((coro_cctx *)prev)->idle_sp = (void *)stacklevel; 1328 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1306 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 */
1307 } 1330 }
1308 else if (expect_true (prev != next)) 1331 else if (expect_true (prev != next))
1309 { 1332 {
1310 coro_cctx *prev__cctx; 1333 coro_cctx *prev__cctx;
1335 1358
1336 prev__cctx = prev->cctx; 1359 prev__cctx = prev->cctx;
1337 1360
1338 /* possibly untie and reuse the cctx */ 1361 /* possibly untie and reuse the cctx */
1339 if (expect_true ( 1362 if (expect_true (
1340 prev__cctx->idle_sp == (void *)stacklevel 1363 prev__cctx->idle_sp == STACKLEVEL
1341 && !(prev__cctx->flags & CC_TRACE) 1364 && !(prev__cctx->flags & CC_TRACE)
1342 && !force_cctx 1365 && !force_cctx
1343 )) 1366 ))
1344 { 1367 {
1345 /* 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 */
1476 1499
1477 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1500 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1478 TRANSFER (ta, 1); 1501 TRANSFER (ta, 1);
1479} 1502}
1480 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
1481/** Coro ********************************************************************/ 1527/** Coro ********************************************************************/
1482 1528
1483INLINE void 1529INLINE void
1484coro_enq (pTHX_ struct coro *coro) 1530coro_enq (pTHX_ struct coro *coro)
1485{ 1531{
1528 ENTER; 1574 ENTER;
1529 SAVETMPS; 1575 SAVETMPS;
1530 1576
1531 PUSHMARK (SP); 1577 PUSHMARK (SP);
1532 PUTBACK; 1578 PUTBACK;
1533 call_sv (sv_hook, G_DISCARD); 1579 call_sv (sv_hook, G_VOID | G_DISCARD);
1534 SPAGAIN;
1535 1580
1536 FREETMPS; 1581 FREETMPS;
1537 LEAVE; 1582 LEAVE;
1538 } 1583 }
1539 1584
1566 ENTER; 1611 ENTER;
1567 SAVETMPS; 1612 SAVETMPS;
1568 1613
1569 PUSHMARK (SP); 1614 PUSHMARK (SP);
1570 PUTBACK; 1615 PUTBACK;
1571 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); 1616 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD);
1572 SPAGAIN;
1573 1617
1574 FREETMPS; 1618 FREETMPS;
1575 LEAVE; 1619 LEAVE;
1576 continue; 1620 continue;
1577 } 1621 }
1687 coro->slot->runops = RUNOPS_DEFAULT; 1731 coro->slot->runops = RUNOPS_DEFAULT;
1688 } 1732 }
1689} 1733}
1690 1734
1691/*****************************************************************************/ 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/*****************************************************************************/
1692/* schedule-like-function opcode (SLF) */ 1847/* schedule-like-function opcode (SLF) */
1693 1848
1694static 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 */
1695static const CV *slf_cv; 1850static const CV *slf_cv;
1696static SV **slf_argv; 1851static SV **slf_argv;
1717 1872
1718 RETURNOP (slf_restore.op_first); 1873 RETURNOP (slf_restore.op_first);
1719} 1874}
1720 1875
1721static void 1876static void
1722slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1723{
1724 prepare_set_stacklevel (ta, (struct coro_cctx *)slf_frame.data);
1725}
1726
1727static void
1728slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1729{
1730 assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1));
1731
1732 frame->prepare = slf_prepare_set_stacklevel;
1733 frame->check = slf_check_nop;
1734 frame->data = (void *)SvIV (arg [0]);
1735}
1736
1737static void
1738slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta) 1877slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta)
1739{ 1878{
1740 SV **arg = (SV **)slf_frame.data; 1879 SV **arg = (SV **)slf_frame.data;
1741 1880
1742 prepare_transfer (aTHX_ ta, arg [0], arg [1]); 1881 prepare_transfer (aTHX_ ta, arg [0], arg [1]);
1771slf_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)
1772{ 1911{
1773 frame->prepare = prepare_cede_notself; 1912 frame->prepare = prepare_cede_notself;
1774 frame->check = slf_check_nop; 1913 frame->check = slf_check_nop;
1775} 1914}
1776
1777/* we hijack an hopefully unused CV flag for our purposes */
1778#define CVf_SLF 0x4000
1779 1915
1780/* 1916/*
1781 * these not obviously related functions are all rolled into one 1917 * these not obviously related functions are all rolled into one
1782 * 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
1783 * stack offset 1919 * stack offset
1839 } 1975 }
1840 while (slf_frame.check (aTHX_ &slf_frame)); 1976 while (slf_frame.check (aTHX_ &slf_frame));
1841 1977
1842 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 */
1843 1979
1844 /* return value handling - mostly like entersub */
1845 {
1846 dSP;
1847 SV **bot = PL_stack_base + checkmark;
1848 int gimme = GIMME_V;
1849
1850 /* make sure we put something on the stack in scalar context */
1851 if (gimme == G_SCALAR)
1852 {
1853 if (sp == bot)
1854 XPUSHs (&PL_sv_undef);
1855
1856 SP = bot + 1;
1857 }
1858
1859 PUTBACK;
1860 }
1861
1862 /* exception handling */ 1980 /* exception handling */
1863 if (expect_false (coro_throw)) 1981 if (expect_false (CORO_THROW))
1864 { 1982 {
1865 SV *exception = sv_2mortal (coro_throw); 1983 SV *exception = sv_2mortal (CORO_THROW);
1866 1984
1867 coro_throw = 0; 1985 CORO_THROW = 0;
1868 sv_setsv (ERRSV, exception); 1986 sv_setsv (ERRSV, exception);
1869 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;
1870 } 2005 }
1871 2006
1872 return NORMAL; 2007 return NORMAL;
1873} 2008}
1874 2009
1996 PerlIOBuf_get_cnt, 2131 PerlIOBuf_get_cnt,
1997 PerlIOBuf_set_ptrcnt, 2132 PerlIOBuf_set_ptrcnt,
1998}; 2133};
1999 2134
2000/*****************************************************************************/ 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 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */
2150 AvMAX (av) = 1;
2151 AvFILLp (av) = 0;
2152 ary [0] = newSViv (count);
2153
2154 return newRV_noinc ((SV *)av);
2155}
2156
2001/* Coro::Semaphore */ 2157/* semaphore */
2002 2158
2003static void 2159static void
2004coro_semaphore_adjust (pTHX_ AV *av, IV adjust) 2160coro_semaphore_adjust (pTHX_ AV *av, IV adjust)
2005{ 2161{
2006 SV *count_sv = AvARRAY (av)[0]; 2162 SV *count_sv = AvARRAY (av)[0];
2018 AvARRAY (av)[0] = AvARRAY (av)[1]; 2174 AvARRAY (av)[0] = AvARRAY (av)[1];
2019 AvARRAY (av)[1] = count_sv; 2175 AvARRAY (av)[1] = count_sv;
2020 cb = av_shift (av); 2176 cb = av_shift (av);
2021 2177
2022 if (SvOBJECT (cb)) 2178 if (SvOBJECT (cb))
2179 {
2023 api_ready (aTHX_ cb); 2180 api_ready (aTHX_ cb);
2024 else 2181 --count;
2025 croak ("callbacks not yet supported"); 2182 }
2183 else if (SvTYPE (cb) == SVt_PVCV)
2184 {
2185 dSP;
2186 PUSHMARK (SP);
2187 XPUSHs (sv_2mortal (newRV_inc ((SV *)av)));
2188 PUTBACK;
2189 call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR);
2190 }
2026 2191
2027 SvREFCNT_dec (cb); 2192 SvREFCNT_dec (cb);
2028
2029 --count;
2030 } 2193 }
2031} 2194}
2032 2195
2033static void 2196static void
2034coro_semaphore_on_destroy (pTHX_ struct coro *coro) 2197coro_semaphore_on_destroy (pTHX_ struct coro *coro)
2036 /* call $sem->adjust (0) to possibly wake up some other waiters */ 2199 /* call $sem->adjust (0) to possibly wake up some other waiters */
2037 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); 2200 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0);
2038} 2201}
2039 2202
2040static int 2203static int
2041slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) 2204slf_check_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, int acquire)
2042{ 2205{
2043 AV *av = (AV *)frame->data; 2206 AV *av = (AV *)frame->data;
2044 SV *count_sv = AvARRAY (av)[0]; 2207 SV *count_sv = AvARRAY (av)[0];
2045 2208
2209 /* if we are about to throw, don't actually acquire the lock, just throw */
2210 if (CORO_THROW)
2211 return 0;
2046 if (SvIVX (count_sv) > 0) 2212 else if (SvIVX (count_sv) > 0)
2047 { 2213 {
2048 SvSTATE_current->on_destroy = 0; 2214 SvSTATE_current->on_destroy = 0;
2215
2216 if (acquire)
2049 SvIVX (count_sv) = SvIVX (count_sv) - 1; 2217 SvIVX (count_sv) = SvIVX (count_sv) - 1;
2218 else
2219 coro_semaphore_adjust (aTHX_ av, 0);
2220
2050 return 0; 2221 return 0;
2051 } 2222 }
2052 else 2223 else
2053 { 2224 {
2054 int i; 2225 int i;
2063 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2234 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2064 return 1; 2235 return 1;
2065 } 2236 }
2066} 2237}
2067 2238
2068static void 2239static int
2240slf_check_semaphore_down (pTHX_ struct CoroSLF *frame)
2241{
2242 return slf_check_semaphore_down_or_wait (aTHX_ frame, 1);
2243}
2244
2245static int
2246slf_check_semaphore_wait (pTHX_ struct CoroSLF *frame)
2247{
2248 return slf_check_semaphore_down_or_wait (aTHX_ frame, 0);
2249}
2250
2251static void
2069slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2252slf_init_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2070{ 2253{
2071 AV *av = (AV *)SvRV (arg [0]); 2254 AV *av = (AV *)SvRV (arg [0]);
2072 2255
2073 if (SvIVX (AvARRAY (av)[0]) > 0) 2256 if (SvIVX (AvARRAY (av)[0]) > 0)
2074 { 2257 {
2075 frame->data = (void *)av; 2258 frame->data = (void *)av;
2076 frame->prepare = prepare_nop; 2259 frame->prepare = prepare_nop;
2077 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2078 } 2260 }
2079 else 2261 else
2080 { 2262 {
2081 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2263 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2082 2264
2083 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); 2265 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av));
2084 frame->prepare = prepare_schedule; 2266 frame->prepare = prepare_schedule;
2085 2267
2086 /* to avoid race conditions when a woken-up coro gets terminated */ 2268 /* to avoid race conditions when a woken-up coro gets terminated */
2087 /* we arrange for a temporary on_destroy that calls adjust (0) */ 2269 /* we arrange for a temporary on_destroy that calls adjust (0) */
2088 assert (!SvSTATE_current->on_destroy);//D
2089 SvSTATE_current->on_destroy = coro_semaphore_on_destroy; 2270 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2090 } 2271 }
2272}
2091 2273
2274static void
2275slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2276{
2277 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items);
2092 frame->check = slf_check_semaphore_down; 2278 frame->check = slf_check_semaphore_down;
2093
2094} 2279}
2095 2280
2096/*****************************************************************************/ 2281static void
2097/* gensub: simple closure generation utility */ 2282slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2098
2099#define GENSUB_ARG CvXSUBANY (cv).any_ptr
2100
2101/* create a closure from XS, returns a code reference */
2102/* the arg can be accessed via GENSUB_ARG from the callback */
2103/* the callback must use dXSARGS/XSRETURN */
2104static SV *
2105gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg)
2106{ 2283{
2107 CV *cv = (CV *)newSV (0); 2284 if (items >= 2)
2285 {
2286 /* callback form */
2287 AV *av = (AV *)SvRV (arg [0]);
2288 HV *st;
2289 GV *gvp;
2290 CV *cb_cv = sv_2cv (arg [1], &st, &gvp, 0);
2108 2291
2109 sv_upgrade ((SV *)cv, SVt_PVCV); 2292 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv));
2110 2293
2111 CvANON_on (cv); 2294 if (SvIVX (AvARRAY (av)[0]) > 0)
2112 CvISXSUB_on (cv); 2295 coro_semaphore_adjust (aTHX_ av, 0);
2113 CvXSUB (cv) = xsub;
2114 GENSUB_ARG = arg;
2115 2296
2116 return newRV_noinc ((SV *)cv); 2297 frame->prepare = prepare_nop;
2298 frame->check = slf_check_nop;
2299 }
2300 else
2301 {
2302 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items);
2303 frame->check = slf_check_semaphore_wait;
2304 }
2305}
2306
2307/* signal */
2308
2309static void
2310coro_signal_wake (pTHX_ AV *av, int count)
2311{
2312 SvIVX (AvARRAY (av)[0]) = 0;
2313
2314 /* now signal count waiters */
2315 while (count > 0 && AvFILLp (av) > 0)
2316 {
2317 SV *cb;
2318
2319 /* swap first two elements so we can shift a waiter */
2320 cb = AvARRAY (av)[0];
2321 AvARRAY (av)[0] = AvARRAY (av)[1];
2322 AvARRAY (av)[1] = cb;
2323
2324 cb = av_shift (av);
2325
2326 api_ready (aTHX_ cb);
2327 sv_setiv (cb, 0); /* signal waiter */
2328 SvREFCNT_dec (cb);
2329
2330 --count;
2331 }
2332}
2333
2334static int
2335slf_check_signal_wait (pTHX_ struct CoroSLF *frame)
2336{
2337 /* if we are about to throw, also stop waiting */
2338 return SvROK ((SV *)frame->data) && !CORO_THROW;
2339}
2340
2341static void
2342slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2343{
2344 AV *av = (AV *)SvRV (arg [0]);
2345
2346 if (SvIVX (AvARRAY (av)[0]))
2347 {
2348 SvIVX (AvARRAY (av)[0]) = 0;
2349 frame->prepare = prepare_nop;
2350 frame->check = slf_check_nop;
2351 }
2352 else
2353 {
2354 SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */
2355
2356 av_push (av, waiter);
2357
2358 frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */
2359 frame->prepare = prepare_schedule;
2360 frame->check = slf_check_signal_wait;
2361 }
2117} 2362}
2118 2363
2119/*****************************************************************************/ 2364/*****************************************************************************/
2120/* Coro::AIO */ 2365/* Coro::AIO */
2121 2366
2170 2415
2171static int 2416static int
2172slf_check_aio_req (pTHX_ struct CoroSLF *frame) 2417slf_check_aio_req (pTHX_ struct CoroSLF *frame)
2173{ 2418{
2174 AV *state = (AV *)frame->data; 2419 AV *state = (AV *)frame->data;
2420
2421 /* if we are about to throw, return early */
2422 /* this does not cancel the aio request, but at least */
2423 /* it quickly returns */
2424 if (CORO_THROW)
2425 return 0;
2175 2426
2176 /* one element that is an RV? repeat! */ 2427 /* one element that is an RV? repeat! */
2177 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) 2428 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0]))
2178 return 1; 2429 return 1;
2179 2430
2376 for (i = 1; i < items; i++) 2627 for (i = 1; i < items; i++)
2377 av_push (coro->args, newSVsv (ST (i))); 2628 av_push (coro->args, newSVsv (ST (i)));
2378} 2629}
2379 OUTPUT: 2630 OUTPUT:
2380 RETVAL 2631 RETVAL
2381
2382void
2383_set_stacklevel (...)
2384 CODE:
2385 CORO_EXECUTE_SLF_XS (slf_init_set_stacklevel);
2386 2632
2387void 2633void
2388transfer (...) 2634transfer (...)
2389 PROTOTYPE: $$ 2635 PROTOTYPE: $$
2390 CODE: 2636 CODE:
2516throw (Coro::State self, SV *throw = &PL_sv_undef) 2762throw (Coro::State self, SV *throw = &PL_sv_undef)
2517 PROTOTYPE: $;$ 2763 PROTOTYPE: $;$
2518 CODE: 2764 CODE:
2519{ 2765{
2520 struct coro *current = SvSTATE_current; 2766 struct coro *current = SvSTATE_current;
2521 SV **throwp = self == current ? &coro_throw : &self->throw; 2767 SV **throwp = self == current ? &CORO_THROW : &self->except;
2522 SvREFCNT_dec (*throwp); 2768 SvREFCNT_dec (*throwp);
2523 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 2769 *throwp = SvOK (throw) ? newSVsv (throw) : 0;
2524} 2770}
2525 2771
2526void 2772void
2615 coroapi.ready = api_ready; 2861 coroapi.ready = api_ready;
2616 coroapi.is_ready = api_is_ready; 2862 coroapi.is_ready = api_is_ready;
2617 coroapi.nready = coro_nready; 2863 coroapi.nready = coro_nready;
2618 coroapi.current = coro_current; 2864 coroapi.current = coro_current;
2619 2865
2620 GCoroAPI = &coroapi; 2866 /*GCoroAPI = &coroapi;*/
2621 sv_setiv (sv, (IV)&coroapi); 2867 sv_setiv (sv, (IV)&coroapi);
2622 SvREADONLY_on (sv); 2868 SvREADONLY_on (sv);
2623 } 2869 }
2624} 2870}
2625 2871
2761 api_trace (aTHX_ coro_current, 0); 3007 api_trace (aTHX_ coro_current, 0);
2762 3008
2763 av_push (av_async_pool, newSVsv (coro_current)); 3009 av_push (av_async_pool, newSVsv (coro_current));
2764} 3010}
2765 3011
3012SV *
3013rouse_cb ()
3014 PROTOTYPE:
3015 CODE:
3016 RETVAL = coro_new_rouse_cb (aTHX);
3017 OUTPUT:
3018 RETVAL
3019
3020void
3021rouse_wait (...)
3022 PROTOTYPE: ;$
3023 PPCODE:
3024 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3025
2766 3026
2767MODULE = Coro::State PACKAGE = PerlIO::cede 3027MODULE = Coro::State PACKAGE = PerlIO::cede
2768 3028
2769BOOT: 3029BOOT:
2770 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3030 PerlIO_define_layer (aTHX_ &PerlIO_cede);
2771 3031
2772 3032
2773MODULE = Coro::State PACKAGE = Coro::Semaphore 3033MODULE = Coro::State PACKAGE = Coro::Semaphore
2774 3034
2775SV * 3035SV *
2776new (SV *klass, SV *count_ = 0) 3036new (SV *klass, SV *count = 0)
2777 CODE: 3037 CODE:
2778{ 3038 RETVAL = sv_bless (
2779 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ 3039 coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1),
2780 AV *av = newAV (); 3040 GvSTASH (CvGV (cv))
2781 av_push (av, newSViv (count_ && SvOK (count_) ? SvIV (count_) : 1)); 3041 );
2782 RETVAL = sv_bless (newRV_noinc ((SV *)av), GvSTASH (CvGV (cv))); 3042 OUTPUT:
2783} 3043 RETVAL
3044
3045# helper for Coro::Channel
3046SV *
3047_alloc (int count)
3048 CODE:
3049 RETVAL = coro_waitarray_new (aTHX_ count);
2784 OUTPUT: 3050 OUTPUT:
2785 RETVAL 3051 RETVAL
2786 3052
2787SV * 3053SV *
2788count (SV *self) 3054count (SV *self)
2797 adjust = 1 3063 adjust = 1
2798 CODE: 3064 CODE:
2799 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); 3065 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1);
2800 3066
2801void 3067void
2802down (SV *self) 3068down (...)
2803 CODE: 3069 CODE:
2804 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); 3070 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down);
3071
3072void
3073wait (...)
3074 CODE:
3075 CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait);
2805 3076
2806void 3077void
2807try (SV *self) 3078try (SV *self)
2808 PPCODE: 3079 PPCODE:
2809{ 3080{
2821 XSRETURN_NO; 3092 XSRETURN_NO;
2822} 3093}
2823 3094
2824void 3095void
2825waiters (SV *self) 3096waiters (SV *self)
2826 CODE: 3097 PPCODE:
2827{ 3098{
2828 AV *av = (AV *)SvRV (self); 3099 AV *av = (AV *)SvRV (self);
3100 int wcount = AvFILLp (av) + 1 - 1;
2829 3101
2830 if (GIMME_V == G_SCALAR) 3102 if (GIMME_V == G_SCALAR)
2831 XPUSHs (sv_2mortal (newSVsv (AvARRAY (av)[0]))); 3103 XPUSHs (sv_2mortal (newSViv (wcount)));
2832 else 3104 else
2833 { 3105 {
2834 int i; 3106 int i;
2835 EXTEND (SP, AvFILLp (av) + 1 - 1); 3107 EXTEND (SP, wcount);
2836 for (i = 1; i <= AvFILLp (av); ++i) 3108 for (i = 1; i <= wcount; ++i)
2837 PUSHs (newSVsv (AvARRAY (av)[i])); 3109 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
2838 } 3110 }
2839} 3111}
3112
3113MODULE = Coro::State PACKAGE = Coro::Signal
3114
3115SV *
3116new (SV *klass)
3117 CODE:
3118 RETVAL = sv_bless (
3119 coro_waitarray_new (aTHX_ 0),
3120 GvSTASH (CvGV (cv))
3121 );
3122 OUTPUT:
3123 RETVAL
3124
3125void
3126wait (...)
3127 CODE:
3128 CORO_EXECUTE_SLF_XS (slf_init_signal_wait);
3129
3130void
3131broadcast (SV *self)
3132 CODE:
3133{
3134 AV *av = (AV *)SvRV (self);
3135 coro_signal_wake (aTHX_ av, AvFILLp (av));
3136}
3137
3138void
3139send (SV *self)
3140 CODE:
3141{
3142 AV *av = (AV *)SvRV (self);
3143
3144 if (AvFILLp (av))
3145 coro_signal_wake (aTHX_ av, 1);
3146 else
3147 SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */
3148}
3149
3150IV
3151awaited (SV *self)
3152 CODE:
3153 RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1;
3154 OUTPUT:
3155 RETVAL
2840 3156
2841 3157
2842MODULE = Coro::State PACKAGE = Coro::AnyEvent 3158MODULE = Coro::State PACKAGE = Coro::AnyEvent
2843 3159
2844BOOT: 3160BOOT:
2859 sv_setsv (sv_activity, &PL_sv_undef); 3175 sv_setsv (sv_activity, &PL_sv_undef);
2860 if (coro_nready >= incede) 3176 if (coro_nready >= incede)
2861 { 3177 {
2862 PUSHMARK (SP); 3178 PUSHMARK (SP);
2863 PUTBACK; 3179 PUTBACK;
2864 call_pv ("Coro::AnyEvent::_activity", G_DISCARD | G_EVAL); 3180 call_pv ("Coro::AnyEvent::_activity", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2865 SPAGAIN;
2866 } 3181 }
2867 3182
2868 --incede; 3183 --incede;
2869} 3184}
2870 3185

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines