… | |
… | |
6 | #include "EXTERN.h" |
6 | #include "EXTERN.h" |
7 | #include "perl.h" |
7 | #include "perl.h" |
8 | #include "XSUB.h" |
8 | #include "XSUB.h" |
9 | #include "perliol.h" |
9 | #include "perliol.h" |
10 | |
10 | |
11 | #include "patchlevel.h" |
11 | #include "schmorp.h" |
12 | |
12 | |
13 | #include <stdio.h> |
13 | #include <stdio.h> |
14 | #include <errno.h> |
14 | #include <errno.h> |
15 | #include <assert.h> |
15 | #include <assert.h> |
16 | |
16 | |
… | |
… | |
51 | #endif |
51 | #endif |
52 | |
52 | |
53 | /* the maximum number of idle cctx that will be pooled */ |
53 | /* the maximum number of idle cctx that will be pooled */ |
54 | static int cctx_max_idle = 4; |
54 | static int cctx_max_idle = 4; |
55 | |
55 | |
56 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
|
|
57 | (PERL_REVISION > (a) \ |
|
|
58 | || (PERL_REVISION == (a) \ |
|
|
59 | && (PERL_VERSION > (b) \ |
|
|
60 | || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c))))) |
|
|
61 | |
|
|
62 | #if !PERL_VERSION_ATLEAST (5,6,0) |
|
|
63 | # ifndef PL_ppaddr |
|
|
64 | # define PL_ppaddr ppaddr |
|
|
65 | # endif |
|
|
66 | # ifndef call_sv |
|
|
67 | # define call_sv perl_call_sv |
|
|
68 | # endif |
|
|
69 | # ifndef get_sv |
|
|
70 | # define get_sv perl_get_sv |
|
|
71 | # endif |
|
|
72 | # ifndef get_cv |
|
|
73 | # define get_cv perl_get_cv |
|
|
74 | # endif |
|
|
75 | # ifndef IS_PADGV |
|
|
76 | # define IS_PADGV(v) 0 |
|
|
77 | # endif |
|
|
78 | # ifndef IS_PADCONST |
|
|
79 | # define IS_PADCONST(v) 0 |
|
|
80 | # endif |
|
|
81 | #endif |
|
|
82 | |
|
|
83 | /* 5.11 */ |
|
|
84 | #ifndef CxHASARGS |
|
|
85 | # define CxHASARGS(cx) (cx)->blk_sub.hasargs |
|
|
86 | #endif |
|
|
87 | |
|
|
88 | /* 5.10.0 */ |
|
|
89 | #ifndef SvREFCNT_inc_NN |
|
|
90 | # define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv) |
|
|
91 | #endif |
|
|
92 | |
|
|
93 | /* 5.8.8 */ |
|
|
94 | #ifndef GV_NOTQUAL |
|
|
95 | # define GV_NOTQUAL 0 |
|
|
96 | #endif |
|
|
97 | #ifndef newSV |
|
|
98 | # define newSV(l) NEWSV(0,l) |
|
|
99 | #endif |
|
|
100 | #ifndef CvISXSUB_on |
|
|
101 | # define CvISXSUB_on(cv) (void)cv |
|
|
102 | #endif |
|
|
103 | #ifndef CvISXSUB |
|
|
104 | # define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE) |
|
|
105 | #endif |
|
|
106 | #ifndef Newx |
|
|
107 | # define Newx(ptr,nitems,type) New (0,ptr,nitems,type) |
|
|
108 | #endif |
|
|
109 | |
|
|
110 | /* 5.8.7 */ |
|
|
111 | #ifndef SvRV_set |
|
|
112 | # define SvRV_set(s,v) SvRV(s) = (v) |
|
|
113 | #endif |
|
|
114 | |
|
|
115 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
56 | #if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 |
116 | # undef CORO_STACKGUARD |
57 | # undef CORO_STACKGUARD |
117 | #endif |
58 | #endif |
118 | |
59 | |
119 | #ifndef CORO_STACKGUARD |
60 | #ifndef CORO_STACKGUARD |
… | |
… | |
201 | static SV *sv_pool_size; |
142 | static SV *sv_pool_size; |
202 | static SV *sv_async_pool_idle; /* description string */ |
143 | static SV *sv_async_pool_idle; /* description string */ |
203 | static AV *av_async_pool; /* idle pool */ |
144 | static AV *av_async_pool; /* idle pool */ |
204 | static SV *sv_Coro; /* class string */ |
145 | static SV *sv_Coro; /* class string */ |
205 | static CV *cv_pool_handler; |
146 | static CV *cv_pool_handler; |
206 | static CV *cv_coro_state_new; |
|
|
207 | |
147 | |
208 | /* Coro::AnyEvent */ |
148 | /* Coro::AnyEvent */ |
209 | static SV *sv_activity; |
149 | static SV *sv_activity; |
210 | |
150 | |
211 | /* enable processtime/realtime profiling */ |
151 | /* enable processtime/realtime profiling */ |
… | |
… | |
342 | static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ |
282 | static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ |
343 | static CV *cv_coro_run, *cv_coro_terminate; |
283 | static CV *cv_coro_run, *cv_coro_terminate; |
344 | static struct coro *coro_first; |
284 | static struct coro *coro_first; |
345 | #define coro_nready coroapi.nready |
285 | #define coro_nready coroapi.nready |
346 | |
286 | |
|
|
287 | /** Coro::Select ************************************************************/ |
|
|
288 | |
|
|
289 | static OP *(*coro_old_pp_sselect) (pTHX); |
|
|
290 | static SV *coro_select_select; |
|
|
291 | |
|
|
292 | /* horrible hack, but if it works... */ |
|
|
293 | static OP * |
|
|
294 | coro_pp_sselect (pTHX) |
|
|
295 | { |
|
|
296 | dSP; |
|
|
297 | PUSHMARK (SP - 4); /* fake argument list */ |
|
|
298 | XPUSHs (coro_select_select); |
|
|
299 | PUTBACK; |
|
|
300 | |
|
|
301 | /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */ |
|
|
302 | PL_op->op_flags |= OPf_STACKED; |
|
|
303 | PL_op->op_private = 0; |
|
|
304 | return PL_ppaddr [OP_ENTERSUB](aTHX); |
|
|
305 | } |
|
|
306 | |
347 | /** lowlevel stuff **********************************************************/ |
307 | /** lowlevel stuff **********************************************************/ |
348 | |
308 | |
349 | static SV * |
309 | static SV * |
350 | coro_get_sv (pTHX_ const char *name, int create) |
310 | coro_get_sv (pTHX_ const char *name, int create) |
351 | { |
311 | { |
… | |
… | |
372 | #if PERL_VERSION_ATLEAST (5,10,0) |
332 | #if PERL_VERSION_ATLEAST (5,10,0) |
373 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
333 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
374 | get_hv (name, create); |
334 | get_hv (name, create); |
375 | #endif |
335 | #endif |
376 | return get_hv (name, create); |
336 | return get_hv (name, create); |
377 | } |
|
|
378 | |
|
|
379 | /* may croak */ |
|
|
380 | INLINE CV * |
|
|
381 | coro_sv_2cv (pTHX_ SV *sv) |
|
|
382 | { |
|
|
383 | HV *st; |
|
|
384 | GV *gvp; |
|
|
385 | CV *cv = sv_2cv (sv, &st, &gvp, 0); |
|
|
386 | |
|
|
387 | if (!cv) |
|
|
388 | croak ("code reference expected"); |
|
|
389 | |
|
|
390 | return cv; |
|
|
391 | } |
337 | } |
392 | |
338 | |
393 | INLINE void |
339 | INLINE void |
394 | coro_times_update () |
340 | coro_times_update () |
395 | { |
341 | { |
… | |
… | |
1328 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1274 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1329 | PL_restartop = PL_op; |
1275 | PL_restartop = PL_op; |
1330 | perl_run (PL_curinterp); |
1276 | perl_run (PL_curinterp); |
1331 | /* |
1277 | /* |
1332 | * Unfortunately, there is no way to get at the return values of the |
1278 | * Unfortunately, there is no way to get at the return values of the |
1333 | * coro body here, as perl_run destroys these |
1279 | * coro body here, as perl_run destroys these. Likewise, we cannot catch |
|
|
1280 | * runtime errors here, as this is just a random interpreter, not a thread. |
1334 | */ |
1281 | */ |
1335 | |
1282 | |
1336 | /* |
1283 | /* |
1337 | * If perl-run returns we assume exit() was being called or the coro |
1284 | * If perl-run returns we assume exit() was being called or the coro |
1338 | * fell off the end, which seems to be the only valid (non-bug) |
1285 | * fell off the end, which seems to be the only valid (non-bug) |
… | |
… | |
1687 | |
1634 | |
1688 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1635 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1689 | TRANSFER (ta, 1); |
1636 | TRANSFER (ta, 1); |
1690 | } |
1637 | } |
1691 | |
1638 | |
1692 | /*****************************************************************************/ |
|
|
1693 | /* gensub: simple closure generation utility */ |
|
|
1694 | |
|
|
1695 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
|
|
1696 | |
|
|
1697 | /* create a closure from XS, returns a code reference */ |
|
|
1698 | /* the arg can be accessed via GENSUB_ARG from the callback */ |
|
|
1699 | /* the callback must use dXSARGS/XSRETURN */ |
|
|
1700 | static SV * |
|
|
1701 | gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
|
|
1702 | { |
|
|
1703 | CV *cv = (CV *)newSV (0); |
|
|
1704 | |
|
|
1705 | sv_upgrade ((SV *)cv, SVt_PVCV); |
|
|
1706 | |
|
|
1707 | CvANON_on (cv); |
|
|
1708 | CvISXSUB_on (cv); |
|
|
1709 | CvXSUB (cv) = xsub; |
|
|
1710 | GENSUB_ARG = arg; |
|
|
1711 | |
|
|
1712 | return newRV_noinc ((SV *)cv); |
|
|
1713 | } |
|
|
1714 | |
|
|
1715 | /** Coro ********************************************************************/ |
1639 | /** Coro ********************************************************************/ |
1716 | |
1640 | |
1717 | INLINE void |
1641 | INLINE void |
1718 | coro_enq (pTHX_ struct coro *coro) |
1642 | coro_enq (pTHX_ struct coro *coro) |
1719 | { |
1643 | { |
… | |
… | |
1744 | } |
1668 | } |
1745 | |
1669 | |
1746 | return 0; |
1670 | return 0; |
1747 | } |
1671 | } |
1748 | |
1672 | |
|
|
1673 | static void |
|
|
1674 | invoke_sv_ready_hook_helper (void) |
|
|
1675 | { |
|
|
1676 | dTHX; |
|
|
1677 | dSP; |
|
|
1678 | |
|
|
1679 | ENTER; |
|
|
1680 | SAVETMPS; |
|
|
1681 | |
|
|
1682 | PUSHMARK (SP); |
|
|
1683 | PUTBACK; |
|
|
1684 | call_sv (coro_readyhook, G_VOID | G_DISCARD); |
|
|
1685 | |
|
|
1686 | FREETMPS; |
|
|
1687 | LEAVE; |
|
|
1688 | } |
|
|
1689 | |
1749 | static int |
1690 | static int |
1750 | api_ready (pTHX_ SV *coro_sv) |
1691 | api_ready (pTHX_ SV *coro_sv) |
1751 | { |
1692 | { |
1752 | struct coro *coro; |
|
|
1753 | SV *sv_hook; |
|
|
1754 | void (*xs_hook)(void); |
|
|
1755 | |
|
|
1756 | coro = SvSTATE (coro_sv); |
1693 | struct coro *coro = SvSTATE (coro_sv); |
1757 | |
1694 | |
1758 | if (coro->flags & CF_READY) |
1695 | if (coro->flags & CF_READY) |
1759 | return 0; |
1696 | return 0; |
1760 | |
1697 | |
1761 | coro->flags |= CF_READY; |
1698 | coro->flags |= CF_READY; |
1762 | |
1699 | |
1763 | sv_hook = coro_nready ? 0 : coro_readyhook; |
|
|
1764 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
|
|
1765 | |
|
|
1766 | coro_enq (aTHX_ coro); |
1700 | coro_enq (aTHX_ coro); |
1767 | ++coro_nready; |
|
|
1768 | |
1701 | |
1769 | if (sv_hook) |
1702 | if (!coro_nready++) |
1770 | { |
1703 | if (coroapi.readyhook) |
1771 | dSP; |
1704 | coroapi.readyhook (); |
1772 | |
|
|
1773 | ENTER; |
|
|
1774 | SAVETMPS; |
|
|
1775 | |
|
|
1776 | PUSHMARK (SP); |
|
|
1777 | PUTBACK; |
|
|
1778 | call_sv (sv_hook, G_VOID | G_DISCARD); |
|
|
1779 | |
|
|
1780 | FREETMPS; |
|
|
1781 | LEAVE; |
|
|
1782 | } |
|
|
1783 | |
|
|
1784 | if (xs_hook) |
|
|
1785 | xs_hook (); |
|
|
1786 | |
1705 | |
1787 | return 1; |
1706 | return 1; |
1788 | } |
1707 | } |
1789 | |
1708 | |
1790 | static int |
1709 | static int |
… | |
… | |
1841 | api_ready (aTHX_ SvRV (sv_idle)); |
1760 | api_ready (aTHX_ SvRV (sv_idle)); |
1842 | --coro_nready; |
1761 | --coro_nready; |
1843 | } |
1762 | } |
1844 | else |
1763 | else |
1845 | { |
1764 | { |
|
|
1765 | /* TODO: deprecated, remove, cannot work reliably *//*D*/ |
1846 | dSP; |
1766 | dSP; |
1847 | |
1767 | |
1848 | ENTER; |
1768 | ENTER; |
1849 | SAVETMPS; |
1769 | SAVETMPS; |
1850 | |
1770 | |
… | |
… | |
1996 | { |
1916 | { |
1997 | int i; |
1917 | int i; |
1998 | HV *hv = (HV *)SvRV (coro_current); |
1918 | HV *hv = (HV *)SvRV (coro_current); |
1999 | AV *av = newAV (); |
1919 | AV *av = newAV (); |
2000 | |
1920 | |
|
|
1921 | /* items are actually not so common, so optimise for this case */ |
|
|
1922 | if (items) |
|
|
1923 | { |
2001 | av_extend (av, items - 1); |
1924 | av_extend (av, items - 1); |
|
|
1925 | |
2002 | for (i = 0; i < items; ++i) |
1926 | for (i = 0; i < items; ++i) |
2003 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
1927 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
|
|
1928 | } |
2004 | |
1929 | |
2005 | hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); |
1930 | hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); |
2006 | |
1931 | |
2007 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
1932 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
2008 | api_ready (aTHX_ sv_manager); |
1933 | api_ready (aTHX_ sv_manager); |
… | |
… | |
2098 | |
2023 | |
2099 | static void |
2024 | static void |
2100 | coro_rouse_callback (pTHX_ CV *cv) |
2025 | coro_rouse_callback (pTHX_ CV *cv) |
2101 | { |
2026 | { |
2102 | dXSARGS; |
2027 | dXSARGS; |
2103 | SV *data = (SV *)GENSUB_ARG; |
2028 | SV *data = (SV *)S_GENSUB_ARG; |
2104 | |
2029 | |
2105 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
2030 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
2106 | { |
2031 | { |
2107 | /* first call, set args */ |
2032 | /* first call, set args */ |
2108 | SV *coro = SvRV (data); |
2033 | SV *coro = SvRV (data); |
… | |
… | |
2174 | || SvTYPE (SvRV (cb)) != SVt_PVCV |
2099 | || SvTYPE (SvRV (cb)) != SVt_PVCV |
2175 | || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) |
2100 | || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) |
2176 | croak ("Coro::rouse_wait called with illegal callback argument,"); |
2101 | croak ("Coro::rouse_wait called with illegal callback argument,"); |
2177 | |
2102 | |
2178 | { |
2103 | { |
2179 | CV *cv = (CV *)SvRV (cb); /* for GENSUB_ARG */ |
2104 | CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */ |
2180 | SV *data = (SV *)GENSUB_ARG; |
2105 | SV *data = (SV *)S_GENSUB_ARG; |
2181 | |
2106 | |
2182 | frame->data = (void *)data; |
2107 | frame->data = (void *)data; |
2183 | frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule; |
2108 | frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule; |
2184 | frame->check = slf_check_rouse_wait; |
2109 | frame->check = slf_check_rouse_wait; |
2185 | } |
2110 | } |
… | |
… | |
2189 | coro_new_rouse_cb (pTHX) |
2114 | coro_new_rouse_cb (pTHX) |
2190 | { |
2115 | { |
2191 | HV *hv = (HV *)SvRV (coro_current); |
2116 | HV *hv = (HV *)SvRV (coro_current); |
2192 | struct coro *coro = SvSTATE_hv (hv); |
2117 | struct coro *coro = SvSTATE_hv (hv); |
2193 | SV *data = newRV_inc ((SV *)hv); |
2118 | SV *data = newRV_inc ((SV *)hv); |
2194 | SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data); |
2119 | SV *cb = s_gensub (aTHX_ coro_rouse_callback, (void *)data); |
2195 | |
2120 | |
2196 | sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0); |
2121 | sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0); |
2197 | SvREFCNT_dec (data); /* magicext increases the refcount */ |
2122 | SvREFCNT_dec (data); /* magicext increases the refcount */ |
2198 | |
2123 | |
2199 | SvREFCNT_dec (coro->rouse_cb); |
2124 | SvREFCNT_dec (coro->rouse_cb); |
… | |
… | |
2406 | if (PL_op->op_ppaddr != PL_ppaddr [OP_ENTERSUB] |
2331 | if (PL_op->op_ppaddr != PL_ppaddr [OP_ENTERSUB] |
2407 | && PL_op->op_ppaddr != pp_slf) |
2332 | && PL_op->op_ppaddr != pp_slf) |
2408 | croak ("FATAL: Coro SLF calls can only be made normally, not via goto or any other means, caught"); |
2333 | croak ("FATAL: Coro SLF calls can only be made normally, not via goto or any other means, caught"); |
2409 | |
2334 | |
2410 | CvFLAGS (cv) |= CVf_SLF; |
2335 | CvFLAGS (cv) |= CVf_SLF; |
|
|
2336 | CvNODEBUG_on (cv); |
2411 | CvXSUBANY (cv).any_ptr = (void *)init_cb; |
2337 | CvXSUBANY (cv).any_ptr = (void *)init_cb; |
2412 | slf_cv = cv; |
2338 | slf_cv = cv; |
2413 | |
2339 | |
2414 | /* we patch the op, and then re-run the whole call */ |
2340 | /* we patch the op, and then re-run the whole call */ |
2415 | /* we have to put the same argument on the stack for this to work */ |
2341 | /* we have to put the same argument on the stack for this to work */ |
… | |
… | |
2722 | { |
2648 | { |
2723 | if (items >= 2) |
2649 | if (items >= 2) |
2724 | { |
2650 | { |
2725 | /* callback form */ |
2651 | /* callback form */ |
2726 | AV *av = (AV *)SvRV (arg [0]); |
2652 | AV *av = (AV *)SvRV (arg [0]); |
2727 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
2653 | SV *cb_cv = s_get_cv_croak (arg [1]); |
2728 | |
2654 | |
2729 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2655 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2730 | |
2656 | |
2731 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2657 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2732 | coro_semaphore_adjust (aTHX_ av, 0); |
2658 | coro_semaphore_adjust (aTHX_ av, 0); |
… | |
… | |
2792 | { |
2718 | { |
2793 | AV *av = (AV *)SvRV (arg [0]); |
2719 | AV *av = (AV *)SvRV (arg [0]); |
2794 | |
2720 | |
2795 | if (items >= 2) |
2721 | if (items >= 2) |
2796 | { |
2722 | { |
2797 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
2723 | SV *cb_cv = s_get_cv_croak (arg [1]); |
2798 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2724 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2799 | |
2725 | |
2800 | if (SvIVX (AvARRAY (av)[0])) |
2726 | if (SvIVX (AvARRAY (av)[0])) |
2801 | coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ |
2727 | coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ |
2802 | |
2728 | |
… | |
… | |
2837 | |
2763 | |
2838 | static void |
2764 | static void |
2839 | coro_aio_callback (pTHX_ CV *cv) |
2765 | coro_aio_callback (pTHX_ CV *cv) |
2840 | { |
2766 | { |
2841 | dXSARGS; |
2767 | dXSARGS; |
2842 | AV *state = (AV *)GENSUB_ARG; |
2768 | AV *state = (AV *)S_GENSUB_ARG; |
2843 | SV *coro = av_pop (state); |
2769 | SV *coro = av_pop (state); |
2844 | SV *data_sv = newSV (sizeof (struct io_state)); |
2770 | SV *data_sv = newSV (sizeof (struct io_state)); |
2845 | |
2771 | |
2846 | av_extend (state, items - 1); |
2772 | av_extend (state, items - 1); |
2847 | |
2773 | |
… | |
… | |
2961 | |
2887 | |
2962 | for (i = 0; i < items; ++i) |
2888 | for (i = 0; i < items; ++i) |
2963 | PUSHs (arg [i]); |
2889 | PUSHs (arg [i]); |
2964 | |
2890 | |
2965 | /* now push the callback closure */ |
2891 | /* now push the callback closure */ |
2966 | PUSHs (sv_2mortal (gensub (aTHX_ coro_aio_callback, (void *)SvREFCNT_inc_NN ((SV *)state)))); |
2892 | PUSHs (sv_2mortal (s_gensub (aTHX_ coro_aio_callback, (void *)SvREFCNT_inc_NN ((SV *)state)))); |
2967 | |
2893 | |
2968 | /* now call the AIO function - we assume our request is uncancelable */ |
2894 | /* now call the AIO function - we assume our request is uncancelable */ |
2969 | PUTBACK; |
2895 | PUTBACK; |
2970 | call_sv ((SV *)req, G_VOID | G_DISCARD); |
2896 | call_sv ((SV *)req, G_VOID | G_DISCARD); |
2971 | } |
2897 | } |
… | |
… | |
2989 | /*****************************************************************************/ |
2915 | /*****************************************************************************/ |
2990 | |
2916 | |
2991 | #if CORO_CLONE |
2917 | #if CORO_CLONE |
2992 | # include "clone.c" |
2918 | # include "clone.c" |
2993 | #endif |
2919 | #endif |
|
|
2920 | |
|
|
2921 | /*****************************************************************************/ |
|
|
2922 | |
|
|
2923 | static SV * |
|
|
2924 | coro_new (pTHX_ HV *stash, SV **argv, int argc, int is_coro) |
|
|
2925 | { |
|
|
2926 | SV *coro_sv; |
|
|
2927 | struct coro *coro; |
|
|
2928 | MAGIC *mg; |
|
|
2929 | HV *hv; |
|
|
2930 | SV *cb; |
|
|
2931 | int i; |
|
|
2932 | |
|
|
2933 | if (argc > 0) |
|
|
2934 | { |
|
|
2935 | cb = s_get_cv_croak (argv [0]); |
|
|
2936 | |
|
|
2937 | if (!is_coro) |
|
|
2938 | { |
|
|
2939 | if (CvISXSUB (cb)) |
|
|
2940 | croak ("Coro::State doesn't support XS functions as coroutine start, caught"); |
|
|
2941 | |
|
|
2942 | if (!CvROOT (cb)) |
|
|
2943 | croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); |
|
|
2944 | } |
|
|
2945 | } |
|
|
2946 | |
|
|
2947 | Newz (0, coro, 1, struct coro); |
|
|
2948 | coro->args = newAV (); |
|
|
2949 | coro->flags = CF_NEW; |
|
|
2950 | |
|
|
2951 | if (coro_first) coro_first->prev = coro; |
|
|
2952 | coro->next = coro_first; |
|
|
2953 | coro_first = coro; |
|
|
2954 | |
|
|
2955 | coro->hv = hv = newHV (); |
|
|
2956 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
|
|
2957 | mg->mg_flags |= MGf_DUP; |
|
|
2958 | coro_sv = sv_bless (newRV_noinc ((SV *)hv), stash); |
|
|
2959 | |
|
|
2960 | if (argc > 0) |
|
|
2961 | { |
|
|
2962 | av_extend (coro->args, argc + is_coro - 1); |
|
|
2963 | |
|
|
2964 | if (is_coro) |
|
|
2965 | { |
|
|
2966 | av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); |
|
|
2967 | cb = (SV *)cv_coro_run; |
|
|
2968 | } |
|
|
2969 | |
|
|
2970 | coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); |
|
|
2971 | |
|
|
2972 | for (i = 1; i < argc; i++) |
|
|
2973 | av_push (coro->args, newSVsv (argv [i])); |
|
|
2974 | } |
|
|
2975 | |
|
|
2976 | return coro_sv; |
|
|
2977 | } |
2994 | |
2978 | |
2995 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2979 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2996 | |
2980 | |
2997 | PROTOTYPES: DISABLE |
2981 | PROTOTYPES: DISABLE |
2998 | |
2982 | |
… | |
… | |
3067 | |
3051 | |
3068 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3052 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3069 | } |
3053 | } |
3070 | |
3054 | |
3071 | SV * |
3055 | SV * |
3072 | new (char *klass, ...) |
3056 | new (SV *klass, ...) |
3073 | ALIAS: |
3057 | ALIAS: |
3074 | Coro::new = 1 |
3058 | Coro::new = 1 |
3075 | CODE: |
3059 | CODE: |
3076 | { |
3060 | RETVAL = coro_new (aTHX_ ix ? coro_stash : coro_state_stash, &ST (1), items - 1, ix); |
3077 | struct coro *coro; |
3061 | OUTPUT: |
3078 | MAGIC *mg; |
|
|
3079 | HV *hv; |
|
|
3080 | CV *cb; |
|
|
3081 | int i; |
|
|
3082 | |
|
|
3083 | if (items > 1) |
|
|
3084 | { |
|
|
3085 | cb = coro_sv_2cv (aTHX_ ST (1)); |
|
|
3086 | |
|
|
3087 | if (!ix) |
|
|
3088 | { |
|
|
3089 | if (CvISXSUB (cb)) |
|
|
3090 | croak ("Coro::State doesn't support XS functions as coroutine start, caught"); |
|
|
3091 | |
|
|
3092 | if (!CvROOT (cb)) |
|
|
3093 | croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); |
|
|
3094 | } |
|
|
3095 | } |
|
|
3096 | |
|
|
3097 | Newz (0, coro, 1, struct coro); |
|
|
3098 | coro->args = newAV (); |
|
|
3099 | coro->flags = CF_NEW; |
|
|
3100 | |
|
|
3101 | if (coro_first) coro_first->prev = coro; |
|
|
3102 | coro->next = coro_first; |
|
|
3103 | coro_first = coro; |
|
|
3104 | |
|
|
3105 | coro->hv = hv = newHV (); |
|
|
3106 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
|
|
3107 | mg->mg_flags |= MGf_DUP; |
|
|
3108 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
|
|
3109 | |
|
|
3110 | if (items > 1) |
|
|
3111 | { |
|
|
3112 | av_extend (coro->args, items - 1 + ix - 1); |
|
|
3113 | |
|
|
3114 | if (ix) |
|
|
3115 | { |
|
|
3116 | av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); |
|
|
3117 | cb = cv_coro_run; |
|
|
3118 | } |
|
|
3119 | |
|
|
3120 | coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); |
|
|
3121 | |
|
|
3122 | for (i = 2; i < items; i++) |
|
|
3123 | av_push (coro->args, newSVsv (ST (i))); |
|
|
3124 | } |
|
|
3125 | } |
|
|
3126 | OUTPUT: |
|
|
3127 | RETVAL |
3062 | RETVAL |
3128 | |
3063 | |
3129 | void |
3064 | void |
3130 | transfer (...) |
3065 | transfer (...) |
3131 | PROTOTYPE: $$ |
3066 | PROTOTYPE: $$ |
… | |
… | |
3398 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3333 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3399 | |
3334 | |
3400 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3335 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
3401 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3336 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3402 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3337 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
3403 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
3338 | CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */ |
3404 | |
3339 | |
3405 | coro_stash = gv_stashpv ("Coro", TRUE); |
3340 | coro_stash = gv_stashpv ("Coro", TRUE); |
3406 | |
3341 | |
3407 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3342 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3408 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3343 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3409 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
3344 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
… | |
… | |
3427 | sv_setiv (sv, (IV)&coroapi); |
3362 | sv_setiv (sv, (IV)&coroapi); |
3428 | SvREADONLY_on (sv); |
3363 | SvREADONLY_on (sv); |
3429 | } |
3364 | } |
3430 | } |
3365 | } |
3431 | |
3366 | |
|
|
3367 | SV * |
|
|
3368 | async (...) |
|
|
3369 | PROTOTYPE: &@ |
|
|
3370 | CODE: |
|
|
3371 | RETVAL = coro_new (aTHX_ coro_stash, &ST (0), items, 1); |
|
|
3372 | api_ready (aTHX_ RETVAL); |
|
|
3373 | OUTPUT: |
|
|
3374 | RETVAL |
|
|
3375 | |
3432 | void |
3376 | void |
3433 | terminate (...) |
3377 | terminate (...) |
3434 | CODE: |
3378 | CODE: |
3435 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
3379 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
3436 | |
3380 | |
… | |
… | |
3470 | _set_readyhook (SV *hook) |
3414 | _set_readyhook (SV *hook) |
3471 | PROTOTYPE: $ |
3415 | PROTOTYPE: $ |
3472 | CODE: |
3416 | CODE: |
3473 | SvREFCNT_dec (coro_readyhook); |
3417 | SvREFCNT_dec (coro_readyhook); |
3474 | SvGETMAGIC (hook); |
3418 | SvGETMAGIC (hook); |
|
|
3419 | if (SvOK (hook)) |
|
|
3420 | { |
3475 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
3421 | coro_readyhook = newSVsv (hook); |
|
|
3422 | CORO_READYHOOK = invoke_sv_ready_hook_helper; |
|
|
3423 | } |
|
|
3424 | else |
|
|
3425 | { |
|
|
3426 | coro_readyhook = 0; |
|
|
3427 | CORO_READYHOOK = 0; |
|
|
3428 | } |
3476 | |
3429 | |
3477 | int |
3430 | int |
3478 | prio (Coro::State coro, int newprio = 0) |
3431 | prio (Coro::State coro, int newprio = 0) |
3479 | PROTOTYPE: $;$ |
3432 | PROTOTYPE: $;$ |
3480 | ALIAS: |
3433 | ALIAS: |
… | |
… | |
3544 | for (i = 1; i < items; ++i) |
3497 | for (i = 1; i < items; ++i) |
3545 | av_push (av, SvREFCNT_inc_NN (ST (i))); |
3498 | av_push (av, SvREFCNT_inc_NN (ST (i))); |
3546 | |
3499 | |
3547 | if ((SV *)hv == &PL_sv_undef) |
3500 | if ((SV *)hv == &PL_sv_undef) |
3548 | { |
3501 | { |
3549 | PUSHMARK (SP); |
3502 | SV *sv = coro_new (aTHX_ coro_stash, (SV **)&cv_pool_handler, 1, 1); |
3550 | EXTEND (SP, 2); |
|
|
3551 | PUSHs (sv_Coro); |
|
|
3552 | PUSHs ((SV *)cv_pool_handler); |
|
|
3553 | PUTBACK; |
|
|
3554 | call_sv ((SV *)cv_coro_state_new, G_SCALAR); |
|
|
3555 | SPAGAIN; |
|
|
3556 | |
|
|
3557 | hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); |
3503 | hv = (HV *)SvREFCNT_inc_NN (SvRV (sv)); |
|
|
3504 | SvREFCNT_dec (sv); |
3558 | } |
3505 | } |
3559 | |
3506 | |
3560 | { |
3507 | { |
3561 | struct coro *coro = SvSTATE_hv (hv); |
3508 | struct coro *coro = SvSTATE_hv (hv); |
3562 | |
3509 | |
… | |
… | |
3596 | CODE: |
3543 | CODE: |
3597 | { |
3544 | { |
3598 | struct coro *coro = SvSTATE_current; |
3545 | struct coro *coro = SvSTATE_current; |
3599 | AV **avp = ix ? &coro->on_leave : &coro->on_enter; |
3546 | AV **avp = ix ? &coro->on_leave : &coro->on_enter; |
3600 | |
3547 | |
3601 | block = (SV *)coro_sv_2cv (aTHX_ block); |
3548 | block = s_get_cv_croak (block); |
3602 | |
3549 | |
3603 | if (!*avp) |
3550 | if (!*avp) |
3604 | *avp = newAV (); |
3551 | *avp = newAV (); |
3605 | |
3552 | |
3606 | av_push (*avp, SvREFCNT_inc (block)); |
3553 | av_push (*avp, SvREFCNT_inc (block)); |
… | |
… | |
3806 | |
3753 | |
3807 | void |
3754 | void |
3808 | _register (char *target, char *proto, SV *req) |
3755 | _register (char *target, char *proto, SV *req) |
3809 | CODE: |
3756 | CODE: |
3810 | { |
3757 | { |
3811 | CV *req_cv = coro_sv_2cv (aTHX_ req); |
3758 | SV *req_cv = s_get_cv_croak (req); |
3812 | /* newXSproto doesn't return the CV on 5.8 */ |
3759 | /* newXSproto doesn't return the CV on 5.8 */ |
3813 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3760 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3814 | sv_setpv ((SV *)slf_cv, proto); |
3761 | sv_setpv ((SV *)slf_cv, proto); |
3815 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3762 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3816 | } |
3763 | } |
3817 | |
3764 | |
|
|
3765 | MODULE = Coro::State PACKAGE = Coro::Select |
|
|
3766 | |
|
|
3767 | void |
|
|
3768 | patch_pp_sselect () |
|
|
3769 | CODE: |
|
|
3770 | if (!coro_old_pp_sselect) |
|
|
3771 | { |
|
|
3772 | coro_select_select = (SV *)get_cv ("Coro::Select::select", 0); |
|
|
3773 | coro_old_pp_sselect = PL_ppaddr [OP_SSELECT]; |
|
|
3774 | PL_ppaddr [OP_SSELECT] = coro_pp_sselect; |
|
|
3775 | } |
|
|
3776 | |
|
|
3777 | void |
|
|
3778 | unpatch_pp_sselect () |
|
|
3779 | CODE: |
|
|
3780 | if (coro_old_pp_sselect) |
|
|
3781 | { |
|
|
3782 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
|
|
3783 | coro_old_pp_sselect = 0; |
|
|
3784 | } |
|
|
3785 | |
|
|
3786 | |