… | |
… | |
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 */ |
|
|
154 | static SV *rv_diehook; |
|
|
155 | static SV *rv_warnhook; |
|
|
156 | static HV *hv_sig; /* %SIG */ |
153 | |
157 | |
154 | /* async_pool helper stuff */ |
158 | /* async_pool helper stuff */ |
155 | static SV *sv_pool_rss; |
159 | static SV *sv_pool_rss; |
156 | static SV *sv_pool_size; |
160 | static SV *sv_pool_size; |
157 | static AV *av_async_pool; |
161 | static AV *av_async_pool; |
… | |
… | |
215 | |
219 | |
216 | /* process data */ |
220 | /* process data */ |
217 | AV *mainstack; |
221 | AV *mainstack; |
218 | perl_slots *slot; /* basically the saved sp */ |
222 | perl_slots *slot; /* basically the saved sp */ |
219 | |
223 | |
220 | /* data associated with this coroutine (initial args) */ |
224 | AV *args; /* data associated with this coroutine (initial args) */ |
221 | AV *args; |
225 | int refcnt; /* coroutines are refcounted, yes */ |
222 | int refcnt; |
|
|
223 | int flags; /* CF_ flags */ |
226 | int flags; /* CF_ flags */ |
|
|
227 | HV *hv; /* the perl hash associated with this coro, if any */ |
224 | |
228 | |
225 | /* statistics */ |
229 | /* statistics */ |
226 | int usecount; /* number of transfers to this coro */ |
230 | int usecount; /* number of transfers to this coro */ |
227 | |
231 | |
228 | /* coro process data */ |
232 | /* coro process data */ |
229 | int prio; |
233 | int prio; |
230 | SV *throw; |
234 | SV *throw; /* exception to be thrown */ |
231 | |
235 | |
232 | /* async_pool */ |
236 | /* async_pool */ |
233 | SV *saved_deffh; |
237 | SV *saved_deffh; |
234 | |
238 | |
235 | /* linked list */ |
239 | /* linked list */ |
236 | struct coro *next, *prev; |
240 | struct coro *next, *prev; |
237 | HV *hv; /* the perl hash associated with this coro, if any */ |
|
|
238 | }; |
241 | }; |
239 | |
242 | |
240 | typedef struct coro *Coro__State; |
243 | typedef struct coro *Coro__State; |
241 | typedef struct coro *Coro__State_or_hashref; |
244 | typedef struct coro *Coro__State_or_hashref; |
242 | |
245 | |
… | |
… | |
254 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
257 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
255 | static int coro_nready; |
258 | static int coro_nready; |
256 | static struct coro *coro_first; |
259 | static struct coro *coro_first; |
257 | |
260 | |
258 | /** lowlevel stuff **********************************************************/ |
261 | /** lowlevel stuff **********************************************************/ |
|
|
262 | |
|
|
263 | static SV * |
|
|
264 | coro_get_sv (pTHX_ const char *name, int create) |
|
|
265 | { |
|
|
266 | #if PERL_VERSION_ATLEAST (5,9,0) |
|
|
267 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
|
|
268 | get_sv (name, create); |
|
|
269 | #endif |
|
|
270 | return get_sv (name, create); |
|
|
271 | } |
|
|
272 | |
|
|
273 | static AV * |
|
|
274 | coro_get_av (pTHX_ const char *name, int create) |
|
|
275 | { |
|
|
276 | #if PERL_VERSION_ATLEAST (5,9,0) |
|
|
277 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
|
|
278 | get_av (name, create); |
|
|
279 | #endif |
|
|
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); |
|
|
291 | } |
259 | |
292 | |
260 | static AV * |
293 | static AV * |
261 | coro_clone_padlist (pTHX_ CV *cv) |
294 | coro_clone_padlist (pTHX_ CV *cv) |
262 | { |
295 | { |
263 | AV *padlist = CvPADLIST (cv); |
296 | AV *padlist = CvPADLIST (cv); |
… | |
… | |
311 | |
344 | |
312 | /* casting is fun. */ |
345 | /* casting is fun. */ |
313 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
346 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
314 | free_padlist (aTHX_ padlist); |
347 | free_padlist (aTHX_ padlist); |
315 | |
348 | |
316 | SvREFCNT_dec (av); |
|
|
317 | |
|
|
318 | return 0; |
349 | return 0; |
319 | } |
350 | } |
320 | |
351 | |
321 | #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 |
322 | |
354 | |
323 | 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 | }; |
324 | |
359 | |
325 | #define CORO_MAGIC(cv) \ |
360 | #define CORO_MAGIC(sv,type) \ |
326 | SvMAGIC (cv) \ |
361 | SvMAGIC (sv) \ |
327 | ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ |
362 | ? SvMAGIC (sv)->mg_type == type \ |
328 | ? SvMAGIC (cv) \ |
363 | ? SvMAGIC (sv) \ |
329 | : mg_find ((SV *)cv, PERL_MAGIC_coro) \ |
364 | : mg_find (sv, type) \ |
330 | : 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) |
331 | |
369 | |
332 | static struct coro * |
370 | static struct coro * |
333 | SvSTATE_ (pTHX_ SV *coro) |
371 | SvSTATE_ (pTHX_ SV *coro) |
334 | { |
372 | { |
335 | HV *stash; |
373 | HV *stash; |
… | |
… | |
347 | /* very slow, but rare, check */ |
385 | /* very slow, but rare, check */ |
348 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
386 | if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) |
349 | croak ("Coro::State object required"); |
387 | croak ("Coro::State object required"); |
350 | } |
388 | } |
351 | |
389 | |
352 | mg = CORO_MAGIC (coro); |
390 | mg = CORO_MAGIC_state (coro); |
353 | return (struct coro *)mg->mg_ptr; |
391 | return (struct coro *)mg->mg_ptr; |
354 | } |
392 | } |
355 | |
393 | |
356 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
394 | #define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) |
357 | |
395 | |
358 | /* the next two functions merely cache the padlists */ |
396 | /* the next two functions merely cache the padlists */ |
359 | static void |
397 | static void |
360 | get_padlist (pTHX_ CV *cv) |
398 | get_padlist (pTHX_ CV *cv) |
361 | { |
399 | { |
362 | MAGIC *mg = CORO_MAGIC (cv); |
400 | MAGIC *mg = CORO_MAGIC_cv (cv); |
363 | AV *av; |
401 | AV *av; |
364 | |
402 | |
365 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
403 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
366 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
404 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
367 | else |
405 | else |
… | |
… | |
379 | } |
417 | } |
380 | |
418 | |
381 | static void |
419 | static void |
382 | put_padlist (pTHX_ CV *cv) |
420 | put_padlist (pTHX_ CV *cv) |
383 | { |
421 | { |
384 | MAGIC *mg = CORO_MAGIC (cv); |
422 | MAGIC *mg = CORO_MAGIC_cv (cv); |
385 | AV *av; |
423 | AV *av; |
386 | |
424 | |
387 | if (expect_false (!mg)) |
425 | if (expect_false (!mg)) |
388 | { |
426 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
389 | sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); |
|
|
390 | mg = mg_find ((SV *)cv, PERL_MAGIC_coro); |
|
|
391 | mg->mg_virtual = &vtbl_coro; |
|
|
392 | mg->mg_obj = (SV *)newAV (); |
|
|
393 | } |
|
|
394 | |
427 | |
395 | av = (AV *)mg->mg_obj; |
428 | av = (AV *)mg->mg_obj; |
396 | |
429 | |
397 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
430 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
398 | av_extend (av, AvMAX (av) + 1); |
431 | av_extend (av, AvMAX (av) + 1); |
… | |
… | |
485 | |
518 | |
486 | PUTBACK; |
519 | PUTBACK; |
487 | } |
520 | } |
488 | |
521 | |
489 | /* 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; |
490 | { |
527 | { |
491 | /* we manually unroll here, as usually 2 slots is enough */ |
|
|
492 | int i; |
528 | int i; |
493 | if (SLOT_COUNT >= 1) CXINC; |
|
|
494 | if (SLOT_COUNT >= 2) CXINC; |
|
|
495 | if (SLOT_COUNT >= 3) CXINC; |
|
|
496 | for (i = 3; i < SLOT_COUNT; ++i) |
529 | for (i = 3; i < SLOT_COUNT; ++i) |
497 | CXINC; |
530 | CXINC; |
498 | |
|
|
499 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
|
|
500 | } |
531 | } |
|
|
532 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
501 | |
533 | |
502 | c->mainstack = PL_mainstack; |
534 | c->mainstack = PL_mainstack; |
503 | |
535 | |
504 | { |
536 | { |
505 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
537 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
… | |
… | |
632 | return rss; |
664 | return rss; |
633 | } |
665 | } |
634 | |
666 | |
635 | /** coroutine stack handling ************************************************/ |
667 | /** coroutine stack handling ************************************************/ |
636 | |
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 | |
637 | static void |
719 | static void |
638 | coro_setup (pTHX_ struct coro *coro) |
720 | coro_setup (pTHX_ struct coro *coro) |
639 | { |
721 | { |
640 | /* |
722 | /* |
641 | * emulate part of the perl startup here. |
723 | * emulate part of the perl startup here. |
… | |
… | |
649 | PL_curpm = 0; |
731 | PL_curpm = 0; |
650 | PL_curpad = 0; |
732 | PL_curpad = 0; |
651 | PL_localizing = 0; |
733 | PL_localizing = 0; |
652 | PL_dirty = 0; |
734 | PL_dirty = 0; |
653 | PL_restartop = 0; |
735 | PL_restartop = 0; |
654 | PL_diehook = 0; |
736 | |
655 | PL_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); |
656 | |
740 | |
657 | GvSV (PL_defgv) = newSV (0); |
741 | GvSV (PL_defgv) = newSV (0); |
658 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
742 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
659 | GvSV (PL_errgv) = newSV (0); |
743 | GvSV (PL_errgv) = newSV (0); |
660 | 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); |
… | |
… | |
675 | PL_op = (OP *)&myop; |
759 | PL_op = (OP *)&myop; |
676 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
760 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
677 | SPAGAIN; |
761 | SPAGAIN; |
678 | } |
762 | } |
679 | |
763 | |
680 | 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; |
681 | } |
770 | } |
682 | |
771 | |
683 | static void |
772 | static void |
684 | coro_destroy (pTHX_ struct coro *coro) |
773 | coro_destroy (pTHX_ struct coro *coro) |
685 | { |
774 | { |
… | |
… | |
852 | |
941 | |
853 | /* 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 */ |
854 | /* _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 */ |
855 | /* during execution of a perl program */ |
944 | /* during execution of a perl program */ |
856 | static void NOINLINE |
945 | static void NOINLINE |
857 | prepare_cctx (pTHX_ coro_cctx *cctx) |
946 | cctx_prepare (pTHX_ coro_cctx *cctx) |
858 | { |
947 | { |
859 | dSP; |
948 | dSP; |
860 | LOGOP myop; |
949 | LOGOP myop; |
861 | |
950 | |
862 | PL_top_env = &PL_start_env; |
951 | PL_top_env = &PL_start_env; |
… | |
… | |
880 | |
969 | |
881 | /* |
970 | /* |
882 | * this is a _very_ stripped down perl interpreter ;) |
971 | * this is a _very_ stripped down perl interpreter ;) |
883 | */ |
972 | */ |
884 | static void |
973 | static void |
885 | coro_run (void *arg) |
974 | cctx_run (void *arg) |
886 | { |
975 | { |
887 | dTHX; |
976 | dTHX; |
888 | |
977 | |
889 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
978 | /* cctx_run is the alternative tail of transfer(), so unlock here. */ |
890 | UNLOCK; |
979 | UNLOCK; |
891 | |
980 | |
892 | /* we now skip the entersub that lead to transfer() */ |
981 | /* we now skip the entersub that lead to transfer() */ |
893 | PL_op = PL_op->op_next; |
982 | PL_op = PL_op->op_next; |
894 | |
983 | |
895 | /* inject a fake subroutine call to cctx_init */ |
984 | /* inject a fake subroutine call to cctx_init */ |
896 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
985 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
897 | |
986 | |
898 | /* 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 */ |
899 | PL_restartop = PL_op; |
988 | PL_restartop = PL_op; |
900 | perl_run (PL_curinterp); |
989 | perl_run (PL_curinterp); |
901 | |
990 | |
… | |
… | |
920 | ++cctx_count; |
1009 | ++cctx_count; |
921 | |
1010 | |
922 | Newz (0, cctx, 1, coro_cctx); |
1011 | Newz (0, cctx, 1, coro_cctx); |
923 | |
1012 | |
924 | #if HAVE_MMAP |
1013 | #if HAVE_MMAP |
925 | |
|
|
926 | 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; |
927 | /* mmap supposedly does allocate-on-write for us */ |
1015 | /* mmap supposedly does allocate-on-write for us */ |
928 | 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); |
929 | |
1017 | |
930 | if (cctx->sptr != (void *)-1) |
1018 | if (cctx->sptr != (void *)-1) |
… | |
… | |
951 | stack_start = cctx->sptr; |
1039 | stack_start = cctx->sptr; |
952 | stack_size = cctx->ssize; |
1040 | stack_size = cctx->ssize; |
953 | } |
1041 | } |
954 | |
1042 | |
955 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
1043 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
956 | 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); |
957 | |
1045 | |
958 | return cctx; |
1046 | return cctx; |
959 | } |
1047 | } |
960 | |
1048 | |
961 | static void |
1049 | static void |
… | |
… | |
1049 | /* always use the TRANSFER macro */ |
1137 | /* always use the TRANSFER macro */ |
1050 | static void NOINLINE |
1138 | static void NOINLINE |
1051 | transfer (pTHX_ struct coro *prev, struct coro *next) |
1139 | transfer (pTHX_ struct coro *prev, struct coro *next) |
1052 | { |
1140 | { |
1053 | dSTACKLEVEL; |
1141 | dSTACKLEVEL; |
|
|
1142 | static volatile int has_throw; |
1054 | |
1143 | |
1055 | /* sometimes transfer is only called to set idle_sp */ |
1144 | /* sometimes transfer is only called to set idle_sp */ |
1056 | if (expect_false (!next)) |
1145 | if (expect_false (!next)) |
1057 | { |
1146 | { |
1058 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1147 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
… | |
… | |
1073 | prev->flags &= ~CF_RUNNING; |
1162 | prev->flags &= ~CF_RUNNING; |
1074 | next->flags |= CF_RUNNING; |
1163 | next->flags |= CF_RUNNING; |
1075 | |
1164 | |
1076 | LOCK; |
1165 | LOCK; |
1077 | |
1166 | |
|
|
1167 | /* first get rid of the old state */ |
|
|
1168 | save_perl (aTHX_ prev); |
|
|
1169 | |
1078 | if (expect_false (next->flags & CF_NEW)) |
1170 | if (expect_false (next->flags & CF_NEW)) |
1079 | { |
1171 | { |
1080 | /* need to start coroutine */ |
1172 | /* need to start coroutine */ |
1081 | next->flags &= ~CF_NEW; |
1173 | next->flags &= ~CF_NEW; |
1082 | /* first get rid of the old state */ |
|
|
1083 | save_perl (aTHX_ prev); |
|
|
1084 | /* setup coroutine call */ |
1174 | /* setup coroutine call */ |
1085 | coro_setup (aTHX_ next); |
1175 | coro_setup (aTHX_ next); |
1086 | } |
1176 | } |
1087 | else |
1177 | else |
1088 | { |
|
|
1089 | /* coroutine already started */ |
|
|
1090 | save_perl (aTHX_ prev); |
|
|
1091 | load_perl (aTHX_ next); |
1178 | load_perl (aTHX_ next); |
1092 | } |
|
|
1093 | |
1179 | |
1094 | prev__cctx = prev->cctx; |
1180 | prev__cctx = prev->cctx; |
1095 | |
1181 | |
1096 | /* possibly "free" the cctx */ |
1182 | /* possibly "free" the cctx */ |
1097 | 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))) |
… | |
… | |
1102 | prev->cctx = 0; |
1188 | prev->cctx = 0; |
1103 | |
1189 | |
1104 | /* 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 */ |
1105 | /* 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 */ |
1106 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
1192 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
|
|
1193 | if (!next->cctx) |
1107 | next->cctx = cctx_get (aTHX); |
1194 | next->cctx = cctx_get (aTHX); |
1108 | |
1195 | |
1109 | cctx_put (prev__cctx); |
1196 | cctx_put (prev__cctx); |
1110 | } |
1197 | } |
1111 | |
1198 | |
1112 | ++next->usecount; |
1199 | ++next->usecount; |
1113 | |
1200 | |
1114 | if (expect_true (!next->cctx)) |
1201 | if (expect_true (!next->cctx)) |
1115 | next->cctx = cctx_get (aTHX); |
1202 | next->cctx = cctx_get (aTHX); |
|
|
1203 | |
|
|
1204 | has_throw = !!next->throw; |
1116 | |
1205 | |
1117 | if (expect_false (prev__cctx != next->cctx)) |
1206 | if (expect_false (prev__cctx != next->cctx)) |
1118 | { |
1207 | { |
1119 | prev__cctx->top_env = PL_top_env; |
1208 | prev__cctx->top_env = PL_top_env; |
1120 | PL_top_env = next->cctx->top_env; |
1209 | PL_top_env = next->cctx->top_env; |
… | |
… | |
1122 | } |
1211 | } |
1123 | |
1212 | |
1124 | free_coro_mortal (aTHX); |
1213 | free_coro_mortal (aTHX); |
1125 | UNLOCK; |
1214 | UNLOCK; |
1126 | |
1215 | |
1127 | if (expect_false (prev->throw || next->throw)) |
1216 | if (expect_false (has_throw)) |
1128 | { |
1217 | { |
1129 | struct coro *coro = SvSTATE (coro_current); |
1218 | struct coro *coro = SvSTATE (coro_current); |
1130 | |
1219 | |
1131 | if (coro->throw) |
1220 | if (coro->throw) |
1132 | { |
1221 | { |
… | |
… | |
1258 | { |
1347 | { |
1259 | 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); |
1260 | } |
1349 | } |
1261 | |
1350 | |
1262 | static SV * |
1351 | static SV * |
1263 | coro_deq (pTHX_ int min_prio) |
1352 | coro_deq (pTHX) |
1264 | { |
1353 | { |
1265 | int prio = PRIO_MAX - PRIO_MIN; |
1354 | int prio; |
1266 | |
1355 | |
1267 | min_prio -= PRIO_MIN; |
|
|
1268 | if (min_prio < 0) |
|
|
1269 | min_prio = 0; |
|
|
1270 | |
|
|
1271 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
1356 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1272 | if (AvFILLp (coro_ready [prio]) >= 0) |
1357 | if (AvFILLp (coro_ready [prio]) >= 0) |
1273 | return av_shift (coro_ready [prio]); |
1358 | return av_shift (coro_ready [prio]); |
1274 | |
1359 | |
1275 | return 0; |
1360 | return 0; |
1276 | } |
1361 | } |
… | |
… | |
1312 | SV *prev_sv, *next_sv; |
1397 | SV *prev_sv, *next_sv; |
1313 | |
1398 | |
1314 | for (;;) |
1399 | for (;;) |
1315 | { |
1400 | { |
1316 | LOCK; |
1401 | LOCK; |
1317 | next_sv = coro_deq (aTHX_ PRIO_MIN); |
1402 | next_sv = coro_deq (aTHX); |
1318 | |
1403 | |
1319 | /* nothing to schedule: call the idle handler */ |
1404 | /* nothing to schedule: call the idle handler */ |
1320 | if (expect_false (!next_sv)) |
1405 | if (expect_false (!next_sv)) |
1321 | { |
1406 | { |
1322 | dSP; |
1407 | dSP; |
… | |
… | |
1326 | SAVETMPS; |
1411 | SAVETMPS; |
1327 | |
1412 | |
1328 | PUSHMARK (SP); |
1413 | PUSHMARK (SP); |
1329 | PUTBACK; |
1414 | PUTBACK; |
1330 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
1415 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
|
|
1416 | SPAGAIN; |
1331 | |
1417 | |
1332 | FREETMPS; |
1418 | FREETMPS; |
1333 | LEAVE; |
1419 | LEAVE; |
1334 | continue; |
1420 | continue; |
1335 | } |
1421 | } |
… | |
… | |
1465 | BOOT_PAGESIZE; |
1551 | BOOT_PAGESIZE; |
1466 | |
1552 | |
1467 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1553 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1468 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1554 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1469 | |
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 | |
|
|
1561 | hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); |
|
|
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)); |
|
|
1564 | |
1470 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1565 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1471 | |
1566 | |
1472 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1567 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1473 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
1568 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
1474 | newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); |
1569 | newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE)); |
… | |
… | |
1479 | |
1574 | |
1480 | while (main_top_env->je_prev) |
1575 | while (main_top_env->je_prev) |
1481 | main_top_env = main_top_env->je_prev; |
1576 | main_top_env = main_top_env->je_prev; |
1482 | |
1577 | |
1483 | coroapi.ver = CORO_API_VERSION; |
1578 | coroapi.ver = CORO_API_VERSION; |
|
|
1579 | coroapi.rev = CORO_API_REVISION; |
1484 | coroapi.transfer = api_transfer; |
1580 | coroapi.transfer = api_transfer; |
1485 | |
1581 | |
1486 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1582 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1487 | } |
1583 | } |
1488 | |
1584 | |
1489 | SV * |
1585 | SV * |
1490 | new (char *klass, ...) |
1586 | new (char *klass, ...) |
1491 | CODE: |
1587 | CODE: |
1492 | { |
1588 | { |
1493 | struct coro *coro; |
1589 | struct coro *coro; |
|
|
1590 | MAGIC *mg; |
1494 | HV *hv; |
1591 | HV *hv; |
1495 | int i; |
1592 | int i; |
1496 | |
1593 | |
1497 | Newz (0, coro, 1, struct coro); |
1594 | Newz (0, coro, 1, struct coro); |
1498 | coro->args = newAV (); |
1595 | coro->args = newAV (); |
… | |
… | |
1501 | if (coro_first) coro_first->prev = coro; |
1598 | if (coro_first) coro_first->prev = coro; |
1502 | coro->next = coro_first; |
1599 | coro->next = coro_first; |
1503 | coro_first = coro; |
1600 | coro_first = coro; |
1504 | |
1601 | |
1505 | coro->hv = hv = newHV (); |
1602 | coro->hv = hv = newHV (); |
1506 | 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; |
1507 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1605 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1508 | |
1606 | |
1509 | av_extend (coro->args, items - 1); |
1607 | av_extend (coro->args, items - 1); |
1510 | for (i = 1; i < items; i++) |
1608 | for (i = 1; i < items; i++) |
1511 | av_push (coro->args, newSVsv (ST (i))); |
1609 | av_push (coro->args, newSVsv (ST (i))); |
… | |
… | |
1525 | Coro::cede_notself = 4 |
1623 | Coro::cede_notself = 4 |
1526 | CODE: |
1624 | CODE: |
1527 | { |
1625 | { |
1528 | struct transfer_args ta; |
1626 | struct transfer_args ta; |
1529 | |
1627 | |
|
|
1628 | PUTBACK; |
1530 | switch (ix) |
1629 | switch (ix) |
1531 | { |
1630 | { |
1532 | case 0: |
1631 | case 0: |
1533 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1632 | ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); |
1534 | ta.next = 0; |
1633 | ta.next = 0; |
… | |
… | |
1553 | if (!prepare_cede_notself (aTHX_ &ta)) |
1652 | if (!prepare_cede_notself (aTHX_ &ta)) |
1554 | XSRETURN_EMPTY; |
1653 | XSRETURN_EMPTY; |
1555 | |
1654 | |
1556 | break; |
1655 | break; |
1557 | } |
1656 | } |
|
|
1657 | SPAGAIN; |
1558 | |
1658 | |
1559 | BARRIER; |
1659 | BARRIER; |
|
|
1660 | PUTBACK; |
1560 | TRANSFER (ta); |
1661 | TRANSFER (ta); |
1561 | |
1662 | SPAGAIN; /* might be the sp of a different coroutine now */ |
1562 | if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev)) |
1663 | /* be extra careful not to ever do anything after TRANSFER */ |
1563 | XSRETURN_YES; |
|
|
1564 | } |
1664 | } |
1565 | |
1665 | |
1566 | bool |
1666 | bool |
1567 | _destroy (SV *coro_sv) |
1667 | _destroy (SV *coro_sv) |
1568 | CODE: |
1668 | CODE: |
… | |
… | |
1620 | { |
1720 | { |
1621 | struct coro temp; |
1721 | struct coro temp; |
1622 | |
1722 | |
1623 | if (!(coro->flags & CF_RUNNING)) |
1723 | if (!(coro->flags & CF_RUNNING)) |
1624 | { |
1724 | { |
|
|
1725 | PUTBACK; |
1625 | save_perl (aTHX_ &temp); |
1726 | save_perl (aTHX_ &temp); |
1626 | load_perl (aTHX_ coro); |
1727 | load_perl (aTHX_ coro); |
1627 | } |
1728 | } |
1628 | |
1729 | |
1629 | { |
1730 | { |
1630 | dSP; |
1731 | dSP; |
1631 | ENTER; |
1732 | ENTER; |
1632 | SAVETMPS; |
1733 | SAVETMPS; |
|
|
1734 | PUTBACK; |
|
|
1735 | PUSHSTACK; |
1633 | PUSHMARK (SP); |
1736 | PUSHMARK (SP); |
1634 | PUTBACK; |
|
|
1635 | |
1737 | |
1636 | if (ix) |
1738 | if (ix) |
1637 | eval_sv (coderef, 0); |
1739 | eval_sv (coderef, 0); |
1638 | else |
1740 | else |
1639 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1741 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1640 | |
1742 | |
|
|
1743 | POPSTACK; |
1641 | SPAGAIN; |
1744 | SPAGAIN; |
1642 | FREETMPS; |
1745 | FREETMPS; |
1643 | LEAVE; |
1746 | LEAVE; |
1644 | PUTBACK; |
1747 | PUTBACK; |
1645 | } |
1748 | } |
1646 | |
1749 | |
1647 | if (!(coro->flags & CF_RUNNING)) |
1750 | if (!(coro->flags & CF_RUNNING)) |
1648 | { |
1751 | { |
1649 | save_perl (aTHX_ coro); |
1752 | save_perl (aTHX_ coro); |
1650 | load_perl (aTHX_ &temp); |
1753 | load_perl (aTHX_ &temp); |
|
|
1754 | SPAGAIN; |
1651 | } |
1755 | } |
1652 | } |
1756 | } |
1653 | } |
1757 | } |
1654 | |
1758 | |
1655 | SV * |
1759 | SV * |
… | |
… | |
1703 | |
1807 | |
1704 | BOOT: |
1808 | BOOT: |
1705 | { |
1809 | { |
1706 | int i; |
1810 | int i; |
1707 | |
1811 | |
1708 | sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE); |
1812 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
1709 | get_sv ("Coro::POOL_RSS" , TRUE); /* silence stupid 5.10 warning */ |
|
|
1710 | sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE); |
1813 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
1711 | get_sv ("Coro::POOL_SIZE" , TRUE); /* silence stupid 5.10 warning */ |
|
|
1712 | av_async_pool = get_av ("Coro::async_pool", TRUE); |
1814 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
1713 | get_av ("Coro::async_pool", TRUE); /* silence stupid 5.10 warning */ |
|
|
1714 | |
1815 | |
1715 | coro_current = get_sv ("Coro::current", FALSE); |
1816 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
1716 | SvREADONLY_on (coro_current); |
1817 | SvREADONLY_on (coro_current); |
1717 | |
1818 | |
1718 | coro_stash = gv_stashpv ("Coro", TRUE); |
1819 | coro_stash = gv_stashpv ("Coro", TRUE); |
1719 | |
1820 | |
1720 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1821 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |