… | |
… | |
221 | |
221 | |
222 | /* process data */ |
222 | /* process data */ |
223 | AV *mainstack; |
223 | AV *mainstack; |
224 | perl_slots *slot; /* basically the saved sp */ |
224 | perl_slots *slot; /* basically the saved sp */ |
225 | |
225 | |
226 | /* data associated with this coroutine (initial args) */ |
226 | AV *args; /* data associated with this coroutine (initial args) */ |
227 | AV *args; |
227 | int refcnt; /* coroutines are refcounted, yes */ |
228 | int refcnt; |
|
|
229 | int flags; /* CF_ flags */ |
228 | int flags; /* CF_ flags */ |
|
|
229 | HV *hv; /* the perl hash associated with this coro, if any */ |
230 | |
230 | |
231 | /* statistics */ |
231 | /* statistics */ |
232 | int usecount; /* number of transfers to this coro */ |
232 | int usecount; /* number of transfers to this coro */ |
233 | |
233 | |
234 | /* coro process data */ |
234 | /* coro process data */ |
235 | int prio; |
235 | int prio; |
236 | SV *throw; |
236 | SV *throw; /* exception to be thrown */ |
237 | |
237 | |
238 | /* async_pool */ |
238 | /* async_pool */ |
239 | SV *saved_deffh; |
239 | SV *saved_deffh; |
240 | |
240 | |
241 | /* linked list */ |
241 | /* linked list */ |
242 | struct coro *next, *prev; |
242 | struct coro *next, *prev; |
243 | HV *hv; /* the perl hash associated with this coro, if any */ |
|
|
244 | }; |
243 | }; |
245 | |
244 | |
246 | typedef struct coro *Coro__State; |
245 | typedef struct coro *Coro__State; |
247 | typedef struct coro *Coro__State_or_hashref; |
246 | typedef struct coro *Coro__State_or_hashref; |
248 | |
247 | |
… | |
… | |
453 | |
452 | |
454 | #define VAR(name,type) PL_ ## name = slot->name; |
453 | #define VAR(name,type) PL_ ## name = slot->name; |
455 | # include "state.h" |
454 | # include "state.h" |
456 | #undef VAR |
455 | #undef VAR |
457 | |
456 | |
|
|
457 | /*hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0);*/ |
|
|
458 | /*hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0);*/ |
|
|
459 | |
458 | { |
460 | { |
459 | dSP; |
461 | dSP; |
460 | |
462 | |
461 | CV *cv; |
463 | CV *cv; |
462 | |
464 | |
… | |
… | |
521 | |
523 | |
522 | PUTBACK; |
524 | PUTBACK; |
523 | } |
525 | } |
524 | |
526 | |
525 | /* allocate some space on the context stack for our purposes */ |
527 | /* allocate some space on the context stack for our purposes */ |
|
|
528 | /* we manually unroll here, as usually 2 slots is enough */ |
|
|
529 | if (SLOT_COUNT >= 1) CXINC; |
|
|
530 | if (SLOT_COUNT >= 2) CXINC; |
|
|
531 | if (SLOT_COUNT >= 3) CXINC; |
526 | { |
532 | { |
527 | /* we manually unroll here, as usually 2 slots is enough */ |
|
|
528 | int i; |
533 | 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) |
534 | for (i = 3; i < SLOT_COUNT; ++i) |
533 | CXINC; |
535 | CXINC; |
534 | |
|
|
535 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
|
|
536 | } |
536 | } |
|
|
537 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
537 | |
538 | |
538 | c->mainstack = PL_mainstack; |
539 | c->mainstack = PL_mainstack; |
539 | |
540 | |
540 | { |
541 | { |
541 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
542 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
… | |
… | |
685 | PL_curpm = 0; |
686 | PL_curpm = 0; |
686 | PL_curpad = 0; |
687 | PL_curpad = 0; |
687 | PL_localizing = 0; |
688 | PL_localizing = 0; |
688 | PL_dirty = 0; |
689 | PL_dirty = 0; |
689 | PL_restartop = 0; |
690 | PL_restartop = 0; |
690 | SvREFCNT_inc (PL_diehook ); hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0); |
691 | 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); |
692 | PL_warnhook = 0; hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0); |
692 | |
693 | |
693 | GvSV (PL_defgv) = newSV (0); |
694 | GvSV (PL_defgv) = newSV (0); |
694 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
695 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
695 | GvSV (PL_errgv) = newSV (0); |
696 | GvSV (PL_errgv) = newSV (0); |
696 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
697 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
… | |
… | |
888 | |
889 | |
889 | /* inject a fake call to Coro::State::_cctx_init into the execution */ |
890 | /* 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 */ |
891 | /* _cctx_init should be careful, as it could be called at almost any time */ |
891 | /* during execution of a perl program */ |
892 | /* during execution of a perl program */ |
892 | static void NOINLINE |
893 | static void NOINLINE |
893 | prepare_cctx (pTHX_ coro_cctx *cctx) |
894 | cctx_prepare (pTHX_ coro_cctx *cctx) |
894 | { |
895 | { |
895 | dSP; |
896 | dSP; |
896 | LOGOP myop; |
897 | LOGOP myop; |
897 | |
898 | |
898 | PL_top_env = &PL_start_env; |
899 | PL_top_env = &PL_start_env; |
… | |
… | |
916 | |
917 | |
917 | /* |
918 | /* |
918 | * this is a _very_ stripped down perl interpreter ;) |
919 | * this is a _very_ stripped down perl interpreter ;) |
919 | */ |
920 | */ |
920 | static void |
921 | static void |
921 | coro_run (void *arg) |
922 | cctx_run (void *arg) |
922 | { |
923 | { |
923 | dTHX; |
924 | dTHX; |
924 | |
925 | |
925 | /* coro_run is the alternative tail of transfer(), so unlock here. */ |
926 | /* cctx_run is the alternative tail of transfer(), so unlock here. */ |
926 | UNLOCK; |
927 | UNLOCK; |
927 | |
928 | |
928 | /* we now skip the entersub that lead to transfer() */ |
929 | /* we now skip the entersub that lead to transfer() */ |
929 | PL_op = PL_op->op_next; |
930 | PL_op = PL_op->op_next; |
930 | |
931 | |
931 | /* inject a fake subroutine call to cctx_init */ |
932 | /* inject a fake subroutine call to cctx_init */ |
932 | prepare_cctx (aTHX_ (coro_cctx *)arg); |
933 | cctx_prepare (aTHX_ (coro_cctx *)arg); |
933 | |
934 | |
934 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
935 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
935 | PL_restartop = PL_op; |
936 | PL_restartop = PL_op; |
936 | perl_run (PL_curinterp); |
937 | perl_run (PL_curinterp); |
937 | |
938 | |
… | |
… | |
956 | ++cctx_count; |
957 | ++cctx_count; |
957 | |
958 | |
958 | Newz (0, cctx, 1, coro_cctx); |
959 | Newz (0, cctx, 1, coro_cctx); |
959 | |
960 | |
960 | #if HAVE_MMAP |
961 | #if HAVE_MMAP |
961 | |
|
|
962 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
962 | cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; |
963 | /* mmap supposedly does allocate-on-write for us */ |
963 | /* 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); |
964 | cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
965 | |
965 | |
966 | if (cctx->sptr != (void *)-1) |
966 | if (cctx->sptr != (void *)-1) |
… | |
… | |
987 | stack_start = cctx->sptr; |
987 | stack_start = cctx->sptr; |
988 | stack_size = cctx->ssize; |
988 | stack_size = cctx->ssize; |
989 | } |
989 | } |
990 | |
990 | |
991 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
991 | REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); |
992 | coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size); |
992 | coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); |
993 | |
993 | |
994 | return cctx; |
994 | return cctx; |
995 | } |
995 | } |
996 | |
996 | |
997 | static void |
997 | static void |
… | |
… | |
1138 | prev->cctx = 0; |
1138 | prev->cctx = 0; |
1139 | |
1139 | |
1140 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1140 | /* 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 */ |
1141 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1142 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
1142 | if (expect_false (CCTX_EXPIRED (prev__cctx))) |
|
|
1143 | if (!next->cctx) |
1143 | next->cctx = cctx_get (aTHX); |
1144 | next->cctx = cctx_get (aTHX); |
1144 | |
1145 | |
1145 | cctx_put (prev__cctx); |
1146 | cctx_put (prev__cctx); |
1146 | } |
1147 | } |
1147 | |
1148 | |
1148 | ++next->usecount; |
1149 | ++next->usecount; |