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.288 by root, Mon Nov 17 07:14:50 2008 UTC vs.
Revision 1.315 by root, Thu Nov 20 05:25:17 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
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
120#endif 123#endif
121 124
122/* The next macros try to return the current stack pointer, in an as 125/* The next macros try to return the current stack pointer, in an as
123 * portable way as possible. */ 126 * portable way as possible. */
124#if __GNUC__ >= 4 127#if __GNUC__ >= 4
128# define dSTACKLEVEL int stacklevel_dummy
125# define dSTACKLEVEL void *stacklevel = __builtin_frame_address (0) 129# define STACKLEVEL __builtin_frame_address (0)
126#else 130#else
127# define dSTACKLEVEL volatile void *stacklevel = (volatile void *)&stacklevel 131# define dSTACKLEVEL volatile void *stacklevel
132# define STACKLEVEL ((void *)&stacklevel)
128#endif 133#endif
129 134
130#define IN_DESTRUCT (PL_main_cv == Nullcv) 135#define IN_DESTRUCT (PL_main_cv == Nullcv)
131 136
132#if __GNUC__ >= 3 137#if __GNUC__ >= 3
143#define expect_true(expr) expect ((expr) != 0, 1) 148#define expect_true(expr) expect ((expr) != 0, 1)
144 149
145#define NOINLINE attribute ((noinline)) 150#define NOINLINE attribute ((noinline))
146 151
147#include "CoroAPI.h" 152#include "CoroAPI.h"
153#define GCoroAPI (&coroapi) /* very sneaky */
148 154
149#ifdef USE_ITHREADS 155#ifdef USE_ITHREADS
150# if CORO_PTHREAD 156# if CORO_PTHREAD
151static void *coro_thx; 157static void *coro_thx;
152# endif 158# endif
153#endif 159#endif
154 160
155static double (*nvtime)(); /* so why doesn't it take void? */ 161static double (*nvtime)(); /* so why doesn't it take void? */
162
163/* we hijack an hopefully unused CV flag for our purposes */
164#define CVf_SLF 0x4000
165static OP *pp_slf (pTHX);
156 166
157static U32 cctx_gen; 167static U32 cctx_gen;
158static size_t cctx_stacksize = CORO_STACKSIZE; 168static size_t cctx_stacksize = CORO_STACKSIZE;
159static struct CoroAPI coroapi; 169static struct CoroAPI coroapi;
160static AV *main_mainstack; /* used to differentiate between $main and others */ 170static AV *main_mainstack; /* used to differentiate between $main and others */
169static HV *hv_sig; /* %SIG */ 179static HV *hv_sig; /* %SIG */
170 180
171/* async_pool helper stuff */ 181/* async_pool helper stuff */
172static SV *sv_pool_rss; 182static SV *sv_pool_rss;
173static SV *sv_pool_size; 183static SV *sv_pool_size;
184static SV *sv_async_pool_idle;
174static AV *av_async_pool; 185static AV *av_async_pool;
186static SV *sv_Coro;
187static CV *cv_pool_handler;
188static CV *cv_coro_state_new;
175 189
176/* Coro::AnyEvent */ 190/* Coro::AnyEvent */
177static SV *sv_activity; 191static SV *sv_activity;
178 192
179static struct coro_cctx *cctx_first; 193static struct coro_cctx *cctx_first;
239 /* state data */ 253 /* state data */
240 struct CoroSLF slf_frame; /* saved slf frame */ 254 struct CoroSLF slf_frame; /* saved slf frame */
241 AV *mainstack; 255 AV *mainstack;
242 perl_slots *slot; /* basically the saved sp */ 256 perl_slots *slot; /* basically the saved sp */
243 257
258 CV *startcv; /* the CV to execute */
244 AV *args; /* data associated with this coroutine (initial args) */ 259 AV *args; /* data associated with this coroutine (initial args) */
245 int refcnt; /* coroutines are refcounted, yes */ 260 int refcnt; /* coroutines are refcounted, yes */
246 int flags; /* CF_ flags */ 261 int flags; /* CF_ flags */
247 HV *hv; /* the perl hash associated with this coro, if any */ 262 HV *hv; /* the perl hash associated with this coro, if any */
248 void (*on_destroy)(pTHX_ struct coro *coro); 263 void (*on_destroy)(pTHX_ struct coro *coro);
249 264
250 /* statistics */ 265 /* statistics */
251 int usecount; /* number of transfers to this coro */ 266 int usecount; /* number of transfers to this coro */
252 267
253 /* coro process data */ 268 /* coro process data */
254 int prio; 269 int prio;
255 SV *throw; /* exception to be thrown */ 270 SV *except; /* exception to be thrown */
271 SV *rouse_cb;
256 272
257 /* async_pool */ 273 /* async_pool */
258 SV *saved_deffh; 274 SV *saved_deffh;
275 SV *invoke_cb;
276 AV *invoke_av;
259 277
260 /* linked list */ 278 /* linked list */
261 struct coro *next, *prev; 279 struct coro *next, *prev;
262}; 280};
263 281
266 284
267/* the following variables are effectively part of the perl context */ 285/* the following variables are effectively part of the perl context */
268/* and get copied between struct coro and these variables */ 286/* and get copied between struct coro and these variables */
269/* the mainr easonw e don't support windows process emulation */ 287/* the mainr easonw e don't support windows process emulation */
270static struct CoroSLF slf_frame; /* the current slf frame */ 288static struct CoroSLF slf_frame; /* the current slf frame */
271static SV *coro_throw;
272 289
273/** Coro ********************************************************************/ 290/** Coro ********************************************************************/
274 291
275#define PRIO_MAX 3 292#define PRIO_MAX 3
276#define PRIO_HIGH 1 293#define PRIO_HIGH 1
281 298
282/* for Coro.pm */ 299/* for Coro.pm */
283static SV *coro_current; 300static SV *coro_current;
284static SV *coro_readyhook; 301static SV *coro_readyhook;
285static 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;
286static struct coro *coro_first; 304static struct coro *coro_first;
287#define coro_nready coroapi.nready 305#define coro_nready coroapi.nready
288 306
289/** lowlevel stuff **********************************************************/ 307/** lowlevel stuff **********************************************************/
290 308
314#if PERL_VERSION_ATLEAST (5,10,0) 332#if PERL_VERSION_ATLEAST (5,10,0)
315 /* 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 */
316 get_hv (name, create); 334 get_hv (name, create);
317#endif 335#endif
318 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);
319} 346}
320 347
321static AV * 348static AV *
322coro_clone_padlist (pTHX_ CV *cv) 349coro_clone_padlist (pTHX_ CV *cv)
323{ 350{
442 else 469 else
443 { 470 {
444#if CORO_PREFER_PERL_FUNCTIONS 471#if CORO_PREFER_PERL_FUNCTIONS
445 /* this is probably cleaner? but also slower! */ 472 /* this is probably cleaner? but also slower! */
446 /* in practise, it seems to be less stable */ 473 /* in practise, it seems to be less stable */
447 CV *cp = Perl_cv_clone (cv); 474 CV *cp = Perl_cv_clone (aTHX_ cv);
448 CvPADLIST (cv) = CvPADLIST (cp); 475 CvPADLIST (cv) = CvPADLIST (cp);
449 CvPADLIST (cp) = 0; 476 CvPADLIST (cp) = 0;
450 SvREFCNT_dec (cp); 477 SvREFCNT_dec (cp);
451#else 478#else
452 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 479 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv);
505 532
506 PUTBACK; 533 PUTBACK;
507 } 534 }
508 535
509 slf_frame = c->slf_frame; 536 slf_frame = c->slf_frame;
510 coro_throw = c->throw; 537 CORO_THROW = c->except;
511} 538}
512 539
513static void 540static void
514save_perl (pTHX_ Coro__State c) 541save_perl (pTHX_ Coro__State c)
515{ 542{
516 c->throw = coro_throw; 543 c->except = CORO_THROW;
517 c->slf_frame = slf_frame; 544 c->slf_frame = slf_frame;
518 545
519 { 546 {
520 dSP; 547 dSP;
521 I32 cxix = cxstack_ix; 548 I32 cxix = cxstack_ix;
596 * of perl.c:init_stacks, except that it uses less memory 623 * of perl.c:init_stacks, except that it uses less memory
597 * on the (sometimes correct) assumption that coroutines do 624 * on the (sometimes correct) assumption that coroutines do
598 * not usually need a lot of stackspace. 625 * not usually need a lot of stackspace.
599 */ 626 */
600#if CORO_PREFER_PERL_FUNCTIONS 627#if CORO_PREFER_PERL_FUNCTIONS
601# define coro_init_stacks init_stacks 628# define coro_init_stacks(thx) init_stacks ()
602#else 629#else
603static void 630static void
604coro_init_stacks (pTHX) 631coro_init_stacks (pTHX)
605{ 632{
606 PL_curstackinfo = new_stackinfo(32, 8); 633 PL_curstackinfo = new_stackinfo(32, 8);
669#if !PERL_VERSION_ATLEAST (5,10,0) 696#if !PERL_VERSION_ATLEAST (5,10,0)
670 Safefree (PL_retstack); 697 Safefree (PL_retstack);
671#endif 698#endif
672} 699}
673 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
674static size_t 710static size_t
675coro_rss (pTHX_ struct coro *coro) 711coro_rss (pTHX_ struct coro *coro)
676{ 712{
677 size_t rss = sizeof (*coro); 713 size_t rss = sizeof (*coro);
678 714
679 if (coro->mainstack) 715 if (coro->mainstack)
680 { 716 {
681 perl_slots tmp_slot;
682 perl_slots *slot;
683
684 if (coro->flags & CF_RUNNING) 717 if (coro->flags & CF_RUNNING)
685 { 718 {
686 slot = &tmp_slot; 719 #define SYM(sym) PL_ ## sym
687 720 CORO_RSS;
688 #define VAR(name,type) slot->name = PL_ ## name;
689 # include "state.h"
690 #undef VAR 721 #undef SYM
691 } 722 }
692 else 723 else
693 slot = coro->slot;
694
695 if (slot)
696 { 724 {
697 rss += sizeof (slot->curstackinfo); 725 #define SYM(sym) coro->slot->sym
698 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 726 CORO_RSS;
699 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); 727 #undef SYM
700 rss += slot->tmps_max * sizeof (SV *);
701 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
702 rss += slot->scopestack_max * sizeof (I32);
703 rss += slot->savestack_max * sizeof (ANY);
704
705#if !PERL_VERSION_ATLEAST (5,10,0)
706 rss += slot->retstack_max * sizeof (OP *);
707#endif
708 } 728 }
709 } 729 }
710 730
711 return rss; 731 return rss;
712} 732}
814slf_check_nop (pTHX_ struct CoroSLF *frame) 834slf_check_nop (pTHX_ struct CoroSLF *frame)
815{ 835{
816 return 0; 836 return 0;
817} 837}
818 838
839static UNOP coro_setup_op;
840
819static void NOINLINE /* noinline to keep it out of the transfer fast path */ 841static void NOINLINE /* noinline to keep it out of the transfer fast path */
820coro_setup (pTHX_ struct coro *coro) 842coro_setup (pTHX_ struct coro *coro)
821{ 843{
822 /* 844 /*
823 * emulate part of the perl startup here. 845 * emulate part of the perl startup here.
851 { 873 {
852 dSP; 874 dSP;
853 UNOP myop; 875 UNOP myop;
854 876
855 Zero (&myop, 1, UNOP); 877 Zero (&myop, 1, UNOP);
856 myop.op_next = Nullop; 878 myop.op_next = Nullop;
879 myop.op_type = OP_ENTERSUB;
857 myop.op_flags = OPf_WANT_VOID; 880 myop.op_flags = OPf_WANT_VOID;
858 881
859 PUSHMARK (SP); 882 PUSHMARK (SP);
860 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 883 PUSHs ((SV *)coro->startcv);
861 PUTBACK; 884 PUTBACK;
862 PL_op = (OP *)&myop; 885 PL_op = (OP *)&myop;
863 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 886 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
864 SPAGAIN;
865 } 887 }
866 888
867 /* 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
868 * 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.
869 */ 891 */
870 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 892 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
871 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ 893 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
872 894
873 coro_throw = coro->throw; 895 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */
896 coro_setup_op.op_next = PL_op;
897 coro_setup_op.op_type = OP_CUSTOM;
898 coro_setup_op.op_ppaddr = pp_slf;
899 /* no flags etc. required, as an init function won't be called */
900
901 PL_op = (OP *)&coro_setup_op;
902
903 /* copy throw, in case it was set before coro_setup */
904 CORO_THROW = coro->except;
874} 905}
875 906
876static void 907static void
877coro_destruct (pTHX_ struct coro *coro) 908coro_destruct (pTHX_ struct coro *coro)
878{ 909{
902 933
903 SvREFCNT_dec (PL_diehook); 934 SvREFCNT_dec (PL_diehook);
904 SvREFCNT_dec (PL_warnhook); 935 SvREFCNT_dec (PL_warnhook);
905 936
906 SvREFCNT_dec (coro->saved_deffh); 937 SvREFCNT_dec (coro->saved_deffh);
907 SvREFCNT_dec (coro_throw); 938 SvREFCNT_dec (coro->rouse_cb);
939 SvREFCNT_dec (coro->invoke_cb);
940 SvREFCNT_dec (coro->invoke_av);
908 941
909 coro_destruct_stacks (aTHX); 942 coro_destruct_stacks (aTHX);
910} 943}
911 944
912INLINE void 945INLINE void
999 SAVETMPS; 1032 SAVETMPS;
1000 EXTEND (SP, 3); 1033 EXTEND (SP, 3);
1001 PUSHMARK (SP); 1034 PUSHMARK (SP);
1002 PUSHs (&PL_sv_yes); 1035 PUSHs (&PL_sv_yes);
1003 PUSHs (fullname); 1036 PUSHs (fullname);
1004 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);
1005 PUTBACK; 1038 PUTBACK;
1006 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);
1007 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);
1008 SPAGAIN; 1041 SPAGAIN;
1009 FREETMPS; 1042 FREETMPS;
1041 1074
1042 TAINT_NOT; 1075 TAINT_NOT;
1043 return 0; 1076 return 0;
1044} 1077}
1045 1078
1079static struct coro_cctx *cctx_ssl_cctx;
1080static struct CoroSLF cctx_ssl_frame;
1081
1046static void 1082static void
1047prepare_set_stacklevel (struct coro_transfer_args *ta, struct coro_cctx *cctx) 1083slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1048{ 1084{
1049 ta->prev = (struct coro *)cctx; 1085 ta->prev = (struct coro *)cctx_ssl_cctx;
1050 ta->next = 0; 1086 ta->next = 0;
1051} 1087}
1052 1088
1053/* inject a fake call to Coro::State::_cctx_init into the execution */ 1089static int
1054/* _cctx_init should be careful, as it could be called at almost any time */ 1090slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1055/* during execution of a perl program */ 1091{
1056/* also initialises PL_top_env */ 1092 *frame = cctx_ssl_frame;
1093
1094 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1095}
1096
1097/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1057static void NOINLINE 1098static void NOINLINE
1058cctx_prepare (pTHX_ coro_cctx *cctx) 1099cctx_prepare (pTHX_ coro_cctx *cctx)
1059{ 1100{
1060 dSP;
1061 UNOP myop;
1062
1063 PL_top_env = &PL_start_env; 1101 PL_top_env = &PL_start_env;
1064 1102
1065 if (cctx->flags & CC_TRACE) 1103 if (cctx->flags & CC_TRACE)
1066 PL_runops = runops_trace; 1104 PL_runops = runops_trace;
1067 1105
1068 Zero (&myop, 1, UNOP); 1106 /* we already must be executing an SLF op, there is no other valid way
1069 myop.op_next = PL_op; 1107 * that can lead to creation of a new cctx */
1070 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 1108 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1109 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1071 1110
1072 PUSHMARK (SP); 1111 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */
1073 EXTEND (SP, 2); 1112 cctx_ssl_cctx = cctx;
1074 PUSHs (sv_2mortal (newSViv ((IV)cctx))); 1113 cctx_ssl_frame = slf_frame;
1075 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 1114
1076 PUTBACK; 1115 slf_frame.prepare = slf_prepare_set_stacklevel;
1077 PL_op = (OP *)&myop; 1116 slf_frame.check = slf_check_set_stacklevel;
1078 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1079 SPAGAIN;
1080} 1117}
1081 1118
1082/* the tail of transfer: execute stuff we can only do after a transfer */ 1119/* the tail of transfer: execute stuff we can only do after a transfer */
1083INLINE void 1120INLINE void
1084transfer_tail (pTHX) 1121transfer_tail (pTHX)
1111 transfer_tail (aTHX); 1148 transfer_tail (aTHX);
1112 1149
1113 /* 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 */
1114 PL_restartop = PL_op; 1151 PL_restartop = PL_op;
1115 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 */
1116 1157
1117 /* 1158 /*
1118 * 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
1119 * 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)
1120 * 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
1300 dSTACKLEVEL; 1341 dSTACKLEVEL;
1301 1342
1302 /* sometimes transfer is only called to set idle_sp */ 1343 /* sometimes transfer is only called to set idle_sp */
1303 if (expect_false (!next)) 1344 if (expect_false (!next))
1304 { 1345 {
1305 ((coro_cctx *)prev)->idle_sp = (void *)stacklevel; 1346 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1306 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1347 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1307 } 1348 }
1308 else if (expect_true (prev != next)) 1349 else if (expect_true (prev != next))
1309 { 1350 {
1310 coro_cctx *prev__cctx; 1351 coro_cctx *prev__cctx;
1335 1376
1336 prev__cctx = prev->cctx; 1377 prev__cctx = prev->cctx;
1337 1378
1338 /* possibly untie and reuse the cctx */ 1379 /* possibly untie and reuse the cctx */
1339 if (expect_true ( 1380 if (expect_true (
1340 prev__cctx->idle_sp == (void *)stacklevel 1381 prev__cctx->idle_sp == STACKLEVEL
1341 && !(prev__cctx->flags & CC_TRACE) 1382 && !(prev__cctx->flags & CC_TRACE)
1342 && !force_cctx 1383 && !force_cctx
1343 )) 1384 ))
1344 { 1385 {
1345 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1386 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1412 1453
1413 coro->slot = 0; 1454 coro->slot = 0;
1414 } 1455 }
1415 1456
1416 cctx_destroy (coro->cctx); 1457 cctx_destroy (coro->cctx);
1458 SvREFCNT_dec (coro->startcv);
1417 SvREFCNT_dec (coro->args); 1459 SvREFCNT_dec (coro->args);
1460 SvREFCNT_dec (CORO_THROW);
1418 1461
1419 if (coro->next) coro->next->prev = coro->prev; 1462 if (coro->next) coro->next->prev = coro->prev;
1420 if (coro->prev) coro->prev->next = coro->next; 1463 if (coro->prev) coro->prev->next = coro->next;
1421 if (coro == coro_first) coro_first = coro->next; 1464 if (coro == coro_first) coro_first = coro->next;
1422 1465
1476 1519
1477 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1520 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1478 TRANSFER (ta, 1); 1521 TRANSFER (ta, 1);
1479} 1522}
1480 1523
1524/*****************************************************************************/
1525/* gensub: simple closure generation utility */
1526
1527#define GENSUB_ARG CvXSUBANY (cv).any_ptr
1528
1529/* create a closure from XS, returns a code reference */
1530/* the arg can be accessed via GENSUB_ARG from the callback */
1531/* the callback must use dXSARGS/XSRETURN */
1532static SV *
1533gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg)
1534{
1535 CV *cv = (CV *)newSV (0);
1536
1537 sv_upgrade ((SV *)cv, SVt_PVCV);
1538
1539 CvANON_on (cv);
1540 CvISXSUB_on (cv);
1541 CvXSUB (cv) = xsub;
1542 GENSUB_ARG = arg;
1543
1544 return newRV_noinc ((SV *)cv);
1545}
1546
1481/** Coro ********************************************************************/ 1547/** Coro ********************************************************************/
1482 1548
1483INLINE void 1549INLINE void
1484coro_enq (pTHX_ struct coro *coro) 1550coro_enq (pTHX_ struct coro *coro)
1485{ 1551{
1528 ENTER; 1594 ENTER;
1529 SAVETMPS; 1595 SAVETMPS;
1530 1596
1531 PUSHMARK (SP); 1597 PUSHMARK (SP);
1532 PUTBACK; 1598 PUTBACK;
1533 call_sv (sv_hook, G_DISCARD); 1599 call_sv (sv_hook, G_VOID | G_DISCARD);
1534 SPAGAIN;
1535 1600
1536 FREETMPS; 1601 FREETMPS;
1537 LEAVE; 1602 LEAVE;
1538 } 1603 }
1539 1604
1566 ENTER; 1631 ENTER;
1567 SAVETMPS; 1632 SAVETMPS;
1568 1633
1569 PUSHMARK (SP); 1634 PUSHMARK (SP);
1570 PUTBACK; 1635 PUTBACK;
1571 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); 1636 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD);
1572 SPAGAIN;
1573 1637
1574 FREETMPS; 1638 FREETMPS;
1575 LEAVE; 1639 LEAVE;
1576 continue; 1640 continue;
1577 } 1641 }
1687 coro->slot->runops = RUNOPS_DEFAULT; 1751 coro->slot->runops = RUNOPS_DEFAULT;
1688 } 1752 }
1689} 1753}
1690 1754
1691/*****************************************************************************/ 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/*****************************************************************************/
1832/* rouse callback */
1833
1834#define CORO_MAGIC_type_rouse PERL_MAGIC_ext
1835
1836static void
1837coro_rouse_callback (pTHX_ CV *cv)
1838{
1839 dXSARGS;
1840 SV *data = (SV *)GENSUB_ARG;
1841
1842 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1843 {
1844 /* first call, set args */
1845 AV *av = newAV ();
1846 SV *coro = SvRV (data);
1847
1848 SvRV_set (data, (SV *)av);
1849 api_ready (aTHX_ coro);
1850 SvREFCNT_dec (coro);
1851
1852 /* better take a full copy of the arguments */
1853 while (items--)
1854 av_store (av, items, newSVsv (ST (items)));
1855 }
1856
1857 XSRETURN_EMPTY;
1858}
1859
1860static int
1861slf_check_rouse_wait (pTHX_ struct CoroSLF *frame)
1862{
1863 SV *data = (SV *)frame->data;
1864
1865 if (CORO_THROW)
1866 return 0;
1867
1868 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1869 return 1;
1870
1871 /* now push all results on the stack */
1872 {
1873 dSP;
1874 AV *av = (AV *)SvRV (data);
1875 int i;
1876
1877 EXTEND (SP, AvFILLp (av) + 1);
1878 for (i = 0; i <= AvFILLp (av); ++i)
1879 PUSHs (sv_2mortal (AvARRAY (av)[i]));
1880
1881 /* we have stolen the elements, so ste length to zero and free */
1882 AvFILLp (av) = -1;
1883 av_undef (av);
1884
1885 PUTBACK;
1886 }
1887
1888 return 0;
1889}
1890
1891static void
1892slf_init_rouse_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1893{
1894 SV *cb;
1895
1896 if (items)
1897 cb = arg [0];
1898 else
1899 {
1900 struct coro *coro = SvSTATE_current;
1901
1902 if (!coro->rouse_cb)
1903 croak ("Coro::rouse_wait called without rouse callback, and no default rouse callback found either,");
1904
1905 cb = sv_2mortal (coro->rouse_cb);
1906 coro->rouse_cb = 0;
1907 }
1908
1909 if (!SvROK (cb)
1910 || SvTYPE (SvRV (cb)) != SVt_PVCV
1911 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback)
1912 croak ("Coro::rouse_wait called with illegal callback argument,");
1913
1914 {
1915 CV *cv = (CV *)SvRV (cb); /* for GENSUB_ARG */
1916 SV *data = (SV *)GENSUB_ARG;
1917
1918 frame->data = (void *)data;
1919 frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule;
1920 frame->check = slf_check_rouse_wait;
1921 }
1922}
1923
1924static SV *
1925coro_new_rouse_cb (pTHX)
1926{
1927 HV *hv = (HV *)SvRV (coro_current);
1928 struct coro *coro = SvSTATE_hv (hv);
1929 SV *data = newRV_inc ((SV *)hv);
1930 SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data);
1931
1932 sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0);
1933 SvREFCNT_dec (data); /* magicext increases the refcount */
1934
1935 SvREFCNT_dec (coro->rouse_cb);
1936 coro->rouse_cb = SvREFCNT_inc_NN (cb);
1937
1938 return cb;
1939}
1940
1941/*****************************************************************************/
1692/* schedule-like-function opcode (SLF) */ 1942/* schedule-like-function opcode (SLF) */
1693 1943
1694static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */ 1944static UNOP slf_restore; /* restore stack as entersub did, for first-re-run */
1695static const CV *slf_cv; 1945static const CV *slf_cv;
1696static SV **slf_argv; 1946static SV **slf_argv;
1717 1967
1718 RETURNOP (slf_restore.op_first); 1968 RETURNOP (slf_restore.op_first);
1719} 1969}
1720 1970
1721static void 1971static void
1722slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1723{
1724 prepare_set_stacklevel (ta, (struct coro_cctx *)slf_frame.data);
1725}
1726
1727static void
1728slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1729{
1730 assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1));
1731
1732 frame->prepare = slf_prepare_set_stacklevel;
1733 frame->check = slf_check_nop;
1734 frame->data = (void *)SvIV (arg [0]);
1735}
1736
1737static void
1738slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta) 1972slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta)
1739{ 1973{
1740 SV **arg = (SV **)slf_frame.data; 1974 SV **arg = (SV **)slf_frame.data;
1741 1975
1742 prepare_transfer (aTHX_ ta, arg [0], arg [1]); 1976 prepare_transfer (aTHX_ ta, arg [0], arg [1]);
1771slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2005slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1772{ 2006{
1773 frame->prepare = prepare_cede_notself; 2007 frame->prepare = prepare_cede_notself;
1774 frame->check = slf_check_nop; 2008 frame->check = slf_check_nop;
1775} 2009}
1776
1777/* we hijack an hopefully unused CV flag for our purposes */
1778#define CVf_SLF 0x4000
1779 2010
1780/* 2011/*
1781 * these not obviously related functions are all rolled into one 2012 * these not obviously related functions are all rolled into one
1782 * function to increase chances that they all will call transfer with the same 2013 * function to increase chances that they all will call transfer with the same
1783 * stack offset 2014 * stack offset
1839 } 2070 }
1840 while (slf_frame.check (aTHX_ &slf_frame)); 2071 while (slf_frame.check (aTHX_ &slf_frame));
1841 2072
1842 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ 2073 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */
1843 2074
1844 /* return value handling - mostly like entersub */
1845 {
1846 dSP;
1847 SV **bot = PL_stack_base + checkmark;
1848 int gimme = GIMME_V;
1849
1850 /* make sure we put something on the stack in scalar context */
1851 if (gimme == G_SCALAR)
1852 {
1853 if (sp == bot)
1854 XPUSHs (&PL_sv_undef);
1855
1856 SP = bot + 1;
1857 }
1858
1859 PUTBACK;
1860 }
1861
1862 /* exception handling */ 2075 /* exception handling */
1863 if (expect_false (coro_throw)) 2076 if (expect_false (CORO_THROW))
1864 { 2077 {
1865 SV *exception = sv_2mortal (coro_throw); 2078 SV *exception = sv_2mortal (CORO_THROW);
1866 2079
1867 coro_throw = 0; 2080 CORO_THROW = 0;
1868 sv_setsv (ERRSV, exception); 2081 sv_setsv (ERRSV, exception);
1869 croak (0); 2082 croak (0);
2083 }
2084
2085 /* return value handling - mostly like entersub */
2086 /* make sure we put something on the stack in scalar context */
2087 if (GIMME_V == G_SCALAR)
2088 {
2089 dSP;
2090 SV **bot = PL_stack_base + checkmark;
2091
2092 if (sp == bot) /* too few, push undef */
2093 bot [1] = &PL_sv_undef;
2094 else if (sp != bot + 1) /* too many, take last one */
2095 bot [1] = *sp;
2096
2097 SP = bot + 1;
2098
2099 PUTBACK;
1870 } 2100 }
1871 2101
1872 return NORMAL; 2102 return NORMAL;
1873} 2103}
1874 2104
1996 PerlIOBuf_get_cnt, 2226 PerlIOBuf_get_cnt,
1997 PerlIOBuf_set_ptrcnt, 2227 PerlIOBuf_set_ptrcnt,
1998}; 2228};
1999 2229
2000/*****************************************************************************/ 2230/*****************************************************************************/
2231/* Coro::Semaphore & Coro::Signal */
2232
2233static SV *
2234coro_waitarray_new (pTHX_ int count)
2235{
2236 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */
2237 AV *av = newAV ();
2238 SV **ary;
2239
2240 /* unfortunately, building manually saves memory */
2241 Newx (ary, 2, SV *);
2242 AvALLOC (av) = ary;
2243 /*AvARRAY (av) = ary;*/
2244 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */
2245 AvMAX (av) = 1;
2246 AvFILLp (av) = 0;
2247 ary [0] = newSViv (count);
2248
2249 return newRV_noinc ((SV *)av);
2250}
2251
2001/* Coro::Semaphore */ 2252/* semaphore */
2002 2253
2003static void 2254static void
2004coro_semaphore_adjust (pTHX_ AV *av, IV adjust) 2255coro_semaphore_adjust (pTHX_ AV *av, IV adjust)
2005{ 2256{
2006 SV *count_sv = AvARRAY (av)[0]; 2257 SV *count_sv = AvARRAY (av)[0];
2018 AvARRAY (av)[0] = AvARRAY (av)[1]; 2269 AvARRAY (av)[0] = AvARRAY (av)[1];
2019 AvARRAY (av)[1] = count_sv; 2270 AvARRAY (av)[1] = count_sv;
2020 cb = av_shift (av); 2271 cb = av_shift (av);
2021 2272
2022 if (SvOBJECT (cb)) 2273 if (SvOBJECT (cb))
2274 {
2023 api_ready (aTHX_ cb); 2275 api_ready (aTHX_ cb);
2024 else 2276 --count;
2025 croak ("callbacks not yet supported"); 2277 }
2278 else if (SvTYPE (cb) == SVt_PVCV)
2279 {
2280 dSP;
2281 PUSHMARK (SP);
2282 XPUSHs (sv_2mortal (newRV_inc ((SV *)av)));
2283 PUTBACK;
2284 call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR);
2285 }
2026 2286
2027 SvREFCNT_dec (cb); 2287 SvREFCNT_dec (cb);
2028
2029 --count;
2030 } 2288 }
2031} 2289}
2032 2290
2033static void 2291static void
2034coro_semaphore_on_destroy (pTHX_ struct coro *coro) 2292coro_semaphore_on_destroy (pTHX_ struct coro *coro)
2036 /* call $sem->adjust (0) to possibly wake up some other waiters */ 2294 /* call $sem->adjust (0) to possibly wake up some other waiters */
2037 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); 2295 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0);
2038} 2296}
2039 2297
2040static int 2298static int
2041slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) 2299slf_check_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, int acquire)
2042{ 2300{
2043 AV *av = (AV *)frame->data; 2301 AV *av = (AV *)frame->data;
2044 SV *count_sv = AvARRAY (av)[0]; 2302 SV *count_sv = AvARRAY (av)[0];
2045 2303
2304 /* if we are about to throw, don't actually acquire the lock, just throw */
2305 if (CORO_THROW)
2306 return 0;
2046 if (SvIVX (count_sv) > 0) 2307 else if (SvIVX (count_sv) > 0)
2047 { 2308 {
2048 SvSTATE_current->on_destroy = 0; 2309 SvSTATE_current->on_destroy = 0;
2310
2311 if (acquire)
2049 SvIVX (count_sv) = SvIVX (count_sv) - 1; 2312 SvIVX (count_sv) = SvIVX (count_sv) - 1;
2313 else
2314 coro_semaphore_adjust (aTHX_ av, 0);
2315
2050 return 0; 2316 return 0;
2051 } 2317 }
2052 else 2318 else
2053 { 2319 {
2054 int i; 2320 int i;
2063 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2329 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2064 return 1; 2330 return 1;
2065 } 2331 }
2066} 2332}
2067 2333
2068static void 2334static int
2335slf_check_semaphore_down (pTHX_ struct CoroSLF *frame)
2336{
2337 return slf_check_semaphore_down_or_wait (aTHX_ frame, 1);
2338}
2339
2340static int
2341slf_check_semaphore_wait (pTHX_ struct CoroSLF *frame)
2342{
2343 return slf_check_semaphore_down_or_wait (aTHX_ frame, 0);
2344}
2345
2346static void
2069slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2347slf_init_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2070{ 2348{
2071 AV *av = (AV *)SvRV (arg [0]); 2349 AV *av = (AV *)SvRV (arg [0]);
2072 2350
2073 if (SvIVX (AvARRAY (av)[0]) > 0) 2351 if (SvIVX (AvARRAY (av)[0]) > 0)
2074 { 2352 {
2075 frame->data = (void *)av; 2353 frame->data = (void *)av;
2076 frame->prepare = prepare_nop; 2354 frame->prepare = prepare_nop;
2077 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2078 } 2355 }
2079 else 2356 else
2080 { 2357 {
2081 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2358 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2082 2359
2083 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); 2360 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av));
2084 frame->prepare = prepare_schedule; 2361 frame->prepare = prepare_schedule;
2085 2362
2086 /* to avoid race conditions when a woken-up coro gets terminated */ 2363 /* to avoid race conditions when a woken-up coro gets terminated */
2087 /* we arrange for a temporary on_destroy that calls adjust (0) */ 2364 /* we arrange for a temporary on_destroy that calls adjust (0) */
2088 assert (!SvSTATE_current->on_destroy);//D
2089 SvSTATE_current->on_destroy = coro_semaphore_on_destroy; 2365 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2090 } 2366 }
2367}
2091 2368
2369static void
2370slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2371{
2372 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items);
2092 frame->check = slf_check_semaphore_down; 2373 frame->check = slf_check_semaphore_down;
2093
2094} 2374}
2095 2375
2096/*****************************************************************************/ 2376static void
2097/* gensub: simple closure generation utility */ 2377slf_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{ 2378{
2107 CV *cv = (CV *)newSV (0); 2379 if (items >= 2)
2380 {
2381 /* callback form */
2382 AV *av = (AV *)SvRV (arg [0]);
2383 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2108 2384
2109 sv_upgrade ((SV *)cv, SVt_PVCV); 2385 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv));
2110 2386
2111 CvANON_on (cv); 2387 if (SvIVX (AvARRAY (av)[0]) > 0)
2112 CvISXSUB_on (cv); 2388 coro_semaphore_adjust (aTHX_ av, 0);
2113 CvXSUB (cv) = xsub;
2114 GENSUB_ARG = arg;
2115 2389
2116 return newRV_noinc ((SV *)cv); 2390 frame->prepare = prepare_nop;
2391 frame->check = slf_check_nop;
2392 }
2393 else
2394 {
2395 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items);
2396 frame->check = slf_check_semaphore_wait;
2397 }
2398}
2399
2400/* signal */
2401
2402static void
2403coro_signal_wake (pTHX_ AV *av, int count)
2404{
2405 SvIVX (AvARRAY (av)[0]) = 0;
2406
2407 /* now signal count waiters */
2408 while (count > 0 && AvFILLp (av) > 0)
2409 {
2410 SV *cb;
2411
2412 /* swap first two elements so we can shift a waiter */
2413 cb = AvARRAY (av)[0];
2414 AvARRAY (av)[0] = AvARRAY (av)[1];
2415 AvARRAY (av)[1] = cb;
2416
2417 cb = av_shift (av);
2418
2419 api_ready (aTHX_ cb);
2420 sv_setiv (cb, 0); /* signal waiter */
2421 SvREFCNT_dec (cb);
2422
2423 --count;
2424 }
2425}
2426
2427static int
2428slf_check_signal_wait (pTHX_ struct CoroSLF *frame)
2429{
2430 /* if we are about to throw, also stop waiting */
2431 return SvROK ((SV *)frame->data) && !CORO_THROW;
2432}
2433
2434static void
2435slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2436{
2437 AV *av = (AV *)SvRV (arg [0]);
2438
2439 if (SvIVX (AvARRAY (av)[0]))
2440 {
2441 SvIVX (AvARRAY (av)[0]) = 0;
2442 frame->prepare = prepare_nop;
2443 frame->check = slf_check_nop;
2444 }
2445 else
2446 {
2447 SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */
2448
2449 av_push (av, waiter);
2450
2451 frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */
2452 frame->prepare = prepare_schedule;
2453 frame->check = slf_check_signal_wait;
2454 }
2117} 2455}
2118 2456
2119/*****************************************************************************/ 2457/*****************************************************************************/
2120/* Coro::AIO */ 2458/* Coro::AIO */
2121 2459
2136 dXSARGS; 2474 dXSARGS;
2137 AV *state = (AV *)GENSUB_ARG; 2475 AV *state = (AV *)GENSUB_ARG;
2138 SV *coro = av_pop (state); 2476 SV *coro = av_pop (state);
2139 SV *data_sv = newSV (sizeof (struct io_state)); 2477 SV *data_sv = newSV (sizeof (struct io_state));
2140 2478
2141 av_extend (state, items); 2479 av_extend (state, items - 1);
2142 2480
2143 sv_upgrade (data_sv, SVt_PV); 2481 sv_upgrade (data_sv, SVt_PV);
2144 SvCUR_set (data_sv, sizeof (struct io_state)); 2482 SvCUR_set (data_sv, sizeof (struct io_state));
2145 SvPOK_only (data_sv); 2483 SvPOK_only (data_sv);
2146 2484
2170 2508
2171static int 2509static int
2172slf_check_aio_req (pTHX_ struct CoroSLF *frame) 2510slf_check_aio_req (pTHX_ struct CoroSLF *frame)
2173{ 2511{
2174 AV *state = (AV *)frame->data; 2512 AV *state = (AV *)frame->data;
2513
2514 /* if we are about to throw, return early */
2515 /* this does not cancel the aio request, but at least */
2516 /* it quickly returns */
2517 if (CORO_THROW)
2518 return 0;
2175 2519
2176 /* one element that is an RV? repeat! */ 2520 /* one element that is an RV? repeat! */
2177 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) 2521 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0]))
2178 return 1; 2522 return 1;
2179 2523
2350 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 2694 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
2351} 2695}
2352 2696
2353SV * 2697SV *
2354new (char *klass, ...) 2698new (char *klass, ...)
2699 ALIAS:
2700 Coro::new = 1
2355 CODE: 2701 CODE:
2356{ 2702{
2357 struct coro *coro; 2703 struct coro *coro;
2358 MAGIC *mg; 2704 MAGIC *mg;
2359 HV *hv; 2705 HV *hv;
2706 CV *cb;
2360 int i; 2707 int i;
2708
2709 if (items > 1)
2710 {
2711 cb = coro_sv_2cv (aTHX_ ST (1));
2712
2713 if (!ix)
2714 {
2715 if (CvISXSUB (cb))
2716 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
2717
2718 if (!CvROOT (cb))
2719 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
2720 }
2721 }
2361 2722
2362 Newz (0, coro, 1, struct coro); 2723 Newz (0, coro, 1, struct coro);
2363 coro->args = newAV (); 2724 coro->args = newAV ();
2364 coro->flags = CF_NEW; 2725 coro->flags = CF_NEW;
2365 2726
2370 coro->hv = hv = newHV (); 2731 coro->hv = hv = newHV ();
2371 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); 2732 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
2372 mg->mg_flags |= MGf_DUP; 2733 mg->mg_flags |= MGf_DUP;
2373 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 2734 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2374 2735
2736 if (items > 1)
2737 {
2375 av_extend (coro->args, items - 1); 2738 av_extend (coro->args, items - 1 + ix - 1);
2739
2740 if (ix)
2741 {
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
2376 for (i = 1; i < items; i++) 2748 for (i = 2; i < items; i++)
2377 av_push (coro->args, newSVsv (ST (i))); 2749 av_push (coro->args, newSVsv (ST (i)));
2750 }
2378} 2751}
2379 OUTPUT: 2752 OUTPUT:
2380 RETVAL 2753 RETVAL
2381
2382void
2383_set_stacklevel (...)
2384 CODE:
2385 CORO_EXECUTE_SLF_XS (slf_init_set_stacklevel);
2386 2754
2387void 2755void
2388transfer (...) 2756transfer (...)
2389 PROTOTYPE: $$ 2757 PROTOTYPE: $$
2390 CODE: 2758 CODE:
2516throw (Coro::State self, SV *throw = &PL_sv_undef) 2884throw (Coro::State self, SV *throw = &PL_sv_undef)
2517 PROTOTYPE: $;$ 2885 PROTOTYPE: $;$
2518 CODE: 2886 CODE:
2519{ 2887{
2520 struct coro *current = SvSTATE_current; 2888 struct coro *current = SvSTATE_current;
2521 SV **throwp = self == current ? &coro_throw : &self->throw; 2889 SV **throwp = self == current ? &CORO_THROW : &self->except;
2522 SvREFCNT_dec (*throwp); 2890 SvREFCNT_dec (*throwp);
2523 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 2891 *throwp = SvOK (throw) ? newSVsv (throw) : 0;
2524} 2892}
2525 2893
2526void 2894void
2585 2953
2586BOOT: 2954BOOT:
2587{ 2955{
2588 int i; 2956 int i;
2589 2957
2590 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 2958 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2591 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 2959 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2592 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 2960 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2593 2961 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD);
2962 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
2594 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); 2963 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
2595 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);
2596 2969
2597 coro_stash = gv_stashpv ("Coro", TRUE); 2970 coro_stash = gv_stashpv ("Coro", TRUE);
2598 2971
2599 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 2972 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2600 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 2973 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2615 coroapi.ready = api_ready; 2988 coroapi.ready = api_ready;
2616 coroapi.is_ready = api_is_ready; 2989 coroapi.is_ready = api_is_ready;
2617 coroapi.nready = coro_nready; 2990 coroapi.nready = coro_nready;
2618 coroapi.current = coro_current; 2991 coroapi.current = coro_current;
2619 2992
2620 GCoroAPI = &coroapi; 2993 /*GCoroAPI = &coroapi;*/
2621 sv_setiv (sv, (IV)&coroapi); 2994 sv_setiv (sv, (IV)&coroapi);
2622 SvREADONLY_on (sv); 2995 SvREADONLY_on (sv);
2623 } 2996 }
2624} 2997}
2625 2998
2689 CODE: 3062 CODE:
2690 RETVAL = coro_nready; 3063 RETVAL = coro_nready;
2691 OUTPUT: 3064 OUTPUT:
2692 RETVAL 3065 RETVAL
2693 3066
2694# for async_pool speedup
2695void 3067void
2696_pool_1 (SV *cb) 3068_pool_handler (...)
2697 CODE: 3069 CODE:
2698{ 3070 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
2699 HV *hv = (HV *)SvRV (coro_current);
2700 struct coro *coro = SvSTATE_hv ((SV *)hv);
2701 AV *defav = GvAV (PL_defgv);
2702 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
2703 AV *invoke_av;
2704 int i, len;
2705 3071
2706 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)
2707 { 3087 {
2708 SV *old = PL_diehook; 3088 PUSHMARK (SP);
2709 PL_diehook = 0; 3089 EXTEND (SP, 2);
2710 SvREFCNT_dec (old); 3090 PUSHs (sv_Coro);
2711 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));
2712 } 3097 }
2713 3098
2714 SvREFCNT_dec (coro->saved_deffh);
2715 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2716
2717 hv_store (hv, "desc", sizeof ("desc") - 1,
2718 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2719
2720 invoke_av = (AV *)SvRV (invoke);
2721 len = av_len (invoke_av);
2722
2723 sv_setsv (cb, AvARRAY (invoke_av)[0]);
2724
2725 if (len > 0)
2726 { 3099 {
2727 av_fill (defav, len - 1); 3100 struct coro *coro = SvSTATE_hv (hv);
2728 for (i = 0; i < len; ++i) 3101
2729 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;
2730 } 3106 }
3107
3108 api_ready (aTHX_ (SV *)hv);
3109
3110 if (GIMME_V != G_VOID)
3111 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
3112 else
3113 SvREFCNT_dec (hv);
2731} 3114}
3115
3116SV *
3117rouse_cb ()
3118 PROTOTYPE:
3119 CODE:
3120 RETVAL = coro_new_rouse_cb (aTHX);
3121 OUTPUT:
3122 RETVAL
2732 3123
2733void 3124void
2734_pool_2 (SV *cb) 3125rouse_wait (...)
3126 PROTOTYPE: ;$
2735 CODE: 3127 PPCODE:
2736{ 3128 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
2737 HV *hv = (HV *)SvRV (coro_current);
2738 struct coro *coro = SvSTATE_hv ((SV *)hv);
2739
2740 sv_setsv (cb, &PL_sv_undef);
2741
2742 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2743 coro->saved_deffh = 0;
2744
2745 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2746 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2747 {
2748 SV *old = PL_diehook;
2749 PL_diehook = 0;
2750 SvREFCNT_dec (old);
2751 croak ("\3async_pool terminate\2\n");
2752 }
2753
2754 av_clear (GvAV (PL_defgv));
2755 hv_store (hv, "desc", sizeof ("desc") - 1,
2756 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
2757
2758 coro->prio = 0;
2759
2760 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
2761 api_trace (aTHX_ coro_current, 0);
2762
2763 av_push (av_async_pool, newSVsv (coro_current));
2764}
2765 3129
2766 3130
2767MODULE = Coro::State PACKAGE = PerlIO::cede 3131MODULE = Coro::State PACKAGE = PerlIO::cede
2768 3132
2769BOOT: 3133BOOT:
2771 3135
2772 3136
2773MODULE = Coro::State PACKAGE = Coro::Semaphore 3137MODULE = Coro::State PACKAGE = Coro::Semaphore
2774 3138
2775SV * 3139SV *
2776new (SV *klass, SV *count_ = 0) 3140new (SV *klass, SV *count = 0)
2777 CODE: 3141 CODE:
2778{ 3142 RETVAL = sv_bless (
2779 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ 3143 coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1),
2780 AV *av = newAV (); 3144 GvSTASH (CvGV (cv))
2781 av_push (av, newSViv (count_ && SvOK (count_) ? SvIV (count_) : 1)); 3145 );
2782 RETVAL = sv_bless (newRV_noinc ((SV *)av), GvSTASH (CvGV (cv))); 3146 OUTPUT:
2783} 3147 RETVAL
3148
3149# helper for Coro::Channel
3150SV *
3151_alloc (int count)
3152 CODE:
3153 RETVAL = coro_waitarray_new (aTHX_ count);
2784 OUTPUT: 3154 OUTPUT:
2785 RETVAL 3155 RETVAL
2786 3156
2787SV * 3157SV *
2788count (SV *self) 3158count (SV *self)
2797 adjust = 1 3167 adjust = 1
2798 CODE: 3168 CODE:
2799 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); 3169 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1);
2800 3170
2801void 3171void
2802down (SV *self) 3172down (...)
2803 CODE: 3173 CODE:
2804 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); 3174 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down);
3175
3176void
3177wait (...)
3178 CODE:
3179 CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait);
2805 3180
2806void 3181void
2807try (SV *self) 3182try (SV *self)
2808 PPCODE: 3183 PPCODE:
2809{ 3184{
2821 XSRETURN_NO; 3196 XSRETURN_NO;
2822} 3197}
2823 3198
2824void 3199void
2825waiters (SV *self) 3200waiters (SV *self)
2826 CODE: 3201 PPCODE:
2827{ 3202{
2828 AV *av = (AV *)SvRV (self); 3203 AV *av = (AV *)SvRV (self);
3204 int wcount = AvFILLp (av) + 1 - 1;
2829 3205
2830 if (GIMME_V == G_SCALAR) 3206 if (GIMME_V == G_SCALAR)
2831 XPUSHs (sv_2mortal (newSVsv (AvARRAY (av)[0]))); 3207 XPUSHs (sv_2mortal (newSViv (wcount)));
2832 else 3208 else
2833 { 3209 {
2834 int i; 3210 int i;
2835 EXTEND (SP, AvFILLp (av) + 1 - 1); 3211 EXTEND (SP, wcount);
2836 for (i = 1; i <= AvFILLp (av); ++i) 3212 for (i = 1; i <= wcount; ++i)
2837 PUSHs (newSVsv (AvARRAY (av)[i])); 3213 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
2838 } 3214 }
2839} 3215}
3216
3217MODULE = Coro::State PACKAGE = Coro::Signal
3218
3219SV *
3220new (SV *klass)
3221 CODE:
3222 RETVAL = sv_bless (
3223 coro_waitarray_new (aTHX_ 0),
3224 GvSTASH (CvGV (cv))
3225 );
3226 OUTPUT:
3227 RETVAL
3228
3229void
3230wait (...)
3231 CODE:
3232 CORO_EXECUTE_SLF_XS (slf_init_signal_wait);
3233
3234void
3235broadcast (SV *self)
3236 CODE:
3237{
3238 AV *av = (AV *)SvRV (self);
3239 coro_signal_wake (aTHX_ av, AvFILLp (av));
3240}
3241
3242void
3243send (SV *self)
3244 CODE:
3245{
3246 AV *av = (AV *)SvRV (self);
3247
3248 if (AvFILLp (av))
3249 coro_signal_wake (aTHX_ av, 1);
3250 else
3251 SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */
3252}
3253
3254IV
3255awaited (SV *self)
3256 CODE:
3257 RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1;
3258 OUTPUT:
3259 RETVAL
2840 3260
2841 3261
2842MODULE = Coro::State PACKAGE = Coro::AnyEvent 3262MODULE = Coro::State PACKAGE = Coro::AnyEvent
2843 3263
2844BOOT: 3264BOOT:
2859 sv_setsv (sv_activity, &PL_sv_undef); 3279 sv_setsv (sv_activity, &PL_sv_undef);
2860 if (coro_nready >= incede) 3280 if (coro_nready >= incede)
2861 { 3281 {
2862 PUSHMARK (SP); 3282 PUSHMARK (SP);
2863 PUTBACK; 3283 PUTBACK;
2864 call_pv ("Coro::AnyEvent::_activity", G_DISCARD | G_EVAL); 3284 call_pv ("Coro::AnyEvent::_activity", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2865 SPAGAIN;
2866 } 3285 }
2867 3286
2868 --incede; 3287 --incede;
2869} 3288}
2870 3289
2873 3292
2874void 3293void
2875_register (char *target, char *proto, SV *req) 3294_register (char *target, char *proto, SV *req)
2876 CODE: 3295 CODE:
2877{ 3296{
2878 HV *st; 3297 CV *req_cv = coro_sv_2cv (aTHX_ req);
2879 GV *gvp;
2880 CV *req_cv = sv_2cv (req, &st, &gvp, 0);
2881 /* newXSproto doesn't return the CV on 5.8 */ 3298 /* newXSproto doesn't return the CV on 5.8 */
2882 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); 3299 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
2883 sv_setpv ((SV *)slf_cv, proto); 3300 sv_setpv ((SV *)slf_cv, proto);
2884 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);
2885} 3302}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines