… | |
… | |
16 | |
16 | |
17 | #ifdef WIN32 |
17 | #ifdef WIN32 |
18 | # undef setjmp |
18 | # undef setjmp |
19 | # undef longjmp |
19 | # undef longjmp |
20 | # undef _exit |
20 | # undef _exit |
21 | # define setjmp _setjmp // deep magic, don't ask |
21 | # define setjmp _setjmp /* deep magic */ |
22 | #else |
22 | #else |
23 | # include <inttypes.h> /* most portable stdint.h */ |
23 | # include <inttypes.h> /* most portable stdint.h */ |
24 | #endif |
24 | #endif |
25 | |
25 | |
26 | #ifdef HAVE_MMAP |
26 | #ifdef HAVE_MMAP |
… | |
… | |
55 | |
55 | |
56 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
56 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
57 | (PERL_REVISION > (a) \ |
57 | (PERL_REVISION > (a) \ |
58 | || (PERL_REVISION == (a) \ |
58 | || (PERL_REVISION == (a) \ |
59 | && (PERL_VERSION > (b) \ |
59 | && (PERL_VERSION > (b) \ |
60 | || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) |
60 | || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c))))) |
61 | |
61 | |
62 | #if !PERL_VERSION_ATLEAST (5,6,0) |
62 | #if !PERL_VERSION_ATLEAST (5,6,0) |
63 | # ifndef PL_ppaddr |
63 | # ifndef PL_ppaddr |
64 | # define PL_ppaddr ppaddr |
64 | # define PL_ppaddr ppaddr |
65 | # endif |
65 | # endif |
… | |
… | |
98 | # define newSV(l) NEWSV(0,l) |
98 | # define newSV(l) NEWSV(0,l) |
99 | #endif |
99 | #endif |
100 | #ifndef CvISXSUB_on |
100 | #ifndef CvISXSUB_on |
101 | # define CvISXSUB_on(cv) (void)cv |
101 | # define CvISXSUB_on(cv) (void)cv |
102 | #endif |
102 | #endif |
|
|
103 | #ifndef CvISXSUB |
|
|
104 | # define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE) |
|
|
105 | #endif |
103 | |
106 | |
104 | /* 5.8.7 */ |
107 | /* 5.8.7 */ |
105 | #ifndef SvRV_set |
108 | #ifndef SvRV_set |
106 | # define SvRV_set(s,v) SvRV(s) = (v) |
109 | # define SvRV_set(s,v) SvRV(s) = (v) |
107 | #endif |
110 | #endif |
… | |
… | |
120 | #endif |
123 | #endif |
121 | |
124 | |
122 | /* The next macros try to return the current stack pointer, in an as |
125 | /* The next macros try to return the current stack pointer, in an as |
123 | * portable way as possible. */ |
126 | * portable way as possible. */ |
124 | #if __GNUC__ >= 4 |
127 | #if __GNUC__ >= 4 |
|
|
128 | # define dSTACKLEVEL int stacklevel_dummy |
125 | # define dSTACKLEVEL void *stacklevel = __builtin_frame_address (0) |
129 | # define STACKLEVEL __builtin_frame_address (0) |
126 | #else |
130 | #else |
127 | # define dSTACKLEVEL volatile void *stacklevel = (volatile void *)&stacklevel |
131 | # define dSTACKLEVEL volatile void *stacklevel |
|
|
132 | # define STACKLEVEL ((void *)&stacklevel) |
128 | #endif |
133 | #endif |
129 | |
134 | |
130 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
135 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
131 | |
136 | |
132 | #if __GNUC__ >= 3 |
137 | #if __GNUC__ >= 3 |
… | |
… | |
143 | #define expect_true(expr) expect ((expr) != 0, 1) |
148 | #define expect_true(expr) expect ((expr) != 0, 1) |
144 | |
149 | |
145 | #define NOINLINE attribute ((noinline)) |
150 | #define NOINLINE attribute ((noinline)) |
146 | |
151 | |
147 | #include "CoroAPI.h" |
152 | #include "CoroAPI.h" |
|
|
153 | #define GCoroAPI (&coroapi) /* very sneaky */ |
148 | |
154 | |
149 | #ifdef USE_ITHREADS |
155 | #ifdef USE_ITHREADS |
150 | # if CORO_PTHREAD |
156 | # if CORO_PTHREAD |
151 | static void *coro_thx; |
157 | static void *coro_thx; |
152 | # endif |
158 | # endif |
… | |
… | |
173 | static HV *hv_sig; /* %SIG */ |
179 | static HV *hv_sig; /* %SIG */ |
174 | |
180 | |
175 | /* async_pool helper stuff */ |
181 | /* async_pool helper stuff */ |
176 | static SV *sv_pool_rss; |
182 | static SV *sv_pool_rss; |
177 | static SV *sv_pool_size; |
183 | static SV *sv_pool_size; |
|
|
184 | static SV *sv_async_pool_idle; |
178 | static AV *av_async_pool; |
185 | static AV *av_async_pool; |
|
|
186 | static SV *sv_Coro; |
|
|
187 | static CV *cv_pool_handler; |
|
|
188 | static CV *cv_coro_state_new; |
179 | |
189 | |
180 | /* Coro::AnyEvent */ |
190 | /* Coro::AnyEvent */ |
181 | static SV *sv_activity; |
191 | static SV *sv_activity; |
182 | |
192 | |
183 | static struct coro_cctx *cctx_first; |
193 | static struct coro_cctx *cctx_first; |
… | |
… | |
243 | /* state data */ |
253 | /* state data */ |
244 | struct CoroSLF slf_frame; /* saved slf frame */ |
254 | struct CoroSLF slf_frame; /* saved slf frame */ |
245 | AV *mainstack; |
255 | AV *mainstack; |
246 | perl_slots *slot; /* basically the saved sp */ |
256 | perl_slots *slot; /* basically the saved sp */ |
247 | |
257 | |
|
|
258 | CV *startcv; /* the CV to execute */ |
248 | AV *args; /* data associated with this coroutine (initial args) */ |
259 | AV *args; /* data associated with this coroutine (initial args) */ |
249 | int refcnt; /* coroutines are refcounted, yes */ |
260 | int refcnt; /* coroutines are refcounted, yes */ |
250 | int flags; /* CF_ flags */ |
261 | int flags; /* CF_ flags */ |
251 | HV *hv; /* the perl hash associated with this coro, if any */ |
262 | HV *hv; /* the perl hash associated with this coro, if any */ |
252 | void (*on_destroy)(pTHX_ struct coro *coro); |
263 | void (*on_destroy)(pTHX_ struct coro *coro); |
253 | |
264 | |
254 | /* statistics */ |
265 | /* statistics */ |
255 | int usecount; /* number of transfers to this coro */ |
266 | int usecount; /* number of transfers to this coro */ |
256 | |
267 | |
257 | /* coro process data */ |
268 | /* coro process data */ |
258 | int prio; |
269 | int prio; |
259 | SV *throw; /* exception to be thrown */ |
270 | SV *except; /* exception to be thrown */ |
|
|
271 | SV *rouse_cb; |
260 | |
272 | |
261 | /* async_pool */ |
273 | /* async_pool */ |
262 | SV *saved_deffh; |
274 | SV *saved_deffh; |
|
|
275 | SV *invoke_cb; |
|
|
276 | AV *invoke_av; |
263 | |
277 | |
264 | /* linked list */ |
278 | /* linked list */ |
265 | struct coro *next, *prev; |
279 | struct coro *next, *prev; |
266 | }; |
280 | }; |
267 | |
281 | |
… | |
… | |
270 | |
284 | |
271 | /* the following variables are effectively part of the perl context */ |
285 | /* the following variables are effectively part of the perl context */ |
272 | /* and get copied between struct coro and these variables */ |
286 | /* and get copied between struct coro and these variables */ |
273 | /* the mainr easonw e don't support windows process emulation */ |
287 | /* the mainr easonw e don't support windows process emulation */ |
274 | static struct CoroSLF slf_frame; /* the current slf frame */ |
288 | static struct CoroSLF slf_frame; /* the current slf frame */ |
275 | static SV *coro_throw; |
|
|
276 | |
289 | |
277 | /** Coro ********************************************************************/ |
290 | /** Coro ********************************************************************/ |
278 | |
291 | |
279 | #define PRIO_MAX 3 |
292 | #define PRIO_MAX 3 |
280 | #define PRIO_HIGH 1 |
293 | #define PRIO_HIGH 1 |
… | |
… | |
285 | |
298 | |
286 | /* for Coro.pm */ |
299 | /* for Coro.pm */ |
287 | static SV *coro_current; |
300 | static SV *coro_current; |
288 | static SV *coro_readyhook; |
301 | static SV *coro_readyhook; |
289 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
302 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
|
|
303 | static CV *cv_coro_run, *cv_coro_terminate; |
290 | static struct coro *coro_first; |
304 | static struct coro *coro_first; |
291 | #define coro_nready coroapi.nready |
305 | #define coro_nready coroapi.nready |
292 | |
306 | |
293 | /** lowlevel stuff **********************************************************/ |
307 | /** lowlevel stuff **********************************************************/ |
294 | |
308 | |
… | |
… | |
318 | #if PERL_VERSION_ATLEAST (5,10,0) |
332 | #if PERL_VERSION_ATLEAST (5,10,0) |
319 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
333 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
320 | get_hv (name, create); |
334 | get_hv (name, create); |
321 | #endif |
335 | #endif |
322 | return get_hv (name, create); |
336 | return get_hv (name, create); |
|
|
337 | } |
|
|
338 | |
|
|
339 | /* may croak */ |
|
|
340 | INLINE CV * |
|
|
341 | coro_sv_2cv (pTHX_ SV *sv) |
|
|
342 | { |
|
|
343 | HV *st; |
|
|
344 | GV *gvp; |
|
|
345 | return sv_2cv (sv, &st, &gvp, 0); |
323 | } |
346 | } |
324 | |
347 | |
325 | static AV * |
348 | static AV * |
326 | coro_clone_padlist (pTHX_ CV *cv) |
349 | coro_clone_padlist (pTHX_ CV *cv) |
327 | { |
350 | { |
… | |
… | |
446 | else |
469 | else |
447 | { |
470 | { |
448 | #if CORO_PREFER_PERL_FUNCTIONS |
471 | #if CORO_PREFER_PERL_FUNCTIONS |
449 | /* this is probably cleaner? but also slower! */ |
472 | /* this is probably cleaner? but also slower! */ |
450 | /* in practise, it seems to be less stable */ |
473 | /* in practise, it seems to be less stable */ |
451 | CV *cp = Perl_cv_clone (cv); |
474 | CV *cp = Perl_cv_clone (aTHX_ cv); |
452 | CvPADLIST (cv) = CvPADLIST (cp); |
475 | CvPADLIST (cv) = CvPADLIST (cp); |
453 | CvPADLIST (cp) = 0; |
476 | CvPADLIST (cp) = 0; |
454 | SvREFCNT_dec (cp); |
477 | SvREFCNT_dec (cp); |
455 | #else |
478 | #else |
456 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
479 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
… | |
… | |
509 | |
532 | |
510 | PUTBACK; |
533 | PUTBACK; |
511 | } |
534 | } |
512 | |
535 | |
513 | slf_frame = c->slf_frame; |
536 | slf_frame = c->slf_frame; |
514 | coro_throw = c->throw; |
537 | CORO_THROW = c->except; |
515 | } |
538 | } |
516 | |
539 | |
517 | static void |
540 | static void |
518 | save_perl (pTHX_ Coro__State c) |
541 | save_perl (pTHX_ Coro__State c) |
519 | { |
542 | { |
520 | c->throw = coro_throw; |
543 | c->except = CORO_THROW; |
521 | c->slf_frame = slf_frame; |
544 | c->slf_frame = slf_frame; |
522 | |
545 | |
523 | { |
546 | { |
524 | dSP; |
547 | dSP; |
525 | I32 cxix = cxstack_ix; |
548 | I32 cxix = cxstack_ix; |
… | |
… | |
600 | * of perl.c:init_stacks, except that it uses less memory |
623 | * of perl.c:init_stacks, except that it uses less memory |
601 | * on the (sometimes correct) assumption that coroutines do |
624 | * on the (sometimes correct) assumption that coroutines do |
602 | * not usually need a lot of stackspace. |
625 | * not usually need a lot of stackspace. |
603 | */ |
626 | */ |
604 | #if CORO_PREFER_PERL_FUNCTIONS |
627 | #if CORO_PREFER_PERL_FUNCTIONS |
605 | # define coro_init_stacks init_stacks |
628 | # define coro_init_stacks(thx) init_stacks () |
606 | #else |
629 | #else |
607 | static void |
630 | static void |
608 | coro_init_stacks (pTHX) |
631 | coro_init_stacks (pTHX) |
609 | { |
632 | { |
610 | PL_curstackinfo = new_stackinfo(32, 8); |
633 | PL_curstackinfo = new_stackinfo(32, 8); |
… | |
… | |
673 | #if !PERL_VERSION_ATLEAST (5,10,0) |
696 | #if !PERL_VERSION_ATLEAST (5,10,0) |
674 | Safefree (PL_retstack); |
697 | Safefree (PL_retstack); |
675 | #endif |
698 | #endif |
676 | } |
699 | } |
677 | |
700 | |
|
|
701 | #define CORO_RSS \ |
|
|
702 | rss += sizeof (SYM (curstackinfo)); \ |
|
|
703 | rss += (SYM (curstackinfo->si_cxmax) + 1) * sizeof (PERL_CONTEXT); \ |
|
|
704 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (SYM (curstack))) * sizeof (SV *); \ |
|
|
705 | rss += SYM (tmps_max) * sizeof (SV *); \ |
|
|
706 | rss += (SYM (markstack_max) - SYM (markstack_ptr)) * sizeof (I32); \ |
|
|
707 | rss += SYM (scopestack_max) * sizeof (I32); \ |
|
|
708 | rss += SYM (savestack_max) * sizeof (ANY); |
|
|
709 | |
678 | static size_t |
710 | static size_t |
679 | coro_rss (pTHX_ struct coro *coro) |
711 | coro_rss (pTHX_ struct coro *coro) |
680 | { |
712 | { |
681 | size_t rss = sizeof (*coro); |
713 | size_t rss = sizeof (*coro); |
682 | |
714 | |
683 | if (coro->mainstack) |
715 | if (coro->mainstack) |
684 | { |
716 | { |
685 | perl_slots tmp_slot; |
|
|
686 | perl_slots *slot; |
|
|
687 | |
|
|
688 | if (coro->flags & CF_RUNNING) |
717 | if (coro->flags & CF_RUNNING) |
689 | { |
718 | { |
690 | slot = &tmp_slot; |
719 | #define SYM(sym) PL_ ## sym |
691 | |
720 | CORO_RSS; |
692 | #define VAR(name,type) slot->name = PL_ ## name; |
|
|
693 | # include "state.h" |
|
|
694 | #undef VAR |
721 | #undef SYM |
695 | } |
722 | } |
696 | else |
723 | else |
697 | slot = coro->slot; |
|
|
698 | |
|
|
699 | if (slot) |
|
|
700 | { |
724 | { |
701 | rss += sizeof (slot->curstackinfo); |
725 | #define SYM(sym) coro->slot->sym |
702 | rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
726 | CORO_RSS; |
703 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); |
727 | #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 | } |
728 | } |
713 | } |
729 | } |
714 | |
730 | |
715 | return rss; |
731 | return rss; |
716 | } |
732 | } |
… | |
… | |
857 | { |
873 | { |
858 | dSP; |
874 | dSP; |
859 | UNOP myop; |
875 | UNOP myop; |
860 | |
876 | |
861 | Zero (&myop, 1, UNOP); |
877 | Zero (&myop, 1, UNOP); |
862 | myop.op_next = Nullop; |
878 | myop.op_next = Nullop; |
|
|
879 | myop.op_type = OP_ENTERSUB; |
863 | myop.op_flags = OPf_WANT_VOID; |
880 | myop.op_flags = OPf_WANT_VOID; |
864 | |
881 | |
865 | PUSHMARK (SP); |
882 | PUSHMARK (SP); |
866 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
883 | PUSHs ((SV *)coro->startcv); |
867 | PUTBACK; |
884 | PUTBACK; |
868 | PL_op = (OP *)&myop; |
885 | PL_op = (OP *)&myop; |
869 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
886 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
870 | SPAGAIN; |
|
|
871 | } |
887 | } |
872 | |
888 | |
873 | /* this newly created coroutine might be run on an existing cctx which most |
889 | /* this newly created coroutine might be run on an existing cctx which most |
874 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
890 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
875 | */ |
891 | */ |
876 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
892 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
877 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
893 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
878 | |
894 | |
879 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
895 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
|
|
896 | coro_setup_op.op_next = PL_op; |
880 | coro_setup_op.op_type = OP_CUSTOM; |
897 | coro_setup_op.op_type = OP_CUSTOM; |
881 | coro_setup_op.op_ppaddr = pp_slf; |
898 | coro_setup_op.op_ppaddr = pp_slf; |
882 | coro_setup_op.op_next = PL_op; |
899 | /* no flags etc. required, as an init function won't be called */ |
883 | |
900 | |
884 | PL_op = (OP *)&coro_setup_op; |
901 | PL_op = (OP *)&coro_setup_op; |
885 | |
902 | |
886 | /* copy throw, in case it was set before coro_setup */ |
903 | /* copy throw, in case it was set before coro_setup */ |
887 | coro_throw = coro->throw; |
904 | CORO_THROW = coro->except; |
888 | } |
905 | } |
889 | |
906 | |
890 | static void |
907 | static void |
891 | coro_destruct (pTHX_ struct coro *coro) |
908 | coro_destruct (pTHX_ struct coro *coro) |
892 | { |
909 | { |
… | |
… | |
916 | |
933 | |
917 | SvREFCNT_dec (PL_diehook); |
934 | SvREFCNT_dec (PL_diehook); |
918 | SvREFCNT_dec (PL_warnhook); |
935 | SvREFCNT_dec (PL_warnhook); |
919 | |
936 | |
920 | SvREFCNT_dec (coro->saved_deffh); |
937 | SvREFCNT_dec (coro->saved_deffh); |
921 | SvREFCNT_dec (coro_throw); |
938 | SvREFCNT_dec (coro->rouse_cb); |
|
|
939 | SvREFCNT_dec (coro->invoke_cb); |
|
|
940 | SvREFCNT_dec (coro->invoke_av); |
922 | |
941 | |
923 | coro_destruct_stacks (aTHX); |
942 | coro_destruct_stacks (aTHX); |
924 | } |
943 | } |
925 | |
944 | |
926 | INLINE void |
945 | INLINE void |
… | |
… | |
1013 | SAVETMPS; |
1032 | SAVETMPS; |
1014 | EXTEND (SP, 3); |
1033 | EXTEND (SP, 3); |
1015 | PUSHMARK (SP); |
1034 | PUSHMARK (SP); |
1016 | PUSHs (&PL_sv_yes); |
1035 | PUSHs (&PL_sv_yes); |
1017 | PUSHs (fullname); |
1036 | PUSHs (fullname); |
1018 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
1037 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
1019 | PUTBACK; |
1038 | PUTBACK; |
1020 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
1039 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
1021 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1040 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1022 | SPAGAIN; |
1041 | SPAGAIN; |
1023 | FREETMPS; |
1042 | FREETMPS; |
… | |
… | |
1070 | static int |
1089 | static int |
1071 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1090 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1072 | { |
1091 | { |
1073 | *frame = cctx_ssl_frame; |
1092 | *frame = cctx_ssl_frame; |
1074 | |
1093 | |
1075 | return 1; |
1094 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
1076 | } |
1095 | } |
1077 | |
1096 | |
1078 | /* initialises PL_top_env and injects a pseudo-slf-call to sett he stacklevel */ |
1097 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1079 | static void NOINLINE |
1098 | static void NOINLINE |
1080 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1099 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1081 | { |
1100 | { |
1082 | PL_top_env = &PL_start_env; |
1101 | PL_top_env = &PL_start_env; |
1083 | |
1102 | |
1084 | if (cctx->flags & CC_TRACE) |
1103 | if (cctx->flags & CC_TRACE) |
1085 | PL_runops = runops_trace; |
1104 | PL_runops = runops_trace; |
1086 | |
1105 | |
1087 | /* we already must be in an SLF call, there is no other valid way |
1106 | /* we already must be executing an SLF op, there is no other valid way |
1088 | * that can lead to creation of a new cctx */ |
1107 | * that can lead to creation of a new cctx */ |
1089 | assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", |
1108 | assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", |
1090 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1109 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1091 | |
1110 | |
|
|
1111 | /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ |
1092 | cctx_ssl_cctx = cctx; |
1112 | cctx_ssl_cctx = cctx; |
1093 | cctx_ssl_frame = slf_frame; |
1113 | cctx_ssl_frame = slf_frame; |
1094 | |
1114 | |
1095 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1115 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1096 | slf_frame.check = slf_check_set_stacklevel; |
1116 | slf_frame.check = slf_check_set_stacklevel; |
… | |
… | |
1128 | transfer_tail (aTHX); |
1148 | transfer_tail (aTHX); |
1129 | |
1149 | |
1130 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1150 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1131 | PL_restartop = PL_op; |
1151 | PL_restartop = PL_op; |
1132 | perl_run (PL_curinterp); |
1152 | perl_run (PL_curinterp); |
|
|
1153 | /* |
|
|
1154 | * Unfortunately, there is no way to get at the return values of the |
|
|
1155 | * coro body here, as perl_run destroys these |
|
|
1156 | */ |
1133 | |
1157 | |
1134 | /* |
1158 | /* |
1135 | * If perl-run returns we assume exit() was being called or the coro |
1159 | * If perl-run returns we assume exit() was being called or the coro |
1136 | * fell off the end, which seems to be the only valid (non-bug) |
1160 | * fell off the end, which seems to be the only valid (non-bug) |
1137 | * reason for perl_run to return. We try to exit by jumping to the |
1161 | * reason for perl_run to return. We try to exit by jumping to the |
… | |
… | |
1317 | dSTACKLEVEL; |
1341 | dSTACKLEVEL; |
1318 | |
1342 | |
1319 | /* sometimes transfer is only called to set idle_sp */ |
1343 | /* sometimes transfer is only called to set idle_sp */ |
1320 | if (expect_false (!next)) |
1344 | if (expect_false (!next)) |
1321 | { |
1345 | { |
1322 | ((coro_cctx *)prev)->idle_sp = (void *)stacklevel; |
1346 | ((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 */ |
1347 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1324 | } |
1348 | } |
1325 | else if (expect_true (prev != next)) |
1349 | else if (expect_true (prev != next)) |
1326 | { |
1350 | { |
1327 | coro_cctx *prev__cctx; |
1351 | coro_cctx *prev__cctx; |
… | |
… | |
1352 | |
1376 | |
1353 | prev__cctx = prev->cctx; |
1377 | prev__cctx = prev->cctx; |
1354 | |
1378 | |
1355 | /* possibly untie and reuse the cctx */ |
1379 | /* possibly untie and reuse the cctx */ |
1356 | if (expect_true ( |
1380 | if (expect_true ( |
1357 | prev__cctx->idle_sp == (void *)stacklevel |
1381 | prev__cctx->idle_sp == STACKLEVEL |
1358 | && !(prev__cctx->flags & CC_TRACE) |
1382 | && !(prev__cctx->flags & CC_TRACE) |
1359 | && !force_cctx |
1383 | && !force_cctx |
1360 | )) |
1384 | )) |
1361 | { |
1385 | { |
1362 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1386 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
… | |
… | |
1429 | |
1453 | |
1430 | coro->slot = 0; |
1454 | coro->slot = 0; |
1431 | } |
1455 | } |
1432 | |
1456 | |
1433 | cctx_destroy (coro->cctx); |
1457 | cctx_destroy (coro->cctx); |
|
|
1458 | SvREFCNT_dec (coro->startcv); |
1434 | SvREFCNT_dec (coro->args); |
1459 | SvREFCNT_dec (coro->args); |
|
|
1460 | SvREFCNT_dec (CORO_THROW); |
1435 | |
1461 | |
1436 | if (coro->next) coro->next->prev = coro->prev; |
1462 | if (coro->next) coro->next->prev = coro->prev; |
1437 | if (coro->prev) coro->prev->next = coro->next; |
1463 | if (coro->prev) coro->prev->next = coro->next; |
1438 | if (coro == coro_first) coro_first = coro->next; |
1464 | if (coro == coro_first) coro_first = coro->next; |
1439 | |
1465 | |
… | |
… | |
1493 | |
1519 | |
1494 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1520 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1495 | TRANSFER (ta, 1); |
1521 | TRANSFER (ta, 1); |
1496 | } |
1522 | } |
1497 | |
1523 | |
|
|
1524 | /*****************************************************************************/ |
|
|
1525 | /* gensub: simple closure generation utility */ |
|
|
1526 | |
|
|
1527 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
|
|
1528 | |
|
|
1529 | /* create a closure from XS, returns a code reference */ |
|
|
1530 | /* the arg can be accessed via GENSUB_ARG from the callback */ |
|
|
1531 | /* the callback must use dXSARGS/XSRETURN */ |
|
|
1532 | static SV * |
|
|
1533 | gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
|
|
1534 | { |
|
|
1535 | CV *cv = (CV *)newSV (0); |
|
|
1536 | |
|
|
1537 | sv_upgrade ((SV *)cv, SVt_PVCV); |
|
|
1538 | |
|
|
1539 | CvANON_on (cv); |
|
|
1540 | CvISXSUB_on (cv); |
|
|
1541 | CvXSUB (cv) = xsub; |
|
|
1542 | GENSUB_ARG = arg; |
|
|
1543 | |
|
|
1544 | return newRV_noinc ((SV *)cv); |
|
|
1545 | } |
|
|
1546 | |
1498 | /** Coro ********************************************************************/ |
1547 | /** Coro ********************************************************************/ |
1499 | |
1548 | |
1500 | INLINE void |
1549 | INLINE void |
1501 | coro_enq (pTHX_ struct coro *coro) |
1550 | coro_enq (pTHX_ struct coro *coro) |
1502 | { |
1551 | { |
… | |
… | |
1563 | api_is_ready (pTHX_ SV *coro_sv) |
1612 | api_is_ready (pTHX_ SV *coro_sv) |
1564 | { |
1613 | { |
1565 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1614 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1566 | } |
1615 | } |
1567 | |
1616 | |
|
|
1617 | /* expects to own a reference to next->hv */ |
1568 | INLINE void |
1618 | INLINE void |
|
|
1619 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
|
|
1620 | { |
|
|
1621 | SV *prev_sv = SvRV (coro_current); |
|
|
1622 | |
|
|
1623 | ta->prev = SvSTATE_hv (prev_sv); |
|
|
1624 | ta->next = next; |
|
|
1625 | |
|
|
1626 | TRANSFER_CHECK (*ta); |
|
|
1627 | |
|
|
1628 | SvRV_set (coro_current, (SV *)next->hv); |
|
|
1629 | |
|
|
1630 | free_coro_mortal (aTHX); |
|
|
1631 | coro_mortal = prev_sv; |
|
|
1632 | } |
|
|
1633 | |
|
|
1634 | static void |
1569 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1635 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1570 | { |
1636 | { |
1571 | SV *prev_sv, *next_sv; |
|
|
1572 | |
|
|
1573 | for (;;) |
1637 | for (;;) |
1574 | { |
1638 | { |
1575 | next_sv = coro_deq (aTHX); |
1639 | SV *next_sv = coro_deq (aTHX); |
1576 | |
1640 | |
1577 | /* nothing to schedule: call the idle handler */ |
|
|
1578 | if (expect_false (!next_sv)) |
1641 | if (expect_true (next_sv)) |
1579 | { |
1642 | { |
|
|
1643 | struct coro *next = SvSTATE_hv (next_sv); |
|
|
1644 | |
|
|
1645 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1646 | if (expect_false (next->flags & CF_DESTROYED)) |
|
|
1647 | SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ |
|
|
1648 | else |
|
|
1649 | { |
|
|
1650 | next->flags &= ~CF_READY; |
|
|
1651 | --coro_nready; |
|
|
1652 | |
|
|
1653 | return prepare_schedule_to (aTHX_ ta, next); |
|
|
1654 | } |
|
|
1655 | } |
|
|
1656 | else |
|
|
1657 | { |
|
|
1658 | /* nothing to schedule: call the idle handler */ |
1580 | dSP; |
1659 | dSP; |
1581 | |
1660 | |
1582 | ENTER; |
1661 | ENTER; |
1583 | SAVETMPS; |
1662 | SAVETMPS; |
1584 | |
1663 | |
… | |
… | |
1586 | PUTBACK; |
1665 | PUTBACK; |
1587 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
1666 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
1588 | |
1667 | |
1589 | FREETMPS; |
1668 | FREETMPS; |
1590 | LEAVE; |
1669 | LEAVE; |
1591 | continue; |
|
|
1592 | } |
1670 | } |
1593 | |
|
|
1594 | ta->next = SvSTATE_hv (next_sv); |
|
|
1595 | |
|
|
1596 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1597 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
|
|
1598 | { |
|
|
1599 | SvREFCNT_dec (next_sv); |
|
|
1600 | /* coro_nready has already been taken care of by destroy */ |
|
|
1601 | continue; |
|
|
1602 | } |
|
|
1603 | |
|
|
1604 | --coro_nready; |
|
|
1605 | break; |
|
|
1606 | } |
1671 | } |
1607 | |
|
|
1608 | /* free this only after the transfer */ |
|
|
1609 | prev_sv = SvRV (coro_current); |
|
|
1610 | ta->prev = SvSTATE_hv (prev_sv); |
|
|
1611 | TRANSFER_CHECK (*ta); |
|
|
1612 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
|
|
1613 | ta->next->flags &= ~CF_READY; |
|
|
1614 | SvRV_set (coro_current, next_sv); |
|
|
1615 | |
|
|
1616 | free_coro_mortal (aTHX); |
|
|
1617 | coro_mortal = prev_sv; |
|
|
1618 | } |
1672 | } |
1619 | |
1673 | |
1620 | INLINE void |
1674 | INLINE void |
1621 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1675 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1622 | { |
1676 | { |
… | |
… | |
1643 | { |
1697 | { |
1644 | struct coro_transfer_args ta; |
1698 | struct coro_transfer_args ta; |
1645 | |
1699 | |
1646 | prepare_schedule (aTHX_ &ta); |
1700 | prepare_schedule (aTHX_ &ta); |
1647 | TRANSFER (ta, 1); |
1701 | TRANSFER (ta, 1); |
|
|
1702 | } |
|
|
1703 | |
|
|
1704 | static void |
|
|
1705 | api_schedule_to (pTHX_ SV *coro_sv) |
|
|
1706 | { |
|
|
1707 | struct coro_transfer_args ta; |
|
|
1708 | struct coro *next = SvSTATE (coro_sv); |
|
|
1709 | |
|
|
1710 | SvREFCNT_inc_NN (coro_sv); |
|
|
1711 | prepare_schedule_to (aTHX_ &ta, next); |
1648 | } |
1712 | } |
1649 | |
1713 | |
1650 | static int |
1714 | static int |
1651 | api_cede (pTHX) |
1715 | api_cede (pTHX) |
1652 | { |
1716 | { |
… | |
… | |
1699 | if (coro->flags & CF_RUNNING) |
1763 | if (coro->flags & CF_RUNNING) |
1700 | PL_runops = RUNOPS_DEFAULT; |
1764 | PL_runops = RUNOPS_DEFAULT; |
1701 | else |
1765 | else |
1702 | coro->slot->runops = RUNOPS_DEFAULT; |
1766 | coro->slot->runops = RUNOPS_DEFAULT; |
1703 | } |
1767 | } |
|
|
1768 | } |
|
|
1769 | |
|
|
1770 | /*****************************************************************************/ |
|
|
1771 | /* async pool handler */ |
|
|
1772 | |
|
|
1773 | static int |
|
|
1774 | slf_check_pool_handler (pTHX_ struct CoroSLF *frame) |
|
|
1775 | { |
|
|
1776 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1777 | struct coro *coro = (struct coro *)frame->data; |
|
|
1778 | |
|
|
1779 | if (!coro->invoke_cb) |
|
|
1780 | return 1; /* loop till we have invoke */ |
|
|
1781 | else |
|
|
1782 | { |
|
|
1783 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
1784 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
1785 | |
|
|
1786 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
|
|
1787 | |
|
|
1788 | { |
|
|
1789 | dSP; |
|
|
1790 | XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0; |
|
|
1791 | PUTBACK; |
|
|
1792 | } |
|
|
1793 | |
|
|
1794 | SvREFCNT_dec (GvAV (PL_defgv)); |
|
|
1795 | GvAV (PL_defgv) = coro->invoke_av; |
|
|
1796 | coro->invoke_av = 0; |
|
|
1797 | |
|
|
1798 | return 0; |
|
|
1799 | } |
|
|
1800 | } |
|
|
1801 | |
|
|
1802 | static void |
|
|
1803 | slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1804 | { |
|
|
1805 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1806 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
1807 | |
|
|
1808 | if (expect_true (coro->saved_deffh)) |
|
|
1809 | { |
|
|
1810 | /* subsequent iteration */ |
|
|
1811 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
|
|
1812 | coro->saved_deffh = 0; |
|
|
1813 | |
|
|
1814 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
|
|
1815 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
1816 | { |
|
|
1817 | coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate); |
|
|
1818 | coro->invoke_av = newAV (); |
|
|
1819 | |
|
|
1820 | frame->prepare = prepare_nop; |
|
|
1821 | } |
|
|
1822 | else |
|
|
1823 | { |
|
|
1824 | av_clear (GvAV (PL_defgv)); |
|
|
1825 | hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); |
|
|
1826 | |
|
|
1827 | coro->prio = 0; |
|
|
1828 | |
|
|
1829 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
|
|
1830 | api_trace (aTHX_ coro_current, 0); |
|
|
1831 | |
|
|
1832 | frame->prepare = prepare_schedule; |
|
|
1833 | av_push (av_async_pool, SvREFCNT_inc (hv)); |
|
|
1834 | } |
|
|
1835 | } |
|
|
1836 | else |
|
|
1837 | { |
|
|
1838 | /* first iteration, simply fall through */ |
|
|
1839 | frame->prepare = prepare_nop; |
|
|
1840 | } |
|
|
1841 | |
|
|
1842 | frame->check = slf_check_pool_handler; |
|
|
1843 | frame->data = (void *)coro; |
|
|
1844 | } |
|
|
1845 | |
|
|
1846 | /*****************************************************************************/ |
|
|
1847 | /* rouse callback */ |
|
|
1848 | |
|
|
1849 | #define CORO_MAGIC_type_rouse PERL_MAGIC_ext |
|
|
1850 | |
|
|
1851 | static void |
|
|
1852 | coro_rouse_callback (pTHX_ CV *cv) |
|
|
1853 | { |
|
|
1854 | dXSARGS; |
|
|
1855 | SV *data = (SV *)GENSUB_ARG; |
|
|
1856 | |
|
|
1857 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
|
|
1858 | { |
|
|
1859 | /* first call, set args */ |
|
|
1860 | AV *av = newAV (); |
|
|
1861 | SV *coro = SvRV (data); |
|
|
1862 | |
|
|
1863 | SvRV_set (data, (SV *)av); |
|
|
1864 | api_ready (aTHX_ coro); |
|
|
1865 | SvREFCNT_dec (coro); |
|
|
1866 | |
|
|
1867 | /* better take a full copy of the arguments */ |
|
|
1868 | while (items--) |
|
|
1869 | av_store (av, items, newSVsv (ST (items))); |
|
|
1870 | } |
|
|
1871 | |
|
|
1872 | XSRETURN_EMPTY; |
|
|
1873 | } |
|
|
1874 | |
|
|
1875 | static int |
|
|
1876 | slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) |
|
|
1877 | { |
|
|
1878 | SV *data = (SV *)frame->data; |
|
|
1879 | |
|
|
1880 | if (CORO_THROW) |
|
|
1881 | return 0; |
|
|
1882 | |
|
|
1883 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
|
|
1884 | return 1; |
|
|
1885 | |
|
|
1886 | /* now push all results on the stack */ |
|
|
1887 | { |
|
|
1888 | dSP; |
|
|
1889 | AV *av = (AV *)SvRV (data); |
|
|
1890 | int i; |
|
|
1891 | |
|
|
1892 | EXTEND (SP, AvFILLp (av) + 1); |
|
|
1893 | for (i = 0; i <= AvFILLp (av); ++i) |
|
|
1894 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
|
|
1895 | |
|
|
1896 | /* we have stolen the elements, so ste length to zero and free */ |
|
|
1897 | AvFILLp (av) = -1; |
|
|
1898 | av_undef (av); |
|
|
1899 | |
|
|
1900 | PUTBACK; |
|
|
1901 | } |
|
|
1902 | |
|
|
1903 | return 0; |
|
|
1904 | } |
|
|
1905 | |
|
|
1906 | static void |
|
|
1907 | slf_init_rouse_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1908 | { |
|
|
1909 | SV *cb; |
|
|
1910 | |
|
|
1911 | if (items) |
|
|
1912 | cb = arg [0]; |
|
|
1913 | else |
|
|
1914 | { |
|
|
1915 | struct coro *coro = SvSTATE_current; |
|
|
1916 | |
|
|
1917 | if (!coro->rouse_cb) |
|
|
1918 | croak ("Coro::rouse_wait called without rouse callback, and no default rouse callback found either,"); |
|
|
1919 | |
|
|
1920 | cb = sv_2mortal (coro->rouse_cb); |
|
|
1921 | coro->rouse_cb = 0; |
|
|
1922 | } |
|
|
1923 | |
|
|
1924 | if (!SvROK (cb) |
|
|
1925 | || SvTYPE (SvRV (cb)) != SVt_PVCV |
|
|
1926 | || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) |
|
|
1927 | croak ("Coro::rouse_wait called with illegal callback argument,"); |
|
|
1928 | |
|
|
1929 | { |
|
|
1930 | CV *cv = (CV *)SvRV (cb); /* for GENSUB_ARG */ |
|
|
1931 | SV *data = (SV *)GENSUB_ARG; |
|
|
1932 | |
|
|
1933 | frame->data = (void *)data; |
|
|
1934 | frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule; |
|
|
1935 | frame->check = slf_check_rouse_wait; |
|
|
1936 | } |
|
|
1937 | } |
|
|
1938 | |
|
|
1939 | static SV * |
|
|
1940 | coro_new_rouse_cb (pTHX) |
|
|
1941 | { |
|
|
1942 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1943 | struct coro *coro = SvSTATE_hv (hv); |
|
|
1944 | SV *data = newRV_inc ((SV *)hv); |
|
|
1945 | SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data); |
|
|
1946 | |
|
|
1947 | sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0); |
|
|
1948 | SvREFCNT_dec (data); /* magicext increases the refcount */ |
|
|
1949 | |
|
|
1950 | SvREFCNT_dec (coro->rouse_cb); |
|
|
1951 | coro->rouse_cb = SvREFCNT_inc_NN (cb); |
|
|
1952 | |
|
|
1953 | return cb; |
1704 | } |
1954 | } |
1705 | |
1955 | |
1706 | /*****************************************************************************/ |
1956 | /*****************************************************************************/ |
1707 | /* schedule-like-function opcode (SLF) */ |
1957 | /* schedule-like-function opcode (SLF) */ |
1708 | |
1958 | |
… | |
… | |
1755 | static void |
2005 | static void |
1756 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2006 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1757 | { |
2007 | { |
1758 | frame->prepare = prepare_schedule; |
2008 | frame->prepare = prepare_schedule; |
1759 | frame->check = slf_check_nop; |
2009 | frame->check = slf_check_nop; |
|
|
2010 | } |
|
|
2011 | |
|
|
2012 | static void |
|
|
2013 | slf_prepare_schedule_to (pTHX_ struct coro_transfer_args *ta) |
|
|
2014 | { |
|
|
2015 | struct coro *next = (struct coro *)slf_frame.data; |
|
|
2016 | |
|
|
2017 | SvREFCNT_inc_NN (next->hv); |
|
|
2018 | prepare_schedule_to (aTHX_ ta, next); |
|
|
2019 | } |
|
|
2020 | |
|
|
2021 | static void |
|
|
2022 | slf_init_schedule_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2023 | { |
|
|
2024 | if (!items) |
|
|
2025 | croak ("Coro::schedule_to expects a coroutine argument, caught"); |
|
|
2026 | |
|
|
2027 | frame->data = (void *)SvSTATE (arg [0]); |
|
|
2028 | frame->prepare = slf_prepare_schedule_to; |
|
|
2029 | frame->check = slf_check_nop; |
|
|
2030 | } |
|
|
2031 | |
|
|
2032 | static void |
|
|
2033 | slf_init_cede_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2034 | { |
|
|
2035 | api_ready (aTHX_ SvRV (coro_current)); |
|
|
2036 | |
|
|
2037 | slf_init_schedule_to (aTHX_ frame, cv, arg, items); |
1760 | } |
2038 | } |
1761 | |
2039 | |
1762 | static void |
2040 | static void |
1763 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2041 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1764 | { |
2042 | { |
… | |
… | |
1835 | } |
2113 | } |
1836 | while (slf_frame.check (aTHX_ &slf_frame)); |
2114 | while (slf_frame.check (aTHX_ &slf_frame)); |
1837 | |
2115 | |
1838 | slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ |
2116 | slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ |
1839 | |
2117 | |
|
|
2118 | /* exception handling */ |
|
|
2119 | if (expect_false (CORO_THROW)) |
|
|
2120 | { |
|
|
2121 | SV *exception = sv_2mortal (CORO_THROW); |
|
|
2122 | |
|
|
2123 | CORO_THROW = 0; |
|
|
2124 | sv_setsv (ERRSV, exception); |
|
|
2125 | croak (0); |
|
|
2126 | } |
|
|
2127 | |
1840 | /* return value handling - mostly like entersub */ |
2128 | /* return value handling - mostly like entersub */ |
1841 | /* make sure we put something on the stack in scalar context */ |
2129 | /* make sure we put something on the stack in scalar context */ |
1842 | if (GIMME_V == G_SCALAR) |
2130 | if (GIMME_V == G_SCALAR) |
1843 | { |
2131 | { |
1844 | dSP; |
2132 | dSP; |
… | |
… | |
1850 | bot [1] = *sp; |
2138 | bot [1] = *sp; |
1851 | |
2139 | |
1852 | SP = bot + 1; |
2140 | SP = bot + 1; |
1853 | |
2141 | |
1854 | PUTBACK; |
2142 | 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 | } |
2143 | } |
1866 | |
2144 | |
1867 | return NORMAL; |
2145 | return NORMAL; |
1868 | } |
2146 | } |
1869 | |
2147 | |
… | |
… | |
1991 | PerlIOBuf_get_cnt, |
2269 | PerlIOBuf_get_cnt, |
1992 | PerlIOBuf_set_ptrcnt, |
2270 | PerlIOBuf_set_ptrcnt, |
1993 | }; |
2271 | }; |
1994 | |
2272 | |
1995 | /*****************************************************************************/ |
2273 | /*****************************************************************************/ |
|
|
2274 | /* Coro::Semaphore & Coro::Signal */ |
|
|
2275 | |
|
|
2276 | static SV * |
|
|
2277 | coro_waitarray_new (pTHX_ int count) |
|
|
2278 | { |
|
|
2279 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
|
|
2280 | AV *av = newAV (); |
|
|
2281 | SV **ary; |
|
|
2282 | |
|
|
2283 | /* unfortunately, building manually saves memory */ |
|
|
2284 | Newx (ary, 2, SV *); |
|
|
2285 | AvALLOC (av) = ary; |
|
|
2286 | /*AvARRAY (av) = ary;*/ |
|
|
2287 | SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ |
|
|
2288 | AvMAX (av) = 1; |
|
|
2289 | AvFILLp (av) = 0; |
|
|
2290 | ary [0] = newSViv (count); |
|
|
2291 | |
|
|
2292 | return newRV_noinc ((SV *)av); |
|
|
2293 | } |
|
|
2294 | |
1996 | /* Coro::Semaphore */ |
2295 | /* semaphore */ |
1997 | |
2296 | |
1998 | static void |
2297 | static void |
1999 | coro_semaphore_adjust (pTHX_ AV *av, IV adjust) |
2298 | coro_semaphore_adjust (pTHX_ AV *av, IV adjust) |
2000 | { |
2299 | { |
2001 | SV *count_sv = AvARRAY (av)[0]; |
2300 | SV *count_sv = AvARRAY (av)[0]; |
… | |
… | |
2013 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2312 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2014 | AvARRAY (av)[1] = count_sv; |
2313 | AvARRAY (av)[1] = count_sv; |
2015 | cb = av_shift (av); |
2314 | cb = av_shift (av); |
2016 | |
2315 | |
2017 | if (SvOBJECT (cb)) |
2316 | if (SvOBJECT (cb)) |
|
|
2317 | { |
2018 | api_ready (aTHX_ cb); |
2318 | api_ready (aTHX_ cb); |
2019 | else |
2319 | --count; |
2020 | croak ("callbacks not yet supported"); |
2320 | } |
|
|
2321 | else if (SvTYPE (cb) == SVt_PVCV) |
|
|
2322 | { |
|
|
2323 | dSP; |
|
|
2324 | PUSHMARK (SP); |
|
|
2325 | XPUSHs (sv_2mortal (newRV_inc ((SV *)av))); |
|
|
2326 | PUTBACK; |
|
|
2327 | call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR); |
|
|
2328 | } |
2021 | |
2329 | |
2022 | SvREFCNT_dec (cb); |
2330 | SvREFCNT_dec (cb); |
2023 | |
|
|
2024 | --count; |
|
|
2025 | } |
2331 | } |
2026 | } |
2332 | } |
2027 | |
2333 | |
2028 | static void |
2334 | static void |
2029 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
2335 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
… | |
… | |
2031 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
2337 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
2032 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
2338 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
2033 | } |
2339 | } |
2034 | |
2340 | |
2035 | static int |
2341 | static int |
2036 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
2342 | slf_check_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, int acquire) |
2037 | { |
2343 | { |
2038 | AV *av = (AV *)frame->data; |
2344 | AV *av = (AV *)frame->data; |
2039 | SV *count_sv = AvARRAY (av)[0]; |
2345 | SV *count_sv = AvARRAY (av)[0]; |
2040 | |
2346 | |
|
|
2347 | /* if we are about to throw, don't actually acquire the lock, just throw */ |
|
|
2348 | if (CORO_THROW) |
|
|
2349 | return 0; |
2041 | if (SvIVX (count_sv) > 0) |
2350 | else if (SvIVX (count_sv) > 0) |
2042 | { |
2351 | { |
2043 | SvSTATE_current->on_destroy = 0; |
2352 | SvSTATE_current->on_destroy = 0; |
|
|
2353 | |
|
|
2354 | if (acquire) |
2044 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2355 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
|
|
2356 | else |
|
|
2357 | coro_semaphore_adjust (aTHX_ av, 0); |
|
|
2358 | |
2045 | return 0; |
2359 | return 0; |
2046 | } |
2360 | } |
2047 | else |
2361 | else |
2048 | { |
2362 | { |
2049 | int i; |
2363 | int i; |
… | |
… | |
2058 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2372 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2059 | return 1; |
2373 | return 1; |
2060 | } |
2374 | } |
2061 | } |
2375 | } |
2062 | |
2376 | |
2063 | static void |
2377 | static int |
|
|
2378 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
|
|
2379 | { |
|
|
2380 | return slf_check_semaphore_down_or_wait (aTHX_ frame, 1); |
|
|
2381 | } |
|
|
2382 | |
|
|
2383 | static int |
|
|
2384 | slf_check_semaphore_wait (pTHX_ struct CoroSLF *frame) |
|
|
2385 | { |
|
|
2386 | return slf_check_semaphore_down_or_wait (aTHX_ frame, 0); |
|
|
2387 | } |
|
|
2388 | |
|
|
2389 | static void |
2064 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2390 | slf_init_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2065 | { |
2391 | { |
2066 | AV *av = (AV *)SvRV (arg [0]); |
2392 | AV *av = (AV *)SvRV (arg [0]); |
2067 | |
2393 | |
2068 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2394 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2069 | { |
2395 | { |
2070 | frame->data = (void *)av; |
2396 | frame->data = (void *)av; |
2071 | frame->prepare = prepare_nop; |
2397 | frame->prepare = prepare_nop; |
2072 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
|
|
2073 | } |
2398 | } |
2074 | else |
2399 | else |
2075 | { |
2400 | { |
2076 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2401 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2077 | |
2402 | |
2078 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2403 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2079 | frame->prepare = prepare_schedule; |
2404 | frame->prepare = prepare_schedule; |
2080 | |
2405 | |
2081 | /* to avoid race conditions when a woken-up coro gets terminated */ |
2406 | /* to avoid race conditions when a woken-up coro gets terminated */ |
2082 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
2407 | /* 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; |
2408 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
2085 | } |
2409 | } |
|
|
2410 | } |
2086 | |
2411 | |
|
|
2412 | static void |
|
|
2413 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2414 | { |
|
|
2415 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
2087 | frame->check = slf_check_semaphore_down; |
2416 | frame->check = slf_check_semaphore_down; |
2088 | |
|
|
2089 | } |
2417 | } |
2090 | |
2418 | |
2091 | /*****************************************************************************/ |
2419 | static void |
2092 | /* gensub: simple closure generation utility */ |
2420 | slf_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 */ |
|
|
2099 | static SV * |
|
|
2100 | gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
|
|
2101 | { |
2421 | { |
2102 | CV *cv = (CV *)newSV (0); |
2422 | if (items >= 2) |
|
|
2423 | { |
|
|
2424 | /* callback form */ |
|
|
2425 | AV *av = (AV *)SvRV (arg [0]); |
|
|
2426 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
2103 | |
2427 | |
2104 | sv_upgrade ((SV *)cv, SVt_PVCV); |
2428 | av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); |
2105 | |
2429 | |
2106 | CvANON_on (cv); |
2430 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2107 | CvISXSUB_on (cv); |
2431 | coro_semaphore_adjust (aTHX_ av, 0); |
2108 | CvXSUB (cv) = xsub; |
|
|
2109 | GENSUB_ARG = arg; |
|
|
2110 | |
2432 | |
2111 | return newRV_noinc ((SV *)cv); |
2433 | frame->prepare = prepare_nop; |
|
|
2434 | frame->check = slf_check_nop; |
|
|
2435 | } |
|
|
2436 | else |
|
|
2437 | { |
|
|
2438 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
|
|
2439 | frame->check = slf_check_semaphore_wait; |
|
|
2440 | } |
|
|
2441 | } |
|
|
2442 | |
|
|
2443 | /* signal */ |
|
|
2444 | |
|
|
2445 | static void |
|
|
2446 | coro_signal_wake (pTHX_ AV *av, int count) |
|
|
2447 | { |
|
|
2448 | SvIVX (AvARRAY (av)[0]) = 0; |
|
|
2449 | |
|
|
2450 | /* now signal count waiters */ |
|
|
2451 | while (count > 0 && AvFILLp (av) > 0) |
|
|
2452 | { |
|
|
2453 | SV *cb; |
|
|
2454 | |
|
|
2455 | /* swap first two elements so we can shift a waiter */ |
|
|
2456 | cb = AvARRAY (av)[0]; |
|
|
2457 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
|
|
2458 | AvARRAY (av)[1] = cb; |
|
|
2459 | |
|
|
2460 | cb = av_shift (av); |
|
|
2461 | |
|
|
2462 | api_ready (aTHX_ cb); |
|
|
2463 | sv_setiv (cb, 0); /* signal waiter */ |
|
|
2464 | SvREFCNT_dec (cb); |
|
|
2465 | |
|
|
2466 | --count; |
|
|
2467 | } |
|
|
2468 | } |
|
|
2469 | |
|
|
2470 | static int |
|
|
2471 | slf_check_signal_wait (pTHX_ struct CoroSLF *frame) |
|
|
2472 | { |
|
|
2473 | /* if we are about to throw, also stop waiting */ |
|
|
2474 | return SvROK ((SV *)frame->data) && !CORO_THROW; |
|
|
2475 | } |
|
|
2476 | |
|
|
2477 | static void |
|
|
2478 | slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2479 | { |
|
|
2480 | AV *av = (AV *)SvRV (arg [0]); |
|
|
2481 | |
|
|
2482 | if (SvIVX (AvARRAY (av)[0])) |
|
|
2483 | { |
|
|
2484 | SvIVX (AvARRAY (av)[0]) = 0; |
|
|
2485 | frame->prepare = prepare_nop; |
|
|
2486 | frame->check = slf_check_nop; |
|
|
2487 | } |
|
|
2488 | else |
|
|
2489 | { |
|
|
2490 | SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */ |
|
|
2491 | |
|
|
2492 | av_push (av, waiter); |
|
|
2493 | |
|
|
2494 | frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ |
|
|
2495 | frame->prepare = prepare_schedule; |
|
|
2496 | frame->check = slf_check_signal_wait; |
|
|
2497 | } |
2112 | } |
2498 | } |
2113 | |
2499 | |
2114 | /*****************************************************************************/ |
2500 | /*****************************************************************************/ |
2115 | /* Coro::AIO */ |
2501 | /* Coro::AIO */ |
2116 | |
2502 | |
… | |
… | |
2131 | dXSARGS; |
2517 | dXSARGS; |
2132 | AV *state = (AV *)GENSUB_ARG; |
2518 | AV *state = (AV *)GENSUB_ARG; |
2133 | SV *coro = av_pop (state); |
2519 | SV *coro = av_pop (state); |
2134 | SV *data_sv = newSV (sizeof (struct io_state)); |
2520 | SV *data_sv = newSV (sizeof (struct io_state)); |
2135 | |
2521 | |
2136 | av_extend (state, items); |
2522 | av_extend (state, items - 1); |
2137 | |
2523 | |
2138 | sv_upgrade (data_sv, SVt_PV); |
2524 | sv_upgrade (data_sv, SVt_PV); |
2139 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2525 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2140 | SvPOK_only (data_sv); |
2526 | SvPOK_only (data_sv); |
2141 | |
2527 | |
… | |
… | |
2165 | |
2551 | |
2166 | static int |
2552 | static int |
2167 | slf_check_aio_req (pTHX_ struct CoroSLF *frame) |
2553 | slf_check_aio_req (pTHX_ struct CoroSLF *frame) |
2168 | { |
2554 | { |
2169 | AV *state = (AV *)frame->data; |
2555 | AV *state = (AV *)frame->data; |
|
|
2556 | |
|
|
2557 | /* if we are about to throw, return early */ |
|
|
2558 | /* this does not cancel the aio request, but at least */ |
|
|
2559 | /* it quickly returns */ |
|
|
2560 | if (CORO_THROW) |
|
|
2561 | return 0; |
2170 | |
2562 | |
2171 | /* one element that is an RV? repeat! */ |
2563 | /* one element that is an RV? repeat! */ |
2172 | if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) |
2564 | if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) |
2173 | return 1; |
2565 | return 1; |
2174 | |
2566 | |
… | |
… | |
2345 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2737 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2346 | } |
2738 | } |
2347 | |
2739 | |
2348 | SV * |
2740 | SV * |
2349 | new (char *klass, ...) |
2741 | new (char *klass, ...) |
|
|
2742 | ALIAS: |
|
|
2743 | Coro::new = 1 |
2350 | CODE: |
2744 | CODE: |
2351 | { |
2745 | { |
2352 | struct coro *coro; |
2746 | struct coro *coro; |
2353 | MAGIC *mg; |
2747 | MAGIC *mg; |
2354 | HV *hv; |
2748 | HV *hv; |
|
|
2749 | CV *cb; |
2355 | int i; |
2750 | int i; |
|
|
2751 | |
|
|
2752 | if (items > 1) |
|
|
2753 | { |
|
|
2754 | cb = coro_sv_2cv (aTHX_ ST (1)); |
|
|
2755 | |
|
|
2756 | if (!ix) |
|
|
2757 | { |
|
|
2758 | if (CvISXSUB (cb)) |
|
|
2759 | croak ("Coro::State doesn't support XS functions as coroutine start, caught"); |
|
|
2760 | |
|
|
2761 | if (!CvROOT (cb)) |
|
|
2762 | croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); |
|
|
2763 | } |
|
|
2764 | } |
2356 | |
2765 | |
2357 | Newz (0, coro, 1, struct coro); |
2766 | Newz (0, coro, 1, struct coro); |
2358 | coro->args = newAV (); |
2767 | coro->args = newAV (); |
2359 | coro->flags = CF_NEW; |
2768 | coro->flags = CF_NEW; |
2360 | |
2769 | |
… | |
… | |
2365 | coro->hv = hv = newHV (); |
2774 | coro->hv = hv = newHV (); |
2366 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2775 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2367 | mg->mg_flags |= MGf_DUP; |
2776 | mg->mg_flags |= MGf_DUP; |
2368 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2777 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2369 | |
2778 | |
|
|
2779 | if (items > 1) |
|
|
2780 | { |
2370 | av_extend (coro->args, items - 1); |
2781 | av_extend (coro->args, items - 1 + ix - 1); |
|
|
2782 | |
|
|
2783 | if (ix) |
|
|
2784 | { |
|
|
2785 | av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); |
|
|
2786 | cb = cv_coro_run; |
|
|
2787 | } |
|
|
2788 | |
|
|
2789 | coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); |
|
|
2790 | |
2371 | for (i = 1; i < items; i++) |
2791 | for (i = 2; i < items; i++) |
2372 | av_push (coro->args, newSVsv (ST (i))); |
2792 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2793 | } |
2373 | } |
2794 | } |
2374 | OUTPUT: |
2795 | OUTPUT: |
2375 | RETVAL |
2796 | RETVAL |
2376 | |
2797 | |
2377 | void |
2798 | void |
… | |
… | |
2506 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
2927 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
2507 | PROTOTYPE: $;$ |
2928 | PROTOTYPE: $;$ |
2508 | CODE: |
2929 | CODE: |
2509 | { |
2930 | { |
2510 | struct coro *current = SvSTATE_current; |
2931 | struct coro *current = SvSTATE_current; |
2511 | SV **throwp = self == current ? &coro_throw : &self->throw; |
2932 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
2512 | SvREFCNT_dec (*throwp); |
2933 | SvREFCNT_dec (*throwp); |
2513 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
2934 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
2514 | } |
2935 | } |
2515 | |
2936 | |
2516 | void |
2937 | void |
… | |
… | |
2575 | |
2996 | |
2576 | BOOT: |
2997 | BOOT: |
2577 | { |
2998 | { |
2578 | int i; |
2999 | int i; |
2579 | |
3000 | |
2580 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3001 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
2581 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3002 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2582 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3003 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2583 | |
3004 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
|
|
3005 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
2584 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
3006 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
2585 | SvREADONLY_on (coro_current); |
3007 | |
|
|
3008 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
|
|
3009 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
|
|
3010 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
|
|
3011 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
2586 | |
3012 | |
2587 | coro_stash = gv_stashpv ("Coro", TRUE); |
3013 | coro_stash = gv_stashpv ("Coro", TRUE); |
2588 | |
3014 | |
2589 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
3015 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
2590 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
3016 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
… | |
… | |
2598 | |
3024 | |
2599 | { |
3025 | { |
2600 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3026 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
2601 | |
3027 | |
2602 | coroapi.schedule = api_schedule; |
3028 | coroapi.schedule = api_schedule; |
|
|
3029 | coroapi.schedule_to = api_schedule_to; |
2603 | coroapi.cede = api_cede; |
3030 | coroapi.cede = api_cede; |
2604 | coroapi.cede_notself = api_cede_notself; |
3031 | coroapi.cede_notself = api_cede_notself; |
2605 | coroapi.ready = api_ready; |
3032 | coroapi.ready = api_ready; |
2606 | coroapi.is_ready = api_is_ready; |
3033 | coroapi.is_ready = api_is_ready; |
2607 | coroapi.nready = coro_nready; |
3034 | coroapi.nready = coro_nready; |
2608 | coroapi.current = coro_current; |
3035 | coroapi.current = coro_current; |
2609 | |
3036 | |
2610 | GCoroAPI = &coroapi; |
3037 | /*GCoroAPI = &coroapi;*/ |
2611 | sv_setiv (sv, (IV)&coroapi); |
3038 | sv_setiv (sv, (IV)&coroapi); |
2612 | SvREADONLY_on (sv); |
3039 | SvREADONLY_on (sv); |
2613 | } |
3040 | } |
2614 | } |
3041 | } |
2615 | |
3042 | |
2616 | void |
3043 | void |
2617 | schedule (...) |
3044 | schedule (...) |
2618 | CODE: |
3045 | CODE: |
2619 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
3046 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
|
|
3047 | |
|
|
3048 | void |
|
|
3049 | schedule_to (...) |
|
|
3050 | CODE: |
|
|
3051 | CORO_EXECUTE_SLF_XS (slf_init_schedule_to); |
|
|
3052 | |
|
|
3053 | void |
|
|
3054 | cede_to (...) |
|
|
3055 | CODE: |
|
|
3056 | CORO_EXECUTE_SLF_XS (slf_init_cede_to); |
2620 | |
3057 | |
2621 | void |
3058 | void |
2622 | cede (...) |
3059 | cede (...) |
2623 | CODE: |
3060 | CODE: |
2624 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
3061 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
… | |
… | |
2679 | CODE: |
3116 | CODE: |
2680 | RETVAL = coro_nready; |
3117 | RETVAL = coro_nready; |
2681 | OUTPUT: |
3118 | OUTPUT: |
2682 | RETVAL |
3119 | RETVAL |
2683 | |
3120 | |
2684 | # for async_pool speedup |
|
|
2685 | void |
3121 | void |
2686 | _pool_1 (SV *cb) |
3122 | _pool_handler (...) |
2687 | CODE: |
3123 | CODE: |
2688 | { |
3124 | 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 | |
3125 | |
2696 | if (!invoke) |
3126 | void |
|
|
3127 | async_pool (SV *cv, ...) |
|
|
3128 | PROTOTYPE: &@ |
|
|
3129 | PPCODE: |
|
|
3130 | { |
|
|
3131 | HV *hv = (HV *)av_pop (av_async_pool); |
|
|
3132 | AV *av = newAV (); |
|
|
3133 | SV *cb = ST (0); |
|
|
3134 | int i; |
|
|
3135 | |
|
|
3136 | av_extend (av, items - 2); |
|
|
3137 | for (i = 1; i < items; ++i) |
|
|
3138 | av_push (av, SvREFCNT_inc_NN (ST (i))); |
|
|
3139 | |
|
|
3140 | if ((SV *)hv == &PL_sv_undef) |
2697 | { |
3141 | { |
2698 | SV *old = PL_diehook; |
3142 | PUSHMARK (SP); |
2699 | PL_diehook = 0; |
3143 | EXTEND (SP, 2); |
2700 | SvREFCNT_dec (old); |
3144 | PUSHs (sv_Coro); |
2701 | croak ("\3async_pool terminate\2\n"); |
3145 | PUSHs ((SV *)cv_pool_handler); |
|
|
3146 | PUTBACK; |
|
|
3147 | call_sv ((SV *)cv_coro_state_new, G_SCALAR); |
|
|
3148 | SPAGAIN; |
|
|
3149 | |
|
|
3150 | hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); |
2702 | } |
3151 | } |
2703 | |
3152 | |
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 | { |
3153 | { |
2717 | av_fill (defav, len - 1); |
3154 | struct coro *coro = SvSTATE_hv (hv); |
2718 | for (i = 0; i < len; ++i) |
3155 | |
2719 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
3156 | assert (!coro->invoke_cb); |
|
|
3157 | assert (!coro->invoke_av); |
|
|
3158 | coro->invoke_cb = SvREFCNT_inc (cb); |
|
|
3159 | coro->invoke_av = av; |
2720 | } |
3160 | } |
|
|
3161 | |
|
|
3162 | api_ready (aTHX_ (SV *)hv); |
|
|
3163 | |
|
|
3164 | if (GIMME_V != G_VOID) |
|
|
3165 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); |
|
|
3166 | else |
|
|
3167 | SvREFCNT_dec (hv); |
2721 | } |
3168 | } |
|
|
3169 | |
|
|
3170 | SV * |
|
|
3171 | rouse_cb () |
|
|
3172 | PROTOTYPE: |
|
|
3173 | CODE: |
|
|
3174 | RETVAL = coro_new_rouse_cb (aTHX); |
|
|
3175 | OUTPUT: |
|
|
3176 | RETVAL |
2722 | |
3177 | |
2723 | void |
3178 | void |
2724 | _pool_2 (SV *cb) |
3179 | rouse_wait (...) |
|
|
3180 | PROTOTYPE: ;$ |
2725 | CODE: |
3181 | PPCODE: |
2726 | { |
3182 | 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 | |
3183 | |
2756 | |
3184 | |
2757 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3185 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2758 | |
3186 | |
2759 | BOOT: |
3187 | BOOT: |
… | |
… | |
2761 | |
3189 | |
2762 | |
3190 | |
2763 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
3191 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
2764 | |
3192 | |
2765 | SV * |
3193 | SV * |
2766 | new (SV *klass, SV *count_ = 0) |
3194 | new (SV *klass, SV *count = 0) |
2767 | CODE: |
3195 | CODE: |
2768 | { |
3196 | RETVAL = sv_bless ( |
2769 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
3197 | coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1), |
2770 | AV *av = newAV (); |
3198 | GvSTASH (CvGV (cv)) |
2771 | SV **ary; |
3199 | ); |
|
|
3200 | OUTPUT: |
|
|
3201 | RETVAL |
2772 | |
3202 | |
2773 | /* unfortunately, building manually saves memory */ |
3203 | # helper for Coro::Channel |
2774 | Newx (ary, 2, SV *); |
3204 | SV * |
2775 | AvALLOC (av) = ary; |
3205 | _alloc (int count) |
2776 | AvARRAY (av) = ary; |
3206 | CODE: |
2777 | AvMAX (av) = 1; |
3207 | 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: |
3208 | OUTPUT: |
2784 | RETVAL |
3209 | RETVAL |
2785 | |
3210 | |
2786 | SV * |
3211 | SV * |
2787 | count (SV *self) |
3212 | count (SV *self) |
… | |
… | |
2796 | adjust = 1 |
3221 | adjust = 1 |
2797 | CODE: |
3222 | CODE: |
2798 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
3223 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
2799 | |
3224 | |
2800 | void |
3225 | void |
2801 | down (SV *self) |
3226 | down (...) |
2802 | CODE: |
3227 | CODE: |
2803 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
3228 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
|
|
3229 | |
|
|
3230 | void |
|
|
3231 | wait (...) |
|
|
3232 | CODE: |
|
|
3233 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); |
2804 | |
3234 | |
2805 | void |
3235 | void |
2806 | try (SV *self) |
3236 | try (SV *self) |
2807 | PPCODE: |
3237 | PPCODE: |
2808 | { |
3238 | { |
… | |
… | |
2820 | XSRETURN_NO; |
3250 | XSRETURN_NO; |
2821 | } |
3251 | } |
2822 | |
3252 | |
2823 | void |
3253 | void |
2824 | waiters (SV *self) |
3254 | waiters (SV *self) |
2825 | CODE: |
3255 | PPCODE: |
2826 | { |
3256 | { |
2827 | AV *av = (AV *)SvRV (self); |
3257 | AV *av = (AV *)SvRV (self); |
|
|
3258 | int wcount = AvFILLp (av) + 1 - 1; |
2828 | |
3259 | |
2829 | if (GIMME_V == G_SCALAR) |
3260 | if (GIMME_V == G_SCALAR) |
2830 | XPUSHs (sv_2mortal (newSVsv (AvARRAY (av)[0]))); |
3261 | XPUSHs (sv_2mortal (newSViv (wcount))); |
2831 | else |
3262 | else |
2832 | { |
3263 | { |
2833 | int i; |
3264 | int i; |
2834 | EXTEND (SP, AvFILLp (av) + 1 - 1); |
3265 | EXTEND (SP, wcount); |
2835 | for (i = 1; i <= AvFILLp (av); ++i) |
3266 | for (i = 1; i <= wcount; ++i) |
2836 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
3267 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
2837 | } |
3268 | } |
2838 | } |
3269 | } |
|
|
3270 | |
|
|
3271 | MODULE = Coro::State PACKAGE = Coro::Signal |
|
|
3272 | |
|
|
3273 | SV * |
|
|
3274 | new (SV *klass) |
|
|
3275 | CODE: |
|
|
3276 | RETVAL = sv_bless ( |
|
|
3277 | coro_waitarray_new (aTHX_ 0), |
|
|
3278 | GvSTASH (CvGV (cv)) |
|
|
3279 | ); |
|
|
3280 | OUTPUT: |
|
|
3281 | RETVAL |
|
|
3282 | |
|
|
3283 | void |
|
|
3284 | wait (...) |
|
|
3285 | CODE: |
|
|
3286 | CORO_EXECUTE_SLF_XS (slf_init_signal_wait); |
|
|
3287 | |
|
|
3288 | void |
|
|
3289 | broadcast (SV *self) |
|
|
3290 | CODE: |
|
|
3291 | { |
|
|
3292 | AV *av = (AV *)SvRV (self); |
|
|
3293 | coro_signal_wake (aTHX_ av, AvFILLp (av)); |
|
|
3294 | } |
|
|
3295 | |
|
|
3296 | void |
|
|
3297 | send (SV *self) |
|
|
3298 | CODE: |
|
|
3299 | { |
|
|
3300 | AV *av = (AV *)SvRV (self); |
|
|
3301 | |
|
|
3302 | if (AvFILLp (av)) |
|
|
3303 | coro_signal_wake (aTHX_ av, 1); |
|
|
3304 | else |
|
|
3305 | SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */ |
|
|
3306 | } |
|
|
3307 | |
|
|
3308 | IV |
|
|
3309 | awaited (SV *self) |
|
|
3310 | CODE: |
|
|
3311 | RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1; |
|
|
3312 | OUTPUT: |
|
|
3313 | RETVAL |
2839 | |
3314 | |
2840 | |
3315 | |
2841 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
3316 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2842 | |
3317 | |
2843 | BOOT: |
3318 | BOOT: |
… | |
… | |
2871 | |
3346 | |
2872 | void |
3347 | void |
2873 | _register (char *target, char *proto, SV *req) |
3348 | _register (char *target, char *proto, SV *req) |
2874 | CODE: |
3349 | CODE: |
2875 | { |
3350 | { |
2876 | HV *st; |
3351 | 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 */ |
3352 | /* newXSproto doesn't return the CV on 5.8 */ |
2880 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3353 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
2881 | sv_setpv ((SV *)slf_cv, proto); |
3354 | sv_setpv ((SV *)slf_cv, proto); |
2882 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3355 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
2883 | } |
3356 | } |