… | |
… | |
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 |
… | |
… | |
173 | static HV *hv_sig; /* %SIG */ |
176 | static HV *hv_sig; /* %SIG */ |
174 | |
177 | |
175 | /* async_pool helper stuff */ |
178 | /* async_pool helper stuff */ |
176 | static SV *sv_pool_rss; |
179 | static SV *sv_pool_rss; |
177 | static SV *sv_pool_size; |
180 | static SV *sv_pool_size; |
|
|
181 | static SV *sv_async_pool_idle; |
178 | 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; |
179 | |
186 | |
180 | /* Coro::AnyEvent */ |
187 | /* Coro::AnyEvent */ |
181 | static SV *sv_activity; |
188 | static SV *sv_activity; |
182 | |
189 | |
183 | static struct coro_cctx *cctx_first; |
190 | static struct coro_cctx *cctx_first; |
… | |
… | |
243 | /* state data */ |
250 | /* state data */ |
244 | struct CoroSLF slf_frame; /* saved slf frame */ |
251 | struct CoroSLF slf_frame; /* saved slf frame */ |
245 | AV *mainstack; |
252 | AV *mainstack; |
246 | perl_slots *slot; /* basically the saved sp */ |
253 | perl_slots *slot; /* basically the saved sp */ |
247 | |
254 | |
|
|
255 | CV *startcv; /* the CV to execute */ |
248 | AV *args; /* data associated with this coroutine (initial args) */ |
256 | AV *args; /* data associated with this coroutine (initial args) */ |
249 | int refcnt; /* coroutines are refcounted, yes */ |
257 | int refcnt; /* coroutines are refcounted, yes */ |
250 | int flags; /* CF_ flags */ |
258 | int flags; /* CF_ flags */ |
251 | HV *hv; /* the perl hash associated with this coro, if any */ |
259 | HV *hv; /* the perl hash associated with this coro, if any */ |
252 | void (*on_destroy)(pTHX_ struct coro *coro); |
260 | void (*on_destroy)(pTHX_ struct coro *coro); |
253 | |
261 | |
254 | /* statistics */ |
262 | /* statistics */ |
255 | int usecount; /* number of transfers to this coro */ |
263 | int usecount; /* number of transfers to this coro */ |
256 | |
264 | |
257 | /* coro process data */ |
265 | /* coro process data */ |
258 | int prio; |
266 | int prio; |
259 | SV *throw; /* exception to be thrown */ |
267 | SV *except; /* exception to be thrown */ |
|
|
268 | SV *rouse_cb; |
260 | |
269 | |
261 | /* async_pool */ |
270 | /* async_pool */ |
262 | SV *saved_deffh; |
271 | SV *saved_deffh; |
|
|
272 | SV *invoke_cb; |
|
|
273 | AV *invoke_av; |
263 | |
274 | |
264 | /* linked list */ |
275 | /* linked list */ |
265 | struct coro *next, *prev; |
276 | struct coro *next, *prev; |
266 | }; |
277 | }; |
267 | |
278 | |
… | |
… | |
270 | |
281 | |
271 | /* the following variables are effectively part of the perl context */ |
282 | /* the following variables are effectively part of the perl context */ |
272 | /* and get copied between struct coro and these variables */ |
283 | /* and get copied between struct coro and these variables */ |
273 | /* the mainr easonw e don't support windows process emulation */ |
284 | /* the mainr easonw e don't support windows process emulation */ |
274 | static struct CoroSLF slf_frame; /* the current slf frame */ |
285 | static struct CoroSLF slf_frame; /* the current slf frame */ |
275 | static SV *coro_throw; |
|
|
276 | |
286 | |
277 | /** Coro ********************************************************************/ |
287 | /** Coro ********************************************************************/ |
278 | |
288 | |
279 | #define PRIO_MAX 3 |
289 | #define PRIO_MAX 3 |
280 | #define PRIO_HIGH 1 |
290 | #define PRIO_HIGH 1 |
… | |
… | |
285 | |
295 | |
286 | /* for Coro.pm */ |
296 | /* for Coro.pm */ |
287 | static SV *coro_current; |
297 | static SV *coro_current; |
288 | static SV *coro_readyhook; |
298 | static SV *coro_readyhook; |
289 | 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; |
290 | static struct coro *coro_first; |
301 | static struct coro *coro_first; |
291 | #define coro_nready coroapi.nready |
302 | #define coro_nready coroapi.nready |
292 | |
303 | |
293 | /** lowlevel stuff **********************************************************/ |
304 | /** lowlevel stuff **********************************************************/ |
294 | |
305 | |
… | |
… | |
318 | #if PERL_VERSION_ATLEAST (5,10,0) |
329 | #if PERL_VERSION_ATLEAST (5,10,0) |
319 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
330 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
320 | get_hv (name, create); |
331 | get_hv (name, create); |
321 | #endif |
332 | #endif |
322 | return get_hv (name, create); |
333 | return get_hv (name, create); |
|
|
334 | } |
|
|
335 | |
|
|
336 | /* may croak */ |
|
|
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); |
323 | } |
343 | } |
324 | |
344 | |
325 | static AV * |
345 | static AV * |
326 | coro_clone_padlist (pTHX_ CV *cv) |
346 | coro_clone_padlist (pTHX_ CV *cv) |
327 | { |
347 | { |
… | |
… | |
446 | else |
466 | else |
447 | { |
467 | { |
448 | #if CORO_PREFER_PERL_FUNCTIONS |
468 | #if CORO_PREFER_PERL_FUNCTIONS |
449 | /* this is probably cleaner? but also slower! */ |
469 | /* this is probably cleaner? but also slower! */ |
450 | /* in practise, it seems to be less stable */ |
470 | /* in practise, it seems to be less stable */ |
451 | CV *cp = Perl_cv_clone (cv); |
471 | CV *cp = Perl_cv_clone (aTHX_ cv); |
452 | CvPADLIST (cv) = CvPADLIST (cp); |
472 | CvPADLIST (cv) = CvPADLIST (cp); |
453 | CvPADLIST (cp) = 0; |
473 | CvPADLIST (cp) = 0; |
454 | SvREFCNT_dec (cp); |
474 | SvREFCNT_dec (cp); |
455 | #else |
475 | #else |
456 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
476 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
… | |
… | |
509 | |
529 | |
510 | PUTBACK; |
530 | PUTBACK; |
511 | } |
531 | } |
512 | |
532 | |
513 | slf_frame = c->slf_frame; |
533 | slf_frame = c->slf_frame; |
514 | coro_throw = c->throw; |
534 | CORO_THROW = c->except; |
515 | } |
535 | } |
516 | |
536 | |
517 | static void |
537 | static void |
518 | save_perl (pTHX_ Coro__State c) |
538 | save_perl (pTHX_ Coro__State c) |
519 | { |
539 | { |
520 | c->throw = coro_throw; |
540 | c->except = CORO_THROW; |
521 | c->slf_frame = slf_frame; |
541 | c->slf_frame = slf_frame; |
522 | |
542 | |
523 | { |
543 | { |
524 | dSP; |
544 | dSP; |
525 | I32 cxix = cxstack_ix; |
545 | I32 cxix = cxstack_ix; |
… | |
… | |
600 | * of perl.c:init_stacks, except that it uses less memory |
620 | * of perl.c:init_stacks, except that it uses less memory |
601 | * on the (sometimes correct) assumption that coroutines do |
621 | * on the (sometimes correct) assumption that coroutines do |
602 | * not usually need a lot of stackspace. |
622 | * not usually need a lot of stackspace. |
603 | */ |
623 | */ |
604 | #if CORO_PREFER_PERL_FUNCTIONS |
624 | #if CORO_PREFER_PERL_FUNCTIONS |
605 | # define coro_init_stacks init_stacks |
625 | # define coro_init_stacks(thx) init_stacks () |
606 | #else |
626 | #else |
607 | static void |
627 | static void |
608 | coro_init_stacks (pTHX) |
628 | coro_init_stacks (pTHX) |
609 | { |
629 | { |
610 | PL_curstackinfo = new_stackinfo(32, 8); |
630 | PL_curstackinfo = new_stackinfo(32, 8); |
… | |
… | |
673 | #if !PERL_VERSION_ATLEAST (5,10,0) |
693 | #if !PERL_VERSION_ATLEAST (5,10,0) |
674 | Safefree (PL_retstack); |
694 | Safefree (PL_retstack); |
675 | #endif |
695 | #endif |
676 | } |
696 | } |
677 | |
697 | |
|
|
698 | #define CORO_RSS \ |
|
|
699 | rss += sizeof (SYM (curstackinfo)); \ |
|
|
700 | rss += (SYM (curstackinfo->si_cxmax) + 1) * sizeof (PERL_CONTEXT); \ |
|
|
701 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (SYM (curstack))) * sizeof (SV *); \ |
|
|
702 | rss += SYM (tmps_max) * sizeof (SV *); \ |
|
|
703 | rss += (SYM (markstack_max) - SYM (markstack_ptr)) * sizeof (I32); \ |
|
|
704 | rss += SYM (scopestack_max) * sizeof (I32); \ |
|
|
705 | rss += SYM (savestack_max) * sizeof (ANY); |
|
|
706 | |
678 | static size_t |
707 | static size_t |
679 | coro_rss (pTHX_ struct coro *coro) |
708 | coro_rss (pTHX_ struct coro *coro) |
680 | { |
709 | { |
681 | size_t rss = sizeof (*coro); |
710 | size_t rss = sizeof (*coro); |
682 | |
711 | |
683 | if (coro->mainstack) |
712 | if (coro->mainstack) |
684 | { |
713 | { |
685 | perl_slots tmp_slot; |
|
|
686 | perl_slots *slot; |
|
|
687 | |
|
|
688 | if (coro->flags & CF_RUNNING) |
714 | if (coro->flags & CF_RUNNING) |
689 | { |
715 | { |
690 | slot = &tmp_slot; |
716 | #define SYM(sym) PL_ ## sym |
691 | |
717 | CORO_RSS; |
692 | #define VAR(name,type) slot->name = PL_ ## name; |
|
|
693 | # include "state.h" |
|
|
694 | #undef VAR |
718 | #undef SYM |
695 | } |
719 | } |
696 | else |
720 | else |
697 | slot = coro->slot; |
|
|
698 | |
|
|
699 | if (slot) |
|
|
700 | { |
721 | { |
701 | rss += sizeof (slot->curstackinfo); |
722 | #define SYM(sym) coro->slot->sym |
702 | rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
723 | CORO_RSS; |
703 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); |
724 | #undef SYM |
704 | rss += slot->tmps_max * sizeof (SV *); |
|
|
705 | rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); |
|
|
706 | rss += slot->scopestack_max * sizeof (I32); |
|
|
707 | rss += slot->savestack_max * sizeof (ANY); |
|
|
708 | |
|
|
709 | #if !PERL_VERSION_ATLEAST (5,10,0) |
|
|
710 | rss += slot->retstack_max * sizeof (OP *); |
|
|
711 | #endif |
|
|
712 | } |
725 | } |
713 | } |
726 | } |
714 | |
727 | |
715 | return rss; |
728 | return rss; |
716 | } |
729 | } |
… | |
… | |
857 | { |
870 | { |
858 | dSP; |
871 | dSP; |
859 | UNOP myop; |
872 | UNOP myop; |
860 | |
873 | |
861 | Zero (&myop, 1, UNOP); |
874 | Zero (&myop, 1, UNOP); |
862 | myop.op_next = Nullop; |
875 | myop.op_next = Nullop; |
|
|
876 | myop.op_type = OP_ENTERSUB; |
863 | myop.op_flags = OPf_WANT_VOID; |
877 | myop.op_flags = OPf_WANT_VOID; |
864 | |
878 | |
865 | PUSHMARK (SP); |
879 | PUSHMARK (SP); |
866 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
880 | PUSHs ((SV *)coro->startcv); |
867 | PUTBACK; |
881 | PUTBACK; |
868 | PL_op = (OP *)&myop; |
882 | PL_op = (OP *)&myop; |
869 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
883 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
870 | SPAGAIN; |
|
|
871 | } |
884 | } |
872 | |
885 | |
873 | /* this newly created coroutine might be run on an existing cctx which most |
886 | /* this newly created coroutine might be run on an existing cctx which most |
874 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
887 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
875 | */ |
888 | */ |
876 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
889 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
877 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
890 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
878 | |
891 | |
879 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
892 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
|
|
893 | coro_setup_op.op_next = PL_op; |
880 | coro_setup_op.op_type = OP_CUSTOM; |
894 | coro_setup_op.op_type = OP_CUSTOM; |
881 | coro_setup_op.op_ppaddr = pp_slf; |
895 | coro_setup_op.op_ppaddr = pp_slf; |
882 | coro_setup_op.op_next = PL_op; |
896 | /* no flags etc. required, as an init function won't be called */ |
883 | |
897 | |
884 | PL_op = (OP *)&coro_setup_op; |
898 | PL_op = (OP *)&coro_setup_op; |
885 | |
899 | |
886 | /* copy throw, in case it was set before coro_setup */ |
900 | /* copy throw, in case it was set before coro_setup */ |
887 | coro_throw = coro->throw; |
901 | CORO_THROW = coro->except; |
888 | } |
902 | } |
889 | |
903 | |
890 | static void |
904 | static void |
891 | coro_destruct (pTHX_ struct coro *coro) |
905 | coro_destruct (pTHX_ struct coro *coro) |
892 | { |
906 | { |
… | |
… | |
916 | |
930 | |
917 | SvREFCNT_dec (PL_diehook); |
931 | SvREFCNT_dec (PL_diehook); |
918 | SvREFCNT_dec (PL_warnhook); |
932 | SvREFCNT_dec (PL_warnhook); |
919 | |
933 | |
920 | SvREFCNT_dec (coro->saved_deffh); |
934 | SvREFCNT_dec (coro->saved_deffh); |
921 | SvREFCNT_dec (coro_throw); |
935 | SvREFCNT_dec (coro->rouse_cb); |
|
|
936 | SvREFCNT_dec (coro->invoke_cb); |
|
|
937 | SvREFCNT_dec (coro->invoke_av); |
922 | |
938 | |
923 | coro_destruct_stacks (aTHX); |
939 | coro_destruct_stacks (aTHX); |
924 | } |
940 | } |
925 | |
941 | |
926 | INLINE void |
942 | INLINE void |
… | |
… | |
1013 | SAVETMPS; |
1029 | SAVETMPS; |
1014 | EXTEND (SP, 3); |
1030 | EXTEND (SP, 3); |
1015 | PUSHMARK (SP); |
1031 | PUSHMARK (SP); |
1016 | PUSHs (&PL_sv_yes); |
1032 | PUSHs (&PL_sv_yes); |
1017 | PUSHs (fullname); |
1033 | PUSHs (fullname); |
1018 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
1034 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
1019 | PUTBACK; |
1035 | PUTBACK; |
1020 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
1036 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
1021 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1037 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1022 | SPAGAIN; |
1038 | SPAGAIN; |
1023 | FREETMPS; |
1039 | FREETMPS; |
… | |
… | |
1070 | static int |
1086 | static int |
1071 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1087 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1072 | { |
1088 | { |
1073 | *frame = cctx_ssl_frame; |
1089 | *frame = cctx_ssl_frame; |
1074 | |
1090 | |
1075 | return 1; /* execute the restored frame - there must be one */ |
1091 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
1076 | } |
1092 | } |
1077 | |
1093 | |
1078 | /* initialises PL_top_env and injects a pseudo-slf-call to sett he stacklevel */ |
1094 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1079 | static void NOINLINE |
1095 | static void NOINLINE |
1080 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1096 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1081 | { |
1097 | { |
1082 | PL_top_env = &PL_start_env; |
1098 | PL_top_env = &PL_start_env; |
1083 | |
1099 | |
1084 | if (cctx->flags & CC_TRACE) |
1100 | if (cctx->flags & CC_TRACE) |
1085 | PL_runops = runops_trace; |
1101 | PL_runops = runops_trace; |
1086 | |
1102 | |
1087 | /* we already must be in an SLF call, there is no other valid way |
1103 | /* we already must be executing an SLF op, there is no other valid way |
1088 | * that can lead to creation of a new cctx */ |
1104 | * that can lead to creation of a new cctx */ |
1089 | assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", |
1105 | assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", |
1090 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1106 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1091 | |
1107 | |
|
|
1108 | /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ |
1092 | cctx_ssl_cctx = cctx; |
1109 | cctx_ssl_cctx = cctx; |
1093 | cctx_ssl_frame = slf_frame; |
1110 | cctx_ssl_frame = slf_frame; |
1094 | |
1111 | |
1095 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1112 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1096 | slf_frame.check = slf_check_set_stacklevel; |
1113 | slf_frame.check = slf_check_set_stacklevel; |
… | |
… | |
1128 | transfer_tail (aTHX); |
1145 | transfer_tail (aTHX); |
1129 | |
1146 | |
1130 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1147 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1131 | PL_restartop = PL_op; |
1148 | PL_restartop = PL_op; |
1132 | perl_run (PL_curinterp); |
1149 | perl_run (PL_curinterp); |
|
|
1150 | /* |
|
|
1151 | * Unfortunately, there is no way to get at the return values of the |
|
|
1152 | * coro body here, as perl_run destroys these |
|
|
1153 | */ |
1133 | |
1154 | |
1134 | /* |
1155 | /* |
1135 | * If perl-run returns we assume exit() was being called or the coro |
1156 | * If perl-run returns we assume exit() was being called or the coro |
1136 | * fell off the end, which seems to be the only valid (non-bug) |
1157 | * fell off the end, which seems to be the only valid (non-bug) |
1137 | * reason for perl_run to return. We try to exit by jumping to the |
1158 | * reason for perl_run to return. We try to exit by jumping to the |
… | |
… | |
1317 | dSTACKLEVEL; |
1338 | dSTACKLEVEL; |
1318 | |
1339 | |
1319 | /* sometimes transfer is only called to set idle_sp */ |
1340 | /* sometimes transfer is only called to set idle_sp */ |
1320 | if (expect_false (!next)) |
1341 | if (expect_false (!next)) |
1321 | { |
1342 | { |
1322 | ((coro_cctx *)prev)->idle_sp = (void *)stacklevel; |
1343 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1323 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1344 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1324 | } |
1345 | } |
1325 | else if (expect_true (prev != next)) |
1346 | else if (expect_true (prev != next)) |
1326 | { |
1347 | { |
1327 | coro_cctx *prev__cctx; |
1348 | coro_cctx *prev__cctx; |
… | |
… | |
1352 | |
1373 | |
1353 | prev__cctx = prev->cctx; |
1374 | prev__cctx = prev->cctx; |
1354 | |
1375 | |
1355 | /* possibly untie and reuse the cctx */ |
1376 | /* possibly untie and reuse the cctx */ |
1356 | if (expect_true ( |
1377 | if (expect_true ( |
1357 | prev__cctx->idle_sp == (void *)stacklevel |
1378 | prev__cctx->idle_sp == STACKLEVEL |
1358 | && !(prev__cctx->flags & CC_TRACE) |
1379 | && !(prev__cctx->flags & CC_TRACE) |
1359 | && !force_cctx |
1380 | && !force_cctx |
1360 | )) |
1381 | )) |
1361 | { |
1382 | { |
1362 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1383 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
… | |
… | |
1429 | |
1450 | |
1430 | coro->slot = 0; |
1451 | coro->slot = 0; |
1431 | } |
1452 | } |
1432 | |
1453 | |
1433 | cctx_destroy (coro->cctx); |
1454 | cctx_destroy (coro->cctx); |
|
|
1455 | SvREFCNT_dec (coro->startcv); |
1434 | SvREFCNT_dec (coro->args); |
1456 | SvREFCNT_dec (coro->args); |
|
|
1457 | SvREFCNT_dec (CORO_THROW); |
1435 | |
1458 | |
1436 | if (coro->next) coro->next->prev = coro->prev; |
1459 | if (coro->next) coro->next->prev = coro->prev; |
1437 | if (coro->prev) coro->prev->next = coro->next; |
1460 | if (coro->prev) coro->prev->next = coro->next; |
1438 | if (coro == coro_first) coro_first = coro->next; |
1461 | if (coro == coro_first) coro_first = coro->next; |
1439 | |
1462 | |
… | |
… | |
1493 | |
1516 | |
1494 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1517 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1495 | TRANSFER (ta, 1); |
1518 | TRANSFER (ta, 1); |
1496 | } |
1519 | } |
1497 | |
1520 | |
|
|
1521 | /*****************************************************************************/ |
|
|
1522 | /* gensub: simple closure generation utility */ |
|
|
1523 | |
|
|
1524 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
|
|
1525 | |
|
|
1526 | /* create a closure from XS, returns a code reference */ |
|
|
1527 | /* the arg can be accessed via GENSUB_ARG from the callback */ |
|
|
1528 | /* the callback must use dXSARGS/XSRETURN */ |
|
|
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 | |
1498 | /** Coro ********************************************************************/ |
1544 | /** Coro ********************************************************************/ |
1499 | |
1545 | |
1500 | INLINE void |
1546 | INLINE void |
1501 | coro_enq (pTHX_ struct coro *coro) |
1547 | coro_enq (pTHX_ struct coro *coro) |
1502 | { |
1548 | { |
… | |
… | |
1699 | if (coro->flags & CF_RUNNING) |
1745 | if (coro->flags & CF_RUNNING) |
1700 | PL_runops = RUNOPS_DEFAULT; |
1746 | PL_runops = RUNOPS_DEFAULT; |
1701 | else |
1747 | else |
1702 | coro->slot->runops = RUNOPS_DEFAULT; |
1748 | coro->slot->runops = RUNOPS_DEFAULT; |
1703 | } |
1749 | } |
|
|
1750 | } |
|
|
1751 | |
|
|
1752 | /*****************************************************************************/ |
|
|
1753 | /* async pool handler */ |
|
|
1754 | |
|
|
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; |
1704 | } |
1941 | } |
1705 | |
1942 | |
1706 | /*****************************************************************************/ |
1943 | /*****************************************************************************/ |
1707 | /* schedule-like-function opcode (SLF) */ |
1944 | /* schedule-like-function opcode (SLF) */ |
1708 | |
1945 | |
… | |
… | |
1835 | } |
2072 | } |
1836 | while (slf_frame.check (aTHX_ &slf_frame)); |
2073 | while (slf_frame.check (aTHX_ &slf_frame)); |
1837 | |
2074 | |
1838 | 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 */ |
1839 | |
2076 | |
|
|
2077 | /* exception handling */ |
|
|
2078 | if (expect_false (CORO_THROW)) |
|
|
2079 | { |
|
|
2080 | SV *exception = sv_2mortal (CORO_THROW); |
|
|
2081 | |
|
|
2082 | CORO_THROW = 0; |
|
|
2083 | sv_setsv (ERRSV, exception); |
|
|
2084 | croak (0); |
|
|
2085 | } |
|
|
2086 | |
1840 | /* return value handling - mostly like entersub */ |
2087 | /* return value handling - mostly like entersub */ |
1841 | /* make sure we put something on the stack in scalar context */ |
2088 | /* make sure we put something on the stack in scalar context */ |
1842 | if (GIMME_V == G_SCALAR) |
2089 | if (GIMME_V == G_SCALAR) |
1843 | { |
2090 | { |
1844 | dSP; |
2091 | dSP; |
… | |
… | |
1850 | bot [1] = *sp; |
2097 | bot [1] = *sp; |
1851 | |
2098 | |
1852 | SP = bot + 1; |
2099 | SP = bot + 1; |
1853 | |
2100 | |
1854 | PUTBACK; |
2101 | 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 | } |
2102 | } |
1866 | |
2103 | |
1867 | return NORMAL; |
2104 | return NORMAL; |
1868 | } |
2105 | } |
1869 | |
2106 | |
… | |
… | |
1991 | PerlIOBuf_get_cnt, |
2228 | PerlIOBuf_get_cnt, |
1992 | PerlIOBuf_set_ptrcnt, |
2229 | PerlIOBuf_set_ptrcnt, |
1993 | }; |
2230 | }; |
1994 | |
2231 | |
1995 | /*****************************************************************************/ |
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 | |
1996 | /* Coro::Semaphore */ |
2254 | /* semaphore */ |
1997 | |
2255 | |
1998 | static void |
2256 | static void |
1999 | coro_semaphore_adjust (pTHX_ AV *av, IV adjust) |
2257 | coro_semaphore_adjust (pTHX_ AV *av, IV adjust) |
2000 | { |
2258 | { |
2001 | SV *count_sv = AvARRAY (av)[0]; |
2259 | SV *count_sv = AvARRAY (av)[0]; |
… | |
… | |
2013 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2271 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2014 | AvARRAY (av)[1] = count_sv; |
2272 | AvARRAY (av)[1] = count_sv; |
2015 | cb = av_shift (av); |
2273 | cb = av_shift (av); |
2016 | |
2274 | |
2017 | if (SvOBJECT (cb)) |
2275 | if (SvOBJECT (cb)) |
|
|
2276 | { |
2018 | api_ready (aTHX_ cb); |
2277 | api_ready (aTHX_ cb); |
2019 | else |
2278 | --count; |
2020 | 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 | } |
2021 | |
2288 | |
2022 | SvREFCNT_dec (cb); |
2289 | SvREFCNT_dec (cb); |
2023 | |
|
|
2024 | --count; |
|
|
2025 | } |
2290 | } |
2026 | } |
2291 | } |
2027 | |
2292 | |
2028 | static void |
2293 | static void |
2029 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
2294 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
… | |
… | |
2031 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
2296 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
2032 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
2297 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
2033 | } |
2298 | } |
2034 | |
2299 | |
2035 | static int |
2300 | static int |
2036 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
2301 | slf_check_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, int acquire) |
2037 | { |
2302 | { |
2038 | AV *av = (AV *)frame->data; |
2303 | AV *av = (AV *)frame->data; |
2039 | SV *count_sv = AvARRAY (av)[0]; |
2304 | SV *count_sv = AvARRAY (av)[0]; |
2040 | |
2305 | |
|
|
2306 | /* if we are about to throw, don't actually acquire the lock, just throw */ |
|
|
2307 | if (CORO_THROW) |
|
|
2308 | return 0; |
2041 | if (SvIVX (count_sv) > 0) |
2309 | else if (SvIVX (count_sv) > 0) |
2042 | { |
2310 | { |
2043 | SvSTATE_current->on_destroy = 0; |
2311 | SvSTATE_current->on_destroy = 0; |
|
|
2312 | |
|
|
2313 | if (acquire) |
2044 | 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 | |
2045 | return 0; |
2318 | return 0; |
2046 | } |
2319 | } |
2047 | else |
2320 | else |
2048 | { |
2321 | { |
2049 | int i; |
2322 | int i; |
… | |
… | |
2058 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2331 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2059 | return 1; |
2332 | return 1; |
2060 | } |
2333 | } |
2061 | } |
2334 | } |
2062 | |
2335 | |
2063 | 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 |
2064 | 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) |
2065 | { |
2350 | { |
2066 | AV *av = (AV *)SvRV (arg [0]); |
2351 | AV *av = (AV *)SvRV (arg [0]); |
2067 | |
2352 | |
2068 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2353 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2069 | { |
2354 | { |
2070 | frame->data = (void *)av; |
2355 | frame->data = (void *)av; |
2071 | frame->prepare = prepare_nop; |
2356 | frame->prepare = prepare_nop; |
2072 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
|
|
2073 | } |
2357 | } |
2074 | else |
2358 | else |
2075 | { |
2359 | { |
2076 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2360 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2077 | |
2361 | |
2078 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2362 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2079 | frame->prepare = prepare_schedule; |
2363 | frame->prepare = prepare_schedule; |
2080 | |
2364 | |
2081 | /* to avoid race conditions when a woken-up coro gets terminated */ |
2365 | /* to avoid race conditions when a woken-up coro gets terminated */ |
2082 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
2366 | /* 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; |
2367 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
2085 | } |
2368 | } |
|
|
2369 | } |
2086 | |
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); |
2087 | frame->check = slf_check_semaphore_down; |
2375 | frame->check = slf_check_semaphore_down; |
2088 | |
|
|
2089 | } |
2376 | } |
2090 | |
2377 | |
2091 | /*****************************************************************************/ |
2378 | static void |
2092 | /* gensub: simple closure generation utility */ |
2379 | 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 | { |
2380 | { |
2102 | 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]); |
2103 | |
2386 | |
2104 | sv_upgrade ((SV *)cv, SVt_PVCV); |
2387 | av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); |
2105 | |
2388 | |
2106 | CvANON_on (cv); |
2389 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2107 | CvISXSUB_on (cv); |
2390 | coro_semaphore_adjust (aTHX_ av, 0); |
2108 | CvXSUB (cv) = xsub; |
|
|
2109 | GENSUB_ARG = arg; |
|
|
2110 | |
2391 | |
2111 | 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 | } |
2112 | } |
2457 | } |
2113 | |
2458 | |
2114 | /*****************************************************************************/ |
2459 | /*****************************************************************************/ |
2115 | /* Coro::AIO */ |
2460 | /* Coro::AIO */ |
2116 | |
2461 | |
… | |
… | |
2131 | dXSARGS; |
2476 | dXSARGS; |
2132 | AV *state = (AV *)GENSUB_ARG; |
2477 | AV *state = (AV *)GENSUB_ARG; |
2133 | SV *coro = av_pop (state); |
2478 | SV *coro = av_pop (state); |
2134 | SV *data_sv = newSV (sizeof (struct io_state)); |
2479 | SV *data_sv = newSV (sizeof (struct io_state)); |
2135 | |
2480 | |
2136 | av_extend (state, items); |
2481 | av_extend (state, items - 1); |
2137 | |
2482 | |
2138 | sv_upgrade (data_sv, SVt_PV); |
2483 | sv_upgrade (data_sv, SVt_PV); |
2139 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2484 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2140 | SvPOK_only (data_sv); |
2485 | SvPOK_only (data_sv); |
2141 | |
2486 | |
… | |
… | |
2165 | |
2510 | |
2166 | static int |
2511 | static int |
2167 | slf_check_aio_req (pTHX_ struct CoroSLF *frame) |
2512 | slf_check_aio_req (pTHX_ struct CoroSLF *frame) |
2168 | { |
2513 | { |
2169 | 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; |
2170 | |
2521 | |
2171 | /* one element that is an RV? repeat! */ |
2522 | /* one element that is an RV? repeat! */ |
2172 | if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) |
2523 | if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) |
2173 | return 1; |
2524 | return 1; |
2174 | |
2525 | |
… | |
… | |
2345 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2696 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2346 | } |
2697 | } |
2347 | |
2698 | |
2348 | SV * |
2699 | SV * |
2349 | new (char *klass, ...) |
2700 | new (char *klass, ...) |
|
|
2701 | ALIAS: |
|
|
2702 | Coro::new = 1 |
2350 | CODE: |
2703 | CODE: |
2351 | { |
2704 | { |
2352 | struct coro *coro; |
2705 | struct coro *coro; |
2353 | MAGIC *mg; |
2706 | MAGIC *mg; |
2354 | HV *hv; |
2707 | HV *hv; |
|
|
2708 | CV *cb; |
2355 | 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 | } |
2356 | |
2724 | |
2357 | Newz (0, coro, 1, struct coro); |
2725 | Newz (0, coro, 1, struct coro); |
2358 | coro->args = newAV (); |
2726 | coro->args = newAV (); |
2359 | coro->flags = CF_NEW; |
2727 | coro->flags = CF_NEW; |
2360 | |
2728 | |
… | |
… | |
2365 | coro->hv = hv = newHV (); |
2733 | coro->hv = hv = newHV (); |
2366 | 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); |
2367 | mg->mg_flags |= MGf_DUP; |
2735 | mg->mg_flags |= MGf_DUP; |
2368 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2736 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2369 | |
2737 | |
|
|
2738 | if (items > 1) |
|
|
2739 | { |
2370 | 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 | |
2371 | for (i = 1; i < items; i++) |
2750 | for (i = 2; i < items; i++) |
2372 | av_push (coro->args, newSVsv (ST (i))); |
2751 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2752 | } |
2373 | } |
2753 | } |
2374 | OUTPUT: |
2754 | OUTPUT: |
2375 | RETVAL |
2755 | RETVAL |
2376 | |
2756 | |
2377 | void |
2757 | void |
… | |
… | |
2506 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
2886 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
2507 | PROTOTYPE: $;$ |
2887 | PROTOTYPE: $;$ |
2508 | CODE: |
2888 | CODE: |
2509 | { |
2889 | { |
2510 | struct coro *current = SvSTATE_current; |
2890 | struct coro *current = SvSTATE_current; |
2511 | SV **throwp = self == current ? &coro_throw : &self->throw; |
2891 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
2512 | SvREFCNT_dec (*throwp); |
2892 | SvREFCNT_dec (*throwp); |
2513 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
2893 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
2514 | } |
2894 | } |
2515 | |
2895 | |
2516 | void |
2896 | void |
… | |
… | |
2575 | |
2955 | |
2576 | BOOT: |
2956 | BOOT: |
2577 | { |
2957 | { |
2578 | int i; |
2958 | int i; |
2579 | |
2959 | |
2580 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
2960 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
2581 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2961 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2582 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2962 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2583 | |
2963 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
|
|
2964 | cv_coro_terminate = get_cv ( "Coro::terminate", GV_ADD); |
2584 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
2965 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); |
2585 | 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); |
2586 | |
2972 | |
2587 | coro_stash = gv_stashpv ("Coro", TRUE); |
2973 | coro_stash = gv_stashpv ("Coro", TRUE); |
2588 | |
2974 | |
2589 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
2975 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
2590 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
2976 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
… | |
… | |
2605 | coroapi.ready = api_ready; |
2991 | coroapi.ready = api_ready; |
2606 | coroapi.is_ready = api_is_ready; |
2992 | coroapi.is_ready = api_is_ready; |
2607 | coroapi.nready = coro_nready; |
2993 | coroapi.nready = coro_nready; |
2608 | coroapi.current = coro_current; |
2994 | coroapi.current = coro_current; |
2609 | |
2995 | |
2610 | GCoroAPI = &coroapi; |
2996 | /*GCoroAPI = &coroapi;*/ |
2611 | sv_setiv (sv, (IV)&coroapi); |
2997 | sv_setiv (sv, (IV)&coroapi); |
2612 | SvREADONLY_on (sv); |
2998 | SvREADONLY_on (sv); |
2613 | } |
2999 | } |
2614 | } |
3000 | } |
2615 | |
3001 | |
… | |
… | |
2679 | CODE: |
3065 | CODE: |
2680 | RETVAL = coro_nready; |
3066 | RETVAL = coro_nready; |
2681 | OUTPUT: |
3067 | OUTPUT: |
2682 | RETVAL |
3068 | RETVAL |
2683 | |
3069 | |
2684 | # for async_pool speedup |
|
|
2685 | void |
3070 | void |
2686 | _pool_1 (SV *cb) |
3071 | _pool_handler (...) |
2687 | CODE: |
3072 | CODE: |
2688 | { |
3073 | 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 | |
3074 | |
2696 | 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) |
2697 | { |
3090 | { |
2698 | SV *old = PL_diehook; |
3091 | PUSHMARK (SP); |
2699 | PL_diehook = 0; |
3092 | EXTEND (SP, 2); |
2700 | SvREFCNT_dec (old); |
3093 | PUSHs (sv_Coro); |
2701 | 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)); |
2702 | } |
3100 | } |
2703 | |
3101 | |
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 | { |
3102 | { |
2717 | av_fill (defav, len - 1); |
3103 | struct coro *coro = SvSTATE_hv (hv); |
2718 | for (i = 0; i < len; ++i) |
3104 | |
2719 | 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; |
2720 | } |
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); |
2721 | } |
3117 | } |
|
|
3118 | |
|
|
3119 | SV * |
|
|
3120 | rouse_cb () |
|
|
3121 | PROTOTYPE: |
|
|
3122 | CODE: |
|
|
3123 | RETVAL = coro_new_rouse_cb (aTHX); |
|
|
3124 | OUTPUT: |
|
|
3125 | RETVAL |
2722 | |
3126 | |
2723 | void |
3127 | void |
2724 | _pool_2 (SV *cb) |
3128 | rouse_wait (...) |
|
|
3129 | PROTOTYPE: ;$ |
2725 | CODE: |
3130 | PPCODE: |
2726 | { |
3131 | 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 | |
3132 | |
2756 | |
3133 | |
2757 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3134 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2758 | |
3135 | |
2759 | BOOT: |
3136 | BOOT: |
… | |
… | |
2761 | |
3138 | |
2762 | |
3139 | |
2763 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
3140 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
2764 | |
3141 | |
2765 | SV * |
3142 | SV * |
2766 | new (SV *klass, SV *count_ = 0) |
3143 | new (SV *klass, SV *count = 0) |
2767 | CODE: |
3144 | CODE: |
2768 | { |
3145 | RETVAL = sv_bless ( |
2769 | /* 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), |
2770 | AV *av = newAV (); |
3147 | GvSTASH (CvGV (cv)) |
2771 | SV **ary; |
3148 | ); |
|
|
3149 | OUTPUT: |
|
|
3150 | RETVAL |
2772 | |
3151 | |
2773 | /* unfortunately, building manually saves memory */ |
3152 | # helper for Coro::Channel |
2774 | Newx (ary, 2, SV *); |
3153 | SV * |
2775 | AvALLOC (av) = ary; |
3154 | _alloc (int count) |
2776 | AvARRAY (av) = ary; |
3155 | CODE: |
2777 | AvMAX (av) = 1; |
3156 | 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: |
3157 | OUTPUT: |
2784 | RETVAL |
3158 | RETVAL |
2785 | |
3159 | |
2786 | SV * |
3160 | SV * |
2787 | count (SV *self) |
3161 | count (SV *self) |
… | |
… | |
2796 | adjust = 1 |
3170 | adjust = 1 |
2797 | CODE: |
3171 | CODE: |
2798 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
3172 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
2799 | |
3173 | |
2800 | void |
3174 | void |
2801 | down (SV *self) |
3175 | down (...) |
2802 | CODE: |
3176 | CODE: |
2803 | 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); |
2804 | |
3183 | |
2805 | void |
3184 | void |
2806 | try (SV *self) |
3185 | try (SV *self) |
2807 | PPCODE: |
3186 | PPCODE: |
2808 | { |
3187 | { |
… | |
… | |
2820 | XSRETURN_NO; |
3199 | XSRETURN_NO; |
2821 | } |
3200 | } |
2822 | |
3201 | |
2823 | void |
3202 | void |
2824 | waiters (SV *self) |
3203 | waiters (SV *self) |
2825 | CODE: |
3204 | PPCODE: |
2826 | { |
3205 | { |
2827 | AV *av = (AV *)SvRV (self); |
3206 | AV *av = (AV *)SvRV (self); |
|
|
3207 | int wcount = AvFILLp (av) + 1 - 1; |
2828 | |
3208 | |
2829 | if (GIMME_V == G_SCALAR) |
3209 | if (GIMME_V == G_SCALAR) |
2830 | XPUSHs (sv_2mortal (newSVsv (AvARRAY (av)[0]))); |
3210 | XPUSHs (sv_2mortal (newSViv (wcount))); |
2831 | else |
3211 | else |
2832 | { |
3212 | { |
2833 | int i; |
3213 | int i; |
2834 | EXTEND (SP, AvFILLp (av) + 1 - 1); |
3214 | EXTEND (SP, wcount); |
2835 | for (i = 1; i <= AvFILLp (av); ++i) |
3215 | for (i = 1; i <= wcount; ++i) |
2836 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
3216 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
2837 | } |
3217 | } |
2838 | } |
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 |
2839 | |
3263 | |
2840 | |
3264 | |
2841 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
3265 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2842 | |
3266 | |
2843 | BOOT: |
3267 | BOOT: |
… | |
… | |
2871 | |
3295 | |
2872 | void |
3296 | void |
2873 | _register (char *target, char *proto, SV *req) |
3297 | _register (char *target, char *proto, SV *req) |
2874 | CODE: |
3298 | CODE: |
2875 | { |
3299 | { |
2876 | HV *st; |
3300 | 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 */ |
3301 | /* newXSproto doesn't return the CV on 5.8 */ |
2880 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3302 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
2881 | sv_setpv ((SV *)slf_cv, proto); |
3303 | sv_setpv ((SV *)slf_cv, proto); |
2882 | 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); |
2883 | } |
3305 | } |