… | |
… | |
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 |
… | |
… | |
144 | static size_t coro_stacksize = CORO_STACKSIZE; |
145 | static size_t coro_stacksize = CORO_STACKSIZE; |
145 | static struct CoroAPI coroapi; |
146 | static struct CoroAPI coroapi; |
146 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
147 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
147 | static JMPENV *main_top_env; |
148 | static JMPENV *main_top_env; |
148 | static HV *coro_state_stash, *coro_stash; |
149 | static HV *coro_state_stash, *coro_stash; |
149 | static SV *coro_mortal; /* will be freed after next transfer */ |
150 | static volatile SV *coro_mortal; /* will be freed after next transfer */ |
150 | |
151 | |
151 | static GV *irsgv; /* $/ */ |
152 | static GV *irsgv; /* $/ */ |
152 | static GV *stdoutgv; /* *STDOUT */ |
153 | static GV *stdoutgv; /* *STDOUT */ |
153 | static SV *sv_diehook; |
154 | static SV *rv_diehook; |
154 | static SV *sv_warnhook; |
155 | static SV *rv_warnhook; |
|
|
156 | static HV *hv_sig; /* %SIG */ |
155 | |
157 | |
156 | /* async_pool helper stuff */ |
158 | /* async_pool helper stuff */ |
157 | static SV *sv_pool_rss; |
159 | static SV *sv_pool_rss; |
158 | static SV *sv_pool_size; |
160 | static SV *sv_pool_size; |
159 | static AV *av_async_pool; |
161 | static AV *av_async_pool; |
… | |
… | |
217 | |
219 | |
218 | /* process data */ |
220 | /* process data */ |
219 | AV *mainstack; |
221 | AV *mainstack; |
220 | perl_slots *slot; /* basically the saved sp */ |
222 | perl_slots *slot; /* basically the saved sp */ |
221 | |
223 | |
222 | /* data associated with this coroutine (initial args) */ |
224 | AV *args; /* data associated with this coroutine (initial args) */ |
223 | AV *args; |
225 | int refcnt; /* coroutines are refcounted, yes */ |
224 | int refcnt; |
|
|
225 | int flags; /* CF_ flags */ |
226 | int flags; /* CF_ flags */ |
|
|
227 | HV *hv; /* the perl hash associated with this coro, if any */ |
226 | |
228 | |
227 | /* statistics */ |
229 | /* statistics */ |
228 | int usecount; /* number of transfers to this coro */ |
230 | int usecount; /* number of transfers to this coro */ |
229 | |
231 | |
230 | /* coro process data */ |
232 | /* coro process data */ |
231 | int prio; |
233 | int prio; |
232 | SV *throw; |
234 | SV *throw; /* exception to be thrown */ |
233 | |
235 | |
234 | /* async_pool */ |
236 | /* async_pool */ |
235 | SV *saved_deffh; |
237 | SV *saved_deffh; |
236 | |
238 | |
237 | /* linked list */ |
239 | /* linked list */ |
238 | struct coro *next, *prev; |
240 | struct coro *next, *prev; |
239 | HV *hv; /* the perl hash associated with this coro, if any */ |
|
|
240 | }; |
241 | }; |
241 | |
242 | |
242 | typedef struct coro *Coro__State; |
243 | typedef struct coro *Coro__State; |
243 | typedef struct coro *Coro__State_or_hashref; |
244 | typedef struct coro *Coro__State_or_hashref; |
244 | |
245 | |
… | |
… | |
258 | static struct coro *coro_first; |
259 | static struct coro *coro_first; |
259 | |
260 | |
260 | /** lowlevel stuff **********************************************************/ |
261 | /** lowlevel stuff **********************************************************/ |
261 | |
262 | |
262 | static SV * |
263 | static SV * |
263 | coro_get_sv (const char *name, int create) |
264 | coro_get_sv (pTHX_ const char *name, int create) |
264 | { |
265 | { |
265 | #if PERL_VERSION_ATLEAST (5,9,0) |
266 | #if PERL_VERSION_ATLEAST (5,9,0) |
266 | /* 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 */ |
267 | get_sv (name, create); |
268 | get_sv (name, create); |
268 | #endif |
269 | #endif |
269 | return get_sv (name, create); |
270 | return get_sv (name, create); |
270 | } |
271 | } |
271 | |
272 | |
272 | static SV * |
273 | static AV * |
273 | coro_get_av (const char *name, int create) |
274 | coro_get_av (pTHX_ const char *name, int create) |
274 | { |
275 | { |
275 | #if PERL_VERSION_ATLEAST (5,9,0) |
276 | #if PERL_VERSION_ATLEAST (5,9,0) |
276 | /* 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 */ |
277 | get_av (name, create); |
278 | get_av (name, create); |
278 | #endif |
279 | #endif |
279 | return get_av (name, create); |
280 | return get_av (name, create); |
|
|
281 | } |
|
|
282 | |
|
|
283 | static HV * |
|
|
284 | coro_get_hv (pTHX_ const char *name, int create) |
|
|
285 | { |
|
|
286 | #if PERL_VERSION_ATLEAST (5,9,0) |
|
|
287 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
|
|
288 | get_hv (name, create); |
|
|
289 | #endif |
|
|
290 | return get_hv (name, create); |
280 | } |
291 | } |
281 | |
292 | |
282 | static AV * |
293 | static AV * |
283 | coro_clone_padlist (pTHX_ CV *cv) |
294 | coro_clone_padlist (pTHX_ CV *cv) |
284 | { |
295 | { |
… | |
… | |
333 | |
344 | |
334 | /* casting is fun. */ |
345 | /* casting is fun. */ |
335 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
346 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
336 | free_padlist (aTHX_ padlist); |
347 | free_padlist (aTHX_ padlist); |
337 | |
348 | |
338 | SvREFCNT_dec (av); |
|
|
339 | |
|
|
340 | return 0; |
349 | return 0; |
341 | } |
350 | } |
342 | |
351 | |
343 | #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 |
344 | |
354 | |
345 | 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 | }; |
346 | |
359 | |
347 | #define CORO_MAGIC(cv) \ |
360 | #define CORO_MAGIC(sv,type) \ |
348 | SvMAGIC (cv) \ |
361 | SvMAGIC (sv) \ |
349 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
362 | ? SvMAGIC (sv)->mg_type == type \ |
350 | ? SvMAGIC (cv) \ |
363 | ? SvMAGIC (sv) \ |
351 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
364 | : mg_find (sv, type) \ |
352 | : 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) |
353 | |
369 | |
354 | static struct coro * |
370 | static struct coro * |
355 | SvSTATE_ (pTHX_ SV *coro) |
371 | SvSTATE_ (pTHX_ SV *coro) |
356 | { |
372 | { |
357 | HV *stash; |
373 | HV *stash; |
… | |
… | |
369 | /* very slow, but rare, check */ |
385 | /* very slow, but rare, check */ |
370 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
386 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
371 | croak ("Coro::State object required"); |
387 | croak ("Coro::State object required"); |
372 | } |
388 | } |
373 | |
389 | |
374 | mg = CORO_MAGIC (coro); |
390 | mg = CORO_MAGIC_state (coro); |
375 | return (struct coro *)mg->mg_ptr; |
391 | return (struct coro *)mg->mg_ptr; |
376 | } |
392 | } |
377 | |
393 | |
378 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
394 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
379 | |
395 | |
380 | /* the next two functions merely cache the padlists */ |
396 | /* the next two functions merely cache the padlists */ |
381 | static void |
397 | static void |
382 | get_padlist (pTHX_ CV *cv) |
398 | get_padlist (pTHX_ CV *cv) |
383 | { |
399 | { |
384 | MAGIC *mg = CORO_MAGIC (cv); |
400 | MAGIC *mg = CORO_MAGIC_cv (cv); |
385 | AV *av; |
401 | AV *av; |
386 | |
402 | |
387 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
403 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
388 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
404 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
389 | else |
405 | else |
… | |
… | |
401 | } |
417 | } |
402 | |
418 | |
403 | static void |
419 | static void |
404 | put_padlist (pTHX_ CV *cv) |
420 | put_padlist (pTHX_ CV *cv) |
405 | { |
421 | { |
406 | MAGIC *mg = CORO_MAGIC (cv); |
422 | MAGIC *mg = CORO_MAGIC_cv (cv); |
407 | AV *av; |
423 | AV *av; |
408 | |
424 | |
409 | if (expect_false (!mg)) |
425 | if (expect_false (!mg)) |
410 | { |
426 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
411 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
|
|
412 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
|
|
413 | mg->mg_virtual = &vtbl_coro; |
|
|
414 | mg->mg_obj = (SV *)newAV (); |
|
|
415 | } |
|
|
416 | |
427 | |
417 | av = (AV *)mg->mg_obj; |
428 | av = (AV *)mg->mg_obj; |
418 | |
429 | |
419 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
430 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
420 | av_extend (av, AvMAX (av) + 1); |
431 | av_extend (av, AvMAX (av) + 1); |
… | |
… | |
507 | |
518 | |
508 | PUTBACK; |
519 | PUTBACK; |
509 | } |
520 | } |
510 | |
521 | |
511 | /* 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; |
512 | { |
527 | { |
513 | /* we manually unroll here, as usually 2 slots is enough */ |
|
|
514 | int i; |
528 | int i; |
515 | if (SLOT_COUNT >= 1) CXINC; |
|
|
516 | if (SLOT_COUNT >= 2) CXINC; |
|
|
517 | if (SLOT_COUNT >= 3) CXINC; |
|
|
518 | for (i = 3; i < SLOT_COUNT; ++i) |
529 | for (i = 3; i < SLOT_COUNT; ++i) |
519 | CXINC; |
530 | CXINC; |
520 | |
|
|
521 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
|
|
522 | } |
531 | } |
|
|
532 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
523 | |
533 | |
524 | c->mainstack = PL_mainstack; |
534 | c->mainstack = PL_mainstack; |
525 | |
535 | |
526 | { |
536 | { |
527 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
537 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
… | |
… | |
654 | return rss; |
664 | return rss; |
655 | } |
665 | } |
656 | |
666 | |
657 | /** coroutine stack handling ************************************************/ |
667 | /** coroutine stack handling ************************************************/ |
658 | |
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 | |
659 | static void |
719 | static void |
660 | coro_setup (pTHX_ struct coro *coro) |
720 | coro_setup (pTHX_ struct coro *coro) |
661 | { |
721 | { |
662 | /* |
722 | /* |
663 | * emulate part of the perl startup here. |
723 | * emulate part of the perl startup here. |
… | |
… | |
671 | PL_curpm = 0; |
731 | PL_curpm = 0; |
672 | PL_curpad = 0; |
732 | PL_curpad = 0; |
673 | PL_localizing = 0; |
733 | PL_localizing = 0; |
674 | PL_dirty = 0; |
734 | PL_dirty = 0; |
675 | PL_restartop = 0; |
735 | PL_restartop = 0; |
676 | PL_diehook = SvREFCNT_inc (sv_diehook); |
736 | |
677 | PL_warnhook = SvREFCNT_inc (sv_warnhook); |
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); |
678 | |
740 | |
679 | GvSV (PL_defgv) = newSV (0); |
741 | GvSV (PL_defgv) = newSV (0); |
680 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
742 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
681 | GvSV (PL_errgv) = newSV (0); |
743 | GvSV (PL_errgv) = newSV (0); |
682 | 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); |
… | |
… | |
697 | PL_op = (OP *)&myop; |
759 | PL_op = (OP *)&myop; |
698 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
760 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
699 | SPAGAIN; |
761 | SPAGAIN; |
700 | } |
762 | } |
701 | |
763 | |
702 | 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; |
703 | } |
770 | } |
704 | |
771 | |
705 | static void |
772 | static void |
706 | coro_destroy (pTHX_ struct coro *coro) |
773 | coro_destroy (pTHX_ struct coro *coro) |
707 | { |
774 | { |
… | |
… | |
874 | |
941 | |
875 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
942 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
876 | /* _cctx_init should be careful, as it could be called at almost any time */ |
943 | /* _cctx_init should be careful, as it could be called at almost any time */ |
877 | /* during execution of a perl program */ |
944 | /* during execution of a perl program */ |
878 | static void NOINLINE |
945 | static void NOINLINE |
879 | prepare_cctx (pTHX_ coro_cctx *cctx) |
946 | cctx_prepare (pTHX_ coro_cctx *cctx) |
880 | { |
947 | { |
881 | dSP; |
948 | dSP; |
882 | LOGOP myop; |
949 | LOGOP myop; |
883 | |
950 | |
884 | PL_top_env = &PL_start_env; |
951 | PL_top_env = &PL_start_env; |
… | |
… | |
902 | |
969 | |
903 | /* |
970 | /* |
904 | * this is a _very_ stripped down perl interpreter ;) |
971 | * this is a _very_ stripped down perl interpreter ;) |
905 | */ |
972 | */ |
906 | static void |
973 | static void |
907 | coro_run (void *arg) |
974 | cctx_run (void *arg) |
908 | { |
975 | { |
909 | dTHX; |
976 | dTHX; |
910 | |
977 | |
911 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
978 | /* cctx_run is the alternative tail of transfer(), so unlock here. */ |
912 | UNLOCK; |
979 | UNLOCK; |
913 | |
980 | |
914 | /* we now skip the entersub that lead to transfer() */ |
981 | /* we now skip the entersub that lead to transfer() */ |
915 | PL_op = PL_op->op_next; |
982 | PL_op = PL_op->op_next; |
916 | |
983 | |
917 | /* inject a fake subroutine call to cctx_init */ |
984 | /* inject a fake subroutine call to cctx_init */ |
918 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
985 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
919 | |
986 | |
920 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
987 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
921 | PL_restartop = PL_op; |
988 | PL_restartop = PL_op; |
922 | perl_run (PL_curinterp); |
989 | perl_run (PL_curinterp); |
923 | |
990 | |
… | |
… | |
942 | ++cctx_count; |
1009 | ++cctx_count; |
943 | |
1010 | |
944 | Newz (0, cctx, 1, coro_cctx); |
1011 | Newz (0, cctx, 1, coro_cctx); |
945 | |
1012 | |
946 | #if HAVE_MMAP |
1013 | #if HAVE_MMAP |
947 | |
|
|
948 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
1014 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
949 | /* mmap supposedly does allocate-on-write for us */ |
1015 | /* mmap supposedly does allocate-on-write for us */ |
950 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
1016 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
951 | |
1017 | |
952 | if (cctx->sptr != (void *)-1) |
1018 | if (cctx->sptr != (void *)-1) |
… | |
… | |
973 | stack_start = cctx->sptr; |
1039 | stack_start = cctx->sptr; |
974 | stack_size = cctx->ssize; |
1040 | stack_size = cctx->ssize; |
975 | } |
1041 | } |
976 | |
1042 | |
977 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
1043 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
978 | coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size); |
1044 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); |
979 | |
1045 | |
980 | return cctx; |
1046 | return cctx; |
981 | } |
1047 | } |
982 | |
1048 | |
983 | static void |
1049 | static void |
… | |
… | |
1057 | if (expect_false (next->flags & CF_DESTROYED)) |
1123 | if (expect_false (next->flags & CF_DESTROYED)) |
1058 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
1124 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
1059 | |
1125 | |
1060 | if ( |
1126 | if ( |
1061 | #if PERL_VERSION_ATLEAST (5,9,0) |
1127 | #if PERL_VERSION_ATLEAST (5,9,0) |
1062 | expect_false (PL_parser) |
1128 | expect_false (PL_parser && PL_parser->lex_state != LEX_NOTPARSING) |
1063 | #else |
1129 | #else |
1064 | expect_false (PL_lex_state != LEX_NOTPARSING) |
1130 | expect_false (PL_lex_state != LEX_NOTPARSING) |
1065 | #endif |
1131 | #endif |
1066 | ) |
1132 | ) |
1067 | croak ("Coro::State::transfer called while parsing, but this is not supported"); |
1133 | croak ("Coro::State::transfer called while parsing, but this is not supported"); |
… | |
… | |
1071 | /* always use the TRANSFER macro */ |
1137 | /* always use the TRANSFER macro */ |
1072 | static void NOINLINE |
1138 | static void NOINLINE |
1073 | transfer (pTHX_ struct coro *prev, struct coro *next) |
1139 | transfer (pTHX_ struct coro *prev, struct coro *next) |
1074 | { |
1140 | { |
1075 | dSTACKLEVEL; |
1141 | dSTACKLEVEL; |
|
|
1142 | static volatile int has_throw; |
1076 | |
1143 | |
1077 | /* sometimes transfer is only called to set idle_sp */ |
1144 | /* sometimes transfer is only called to set idle_sp */ |
1078 | if (expect_false (!next)) |
1145 | if (expect_false (!next)) |
1079 | { |
1146 | { |
1080 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1147 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
… | |
… | |
1095 | prev->flags &= ~CF_RUNNING; |
1162 | prev->flags &= ~CF_RUNNING; |
1096 | next->flags |= CF_RUNNING; |
1163 | next->flags |= CF_RUNNING; |
1097 | |
1164 | |
1098 | LOCK; |
1165 | LOCK; |
1099 | |
1166 | |
|
|
1167 | /* first get rid of the old state */ |
|
|
1168 | save_perl (aTHX_ prev); |
|
|
1169 | |
1100 | if (expect_false (next->flags & CF_NEW)) |
1170 | if (expect_false (next->flags & CF_NEW)) |
1101 | { |
1171 | { |
1102 | /* need to start coroutine */ |
1172 | /* need to start coroutine */ |
1103 | next->flags &= ~CF_NEW; |
1173 | next->flags &= ~CF_NEW; |
1104 | /* first get rid of the old state */ |
|
|
1105 | save_perl (aTHX_ prev); |
|
|
1106 | /* setup coroutine call */ |
1174 | /* setup coroutine call */ |
1107 | coro_setup (aTHX_ next); |
1175 | coro_setup (aTHX_ next); |
1108 | } |
1176 | } |
1109 | else |
1177 | else |
1110 | { |
|
|
1111 | /* coroutine already started */ |
|
|
1112 | save_perl (aTHX_ prev); |
|
|
1113 | load_perl (aTHX_ next); |
1178 | load_perl (aTHX_ next); |
1114 | } |
|
|
1115 | |
1179 | |
1116 | prev__cctx = prev->cctx; |
1180 | prev__cctx = prev->cctx; |
1117 | |
1181 | |
1118 | /* possibly "free" the cctx */ |
1182 | /* possibly "free" the cctx */ |
1119 | 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))) |
… | |
… | |
1124 | prev->cctx = 0; |
1188 | prev->cctx = 0; |
1125 | |
1189 | |
1126 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1190 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1127 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1191 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1128 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
1192 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
|
|
1193 | if (!next->cctx) |
1129 | next->cctx = cctx_get (aTHX); |
1194 | next->cctx = cctx_get (aTHX); |
1130 | |
1195 | |
1131 | cctx_put (prev__cctx); |
1196 | cctx_put (prev__cctx); |
1132 | } |
1197 | } |
1133 | |
1198 | |
1134 | ++next->usecount; |
1199 | ++next->usecount; |
1135 | |
1200 | |
1136 | if (expect_true (!next->cctx)) |
1201 | if (expect_true (!next->cctx)) |
1137 | next->cctx = cctx_get (aTHX); |
1202 | next->cctx = cctx_get (aTHX); |
|
|
1203 | |
|
|
1204 | has_throw = !!next->throw; |
1138 | |
1205 | |
1139 | if (expect_false (prev__cctx != next->cctx)) |
1206 | if (expect_false (prev__cctx != next->cctx)) |
1140 | { |
1207 | { |
1141 | prev__cctx->top_env = PL_top_env; |
1208 | prev__cctx->top_env = PL_top_env; |
1142 | PL_top_env = next->cctx->top_env; |
1209 | PL_top_env = next->cctx->top_env; |
… | |
… | |
1144 | } |
1211 | } |
1145 | |
1212 | |
1146 | free_coro_mortal (aTHX); |
1213 | free_coro_mortal (aTHX); |
1147 | UNLOCK; |
1214 | UNLOCK; |
1148 | |
1215 | |
1149 | if (expect_false (prev->throw || next->throw)) |
1216 | if (expect_false (has_throw)) |
1150 | { |
1217 | { |
1151 | struct coro *coro = SvSTATE (coro_current); |
1218 | struct coro *coro = SvSTATE (coro_current); |
1152 | |
1219 | |
1153 | if (coro->throw) |
1220 | if (coro->throw) |
1154 | { |
1221 | { |
… | |
… | |
1280 | { |
1347 | { |
1281 | 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); |
1282 | } |
1349 | } |
1283 | |
1350 | |
1284 | static SV * |
1351 | static SV * |
1285 | coro_deq (pTHX_ int min_prio) |
1352 | coro_deq (pTHX) |
1286 | { |
1353 | { |
1287 | int prio = PRIO_MAX - PRIO_MIN; |
1354 | int prio; |
1288 | |
1355 | |
1289 | min_prio -= PRIO_MIN; |
|
|
1290 | if (min_prio < 0) |
|
|
1291 | min_prio = 0; |
|
|
1292 | |
|
|
1293 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
1356 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1294 | if (AvFILLp (coro_ready [prio]) >= 0) |
1357 | if (AvFILLp (coro_ready [prio]) >= 0) |
1295 | return av_shift (coro_ready [prio]); |
1358 | return av_shift (coro_ready [prio]); |
1296 | |
1359 | |
1297 | return 0; |
1360 | return 0; |
1298 | } |
1361 | } |
… | |
… | |
1334 | SV *prev_sv, *next_sv; |
1397 | SV *prev_sv, *next_sv; |
1335 | |
1398 | |
1336 | for (;;) |
1399 | for (;;) |
1337 | { |
1400 | { |
1338 | LOCK; |
1401 | LOCK; |
1339 | next_sv = coro_deq (aTHX_ PRIO_MIN); |
1402 | next_sv = coro_deq (aTHX); |
1340 | |
1403 | |
1341 | /* nothing to schedule: call the idle handler */ |
1404 | /* nothing to schedule: call the idle handler */ |
1342 | if (expect_false (!next_sv)) |
1405 | if (expect_false (!next_sv)) |
1343 | { |
1406 | { |
1344 | dSP; |
1407 | dSP; |
… | |
… | |
1348 | SAVETMPS; |
1411 | SAVETMPS; |
1349 | |
1412 | |
1350 | PUSHMARK (SP); |
1413 | PUSHMARK (SP); |
1351 | PUTBACK; |
1414 | PUTBACK; |
1352 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
1415 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
|
|
1416 | SPAGAIN; |
1353 | |
1417 | |
1354 | FREETMPS; |
1418 | FREETMPS; |
1355 | LEAVE; |
1419 | LEAVE; |
1356 | continue; |
1420 | continue; |
1357 | } |
1421 | } |
… | |
… | |
1487 | BOOT_PAGESIZE; |
1551 | BOOT_PAGESIZE; |
1488 | |
1552 | |
1489 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1553 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1490 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1554 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1491 | |
1555 | |
1492 | sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE); |
1556 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; |
1493 | sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE); |
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; |
1494 | |
1560 | |
1495 | if (!PL_diehook) PL_diehook = SvREFCNT_inc (sv_diehook); |
1561 | hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); |
1496 | if (!PL_warnhook) PL_warnhook = SvREFCNT_inc (sv_warnhook); |
1562 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
|
|
1563 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
1497 | |
1564 | |
1498 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1565 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1499 | |
1566 | |
1500 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1567 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1501 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
1568 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
… | |
… | |
1507 | |
1574 | |
1508 | while (main_top_env->je_prev) |
1575 | while (main_top_env->je_prev) |
1509 | main_top_env = main_top_env->je_prev; |
1576 | main_top_env = main_top_env->je_prev; |
1510 | |
1577 | |
1511 | coroapi.ver = CORO_API_VERSION; |
1578 | coroapi.ver = CORO_API_VERSION; |
|
|
1579 | coroapi.rev = CORO_API_REVISION; |
1512 | coroapi.transfer = api_transfer; |
1580 | coroapi.transfer = api_transfer; |
1513 | |
1581 | |
1514 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1582 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1515 | } |
1583 | } |
1516 | |
1584 | |
1517 | SV * |
1585 | SV * |
1518 | new (char *klass, ...) |
1586 | new (char *klass, ...) |
1519 | CODE: |
1587 | CODE: |
1520 | { |
1588 | { |
1521 | struct coro *coro; |
1589 | struct coro *coro; |
|
|
1590 | MAGIC *mg; |
1522 | HV *hv; |
1591 | HV *hv; |
1523 | int i; |
1592 | int i; |
1524 | |
1593 | |
1525 | Newz (0, coro, 1, struct coro); |
1594 | Newz (0, coro, 1, struct coro); |
1526 | coro->args = newAV (); |
1595 | coro->args = newAV (); |
… | |
… | |
1529 | if (coro_first) coro_first->prev = coro; |
1598 | if (coro_first) coro_first->prev = coro; |
1530 | coro->next = coro_first; |
1599 | coro->next = coro_first; |
1531 | coro_first = coro; |
1600 | coro_first = coro; |
1532 | |
1601 | |
1533 | coro->hv = hv = newHV (); |
1602 | coro->hv = hv = newHV (); |
1534 | 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; |
1535 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1605 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1536 | |
1606 | |
1537 | av_extend (coro->args, items - 1); |
1607 | av_extend (coro->args, items - 1); |
1538 | for (i = 1; i < items; i++) |
1608 | for (i = 1; i < items; i++) |
1539 | av_push (coro->args, newSVsv (ST (i))); |
1609 | av_push (coro->args, newSVsv (ST (i))); |
… | |
… | |
1553 | Coro::cede_notself = 4 |
1623 | Coro::cede_notself = 4 |
1554 | CODE: |
1624 | CODE: |
1555 | { |
1625 | { |
1556 | struct transfer_args ta; |
1626 | struct transfer_args ta; |
1557 | |
1627 | |
|
|
1628 | PUTBACK; |
1558 | switch (ix) |
1629 | switch (ix) |
1559 | { |
1630 | { |
1560 | case 0: |
1631 | case 0: |
1561 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1632 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1562 | ta.next = 0; |
1633 | ta.next = 0; |
… | |
… | |
1581 | if (!prepare_cede_notself (aTHX_ &ta)) |
1652 | if (!prepare_cede_notself (aTHX_ &ta)) |
1582 | XSRETURN_EMPTY; |
1653 | XSRETURN_EMPTY; |
1583 | |
1654 | |
1584 | break; |
1655 | break; |
1585 | } |
1656 | } |
|
|
1657 | SPAGAIN; |
1586 | |
1658 | |
1587 | BARRIER; |
1659 | BARRIER; |
|
|
1660 | PUTBACK; |
1588 | TRANSFER (ta); |
1661 | TRANSFER (ta); |
1589 | |
1662 | SPAGAIN; /* might be the sp of a different coroutine now */ |
1590 | if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev)) |
1663 | /* be extra careful not to ever do anything after TRANSFER */ |
1591 | XSRETURN_YES; |
|
|
1592 | } |
1664 | } |
1593 | |
1665 | |
1594 | bool |
1666 | bool |
1595 | _destroy (SV *coro_sv) |
1667 | _destroy (SV *coro_sv) |
1596 | CODE: |
1668 | CODE: |
… | |
… | |
1648 | { |
1720 | { |
1649 | struct coro temp; |
1721 | struct coro temp; |
1650 | |
1722 | |
1651 | if (!(coro->flags & CF_RUNNING)) |
1723 | if (!(coro->flags & CF_RUNNING)) |
1652 | { |
1724 | { |
|
|
1725 | PUTBACK; |
1653 | save_perl (aTHX_ &temp); |
1726 | save_perl (aTHX_ &temp); |
1654 | load_perl (aTHX_ coro); |
1727 | load_perl (aTHX_ coro); |
1655 | } |
1728 | } |
1656 | |
1729 | |
1657 | { |
1730 | { |
1658 | dSP; |
1731 | dSP; |
1659 | ENTER; |
1732 | ENTER; |
1660 | SAVETMPS; |
1733 | SAVETMPS; |
|
|
1734 | PUTBACK; |
|
|
1735 | PUSHSTACK; |
1661 | PUSHMARK (SP); |
1736 | PUSHMARK (SP); |
1662 | PUTBACK; |
|
|
1663 | |
1737 | |
1664 | if (ix) |
1738 | if (ix) |
1665 | eval_sv (coderef, 0); |
1739 | eval_sv (coderef, 0); |
1666 | else |
1740 | else |
1667 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1741 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1668 | |
1742 | |
|
|
1743 | POPSTACK; |
1669 | SPAGAIN; |
1744 | SPAGAIN; |
1670 | FREETMPS; |
1745 | FREETMPS; |
1671 | LEAVE; |
1746 | LEAVE; |
1672 | PUTBACK; |
1747 | PUTBACK; |
1673 | } |
1748 | } |
1674 | |
1749 | |
1675 | if (!(coro->flags & CF_RUNNING)) |
1750 | if (!(coro->flags & CF_RUNNING)) |
1676 | { |
1751 | { |
1677 | save_perl (aTHX_ coro); |
1752 | save_perl (aTHX_ coro); |
1678 | load_perl (aTHX_ &temp); |
1753 | load_perl (aTHX_ &temp); |
|
|
1754 | SPAGAIN; |
1679 | } |
1755 | } |
1680 | } |
1756 | } |
1681 | } |
1757 | } |
1682 | |
1758 | |
1683 | SV * |
1759 | SV * |
… | |
… | |
1731 | |
1807 | |
1732 | BOOT: |
1808 | BOOT: |
1733 | { |
1809 | { |
1734 | int i; |
1810 | int i; |
1735 | |
1811 | |
1736 | sv_pool_rss = coro_get_sv ("Coro::POOL_RSS" , TRUE); |
1812 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
1737 | sv_pool_size = coro_get_sv ("Coro::POOL_SIZE" , TRUE); |
1813 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
1738 | av_async_pool = coro_get_av ("Coro::async_pool", TRUE); |
1814 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
1739 | |
1815 | |
1740 | coro_current = coro_get_sv ("Coro::current", FALSE); |
1816 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
1741 | SvREADONLY_on (coro_current); |
1817 | SvREADONLY_on (coro_current); |
1742 | |
1818 | |
1743 | coro_stash = gv_stashpv ("Coro", TRUE); |
1819 | coro_stash = gv_stashpv ("Coro", TRUE); |
1744 | |
1820 | |
1745 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1821 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
… | |
… | |
1833 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
1909 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
1834 | AV *invoke_av; |
1910 | AV *invoke_av; |
1835 | int i, len; |
1911 | int i, len; |
1836 | |
1912 | |
1837 | if (!invoke) |
1913 | if (!invoke) |
|
|
1914 | { |
|
|
1915 | SvREFCNT_dec (PL_diehook); PL_diehook = 0; |
1838 | croak ("\3async_pool terminate\2\n"); |
1916 | croak ("\3async_pool terminate\2\n"); |
|
|
1917 | } |
1839 | |
1918 | |
1840 | SvREFCNT_dec (coro->saved_deffh); |
1919 | SvREFCNT_dec (coro->saved_deffh); |
1841 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
1920 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
1842 | |
1921 | |
1843 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1922 | hv_store (hv, "desc", sizeof ("desc") - 1, |
… | |
… | |
1869 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
1948 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
1870 | coro->saved_deffh = 0; |
1949 | coro->saved_deffh = 0; |
1871 | |
1950 | |
1872 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1951 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1873 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1952 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
1953 | { |
|
|
1954 | SvREFCNT_dec (PL_diehook); PL_diehook = 0; |
1874 | croak ("\3async_pool terminate\2\n"); |
1955 | croak ("\3async_pool terminate\2\n"); |
|
|
1956 | } |
1875 | |
1957 | |
1876 | av_clear (GvAV (PL_defgv)); |
1958 | av_clear (GvAV (PL_defgv)); |
1877 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1959 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1878 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
1960 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
1879 | |
1961 | |