… | |
… | |
10 | #include "patchlevel.h" |
10 | #include "patchlevel.h" |
11 | |
11 | |
12 | #include <stdio.h> |
12 | #include <stdio.h> |
13 | #include <errno.h> |
13 | #include <errno.h> |
14 | #include <assert.h> |
14 | #include <assert.h> |
|
|
15 | #include <inttypes.h> /* portable stdint.h */ |
15 | |
16 | |
16 | #ifdef HAVE_MMAP |
17 | #ifdef HAVE_MMAP |
17 | # include <unistd.h> |
18 | # include <unistd.h> |
18 | # include <sys/mman.h> |
19 | # include <sys/mman.h> |
19 | # ifndef MAP_ANONYMOUS |
20 | # ifndef MAP_ANONYMOUS |
… | |
… | |
130 | #else |
131 | #else |
131 | # define LOCK (void)0 |
132 | # define LOCK (void)0 |
132 | # define UNLOCK (void)0 |
133 | # define UNLOCK (void)0 |
133 | #endif |
134 | #endif |
134 | |
135 | |
135 | #define strpair(const) const, sizeof (const) - 1 |
|
|
136 | |
|
|
137 | /* helper storage struct for Coro::AIO */ |
136 | /* helper storage struct for Coro::AIO */ |
138 | struct io_state |
137 | struct io_state |
139 | { |
138 | { |
140 | int errorno; |
139 | int errorno; |
141 | I32 laststype; |
140 | I32 laststype; |
… | |
… | |
150 | static HV *coro_state_stash, *coro_stash; |
149 | static HV *coro_state_stash, *coro_stash; |
151 | static SV *coro_mortal; /* will be freed after next transfer */ |
150 | static SV *coro_mortal; /* will be freed after next transfer */ |
152 | |
151 | |
153 | static GV *irsgv; /* $/ */ |
152 | static GV *irsgv; /* $/ */ |
154 | static GV *stdoutgv; /* *STDOUT */ |
153 | static GV *stdoutgv; /* *STDOUT */ |
155 | |
154 | static SV *rv_diehook; |
|
|
155 | static SV *rv_warnhook; |
156 | static HV *hv_sig; /* %SIG */ |
156 | static HV *hv_sig; /* %SIG */ |
157 | static SV *sv_diehook; |
|
|
158 | static SV *sv_warnhook; |
|
|
159 | |
157 | |
160 | /* async_pool helper stuff */ |
158 | /* async_pool helper stuff */ |
161 | static SV *sv_pool_rss; |
159 | static SV *sv_pool_rss; |
162 | static SV *sv_pool_size; |
160 | static SV *sv_pool_size; |
163 | static AV *av_async_pool; |
161 | static AV *av_async_pool; |
… | |
… | |
221 | |
219 | |
222 | /* process data */ |
220 | /* process data */ |
223 | AV *mainstack; |
221 | AV *mainstack; |
224 | perl_slots *slot; /* basically the saved sp */ |
222 | perl_slots *slot; /* basically the saved sp */ |
225 | |
223 | |
226 | /* data associated with this coroutine (initial args) */ |
224 | AV *args; /* data associated with this coroutine (initial args) */ |
227 | AV *args; |
225 | int refcnt; /* coroutines are refcounted, yes */ |
228 | int refcnt; |
|
|
229 | int flags; /* CF_ flags */ |
226 | int flags; /* CF_ flags */ |
|
|
227 | HV *hv; /* the perl hash associated with this coro, if any */ |
230 | |
228 | |
231 | /* statistics */ |
229 | /* statistics */ |
232 | int usecount; /* number of transfers to this coro */ |
230 | int usecount; /* number of transfers to this coro */ |
233 | |
231 | |
234 | /* coro process data */ |
232 | /* coro process data */ |
235 | int prio; |
233 | int prio; |
236 | SV *throw; |
234 | SV *throw; /* exception to be thrown */ |
237 | |
235 | |
238 | /* async_pool */ |
236 | /* async_pool */ |
239 | SV *saved_deffh; |
237 | SV *saved_deffh; |
240 | |
238 | |
241 | /* linked list */ |
239 | /* linked list */ |
242 | struct coro *next, *prev; |
240 | struct coro *next, *prev; |
243 | HV *hv; /* the perl hash associated with this coro, if any */ |
|
|
244 | }; |
241 | }; |
245 | |
242 | |
246 | typedef struct coro *Coro__State; |
243 | typedef struct coro *Coro__State; |
247 | typedef struct coro *Coro__State_or_hashref; |
244 | typedef struct coro *Coro__State_or_hashref; |
248 | |
245 | |
… | |
… | |
262 | static struct coro *coro_first; |
259 | static struct coro *coro_first; |
263 | |
260 | |
264 | /** lowlevel stuff **********************************************************/ |
261 | /** lowlevel stuff **********************************************************/ |
265 | |
262 | |
266 | static SV * |
263 | static SV * |
267 | coro_get_sv (const char *name, int create) |
264 | coro_get_sv (pTHX_ const char *name, int create) |
268 | { |
265 | { |
269 | #if PERL_VERSION_ATLEAST (5,9,0) |
266 | #if PERL_VERSION_ATLEAST (5,9,0) |
270 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
267 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
271 | get_sv (name, create); |
268 | get_sv (name, create); |
272 | #endif |
269 | #endif |
273 | return get_sv (name, create); |
270 | return get_sv (name, create); |
274 | } |
271 | } |
275 | |
272 | |
276 | static AV * |
273 | static AV * |
277 | coro_get_av (const char *name, int create) |
274 | coro_get_av (pTHX_ const char *name, int create) |
278 | { |
275 | { |
279 | #if PERL_VERSION_ATLEAST (5,9,0) |
276 | #if PERL_VERSION_ATLEAST (5,9,0) |
280 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
277 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
281 | get_av (name, create); |
278 | get_av (name, create); |
282 | #endif |
279 | #endif |
283 | return get_av (name, create); |
280 | return get_av (name, create); |
284 | } |
281 | } |
285 | |
282 | |
286 | static HV * |
283 | static HV * |
287 | coro_get_hv (const char *name, int create) |
284 | coro_get_hv (pTHX_ const char *name, int create) |
288 | { |
285 | { |
289 | #if PERL_VERSION_ATLEAST (5,9,0) |
286 | #if PERL_VERSION_ATLEAST (5,9,0) |
290 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
287 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
291 | get_hv (name, create); |
288 | get_hv (name, create); |
292 | #endif |
289 | #endif |
… | |
… | |
347 | |
344 | |
348 | /* casting is fun. */ |
345 | /* casting is fun. */ |
349 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
346 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
350 | free_padlist (aTHX_ padlist); |
347 | free_padlist (aTHX_ padlist); |
351 | |
348 | |
352 | SvREFCNT_dec (av); |
|
|
353 | |
|
|
354 | return 0; |
349 | return 0; |
355 | } |
350 | } |
356 | |
351 | |
357 | #define PERL_MAGIC_coro PERL_MAGIC_ext |
352 | #define CORO_MAGIC_type_cv PERL_MAGIC_ext |
|
|
353 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
358 | |
354 | |
359 | static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; |
355 | static MGVTBL coro_cv_vtbl = { |
|
|
356 | 0, 0, 0, 0, |
|
|
357 | coro_cv_free |
|
|
358 | }; |
360 | |
359 | |
361 | #define CORO_MAGIC(cv) \ |
360 | #define CORO_MAGIC(sv,type) \ |
362 | SvMAGIC (cv) \ |
361 | SvMAGIC (sv) \ |
363 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
362 | ? SvMAGIC (sv)->mg_type == type \ |
364 | ? SvMAGIC (cv) \ |
363 | ? SvMAGIC (sv) \ |
365 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
364 | : mg_find (sv, type) \ |
366 | : 0 |
365 | : 0 |
|
|
366 | |
|
|
367 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
|
|
368 | #define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) |
367 | |
369 | |
368 | static struct coro * |
370 | static struct coro * |
369 | SvSTATE_ (pTHX_ SV *coro) |
371 | SvSTATE_ (pTHX_ SV *coro) |
370 | { |
372 | { |
371 | HV *stash; |
373 | HV *stash; |
… | |
… | |
383 | /* very slow, but rare, check */ |
385 | /* very slow, but rare, check */ |
384 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
386 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
385 | croak ("Coro::State object required"); |
387 | croak ("Coro::State object required"); |
386 | } |
388 | } |
387 | |
389 | |
388 | mg = CORO_MAGIC (coro); |
390 | mg = CORO_MAGIC_state (coro); |
389 | return (struct coro *)mg->mg_ptr; |
391 | return (struct coro *)mg->mg_ptr; |
390 | } |
392 | } |
391 | |
393 | |
392 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
394 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
393 | |
395 | |
394 | /* the next two functions merely cache the padlists */ |
396 | /* the next two functions merely cache the padlists */ |
395 | static void |
397 | static void |
396 | get_padlist (pTHX_ CV *cv) |
398 | get_padlist (pTHX_ CV *cv) |
397 | { |
399 | { |
398 | MAGIC *mg = CORO_MAGIC (cv); |
400 | MAGIC *mg = CORO_MAGIC_cv (cv); |
399 | AV *av; |
401 | AV *av; |
400 | |
402 | |
401 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
403 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
402 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
404 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
403 | else |
405 | else |
… | |
… | |
415 | } |
417 | } |
416 | |
418 | |
417 | static void |
419 | static void |
418 | put_padlist (pTHX_ CV *cv) |
420 | put_padlist (pTHX_ CV *cv) |
419 | { |
421 | { |
420 | MAGIC *mg = CORO_MAGIC (cv); |
422 | MAGIC *mg = CORO_MAGIC_cv (cv); |
421 | AV *av; |
423 | AV *av; |
422 | |
424 | |
423 | if (expect_false (!mg)) |
425 | if (expect_false (!mg)) |
424 | { |
426 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
425 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
|
|
426 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
|
|
427 | mg->mg_virtual = &vtbl_coro; |
|
|
428 | mg->mg_obj = (SV *)newAV (); |
|
|
429 | } |
|
|
430 | |
427 | |
431 | av = (AV *)mg->mg_obj; |
428 | av = (AV *)mg->mg_obj; |
432 | |
429 | |
433 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
430 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
434 | av_extend (av, AvMAX (av) + 1); |
431 | av_extend (av, AvMAX (av) + 1); |
… | |
… | |
521 | |
518 | |
522 | PUTBACK; |
519 | PUTBACK; |
523 | } |
520 | } |
524 | |
521 | |
525 | /* allocate some space on the context stack for our purposes */ |
522 | /* allocate some space on the context stack for our purposes */ |
|
|
523 | /* we manually unroll here, as usually 2 slots is enough */ |
|
|
524 | if (SLOT_COUNT >= 1) CXINC; |
|
|
525 | if (SLOT_COUNT >= 2) CXINC; |
|
|
526 | if (SLOT_COUNT >= 3) CXINC; |
526 | { |
527 | { |
527 | /* we manually unroll here, as usually 2 slots is enough */ |
|
|
528 | int i; |
528 | int i; |
529 | if (SLOT_COUNT >= 1) CXINC; |
|
|
530 | if (SLOT_COUNT >= 2) CXINC; |
|
|
531 | if (SLOT_COUNT >= 3) CXINC; |
|
|
532 | for (i = 3; i < SLOT_COUNT; ++i) |
529 | for (i = 3; i < SLOT_COUNT; ++i) |
533 | CXINC; |
530 | CXINC; |
534 | |
|
|
535 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
|
|
536 | } |
531 | } |
|
|
532 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
537 | |
533 | |
538 | c->mainstack = PL_mainstack; |
534 | c->mainstack = PL_mainstack; |
539 | |
535 | |
540 | { |
536 | { |
541 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
537 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
… | |
… | |
668 | return rss; |
664 | return rss; |
669 | } |
665 | } |
670 | |
666 | |
671 | /** coroutine stack handling ************************************************/ |
667 | /** coroutine stack handling ************************************************/ |
672 | |
668 | |
|
|
669 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
|
|
670 | |
|
|
671 | /* |
|
|
672 | * This overrides the default magic get method of %SIG elements. |
|
|
673 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
|
|
674 | * and instead of tryign to save and restore the hash elements, we just provide |
|
|
675 | * readback here. |
|
|
676 | * We only do this when the hook is != 0, as they are often set to 0 temporarily, |
|
|
677 | * not expecting this to actually change the hook. This is a potential problem |
|
|
678 | * when a schedule happens then, but we ignore this. |
|
|
679 | */ |
|
|
680 | static int |
|
|
681 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
|
|
682 | { |
|
|
683 | const char *s = MgPV_nolen_const (mg); |
|
|
684 | |
|
|
685 | if (*s == '_') |
|
|
686 | { |
|
|
687 | if (strEQ (s, "__DIE__" ) && PL_diehook ) return sv_setsv (sv, PL_diehook ), 0; |
|
|
688 | if (strEQ (s, "__WARN__") && PL_warnhook) return sv_setsv (sv, PL_warnhook), 0; |
|
|
689 | } |
|
|
690 | |
|
|
691 | return orig_sigelem_get (aTHX_ sv, mg); |
|
|
692 | } |
|
|
693 | |
673 | static void |
694 | static void |
674 | coro_setup (pTHX_ struct coro *coro) |
695 | coro_setup (pTHX_ struct coro *coro) |
675 | { |
696 | { |
676 | /* |
697 | /* |
677 | * emulate part of the perl startup here. |
698 | * emulate part of the perl startup here. |
… | |
… | |
685 | PL_curpm = 0; |
706 | PL_curpm = 0; |
686 | PL_curpad = 0; |
707 | PL_curpad = 0; |
687 | PL_localizing = 0; |
708 | PL_localizing = 0; |
688 | PL_dirty = 0; |
709 | PL_dirty = 0; |
689 | PL_restartop = 0; |
710 | PL_restartop = 0; |
690 | SvREFCNT_inc (PL_diehook ); hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0); |
711 | |
691 | SvREFCNT_inc (PL_warnhook); hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0); |
712 | /* recreate the die/warn hooks */ |
|
|
713 | PL_diehook = 0; |
|
|
714 | hv_store (hv_sig, "__DIE__", sizeof ("__DIE__") - 1, newSV (0), 0); |
|
|
715 | PL_diehook = SvREFCNT_inc (rv_diehook); |
|
|
716 | |
|
|
717 | PL_warnhook = 0; |
|
|
718 | hv_store (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, newSV (0), 0); |
|
|
719 | PL_warnhook = SvREFCNT_inc (rv_warnhook); |
692 | |
720 | |
693 | GvSV (PL_defgv) = newSV (0); |
721 | GvSV (PL_defgv) = newSV (0); |
694 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
722 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
695 | GvSV (PL_errgv) = newSV (0); |
723 | GvSV (PL_errgv) = newSV (0); |
696 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
724 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
… | |
… | |
711 | PL_op = (OP *)&myop; |
739 | PL_op = (OP *)&myop; |
712 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
740 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
713 | SPAGAIN; |
741 | SPAGAIN; |
714 | } |
742 | } |
715 | |
743 | |
716 | ENTER; /* necessary e.g. for dounwind and to balance the xsub-entersub */ |
744 | /* this newly created coroutine might be run on an existing cctx which most |
|
|
745 | * likely was suspended in set_stacklevel, called from entersub. |
|
|
746 | * set_stacklevl doesn't do anything on return, but entersub does LEAVE, |
|
|
747 | * so we ENTER here for symmetry |
|
|
748 | */ |
|
|
749 | ENTER; |
717 | } |
750 | } |
718 | |
751 | |
719 | static void |
752 | static void |
720 | coro_destroy (pTHX_ struct coro *coro) |
753 | coro_destroy (pTHX_ struct coro *coro) |
721 | { |
754 | { |
… | |
… | |
805 | PUSHMARK (SP); |
838 | PUSHMARK (SP); |
806 | PUSHs (&PL_sv_no); |
839 | PUSHs (&PL_sv_no); |
807 | PUSHs (fullname); |
840 | PUSHs (fullname); |
808 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
841 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
809 | PUTBACK; |
842 | PUTBACK; |
810 | cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0); |
843 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
811 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
844 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
812 | SPAGAIN; |
845 | SPAGAIN; |
813 | FREETMPS; |
846 | FREETMPS; |
814 | LEAVE; |
847 | LEAVE; |
815 | PL_runops = runops_trace; |
848 | PL_runops = runops_trace; |
… | |
… | |
844 | PUSHMARK (SP); |
877 | PUSHMARK (SP); |
845 | PUSHs (&PL_sv_yes); |
878 | PUSHs (&PL_sv_yes); |
846 | PUSHs (fullname); |
879 | PUSHs (fullname); |
847 | PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
880 | PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
848 | PUTBACK; |
881 | PUTBACK; |
849 | cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0); |
882 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
850 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
883 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
851 | SPAGAIN; |
884 | SPAGAIN; |
852 | FREETMPS; |
885 | FREETMPS; |
853 | LEAVE; |
886 | LEAVE; |
854 | PL_runops = runops_trace; |
887 | PL_runops = runops_trace; |
… | |
… | |
868 | PL_runops = RUNOPS_DEFAULT; |
901 | PL_runops = RUNOPS_DEFAULT; |
869 | PUSHMARK (SP); |
902 | PUSHMARK (SP); |
870 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
903 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
871 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
904 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
872 | PUTBACK; |
905 | PUTBACK; |
873 | cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_line_cb"), 0); |
906 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0); |
874 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
907 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
875 | SPAGAIN; |
908 | SPAGAIN; |
876 | FREETMPS; |
909 | FREETMPS; |
877 | LEAVE; |
910 | LEAVE; |
878 | PL_runops = runops_trace; |
911 | PL_runops = runops_trace; |
… | |
… | |
1108 | prev->flags &= ~CF_RUNNING; |
1141 | prev->flags &= ~CF_RUNNING; |
1109 | next->flags |= CF_RUNNING; |
1142 | next->flags |= CF_RUNNING; |
1110 | |
1143 | |
1111 | LOCK; |
1144 | LOCK; |
1112 | |
1145 | |
|
|
1146 | /* first get rid of the old state */ |
|
|
1147 | save_perl (aTHX_ prev); |
|
|
1148 | |
1113 | if (expect_false (next->flags & CF_NEW)) |
1149 | if (expect_false (next->flags & CF_NEW)) |
1114 | { |
1150 | { |
1115 | /* need to start coroutine */ |
1151 | /* need to start coroutine */ |
1116 | next->flags &= ~CF_NEW; |
1152 | next->flags &= ~CF_NEW; |
1117 | /* first get rid of the old state */ |
|
|
1118 | save_perl (aTHX_ prev); |
|
|
1119 | /* setup coroutine call */ |
1153 | /* setup coroutine call */ |
1120 | coro_setup (aTHX_ next); |
1154 | coro_setup (aTHX_ next); |
1121 | } |
1155 | } |
1122 | else |
1156 | else |
1123 | { |
|
|
1124 | /* coroutine already started */ |
|
|
1125 | save_perl (aTHX_ prev); |
|
|
1126 | load_perl (aTHX_ next); |
1157 | load_perl (aTHX_ next); |
1127 | } |
|
|
1128 | |
1158 | |
1129 | prev__cctx = prev->cctx; |
1159 | prev__cctx = prev->cctx; |
1130 | |
1160 | |
1131 | /* possibly "free" the cctx */ |
1161 | /* possibly "free" the cctx */ |
1132 | if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))) |
1162 | if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))) |
… | |
… | |
1501 | BOOT_PAGESIZE; |
1531 | BOOT_PAGESIZE; |
1502 | |
1532 | |
1503 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1533 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1504 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1534 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1505 | |
1535 | |
|
|
1536 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; |
|
|
1537 | PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
|
|
1538 | |
1506 | hv_sig = coro_get_hv ("SIG", TRUE); |
1539 | hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); |
1507 | sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE); |
1540 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
1508 | sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE); |
1541 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
1509 | |
|
|
1510 | if (!PL_diehook ) hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0); |
|
|
1511 | if (!PL_warnhook) hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0); |
|
|
1512 | |
1542 | |
1513 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1543 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1514 | |
1544 | |
1515 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1545 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1516 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
1546 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
… | |
… | |
1522 | |
1552 | |
1523 | while (main_top_env->je_prev) |
1553 | while (main_top_env->je_prev) |
1524 | main_top_env = main_top_env->je_prev; |
1554 | main_top_env = main_top_env->je_prev; |
1525 | |
1555 | |
1526 | coroapi.ver = CORO_API_VERSION; |
1556 | coroapi.ver = CORO_API_VERSION; |
|
|
1557 | coroapi.rev = CORO_API_REVISION; |
1527 | coroapi.transfer = api_transfer; |
1558 | coroapi.transfer = api_transfer; |
1528 | |
1559 | |
1529 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1560 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1530 | } |
1561 | } |
1531 | |
1562 | |
1532 | SV * |
1563 | SV * |
1533 | new (char *klass, ...) |
1564 | new (char *klass, ...) |
1534 | CODE: |
1565 | CODE: |
1535 | { |
1566 | { |
1536 | struct coro *coro; |
1567 | struct coro *coro; |
|
|
1568 | MAGIC *mg; |
1537 | HV *hv; |
1569 | HV *hv; |
1538 | int i; |
1570 | int i; |
1539 | |
1571 | |
1540 | Newz (0, coro, 1, struct coro); |
1572 | Newz (0, coro, 1, struct coro); |
1541 | coro->args = newAV (); |
1573 | coro->args = newAV (); |
… | |
… | |
1544 | if (coro_first) coro_first->prev = coro; |
1576 | if (coro_first) coro_first->prev = coro; |
1545 | coro->next = coro_first; |
1577 | coro->next = coro_first; |
1546 | coro_first = coro; |
1578 | coro_first = coro; |
1547 | |
1579 | |
1548 | coro->hv = hv = newHV (); |
1580 | coro->hv = hv = newHV (); |
1549 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1581 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
|
|
1582 | mg->mg_flags |= MGf_DUP; |
1550 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1583 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1551 | |
1584 | |
1552 | av_extend (coro->args, items - 1); |
1585 | av_extend (coro->args, items - 1); |
1553 | for (i = 1; i < items; i++) |
1586 | for (i = 1; i < items; i++) |
1554 | av_push (coro->args, newSVsv (ST (i))); |
1587 | av_push (coro->args, newSVsv (ST (i))); |
… | |
… | |
1598 | |
1631 | |
1599 | break; |
1632 | break; |
1600 | } |
1633 | } |
1601 | |
1634 | |
1602 | BARRIER; |
1635 | BARRIER; |
|
|
1636 | PUTBACK; |
1603 | TRANSFER (ta); |
1637 | TRANSFER (ta); |
1604 | |
1638 | SPAGAIN; /* might be the sp of a different coroutine now */ |
1605 | if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev)) |
1639 | /* be extra careful not to ever do anything after TRANSFER */ |
1606 | XSRETURN_YES; |
|
|
1607 | } |
1640 | } |
1608 | |
1641 | |
1609 | bool |
1642 | bool |
1610 | _destroy (SV *coro_sv) |
1643 | _destroy (SV *coro_sv) |
1611 | CODE: |
1644 | CODE: |
… | |
… | |
1671 | |
1704 | |
1672 | { |
1705 | { |
1673 | dSP; |
1706 | dSP; |
1674 | ENTER; |
1707 | ENTER; |
1675 | SAVETMPS; |
1708 | SAVETMPS; |
|
|
1709 | PUTBACK; |
|
|
1710 | PUSHSTACK; |
1676 | PUSHMARK (SP); |
1711 | PUSHMARK (SP); |
1677 | PUTBACK; |
|
|
1678 | |
1712 | |
1679 | if (ix) |
1713 | if (ix) |
1680 | eval_sv (coderef, 0); |
1714 | eval_sv (coderef, 0); |
1681 | else |
1715 | else |
1682 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1716 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1683 | |
1717 | |
|
|
1718 | POPSTACK; |
1684 | SPAGAIN; |
1719 | SPAGAIN; |
1685 | FREETMPS; |
1720 | FREETMPS; |
1686 | LEAVE; |
1721 | LEAVE; |
1687 | PUTBACK; |
1722 | PUTBACK; |
1688 | } |
1723 | } |
… | |
… | |
1746 | |
1781 | |
1747 | BOOT: |
1782 | BOOT: |
1748 | { |
1783 | { |
1749 | int i; |
1784 | int i; |
1750 | |
1785 | |
1751 | sv_pool_rss = coro_get_sv ("Coro::POOL_RSS" , TRUE); |
1786 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
1752 | sv_pool_size = coro_get_sv ("Coro::POOL_SIZE" , TRUE); |
1787 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
1753 | av_async_pool = coro_get_av ("Coro::async_pool", TRUE); |
1788 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
1754 | |
1789 | |
1755 | coro_current = coro_get_sv ("Coro::current", FALSE); |
1790 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
1756 | SvREADONLY_on (coro_current); |
1791 | SvREADONLY_on (coro_current); |
1757 | |
1792 | |
1758 | coro_stash = gv_stashpv ("Coro", TRUE); |
1793 | coro_stash = gv_stashpv ("Coro", TRUE); |
1759 | |
1794 | |
1760 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1795 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
… | |
… | |
1843 | CODE: |
1878 | CODE: |
1844 | { |
1879 | { |
1845 | struct coro *coro = SvSTATE (coro_current); |
1880 | struct coro *coro = SvSTATE (coro_current); |
1846 | HV *hv = (HV *)SvRV (coro_current); |
1881 | HV *hv = (HV *)SvRV (coro_current); |
1847 | AV *defav = GvAV (PL_defgv); |
1882 | AV *defav = GvAV (PL_defgv); |
1848 | SV *invoke = hv_delete (hv, strpair ("_invoke"), 0); |
1883 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
1849 | AV *invoke_av; |
1884 | AV *invoke_av; |
1850 | int i, len; |
1885 | int i, len; |
1851 | |
1886 | |
1852 | if (!invoke) |
1887 | if (!invoke) |
1853 | croak ("\3async_pool terminate\2\n"); |
1888 | croak ("\3async_pool terminate\2\n"); |
1854 | |
1889 | |
1855 | SvREFCNT_dec (coro->saved_deffh); |
1890 | SvREFCNT_dec (coro->saved_deffh); |
1856 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
1891 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
1857 | |
1892 | |
1858 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1893 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1859 | newSVpvn (strpair ("[async_pool]")), 0); |
1894 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
1860 | |
1895 | |
1861 | invoke_av = (AV *)SvRV (invoke); |
1896 | invoke_av = (AV *)SvRV (invoke); |
1862 | len = av_len (invoke_av); |
1897 | len = av_len (invoke_av); |
1863 | |
1898 | |
1864 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
1899 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
… | |
… | |
1887 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1922 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1888 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1923 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1889 | croak ("\3async_pool terminate\2\n"); |
1924 | croak ("\3async_pool terminate\2\n"); |
1890 | |
1925 | |
1891 | av_clear (GvAV (PL_defgv)); |
1926 | av_clear (GvAV (PL_defgv)); |
1892 | hv_store ((HV *)SvRV (coro_current), strpair ("desc"), |
1927 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1893 | newSVpvn (strpair ("[async_pool idle]")), 0); |
1928 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
1894 | |
1929 | |
1895 | coro->prio = 0; |
1930 | coro->prio = 0; |
1896 | |
1931 | |
1897 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
1932 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
1898 | api_trace (coro_current, 0); |
1933 | api_trace (coro_current, 0); |