… | |
… | |
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; |
… | |
… | |
146 | static size_t coro_stacksize = CORO_STACKSIZE; |
145 | static size_t coro_stacksize = CORO_STACKSIZE; |
147 | static struct CoroAPI coroapi; |
146 | static struct CoroAPI coroapi; |
148 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
147 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
149 | static JMPENV *main_top_env; |
148 | static JMPENV *main_top_env; |
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 volatile 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; |
… | |
… | |
261 | static struct coro *coro_first; |
259 | static struct coro *coro_first; |
262 | |
260 | |
263 | /** lowlevel stuff **********************************************************/ |
261 | /** lowlevel stuff **********************************************************/ |
264 | |
262 | |
265 | static SV * |
263 | static SV * |
266 | coro_get_sv (const char *name, int create) |
264 | coro_get_sv (pTHX_ const char *name, int create) |
267 | { |
265 | { |
268 | #if PERL_VERSION_ATLEAST (5,9,0) |
266 | #if PERL_VERSION_ATLEAST (5,9,0) |
269 | /* 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 */ |
270 | get_sv (name, create); |
268 | get_sv (name, create); |
271 | #endif |
269 | #endif |
272 | return get_sv (name, create); |
270 | return get_sv (name, create); |
273 | } |
271 | } |
274 | |
272 | |
275 | static AV * |
273 | static AV * |
276 | coro_get_av (const char *name, int create) |
274 | coro_get_av (pTHX_ const char *name, int create) |
277 | { |
275 | { |
278 | #if PERL_VERSION_ATLEAST (5,9,0) |
276 | #if PERL_VERSION_ATLEAST (5,9,0) |
279 | /* 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 */ |
280 | get_av (name, create); |
278 | get_av (name, create); |
281 | #endif |
279 | #endif |
282 | return get_av (name, create); |
280 | return get_av (name, create); |
283 | } |
281 | } |
284 | |
282 | |
285 | static HV * |
283 | static HV * |
286 | coro_get_hv (const char *name, int create) |
284 | coro_get_hv (pTHX_ const char *name, int create) |
287 | { |
285 | { |
288 | #if PERL_VERSION_ATLEAST (5,9,0) |
286 | #if PERL_VERSION_ATLEAST (5,9,0) |
289 | /* 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 */ |
290 | get_hv (name, create); |
288 | get_hv (name, create); |
291 | #endif |
289 | #endif |
… | |
… | |
346 | |
344 | |
347 | /* casting is fun. */ |
345 | /* casting is fun. */ |
348 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
346 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
349 | free_padlist (aTHX_ padlist); |
347 | free_padlist (aTHX_ padlist); |
350 | |
348 | |
351 | SvREFCNT_dec (av); |
|
|
352 | |
|
|
353 | return 0; |
349 | return 0; |
354 | } |
350 | } |
355 | |
351 | |
356 | #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 |
357 | |
354 | |
358 | 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 | }; |
359 | |
359 | |
360 | #define CORO_MAGIC(cv) \ |
360 | #define CORO_MAGIC(sv,type) \ |
361 | SvMAGIC (cv) \ |
361 | SvMAGIC (sv) \ |
362 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
362 | ? SvMAGIC (sv)->mg_type == type \ |
363 | ? SvMAGIC (cv) \ |
363 | ? SvMAGIC (sv) \ |
364 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
364 | : mg_find (sv, type) \ |
365 | : 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) |
366 | |
369 | |
367 | static struct coro * |
370 | static struct coro * |
368 | SvSTATE_ (pTHX_ SV *coro) |
371 | SvSTATE_ (pTHX_ SV *coro) |
369 | { |
372 | { |
370 | HV *stash; |
373 | HV *stash; |
… | |
… | |
382 | /* very slow, but rare, check */ |
385 | /* very slow, but rare, check */ |
383 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
386 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
384 | croak ("Coro::State object required"); |
387 | croak ("Coro::State object required"); |
385 | } |
388 | } |
386 | |
389 | |
387 | mg = CORO_MAGIC (coro); |
390 | mg = CORO_MAGIC_state (coro); |
388 | return (struct coro *)mg->mg_ptr; |
391 | return (struct coro *)mg->mg_ptr; |
389 | } |
392 | } |
390 | |
393 | |
391 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
394 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
392 | |
395 | |
393 | /* the next two functions merely cache the padlists */ |
396 | /* the next two functions merely cache the padlists */ |
394 | static void |
397 | static void |
395 | get_padlist (pTHX_ CV *cv) |
398 | get_padlist (pTHX_ CV *cv) |
396 | { |
399 | { |
397 | MAGIC *mg = CORO_MAGIC (cv); |
400 | MAGIC *mg = CORO_MAGIC_cv (cv); |
398 | AV *av; |
401 | AV *av; |
399 | |
402 | |
400 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
403 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
401 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
404 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
402 | else |
405 | else |
… | |
… | |
414 | } |
417 | } |
415 | |
418 | |
416 | static void |
419 | static void |
417 | put_padlist (pTHX_ CV *cv) |
420 | put_padlist (pTHX_ CV *cv) |
418 | { |
421 | { |
419 | MAGIC *mg = CORO_MAGIC (cv); |
422 | MAGIC *mg = CORO_MAGIC_cv (cv); |
420 | AV *av; |
423 | AV *av; |
421 | |
424 | |
422 | if (expect_false (!mg)) |
425 | if (expect_false (!mg)) |
423 | { |
426 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
424 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
|
|
425 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
|
|
426 | mg->mg_virtual = &vtbl_coro; |
|
|
427 | mg->mg_obj = (SV *)newAV (); |
|
|
428 | } |
|
|
429 | |
427 | |
430 | av = (AV *)mg->mg_obj; |
428 | av = (AV *)mg->mg_obj; |
431 | |
429 | |
432 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
430 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
433 | av_extend (av, AvMAX (av) + 1); |
431 | av_extend (av, AvMAX (av) + 1); |
… | |
… | |
451 | GvSV (irsgv) = slot->irsgv; |
449 | GvSV (irsgv) = slot->irsgv; |
452 | |
450 | |
453 | #define VAR(name,type) PL_ ## name = slot->name; |
451 | #define VAR(name,type) PL_ ## name = slot->name; |
454 | # include "state.h" |
452 | # include "state.h" |
455 | #undef VAR |
453 | #undef VAR |
456 | |
|
|
457 | /*hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0);*/ |
|
|
458 | /*hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0);*/ |
|
|
459 | |
454 | |
460 | { |
455 | { |
461 | dSP; |
456 | dSP; |
462 | |
457 | |
463 | CV *cv; |
458 | CV *cv; |
… | |
… | |
669 | return rss; |
664 | return rss; |
670 | } |
665 | } |
671 | |
666 | |
672 | /** coroutine stack handling ************************************************/ |
667 | /** coroutine stack handling ************************************************/ |
673 | |
668 | |
|
|
669 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
|
|
670 | static int (*orig_sigelem_set) (pTHX_ SV *sv, MAGIC *mg); |
|
|
671 | |
|
|
672 | /* |
|
|
673 | * This overrides the default magic get method of %SIG elements. |
|
|
674 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
|
|
675 | * and instead of tryign to save and restore the hash elements, we just provide |
|
|
676 | * readback here. |
|
|
677 | * We only do this when the hook is != 0, as they are often set to 0 temporarily, |
|
|
678 | * not expecting this to actually change the hook. This is a potential problem |
|
|
679 | * when a schedule happens then, but we ignore this. |
|
|
680 | */ |
|
|
681 | static int |
|
|
682 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
|
|
683 | { |
|
|
684 | const char *s = MgPV_nolen_const (mg); |
|
|
685 | |
|
|
686 | if (*s == '_') |
|
|
687 | { |
|
|
688 | if (strEQ (s, "__DIE__" ) && PL_diehook ) return sv_setsv (sv, PL_diehook ), 0; |
|
|
689 | if (strEQ (s, "__WARN__") && PL_warnhook) return sv_setsv (sv, PL_warnhook), 0; |
|
|
690 | } |
|
|
691 | |
|
|
692 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
|
|
693 | } |
|
|
694 | |
|
|
695 | static int |
|
|
696 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
|
|
697 | { |
|
|
698 | const char *s = MgPV_nolen_const (mg); |
|
|
699 | |
|
|
700 | if (*s == '_') |
|
|
701 | { |
|
|
702 | SV **svp = 0; |
|
|
703 | |
|
|
704 | if (strEQ (s, "__DIE__" )) svp = &PL_diehook; |
|
|
705 | if (strEQ (s, "__WARN__")) svp = &PL_warnhook; |
|
|
706 | |
|
|
707 | if (svp) |
|
|
708 | { |
|
|
709 | SV *old = *svp; |
|
|
710 | *svp = newSVsv (sv); |
|
|
711 | SvREFCNT_dec (old); |
|
|
712 | return; |
|
|
713 | } |
|
|
714 | } |
|
|
715 | |
|
|
716 | return orig_sigelem_set ? orig_sigelem_set (aTHX_ sv, mg) : 0; |
|
|
717 | } |
|
|
718 | |
674 | static void |
719 | static void |
675 | coro_setup (pTHX_ struct coro *coro) |
720 | coro_setup (pTHX_ struct coro *coro) |
676 | { |
721 | { |
677 | /* |
722 | /* |
678 | * emulate part of the perl startup here. |
723 | * emulate part of the perl startup here. |
… | |
… | |
686 | PL_curpm = 0; |
731 | PL_curpm = 0; |
687 | PL_curpad = 0; |
732 | PL_curpad = 0; |
688 | PL_localizing = 0; |
733 | PL_localizing = 0; |
689 | PL_dirty = 0; |
734 | PL_dirty = 0; |
690 | PL_restartop = 0; |
735 | PL_restartop = 0; |
691 | PL_diehook = 0; hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0); |
736 | |
692 | PL_warnhook = 0; hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0); |
737 | /* recreate the die/warn hooks */ |
|
|
738 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
|
|
739 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
693 | |
740 | |
694 | GvSV (PL_defgv) = newSV (0); |
741 | GvSV (PL_defgv) = newSV (0); |
695 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
742 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
696 | GvSV (PL_errgv) = newSV (0); |
743 | GvSV (PL_errgv) = newSV (0); |
697 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
744 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
… | |
… | |
712 | PL_op = (OP *)&myop; |
759 | PL_op = (OP *)&myop; |
713 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
760 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
714 | SPAGAIN; |
761 | SPAGAIN; |
715 | } |
762 | } |
716 | |
763 | |
717 | ENTER; /* necessary e.g. for dounwind and to balance the xsub-entersub */ |
764 | /* this newly created coroutine might be run on an existing cctx which most |
|
|
765 | * likely was suspended in set_stacklevel, called from entersub. |
|
|
766 | * set_stacklevl doesn't do anything on return, but entersub does LEAVE, |
|
|
767 | * so we ENTER here for symmetry |
|
|
768 | */ |
|
|
769 | ENTER; |
718 | } |
770 | } |
719 | |
771 | |
720 | static void |
772 | static void |
721 | coro_destroy (pTHX_ struct coro *coro) |
773 | coro_destroy (pTHX_ struct coro *coro) |
722 | { |
774 | { |
… | |
… | |
806 | PUSHMARK (SP); |
858 | PUSHMARK (SP); |
807 | PUSHs (&PL_sv_no); |
859 | PUSHs (&PL_sv_no); |
808 | PUSHs (fullname); |
860 | PUSHs (fullname); |
809 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
861 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
810 | PUTBACK; |
862 | PUTBACK; |
811 | cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0); |
863 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
812 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
864 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
813 | SPAGAIN; |
865 | SPAGAIN; |
814 | FREETMPS; |
866 | FREETMPS; |
815 | LEAVE; |
867 | LEAVE; |
816 | PL_runops = runops_trace; |
868 | PL_runops = runops_trace; |
… | |
… | |
845 | PUSHMARK (SP); |
897 | PUSHMARK (SP); |
846 | PUSHs (&PL_sv_yes); |
898 | PUSHs (&PL_sv_yes); |
847 | PUSHs (fullname); |
899 | PUSHs (fullname); |
848 | PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
900 | PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
849 | PUTBACK; |
901 | PUTBACK; |
850 | cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0); |
902 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
851 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
903 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
852 | SPAGAIN; |
904 | SPAGAIN; |
853 | FREETMPS; |
905 | FREETMPS; |
854 | LEAVE; |
906 | LEAVE; |
855 | PL_runops = runops_trace; |
907 | PL_runops = runops_trace; |
… | |
… | |
869 | PL_runops = RUNOPS_DEFAULT; |
921 | PL_runops = RUNOPS_DEFAULT; |
870 | PUSHMARK (SP); |
922 | PUSHMARK (SP); |
871 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
923 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
872 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
924 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
873 | PUTBACK; |
925 | PUTBACK; |
874 | cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_line_cb"), 0); |
926 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0); |
875 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
927 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
876 | SPAGAIN; |
928 | SPAGAIN; |
877 | FREETMPS; |
929 | FREETMPS; |
878 | LEAVE; |
930 | LEAVE; |
879 | PL_runops = runops_trace; |
931 | PL_runops = runops_trace; |
… | |
… | |
1085 | /* always use the TRANSFER macro */ |
1137 | /* always use the TRANSFER macro */ |
1086 | static void NOINLINE |
1138 | static void NOINLINE |
1087 | transfer (pTHX_ struct coro *prev, struct coro *next) |
1139 | transfer (pTHX_ struct coro *prev, struct coro *next) |
1088 | { |
1140 | { |
1089 | dSTACKLEVEL; |
1141 | dSTACKLEVEL; |
|
|
1142 | static volatile int has_throw; |
1090 | |
1143 | |
1091 | /* sometimes transfer is only called to set idle_sp */ |
1144 | /* sometimes transfer is only called to set idle_sp */ |
1092 | if (expect_false (!next)) |
1145 | if (expect_false (!next)) |
1093 | { |
1146 | { |
1094 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1147 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
… | |
… | |
1109 | prev->flags &= ~CF_RUNNING; |
1162 | prev->flags &= ~CF_RUNNING; |
1110 | next->flags |= CF_RUNNING; |
1163 | next->flags |= CF_RUNNING; |
1111 | |
1164 | |
1112 | LOCK; |
1165 | LOCK; |
1113 | |
1166 | |
|
|
1167 | /* first get rid of the old state */ |
|
|
1168 | save_perl (aTHX_ prev); |
|
|
1169 | |
1114 | if (expect_false (next->flags & CF_NEW)) |
1170 | if (expect_false (next->flags & CF_NEW)) |
1115 | { |
1171 | { |
1116 | /* need to start coroutine */ |
1172 | /* need to start coroutine */ |
1117 | next->flags &= ~CF_NEW; |
1173 | next->flags &= ~CF_NEW; |
1118 | /* first get rid of the old state */ |
|
|
1119 | save_perl (aTHX_ prev); |
|
|
1120 | /* setup coroutine call */ |
1174 | /* setup coroutine call */ |
1121 | coro_setup (aTHX_ next); |
1175 | coro_setup (aTHX_ next); |
1122 | } |
1176 | } |
1123 | else |
1177 | else |
1124 | { |
|
|
1125 | /* coroutine already started */ |
|
|
1126 | save_perl (aTHX_ prev); |
|
|
1127 | load_perl (aTHX_ next); |
1178 | load_perl (aTHX_ next); |
1128 | } |
|
|
1129 | |
1179 | |
1130 | prev__cctx = prev->cctx; |
1180 | prev__cctx = prev->cctx; |
1131 | |
1181 | |
1132 | /* possibly "free" the cctx */ |
1182 | /* possibly "free" the cctx */ |
1133 | if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))) |
1183 | if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))) |
… | |
… | |
1148 | |
1198 | |
1149 | ++next->usecount; |
1199 | ++next->usecount; |
1150 | |
1200 | |
1151 | if (expect_true (!next->cctx)) |
1201 | if (expect_true (!next->cctx)) |
1152 | next->cctx = cctx_get (aTHX); |
1202 | next->cctx = cctx_get (aTHX); |
|
|
1203 | |
|
|
1204 | has_throw = !!next->throw; |
1153 | |
1205 | |
1154 | if (expect_false (prev__cctx != next->cctx)) |
1206 | if (expect_false (prev__cctx != next->cctx)) |
1155 | { |
1207 | { |
1156 | prev__cctx->top_env = PL_top_env; |
1208 | prev__cctx->top_env = PL_top_env; |
1157 | PL_top_env = next->cctx->top_env; |
1209 | PL_top_env = next->cctx->top_env; |
… | |
… | |
1159 | } |
1211 | } |
1160 | |
1212 | |
1161 | free_coro_mortal (aTHX); |
1213 | free_coro_mortal (aTHX); |
1162 | UNLOCK; |
1214 | UNLOCK; |
1163 | |
1215 | |
1164 | if (expect_false (prev->throw || next->throw)) |
1216 | if (expect_false (has_throw)) |
1165 | { |
1217 | { |
1166 | struct coro *coro = SvSTATE (coro_current); |
1218 | struct coro *coro = SvSTATE (coro_current); |
1167 | |
1219 | |
1168 | if (coro->throw) |
1220 | if (coro->throw) |
1169 | { |
1221 | { |
… | |
… | |
1295 | { |
1347 | { |
1296 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
1348 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
1297 | } |
1349 | } |
1298 | |
1350 | |
1299 | static SV * |
1351 | static SV * |
1300 | coro_deq (pTHX_ int min_prio) |
1352 | coro_deq (pTHX) |
1301 | { |
1353 | { |
1302 | int prio = PRIO_MAX - PRIO_MIN; |
1354 | int prio; |
1303 | |
1355 | |
1304 | min_prio -= PRIO_MIN; |
|
|
1305 | if (min_prio < 0) |
|
|
1306 | min_prio = 0; |
|
|
1307 | |
|
|
1308 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
1356 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1309 | if (AvFILLp (coro_ready [prio]) >= 0) |
1357 | if (AvFILLp (coro_ready [prio]) >= 0) |
1310 | return av_shift (coro_ready [prio]); |
1358 | return av_shift (coro_ready [prio]); |
1311 | |
1359 | |
1312 | return 0; |
1360 | return 0; |
1313 | } |
1361 | } |
… | |
… | |
1349 | SV *prev_sv, *next_sv; |
1397 | SV *prev_sv, *next_sv; |
1350 | |
1398 | |
1351 | for (;;) |
1399 | for (;;) |
1352 | { |
1400 | { |
1353 | LOCK; |
1401 | LOCK; |
1354 | next_sv = coro_deq (aTHX_ PRIO_MIN); |
1402 | next_sv = coro_deq (aTHX); |
1355 | |
1403 | |
1356 | /* nothing to schedule: call the idle handler */ |
1404 | /* nothing to schedule: call the idle handler */ |
1357 | if (expect_false (!next_sv)) |
1405 | if (expect_false (!next_sv)) |
1358 | { |
1406 | { |
1359 | dSP; |
1407 | dSP; |
… | |
… | |
1363 | SAVETMPS; |
1411 | SAVETMPS; |
1364 | |
1412 | |
1365 | PUSHMARK (SP); |
1413 | PUSHMARK (SP); |
1366 | PUTBACK; |
1414 | PUTBACK; |
1367 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
1415 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
|
|
1416 | SPAGAIN; |
1368 | |
1417 | |
1369 | FREETMPS; |
1418 | FREETMPS; |
1370 | LEAVE; |
1419 | LEAVE; |
1371 | continue; |
1420 | continue; |
1372 | } |
1421 | } |
… | |
… | |
1502 | BOOT_PAGESIZE; |
1551 | BOOT_PAGESIZE; |
1503 | |
1552 | |
1504 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1553 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1505 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1554 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1506 | |
1555 | |
|
|
1556 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; |
|
|
1557 | PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
|
|
1558 | orig_sigelem_set = PL_vtbl_sigelem.svt_set; |
|
|
1559 | PL_vtbl_sigelem.svt_set = coro_sigelem_set; |
|
|
1560 | |
1507 | hv_sig = coro_get_hv ("SIG", TRUE); |
1561 | hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); |
1508 | sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE); |
1562 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
1509 | sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE); |
1563 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
1510 | |
|
|
1511 | if (!PL_diehook ) hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0); |
|
|
1512 | if (!PL_warnhook) hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0); |
|
|
1513 | |
1564 | |
1514 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1565 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1515 | |
1566 | |
1516 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1567 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1517 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
1568 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
… | |
… | |
1523 | |
1574 | |
1524 | while (main_top_env->je_prev) |
1575 | while (main_top_env->je_prev) |
1525 | main_top_env = main_top_env->je_prev; |
1576 | main_top_env = main_top_env->je_prev; |
1526 | |
1577 | |
1527 | coroapi.ver = CORO_API_VERSION; |
1578 | coroapi.ver = CORO_API_VERSION; |
|
|
1579 | coroapi.rev = CORO_API_REVISION; |
1528 | coroapi.transfer = api_transfer; |
1580 | coroapi.transfer = api_transfer; |
1529 | |
1581 | |
1530 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1582 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1531 | } |
1583 | } |
1532 | |
1584 | |
1533 | SV * |
1585 | SV * |
1534 | new (char *klass, ...) |
1586 | new (char *klass, ...) |
1535 | CODE: |
1587 | CODE: |
1536 | { |
1588 | { |
1537 | struct coro *coro; |
1589 | struct coro *coro; |
|
|
1590 | MAGIC *mg; |
1538 | HV *hv; |
1591 | HV *hv; |
1539 | int i; |
1592 | int i; |
1540 | |
1593 | |
1541 | Newz (0, coro, 1, struct coro); |
1594 | Newz (0, coro, 1, struct coro); |
1542 | coro->args = newAV (); |
1595 | coro->args = newAV (); |
… | |
… | |
1545 | if (coro_first) coro_first->prev = coro; |
1598 | if (coro_first) coro_first->prev = coro; |
1546 | coro->next = coro_first; |
1599 | coro->next = coro_first; |
1547 | coro_first = coro; |
1600 | coro_first = coro; |
1548 | |
1601 | |
1549 | coro->hv = hv = newHV (); |
1602 | coro->hv = hv = newHV (); |
1550 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1603 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
|
|
1604 | mg->mg_flags |= MGf_DUP; |
1551 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1605 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1552 | |
1606 | |
1553 | av_extend (coro->args, items - 1); |
1607 | av_extend (coro->args, items - 1); |
1554 | for (i = 1; i < items; i++) |
1608 | for (i = 1; i < items; i++) |
1555 | av_push (coro->args, newSVsv (ST (i))); |
1609 | av_push (coro->args, newSVsv (ST (i))); |
… | |
… | |
1561 | # function to increase chances that they all will call transfer with the same |
1615 | # function to increase chances that they all will call transfer with the same |
1562 | # stack offset |
1616 | # stack offset |
1563 | void |
1617 | void |
1564 | _set_stacklevel (...) |
1618 | _set_stacklevel (...) |
1565 | ALIAS: |
1619 | ALIAS: |
1566 | Coro::State::transfer = 1 |
1620 | Coro::State::transfer = 1 |
1567 | Coro::schedule = 2 |
1621 | Coro::schedule = 2 |
1568 | Coro::cede = 3 |
1622 | Coro::cede = 3 |
1569 | Coro::cede_notself = 4 |
1623 | Coro::cede_notself = 4 |
1570 | Coro::Event::next = 5 |
|
|
1571 | Coro::Event::next_cancel = 6 |
|
|
1572 | PPCODE: |
1624 | CODE: |
1573 | { |
1625 | { |
1574 | struct transfer_args ta; |
1626 | struct transfer_args ta; |
1575 | int again = 0; |
|
|
1576 | |
1627 | |
1577 | do |
1628 | PUTBACK; |
|
|
1629 | switch (ix) |
1578 | { |
1630 | { |
1579 | switch (ix) |
|
|
1580 | { |
|
|
1581 | case 0: |
1631 | case 0: |
1582 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1632 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1583 | ta.next = 0; |
1633 | ta.next = 0; |
1584 | break; |
1634 | break; |
1585 | |
1635 | |
1586 | case 1: |
1636 | case 1: |
1587 | if (items != 2) |
1637 | if (items != 2) |
1588 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1638 | croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); |
1589 | |
1639 | |
1590 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1640 | prepare_transfer (aTHX_ &ta, ST (0), ST (1)); |
1591 | break; |
1641 | break; |
1592 | |
1642 | |
1593 | case 2: |
1643 | case 2: |
1594 | prepare_schedule (aTHX_ &ta); |
1644 | prepare_schedule (aTHX_ &ta); |
1595 | break; |
1645 | break; |
1596 | |
1646 | |
1597 | case 3: |
1647 | case 3: |
1598 | prepare_cede (aTHX_ &ta); |
1648 | prepare_cede (aTHX_ &ta); |
1599 | break; |
1649 | break; |
1600 | |
1650 | |
1601 | case 4: |
1651 | case 4: |
1602 | if (!prepare_cede_notself (aTHX_ &ta)) |
1652 | if (!prepare_cede_notself (aTHX_ &ta)) |
1603 | XSRETURN_EMPTY; |
1653 | XSRETURN_EMPTY; |
1604 | |
1654 | |
1605 | break; |
1655 | break; |
1606 | |
|
|
1607 | case 5: |
|
|
1608 | case 6: |
|
|
1609 | if (items != 1) |
|
|
1610 | croak ("Coro::Event::next (watcher) expects one argument, not %d", items); |
|
|
1611 | |
|
|
1612 | { |
|
|
1613 | SV *ev = coroapi.coro_event_next (ST (0), ix == 6, GIMME_V != G_VOID); |
|
|
1614 | |
|
|
1615 | if (ev) |
|
|
1616 | { |
|
|
1617 | if (GIMME_V != G_VOID) |
|
|
1618 | { |
|
|
1619 | XPUSHs (ev); |
|
|
1620 | XSRETURN (1); |
|
|
1621 | } |
|
|
1622 | else |
|
|
1623 | XSRETURN_EMPTY; |
|
|
1624 | } |
|
|
1625 | } |
|
|
1626 | |
|
|
1627 | prepare_schedule (aTHX_ &ta); |
|
|
1628 | again = 1; |
|
|
1629 | break; |
|
|
1630 | } |
|
|
1631 | |
|
|
1632 | BARRIER; |
|
|
1633 | TRANSFER (ta); |
|
|
1634 | BARRIER; |
|
|
1635 | } |
1656 | } |
1636 | while (again); |
1657 | SPAGAIN; |
1637 | |
1658 | |
1638 | if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev)) |
1659 | BARRIER; |
1639 | XSRETURN_YES; |
1660 | PUTBACK; |
1640 | |
1661 | TRANSFER (ta); |
1641 | XSRETURN_EMPTY; /* not understood why this is necessary, likely some stack handling bug */ |
1662 | SPAGAIN; /* might be the sp of a different coroutine now */ |
|
|
1663 | /* be extra careful not to ever do anything after TRANSFER */ |
|
|
1664 | } |
1642 | |
1665 | |
1643 | bool |
1666 | bool |
1644 | _destroy (SV *coro_sv) |
1667 | _destroy (SV *coro_sv) |
1645 | CODE: |
1668 | CODE: |
1646 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
1669 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
… | |
… | |
1697 | { |
1720 | { |
1698 | struct coro temp; |
1721 | struct coro temp; |
1699 | |
1722 | |
1700 | if (!(coro->flags & CF_RUNNING)) |
1723 | if (!(coro->flags & CF_RUNNING)) |
1701 | { |
1724 | { |
|
|
1725 | PUTBACK; |
1702 | save_perl (aTHX_ &temp); |
1726 | save_perl (aTHX_ &temp); |
1703 | load_perl (aTHX_ coro); |
1727 | load_perl (aTHX_ coro); |
1704 | } |
1728 | } |
1705 | |
1729 | |
1706 | { |
1730 | { |
… | |
… | |
1715 | eval_sv (coderef, 0); |
1739 | eval_sv (coderef, 0); |
1716 | else |
1740 | else |
1717 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1741 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1718 | |
1742 | |
1719 | POPSTACK; |
1743 | POPSTACK; |
|
|
1744 | SPAGAIN; |
1720 | FREETMPS; |
1745 | FREETMPS; |
1721 | LEAVE; |
1746 | LEAVE; |
|
|
1747 | PUTBACK; |
1722 | } |
1748 | } |
1723 | |
1749 | |
1724 | if (!(coro->flags & CF_RUNNING)) |
1750 | if (!(coro->flags & CF_RUNNING)) |
1725 | { |
1751 | { |
1726 | save_perl (aTHX_ coro); |
1752 | save_perl (aTHX_ coro); |
1727 | load_perl (aTHX_ &temp); |
1753 | load_perl (aTHX_ &temp); |
|
|
1754 | SPAGAIN; |
1728 | } |
1755 | } |
1729 | } |
1756 | } |
1730 | } |
1757 | } |
1731 | |
1758 | |
1732 | SV * |
1759 | SV * |
… | |
… | |
1780 | |
1807 | |
1781 | BOOT: |
1808 | BOOT: |
1782 | { |
1809 | { |
1783 | int i; |
1810 | int i; |
1784 | |
1811 | |
1785 | sv_pool_rss = coro_get_sv ("Coro::POOL_RSS" , TRUE); |
1812 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
1786 | sv_pool_size = coro_get_sv ("Coro::POOL_SIZE" , TRUE); |
1813 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
1787 | av_async_pool = coro_get_av ("Coro::async_pool", TRUE); |
1814 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
1788 | |
1815 | |
1789 | coro_current = coro_get_sv ("Coro::current", FALSE); |
1816 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
1790 | SvREADONLY_on (coro_current); |
1817 | SvREADONLY_on (coro_current); |
1791 | |
1818 | |
1792 | coro_stash = gv_stashpv ("Coro", TRUE); |
1819 | coro_stash = gv_stashpv ("Coro", TRUE); |
1793 | |
1820 | |
1794 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1821 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
… | |
… | |
1877 | CODE: |
1904 | CODE: |
1878 | { |
1905 | { |
1879 | struct coro *coro = SvSTATE (coro_current); |
1906 | struct coro *coro = SvSTATE (coro_current); |
1880 | HV *hv = (HV *)SvRV (coro_current); |
1907 | HV *hv = (HV *)SvRV (coro_current); |
1881 | AV *defav = GvAV (PL_defgv); |
1908 | AV *defav = GvAV (PL_defgv); |
1882 | SV *invoke = hv_delete (hv, strpair ("_invoke"), 0); |
1909 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
1883 | AV *invoke_av; |
1910 | AV *invoke_av; |
1884 | int i, len; |
1911 | int i, len; |
1885 | |
1912 | |
1886 | if (!invoke) |
1913 | if (!invoke) |
1887 | croak ("\3async_pool terminate\2\n"); |
1914 | croak ("\3async_pool terminate\2\n"); |
1888 | |
1915 | |
1889 | SvREFCNT_dec (coro->saved_deffh); |
1916 | SvREFCNT_dec (coro->saved_deffh); |
1890 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
1917 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
1891 | |
1918 | |
1892 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1919 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1893 | newSVpvn (strpair ("[async_pool]")), 0); |
1920 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
1894 | |
1921 | |
1895 | invoke_av = (AV *)SvRV (invoke); |
1922 | invoke_av = (AV *)SvRV (invoke); |
1896 | len = av_len (invoke_av); |
1923 | len = av_len (invoke_av); |
1897 | |
1924 | |
1898 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
1925 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
… | |
… | |
1921 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1948 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1922 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1949 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1923 | croak ("\3async_pool terminate\2\n"); |
1950 | croak ("\3async_pool terminate\2\n"); |
1924 | |
1951 | |
1925 | av_clear (GvAV (PL_defgv)); |
1952 | av_clear (GvAV (PL_defgv)); |
1926 | hv_store ((HV *)SvRV (coro_current), strpair ("desc"), |
1953 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1927 | newSVpvn (strpair ("[async_pool idle]")), 0); |
1954 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
1928 | |
1955 | |
1929 | coro->prio = 0; |
1956 | coro->prio = 0; |
1930 | |
1957 | |
1931 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
1958 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
1932 | api_trace (coro_current, 0); |
1959 | api_trace (coro_current, 0); |