… | |
… | |
55 | |
55 | |
56 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
56 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
57 | (PERL_REVISION > (a) \ |
57 | (PERL_REVISION > (a) \ |
58 | || (PERL_REVISION == (a) \ |
58 | || (PERL_REVISION == (a) \ |
59 | && (PERL_VERSION > (b) \ |
59 | && (PERL_VERSION > (b) \ |
60 | || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) |
60 | || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c))))) |
61 | |
61 | |
62 | #if !PERL_VERSION_ATLEAST (5,6,0) |
62 | #if !PERL_VERSION_ATLEAST (5,6,0) |
63 | # ifndef PL_ppaddr |
63 | # ifndef PL_ppaddr |
64 | # define PL_ppaddr ppaddr |
64 | # define PL_ppaddr ppaddr |
65 | # endif |
65 | # endif |
… | |
… | |
98 | # define newSV(l) NEWSV(0,l) |
98 | # define newSV(l) NEWSV(0,l) |
99 | #endif |
99 | #endif |
100 | #ifndef CvISXSUB_on |
100 | #ifndef CvISXSUB_on |
101 | # define CvISXSUB_on(cv) (void)cv |
101 | # define CvISXSUB_on(cv) (void)cv |
102 | #endif |
102 | #endif |
|
|
103 | #ifndef CvISXSUB |
|
|
104 | # define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE) |
|
|
105 | #endif |
|
|
106 | #ifndef Newx |
|
|
107 | # define Newx(ptr,nitems,type) New (0,ptr,nitems,type) |
|
|
108 | #endif |
103 | |
109 | |
104 | /* 5.8.7 */ |
110 | /* 5.8.7 */ |
105 | #ifndef SvRV_set |
111 | #ifndef SvRV_set |
106 | # define SvRV_set(s,v) SvRV(s) = (v) |
112 | # define SvRV_set(s,v) SvRV(s) = (v) |
107 | #endif |
113 | #endif |
… | |
… | |
167 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
173 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
168 | static JMPENV *main_top_env; |
174 | static JMPENV *main_top_env; |
169 | static HV *coro_state_stash, *coro_stash; |
175 | static HV *coro_state_stash, *coro_stash; |
170 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
176 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
171 | |
177 | |
|
|
178 | static AV *av_destroy; /* destruction queue */ |
|
|
179 | static SV *sv_manager; /* the manager coro */ |
|
|
180 | static SV *sv_idle; /* $Coro::idle */ |
|
|
181 | |
172 | static GV *irsgv; /* $/ */ |
182 | static GV *irsgv; /* $/ */ |
173 | static GV *stdoutgv; /* *STDOUT */ |
183 | static GV *stdoutgv; /* *STDOUT */ |
174 | static SV *rv_diehook; |
184 | static SV *rv_diehook; |
175 | static SV *rv_warnhook; |
185 | static SV *rv_warnhook; |
176 | static HV *hv_sig; /* %SIG */ |
186 | static HV *hv_sig; /* %SIG */ |
177 | |
187 | |
178 | /* async_pool helper stuff */ |
188 | /* async_pool helper stuff */ |
179 | static SV *sv_pool_rss; |
189 | static SV *sv_pool_rss; |
180 | static SV *sv_pool_size; |
190 | static SV *sv_pool_size; |
|
|
191 | static SV *sv_async_pool_idle; /* description string */ |
181 | static AV *av_async_pool; |
192 | static AV *av_async_pool; /* idle pool */ |
|
|
193 | static SV *sv_Coro; /* class string */ |
|
|
194 | static CV *cv_pool_handler; |
|
|
195 | static CV *cv_coro_state_new; |
182 | |
196 | |
183 | /* Coro::AnyEvent */ |
197 | /* Coro::AnyEvent */ |
184 | static SV *sv_activity; |
198 | static SV *sv_activity; |
185 | |
199 | |
186 | static struct coro_cctx *cctx_first; |
200 | static struct coro_cctx *cctx_first; |
… | |
… | |
215 | int valgrind_id; |
229 | int valgrind_id; |
216 | #endif |
230 | #endif |
217 | unsigned char flags; |
231 | unsigned char flags; |
218 | } coro_cctx; |
232 | } coro_cctx; |
219 | |
233 | |
|
|
234 | coro_cctx *cctx_current; /* the currently running cctx */ |
|
|
235 | |
|
|
236 | /*****************************************************************************/ |
|
|
237 | |
220 | enum { |
238 | enum { |
221 | CF_RUNNING = 0x0001, /* coroutine is running */ |
239 | CF_RUNNING = 0x0001, /* coroutine is running */ |
222 | CF_READY = 0x0002, /* coroutine is ready */ |
240 | CF_READY = 0x0002, /* coroutine is ready */ |
223 | CF_NEW = 0x0004, /* has never been switched to */ |
241 | CF_NEW = 0x0004, /* has never been switched to */ |
224 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
242 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
… | |
… | |
229 | { |
247 | { |
230 | SV *defsv; |
248 | SV *defsv; |
231 | AV *defav; |
249 | AV *defav; |
232 | SV *errsv; |
250 | SV *errsv; |
233 | SV *irsgv; |
251 | SV *irsgv; |
|
|
252 | HV *hinthv; |
234 | #define VAR(name,type) type name; |
253 | #define VAR(name,type) type name; |
235 | # include "state.h" |
254 | # include "state.h" |
236 | #undef VAR |
255 | #undef VAR |
237 | } perl_slots; |
256 | } perl_slots; |
238 | |
257 | |
… | |
… | |
246 | /* state data */ |
265 | /* state data */ |
247 | struct CoroSLF slf_frame; /* saved slf frame */ |
266 | struct CoroSLF slf_frame; /* saved slf frame */ |
248 | AV *mainstack; |
267 | AV *mainstack; |
249 | perl_slots *slot; /* basically the saved sp */ |
268 | perl_slots *slot; /* basically the saved sp */ |
250 | |
269 | |
|
|
270 | CV *startcv; /* the CV to execute */ |
251 | AV *args; /* data associated with this coroutine (initial args) */ |
271 | AV *args; /* data associated with this coroutine (initial args) */ |
252 | int refcnt; /* coroutines are refcounted, yes */ |
272 | int refcnt; /* coroutines are refcounted, yes */ |
253 | int flags; /* CF_ flags */ |
273 | int flags; /* CF_ flags */ |
254 | HV *hv; /* the perl hash associated with this coro, if any */ |
274 | HV *hv; /* the perl hash associated with this coro, if any */ |
255 | void (*on_destroy)(pTHX_ struct coro *coro); |
275 | void (*on_destroy)(pTHX_ struct coro *coro); |
256 | |
276 | |
257 | /* statistics */ |
277 | /* statistics */ |
258 | int usecount; /* number of transfers to this coro */ |
278 | int usecount; /* number of transfers to this coro */ |
259 | |
279 | |
… | |
… | |
262 | SV *except; /* exception to be thrown */ |
282 | SV *except; /* exception to be thrown */ |
263 | SV *rouse_cb; |
283 | SV *rouse_cb; |
264 | |
284 | |
265 | /* async_pool */ |
285 | /* async_pool */ |
266 | SV *saved_deffh; |
286 | SV *saved_deffh; |
|
|
287 | SV *invoke_cb; |
|
|
288 | AV *invoke_av; |
267 | |
289 | |
268 | /* linked list */ |
290 | /* linked list */ |
269 | struct coro *next, *prev; |
291 | struct coro *next, *prev; |
270 | }; |
292 | }; |
271 | |
293 | |
… | |
… | |
288 | |
310 | |
289 | /* for Coro.pm */ |
311 | /* for Coro.pm */ |
290 | static SV *coro_current; |
312 | static SV *coro_current; |
291 | static SV *coro_readyhook; |
313 | static SV *coro_readyhook; |
292 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
314 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
|
|
315 | static CV *cv_coro_run, *cv_coro_terminate; |
293 | static struct coro *coro_first; |
316 | static struct coro *coro_first; |
294 | #define coro_nready coroapi.nready |
317 | #define coro_nready coroapi.nready |
295 | |
318 | |
296 | /** lowlevel stuff **********************************************************/ |
319 | /** lowlevel stuff **********************************************************/ |
297 | |
320 | |
… | |
… | |
323 | get_hv (name, create); |
346 | get_hv (name, create); |
324 | #endif |
347 | #endif |
325 | return get_hv (name, create); |
348 | return get_hv (name, create); |
326 | } |
349 | } |
327 | |
350 | |
|
|
351 | /* may croak */ |
|
|
352 | INLINE CV * |
|
|
353 | coro_sv_2cv (pTHX_ SV *sv) |
|
|
354 | { |
|
|
355 | HV *st; |
|
|
356 | GV *gvp; |
|
|
357 | return sv_2cv (sv, &st, &gvp, 0); |
|
|
358 | } |
|
|
359 | |
328 | static AV * |
360 | static AV * |
329 | coro_clone_padlist (pTHX_ CV *cv) |
361 | coro_derive_padlist (pTHX_ CV *cv) |
330 | { |
362 | { |
331 | AV *padlist = CvPADLIST (cv); |
363 | AV *padlist = CvPADLIST (cv); |
332 | AV *newpadlist, *newpad; |
364 | AV *newpadlist, *newpad; |
333 | |
365 | |
334 | newpadlist = newAV (); |
366 | newpadlist = newAV (); |
… | |
… | |
354 | if (SvREFCNT (padlist)) |
386 | if (SvREFCNT (padlist)) |
355 | { |
387 | { |
356 | I32 i = AvFILLp (padlist); |
388 | I32 i = AvFILLp (padlist); |
357 | while (i >= 0) |
389 | while (i >= 0) |
358 | { |
390 | { |
359 | SV **svp = av_fetch (padlist, i--, FALSE); |
391 | SV **svp = AvARRAY (padlist)[i--]; |
360 | if (svp) |
392 | if (svp) |
361 | { |
393 | { |
362 | SV *sv; |
394 | AvREAL_on (*svp); |
363 | while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) |
|
|
364 | SvREFCNT_dec (sv); |
395 | av_undef (*svp); |
365 | |
|
|
366 | SvREFCNT_dec (*svp); |
396 | SvREFCNT_dec (*svp); |
367 | } |
397 | } |
368 | } |
398 | } |
369 | |
399 | |
370 | SvREFCNT_dec ((SV*)padlist); |
400 | SvREFCNT_dec ((SV*)padlist); |
… | |
… | |
449 | else |
479 | else |
450 | { |
480 | { |
451 | #if CORO_PREFER_PERL_FUNCTIONS |
481 | #if CORO_PREFER_PERL_FUNCTIONS |
452 | /* this is probably cleaner? but also slower! */ |
482 | /* this is probably cleaner? but also slower! */ |
453 | /* in practise, it seems to be less stable */ |
483 | /* in practise, it seems to be less stable */ |
454 | CV *cp = Perl_cv_clone (cv); |
484 | CV *cp = Perl_cv_clone (aTHX_ cv); |
455 | CvPADLIST (cv) = CvPADLIST (cp); |
485 | CvPADLIST (cv) = CvPADLIST (cp); |
456 | CvPADLIST (cp) = 0; |
486 | CvPADLIST (cp) = 0; |
457 | SvREFCNT_dec (cp); |
487 | SvREFCNT_dec (cp); |
458 | #else |
488 | #else |
459 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
489 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
460 | #endif |
490 | #endif |
461 | } |
491 | } |
462 | } |
492 | } |
463 | |
493 | |
464 | static void |
494 | static void |
… | |
… | |
471 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
501 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
472 | |
502 | |
473 | av = (AV *)mg->mg_obj; |
503 | av = (AV *)mg->mg_obj; |
474 | |
504 | |
475 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
505 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
476 | av_extend (av, AvMAX (av) + 1); |
506 | av_extend (av, AvFILLp (av) + 1); |
477 | |
507 | |
478 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
508 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
479 | } |
509 | } |
480 | |
510 | |
481 | /** load & save, init *******************************************************/ |
511 | /** load & save, init *******************************************************/ |
… | |
… | |
486 | perl_slots *slot = c->slot; |
516 | perl_slots *slot = c->slot; |
487 | c->slot = 0; |
517 | c->slot = 0; |
488 | |
518 | |
489 | PL_mainstack = c->mainstack; |
519 | PL_mainstack = c->mainstack; |
490 | |
520 | |
491 | GvSV (PL_defgv) = slot->defsv; |
521 | GvSV (PL_defgv) = slot->defsv; |
492 | GvAV (PL_defgv) = slot->defav; |
522 | GvAV (PL_defgv) = slot->defav; |
493 | GvSV (PL_errgv) = slot->errsv; |
523 | GvSV (PL_errgv) = slot->errsv; |
494 | GvSV (irsgv) = slot->irsgv; |
524 | GvSV (irsgv) = slot->irsgv; |
|
|
525 | GvHV (PL_hintgv) = slot->hinthv; |
495 | |
526 | |
496 | #define VAR(name,type) PL_ ## name = slot->name; |
527 | #define VAR(name,type) PL_ ## name = slot->name; |
497 | # include "state.h" |
528 | # include "state.h" |
498 | #undef VAR |
529 | #undef VAR |
499 | |
530 | |
… | |
… | |
585 | c->mainstack = PL_mainstack; |
616 | c->mainstack = PL_mainstack; |
586 | |
617 | |
587 | { |
618 | { |
588 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
619 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
589 | |
620 | |
590 | slot->defav = GvAV (PL_defgv); |
621 | slot->defav = GvAV (PL_defgv); |
591 | slot->defsv = DEFSV; |
622 | slot->defsv = DEFSV; |
592 | slot->errsv = ERRSV; |
623 | slot->errsv = ERRSV; |
593 | slot->irsgv = GvSV (irsgv); |
624 | slot->irsgv = GvSV (irsgv); |
|
|
625 | slot->hinthv = GvHV (PL_hintgv); |
594 | |
626 | |
595 | #define VAR(name,type) slot->name = PL_ ## name; |
627 | #define VAR(name,type) slot->name = PL_ ## name; |
596 | # include "state.h" |
628 | # include "state.h" |
597 | #undef VAR |
629 | #undef VAR |
598 | } |
630 | } |
… | |
… | |
603 | * of perl.c:init_stacks, except that it uses less memory |
635 | * of perl.c:init_stacks, except that it uses less memory |
604 | * on the (sometimes correct) assumption that coroutines do |
636 | * on the (sometimes correct) assumption that coroutines do |
605 | * not usually need a lot of stackspace. |
637 | * not usually need a lot of stackspace. |
606 | */ |
638 | */ |
607 | #if CORO_PREFER_PERL_FUNCTIONS |
639 | #if CORO_PREFER_PERL_FUNCTIONS |
608 | # define coro_init_stacks init_stacks |
640 | # define coro_init_stacks(thx) init_stacks () |
609 | #else |
641 | #else |
610 | static void |
642 | static void |
611 | coro_init_stacks (pTHX) |
643 | coro_init_stacks (pTHX) |
612 | { |
644 | { |
613 | PL_curstackinfo = new_stackinfo(32, 8); |
645 | PL_curstackinfo = new_stackinfo(32, 8); |
… | |
… | |
676 | #if !PERL_VERSION_ATLEAST (5,10,0) |
708 | #if !PERL_VERSION_ATLEAST (5,10,0) |
677 | Safefree (PL_retstack); |
709 | Safefree (PL_retstack); |
678 | #endif |
710 | #endif |
679 | } |
711 | } |
680 | |
712 | |
|
|
713 | #define CORO_RSS \ |
|
|
714 | rss += sizeof (SYM (curstackinfo)); \ |
|
|
715 | rss += (SYM (curstackinfo->si_cxmax) + 1) * sizeof (PERL_CONTEXT); \ |
|
|
716 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (SYM (curstack))) * sizeof (SV *); \ |
|
|
717 | rss += SYM (tmps_max) * sizeof (SV *); \ |
|
|
718 | rss += (SYM (markstack_max) - SYM (markstack_ptr)) * sizeof (I32); \ |
|
|
719 | rss += SYM (scopestack_max) * sizeof (I32); \ |
|
|
720 | rss += SYM (savestack_max) * sizeof (ANY); |
|
|
721 | |
681 | static size_t |
722 | static size_t |
682 | coro_rss (pTHX_ struct coro *coro) |
723 | coro_rss (pTHX_ struct coro *coro) |
683 | { |
724 | { |
684 | size_t rss = sizeof (*coro); |
725 | size_t rss = sizeof (*coro); |
685 | |
726 | |
686 | if (coro->mainstack) |
727 | if (coro->mainstack) |
687 | { |
728 | { |
688 | perl_slots tmp_slot; |
|
|
689 | perl_slots *slot; |
|
|
690 | |
|
|
691 | if (coro->flags & CF_RUNNING) |
729 | if (coro->flags & CF_RUNNING) |
692 | { |
730 | { |
693 | slot = &tmp_slot; |
731 | #define SYM(sym) PL_ ## sym |
694 | |
732 | CORO_RSS; |
695 | #define VAR(name,type) slot->name = PL_ ## name; |
|
|
696 | # include "state.h" |
|
|
697 | #undef VAR |
733 | #undef SYM |
698 | } |
734 | } |
699 | else |
735 | else |
700 | slot = coro->slot; |
|
|
701 | |
|
|
702 | if (slot) |
|
|
703 | { |
736 | { |
704 | rss += sizeof (slot->curstackinfo); |
737 | #define SYM(sym) coro->slot->sym |
705 | rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
738 | CORO_RSS; |
706 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); |
739 | #undef SYM |
707 | rss += slot->tmps_max * sizeof (SV *); |
|
|
708 | rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); |
|
|
709 | rss += slot->scopestack_max * sizeof (I32); |
|
|
710 | rss += slot->savestack_max * sizeof (ANY); |
|
|
711 | |
|
|
712 | #if !PERL_VERSION_ATLEAST (5,10,0) |
|
|
713 | rss += slot->retstack_max * sizeof (OP *); |
|
|
714 | #endif |
|
|
715 | } |
740 | } |
716 | } |
741 | } |
717 | |
742 | |
718 | return rss; |
743 | return rss; |
719 | } |
744 | } |
… | |
… | |
821 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
846 | slf_check_nop (pTHX_ struct CoroSLF *frame) |
822 | { |
847 | { |
823 | return 0; |
848 | return 0; |
824 | } |
849 | } |
825 | |
850 | |
|
|
851 | static int |
|
|
852 | slf_check_repeat (pTHX_ struct CoroSLF *frame) |
|
|
853 | { |
|
|
854 | return 1; |
|
|
855 | } |
|
|
856 | |
826 | static UNOP coro_setup_op; |
857 | static UNOP coro_setup_op; |
827 | |
858 | |
828 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
859 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
829 | coro_setup (pTHX_ struct coro *coro) |
860 | coro_setup (pTHX_ struct coro *coro) |
830 | { |
861 | { |
… | |
… | |
835 | |
866 | |
836 | PL_runops = RUNOPS_DEFAULT; |
867 | PL_runops = RUNOPS_DEFAULT; |
837 | PL_curcop = &PL_compiling; |
868 | PL_curcop = &PL_compiling; |
838 | PL_in_eval = EVAL_NULL; |
869 | PL_in_eval = EVAL_NULL; |
839 | PL_comppad = 0; |
870 | PL_comppad = 0; |
|
|
871 | PL_comppad_name = 0; |
|
|
872 | PL_comppad_name_fill = 0; |
|
|
873 | PL_comppad_name_floor = 0; |
840 | PL_curpm = 0; |
874 | PL_curpm = 0; |
841 | PL_curpad = 0; |
875 | PL_curpad = 0; |
842 | PL_localizing = 0; |
876 | PL_localizing = 0; |
843 | PL_dirty = 0; |
877 | PL_dirty = 0; |
844 | PL_restartop = 0; |
878 | PL_restartop = 0; |
845 | #if PERL_VERSION_ATLEAST (5,10,0) |
879 | #if PERL_VERSION_ATLEAST (5,10,0) |
846 | PL_parser = 0; |
880 | PL_parser = 0; |
847 | #endif |
881 | #endif |
|
|
882 | PL_hints = 0; |
848 | |
883 | |
849 | /* recreate the die/warn hooks */ |
884 | /* recreate the die/warn hooks */ |
850 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
885 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
851 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
886 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
852 | |
887 | |
853 | GvSV (PL_defgv) = newSV (0); |
888 | GvSV (PL_defgv) = newSV (0); |
854 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
889 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
855 | GvSV (PL_errgv) = newSV (0); |
890 | GvSV (PL_errgv) = newSV (0); |
856 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
891 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
|
|
892 | GvHV (PL_hintgv) = 0; |
857 | PL_rs = newSVsv (GvSV (irsgv)); |
893 | PL_rs = newSVsv (GvSV (irsgv)); |
858 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
894 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
859 | |
895 | |
860 | { |
896 | { |
861 | dSP; |
897 | dSP; |
862 | UNOP myop; |
898 | UNOP myop; |
863 | |
899 | |
864 | Zero (&myop, 1, UNOP); |
900 | Zero (&myop, 1, UNOP); |
865 | myop.op_next = Nullop; |
901 | myop.op_next = Nullop; |
|
|
902 | myop.op_type = OP_ENTERSUB; |
866 | myop.op_flags = OPf_WANT_VOID; |
903 | myop.op_flags = OPf_WANT_VOID; |
867 | |
904 | |
868 | PUSHMARK (SP); |
905 | PUSHMARK (SP); |
869 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
906 | PUSHs ((SV *)coro->startcv); |
870 | PUTBACK; |
907 | PUTBACK; |
871 | PL_op = (OP *)&myop; |
908 | PL_op = (OP *)&myop; |
872 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
909 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
873 | SPAGAIN; |
|
|
874 | } |
910 | } |
875 | |
911 | |
876 | /* this newly created coroutine might be run on an existing cctx which most |
912 | /* this newly created coroutine might be run on an existing cctx which most |
877 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
913 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
878 | */ |
914 | */ |
879 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
915 | slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ |
880 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
916 | slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ |
881 | |
917 | |
882 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
918 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
883 | coro_setup_op.op_next = PL_op; |
919 | coro_setup_op.op_next = PL_op; |
884 | coro_setup_op.op_type = OP_CUSTOM; |
920 | coro_setup_op.op_type = OP_ENTERSUB; |
885 | coro_setup_op.op_ppaddr = pp_slf; |
921 | coro_setup_op.op_ppaddr = pp_slf; |
886 | /* no flags required, as an init function won't be called */ |
922 | /* no flags etc. required, as an init function won't be called */ |
887 | |
923 | |
888 | PL_op = (OP *)&coro_setup_op; |
924 | PL_op = (OP *)&coro_setup_op; |
889 | |
925 | |
890 | /* copy throw, in case it was set before coro_setup */ |
926 | /* copy throw, in case it was set before coro_setup */ |
891 | CORO_THROW = coro->except; |
927 | CORO_THROW = coro->except; |
… | |
… | |
915 | SvREFCNT_dec (GvAV (PL_defgv)); |
951 | SvREFCNT_dec (GvAV (PL_defgv)); |
916 | SvREFCNT_dec (GvSV (PL_errgv)); |
952 | SvREFCNT_dec (GvSV (PL_errgv)); |
917 | SvREFCNT_dec (PL_defoutgv); |
953 | SvREFCNT_dec (PL_defoutgv); |
918 | SvREFCNT_dec (PL_rs); |
954 | SvREFCNT_dec (PL_rs); |
919 | SvREFCNT_dec (GvSV (irsgv)); |
955 | SvREFCNT_dec (GvSV (irsgv)); |
|
|
956 | SvREFCNT_dec (GvHV (PL_hintgv)); |
920 | |
957 | |
921 | SvREFCNT_dec (PL_diehook); |
958 | SvREFCNT_dec (PL_diehook); |
922 | SvREFCNT_dec (PL_warnhook); |
959 | SvREFCNT_dec (PL_warnhook); |
923 | |
960 | |
924 | SvREFCNT_dec (CORO_THROW); |
|
|
925 | SvREFCNT_dec (coro->saved_deffh); |
961 | SvREFCNT_dec (coro->saved_deffh); |
926 | SvREFCNT_dec (coro->rouse_cb); |
962 | SvREFCNT_dec (coro->rouse_cb); |
|
|
963 | SvREFCNT_dec (coro->invoke_cb); |
|
|
964 | SvREFCNT_dec (coro->invoke_av); |
927 | |
965 | |
928 | coro_destruct_stacks (aTHX); |
966 | coro_destruct_stacks (aTHX); |
929 | } |
967 | } |
930 | |
968 | |
931 | INLINE void |
969 | INLINE void |
… | |
… | |
941 | static int |
979 | static int |
942 | runops_trace (pTHX) |
980 | runops_trace (pTHX) |
943 | { |
981 | { |
944 | COP *oldcop = 0; |
982 | COP *oldcop = 0; |
945 | int oldcxix = -2; |
983 | int oldcxix = -2; |
946 | struct coro *coro = SvSTATE_current; /* trace cctx is tied to specific coro */ |
|
|
947 | coro_cctx *cctx = coro->cctx; |
|
|
948 | |
984 | |
949 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
985 | while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX))) |
950 | { |
986 | { |
951 | PERL_ASYNC_CHECK (); |
987 | PERL_ASYNC_CHECK (); |
952 | |
988 | |
953 | if (cctx->flags & CC_TRACE_ALL) |
989 | if (cctx_current->flags & CC_TRACE_ALL) |
954 | { |
990 | { |
955 | if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB) |
991 | if (PL_op->op_type == OP_LEAVESUB && cctx_current->flags & CC_TRACE_SUB) |
956 | { |
992 | { |
957 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
993 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
958 | SV **bot, **top; |
994 | SV **bot, **top; |
959 | AV *av = newAV (); /* return values */ |
995 | AV *av = newAV (); /* return values */ |
960 | SV **cb; |
996 | SV **cb; |
… | |
… | |
997 | |
1033 | |
998 | if (PL_curcop != &PL_compiling) |
1034 | if (PL_curcop != &PL_compiling) |
999 | { |
1035 | { |
1000 | SV **cb; |
1036 | SV **cb; |
1001 | |
1037 | |
1002 | if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB) |
1038 | if (oldcxix != cxstack_ix && cctx_current->flags & CC_TRACE_SUB) |
1003 | { |
1039 | { |
1004 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1040 | PERL_CONTEXT *cx = &cxstack[cxstack_ix]; |
1005 | |
1041 | |
1006 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1042 | if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix) |
1007 | { |
1043 | { |
1008 | runops_proc_t old_runops = PL_runops; |
|
|
1009 | dSP; |
1044 | dSP; |
1010 | GV *gv = CvGV (cx->blk_sub.cv); |
1045 | GV *gv = CvGV (cx->blk_sub.cv); |
1011 | SV *fullname = sv_2mortal (newSV (0)); |
1046 | SV *fullname = sv_2mortal (newSV (0)); |
1012 | |
1047 | |
1013 | if (isGV (gv)) |
1048 | if (isGV (gv)) |
… | |
… | |
1018 | SAVETMPS; |
1053 | SAVETMPS; |
1019 | EXTEND (SP, 3); |
1054 | EXTEND (SP, 3); |
1020 | PUSHMARK (SP); |
1055 | PUSHMARK (SP); |
1021 | PUSHs (&PL_sv_yes); |
1056 | PUSHs (&PL_sv_yes); |
1022 | PUSHs (fullname); |
1057 | PUSHs (fullname); |
1023 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
1058 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
1024 | PUTBACK; |
1059 | PUTBACK; |
1025 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
1060 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
1026 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1061 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1027 | SPAGAIN; |
1062 | SPAGAIN; |
1028 | FREETMPS; |
1063 | FREETMPS; |
… | |
… | |
1031 | } |
1066 | } |
1032 | |
1067 | |
1033 | oldcxix = cxstack_ix; |
1068 | oldcxix = cxstack_ix; |
1034 | } |
1069 | } |
1035 | |
1070 | |
1036 | if (cctx->flags & CC_TRACE_LINE) |
1071 | if (cctx_current->flags & CC_TRACE_LINE) |
1037 | { |
1072 | { |
1038 | dSP; |
1073 | dSP; |
1039 | |
1074 | |
1040 | PL_runops = RUNOPS_DEFAULT; |
1075 | PL_runops = RUNOPS_DEFAULT; |
1041 | ENTER; |
1076 | ENTER; |
… | |
… | |
1060 | |
1095 | |
1061 | TAINT_NOT; |
1096 | TAINT_NOT; |
1062 | return 0; |
1097 | return 0; |
1063 | } |
1098 | } |
1064 | |
1099 | |
1065 | static struct coro_cctx *cctx_ssl_cctx; |
|
|
1066 | static struct CoroSLF cctx_ssl_frame; |
1100 | static struct CoroSLF cctx_ssl_frame; |
1067 | |
1101 | |
1068 | static void |
1102 | static void |
1069 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
1103 | slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta) |
1070 | { |
1104 | { |
1071 | ta->prev = (struct coro *)cctx_ssl_cctx; |
|
|
1072 | ta->next = 0; |
1105 | ta->prev = 0; |
1073 | } |
1106 | } |
1074 | |
1107 | |
1075 | static int |
1108 | static int |
1076 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1109 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1077 | { |
1110 | { |
… | |
… | |
1080 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
1113 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
1081 | } |
1114 | } |
1082 | |
1115 | |
1083 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1116 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1084 | static void NOINLINE |
1117 | static void NOINLINE |
1085 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1118 | cctx_prepare (pTHX) |
1086 | { |
1119 | { |
1087 | PL_top_env = &PL_start_env; |
1120 | PL_top_env = &PL_start_env; |
1088 | |
1121 | |
1089 | if (cctx->flags & CC_TRACE) |
1122 | if (cctx_current->flags & CC_TRACE) |
1090 | PL_runops = runops_trace; |
1123 | PL_runops = runops_trace; |
1091 | |
1124 | |
1092 | /* we already must be executing an SLF op, there is no other valid way |
1125 | /* we already must be executing an SLF op, there is no other valid way |
1093 | * that can lead to creation of a new cctx */ |
1126 | * that can lead to creation of a new cctx */ |
1094 | assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", |
1127 | assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", |
1095 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1128 | slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); |
1096 | |
1129 | |
1097 | /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ |
1130 | /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */ |
1098 | cctx_ssl_cctx = cctx; |
|
|
1099 | cctx_ssl_frame = slf_frame; |
1131 | cctx_ssl_frame = slf_frame; |
1100 | |
1132 | |
1101 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1133 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1102 | slf_frame.check = slf_check_set_stacklevel; |
1134 | slf_frame.check = slf_check_set_stacklevel; |
1103 | } |
1135 | } |
… | |
… | |
1126 | /* normally we would need to skip the entersub here */ |
1158 | /* normally we would need to skip the entersub here */ |
1127 | /* not doing so will re-execute it, which is exactly what we want */ |
1159 | /* not doing so will re-execute it, which is exactly what we want */ |
1128 | /* PL_nop = PL_nop->op_next */ |
1160 | /* PL_nop = PL_nop->op_next */ |
1129 | |
1161 | |
1130 | /* inject a fake subroutine call to cctx_init */ |
1162 | /* inject a fake subroutine call to cctx_init */ |
1131 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
1163 | cctx_prepare (aTHX); |
1132 | |
1164 | |
1133 | /* cctx_run is the alternative tail of transfer() */ |
1165 | /* cctx_run is the alternative tail of transfer() */ |
1134 | transfer_tail (aTHX); |
1166 | transfer_tail (aTHX); |
1135 | |
1167 | |
1136 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1168 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1137 | PL_restartop = PL_op; |
1169 | PL_restartop = PL_op; |
1138 | perl_run (PL_curinterp); |
1170 | perl_run (PL_curinterp); |
|
|
1171 | /* |
|
|
1172 | * Unfortunately, there is no way to get at the return values of the |
|
|
1173 | * coro body here, as perl_run destroys these |
|
|
1174 | */ |
1139 | |
1175 | |
1140 | /* |
1176 | /* |
1141 | * If perl-run returns we assume exit() was being called or the coro |
1177 | * If perl-run returns we assume exit() was being called or the coro |
1142 | * fell off the end, which seems to be the only valid (non-bug) |
1178 | * fell off the end, which seems to be the only valid (non-bug) |
1143 | * reason for perl_run to return. We try to exit by jumping to the |
1179 | * reason for perl_run to return. We try to exit by jumping to the |
1144 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1180 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1145 | * coroutine as Coro has no such concept |
1181 | * coroutine as Coro has no such concept. |
|
|
1182 | * This actually isn't valid with the pthread backend, but OSes requiring |
|
|
1183 | * that backend are too broken to do it in a standards-compliant way. |
1146 | */ |
1184 | */ |
1147 | PL_top_env = main_top_env; |
1185 | PL_top_env = main_top_env; |
1148 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1186 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1149 | } |
1187 | } |
1150 | } |
1188 | } |
… | |
… | |
1227 | cctx_destroy (coro_cctx *cctx) |
1265 | cctx_destroy (coro_cctx *cctx) |
1228 | { |
1266 | { |
1229 | if (!cctx) |
1267 | if (!cctx) |
1230 | return; |
1268 | return; |
1231 | |
1269 | |
|
|
1270 | assert (cctx != cctx_current);//D temporary |
|
|
1271 | |
1232 | --cctx_count; |
1272 | --cctx_count; |
1233 | coro_destroy (&cctx->cctx); |
1273 | coro_destroy (&cctx->cctx); |
1234 | |
1274 | |
1235 | /* coro_transfer creates new, empty cctx's */ |
1275 | /* coro_transfer creates new, empty cctx's */ |
1236 | if (cctx->sptr) |
1276 | if (cctx->sptr) |
… | |
… | |
1299 | /* TODO: throwing up here is considered harmful */ |
1339 | /* TODO: throwing up here is considered harmful */ |
1300 | |
1340 | |
1301 | if (expect_true (prev != next)) |
1341 | if (expect_true (prev != next)) |
1302 | { |
1342 | { |
1303 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1343 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1304 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); |
1344 | croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,"); |
1305 | |
1345 | |
1306 | if (expect_false (next->flags & CF_RUNNING)) |
1346 | if (expect_false (next->flags & CF_RUNNING)) |
1307 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
1347 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
1308 | |
1348 | |
1309 | if (expect_false (next->flags & CF_DESTROYED)) |
1349 | if (expect_false (next->flags & CF_DESTROYED)) |
… | |
… | |
1321 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1361 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1322 | { |
1362 | { |
1323 | dSTACKLEVEL; |
1363 | dSTACKLEVEL; |
1324 | |
1364 | |
1325 | /* sometimes transfer is only called to set idle_sp */ |
1365 | /* sometimes transfer is only called to set idle_sp */ |
1326 | if (expect_false (!next)) |
1366 | if (expect_false (!prev)) |
1327 | { |
1367 | { |
1328 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1368 | cctx_current->idle_sp = STACKLEVEL; |
1329 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1369 | assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1330 | } |
1370 | } |
1331 | else if (expect_true (prev != next)) |
1371 | else if (expect_true (prev != next)) |
1332 | { |
1372 | { |
1333 | coro_cctx *prev__cctx; |
1373 | coro_cctx *cctx_prev; |
1334 | |
1374 | |
1335 | if (expect_false (prev->flags & CF_NEW)) |
1375 | if (expect_false (prev->flags & CF_NEW)) |
1336 | { |
1376 | { |
1337 | /* create a new empty/source context */ |
1377 | /* create a new empty/source context */ |
1338 | prev->cctx = cctx_new_empty (); |
|
|
1339 | prev->flags &= ~CF_NEW; |
1378 | prev->flags &= ~CF_NEW; |
1340 | prev->flags |= CF_RUNNING; |
1379 | prev->flags |= CF_RUNNING; |
1341 | } |
1380 | } |
1342 | |
1381 | |
1343 | prev->flags &= ~CF_RUNNING; |
1382 | prev->flags &= ~CF_RUNNING; |
… | |
… | |
1354 | coro_setup (aTHX_ next); |
1393 | coro_setup (aTHX_ next); |
1355 | } |
1394 | } |
1356 | else |
1395 | else |
1357 | load_perl (aTHX_ next); |
1396 | load_perl (aTHX_ next); |
1358 | |
1397 | |
1359 | prev__cctx = prev->cctx; |
1398 | assert (!prev->cctx);//D temporary |
1360 | |
1399 | |
1361 | /* possibly untie and reuse the cctx */ |
1400 | /* possibly untie and reuse the cctx */ |
1362 | if (expect_true ( |
1401 | if (expect_true ( |
1363 | prev__cctx->idle_sp == STACKLEVEL |
1402 | cctx_current->idle_sp == STACKLEVEL |
1364 | && !(prev__cctx->flags & CC_TRACE) |
1403 | && !(cctx_current->flags & CC_TRACE) |
1365 | && !force_cctx |
1404 | && !force_cctx |
1366 | )) |
1405 | )) |
1367 | { |
1406 | { |
1368 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1407 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1369 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te)); |
1408 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); |
1370 | |
1409 | |
1371 | prev->cctx = 0; |
|
|
1372 | |
|
|
1373 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1410 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */ |
1374 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1411 | /* without this the next cctx_get might destroy the running cctx while still in use */ |
1375 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
1412 | if (expect_false (CCTX_EXPIRED (cctx_current))) |
1376 | if (!next->cctx) |
1413 | if (expect_true (!next->cctx)) |
1377 | next->cctx = cctx_get (aTHX); |
1414 | next->cctx = cctx_get (aTHX); |
1378 | |
1415 | |
1379 | cctx_put (prev__cctx); |
1416 | cctx_put (cctx_current); |
1380 | } |
1417 | } |
|
|
1418 | else |
|
|
1419 | prev->cctx = cctx_current; |
1381 | |
1420 | |
1382 | ++next->usecount; |
1421 | ++next->usecount; |
1383 | |
1422 | |
1384 | if (expect_true (!next->cctx)) |
1423 | cctx_prev = cctx_current; |
1385 | next->cctx = cctx_get (aTHX); |
1424 | cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX); |
1386 | |
1425 | |
1387 | if (expect_false (prev__cctx != next->cctx)) |
1426 | next->cctx = 0; |
|
|
1427 | |
|
|
1428 | if (expect_false (cctx_prev != cctx_current)) |
1388 | { |
1429 | { |
1389 | prev__cctx->top_env = PL_top_env; |
1430 | cctx_prev->top_env = PL_top_env; |
1390 | PL_top_env = next->cctx->top_env; |
1431 | PL_top_env = cctx_current->top_env; |
1391 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1432 | coro_transfer (&cctx_prev->cctx, &cctx_current->cctx); |
1392 | } |
1433 | } |
1393 | |
1434 | |
1394 | transfer_tail (aTHX); |
1435 | transfer_tail (aTHX); |
1395 | } |
1436 | } |
1396 | } |
1437 | } |
… | |
… | |
1435 | |
1476 | |
1436 | coro->slot = 0; |
1477 | coro->slot = 0; |
1437 | } |
1478 | } |
1438 | |
1479 | |
1439 | cctx_destroy (coro->cctx); |
1480 | cctx_destroy (coro->cctx); |
|
|
1481 | SvREFCNT_dec (coro->startcv); |
1440 | SvREFCNT_dec (coro->args); |
1482 | SvREFCNT_dec (coro->args); |
|
|
1483 | SvREFCNT_dec (CORO_THROW); |
1441 | |
1484 | |
1442 | if (coro->next) coro->next->prev = coro->prev; |
1485 | if (coro->next) coro->next->prev = coro->prev; |
1443 | if (coro->prev) coro->prev->next = coro->next; |
1486 | if (coro->prev) coro->prev->next = coro->next; |
1444 | if (coro == coro_first) coro_first = coro->next; |
1487 | if (coro == coro_first) coro_first = coro->next; |
1445 | |
1488 | |
… | |
… | |
1549 | { |
1592 | { |
1550 | struct coro *coro; |
1593 | struct coro *coro; |
1551 | SV *sv_hook; |
1594 | SV *sv_hook; |
1552 | void (*xs_hook)(void); |
1595 | void (*xs_hook)(void); |
1553 | |
1596 | |
1554 | if (SvROK (coro_sv)) |
|
|
1555 | coro_sv = SvRV (coro_sv); |
|
|
1556 | |
|
|
1557 | coro = SvSTATE (coro_sv); |
1597 | coro = SvSTATE (coro_sv); |
1558 | |
1598 | |
1559 | if (coro->flags & CF_READY) |
1599 | if (coro->flags & CF_READY) |
1560 | return 0; |
1600 | return 0; |
1561 | |
1601 | |
… | |
… | |
1592 | api_is_ready (pTHX_ SV *coro_sv) |
1632 | api_is_ready (pTHX_ SV *coro_sv) |
1593 | { |
1633 | { |
1594 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1634 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1595 | } |
1635 | } |
1596 | |
1636 | |
|
|
1637 | /* expects to own a reference to next->hv */ |
1597 | INLINE void |
1638 | INLINE void |
1598 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1639 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1599 | { |
1640 | { |
1600 | SV *prev_sv, *next_sv; |
|
|
1601 | |
|
|
1602 | for (;;) |
|
|
1603 | { |
|
|
1604 | next_sv = coro_deq (aTHX); |
|
|
1605 | |
|
|
1606 | /* nothing to schedule: call the idle handler */ |
|
|
1607 | if (expect_false (!next_sv)) |
|
|
1608 | { |
|
|
1609 | dSP; |
|
|
1610 | |
|
|
1611 | ENTER; |
|
|
1612 | SAVETMPS; |
|
|
1613 | |
|
|
1614 | PUSHMARK (SP); |
|
|
1615 | PUTBACK; |
|
|
1616 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
|
|
1617 | |
|
|
1618 | FREETMPS; |
|
|
1619 | LEAVE; |
|
|
1620 | continue; |
|
|
1621 | } |
|
|
1622 | |
|
|
1623 | ta->next = SvSTATE_hv (next_sv); |
|
|
1624 | |
|
|
1625 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1626 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
|
|
1627 | { |
|
|
1628 | SvREFCNT_dec (next_sv); |
|
|
1629 | /* coro_nready has already been taken care of by destroy */ |
|
|
1630 | continue; |
|
|
1631 | } |
|
|
1632 | |
|
|
1633 | --coro_nready; |
|
|
1634 | break; |
|
|
1635 | } |
|
|
1636 | |
|
|
1637 | /* free this only after the transfer */ |
|
|
1638 | prev_sv = SvRV (coro_current); |
1641 | SV *prev_sv = SvRV (coro_current); |
|
|
1642 | |
1639 | ta->prev = SvSTATE_hv (prev_sv); |
1643 | ta->prev = SvSTATE_hv (prev_sv); |
|
|
1644 | ta->next = next; |
|
|
1645 | |
1640 | TRANSFER_CHECK (*ta); |
1646 | TRANSFER_CHECK (*ta); |
1641 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
1647 | |
1642 | ta->next->flags &= ~CF_READY; |
|
|
1643 | SvRV_set (coro_current, next_sv); |
1648 | SvRV_set (coro_current, (SV *)next->hv); |
1644 | |
1649 | |
1645 | free_coro_mortal (aTHX); |
1650 | free_coro_mortal (aTHX); |
1646 | coro_mortal = prev_sv; |
1651 | coro_mortal = prev_sv; |
1647 | } |
1652 | } |
1648 | |
1653 | |
|
|
1654 | static void |
|
|
1655 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
|
|
1656 | { |
|
|
1657 | for (;;) |
|
|
1658 | { |
|
|
1659 | SV *next_sv = coro_deq (aTHX); |
|
|
1660 | |
|
|
1661 | if (expect_true (next_sv)) |
|
|
1662 | { |
|
|
1663 | struct coro *next = SvSTATE_hv (next_sv); |
|
|
1664 | |
|
|
1665 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1666 | if (expect_false (next->flags & CF_DESTROYED)) |
|
|
1667 | SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ |
|
|
1668 | else |
|
|
1669 | { |
|
|
1670 | next->flags &= ~CF_READY; |
|
|
1671 | --coro_nready; |
|
|
1672 | |
|
|
1673 | prepare_schedule_to (aTHX_ ta, next); |
|
|
1674 | break; |
|
|
1675 | } |
|
|
1676 | } |
|
|
1677 | else |
|
|
1678 | { |
|
|
1679 | /* nothing to schedule: call the idle handler */ |
|
|
1680 | if (SvROK (sv_idle) |
|
|
1681 | && SvOBJECT (SvRV (sv_idle))) |
|
|
1682 | { |
|
|
1683 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
|
|
1684 | api_ready (aTHX_ SvRV (sv_idle)); |
|
|
1685 | --coro_nready; |
|
|
1686 | } |
|
|
1687 | else |
|
|
1688 | { |
|
|
1689 | dSP; |
|
|
1690 | |
|
|
1691 | ENTER; |
|
|
1692 | SAVETMPS; |
|
|
1693 | |
|
|
1694 | PUSHMARK (SP); |
|
|
1695 | PUTBACK; |
|
|
1696 | call_sv (sv_idle, G_VOID | G_DISCARD); |
|
|
1697 | |
|
|
1698 | FREETMPS; |
|
|
1699 | LEAVE; |
|
|
1700 | } |
|
|
1701 | } |
|
|
1702 | } |
|
|
1703 | } |
|
|
1704 | |
1649 | INLINE void |
1705 | INLINE void |
1650 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1706 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1651 | { |
1707 | { |
1652 | api_ready (aTHX_ coro_current); |
1708 | api_ready (aTHX_ coro_current); |
1653 | prepare_schedule (aTHX_ ta); |
1709 | prepare_schedule (aTHX_ ta); |
… | |
… | |
1672 | { |
1728 | { |
1673 | struct coro_transfer_args ta; |
1729 | struct coro_transfer_args ta; |
1674 | |
1730 | |
1675 | prepare_schedule (aTHX_ &ta); |
1731 | prepare_schedule (aTHX_ &ta); |
1676 | TRANSFER (ta, 1); |
1732 | TRANSFER (ta, 1); |
|
|
1733 | } |
|
|
1734 | |
|
|
1735 | static void |
|
|
1736 | api_schedule_to (pTHX_ SV *coro_sv) |
|
|
1737 | { |
|
|
1738 | struct coro_transfer_args ta; |
|
|
1739 | struct coro *next = SvSTATE (coro_sv); |
|
|
1740 | |
|
|
1741 | SvREFCNT_inc_NN (coro_sv); |
|
|
1742 | prepare_schedule_to (aTHX_ &ta, next); |
1677 | } |
1743 | } |
1678 | |
1744 | |
1679 | static int |
1745 | static int |
1680 | api_cede (pTHX) |
1746 | api_cede (pTHX) |
1681 | { |
1747 | { |
… | |
… | |
1710 | static void |
1776 | static void |
1711 | api_trace (pTHX_ SV *coro_sv, int flags) |
1777 | api_trace (pTHX_ SV *coro_sv, int flags) |
1712 | { |
1778 | { |
1713 | struct coro *coro = SvSTATE (coro_sv); |
1779 | struct coro *coro = SvSTATE (coro_sv); |
1714 | |
1780 | |
|
|
1781 | if (coro->flags & CF_RUNNING) |
|
|
1782 | croak ("cannot enable tracing on a running coroutine, caught"); |
|
|
1783 | |
1715 | if (flags & CC_TRACE) |
1784 | if (flags & CC_TRACE) |
1716 | { |
1785 | { |
1717 | if (!coro->cctx) |
1786 | if (!coro->cctx) |
1718 | coro->cctx = cctx_new_run (); |
1787 | coro->cctx = cctx_new_run (); |
1719 | else if (!(coro->cctx->flags & CC_TRACE)) |
1788 | else if (!(coro->cctx->flags & CC_TRACE)) |
1720 | croak ("cannot enable tracing on coroutine with custom stack,"); |
1789 | croak ("cannot enable tracing on coroutine with custom stack, caught"); |
1721 | |
1790 | |
1722 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1791 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1723 | } |
1792 | } |
1724 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1793 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1725 | { |
1794 | { |
… | |
… | |
1730 | else |
1799 | else |
1731 | coro->slot->runops = RUNOPS_DEFAULT; |
1800 | coro->slot->runops = RUNOPS_DEFAULT; |
1732 | } |
1801 | } |
1733 | } |
1802 | } |
1734 | |
1803 | |
|
|
1804 | static void |
|
|
1805 | coro_call_on_destroy (pTHX_ struct coro *coro) |
|
|
1806 | { |
|
|
1807 | SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0); |
|
|
1808 | SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0); |
|
|
1809 | |
|
|
1810 | if (on_destroyp) |
|
|
1811 | { |
|
|
1812 | AV *on_destroy = (AV *)SvRV (*on_destroyp); |
|
|
1813 | |
|
|
1814 | while (AvFILLp (on_destroy) >= 0) |
|
|
1815 | { |
|
|
1816 | dSP; /* don't disturb outer sp */ |
|
|
1817 | SV *cb = av_pop (on_destroy); |
|
|
1818 | |
|
|
1819 | PUSHMARK (SP); |
|
|
1820 | |
|
|
1821 | if (statusp) |
|
|
1822 | { |
|
|
1823 | int i; |
|
|
1824 | AV *status = (AV *)SvRV (*statusp); |
|
|
1825 | EXTEND (SP, AvFILLp (status) + 1); |
|
|
1826 | |
|
|
1827 | for (i = 0; i <= AvFILLp (status); ++i) |
|
|
1828 | PUSHs (AvARRAY (status)[i]); |
|
|
1829 | } |
|
|
1830 | |
|
|
1831 | PUTBACK; |
|
|
1832 | call_sv (sv_2mortal (cb), G_VOID | G_DISCARD); |
|
|
1833 | } |
|
|
1834 | } |
|
|
1835 | } |
|
|
1836 | |
|
|
1837 | static void |
|
|
1838 | slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1839 | { |
|
|
1840 | int i; |
|
|
1841 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1842 | AV *av = newAV (); |
|
|
1843 | |
|
|
1844 | av_extend (av, items - 1); |
|
|
1845 | for (i = 0; i < items; ++i) |
|
|
1846 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
|
|
1847 | |
|
|
1848 | hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); |
|
|
1849 | |
|
|
1850 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
|
|
1851 | api_ready (aTHX_ sv_manager); |
|
|
1852 | |
|
|
1853 | frame->prepare = prepare_schedule; |
|
|
1854 | frame->check = slf_check_repeat; |
|
|
1855 | } |
|
|
1856 | |
|
|
1857 | /*****************************************************************************/ |
|
|
1858 | /* async pool handler */ |
|
|
1859 | |
|
|
1860 | static int |
|
|
1861 | slf_check_pool_handler (pTHX_ struct CoroSLF *frame) |
|
|
1862 | { |
|
|
1863 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1864 | struct coro *coro = (struct coro *)frame->data; |
|
|
1865 | |
|
|
1866 | if (!coro->invoke_cb) |
|
|
1867 | return 1; /* loop till we have invoke */ |
|
|
1868 | else |
|
|
1869 | { |
|
|
1870 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
1871 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
1872 | |
|
|
1873 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
|
|
1874 | |
|
|
1875 | { |
|
|
1876 | dSP; |
|
|
1877 | XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0; |
|
|
1878 | PUTBACK; |
|
|
1879 | } |
|
|
1880 | |
|
|
1881 | SvREFCNT_dec (GvAV (PL_defgv)); |
|
|
1882 | GvAV (PL_defgv) = coro->invoke_av; |
|
|
1883 | coro->invoke_av = 0; |
|
|
1884 | |
|
|
1885 | return 0; |
|
|
1886 | } |
|
|
1887 | } |
|
|
1888 | |
|
|
1889 | static void |
|
|
1890 | slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1891 | { |
|
|
1892 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1893 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
1894 | |
|
|
1895 | if (expect_true (coro->saved_deffh)) |
|
|
1896 | { |
|
|
1897 | /* subsequent iteration */ |
|
|
1898 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
|
|
1899 | coro->saved_deffh = 0; |
|
|
1900 | |
|
|
1901 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
|
|
1902 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
1903 | { |
|
|
1904 | coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate); |
|
|
1905 | coro->invoke_av = newAV (); |
|
|
1906 | |
|
|
1907 | frame->prepare = prepare_nop; |
|
|
1908 | } |
|
|
1909 | else |
|
|
1910 | { |
|
|
1911 | av_clear (GvAV (PL_defgv)); |
|
|
1912 | hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); |
|
|
1913 | |
|
|
1914 | coro->prio = 0; |
|
|
1915 | |
|
|
1916 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
|
|
1917 | api_trace (aTHX_ coro_current, 0); |
|
|
1918 | |
|
|
1919 | frame->prepare = prepare_schedule; |
|
|
1920 | av_push (av_async_pool, SvREFCNT_inc (hv)); |
|
|
1921 | } |
|
|
1922 | } |
|
|
1923 | else |
|
|
1924 | { |
|
|
1925 | /* first iteration, simply fall through */ |
|
|
1926 | frame->prepare = prepare_nop; |
|
|
1927 | } |
|
|
1928 | |
|
|
1929 | frame->check = slf_check_pool_handler; |
|
|
1930 | frame->data = (void *)coro; |
|
|
1931 | } |
|
|
1932 | |
1735 | /*****************************************************************************/ |
1933 | /*****************************************************************************/ |
1736 | /* rouse callback */ |
1934 | /* rouse callback */ |
1737 | |
1935 | |
1738 | #define CORO_MAGIC_type_rouse PERL_MAGIC_ext |
1936 | #define CORO_MAGIC_type_rouse PERL_MAGIC_ext |
1739 | |
1937 | |
… | |
… | |
1744 | SV *data = (SV *)GENSUB_ARG; |
1942 | SV *data = (SV *)GENSUB_ARG; |
1745 | |
1943 | |
1746 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
1944 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
1747 | { |
1945 | { |
1748 | /* first call, set args */ |
1946 | /* first call, set args */ |
1749 | int i; |
|
|
1750 | AV *av = newAV (); |
1947 | AV *av = newAV (); |
1751 | SV *coro = SvRV (data); |
1948 | SV *coro = SvRV (data); |
1752 | |
1949 | |
1753 | SvRV_set (data, (SV *)av); |
1950 | SvRV_set (data, (SV *)av); |
1754 | api_ready (aTHX_ coro); |
1951 | api_ready (aTHX_ coro); |
1755 | SvREFCNT_dec (coro); |
1952 | SvREFCNT_dec (coro); |
1756 | |
1953 | |
|
|
1954 | /* better take a full copy of the arguments */ |
1757 | while (items--) |
1955 | while (items--) |
1758 | av_store (av, items, SvREFCNT_inc_NN (ST (items))); |
1956 | av_store (av, items, newSVsv (ST (items))); |
1759 | } |
1957 | } |
1760 | |
1958 | |
1761 | XSRETURN_EMPTY; |
1959 | XSRETURN_EMPTY; |
1762 | } |
1960 | } |
1763 | |
1961 | |
… | |
… | |
1778 | AV *av = (AV *)SvRV (data); |
1976 | AV *av = (AV *)SvRV (data); |
1779 | int i; |
1977 | int i; |
1780 | |
1978 | |
1781 | EXTEND (SP, AvFILLp (av) + 1); |
1979 | EXTEND (SP, AvFILLp (av) + 1); |
1782 | for (i = 0; i <= AvFILLp (av); ++i) |
1980 | for (i = 0; i <= AvFILLp (av); ++i) |
1783 | PUSHs (AvARRAY (av)[i]); |
1981 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
|
|
1982 | |
|
|
1983 | /* we have stolen the elements, so ste length to zero and free */ |
|
|
1984 | AvFILLp (av) = -1; |
|
|
1985 | av_undef (av); |
1784 | |
1986 | |
1785 | PUTBACK; |
1987 | PUTBACK; |
1786 | } |
1988 | } |
1787 | |
1989 | |
1788 | return 0; |
1990 | return 0; |
… | |
… | |
1828 | struct coro *coro = SvSTATE_hv (hv); |
2030 | struct coro *coro = SvSTATE_hv (hv); |
1829 | SV *data = newRV_inc ((SV *)hv); |
2031 | SV *data = newRV_inc ((SV *)hv); |
1830 | SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data); |
2032 | SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data); |
1831 | |
2033 | |
1832 | sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0); |
2034 | sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0); |
|
|
2035 | SvREFCNT_dec (data); /* magicext increases the refcount */ |
1833 | |
2036 | |
1834 | SvREFCNT_dec (coro->rouse_cb); |
2037 | SvREFCNT_dec (coro->rouse_cb); |
1835 | coro->rouse_cb = SvREFCNT_inc_NN (cb); |
2038 | coro->rouse_cb = SvREFCNT_inc_NN (cb); |
1836 | |
2039 | |
1837 | return cb; |
2040 | return cb; |
… | |
… | |
1889 | static void |
2092 | static void |
1890 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2093 | slf_init_schedule (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1891 | { |
2094 | { |
1892 | frame->prepare = prepare_schedule; |
2095 | frame->prepare = prepare_schedule; |
1893 | frame->check = slf_check_nop; |
2096 | frame->check = slf_check_nop; |
|
|
2097 | } |
|
|
2098 | |
|
|
2099 | static void |
|
|
2100 | slf_prepare_schedule_to (pTHX_ struct coro_transfer_args *ta) |
|
|
2101 | { |
|
|
2102 | struct coro *next = (struct coro *)slf_frame.data; |
|
|
2103 | |
|
|
2104 | SvREFCNT_inc_NN (next->hv); |
|
|
2105 | prepare_schedule_to (aTHX_ ta, next); |
|
|
2106 | } |
|
|
2107 | |
|
|
2108 | static void |
|
|
2109 | slf_init_schedule_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2110 | { |
|
|
2111 | if (!items) |
|
|
2112 | croak ("Coro::schedule_to expects a coroutine argument, caught"); |
|
|
2113 | |
|
|
2114 | frame->data = (void *)SvSTATE (arg [0]); |
|
|
2115 | frame->prepare = slf_prepare_schedule_to; |
|
|
2116 | frame->check = slf_check_nop; |
|
|
2117 | } |
|
|
2118 | |
|
|
2119 | static void |
|
|
2120 | slf_init_cede_to (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2121 | { |
|
|
2122 | api_ready (aTHX_ SvRV (coro_current)); |
|
|
2123 | |
|
|
2124 | slf_init_schedule_to (aTHX_ frame, cv, arg, items); |
1894 | } |
2125 | } |
1895 | |
2126 | |
1896 | static void |
2127 | static void |
1897 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2128 | slf_init_cede (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
1898 | { |
2129 | { |
… | |
… | |
2044 | } |
2275 | } |
2045 | else |
2276 | else |
2046 | slf_argc = 0; |
2277 | slf_argc = 0; |
2047 | |
2278 | |
2048 | PL_op->op_ppaddr = pp_slf; |
2279 | PL_op->op_ppaddr = pp_slf; |
2049 | PL_op->op_type = OP_CUSTOM; /* maybe we should leave it at entersub? */ |
2280 | /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ |
2050 | |
2281 | |
2051 | PL_op = (OP *)&slf_restore; |
2282 | PL_op = (OP *)&slf_restore; |
2052 | } |
2283 | } |
2053 | |
2284 | |
2054 | /*****************************************************************************/ |
2285 | /*****************************************************************************/ |
… | |
… | |
2137 | SV **ary; |
2368 | SV **ary; |
2138 | |
2369 | |
2139 | /* unfortunately, building manually saves memory */ |
2370 | /* unfortunately, building manually saves memory */ |
2140 | Newx (ary, 2, SV *); |
2371 | Newx (ary, 2, SV *); |
2141 | AvALLOC (av) = ary; |
2372 | AvALLOC (av) = ary; |
|
|
2373 | #if PERL_VERSION_ATLEAST (5,10,0) |
2142 | AvARRAY (av) = ary; |
2374 | AvARRAY (av) = ary; |
|
|
2375 | #else |
|
|
2376 | /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */ |
|
|
2377 | /* -DDEBUGGING flags this as a bug, despite it perfectly working */ |
|
|
2378 | SvPVX ((SV *)av) = (char *)ary; |
|
|
2379 | #endif |
2143 | AvMAX (av) = 1; |
2380 | AvMAX (av) = 1; |
2144 | AvFILLp (av) = 0; |
2381 | AvFILLp (av) = 0; |
2145 | ary [0] = newSViv (count); |
2382 | ary [0] = newSViv (count); |
2146 | |
2383 | |
2147 | return newRV_noinc ((SV *)av); |
2384 | return newRV_noinc ((SV *)av); |
… | |
… | |
2167 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2404 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2168 | AvARRAY (av)[1] = count_sv; |
2405 | AvARRAY (av)[1] = count_sv; |
2169 | cb = av_shift (av); |
2406 | cb = av_shift (av); |
2170 | |
2407 | |
2171 | if (SvOBJECT (cb)) |
2408 | if (SvOBJECT (cb)) |
|
|
2409 | { |
2172 | api_ready (aTHX_ cb); |
2410 | api_ready (aTHX_ cb); |
2173 | else |
2411 | --count; |
2174 | croak ("callbacks not yet supported"); |
2412 | } |
|
|
2413 | else if (SvTYPE (cb) == SVt_PVCV) |
|
|
2414 | { |
|
|
2415 | dSP; |
|
|
2416 | PUSHMARK (SP); |
|
|
2417 | XPUSHs (sv_2mortal (newRV_inc ((SV *)av))); |
|
|
2418 | PUTBACK; |
|
|
2419 | call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR); |
|
|
2420 | } |
2175 | |
2421 | |
2176 | SvREFCNT_dec (cb); |
2422 | SvREFCNT_dec (cb); |
2177 | |
|
|
2178 | --count; |
|
|
2179 | } |
2423 | } |
2180 | } |
2424 | } |
2181 | |
2425 | |
2182 | static void |
2426 | static void |
2183 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
2427 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
… | |
… | |
2265 | } |
2509 | } |
2266 | |
2510 | |
2267 | static void |
2511 | static void |
2268 | slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2512 | slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2269 | { |
2513 | { |
|
|
2514 | if (items >= 2) |
|
|
2515 | { |
|
|
2516 | /* callback form */ |
|
|
2517 | AV *av = (AV *)SvRV (arg [0]); |
|
|
2518 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
|
|
2519 | |
|
|
2520 | av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); |
|
|
2521 | |
|
|
2522 | if (SvIVX (AvARRAY (av)[0]) > 0) |
|
|
2523 | coro_semaphore_adjust (aTHX_ av, 0); |
|
|
2524 | |
|
|
2525 | frame->prepare = prepare_nop; |
|
|
2526 | frame->check = slf_check_nop; |
|
|
2527 | } |
|
|
2528 | else |
|
|
2529 | { |
2270 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
2530 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
2271 | frame->check = slf_check_semaphore_wait; |
2531 | frame->check = slf_check_semaphore_wait; |
|
|
2532 | } |
2272 | } |
2533 | } |
2273 | |
2534 | |
2274 | /* signal */ |
2535 | /* signal */ |
2275 | |
2536 | |
2276 | static void |
2537 | static void |
… | |
… | |
2348 | dXSARGS; |
2609 | dXSARGS; |
2349 | AV *state = (AV *)GENSUB_ARG; |
2610 | AV *state = (AV *)GENSUB_ARG; |
2350 | SV *coro = av_pop (state); |
2611 | SV *coro = av_pop (state); |
2351 | SV *data_sv = newSV (sizeof (struct io_state)); |
2612 | SV *data_sv = newSV (sizeof (struct io_state)); |
2352 | |
2613 | |
2353 | av_extend (state, items); |
2614 | av_extend (state, items - 1); |
2354 | |
2615 | |
2355 | sv_upgrade (data_sv, SVt_PV); |
2616 | sv_upgrade (data_sv, SVt_PV); |
2356 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2617 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2357 | SvPOK_only (data_sv); |
2618 | SvPOK_only (data_sv); |
2358 | |
2619 | |
… | |
… | |
2493 | XSRETURN_EMPTY; |
2754 | XSRETURN_EMPTY; |
2494 | } |
2755 | } |
2495 | |
2756 | |
2496 | /*****************************************************************************/ |
2757 | /*****************************************************************************/ |
2497 | |
2758 | |
|
|
2759 | #if CORO_CLONE |
|
|
2760 | # include "clone.c" |
|
|
2761 | #endif |
|
|
2762 | |
2498 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2763 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2499 | |
2764 | |
2500 | PROTOTYPES: DISABLE |
2765 | PROTOTYPES: DISABLE |
2501 | |
2766 | |
2502 | BOOT: |
2767 | BOOT: |
… | |
… | |
2506 | coro_thx = PERL_GET_CONTEXT; |
2771 | coro_thx = PERL_GET_CONTEXT; |
2507 | # endif |
2772 | # endif |
2508 | #endif |
2773 | #endif |
2509 | BOOT_PAGESIZE; |
2774 | BOOT_PAGESIZE; |
2510 | |
2775 | |
|
|
2776 | cctx_current = cctx_new_empty (); |
|
|
2777 | |
2511 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
2778 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
2512 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
2779 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
2513 | |
2780 | |
2514 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
2781 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
2515 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
2782 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
… | |
… | |
2534 | |
2801 | |
2535 | { |
2802 | { |
2536 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
2803 | SV *slf = sv_2mortal (newSViv (PTR2IV (pp_slf))); |
2537 | |
2804 | |
2538 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
2805 | if (!PL_custom_op_names) PL_custom_op_names = newHV (); |
2539 | hv_store_ent (PL_custom_op_names, slf, |
2806 | hv_store_ent (PL_custom_op_names, slf, newSVpv ("coro_slf", 0), 0); |
2540 | newSVpv ("coro_slf", 0), 0); |
|
|
2541 | |
2807 | |
2542 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
2808 | if (!PL_custom_op_descs) PL_custom_op_descs = newHV (); |
2543 | hv_store_ent (PL_custom_op_descs, slf, |
2809 | hv_store_ent (PL_custom_op_descs, slf, newSVpv ("coro schedule like function", 0), 0); |
2544 | newSVpv ("coro schedule like function", 0), 0); |
|
|
2545 | } |
2810 | } |
2546 | |
2811 | |
2547 | coroapi.ver = CORO_API_VERSION; |
2812 | coroapi.ver = CORO_API_VERSION; |
2548 | coroapi.rev = CORO_API_REVISION; |
2813 | coroapi.rev = CORO_API_REVISION; |
2549 | |
2814 | |
… | |
… | |
2568 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2833 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2569 | } |
2834 | } |
2570 | |
2835 | |
2571 | SV * |
2836 | SV * |
2572 | new (char *klass, ...) |
2837 | new (char *klass, ...) |
|
|
2838 | ALIAS: |
|
|
2839 | Coro::new = 1 |
2573 | CODE: |
2840 | CODE: |
2574 | { |
2841 | { |
2575 | struct coro *coro; |
2842 | struct coro *coro; |
2576 | MAGIC *mg; |
2843 | MAGIC *mg; |
2577 | HV *hv; |
2844 | HV *hv; |
|
|
2845 | CV *cb; |
2578 | int i; |
2846 | int i; |
|
|
2847 | |
|
|
2848 | if (items > 1) |
|
|
2849 | { |
|
|
2850 | cb = coro_sv_2cv (aTHX_ ST (1)); |
|
|
2851 | |
|
|
2852 | if (!ix) |
|
|
2853 | { |
|
|
2854 | if (CvISXSUB (cb)) |
|
|
2855 | croak ("Coro::State doesn't support XS functions as coroutine start, caught"); |
|
|
2856 | |
|
|
2857 | if (!CvROOT (cb)) |
|
|
2858 | croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); |
|
|
2859 | } |
|
|
2860 | } |
2579 | |
2861 | |
2580 | Newz (0, coro, 1, struct coro); |
2862 | Newz (0, coro, 1, struct coro); |
2581 | coro->args = newAV (); |
2863 | coro->args = newAV (); |
2582 | coro->flags = CF_NEW; |
2864 | coro->flags = CF_NEW; |
2583 | |
2865 | |
… | |
… | |
2588 | coro->hv = hv = newHV (); |
2870 | coro->hv = hv = newHV (); |
2589 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2871 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2590 | mg->mg_flags |= MGf_DUP; |
2872 | mg->mg_flags |= MGf_DUP; |
2591 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2873 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2592 | |
2874 | |
|
|
2875 | if (items > 1) |
|
|
2876 | { |
2593 | av_extend (coro->args, items - 1); |
2877 | av_extend (coro->args, items - 1 + ix - 1); |
|
|
2878 | |
|
|
2879 | if (ix) |
|
|
2880 | { |
|
|
2881 | av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); |
|
|
2882 | cb = cv_coro_run; |
|
|
2883 | } |
|
|
2884 | |
|
|
2885 | coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); |
|
|
2886 | |
2594 | for (i = 1; i < items; i++) |
2887 | for (i = 2; i < items; i++) |
2595 | av_push (coro->args, newSVsv (ST (i))); |
2888 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2889 | } |
2596 | } |
2890 | } |
2597 | OUTPUT: |
2891 | OUTPUT: |
2598 | RETVAL |
2892 | RETVAL |
2599 | |
2893 | |
2600 | void |
2894 | void |
… | |
… | |
2613 | void |
2907 | void |
2614 | _exit (int code) |
2908 | _exit (int code) |
2615 | PROTOTYPE: $ |
2909 | PROTOTYPE: $ |
2616 | CODE: |
2910 | CODE: |
2617 | _exit (code); |
2911 | _exit (code); |
|
|
2912 | |
|
|
2913 | SV * |
|
|
2914 | clone (Coro::State coro) |
|
|
2915 | CODE: |
|
|
2916 | { |
|
|
2917 | #if CORO_CLONE |
|
|
2918 | struct coro *ncoro = coro_clone (coro); |
|
|
2919 | MAGIC *mg; |
|
|
2920 | /* TODO: too much duplication */ |
|
|
2921 | ncoro->hv = newHV (); |
|
|
2922 | mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); |
|
|
2923 | mg->mg_flags |= MGf_DUP; |
|
|
2924 | RETVAL = sv_bless (newRV_noinc ((SV *)ncoro->hv), SvSTASH (coro->hv)); |
|
|
2925 | #else |
|
|
2926 | croak ("Coro::State->clone has not been configured into this installation of Coro, realised"); |
|
|
2927 | #endif |
|
|
2928 | } |
|
|
2929 | OUTPUT: |
|
|
2930 | RETVAL |
2618 | |
2931 | |
2619 | int |
2932 | int |
2620 | cctx_stacksize (int new_stacksize = 0) |
2933 | cctx_stacksize (int new_stacksize = 0) |
2621 | PROTOTYPE: ;$ |
2934 | PROTOTYPE: ;$ |
2622 | CODE: |
2935 | CODE: |
… | |
… | |
2743 | |
3056 | |
2744 | SV * |
3057 | SV * |
2745 | has_cctx (Coro::State coro) |
3058 | has_cctx (Coro::State coro) |
2746 | PROTOTYPE: $ |
3059 | PROTOTYPE: $ |
2747 | CODE: |
3060 | CODE: |
2748 | RETVAL = boolSV (!!coro->cctx); |
3061 | /* maybe manage the running flag differently */ |
|
|
3062 | RETVAL = boolSV (!!coro->cctx || (coro->flags & CF_RUNNING)); |
2749 | OUTPUT: |
3063 | OUTPUT: |
2750 | RETVAL |
3064 | RETVAL |
2751 | |
3065 | |
2752 | int |
3066 | int |
2753 | is_traced (Coro::State coro) |
3067 | is_traced (Coro::State coro) |
… | |
… | |
2773 | |
3087 | |
2774 | void |
3088 | void |
2775 | force_cctx () |
3089 | force_cctx () |
2776 | PROTOTYPE: |
3090 | PROTOTYPE: |
2777 | CODE: |
3091 | CODE: |
2778 | SvSTATE_current->cctx->idle_sp = 0; |
3092 | cctx_current->idle_sp = 0; |
2779 | |
3093 | |
2780 | void |
3094 | void |
2781 | swap_defsv (Coro::State self) |
3095 | swap_defsv (Coro::State self) |
2782 | PROTOTYPE: $ |
3096 | PROTOTYPE: $ |
2783 | ALIAS: |
3097 | ALIAS: |
… | |
… | |
2798 | |
3112 | |
2799 | BOOT: |
3113 | BOOT: |
2800 | { |
3114 | { |
2801 | int i; |
3115 | int i; |
2802 | |
3116 | |
2803 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
|
|
2804 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3117 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2805 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
3118 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2806 | |
3119 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
|
|
3120 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
2807 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
3121 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
2808 | SvREADONLY_on (coro_current); |
3122 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
|
|
3123 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
|
|
3124 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
|
|
3125 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
|
|
3126 | |
|
|
3127 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
|
|
3128 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
|
|
3129 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
|
|
3130 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
2809 | |
3131 | |
2810 | coro_stash = gv_stashpv ("Coro", TRUE); |
3132 | coro_stash = gv_stashpv ("Coro", TRUE); |
2811 | |
3133 | |
2812 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
3134 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
2813 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
3135 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
… | |
… | |
2821 | |
3143 | |
2822 | { |
3144 | { |
2823 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
3145 | SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); |
2824 | |
3146 | |
2825 | coroapi.schedule = api_schedule; |
3147 | coroapi.schedule = api_schedule; |
|
|
3148 | coroapi.schedule_to = api_schedule_to; |
2826 | coroapi.cede = api_cede; |
3149 | coroapi.cede = api_cede; |
2827 | coroapi.cede_notself = api_cede_notself; |
3150 | coroapi.cede_notself = api_cede_notself; |
2828 | coroapi.ready = api_ready; |
3151 | coroapi.ready = api_ready; |
2829 | coroapi.is_ready = api_is_ready; |
3152 | coroapi.is_ready = api_is_ready; |
2830 | coroapi.nready = coro_nready; |
3153 | coroapi.nready = coro_nready; |
… | |
… | |
2835 | SvREADONLY_on (sv); |
3158 | SvREADONLY_on (sv); |
2836 | } |
3159 | } |
2837 | } |
3160 | } |
2838 | |
3161 | |
2839 | void |
3162 | void |
|
|
3163 | terminate (...) |
|
|
3164 | CODE: |
|
|
3165 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
|
|
3166 | |
|
|
3167 | void |
2840 | schedule (...) |
3168 | schedule (...) |
2841 | CODE: |
3169 | CODE: |
2842 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
3170 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
|
|
3171 | |
|
|
3172 | void |
|
|
3173 | schedule_to (...) |
|
|
3174 | CODE: |
|
|
3175 | CORO_EXECUTE_SLF_XS (slf_init_schedule_to); |
|
|
3176 | |
|
|
3177 | void |
|
|
3178 | cede_to (...) |
|
|
3179 | CODE: |
|
|
3180 | CORO_EXECUTE_SLF_XS (slf_init_cede_to); |
2843 | |
3181 | |
2844 | void |
3182 | void |
2845 | cede (...) |
3183 | cede (...) |
2846 | CODE: |
3184 | CODE: |
2847 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
3185 | CORO_EXECUTE_SLF_XS (slf_init_cede); |
2848 | |
3186 | |
2849 | void |
3187 | void |
2850 | cede_notself (...) |
3188 | cede_notself (...) |
2851 | CODE: |
3189 | CODE: |
2852 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
3190 | CORO_EXECUTE_SLF_XS (slf_init_cede_notself); |
|
|
3191 | |
|
|
3192 | void |
|
|
3193 | _cancel (Coro::State self) |
|
|
3194 | CODE: |
|
|
3195 | coro_state_destroy (aTHX_ self); |
|
|
3196 | coro_call_on_destroy (aTHX_ self); |
2853 | |
3197 | |
2854 | void |
3198 | void |
2855 | _set_current (SV *current) |
3199 | _set_current (SV *current) |
2856 | PROTOTYPE: $ |
3200 | PROTOTYPE: $ |
2857 | CODE: |
3201 | CODE: |
… | |
… | |
2902 | CODE: |
3246 | CODE: |
2903 | RETVAL = coro_nready; |
3247 | RETVAL = coro_nready; |
2904 | OUTPUT: |
3248 | OUTPUT: |
2905 | RETVAL |
3249 | RETVAL |
2906 | |
3250 | |
2907 | # for async_pool speedup |
|
|
2908 | void |
3251 | void |
2909 | _pool_1 (SV *cb) |
3252 | _pool_handler (...) |
2910 | CODE: |
3253 | CODE: |
2911 | { |
3254 | CORO_EXECUTE_SLF_XS (slf_init_pool_handler); |
2912 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2913 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
2914 | AV *defav = GvAV (PL_defgv); |
|
|
2915 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
|
|
2916 | AV *invoke_av; |
|
|
2917 | int i, len; |
|
|
2918 | |
3255 | |
2919 | if (!invoke) |
3256 | void |
|
|
3257 | async_pool (SV *cv, ...) |
|
|
3258 | PROTOTYPE: &@ |
|
|
3259 | PPCODE: |
|
|
3260 | { |
|
|
3261 | HV *hv = (HV *)av_pop (av_async_pool); |
|
|
3262 | AV *av = newAV (); |
|
|
3263 | SV *cb = ST (0); |
|
|
3264 | int i; |
|
|
3265 | |
|
|
3266 | av_extend (av, items - 2); |
|
|
3267 | for (i = 1; i < items; ++i) |
|
|
3268 | av_push (av, SvREFCNT_inc_NN (ST (i))); |
|
|
3269 | |
|
|
3270 | if ((SV *)hv == &PL_sv_undef) |
2920 | { |
3271 | { |
2921 | SV *old = PL_diehook; |
3272 | PUSHMARK (SP); |
2922 | PL_diehook = 0; |
3273 | EXTEND (SP, 2); |
2923 | SvREFCNT_dec (old); |
3274 | PUSHs (sv_Coro); |
2924 | croak ("\3async_pool terminate\2\n"); |
3275 | PUSHs ((SV *)cv_pool_handler); |
|
|
3276 | PUTBACK; |
|
|
3277 | call_sv ((SV *)cv_coro_state_new, G_SCALAR); |
|
|
3278 | SPAGAIN; |
|
|
3279 | |
|
|
3280 | hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); |
2925 | } |
3281 | } |
2926 | |
3282 | |
2927 | SvREFCNT_dec (coro->saved_deffh); |
|
|
2928 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
|
|
2929 | |
|
|
2930 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
2931 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
2932 | |
|
|
2933 | invoke_av = (AV *)SvRV (invoke); |
|
|
2934 | len = av_len (invoke_av); |
|
|
2935 | |
|
|
2936 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
|
|
2937 | |
|
|
2938 | if (len > 0) |
|
|
2939 | { |
3283 | { |
2940 | av_fill (defav, len - 1); |
3284 | struct coro *coro = SvSTATE_hv (hv); |
2941 | for (i = 0; i < len; ++i) |
3285 | |
2942 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
3286 | assert (!coro->invoke_cb); |
|
|
3287 | assert (!coro->invoke_av); |
|
|
3288 | coro->invoke_cb = SvREFCNT_inc (cb); |
|
|
3289 | coro->invoke_av = av; |
2943 | } |
3290 | } |
2944 | } |
|
|
2945 | |
3291 | |
2946 | void |
3292 | api_ready (aTHX_ (SV *)hv); |
2947 | _pool_2 (SV *cb) |
|
|
2948 | CODE: |
|
|
2949 | { |
|
|
2950 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2951 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
2952 | |
3293 | |
2953 | sv_setsv (cb, &PL_sv_undef); |
3294 | if (GIMME_V != G_VOID) |
2954 | |
3295 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); |
2955 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
3296 | else |
2956 | coro->saved_deffh = 0; |
|
|
2957 | |
|
|
2958 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
|
|
2959 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
2960 | { |
|
|
2961 | SV *old = PL_diehook; |
|
|
2962 | PL_diehook = 0; |
|
|
2963 | SvREFCNT_dec (old); |
3297 | SvREFCNT_dec (hv); |
2964 | croak ("\3async_pool terminate\2\n"); |
|
|
2965 | } |
|
|
2966 | |
|
|
2967 | av_clear (GvAV (PL_defgv)); |
|
|
2968 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
2969 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
|
|
2970 | |
|
|
2971 | coro->prio = 0; |
|
|
2972 | |
|
|
2973 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
|
|
2974 | api_trace (aTHX_ coro_current, 0); |
|
|
2975 | |
|
|
2976 | av_push (av_async_pool, newSVsv (coro_current)); |
|
|
2977 | } |
3298 | } |
2978 | |
3299 | |
2979 | SV * |
3300 | SV * |
2980 | rouse_cb () |
3301 | rouse_cb () |
2981 | PROTOTYPE: |
3302 | PROTOTYPE: |
… | |
… | |
2983 | RETVAL = coro_new_rouse_cb (aTHX); |
3304 | RETVAL = coro_new_rouse_cb (aTHX); |
2984 | OUTPUT: |
3305 | OUTPUT: |
2985 | RETVAL |
3306 | RETVAL |
2986 | |
3307 | |
2987 | void |
3308 | void |
2988 | rouse_wait (SV *cb = 0) |
3309 | rouse_wait (...) |
2989 | PROTOTYPE: ;$ |
3310 | PROTOTYPE: ;$ |
2990 | PPCODE: |
3311 | PPCODE: |
2991 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
3312 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
2992 | |
3313 | |
2993 | |
3314 | |
… | |
… | |
3030 | adjust = 1 |
3351 | adjust = 1 |
3031 | CODE: |
3352 | CODE: |
3032 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
3353 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
3033 | |
3354 | |
3034 | void |
3355 | void |
3035 | down (SV *self) |
3356 | down (...) |
3036 | CODE: |
3357 | CODE: |
3037 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
3358 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
3038 | |
3359 | |
3039 | void |
3360 | void |
3040 | wait (SV *self) |
3361 | wait (...) |
3041 | CODE: |
3362 | CODE: |
3042 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); |
3363 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); |
3043 | |
3364 | |
3044 | void |
3365 | void |
3045 | try (SV *self) |
3366 | try (SV *self) |
… | |
… | |
3088 | ); |
3409 | ); |
3089 | OUTPUT: |
3410 | OUTPUT: |
3090 | RETVAL |
3411 | RETVAL |
3091 | |
3412 | |
3092 | void |
3413 | void |
3093 | wait (SV *self) |
3414 | wait (...) |
3094 | CODE: |
3415 | CODE: |
3095 | CORO_EXECUTE_SLF_XS (slf_init_signal_wait); |
3416 | CORO_EXECUTE_SLF_XS (slf_init_signal_wait); |
3096 | |
3417 | |
3097 | void |
3418 | void |
3098 | broadcast (SV *self) |
3419 | broadcast (SV *self) |
… | |
… | |
3155 | |
3476 | |
3156 | void |
3477 | void |
3157 | _register (char *target, char *proto, SV *req) |
3478 | _register (char *target, char *proto, SV *req) |
3158 | CODE: |
3479 | CODE: |
3159 | { |
3480 | { |
3160 | HV *st; |
3481 | CV *req_cv = coro_sv_2cv (aTHX_ req); |
3161 | GV *gvp; |
|
|
3162 | CV *req_cv = sv_2cv (req, &st, &gvp, 0); |
|
|
3163 | /* newXSproto doesn't return the CV on 5.8 */ |
3482 | /* newXSproto doesn't return the CV on 5.8 */ |
3164 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3483 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3165 | sv_setpv ((SV *)slf_cv, proto); |
3484 | sv_setpv ((SV *)slf_cv, proto); |
3166 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3485 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3167 | } |
3486 | } |