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.306 by root, Wed Nov 19 11:11:10 2008 UTC vs.
Revision 1.316 by root, Thu Nov 20 06:01:40 2008 UTC

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
98# define newSV(l) NEWSV(0,l) 98# define newSV(l) NEWSV(0,l)
99#endif 99#endif
100#ifndef CvISXSUB_on 100#ifndef CvISXSUB_on
101# define CvISXSUB_on(cv) (void)cv 101# define CvISXSUB_on(cv) (void)cv
102#endif 102#endif
103#ifndef CvISXSUB
104# define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE)
105#endif
103 106
104/* 5.8.7 */ 107/* 5.8.7 */
105#ifndef SvRV_set 108#ifndef SvRV_set
106# define SvRV_set(s,v) SvRV(s) = (v) 109# define SvRV_set(s,v) SvRV(s) = (v)
107#endif 110#endif
176static HV *hv_sig; /* %SIG */ 179static HV *hv_sig; /* %SIG */
177 180
178/* async_pool helper stuff */ 181/* async_pool helper stuff */
179static SV *sv_pool_rss; 182static SV *sv_pool_rss;
180static SV *sv_pool_size; 183static SV *sv_pool_size;
184static SV *sv_async_pool_idle;
181static AV *av_async_pool; 185static AV *av_async_pool;
186static SV *sv_Coro;
187static CV *cv_pool_handler;
188static CV *cv_coro_state_new;
182 189
183/* Coro::AnyEvent */ 190/* Coro::AnyEvent */
184static SV *sv_activity; 191static SV *sv_activity;
185 192
186static struct coro_cctx *cctx_first; 193static struct coro_cctx *cctx_first;
246 /* state data */ 253 /* state data */
247 struct CoroSLF slf_frame; /* saved slf frame */ 254 struct CoroSLF slf_frame; /* saved slf frame */
248 AV *mainstack; 255 AV *mainstack;
249 perl_slots *slot; /* basically the saved sp */ 256 perl_slots *slot; /* basically the saved sp */
250 257
258 CV *startcv; /* the CV to execute */
251 AV *args; /* data associated with this coroutine (initial args) */ 259 AV *args; /* data associated with this coroutine (initial args) */
252 int refcnt; /* coroutines are refcounted, yes */ 260 int refcnt; /* coroutines are refcounted, yes */
253 int flags; /* CF_ flags */ 261 int flags; /* CF_ flags */
254 HV *hv; /* the perl hash associated with this coro, if any */ 262 HV *hv; /* the perl hash associated with this coro, if any */
255 void (*on_destroy)(pTHX_ struct coro *coro); 263 void (*on_destroy)(pTHX_ struct coro *coro);
256 264
257 /* statistics */ 265 /* statistics */
258 int usecount; /* number of transfers to this coro */ 266 int usecount; /* number of transfers to this coro */
259 267
262 SV *except; /* exception to be thrown */ 270 SV *except; /* exception to be thrown */
263 SV *rouse_cb; 271 SV *rouse_cb;
264 272
265 /* async_pool */ 273 /* async_pool */
266 SV *saved_deffh; 274 SV *saved_deffh;
275 SV *invoke_cb;
276 AV *invoke_av;
267 277
268 /* linked list */ 278 /* linked list */
269 struct coro *next, *prev; 279 struct coro *next, *prev;
270}; 280};
271 281
288 298
289/* for Coro.pm */ 299/* for Coro.pm */
290static SV *coro_current; 300static SV *coro_current;
291static SV *coro_readyhook; 301static SV *coro_readyhook;
292static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 302static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
303static CV *cv_coro_run, *cv_coro_terminate;
293static struct coro *coro_first; 304static struct coro *coro_first;
294#define coro_nready coroapi.nready 305#define coro_nready coroapi.nready
295 306
296/** lowlevel stuff **********************************************************/ 307/** lowlevel stuff **********************************************************/
297 308
321#if PERL_VERSION_ATLEAST (5,10,0) 332#if PERL_VERSION_ATLEAST (5,10,0)
322 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 333 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
323 get_hv (name, create); 334 get_hv (name, create);
324#endif 335#endif
325 return get_hv (name, create); 336 return get_hv (name, create);
337}
338
339/* may croak */
340INLINE CV *
341coro_sv_2cv (pTHX_ SV *sv)
342{
343 HV *st;
344 GV *gvp;
345 return sv_2cv (sv, &st, &gvp, 0);
326} 346}
327 347
328static AV * 348static AV *
329coro_clone_padlist (pTHX_ CV *cv) 349coro_clone_padlist (pTHX_ CV *cv)
330{ 350{
676#if !PERL_VERSION_ATLEAST (5,10,0) 696#if !PERL_VERSION_ATLEAST (5,10,0)
677 Safefree (PL_retstack); 697 Safefree (PL_retstack);
678#endif 698#endif
679} 699}
680 700
701#define CORO_RSS \
702 rss += sizeof (SYM (curstackinfo)); \
703 rss += (SYM (curstackinfo->si_cxmax) + 1) * sizeof (PERL_CONTEXT); \
704 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (SYM (curstack))) * sizeof (SV *); \
705 rss += SYM (tmps_max) * sizeof (SV *); \
706 rss += (SYM (markstack_max) - SYM (markstack_ptr)) * sizeof (I32); \
707 rss += SYM (scopestack_max) * sizeof (I32); \
708 rss += SYM (savestack_max) * sizeof (ANY);
709
681static size_t 710static size_t
682coro_rss (pTHX_ struct coro *coro) 711coro_rss (pTHX_ struct coro *coro)
683{ 712{
684 size_t rss = sizeof (*coro); 713 size_t rss = sizeof (*coro);
685 714
686 if (coro->mainstack) 715 if (coro->mainstack)
687 { 716 {
688 perl_slots tmp_slot;
689 perl_slots *slot;
690
691 if (coro->flags & CF_RUNNING) 717 if (coro->flags & CF_RUNNING)
692 { 718 {
693 slot = &tmp_slot; 719 #define SYM(sym) PL_ ## sym
694 720 CORO_RSS;
695 #define VAR(name,type) slot->name = PL_ ## name;
696 # include "state.h"
697 #undef VAR 721 #undef SYM
698 } 722 }
699 else 723 else
700 slot = coro->slot;
701
702 if (slot)
703 { 724 {
704 rss += sizeof (slot->curstackinfo); 725 #define SYM(sym) coro->slot->sym
705 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 726 CORO_RSS;
706 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); 727 #undef SYM
707 rss += slot->tmps_max * sizeof (SV *);
708 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
709 rss += slot->scopestack_max * sizeof (I32);
710 rss += slot->savestack_max * sizeof (ANY);
711
712#if !PERL_VERSION_ATLEAST (5,10,0)
713 rss += slot->retstack_max * sizeof (OP *);
714#endif
715 } 728 }
716 } 729 }
717 730
718 return rss; 731 return rss;
719} 732}
861 dSP; 874 dSP;
862 UNOP myop; 875 UNOP myop;
863 876
864 Zero (&myop, 1, UNOP); 877 Zero (&myop, 1, UNOP);
865 myop.op_next = Nullop; 878 myop.op_next = Nullop;
879 myop.op_type = OP_ENTERSUB;
866 myop.op_flags = OPf_WANT_VOID; 880 myop.op_flags = OPf_WANT_VOID;
867 881
868 PUSHMARK (SP); 882 PUSHMARK (SP);
869 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 883 PUSHs ((SV *)coro->startcv);
870 PUTBACK; 884 PUTBACK;
871 PL_op = (OP *)&myop; 885 PL_op = (OP *)&myop;
872 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 886 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
873 SPAGAIN;
874 } 887 }
875 888
876 /* this newly created coroutine might be run on an existing cctx which most 889 /* this newly created coroutine might be run on an existing cctx which most
877 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. 890 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here.
878 */ 891 */
881 894
882 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ 895 /* 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; 896 coro_setup_op.op_next = PL_op;
884 coro_setup_op.op_type = OP_CUSTOM; 897 coro_setup_op.op_type = OP_CUSTOM;
885 coro_setup_op.op_ppaddr = pp_slf; 898 coro_setup_op.op_ppaddr = pp_slf;
886 /* no flags required, as an init function won't be called */ 899 /* no flags etc. required, as an init function won't be called */
887 900
888 PL_op = (OP *)&coro_setup_op; 901 PL_op = (OP *)&coro_setup_op;
889 902
890 /* copy throw, in case it was set before coro_setup */ 903 /* copy throw, in case it was set before coro_setup */
891 CORO_THROW = coro->except; 904 CORO_THROW = coro->except;
919 SvREFCNT_dec (GvSV (irsgv)); 932 SvREFCNT_dec (GvSV (irsgv));
920 933
921 SvREFCNT_dec (PL_diehook); 934 SvREFCNT_dec (PL_diehook);
922 SvREFCNT_dec (PL_warnhook); 935 SvREFCNT_dec (PL_warnhook);
923 936
924 SvREFCNT_dec (CORO_THROW);
925 SvREFCNT_dec (coro->saved_deffh); 937 SvREFCNT_dec (coro->saved_deffh);
926 SvREFCNT_dec (coro->rouse_cb); 938 SvREFCNT_dec (coro->rouse_cb);
939 SvREFCNT_dec (coro->invoke_cb);
940 SvREFCNT_dec (coro->invoke_av);
927 941
928 coro_destruct_stacks (aTHX); 942 coro_destruct_stacks (aTHX);
929} 943}
930 944
931INLINE void 945INLINE void
1018 SAVETMPS; 1032 SAVETMPS;
1019 EXTEND (SP, 3); 1033 EXTEND (SP, 3);
1020 PUSHMARK (SP); 1034 PUSHMARK (SP);
1021 PUSHs (&PL_sv_yes); 1035 PUSHs (&PL_sv_yes);
1022 PUSHs (fullname); 1036 PUSHs (fullname);
1023 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); 1037 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
1024 PUTBACK; 1038 PUTBACK;
1025 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); 1039 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
1026 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1040 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1027 SPAGAIN; 1041 SPAGAIN;
1028 FREETMPS; 1042 FREETMPS;
1134 transfer_tail (aTHX); 1148 transfer_tail (aTHX);
1135 1149
1136 /* somebody or something will hit me for both perl_run and PL_restartop */ 1150 /* somebody or something will hit me for both perl_run and PL_restartop */
1137 PL_restartop = PL_op; 1151 PL_restartop = PL_op;
1138 perl_run (PL_curinterp); 1152 perl_run (PL_curinterp);
1153 /*
1154 * Unfortunately, there is no way to get at the return values of the
1155 * coro body here, as perl_run destroys these
1156 */
1139 1157
1140 /* 1158 /*
1141 * If perl-run returns we assume exit() was being called or the coro 1159 * If perl-run returns we assume exit() was being called or the coro
1142 * fell off the end, which seems to be the only valid (non-bug) 1160 * fell off the end, which seems to be the only valid (non-bug)
1143 * reason for perl_run to return. We try to exit by jumping to the 1161 * reason for perl_run to return. We try to exit by jumping to the
1435 1453
1436 coro->slot = 0; 1454 coro->slot = 0;
1437 } 1455 }
1438 1456
1439 cctx_destroy (coro->cctx); 1457 cctx_destroy (coro->cctx);
1458 SvREFCNT_dec (coro->startcv);
1440 SvREFCNT_dec (coro->args); 1459 SvREFCNT_dec (coro->args);
1460 SvREFCNT_dec (CORO_THROW);
1441 1461
1442 if (coro->next) coro->next->prev = coro->prev; 1462 if (coro->next) coro->next->prev = coro->prev;
1443 if (coro->prev) coro->prev->next = coro->next; 1463 if (coro->prev) coro->prev->next = coro->next;
1444 if (coro == coro_first) coro_first = coro->next; 1464 if (coro == coro_first) coro_first = coro->next;
1445 1465
1592api_is_ready (pTHX_ SV *coro_sv) 1612api_is_ready (pTHX_ SV *coro_sv)
1593{ 1613{
1594 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1614 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1595} 1615}
1596 1616
1617/* expects to own a reference to next->hv */
1597INLINE void 1618INLINE void
1619prepare_cede_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
1620{
1621 SV *prev_sv = SvRV (coro_current);
1622
1623 ta->prev = SvSTATE_hv (prev_sv);
1624 ta->next = next;
1625
1626 TRANSFER_CHECK (*ta);
1627
1628 SvRV_set (coro_current, next->hv);
1629
1630 free_coro_mortal (aTHX);
1631 coro_mortal = prev_sv;
1632}
1633
1634static void
1598prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1635prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1599{ 1636{
1600 SV *prev_sv, *next_sv;
1601
1602 for (;;) 1637 for (;;)
1603 { 1638 {
1604 next_sv = coro_deq (aTHX); 1639 SV *next_sv = coro_deq (aTHX);
1605 1640
1606 /* nothing to schedule: call the idle handler */
1607 if (expect_false (!next_sv)) 1641 if (expect_false (!next_sv))
1608 { 1642 {
1643 /* nothing to schedule: call the idle handler */
1609 dSP; 1644 dSP;
1610 1645
1611 ENTER; 1646 ENTER;
1612 SAVETMPS; 1647 SAVETMPS;
1613 1648
1615 PUTBACK; 1650 PUTBACK;
1616 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1651 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD);
1617 1652
1618 FREETMPS; 1653 FREETMPS;
1619 LEAVE; 1654 LEAVE;
1620 continue;
1621 } 1655 }
1622 1656 else
1623 ta->next = SvSTATE_hv (next_sv);
1624
1625 /* cannot transfer to destroyed coros, skip and look for next */
1626 if (expect_false (ta->next->flags & CF_DESTROYED))
1627 { 1657 {
1628 SvREFCNT_dec (next_sv); 1658 struct coro *next = SvSTATE_hv (next_sv);
1659
1660 /* cannot transfer to destroyed coros, skip and look for next */
1661 if (expect_false (next->flags & CF_DESTROYED))
1629 /* coro_nready has already been taken care of by destroy */ 1662 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */
1630 continue; 1663 else
1664 {
1665 next->flags &= ~CF_READY;
1666 --coro_nready;
1667
1668 return prepare_cede_to (aTHX_ ta, next);
1669 }
1631 } 1670 }
1632
1633 --coro_nready;
1634 break;
1635 } 1671 }
1636
1637 /* free this only after the transfer */
1638 prev_sv = SvRV (coro_current);
1639 ta->prev = SvSTATE_hv (prev_sv);
1640 TRANSFER_CHECK (*ta);
1641 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY));
1642 ta->next->flags &= ~CF_READY;
1643 SvRV_set (coro_current, next_sv);
1644
1645 free_coro_mortal (aTHX);
1646 coro_mortal = prev_sv;
1647} 1672}
1648 1673
1649INLINE void 1674INLINE void
1650prepare_cede (pTHX_ struct coro_transfer_args *ta) 1675prepare_cede (pTHX_ struct coro_transfer_args *ta)
1651{ 1676{
1731 coro->slot->runops = RUNOPS_DEFAULT; 1756 coro->slot->runops = RUNOPS_DEFAULT;
1732 } 1757 }
1733} 1758}
1734 1759
1735/*****************************************************************************/ 1760/*****************************************************************************/
1761/* async pool handler */
1762
1763static int
1764slf_check_pool_handler (pTHX_ struct CoroSLF *frame)
1765{
1766 HV *hv = (HV *)SvRV (coro_current);
1767 struct coro *coro = (struct coro *)frame->data;
1768
1769 if (!coro->invoke_cb)
1770 return 1; /* loop till we have invoke */
1771 else
1772 {
1773 hv_store (hv, "desc", sizeof ("desc") - 1,
1774 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1775
1776 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1777
1778 {
1779 dSP;
1780 XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0;
1781 PUTBACK;
1782 }
1783
1784 SvREFCNT_dec (GvAV (PL_defgv));
1785 GvAV (PL_defgv) = coro->invoke_av;
1786 coro->invoke_av = 0;
1787
1788 return 0;
1789 }
1790}
1791
1792static void
1793slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1794{
1795 HV *hv = (HV *)SvRV (coro_current);
1796 struct coro *coro = SvSTATE_hv ((SV *)hv);
1797
1798 if (expect_true (coro->saved_deffh))
1799 {
1800 /* subsequent iteration */
1801 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1802 coro->saved_deffh = 0;
1803
1804 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
1805 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1806 {
1807 coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate);
1808 coro->invoke_av = newAV ();
1809
1810 frame->prepare = prepare_nop;
1811 }
1812 else
1813 {
1814 av_clear (GvAV (PL_defgv));
1815 hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0);
1816
1817 coro->prio = 0;
1818
1819 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1820 api_trace (aTHX_ coro_current, 0);
1821
1822 frame->prepare = prepare_schedule;
1823 av_push (av_async_pool, SvREFCNT_inc (hv));
1824 }
1825 }
1826 else
1827 {
1828 /* first iteration, simply fall through */
1829 frame->prepare = prepare_nop;
1830 }
1831
1832 frame->check = slf_check_pool_handler;
1833 frame->data = (void *)coro;
1834}
1835
1836/*****************************************************************************/
1736/* rouse callback */ 1837/* rouse callback */
1737 1838
1738#define CORO_MAGIC_type_rouse PERL_MAGIC_ext 1839#define CORO_MAGIC_type_rouse PERL_MAGIC_ext
1739 1840
1740static void 1841static void
1744 SV *data = (SV *)GENSUB_ARG; 1845 SV *data = (SV *)GENSUB_ARG;
1745 1846
1746 if (SvTYPE (SvRV (data)) != SVt_PVAV) 1847 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1747 { 1848 {
1748 /* first call, set args */ 1849 /* first call, set args */
1749 int i;
1750 AV *av = newAV (); 1850 AV *av = newAV ();
1751 SV *coro = SvRV (data); 1851 SV *coro = SvRV (data);
1752 1852
1753 SvRV_set (data, (SV *)av); 1853 SvRV_set (data, (SV *)av);
1754 api_ready (aTHX_ coro); 1854 api_ready (aTHX_ coro);
2283{ 2383{
2284 if (items >= 2) 2384 if (items >= 2)
2285 { 2385 {
2286 /* callback form */ 2386 /* callback form */
2287 AV *av = (AV *)SvRV (arg [0]); 2387 AV *av = (AV *)SvRV (arg [0]);
2288 HV *st; 2388 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2289 GV *gvp;
2290 CV *cb_cv = sv_2cv (arg [1], &st, &gvp, 0);
2291 2389
2292 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); 2390 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv));
2293 2391
2294 if (SvIVX (AvARRAY (av)[0]) > 0) 2392 if (SvIVX (AvARRAY (av)[0]) > 0)
2295 coro_semaphore_adjust (aTHX_ av, 0); 2393 coro_semaphore_adjust (aTHX_ av, 0);
2381 dXSARGS; 2479 dXSARGS;
2382 AV *state = (AV *)GENSUB_ARG; 2480 AV *state = (AV *)GENSUB_ARG;
2383 SV *coro = av_pop (state); 2481 SV *coro = av_pop (state);
2384 SV *data_sv = newSV (sizeof (struct io_state)); 2482 SV *data_sv = newSV (sizeof (struct io_state));
2385 2483
2386 av_extend (state, items); 2484 av_extend (state, items - 1);
2387 2485
2388 sv_upgrade (data_sv, SVt_PV); 2486 sv_upgrade (data_sv, SVt_PV);
2389 SvCUR_set (data_sv, sizeof (struct io_state)); 2487 SvCUR_set (data_sv, sizeof (struct io_state));
2390 SvPOK_only (data_sv); 2488 SvPOK_only (data_sv);
2391 2489
2601 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 2699 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
2602} 2700}
2603 2701
2604SV * 2702SV *
2605new (char *klass, ...) 2703new (char *klass, ...)
2704 ALIAS:
2705 Coro::new = 1
2606 CODE: 2706 CODE:
2607{ 2707{
2608 struct coro *coro; 2708 struct coro *coro;
2609 MAGIC *mg; 2709 MAGIC *mg;
2610 HV *hv; 2710 HV *hv;
2711 CV *cb;
2611 int i; 2712 int i;
2713
2714 if (items > 1)
2715 {
2716 cb = coro_sv_2cv (aTHX_ ST (1));
2717
2718 if (!ix)
2719 {
2720 if (CvISXSUB (cb))
2721 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
2722
2723 if (!CvROOT (cb))
2724 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
2725 }
2726 }
2612 2727
2613 Newz (0, coro, 1, struct coro); 2728 Newz (0, coro, 1, struct coro);
2614 coro->args = newAV (); 2729 coro->args = newAV ();
2615 coro->flags = CF_NEW; 2730 coro->flags = CF_NEW;
2616 2731
2621 coro->hv = hv = newHV (); 2736 coro->hv = hv = newHV ();
2622 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); 2737 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
2623 mg->mg_flags |= MGf_DUP; 2738 mg->mg_flags |= MGf_DUP;
2624 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 2739 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2625 2740
2741 if (items > 1)
2742 {
2626 av_extend (coro->args, items - 1); 2743 av_extend (coro->args, items - 1 + ix - 1);
2744
2745 if (ix)
2746 {
2747 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
2748 cb = cv_coro_run;
2749 }
2750
2751 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
2752
2627 for (i = 1; i < items; i++) 2753 for (i = 2; i < items; i++)
2628 av_push (coro->args, newSVsv (ST (i))); 2754 av_push (coro->args, newSVsv (ST (i)));
2755 }
2629} 2756}
2630 OUTPUT: 2757 OUTPUT:
2631 RETVAL 2758 RETVAL
2632 2759
2633void 2760void
2831 2958
2832BOOT: 2959BOOT:
2833{ 2960{
2834 int i; 2961 int i;
2835 2962
2836 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 2963 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2837 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 2964 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2838 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 2965 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2839 2966 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD);
2967 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
2840 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); 2968 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
2841 SvREADONLY_on (coro_current); 2969
2970 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
2971 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
2972 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
2973 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
2842 2974
2843 coro_stash = gv_stashpv ("Coro", TRUE); 2975 coro_stash = gv_stashpv ("Coro", TRUE);
2844 2976
2845 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 2977 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2846 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 2978 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2935 CODE: 3067 CODE:
2936 RETVAL = coro_nready; 3068 RETVAL = coro_nready;
2937 OUTPUT: 3069 OUTPUT:
2938 RETVAL 3070 RETVAL
2939 3071
2940# for async_pool speedup
2941void 3072void
2942_pool_1 (SV *cb) 3073_pool_handler (...)
2943 CODE: 3074 CODE:
2944{ 3075 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
2945 HV *hv = (HV *)SvRV (coro_current);
2946 struct coro *coro = SvSTATE_hv ((SV *)hv);
2947 AV *defav = GvAV (PL_defgv);
2948 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
2949 AV *invoke_av;
2950 int i, len;
2951 3076
2952 if (!invoke) 3077void
3078async_pool (SV *cv, ...)
3079 PROTOTYPE: &@
3080 PPCODE:
3081{
3082 HV *hv = (HV *)av_pop (av_async_pool);
3083 AV *av = newAV ();
3084 SV *cb = ST (0);
3085 int i;
3086
3087 av_extend (av, items - 2);
3088 for (i = 1; i < items; ++i)
3089 av_push (av, SvREFCNT_inc_NN (ST (i)));
3090
3091 if ((SV *)hv == &PL_sv_undef)
2953 { 3092 {
2954 SV *old = PL_diehook; 3093 PUSHMARK (SP);
2955 PL_diehook = 0; 3094 EXTEND (SP, 2);
2956 SvREFCNT_dec (old); 3095 PUSHs (sv_Coro);
2957 croak ("\3async_pool terminate\2\n"); 3096 PUSHs ((SV *)cv_pool_handler);
3097 PUTBACK;
3098 call_sv ((SV *)cv_coro_state_new, G_SCALAR);
3099 SPAGAIN;
3100
3101 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs));
2958 } 3102 }
2959 3103
2960 SvREFCNT_dec (coro->saved_deffh);
2961 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2962
2963 hv_store (hv, "desc", sizeof ("desc") - 1,
2964 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2965
2966 invoke_av = (AV *)SvRV (invoke);
2967 len = av_len (invoke_av);
2968
2969 sv_setsv (cb, AvARRAY (invoke_av)[0]);
2970
2971 if (len > 0)
2972 { 3104 {
2973 av_fill (defav, len - 1); 3105 struct coro *coro = SvSTATE_hv (hv);
2974 for (i = 0; i < len; ++i) 3106
2975 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); 3107 assert (!coro->invoke_cb);
3108 assert (!coro->invoke_av);
3109 coro->invoke_cb = SvREFCNT_inc (cb);
3110 coro->invoke_av = av;
2976 } 3111 }
2977}
2978 3112
2979void 3113 api_ready (aTHX_ (SV *)hv);
2980_pool_2 (SV *cb)
2981 CODE:
2982{
2983 HV *hv = (HV *)SvRV (coro_current);
2984 struct coro *coro = SvSTATE_hv ((SV *)hv);
2985 3114
2986 sv_setsv (cb, &PL_sv_undef); 3115 if (GIMME_V != G_VOID)
2987 3116 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
2988 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 3117 else
2989 coro->saved_deffh = 0;
2990
2991 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2992 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2993 {
2994 SV *old = PL_diehook;
2995 PL_diehook = 0;
2996 SvREFCNT_dec (old); 3118 SvREFCNT_dec (hv);
2997 croak ("\3async_pool terminate\2\n");
2998 }
2999
3000 av_clear (GvAV (PL_defgv));
3001 hv_store (hv, "desc", sizeof ("desc") - 1,
3002 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
3003
3004 coro->prio = 0;
3005
3006 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
3007 api_trace (aTHX_ coro_current, 0);
3008
3009 av_push (av_async_pool, newSVsv (coro_current));
3010} 3119}
3011 3120
3012SV * 3121SV *
3013rouse_cb () 3122rouse_cb ()
3014 PROTOTYPE: 3123 PROTOTYPE:
3188 3297
3189void 3298void
3190_register (char *target, char *proto, SV *req) 3299_register (char *target, char *proto, SV *req)
3191 CODE: 3300 CODE:
3192{ 3301{
3193 HV *st; 3302 CV *req_cv = coro_sv_2cv (aTHX_ req);
3194 GV *gvp;
3195 CV *req_cv = sv_2cv (req, &st, &gvp, 0);
3196 /* newXSproto doesn't return the CV on 5.8 */ 3303 /* newXSproto doesn't return the CV on 5.8 */
3197 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); 3304 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
3198 sv_setpv ((SV *)slf_cv, proto); 3305 sv_setpv ((SV *)slf_cv, proto);
3199 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 3306 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
3200} 3307}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines