… | |
… | |
145 | static size_t coro_stacksize = CORO_STACKSIZE; |
145 | static size_t coro_stacksize = CORO_STACKSIZE; |
146 | static struct CoroAPI coroapi; |
146 | static struct CoroAPI coroapi; |
147 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
147 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
148 | static JMPENV *main_top_env; |
148 | static JMPENV *main_top_env; |
149 | static HV *coro_state_stash, *coro_stash; |
149 | static HV *coro_state_stash, *coro_stash; |
150 | static SV *coro_mortal; /* will be freed after next transfer */ |
150 | static volatile SV *coro_mortal; /* will be freed after next transfer */ |
151 | |
151 | |
152 | static GV *irsgv; /* $/ */ |
152 | static GV *irsgv; /* $/ */ |
153 | static GV *stdoutgv; /* *STDOUT */ |
153 | static GV *stdoutgv; /* *STDOUT */ |
154 | static SV *rv_diehook; |
154 | static SV *rv_diehook; |
155 | static SV *rv_warnhook; |
155 | static SV *rv_warnhook; |
… | |
… | |
664 | return rss; |
664 | return rss; |
665 | } |
665 | } |
666 | |
666 | |
667 | /** coroutine stack handling ************************************************/ |
667 | /** coroutine stack handling ************************************************/ |
668 | |
668 | |
|
|
669 | #if 0 |
669 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
670 | static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); |
670 | |
671 | |
671 | /* |
672 | /* |
672 | * This overrides the default magic get method of %SIG elements. |
673 | * This overrides the default magic get method of %SIG elements. |
673 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
674 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
… | |
… | |
678 | * when a schedule happens then, but we ignore this. |
679 | * when a schedule happens then, but we ignore this. |
679 | */ |
680 | */ |
680 | static int |
681 | static int |
681 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
682 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
682 | { |
683 | { |
|
|
684 | return orig_sigelem_get (aTHX_ sv, mg); |
683 | const char *s = MgPV_nolen_const (mg); |
685 | const char *s = MgPV_nolen_const (mg); |
684 | |
686 | |
685 | if (*s == '_') |
687 | if (*s == '_') |
686 | { |
688 | { |
687 | if (strEQ (s, "__DIE__" ) && PL_diehook ) return sv_setsv (sv, PL_diehook ), 0; |
689 | if (strEQ (s, "__DIE__" ) && PL_diehook ) return sv_setsv (sv, PL_diehook ), 0; |
688 | if (strEQ (s, "__WARN__") && PL_warnhook) return sv_setsv (sv, PL_warnhook), 0; |
690 | if (strEQ (s, "__WARN__") && PL_warnhook) return sv_setsv (sv, PL_warnhook), 0; |
689 | } |
691 | } |
690 | |
692 | |
691 | return orig_sigelem_get (aTHX_ sv, mg); |
693 | return orig_sigelem_get (aTHX_ sv, mg); |
692 | } |
694 | } |
|
|
695 | #endif |
693 | |
696 | |
694 | static void |
697 | static void |
695 | coro_setup (pTHX_ struct coro *coro) |
698 | coro_setup (pTHX_ struct coro *coro) |
696 | { |
699 | { |
697 | /* |
700 | /* |
… | |
… | |
1117 | /* always use the TRANSFER macro */ |
1120 | /* always use the TRANSFER macro */ |
1118 | static void NOINLINE |
1121 | static void NOINLINE |
1119 | transfer (pTHX_ struct coro *prev, struct coro *next) |
1122 | transfer (pTHX_ struct coro *prev, struct coro *next) |
1120 | { |
1123 | { |
1121 | dSTACKLEVEL; |
1124 | dSTACKLEVEL; |
|
|
1125 | static volatile int has_throw; |
1122 | |
1126 | |
1123 | /* sometimes transfer is only called to set idle_sp */ |
1127 | /* sometimes transfer is only called to set idle_sp */ |
1124 | if (expect_false (!next)) |
1128 | if (expect_false (!next)) |
1125 | { |
1129 | { |
1126 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
1130 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
… | |
… | |
1178 | ++next->usecount; |
1182 | ++next->usecount; |
1179 | |
1183 | |
1180 | if (expect_true (!next->cctx)) |
1184 | if (expect_true (!next->cctx)) |
1181 | next->cctx = cctx_get (aTHX); |
1185 | next->cctx = cctx_get (aTHX); |
1182 | |
1186 | |
|
|
1187 | has_throw = !!next->throw; |
|
|
1188 | |
1183 | if (expect_false (prev__cctx != next->cctx)) |
1189 | if (expect_false (prev__cctx != next->cctx)) |
1184 | { |
1190 | { |
1185 | prev__cctx->top_env = PL_top_env; |
1191 | prev__cctx->top_env = PL_top_env; |
1186 | PL_top_env = next->cctx->top_env; |
1192 | PL_top_env = next->cctx->top_env; |
1187 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1193 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
1188 | } |
1194 | } |
1189 | |
1195 | |
1190 | free_coro_mortal (aTHX); |
1196 | free_coro_mortal (aTHX); |
1191 | UNLOCK; |
1197 | UNLOCK; |
1192 | |
1198 | |
1193 | if (expect_false (prev->throw || next->throw)) |
1199 | if (expect_false (has_throw)) |
1194 | { |
1200 | { |
1195 | struct coro *coro = SvSTATE (coro_current); |
1201 | struct coro *coro = SvSTATE (coro_current); |
1196 | |
1202 | |
1197 | if (coro->throw) |
1203 | if (coro->throw) |
1198 | { |
1204 | { |
… | |
… | |
1324 | { |
1330 | { |
1325 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
1331 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
1326 | } |
1332 | } |
1327 | |
1333 | |
1328 | static SV * |
1334 | static SV * |
1329 | coro_deq (pTHX_ int min_prio) |
1335 | coro_deq (pTHX) |
1330 | { |
1336 | { |
1331 | int prio = PRIO_MAX - PRIO_MIN; |
1337 | int prio; |
1332 | |
1338 | |
1333 | min_prio -= PRIO_MIN; |
|
|
1334 | if (min_prio < 0) |
|
|
1335 | min_prio = 0; |
|
|
1336 | |
|
|
1337 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) |
1339 | for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) |
1338 | if (AvFILLp (coro_ready [prio]) >= 0) |
1340 | if (AvFILLp (coro_ready [prio]) >= 0) |
1339 | return av_shift (coro_ready [prio]); |
1341 | return av_shift (coro_ready [prio]); |
1340 | |
1342 | |
1341 | return 0; |
1343 | return 0; |
1342 | } |
1344 | } |
… | |
… | |
1378 | SV *prev_sv, *next_sv; |
1380 | SV *prev_sv, *next_sv; |
1379 | |
1381 | |
1380 | for (;;) |
1382 | for (;;) |
1381 | { |
1383 | { |
1382 | LOCK; |
1384 | LOCK; |
1383 | next_sv = coro_deq (aTHX_ PRIO_MIN); |
1385 | next_sv = coro_deq (aTHX); |
1384 | |
1386 | |
1385 | /* nothing to schedule: call the idle handler */ |
1387 | /* nothing to schedule: call the idle handler */ |
1386 | if (expect_false (!next_sv)) |
1388 | if (expect_false (!next_sv)) |
1387 | { |
1389 | { |
1388 | dSP; |
1390 | dSP; |
… | |
… | |
1530 | #endif |
1532 | #endif |
1531 | BOOT_PAGESIZE; |
1533 | BOOT_PAGESIZE; |
1532 | |
1534 | |
1533 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1535 | irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); |
1534 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1536 | stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); |
1535 | |
1537 | #if 0 |
1536 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; |
1538 | orig_sigelem_get = PL_vtbl_sigelem.svt_get; |
1537 | PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
1539 | PL_vtbl_sigelem.svt_get = coro_sigelem_get; |
1538 | |
1540 | #endif |
1539 | hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); |
1541 | hv_sig = coro_get_hv (aTHX_ "SIG", TRUE); |
1540 | rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
1542 | rv_diehook = SvREFCNT_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV)); |
1541 | rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
1543 | rv_warnhook = SvREFCNT_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV)); |
1542 | |
1544 | |
1543 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1545 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1544 | |
1546 | |
1545 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1547 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1546 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
1548 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |