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.293 by root, Tue Nov 18 06:10:03 2008 UTC vs.
Revision 1.313 by root, Thu Nov 20 03:22:59 2008 UTC

16 16
17#ifdef WIN32 17#ifdef WIN32
18# undef setjmp 18# undef setjmp
19# undef longjmp 19# undef longjmp
20# undef _exit 20# undef _exit
21# define setjmp _setjmp // deep magic, don't ask 21# define setjmp _setjmp /* deep magic */
22#else 22#else
23# include <inttypes.h> /* most portable stdint.h */ 23# include <inttypes.h> /* most portable stdint.h */
24#endif 24#endif
25 25
26#ifdef HAVE_MMAP 26#ifdef HAVE_MMAP
55 55
56#define PERL_VERSION_ATLEAST(a,b,c) \ 56#define PERL_VERSION_ATLEAST(a,b,c) \
57 (PERL_REVISION > (a) \ 57 (PERL_REVISION > (a) \
58 || (PERL_REVISION == (a) \ 58 || (PERL_REVISION == (a) \
59 && (PERL_VERSION > (b) \ 59 && (PERL_VERSION > (b) \
60 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) 60 || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c)))))
61 61
62#if !PERL_VERSION_ATLEAST (5,6,0) 62#if !PERL_VERSION_ATLEAST (5,6,0)
63# ifndef PL_ppaddr 63# ifndef PL_ppaddr
64# define PL_ppaddr ppaddr 64# define PL_ppaddr ppaddr
65# endif 65# endif
120#endif 120#endif
121 121
122/* The next macros try to return the current stack pointer, in an as 122/* The next macros try to return the current stack pointer, in an as
123 * portable way as possible. */ 123 * portable way as possible. */
124#if __GNUC__ >= 4 124#if __GNUC__ >= 4
125# define dSTACKLEVEL int stacklevel_dummy
125# define dSTACKLEVEL void *stacklevel = __builtin_frame_address (0) 126# define STACKLEVEL __builtin_frame_address (0)
126#else 127#else
127# define dSTACKLEVEL volatile void *stacklevel = (volatile void *)&stacklevel 128# define dSTACKLEVEL volatile void *stacklevel
129# define STACKLEVEL ((void *)&stacklevel)
128#endif 130#endif
129 131
130#define IN_DESTRUCT (PL_main_cv == Nullcv) 132#define IN_DESTRUCT (PL_main_cv == Nullcv)
131 133
132#if __GNUC__ >= 3 134#if __GNUC__ >= 3
143#define expect_true(expr) expect ((expr) != 0, 1) 145#define expect_true(expr) expect ((expr) != 0, 1)
144 146
145#define NOINLINE attribute ((noinline)) 147#define NOINLINE attribute ((noinline))
146 148
147#include "CoroAPI.h" 149#include "CoroAPI.h"
150#define GCoroAPI (&coroapi) /* very sneaky */
148 151
149#ifdef USE_ITHREADS 152#ifdef USE_ITHREADS
150# if CORO_PTHREAD 153# if CORO_PTHREAD
151static void *coro_thx; 154static void *coro_thx;
152# endif 155# endif
173static HV *hv_sig; /* %SIG */ 176static HV *hv_sig; /* %SIG */
174 177
175/* async_pool helper stuff */ 178/* async_pool helper stuff */
176static SV *sv_pool_rss; 179static SV *sv_pool_rss;
177static SV *sv_pool_size; 180static SV *sv_pool_size;
181static SV *sv_async_pool_idle;
178static AV *av_async_pool; 182static AV *av_async_pool;
183static SV *sv_Coro;
184static CV *cv_pool_handler;
185static CV *cv_coro_new;
179 186
180/* Coro::AnyEvent */ 187/* Coro::AnyEvent */
181static SV *sv_activity; 188static SV *sv_activity;
182 189
183static struct coro_cctx *cctx_first; 190static struct coro_cctx *cctx_first;
243 /* state data */ 250 /* state data */
244 struct CoroSLF slf_frame; /* saved slf frame */ 251 struct CoroSLF slf_frame; /* saved slf frame */
245 AV *mainstack; 252 AV *mainstack;
246 perl_slots *slot; /* basically the saved sp */ 253 perl_slots *slot; /* basically the saved sp */
247 254
255 CV *startcv; /* the CV to execute */
248 AV *args; /* data associated with this coroutine (initial args) */ 256 AV *args; /* data associated with this coroutine (initial args) */
249 int refcnt; /* coroutines are refcounted, yes */ 257 int refcnt; /* coroutines are refcounted, yes */
250 int flags; /* CF_ flags */ 258 int flags; /* CF_ flags */
251 HV *hv; /* the perl hash associated with this coro, if any */ 259 HV *hv; /* the perl hash associated with this coro, if any */
252 void (*on_destroy)(pTHX_ struct coro *coro); 260 void (*on_destroy)(pTHX_ struct coro *coro);
253 261
254 /* statistics */ 262 /* statistics */
255 int usecount; /* number of transfers to this coro */ 263 int usecount; /* number of transfers to this coro */
256 264
257 /* coro process data */ 265 /* coro process data */
258 int prio; 266 int prio;
259 SV *throw; /* exception to be thrown */ 267 SV *except; /* exception to be thrown */
268 SV *rouse_cb;
260 269
261 /* async_pool */ 270 /* async_pool */
262 SV *saved_deffh; 271 SV *saved_deffh;
272 SV *invoke_cb;
273 AV *invoke_av;
263 274
264 /* linked list */ 275 /* linked list */
265 struct coro *next, *prev; 276 struct coro *next, *prev;
266}; 277};
267 278
270 281
271/* the following variables are effectively part of the perl context */ 282/* the following variables are effectively part of the perl context */
272/* and get copied between struct coro and these variables */ 283/* and get copied between struct coro and these variables */
273/* the mainr easonw e don't support windows process emulation */ 284/* the mainr easonw e don't support windows process emulation */
274static struct CoroSLF slf_frame; /* the current slf frame */ 285static struct CoroSLF slf_frame; /* the current slf frame */
275static SV *coro_throw;
276 286
277/** Coro ********************************************************************/ 287/** Coro ********************************************************************/
278 288
279#define PRIO_MAX 3 289#define PRIO_MAX 3
280#define PRIO_HIGH 1 290#define PRIO_HIGH 1
285 295
286/* for Coro.pm */ 296/* for Coro.pm */
287static SV *coro_current; 297static SV *coro_current;
288static SV *coro_readyhook; 298static SV *coro_readyhook;
289static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 299static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
300static CV *cv_coro_run, *cv_coro_terminate;
290static struct coro *coro_first; 301static struct coro *coro_first;
291#define coro_nready coroapi.nready 302#define coro_nready coroapi.nready
292 303
293/** lowlevel stuff **********************************************************/ 304/** lowlevel stuff **********************************************************/
294 305
318#if PERL_VERSION_ATLEAST (5,10,0) 329#if PERL_VERSION_ATLEAST (5,10,0)
319 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 330 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
320 get_hv (name, create); 331 get_hv (name, create);
321#endif 332#endif
322 return get_hv (name, create); 333 return get_hv (name, create);
334}
335
336/* may croak */
337INLINE CV *
338coro_sv_2cv (pTHX_ SV *sv)
339{
340 HV *st;
341 GV *gvp;
342 return sv_2cv (sv, &st, &gvp, 0);
323} 343}
324 344
325static AV * 345static AV *
326coro_clone_padlist (pTHX_ CV *cv) 346coro_clone_padlist (pTHX_ CV *cv)
327{ 347{
446 else 466 else
447 { 467 {
448#if CORO_PREFER_PERL_FUNCTIONS 468#if CORO_PREFER_PERL_FUNCTIONS
449 /* this is probably cleaner? but also slower! */ 469 /* this is probably cleaner? but also slower! */
450 /* in practise, it seems to be less stable */ 470 /* in practise, it seems to be less stable */
451 CV *cp = Perl_cv_clone (cv); 471 CV *cp = Perl_cv_clone (aTHX_ cv);
452 CvPADLIST (cv) = CvPADLIST (cp); 472 CvPADLIST (cv) = CvPADLIST (cp);
453 CvPADLIST (cp) = 0; 473 CvPADLIST (cp) = 0;
454 SvREFCNT_dec (cp); 474 SvREFCNT_dec (cp);
455#else 475#else
456 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 476 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv);
509 529
510 PUTBACK; 530 PUTBACK;
511 } 531 }
512 532
513 slf_frame = c->slf_frame; 533 slf_frame = c->slf_frame;
514 coro_throw = c->throw; 534 CORO_THROW = c->except;
515} 535}
516 536
517static void 537static void
518save_perl (pTHX_ Coro__State c) 538save_perl (pTHX_ Coro__State c)
519{ 539{
520 c->throw = coro_throw; 540 c->except = CORO_THROW;
521 c->slf_frame = slf_frame; 541 c->slf_frame = slf_frame;
522 542
523 { 543 {
524 dSP; 544 dSP;
525 I32 cxix = cxstack_ix; 545 I32 cxix = cxstack_ix;
600 * of perl.c:init_stacks, except that it uses less memory 620 * of perl.c:init_stacks, except that it uses less memory
601 * on the (sometimes correct) assumption that coroutines do 621 * on the (sometimes correct) assumption that coroutines do
602 * not usually need a lot of stackspace. 622 * not usually need a lot of stackspace.
603 */ 623 */
604#if CORO_PREFER_PERL_FUNCTIONS 624#if CORO_PREFER_PERL_FUNCTIONS
605# define coro_init_stacks init_stacks 625# define coro_init_stacks(thx) init_stacks ()
606#else 626#else
607static void 627static void
608coro_init_stacks (pTHX) 628coro_init_stacks (pTHX)
609{ 629{
610 PL_curstackinfo = new_stackinfo(32, 8); 630 PL_curstackinfo = new_stackinfo(32, 8);
673#if !PERL_VERSION_ATLEAST (5,10,0) 693#if !PERL_VERSION_ATLEAST (5,10,0)
674 Safefree (PL_retstack); 694 Safefree (PL_retstack);
675#endif 695#endif
676} 696}
677 697
698#define CORO_RSS \
699 rss += sizeof (SYM (curstackinfo)); \
700 rss += (SYM (curstackinfo->si_cxmax) + 1) * sizeof (PERL_CONTEXT); \
701 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (SYM (curstack))) * sizeof (SV *); \
702 rss += SYM (tmps_max) * sizeof (SV *); \
703 rss += (SYM (markstack_max) - SYM (markstack_ptr)) * sizeof (I32); \
704 rss += SYM (scopestack_max) * sizeof (I32); \
705 rss += SYM (savestack_max) * sizeof (ANY);
706
678static size_t 707static size_t
679coro_rss (pTHX_ struct coro *coro) 708coro_rss (pTHX_ struct coro *coro)
680{ 709{
681 size_t rss = sizeof (*coro); 710 size_t rss = sizeof (*coro);
682 711
683 if (coro->mainstack) 712 if (coro->mainstack)
684 { 713 {
685 perl_slots tmp_slot;
686 perl_slots *slot;
687
688 if (coro->flags & CF_RUNNING) 714 if (coro->flags & CF_RUNNING)
689 { 715 {
690 slot = &tmp_slot; 716 #define SYM(sym) PL_ ## sym
691 717 CORO_RSS;
692 #define VAR(name,type) slot->name = PL_ ## name;
693 # include "state.h"
694 #undef VAR 718 #undef SYM
695 } 719 }
696 else 720 else
697 slot = coro->slot;
698
699 if (slot)
700 { 721 {
701 rss += sizeof (slot->curstackinfo); 722 #define SYM(sym) coro->slot->sym
702 rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); 723 CORO_RSS;
703 rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); 724 #undef SYM
704 rss += slot->tmps_max * sizeof (SV *);
705 rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32);
706 rss += slot->scopestack_max * sizeof (I32);
707 rss += slot->savestack_max * sizeof (ANY);
708
709#if !PERL_VERSION_ATLEAST (5,10,0)
710 rss += slot->retstack_max * sizeof (OP *);
711#endif
712 } 725 }
713 } 726 }
714 727
715 return rss; 728 return rss;
716} 729}
857 { 870 {
858 dSP; 871 dSP;
859 UNOP myop; 872 UNOP myop;
860 873
861 Zero (&myop, 1, UNOP); 874 Zero (&myop, 1, UNOP);
862 myop.op_next = Nullop; 875 myop.op_next = Nullop;
876 myop.op_type = OP_ENTERSUB;
863 myop.op_flags = OPf_WANT_VOID; 877 myop.op_flags = OPf_WANT_VOID;
864 878
865 PUSHMARK (SP); 879 PUSHMARK (SP);
866 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 880 PUSHs ((SV *)coro->startcv);
867 PUTBACK; 881 PUTBACK;
868 PL_op = (OP *)&myop; 882 PL_op = (OP *)&myop;
869 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 883 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
870 SPAGAIN;
871 } 884 }
872 885
873 /* this newly created coroutine might be run on an existing cctx which most 886 /* this newly created coroutine might be run on an existing cctx which most
874 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. 887 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here.
875 */ 888 */
876 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 889 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
877 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ 890 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
878 891
879 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ 892 /* 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;
880 coro_setup_op.op_type = OP_CUSTOM; 894 coro_setup_op.op_type = OP_CUSTOM;
881 coro_setup_op.op_ppaddr = pp_slf; 895 coro_setup_op.op_ppaddr = pp_slf;
882 coro_setup_op.op_next = PL_op; 896 /* no flags etc. required, as an init function won't be called */
883 897
884 PL_op = (OP *)&coro_setup_op; 898 PL_op = (OP *)&coro_setup_op;
885 899
886 /* copy throw, in case it was set before coro_setup */ 900 /* copy throw, in case it was set before coro_setup */
887 coro_throw = coro->throw; 901 CORO_THROW = coro->except;
888} 902}
889 903
890static void 904static void
891coro_destruct (pTHX_ struct coro *coro) 905coro_destruct (pTHX_ struct coro *coro)
892{ 906{
916 930
917 SvREFCNT_dec (PL_diehook); 931 SvREFCNT_dec (PL_diehook);
918 SvREFCNT_dec (PL_warnhook); 932 SvREFCNT_dec (PL_warnhook);
919 933
920 SvREFCNT_dec (coro->saved_deffh); 934 SvREFCNT_dec (coro->saved_deffh);
921 SvREFCNT_dec (coro_throw); 935 SvREFCNT_dec (coro->rouse_cb);
936 SvREFCNT_dec (coro->invoke_cb);
937 SvREFCNT_dec (coro->invoke_av);
922 938
923 coro_destruct_stacks (aTHX); 939 coro_destruct_stacks (aTHX);
924} 940}
925 941
926INLINE void 942INLINE void
1013 SAVETMPS; 1029 SAVETMPS;
1014 EXTEND (SP, 3); 1030 EXTEND (SP, 3);
1015 PUSHMARK (SP); 1031 PUSHMARK (SP);
1016 PUSHs (&PL_sv_yes); 1032 PUSHs (&PL_sv_yes);
1017 PUSHs (fullname); 1033 PUSHs (fullname);
1018 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); 1034 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
1019 PUTBACK; 1035 PUTBACK;
1020 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); 1036 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
1021 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1037 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1022 SPAGAIN; 1038 SPAGAIN;
1023 FREETMPS; 1039 FREETMPS;
1070static int 1086static int
1071slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1087slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1072{ 1088{
1073 *frame = cctx_ssl_frame; 1089 *frame = cctx_ssl_frame;
1074 1090
1075 return 1; /* execute the restored frame - there must be one */ 1091 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1076} 1092}
1077 1093
1078/* initialises PL_top_env and injects a pseudo-slf-call to sett he stacklevel */ 1094/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1079static void NOINLINE 1095static void NOINLINE
1080cctx_prepare (pTHX_ coro_cctx *cctx) 1096cctx_prepare (pTHX_ coro_cctx *cctx)
1081{ 1097{
1082 PL_top_env = &PL_start_env; 1098 PL_top_env = &PL_start_env;
1083 1099
1087 /* we already must be executing an SLF op, there is no other valid way 1103 /* we already must be executing an SLF op, there is no other valid way
1088 * that can lead to creation of a new cctx */ 1104 * that can lead to creation of a new cctx */
1089 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", 1105 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1090 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1106 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1091 1107
1108 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */
1092 cctx_ssl_cctx = cctx; 1109 cctx_ssl_cctx = cctx;
1093 cctx_ssl_frame = slf_frame; 1110 cctx_ssl_frame = slf_frame;
1094 1111
1095 slf_frame.prepare = slf_prepare_set_stacklevel; 1112 slf_frame.prepare = slf_prepare_set_stacklevel;
1096 slf_frame.check = slf_check_set_stacklevel; 1113 slf_frame.check = slf_check_set_stacklevel;
1128 transfer_tail (aTHX); 1145 transfer_tail (aTHX);
1129 1146
1130 /* somebody or something will hit me for both perl_run and PL_restartop */ 1147 /* somebody or something will hit me for both perl_run and PL_restartop */
1131 PL_restartop = PL_op; 1148 PL_restartop = PL_op;
1132 perl_run (PL_curinterp); 1149 perl_run (PL_curinterp);
1150 /*
1151 * Unfortunately, there is no way to get at the return values of the
1152 * coro body here, as perl_run destroys these
1153 */
1133 1154
1134 /* 1155 /*
1135 * If perl-run returns we assume exit() was being called or the coro 1156 * If perl-run returns we assume exit() was being called or the coro
1136 * fell off the end, which seems to be the only valid (non-bug) 1157 * fell off the end, which seems to be the only valid (non-bug)
1137 * reason for perl_run to return. We try to exit by jumping to the 1158 * reason for perl_run to return. We try to exit by jumping to the
1317 dSTACKLEVEL; 1338 dSTACKLEVEL;
1318 1339
1319 /* sometimes transfer is only called to set idle_sp */ 1340 /* sometimes transfer is only called to set idle_sp */
1320 if (expect_false (!next)) 1341 if (expect_false (!next))
1321 { 1342 {
1322 ((coro_cctx *)prev)->idle_sp = (void *)stacklevel; 1343 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1323 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1344 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1324 } 1345 }
1325 else if (expect_true (prev != next)) 1346 else if (expect_true (prev != next))
1326 { 1347 {
1327 coro_cctx *prev__cctx; 1348 coro_cctx *prev__cctx;
1352 1373
1353 prev__cctx = prev->cctx; 1374 prev__cctx = prev->cctx;
1354 1375
1355 /* possibly untie and reuse the cctx */ 1376 /* possibly untie and reuse the cctx */
1356 if (expect_true ( 1377 if (expect_true (
1357 prev__cctx->idle_sp == (void *)stacklevel 1378 prev__cctx->idle_sp == STACKLEVEL
1358 && !(prev__cctx->flags & CC_TRACE) 1379 && !(prev__cctx->flags & CC_TRACE)
1359 && !force_cctx 1380 && !force_cctx
1360 )) 1381 ))
1361 { 1382 {
1362 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1383 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1429 1450
1430 coro->slot = 0; 1451 coro->slot = 0;
1431 } 1452 }
1432 1453
1433 cctx_destroy (coro->cctx); 1454 cctx_destroy (coro->cctx);
1455 SvREFCNT_dec (coro->startcv);
1434 SvREFCNT_dec (coro->args); 1456 SvREFCNT_dec (coro->args);
1457 SvREFCNT_dec (CORO_THROW);
1435 1458
1436 if (coro->next) coro->next->prev = coro->prev; 1459 if (coro->next) coro->next->prev = coro->prev;
1437 if (coro->prev) coro->prev->next = coro->next; 1460 if (coro->prev) coro->prev->next = coro->next;
1438 if (coro == coro_first) coro_first = coro->next; 1461 if (coro == coro_first) coro_first = coro->next;
1439 1462
1493 1516
1494 prepare_transfer (aTHX_ &ta, prev_sv, next_sv); 1517 prepare_transfer (aTHX_ &ta, prev_sv, next_sv);
1495 TRANSFER (ta, 1); 1518 TRANSFER (ta, 1);
1496} 1519}
1497 1520
1521/*****************************************************************************/
1522/* gensub: simple closure generation utility */
1523
1524#define GENSUB_ARG CvXSUBANY (cv).any_ptr
1525
1526/* create a closure from XS, returns a code reference */
1527/* the arg can be accessed via GENSUB_ARG from the callback */
1528/* the callback must use dXSARGS/XSRETURN */
1529static SV *
1530gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg)
1531{
1532 CV *cv = (CV *)newSV (0);
1533
1534 sv_upgrade ((SV *)cv, SVt_PVCV);
1535
1536 CvANON_on (cv);
1537 CvISXSUB_on (cv);
1538 CvXSUB (cv) = xsub;
1539 GENSUB_ARG = arg;
1540
1541 return newRV_noinc ((SV *)cv);
1542}
1543
1498/** Coro ********************************************************************/ 1544/** Coro ********************************************************************/
1499 1545
1500INLINE void 1546INLINE void
1501coro_enq (pTHX_ struct coro *coro) 1547coro_enq (pTHX_ struct coro *coro)
1502{ 1548{
1699 if (coro->flags & CF_RUNNING) 1745 if (coro->flags & CF_RUNNING)
1700 PL_runops = RUNOPS_DEFAULT; 1746 PL_runops = RUNOPS_DEFAULT;
1701 else 1747 else
1702 coro->slot->runops = RUNOPS_DEFAULT; 1748 coro->slot->runops = RUNOPS_DEFAULT;
1703 } 1749 }
1750}
1751
1752/*****************************************************************************/
1753/* async pool handler */
1754
1755static int
1756slf_check_pool_handler (pTHX_ struct CoroSLF *frame)
1757{
1758 HV *hv = (HV *)SvRV (coro_current);
1759 struct coro *coro = (struct coro *)frame->data;
1760
1761 if (!coro->invoke_cb)
1762 return 1; /* loop till we have invoke */
1763 else
1764 {
1765 hv_store (hv, "desc", sizeof ("desc") - 1,
1766 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1767
1768 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
1769
1770 {
1771 dSP;
1772 XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0;
1773 PUTBACK;
1774 }
1775
1776 SvREFCNT_dec (GvAV (PL_defgv));
1777 GvAV (PL_defgv) = coro->invoke_av;
1778 coro->invoke_av = 0;
1779
1780 return 0;
1781 }
1782}
1783
1784static void
1785slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1786{
1787 HV *hv = (HV *)SvRV (coro_current);
1788 struct coro *coro = SvSTATE_hv ((SV *)hv);
1789
1790 if (expect_true (coro->saved_deffh))
1791 {
1792 /* subsequent iteration */
1793 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
1794 coro->saved_deffh = 0;
1795
1796 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
1797 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1798 {
1799 coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate);
1800 coro->invoke_av = newAV ();
1801
1802 frame->prepare = prepare_nop;
1803 }
1804 else
1805 {
1806 av_clear (GvAV (PL_defgv));
1807 hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0);
1808
1809 coro->prio = 0;
1810
1811 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1812 api_trace (aTHX_ coro_current, 0);
1813
1814 frame->prepare = prepare_schedule;
1815 av_push (av_async_pool, SvREFCNT_inc (hv));
1816 }
1817 }
1818 else
1819 {
1820 /* first iteration, simply fall through */
1821 frame->prepare = prepare_nop;
1822 }
1823
1824 frame->check = slf_check_pool_handler;
1825 frame->data = (void *)coro;
1826}
1827
1828/*****************************************************************************/
1829/* rouse callback */
1830
1831#define CORO_MAGIC_type_rouse PERL_MAGIC_ext
1832
1833static void
1834coro_rouse_callback (pTHX_ CV *cv)
1835{
1836 dXSARGS;
1837 SV *data = (SV *)GENSUB_ARG;
1838
1839 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1840 {
1841 /* first call, set args */
1842 AV *av = newAV ();
1843 SV *coro = SvRV (data);
1844
1845 SvRV_set (data, (SV *)av);
1846 api_ready (aTHX_ coro);
1847 SvREFCNT_dec (coro);
1848
1849 /* better take a full copy of the arguments */
1850 while (items--)
1851 av_store (av, items, newSVsv (ST (items)));
1852 }
1853
1854 XSRETURN_EMPTY;
1855}
1856
1857static int
1858slf_check_rouse_wait (pTHX_ struct CoroSLF *frame)
1859{
1860 SV *data = (SV *)frame->data;
1861
1862 if (CORO_THROW)
1863 return 0;
1864
1865 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1866 return 1;
1867
1868 /* now push all results on the stack */
1869 {
1870 dSP;
1871 AV *av = (AV *)SvRV (data);
1872 int i;
1873
1874 EXTEND (SP, AvFILLp (av) + 1);
1875 for (i = 0; i <= AvFILLp (av); ++i)
1876 PUSHs (sv_2mortal (AvARRAY (av)[i]));
1877
1878 /* we have stolen the elements, so ste length to zero and free */
1879 AvFILLp (av) = -1;
1880 av_undef (av);
1881
1882 PUTBACK;
1883 }
1884
1885 return 0;
1886}
1887
1888static void
1889slf_init_rouse_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1890{
1891 SV *cb;
1892
1893 if (items)
1894 cb = arg [0];
1895 else
1896 {
1897 struct coro *coro = SvSTATE_current;
1898
1899 if (!coro->rouse_cb)
1900 croak ("Coro::rouse_wait called without rouse callback, and no default rouse callback found either,");
1901
1902 cb = sv_2mortal (coro->rouse_cb);
1903 coro->rouse_cb = 0;
1904 }
1905
1906 if (!SvROK (cb)
1907 || SvTYPE (SvRV (cb)) != SVt_PVCV
1908 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback)
1909 croak ("Coro::rouse_wait called with illegal callback argument,");
1910
1911 {
1912 CV *cv = (CV *)SvRV (cb); /* for GENSUB_ARG */
1913 SV *data = (SV *)GENSUB_ARG;
1914
1915 frame->data = (void *)data;
1916 frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule;
1917 frame->check = slf_check_rouse_wait;
1918 }
1919}
1920
1921static SV *
1922coro_new_rouse_cb (pTHX)
1923{
1924 HV *hv = (HV *)SvRV (coro_current);
1925 struct coro *coro = SvSTATE_hv (hv);
1926 SV *data = newRV_inc ((SV *)hv);
1927 SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data);
1928
1929 sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0);
1930 SvREFCNT_dec (data); /* magicext increases the refcount */
1931
1932 SvREFCNT_dec (coro->rouse_cb);
1933 coro->rouse_cb = SvREFCNT_inc_NN (cb);
1934
1935 return cb;
1704} 1936}
1705 1937
1706/*****************************************************************************/ 1938/*****************************************************************************/
1707/* schedule-like-function opcode (SLF) */ 1939/* schedule-like-function opcode (SLF) */
1708 1940
1835 } 2067 }
1836 while (slf_frame.check (aTHX_ &slf_frame)); 2068 while (slf_frame.check (aTHX_ &slf_frame));
1837 2069
1838 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ 2070 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */
1839 2071
2072 /* exception handling */
2073 if (expect_false (CORO_THROW))
2074 {
2075 SV *exception = sv_2mortal (CORO_THROW);
2076
2077 CORO_THROW = 0;
2078 sv_setsv (ERRSV, exception);
2079 croak (0);
2080 }
2081
1840 /* return value handling - mostly like entersub */ 2082 /* return value handling - mostly like entersub */
1841 /* make sure we put something on the stack in scalar context */ 2083 /* make sure we put something on the stack in scalar context */
1842 if (GIMME_V == G_SCALAR) 2084 if (GIMME_V == G_SCALAR)
1843 { 2085 {
1844 dSP; 2086 dSP;
1850 bot [1] = *sp; 2092 bot [1] = *sp;
1851 2093
1852 SP = bot + 1; 2094 SP = bot + 1;
1853 2095
1854 PUTBACK; 2096 PUTBACK;
1855 }
1856
1857 /* exception handling */
1858 if (expect_false (coro_throw))
1859 {
1860 SV *exception = sv_2mortal (coro_throw);
1861
1862 coro_throw = 0;
1863 sv_setsv (ERRSV, exception);
1864 croak (0);
1865 } 2097 }
1866 2098
1867 return NORMAL; 2099 return NORMAL;
1868} 2100}
1869 2101
1991 PerlIOBuf_get_cnt, 2223 PerlIOBuf_get_cnt,
1992 PerlIOBuf_set_ptrcnt, 2224 PerlIOBuf_set_ptrcnt,
1993}; 2225};
1994 2226
1995/*****************************************************************************/ 2227/*****************************************************************************/
2228/* Coro::Semaphore & Coro::Signal */
2229
2230static SV *
2231coro_waitarray_new (pTHX_ int count)
2232{
2233 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */
2234 AV *av = newAV ();
2235 SV **ary;
2236
2237 /* unfortunately, building manually saves memory */
2238 Newx (ary, 2, SV *);
2239 AvALLOC (av) = ary;
2240 /*AvARRAY (av) = ary;*/
2241 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */
2242 AvMAX (av) = 1;
2243 AvFILLp (av) = 0;
2244 ary [0] = newSViv (count);
2245
2246 return newRV_noinc ((SV *)av);
2247}
2248
1996/* Coro::Semaphore */ 2249/* semaphore */
1997 2250
1998static void 2251static void
1999coro_semaphore_adjust (pTHX_ AV *av, IV adjust) 2252coro_semaphore_adjust (pTHX_ AV *av, IV adjust)
2000{ 2253{
2001 SV *count_sv = AvARRAY (av)[0]; 2254 SV *count_sv = AvARRAY (av)[0];
2013 AvARRAY (av)[0] = AvARRAY (av)[1]; 2266 AvARRAY (av)[0] = AvARRAY (av)[1];
2014 AvARRAY (av)[1] = count_sv; 2267 AvARRAY (av)[1] = count_sv;
2015 cb = av_shift (av); 2268 cb = av_shift (av);
2016 2269
2017 if (SvOBJECT (cb)) 2270 if (SvOBJECT (cb))
2271 {
2018 api_ready (aTHX_ cb); 2272 api_ready (aTHX_ cb);
2019 else 2273 --count;
2020 croak ("callbacks not yet supported"); 2274 }
2275 else if (SvTYPE (cb) == SVt_PVCV)
2276 {
2277 dSP;
2278 PUSHMARK (SP);
2279 XPUSHs (sv_2mortal (newRV_inc ((SV *)av)));
2280 PUTBACK;
2281 call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR);
2282 }
2021 2283
2022 SvREFCNT_dec (cb); 2284 SvREFCNT_dec (cb);
2023
2024 --count;
2025 } 2285 }
2026} 2286}
2027 2287
2028static void 2288static void
2029coro_semaphore_on_destroy (pTHX_ struct coro *coro) 2289coro_semaphore_on_destroy (pTHX_ struct coro *coro)
2031 /* call $sem->adjust (0) to possibly wake up some other waiters */ 2291 /* call $sem->adjust (0) to possibly wake up some other waiters */
2032 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); 2292 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0);
2033} 2293}
2034 2294
2035static int 2295static int
2036slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) 2296slf_check_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, int acquire)
2037{ 2297{
2038 AV *av = (AV *)frame->data; 2298 AV *av = (AV *)frame->data;
2039 SV *count_sv = AvARRAY (av)[0]; 2299 SV *count_sv = AvARRAY (av)[0];
2040 2300
2301 /* if we are about to throw, don't actually acquire the lock, just throw */
2302 if (CORO_THROW)
2303 return 0;
2041 if (SvIVX (count_sv) > 0) 2304 else if (SvIVX (count_sv) > 0)
2042 { 2305 {
2043 SvSTATE_current->on_destroy = 0; 2306 SvSTATE_current->on_destroy = 0;
2307
2308 if (acquire)
2044 SvIVX (count_sv) = SvIVX (count_sv) - 1; 2309 SvIVX (count_sv) = SvIVX (count_sv) - 1;
2310 else
2311 coro_semaphore_adjust (aTHX_ av, 0);
2312
2045 return 0; 2313 return 0;
2046 } 2314 }
2047 else 2315 else
2048 { 2316 {
2049 int i; 2317 int i;
2058 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2326 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2059 return 1; 2327 return 1;
2060 } 2328 }
2061} 2329}
2062 2330
2063static void 2331static int
2332slf_check_semaphore_down (pTHX_ struct CoroSLF *frame)
2333{
2334 return slf_check_semaphore_down_or_wait (aTHX_ frame, 1);
2335}
2336
2337static int
2338slf_check_semaphore_wait (pTHX_ struct CoroSLF *frame)
2339{
2340 return slf_check_semaphore_down_or_wait (aTHX_ frame, 0);
2341}
2342
2343static void
2064slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2344slf_init_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2065{ 2345{
2066 AV *av = (AV *)SvRV (arg [0]); 2346 AV *av = (AV *)SvRV (arg [0]);
2067 2347
2068 if (SvIVX (AvARRAY (av)[0]) > 0) 2348 if (SvIVX (AvARRAY (av)[0]) > 0)
2069 { 2349 {
2070 frame->data = (void *)av; 2350 frame->data = (void *)av;
2071 frame->prepare = prepare_nop; 2351 frame->prepare = prepare_nop;
2072 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2073 } 2352 }
2074 else 2353 else
2075 { 2354 {
2076 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2355 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2077 2356
2078 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); 2357 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av));
2079 frame->prepare = prepare_schedule; 2358 frame->prepare = prepare_schedule;
2080 2359
2081 /* to avoid race conditions when a woken-up coro gets terminated */ 2360 /* to avoid race conditions when a woken-up coro gets terminated */
2082 /* we arrange for a temporary on_destroy that calls adjust (0) */ 2361 /* we arrange for a temporary on_destroy that calls adjust (0) */
2083 assert (!SvSTATE_current->on_destroy);//D
2084 SvSTATE_current->on_destroy = coro_semaphore_on_destroy; 2362 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2085 } 2363 }
2364}
2086 2365
2366static void
2367slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2368{
2369 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items);
2087 frame->check = slf_check_semaphore_down; 2370 frame->check = slf_check_semaphore_down;
2088
2089} 2371}
2090 2372
2091/*****************************************************************************/ 2373static void
2092/* gensub: simple closure generation utility */ 2374slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2093
2094#define GENSUB_ARG CvXSUBANY (cv).any_ptr
2095
2096/* create a closure from XS, returns a code reference */
2097/* the arg can be accessed via GENSUB_ARG from the callback */
2098/* the callback must use dXSARGS/XSRETURN */
2099static SV *
2100gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg)
2101{ 2375{
2102 CV *cv = (CV *)newSV (0); 2376 if (items >= 2)
2377 {
2378 /* callback form */
2379 AV *av = (AV *)SvRV (arg [0]);
2380 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2103 2381
2104 sv_upgrade ((SV *)cv, SVt_PVCV); 2382 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv));
2105 2383
2106 CvANON_on (cv); 2384 if (SvIVX (AvARRAY (av)[0]) > 0)
2107 CvISXSUB_on (cv); 2385 coro_semaphore_adjust (aTHX_ av, 0);
2108 CvXSUB (cv) = xsub;
2109 GENSUB_ARG = arg;
2110 2386
2111 return newRV_noinc ((SV *)cv); 2387 frame->prepare = prepare_nop;
2388 frame->check = slf_check_nop;
2389 }
2390 else
2391 {
2392 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items);
2393 frame->check = slf_check_semaphore_wait;
2394 }
2395}
2396
2397/* signal */
2398
2399static void
2400coro_signal_wake (pTHX_ AV *av, int count)
2401{
2402 SvIVX (AvARRAY (av)[0]) = 0;
2403
2404 /* now signal count waiters */
2405 while (count > 0 && AvFILLp (av) > 0)
2406 {
2407 SV *cb;
2408
2409 /* swap first two elements so we can shift a waiter */
2410 cb = AvARRAY (av)[0];
2411 AvARRAY (av)[0] = AvARRAY (av)[1];
2412 AvARRAY (av)[1] = cb;
2413
2414 cb = av_shift (av);
2415
2416 api_ready (aTHX_ cb);
2417 sv_setiv (cb, 0); /* signal waiter */
2418 SvREFCNT_dec (cb);
2419
2420 --count;
2421 }
2422}
2423
2424static int
2425slf_check_signal_wait (pTHX_ struct CoroSLF *frame)
2426{
2427 /* if we are about to throw, also stop waiting */
2428 return SvROK ((SV *)frame->data) && !CORO_THROW;
2429}
2430
2431static void
2432slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2433{
2434 AV *av = (AV *)SvRV (arg [0]);
2435
2436 if (SvIVX (AvARRAY (av)[0]))
2437 {
2438 SvIVX (AvARRAY (av)[0]) = 0;
2439 frame->prepare = prepare_nop;
2440 frame->check = slf_check_nop;
2441 }
2442 else
2443 {
2444 SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */
2445
2446 av_push (av, waiter);
2447
2448 frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */
2449 frame->prepare = prepare_schedule;
2450 frame->check = slf_check_signal_wait;
2451 }
2112} 2452}
2113 2453
2114/*****************************************************************************/ 2454/*****************************************************************************/
2115/* Coro::AIO */ 2455/* Coro::AIO */
2116 2456
2131 dXSARGS; 2471 dXSARGS;
2132 AV *state = (AV *)GENSUB_ARG; 2472 AV *state = (AV *)GENSUB_ARG;
2133 SV *coro = av_pop (state); 2473 SV *coro = av_pop (state);
2134 SV *data_sv = newSV (sizeof (struct io_state)); 2474 SV *data_sv = newSV (sizeof (struct io_state));
2135 2475
2136 av_extend (state, items); 2476 av_extend (state, items - 1);
2137 2477
2138 sv_upgrade (data_sv, SVt_PV); 2478 sv_upgrade (data_sv, SVt_PV);
2139 SvCUR_set (data_sv, sizeof (struct io_state)); 2479 SvCUR_set (data_sv, sizeof (struct io_state));
2140 SvPOK_only (data_sv); 2480 SvPOK_only (data_sv);
2141 2481
2165 2505
2166static int 2506static int
2167slf_check_aio_req (pTHX_ struct CoroSLF *frame) 2507slf_check_aio_req (pTHX_ struct CoroSLF *frame)
2168{ 2508{
2169 AV *state = (AV *)frame->data; 2509 AV *state = (AV *)frame->data;
2510
2511 /* if we are about to throw, return early */
2512 /* this does not cancel the aio request, but at least */
2513 /* it quickly returns */
2514 if (CORO_THROW)
2515 return 0;
2170 2516
2171 /* one element that is an RV? repeat! */ 2517 /* one element that is an RV? repeat! */
2172 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) 2518 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0]))
2173 return 1; 2519 return 1;
2174 2520
2345 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 2691 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
2346} 2692}
2347 2693
2348SV * 2694SV *
2349new (char *klass, ...) 2695new (char *klass, ...)
2696 ALIAS:
2697 Coro::new = 1
2350 CODE: 2698 CODE:
2351{ 2699{
2352 struct coro *coro; 2700 struct coro *coro;
2353 MAGIC *mg; 2701 MAGIC *mg;
2354 HV *hv; 2702 HV *hv;
2703 CV *cb;
2355 int i; 2704 int i;
2705
2706 if (items > 1)
2707 {
2708 cb = coro_sv_2cv (aTHX_ ST (1));
2709
2710 if (!ix)
2711 {
2712 if (CvISXSUB (cb))
2713 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
2714
2715 if (!CvROOT (cb))
2716 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
2717 }
2718 }
2356 2719
2357 Newz (0, coro, 1, struct coro); 2720 Newz (0, coro, 1, struct coro);
2358 coro->args = newAV (); 2721 coro->args = newAV ();
2359 coro->flags = CF_NEW; 2722 coro->flags = CF_NEW;
2360 2723
2365 coro->hv = hv = newHV (); 2728 coro->hv = hv = newHV ();
2366 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); 2729 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
2367 mg->mg_flags |= MGf_DUP; 2730 mg->mg_flags |= MGf_DUP;
2368 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 2731 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2369 2732
2733 if (items > 1)
2734 {
2370 av_extend (coro->args, items - 1); 2735 av_extend (coro->args, items - 1 + ix - 1);
2736
2737 if (ix)
2738 {
2739 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
2740 cb = cv_coro_run;
2741 }
2742
2743 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
2744
2371 for (i = 1; i < items; i++) 2745 for (i = 2; i < items; i++)
2372 av_push (coro->args, newSVsv (ST (i))); 2746 av_push (coro->args, newSVsv (ST (i)));
2747 }
2373} 2748}
2374 OUTPUT: 2749 OUTPUT:
2375 RETVAL 2750 RETVAL
2376 2751
2377void 2752void
2506throw (Coro::State self, SV *throw = &PL_sv_undef) 2881throw (Coro::State self, SV *throw = &PL_sv_undef)
2507 PROTOTYPE: $;$ 2882 PROTOTYPE: $;$
2508 CODE: 2883 CODE:
2509{ 2884{
2510 struct coro *current = SvSTATE_current; 2885 struct coro *current = SvSTATE_current;
2511 SV **throwp = self == current ? &coro_throw : &self->throw; 2886 SV **throwp = self == current ? &CORO_THROW : &self->except;
2512 SvREFCNT_dec (*throwp); 2887 SvREFCNT_dec (*throwp);
2513 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 2888 *throwp = SvOK (throw) ? newSVsv (throw) : 0;
2514} 2889}
2515 2890
2516void 2891void
2575 2950
2576BOOT: 2951BOOT:
2577{ 2952{
2578 int i; 2953 int i;
2579 2954
2580 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 2955 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
2581 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 2956 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
2582 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 2957 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
2583 2958 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD);
2959 cv_coro_terminate = get_cv ( "Coro::terminate", GV_ADD);
2584 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); 2960 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE);
2585 SvREADONLY_on (coro_current); 2961 SvREADONLY_on (coro_current);
2962
2963 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
2964 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
2965 cv_pool_handler = get_cv ("Coro::_pool_handler", 0); SvREADONLY_on (cv_pool_handler);
2966 cv_coro_new = get_cv ("Coro::new", 0); SvREADONLY_on (cv_coro_new);
2586 2967
2587 coro_stash = gv_stashpv ("Coro", TRUE); 2968 coro_stash = gv_stashpv ("Coro", TRUE);
2588 2969
2589 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 2970 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
2590 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 2971 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
2605 coroapi.ready = api_ready; 2986 coroapi.ready = api_ready;
2606 coroapi.is_ready = api_is_ready; 2987 coroapi.is_ready = api_is_ready;
2607 coroapi.nready = coro_nready; 2988 coroapi.nready = coro_nready;
2608 coroapi.current = coro_current; 2989 coroapi.current = coro_current;
2609 2990
2610 GCoroAPI = &coroapi; 2991 /*GCoroAPI = &coroapi;*/
2611 sv_setiv (sv, (IV)&coroapi); 2992 sv_setiv (sv, (IV)&coroapi);
2612 SvREADONLY_on (sv); 2993 SvREADONLY_on (sv);
2613 } 2994 }
2614} 2995}
2615 2996
2679 CODE: 3060 CODE:
2680 RETVAL = coro_nready; 3061 RETVAL = coro_nready;
2681 OUTPUT: 3062 OUTPUT:
2682 RETVAL 3063 RETVAL
2683 3064
2684# for async_pool speedup
2685void 3065void
2686_pool_1 (SV *cb) 3066_pool_handler (...)
2687 CODE: 3067 CODE:
2688{ 3068 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
2689 HV *hv = (HV *)SvRV (coro_current);
2690 struct coro *coro = SvSTATE_hv ((SV *)hv);
2691 AV *defav = GvAV (PL_defgv);
2692 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
2693 AV *invoke_av;
2694 int i, len;
2695 3069
2696 if (!invoke) 3070void
3071async_pool (SV *cv, ...)
3072 PROTOTYPE: &@
3073 PPCODE:
3074{
3075 HV *hv = (HV *)av_pop (av_async_pool);
3076 AV *av = newAV ();
3077 SV *cb = ST (0);
3078 int i;
3079
3080 av_extend (av, items - 2);
3081 for (i = 1; i < items; ++i)
3082 av_push (av, SvREFCNT_inc_NN (ST (i)));
3083
3084 if ((SV *)hv == &PL_sv_undef)
2697 { 3085 {
2698 SV *old = PL_diehook; 3086 PUSHMARK (SP);
2699 PL_diehook = 0; 3087 EXTEND (SP, 2);
2700 SvREFCNT_dec (old); 3088 PUSHs (sv_Coro);
2701 croak ("\3async_pool terminate\2\n"); 3089 PUSHs ((SV *)cv_pool_handler);
3090 PUTBACK;
3091 call_sv ((SV *)cv_coro_new, G_SCALAR);
3092 SPAGAIN;
3093
3094 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs));
2702 } 3095 }
2703 3096
2704 SvREFCNT_dec (coro->saved_deffh);
2705 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2706
2707 hv_store (hv, "desc", sizeof ("desc") - 1,
2708 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2709
2710 invoke_av = (AV *)SvRV (invoke);
2711 len = av_len (invoke_av);
2712
2713 sv_setsv (cb, AvARRAY (invoke_av)[0]);
2714
2715 if (len > 0)
2716 { 3097 {
2717 av_fill (defav, len - 1); 3098 struct coro *coro = SvSTATE_hv (hv);
2718 for (i = 0; i < len; ++i) 3099
2719 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); 3100 assert (!coro->invoke_cb);
3101 assert (!coro->invoke_av);
3102 coro->invoke_cb = SvREFCNT_inc (cb);
3103 coro->invoke_av = av;
2720 } 3104 }
3105
3106 api_ready (aTHX_ (SV *)hv);
3107
3108 if (GIMME_V != G_VOID)
3109 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
3110 else
3111 SvREFCNT_dec (hv);
2721} 3112}
3113
3114SV *
3115rouse_cb ()
3116 PROTOTYPE:
3117 CODE:
3118 RETVAL = coro_new_rouse_cb (aTHX);
3119 OUTPUT:
3120 RETVAL
2722 3121
2723void 3122void
2724_pool_2 (SV *cb) 3123rouse_wait (...)
3124 PROTOTYPE: ;$
2725 CODE: 3125 PPCODE:
2726{ 3126 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
2727 HV *hv = (HV *)SvRV (coro_current);
2728 struct coro *coro = SvSTATE_hv ((SV *)hv);
2729
2730 sv_setsv (cb, &PL_sv_undef);
2731
2732 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2733 coro->saved_deffh = 0;
2734
2735 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2736 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2737 {
2738 SV *old = PL_diehook;
2739 PL_diehook = 0;
2740 SvREFCNT_dec (old);
2741 croak ("\3async_pool terminate\2\n");
2742 }
2743
2744 av_clear (GvAV (PL_defgv));
2745 hv_store (hv, "desc", sizeof ("desc") - 1,
2746 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
2747
2748 coro->prio = 0;
2749
2750 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
2751 api_trace (aTHX_ coro_current, 0);
2752
2753 av_push (av_async_pool, newSVsv (coro_current));
2754}
2755 3127
2756 3128
2757MODULE = Coro::State PACKAGE = PerlIO::cede 3129MODULE = Coro::State PACKAGE = PerlIO::cede
2758 3130
2759BOOT: 3131BOOT:
2761 3133
2762 3134
2763MODULE = Coro::State PACKAGE = Coro::Semaphore 3135MODULE = Coro::State PACKAGE = Coro::Semaphore
2764 3136
2765SV * 3137SV *
2766new (SV *klass, SV *count_ = 0) 3138new (SV *klass, SV *count = 0)
2767 CODE: 3139 CODE:
2768{ 3140 RETVAL = sv_bless (
2769 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ 3141 coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1),
2770 AV *av = newAV (); 3142 GvSTASH (CvGV (cv))
2771 SV **ary; 3143 );
3144 OUTPUT:
3145 RETVAL
2772 3146
2773 /* unfortunately, building manually saves memory */ 3147# helper for Coro::Channel
2774 Newx (ary, 2, SV *); 3148SV *
2775 AvALLOC (av) = ary; 3149_alloc (int count)
2776 AvARRAY (av) = ary; 3150 CODE:
2777 AvMAX (av) = 1; 3151 RETVAL = coro_waitarray_new (aTHX_ count);
2778 AvFILLp (av) = 0;
2779 ary [0] = newSViv (count_ && SvOK (count_) ? SvIV (count_) : 1);
2780
2781 RETVAL = sv_bless (newRV_noinc ((SV *)av), GvSTASH (CvGV (cv)));
2782}
2783 OUTPUT: 3152 OUTPUT:
2784 RETVAL 3153 RETVAL
2785 3154
2786SV * 3155SV *
2787count (SV *self) 3156count (SV *self)
2796 adjust = 1 3165 adjust = 1
2797 CODE: 3166 CODE:
2798 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); 3167 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1);
2799 3168
2800void 3169void
2801down (SV *self) 3170down (...)
2802 CODE: 3171 CODE:
2803 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); 3172 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down);
3173
3174void
3175wait (...)
3176 CODE:
3177 CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait);
2804 3178
2805void 3179void
2806try (SV *self) 3180try (SV *self)
2807 PPCODE: 3181 PPCODE:
2808{ 3182{
2820 XSRETURN_NO; 3194 XSRETURN_NO;
2821} 3195}
2822 3196
2823void 3197void
2824waiters (SV *self) 3198waiters (SV *self)
2825 CODE: 3199 PPCODE:
2826{ 3200{
2827 AV *av = (AV *)SvRV (self); 3201 AV *av = (AV *)SvRV (self);
3202 int wcount = AvFILLp (av) + 1 - 1;
2828 3203
2829 if (GIMME_V == G_SCALAR) 3204 if (GIMME_V == G_SCALAR)
2830 XPUSHs (sv_2mortal (newSVsv (AvARRAY (av)[0]))); 3205 XPUSHs (sv_2mortal (newSViv (wcount)));
2831 else 3206 else
2832 { 3207 {
2833 int i; 3208 int i;
2834 EXTEND (SP, AvFILLp (av) + 1 - 1); 3209 EXTEND (SP, wcount);
2835 for (i = 1; i <= AvFILLp (av); ++i) 3210 for (i = 1; i <= wcount; ++i)
2836 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3211 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
2837 } 3212 }
2838} 3213}
3214
3215MODULE = Coro::State PACKAGE = Coro::Signal
3216
3217SV *
3218new (SV *klass)
3219 CODE:
3220 RETVAL = sv_bless (
3221 coro_waitarray_new (aTHX_ 0),
3222 GvSTASH (CvGV (cv))
3223 );
3224 OUTPUT:
3225 RETVAL
3226
3227void
3228wait (...)
3229 CODE:
3230 CORO_EXECUTE_SLF_XS (slf_init_signal_wait);
3231
3232void
3233broadcast (SV *self)
3234 CODE:
3235{
3236 AV *av = (AV *)SvRV (self);
3237 coro_signal_wake (aTHX_ av, AvFILLp (av));
3238}
3239
3240void
3241send (SV *self)
3242 CODE:
3243{
3244 AV *av = (AV *)SvRV (self);
3245
3246 if (AvFILLp (av))
3247 coro_signal_wake (aTHX_ av, 1);
3248 else
3249 SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */
3250}
3251
3252IV
3253awaited (SV *self)
3254 CODE:
3255 RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1;
3256 OUTPUT:
3257 RETVAL
2839 3258
2840 3259
2841MODULE = Coro::State PACKAGE = Coro::AnyEvent 3260MODULE = Coro::State PACKAGE = Coro::AnyEvent
2842 3261
2843BOOT: 3262BOOT:
2871 3290
2872void 3291void
2873_register (char *target, char *proto, SV *req) 3292_register (char *target, char *proto, SV *req)
2874 CODE: 3293 CODE:
2875{ 3294{
2876 HV *st; 3295 CV *req_cv = coro_sv_2cv (aTHX_ req);
2877 GV *gvp;
2878 CV *req_cv = sv_2cv (req, &st, &gvp, 0);
2879 /* newXSproto doesn't return the CV on 5.8 */ 3296 /* newXSproto doesn't return the CV on 5.8 */
2880 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); 3297 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
2881 sv_setpv ((SV *)slf_cv, proto); 3298 sv_setpv ((SV *)slf_cv, proto);
2882 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 3299 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
2883} 3300}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines