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.307 by root, Wed Nov 19 14:34:46 2008 UTC vs.
Revision 1.315 by root, Thu Nov 20 05:25:17 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;
263 SV *except; /* exception to be thrown */ 270 SV *except; /* exception to be thrown */
264 SV *rouse_cb; 271 SV *rouse_cb;
265 272
266 /* async_pool */ 273 /* async_pool */
267 SV *saved_deffh; 274 SV *saved_deffh;
275 SV *invoke_cb;
276 AV *invoke_av;
268 277
269 /* linked list */ 278 /* linked list */
270 struct coro *next, *prev; 279 struct coro *next, *prev;
271}; 280};
272 281
289 298
290/* for Coro.pm */ 299/* for Coro.pm */
291static SV *coro_current; 300static SV *coro_current;
292static SV *coro_readyhook; 301static SV *coro_readyhook;
293static 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;
294static struct coro *coro_first; 304static struct coro *coro_first;
295#define coro_nready coroapi.nready 305#define coro_nready coroapi.nready
296 306
297/** lowlevel stuff **********************************************************/ 307/** lowlevel stuff **********************************************************/
298 308
326 return get_hv (name, create); 336 return get_hv (name, create);
327} 337}
328 338
329/* may croak */ 339/* may croak */
330INLINE CV * 340INLINE CV *
331coro_sv_2cv (SV *sv) 341coro_sv_2cv (pTHX_ SV *sv)
332{ 342{
333 HV *st; 343 HV *st;
334 GV *gvp; 344 GV *gvp;
335 return sv_2cv (sv, &st, &gvp, 0); 345 return sv_2cv (sv, &st, &gvp, 0);
336} 346}
686#if !PERL_VERSION_ATLEAST (5,10,0) 696#if !PERL_VERSION_ATLEAST (5,10,0)
687 Safefree (PL_retstack); 697 Safefree (PL_retstack);
688#endif 698#endif
689} 699}
690 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
691static size_t 710static size_t
692coro_rss (pTHX_ struct coro *coro) 711coro_rss (pTHX_ struct coro *coro)
693{ 712{
694 size_t rss = sizeof (*coro); 713 size_t rss = sizeof (*coro);
695 714
696 if (coro->mainstack) 715 if (coro->mainstack)
697 { 716 {
698 perl_slots tmp_slot;
699 perl_slots *slot;
700
701 if (coro->flags & CF_RUNNING) 717 if (coro->flags & CF_RUNNING)
702 { 718 {
703 slot = &tmp_slot; 719 #define SYM(sym) PL_ ## sym
704 720 CORO_RSS;
705 #define VAR(name,type) slot->name = PL_ ## name;
706 # include "state.h"
707 #undef VAR 721 #undef SYM
708 } 722 }
709 else 723 else
710 slot = coro->slot;
711
712 if (slot)
713 { 724 {
714 rss += sizeof (slot->curstackinfo); 725 #define SYM(sym) coro->slot->sym
715 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 726 CORO_RSS;
716 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); 727 #undef SYM
717 rss += slot->tmps_max * sizeof (SV *);
718 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
719 rss += slot->scopestack_max * sizeof (I32);
720 rss += slot->savestack_max * sizeof (ANY);
721
722#if !PERL_VERSION_ATLEAST (5,10,0)
723 rss += slot->retstack_max * sizeof (OP *);
724#endif
725 } 728 }
726 } 729 }
727 730
728 return rss; 731 return rss;
729} 732}
871 dSP; 874 dSP;
872 UNOP myop; 875 UNOP myop;
873 876
874 Zero (&myop, 1, UNOP); 877 Zero (&myop, 1, UNOP);
875 myop.op_next = Nullop; 878 myop.op_next = Nullop;
879 myop.op_type = OP_ENTERSUB;
876 myop.op_flags = OPf_WANT_VOID; 880 myop.op_flags = OPf_WANT_VOID;
877 881
878 PUSHMARK (SP); 882 PUSHMARK (SP);
879 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 883 PUSHs ((SV *)coro->startcv);
880 PUTBACK; 884 PUTBACK;
881 PL_op = (OP *)&myop; 885 PL_op = (OP *)&myop;
882 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 886 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
883 SPAGAIN;
884 } 887 }
885 888
886 /* 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
887 * 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.
888 */ 891 */
891 894
892 /* 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 */
893 coro_setup_op.op_next = PL_op; 896 coro_setup_op.op_next = PL_op;
894 coro_setup_op.op_type = OP_CUSTOM; 897 coro_setup_op.op_type = OP_CUSTOM;
895 coro_setup_op.op_ppaddr = pp_slf; 898 coro_setup_op.op_ppaddr = pp_slf;
896 /* no flags required, as an init function won't be called */ 899 /* no flags etc. required, as an init function won't be called */
897 900
898 PL_op = (OP *)&coro_setup_op; 901 PL_op = (OP *)&coro_setup_op;
899 902
900 /* copy throw, in case it was set before coro_setup */ 903 /* copy throw, in case it was set before coro_setup */
901 CORO_THROW = coro->except; 904 CORO_THROW = coro->except;
929 SvREFCNT_dec (GvSV (irsgv)); 932 SvREFCNT_dec (GvSV (irsgv));
930 933
931 SvREFCNT_dec (PL_diehook); 934 SvREFCNT_dec (PL_diehook);
932 SvREFCNT_dec (PL_warnhook); 935 SvREFCNT_dec (PL_warnhook);
933 936
934 SvREFCNT_dec (CORO_THROW);
935 SvREFCNT_dec (coro->saved_deffh); 937 SvREFCNT_dec (coro->saved_deffh);
936 SvREFCNT_dec (coro->rouse_cb); 938 SvREFCNT_dec (coro->rouse_cb);
939 SvREFCNT_dec (coro->invoke_cb);
940 SvREFCNT_dec (coro->invoke_av);
937 941
938 coro_destruct_stacks (aTHX); 942 coro_destruct_stacks (aTHX);
939} 943}
940 944
941INLINE void 945INLINE void
1144 transfer_tail (aTHX); 1148 transfer_tail (aTHX);
1145 1149
1146 /* 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 */
1147 PL_restartop = PL_op; 1151 PL_restartop = PL_op;
1148 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 */
1149 1157
1150 /* 1158 /*
1151 * 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
1152 * 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)
1153 * 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
1447 } 1455 }
1448 1456
1449 cctx_destroy (coro->cctx); 1457 cctx_destroy (coro->cctx);
1450 SvREFCNT_dec (coro->startcv); 1458 SvREFCNT_dec (coro->startcv);
1451 SvREFCNT_dec (coro->args); 1459 SvREFCNT_dec (coro->args);
1460 SvREFCNT_dec (CORO_THROW);
1452 1461
1453 if (coro->next) coro->next->prev = coro->prev; 1462 if (coro->next) coro->next->prev = coro->prev;
1454 if (coro->prev) coro->prev->next = coro->next; 1463 if (coro->prev) coro->prev->next = coro->next;
1455 if (coro == coro_first) coro_first = coro->next; 1464 if (coro == coro_first) coro_first = coro->next;
1456 1465
1742 coro->slot->runops = RUNOPS_DEFAULT; 1751 coro->slot->runops = RUNOPS_DEFAULT;
1743 } 1752 }
1744} 1753}
1745 1754
1746/*****************************************************************************/ 1755/*****************************************************************************/
1756/* async pool handler */
1757
1758static int
1759slf_check_pool_handler (pTHX_ struct CoroSLF *frame)
1760{
1761 HV *hv = (HV *)SvRV (coro_current);
1762 struct coro *coro = (struct coro *)frame->data;
1763
1764 if (!coro->invoke_cb)
1765 return 1; /* loop till we have invoke */
1766 else
1767 {
1768 hv_store (hv, "desc", sizeof ("desc") - 1,
1769 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1770
1771 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1772
1773 {
1774 dSP;
1775 XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0;
1776 PUTBACK;
1777 }
1778
1779 SvREFCNT_dec (GvAV (PL_defgv));
1780 GvAV (PL_defgv) = coro->invoke_av;
1781 coro->invoke_av = 0;
1782
1783 return 0;
1784 }
1785}
1786
1787static void
1788slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1789{
1790 HV *hv = (HV *)SvRV (coro_current);
1791 struct coro *coro = SvSTATE_hv ((SV *)hv);
1792
1793 if (expect_true (coro->saved_deffh))
1794 {
1795 /* subsequent iteration */
1796 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1797 coro->saved_deffh = 0;
1798
1799 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
1800 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1801 {
1802 coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate);
1803 coro->invoke_av = newAV ();
1804
1805 frame->prepare = prepare_nop;
1806 }
1807 else
1808 {
1809 av_clear (GvAV (PL_defgv));
1810 hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0);
1811
1812 coro->prio = 0;
1813
1814 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1815 api_trace (aTHX_ coro_current, 0);
1816
1817 frame->prepare = prepare_schedule;
1818 av_push (av_async_pool, SvREFCNT_inc (hv));
1819 }
1820 }
1821 else
1822 {
1823 /* first iteration, simply fall through */
1824 frame->prepare = prepare_nop;
1825 }
1826
1827 frame->check = slf_check_pool_handler;
1828 frame->data = (void *)coro;
1829}
1830
1831/*****************************************************************************/
1747/* rouse callback */ 1832/* rouse callback */
1748 1833
1749#define CORO_MAGIC_type_rouse PERL_MAGIC_ext 1834#define CORO_MAGIC_type_rouse PERL_MAGIC_ext
1750 1835
1751static void 1836static void
1755 SV *data = (SV *)GENSUB_ARG; 1840 SV *data = (SV *)GENSUB_ARG;
1756 1841
1757 if (SvTYPE (SvRV (data)) != SVt_PVAV) 1842 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1758 { 1843 {
1759 /* first call, set args */ 1844 /* first call, set args */
1760 int i;
1761 AV *av = newAV (); 1845 AV *av = newAV ();
1762 SV *coro = SvRV (data); 1846 SV *coro = SvRV (data);
1763 1847
1764 SvRV_set (data, (SV *)av); 1848 SvRV_set (data, (SV *)av);
1765 api_ready (aTHX_ coro); 1849 api_ready (aTHX_ coro);
2294{ 2378{
2295 if (items >= 2) 2379 if (items >= 2)
2296 { 2380 {
2297 /* callback form */ 2381 /* callback form */
2298 AV *av = (AV *)SvRV (arg [0]); 2382 AV *av = (AV *)SvRV (arg [0]);
2299 CV *cb_cv = coro_sv_2cv (arg [1]); 2383 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2300 2384
2301 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); 2385 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv));
2302 2386
2303 if (SvIVX (AvARRAY (av)[0]) > 0) 2387 if (SvIVX (AvARRAY (av)[0]) > 0)
2304 coro_semaphore_adjust (aTHX_ av, 0); 2388 coro_semaphore_adjust (aTHX_ av, 0);
2390 dXSARGS; 2474 dXSARGS;
2391 AV *state = (AV *)GENSUB_ARG; 2475 AV *state = (AV *)GENSUB_ARG;
2392 SV *coro = av_pop (state); 2476 SV *coro = av_pop (state);
2393 SV *data_sv = newSV (sizeof (struct io_state)); 2477 SV *data_sv = newSV (sizeof (struct io_state));
2394 2478
2395 av_extend (state, items); 2479 av_extend (state, items - 1);
2396 2480
2397 sv_upgrade (data_sv, SVt_PV); 2481 sv_upgrade (data_sv, SVt_PV);
2398 SvCUR_set (data_sv, sizeof (struct io_state)); 2482 SvCUR_set (data_sv, sizeof (struct io_state));
2399 SvPOK_only (data_sv); 2483 SvPOK_only (data_sv);
2400 2484
2610 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 2694 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
2611} 2695}
2612 2696
2613SV * 2697SV *
2614new (char *klass, ...) 2698new (char *klass, ...)
2699 ALIAS:
2700 Coro::new = 1
2615 CODE: 2701 CODE:
2616{ 2702{
2617 struct coro *coro; 2703 struct coro *coro;
2618 MAGIC *mg; 2704 MAGIC *mg;
2619 HV *hv; 2705 HV *hv;
2620 CV *cb; 2706 CV *cb;
2621 int i; 2707 int i;
2622 2708
2623 if (items > 1) 2709 if (items > 1)
2624 { 2710 {
2625 cb = coro_sv_2cv (ST (1)); 2711 cb = coro_sv_2cv (aTHX_ ST (1));
2626 2712
2713 if (!ix)
2714 {
2627 if (CvISXSUB (cb)) 2715 if (CvISXSUB (cb))
2628 croak ("Coro::State doesn't support XS functions as coroutine start, caught"); 2716 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
2629 2717
2630 if (!CvROOT (cb)) 2718 if (!CvROOT (cb))
2631 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); 2719 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
2720 }
2632 } 2721 }
2633 2722
2634 Newz (0, coro, 1, struct coro); 2723 Newz (0, coro, 1, struct coro);
2635 coro->args = newAV (); 2724 coro->args = newAV ();
2636 coro->flags = CF_NEW; 2725 coro->flags = CF_NEW;
2644 mg->mg_flags |= MGf_DUP; 2733 mg->mg_flags |= MGf_DUP;
2645 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 2734 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2646 2735
2647 if (items > 1) 2736 if (items > 1)
2648 { 2737 {
2649 coro->startcv = SvREFCNT_inc_NN (cb);
2650
2651 av_extend (coro->args, items - 1); 2738 av_extend (coro->args, items - 1 + ix - 1);
2739
2740 if (ix)
2741 {
2652 av_push (coro->args, SvREFCNT_inc (cb)); 2742 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
2743 cb = cv_coro_run;
2744 }
2745
2746 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
2747
2653 for (i = 2; i < items; i++) 2748 for (i = 2; i < items; i++)
2654 av_push (coro->args, newSVsv (ST (i))); 2749 av_push (coro->args, newSVsv (ST (i)));
2655 } 2750 }
2656} 2751}
2657 OUTPUT: 2752 OUTPUT:
2858 2953
2859BOOT: 2954BOOT:
2860{ 2955{
2861 int i; 2956 int i;
2862 2957
2863 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 2958 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2864 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 2959 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2865 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 2960 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2866 2961 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD);
2962 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
2867 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); 2963 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
2868 SvREADONLY_on (coro_current); 2964
2965 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
2966 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
2967 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
2968 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
2869 2969
2870 coro_stash = gv_stashpv ("Coro", TRUE); 2970 coro_stash = gv_stashpv ("Coro", TRUE);
2871 2971
2872 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 2972 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2873 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 2973 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2962 CODE: 3062 CODE:
2963 RETVAL = coro_nready; 3063 RETVAL = coro_nready;
2964 OUTPUT: 3064 OUTPUT:
2965 RETVAL 3065 RETVAL
2966 3066
2967# for async_pool speedup
2968void 3067void
2969_pool_1 (SV *cb) 3068_pool_handler (...)
2970 CODE: 3069 CODE:
2971{ 3070 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
2972 HV *hv = (HV *)SvRV (coro_current);
2973 struct coro *coro = SvSTATE_hv ((SV *)hv);
2974 AV *defav = GvAV (PL_defgv);
2975 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
2976 AV *invoke_av;
2977 int i, len;
2978 3071
2979 if (!invoke) 3072void
3073async_pool (SV *cv, ...)
3074 PROTOTYPE: &@
3075 PPCODE:
3076{
3077 HV *hv = (HV *)av_pop (av_async_pool);
3078 AV *av = newAV ();
3079 SV *cb = ST (0);
3080 int i;
3081
3082 av_extend (av, items - 2);
3083 for (i = 1; i < items; ++i)
3084 av_push (av, SvREFCNT_inc_NN (ST (i)));
3085
3086 if ((SV *)hv == &PL_sv_undef)
2980 { 3087 {
2981 SV *old = PL_diehook; 3088 PUSHMARK (SP);
2982 PL_diehook = 0; 3089 EXTEND (SP, 2);
2983 SvREFCNT_dec (old); 3090 PUSHs (sv_Coro);
2984 croak ("\3async_pool terminate\2\n"); 3091 PUSHs ((SV *)cv_pool_handler);
3092 PUTBACK;
3093 call_sv ((SV *)cv_coro_state_new, G_SCALAR);
3094 SPAGAIN;
3095
3096 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs));
2985 } 3097 }
2986 3098
2987 SvREFCNT_dec (coro->saved_deffh);
2988 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2989
2990 hv_store (hv, "desc", sizeof ("desc") - 1,
2991 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2992
2993 invoke_av = (AV *)SvRV (invoke);
2994 len = av_len (invoke_av);
2995
2996 sv_setsv (cb, AvARRAY (invoke_av)[0]);
2997
2998 if (len > 0)
2999 { 3099 {
3000 av_fill (defav, len - 1); 3100 struct coro *coro = SvSTATE_hv (hv);
3001 for (i = 0; i < len; ++i) 3101
3002 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); 3102 assert (!coro->invoke_cb);
3103 assert (!coro->invoke_av);
3104 coro->invoke_cb = SvREFCNT_inc (cb);
3105 coro->invoke_av = av;
3003 } 3106 }
3004}
3005 3107
3006void 3108 api_ready (aTHX_ (SV *)hv);
3007_pool_2 (SV *cb)
3008 CODE:
3009{
3010 HV *hv = (HV *)SvRV (coro_current);
3011 struct coro *coro = SvSTATE_hv ((SV *)hv);
3012 3109
3013 sv_setsv (cb, &PL_sv_undef); 3110 if (GIMME_V != G_VOID)
3014 3111 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
3015 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 3112 else
3016 coro->saved_deffh = 0;
3017
3018 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
3019 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
3020 {
3021 SV *old = PL_diehook;
3022 PL_diehook = 0;
3023 SvREFCNT_dec (old); 3113 SvREFCNT_dec (hv);
3024 croak ("\3async_pool terminate\2\n");
3025 }
3026
3027 av_clear (GvAV (PL_defgv));
3028 hv_store (hv, "desc", sizeof ("desc") - 1,
3029 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
3030
3031 coro->prio = 0;
3032
3033 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
3034 api_trace (aTHX_ coro_current, 0);
3035
3036 av_push (av_async_pool, newSVsv (coro_current));
3037} 3114}
3038 3115
3039SV * 3116SV *
3040rouse_cb () 3117rouse_cb ()
3041 PROTOTYPE: 3118 PROTOTYPE:
3215 3292
3216void 3293void
3217_register (char *target, char *proto, SV *req) 3294_register (char *target, char *proto, SV *req)
3218 CODE: 3295 CODE:
3219{ 3296{
3220 CV *req_cv = coro_sv_2cv (req); 3297 CV *req_cv = coro_sv_2cv (aTHX_ req);
3221 /* newXSproto doesn't return the CV on 5.8 */ 3298 /* newXSproto doesn't return the CV on 5.8 */
3222 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); 3299 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
3223 sv_setpv ((SV *)slf_cv, proto); 3300 sv_setpv ((SV *)slf_cv, proto);
3224 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 3301 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
3225} 3302}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines