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.297 by root, Tue Nov 18 11:04:42 2008 UTC vs.
Revision 1.313 by root, Thu Nov 20 03:22:59 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
55 55
56#define PERL_VERSION_ATLEAST(a,b,c) \ 56#define PERL_VERSION_ATLEAST(a,b,c) \
57 (PERL_REVISION > (a) \ 57 (PERL_REVISION > (a) \
58 || (PERL_REVISION == (a) \ 58 || (PERL_REVISION == (a) \
59 && (PERL_VERSION > (b) \ 59 && (PERL_VERSION > (b) \
60 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) 60 || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c)))))
61 61
62#if !PERL_VERSION_ATLEAST (5,6,0) 62#if !PERL_VERSION_ATLEAST (5,6,0)
63# ifndef PL_ppaddr 63# ifndef PL_ppaddr
64# define PL_ppaddr ppaddr 64# define PL_ppaddr ppaddr
65# endif 65# endif
176static HV *hv_sig; /* %SIG */ 176static HV *hv_sig; /* %SIG */
177 177
178/* async_pool helper stuff */ 178/* async_pool helper stuff */
179static SV *sv_pool_rss; 179static SV *sv_pool_rss;
180static SV *sv_pool_size; 180static SV *sv_pool_size;
181static SV *sv_async_pool_idle;
181static AV *av_async_pool; 182static AV *av_async_pool;
183static SV *sv_Coro;
184static CV *cv_pool_handler;
185static CV *cv_coro_new;
182 186
183/* Coro::AnyEvent */ 187/* Coro::AnyEvent */
184static SV *sv_activity; 188static SV *sv_activity;
185 189
186static struct coro_cctx *cctx_first; 190static struct coro_cctx *cctx_first;
246 /* state data */ 250 /* state data */
247 struct CoroSLF slf_frame; /* saved slf frame */ 251 struct CoroSLF slf_frame; /* saved slf frame */
248 AV *mainstack; 252 AV *mainstack;
249 perl_slots *slot; /* basically the saved sp */ 253 perl_slots *slot; /* basically the saved sp */
250 254
255 CV *startcv; /* the CV to execute */
251 AV *args; /* data associated with this coroutine (initial args) */ 256 AV *args; /* data associated with this coroutine (initial args) */
252 int refcnt; /* coroutines are refcounted, yes */ 257 int refcnt; /* coroutines are refcounted, yes */
253 int flags; /* CF_ flags */ 258 int flags; /* CF_ flags */
254 HV *hv; /* the perl hash associated with this coro, if any */ 259 HV *hv; /* the perl hash associated with this coro, if any */
255 void (*on_destroy)(pTHX_ struct coro *coro); 260 void (*on_destroy)(pTHX_ struct coro *coro);
256 261
257 /* statistics */ 262 /* statistics */
258 int usecount; /* number of transfers to this coro */ 263 int usecount; /* number of transfers to this coro */
259 264
260 /* coro process data */ 265 /* coro process data */
261 int prio; 266 int prio;
262 SV *except; /* exception to be thrown */ 267 SV *except; /* exception to be thrown */
268 SV *rouse_cb;
263 269
264 /* async_pool */ 270 /* async_pool */
265 SV *saved_deffh; 271 SV *saved_deffh;
272 SV *invoke_cb;
273 AV *invoke_av;
266 274
267 /* linked list */ 275 /* linked list */
268 struct coro *next, *prev; 276 struct coro *next, *prev;
269}; 277};
270 278
287 295
288/* for Coro.pm */ 296/* for Coro.pm */
289static SV *coro_current; 297static SV *coro_current;
290static SV *coro_readyhook; 298static SV *coro_readyhook;
291static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 299static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
300static CV *cv_coro_run, *cv_coro_terminate;
292static struct coro *coro_first; 301static struct coro *coro_first;
293#define coro_nready coroapi.nready 302#define coro_nready coroapi.nready
294 303
295/** lowlevel stuff **********************************************************/ 304/** lowlevel stuff **********************************************************/
296 305
320#if PERL_VERSION_ATLEAST (5,10,0) 329#if PERL_VERSION_ATLEAST (5,10,0)
321 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 330 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
322 get_hv (name, create); 331 get_hv (name, create);
323#endif 332#endif
324 return get_hv (name, create); 333 return get_hv (name, create);
334}
335
336/* may croak */
337INLINE CV *
338coro_sv_2cv (pTHX_ SV *sv)
339{
340 HV *st;
341 GV *gvp;
342 return sv_2cv (sv, &st, &gvp, 0);
325} 343}
326 344
327static AV * 345static AV *
328coro_clone_padlist (pTHX_ CV *cv) 346coro_clone_padlist (pTHX_ CV *cv)
329{ 347{
448 else 466 else
449 { 467 {
450#if CORO_PREFER_PERL_FUNCTIONS 468#if CORO_PREFER_PERL_FUNCTIONS
451 /* this is probably cleaner? but also slower! */ 469 /* this is probably cleaner? but also slower! */
452 /* in practise, it seems to be less stable */ 470 /* in practise, it seems to be less stable */
453 CV *cp = Perl_cv_clone (cv); 471 CV *cp = Perl_cv_clone (aTHX_ cv);
454 CvPADLIST (cv) = CvPADLIST (cp); 472 CvPADLIST (cv) = CvPADLIST (cp);
455 CvPADLIST (cp) = 0; 473 CvPADLIST (cp) = 0;
456 SvREFCNT_dec (cp); 474 SvREFCNT_dec (cp);
457#else 475#else
458 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 476 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv);
602 * of perl.c:init_stacks, except that it uses less memory 620 * of perl.c:init_stacks, except that it uses less memory
603 * on the (sometimes correct) assumption that coroutines do 621 * on the (sometimes correct) assumption that coroutines do
604 * not usually need a lot of stackspace. 622 * not usually need a lot of stackspace.
605 */ 623 */
606#if CORO_PREFER_PERL_FUNCTIONS 624#if CORO_PREFER_PERL_FUNCTIONS
607# define coro_init_stacks init_stacks 625# define coro_init_stacks(thx) init_stacks ()
608#else 626#else
609static void 627static void
610coro_init_stacks (pTHX) 628coro_init_stacks (pTHX)
611{ 629{
612 PL_curstackinfo = new_stackinfo(32, 8); 630 PL_curstackinfo = new_stackinfo(32, 8);
675#if !PERL_VERSION_ATLEAST (5,10,0) 693#if !PERL_VERSION_ATLEAST (5,10,0)
676 Safefree (PL_retstack); 694 Safefree (PL_retstack);
677#endif 695#endif
678} 696}
679 697
698#define CORO_RSS \
699 rss += sizeof (SYM (curstackinfo)); \
700 rss += (SYM (curstackinfo->si_cxmax) + 1) * sizeof (PERL_CONTEXT); \
701 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (SYM (curstack))) * sizeof (SV *); \
702 rss += SYM (tmps_max) * sizeof (SV *); \
703 rss += (SYM (markstack_max) - SYM (markstack_ptr)) * sizeof (I32); \
704 rss += SYM (scopestack_max) * sizeof (I32); \
705 rss += SYM (savestack_max) * sizeof (ANY);
706
680static size_t 707static size_t
681coro_rss (pTHX_ struct coro *coro) 708coro_rss (pTHX_ struct coro *coro)
682{ 709{
683 size_t rss = sizeof (*coro); 710 size_t rss = sizeof (*coro);
684 711
685 if (coro->mainstack) 712 if (coro->mainstack)
686 { 713 {
687 perl_slots tmp_slot;
688 perl_slots *slot;
689
690 if (coro->flags & CF_RUNNING) 714 if (coro->flags & CF_RUNNING)
691 { 715 {
692 slot = &tmp_slot; 716 #define SYM(sym) PL_ ## sym
693 717 CORO_RSS;
694 #define VAR(name,type) slot->name = PL_ ## name;
695 # include "state.h"
696 #undef VAR 718 #undef SYM
697 } 719 }
698 else 720 else
699 slot = coro->slot;
700
701 if (slot)
702 { 721 {
703 rss += sizeof (slot->curstackinfo); 722 #define SYM(sym) coro->slot->sym
704 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 723 CORO_RSS;
705 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); 724 #undef SYM
706 rss += slot->tmps_max * sizeof (SV *);
707 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
708 rss += slot->scopestack_max * sizeof (I32);
709 rss += slot->savestack_max * sizeof (ANY);
710
711#if !PERL_VERSION_ATLEAST (5,10,0)
712 rss += slot->retstack_max * sizeof (OP *);
713#endif
714 } 725 }
715 } 726 }
716 727
717 return rss; 728 return rss;
718} 729}
860 dSP; 871 dSP;
861 UNOP myop; 872 UNOP myop;
862 873
863 Zero (&myop, 1, UNOP); 874 Zero (&myop, 1, UNOP);
864 myop.op_next = Nullop; 875 myop.op_next = Nullop;
876 myop.op_type = OP_ENTERSUB;
865 myop.op_flags = OPf_WANT_VOID; 877 myop.op_flags = OPf_WANT_VOID;
866 878
867 PUSHMARK (SP); 879 PUSHMARK (SP);
868 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 880 PUSHs ((SV *)coro->startcv);
869 PUTBACK; 881 PUTBACK;
870 PL_op = (OP *)&myop; 882 PL_op = (OP *)&myop;
871 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 883 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
872 SPAGAIN;
873 } 884 }
874 885
875 /* this newly created coroutine might be run on an existing cctx which most 886 /* this newly created coroutine might be run on an existing cctx which most
876 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. 887 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here.
877 */ 888 */
880 891
881 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ 892 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */
882 coro_setup_op.op_next = PL_op; 893 coro_setup_op.op_next = PL_op;
883 coro_setup_op.op_type = OP_CUSTOM; 894 coro_setup_op.op_type = OP_CUSTOM;
884 coro_setup_op.op_ppaddr = pp_slf; 895 coro_setup_op.op_ppaddr = pp_slf;
885 /* no flags required, as an init function won't be called */ 896 /* no flags etc. required, as an init function won't be called */
886 897
887 PL_op = (OP *)&coro_setup_op; 898 PL_op = (OP *)&coro_setup_op;
888 899
889 /* copy throw, in case it was set before coro_setup */ 900 /* copy throw, in case it was set before coro_setup */
890 CORO_THROW = coro->except; 901 CORO_THROW = coro->except;
919 930
920 SvREFCNT_dec (PL_diehook); 931 SvREFCNT_dec (PL_diehook);
921 SvREFCNT_dec (PL_warnhook); 932 SvREFCNT_dec (PL_warnhook);
922 933
923 SvREFCNT_dec (coro->saved_deffh); 934 SvREFCNT_dec (coro->saved_deffh);
924 SvREFCNT_dec (CORO_THROW); 935 SvREFCNT_dec (coro->rouse_cb);
936 SvREFCNT_dec (coro->invoke_cb);
937 SvREFCNT_dec (coro->invoke_av);
925 938
926 coro_destruct_stacks (aTHX); 939 coro_destruct_stacks (aTHX);
927} 940}
928 941
929INLINE void 942INLINE void
1016 SAVETMPS; 1029 SAVETMPS;
1017 EXTEND (SP, 3); 1030 EXTEND (SP, 3);
1018 PUSHMARK (SP); 1031 PUSHMARK (SP);
1019 PUSHs (&PL_sv_yes); 1032 PUSHs (&PL_sv_yes);
1020 PUSHs (fullname); 1033 PUSHs (fullname);
1021 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); 1034 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
1022 PUTBACK; 1035 PUTBACK;
1023 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); 1036 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
1024 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1037 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1025 SPAGAIN; 1038 SPAGAIN;
1026 FREETMPS; 1039 FREETMPS;
1132 transfer_tail (aTHX); 1145 transfer_tail (aTHX);
1133 1146
1134 /* somebody or something will hit me for both perl_run and PL_restartop */ 1147 /* somebody or something will hit me for both perl_run and PL_restartop */
1135 PL_restartop = PL_op; 1148 PL_restartop = PL_op;
1136 perl_run (PL_curinterp); 1149 perl_run (PL_curinterp);
1150 /*
1151 * Unfortunately, there is no way to get at the return values of the
1152 * coro body here, as perl_run destroys these
1153 */
1137 1154
1138 /* 1155 /*
1139 * If perl-run returns we assume exit() was being called or the coro 1156 * If perl-run returns we assume exit() was being called or the coro
1140 * fell off the end, which seems to be the only valid (non-bug) 1157 * fell off the end, which seems to be the only valid (non-bug)
1141 * reason for perl_run to return. We try to exit by jumping to the 1158 * reason for perl_run to return. We try to exit by jumping to the
1433 1450
1434 coro->slot = 0; 1451 coro->slot = 0;
1435 } 1452 }
1436 1453
1437 cctx_destroy (coro->cctx); 1454 cctx_destroy (coro->cctx);
1455 SvREFCNT_dec (coro->startcv);
1438 SvREFCNT_dec (coro->args); 1456 SvREFCNT_dec (coro->args);
1457 SvREFCNT_dec (CORO_THROW);
1439 1458
1440 if (coro->next) coro->next->prev = coro->prev; 1459 if (coro->next) coro->next->prev = coro->prev;
1441 if (coro->prev) coro->prev->next = coro->next; 1460 if (coro->prev) coro->prev->next = coro->next;
1442 if (coro == coro_first) coro_first = coro->next; 1461 if (coro == coro_first) coro_first = coro->next;
1443 1462
1497 1516
1498 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1517 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1499 TRANSFER (ta, 1); 1518 TRANSFER (ta, 1);
1500} 1519}
1501 1520
1521/*****************************************************************************/
1522/* gensub: simple closure generation utility */
1523
1524#define GENSUB_ARG CvXSUBANY (cv).any_ptr
1525
1526/* create a closure from XS, returns a code reference */
1527/* the arg can be accessed via GENSUB_ARG from the callback */
1528/* the callback must use dXSARGS/XSRETURN */
1529static SV *
1530gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg)
1531{
1532 CV *cv = (CV *)newSV (0);
1533
1534 sv_upgrade ((SV *)cv, SVt_PVCV);
1535
1536 CvANON_on (cv);
1537 CvISXSUB_on (cv);
1538 CvXSUB (cv) = xsub;
1539 GENSUB_ARG = arg;
1540
1541 return newRV_noinc ((SV *)cv);
1542}
1543
1502/** Coro ********************************************************************/ 1544/** Coro ********************************************************************/
1503 1545
1504INLINE void 1546INLINE void
1505coro_enq (pTHX_ struct coro *coro) 1547coro_enq (pTHX_ struct coro *coro)
1506{ 1548{
1703 if (coro->flags & CF_RUNNING) 1745 if (coro->flags & CF_RUNNING)
1704 PL_runops = RUNOPS_DEFAULT; 1746 PL_runops = RUNOPS_DEFAULT;
1705 else 1747 else
1706 coro->slot->runops = RUNOPS_DEFAULT; 1748 coro->slot->runops = RUNOPS_DEFAULT;
1707 } 1749 }
1750}
1751
1752/*****************************************************************************/
1753/* async pool handler */
1754
1755static int
1756slf_check_pool_handler (pTHX_ struct CoroSLF *frame)
1757{
1758 HV *hv = (HV *)SvRV (coro_current);
1759 struct coro *coro = (struct coro *)frame->data;
1760
1761 if (!coro->invoke_cb)
1762 return 1; /* loop till we have invoke */
1763 else
1764 {
1765 hv_store (hv, "desc", sizeof ("desc") - 1,
1766 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1767
1768 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1769
1770 {
1771 dSP;
1772 XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0;
1773 PUTBACK;
1774 }
1775
1776 SvREFCNT_dec (GvAV (PL_defgv));
1777 GvAV (PL_defgv) = coro->invoke_av;
1778 coro->invoke_av = 0;
1779
1780 return 0;
1781 }
1782}
1783
1784static void
1785slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1786{
1787 HV *hv = (HV *)SvRV (coro_current);
1788 struct coro *coro = SvSTATE_hv ((SV *)hv);
1789
1790 if (expect_true (coro->saved_deffh))
1791 {
1792 /* subsequent iteration */
1793 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1794 coro->saved_deffh = 0;
1795
1796 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
1797 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1798 {
1799 coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate);
1800 coro->invoke_av = newAV ();
1801
1802 frame->prepare = prepare_nop;
1803 }
1804 else
1805 {
1806 av_clear (GvAV (PL_defgv));
1807 hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0);
1808
1809 coro->prio = 0;
1810
1811 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1812 api_trace (aTHX_ coro_current, 0);
1813
1814 frame->prepare = prepare_schedule;
1815 av_push (av_async_pool, SvREFCNT_inc (hv));
1816 }
1817 }
1818 else
1819 {
1820 /* first iteration, simply fall through */
1821 frame->prepare = prepare_nop;
1822 }
1823
1824 frame->check = slf_check_pool_handler;
1825 frame->data = (void *)coro;
1826}
1827
1828/*****************************************************************************/
1829/* rouse callback */
1830
1831#define CORO_MAGIC_type_rouse PERL_MAGIC_ext
1832
1833static void
1834coro_rouse_callback (pTHX_ CV *cv)
1835{
1836 dXSARGS;
1837 SV *data = (SV *)GENSUB_ARG;
1838
1839 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1840 {
1841 /* first call, set args */
1842 AV *av = newAV ();
1843 SV *coro = SvRV (data);
1844
1845 SvRV_set (data, (SV *)av);
1846 api_ready (aTHX_ coro);
1847 SvREFCNT_dec (coro);
1848
1849 /* better take a full copy of the arguments */
1850 while (items--)
1851 av_store (av, items, newSVsv (ST (items)));
1852 }
1853
1854 XSRETURN_EMPTY;
1855}
1856
1857static int
1858slf_check_rouse_wait (pTHX_ struct CoroSLF *frame)
1859{
1860 SV *data = (SV *)frame->data;
1861
1862 if (CORO_THROW)
1863 return 0;
1864
1865 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1866 return 1;
1867
1868 /* now push all results on the stack */
1869 {
1870 dSP;
1871 AV *av = (AV *)SvRV (data);
1872 int i;
1873
1874 EXTEND (SP, AvFILLp (av) + 1);
1875 for (i = 0; i <= AvFILLp (av); ++i)
1876 PUSHs (sv_2mortal (AvARRAY (av)[i]));
1877
1878 /* we have stolen the elements, so ste length to zero and free */
1879 AvFILLp (av) = -1;
1880 av_undef (av);
1881
1882 PUTBACK;
1883 }
1884
1885 return 0;
1886}
1887
1888static void
1889slf_init_rouse_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1890{
1891 SV *cb;
1892
1893 if (items)
1894 cb = arg [0];
1895 else
1896 {
1897 struct coro *coro = SvSTATE_current;
1898
1899 if (!coro->rouse_cb)
1900 croak ("Coro::rouse_wait called without rouse callback, and no default rouse callback found either,");
1901
1902 cb = sv_2mortal (coro->rouse_cb);
1903 coro->rouse_cb = 0;
1904 }
1905
1906 if (!SvROK (cb)
1907 || SvTYPE (SvRV (cb)) != SVt_PVCV
1908 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback)
1909 croak ("Coro::rouse_wait called with illegal callback argument,");
1910
1911 {
1912 CV *cv = (CV *)SvRV (cb); /* for GENSUB_ARG */
1913 SV *data = (SV *)GENSUB_ARG;
1914
1915 frame->data = (void *)data;
1916 frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule;
1917 frame->check = slf_check_rouse_wait;
1918 }
1919}
1920
1921static SV *
1922coro_new_rouse_cb (pTHX)
1923{
1924 HV *hv = (HV *)SvRV (coro_current);
1925 struct coro *coro = SvSTATE_hv (hv);
1926 SV *data = newRV_inc ((SV *)hv);
1927 SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data);
1928
1929 sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0);
1930 SvREFCNT_dec (data); /* magicext increases the refcount */
1931
1932 SvREFCNT_dec (coro->rouse_cb);
1933 coro->rouse_cb = SvREFCNT_inc_NN (cb);
1934
1935 return cb;
1708} 1936}
1709 1937
1710/*****************************************************************************/ 1938/*****************************************************************************/
1711/* schedule-like-function opcode (SLF) */ 1939/* schedule-like-function opcode (SLF) */
1712 1940
1995 PerlIOBuf_get_cnt, 2223 PerlIOBuf_get_cnt,
1996 PerlIOBuf_set_ptrcnt, 2224 PerlIOBuf_set_ptrcnt,
1997}; 2225};
1998 2226
1999/*****************************************************************************/ 2227/*****************************************************************************/
2228/* Coro::Semaphore & Coro::Signal */
2229
2230static SV *
2231coro_waitarray_new (pTHX_ int count)
2232{
2233 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */
2234 AV *av = newAV ();
2235 SV **ary;
2236
2237 /* unfortunately, building manually saves memory */
2238 Newx (ary, 2, SV *);
2239 AvALLOC (av) = ary;
2240 /*AvARRAY (av) = ary;*/
2241 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */
2242 AvMAX (av) = 1;
2243 AvFILLp (av) = 0;
2244 ary [0] = newSViv (count);
2245
2246 return newRV_noinc ((SV *)av);
2247}
2248
2000/* Coro::Semaphore */ 2249/* semaphore */
2001 2250
2002static void 2251static void
2003coro_semaphore_adjust (pTHX_ AV *av, IV adjust) 2252coro_semaphore_adjust (pTHX_ AV *av, IV adjust)
2004{ 2253{
2005 SV *count_sv = AvARRAY (av)[0]; 2254 SV *count_sv = AvARRAY (av)[0];
2017 AvARRAY (av)[0] = AvARRAY (av)[1]; 2266 AvARRAY (av)[0] = AvARRAY (av)[1];
2018 AvARRAY (av)[1] = count_sv; 2267 AvARRAY (av)[1] = count_sv;
2019 cb = av_shift (av); 2268 cb = av_shift (av);
2020 2269
2021 if (SvOBJECT (cb)) 2270 if (SvOBJECT (cb))
2271 {
2022 api_ready (aTHX_ cb); 2272 api_ready (aTHX_ cb);
2023 else 2273 --count;
2024 croak ("callbacks not yet supported"); 2274 }
2275 else if (SvTYPE (cb) == SVt_PVCV)
2276 {
2277 dSP;
2278 PUSHMARK (SP);
2279 XPUSHs (sv_2mortal (newRV_inc ((SV *)av)));
2280 PUTBACK;
2281 call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR);
2282 }
2025 2283
2026 SvREFCNT_dec (cb); 2284 SvREFCNT_dec (cb);
2027
2028 --count;
2029 } 2285 }
2030} 2286}
2031 2287
2032static void 2288static void
2033coro_semaphore_on_destroy (pTHX_ struct coro *coro) 2289coro_semaphore_on_destroy (pTHX_ struct coro *coro)
2035 /* call $sem->adjust (0) to possibly wake up some other waiters */ 2291 /* call $sem->adjust (0) to possibly wake up some other waiters */
2036 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); 2292 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0);
2037} 2293}
2038 2294
2039static int 2295static int
2040slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) 2296slf_check_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, int acquire)
2041{ 2297{
2042 AV *av = (AV *)frame->data; 2298 AV *av = (AV *)frame->data;
2043 SV *count_sv = AvARRAY (av)[0]; 2299 SV *count_sv = AvARRAY (av)[0];
2044 2300
2045 /* if we are about to throw, don't actually acquire the lock, just throw */ 2301 /* if we are about to throw, don't actually acquire the lock, just throw */
2046 if (CORO_THROW) 2302 if (CORO_THROW)
2047 return 0; 2303 return 0;
2048 else if (SvIVX (count_sv) > 0) 2304 else if (SvIVX (count_sv) > 0)
2049 { 2305 {
2050 SvSTATE_current->on_destroy = 0; 2306 SvSTATE_current->on_destroy = 0;
2307
2308 if (acquire)
2051 SvIVX (count_sv) = SvIVX (count_sv) - 1; 2309 SvIVX (count_sv) = SvIVX (count_sv) - 1;
2310 else
2311 coro_semaphore_adjust (aTHX_ av, 0);
2312
2052 return 0; 2313 return 0;
2053 } 2314 }
2054 else 2315 else
2055 { 2316 {
2056 int i; 2317 int i;
2065 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2326 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2066 return 1; 2327 return 1;
2067 } 2328 }
2068} 2329}
2069 2330
2070static void 2331static int
2332slf_check_semaphore_down (pTHX_ struct CoroSLF *frame)
2333{
2334 return slf_check_semaphore_down_or_wait (aTHX_ frame, 1);
2335}
2336
2337static int
2338slf_check_semaphore_wait (pTHX_ struct CoroSLF *frame)
2339{
2340 return slf_check_semaphore_down_or_wait (aTHX_ frame, 0);
2341}
2342
2343static void
2071slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2344slf_init_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2072{ 2345{
2073 AV *av = (AV *)SvRV (arg [0]); 2346 AV *av = (AV *)SvRV (arg [0]);
2074 2347
2075 if (SvIVX (AvARRAY (av)[0]) > 0) 2348 if (SvIVX (AvARRAY (av)[0]) > 0)
2076 { 2349 {
2086 2359
2087 /* to avoid race conditions when a woken-up coro gets terminated */ 2360 /* to avoid race conditions when a woken-up coro gets terminated */
2088 /* we arrange for a temporary on_destroy that calls adjust (0) */ 2361 /* we arrange for a temporary on_destroy that calls adjust (0) */
2089 SvSTATE_current->on_destroy = coro_semaphore_on_destroy; 2362 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2090 } 2363 }
2364}
2091 2365
2366static void
2367slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2368{
2369 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items);
2092 frame->check = slf_check_semaphore_down; 2370 frame->check = slf_check_semaphore_down;
2093
2094} 2371}
2095 2372
2096/*****************************************************************************/ 2373static void
2097/* gensub: simple closure generation utility */ 2374slf_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{ 2375{
2107 CV *cv = (CV *)newSV (0); 2376 if (items >= 2)
2377 {
2378 /* callback form */
2379 AV *av = (AV *)SvRV (arg [0]);
2380 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2108 2381
2109 sv_upgrade ((SV *)cv, SVt_PVCV); 2382 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv));
2110 2383
2111 CvANON_on (cv); 2384 if (SvIVX (AvARRAY (av)[0]) > 0)
2112 CvISXSUB_on (cv); 2385 coro_semaphore_adjust (aTHX_ av, 0);
2113 CvXSUB (cv) = xsub;
2114 GENSUB_ARG = arg;
2115 2386
2116 return newRV_noinc ((SV *)cv); 2387 frame->prepare = prepare_nop;
2388 frame->check = slf_check_nop;
2389 }
2390 else
2391 {
2392 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items);
2393 frame->check = slf_check_semaphore_wait;
2394 }
2395}
2396
2397/* signal */
2398
2399static void
2400coro_signal_wake (pTHX_ AV *av, int count)
2401{
2402 SvIVX (AvARRAY (av)[0]) = 0;
2403
2404 /* now signal count waiters */
2405 while (count > 0 && AvFILLp (av) > 0)
2406 {
2407 SV *cb;
2408
2409 /* swap first two elements so we can shift a waiter */
2410 cb = AvARRAY (av)[0];
2411 AvARRAY (av)[0] = AvARRAY (av)[1];
2412 AvARRAY (av)[1] = cb;
2413
2414 cb = av_shift (av);
2415
2416 api_ready (aTHX_ cb);
2417 sv_setiv (cb, 0); /* signal waiter */
2418 SvREFCNT_dec (cb);
2419
2420 --count;
2421 }
2422}
2423
2424static int
2425slf_check_signal_wait (pTHX_ struct CoroSLF *frame)
2426{
2427 /* if we are about to throw, also stop waiting */
2428 return SvROK ((SV *)frame->data) && !CORO_THROW;
2429}
2430
2431static void
2432slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2433{
2434 AV *av = (AV *)SvRV (arg [0]);
2435
2436 if (SvIVX (AvARRAY (av)[0]))
2437 {
2438 SvIVX (AvARRAY (av)[0]) = 0;
2439 frame->prepare = prepare_nop;
2440 frame->check = slf_check_nop;
2441 }
2442 else
2443 {
2444 SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */
2445
2446 av_push (av, waiter);
2447
2448 frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */
2449 frame->prepare = prepare_schedule;
2450 frame->check = slf_check_signal_wait;
2451 }
2117} 2452}
2118 2453
2119/*****************************************************************************/ 2454/*****************************************************************************/
2120/* Coro::AIO */ 2455/* Coro::AIO */
2121 2456
2136 dXSARGS; 2471 dXSARGS;
2137 AV *state = (AV *)GENSUB_ARG; 2472 AV *state = (AV *)GENSUB_ARG;
2138 SV *coro = av_pop (state); 2473 SV *coro = av_pop (state);
2139 SV *data_sv = newSV (sizeof (struct io_state)); 2474 SV *data_sv = newSV (sizeof (struct io_state));
2140 2475
2141 av_extend (state, items); 2476 av_extend (state, items - 1);
2142 2477
2143 sv_upgrade (data_sv, SVt_PV); 2478 sv_upgrade (data_sv, SVt_PV);
2144 SvCUR_set (data_sv, sizeof (struct io_state)); 2479 SvCUR_set (data_sv, sizeof (struct io_state));
2145 SvPOK_only (data_sv); 2480 SvPOK_only (data_sv);
2146 2481
2356 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 2691 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
2357} 2692}
2358 2693
2359SV * 2694SV *
2360new (char *klass, ...) 2695new (char *klass, ...)
2696 ALIAS:
2697 Coro::new = 1
2361 CODE: 2698 CODE:
2362{ 2699{
2363 struct coro *coro; 2700 struct coro *coro;
2364 MAGIC *mg; 2701 MAGIC *mg;
2365 HV *hv; 2702 HV *hv;
2703 CV *cb;
2366 int i; 2704 int i;
2705
2706 if (items > 1)
2707 {
2708 cb = coro_sv_2cv (aTHX_ ST (1));
2709
2710 if (!ix)
2711 {
2712 if (CvISXSUB (cb))
2713 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
2714
2715 if (!CvROOT (cb))
2716 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
2717 }
2718 }
2367 2719
2368 Newz (0, coro, 1, struct coro); 2720 Newz (0, coro, 1, struct coro);
2369 coro->args = newAV (); 2721 coro->args = newAV ();
2370 coro->flags = CF_NEW; 2722 coro->flags = CF_NEW;
2371 2723
2376 coro->hv = hv = newHV (); 2728 coro->hv = hv = newHV ();
2377 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); 2729 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
2378 mg->mg_flags |= MGf_DUP; 2730 mg->mg_flags |= MGf_DUP;
2379 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 2731 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2380 2732
2733 if (items > 1)
2734 {
2381 av_extend (coro->args, items - 1); 2735 av_extend (coro->args, items - 1 + ix - 1);
2736
2737 if (ix)
2738 {
2739 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
2740 cb = cv_coro_run;
2741 }
2742
2743 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
2744
2382 for (i = 1; i < items; i++) 2745 for (i = 2; i < items; i++)
2383 av_push (coro->args, newSVsv (ST (i))); 2746 av_push (coro->args, newSVsv (ST (i)));
2747 }
2384} 2748}
2385 OUTPUT: 2749 OUTPUT:
2386 RETVAL 2750 RETVAL
2387 2751
2388void 2752void
2586 2950
2587BOOT: 2951BOOT:
2588{ 2952{
2589 int i; 2953 int i;
2590 2954
2591 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 2955 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2592 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 2956 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2593 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 2957 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2594 2958 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD);
2959 cv_coro_terminate = get_cv ( "Coro::terminate", GV_ADD);
2595 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); 2960 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE);
2596 SvREADONLY_on (coro_current); 2961 SvREADONLY_on (coro_current);
2962
2963 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
2964 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
2965 cv_pool_handler = get_cv ("Coro::_pool_handler", 0); SvREADONLY_on (cv_pool_handler);
2966 cv_coro_new = get_cv ("Coro::new", 0); SvREADONLY_on (cv_coro_new);
2597 2967
2598 coro_stash = gv_stashpv ("Coro", TRUE); 2968 coro_stash = gv_stashpv ("Coro", TRUE);
2599 2969
2600 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 2970 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2601 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 2971 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2690 CODE: 3060 CODE:
2691 RETVAL = coro_nready; 3061 RETVAL = coro_nready;
2692 OUTPUT: 3062 OUTPUT:
2693 RETVAL 3063 RETVAL
2694 3064
2695# for async_pool speedup
2696void 3065void
2697_pool_1 (SV *cb) 3066_pool_handler (...)
2698 CODE: 3067 CODE:
2699{ 3068 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
2700 HV *hv = (HV *)SvRV (coro_current);
2701 struct coro *coro = SvSTATE_hv ((SV *)hv);
2702 AV *defav = GvAV (PL_defgv);
2703 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
2704 AV *invoke_av;
2705 int i, len;
2706 3069
2707 if (!invoke) 3070void
3071async_pool (SV *cv, ...)
3072 PROTOTYPE: &@
3073 PPCODE:
3074{
3075 HV *hv = (HV *)av_pop (av_async_pool);
3076 AV *av = newAV ();
3077 SV *cb = ST (0);
3078 int i;
3079
3080 av_extend (av, items - 2);
3081 for (i = 1; i < items; ++i)
3082 av_push (av, SvREFCNT_inc_NN (ST (i)));
3083
3084 if ((SV *)hv == &PL_sv_undef)
2708 { 3085 {
2709 SV *old = PL_diehook; 3086 PUSHMARK (SP);
2710 PL_diehook = 0; 3087 EXTEND (SP, 2);
2711 SvREFCNT_dec (old); 3088 PUSHs (sv_Coro);
2712 croak ("\3async_pool terminate\2\n"); 3089 PUSHs ((SV *)cv_pool_handler);
3090 PUTBACK;
3091 call_sv ((SV *)cv_coro_new, G_SCALAR);
3092 SPAGAIN;
3093
3094 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs));
2713 } 3095 }
2714 3096
2715 SvREFCNT_dec (coro->saved_deffh);
2716 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2717
2718 hv_store (hv, "desc", sizeof ("desc") - 1,
2719 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2720
2721 invoke_av = (AV *)SvRV (invoke);
2722 len = av_len (invoke_av);
2723
2724 sv_setsv (cb, AvARRAY (invoke_av)[0]);
2725
2726 if (len > 0)
2727 { 3097 {
2728 av_fill (defav, len - 1); 3098 struct coro *coro = SvSTATE_hv (hv);
2729 for (i = 0; i < len; ++i) 3099
2730 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); 3100 assert (!coro->invoke_cb);
3101 assert (!coro->invoke_av);
3102 coro->invoke_cb = SvREFCNT_inc (cb);
3103 coro->invoke_av = av;
2731 } 3104 }
3105
3106 api_ready (aTHX_ (SV *)hv);
3107
3108 if (GIMME_V != G_VOID)
3109 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
3110 else
3111 SvREFCNT_dec (hv);
2732} 3112}
3113
3114SV *
3115rouse_cb ()
3116 PROTOTYPE:
3117 CODE:
3118 RETVAL = coro_new_rouse_cb (aTHX);
3119 OUTPUT:
3120 RETVAL
2733 3121
2734void 3122void
2735_pool_2 (SV *cb) 3123rouse_wait (...)
3124 PROTOTYPE: ;$
2736 CODE: 3125 PPCODE:
2737{ 3126 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
2738 HV *hv = (HV *)SvRV (coro_current);
2739 struct coro *coro = SvSTATE_hv ((SV *)hv);
2740
2741 sv_setsv (cb, &PL_sv_undef);
2742
2743 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2744 coro->saved_deffh = 0;
2745
2746 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2747 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2748 {
2749 SV *old = PL_diehook;
2750 PL_diehook = 0;
2751 SvREFCNT_dec (old);
2752 croak ("\3async_pool terminate\2\n");
2753 }
2754
2755 av_clear (GvAV (PL_defgv));
2756 hv_store (hv, "desc", sizeof ("desc") - 1,
2757 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
2758
2759 coro->prio = 0;
2760
2761 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
2762 api_trace (aTHX_ coro_current, 0);
2763
2764 av_push (av_async_pool, newSVsv (coro_current));
2765}
2766 3127
2767 3128
2768MODULE = Coro::State PACKAGE = PerlIO::cede 3129MODULE = Coro::State PACKAGE = PerlIO::cede
2769 3130
2770BOOT: 3131BOOT:
2772 3133
2773 3134
2774MODULE = Coro::State PACKAGE = Coro::Semaphore 3135MODULE = Coro::State PACKAGE = Coro::Semaphore
2775 3136
2776SV * 3137SV *
2777new (SV *klass, SV *count_ = 0) 3138new (SV *klass, SV *count = 0)
2778 CODE: 3139 CODE:
2779{ 3140 RETVAL = sv_bless (
2780 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ 3141 coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1),
2781 AV *av = newAV (); 3142 GvSTASH (CvGV (cv))
2782 SV **ary; 3143 );
3144 OUTPUT:
3145 RETVAL
2783 3146
2784 /* unfortunately, building manually saves memory */ 3147# helper for Coro::Channel
2785 Newx (ary, 2, SV *); 3148SV *
2786 AvALLOC (av) = ary; 3149_alloc (int count)
2787 AvARRAY (av) = ary; 3150 CODE:
2788 AvMAX (av) = 1; 3151 RETVAL = coro_waitarray_new (aTHX_ count);
2789 AvFILLp (av) = 0;
2790 ary [0] = newSViv (count_ && SvOK (count_) ? SvIV (count_) : 1);
2791
2792 RETVAL = sv_bless (newRV_noinc ((SV *)av), GvSTASH (CvGV (cv)));
2793}
2794 OUTPUT: 3152 OUTPUT:
2795 RETVAL 3153 RETVAL
2796 3154
2797SV * 3155SV *
2798count (SV *self) 3156count (SV *self)
2807 adjust = 1 3165 adjust = 1
2808 CODE: 3166 CODE:
2809 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); 3167 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1);
2810 3168
2811void 3169void
2812down (SV *self) 3170down (...)
2813 CODE: 3171 CODE:
2814 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); 3172 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down);
3173
3174void
3175wait (...)
3176 CODE:
3177 CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait);
2815 3178
2816void 3179void
2817try (SV *self) 3180try (SV *self)
2818 PPCODE: 3181 PPCODE:
2819{ 3182{
2831 XSRETURN_NO; 3194 XSRETURN_NO;
2832} 3195}
2833 3196
2834void 3197void
2835waiters (SV *self) 3198waiters (SV *self)
2836 CODE: 3199 PPCODE:
2837{ 3200{
2838 AV *av = (AV *)SvRV (self); 3201 AV *av = (AV *)SvRV (self);
3202 int wcount = AvFILLp (av) + 1 - 1;
2839 3203
2840 if (GIMME_V == G_SCALAR) 3204 if (GIMME_V == G_SCALAR)
2841 XPUSHs (sv_2mortal (newSVsv (AvARRAY (av)[0]))); 3205 XPUSHs (sv_2mortal (newSViv (wcount)));
2842 else 3206 else
2843 { 3207 {
2844 int i; 3208 int i;
2845 EXTEND (SP, AvFILLp (av) + 1 - 1); 3209 EXTEND (SP, wcount);
2846 for (i = 1; i <= AvFILLp (av); ++i) 3210 for (i = 1; i <= wcount; ++i)
2847 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3211 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
2848 } 3212 }
2849} 3213}
3214
3215MODULE = Coro::State PACKAGE = Coro::Signal
3216
3217SV *
3218new (SV *klass)
3219 CODE:
3220 RETVAL = sv_bless (
3221 coro_waitarray_new (aTHX_ 0),
3222 GvSTASH (CvGV (cv))
3223 );
3224 OUTPUT:
3225 RETVAL
3226
3227void
3228wait (...)
3229 CODE:
3230 CORO_EXECUTE_SLF_XS (slf_init_signal_wait);
3231
3232void
3233broadcast (SV *self)
3234 CODE:
3235{
3236 AV *av = (AV *)SvRV (self);
3237 coro_signal_wake (aTHX_ av, AvFILLp (av));
3238}
3239
3240void
3241send (SV *self)
3242 CODE:
3243{
3244 AV *av = (AV *)SvRV (self);
3245
3246 if (AvFILLp (av))
3247 coro_signal_wake (aTHX_ av, 1);
3248 else
3249 SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */
3250}
3251
3252IV
3253awaited (SV *self)
3254 CODE:
3255 RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1;
3256 OUTPUT:
3257 RETVAL
2850 3258
2851 3259
2852MODULE = Coro::State PACKAGE = Coro::AnyEvent 3260MODULE = Coro::State PACKAGE = Coro::AnyEvent
2853 3261
2854BOOT: 3262BOOT:
2882 3290
2883void 3291void
2884_register (char *target, char *proto, SV *req) 3292_register (char *target, char *proto, SV *req)
2885 CODE: 3293 CODE:
2886{ 3294{
2887 HV *st; 3295 CV *req_cv = coro_sv_2cv (aTHX_ req);
2888 GV *gvp;
2889 CV *req_cv = sv_2cv (req, &st, &gvp, 0);
2890 /* newXSproto doesn't return the CV on 5.8 */ 3296 /* newXSproto doesn't return the CV on 5.8 */
2891 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); 3297 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
2892 sv_setpv ((SV *)slf_cv, proto); 3298 sv_setpv ((SV *)slf_cv, proto);
2893 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 3299 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
2894} 3300}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines