… | |
… | |
175 | static HV *coro_state_stash, *coro_stash; |
175 | static HV *coro_state_stash, *coro_stash; |
176 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
176 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
177 | |
177 | |
178 | static AV *av_destroy; /* destruction queue */ |
178 | static AV *av_destroy; /* destruction queue */ |
179 | static SV *sv_manager; /* the manager coro */ |
179 | static SV *sv_manager; /* the manager coro */ |
|
|
180 | static SV *sv_idle; /* $Coro::idle */ |
180 | |
181 | |
181 | static GV *irsgv; /* $/ */ |
182 | static GV *irsgv; /* $/ */ |
182 | static GV *stdoutgv; /* *STDOUT */ |
183 | static GV *stdoutgv; /* *STDOUT */ |
183 | static SV *rv_diehook; |
184 | static SV *rv_diehook; |
184 | static SV *rv_warnhook; |
185 | static SV *rv_warnhook; |
… | |
… | |
246 | { |
247 | { |
247 | SV *defsv; |
248 | SV *defsv; |
248 | AV *defav; |
249 | AV *defav; |
249 | SV *errsv; |
250 | SV *errsv; |
250 | SV *irsgv; |
251 | SV *irsgv; |
|
|
252 | HV *hinthv; |
251 | #define VAR(name,type) type name; |
253 | #define VAR(name,type) type name; |
252 | # include "state.h" |
254 | # include "state.h" |
253 | #undef VAR |
255 | #undef VAR |
254 | } perl_slots; |
256 | } perl_slots; |
255 | |
257 | |
… | |
… | |
384 | if (SvREFCNT (padlist)) |
386 | if (SvREFCNT (padlist)) |
385 | { |
387 | { |
386 | I32 i = AvFILLp (padlist); |
388 | I32 i = AvFILLp (padlist); |
387 | while (i >= 0) |
389 | while (i >= 0) |
388 | { |
390 | { |
389 | SV **svp = av_fetch (padlist, i--, FALSE); |
391 | SV **svp = AvARRAY (padlist)[i--]; |
390 | if (svp) |
392 | if (svp) |
391 | { |
393 | { |
392 | SV *sv; |
394 | AvREAL_on (*svp); |
393 | while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) |
|
|
394 | SvREFCNT_dec (sv); |
395 | av_undef (*svp); |
395 | |
|
|
396 | SvREFCNT_dec (*svp); |
396 | SvREFCNT_dec (*svp); |
397 | } |
397 | } |
398 | } |
398 | } |
399 | |
399 | |
400 | SvREFCNT_dec ((SV*)padlist); |
400 | SvREFCNT_dec ((SV*)padlist); |
… | |
… | |
501 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
501 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
502 | |
502 | |
503 | av = (AV *)mg->mg_obj; |
503 | av = (AV *)mg->mg_obj; |
504 | |
504 | |
505 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
505 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
506 | av_extend (av, AvMAX (av) + 1); |
506 | av_extend (av, AvFILLp (av) + 1); |
507 | |
507 | |
508 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
508 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
509 | } |
509 | } |
510 | |
510 | |
511 | /** load & save, init *******************************************************/ |
511 | /** load & save, init *******************************************************/ |
… | |
… | |
516 | perl_slots *slot = c->slot; |
516 | perl_slots *slot = c->slot; |
517 | c->slot = 0; |
517 | c->slot = 0; |
518 | |
518 | |
519 | PL_mainstack = c->mainstack; |
519 | PL_mainstack = c->mainstack; |
520 | |
520 | |
521 | GvSV (PL_defgv) = slot->defsv; |
521 | GvSV (PL_defgv) = slot->defsv; |
522 | GvAV (PL_defgv) = slot->defav; |
522 | GvAV (PL_defgv) = slot->defav; |
523 | GvSV (PL_errgv) = slot->errsv; |
523 | GvSV (PL_errgv) = slot->errsv; |
524 | GvSV (irsgv) = slot->irsgv; |
524 | GvSV (irsgv) = slot->irsgv; |
|
|
525 | GvHV (PL_hintgv) = slot->hinthv; |
525 | |
526 | |
526 | #define VAR(name,type) PL_ ## name = slot->name; |
527 | #define VAR(name,type) PL_ ## name = slot->name; |
527 | # include "state.h" |
528 | # include "state.h" |
528 | #undef VAR |
529 | #undef VAR |
529 | |
530 | |
… | |
… | |
615 | c->mainstack = PL_mainstack; |
616 | c->mainstack = PL_mainstack; |
616 | |
617 | |
617 | { |
618 | { |
618 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
619 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
619 | |
620 | |
620 | slot->defav = GvAV (PL_defgv); |
621 | slot->defav = GvAV (PL_defgv); |
621 | slot->defsv = DEFSV; |
622 | slot->defsv = DEFSV; |
622 | slot->errsv = ERRSV; |
623 | slot->errsv = ERRSV; |
623 | slot->irsgv = GvSV (irsgv); |
624 | slot->irsgv = GvSV (irsgv); |
|
|
625 | slot->hinthv = GvHV (PL_hintgv); |
624 | |
626 | |
625 | #define VAR(name,type) slot->name = PL_ ## name; |
627 | #define VAR(name,type) slot->name = PL_ ## name; |
626 | # include "state.h" |
628 | # include "state.h" |
627 | #undef VAR |
629 | #undef VAR |
628 | } |
630 | } |
… | |
… | |
864 | |
866 | |
865 | PL_runops = RUNOPS_DEFAULT; |
867 | PL_runops = RUNOPS_DEFAULT; |
866 | PL_curcop = &PL_compiling; |
868 | PL_curcop = &PL_compiling; |
867 | PL_in_eval = EVAL_NULL; |
869 | PL_in_eval = EVAL_NULL; |
868 | PL_comppad = 0; |
870 | PL_comppad = 0; |
|
|
871 | PL_comppad_name = 0; |
|
|
872 | PL_comppad_name_fill = 0; |
|
|
873 | PL_comppad_name_floor = 0; |
869 | PL_curpm = 0; |
874 | PL_curpm = 0; |
870 | PL_curpad = 0; |
875 | PL_curpad = 0; |
871 | PL_localizing = 0; |
876 | PL_localizing = 0; |
872 | PL_dirty = 0; |
877 | PL_dirty = 0; |
873 | PL_restartop = 0; |
878 | PL_restartop = 0; |
874 | #if PERL_VERSION_ATLEAST (5,10,0) |
879 | #if PERL_VERSION_ATLEAST (5,10,0) |
875 | PL_parser = 0; |
880 | PL_parser = 0; |
876 | #endif |
881 | #endif |
|
|
882 | PL_hints = 0; |
877 | |
883 | |
878 | /* recreate the die/warn hooks */ |
884 | /* recreate the die/warn hooks */ |
879 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
885 | PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); |
880 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
886 | PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); |
881 | |
887 | |
882 | GvSV (PL_defgv) = newSV (0); |
888 | GvSV (PL_defgv) = newSV (0); |
883 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
889 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
884 | GvSV (PL_errgv) = newSV (0); |
890 | GvSV (PL_errgv) = newSV (0); |
885 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
891 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
|
|
892 | GvHV (PL_hintgv) = 0; |
886 | PL_rs = newSVsv (GvSV (irsgv)); |
893 | PL_rs = newSVsv (GvSV (irsgv)); |
887 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
894 | PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); |
888 | |
895 | |
889 | { |
896 | { |
890 | dSP; |
897 | dSP; |
… | |
… | |
944 | SvREFCNT_dec (GvAV (PL_defgv)); |
951 | SvREFCNT_dec (GvAV (PL_defgv)); |
945 | SvREFCNT_dec (GvSV (PL_errgv)); |
952 | SvREFCNT_dec (GvSV (PL_errgv)); |
946 | SvREFCNT_dec (PL_defoutgv); |
953 | SvREFCNT_dec (PL_defoutgv); |
947 | SvREFCNT_dec (PL_rs); |
954 | SvREFCNT_dec (PL_rs); |
948 | SvREFCNT_dec (GvSV (irsgv)); |
955 | SvREFCNT_dec (GvSV (irsgv)); |
|
|
956 | SvREFCNT_dec (GvHV (PL_hintgv)); |
949 | |
957 | |
950 | SvREFCNT_dec (PL_diehook); |
958 | SvREFCNT_dec (PL_diehook); |
951 | SvREFCNT_dec (PL_warnhook); |
959 | SvREFCNT_dec (PL_warnhook); |
952 | |
960 | |
953 | SvREFCNT_dec (coro->saved_deffh); |
961 | SvREFCNT_dec (coro->saved_deffh); |
… | |
… | |
1168 | /* |
1176 | /* |
1169 | * If perl-run returns we assume exit() was being called or the coro |
1177 | * If perl-run returns we assume exit() was being called or the coro |
1170 | * fell off the end, which seems to be the only valid (non-bug) |
1178 | * fell off the end, which seems to be the only valid (non-bug) |
1171 | * reason for perl_run to return. We try to exit by jumping to the |
1179 | * reason for perl_run to return. We try to exit by jumping to the |
1172 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1180 | * bootstrap-time "top" top_env, as we cannot restore the "main" |
1173 | * coroutine as Coro has no such concept |
1181 | * coroutine as Coro has no such concept. |
|
|
1182 | * This actually isn't valid with the pthread backend, but OSes requiring |
|
|
1183 | * that backend are too broken to do it in a standards-compliant way. |
1174 | */ |
1184 | */ |
1175 | PL_top_env = main_top_env; |
1185 | PL_top_env = main_top_env; |
1176 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1186 | JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ |
1177 | } |
1187 | } |
1178 | } |
1188 | } |
… | |
… | |
1329 | /* TODO: throwing up here is considered harmful */ |
1339 | /* TODO: throwing up here is considered harmful */ |
1330 | |
1340 | |
1331 | if (expect_true (prev != next)) |
1341 | if (expect_true (prev != next)) |
1332 | { |
1342 | { |
1333 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1343 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1334 | croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); |
1344 | croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,"); |
1335 | |
1345 | |
1336 | if (expect_false (next->flags & CF_RUNNING)) |
1346 | if (expect_false (next->flags & CF_RUNNING)) |
1337 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
1347 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); |
1338 | |
1348 | |
1339 | if (expect_false (next->flags & CF_DESTROYED)) |
1349 | if (expect_false (next->flags & CF_DESTROYED)) |
… | |
… | |
1383 | coro_setup (aTHX_ next); |
1393 | coro_setup (aTHX_ next); |
1384 | } |
1394 | } |
1385 | else |
1395 | else |
1386 | load_perl (aTHX_ next); |
1396 | load_perl (aTHX_ next); |
1387 | |
1397 | |
|
|
1398 | assert (!prev->cctx);//D temporary |
|
|
1399 | |
1388 | /* possibly untie and reuse the cctx */ |
1400 | /* possibly untie and reuse the cctx */ |
1389 | if (expect_true ( |
1401 | if (expect_true ( |
1390 | cctx_current->idle_sp == STACKLEVEL |
1402 | cctx_current->idle_sp == STACKLEVEL |
1391 | && !(cctx_current->flags & CC_TRACE) |
1403 | && !(cctx_current->flags & CC_TRACE) |
1392 | && !force_cctx |
1404 | && !force_cctx |
1393 | )) |
1405 | )) |
1394 | { |
1406 | { |
1395 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1407 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1396 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); |
1408 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); |
1397 | |
1409 | |
1398 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ |
1410 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */ |
1399 | /* without this the next cctx_get might destroy the prev__cctx while still in use */ |
1411 | /* without this the next cctx_get might destroy the running cctx while still in use */ |
1400 | if (expect_false (CCTX_EXPIRED (cctx_current))) |
1412 | if (expect_false (CCTX_EXPIRED (cctx_current))) |
1401 | if (!next->cctx) |
1413 | if (expect_true (!next->cctx)) |
1402 | next->cctx = cctx_get (aTHX); |
1414 | next->cctx = cctx_get (aTHX); |
1403 | |
1415 | |
1404 | cctx_put (cctx_current); |
1416 | cctx_put (cctx_current); |
1405 | assert (!prev->cctx);//D temporary |
|
|
1406 | } |
1417 | } |
1407 | else |
1418 | else |
1408 | prev->cctx = cctx_current; |
1419 | prev->cctx = cctx_current; |
1409 | |
1420 | |
1410 | ++next->usecount; |
1421 | ++next->usecount; |
… | |
… | |
1581 | { |
1592 | { |
1582 | struct coro *coro; |
1593 | struct coro *coro; |
1583 | SV *sv_hook; |
1594 | SV *sv_hook; |
1584 | void (*xs_hook)(void); |
1595 | void (*xs_hook)(void); |
1585 | |
1596 | |
1586 | if (SvROK (coro_sv)) |
|
|
1587 | coro_sv = SvRV (coro_sv); |
|
|
1588 | |
|
|
1589 | coro = SvSTATE (coro_sv); |
1597 | coro = SvSTATE (coro_sv); |
1590 | |
1598 | |
1591 | if (coro->flags & CF_READY) |
1599 | if (coro->flags & CF_READY) |
1592 | return 0; |
1600 | return 0; |
1593 | |
1601 | |
… | |
… | |
1667 | } |
1675 | } |
1668 | } |
1676 | } |
1669 | else |
1677 | else |
1670 | { |
1678 | { |
1671 | /* nothing to schedule: call the idle handler */ |
1679 | /* nothing to schedule: call the idle handler */ |
|
|
1680 | if (SvROK (sv_idle) |
|
|
1681 | && SvOBJECT (SvRV (sv_idle))) |
|
|
1682 | { |
|
|
1683 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
|
|
1684 | api_ready (aTHX_ SvRV (sv_idle)); |
|
|
1685 | --coro_nready; |
|
|
1686 | } |
|
|
1687 | else |
|
|
1688 | { |
1672 | dSP; |
1689 | dSP; |
1673 | |
1690 | |
1674 | ENTER; |
1691 | ENTER; |
1675 | SAVETMPS; |
1692 | SAVETMPS; |
1676 | |
1693 | |
1677 | PUSHMARK (SP); |
1694 | PUSHMARK (SP); |
1678 | PUTBACK; |
1695 | PUTBACK; |
1679 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
1696 | call_sv (sv_idle, G_VOID | G_DISCARD); |
1680 | |
1697 | |
1681 | FREETMPS; |
1698 | FREETMPS; |
1682 | LEAVE; |
1699 | LEAVE; |
|
|
1700 | } |
1683 | } |
1701 | } |
1684 | } |
1702 | } |
1685 | } |
1703 | } |
1686 | |
1704 | |
1687 | INLINE void |
1705 | INLINE void |
… | |
… | |
1758 | static void |
1776 | static void |
1759 | api_trace (pTHX_ SV *coro_sv, int flags) |
1777 | api_trace (pTHX_ SV *coro_sv, int flags) |
1760 | { |
1778 | { |
1761 | struct coro *coro = SvSTATE (coro_sv); |
1779 | struct coro *coro = SvSTATE (coro_sv); |
1762 | |
1780 | |
|
|
1781 | if (coro->flags & CF_RUNNING) |
|
|
1782 | croak ("cannot enable tracing on a running coroutine, caught"); |
|
|
1783 | |
1763 | if (flags & CC_TRACE) |
1784 | if (flags & CC_TRACE) |
1764 | { |
1785 | { |
1765 | if (!coro->cctx) |
1786 | if (!coro->cctx) |
1766 | coro->cctx = cctx_new_run (); |
1787 | coro->cctx = cctx_new_run (); |
1767 | else if (!(coro->cctx->flags & CC_TRACE)) |
1788 | else if (!(coro->cctx->flags & CC_TRACE)) |
1768 | croak ("cannot enable tracing on coroutine with custom stack,"); |
1789 | croak ("cannot enable tracing on coroutine with custom stack, caught"); |
1769 | |
1790 | |
1770 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1791 | coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); |
1771 | } |
1792 | } |
1772 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1793 | else if (coro->cctx && coro->cctx->flags & CC_TRACE) |
1773 | { |
1794 | { |
… | |
… | |
2347 | SV **ary; |
2368 | SV **ary; |
2348 | |
2369 | |
2349 | /* unfortunately, building manually saves memory */ |
2370 | /* unfortunately, building manually saves memory */ |
2350 | Newx (ary, 2, SV *); |
2371 | Newx (ary, 2, SV *); |
2351 | AvALLOC (av) = ary; |
2372 | AvALLOC (av) = ary; |
|
|
2373 | #if PERL_VERSION_ATLEAST (5,10,0) |
2352 | /*AvARRAY (av) = ary;*/ |
2374 | AvARRAY (av) = ary; |
|
|
2375 | #else |
2353 | SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ |
2376 | /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */ |
|
|
2377 | /* -DDEBUGGING flags this as a bug, despite it perfectly working */ |
|
|
2378 | SvPVX ((SV *)av) = (char *)ary; |
|
|
2379 | #endif |
2354 | AvMAX (av) = 1; |
2380 | AvMAX (av) = 1; |
2355 | AvFILLp (av) = 0; |
2381 | AvFILLp (av) = 0; |
2356 | ary [0] = newSViv (count); |
2382 | ary [0] = newSViv (count); |
2357 | |
2383 | |
2358 | return newRV_noinc ((SV *)av); |
2384 | return newRV_noinc ((SV *)av); |
… | |
… | |
3094 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3120 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
3095 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3121 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
3096 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3122 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
3097 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3123 | av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); |
3098 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
3124 | sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); |
|
|
3125 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3099 | |
3126 | |
3100 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3127 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3101 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3128 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3102 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3129 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3103 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
3130 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |