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.308 by root, Wed Nov 19 15:09:57 2008 UTC vs.
Revision 1.327 by root, Mon Nov 24 06:07:16 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
106#ifndef Newx
107# define Newx(ptr,nitems,type) New (0,ptr,nitems,type)
108#endif
103 109
104/* 5.8.7 */ 110/* 5.8.7 */
105#ifndef SvRV_set 111#ifndef SvRV_set
106# define SvRV_set(s,v) SvRV(s) = (v) 112# define SvRV_set(s,v) SvRV(s) = (v)
107#endif 113#endif
167static AV *main_mainstack; /* used to differentiate between $main and others */ 173static AV *main_mainstack; /* used to differentiate between $main and others */
168static JMPENV *main_top_env; 174static JMPENV *main_top_env;
169static HV *coro_state_stash, *coro_stash; 175static HV *coro_state_stash, *coro_stash;
170static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 176static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
171 177
178static AV *av_destroy; /* destruction queue */
179static SV *sv_manager; /* the manager coro */
180static SV *sv_idle; /* $Coro::idle */
181
172static GV *irsgv; /* $/ */ 182static GV *irsgv; /* $/ */
173static GV *stdoutgv; /* *STDOUT */ 183static GV *stdoutgv; /* *STDOUT */
174static SV *rv_diehook; 184static SV *rv_diehook;
175static SV *rv_warnhook; 185static SV *rv_warnhook;
176static HV *hv_sig; /* %SIG */ 186static HV *hv_sig; /* %SIG */
177 187
178/* async_pool helper stuff */ 188/* async_pool helper stuff */
179static SV *sv_pool_rss; 189static SV *sv_pool_rss;
180static SV *sv_pool_size; 190static SV *sv_pool_size;
191static SV *sv_async_pool_idle; /* description string */
181static AV *av_async_pool; 192static AV *av_async_pool; /* idle pool */
193static SV *sv_Coro; /* class string */
194static CV *cv_pool_handler;
195static CV *cv_coro_state_new;
182 196
183/* Coro::AnyEvent */ 197/* Coro::AnyEvent */
184static SV *sv_activity; 198static SV *sv_activity;
185 199
186static struct coro_cctx *cctx_first; 200static struct coro_cctx *cctx_first;
215 int valgrind_id; 229 int valgrind_id;
216#endif 230#endif
217 unsigned char flags; 231 unsigned char flags;
218} coro_cctx; 232} coro_cctx;
219 233
234coro_cctx *cctx_current; /* the currently running cctx */
235
236/*****************************************************************************/
237
220enum { 238enum {
221 CF_RUNNING = 0x0001, /* coroutine is running */ 239 CF_RUNNING = 0x0001, /* coroutine is running */
222 CF_READY = 0x0002, /* coroutine is ready */ 240 CF_READY = 0x0002, /* coroutine is ready */
223 CF_NEW = 0x0004, /* has never been switched to */ 241 CF_NEW = 0x0004, /* has never been switched to */
224 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 242 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
229{ 247{
230 SV *defsv; 248 SV *defsv;
231 AV *defav; 249 AV *defav;
232 SV *errsv; 250 SV *errsv;
233 SV *irsgv; 251 SV *irsgv;
252 HV *hinthv;
234#define VAR(name,type) type name; 253#define VAR(name,type) type name;
235# include "state.h" 254# include "state.h"
236#undef VAR 255#undef VAR
237} perl_slots; 256} perl_slots;
238 257
263 SV *except; /* exception to be thrown */ 282 SV *except; /* exception to be thrown */
264 SV *rouse_cb; 283 SV *rouse_cb;
265 284
266 /* async_pool */ 285 /* async_pool */
267 SV *saved_deffh; 286 SV *saved_deffh;
287 SV *invoke_cb;
288 AV *invoke_av;
268 289
269 /* linked list */ 290 /* linked list */
270 struct coro *next, *prev; 291 struct coro *next, *prev;
271}; 292};
272 293
289 310
290/* for Coro.pm */ 311/* for Coro.pm */
291static SV *coro_current; 312static SV *coro_current;
292static SV *coro_readyhook; 313static SV *coro_readyhook;
293static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 314static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
315static CV *cv_coro_run, *cv_coro_terminate;
294static struct coro *coro_first; 316static struct coro *coro_first;
295#define coro_nready coroapi.nready 317#define coro_nready coroapi.nready
296 318
297/** lowlevel stuff **********************************************************/ 319/** lowlevel stuff **********************************************************/
298 320
326 return get_hv (name, create); 348 return get_hv (name, create);
327} 349}
328 350
329/* may croak */ 351/* may croak */
330INLINE CV * 352INLINE CV *
331coro_sv_2cv (SV *sv) 353coro_sv_2cv (pTHX_ SV *sv)
332{ 354{
333 HV *st; 355 HV *st;
334 GV *gvp; 356 GV *gvp;
335 return sv_2cv (sv, &st, &gvp, 0); 357 return sv_2cv (sv, &st, &gvp, 0);
336} 358}
337 359
338static AV * 360static AV *
339coro_clone_padlist (pTHX_ CV *cv) 361coro_derive_padlist (pTHX_ CV *cv)
340{ 362{
341 AV *padlist = CvPADLIST (cv); 363 AV *padlist = CvPADLIST (cv);
342 AV *newpadlist, *newpad; 364 AV *newpadlist, *newpad;
343 365
344 newpadlist = newAV (); 366 newpadlist = newAV ();
464 CV *cp = Perl_cv_clone (aTHX_ cv); 486 CV *cp = Perl_cv_clone (aTHX_ cv);
465 CvPADLIST (cv) = CvPADLIST (cp); 487 CvPADLIST (cv) = CvPADLIST (cp);
466 CvPADLIST (cp) = 0; 488 CvPADLIST (cp) = 0;
467 SvREFCNT_dec (cp); 489 SvREFCNT_dec (cp);
468#else 490#else
469 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 491 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
470#endif 492#endif
471 } 493 }
472} 494}
473 495
474static void 496static void
496 perl_slots *slot = c->slot; 518 perl_slots *slot = c->slot;
497 c->slot = 0; 519 c->slot = 0;
498 520
499 PL_mainstack = c->mainstack; 521 PL_mainstack = c->mainstack;
500 522
501 GvSV (PL_defgv) = slot->defsv; 523 GvSV (PL_defgv) = slot->defsv;
502 GvAV (PL_defgv) = slot->defav; 524 GvAV (PL_defgv) = slot->defav;
503 GvSV (PL_errgv) = slot->errsv; 525 GvSV (PL_errgv) = slot->errsv;
504 GvSV (irsgv) = slot->irsgv; 526 GvSV (irsgv) = slot->irsgv;
527 GvHV (PL_hintgv) = slot->hinthv;
505 528
506 #define VAR(name,type) PL_ ## name = slot->name; 529 #define VAR(name,type) PL_ ## name = slot->name;
507 # include "state.h" 530 # include "state.h"
508 #undef VAR 531 #undef VAR
509 532
595 c->mainstack = PL_mainstack; 618 c->mainstack = PL_mainstack;
596 619
597 { 620 {
598 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 621 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
599 622
600 slot->defav = GvAV (PL_defgv); 623 slot->defav = GvAV (PL_defgv);
601 slot->defsv = DEFSV; 624 slot->defsv = DEFSV;
602 slot->errsv = ERRSV; 625 slot->errsv = ERRSV;
603 slot->irsgv = GvSV (irsgv); 626 slot->irsgv = GvSV (irsgv);
627 slot->hinthv = GvHV (PL_hintgv);
604 628
605 #define VAR(name,type) slot->name = PL_ ## name; 629 #define VAR(name,type) slot->name = PL_ ## name;
606 # include "state.h" 630 # include "state.h"
607 #undef VAR 631 #undef VAR
608 } 632 }
686#if !PERL_VERSION_ATLEAST (5,10,0) 710#if !PERL_VERSION_ATLEAST (5,10,0)
687 Safefree (PL_retstack); 711 Safefree (PL_retstack);
688#endif 712#endif
689} 713}
690 714
715#define CORO_RSS \
716 rss += sizeof (SYM (curstackinfo)); \
717 rss += (SYM (curstackinfo->si_cxmax) + 1) * sizeof (PERL_CONTEXT); \
718 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (SYM (curstack))) * sizeof (SV *); \
719 rss += SYM (tmps_max) * sizeof (SV *); \
720 rss += (SYM (markstack_max) - SYM (markstack_ptr)) * sizeof (I32); \
721 rss += SYM (scopestack_max) * sizeof (I32); \
722 rss += SYM (savestack_max) * sizeof (ANY);
723
691static size_t 724static size_t
692coro_rss (pTHX_ struct coro *coro) 725coro_rss (pTHX_ struct coro *coro)
693{ 726{
694 size_t rss = sizeof (*coro); 727 size_t rss = sizeof (*coro);
695 728
696 if (coro->mainstack) 729 if (coro->mainstack)
697 { 730 {
698 perl_slots tmp_slot;
699 perl_slots *slot;
700
701 if (coro->flags & CF_RUNNING) 731 if (coro->flags & CF_RUNNING)
702 { 732 {
703 slot = &tmp_slot; 733 #define SYM(sym) PL_ ## sym
704 734 CORO_RSS;
705 #define VAR(name,type) slot->name = PL_ ## name;
706 # include "state.h"
707 #undef VAR 735 #undef SYM
708 } 736 }
709 else 737 else
710 slot = coro->slot;
711
712 if (slot)
713 { 738 {
714 rss += sizeof (slot->curstackinfo); 739 #define SYM(sym) coro->slot->sym
715 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 740 CORO_RSS;
716 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); 741 #undef SYM
717 rss += slot->tmps_max * sizeof (SV *);
718 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
719 rss += slot->scopestack_max * sizeof (I32);
720 rss += slot->savestack_max * sizeof (ANY);
721
722#if !PERL_VERSION_ATLEAST (5,10,0)
723 rss += slot->retstack_max * sizeof (OP *);
724#endif
725 } 742 }
726 } 743 }
727 744
728 return rss; 745 return rss;
729} 746}
831slf_check_nop (pTHX_ struct CoroSLF *frame) 848slf_check_nop (pTHX_ struct CoroSLF *frame)
832{ 849{
833 return 0; 850 return 0;
834} 851}
835 852
853static int
854slf_check_repeat (pTHX_ struct CoroSLF *frame)
855{
856 return 1;
857}
858
836static UNOP coro_setup_op; 859static UNOP coro_setup_op;
837 860
838static void NOINLINE /* noinline to keep it out of the transfer fast path */ 861static void NOINLINE /* noinline to keep it out of the transfer fast path */
839coro_setup (pTHX_ struct coro *coro) 862coro_setup (pTHX_ struct coro *coro)
840{ 863{
845 868
846 PL_runops = RUNOPS_DEFAULT; 869 PL_runops = RUNOPS_DEFAULT;
847 PL_curcop = &PL_compiling; 870 PL_curcop = &PL_compiling;
848 PL_in_eval = EVAL_NULL; 871 PL_in_eval = EVAL_NULL;
849 PL_comppad = 0; 872 PL_comppad = 0;
873 PL_comppad_name = 0;
874 PL_comppad_name_fill = 0;
875 PL_comppad_name_floor = 0;
850 PL_curpm = 0; 876 PL_curpm = 0;
851 PL_curpad = 0; 877 PL_curpad = 0;
852 PL_localizing = 0; 878 PL_localizing = 0;
853 PL_dirty = 0; 879 PL_dirty = 0;
854 PL_restartop = 0; 880 PL_restartop = 0;
855#if PERL_VERSION_ATLEAST (5,10,0) 881#if PERL_VERSION_ATLEAST (5,10,0)
856 PL_parser = 0; 882 PL_parser = 0;
857#endif 883#endif
884 PL_hints = 0;
858 885
859 /* recreate the die/warn hooks */ 886 /* recreate the die/warn hooks */
860 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); 887 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook );
861 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); 888 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
862 889
863 GvSV (PL_defgv) = newSV (0); 890 GvSV (PL_defgv) = newSV (0);
864 GvAV (PL_defgv) = coro->args; coro->args = 0; 891 GvAV (PL_defgv) = coro->args; coro->args = 0;
865 GvSV (PL_errgv) = newSV (0); 892 GvSV (PL_errgv) = newSV (0);
866 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 893 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
894 GvHV (PL_hintgv) = 0;
867 PL_rs = newSVsv (GvSV (irsgv)); 895 PL_rs = newSVsv (GvSV (irsgv));
868 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 896 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
869 897
870 { 898 {
871 dSP; 899 dSP;
889 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 917 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
890 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ 918 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
891 919
892 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ 920 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */
893 coro_setup_op.op_next = PL_op; 921 coro_setup_op.op_next = PL_op;
894 coro_setup_op.op_type = OP_CUSTOM; 922 coro_setup_op.op_type = OP_ENTERSUB;
895 coro_setup_op.op_ppaddr = pp_slf; 923 coro_setup_op.op_ppaddr = pp_slf;
896 /* no flags etc. required, as an init function won't be called */ 924 /* no flags etc. required, as an init function won't be called */
897 925
898 PL_op = (OP *)&coro_setup_op; 926 PL_op = (OP *)&coro_setup_op;
899 927
925 SvREFCNT_dec (GvAV (PL_defgv)); 953 SvREFCNT_dec (GvAV (PL_defgv));
926 SvREFCNT_dec (GvSV (PL_errgv)); 954 SvREFCNT_dec (GvSV (PL_errgv));
927 SvREFCNT_dec (PL_defoutgv); 955 SvREFCNT_dec (PL_defoutgv);
928 SvREFCNT_dec (PL_rs); 956 SvREFCNT_dec (PL_rs);
929 SvREFCNT_dec (GvSV (irsgv)); 957 SvREFCNT_dec (GvSV (irsgv));
958 SvREFCNT_dec (GvHV (PL_hintgv));
930 959
931 SvREFCNT_dec (PL_diehook); 960 SvREFCNT_dec (PL_diehook);
932 SvREFCNT_dec (PL_warnhook); 961 SvREFCNT_dec (PL_warnhook);
933 962
934 SvREFCNT_dec (coro->saved_deffh); 963 SvREFCNT_dec (coro->saved_deffh);
935 SvREFCNT_dec (coro->rouse_cb); 964 SvREFCNT_dec (coro->rouse_cb);
965 SvREFCNT_dec (coro->invoke_cb);
966 SvREFCNT_dec (coro->invoke_av);
936 967
937 coro_destruct_stacks (aTHX); 968 coro_destruct_stacks (aTHX);
938} 969}
939 970
940INLINE void 971INLINE void
950static int 981static int
951runops_trace (pTHX) 982runops_trace (pTHX)
952{ 983{
953 COP *oldcop = 0; 984 COP *oldcop = 0;
954 int oldcxix = -2; 985 int oldcxix = -2;
955 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
956 coro_cctx *cctx = coro->cctx;
957 986
958 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 987 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
959 { 988 {
960 PERL_ASYNC_CHECK (); 989 PERL_ASYNC_CHECK ();
961 990
962 if (cctx->flags & CC_TRACE_ALL) 991 if (cctx_current->flags & CC_TRACE_ALL)
963 { 992 {
964 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) 993 if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB)
965 { 994 {
966 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 995 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
967 SV **bot, **top; 996 SV **bot, **top;
968 AV *av = newAV (); /* return values */ 997 AV *av = newAV (); /* return values */
969 SV **cb; 998 SV **cb;
1006 1035
1007 if (PL_curcop != &PL_compiling) 1036 if (PL_curcop != &PL_compiling)
1008 { 1037 {
1009 SV **cb; 1038 SV **cb;
1010 1039
1011 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1040 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1012 { 1041 {
1013 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1042 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1014 1043
1015 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1044 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1016 { 1045 {
1017 runops_proc_t old_runops = PL_runops;
1018 dSP; 1046 dSP;
1019 GV *gv = CvGV (cx->blk_sub.cv); 1047 GV *gv = CvGV (cx->blk_sub.cv);
1020 SV *fullname = sv_2mortal (newSV (0)); 1048 SV *fullname = sv_2mortal (newSV (0));
1021 1049
1022 if (isGV (gv)) 1050 if (isGV (gv))
1040 } 1068 }
1041 1069
1042 oldcxix = cxstack_ix; 1070 oldcxix = cxstack_ix;
1043 } 1071 }
1044 1072
1045 if (cctx->flags & CC_TRACE_LINE) 1073 if (cctx_current->flags & CC_TRACE_LINE)
1046 { 1074 {
1047 dSP; 1075 dSP;
1048 1076
1049 PL_runops = RUNOPS_DEFAULT; 1077 PL_runops = RUNOPS_DEFAULT;
1050 ENTER; 1078 ENTER;
1069 1097
1070 TAINT_NOT; 1098 TAINT_NOT;
1071 return 0; 1099 return 0;
1072} 1100}
1073 1101
1074static struct coro_cctx *cctx_ssl_cctx;
1075static struct CoroSLF cctx_ssl_frame; 1102static struct CoroSLF cctx_ssl_frame;
1076 1103
1077static void 1104static void
1078slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1105slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1079{ 1106{
1080 ta->prev = (struct coro *)cctx_ssl_cctx;
1081 ta->next = 0; 1107 ta->prev = 0;
1082} 1108}
1083 1109
1084static int 1110static int
1085slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1111slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1086{ 1112{
1089 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ 1115 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1090} 1116}
1091 1117
1092/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ 1118/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1093static void NOINLINE 1119static void NOINLINE
1094cctx_prepare (pTHX_ coro_cctx *cctx) 1120cctx_prepare (pTHX)
1095{ 1121{
1096 PL_top_env = &PL_start_env; 1122 PL_top_env = &PL_start_env;
1097 1123
1098 if (cctx->flags & CC_TRACE) 1124 if (cctx_current->flags & CC_TRACE)
1099 PL_runops = runops_trace; 1125 PL_runops = runops_trace;
1100 1126
1101 /* we already must be executing an SLF op, there is no other valid way 1127 /* we already must be executing an SLF op, there is no other valid way
1102 * that can lead to creation of a new cctx */ 1128 * that can lead to creation of a new cctx */
1103 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", 1129 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1104 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1130 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1105 1131
1106 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ 1132 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */
1107 cctx_ssl_cctx = cctx;
1108 cctx_ssl_frame = slf_frame; 1133 cctx_ssl_frame = slf_frame;
1109 1134
1110 slf_frame.prepare = slf_prepare_set_stacklevel; 1135 slf_frame.prepare = slf_prepare_set_stacklevel;
1111 slf_frame.check = slf_check_set_stacklevel; 1136 slf_frame.check = slf_check_set_stacklevel;
1112} 1137}
1135 /* normally we would need to skip the entersub here */ 1160 /* normally we would need to skip the entersub here */
1136 /* not doing so will re-execute it, which is exactly what we want */ 1161 /* not doing so will re-execute it, which is exactly what we want */
1137 /* PL_nop = PL_nop->op_next */ 1162 /* PL_nop = PL_nop->op_next */
1138 1163
1139 /* inject a fake subroutine call to cctx_init */ 1164 /* inject a fake subroutine call to cctx_init */
1140 cctx_prepare (aTHX_ (coro_cctx *)arg); 1165 cctx_prepare (aTHX);
1141 1166
1142 /* cctx_run is the alternative tail of transfer() */ 1167 /* cctx_run is the alternative tail of transfer() */
1143 transfer_tail (aTHX); 1168 transfer_tail (aTHX);
1144 1169
1145 /* somebody or something will hit me for both perl_run and PL_restartop */ 1170 /* somebody or something will hit me for both perl_run and PL_restartop */
1240cctx_destroy (coro_cctx *cctx) 1265cctx_destroy (coro_cctx *cctx)
1241{ 1266{
1242 if (!cctx) 1267 if (!cctx)
1243 return; 1268 return;
1244 1269
1270 assert (cctx != cctx_current);//D temporary
1271
1245 --cctx_count; 1272 --cctx_count;
1246 coro_destroy (&cctx->cctx); 1273 coro_destroy (&cctx->cctx);
1247 1274
1248 /* coro_transfer creates new, empty cctx's */ 1275 /* coro_transfer creates new, empty cctx's */
1249 if (cctx->sptr) 1276 if (cctx->sptr)
1312 /* TODO: throwing up here is considered harmful */ 1339 /* TODO: throwing up here is considered harmful */
1313 1340
1314 if (expect_true (prev != next)) 1341 if (expect_true (prev != next))
1315 { 1342 {
1316 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1343 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1317 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); 1344 croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,");
1318 1345
1319 if (expect_false (next->flags & CF_RUNNING)) 1346 if (expect_false (next->flags & CF_RUNNING))
1320 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); 1347 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1321 1348
1322 if (expect_false (next->flags & CF_DESTROYED)) 1349 if (expect_false (next->flags & CF_DESTROYED))
1334transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1361transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1335{ 1362{
1336 dSTACKLEVEL; 1363 dSTACKLEVEL;
1337 1364
1338 /* sometimes transfer is only called to set idle_sp */ 1365 /* sometimes transfer is only called to set idle_sp */
1339 if (expect_false (!next)) 1366 if (expect_false (!prev))
1340 { 1367 {
1341 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1368 cctx_current->idle_sp = STACKLEVEL;
1342 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1369 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1343 } 1370 }
1344 else if (expect_true (prev != next)) 1371 else if (expect_true (prev != next))
1345 { 1372 {
1346 coro_cctx *prev__cctx; 1373 coro_cctx *cctx_prev;
1347 1374
1348 if (expect_false (prev->flags & CF_NEW)) 1375 if (expect_false (prev->flags & CF_NEW))
1349 { 1376 {
1350 /* create a new empty/source context */ 1377 /* create a new empty/source context */
1351 prev->cctx = cctx_new_empty ();
1352 prev->flags &= ~CF_NEW; 1378 prev->flags &= ~CF_NEW;
1353 prev->flags |= CF_RUNNING; 1379 prev->flags |= CF_RUNNING;
1354 } 1380 }
1355 1381
1356 prev->flags &= ~CF_RUNNING; 1382 prev->flags &= ~CF_RUNNING;
1367 coro_setup (aTHX_ next); 1393 coro_setup (aTHX_ next);
1368 } 1394 }
1369 else 1395 else
1370 load_perl (aTHX_ next); 1396 load_perl (aTHX_ next);
1371 1397
1372 prev__cctx = prev->cctx; 1398 assert (!prev->cctx);//D temporary
1373 1399
1374 /* possibly untie and reuse the cctx */ 1400 /* possibly untie and reuse the cctx */
1375 if (expect_true ( 1401 if (expect_true (
1376 prev__cctx->idle_sp == STACKLEVEL 1402 cctx_current->idle_sp == STACKLEVEL
1377 && !(prev__cctx->flags & CC_TRACE) 1403 && !(cctx_current->flags & CC_TRACE)
1378 && !force_cctx 1404 && !force_cctx
1379 )) 1405 ))
1380 { 1406 {
1381 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1407 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1382 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); 1408 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1383 1409
1384 prev->cctx = 0;
1385
1386 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1410 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */
1387 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1411 /* without this the next cctx_get might destroy the running cctx while still in use */
1388 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1412 if (expect_false (CCTX_EXPIRED (cctx_current)))
1389 if (!next->cctx) 1413 if (expect_true (!next->cctx))
1390 next->cctx = cctx_get (aTHX); 1414 next->cctx = cctx_get (aTHX);
1391 1415
1392 cctx_put (prev__cctx); 1416 cctx_put (cctx_current);
1393 } 1417 }
1418 else
1419 prev->cctx = cctx_current;
1394 1420
1395 ++next->usecount; 1421 ++next->usecount;
1396 1422
1397 if (expect_true (!next->cctx)) 1423 cctx_prev = cctx_current;
1398 next->cctx = cctx_get (aTHX); 1424 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1399 1425
1400 if (expect_false (prev__cctx != next->cctx)) 1426 next->cctx = 0;
1427
1428 if (expect_false (cctx_prev != cctx_current))
1401 { 1429 {
1402 prev__cctx->top_env = PL_top_env; 1430 cctx_prev->top_env = PL_top_env;
1403 PL_top_env = next->cctx->top_env; 1431 PL_top_env = cctx_current->top_env;
1404 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1432 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1405 } 1433 }
1406 1434
1407 transfer_tail (aTHX); 1435 transfer_tail (aTHX);
1408 } 1436 }
1409} 1437}
1564{ 1592{
1565 struct coro *coro; 1593 struct coro *coro;
1566 SV *sv_hook; 1594 SV *sv_hook;
1567 void (*xs_hook)(void); 1595 void (*xs_hook)(void);
1568 1596
1569 if (SvROK (coro_sv))
1570 coro_sv = SvRV (coro_sv);
1571
1572 coro = SvSTATE (coro_sv); 1597 coro = SvSTATE (coro_sv);
1573 1598
1574 if (coro->flags & CF_READY) 1599 if (coro->flags & CF_READY)
1575 return 0; 1600 return 0;
1576 1601
1607api_is_ready (pTHX_ SV *coro_sv) 1632api_is_ready (pTHX_ SV *coro_sv)
1608{ 1633{
1609 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1634 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1610} 1635}
1611 1636
1637/* expects to own a reference to next->hv */
1612INLINE void 1638INLINE void
1613prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1639prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
1614{ 1640{
1615 SV *prev_sv, *next_sv;
1616
1617 for (;;)
1618 {
1619 next_sv = coro_deq (aTHX);
1620
1621 /* nothing to schedule: call the idle handler */
1622 if (expect_false (!next_sv))
1623 {
1624 dSP;
1625
1626 ENTER;
1627 SAVETMPS;
1628
1629 PUSHMARK (SP);
1630 PUTBACK;
1631 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD);
1632
1633 FREETMPS;
1634 LEAVE;
1635 continue;
1636 }
1637
1638 ta->next = SvSTATE_hv (next_sv);
1639
1640 /* cannot transfer to destroyed coros, skip and look for next */
1641 if (expect_false (ta->next->flags & CF_DESTROYED))
1642 {
1643 SvREFCNT_dec (next_sv);
1644 /* coro_nready has already been taken care of by destroy */
1645 continue;
1646 }
1647
1648 --coro_nready;
1649 break;
1650 }
1651
1652 /* free this only after the transfer */
1653 prev_sv = SvRV (coro_current); 1641 SV *prev_sv = SvRV (coro_current);
1642
1654 ta->prev = SvSTATE_hv (prev_sv); 1643 ta->prev = SvSTATE_hv (prev_sv);
1644 ta->next = next;
1645
1655 TRANSFER_CHECK (*ta); 1646 TRANSFER_CHECK (*ta);
1656 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); 1647
1657 ta->next->flags &= ~CF_READY;
1658 SvRV_set (coro_current, next_sv); 1648 SvRV_set (coro_current, (SV *)next->hv);
1659 1649
1660 free_coro_mortal (aTHX); 1650 free_coro_mortal (aTHX);
1661 coro_mortal = prev_sv; 1651 coro_mortal = prev_sv;
1662} 1652}
1663 1653
1654static void
1655prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1656{
1657 for (;;)
1658 {
1659 SV *next_sv = coro_deq (aTHX);
1660
1661 if (expect_true (next_sv))
1662 {
1663 struct coro *next = SvSTATE_hv (next_sv);
1664
1665 /* cannot transfer to destroyed coros, skip and look for next */
1666 if (expect_false (next->flags & CF_DESTROYED))
1667 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */
1668 else
1669 {
1670 next->flags &= ~CF_READY;
1671 --coro_nready;
1672
1673 prepare_schedule_to (aTHX_ ta, next);
1674 break;
1675 }
1676 }
1677 else
1678 {
1679 /* nothing to schedule: call the idle handler */
1680 if (SvROK (sv_idle)
1681 && SvOBJECT (SvRV (sv_idle)))
1682 {
1683 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1684 api_ready (aTHX_ SvRV (sv_idle));
1685 --coro_nready;
1686 }
1687 else
1688 {
1689 dSP;
1690
1691 ENTER;
1692 SAVETMPS;
1693
1694 PUSHMARK (SP);
1695 PUTBACK;
1696 call_sv (sv_idle, G_VOID | G_DISCARD);
1697
1698 FREETMPS;
1699 LEAVE;
1700 }
1701 }
1702 }
1703}
1704
1664INLINE void 1705INLINE void
1665prepare_cede (pTHX_ struct coro_transfer_args *ta) 1706prepare_cede (pTHX_ struct coro_transfer_args *ta)
1666{ 1707{
1667 api_ready (aTHX_ coro_current); 1708 api_ready (aTHX_ coro_current);
1668 prepare_schedule (aTHX_ ta); 1709 prepare_schedule (aTHX_ ta);
1687{ 1728{
1688 struct coro_transfer_args ta; 1729 struct coro_transfer_args ta;
1689 1730
1690 prepare_schedule (aTHX_ &ta); 1731 prepare_schedule (aTHX_ &ta);
1691 TRANSFER (ta, 1); 1732 TRANSFER (ta, 1);
1733}
1734
1735static void
1736api_schedule_to (pTHX_ SV *coro_sv)
1737{
1738 struct coro_transfer_args ta;
1739 struct coro *next = SvSTATE (coro_sv);
1740
1741 SvREFCNT_inc_NN (coro_sv);
1742 prepare_schedule_to (aTHX_ &ta, next);
1692} 1743}
1693 1744
1694static int 1745static int
1695api_cede (pTHX) 1746api_cede (pTHX)
1696{ 1747{
1725static void 1776static void
1726api_trace (pTHX_ SV *coro_sv, int flags) 1777api_trace (pTHX_ SV *coro_sv, int flags)
1727{ 1778{
1728 struct coro *coro = SvSTATE (coro_sv); 1779 struct coro *coro = SvSTATE (coro_sv);
1729 1780
1781 if (coro->flags & CF_RUNNING)
1782 croak ("cannot enable tracing on a running coroutine, caught");
1783
1730 if (flags & CC_TRACE) 1784 if (flags & CC_TRACE)
1731 { 1785 {
1732 if (!coro->cctx) 1786 if (!coro->cctx)
1733 coro->cctx = cctx_new_run (); 1787 coro->cctx = cctx_new_run ();
1734 else if (!(coro->cctx->flags & CC_TRACE)) 1788 else if (!(coro->cctx->flags & CC_TRACE))
1735 croak ("cannot enable tracing on coroutine with custom stack,"); 1789 croak ("cannot enable tracing on coroutine with custom stack, caught");
1736 1790
1737 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1791 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1738 } 1792 }
1739 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1793 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1740 { 1794 {
1745 else 1799 else
1746 coro->slot->runops = RUNOPS_DEFAULT; 1800 coro->slot->runops = RUNOPS_DEFAULT;
1747 } 1801 }
1748} 1802}
1749 1803
1804static void
1805coro_call_on_destroy (pTHX_ struct coro *coro)
1806{
1807 SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0);
1808 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
1809
1810 if (on_destroyp)
1811 {
1812 AV *on_destroy = (AV *)SvRV (*on_destroyp);
1813
1814 while (AvFILLp (on_destroy) >= 0)
1815 {
1816 dSP; /* don't disturb outer sp */
1817 SV *cb = av_pop (on_destroy);
1818
1819 PUSHMARK (SP);
1820
1821 if (statusp)
1822 {
1823 int i;
1824 AV *status = (AV *)SvRV (*statusp);
1825 EXTEND (SP, AvFILLp (status) + 1);
1826
1827 for (i = 0; i <= AvFILLp (status); ++i)
1828 PUSHs (AvARRAY (status)[i]);
1829 }
1830
1831 PUTBACK;
1832 call_sv (sv_2mortal (cb), G_VOID | G_DISCARD);
1833 }
1834 }
1835}
1836
1837static void
1838slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1839{
1840 int i;
1841 HV *hv = (HV *)SvRV (coro_current);
1842 AV *av = newAV ();
1843
1844 av_extend (av, items - 1);
1845 for (i = 0; i < items; ++i)
1846 av_push (av, SvREFCNT_inc_NN (arg [i]));
1847
1848 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0);
1849
1850 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1851 api_ready (aTHX_ sv_manager);
1852
1853 frame->prepare = prepare_schedule;
1854 frame->check = slf_check_repeat;
1855}
1856
1857/*****************************************************************************/
1858/* async pool handler */
1859
1860static int
1861slf_check_pool_handler (pTHX_ struct CoroSLF *frame)
1862{
1863 HV *hv = (HV *)SvRV (coro_current);
1864 struct coro *coro = (struct coro *)frame->data;
1865
1866 if (!coro->invoke_cb)
1867 return 1; /* loop till we have invoke */
1868 else
1869 {
1870 hv_store (hv, "desc", sizeof ("desc") - 1,
1871 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1872
1873 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1874
1875 {
1876 dSP;
1877 XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0;
1878 PUTBACK;
1879 }
1880
1881 SvREFCNT_dec (GvAV (PL_defgv));
1882 GvAV (PL_defgv) = coro->invoke_av;
1883 coro->invoke_av = 0;
1884
1885 return 0;
1886 }
1887}
1888
1889static void
1890slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1891{
1892 HV *hv = (HV *)SvRV (coro_current);
1893 struct coro *coro = SvSTATE_hv ((SV *)hv);
1894
1895 if (expect_true (coro->saved_deffh))
1896 {
1897 /* subsequent iteration */
1898 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1899 coro->saved_deffh = 0;
1900
1901 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
1902 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1903 {
1904 coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate);
1905 coro->invoke_av = newAV ();
1906
1907 frame->prepare = prepare_nop;
1908 }
1909 else
1910 {
1911 av_clear (GvAV (PL_defgv));
1912 hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0);
1913
1914 coro->prio = 0;
1915
1916 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1917 api_trace (aTHX_ coro_current, 0);
1918
1919 frame->prepare = prepare_schedule;
1920 av_push (av_async_pool, SvREFCNT_inc (hv));
1921 }
1922 }
1923 else
1924 {
1925 /* first iteration, simply fall through */
1926 frame->prepare = prepare_nop;
1927 }
1928
1929 frame->check = slf_check_pool_handler;
1930 frame->data = (void *)coro;
1931}
1932
1750/*****************************************************************************/ 1933/*****************************************************************************/
1751/* rouse callback */ 1934/* rouse callback */
1752 1935
1753#define CORO_MAGIC_type_rouse PERL_MAGIC_ext 1936#define CORO_MAGIC_type_rouse PERL_MAGIC_ext
1754 1937
1759 SV *data = (SV *)GENSUB_ARG; 1942 SV *data = (SV *)GENSUB_ARG;
1760 1943
1761 if (SvTYPE (SvRV (data)) != SVt_PVAV) 1944 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1762 { 1945 {
1763 /* first call, set args */ 1946 /* first call, set args */
1764 int i;
1765 AV *av = newAV (); 1947 AV *av = newAV ();
1766 SV *coro = SvRV (data); 1948 SV *coro = SvRV (data);
1767 1949
1768 SvRV_set (data, (SV *)av); 1950 SvRV_set (data, (SV *)av);
1769 api_ready (aTHX_ coro); 1951 api_ready (aTHX_ coro);
1910static void 2092static void
1911slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2093slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1912{ 2094{
1913 frame->prepare = prepare_schedule; 2095 frame->prepare = prepare_schedule;
1914 frame->check = slf_check_nop; 2096 frame->check = slf_check_nop;
2097}
2098
2099static void
2100slf_prepare_schedule_to (pTHX_ struct coro_transfer_args *ta)
2101{
2102 struct coro *next = (struct coro *)slf_frame.data;
2103
2104 SvREFCNT_inc_NN (next->hv);
2105 prepare_schedule_to (aTHX_ ta, next);
2106}
2107
2108static void
2109slf_init_schedule_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2110{
2111 if (!items)
2112 croak ("Coro::schedule_to expects a coroutine argument, caught");
2113
2114 frame->data = (void *)SvSTATE (arg [0]);
2115 frame->prepare = slf_prepare_schedule_to;
2116 frame->check = slf_check_nop;
2117}
2118
2119static void
2120slf_init_cede_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2121{
2122 api_ready (aTHX_ SvRV (coro_current));
2123
2124 slf_init_schedule_to (aTHX_ frame, cv, arg, items);
1915} 2125}
1916 2126
1917static void 2127static void
1918slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2128slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1919{ 2129{
2065 } 2275 }
2066 else 2276 else
2067 slf_argc = 0; 2277 slf_argc = 0;
2068 2278
2069 PL_op->op_ppaddr = pp_slf; 2279 PL_op->op_ppaddr = pp_slf;
2070 PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ 2280 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2071 2281
2072 PL_op = (OP *)&slf_restore; 2282 PL_op = (OP *)&slf_restore;
2073} 2283}
2074 2284
2075/*****************************************************************************/ 2285/*****************************************************************************/
2298{ 2508{
2299 if (items >= 2) 2509 if (items >= 2)
2300 { 2510 {
2301 /* callback form */ 2511 /* callback form */
2302 AV *av = (AV *)SvRV (arg [0]); 2512 AV *av = (AV *)SvRV (arg [0]);
2303 CV *cb_cv = coro_sv_2cv (arg [1]); 2513 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2304 2514
2305 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); 2515 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv));
2306 2516
2307 if (SvIVX (AvARRAY (av)[0]) > 0) 2517 if (SvIVX (AvARRAY (av)[0]) > 0)
2308 coro_semaphore_adjust (aTHX_ av, 0); 2518 coro_semaphore_adjust (aTHX_ av, 0);
2394 dXSARGS; 2604 dXSARGS;
2395 AV *state = (AV *)GENSUB_ARG; 2605 AV *state = (AV *)GENSUB_ARG;
2396 SV *coro = av_pop (state); 2606 SV *coro = av_pop (state);
2397 SV *data_sv = newSV (sizeof (struct io_state)); 2607 SV *data_sv = newSV (sizeof (struct io_state));
2398 2608
2399 av_extend (state, items); 2609 av_extend (state, items - 1);
2400 2610
2401 sv_upgrade (data_sv, SVt_PV); 2611 sv_upgrade (data_sv, SVt_PV);
2402 SvCUR_set (data_sv, sizeof (struct io_state)); 2612 SvCUR_set (data_sv, sizeof (struct io_state));
2403 SvPOK_only (data_sv); 2613 SvPOK_only (data_sv);
2404 2614
2539 XSRETURN_EMPTY; 2749 XSRETURN_EMPTY;
2540} 2750}
2541 2751
2542/*****************************************************************************/ 2752/*****************************************************************************/
2543 2753
2754#if CORO_CLONE
2755# include "clone.c"
2756#endif
2757
2544MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2758MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2545 2759
2546PROTOTYPES: DISABLE 2760PROTOTYPES: DISABLE
2547 2761
2548BOOT: 2762BOOT:
2552 coro_thx = PERL_GET_CONTEXT; 2766 coro_thx = PERL_GET_CONTEXT;
2553# endif 2767# endif
2554#endif 2768#endif
2555 BOOT_PAGESIZE; 2769 BOOT_PAGESIZE;
2556 2770
2771 cctx_current = cctx_new_empty ();
2772
2557 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2773 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2558 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2774 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2559 2775
2560 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 2776 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
2561 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; 2777 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
2580 2796
2581 { 2797 {
2582 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); 2798 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf)));
2583 2799
2584 if (!PL_custom_op_names) PL_custom_op_names = newHV (); 2800 if (!PL_custom_op_names) PL_custom_op_names = newHV ();
2585 hv_store_ent (PL_custom_op_names, slf, 2801 hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0);
2586 newSVpv ("coro_slf", 0), 0);
2587 2802
2588 if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); 2803 if (!PL_custom_op_descs) PL_custom_op_descs = newHV ();
2589 hv_store_ent (PL_custom_op_descs, slf, 2804 hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0);
2590 newSVpv ("coro schedule like function", 0), 0);
2591 } 2805 }
2592 2806
2593 coroapi.ver = CORO_API_VERSION; 2807 coroapi.ver = CORO_API_VERSION;
2594 coroapi.rev = CORO_API_REVISION; 2808 coroapi.rev = CORO_API_REVISION;
2595 2809
2614 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 2828 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
2615} 2829}
2616 2830
2617SV * 2831SV *
2618new (char *klass, ...) 2832new (char *klass, ...)
2833 ALIAS:
2834 Coro::new = 1
2619 CODE: 2835 CODE:
2620{ 2836{
2621 struct coro *coro; 2837 struct coro *coro;
2622 MAGIC *mg; 2838 MAGIC *mg;
2623 HV *hv; 2839 HV *hv;
2624 CV *cb; 2840 CV *cb;
2625 int i; 2841 int i;
2626 2842
2627 if (items > 1) 2843 if (items > 1)
2628 { 2844 {
2629 cb = coro_sv_2cv (ST (1)); 2845 cb = coro_sv_2cv (aTHX_ ST (1));
2630 2846
2847 if (!ix)
2848 {
2631 if (CvISXSUB (cb)) 2849 if (CvISXSUB (cb))
2632 croak ("Coro::State doesn't support XS functions as coroutine start, caught"); 2850 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
2633 2851
2634 if (!CvROOT (cb)) 2852 if (!CvROOT (cb))
2635 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); 2853 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
2854 }
2636 } 2855 }
2637 2856
2638 Newz (0, coro, 1, struct coro); 2857 Newz (0, coro, 1, struct coro);
2639 coro->args = newAV (); 2858 coro->args = newAV ();
2640 coro->flags = CF_NEW; 2859 coro->flags = CF_NEW;
2648 mg->mg_flags |= MGf_DUP; 2867 mg->mg_flags |= MGf_DUP;
2649 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 2868 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2650 2869
2651 if (items > 1) 2870 if (items > 1)
2652 { 2871 {
2653 coro->startcv = SvREFCNT_inc_NN (cb);
2654
2655 av_extend (coro->args, items - 1); 2872 av_extend (coro->args, items - 1 + ix - 1);
2873
2874 if (ix)
2875 {
2876 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
2877 cb = cv_coro_run;
2878 }
2879
2880 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
2881
2656 for (i = 2; i < items; i++) 2882 for (i = 2; i < items; i++)
2657 av_push (coro->args, newSVsv (ST (i))); 2883 av_push (coro->args, newSVsv (ST (i)));
2658 } 2884 }
2659} 2885}
2660 OUTPUT: 2886 OUTPUT:
2676void 2902void
2677_exit (int code) 2903_exit (int code)
2678 PROTOTYPE: $ 2904 PROTOTYPE: $
2679 CODE: 2905 CODE:
2680 _exit (code); 2906 _exit (code);
2907
2908SV *
2909clone (Coro::State coro)
2910 CODE:
2911{
2912#if CORO_CLONE
2913 struct coro *ncoro = coro_clone (coro);
2914 MAGIC *mg;
2915 /* TODO: too much duplication */
2916 ncoro->hv = newHV ();
2917 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2918 mg->mg_flags |= MGf_DUP;
2919 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv));
2920#else
2921 croak ("Coro::State->clone has not been configured into this installation of Coro, realised");
2922#endif
2923}
2924 OUTPUT:
2925 RETVAL
2681 2926
2682int 2927int
2683cctx_stacksize (int new_stacksize = 0) 2928cctx_stacksize (int new_stacksize = 0)
2684 PROTOTYPE: ;$ 2929 PROTOTYPE: ;$
2685 CODE: 2930 CODE:
2806 3051
2807SV * 3052SV *
2808has_cctx (Coro::State coro) 3053has_cctx (Coro::State coro)
2809 PROTOTYPE: $ 3054 PROTOTYPE: $
2810 CODE: 3055 CODE:
2811 RETVAL = boolSV (!!coro->cctx); 3056 /* maybe manage the running flag differently */
3057 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
2812 OUTPUT: 3058 OUTPUT:
2813 RETVAL 3059 RETVAL
2814 3060
2815int 3061int
2816is_traced (Coro::State coro) 3062is_traced (Coro::State coro)
2836 3082
2837void 3083void
2838force_cctx () 3084force_cctx ()
2839 PROTOTYPE: 3085 PROTOTYPE:
2840 CODE: 3086 CODE:
2841 SvSTATE_current->cctx->idle_sp = 0; 3087 cctx_current->idle_sp = 0;
2842 3088
2843void 3089void
2844swap_defsv (Coro::State self) 3090swap_defsv (Coro::State self)
2845 PROTOTYPE: $ 3091 PROTOTYPE: $
2846 ALIAS: 3092 ALIAS:
2861 3107
2862BOOT: 3108BOOT:
2863{ 3109{
2864 int i; 3110 int i;
2865 3111
2866 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2867 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3112 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2868 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3113 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2869 3114 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD);
3115 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
2870 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); 3116 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
2871 SvREADONLY_on (coro_current); 3117 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3118 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3119 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3120 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3121
3122 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3123 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3124 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3125 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
2872 3126
2873 coro_stash = gv_stashpv ("Coro", TRUE); 3127 coro_stash = gv_stashpv ("Coro", TRUE);
2874 3128
2875 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 3129 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2876 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3130 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2884 3138
2885 { 3139 {
2886 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3140 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2887 3141
2888 coroapi.schedule = api_schedule; 3142 coroapi.schedule = api_schedule;
3143 coroapi.schedule_to = api_schedule_to;
2889 coroapi.cede = api_cede; 3144 coroapi.cede = api_cede;
2890 coroapi.cede_notself = api_cede_notself; 3145 coroapi.cede_notself = api_cede_notself;
2891 coroapi.ready = api_ready; 3146 coroapi.ready = api_ready;
2892 coroapi.is_ready = api_is_ready; 3147 coroapi.is_ready = api_is_ready;
2893 coroapi.nready = coro_nready; 3148 coroapi.nready = coro_nready;
2898 SvREADONLY_on (sv); 3153 SvREADONLY_on (sv);
2899 } 3154 }
2900} 3155}
2901 3156
2902void 3157void
3158terminate (...)
3159 CODE:
3160 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3161
3162void
2903schedule (...) 3163schedule (...)
2904 CODE: 3164 CODE:
2905 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3165 CORO_EXECUTE_SLF_XS (slf_init_schedule);
3166
3167void
3168schedule_to (...)
3169 CODE:
3170 CORO_EXECUTE_SLF_XS (slf_init_schedule_to);
3171
3172void
3173cede_to (...)
3174 CODE:
3175 CORO_EXECUTE_SLF_XS (slf_init_cede_to);
2906 3176
2907void 3177void
2908cede (...) 3178cede (...)
2909 CODE: 3179 CODE:
2910 CORO_EXECUTE_SLF_XS (slf_init_cede); 3180 CORO_EXECUTE_SLF_XS (slf_init_cede);
2911 3181
2912void 3182void
2913cede_notself (...) 3183cede_notself (...)
2914 CODE: 3184 CODE:
2915 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3185 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3186
3187void
3188_cancel (Coro::State self)
3189 CODE:
3190 coro_state_destroy (aTHX_ self);
3191 coro_call_on_destroy (aTHX_ self);
2916 3192
2917void 3193void
2918_set_current (SV *current) 3194_set_current (SV *current)
2919 PROTOTYPE: $ 3195 PROTOTYPE: $
2920 CODE: 3196 CODE:
2965 CODE: 3241 CODE:
2966 RETVAL = coro_nready; 3242 RETVAL = coro_nready;
2967 OUTPUT: 3243 OUTPUT:
2968 RETVAL 3244 RETVAL
2969 3245
2970# for async_pool speedup
2971void 3246void
2972_pool_1 (SV *cb) 3247_pool_handler (...)
2973 CODE: 3248 CODE:
2974{ 3249 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
2975 HV *hv = (HV *)SvRV (coro_current);
2976 struct coro *coro = SvSTATE_hv ((SV *)hv);
2977 AV *defav = GvAV (PL_defgv);
2978 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
2979 AV *invoke_av;
2980 int i, len;
2981 3250
2982 if (!invoke) 3251void
3252async_pool (SV *cv, ...)
3253 PROTOTYPE: &@
3254 PPCODE:
3255{
3256 HV *hv = (HV *)av_pop (av_async_pool);
3257 AV *av = newAV ();
3258 SV *cb = ST (0);
3259 int i;
3260
3261 av_extend (av, items - 2);
3262 for (i = 1; i < items; ++i)
3263 av_push (av, SvREFCNT_inc_NN (ST (i)));
3264
3265 if ((SV *)hv == &PL_sv_undef)
2983 { 3266 {
2984 SV *old = PL_diehook; 3267 PUSHMARK (SP);
2985 PL_diehook = 0; 3268 EXTEND (SP, 2);
2986 SvREFCNT_dec (old); 3269 PUSHs (sv_Coro);
2987 croak ("\3async_pool terminate\2\n"); 3270 PUSHs ((SV *)cv_pool_handler);
3271 PUTBACK;
3272 call_sv ((SV *)cv_coro_state_new, G_SCALAR);
3273 SPAGAIN;
3274
3275 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs));
2988 } 3276 }
2989 3277
2990 SvREFCNT_dec (coro->saved_deffh);
2991 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2992
2993 hv_store (hv, "desc", sizeof ("desc") - 1,
2994 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2995
2996 invoke_av = (AV *)SvRV (invoke);
2997 len = av_len (invoke_av);
2998
2999 sv_setsv (cb, AvARRAY (invoke_av)[0]);
3000
3001 if (len > 0)
3002 { 3278 {
3003 av_fill (defav, len - 1); 3279 struct coro *coro = SvSTATE_hv (hv);
3004 for (i = 0; i < len; ++i) 3280
3005 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); 3281 assert (!coro->invoke_cb);
3282 assert (!coro->invoke_av);
3283 coro->invoke_cb = SvREFCNT_inc (cb);
3284 coro->invoke_av = av;
3006 } 3285 }
3007}
3008 3286
3009void 3287 api_ready (aTHX_ (SV *)hv);
3010_pool_2 (SV *cb)
3011 CODE:
3012{
3013 HV *hv = (HV *)SvRV (coro_current);
3014 struct coro *coro = SvSTATE_hv ((SV *)hv);
3015 3288
3016 sv_setsv (cb, &PL_sv_undef); 3289 if (GIMME_V != G_VOID)
3017 3290 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
3018 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 3291 else
3019 coro->saved_deffh = 0;
3020
3021 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
3022 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
3023 {
3024 SV *old = PL_diehook;
3025 PL_diehook = 0;
3026 SvREFCNT_dec (old); 3292 SvREFCNT_dec (hv);
3027 croak ("\3async_pool terminate\2\n");
3028 }
3029
3030 av_clear (GvAV (PL_defgv));
3031 hv_store (hv, "desc", sizeof ("desc") - 1,
3032 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
3033
3034 coro->prio = 0;
3035
3036 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
3037 api_trace (aTHX_ coro_current, 0);
3038
3039 av_push (av_async_pool, newSVsv (coro_current));
3040} 3293}
3041 3294
3042SV * 3295SV *
3043rouse_cb () 3296rouse_cb ()
3044 PROTOTYPE: 3297 PROTOTYPE:
3218 3471
3219void 3472void
3220_register (char *target, char *proto, SV *req) 3473_register (char *target, char *proto, SV *req)
3221 CODE: 3474 CODE:
3222{ 3475{
3223 CV *req_cv = coro_sv_2cv (req); 3476 CV *req_cv = coro_sv_2cv (aTHX_ req);
3224 /* newXSproto doesn't return the CV on 5.8 */ 3477 /* newXSproto doesn't return the CV on 5.8 */
3225 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); 3478 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
3226 sv_setpv ((SV *)slf_cv, proto); 3479 sv_setpv ((SV *)slf_cv, proto);
3227 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 3480 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
3228} 3481}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines