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