… | |
… | |
453 | |
453 | |
454 | #define VAR(name,type) PL_ ## name = slot->name; |
454 | #define VAR(name,type) PL_ ## name = slot->name; |
455 | # include "state.h" |
455 | # include "state.h" |
456 | #undef VAR |
456 | #undef VAR |
457 | |
457 | |
|
|
458 | /*hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0);*/ |
|
|
459 | /*hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0);*/ |
|
|
460 | |
458 | { |
461 | { |
459 | dSP; |
462 | dSP; |
460 | |
463 | |
461 | CV *cv; |
464 | CV *cv; |
462 | |
465 | |
… | |
… | |
521 | |
524 | |
522 | PUTBACK; |
525 | PUTBACK; |
523 | } |
526 | } |
524 | |
527 | |
525 | /* allocate some space on the context stack for our purposes */ |
528 | /* allocate some space on the context stack for our purposes */ |
|
|
529 | /* we manually unroll here, as usually 2 slots is enough */ |
|
|
530 | if (SLOT_COUNT >= 1) CXINC; |
|
|
531 | if (SLOT_COUNT >= 2) CXINC; |
|
|
532 | if (SLOT_COUNT >= 3) CXINC; |
526 | { |
533 | { |
527 | /* we manually unroll here, as usually 2 slots is enough */ |
|
|
528 | int i; |
534 | int i; |
529 | if (SLOT_COUNT >= 1) CXINC; |
|
|
530 | if (SLOT_COUNT >= 2) CXINC; |
|
|
531 | if (SLOT_COUNT >= 3) CXINC; |
|
|
532 | for (i = 3; i < SLOT_COUNT; ++i) |
535 | for (i = 3; i < SLOT_COUNT; ++i) |
533 | CXINC; |
536 | CXINC; |
534 | |
|
|
535 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
|
|
536 | } |
537 | } |
|
|
538 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
537 | |
539 | |
538 | c->mainstack = PL_mainstack; |
540 | c->mainstack = PL_mainstack; |
539 | |
541 | |
540 | { |
542 | { |
541 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
543 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
… | |
… | |
555 | * allocate various perl stacks. This is an exact copy |
557 | * allocate various perl stacks. This is an exact copy |
556 | * of perl.c:init_stacks, except that it uses less memory |
558 | * of perl.c:init_stacks, except that it uses less memory |
557 | * on the (sometimes correct) assumption that coroutines do |
559 | * on the (sometimes correct) assumption that coroutines do |
558 | * not usually need a lot of stackspace. |
560 | * not usually need a lot of stackspace. |
559 | */ |
561 | */ |
560 | #if CORO_PREFER_PERL_FUNCTIONS |
562 | #if 1 |
561 | # define coro_init_stacks init_stacks |
563 | # define coro_init_stacks init_stacks |
562 | #else |
564 | #else |
563 | static void |
565 | static void |
564 | coro_init_stacks (pTHX) |
566 | coro_init_stacks (pTHX) |
565 | { |
567 | { |
… | |
… | |
685 | PL_curpm = 0; |
687 | PL_curpm = 0; |
686 | PL_curpad = 0; |
688 | PL_curpad = 0; |
687 | PL_localizing = 0; |
689 | PL_localizing = 0; |
688 | PL_dirty = 0; |
690 | PL_dirty = 0; |
689 | PL_restartop = 0; |
691 | PL_restartop = 0; |
690 | SvREFCNT_inc (PL_diehook ); hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0); |
692 | PL_diehook = 0; hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0); |
691 | SvREFCNT_inc (PL_warnhook); hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0); |
693 | PL_warnhook = 0; hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0); |
692 | |
694 | |
693 | GvSV (PL_defgv) = newSV (0); |
695 | GvSV (PL_defgv) = newSV (0); |
694 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
696 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
695 | GvSV (PL_errgv) = newSV (0); |
697 | GvSV (PL_errgv) = newSV (0); |
696 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
698 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
… | |
… | |
888 | |
890 | |
889 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
891 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
890 | /* _cctx_init should be careful, as it could be called at almost any time */ |
892 | /* _cctx_init should be careful, as it could be called at almost any time */ |
891 | /* during execution of a perl program */ |
893 | /* during execution of a perl program */ |
892 | static void NOINLINE |
894 | static void NOINLINE |
893 | prepare_cctx (pTHX_ coro_cctx *cctx) |
895 | cctx_prepare (pTHX_ coro_cctx *cctx) |
894 | { |
896 | { |
895 | dSP; |
897 | dSP; |
896 | LOGOP myop; |
898 | LOGOP myop; |
897 | |
899 | |
898 | PL_top_env = &PL_start_env; |
900 | PL_top_env = &PL_start_env; |
… | |
… | |
916 | |
918 | |
917 | /* |
919 | /* |
918 | * this is a _very_ stripped down perl interpreter ;) |
920 | * this is a _very_ stripped down perl interpreter ;) |
919 | */ |
921 | */ |
920 | static void |
922 | static void |
921 | coro_run (void *arg) |
923 | cctx_run (void *arg) |
922 | { |
924 | { |
923 | dTHX; |
925 | dTHX; |
924 | |
926 | |
925 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
927 | /* cctx_run is the alternative tail of transfer(), so unlock here. */ |
926 | UNLOCK; |
928 | UNLOCK; |
927 | |
929 | |
928 | /* we now skip the entersub that lead to transfer() */ |
930 | /* we now skip the entersub that lead to transfer() */ |
929 | PL_op = PL_op->op_next; |
931 | PL_op = PL_op->op_next; |
930 | |
932 | |
931 | /* inject a fake subroutine call to cctx_init */ |
933 | /* inject a fake subroutine call to cctx_init */ |
932 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
934 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
933 | |
935 | |
934 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
936 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
935 | PL_restartop = PL_op; |
937 | PL_restartop = PL_op; |
936 | perl_run (PL_curinterp); |
938 | perl_run (PL_curinterp); |
937 | |
939 | |
… | |
… | |
956 | ++cctx_count; |
958 | ++cctx_count; |
957 | |
959 | |
958 | Newz (0, cctx, 1, coro_cctx); |
960 | Newz (0, cctx, 1, coro_cctx); |
959 | |
961 | |
960 | #if HAVE_MMAP |
962 | #if HAVE_MMAP |
961 | |
|
|
962 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
963 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
963 | /* mmap supposedly does allocate-on-write for us */ |
964 | /* mmap supposedly does allocate-on-write for us */ |
964 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
965 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
965 | |
966 | |
966 | if (cctx->sptr != (void *)-1) |
967 | if (cctx->sptr != (void *)-1) |
… | |
… | |
987 | stack_start = cctx->sptr; |
988 | stack_start = cctx->sptr; |
988 | stack_size = cctx->ssize; |
989 | stack_size = cctx->ssize; |
989 | } |
990 | } |
990 | |
991 | |
991 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
992 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
992 | coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size); |
993 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); |
993 | |
994 | |
994 | return cctx; |
995 | return cctx; |
995 | } |
996 | } |
996 | |
997 | |
997 | static void |
998 | static void |
… | |
… | |
1138 | prev->cctx = 0; |
1139 | prev->cctx = 0; |
1139 | |
1140 | |
1140 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1141 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1141 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1142 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1142 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
1143 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
|
|
1144 | if (!next->cctx) |
1143 | next->cctx = cctx_get (aTHX); |
1145 | next->cctx = cctx_get (aTHX); |
1144 | |
1146 | |
1145 | cctx_put (prev__cctx); |
1147 | cctx_put (prev__cctx); |
1146 | } |
1148 | } |
1147 | |
1149 | |
1148 | ++next->usecount; |
1150 | ++next->usecount; |