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.290 by root, Tue Nov 18 05:51:38 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
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
254 /* statistics */ 257 /* statistics */
255 int usecount; /* number of transfers to this coro */ 258 int usecount; /* number of transfers to this coro */
256 259
257 /* coro process data */ 260 /* coro process data */
258 int prio; 261 int prio;
259 SV *throw; /* exception to be thrown */ 262 SV *except; /* exception to be thrown */
263 SV *rouse_cb;
260 264
261 /* async_pool */ 265 /* async_pool */
262 SV *saved_deffh; 266 SV *saved_deffh;
263 267
264 /* linked list */ 268 /* linked list */
270 274
271/* the following variables are effectively part of the perl context */ 275/* the following variables are effectively part of the perl context */
272/* and get copied between struct coro and these variables */ 276/* and get copied between struct coro and these variables */
273/* the mainr easonw e don't support windows process emulation */ 277/* the mainr easonw e don't support windows process emulation */
274static struct CoroSLF slf_frame; /* the current slf frame */ 278static struct CoroSLF slf_frame; /* the current slf frame */
275static SV *coro_throw;
276 279
277/** Coro ********************************************************************/ 280/** Coro ********************************************************************/
278 281
279#define PRIO_MAX 3 282#define PRIO_MAX 3
280#define PRIO_HIGH 1 283#define PRIO_HIGH 1
509 512
510 PUTBACK; 513 PUTBACK;
511 } 514 }
512 515
513 slf_frame = c->slf_frame; 516 slf_frame = c->slf_frame;
514 coro_throw = c->throw; 517 CORO_THROW = c->except;
515} 518}
516 519
517static void 520static void
518save_perl (pTHX_ Coro__State c) 521save_perl (pTHX_ Coro__State c)
519{ 522{
520 c->throw = coro_throw; 523 c->except = CORO_THROW;
521 c->slf_frame = slf_frame; 524 c->slf_frame = slf_frame;
522 525
523 { 526 {
524 dSP; 527 dSP;
525 I32 cxix = cxstack_ix; 528 I32 cxix = cxstack_ix;
857 { 860 {
858 dSP; 861 dSP;
859 UNOP myop; 862 UNOP myop;
860 863
861 Zero (&myop, 1, UNOP); 864 Zero (&myop, 1, UNOP);
862 myop.op_next = Nullop; 865 myop.op_next = Nullop;
863 myop.op_flags = OPf_WANT_VOID; 866 myop.op_flags = OPf_WANT_VOID;
864 867
865 PUSHMARK (SP); 868 PUSHMARK (SP);
866 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 869 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
867 PUTBACK; 870 PUTBACK;
875 */ 878 */
876 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 */
877 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 */
878 881
879 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ 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;
880 coro_setup_op.op_type = OP_CUSTOM; 884 coro_setup_op.op_type = OP_CUSTOM;
881 coro_setup_op.op_ppaddr = pp_slf; 885 coro_setup_op.op_ppaddr = pp_slf;
882 coro_setup_op.op_next = PL_op; 886 /* no flags required, as an init function won't be called */
883 887
884 PL_op = (OP *)&coro_setup_op; 888 PL_op = (OP *)&coro_setup_op;
885 889
886 /* copy throw, in case it was set before coro_setup */ 890 /* copy throw, in case it was set before coro_setup */
887 coro_throw = coro->throw; 891 CORO_THROW = coro->except;
888} 892}
889 893
890static void 894static void
891coro_destruct (pTHX_ struct coro *coro) 895coro_destruct (pTHX_ struct coro *coro)
892{ 896{
915 SvREFCNT_dec (GvSV (irsgv)); 919 SvREFCNT_dec (GvSV (irsgv));
916 920
917 SvREFCNT_dec (PL_diehook); 921 SvREFCNT_dec (PL_diehook);
918 SvREFCNT_dec (PL_warnhook); 922 SvREFCNT_dec (PL_warnhook);
919 923
924 SvREFCNT_dec (CORO_THROW);
920 SvREFCNT_dec (coro->saved_deffh); 925 SvREFCNT_dec (coro->saved_deffh);
921 SvREFCNT_dec (coro_throw); 926 SvREFCNT_dec (coro->rouse_cb);
922 927
923 coro_destruct_stacks (aTHX); 928 coro_destruct_stacks (aTHX);
924} 929}
925 930
926INLINE void 931INLINE void
1070static int 1075static int
1071slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1076slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1072{ 1077{
1073 *frame = cctx_ssl_frame; 1078 *frame = cctx_ssl_frame;
1074 1079
1075 return 0; 1080 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1076} 1081}
1077 1082
1078/* initialises PL_top_env and injects a pseudo-slf-call to sett he stacklevel */ 1083/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1079static void NOINLINE 1084static void NOINLINE
1080cctx_prepare (pTHX_ coro_cctx *cctx) 1085cctx_prepare (pTHX_ coro_cctx *cctx)
1081{ 1086{
1082 PL_top_env = &PL_start_env; 1087 PL_top_env = &PL_start_env;
1083 1088
1084 if (cctx->flags & CC_TRACE) 1089 if (cctx->flags & CC_TRACE)
1085 PL_runops = runops_trace; 1090 PL_runops = runops_trace;
1086 1091
1087 /* we already must be in an SLF call, there is no other valid way 1092 /* we already must be executing an SLF op, there is no other valid way
1088 * that can lead to creation of a new cctx */ 1093 * that can lead to creation of a new cctx */
1089 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", 1094 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1090 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1095 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1091 1096
1097 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */
1092 cctx_ssl_cctx = cctx; 1098 cctx_ssl_cctx = cctx;
1093 cctx_ssl_frame = slf_frame; 1099 cctx_ssl_frame = slf_frame;
1094 1100
1095 slf_frame.prepare = slf_prepare_set_stacklevel; 1101 slf_frame.prepare = slf_prepare_set_stacklevel;
1096 slf_frame.check = slf_check_set_stacklevel; 1102 slf_frame.check = slf_check_set_stacklevel;
1317 dSTACKLEVEL; 1323 dSTACKLEVEL;
1318 1324
1319 /* sometimes transfer is only called to set idle_sp */ 1325 /* sometimes transfer is only called to set idle_sp */
1320 if (expect_false (!next)) 1326 if (expect_false (!next))
1321 { 1327 {
1322 ((coro_cctx *)prev)->idle_sp = (void *)stacklevel; 1328 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1323 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 */
1324 } 1330 }
1325 else if (expect_true (prev != next)) 1331 else if (expect_true (prev != next))
1326 { 1332 {
1327 coro_cctx *prev__cctx; 1333 coro_cctx *prev__cctx;
1352 1358
1353 prev__cctx = prev->cctx; 1359 prev__cctx = prev->cctx;
1354 1360
1355 /* possibly untie and reuse the cctx */ 1361 /* possibly untie and reuse the cctx */
1356 if (expect_true ( 1362 if (expect_true (
1357 prev__cctx->idle_sp == (void *)stacklevel 1363 prev__cctx->idle_sp == STACKLEVEL
1358 && !(prev__cctx->flags & CC_TRACE) 1364 && !(prev__cctx->flags & CC_TRACE)
1359 && !force_cctx 1365 && !force_cctx
1360 )) 1366 ))
1361 { 1367 {
1362 /* 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 */
1493 1499
1494 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1500 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1495 TRANSFER (ta, 1); 1501 TRANSFER (ta, 1);
1496} 1502}
1497 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
1498/** Coro ********************************************************************/ 1527/** Coro ********************************************************************/
1499 1528
1500INLINE void 1529INLINE void
1501coro_enq (pTHX_ struct coro *coro) 1530coro_enq (pTHX_ struct coro *coro)
1502{ 1531{
1699 if (coro->flags & CF_RUNNING) 1728 if (coro->flags & CF_RUNNING)
1700 PL_runops = RUNOPS_DEFAULT; 1729 PL_runops = RUNOPS_DEFAULT;
1701 else 1730 else
1702 coro->slot->runops = RUNOPS_DEFAULT; 1731 coro->slot->runops = RUNOPS_DEFAULT;
1703 } 1732 }
1733}
1734
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;
1704} 1844}
1705 1845
1706/*****************************************************************************/ 1846/*****************************************************************************/
1707/* schedule-like-function opcode (SLF) */ 1847/* schedule-like-function opcode (SLF) */
1708 1848
1835 } 1975 }
1836 while (slf_frame.check (aTHX_ &slf_frame)); 1976 while (slf_frame.check (aTHX_ &slf_frame));
1837 1977
1838 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 */
1839 1979
1980 /* exception handling */
1981 if (expect_false (CORO_THROW))
1982 {
1983 SV *exception = sv_2mortal (CORO_THROW);
1984
1985 CORO_THROW = 0;
1986 sv_setsv (ERRSV, exception);
1987 croak (0);
1988 }
1989
1840 /* return value handling - mostly like entersub */ 1990 /* return value handling - mostly like entersub */
1841 /* make sure we put something on the stack in scalar context */ 1991 /* make sure we put something on the stack in scalar context */
1842 if (GIMME_V == G_SCALAR) 1992 if (GIMME_V == G_SCALAR)
1843 { 1993 {
1844 dSP; 1994 dSP;
1850 bot [1] = *sp; 2000 bot [1] = *sp;
1851 2001
1852 SP = bot + 1; 2002 SP = bot + 1;
1853 2003
1854 PUTBACK; 2004 PUTBACK;
1855 }
1856
1857 /* exception handling */
1858 if (expect_false (coro_throw))
1859 {
1860 SV *exception = sv_2mortal (coro_throw);
1861
1862 coro_throw = 0;
1863 sv_setsv (ERRSV, exception);
1864 croak (0);
1865 } 2005 }
1866 2006
1867 return NORMAL; 2007 return NORMAL;
1868} 2008}
1869 2009
1991 PerlIOBuf_get_cnt, 2131 PerlIOBuf_get_cnt,
1992 PerlIOBuf_set_ptrcnt, 2132 PerlIOBuf_set_ptrcnt,
1993}; 2133};
1994 2134
1995/*****************************************************************************/ 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
1996/* Coro::Semaphore */ 2156/* semaphore */
1997 2157
1998static void 2158static void
1999coro_semaphore_adjust (pTHX_ AV *av, IV adjust) 2159coro_semaphore_adjust (pTHX_ AV *av, IV adjust)
2000{ 2160{
2001 SV *count_sv = AvARRAY (av)[0]; 2161 SV *count_sv = AvARRAY (av)[0];
2031 /* call $sem->adjust (0) to possibly wake up some other waiters */ 2191 /* call $sem->adjust (0) to possibly wake up some other waiters */
2032 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); 2192 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0);
2033} 2193}
2034 2194
2035static int 2195static int
2036slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) 2196slf_check_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, int acquire)
2037{ 2197{
2038 AV *av = (AV *)frame->data; 2198 AV *av = (AV *)frame->data;
2039 SV *count_sv = AvARRAY (av)[0]; 2199 SV *count_sv = AvARRAY (av)[0];
2040 2200
2201 /* if we are about to throw, don't actually acquire the lock, just throw */
2202 if (CORO_THROW)
2203 return 0;
2041 if (SvIVX (count_sv) > 0) 2204 else if (SvIVX (count_sv) > 0)
2042 { 2205 {
2043 SvSTATE_current->on_destroy = 0; 2206 SvSTATE_current->on_destroy = 0;
2207
2208 if (acquire)
2044 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
2045 return 0; 2213 return 0;
2046 } 2214 }
2047 else 2215 else
2048 { 2216 {
2049 int i; 2217 int i;
2058 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2226 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2059 return 1; 2227 return 1;
2060 } 2228 }
2061} 2229}
2062 2230
2063static 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
2064slf_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)
2065{ 2245{
2066 AV *av = (AV *)SvRV (arg [0]); 2246 AV *av = (AV *)SvRV (arg [0]);
2067 2247
2068 if (SvIVX (AvARRAY (av)[0]) > 0) 2248 if (SvIVX (AvARRAY (av)[0]) > 0)
2069 { 2249 {
2070 frame->data = (void *)av; 2250 frame->data = (void *)av;
2071 frame->prepare = prepare_nop; 2251 frame->prepare = prepare_nop;
2072 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2073 } 2252 }
2074 else 2253 else
2075 { 2254 {
2076 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2255 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2077 2256
2078 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); 2257 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av));
2079 frame->prepare = prepare_schedule; 2258 frame->prepare = prepare_schedule;
2080 2259
2081 /* to avoid race conditions when a woken-up coro gets terminated */ 2260 /* to avoid race conditions when a woken-up coro gets terminated */
2082 /* we arrange for a temporary on_destroy that calls adjust (0) */ 2261 /* we arrange for a temporary on_destroy that calls adjust (0) */
2083 assert (!SvSTATE_current->on_destroy);//D
2084 SvSTATE_current->on_destroy = coro_semaphore_on_destroy; 2262 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2085 } 2263 }
2264}
2086 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);
2087 frame->check = slf_check_semaphore_down; 2270 frame->check = slf_check_semaphore_down;
2088
2089} 2271}
2090 2272
2091/*****************************************************************************/ 2273static void
2092/* gensub: simple closure generation utility */ 2274slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2093
2094#define GENSUB_ARG CvXSUBANY (cv).any_ptr
2095
2096/* create a closure from XS, returns a code reference */
2097/* the arg can be accessed via GENSUB_ARG from the callback */
2098/* the callback must use dXSARGS/XSRETURN */
2099static SV *
2100gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg)
2101{ 2275{
2102 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}
2103 2279
2104 sv_upgrade ((SV *)cv, SVt_PVCV); 2280/* signal */
2105 2281
2106 CvANON_on (cv); 2282static void
2107 CvISXSUB_on (cv); 2283coro_signal_wake (pTHX_ AV *av, int count)
2108 CvXSUB (cv) = xsub; 2284{
2109 GENSUB_ARG = arg; 2285 SvIVX (AvARRAY (av)[0]) = 0;
2110 2286
2111 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 }
2112} 2335}
2113 2336
2114/*****************************************************************************/ 2337/*****************************************************************************/
2115/* Coro::AIO */ 2338/* Coro::AIO */
2116 2339
2165 2388
2166static int 2389static int
2167slf_check_aio_req (pTHX_ struct CoroSLF *frame) 2390slf_check_aio_req (pTHX_ struct CoroSLF *frame)
2168{ 2391{
2169 AV *state = (AV *)frame->data; 2392 AV *state = (AV *)frame->data;
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;
2170 2399
2171 /* one element that is an RV? repeat! */ 2400 /* one element that is an RV? repeat! */
2172 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) 2401 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0]))
2173 return 1; 2402 return 1;
2174 2403
2506throw (Coro::State self, SV *throw = &PL_sv_undef) 2735throw (Coro::State self, SV *throw = &PL_sv_undef)
2507 PROTOTYPE: $;$ 2736 PROTOTYPE: $;$
2508 CODE: 2737 CODE:
2509{ 2738{
2510 struct coro *current = SvSTATE_current; 2739 struct coro *current = SvSTATE_current;
2511 SV **throwp = self == current ? &coro_throw : &self->throw; 2740 SV **throwp = self == current ? &CORO_THROW : &self->except;
2512 SvREFCNT_dec (*throwp); 2741 SvREFCNT_dec (*throwp);
2513 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 2742 *throwp = SvOK (throw) ? newSVsv (throw) : 0;
2514} 2743}
2515 2744
2516void 2745void
2605 coroapi.ready = api_ready; 2834 coroapi.ready = api_ready;
2606 coroapi.is_ready = api_is_ready; 2835 coroapi.is_ready = api_is_ready;
2607 coroapi.nready = coro_nready; 2836 coroapi.nready = coro_nready;
2608 coroapi.current = coro_current; 2837 coroapi.current = coro_current;
2609 2838
2610 GCoroAPI = &coroapi; 2839 /*GCoroAPI = &coroapi;*/
2611 sv_setiv (sv, (IV)&coroapi); 2840 sv_setiv (sv, (IV)&coroapi);
2612 SvREADONLY_on (sv); 2841 SvREADONLY_on (sv);
2613 } 2842 }
2614} 2843}
2615 2844
2751 api_trace (aTHX_ coro_current, 0); 2980 api_trace (aTHX_ coro_current, 0);
2752 2981
2753 av_push (av_async_pool, newSVsv (coro_current)); 2982 av_push (av_async_pool, newSVsv (coro_current));
2754} 2983}
2755 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
2756 2999
2757MODULE = Coro::State PACKAGE = PerlIO::cede 3000MODULE = Coro::State PACKAGE = PerlIO::cede
2758 3001
2759BOOT: 3002BOOT:
2760 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3003 PerlIO_define_layer (aTHX_ &PerlIO_cede);
2761 3004
2762 3005
2763MODULE = Coro::State PACKAGE = Coro::Semaphore 3006MODULE = Coro::State PACKAGE = Coro::Semaphore
2764 3007
2765SV * 3008SV *
2766new (SV *klass, SV *count_ = 0) 3009new (SV *klass, SV *count = 0)
2767 CODE: 3010 CODE:
2768{ 3011 RETVAL = sv_bless (
2769 /* 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),
2770 AV *av = newAV (); 3013 GvSTASH (CvGV (cv))
2771 SV **ary; 3014 );
3015 OUTPUT:
3016 RETVAL
2772 3017
2773 /* unfortunately, building manually saves memory */ 3018# helper for Coro::Channel
2774 Newx (ary, 2, SV *); 3019SV *
2775 AvALLOC (av) = ary; 3020_alloc (int count)
2776 AvARRAY (av) = ary; 3021 CODE:
2777 AvMAX (av) = 1; 3022 RETVAL = coro_waitarray_new (aTHX_ count);
2778 AvFILLp (av) = 0;
2779 ary [0] = newSViv (count_ && SvOK (count_) ? SvIV (count_) : 1);
2780
2781 RETVAL = sv_bless (newRV_noinc ((SV *)av), GvSTASH (CvGV (cv)));
2782}
2783 OUTPUT: 3023 OUTPUT:
2784 RETVAL 3024 RETVAL
2785 3025
2786SV * 3026SV *
2787count (SV *self) 3027count (SV *self)
2799 3039
2800void 3040void
2801down (SV *self) 3041down (SV *self)
2802 CODE: 3042 CODE:
2803 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);
2804 3049
2805void 3050void
2806try (SV *self) 3051try (SV *self)
2807 PPCODE: 3052 PPCODE:
2808{ 3053{
2820 XSRETURN_NO; 3065 XSRETURN_NO;
2821} 3066}
2822 3067
2823void 3068void
2824waiters (SV *self) 3069waiters (SV *self)
2825 CODE: 3070 PPCODE:
2826{ 3071{
2827 AV *av = (AV *)SvRV (self); 3072 AV *av = (AV *)SvRV (self);
3073 int wcount = AvFILLp (av) + 1 - 1;
2828 3074
2829 if (GIMME_V == G_SCALAR) 3075 if (GIMME_V == G_SCALAR)
2830 XPUSHs (sv_2mortal (newSVsv (AvARRAY (av)[0]))); 3076 XPUSHs (sv_2mortal (newSViv (wcount)));
2831 else 3077 else
2832 { 3078 {
2833 int i; 3079 int i;
2834 EXTEND (SP, AvFILLp (av) + 1 - 1); 3080 EXTEND (SP, wcount);
2835 for (i = 1; i <= AvFILLp (av); ++i) 3081 for (i = 1; i <= wcount; ++i)
2836 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3082 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
2837 } 3083 }
2838} 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
2839 3129
2840 3130
2841MODULE = Coro::State PACKAGE = Coro::AnyEvent 3131MODULE = Coro::State PACKAGE = Coro::AnyEvent
2842 3132
2843BOOT: 3133BOOT:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines