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.303 by root, Wed Nov 19 05:52:42 2008 UTC vs.
Revision 1.320 by root, Fri Nov 21 07:29:04 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
167static AV *main_mainstack; /* used to differentiate between $main and others */ 170static AV *main_mainstack; /* used to differentiate between $main and others */
168static JMPENV *main_top_env; 171static JMPENV *main_top_env;
169static HV *coro_state_stash, *coro_stash; 172static HV *coro_state_stash, *coro_stash;
170static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 173static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
171 174
175static AV *av_destroy; /* destruction queue */
176static SV *sv_manager; /* the manager coro */
177
172static GV *irsgv; /* $/ */ 178static GV *irsgv; /* $/ */
173static GV *stdoutgv; /* *STDOUT */ 179static GV *stdoutgv; /* *STDOUT */
174static SV *rv_diehook; 180static SV *rv_diehook;
175static SV *rv_warnhook; 181static SV *rv_warnhook;
176static HV *hv_sig; /* %SIG */ 182static HV *hv_sig; /* %SIG */
177 183
178/* async_pool helper stuff */ 184/* async_pool helper stuff */
179static SV *sv_pool_rss; 185static SV *sv_pool_rss;
180static SV *sv_pool_size; 186static SV *sv_pool_size;
187static SV *sv_async_pool_idle; /* description string */
181static AV *av_async_pool; 188static AV *av_async_pool; /* idle pool */
189static SV *sv_Coro; /* class string */
190static CV *cv_pool_handler;
191static CV *cv_coro_state_new;
182 192
183/* Coro::AnyEvent */ 193/* Coro::AnyEvent */
184static SV *sv_activity; 194static SV *sv_activity;
185 195
186static struct coro_cctx *cctx_first; 196static struct coro_cctx *cctx_first;
246 /* state data */ 256 /* state data */
247 struct CoroSLF slf_frame; /* saved slf frame */ 257 struct CoroSLF slf_frame; /* saved slf frame */
248 AV *mainstack; 258 AV *mainstack;
249 perl_slots *slot; /* basically the saved sp */ 259 perl_slots *slot; /* basically the saved sp */
250 260
261 CV *startcv; /* the CV to execute */
251 AV *args; /* data associated with this coroutine (initial args) */ 262 AV *args; /* data associated with this coroutine (initial args) */
252 int refcnt; /* coroutines are refcounted, yes */ 263 int refcnt; /* coroutines are refcounted, yes */
253 int flags; /* CF_ flags */ 264 int flags; /* CF_ flags */
254 HV *hv; /* the perl hash associated with this coro, if any */ 265 HV *hv; /* the perl hash associated with this coro, if any */
255 void (*on_destroy)(pTHX_ struct coro *coro); 266 void (*on_destroy)(pTHX_ struct coro *coro);
256 267
257 /* statistics */ 268 /* statistics */
258 int usecount; /* number of transfers to this coro */ 269 int usecount; /* number of transfers to this coro */
259 270
262 SV *except; /* exception to be thrown */ 273 SV *except; /* exception to be thrown */
263 SV *rouse_cb; 274 SV *rouse_cb;
264 275
265 /* async_pool */ 276 /* async_pool */
266 SV *saved_deffh; 277 SV *saved_deffh;
278 SV *invoke_cb;
279 AV *invoke_av;
267 280
268 /* linked list */ 281 /* linked list */
269 struct coro *next, *prev; 282 struct coro *next, *prev;
270}; 283};
271 284
288 301
289/* for Coro.pm */ 302/* for Coro.pm */
290static SV *coro_current; 303static SV *coro_current;
291static SV *coro_readyhook; 304static SV *coro_readyhook;
292static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 305static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
306static CV *cv_coro_run, *cv_coro_terminate;
293static struct coro *coro_first; 307static struct coro *coro_first;
294#define coro_nready coroapi.nready 308#define coro_nready coroapi.nready
295 309
296/** lowlevel stuff **********************************************************/ 310/** lowlevel stuff **********************************************************/
297 311
321#if PERL_VERSION_ATLEAST (5,10,0) 335#if PERL_VERSION_ATLEAST (5,10,0)
322 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 336 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
323 get_hv (name, create); 337 get_hv (name, create);
324#endif 338#endif
325 return get_hv (name, create); 339 return get_hv (name, create);
340}
341
342/* may croak */
343INLINE CV *
344coro_sv_2cv (pTHX_ SV *sv)
345{
346 HV *st;
347 GV *gvp;
348 return sv_2cv (sv, &st, &gvp, 0);
326} 349}
327 350
328static AV * 351static AV *
329coro_clone_padlist (pTHX_ CV *cv) 352coro_clone_padlist (pTHX_ CV *cv)
330{ 353{
449 else 472 else
450 { 473 {
451#if CORO_PREFER_PERL_FUNCTIONS 474#if CORO_PREFER_PERL_FUNCTIONS
452 /* this is probably cleaner? but also slower! */ 475 /* this is probably cleaner? but also slower! */
453 /* in practise, it seems to be less stable */ 476 /* in practise, it seems to be less stable */
454 CV *cp = Perl_cv_clone (cv); 477 CV *cp = Perl_cv_clone (aTHX_ cv);
455 CvPADLIST (cv) = CvPADLIST (cp); 478 CvPADLIST (cv) = CvPADLIST (cp);
456 CvPADLIST (cp) = 0; 479 CvPADLIST (cp) = 0;
457 SvREFCNT_dec (cp); 480 SvREFCNT_dec (cp);
458#else 481#else
459 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 482 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv);
603 * of perl.c:init_stacks, except that it uses less memory 626 * of perl.c:init_stacks, except that it uses less memory
604 * on the (sometimes correct) assumption that coroutines do 627 * on the (sometimes correct) assumption that coroutines do
605 * not usually need a lot of stackspace. 628 * not usually need a lot of stackspace.
606 */ 629 */
607#if CORO_PREFER_PERL_FUNCTIONS 630#if CORO_PREFER_PERL_FUNCTIONS
608# define coro_init_stacks init_stacks 631# define coro_init_stacks(thx) init_stacks ()
609#else 632#else
610static void 633static void
611coro_init_stacks (pTHX) 634coro_init_stacks (pTHX)
612{ 635{
613 PL_curstackinfo = new_stackinfo(32, 8); 636 PL_curstackinfo = new_stackinfo(32, 8);
676#if !PERL_VERSION_ATLEAST (5,10,0) 699#if !PERL_VERSION_ATLEAST (5,10,0)
677 Safefree (PL_retstack); 700 Safefree (PL_retstack);
678#endif 701#endif
679} 702}
680 703
704#define CORO_RSS \
705 rss += sizeof (SYM (curstackinfo)); \
706 rss += (SYM (curstackinfo->si_cxmax) + 1) * sizeof (PERL_CONTEXT); \
707 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (SYM (curstack))) * sizeof (SV *); \
708 rss += SYM (tmps_max) * sizeof (SV *); \
709 rss += (SYM (markstack_max) - SYM (markstack_ptr)) * sizeof (I32); \
710 rss += SYM (scopestack_max) * sizeof (I32); \
711 rss += SYM (savestack_max) * sizeof (ANY);
712
681static size_t 713static size_t
682coro_rss (pTHX_ struct coro *coro) 714coro_rss (pTHX_ struct coro *coro)
683{ 715{
684 size_t rss = sizeof (*coro); 716 size_t rss = sizeof (*coro);
685 717
686 if (coro->mainstack) 718 if (coro->mainstack)
687 { 719 {
688 perl_slots tmp_slot;
689 perl_slots *slot;
690
691 if (coro->flags & CF_RUNNING) 720 if (coro->flags & CF_RUNNING)
692 { 721 {
693 slot = &tmp_slot; 722 #define SYM(sym) PL_ ## sym
694 723 CORO_RSS;
695 #define VAR(name,type) slot->name = PL_ ## name;
696 # include "state.h"
697 #undef VAR 724 #undef SYM
698 } 725 }
699 else 726 else
700 slot = coro->slot;
701
702 if (slot)
703 { 727 {
704 rss += sizeof (slot->curstackinfo); 728 #define SYM(sym) coro->slot->sym
705 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 729 CORO_RSS;
706 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); 730 #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 } 731 }
716 } 732 }
717 733
718 return rss; 734 return rss;
719} 735}
819 835
820static int 836static int
821slf_check_nop (pTHX_ struct CoroSLF *frame) 837slf_check_nop (pTHX_ struct CoroSLF *frame)
822{ 838{
823 return 0; 839 return 0;
840}
841
842static int
843slf_check_repeat (pTHX_ struct CoroSLF *frame)
844{
845 return 1;
824} 846}
825 847
826static UNOP coro_setup_op; 848static UNOP coro_setup_op;
827 849
828static void NOINLINE /* noinline to keep it out of the transfer fast path */ 850static void NOINLINE /* noinline to keep it out of the transfer fast path */
861 dSP; 883 dSP;
862 UNOP myop; 884 UNOP myop;
863 885
864 Zero (&myop, 1, UNOP); 886 Zero (&myop, 1, UNOP);
865 myop.op_next = Nullop; 887 myop.op_next = Nullop;
888 myop.op_type = OP_ENTERSUB;
866 myop.op_flags = OPf_WANT_VOID; 889 myop.op_flags = OPf_WANT_VOID;
867 890
868 PUSHMARK (SP); 891 PUSHMARK (SP);
869 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 892 PUSHs ((SV *)coro->startcv);
870 PUTBACK; 893 PUTBACK;
871 PL_op = (OP *)&myop; 894 PL_op = (OP *)&myop;
872 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 895 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
873 SPAGAIN;
874 } 896 }
875 897
876 /* this newly created coroutine might be run on an existing cctx which most 898 /* 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. 899 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here.
878 */ 900 */
879 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 901 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
880 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ 902 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
881 903
882 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ 904 /* 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; 905 coro_setup_op.op_next = PL_op;
884 coro_setup_op.op_type = OP_CUSTOM; 906 coro_setup_op.op_type = OP_ENTERSUB;
885 coro_setup_op.op_ppaddr = pp_slf; 907 coro_setup_op.op_ppaddr = pp_slf;
886 /* no flags required, as an init function won't be called */ 908 /* no flags etc. required, as an init function won't be called */
887 909
888 PL_op = (OP *)&coro_setup_op; 910 PL_op = (OP *)&coro_setup_op;
889 911
890 /* copy throw, in case it was set before coro_setup */ 912 /* copy throw, in case it was set before coro_setup */
891 CORO_THROW = coro->except; 913 CORO_THROW = coro->except;
919 SvREFCNT_dec (GvSV (irsgv)); 941 SvREFCNT_dec (GvSV (irsgv));
920 942
921 SvREFCNT_dec (PL_diehook); 943 SvREFCNT_dec (PL_diehook);
922 SvREFCNT_dec (PL_warnhook); 944 SvREFCNT_dec (PL_warnhook);
923 945
924 SvREFCNT_dec (CORO_THROW);
925 SvREFCNT_dec (coro->saved_deffh); 946 SvREFCNT_dec (coro->saved_deffh);
926 SvREFCNT_dec (coro->rouse_cb); 947 SvREFCNT_dec (coro->rouse_cb);
948 SvREFCNT_dec (coro->invoke_cb);
949 SvREFCNT_dec (coro->invoke_av);
927 950
928 coro_destruct_stacks (aTHX); 951 coro_destruct_stacks (aTHX);
929} 952}
930 953
931INLINE void 954INLINE void
1003 { 1026 {
1004 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1027 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1005 1028
1006 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1029 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1007 { 1030 {
1008 runops_proc_t old_runops = PL_runops;
1009 dSP; 1031 dSP;
1010 GV *gv = CvGV (cx->blk_sub.cv); 1032 GV *gv = CvGV (cx->blk_sub.cv);
1011 SV *fullname = sv_2mortal (newSV (0)); 1033 SV *fullname = sv_2mortal (newSV (0));
1012 1034
1013 if (isGV (gv)) 1035 if (isGV (gv))
1018 SAVETMPS; 1040 SAVETMPS;
1019 EXTEND (SP, 3); 1041 EXTEND (SP, 3);
1020 PUSHMARK (SP); 1042 PUSHMARK (SP);
1021 PUSHs (&PL_sv_yes); 1043 PUSHs (&PL_sv_yes);
1022 PUSHs (fullname); 1044 PUSHs (fullname);
1023 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); 1045 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
1024 PUTBACK; 1046 PUTBACK;
1025 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); 1047 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); 1048 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1027 SPAGAIN; 1049 SPAGAIN;
1028 FREETMPS; 1050 FREETMPS;
1134 transfer_tail (aTHX); 1156 transfer_tail (aTHX);
1135 1157
1136 /* somebody or something will hit me for both perl_run and PL_restartop */ 1158 /* somebody or something will hit me for both perl_run and PL_restartop */
1137 PL_restartop = PL_op; 1159 PL_restartop = PL_op;
1138 perl_run (PL_curinterp); 1160 perl_run (PL_curinterp);
1161 /*
1162 * Unfortunately, there is no way to get at the return values of the
1163 * coro body here, as perl_run destroys these
1164 */
1139 1165
1140 /* 1166 /*
1141 * If perl-run returns we assume exit() was being called or the coro 1167 * 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) 1168 * 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 1169 * reason for perl_run to return. We try to exit by jumping to the
1435 1461
1436 coro->slot = 0; 1462 coro->slot = 0;
1437 } 1463 }
1438 1464
1439 cctx_destroy (coro->cctx); 1465 cctx_destroy (coro->cctx);
1466 SvREFCNT_dec (coro->startcv);
1440 SvREFCNT_dec (coro->args); 1467 SvREFCNT_dec (coro->args);
1468 SvREFCNT_dec (CORO_THROW);
1441 1469
1442 if (coro->next) coro->next->prev = coro->prev; 1470 if (coro->next) coro->next->prev = coro->prev;
1443 if (coro->prev) coro->prev->next = coro->next; 1471 if (coro->prev) coro->prev->next = coro->next;
1444 if (coro == coro_first) coro_first = coro->next; 1472 if (coro == coro_first) coro_first = coro->next;
1445 1473
1592api_is_ready (pTHX_ SV *coro_sv) 1620api_is_ready (pTHX_ SV *coro_sv)
1593{ 1621{
1594 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1622 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1595} 1623}
1596 1624
1625/* expects to own a reference to next->hv */
1597INLINE void 1626INLINE void
1627prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
1628{
1629 SV *prev_sv = SvRV (coro_current);
1630
1631 ta->prev = SvSTATE_hv (prev_sv);
1632 ta->next = next;
1633
1634 TRANSFER_CHECK (*ta);
1635
1636 SvRV_set (coro_current, (SV *)next->hv);
1637
1638 free_coro_mortal (aTHX);
1639 coro_mortal = prev_sv;
1640}
1641
1642static void
1598prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1643prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1599{ 1644{
1600 SV *prev_sv, *next_sv;
1601
1602 for (;;) 1645 for (;;)
1603 { 1646 {
1604 next_sv = coro_deq (aTHX); 1647 SV *next_sv = coro_deq (aTHX);
1605 1648
1606 /* nothing to schedule: call the idle handler */
1607 if (expect_false (!next_sv)) 1649 if (expect_true (next_sv))
1608 { 1650 {
1651 struct coro *next = SvSTATE_hv (next_sv);
1652
1653 /* cannot transfer to destroyed coros, skip and look for next */
1654 if (expect_false (next->flags & CF_DESTROYED))
1655 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */
1656 else
1657 {
1658 next->flags &= ~CF_READY;
1659 --coro_nready;
1660
1661 prepare_schedule_to (aTHX_ ta, next);
1662 break;
1663 }
1664 }
1665 else
1666 {
1667 /* nothing to schedule: call the idle handler */
1609 dSP; 1668 dSP;
1610 1669
1611 ENTER; 1670 ENTER;
1612 SAVETMPS; 1671 SAVETMPS;
1613 1672
1615 PUTBACK; 1674 PUTBACK;
1616 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1675 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD);
1617 1676
1618 FREETMPS; 1677 FREETMPS;
1619 LEAVE; 1678 LEAVE;
1620 continue;
1621 } 1679 }
1622
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 {
1628 SvREFCNT_dec (next_sv);
1629 /* coro_nready has already been taken care of by destroy */
1630 continue;
1631 }
1632
1633 --coro_nready;
1634 break;
1635 } 1680 }
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} 1681}
1648 1682
1649INLINE void 1683INLINE void
1650prepare_cede (pTHX_ struct coro_transfer_args *ta) 1684prepare_cede (pTHX_ struct coro_transfer_args *ta)
1651{ 1685{
1672{ 1706{
1673 struct coro_transfer_args ta; 1707 struct coro_transfer_args ta;
1674 1708
1675 prepare_schedule (aTHX_ &ta); 1709 prepare_schedule (aTHX_ &ta);
1676 TRANSFER (ta, 1); 1710 TRANSFER (ta, 1);
1711}
1712
1713static void
1714api_schedule_to (pTHX_ SV *coro_sv)
1715{
1716 struct coro_transfer_args ta;
1717 struct coro *next = SvSTATE (coro_sv);
1718
1719 SvREFCNT_inc_NN (coro_sv);
1720 prepare_schedule_to (aTHX_ &ta, next);
1677} 1721}
1678 1722
1679static int 1723static int
1680api_cede (pTHX) 1724api_cede (pTHX)
1681{ 1725{
1730 else 1774 else
1731 coro->slot->runops = RUNOPS_DEFAULT; 1775 coro->slot->runops = RUNOPS_DEFAULT;
1732 } 1776 }
1733} 1777}
1734 1778
1779static void
1780coro_call_on_destroy (pTHX_ struct coro *coro)
1781{
1782 SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0);
1783 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
1784
1785 if (on_destroyp)
1786 {
1787 AV *on_destroy = (AV *)SvRV (*on_destroyp);
1788
1789 while (AvFILLp (on_destroy) >= 0)
1790 {
1791 dSP; /* don't disturb outer sp */
1792 SV *cb = av_pop (on_destroy);
1793
1794 PUSHMARK (SP);
1795
1796 if (statusp)
1797 {
1798 int i;
1799 AV *status = (AV *)SvRV (*statusp);
1800 EXTEND (SP, AvFILLp (status) + 1);
1801
1802 for (i = 0; i <= AvFILLp (status); ++i)
1803 PUSHs (AvARRAY (status)[i]);
1804 }
1805
1806 PUTBACK;
1807 call_sv (sv_2mortal (cb), G_VOID | G_DISCARD);
1808 }
1809 }
1810}
1811
1812static void
1813slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1814{
1815 int i;
1816 HV *hv = (HV *)SvRV (coro_current);
1817 AV *av = newAV ();
1818
1819 av_extend (av, items - 1);
1820 for (i = 0; i < items; ++i)
1821 av_push (av, SvREFCNT_inc_NN (arg [i]));
1822
1823 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0);
1824
1825 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1826 api_ready (aTHX_ sv_manager);
1827
1828 frame->prepare = prepare_schedule;
1829 frame->check = slf_check_repeat;
1830}
1831
1832/*****************************************************************************/
1833/* async pool handler */
1834
1835static int
1836slf_check_pool_handler (pTHX_ struct CoroSLF *frame)
1837{
1838 HV *hv = (HV *)SvRV (coro_current);
1839 struct coro *coro = (struct coro *)frame->data;
1840
1841 if (!coro->invoke_cb)
1842 return 1; /* loop till we have invoke */
1843 else
1844 {
1845 hv_store (hv, "desc", sizeof ("desc") - 1,
1846 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1847
1848 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1849
1850 {
1851 dSP;
1852 XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0;
1853 PUTBACK;
1854 }
1855
1856 SvREFCNT_dec (GvAV (PL_defgv));
1857 GvAV (PL_defgv) = coro->invoke_av;
1858 coro->invoke_av = 0;
1859
1860 return 0;
1861 }
1862}
1863
1864static void
1865slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1866{
1867 HV *hv = (HV *)SvRV (coro_current);
1868 struct coro *coro = SvSTATE_hv ((SV *)hv);
1869
1870 if (expect_true (coro->saved_deffh))
1871 {
1872 /* subsequent iteration */
1873 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1874 coro->saved_deffh = 0;
1875
1876 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
1877 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1878 {
1879 coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate);
1880 coro->invoke_av = newAV ();
1881
1882 frame->prepare = prepare_nop;
1883 }
1884 else
1885 {
1886 av_clear (GvAV (PL_defgv));
1887 hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0);
1888
1889 coro->prio = 0;
1890
1891 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1892 api_trace (aTHX_ coro_current, 0);
1893
1894 frame->prepare = prepare_schedule;
1895 av_push (av_async_pool, SvREFCNT_inc (hv));
1896 }
1897 }
1898 else
1899 {
1900 /* first iteration, simply fall through */
1901 frame->prepare = prepare_nop;
1902 }
1903
1904 frame->check = slf_check_pool_handler;
1905 frame->data = (void *)coro;
1906}
1907
1735/*****************************************************************************/ 1908/*****************************************************************************/
1736/* rouse callback */ 1909/* rouse callback */
1737 1910
1738#define CORO_MAGIC_type_rouse PERL_MAGIC_ext 1911#define CORO_MAGIC_type_rouse PERL_MAGIC_ext
1739 1912
1744 SV *data = (SV *)GENSUB_ARG; 1917 SV *data = (SV *)GENSUB_ARG;
1745 1918
1746 if (SvTYPE (SvRV (data)) != SVt_PVAV) 1919 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1747 { 1920 {
1748 /* first call, set args */ 1921 /* first call, set args */
1749 int i;
1750 AV *av = newAV (); 1922 AV *av = newAV ();
1751 SV *coro = SvRV (data); 1923 SV *coro = SvRV (data);
1752 1924
1753 SvRV_set (data, (SV *)av); 1925 SvRV_set (data, (SV *)av);
1754 api_ready (aTHX_ coro); 1926 api_ready (aTHX_ coro);
1895static void 2067static void
1896slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2068slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1897{ 2069{
1898 frame->prepare = prepare_schedule; 2070 frame->prepare = prepare_schedule;
1899 frame->check = slf_check_nop; 2071 frame->check = slf_check_nop;
2072}
2073
2074static void
2075slf_prepare_schedule_to (pTHX_ struct coro_transfer_args *ta)
2076{
2077 struct coro *next = (struct coro *)slf_frame.data;
2078
2079 SvREFCNT_inc_NN (next->hv);
2080 prepare_schedule_to (aTHX_ ta, next);
2081}
2082
2083static void
2084slf_init_schedule_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2085{
2086 if (!items)
2087 croak ("Coro::schedule_to expects a coroutine argument, caught");
2088
2089 frame->data = (void *)SvSTATE (arg [0]);
2090 frame->prepare = slf_prepare_schedule_to;
2091 frame->check = slf_check_nop;
2092}
2093
2094static void
2095slf_init_cede_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2096{
2097 api_ready (aTHX_ SvRV (coro_current));
2098
2099 slf_init_schedule_to (aTHX_ frame, cv, arg, items);
1900} 2100}
1901 2101
1902static void 2102static void
1903slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2103slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1904{ 2104{
2050 } 2250 }
2051 else 2251 else
2052 slf_argc = 0; 2252 slf_argc = 0;
2053 2253
2054 PL_op->op_ppaddr = pp_slf; 2254 PL_op->op_ppaddr = pp_slf;
2055 PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ 2255 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2056 2256
2057 PL_op = (OP *)&slf_restore; 2257 PL_op = (OP *)&slf_restore;
2058} 2258}
2059 2259
2060/*****************************************************************************/ 2260/*****************************************************************************/
2143 SV **ary; 2343 SV **ary;
2144 2344
2145 /* unfortunately, building manually saves memory */ 2345 /* unfortunately, building manually saves memory */
2146 Newx (ary, 2, SV *); 2346 Newx (ary, 2, SV *);
2147 AvALLOC (av) = ary; 2347 AvALLOC (av) = ary;
2148 AvARRAY (av) = ary; 2348 /*AvARRAY (av) = ary;*/
2349 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */
2149 AvMAX (av) = 1; 2350 AvMAX (av) = 1;
2150 AvFILLp (av) = 0; 2351 AvFILLp (av) = 0;
2151 ary [0] = newSViv (count); 2352 ary [0] = newSViv (count);
2152 2353
2153 return newRV_noinc ((SV *)av); 2354 return newRV_noinc ((SV *)av);
2173 AvARRAY (av)[0] = AvARRAY (av)[1]; 2374 AvARRAY (av)[0] = AvARRAY (av)[1];
2174 AvARRAY (av)[1] = count_sv; 2375 AvARRAY (av)[1] = count_sv;
2175 cb = av_shift (av); 2376 cb = av_shift (av);
2176 2377
2177 if (SvOBJECT (cb)) 2378 if (SvOBJECT (cb))
2379 {
2178 api_ready (aTHX_ cb); 2380 api_ready (aTHX_ cb);
2179 else 2381 --count;
2180 croak ("callbacks not yet supported"); 2382 }
2383 else if (SvTYPE (cb) == SVt_PVCV)
2384 {
2385 dSP;
2386 PUSHMARK (SP);
2387 XPUSHs (sv_2mortal (newRV_inc ((SV *)av)));
2388 PUTBACK;
2389 call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR);
2390 }
2181 2391
2182 SvREFCNT_dec (cb); 2392 SvREFCNT_dec (cb);
2183
2184 --count;
2185 } 2393 }
2186} 2394}
2187 2395
2188static void 2396static void
2189coro_semaphore_on_destroy (pTHX_ struct coro *coro) 2397coro_semaphore_on_destroy (pTHX_ struct coro *coro)
2271} 2479}
2272 2480
2273static void 2481static void
2274slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2482slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2275{ 2483{
2484 if (items >= 2)
2485 {
2486 /* callback form */
2487 AV *av = (AV *)SvRV (arg [0]);
2488 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2489
2490 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv));
2491
2492 if (SvIVX (AvARRAY (av)[0]) > 0)
2493 coro_semaphore_adjust (aTHX_ av, 0);
2494
2495 frame->prepare = prepare_nop;
2496 frame->check = slf_check_nop;
2497 }
2498 else
2499 {
2276 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); 2500 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items);
2277 frame->check = slf_check_semaphore_wait; 2501 frame->check = slf_check_semaphore_wait;
2502 }
2278} 2503}
2279 2504
2280/* signal */ 2505/* signal */
2281 2506
2282static void 2507static void
2354 dXSARGS; 2579 dXSARGS;
2355 AV *state = (AV *)GENSUB_ARG; 2580 AV *state = (AV *)GENSUB_ARG;
2356 SV *coro = av_pop (state); 2581 SV *coro = av_pop (state);
2357 SV *data_sv = newSV (sizeof (struct io_state)); 2582 SV *data_sv = newSV (sizeof (struct io_state));
2358 2583
2359 av_extend (state, items); 2584 av_extend (state, items - 1);
2360 2585
2361 sv_upgrade (data_sv, SVt_PV); 2586 sv_upgrade (data_sv, SVt_PV);
2362 SvCUR_set (data_sv, sizeof (struct io_state)); 2587 SvCUR_set (data_sv, sizeof (struct io_state));
2363 SvPOK_only (data_sv); 2588 SvPOK_only (data_sv);
2364 2589
2540 2765
2541 { 2766 {
2542 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); 2767 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf)));
2543 2768
2544 if (!PL_custom_op_names) PL_custom_op_names = newHV (); 2769 if (!PL_custom_op_names) PL_custom_op_names = newHV ();
2545 hv_store_ent (PL_custom_op_names, slf, 2770 hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0);
2546 newSVpv ("coro_slf", 0), 0);
2547 2771
2548 if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); 2772 if (!PL_custom_op_descs) PL_custom_op_descs = newHV ();
2549 hv_store_ent (PL_custom_op_descs, slf, 2773 hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0);
2550 newSVpv ("coro schedule like function", 0), 0);
2551 } 2774 }
2552 2775
2553 coroapi.ver = CORO_API_VERSION; 2776 coroapi.ver = CORO_API_VERSION;
2554 coroapi.rev = CORO_API_REVISION; 2777 coroapi.rev = CORO_API_REVISION;
2555 2778
2574 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 2797 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
2575} 2798}
2576 2799
2577SV * 2800SV *
2578new (char *klass, ...) 2801new (char *klass, ...)
2802 ALIAS:
2803 Coro::new = 1
2579 CODE: 2804 CODE:
2580{ 2805{
2581 struct coro *coro; 2806 struct coro *coro;
2582 MAGIC *mg; 2807 MAGIC *mg;
2583 HV *hv; 2808 HV *hv;
2809 CV *cb;
2584 int i; 2810 int i;
2811
2812 if (items > 1)
2813 {
2814 cb = coro_sv_2cv (aTHX_ ST (1));
2815
2816 if (!ix)
2817 {
2818 if (CvISXSUB (cb))
2819 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
2820
2821 if (!CvROOT (cb))
2822 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
2823 }
2824 }
2585 2825
2586 Newz (0, coro, 1, struct coro); 2826 Newz (0, coro, 1, struct coro);
2587 coro->args = newAV (); 2827 coro->args = newAV ();
2588 coro->flags = CF_NEW; 2828 coro->flags = CF_NEW;
2589 2829
2594 coro->hv = hv = newHV (); 2834 coro->hv = hv = newHV ();
2595 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); 2835 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
2596 mg->mg_flags |= MGf_DUP; 2836 mg->mg_flags |= MGf_DUP;
2597 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 2837 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2598 2838
2839 if (items > 1)
2840 {
2599 av_extend (coro->args, items - 1); 2841 av_extend (coro->args, items - 1 + ix - 1);
2842
2843 if (ix)
2844 {
2845 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
2846 cb = cv_coro_run;
2847 }
2848
2849 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
2850
2600 for (i = 1; i < items; i++) 2851 for (i = 2; i < items; i++)
2601 av_push (coro->args, newSVsv (ST (i))); 2852 av_push (coro->args, newSVsv (ST (i)));
2853 }
2602} 2854}
2603 OUTPUT: 2855 OUTPUT:
2604 RETVAL 2856 RETVAL
2605 2857
2606void 2858void
2804 3056
2805BOOT: 3057BOOT:
2806{ 3058{
2807 int i; 3059 int i;
2808 3060
2809 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2810 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3061 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2811 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3062 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2812 3063 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD);
3064 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
2813 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); 3065 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
2814 SvREADONLY_on (coro_current); 3066 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3067 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3068 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3069
3070 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3071 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3072 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3073 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
2815 3074
2816 coro_stash = gv_stashpv ("Coro", TRUE); 3075 coro_stash = gv_stashpv ("Coro", TRUE);
2817 3076
2818 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 3077 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2819 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3078 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2827 3086
2828 { 3087 {
2829 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3088 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2830 3089
2831 coroapi.schedule = api_schedule; 3090 coroapi.schedule = api_schedule;
3091 coroapi.schedule_to = api_schedule_to;
2832 coroapi.cede = api_cede; 3092 coroapi.cede = api_cede;
2833 coroapi.cede_notself = api_cede_notself; 3093 coroapi.cede_notself = api_cede_notself;
2834 coroapi.ready = api_ready; 3094 coroapi.ready = api_ready;
2835 coroapi.is_ready = api_is_ready; 3095 coroapi.is_ready = api_is_ready;
2836 coroapi.nready = coro_nready; 3096 coroapi.nready = coro_nready;
2841 SvREADONLY_on (sv); 3101 SvREADONLY_on (sv);
2842 } 3102 }
2843} 3103}
2844 3104
2845void 3105void
3106terminate (...)
3107 CODE:
3108 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3109
3110void
2846schedule (...) 3111schedule (...)
2847 CODE: 3112 CODE:
2848 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3113 CORO_EXECUTE_SLF_XS (slf_init_schedule);
3114
3115void
3116schedule_to (...)
3117 CODE:
3118 CORO_EXECUTE_SLF_XS (slf_init_schedule_to);
3119
3120void
3121cede_to (...)
3122 CODE:
3123 CORO_EXECUTE_SLF_XS (slf_init_cede_to);
2849 3124
2850void 3125void
2851cede (...) 3126cede (...)
2852 CODE: 3127 CODE:
2853 CORO_EXECUTE_SLF_XS (slf_init_cede); 3128 CORO_EXECUTE_SLF_XS (slf_init_cede);
2854 3129
2855void 3130void
2856cede_notself (...) 3131cede_notself (...)
2857 CODE: 3132 CODE:
2858 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3133 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3134
3135void
3136_cancel (Coro::State self)
3137 CODE:
3138 coro_state_destroy (aTHX_ self);
3139 coro_call_on_destroy (aTHX_ self);
2859 3140
2860void 3141void
2861_set_current (SV *current) 3142_set_current (SV *current)
2862 PROTOTYPE: $ 3143 PROTOTYPE: $
2863 CODE: 3144 CODE:
2908 CODE: 3189 CODE:
2909 RETVAL = coro_nready; 3190 RETVAL = coro_nready;
2910 OUTPUT: 3191 OUTPUT:
2911 RETVAL 3192 RETVAL
2912 3193
2913# for async_pool speedup
2914void 3194void
2915_pool_1 (SV *cb) 3195_pool_handler (...)
2916 CODE: 3196 CODE:
2917{ 3197 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
2918 HV *hv = (HV *)SvRV (coro_current);
2919 struct coro *coro = SvSTATE_hv ((SV *)hv);
2920 AV *defav = GvAV (PL_defgv);
2921 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
2922 AV *invoke_av;
2923 int i, len;
2924 3198
2925 if (!invoke) 3199void
3200async_pool (SV *cv, ...)
3201 PROTOTYPE: &@
3202 PPCODE:
3203{
3204 HV *hv = (HV *)av_pop (av_async_pool);
3205 AV *av = newAV ();
3206 SV *cb = ST (0);
3207 int i;
3208
3209 av_extend (av, items - 2);
3210 for (i = 1; i < items; ++i)
3211 av_push (av, SvREFCNT_inc_NN (ST (i)));
3212
3213 if ((SV *)hv == &PL_sv_undef)
2926 { 3214 {
2927 SV *old = PL_diehook; 3215 PUSHMARK (SP);
2928 PL_diehook = 0; 3216 EXTEND (SP, 2);
2929 SvREFCNT_dec (old); 3217 PUSHs (sv_Coro);
2930 croak ("\3async_pool terminate\2\n"); 3218 PUSHs ((SV *)cv_pool_handler);
3219 PUTBACK;
3220 call_sv ((SV *)cv_coro_state_new, G_SCALAR);
3221 SPAGAIN;
3222
3223 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs));
2931 } 3224 }
2932 3225
2933 SvREFCNT_dec (coro->saved_deffh);
2934 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2935
2936 hv_store (hv, "desc", sizeof ("desc") - 1,
2937 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2938
2939 invoke_av = (AV *)SvRV (invoke);
2940 len = av_len (invoke_av);
2941
2942 sv_setsv (cb, AvARRAY (invoke_av)[0]);
2943
2944 if (len > 0)
2945 { 3226 {
2946 av_fill (defav, len - 1); 3227 struct coro *coro = SvSTATE_hv (hv);
2947 for (i = 0; i < len; ++i) 3228
2948 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); 3229 assert (!coro->invoke_cb);
3230 assert (!coro->invoke_av);
3231 coro->invoke_cb = SvREFCNT_inc (cb);
3232 coro->invoke_av = av;
2949 } 3233 }
2950}
2951 3234
2952void 3235 api_ready (aTHX_ (SV *)hv);
2953_pool_2 (SV *cb)
2954 CODE:
2955{
2956 HV *hv = (HV *)SvRV (coro_current);
2957 struct coro *coro = SvSTATE_hv ((SV *)hv);
2958 3236
2959 sv_setsv (cb, &PL_sv_undef); 3237 if (GIMME_V != G_VOID)
2960 3238 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
2961 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 3239 else
2962 coro->saved_deffh = 0;
2963
2964 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2965 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2966 {
2967 SV *old = PL_diehook;
2968 PL_diehook = 0;
2969 SvREFCNT_dec (old); 3240 SvREFCNT_dec (hv);
2970 croak ("\3async_pool terminate\2\n");
2971 }
2972
2973 av_clear (GvAV (PL_defgv));
2974 hv_store (hv, "desc", sizeof ("desc") - 1,
2975 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
2976
2977 coro->prio = 0;
2978
2979 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
2980 api_trace (aTHX_ coro_current, 0);
2981
2982 av_push (av_async_pool, newSVsv (coro_current));
2983} 3241}
2984 3242
2985SV * 3243SV *
2986rouse_cb () 3244rouse_cb ()
2987 PROTOTYPE: 3245 PROTOTYPE:
2989 RETVAL = coro_new_rouse_cb (aTHX); 3247 RETVAL = coro_new_rouse_cb (aTHX);
2990 OUTPUT: 3248 OUTPUT:
2991 RETVAL 3249 RETVAL
2992 3250
2993void 3251void
2994rouse_wait (SV *cb = 0) 3252rouse_wait (...)
2995 PROTOTYPE: ;$ 3253 PROTOTYPE: ;$
2996 PPCODE: 3254 PPCODE:
2997 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3255 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
2998 3256
2999 3257
3036 adjust = 1 3294 adjust = 1
3037 CODE: 3295 CODE:
3038 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); 3296 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1);
3039 3297
3040void 3298void
3041down (SV *self) 3299down (...)
3042 CODE: 3300 CODE:
3043 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); 3301 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down);
3044 3302
3045void 3303void
3046wait (SV *self) 3304wait (...)
3047 CODE: 3305 CODE:
3048 CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); 3306 CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait);
3049 3307
3050void 3308void
3051try (SV *self) 3309try (SV *self)
3094 ); 3352 );
3095 OUTPUT: 3353 OUTPUT:
3096 RETVAL 3354 RETVAL
3097 3355
3098void 3356void
3099wait (SV *self) 3357wait (...)
3100 CODE: 3358 CODE:
3101 CORO_EXECUTE_SLF_XS (slf_init_signal_wait); 3359 CORO_EXECUTE_SLF_XS (slf_init_signal_wait);
3102 3360
3103void 3361void
3104broadcast (SV *self) 3362broadcast (SV *self)
3161 3419
3162void 3420void
3163_register (char *target, char *proto, SV *req) 3421_register (char *target, char *proto, SV *req)
3164 CODE: 3422 CODE:
3165{ 3423{
3166 HV *st; 3424 CV *req_cv = coro_sv_2cv (aTHX_ req);
3167 GV *gvp;
3168 CV *req_cv = sv_2cv (req, &st, &gvp, 0);
3169 /* newXSproto doesn't return the CV on 5.8 */ 3425 /* newXSproto doesn't return the CV on 5.8 */
3170 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); 3426 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
3171 sv_setpv ((SV *)slf_cv, proto); 3427 sv_setpv ((SV *)slf_cv, proto);
3172 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 3428 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
3173} 3429}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines