… | |
… | |
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 |
… | |
… | |
130 | #else |
131 | #else |
131 | # define LOCK (void)0 |
132 | # define LOCK (void)0 |
132 | # define UNLOCK (void)0 |
133 | # define UNLOCK (void)0 |
133 | #endif |
134 | #endif |
134 | |
135 | |
135 | #define strpair(const) const, sizeof (const) - 1 |
|
|
136 | |
|
|
137 | /* helper storage struct for Coro::AIO */ |
136 | /* helper storage struct for Coro::AIO */ |
138 | struct io_state |
137 | struct io_state |
139 | { |
138 | { |
140 | int errorno; |
139 | int errorno; |
141 | I32 laststype; |
140 | I32 laststype; |
… | |
… | |
221 | |
220 | |
222 | /* process data */ |
221 | /* process data */ |
223 | AV *mainstack; |
222 | AV *mainstack; |
224 | perl_slots *slot; /* basically the saved sp */ |
223 | perl_slots *slot; /* basically the saved sp */ |
225 | |
224 | |
226 | /* data associated with this coroutine (initial args) */ |
225 | AV *args; /* data associated with this coroutine (initial args) */ |
227 | AV *args; |
226 | int refcnt; /* coroutines are refcounted, yes */ |
228 | int refcnt; |
|
|
229 | int flags; /* CF_ flags */ |
227 | int flags; /* CF_ flags */ |
|
|
228 | HV *hv; /* the perl hash associated with this coro, if any */ |
230 | |
229 | |
231 | /* statistics */ |
230 | /* statistics */ |
232 | int usecount; /* number of transfers to this coro */ |
231 | int usecount; /* number of transfers to this coro */ |
233 | |
232 | |
234 | /* coro process data */ |
233 | /* coro process data */ |
235 | int prio; |
234 | int prio; |
236 | SV *throw; |
235 | SV *throw; /* exception to be thrown */ |
237 | |
236 | |
238 | /* async_pool */ |
237 | /* async_pool */ |
239 | SV *saved_deffh; |
238 | SV *saved_deffh; |
240 | |
239 | |
241 | /* linked list */ |
240 | /* linked list */ |
242 | struct coro *next, *prev; |
241 | struct coro *next, *prev; |
243 | HV *hv; /* the perl hash associated with this coro, if any */ |
|
|
244 | }; |
242 | }; |
245 | |
243 | |
246 | typedef struct coro *Coro__State; |
244 | typedef struct coro *Coro__State; |
247 | typedef struct coro *Coro__State_or_hashref; |
245 | typedef struct coro *Coro__State_or_hashref; |
248 | |
246 | |
… | |
… | |
453 | |
451 | |
454 | #define VAR(name,type) PL_ ## name = slot->name; |
452 | #define VAR(name,type) PL_ ## name = slot->name; |
455 | # include "state.h" |
453 | # include "state.h" |
456 | #undef VAR |
454 | #undef VAR |
457 | |
455 | |
|
|
456 | /*hv_store (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, SvREFCNT_inc (sv_diehook ), 0);*/ |
|
|
457 | /*hv_store (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, SvREFCNT_inc (sv_warnhook), 0);*/ |
|
|
458 | |
458 | { |
459 | { |
459 | dSP; |
460 | dSP; |
460 | |
461 | |
461 | CV *cv; |
462 | CV *cv; |
462 | |
463 | |
… | |
… | |
521 | |
522 | |
522 | PUTBACK; |
523 | PUTBACK; |
523 | } |
524 | } |
524 | |
525 | |
525 | /* allocate some space on the context stack for our purposes */ |
526 | /* allocate some space on the context stack for our purposes */ |
|
|
527 | /* we manually unroll here, as usually 2 slots is enough */ |
|
|
528 | if (SLOT_COUNT >= 1) CXINC; |
|
|
529 | if (SLOT_COUNT >= 2) CXINC; |
|
|
530 | if (SLOT_COUNT >= 3) CXINC; |
526 | { |
531 | { |
527 | /* we manually unroll here, as usually 2 slots is enough */ |
|
|
528 | int i; |
532 | 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) |
533 | for (i = 3; i < SLOT_COUNT; ++i) |
533 | CXINC; |
534 | CXINC; |
534 | |
|
|
535 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
|
|
536 | } |
535 | } |
|
|
536 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
537 | |
537 | |
538 | c->mainstack = PL_mainstack; |
538 | c->mainstack = PL_mainstack; |
539 | |
539 | |
540 | { |
540 | { |
541 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
541 | perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); |
… | |
… | |
685 | PL_curpm = 0; |
685 | PL_curpm = 0; |
686 | PL_curpad = 0; |
686 | PL_curpad = 0; |
687 | PL_localizing = 0; |
687 | PL_localizing = 0; |
688 | PL_dirty = 0; |
688 | PL_dirty = 0; |
689 | PL_restartop = 0; |
689 | PL_restartop = 0; |
690 | SvREFCNT_inc (PL_diehook ); hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0); |
690 | PL_diehook = 0; hv_store (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, SvREFCNT_inc (sv_diehook ), 0); |
691 | SvREFCNT_inc (PL_warnhook); hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0); |
691 | PL_warnhook = 0; hv_store (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, SvREFCNT_inc (sv_warnhook), 0); |
692 | |
692 | |
693 | GvSV (PL_defgv) = newSV (0); |
693 | GvSV (PL_defgv) = newSV (0); |
694 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
694 | GvAV (PL_defgv) = coro->args; coro->args = 0; |
695 | GvSV (PL_errgv) = newSV (0); |
695 | GvSV (PL_errgv) = newSV (0); |
696 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
696 | GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); |
… | |
… | |
711 | PL_op = (OP *)&myop; |
711 | PL_op = (OP *)&myop; |
712 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
712 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
713 | SPAGAIN; |
713 | SPAGAIN; |
714 | } |
714 | } |
715 | |
715 | |
716 | ENTER; /* necessary e.g. for dounwind and to balance the xsub-entersub */ |
716 | /* this newly created coroutine might be run on an existing cctx which most |
|
|
717 | * likely was suspended in set_stacklevel, called from entersub. |
|
|
718 | * set_stacklevl doesn't do anything on return, but entersub does LEAVE, |
|
|
719 | * so we ENTER here for symmetry |
|
|
720 | */ |
|
|
721 | ENTER; |
717 | } |
722 | } |
718 | |
723 | |
719 | static void |
724 | static void |
720 | coro_destroy (pTHX_ struct coro *coro) |
725 | coro_destroy (pTHX_ struct coro *coro) |
721 | { |
726 | { |
… | |
… | |
805 | PUSHMARK (SP); |
810 | PUSHMARK (SP); |
806 | PUSHs (&PL_sv_no); |
811 | PUSHs (&PL_sv_no); |
807 | PUSHs (fullname); |
812 | PUSHs (fullname); |
808 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
813 | PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); |
809 | PUTBACK; |
814 | PUTBACK; |
810 | cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0); |
815 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
811 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
816 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
812 | SPAGAIN; |
817 | SPAGAIN; |
813 | FREETMPS; |
818 | FREETMPS; |
814 | LEAVE; |
819 | LEAVE; |
815 | PL_runops = runops_trace; |
820 | PL_runops = runops_trace; |
… | |
… | |
844 | PUSHMARK (SP); |
849 | PUSHMARK (SP); |
845 | PUSHs (&PL_sv_yes); |
850 | PUSHs (&PL_sv_yes); |
846 | PUSHs (fullname); |
851 | PUSHs (fullname); |
847 | PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
852 | PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
848 | PUTBACK; |
853 | PUTBACK; |
849 | cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0); |
854 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
850 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
855 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
851 | SPAGAIN; |
856 | SPAGAIN; |
852 | FREETMPS; |
857 | FREETMPS; |
853 | LEAVE; |
858 | LEAVE; |
854 | PL_runops = runops_trace; |
859 | PL_runops = runops_trace; |
… | |
… | |
868 | PL_runops = RUNOPS_DEFAULT; |
873 | PL_runops = RUNOPS_DEFAULT; |
869 | PUSHMARK (SP); |
874 | PUSHMARK (SP); |
870 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
875 | PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); |
871 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
876 | PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); |
872 | PUTBACK; |
877 | PUTBACK; |
873 | cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_line_cb"), 0); |
878 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0); |
874 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
879 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
875 | SPAGAIN; |
880 | SPAGAIN; |
876 | FREETMPS; |
881 | FREETMPS; |
877 | LEAVE; |
882 | LEAVE; |
878 | PL_runops = runops_trace; |
883 | PL_runops = runops_trace; |
… | |
… | |
1505 | |
1510 | |
1506 | hv_sig = coro_get_hv ("SIG", TRUE); |
1511 | hv_sig = coro_get_hv ("SIG", TRUE); |
1507 | sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE); |
1512 | sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE); |
1508 | sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE); |
1513 | sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE); |
1509 | |
1514 | |
1510 | if (!PL_diehook ) hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0); |
1515 | if (!PL_diehook ) hv_store (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, SvREFCNT_inc (sv_diehook ), 0); |
1511 | if (!PL_warnhook) hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0); |
1516 | if (!PL_warnhook) hv_store (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, SvREFCNT_inc (sv_warnhook), 0); |
1512 | |
1517 | |
1513 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1518 | coro_state_stash = gv_stashpv ("Coro::State", TRUE); |
1514 | |
1519 | |
1515 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1520 | newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); |
1516 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
1521 | newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); |
… | |
… | |
1522 | |
1527 | |
1523 | while (main_top_env->je_prev) |
1528 | while (main_top_env->je_prev) |
1524 | main_top_env = main_top_env->je_prev; |
1529 | main_top_env = main_top_env->je_prev; |
1525 | |
1530 | |
1526 | coroapi.ver = CORO_API_VERSION; |
1531 | coroapi.ver = CORO_API_VERSION; |
|
|
1532 | coroapi.rev = CORO_API_REVISION; |
1527 | coroapi.transfer = api_transfer; |
1533 | coroapi.transfer = api_transfer; |
1528 | |
1534 | |
1529 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1535 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
1530 | } |
1536 | } |
1531 | |
1537 | |
… | |
… | |
1598 | |
1604 | |
1599 | break; |
1605 | break; |
1600 | } |
1606 | } |
1601 | |
1607 | |
1602 | BARRIER; |
1608 | BARRIER; |
|
|
1609 | PUTBACK; |
1603 | TRANSFER (ta); |
1610 | TRANSFER (ta); |
1604 | |
1611 | SPAGAIN; /* might be the sp of a different coroutine now */ |
1605 | if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev)) |
1612 | /* be extra careful not to ever do anything after TRANSFER */ |
1606 | XSRETURN_YES; |
|
|
1607 | } |
1613 | } |
1608 | |
1614 | |
1609 | bool |
1615 | bool |
1610 | _destroy (SV *coro_sv) |
1616 | _destroy (SV *coro_sv) |
1611 | CODE: |
1617 | CODE: |
… | |
… | |
1671 | |
1677 | |
1672 | { |
1678 | { |
1673 | dSP; |
1679 | dSP; |
1674 | ENTER; |
1680 | ENTER; |
1675 | SAVETMPS; |
1681 | SAVETMPS; |
|
|
1682 | PUTBACK; |
|
|
1683 | PUSHSTACK; |
1676 | PUSHMARK (SP); |
1684 | PUSHMARK (SP); |
1677 | PUTBACK; |
|
|
1678 | |
1685 | |
1679 | if (ix) |
1686 | if (ix) |
1680 | eval_sv (coderef, 0); |
1687 | eval_sv (coderef, 0); |
1681 | else |
1688 | else |
1682 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1689 | call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1683 | |
1690 | |
|
|
1691 | POPSTACK; |
1684 | SPAGAIN; |
1692 | SPAGAIN; |
1685 | FREETMPS; |
1693 | FREETMPS; |
1686 | LEAVE; |
1694 | LEAVE; |
1687 | PUTBACK; |
1695 | PUTBACK; |
1688 | } |
1696 | } |
… | |
… | |
1843 | CODE: |
1851 | CODE: |
1844 | { |
1852 | { |
1845 | struct coro *coro = SvSTATE (coro_current); |
1853 | struct coro *coro = SvSTATE (coro_current); |
1846 | HV *hv = (HV *)SvRV (coro_current); |
1854 | HV *hv = (HV *)SvRV (coro_current); |
1847 | AV *defav = GvAV (PL_defgv); |
1855 | AV *defav = GvAV (PL_defgv); |
1848 | SV *invoke = hv_delete (hv, strpair ("_invoke"), 0); |
1856 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
1849 | AV *invoke_av; |
1857 | AV *invoke_av; |
1850 | int i, len; |
1858 | int i, len; |
1851 | |
1859 | |
1852 | if (!invoke) |
1860 | if (!invoke) |
1853 | croak ("\3async_pool terminate\2\n"); |
1861 | croak ("\3async_pool terminate\2\n"); |
1854 | |
1862 | |
1855 | SvREFCNT_dec (coro->saved_deffh); |
1863 | SvREFCNT_dec (coro->saved_deffh); |
1856 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
1864 | coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); |
1857 | |
1865 | |
1858 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1866 | hv_store (hv, "desc", sizeof ("desc") - 1, |
1859 | newSVpvn (strpair ("[async_pool]")), 0); |
1867 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
1860 | |
1868 | |
1861 | invoke_av = (AV *)SvRV (invoke); |
1869 | invoke_av = (AV *)SvRV (invoke); |
1862 | len = av_len (invoke_av); |
1870 | len = av_len (invoke_av); |
1863 | |
1871 | |
1864 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
1872 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
… | |
… | |
1887 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1895 | if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) |
1888 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1896 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
1889 | croak ("\3async_pool terminate\2\n"); |
1897 | croak ("\3async_pool terminate\2\n"); |
1890 | |
1898 | |
1891 | av_clear (GvAV (PL_defgv)); |
1899 | av_clear (GvAV (PL_defgv)); |
1892 | hv_store ((HV *)SvRV (coro_current), strpair ("desc"), |
1900 | hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, |
1893 | newSVpvn (strpair ("[async_pool idle]")), 0); |
1901 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
1894 | |
1902 | |
1895 | coro->prio = 0; |
1903 | coro->prio = 0; |
1896 | |
1904 | |
1897 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
1905 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
1898 | api_trace (coro_current, 0); |
1906 | api_trace (coro_current, 0); |