… | |
… | |
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 |
… | |
… | |
95 | # define GV_NOTQUAL 0 |
95 | # define GV_NOTQUAL 0 |
96 | #endif |
96 | #endif |
97 | #ifndef newSV |
97 | #ifndef newSV |
98 | # define newSV(l) NEWSV(0,l) |
98 | # define newSV(l) NEWSV(0,l) |
99 | #endif |
99 | #endif |
|
|
100 | #ifndef CvISXSUB_on |
|
|
101 | # define CvISXSUB_on(cv) (void)cv |
|
|
102 | #endif |
|
|
103 | #ifndef CvISXSUB |
|
|
104 | # define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE) |
|
|
105 | #endif |
100 | |
106 | |
101 | /* 5.8.7 */ |
107 | /* 5.8.7 */ |
102 | #ifndef SvRV_set |
108 | #ifndef SvRV_set |
103 | # define SvRV_set(s,v) SvRV(s) = (v) |
109 | # define SvRV_set(s,v) SvRV(s) = (v) |
104 | #endif |
110 | #endif |
… | |
… | |
117 | #endif |
123 | #endif |
118 | |
124 | |
119 | /* 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 |
120 | * portable way as possible. */ |
126 | * portable way as possible. */ |
121 | #if __GNUC__ >= 4 |
127 | #if __GNUC__ >= 4 |
|
|
128 | # define dSTACKLEVEL int stacklevel_dummy |
122 | # define dSTACKLEVEL void *stacklevel = __builtin_frame_address (0) |
129 | # define STACKLEVEL __builtin_frame_address (0) |
123 | #else |
130 | #else |
124 | # define dSTACKLEVEL volatile void *stacklevel = (volatile void *)&stacklevel |
131 | # define dSTACKLEVEL volatile void *stacklevel |
|
|
132 | # define STACKLEVEL ((void *)&stacklevel) |
125 | #endif |
133 | #endif |
126 | |
134 | |
127 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
135 | #define IN_DESTRUCT (PL_main_cv == Nullcv) |
128 | |
136 | |
129 | #if __GNUC__ >= 3 |
137 | #if __GNUC__ >= 3 |
… | |
… | |
140 | #define expect_true(expr) expect ((expr) != 0, 1) |
148 | #define expect_true(expr) expect ((expr) != 0, 1) |
141 | |
149 | |
142 | #define NOINLINE attribute ((noinline)) |
150 | #define NOINLINE attribute ((noinline)) |
143 | |
151 | |
144 | #include "CoroAPI.h" |
152 | #include "CoroAPI.h" |
|
|
153 | #define GCoroAPI (&coroapi) /* very sneaky */ |
145 | |
154 | |
146 | #ifdef USE_ITHREADS |
155 | #ifdef USE_ITHREADS |
147 | # if CORO_PTHREAD |
156 | # if CORO_PTHREAD |
148 | static void *coro_thx; |
157 | static void *coro_thx; |
149 | # endif |
158 | # endif |
150 | #endif |
159 | #endif |
151 | |
160 | |
152 | static double (*nvtime)(); /* so why doesn't it take void? */ |
161 | static double (*nvtime)(); /* so why doesn't it take void? */ |
|
|
162 | |
|
|
163 | /* we hijack an hopefully unused CV flag for our purposes */ |
|
|
164 | #define CVf_SLF 0x4000 |
|
|
165 | static OP *pp_slf (pTHX); |
153 | |
166 | |
154 | static U32 cctx_gen; |
167 | static U32 cctx_gen; |
155 | static size_t cctx_stacksize = CORO_STACKSIZE; |
168 | static size_t cctx_stacksize = CORO_STACKSIZE; |
156 | static struct CoroAPI coroapi; |
169 | static struct CoroAPI coroapi; |
157 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
170 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
… | |
… | |
166 | static HV *hv_sig; /* %SIG */ |
179 | static HV *hv_sig; /* %SIG */ |
167 | |
180 | |
168 | /* async_pool helper stuff */ |
181 | /* async_pool helper stuff */ |
169 | static SV *sv_pool_rss; |
182 | static SV *sv_pool_rss; |
170 | static SV *sv_pool_size; |
183 | static SV *sv_pool_size; |
|
|
184 | static SV *sv_async_pool_idle; |
171 | 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; |
172 | |
189 | |
173 | /* Coro::AnyEvent */ |
190 | /* Coro::AnyEvent */ |
174 | static SV *sv_activity; |
191 | static SV *sv_activity; |
175 | |
192 | |
176 | static struct coro_cctx *cctx_first; |
193 | static struct coro_cctx *cctx_first; |
… | |
… | |
236 | /* state data */ |
253 | /* state data */ |
237 | struct CoroSLF slf_frame; /* saved slf frame */ |
254 | struct CoroSLF slf_frame; /* saved slf frame */ |
238 | AV *mainstack; |
255 | AV *mainstack; |
239 | perl_slots *slot; /* basically the saved sp */ |
256 | perl_slots *slot; /* basically the saved sp */ |
240 | |
257 | |
|
|
258 | CV *startcv; /* the CV to execute */ |
241 | AV *args; /* data associated with this coroutine (initial args) */ |
259 | AV *args; /* data associated with this coroutine (initial args) */ |
242 | int refcnt; /* coroutines are refcounted, yes */ |
260 | int refcnt; /* coroutines are refcounted, yes */ |
243 | int flags; /* CF_ flags */ |
261 | int flags; /* CF_ flags */ |
244 | HV *hv; /* the perl hash associated with this coro, if any */ |
262 | HV *hv; /* the perl hash associated with this coro, if any */ |
245 | void (*on_destroy)(pTHX_ struct coro *coro); |
263 | void (*on_destroy)(pTHX_ struct coro *coro); |
246 | |
264 | |
247 | /* statistics */ |
265 | /* statistics */ |
248 | int usecount; /* number of transfers to this coro */ |
266 | int usecount; /* number of transfers to this coro */ |
249 | |
267 | |
250 | /* coro process data */ |
268 | /* coro process data */ |
251 | int prio; |
269 | int prio; |
252 | SV *throw; /* exception to be thrown */ |
270 | SV *except; /* exception to be thrown */ |
|
|
271 | SV *rouse_cb; |
253 | |
272 | |
254 | /* async_pool */ |
273 | /* async_pool */ |
255 | SV *saved_deffh; |
274 | SV *saved_deffh; |
|
|
275 | SV *invoke_cb; |
|
|
276 | AV *invoke_av; |
256 | |
277 | |
257 | /* linked list */ |
278 | /* linked list */ |
258 | struct coro *next, *prev; |
279 | struct coro *next, *prev; |
259 | }; |
280 | }; |
260 | |
281 | |
… | |
… | |
263 | |
284 | |
264 | /* the following variables are effectively part of the perl context */ |
285 | /* the following variables are effectively part of the perl context */ |
265 | /* and get copied between struct coro and these variables */ |
286 | /* and get copied between struct coro and these variables */ |
266 | /* the mainr easonw e don't support windows process emulation */ |
287 | /* the mainr easonw e don't support windows process emulation */ |
267 | static struct CoroSLF slf_frame; /* the current slf frame */ |
288 | static struct CoroSLF slf_frame; /* the current slf frame */ |
268 | static SV *coro_throw; |
|
|
269 | |
289 | |
270 | /** Coro ********************************************************************/ |
290 | /** Coro ********************************************************************/ |
271 | |
291 | |
272 | #define PRIO_MAX 3 |
292 | #define PRIO_MAX 3 |
273 | #define PRIO_HIGH 1 |
293 | #define PRIO_HIGH 1 |
… | |
… | |
278 | |
298 | |
279 | /* for Coro.pm */ |
299 | /* for Coro.pm */ |
280 | static SV *coro_current; |
300 | static SV *coro_current; |
281 | static SV *coro_readyhook; |
301 | static SV *coro_readyhook; |
282 | 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; |
283 | static struct coro *coro_first; |
304 | static struct coro *coro_first; |
284 | #define coro_nready coroapi.nready |
305 | #define coro_nready coroapi.nready |
285 | |
306 | |
286 | /** lowlevel stuff **********************************************************/ |
307 | /** lowlevel stuff **********************************************************/ |
287 | |
308 | |
… | |
… | |
311 | #if PERL_VERSION_ATLEAST (5,10,0) |
332 | #if PERL_VERSION_ATLEAST (5,10,0) |
312 | /* 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 */ |
313 | get_hv (name, create); |
334 | get_hv (name, create); |
314 | #endif |
335 | #endif |
315 | 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); |
316 | } |
346 | } |
317 | |
347 | |
318 | static AV * |
348 | static AV * |
319 | coro_clone_padlist (pTHX_ CV *cv) |
349 | coro_clone_padlist (pTHX_ CV *cv) |
320 | { |
350 | { |
… | |
… | |
439 | else |
469 | else |
440 | { |
470 | { |
441 | #if CORO_PREFER_PERL_FUNCTIONS |
471 | #if CORO_PREFER_PERL_FUNCTIONS |
442 | /* this is probably cleaner? but also slower! */ |
472 | /* this is probably cleaner? but also slower! */ |
443 | /* in practise, it seems to be less stable */ |
473 | /* in practise, it seems to be less stable */ |
444 | CV *cp = Perl_cv_clone (cv); |
474 | CV *cp = Perl_cv_clone (aTHX_ cv); |
445 | CvPADLIST (cv) = CvPADLIST (cp); |
475 | CvPADLIST (cv) = CvPADLIST (cp); |
446 | CvPADLIST (cp) = 0; |
476 | CvPADLIST (cp) = 0; |
447 | SvREFCNT_dec (cp); |
477 | SvREFCNT_dec (cp); |
448 | #else |
478 | #else |
449 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
479 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
… | |
… | |
502 | |
532 | |
503 | PUTBACK; |
533 | PUTBACK; |
504 | } |
534 | } |
505 | |
535 | |
506 | slf_frame = c->slf_frame; |
536 | slf_frame = c->slf_frame; |
507 | coro_throw = c->throw; |
537 | CORO_THROW = c->except; |
508 | } |
538 | } |
509 | |
539 | |
510 | static void |
540 | static void |
511 | save_perl (pTHX_ Coro__State c) |
541 | save_perl (pTHX_ Coro__State c) |
512 | { |
542 | { |
513 | c->throw = coro_throw; |
543 | c->except = CORO_THROW; |
514 | c->slf_frame = slf_frame; |
544 | c->slf_frame = slf_frame; |
515 | |
545 | |
516 | { |
546 | { |
517 | dSP; |
547 | dSP; |
518 | I32 cxix = cxstack_ix; |
548 | I32 cxix = cxstack_ix; |
… | |
… | |
593 | * of perl.c:init_stacks, except that it uses less memory |
623 | * of perl.c:init_stacks, except that it uses less memory |
594 | * on the (sometimes correct) assumption that coroutines do |
624 | * on the (sometimes correct) assumption that coroutines do |
595 | * not usually need a lot of stackspace. |
625 | * not usually need a lot of stackspace. |
596 | */ |
626 | */ |
597 | #if CORO_PREFER_PERL_FUNCTIONS |
627 | #if CORO_PREFER_PERL_FUNCTIONS |
598 | # define coro_init_stacks init_stacks |
628 | # define coro_init_stacks(thx) init_stacks () |
599 | #else |
629 | #else |
600 | static void |
630 | static void |
601 | coro_init_stacks (pTHX) |
631 | coro_init_stacks (pTHX) |
602 | { |
632 | { |
603 | PL_curstackinfo = new_stackinfo(32, 8); |
633 | PL_curstackinfo = new_stackinfo(32, 8); |
… | |
… | |
666 | #if !PERL_VERSION_ATLEAST (5,10,0) |
696 | #if !PERL_VERSION_ATLEAST (5,10,0) |
667 | Safefree (PL_retstack); |
697 | Safefree (PL_retstack); |
668 | #endif |
698 | #endif |
669 | } |
699 | } |
670 | |
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 | |
671 | static size_t |
710 | static size_t |
672 | coro_rss (pTHX_ struct coro *coro) |
711 | coro_rss (pTHX_ struct coro *coro) |
673 | { |
712 | { |
674 | size_t rss = sizeof (*coro); |
713 | size_t rss = sizeof (*coro); |
675 | |
714 | |
676 | if (coro->mainstack) |
715 | if (coro->mainstack) |
677 | { |
716 | { |
678 | perl_slots tmp_slot; |
|
|
679 | perl_slots *slot; |
|
|
680 | |
|
|
681 | if (coro->flags & CF_RUNNING) |
717 | if (coro->flags & CF_RUNNING) |
682 | { |
718 | { |
683 | slot = &tmp_slot; |
719 | #define SYM(sym) PL_ ## sym |
684 | |
720 | CORO_RSS; |
685 | #define VAR(name,type) slot->name = PL_ ## name; |
|
|
686 | # include "state.h" |
|
|
687 | #undef VAR |
721 | #undef SYM |
688 | } |
722 | } |
689 | else |
723 | else |
690 | slot = coro->slot; |
|
|
691 | |
|
|
692 | if (slot) |
|
|
693 | { |
724 | { |
694 | rss += sizeof (slot->curstackinfo); |
725 | #define SYM(sym) coro->slot->sym |
695 | rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
726 | CORO_RSS; |
696 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); |
727 | #undef SYM |
697 | rss += slot->tmps_max * sizeof (SV *); |
|
|
698 | rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); |
|
|
699 | rss += slot->scopestack_max * sizeof (I32); |
|
|
700 | rss += slot->savestack_max * sizeof (ANY); |
|
|
701 | |
|
|
702 | #if !PERL_VERSION_ATLEAST (5,10,0) |
|
|
703 | rss += slot->retstack_max * sizeof (OP *); |
|
|
704 | #endif |
|
|
705 | } |
728 | } |
706 | } |
729 | } |
707 | |
730 | |
708 | return rss; |
731 | return rss; |
709 | } |
732 | } |
… | |
… | |
811 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
834 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
812 | { |
835 | { |
813 | return 0; |
836 | return 0; |
814 | } |
837 | } |
815 | |
838 | |
|
|
839 | static UNOP coro_setup_op; |
|
|
840 | |
816 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
841 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
817 | coro_setup (pTHX_ struct coro *coro) |
842 | coro_setup (pTHX_ struct coro *coro) |
818 | { |
843 | { |
819 | /* |
844 | /* |
820 | * emulate part of the perl startup here. |
845 | * emulate part of the perl startup here. |
… | |
… | |
848 | { |
873 | { |
849 | dSP; |
874 | dSP; |
850 | UNOP myop; |
875 | UNOP myop; |
851 | |
876 | |
852 | Zero (&myop, 1, UNOP); |
877 | Zero (&myop, 1, UNOP); |
853 | myop.op_next = Nullop; |
878 | myop.op_next = Nullop; |
|
|
879 | myop.op_type = OP_ENTERSUB; |
854 | myop.op_flags = OPf_WANT_VOID; |
880 | myop.op_flags = OPf_WANT_VOID; |
855 | |
881 | |
856 | PUSHMARK (SP); |
882 | PUSHMARK (SP); |
857 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
883 | PUSHs ((SV *)coro->startcv); |
858 | PUTBACK; |
884 | PUTBACK; |
859 | PL_op = (OP *)&myop; |
885 | PL_op = (OP *)&myop; |
860 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
886 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
861 | SPAGAIN; |
|
|
862 | } |
887 | } |
863 | |
888 | |
864 | /* 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 |
865 | * 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. |
866 | */ |
891 | */ |
867 | 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 */ |
868 | 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 */ |
869 | |
894 | |
870 | coro_throw = coro->throw; |
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; |
|
|
897 | coro_setup_op.op_type = OP_CUSTOM; |
|
|
898 | coro_setup_op.op_ppaddr = pp_slf; |
|
|
899 | /* no flags etc. required, as an init function won't be called */ |
|
|
900 | |
|
|
901 | PL_op = (OP *)&coro_setup_op; |
|
|
902 | |
|
|
903 | /* copy throw, in case it was set before coro_setup */ |
|
|
904 | CORO_THROW = coro->except; |
871 | } |
905 | } |
872 | |
906 | |
873 | static void |
907 | static void |
874 | coro_destruct (pTHX_ struct coro *coro) |
908 | coro_destruct (pTHX_ struct coro *coro) |
875 | { |
909 | { |
… | |
… | |
899 | |
933 | |
900 | SvREFCNT_dec (PL_diehook); |
934 | SvREFCNT_dec (PL_diehook); |
901 | SvREFCNT_dec (PL_warnhook); |
935 | SvREFCNT_dec (PL_warnhook); |
902 | |
936 | |
903 | SvREFCNT_dec (coro->saved_deffh); |
937 | SvREFCNT_dec (coro->saved_deffh); |
904 | SvREFCNT_dec (coro_throw); |
938 | SvREFCNT_dec (coro->rouse_cb); |
|
|
939 | SvREFCNT_dec (coro->invoke_cb); |
|
|
940 | SvREFCNT_dec (coro->invoke_av); |
905 | |
941 | |
906 | coro_destruct_stacks (aTHX); |
942 | coro_destruct_stacks (aTHX); |
907 | } |
943 | } |
908 | |
944 | |
909 | INLINE void |
945 | INLINE void |
… | |
… | |
996 | SAVETMPS; |
1032 | SAVETMPS; |
997 | EXTEND (SP, 3); |
1033 | EXTEND (SP, 3); |
998 | PUSHMARK (SP); |
1034 | PUSHMARK (SP); |
999 | PUSHs (&PL_sv_yes); |
1035 | PUSHs (&PL_sv_yes); |
1000 | PUSHs (fullname); |
1036 | PUSHs (fullname); |
1001 | 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); |
1002 | PUTBACK; |
1038 | PUTBACK; |
1003 | 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); |
1004 | 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); |
1005 | SPAGAIN; |
1041 | SPAGAIN; |
1006 | FREETMPS; |
1042 | FREETMPS; |
… | |
… | |
1038 | |
1074 | |
1039 | TAINT_NOT; |
1075 | TAINT_NOT; |
1040 | return 0; |
1076 | return 0; |
1041 | } |
1077 | } |
1042 | |
1078 | |
|
|
1079 | static struct coro_cctx *cctx_ssl_cctx; |
|
|
1080 | static struct CoroSLF cctx_ssl_frame; |
|
|
1081 | |
1043 | static void |
1082 | static void |
1044 | prepare_set_stacklevel (struct coro_transfer_args *ta, struct coro_cctx *cctx) |
1083 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
1045 | { |
1084 | { |
1046 | ta->prev = (struct coro *)cctx; |
1085 | ta->prev = (struct coro *)cctx_ssl_cctx; |
1047 | ta->next = 0; |
1086 | ta->next = 0; |
1048 | } |
1087 | } |
1049 | |
1088 | |
1050 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
1089 | static int |
1051 | /* _cctx_init should be careful, as it could be called at almost any time */ |
1090 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1052 | /* during execution of a perl program */ |
1091 | { |
1053 | /* also initialises PL_top_env */ |
1092 | *frame = cctx_ssl_frame; |
|
|
1093 | |
|
|
1094 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
|
|
1095 | } |
|
|
1096 | |
|
|
1097 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1054 | static void NOINLINE |
1098 | static void NOINLINE |
1055 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1099 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1056 | { |
1100 | { |
1057 | dSP; |
|
|
1058 | UNOP myop; |
|
|
1059 | |
|
|
1060 | PL_top_env = &PL_start_env; |
1101 | PL_top_env = &PL_start_env; |
1061 | |
1102 | |
1062 | if (cctx->flags & CC_TRACE) |
1103 | if (cctx->flags & CC_TRACE) |
1063 | PL_runops = runops_trace; |
1104 | PL_runops = runops_trace; |
1064 | |
1105 | |
1065 | Zero (&myop, 1, UNOP); |
1106 | /* we already must be executing an SLF op, there is no other valid way |
1066 | myop.op_next = PL_op; |
1107 | * that can lead to creation of a new cctx */ |
1067 | myop.op_flags = OPf_WANT_VOID | OPf_STACKED; |
1108 | assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", |
|
|
1109 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1068 | |
1110 | |
1069 | PUSHMARK (SP); |
1111 | /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ |
1070 | EXTEND (SP, 2); |
1112 | cctx_ssl_cctx = cctx; |
1071 | PUSHs (sv_2mortal (newSViv ((IV)cctx))); |
1113 | cctx_ssl_frame = slf_frame; |
1072 | PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); |
1114 | |
1073 | PUTBACK; |
1115 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1074 | PL_op = (OP *)&myop; |
1116 | slf_frame.check = slf_check_set_stacklevel; |
1075 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
|
|
1076 | SPAGAIN; |
|
|
1077 | } |
1117 | } |
1078 | |
1118 | |
1079 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1119 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1080 | INLINE void |
1120 | INLINE void |
1081 | transfer_tail (pTHX) |
1121 | transfer_tail (pTHX) |
… | |
… | |
1108 | transfer_tail (aTHX); |
1148 | transfer_tail (aTHX); |
1109 | |
1149 | |
1110 | /* 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 */ |
1111 | PL_restartop = PL_op; |
1151 | PL_restartop = PL_op; |
1112 | 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 | */ |
1113 | |
1157 | |
1114 | /* |
1158 | /* |
1115 | * 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 |
1116 | * 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) |
1117 | * 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 |
… | |
… | |
1297 | dSTACKLEVEL; |
1341 | dSTACKLEVEL; |
1298 | |
1342 | |
1299 | /* sometimes transfer is only called to set idle_sp */ |
1343 | /* sometimes transfer is only called to set idle_sp */ |
1300 | if (expect_false (!next)) |
1344 | if (expect_false (!next)) |
1301 | { |
1345 | { |
1302 | ((coro_cctx *)prev)->idle_sp = (void *)stacklevel; |
1346 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1303 | 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 */ |
1304 | } |
1348 | } |
1305 | else if (expect_true (prev != next)) |
1349 | else if (expect_true (prev != next)) |
1306 | { |
1350 | { |
1307 | coro_cctx *prev__cctx; |
1351 | coro_cctx *prev__cctx; |
… | |
… | |
1332 | |
1376 | |
1333 | prev__cctx = prev->cctx; |
1377 | prev__cctx = prev->cctx; |
1334 | |
1378 | |
1335 | /* possibly untie and reuse the cctx */ |
1379 | /* possibly untie and reuse the cctx */ |
1336 | if (expect_true ( |
1380 | if (expect_true ( |
1337 | prev__cctx->idle_sp == (void *)stacklevel |
1381 | prev__cctx->idle_sp == STACKLEVEL |
1338 | && !(prev__cctx->flags & CC_TRACE) |
1382 | && !(prev__cctx->flags & CC_TRACE) |
1339 | && !force_cctx |
1383 | && !force_cctx |
1340 | )) |
1384 | )) |
1341 | { |
1385 | { |
1342 | /* 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 */ |
… | |
… | |
1409 | |
1453 | |
1410 | coro->slot = 0; |
1454 | coro->slot = 0; |
1411 | } |
1455 | } |
1412 | |
1456 | |
1413 | cctx_destroy (coro->cctx); |
1457 | cctx_destroy (coro->cctx); |
|
|
1458 | SvREFCNT_dec (coro->startcv); |
1414 | SvREFCNT_dec (coro->args); |
1459 | SvREFCNT_dec (coro->args); |
|
|
1460 | SvREFCNT_dec (CORO_THROW); |
1415 | |
1461 | |
1416 | if (coro->next) coro->next->prev = coro->prev; |
1462 | if (coro->next) coro->next->prev = coro->prev; |
1417 | if (coro->prev) coro->prev->next = coro->next; |
1463 | if (coro->prev) coro->prev->next = coro->next; |
1418 | if (coro == coro_first) coro_first = coro->next; |
1464 | if (coro == coro_first) coro_first = coro->next; |
1419 | |
1465 | |
… | |
… | |
1473 | |
1519 | |
1474 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1520 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1475 | TRANSFER (ta, 1); |
1521 | TRANSFER (ta, 1); |
1476 | } |
1522 | } |
1477 | |
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 | |
1478 | /** Coro ********************************************************************/ |
1547 | /** Coro ********************************************************************/ |
1479 | |
1548 | |
1480 | INLINE void |
1549 | INLINE void |
1481 | coro_enq (pTHX_ struct coro *coro) |
1550 | coro_enq (pTHX_ struct coro *coro) |
1482 | { |
1551 | { |
… | |
… | |
1525 | ENTER; |
1594 | ENTER; |
1526 | SAVETMPS; |
1595 | SAVETMPS; |
1527 | |
1596 | |
1528 | PUSHMARK (SP); |
1597 | PUSHMARK (SP); |
1529 | PUTBACK; |
1598 | PUTBACK; |
1530 | call_sv (sv_hook, G_DISCARD); |
1599 | call_sv (sv_hook, G_VOID | G_DISCARD); |
1531 | SPAGAIN; |
|
|
1532 | |
1600 | |
1533 | FREETMPS; |
1601 | FREETMPS; |
1534 | LEAVE; |
1602 | LEAVE; |
1535 | } |
1603 | } |
1536 | |
1604 | |
… | |
… | |
1544 | api_is_ready (pTHX_ SV *coro_sv) |
1612 | api_is_ready (pTHX_ SV *coro_sv) |
1545 | { |
1613 | { |
1546 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1614 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1547 | } |
1615 | } |
1548 | |
1616 | |
|
|
1617 | /* expects to own a reference to next->hv */ |
1549 | INLINE void |
1618 | INLINE void |
|
|
1619 | prepare_cede_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, next->hv); |
|
|
1629 | |
|
|
1630 | free_coro_mortal (aTHX); |
|
|
1631 | coro_mortal = prev_sv; |
|
|
1632 | } |
|
|
1633 | |
|
|
1634 | static void |
1550 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1635 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1551 | { |
1636 | { |
1552 | SV *prev_sv, *next_sv; |
|
|
1553 | |
|
|
1554 | for (;;) |
1637 | for (;;) |
1555 | { |
1638 | { |
1556 | next_sv = coro_deq (aTHX); |
1639 | SV *next_sv = coro_deq (aTHX); |
1557 | |
1640 | |
1558 | /* nothing to schedule: call the idle handler */ |
|
|
1559 | if (expect_false (!next_sv)) |
1641 | if (expect_false (!next_sv)) |
1560 | { |
1642 | { |
|
|
1643 | /* nothing to schedule: call the idle handler */ |
1561 | dSP; |
1644 | dSP; |
1562 | |
1645 | |
1563 | ENTER; |
1646 | ENTER; |
1564 | SAVETMPS; |
1647 | SAVETMPS; |
1565 | |
1648 | |
1566 | PUSHMARK (SP); |
1649 | PUSHMARK (SP); |
1567 | PUTBACK; |
1650 | PUTBACK; |
1568 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
1651 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
1569 | SPAGAIN; |
|
|
1570 | |
1652 | |
1571 | FREETMPS; |
1653 | FREETMPS; |
1572 | LEAVE; |
1654 | LEAVE; |
1573 | continue; |
|
|
1574 | } |
1655 | } |
1575 | |
1656 | else |
1576 | ta->next = SvSTATE_hv (next_sv); |
|
|
1577 | |
|
|
1578 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1579 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
|
|
1580 | { |
1657 | { |
1581 | SvREFCNT_dec (next_sv); |
1658 | struct coro *next = SvSTATE_hv (next_sv); |
|
|
1659 | |
|
|
1660 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1661 | if (expect_false (next->flags & CF_DESTROYED)) |
1582 | /* coro_nready has already been taken care of by destroy */ |
1662 | SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ |
1583 | continue; |
1663 | else |
|
|
1664 | { |
|
|
1665 | next->flags &= ~CF_READY; |
|
|
1666 | --coro_nready; |
|
|
1667 | |
|
|
1668 | return prepare_cede_to (aTHX_ ta, next); |
|
|
1669 | } |
1584 | } |
1670 | } |
1585 | |
|
|
1586 | --coro_nready; |
|
|
1587 | break; |
|
|
1588 | } |
1671 | } |
1589 | |
|
|
1590 | /* free this only after the transfer */ |
|
|
1591 | prev_sv = SvRV (coro_current); |
|
|
1592 | ta->prev = SvSTATE_hv (prev_sv); |
|
|
1593 | TRANSFER_CHECK (*ta); |
|
|
1594 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
|
|
1595 | ta->next->flags &= ~CF_READY; |
|
|
1596 | SvRV_set (coro_current, next_sv); |
|
|
1597 | |
|
|
1598 | free_coro_mortal (aTHX); |
|
|
1599 | coro_mortal = prev_sv; |
|
|
1600 | } |
1672 | } |
1601 | |
1673 | |
1602 | INLINE void |
1674 | INLINE void |
1603 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1675 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1604 | { |
1676 | { |
… | |
… | |
1681 | if (coro->flags & CF_RUNNING) |
1753 | if (coro->flags & CF_RUNNING) |
1682 | PL_runops = RUNOPS_DEFAULT; |
1754 | PL_runops = RUNOPS_DEFAULT; |
1683 | else |
1755 | else |
1684 | coro->slot->runops = RUNOPS_DEFAULT; |
1756 | coro->slot->runops = RUNOPS_DEFAULT; |
1685 | } |
1757 | } |
|
|
1758 | } |
|
|
1759 | |
|
|
1760 | /*****************************************************************************/ |
|
|
1761 | /* async pool handler */ |
|
|
1762 | |
|
|
1763 | static int |
|
|
1764 | slf_check_pool_handler (pTHX_ struct CoroSLF *frame) |
|
|
1765 | { |
|
|
1766 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1767 | struct coro *coro = (struct coro *)frame->data; |
|
|
1768 | |
|
|
1769 | if (!coro->invoke_cb) |
|
|
1770 | return 1; /* loop till we have invoke */ |
|
|
1771 | else |
|
|
1772 | { |
|
|
1773 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
1774 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
1775 | |
|
|
1776 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
|
|
1777 | |
|
|
1778 | { |
|
|
1779 | dSP; |
|
|
1780 | XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0; |
|
|
1781 | PUTBACK; |
|
|
1782 | } |
|
|
1783 | |
|
|
1784 | SvREFCNT_dec (GvAV (PL_defgv)); |
|
|
1785 | GvAV (PL_defgv) = coro->invoke_av; |
|
|
1786 | coro->invoke_av = 0; |
|
|
1787 | |
|
|
1788 | return 0; |
|
|
1789 | } |
|
|
1790 | } |
|
|
1791 | |
|
|
1792 | static void |
|
|
1793 | slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1794 | { |
|
|
1795 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1796 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
1797 | |
|
|
1798 | if (expect_true (coro->saved_deffh)) |
|
|
1799 | { |
|
|
1800 | /* subsequent iteration */ |
|
|
1801 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
|
|
1802 | coro->saved_deffh = 0; |
|
|
1803 | |
|
|
1804 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
|
|
1805 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
1806 | { |
|
|
1807 | coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate); |
|
|
1808 | coro->invoke_av = newAV (); |
|
|
1809 | |
|
|
1810 | frame->prepare = prepare_nop; |
|
|
1811 | } |
|
|
1812 | else |
|
|
1813 | { |
|
|
1814 | av_clear (GvAV (PL_defgv)); |
|
|
1815 | hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); |
|
|
1816 | |
|
|
1817 | coro->prio = 0; |
|
|
1818 | |
|
|
1819 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
|
|
1820 | api_trace (aTHX_ coro_current, 0); |
|
|
1821 | |
|
|
1822 | frame->prepare = prepare_schedule; |
|
|
1823 | av_push (av_async_pool, SvREFCNT_inc (hv)); |
|
|
1824 | } |
|
|
1825 | } |
|
|
1826 | else |
|
|
1827 | { |
|
|
1828 | /* first iteration, simply fall through */ |
|
|
1829 | frame->prepare = prepare_nop; |
|
|
1830 | } |
|
|
1831 | |
|
|
1832 | frame->check = slf_check_pool_handler; |
|
|
1833 | frame->data = (void *)coro; |
|
|
1834 | } |
|
|
1835 | |
|
|
1836 | /*****************************************************************************/ |
|
|
1837 | /* rouse callback */ |
|
|
1838 | |
|
|
1839 | #define CORO_MAGIC_type_rouse PERL_MAGIC_ext |
|
|
1840 | |
|
|
1841 | static void |
|
|
1842 | coro_rouse_callback (pTHX_ CV *cv) |
|
|
1843 | { |
|
|
1844 | dXSARGS; |
|
|
1845 | SV *data = (SV *)GENSUB_ARG; |
|
|
1846 | |
|
|
1847 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
|
|
1848 | { |
|
|
1849 | /* first call, set args */ |
|
|
1850 | AV *av = newAV (); |
|
|
1851 | SV *coro = SvRV (data); |
|
|
1852 | |
|
|
1853 | SvRV_set (data, (SV *)av); |
|
|
1854 | api_ready (aTHX_ coro); |
|
|
1855 | SvREFCNT_dec (coro); |
|
|
1856 | |
|
|
1857 | /* better take a full copy of the arguments */ |
|
|
1858 | while (items--) |
|
|
1859 | av_store (av, items, newSVsv (ST (items))); |
|
|
1860 | } |
|
|
1861 | |
|
|
1862 | XSRETURN_EMPTY; |
|
|
1863 | } |
|
|
1864 | |
|
|
1865 | static int |
|
|
1866 | slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) |
|
|
1867 | { |
|
|
1868 | SV *data = (SV *)frame->data; |
|
|
1869 | |
|
|
1870 | if (CORO_THROW) |
|
|
1871 | return 0; |
|
|
1872 | |
|
|
1873 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
|
|
1874 | return 1; |
|
|
1875 | |
|
|
1876 | /* now push all results on the stack */ |
|
|
1877 | { |
|
|
1878 | dSP; |
|
|
1879 | AV *av = (AV *)SvRV (data); |
|
|
1880 | int i; |
|
|
1881 | |
|
|
1882 | EXTEND (SP, AvFILLp (av) + 1); |
|
|
1883 | for (i = 0; i <= AvFILLp (av); ++i) |
|
|
1884 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
|
|
1885 | |
|
|
1886 | /* we have stolen the elements, so ste length to zero and free */ |
|
|
1887 | AvFILLp (av) = -1; |
|
|
1888 | av_undef (av); |
|
|
1889 | |
|
|
1890 | PUTBACK; |
|
|
1891 | } |
|
|
1892 | |
|
|
1893 | return 0; |
|
|
1894 | } |
|
|
1895 | |
|
|
1896 | static void |
|
|
1897 | slf_init_rouse_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1898 | { |
|
|
1899 | SV *cb; |
|
|
1900 | |
|
|
1901 | if (items) |
|
|
1902 | cb = arg [0]; |
|
|
1903 | else |
|
|
1904 | { |
|
|
1905 | struct coro *coro = SvSTATE_current; |
|
|
1906 | |
|
|
1907 | if (!coro->rouse_cb) |
|
|
1908 | croak ("Coro::rouse_wait called without rouse callback, and no default rouse callback found either,"); |
|
|
1909 | |
|
|
1910 | cb = sv_2mortal (coro->rouse_cb); |
|
|
1911 | coro->rouse_cb = 0; |
|
|
1912 | } |
|
|
1913 | |
|
|
1914 | if (!SvROK (cb) |
|
|
1915 | || SvTYPE (SvRV (cb)) != SVt_PVCV |
|
|
1916 | || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) |
|
|
1917 | croak ("Coro::rouse_wait called with illegal callback argument,"); |
|
|
1918 | |
|
|
1919 | { |
|
|
1920 | CV *cv = (CV *)SvRV (cb); /* for GENSUB_ARG */ |
|
|
1921 | SV *data = (SV *)GENSUB_ARG; |
|
|
1922 | |
|
|
1923 | frame->data = (void *)data; |
|
|
1924 | frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule; |
|
|
1925 | frame->check = slf_check_rouse_wait; |
|
|
1926 | } |
|
|
1927 | } |
|
|
1928 | |
|
|
1929 | static SV * |
|
|
1930 | coro_new_rouse_cb (pTHX) |
|
|
1931 | { |
|
|
1932 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1933 | struct coro *coro = SvSTATE_hv (hv); |
|
|
1934 | SV *data = newRV_inc ((SV *)hv); |
|
|
1935 | SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data); |
|
|
1936 | |
|
|
1937 | sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0); |
|
|
1938 | SvREFCNT_dec (data); /* magicext increases the refcount */ |
|
|
1939 | |
|
|
1940 | SvREFCNT_dec (coro->rouse_cb); |
|
|
1941 | coro->rouse_cb = SvREFCNT_inc_NN (cb); |
|
|
1942 | |
|
|
1943 | return cb; |
1686 | } |
1944 | } |
1687 | |
1945 | |
1688 | /*****************************************************************************/ |
1946 | /*****************************************************************************/ |
1689 | /* schedule-like-function opcode (SLF) */ |
1947 | /* schedule-like-function opcode (SLF) */ |
1690 | |
1948 | |
… | |
… | |
1714 | |
1972 | |
1715 | RETURNOP (slf_restore.op_first); |
1973 | RETURNOP (slf_restore.op_first); |
1716 | } |
1974 | } |
1717 | |
1975 | |
1718 | static void |
1976 | static void |
1719 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
|
|
1720 | { |
|
|
1721 | prepare_set_stacklevel (ta, (struct coro_cctx *)slf_frame.data); |
|
|
1722 | } |
|
|
1723 | |
|
|
1724 | static void |
|
|
1725 | slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1726 | { |
|
|
1727 | assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1)); |
|
|
1728 | |
|
|
1729 | frame->prepare = slf_prepare_set_stacklevel; |
|
|
1730 | frame->check = slf_check_nop; |
|
|
1731 | frame->data = (void *)SvIV (arg [0]); |
|
|
1732 | } |
|
|
1733 | |
|
|
1734 | static void |
|
|
1735 | slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta) |
1977 | slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta) |
1736 | { |
1978 | { |
1737 | SV **arg = (SV **)slf_frame.data; |
1979 | SV **arg = (SV **)slf_frame.data; |
1738 | |
1980 | |
1739 | prepare_transfer (aTHX_ ta, arg [0], arg [1]); |
1981 | prepare_transfer (aTHX_ ta, arg [0], arg [1]); |
… | |
… | |
1768 | slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2010 | slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1769 | { |
2011 | { |
1770 | frame->prepare = prepare_cede_notself; |
2012 | frame->prepare = prepare_cede_notself; |
1771 | frame->check = slf_check_nop; |
2013 | frame->check = slf_check_nop; |
1772 | } |
2014 | } |
1773 | |
|
|
1774 | /* we hijack an hopefully unused CV flag for our purposes */ |
|
|
1775 | #define CVf_SLF 0x4000 |
|
|
1776 | |
2015 | |
1777 | /* |
2016 | /* |
1778 | * these not obviously related functions are all rolled into one |
2017 | * these not obviously related functions are all rolled into one |
1779 | * function to increase chances that they all will call transfer with the same |
2018 | * function to increase chances that they all will call transfer with the same |
1780 | * stack offset |
2019 | * stack offset |
… | |
… | |
1836 | } |
2075 | } |
1837 | while (slf_frame.check (aTHX_ &slf_frame)); |
2076 | while (slf_frame.check (aTHX_ &slf_frame)); |
1838 | |
2077 | |
1839 | slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ |
2078 | slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ |
1840 | |
2079 | |
1841 | /* return value handling - mostly like entersub */ |
|
|
1842 | { |
|
|
1843 | dSP; |
|
|
1844 | SV **bot = PL_stack_base + checkmark; |
|
|
1845 | int gimme = GIMME_V; |
|
|
1846 | |
|
|
1847 | /* make sure we put something on the stack in scalar context */ |
|
|
1848 | if (gimme == G_SCALAR) |
|
|
1849 | { |
|
|
1850 | if (sp == bot) |
|
|
1851 | XPUSHs (&PL_sv_undef); |
|
|
1852 | |
|
|
1853 | SP = bot + 1; |
|
|
1854 | } |
|
|
1855 | |
|
|
1856 | PUTBACK; |
|
|
1857 | } |
|
|
1858 | |
|
|
1859 | /* exception handling */ |
2080 | /* exception handling */ |
1860 | if (expect_false (coro_throw)) |
2081 | if (expect_false (CORO_THROW)) |
1861 | { |
2082 | { |
1862 | SV *exception = sv_2mortal (coro_throw); |
2083 | SV *exception = sv_2mortal (CORO_THROW); |
1863 | |
2084 | |
1864 | coro_throw = 0; |
2085 | CORO_THROW = 0; |
1865 | sv_setsv (ERRSV, exception); |
2086 | sv_setsv (ERRSV, exception); |
1866 | croak (0); |
2087 | croak (0); |
|
|
2088 | } |
|
|
2089 | |
|
|
2090 | /* return value handling - mostly like entersub */ |
|
|
2091 | /* make sure we put something on the stack in scalar context */ |
|
|
2092 | if (GIMME_V == G_SCALAR) |
|
|
2093 | { |
|
|
2094 | dSP; |
|
|
2095 | SV **bot = PL_stack_base + checkmark; |
|
|
2096 | |
|
|
2097 | if (sp == bot) /* too few, push undef */ |
|
|
2098 | bot [1] = &PL_sv_undef; |
|
|
2099 | else if (sp != bot + 1) /* too many, take last one */ |
|
|
2100 | bot [1] = *sp; |
|
|
2101 | |
|
|
2102 | SP = bot + 1; |
|
|
2103 | |
|
|
2104 | PUTBACK; |
1867 | } |
2105 | } |
1868 | |
2106 | |
1869 | return NORMAL; |
2107 | return NORMAL; |
1870 | } |
2108 | } |
1871 | |
2109 | |
… | |
… | |
1993 | PerlIOBuf_get_cnt, |
2231 | PerlIOBuf_get_cnt, |
1994 | PerlIOBuf_set_ptrcnt, |
2232 | PerlIOBuf_set_ptrcnt, |
1995 | }; |
2233 | }; |
1996 | |
2234 | |
1997 | /*****************************************************************************/ |
2235 | /*****************************************************************************/ |
|
|
2236 | /* Coro::Semaphore & Coro::Signal */ |
|
|
2237 | |
|
|
2238 | static SV * |
|
|
2239 | coro_waitarray_new (pTHX_ int count) |
|
|
2240 | { |
|
|
2241 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
|
|
2242 | AV *av = newAV (); |
|
|
2243 | SV **ary; |
|
|
2244 | |
|
|
2245 | /* unfortunately, building manually saves memory */ |
|
|
2246 | Newx (ary, 2, SV *); |
|
|
2247 | AvALLOC (av) = ary; |
|
|
2248 | /*AvARRAY (av) = ary;*/ |
|
|
2249 | SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ |
|
|
2250 | AvMAX (av) = 1; |
|
|
2251 | AvFILLp (av) = 0; |
|
|
2252 | ary [0] = newSViv (count); |
|
|
2253 | |
|
|
2254 | return newRV_noinc ((SV *)av); |
|
|
2255 | } |
|
|
2256 | |
1998 | /* Coro::Semaphore */ |
2257 | /* semaphore */ |
1999 | |
2258 | |
2000 | static void |
2259 | static void |
2001 | coro_semaphore_adjust (pTHX_ AV *av, IV adjust) |
2260 | coro_semaphore_adjust (pTHX_ AV *av, IV adjust) |
2002 | { |
2261 | { |
2003 | SV *count_sv = AvARRAY (av)[0]; |
2262 | SV *count_sv = AvARRAY (av)[0]; |
… | |
… | |
2015 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2274 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2016 | AvARRAY (av)[1] = count_sv; |
2275 | AvARRAY (av)[1] = count_sv; |
2017 | cb = av_shift (av); |
2276 | cb = av_shift (av); |
2018 | |
2277 | |
2019 | if (SvOBJECT (cb)) |
2278 | if (SvOBJECT (cb)) |
|
|
2279 | { |
2020 | api_ready (aTHX_ cb); |
2280 | api_ready (aTHX_ cb); |
2021 | else |
2281 | --count; |
2022 | croak ("callbacks not yet supported"); |
2282 | } |
|
|
2283 | else if (SvTYPE (cb) == SVt_PVCV) |
|
|
2284 | { |
|
|
2285 | dSP; |
|
|
2286 | PUSHMARK (SP); |
|
|
2287 | XPUSHs (sv_2mortal (newRV_inc ((SV *)av))); |
|
|
2288 | PUTBACK; |
|
|
2289 | call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR); |
|
|
2290 | } |
2023 | |
2291 | |
2024 | SvREFCNT_dec (cb); |
2292 | SvREFCNT_dec (cb); |
2025 | |
|
|
2026 | --count; |
|
|
2027 | } |
2293 | } |
2028 | } |
2294 | } |
2029 | |
2295 | |
2030 | static void |
2296 | static void |
2031 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
2297 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
… | |
… | |
2033 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
2299 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
2034 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
2300 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
2035 | } |
2301 | } |
2036 | |
2302 | |
2037 | static int |
2303 | static int |
2038 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
2304 | slf_check_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, int acquire) |
2039 | { |
2305 | { |
2040 | AV *av = (AV *)frame->data; |
2306 | AV *av = (AV *)frame->data; |
2041 | SV *count_sv = AvARRAY (av)[0]; |
2307 | SV *count_sv = AvARRAY (av)[0]; |
2042 | |
2308 | |
|
|
2309 | /* if we are about to throw, don't actually acquire the lock, just throw */ |
|
|
2310 | if (CORO_THROW) |
|
|
2311 | return 0; |
2043 | if (SvIVX (count_sv) > 0) |
2312 | else if (SvIVX (count_sv) > 0) |
2044 | { |
2313 | { |
2045 | SvSTATE_current->on_destroy = 0; |
2314 | SvSTATE_current->on_destroy = 0; |
|
|
2315 | |
|
|
2316 | if (acquire) |
2046 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2317 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
|
|
2318 | else |
|
|
2319 | coro_semaphore_adjust (aTHX_ av, 0); |
|
|
2320 | |
2047 | return 0; |
2321 | return 0; |
2048 | } |
2322 | } |
2049 | else |
2323 | else |
2050 | { |
2324 | { |
2051 | int i; |
2325 | int i; |
… | |
… | |
2060 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2334 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2061 | return 1; |
2335 | return 1; |
2062 | } |
2336 | } |
2063 | } |
2337 | } |
2064 | |
2338 | |
2065 | static void |
2339 | static int |
|
|
2340 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
|
|
2341 | { |
|
|
2342 | return slf_check_semaphore_down_or_wait (aTHX_ frame, 1); |
|
|
2343 | } |
|
|
2344 | |
|
|
2345 | static int |
|
|
2346 | slf_check_semaphore_wait (pTHX_ struct CoroSLF *frame) |
|
|
2347 | { |
|
|
2348 | return slf_check_semaphore_down_or_wait (aTHX_ frame, 0); |
|
|
2349 | } |
|
|
2350 | |
|
|
2351 | static void |
2066 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2352 | slf_init_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2067 | { |
2353 | { |
2068 | AV *av = (AV *)SvRV (arg [0]); |
2354 | AV *av = (AV *)SvRV (arg [0]); |
2069 | |
2355 | |
2070 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2356 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2071 | { |
2357 | { |
2072 | frame->data = (void *)av; |
2358 | frame->data = (void *)av; |
2073 | frame->prepare = prepare_nop; |
2359 | frame->prepare = prepare_nop; |
2074 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
|
|
2075 | } |
2360 | } |
2076 | else |
2361 | else |
2077 | { |
2362 | { |
2078 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2363 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2079 | |
2364 | |
2080 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2365 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2081 | frame->prepare = prepare_schedule; |
2366 | frame->prepare = prepare_schedule; |
2082 | |
2367 | |
2083 | /* to avoid race conditions when a woken-up coro gets terminated */ |
2368 | /* to avoid race conditions when a woken-up coro gets terminated */ |
2084 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
2369 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
2085 | assert (!SvSTATE_current->on_destroy);//D |
|
|
2086 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
2370 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
2087 | } |
2371 | } |
|
|
2372 | } |
2088 | |
2373 | |
|
|
2374 | static void |
|
|
2375 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2376 | { |
|
|
2377 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
2089 | frame->check = slf_check_semaphore_down; |
2378 | frame->check = slf_check_semaphore_down; |
2090 | |
|
|
2091 | } |
2379 | } |
2092 | |
2380 | |
2093 | /*****************************************************************************/ |
2381 | static void |
2094 | /* gensub: simple closure generation utility */ |
2382 | slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2095 | |
|
|
2096 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
|
|
2097 | |
|
|
2098 | /* create a closure from XS, returns a code reference */ |
|
|
2099 | /* the arg can be accessed via GENSUB_ARG from the callback */ |
|
|
2100 | /* the callback must use dXSARGS/XSRETURN */ |
|
|
2101 | static SV * |
|
|
2102 | gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
|
|
2103 | { |
2383 | { |
2104 | CV *cv = (CV *)newSV (0); |
2384 | if (items >= 2) |
|
|
2385 | { |
|
|
2386 | /* callback form */ |
|
|
2387 | AV *av = (AV *)SvRV (arg [0]); |
|
|
2388 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
2105 | |
2389 | |
2106 | sv_upgrade ((SV *)cv, SVt_PVCV); |
2390 | av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); |
2107 | |
2391 | |
2108 | CvANON_on (cv); |
2392 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2109 | CvISXSUB_on (cv); |
2393 | coro_semaphore_adjust (aTHX_ av, 0); |
2110 | CvXSUB (cv) = xsub; |
|
|
2111 | GENSUB_ARG = arg; |
|
|
2112 | |
2394 | |
2113 | return newRV_noinc ((SV *)cv); |
2395 | frame->prepare = prepare_nop; |
|
|
2396 | frame->check = slf_check_nop; |
|
|
2397 | } |
|
|
2398 | else |
|
|
2399 | { |
|
|
2400 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
|
|
2401 | frame->check = slf_check_semaphore_wait; |
|
|
2402 | } |
|
|
2403 | } |
|
|
2404 | |
|
|
2405 | /* signal */ |
|
|
2406 | |
|
|
2407 | static void |
|
|
2408 | coro_signal_wake (pTHX_ AV *av, int count) |
|
|
2409 | { |
|
|
2410 | SvIVX (AvARRAY (av)[0]) = 0; |
|
|
2411 | |
|
|
2412 | /* now signal count waiters */ |
|
|
2413 | while (count > 0 && AvFILLp (av) > 0) |
|
|
2414 | { |
|
|
2415 | SV *cb; |
|
|
2416 | |
|
|
2417 | /* swap first two elements so we can shift a waiter */ |
|
|
2418 | cb = AvARRAY (av)[0]; |
|
|
2419 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
|
|
2420 | AvARRAY (av)[1] = cb; |
|
|
2421 | |
|
|
2422 | cb = av_shift (av); |
|
|
2423 | |
|
|
2424 | api_ready (aTHX_ cb); |
|
|
2425 | sv_setiv (cb, 0); /* signal waiter */ |
|
|
2426 | SvREFCNT_dec (cb); |
|
|
2427 | |
|
|
2428 | --count; |
|
|
2429 | } |
|
|
2430 | } |
|
|
2431 | |
|
|
2432 | static int |
|
|
2433 | slf_check_signal_wait (pTHX_ struct CoroSLF *frame) |
|
|
2434 | { |
|
|
2435 | /* if we are about to throw, also stop waiting */ |
|
|
2436 | return SvROK ((SV *)frame->data) && !CORO_THROW; |
|
|
2437 | } |
|
|
2438 | |
|
|
2439 | static void |
|
|
2440 | slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2441 | { |
|
|
2442 | AV *av = (AV *)SvRV (arg [0]); |
|
|
2443 | |
|
|
2444 | if (SvIVX (AvARRAY (av)[0])) |
|
|
2445 | { |
|
|
2446 | SvIVX (AvARRAY (av)[0]) = 0; |
|
|
2447 | frame->prepare = prepare_nop; |
|
|
2448 | frame->check = slf_check_nop; |
|
|
2449 | } |
|
|
2450 | else |
|
|
2451 | { |
|
|
2452 | SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */ |
|
|
2453 | |
|
|
2454 | av_push (av, waiter); |
|
|
2455 | |
|
|
2456 | frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ |
|
|
2457 | frame->prepare = prepare_schedule; |
|
|
2458 | frame->check = slf_check_signal_wait; |
|
|
2459 | } |
2114 | } |
2460 | } |
2115 | |
2461 | |
2116 | /*****************************************************************************/ |
2462 | /*****************************************************************************/ |
2117 | /* Coro::AIO */ |
2463 | /* Coro::AIO */ |
2118 | |
2464 | |
… | |
… | |
2133 | dXSARGS; |
2479 | dXSARGS; |
2134 | AV *state = (AV *)GENSUB_ARG; |
2480 | AV *state = (AV *)GENSUB_ARG; |
2135 | SV *coro = av_pop (state); |
2481 | SV *coro = av_pop (state); |
2136 | SV *data_sv = newSV (sizeof (struct io_state)); |
2482 | SV *data_sv = newSV (sizeof (struct io_state)); |
2137 | |
2483 | |
2138 | av_extend (state, items); |
2484 | av_extend (state, items - 1); |
2139 | |
2485 | |
2140 | sv_upgrade (data_sv, SVt_PV); |
2486 | sv_upgrade (data_sv, SVt_PV); |
2141 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2487 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2142 | SvPOK_only (data_sv); |
2488 | SvPOK_only (data_sv); |
2143 | |
2489 | |
… | |
… | |
2168 | static int |
2514 | static int |
2169 | slf_check_aio_req (pTHX_ struct CoroSLF *frame) |
2515 | slf_check_aio_req (pTHX_ struct CoroSLF *frame) |
2170 | { |
2516 | { |
2171 | AV *state = (AV *)frame->data; |
2517 | AV *state = (AV *)frame->data; |
2172 | |
2518 | |
|
|
2519 | /* if we are about to throw, return early */ |
|
|
2520 | /* this does not cancel the aio request, but at least */ |
|
|
2521 | /* it quickly returns */ |
|
|
2522 | if (CORO_THROW) |
|
|
2523 | return 0; |
|
|
2524 | |
2173 | /* one element that is an RV? repeat! */ |
2525 | /* one element that is an RV? repeat! */ |
2174 | if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) |
2526 | if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) |
2175 | return 1; |
2527 | return 1; |
2176 | |
2528 | |
2177 | /* restore status */ |
2529 | /* restore status */ |
… | |
… | |
2247 | |
2599 | |
2248 | for (i = 0; i < items; ++i) |
2600 | for (i = 0; i < items; ++i) |
2249 | PUSHs (arg [i]); |
2601 | PUSHs (arg [i]); |
2250 | |
2602 | |
2251 | /* now push the callback closure */ |
2603 | /* now push the callback closure */ |
2252 | PUSHs (sv_2mortal (gensub (coro_aio_callback, (void *)SvREFCNT_inc_NN ((SV *)state)))); |
2604 | PUSHs (sv_2mortal (gensub (aTHX_ coro_aio_callback, (void *)SvREFCNT_inc_NN ((SV *)state)))); |
2253 | |
2605 | |
2254 | /* now call the AIO function - we assume our request is uncancelable */ |
2606 | /* now call the AIO function - we assume our request is uncancelable */ |
2255 | PUTBACK; |
2607 | PUTBACK; |
2256 | call_sv ((SV *)req, G_VOID | G_DISCARD); |
2608 | call_sv ((SV *)req, G_VOID | G_DISCARD); |
2257 | } |
2609 | } |
… | |
… | |
2263 | } |
2615 | } |
2264 | |
2616 | |
2265 | static void |
2617 | static void |
2266 | coro_aio_req_xs (pTHX_ CV *cv) |
2618 | coro_aio_req_xs (pTHX_ CV *cv) |
2267 | { |
2619 | { |
2268 | dVAR; |
|
|
2269 | dXSARGS; |
2620 | dXSARGS; |
2270 | |
2621 | |
2271 | CORO_EXECUTE_SLF_XS (slf_init_aio_req); |
2622 | CORO_EXECUTE_SLF_XS (slf_init_aio_req); |
2272 | |
2623 | |
2273 | XSRETURN_EMPTY; |
2624 | XSRETURN_EMPTY; |
… | |
… | |
2348 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2699 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2349 | } |
2700 | } |
2350 | |
2701 | |
2351 | SV * |
2702 | SV * |
2352 | new (char *klass, ...) |
2703 | new (char *klass, ...) |
|
|
2704 | ALIAS: |
|
|
2705 | Coro::new = 1 |
2353 | CODE: |
2706 | CODE: |
2354 | { |
2707 | { |
2355 | struct coro *coro; |
2708 | struct coro *coro; |
2356 | MAGIC *mg; |
2709 | MAGIC *mg; |
2357 | HV *hv; |
2710 | HV *hv; |
|
|
2711 | CV *cb; |
2358 | int i; |
2712 | int i; |
|
|
2713 | |
|
|
2714 | if (items > 1) |
|
|
2715 | { |
|
|
2716 | cb = coro_sv_2cv (aTHX_ ST (1)); |
|
|
2717 | |
|
|
2718 | if (!ix) |
|
|
2719 | { |
|
|
2720 | if (CvISXSUB (cb)) |
|
|
2721 | croak ("Coro::State doesn't support XS functions as coroutine start, caught"); |
|
|
2722 | |
|
|
2723 | if (!CvROOT (cb)) |
|
|
2724 | croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); |
|
|
2725 | } |
|
|
2726 | } |
2359 | |
2727 | |
2360 | Newz (0, coro, 1, struct coro); |
2728 | Newz (0, coro, 1, struct coro); |
2361 | coro->args = newAV (); |
2729 | coro->args = newAV (); |
2362 | coro->flags = CF_NEW; |
2730 | coro->flags = CF_NEW; |
2363 | |
2731 | |
… | |
… | |
2368 | coro->hv = hv = newHV (); |
2736 | coro->hv = hv = newHV (); |
2369 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2737 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2370 | mg->mg_flags |= MGf_DUP; |
2738 | mg->mg_flags |= MGf_DUP; |
2371 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2739 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2372 | |
2740 | |
|
|
2741 | if (items > 1) |
|
|
2742 | { |
2373 | av_extend (coro->args, items - 1); |
2743 | av_extend (coro->args, items - 1 + ix - 1); |
|
|
2744 | |
|
|
2745 | if (ix) |
|
|
2746 | { |
|
|
2747 | av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); |
|
|
2748 | cb = cv_coro_run; |
|
|
2749 | } |
|
|
2750 | |
|
|
2751 | coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); |
|
|
2752 | |
2374 | for (i = 1; i < items; i++) |
2753 | for (i = 2; i < items; i++) |
2375 | av_push (coro->args, newSVsv (ST (i))); |
2754 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2755 | } |
2376 | } |
2756 | } |
2377 | OUTPUT: |
2757 | OUTPUT: |
2378 | RETVAL |
2758 | RETVAL |
2379 | |
|
|
2380 | void |
|
|
2381 | _set_stacklevel (...) |
|
|
2382 | CODE: |
|
|
2383 | CORO_EXECUTE_SLF_XS (slf_init_set_stacklevel); |
|
|
2384 | |
2759 | |
2385 | void |
2760 | void |
2386 | transfer (...) |
2761 | transfer (...) |
2387 | PROTOTYPE: $$ |
2762 | PROTOTYPE: $$ |
2388 | CODE: |
2763 | CODE: |
… | |
… | |
2514 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
2889 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
2515 | PROTOTYPE: $;$ |
2890 | PROTOTYPE: $;$ |
2516 | CODE: |
2891 | CODE: |
2517 | { |
2892 | { |
2518 | struct coro *current = SvSTATE_current; |
2893 | struct coro *current = SvSTATE_current; |
2519 | SV **throwp = self == current ? &coro_throw : &self->throw; |
2894 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
2520 | SvREFCNT_dec (*throwp); |
2895 | SvREFCNT_dec (*throwp); |
2521 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
2896 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
2522 | } |
2897 | } |
2523 | |
2898 | |
2524 | void |
2899 | void |
… | |
… | |
2583 | |
2958 | |
2584 | BOOT: |
2959 | BOOT: |
2585 | { |
2960 | { |
2586 | int i; |
2961 | int i; |
2587 | |
2962 | |
2588 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
2963 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
2589 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2964 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2590 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2965 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2591 | |
2966 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
|
|
2967 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
2592 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
2968 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
2593 | SvREADONLY_on (coro_current); |
2969 | |
|
|
2970 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
|
|
2971 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
|
|
2972 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
|
|
2973 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
2594 | |
2974 | |
2595 | coro_stash = gv_stashpv ("Coro", TRUE); |
2975 | coro_stash = gv_stashpv ("Coro", TRUE); |
2596 | |
2976 | |
2597 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
2977 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
2598 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
2978 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
… | |
… | |
2613 | coroapi.ready = api_ready; |
2993 | coroapi.ready = api_ready; |
2614 | coroapi.is_ready = api_is_ready; |
2994 | coroapi.is_ready = api_is_ready; |
2615 | coroapi.nready = coro_nready; |
2995 | coroapi.nready = coro_nready; |
2616 | coroapi.current = coro_current; |
2996 | coroapi.current = coro_current; |
2617 | |
2997 | |
2618 | GCoroAPI = &coroapi; |
2998 | /*GCoroAPI = &coroapi;*/ |
2619 | sv_setiv (sv, (IV)&coroapi); |
2999 | sv_setiv (sv, (IV)&coroapi); |
2620 | SvREADONLY_on (sv); |
3000 | SvREADONLY_on (sv); |
2621 | } |
3001 | } |
2622 | } |
3002 | } |
2623 | |
3003 | |
… | |
… | |
2687 | CODE: |
3067 | CODE: |
2688 | RETVAL = coro_nready; |
3068 | RETVAL = coro_nready; |
2689 | OUTPUT: |
3069 | OUTPUT: |
2690 | RETVAL |
3070 | RETVAL |
2691 | |
3071 | |
2692 | # for async_pool speedup |
|
|
2693 | void |
3072 | void |
2694 | _pool_1 (SV *cb) |
3073 | _pool_handler (...) |
2695 | CODE: |
3074 | CODE: |
2696 | { |
3075 | CORO_EXECUTE_SLF_XS (slf_init_pool_handler); |
2697 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2698 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
2699 | AV *defav = GvAV (PL_defgv); |
|
|
2700 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
|
|
2701 | AV *invoke_av; |
|
|
2702 | int i, len; |
|
|
2703 | |
3076 | |
2704 | if (!invoke) |
3077 | void |
|
|
3078 | async_pool (SV *cv, ...) |
|
|
3079 | PROTOTYPE: &@ |
|
|
3080 | PPCODE: |
|
|
3081 | { |
|
|
3082 | HV *hv = (HV *)av_pop (av_async_pool); |
|
|
3083 | AV *av = newAV (); |
|
|
3084 | SV *cb = ST (0); |
|
|
3085 | int i; |
|
|
3086 | |
|
|
3087 | av_extend (av, items - 2); |
|
|
3088 | for (i = 1; i < items; ++i) |
|
|
3089 | av_push (av, SvREFCNT_inc_NN (ST (i))); |
|
|
3090 | |
|
|
3091 | if ((SV *)hv == &PL_sv_undef) |
2705 | { |
3092 | { |
2706 | SV *old = PL_diehook; |
3093 | PUSHMARK (SP); |
2707 | PL_diehook = 0; |
3094 | EXTEND (SP, 2); |
2708 | SvREFCNT_dec (old); |
3095 | PUSHs (sv_Coro); |
2709 | croak ("\3async_pool terminate\2\n"); |
3096 | PUSHs ((SV *)cv_pool_handler); |
|
|
3097 | PUTBACK; |
|
|
3098 | call_sv ((SV *)cv_coro_state_new, G_SCALAR); |
|
|
3099 | SPAGAIN; |
|
|
3100 | |
|
|
3101 | hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); |
2710 | } |
3102 | } |
2711 | |
3103 | |
2712 | SvREFCNT_dec (coro->saved_deffh); |
|
|
2713 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
|
|
2714 | |
|
|
2715 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
2716 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
2717 | |
|
|
2718 | invoke_av = (AV *)SvRV (invoke); |
|
|
2719 | len = av_len (invoke_av); |
|
|
2720 | |
|
|
2721 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
|
|
2722 | |
|
|
2723 | if (len > 0) |
|
|
2724 | { |
3104 | { |
2725 | av_fill (defav, len - 1); |
3105 | struct coro *coro = SvSTATE_hv (hv); |
2726 | for (i = 0; i < len; ++i) |
3106 | |
2727 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
3107 | assert (!coro->invoke_cb); |
|
|
3108 | assert (!coro->invoke_av); |
|
|
3109 | coro->invoke_cb = SvREFCNT_inc (cb); |
|
|
3110 | coro->invoke_av = av; |
2728 | } |
3111 | } |
|
|
3112 | |
|
|
3113 | api_ready (aTHX_ (SV *)hv); |
|
|
3114 | |
|
|
3115 | if (GIMME_V != G_VOID) |
|
|
3116 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); |
|
|
3117 | else |
|
|
3118 | SvREFCNT_dec (hv); |
2729 | } |
3119 | } |
|
|
3120 | |
|
|
3121 | SV * |
|
|
3122 | rouse_cb () |
|
|
3123 | PROTOTYPE: |
|
|
3124 | CODE: |
|
|
3125 | RETVAL = coro_new_rouse_cb (aTHX); |
|
|
3126 | OUTPUT: |
|
|
3127 | RETVAL |
2730 | |
3128 | |
2731 | void |
3129 | void |
2732 | _pool_2 (SV *cb) |
3130 | rouse_wait (...) |
|
|
3131 | PROTOTYPE: ;$ |
2733 | CODE: |
3132 | PPCODE: |
2734 | { |
3133 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
2735 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2736 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
2737 | |
|
|
2738 | sv_setsv (cb, &PL_sv_undef); |
|
|
2739 | |
|
|
2740 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
|
|
2741 | coro->saved_deffh = 0; |
|
|
2742 | |
|
|
2743 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
|
|
2744 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
2745 | { |
|
|
2746 | SV *old = PL_diehook; |
|
|
2747 | PL_diehook = 0; |
|
|
2748 | SvREFCNT_dec (old); |
|
|
2749 | croak ("\3async_pool terminate\2\n"); |
|
|
2750 | } |
|
|
2751 | |
|
|
2752 | av_clear (GvAV (PL_defgv)); |
|
|
2753 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
2754 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
|
|
2755 | |
|
|
2756 | coro->prio = 0; |
|
|
2757 | |
|
|
2758 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
|
|
2759 | api_trace (aTHX_ coro_current, 0); |
|
|
2760 | |
|
|
2761 | av_push (av_async_pool, newSVsv (coro_current)); |
|
|
2762 | } |
|
|
2763 | |
3134 | |
2764 | |
3135 | |
2765 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3136 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2766 | |
3137 | |
2767 | BOOT: |
3138 | BOOT: |
… | |
… | |
2769 | |
3140 | |
2770 | |
3141 | |
2771 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
3142 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
2772 | |
3143 | |
2773 | SV * |
3144 | SV * |
2774 | new (SV *klass, SV *count_ = 0) |
3145 | new (SV *klass, SV *count = 0) |
2775 | CODE: |
3146 | CODE: |
2776 | { |
3147 | RETVAL = sv_bless ( |
2777 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
3148 | coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1), |
2778 | AV *av = newAV (); |
3149 | GvSTASH (CvGV (cv)) |
2779 | av_push (av, newSViv (count_ && SvOK (count_) ? SvIV (count_) : 1)); |
3150 | ); |
2780 | RETVAL = sv_bless (newRV_noinc ((SV *)av), GvSTASH (CvGV (cv))); |
3151 | OUTPUT: |
2781 | } |
3152 | RETVAL |
|
|
3153 | |
|
|
3154 | # helper for Coro::Channel |
|
|
3155 | SV * |
|
|
3156 | _alloc (int count) |
|
|
3157 | CODE: |
|
|
3158 | RETVAL = coro_waitarray_new (aTHX_ count); |
2782 | OUTPUT: |
3159 | OUTPUT: |
2783 | RETVAL |
3160 | RETVAL |
2784 | |
3161 | |
2785 | SV * |
3162 | SV * |
2786 | count (SV *self) |
3163 | count (SV *self) |
… | |
… | |
2795 | adjust = 1 |
3172 | adjust = 1 |
2796 | CODE: |
3173 | CODE: |
2797 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
3174 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
2798 | |
3175 | |
2799 | void |
3176 | void |
2800 | down (SV *self) |
3177 | down (...) |
2801 | CODE: |
3178 | CODE: |
2802 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
3179 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
|
|
3180 | |
|
|
3181 | void |
|
|
3182 | wait (...) |
|
|
3183 | CODE: |
|
|
3184 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); |
2803 | |
3185 | |
2804 | void |
3186 | void |
2805 | try (SV *self) |
3187 | try (SV *self) |
2806 | PPCODE: |
3188 | PPCODE: |
2807 | { |
3189 | { |
… | |
… | |
2819 | XSRETURN_NO; |
3201 | XSRETURN_NO; |
2820 | } |
3202 | } |
2821 | |
3203 | |
2822 | void |
3204 | void |
2823 | waiters (SV *self) |
3205 | waiters (SV *self) |
2824 | CODE: |
3206 | PPCODE: |
2825 | { |
3207 | { |
2826 | AV *av = (AV *)SvRV (self); |
3208 | AV *av = (AV *)SvRV (self); |
|
|
3209 | int wcount = AvFILLp (av) + 1 - 1; |
2827 | |
3210 | |
2828 | if (GIMME_V == G_SCALAR) |
3211 | if (GIMME_V == G_SCALAR) |
2829 | XPUSHs (sv_2mortal (newSVsv (AvARRAY (av)[0]))); |
3212 | XPUSHs (sv_2mortal (newSViv (wcount))); |
2830 | else |
3213 | else |
2831 | { |
3214 | { |
2832 | int i; |
3215 | int i; |
2833 | EXTEND (SP, AvFILLp (av) + 1 - 1); |
3216 | EXTEND (SP, wcount); |
2834 | for (i = 1; i <= AvFILLp (av); ++i) |
3217 | for (i = 1; i <= wcount; ++i) |
2835 | PUSHs (newSVsv (AvARRAY (av)[i])); |
3218 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
2836 | } |
3219 | } |
2837 | } |
3220 | } |
|
|
3221 | |
|
|
3222 | MODULE = Coro::State PACKAGE = Coro::Signal |
|
|
3223 | |
|
|
3224 | SV * |
|
|
3225 | new (SV *klass) |
|
|
3226 | CODE: |
|
|
3227 | RETVAL = sv_bless ( |
|
|
3228 | coro_waitarray_new (aTHX_ 0), |
|
|
3229 | GvSTASH (CvGV (cv)) |
|
|
3230 | ); |
|
|
3231 | OUTPUT: |
|
|
3232 | RETVAL |
|
|
3233 | |
|
|
3234 | void |
|
|
3235 | wait (...) |
|
|
3236 | CODE: |
|
|
3237 | CORO_EXECUTE_SLF_XS (slf_init_signal_wait); |
|
|
3238 | |
|
|
3239 | void |
|
|
3240 | broadcast (SV *self) |
|
|
3241 | CODE: |
|
|
3242 | { |
|
|
3243 | AV *av = (AV *)SvRV (self); |
|
|
3244 | coro_signal_wake (aTHX_ av, AvFILLp (av)); |
|
|
3245 | } |
|
|
3246 | |
|
|
3247 | void |
|
|
3248 | send (SV *self) |
|
|
3249 | CODE: |
|
|
3250 | { |
|
|
3251 | AV *av = (AV *)SvRV (self); |
|
|
3252 | |
|
|
3253 | if (AvFILLp (av)) |
|
|
3254 | coro_signal_wake (aTHX_ av, 1); |
|
|
3255 | else |
|
|
3256 | SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */ |
|
|
3257 | } |
|
|
3258 | |
|
|
3259 | IV |
|
|
3260 | awaited (SV *self) |
|
|
3261 | CODE: |
|
|
3262 | RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1; |
|
|
3263 | OUTPUT: |
|
|
3264 | RETVAL |
2838 | |
3265 | |
2839 | |
3266 | |
2840 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
3267 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2841 | |
3268 | |
2842 | BOOT: |
3269 | BOOT: |
… | |
… | |
2857 | sv_setsv (sv_activity, &PL_sv_undef); |
3284 | sv_setsv (sv_activity, &PL_sv_undef); |
2858 | if (coro_nready >= incede) |
3285 | if (coro_nready >= incede) |
2859 | { |
3286 | { |
2860 | PUSHMARK (SP); |
3287 | PUSHMARK (SP); |
2861 | PUTBACK; |
3288 | PUTBACK; |
2862 | call_pv ("Coro::AnyEvent::_activity", G_DISCARD | G_EVAL); |
3289 | call_pv ("Coro::AnyEvent::_activity", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
2863 | SPAGAIN; |
|
|
2864 | } |
3290 | } |
2865 | |
3291 | |
2866 | --incede; |
3292 | --incede; |
2867 | } |
3293 | } |
2868 | |
3294 | |
… | |
… | |
2871 | |
3297 | |
2872 | void |
3298 | void |
2873 | _register (char *target, char *proto, SV *req) |
3299 | _register (char *target, char *proto, SV *req) |
2874 | CODE: |
3300 | CODE: |
2875 | { |
3301 | { |
2876 | HV *st; |
3302 | CV *req_cv = coro_sv_2cv (aTHX_ req); |
2877 | GV *gvp; |
3303 | /* newXSproto doesn't return the CV on 5.8 */ |
2878 | CV *req_cv = sv_2cv (req, &st, &gvp, 0); |
|
|
2879 | CV *slf_cv = newXSproto (target, coro_aio_req_xs, __FILE__, proto); |
3304 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
|
|
3305 | sv_setpv ((SV *)slf_cv, proto); |
2880 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3306 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
2881 | } |
3307 | } |
2882 | |
3308 | |