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.310 by root, Wed Nov 19 16:01:32 2008 UTC vs.
Revision 1.323 by root, Sat Nov 22 06:03:10 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 */
180
172static GV *irsgv; /* $/ */ 181static GV *irsgv; /* $/ */
173static GV *stdoutgv; /* *STDOUT */ 182static GV *stdoutgv; /* *STDOUT */
174static SV *rv_diehook; 183static SV *rv_diehook;
175static SV *rv_warnhook; 184static SV *rv_warnhook;
176static HV *hv_sig; /* %SIG */ 185static HV *hv_sig; /* %SIG */
177 186
178/* async_pool helper stuff */ 187/* async_pool helper stuff */
179static SV *sv_pool_rss; 188static SV *sv_pool_rss;
180static SV *sv_pool_size; 189static SV *sv_pool_size;
190static SV *sv_async_pool_idle; /* description string */
181static AV *av_async_pool; 191static AV *av_async_pool; /* idle pool */
192static SV *sv_Coro; /* class string */
193static CV *cv_pool_handler;
194static CV *cv_coro_state_new;
182 195
183/* Coro::AnyEvent */ 196/* Coro::AnyEvent */
184static SV *sv_activity; 197static SV *sv_activity;
185 198
186static struct coro_cctx *cctx_first; 199static struct coro_cctx *cctx_first;
215 int valgrind_id; 228 int valgrind_id;
216#endif 229#endif
217 unsigned char flags; 230 unsigned char flags;
218} coro_cctx; 231} coro_cctx;
219 232
233coro_cctx *cctx_current; /* the currently running cctx */
234
235/*****************************************************************************/
236
220enum { 237enum {
221 CF_RUNNING = 0x0001, /* coroutine is running */ 238 CF_RUNNING = 0x0001, /* coroutine is running */
222 CF_READY = 0x0002, /* coroutine is ready */ 239 CF_READY = 0x0002, /* coroutine is ready */
223 CF_NEW = 0x0004, /* has never been switched to */ 240 CF_NEW = 0x0004, /* has never been switched to */
224 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 241 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
263 SV *except; /* exception to be thrown */ 280 SV *except; /* exception to be thrown */
264 SV *rouse_cb; 281 SV *rouse_cb;
265 282
266 /* async_pool */ 283 /* async_pool */
267 SV *saved_deffh; 284 SV *saved_deffh;
285 SV *invoke_cb;
286 AV *invoke_av;
268 287
269 /* linked list */ 288 /* linked list */
270 struct coro *next, *prev; 289 struct coro *next, *prev;
271}; 290};
272 291
289 308
290/* for Coro.pm */ 309/* for Coro.pm */
291static SV *coro_current; 310static SV *coro_current;
292static SV *coro_readyhook; 311static SV *coro_readyhook;
293static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 312static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
294static CV *cv_coro_run; 313static CV *cv_coro_run, *cv_coro_terminate;
295static struct coro *coro_first; 314static struct coro *coro_first;
296#define coro_nready coroapi.nready 315#define coro_nready coroapi.nready
297 316
298/** lowlevel stuff **********************************************************/ 317/** lowlevel stuff **********************************************************/
299 318
327 return get_hv (name, create); 346 return get_hv (name, create);
328} 347}
329 348
330/* may croak */ 349/* may croak */
331INLINE CV * 350INLINE CV *
332coro_sv_2cv (SV *sv) 351coro_sv_2cv (pTHX_ SV *sv)
333{ 352{
334 HV *st; 353 HV *st;
335 GV *gvp; 354 GV *gvp;
336 return sv_2cv (sv, &st, &gvp, 0); 355 return sv_2cv (sv, &st, &gvp, 0);
337} 356}
338 357
339static AV * 358static AV *
340coro_clone_padlist (pTHX_ CV *cv) 359coro_derive_padlist (pTHX_ CV *cv)
341{ 360{
342 AV *padlist = CvPADLIST (cv); 361 AV *padlist = CvPADLIST (cv);
343 AV *newpadlist, *newpad; 362 AV *newpadlist, *newpad;
344 363
345 newpadlist = newAV (); 364 newpadlist = newAV ();
465 CV *cp = Perl_cv_clone (aTHX_ cv); 484 CV *cp = Perl_cv_clone (aTHX_ cv);
466 CvPADLIST (cv) = CvPADLIST (cp); 485 CvPADLIST (cv) = CvPADLIST (cp);
467 CvPADLIST (cp) = 0; 486 CvPADLIST (cp) = 0;
468 SvREFCNT_dec (cp); 487 SvREFCNT_dec (cp);
469#else 488#else
470 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 489 CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv);
471#endif 490#endif
472 } 491 }
473} 492}
474 493
475static void 494static void
687#if !PERL_VERSION_ATLEAST (5,10,0) 706#if !PERL_VERSION_ATLEAST (5,10,0)
688 Safefree (PL_retstack); 707 Safefree (PL_retstack);
689#endif 708#endif
690} 709}
691 710
711#define CORO_RSS \
712 rss += sizeof (SYM (curstackinfo)); \
713 rss += (SYM (curstackinfo->si_cxmax) + 1) * sizeof (PERL_CONTEXT); \
714 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (SYM (curstack))) * sizeof (SV *); \
715 rss += SYM (tmps_max) * sizeof (SV *); \
716 rss += (SYM (markstack_max) - SYM (markstack_ptr)) * sizeof (I32); \
717 rss += SYM (scopestack_max) * sizeof (I32); \
718 rss += SYM (savestack_max) * sizeof (ANY);
719
692static size_t 720static size_t
693coro_rss (pTHX_ struct coro *coro) 721coro_rss (pTHX_ struct coro *coro)
694{ 722{
695 size_t rss = sizeof (*coro); 723 size_t rss = sizeof (*coro);
696 724
697 if (coro->mainstack) 725 if (coro->mainstack)
698 { 726 {
699 perl_slots tmp_slot;
700 perl_slots *slot;
701
702 if (coro->flags & CF_RUNNING) 727 if (coro->flags & CF_RUNNING)
703 { 728 {
704 slot = &tmp_slot; 729 #define SYM(sym) PL_ ## sym
705 730 CORO_RSS;
706 #define VAR(name,type) slot->name = PL_ ## name;
707 # include "state.h"
708 #undef VAR 731 #undef SYM
709 } 732 }
710 else 733 else
711 slot = coro->slot;
712
713 if (slot)
714 { 734 {
715 rss += sizeof (slot->curstackinfo); 735 #define SYM(sym) coro->slot->sym
716 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 736 CORO_RSS;
717 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); 737 #undef SYM
718 rss += slot->tmps_max * sizeof (SV *);
719 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
720 rss += slot->scopestack_max * sizeof (I32);
721 rss += slot->savestack_max * sizeof (ANY);
722
723#if !PERL_VERSION_ATLEAST (5,10,0)
724 rss += slot->retstack_max * sizeof (OP *);
725#endif
726 } 738 }
727 } 739 }
728 740
729 return rss; 741 return rss;
730} 742}
832slf_check_nop (pTHX_ struct CoroSLF *frame) 844slf_check_nop (pTHX_ struct CoroSLF *frame)
833{ 845{
834 return 0; 846 return 0;
835} 847}
836 848
849static int
850slf_check_repeat (pTHX_ struct CoroSLF *frame)
851{
852 return 1;
853}
854
837static UNOP coro_setup_op; 855static UNOP coro_setup_op;
838 856
839static void NOINLINE /* noinline to keep it out of the transfer fast path */ 857static void NOINLINE /* noinline to keep it out of the transfer fast path */
840coro_setup (pTHX_ struct coro *coro) 858coro_setup (pTHX_ struct coro *coro)
841{ 859{
890 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 908 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
891 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ 909 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
892 910
893 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ 911 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */
894 coro_setup_op.op_next = PL_op; 912 coro_setup_op.op_next = PL_op;
895 coro_setup_op.op_type = OP_CUSTOM; 913 coro_setup_op.op_type = OP_ENTERSUB;
896 coro_setup_op.op_ppaddr = pp_slf; 914 coro_setup_op.op_ppaddr = pp_slf;
897 /* no flags etc. required, as an init function won't be called */ 915 /* no flags etc. required, as an init function won't be called */
898 916
899 PL_op = (OP *)&coro_setup_op; 917 PL_op = (OP *)&coro_setup_op;
900 918
932 SvREFCNT_dec (PL_diehook); 950 SvREFCNT_dec (PL_diehook);
933 SvREFCNT_dec (PL_warnhook); 951 SvREFCNT_dec (PL_warnhook);
934 952
935 SvREFCNT_dec (coro->saved_deffh); 953 SvREFCNT_dec (coro->saved_deffh);
936 SvREFCNT_dec (coro->rouse_cb); 954 SvREFCNT_dec (coro->rouse_cb);
955 SvREFCNT_dec (coro->invoke_cb);
956 SvREFCNT_dec (coro->invoke_av);
937 957
938 coro_destruct_stacks (aTHX); 958 coro_destruct_stacks (aTHX);
939} 959}
940 960
941INLINE void 961INLINE void
951static int 971static int
952runops_trace (pTHX) 972runops_trace (pTHX)
953{ 973{
954 COP *oldcop = 0; 974 COP *oldcop = 0;
955 int oldcxix = -2; 975 int oldcxix = -2;
956 struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */
957 coro_cctx *cctx = coro->cctx;
958 976
959 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) 977 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
960 { 978 {
961 PERL_ASYNC_CHECK (); 979 PERL_ASYNC_CHECK ();
962 980
963 if (cctx->flags & CC_TRACE_ALL) 981 if (cctx_current->flags & CC_TRACE_ALL)
964 { 982 {
965 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) 983 if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB)
966 { 984 {
967 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 985 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
968 SV **bot, **top; 986 SV **bot, **top;
969 AV *av = newAV (); /* return values */ 987 AV *av = newAV (); /* return values */
970 SV **cb; 988 SV **cb;
1007 1025
1008 if (PL_curcop != &PL_compiling) 1026 if (PL_curcop != &PL_compiling)
1009 { 1027 {
1010 SV **cb; 1028 SV **cb;
1011 1029
1012 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) 1030 if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB)
1013 { 1031 {
1014 PERL_CONTEXT *cx = &cxstack[cxstack_ix]; 1032 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
1015 1033
1016 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) 1034 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
1017 { 1035 {
1018 runops_proc_t old_runops = PL_runops;
1019 dSP; 1036 dSP;
1020 GV *gv = CvGV (cx->blk_sub.cv); 1037 GV *gv = CvGV (cx->blk_sub.cv);
1021 SV *fullname = sv_2mortal (newSV (0)); 1038 SV *fullname = sv_2mortal (newSV (0));
1022 1039
1023 if (isGV (gv)) 1040 if (isGV (gv))
1041 } 1058 }
1042 1059
1043 oldcxix = cxstack_ix; 1060 oldcxix = cxstack_ix;
1044 } 1061 }
1045 1062
1046 if (cctx->flags & CC_TRACE_LINE) 1063 if (cctx_current->flags & CC_TRACE_LINE)
1047 { 1064 {
1048 dSP; 1065 dSP;
1049 1066
1050 PL_runops = RUNOPS_DEFAULT; 1067 PL_runops = RUNOPS_DEFAULT;
1051 ENTER; 1068 ENTER;
1070 1087
1071 TAINT_NOT; 1088 TAINT_NOT;
1072 return 0; 1089 return 0;
1073} 1090}
1074 1091
1075static struct coro_cctx *cctx_ssl_cctx;
1076static struct CoroSLF cctx_ssl_frame; 1092static struct CoroSLF cctx_ssl_frame;
1077 1093
1078static void 1094static void
1079slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) 1095slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1080{ 1096{
1081 ta->prev = (struct coro *)cctx_ssl_cctx;
1082 ta->next = 0; 1097 ta->prev = 0;
1083} 1098}
1084 1099
1085static int 1100static int
1086slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1101slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1087{ 1102{
1090 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ 1105 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1091} 1106}
1092 1107
1093/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ 1108/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1094static void NOINLINE 1109static void NOINLINE
1095cctx_prepare (pTHX_ coro_cctx *cctx) 1110cctx_prepare (pTHX)
1096{ 1111{
1097 PL_top_env = &PL_start_env; 1112 PL_top_env = &PL_start_env;
1098 1113
1099 if (cctx->flags & CC_TRACE) 1114 if (cctx_current->flags & CC_TRACE)
1100 PL_runops = runops_trace; 1115 PL_runops = runops_trace;
1101 1116
1102 /* we already must be executing an SLF op, there is no other valid way 1117 /* we already must be executing an SLF op, there is no other valid way
1103 * that can lead to creation of a new cctx */ 1118 * that can lead to creation of a new cctx */
1104 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", 1119 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1105 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1120 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1106 1121
1107 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ 1122 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */
1108 cctx_ssl_cctx = cctx;
1109 cctx_ssl_frame = slf_frame; 1123 cctx_ssl_frame = slf_frame;
1110 1124
1111 slf_frame.prepare = slf_prepare_set_stacklevel; 1125 slf_frame.prepare = slf_prepare_set_stacklevel;
1112 slf_frame.check = slf_check_set_stacklevel; 1126 slf_frame.check = slf_check_set_stacklevel;
1113} 1127}
1136 /* normally we would need to skip the entersub here */ 1150 /* normally we would need to skip the entersub here */
1137 /* not doing so will re-execute it, which is exactly what we want */ 1151 /* not doing so will re-execute it, which is exactly what we want */
1138 /* PL_nop = PL_nop->op_next */ 1152 /* PL_nop = PL_nop->op_next */
1139 1153
1140 /* inject a fake subroutine call to cctx_init */ 1154 /* inject a fake subroutine call to cctx_init */
1141 cctx_prepare (aTHX_ (coro_cctx *)arg); 1155 cctx_prepare (aTHX);
1142 1156
1143 /* cctx_run is the alternative tail of transfer() */ 1157 /* cctx_run is the alternative tail of transfer() */
1144 transfer_tail (aTHX); 1158 transfer_tail (aTHX);
1145 1159
1146 /* somebody or something will hit me for both perl_run and PL_restartop */ 1160 /* somebody or something will hit me for both perl_run and PL_restartop */
1241cctx_destroy (coro_cctx *cctx) 1255cctx_destroy (coro_cctx *cctx)
1242{ 1256{
1243 if (!cctx) 1257 if (!cctx)
1244 return; 1258 return;
1245 1259
1260 assert (cctx != cctx_current);//D temporary
1261
1246 --cctx_count; 1262 --cctx_count;
1247 coro_destroy (&cctx->cctx); 1263 coro_destroy (&cctx->cctx);
1248 1264
1249 /* coro_transfer creates new, empty cctx's */ 1265 /* coro_transfer creates new, empty cctx's */
1250 if (cctx->sptr) 1266 if (cctx->sptr)
1335transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1351transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1336{ 1352{
1337 dSTACKLEVEL; 1353 dSTACKLEVEL;
1338 1354
1339 /* sometimes transfer is only called to set idle_sp */ 1355 /* sometimes transfer is only called to set idle_sp */
1340 if (expect_false (!next)) 1356 if (expect_false (!prev))
1341 { 1357 {
1342 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1358 cctx_current->idle_sp = STACKLEVEL;
1343 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1359 assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1344 } 1360 }
1345 else if (expect_true (prev != next)) 1361 else if (expect_true (prev != next))
1346 { 1362 {
1347 coro_cctx *prev__cctx; 1363 coro_cctx *cctx_prev;
1348 1364
1349 if (expect_false (prev->flags & CF_NEW)) 1365 if (expect_false (prev->flags & CF_NEW))
1350 { 1366 {
1351 /* create a new empty/source context */ 1367 /* create a new empty/source context */
1352 prev->cctx = cctx_new_empty ();
1353 prev->flags &= ~CF_NEW; 1368 prev->flags &= ~CF_NEW;
1354 prev->flags |= CF_RUNNING; 1369 prev->flags |= CF_RUNNING;
1355 } 1370 }
1356 1371
1357 prev->flags &= ~CF_RUNNING; 1372 prev->flags &= ~CF_RUNNING;
1368 coro_setup (aTHX_ next); 1383 coro_setup (aTHX_ next);
1369 } 1384 }
1370 else 1385 else
1371 load_perl (aTHX_ next); 1386 load_perl (aTHX_ next);
1372 1387
1373 prev__cctx = prev->cctx;
1374
1375 /* possibly untie and reuse the cctx */ 1388 /* possibly untie and reuse the cctx */
1376 if (expect_true ( 1389 if (expect_true (
1377 prev__cctx->idle_sp == STACKLEVEL 1390 cctx_current->idle_sp == STACKLEVEL
1378 && !(prev__cctx->flags & CC_TRACE) 1391 && !(cctx_current->flags & CC_TRACE)
1379 && !force_cctx 1392 && !force_cctx
1380 )) 1393 ))
1381 { 1394 {
1382 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1395 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1383 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); 1396 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1384
1385 prev->cctx = 0;
1386 1397
1387 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1398 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1388 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1399 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1389 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1400 if (expect_false (CCTX_EXPIRED (cctx_current)))
1390 if (!next->cctx) 1401 if (!next->cctx)
1391 next->cctx = cctx_get (aTHX); 1402 next->cctx = cctx_get (aTHX);
1392 1403
1393 cctx_put (prev__cctx); 1404 cctx_put (cctx_current);
1405 assert (!prev->cctx);//D temporary
1394 } 1406 }
1407 else
1408 prev->cctx = cctx_current;
1395 1409
1396 ++next->usecount; 1410 ++next->usecount;
1397 1411
1398 if (expect_true (!next->cctx)) 1412 cctx_prev = cctx_current;
1399 next->cctx = cctx_get (aTHX); 1413 cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX);
1400 1414
1401 if (expect_false (prev__cctx != next->cctx)) 1415 next->cctx = 0;
1416
1417 if (expect_false (cctx_prev != cctx_current))
1402 { 1418 {
1403 prev__cctx->top_env = PL_top_env; 1419 cctx_prev->top_env = PL_top_env;
1404 PL_top_env = next->cctx->top_env; 1420 PL_top_env = cctx_current->top_env;
1405 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1421 coro_transfer (&cctx_prev->cctx, &cctx_current->cctx);
1406 } 1422 }
1407 1423
1408 transfer_tail (aTHX); 1424 transfer_tail (aTHX);
1409 } 1425 }
1410} 1426}
1608api_is_ready (pTHX_ SV *coro_sv) 1624api_is_ready (pTHX_ SV *coro_sv)
1609{ 1625{
1610 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1626 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1611} 1627}
1612 1628
1629/* expects to own a reference to next->hv */
1613INLINE void 1630INLINE void
1631prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next)
1632{
1633 SV *prev_sv = SvRV (coro_current);
1634
1635 ta->prev = SvSTATE_hv (prev_sv);
1636 ta->next = next;
1637
1638 TRANSFER_CHECK (*ta);
1639
1640 SvRV_set (coro_current, (SV *)next->hv);
1641
1642 free_coro_mortal (aTHX);
1643 coro_mortal = prev_sv;
1644}
1645
1646static void
1614prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1647prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1615{ 1648{
1616 SV *prev_sv, *next_sv;
1617
1618 for (;;) 1649 for (;;)
1619 { 1650 {
1620 next_sv = coro_deq (aTHX); 1651 SV *next_sv = coro_deq (aTHX);
1621 1652
1622 /* nothing to schedule: call the idle handler */
1623 if (expect_false (!next_sv)) 1653 if (expect_true (next_sv))
1624 { 1654 {
1655 struct coro *next = SvSTATE_hv (next_sv);
1656
1657 /* cannot transfer to destroyed coros, skip and look for next */
1658 if (expect_false (next->flags & CF_DESTROYED))
1659 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */
1660 else
1661 {
1662 next->flags &= ~CF_READY;
1663 --coro_nready;
1664
1665 prepare_schedule_to (aTHX_ ta, next);
1666 break;
1667 }
1668 }
1669 else
1670 {
1671 /* nothing to schedule: call the idle handler */
1625 dSP; 1672 dSP;
1626 1673
1627 ENTER; 1674 ENTER;
1628 SAVETMPS; 1675 SAVETMPS;
1629 1676
1631 PUTBACK; 1678 PUTBACK;
1632 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1679 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD);
1633 1680
1634 FREETMPS; 1681 FREETMPS;
1635 LEAVE; 1682 LEAVE;
1636 continue;
1637 } 1683 }
1638
1639 ta->next = SvSTATE_hv (next_sv);
1640
1641 /* cannot transfer to destroyed coros, skip and look for next */
1642 if (expect_false (ta->next->flags & CF_DESTROYED))
1643 {
1644 SvREFCNT_dec (next_sv);
1645 /* coro_nready has already been taken care of by destroy */
1646 continue;
1647 }
1648
1649 --coro_nready;
1650 break;
1651 } 1684 }
1652
1653 /* free this only after the transfer */
1654 prev_sv = SvRV (coro_current);
1655 ta->prev = SvSTATE_hv (prev_sv);
1656 TRANSFER_CHECK (*ta);
1657 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY));
1658 ta->next->flags &= ~CF_READY;
1659 SvRV_set (coro_current, next_sv);
1660
1661 free_coro_mortal (aTHX);
1662 coro_mortal = prev_sv;
1663} 1685}
1664 1686
1665INLINE void 1687INLINE void
1666prepare_cede (pTHX_ struct coro_transfer_args *ta) 1688prepare_cede (pTHX_ struct coro_transfer_args *ta)
1667{ 1689{
1688{ 1710{
1689 struct coro_transfer_args ta; 1711 struct coro_transfer_args ta;
1690 1712
1691 prepare_schedule (aTHX_ &ta); 1713 prepare_schedule (aTHX_ &ta);
1692 TRANSFER (ta, 1); 1714 TRANSFER (ta, 1);
1715}
1716
1717static void
1718api_schedule_to (pTHX_ SV *coro_sv)
1719{
1720 struct coro_transfer_args ta;
1721 struct coro *next = SvSTATE (coro_sv);
1722
1723 SvREFCNT_inc_NN (coro_sv);
1724 prepare_schedule_to (aTHX_ &ta, next);
1693} 1725}
1694 1726
1695static int 1727static int
1696api_cede (pTHX) 1728api_cede (pTHX)
1697{ 1729{
1746 else 1778 else
1747 coro->slot->runops = RUNOPS_DEFAULT; 1779 coro->slot->runops = RUNOPS_DEFAULT;
1748 } 1780 }
1749} 1781}
1750 1782
1783static void
1784coro_call_on_destroy (pTHX_ struct coro *coro)
1785{
1786 SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0);
1787 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
1788
1789 if (on_destroyp)
1790 {
1791 AV *on_destroy = (AV *)SvRV (*on_destroyp);
1792
1793 while (AvFILLp (on_destroy) >= 0)
1794 {
1795 dSP; /* don't disturb outer sp */
1796 SV *cb = av_pop (on_destroy);
1797
1798 PUSHMARK (SP);
1799
1800 if (statusp)
1801 {
1802 int i;
1803 AV *status = (AV *)SvRV (*statusp);
1804 EXTEND (SP, AvFILLp (status) + 1);
1805
1806 for (i = 0; i <= AvFILLp (status); ++i)
1807 PUSHs (AvARRAY (status)[i]);
1808 }
1809
1810 PUTBACK;
1811 call_sv (sv_2mortal (cb), G_VOID | G_DISCARD);
1812 }
1813 }
1814}
1815
1816static void
1817slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1818{
1819 int i;
1820 HV *hv = (HV *)SvRV (coro_current);
1821 AV *av = newAV ();
1822
1823 av_extend (av, items - 1);
1824 for (i = 0; i < items; ++i)
1825 av_push (av, SvREFCNT_inc_NN (arg [i]));
1826
1827 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0);
1828
1829 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1830 api_ready (aTHX_ sv_manager);
1831
1832 frame->prepare = prepare_schedule;
1833 frame->check = slf_check_repeat;
1834}
1835
1836/*****************************************************************************/
1837/* async pool handler */
1838
1839static int
1840slf_check_pool_handler (pTHX_ struct CoroSLF *frame)
1841{
1842 HV *hv = (HV *)SvRV (coro_current);
1843 struct coro *coro = (struct coro *)frame->data;
1844
1845 if (!coro->invoke_cb)
1846 return 1; /* loop till we have invoke */
1847 else
1848 {
1849 hv_store (hv, "desc", sizeof ("desc") - 1,
1850 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1851
1852 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1853
1854 {
1855 dSP;
1856 XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0;
1857 PUTBACK;
1858 }
1859
1860 SvREFCNT_dec (GvAV (PL_defgv));
1861 GvAV (PL_defgv) = coro->invoke_av;
1862 coro->invoke_av = 0;
1863
1864 return 0;
1865 }
1866}
1867
1868static void
1869slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1870{
1871 HV *hv = (HV *)SvRV (coro_current);
1872 struct coro *coro = SvSTATE_hv ((SV *)hv);
1873
1874 if (expect_true (coro->saved_deffh))
1875 {
1876 /* subsequent iteration */
1877 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1878 coro->saved_deffh = 0;
1879
1880 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
1881 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1882 {
1883 coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate);
1884 coro->invoke_av = newAV ();
1885
1886 frame->prepare = prepare_nop;
1887 }
1888 else
1889 {
1890 av_clear (GvAV (PL_defgv));
1891 hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0);
1892
1893 coro->prio = 0;
1894
1895 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1896 api_trace (aTHX_ coro_current, 0);
1897
1898 frame->prepare = prepare_schedule;
1899 av_push (av_async_pool, SvREFCNT_inc (hv));
1900 }
1901 }
1902 else
1903 {
1904 /* first iteration, simply fall through */
1905 frame->prepare = prepare_nop;
1906 }
1907
1908 frame->check = slf_check_pool_handler;
1909 frame->data = (void *)coro;
1910}
1911
1751/*****************************************************************************/ 1912/*****************************************************************************/
1752/* rouse callback */ 1913/* rouse callback */
1753 1914
1754#define CORO_MAGIC_type_rouse PERL_MAGIC_ext 1915#define CORO_MAGIC_type_rouse PERL_MAGIC_ext
1755 1916
1760 SV *data = (SV *)GENSUB_ARG; 1921 SV *data = (SV *)GENSUB_ARG;
1761 1922
1762 if (SvTYPE (SvRV (data)) != SVt_PVAV) 1923 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1763 { 1924 {
1764 /* first call, set args */ 1925 /* first call, set args */
1765 int i;
1766 AV *av = newAV (); 1926 AV *av = newAV ();
1767 SV *coro = SvRV (data); 1927 SV *coro = SvRV (data);
1768 1928
1769 SvRV_set (data, (SV *)av); 1929 SvRV_set (data, (SV *)av);
1770 api_ready (aTHX_ coro); 1930 api_ready (aTHX_ coro);
1911static void 2071static void
1912slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2072slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1913{ 2073{
1914 frame->prepare = prepare_schedule; 2074 frame->prepare = prepare_schedule;
1915 frame->check = slf_check_nop; 2075 frame->check = slf_check_nop;
2076}
2077
2078static void
2079slf_prepare_schedule_to (pTHX_ struct coro_transfer_args *ta)
2080{
2081 struct coro *next = (struct coro *)slf_frame.data;
2082
2083 SvREFCNT_inc_NN (next->hv);
2084 prepare_schedule_to (aTHX_ ta, next);
2085}
2086
2087static void
2088slf_init_schedule_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2089{
2090 if (!items)
2091 croak ("Coro::schedule_to expects a coroutine argument, caught");
2092
2093 frame->data = (void *)SvSTATE (arg [0]);
2094 frame->prepare = slf_prepare_schedule_to;
2095 frame->check = slf_check_nop;
2096}
2097
2098static void
2099slf_init_cede_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2100{
2101 api_ready (aTHX_ SvRV (coro_current));
2102
2103 slf_init_schedule_to (aTHX_ frame, cv, arg, items);
1916} 2104}
1917 2105
1918static void 2106static void
1919slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2107slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1920{ 2108{
2066 } 2254 }
2067 else 2255 else
2068 slf_argc = 0; 2256 slf_argc = 0;
2069 2257
2070 PL_op->op_ppaddr = pp_slf; 2258 PL_op->op_ppaddr = pp_slf;
2071 PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ 2259 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2072 2260
2073 PL_op = (OP *)&slf_restore; 2261 PL_op = (OP *)&slf_restore;
2074} 2262}
2075 2263
2076/*****************************************************************************/ 2264/*****************************************************************************/
2299{ 2487{
2300 if (items >= 2) 2488 if (items >= 2)
2301 { 2489 {
2302 /* callback form */ 2490 /* callback form */
2303 AV *av = (AV *)SvRV (arg [0]); 2491 AV *av = (AV *)SvRV (arg [0]);
2304 CV *cb_cv = coro_sv_2cv (arg [1]); 2492 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2305 2493
2306 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); 2494 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv));
2307 2495
2308 if (SvIVX (AvARRAY (av)[0]) > 0) 2496 if (SvIVX (AvARRAY (av)[0]) > 0)
2309 coro_semaphore_adjust (aTHX_ av, 0); 2497 coro_semaphore_adjust (aTHX_ av, 0);
2395 dXSARGS; 2583 dXSARGS;
2396 AV *state = (AV *)GENSUB_ARG; 2584 AV *state = (AV *)GENSUB_ARG;
2397 SV *coro = av_pop (state); 2585 SV *coro = av_pop (state);
2398 SV *data_sv = newSV (sizeof (struct io_state)); 2586 SV *data_sv = newSV (sizeof (struct io_state));
2399 2587
2400 av_extend (state, items); 2588 av_extend (state, items - 1);
2401 2589
2402 sv_upgrade (data_sv, SVt_PV); 2590 sv_upgrade (data_sv, SVt_PV);
2403 SvCUR_set (data_sv, sizeof (struct io_state)); 2591 SvCUR_set (data_sv, sizeof (struct io_state));
2404 SvPOK_only (data_sv); 2592 SvPOK_only (data_sv);
2405 2593
2540 XSRETURN_EMPTY; 2728 XSRETURN_EMPTY;
2541} 2729}
2542 2730
2543/*****************************************************************************/ 2731/*****************************************************************************/
2544 2732
2733#if CORO_CLONE
2734# include "clone.c"
2735#endif
2736
2545MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2737MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2546 2738
2547PROTOTYPES: DISABLE 2739PROTOTYPES: DISABLE
2548 2740
2549BOOT: 2741BOOT:
2553 coro_thx = PERL_GET_CONTEXT; 2745 coro_thx = PERL_GET_CONTEXT;
2554# endif 2746# endif
2555#endif 2747#endif
2556 BOOT_PAGESIZE; 2748 BOOT_PAGESIZE;
2557 2749
2750 cctx_current = cctx_new_empty ();
2751
2558 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 2752 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
2559 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 2753 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
2560 2754
2561 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; 2755 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
2562 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; 2756 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
2581 2775
2582 { 2776 {
2583 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); 2777 SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf)));
2584 2778
2585 if (!PL_custom_op_names) PL_custom_op_names = newHV (); 2779 if (!PL_custom_op_names) PL_custom_op_names = newHV ();
2586 hv_store_ent (PL_custom_op_names, slf, 2780 hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0);
2587 newSVpv ("coro_slf", 0), 0);
2588 2781
2589 if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); 2782 if (!PL_custom_op_descs) PL_custom_op_descs = newHV ();
2590 hv_store_ent (PL_custom_op_descs, slf, 2783 hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0);
2591 newSVpv ("coro schedule like function", 0), 0);
2592 } 2784 }
2593 2785
2594 coroapi.ver = CORO_API_VERSION; 2786 coroapi.ver = CORO_API_VERSION;
2595 coroapi.rev = CORO_API_REVISION; 2787 coroapi.rev = CORO_API_REVISION;
2596 2788
2627 CV *cb; 2819 CV *cb;
2628 int i; 2820 int i;
2629 2821
2630 if (items > 1) 2822 if (items > 1)
2631 { 2823 {
2632 cb = coro_sv_2cv (ST (1)); 2824 cb = coro_sv_2cv (aTHX_ ST (1));
2633 2825
2634 if (!ix) 2826 if (!ix)
2635 { 2827 {
2636 if (CvISXSUB (cb)) 2828 if (CvISXSUB (cb))
2637 croak ("Coro::State doesn't support XS functions as coroutine start, caught"); 2829 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
2654 mg->mg_flags |= MGf_DUP; 2846 mg->mg_flags |= MGf_DUP;
2655 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 2847 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2656 2848
2657 if (items > 1) 2849 if (items > 1)
2658 { 2850 {
2659 av_extend (coro->args, items - 1 + ix); 2851 av_extend (coro->args, items - 1 + ix - 1);
2660 2852
2661 if (ix) 2853 if (ix)
2662 { 2854 {
2663 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); 2855 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
2664 cb = cv_coro_run; 2856 cb = cv_coro_run;
2689void 2881void
2690_exit (int code) 2882_exit (int code)
2691 PROTOTYPE: $ 2883 PROTOTYPE: $
2692 CODE: 2884 CODE:
2693 _exit (code); 2885 _exit (code);
2886
2887SV *
2888clone (Coro::State coro)
2889 CODE:
2890{
2891#if CORO_CLONE
2892 struct coro *ncoro = coro_clone (coro);
2893 MAGIC *mg;
2894 /* TODO: too much duplication */
2895 ncoro->hv = newHV ();
2896 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2897 mg->mg_flags |= MGf_DUP;
2898 RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv));
2899#else
2900 croak ("Coro::State->clone has not been configured into this installation of Coro, realised");
2901#endif
2902}
2903 OUTPUT:
2904 RETVAL
2694 2905
2695int 2906int
2696cctx_stacksize (int new_stacksize = 0) 2907cctx_stacksize (int new_stacksize = 0)
2697 PROTOTYPE: ;$ 2908 PROTOTYPE: ;$
2698 CODE: 2909 CODE:
2819 3030
2820SV * 3031SV *
2821has_cctx (Coro::State coro) 3032has_cctx (Coro::State coro)
2822 PROTOTYPE: $ 3033 PROTOTYPE: $
2823 CODE: 3034 CODE:
2824 RETVAL = boolSV (!!coro->cctx); 3035 /* maybe manage the running flag differently */
3036 RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING));
2825 OUTPUT: 3037 OUTPUT:
2826 RETVAL 3038 RETVAL
2827 3039
2828int 3040int
2829is_traced (Coro::State coro) 3041is_traced (Coro::State coro)
2849 3061
2850void 3062void
2851force_cctx () 3063force_cctx ()
2852 PROTOTYPE: 3064 PROTOTYPE:
2853 CODE: 3065 CODE:
2854 SvSTATE_current->cctx->idle_sp = 0; 3066 cctx_current->idle_sp = 0;
2855 3067
2856void 3068void
2857swap_defsv (Coro::State self) 3069swap_defsv (Coro::State self)
2858 PROTOTYPE: $ 3070 PROTOTYPE: $
2859 ALIAS: 3071 ALIAS:
2874 3086
2875BOOT: 3087BOOT:
2876{ 3088{
2877 int i; 3089 int i;
2878 3090
2879 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2880 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3091 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2881 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3092 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2882 cv_coro_run = get_cv (aTHX_ "Coro::_terminate", GV_ADD); 3093 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD);
3094 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
2883 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); 3095 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
2884 SvREADONLY_on (coro_current); 3096 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3097 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3098 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3099
3100 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3101 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3102 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3103 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);
2885 3104
2886 coro_stash = gv_stashpv ("Coro", TRUE); 3105 coro_stash = gv_stashpv ("Coro", TRUE);
2887 3106
2888 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 3107 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2889 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3108 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2897 3116
2898 { 3117 {
2899 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3118 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2900 3119
2901 coroapi.schedule = api_schedule; 3120 coroapi.schedule = api_schedule;
3121 coroapi.schedule_to = api_schedule_to;
2902 coroapi.cede = api_cede; 3122 coroapi.cede = api_cede;
2903 coroapi.cede_notself = api_cede_notself; 3123 coroapi.cede_notself = api_cede_notself;
2904 coroapi.ready = api_ready; 3124 coroapi.ready = api_ready;
2905 coroapi.is_ready = api_is_ready; 3125 coroapi.is_ready = api_is_ready;
2906 coroapi.nready = coro_nready; 3126 coroapi.nready = coro_nready;
2911 SvREADONLY_on (sv); 3131 SvREADONLY_on (sv);
2912 } 3132 }
2913} 3133}
2914 3134
2915void 3135void
3136terminate (...)
3137 CODE:
3138 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3139
3140void
2916schedule (...) 3141schedule (...)
2917 CODE: 3142 CODE:
2918 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3143 CORO_EXECUTE_SLF_XS (slf_init_schedule);
3144
3145void
3146schedule_to (...)
3147 CODE:
3148 CORO_EXECUTE_SLF_XS (slf_init_schedule_to);
3149
3150void
3151cede_to (...)
3152 CODE:
3153 CORO_EXECUTE_SLF_XS (slf_init_cede_to);
2919 3154
2920void 3155void
2921cede (...) 3156cede (...)
2922 CODE: 3157 CODE:
2923 CORO_EXECUTE_SLF_XS (slf_init_cede); 3158 CORO_EXECUTE_SLF_XS (slf_init_cede);
2924 3159
2925void 3160void
2926cede_notself (...) 3161cede_notself (...)
2927 CODE: 3162 CODE:
2928 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3163 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3164
3165void
3166_cancel (Coro::State self)
3167 CODE:
3168 coro_state_destroy (aTHX_ self);
3169 coro_call_on_destroy (aTHX_ self);
2929 3170
2930void 3171void
2931_set_current (SV *current) 3172_set_current (SV *current)
2932 PROTOTYPE: $ 3173 PROTOTYPE: $
2933 CODE: 3174 CODE:
2978 CODE: 3219 CODE:
2979 RETVAL = coro_nready; 3220 RETVAL = coro_nready;
2980 OUTPUT: 3221 OUTPUT:
2981 RETVAL 3222 RETVAL
2982 3223
2983# for async_pool speedup
2984void 3224void
2985_pool_1 (SV *cb) 3225_pool_handler (...)
2986 CODE: 3226 CODE:
2987{ 3227 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
2988 HV *hv = (HV *)SvRV (coro_current);
2989 struct coro *coro = SvSTATE_hv ((SV *)hv);
2990 AV *defav = GvAV (PL_defgv);
2991 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
2992 AV *invoke_av;
2993 int i, len;
2994 3228
2995 if (!invoke) 3229void
3230async_pool (SV *cv, ...)
3231 PROTOTYPE: &@
3232 PPCODE:
3233{
3234 HV *hv = (HV *)av_pop (av_async_pool);
3235 AV *av = newAV ();
3236 SV *cb = ST (0);
3237 int i;
3238
3239 av_extend (av, items - 2);
3240 for (i = 1; i < items; ++i)
3241 av_push (av, SvREFCNT_inc_NN (ST (i)));
3242
3243 if ((SV *)hv == &PL_sv_undef)
2996 { 3244 {
2997 SV *old = PL_diehook; 3245 PUSHMARK (SP);
2998 PL_diehook = 0; 3246 EXTEND (SP, 2);
2999 SvREFCNT_dec (old); 3247 PUSHs (sv_Coro);
3000 croak ("\3async_pool terminate\2\n"); 3248 PUSHs ((SV *)cv_pool_handler);
3249 PUTBACK;
3250 call_sv ((SV *)cv_coro_state_new, G_SCALAR);
3251 SPAGAIN;
3252
3253 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs));
3001 } 3254 }
3002 3255
3003 SvREFCNT_dec (coro->saved_deffh);
3004 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
3005
3006 hv_store (hv, "desc", sizeof ("desc") - 1,
3007 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
3008
3009 invoke_av = (AV *)SvRV (invoke);
3010 len = av_len (invoke_av);
3011
3012 sv_setsv (cb, AvARRAY (invoke_av)[0]);
3013
3014 if (len > 0)
3015 { 3256 {
3016 av_fill (defav, len - 1); 3257 struct coro *coro = SvSTATE_hv (hv);
3017 for (i = 0; i < len; ++i) 3258
3018 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); 3259 assert (!coro->invoke_cb);
3260 assert (!coro->invoke_av);
3261 coro->invoke_cb = SvREFCNT_inc (cb);
3262 coro->invoke_av = av;
3019 } 3263 }
3020}
3021 3264
3022void 3265 api_ready (aTHX_ (SV *)hv);
3023_pool_2 (SV *cb)
3024 CODE:
3025{
3026 HV *hv = (HV *)SvRV (coro_current);
3027 struct coro *coro = SvSTATE_hv ((SV *)hv);
3028 3266
3029 sv_setsv (cb, &PL_sv_undef); 3267 if (GIMME_V != G_VOID)
3030 3268 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
3031 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 3269 else
3032 coro->saved_deffh = 0;
3033
3034 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
3035 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
3036 {
3037 SV *old = PL_diehook;
3038 PL_diehook = 0;
3039 SvREFCNT_dec (old); 3270 SvREFCNT_dec (hv);
3040 croak ("\3async_pool terminate\2\n");
3041 }
3042
3043 av_clear (GvAV (PL_defgv));
3044 hv_store (hv, "desc", sizeof ("desc") - 1,
3045 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
3046
3047 coro->prio = 0;
3048
3049 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
3050 api_trace (aTHX_ coro_current, 0);
3051
3052 av_push (av_async_pool, newSVsv (coro_current));
3053} 3271}
3054 3272
3055SV * 3273SV *
3056rouse_cb () 3274rouse_cb ()
3057 PROTOTYPE: 3275 PROTOTYPE:
3231 3449
3232void 3450void
3233_register (char *target, char *proto, SV *req) 3451_register (char *target, char *proto, SV *req)
3234 CODE: 3452 CODE:
3235{ 3453{
3236 CV *req_cv = coro_sv_2cv (req); 3454 CV *req_cv = coro_sv_2cv (aTHX_ req);
3237 /* newXSproto doesn't return the CV on 5.8 */ 3455 /* newXSproto doesn't return the CV on 5.8 */
3238 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); 3456 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
3239 sv_setpv ((SV *)slf_cv, proto); 3457 sv_setpv ((SV *)slf_cv, proto);
3240 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 3458 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
3241} 3459}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines