… | |
… | |
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 |
… | |
… | |
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 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 | #if 0 |
|
|
670 | static int (*orig_sigelem_get) (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 | return orig_sigelem_get (aTHX_ sv, mg); |
|
|
685 | const char *s = MgPV_nolen_const (mg); |
|
|
686 | |
|
|
687 | if (*s == '_') |
|
|
688 | { |
|
|
689 | if (strEQ (s, "__DIE__" ) && PL_diehook ) return sv_setsv (sv, PL_diehook ), 0; |
|
|
690 | if (strEQ (s, "__WARN__") && PL_warnhook) return sv_setsv (sv, PL_warnhook), 0; |
|
|
691 | } |
|
|
692 | |
|
|
693 | return orig_sigelem_get (aTHX_ sv, mg); |
|
|
694 | } |
|
|
695 | #endif |
|
|
696 | |
659 | static void |
697 | static void |
660 | coro_setup (pTHX_ struct coro *coro) |
698 | coro_setup (pTHX_ struct coro *coro) |
661 | { |
699 | { |
662 | /* |
700 | /* |
663 | * emulate part of the perl startup here. |
701 | * emulate part of the perl startup here. |
… | |
… | |
671 | PL_curpm = 0; |
709 | PL_curpm = 0; |
672 | PL_curpad = 0; |
710 | PL_curpad = 0; |
673 | PL_localizing = 0; |
711 | PL_localizing = 0; |
674 | PL_dirty = 0; |
712 | PL_dirty = 0; |
675 | PL_restartop = 0; |
713 | PL_restartop = 0; |
|
|
714 | |
|
|
715 | /* recreate the die/warn hooks */ |
|
|
716 | PL_diehook = 0; |
|
|
717 | hv_store (hv_sig, "__DIE__", sizeof ("__DIE__") - 1, newSV (0), 0); |
676 | PL_diehook = SvREFCNT_inc (sv_diehook); |
718 | PL_diehook = SvREFCNT_inc (rv_diehook); |
|
|
719 | |
|
|
720 | PL_warnhook = 0; |
|
|
721 | hv_store (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, newSV (0), 0); |
677 | PL_warnhook = SvREFCNT_inc (sv_warnhook); |
722 | PL_warnhook = SvREFCNT_inc (rv_warnhook); |
678 | |
723 | |
679 | GvSV (PL_defgv) = newSV (0); |
724 | GvSV (PL_defgv) = newSV (0); |
680 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
725 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
681 | GvSV (PL_errgv) = newSV (0); |
726 | GvSV (PL_errgv) = newSV (0); |
682 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
727 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
… | |
… | |
697 | PL_op = (OP *)&myop; |
742 | PL_op = (OP *)&myop; |
698 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
743 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
699 | SPAGAIN; |
744 | SPAGAIN; |
700 | } |
745 | } |
701 | |
746 | |
702 | ENTER; /* necessary e.g. for dounwind and to balance the xsub-entersub */ |
747 | /* this newly created coroutine might be run on an existing cctx which most |
|
|
748 | * likely was suspended in set_stacklevel, called from entersub. |
|
|
749 | * set_stacklevl doesn't do anything on return, but entersub does LEAVE, |
|
|
750 | * so we ENTER here for symmetry |
|
|
751 | */ |
|
|
752 | ENTER; |
703 | } |
753 | } |
704 | |
754 | |
705 | static void |
755 | static void |
706 | coro_destroy (pTHX_ struct coro *coro) |
756 | coro_destroy (pTHX_ struct coro *coro) |
707 | { |
757 | { |
… | |
… | |
874 | |
924 | |
875 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
925 | /* 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 */ |
926 | /* _cctx_init should be careful, as it could be called at almost any time */ |
877 | /* during execution of a perl program */ |
927 | /* during execution of a perl program */ |
878 | static void NOINLINE |
928 | static void NOINLINE |
879 | prepare_cctx (pTHX_ coro_cctx *cctx) |
929 | cctx_prepare (pTHX_ coro_cctx *cctx) |
880 | { |
930 | { |
881 | dSP; |
931 | dSP; |
882 | LOGOP myop; |
932 | LOGOP myop; |
883 | |
933 | |
884 | PL_top_env = &PL_start_env; |
934 | PL_top_env = &PL_start_env; |
… | |
… | |
902 | |
952 | |
903 | /* |
953 | /* |
904 | * this is a _very_ stripped down perl interpreter ;) |
954 | * this is a _very_ stripped down perl interpreter ;) |
905 | */ |
955 | */ |
906 | static void |
956 | static void |
907 | coro_run (void *arg) |
957 | cctx_run (void *arg) |
908 | { |
958 | { |
909 | dTHX; |
959 | dTHX; |
910 | |
960 | |
911 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
961 | /* cctx_run is the alternative tail of transfer(), so unlock here. */ |
912 | UNLOCK; |
962 | UNLOCK; |
913 | |
963 | |
914 | /* we now skip the entersub that lead to transfer() */ |
964 | /* we now skip the entersub that lead to transfer() */ |
915 | PL_op = PL_op->op_next; |
965 | PL_op = PL_op->op_next; |
916 | |
966 | |
917 | /* inject a fake subroutine call to cctx_init */ |
967 | /* inject a fake subroutine call to cctx_init */ |
918 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
968 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
919 | |
969 | |
920 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
970 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
921 | PL_restartop = PL_op; |
971 | PL_restartop = PL_op; |
922 | perl_run (PL_curinterp); |
972 | perl_run (PL_curinterp); |
923 | |
973 | |
… | |
… | |
942 | ++cctx_count; |
992 | ++cctx_count; |
943 | |
993 | |
944 | Newz (0, cctx, 1, coro_cctx); |
994 | Newz (0, cctx, 1, coro_cctx); |
945 | |
995 | |
946 | #if HAVE_MMAP |
996 | #if HAVE_MMAP |
947 | |
|
|
948 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
997 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
949 | /* mmap supposedly does allocate-on-write for us */ |
998 | /* 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); |
999 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
951 | |
1000 | |
952 | if (cctx->sptr != (void *)-1) |
1001 | if (cctx->sptr != (void *)-1) |
… | |
… | |
973 | stack_start = cctx->sptr; |
1022 | stack_start = cctx->sptr; |
974 | stack_size = cctx->ssize; |
1023 | stack_size = cctx->ssize; |
975 | } |
1024 | } |
976 | |
1025 | |
977 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
1026 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
978 | coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size); |
1027 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); |
979 | |
1028 | |
980 | return cctx; |
1029 | return cctx; |
981 | } |
1030 | } |
982 | |
1031 | |
983 | static void |
1032 | static void |
… | |
… | |
1071 | /* always use the TRANSFER macro */ |
1120 | /* always use the TRANSFER macro */ |
1072 | static void NOINLINE |
1121 | static void NOINLINE |
1073 | transfer (pTHX_ struct coro *prev, struct coro *next) |
1122 | transfer (pTHX_ struct coro *prev, struct coro *next) |
1074 | { |
1123 | { |
1075 | dSTACKLEVEL; |
1124 | dSTACKLEVEL; |
|
|
1125 | static volatile int has_throw; |
1076 | |
1126 | |
1077 | /* sometimes transfer is only called to set idle_sp */ |
1127 | /* sometimes transfer is only called to set idle_sp */ |
1078 | if (expect_false (!next)) |
1128 | if (expect_false (!next)) |
1079 | { |
1129 | { |
1080 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1130 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
… | |
… | |
1095 | prev->flags &= ~CF_RUNNING; |
1145 | prev->flags &= ~CF_RUNNING; |
1096 | next->flags |= CF_RUNNING; |
1146 | next->flags |= CF_RUNNING; |
1097 | |
1147 | |
1098 | LOCK; |
1148 | LOCK; |
1099 | |
1149 | |
|
|
1150 | /* first get rid of the old state */ |
|
|
1151 | save_perl (aTHX_ prev); |
|
|
1152 | |
1100 | if (expect_false (next->flags & CF_NEW)) |
1153 | if (expect_false (next->flags & CF_NEW)) |
1101 | { |
1154 | { |
1102 | /* need to start coroutine */ |
1155 | /* need to start coroutine */ |
1103 | next->flags &= ~CF_NEW; |
1156 | next->flags &= ~CF_NEW; |
1104 | /* first get rid of the old state */ |
|
|
1105 | save_perl (aTHX_ prev); |
|
|
1106 | /* setup coroutine call */ |
1157 | /* setup coroutine call */ |
1107 | coro_setup (aTHX_ next); |
1158 | coro_setup (aTHX_ next); |
1108 | } |
1159 | } |
1109 | else |
1160 | else |
1110 | { |
|
|
1111 | /* coroutine already started */ |
|
|
1112 | save_perl (aTHX_ prev); |
|
|
1113 | load_perl (aTHX_ next); |
1161 | load_perl (aTHX_ next); |
1114 | } |
|
|
1115 | |
1162 | |
1116 | prev__cctx = prev->cctx; |
1163 | prev__cctx = prev->cctx; |
1117 | |
1164 | |
1118 | /* possibly "free" the cctx */ |
1165 | /* possibly "free" the cctx */ |
1119 | if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))) |
1166 | if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))) |
… | |
… | |
1124 | prev->cctx = 0; |
1171 | prev->cctx = 0; |
1125 | |
1172 | |
1126 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1173 | /* 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 */ |
1174 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1128 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
1175 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
|
|
1176 | if (!next->cctx) |
1129 | next->cctx = cctx_get (aTHX); |
1177 | next->cctx = cctx_get (aTHX); |
1130 | |
1178 | |
1131 | cctx_put (prev__cctx); |
1179 | cctx_put (prev__cctx); |
1132 | } |
1180 | } |
1133 | |
1181 | |
1134 | ++next->usecount; |
1182 | ++next->usecount; |
1135 | |
1183 | |
1136 | if (expect_true (!next->cctx)) |
1184 | if (expect_true (!next->cctx)) |
1137 | next->cctx = cctx_get (aTHX); |
1185 | next->cctx = cctx_get (aTHX); |
|
|
1186 | |
|
|
1187 | has_throw = !!next->throw; |
1138 | |
1188 | |
1139 | if (expect_false (prev__cctx != next->cctx)) |
1189 | if (expect_false (prev__cctx != next->cctx)) |
1140 | { |
1190 | { |
1141 | prev__cctx->top_env = PL_top_env; |
1191 | prev__cctx->top_env = PL_top_env; |
1142 | PL_top_env = next->cctx->top_env; |
1192 | PL_top_env = next->cctx->top_env; |
… | |
… | |
1144 | } |
1194 | } |
1145 | |
1195 | |
1146 | free_coro_mortal (aTHX); |
1196 | free_coro_mortal (aTHX); |
1147 | UNLOCK; |
1197 | UNLOCK; |
1148 | |
1198 | |
1149 | if (expect_false (prev->throw || next->throw)) |
1199 | if (expect_false (has_throw)) |
1150 | { |
1200 | { |
1151 | struct coro *coro = SvSTATE (coro_current); |
1201 | struct coro *coro = SvSTATE (coro_current); |
1152 | |
1202 | |
1153 | if (coro->throw) |
1203 | if (coro->throw) |
1154 | { |
1204 | { |
… | |
… | |
1486 | #endif |
1536 | #endif |
1487 | BOOT_PAGESIZE; |
1537 | BOOT_PAGESIZE; |
1488 | |
1538 | |
1489 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1539 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1490 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1540 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1491 | |
1541 | #if 0 |
1492 | sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE); |
1542 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; |
1493 | sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE); |
1543 | PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
1494 | |
1544 | #endif |
1495 | if (!PL_diehook) PL_diehook = SvREFCNT_inc (sv_diehook); |
1545 | hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); |
1496 | if (!PL_warnhook) PL_warnhook = SvREFCNT_inc (sv_warnhook); |
1546 | rv_diehook = SvREFCNT_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
|
|
1547 | rv_warnhook = SvREFCNT_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
1497 | |
1548 | |
1498 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1549 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1499 | |
1550 | |
1500 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1551 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1501 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
1552 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
… | |
… | |
1507 | |
1558 | |
1508 | while (main_top_env->je_prev) |
1559 | while (main_top_env->je_prev) |
1509 | main_top_env = main_top_env->je_prev; |
1560 | main_top_env = main_top_env->je_prev; |
1510 | |
1561 | |
1511 | coroapi.ver = CORO_API_VERSION; |
1562 | coroapi.ver = CORO_API_VERSION; |
|
|
1563 | coroapi.rev = CORO_API_REVISION; |
1512 | coroapi.transfer = api_transfer; |
1564 | coroapi.transfer = api_transfer; |
1513 | |
1565 | |
1514 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1566 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1515 | } |
1567 | } |
1516 | |
1568 | |
1517 | SV * |
1569 | SV * |
1518 | new (char *klass, ...) |
1570 | new (char *klass, ...) |
1519 | CODE: |
1571 | CODE: |
1520 | { |
1572 | { |
1521 | struct coro *coro; |
1573 | struct coro *coro; |
|
|
1574 | MAGIC *mg; |
1522 | HV *hv; |
1575 | HV *hv; |
1523 | int i; |
1576 | int i; |
1524 | |
1577 | |
1525 | Newz (0, coro, 1, struct coro); |
1578 | Newz (0, coro, 1, struct coro); |
1526 | coro->args = newAV (); |
1579 | coro->args = newAV (); |
… | |
… | |
1529 | if (coro_first) coro_first->prev = coro; |
1582 | if (coro_first) coro_first->prev = coro; |
1530 | coro->next = coro_first; |
1583 | coro->next = coro_first; |
1531 | coro_first = coro; |
1584 | coro_first = coro; |
1532 | |
1585 | |
1533 | coro->hv = hv = newHV (); |
1586 | coro->hv = hv = newHV (); |
1534 | sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; |
1587 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
|
|
1588 | mg->mg_flags |= MGf_DUP; |
1535 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1589 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
1536 | |
1590 | |
1537 | av_extend (coro->args, items - 1); |
1591 | av_extend (coro->args, items - 1); |
1538 | for (i = 1; i < items; i++) |
1592 | for (i = 1; i < items; i++) |
1539 | av_push (coro->args, newSVsv (ST (i))); |
1593 | av_push (coro->args, newSVsv (ST (i))); |
… | |
… | |
1583 | |
1637 | |
1584 | break; |
1638 | break; |
1585 | } |
1639 | } |
1586 | |
1640 | |
1587 | BARRIER; |
1641 | BARRIER; |
|
|
1642 | PUTBACK; |
1588 | TRANSFER (ta); |
1643 | TRANSFER (ta); |
1589 | |
1644 | SPAGAIN; /* might be the sp of a different coroutine now */ |
1590 | if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev)) |
1645 | /* be extra careful not to ever do anything after TRANSFER */ |
1591 | XSRETURN_YES; |
|
|
1592 | } |
1646 | } |
1593 | |
1647 | |
1594 | bool |
1648 | bool |
1595 | _destroy (SV *coro_sv) |
1649 | _destroy (SV *coro_sv) |
1596 | CODE: |
1650 | CODE: |
… | |
… | |
1656 | |
1710 | |
1657 | { |
1711 | { |
1658 | dSP; |
1712 | dSP; |
1659 | ENTER; |
1713 | ENTER; |
1660 | SAVETMPS; |
1714 | SAVETMPS; |
|
|
1715 | PUTBACK; |
|
|
1716 | PUSHSTACK; |
1661 | PUSHMARK (SP); |
1717 | PUSHMARK (SP); |
1662 | PUTBACK; |
|
|
1663 | |
1718 | |
1664 | if (ix) |
1719 | if (ix) |
1665 | eval_sv (coderef, 0); |
1720 | eval_sv (coderef, 0); |
1666 | else |
1721 | else |
1667 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1722 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1668 | |
1723 | |
|
|
1724 | POPSTACK; |
1669 | SPAGAIN; |
1725 | SPAGAIN; |
1670 | FREETMPS; |
1726 | FREETMPS; |
1671 | LEAVE; |
1727 | LEAVE; |
1672 | PUTBACK; |
1728 | PUTBACK; |
1673 | } |
1729 | } |
… | |
… | |
1731 | |
1787 | |
1732 | BOOT: |
1788 | BOOT: |
1733 | { |
1789 | { |
1734 | int i; |
1790 | int i; |
1735 | |
1791 | |
1736 | sv_pool_rss = coro_get_sv ("Coro::POOL_RSS" , TRUE); |
1792 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
1737 | sv_pool_size = coro_get_sv ("Coro::POOL_SIZE" , TRUE); |
1793 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
1738 | av_async_pool = coro_get_av ("Coro::async_pool", TRUE); |
1794 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
1739 | |
1795 | |
1740 | coro_current = coro_get_sv ("Coro::current", FALSE); |
1796 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
1741 | SvREADONLY_on (coro_current); |
1797 | SvREADONLY_on (coro_current); |
1742 | |
1798 | |
1743 | coro_stash = gv_stashpv ("Coro", TRUE); |
1799 | coro_stash = gv_stashpv ("Coro", TRUE); |
1744 | |
1800 | |
1745 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
1801 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |