… | |
… | |
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 |
… | |
… | |
170 | # define CORO_CLOCK_THREAD_CPUTIME_ID 3 |
111 | # define CORO_CLOCK_THREAD_CPUTIME_ID 3 |
171 | # endif |
112 | # endif |
172 | #endif |
113 | #endif |
173 | |
114 | |
174 | static double (*nvtime)(); /* so why doesn't it take void? */ |
115 | static double (*nvtime)(); /* so why doesn't it take void? */ |
|
|
116 | static void (*u2time)(pTHX_ UV ret[2]); |
175 | |
117 | |
176 | /* we hijack an hopefully unused CV flag for our purposes */ |
118 | /* we hijack an hopefully unused CV flag for our purposes */ |
177 | #define CVf_SLF 0x4000 |
119 | #define CVf_SLF 0x4000 |
178 | static OP *pp_slf (pTHX); |
120 | static OP *pp_slf (pTHX); |
179 | |
121 | |
… | |
… | |
200 | static SV *sv_pool_size; |
142 | static SV *sv_pool_size; |
201 | static SV *sv_async_pool_idle; /* description string */ |
143 | static SV *sv_async_pool_idle; /* description string */ |
202 | static AV *av_async_pool; /* idle pool */ |
144 | static AV *av_async_pool; /* idle pool */ |
203 | static SV *sv_Coro; /* class string */ |
145 | static SV *sv_Coro; /* class string */ |
204 | static CV *cv_pool_handler; |
146 | static CV *cv_pool_handler; |
205 | static CV *cv_coro_state_new; |
|
|
206 | |
147 | |
207 | /* Coro::AnyEvent */ |
148 | /* Coro::AnyEvent */ |
208 | static SV *sv_activity; |
149 | static SV *sv_activity; |
|
|
150 | |
|
|
151 | /* enable processtime/realtime profiling */ |
|
|
152 | static char enable_times; |
|
|
153 | typedef U32 coro_ts[2]; |
|
|
154 | static coro_ts time_real, time_cpu; |
|
|
155 | static char times_valid; |
209 | |
156 | |
210 | static struct coro_cctx *cctx_first; |
157 | static struct coro_cctx *cctx_first; |
211 | static int cctx_count, cctx_idle; |
158 | static int cctx_count, cctx_idle; |
212 | |
159 | |
213 | enum { |
160 | enum { |
… | |
… | |
303 | |
250 | |
304 | /* on_enter/on_leave */ |
251 | /* on_enter/on_leave */ |
305 | AV *on_enter; |
252 | AV *on_enter; |
306 | AV *on_leave; |
253 | AV *on_leave; |
307 | |
254 | |
|
|
255 | /* times */ |
|
|
256 | coro_ts t_cpu, t_real; |
|
|
257 | |
308 | /* linked list */ |
258 | /* linked list */ |
309 | struct coro *next, *prev; |
259 | struct coro *next, *prev; |
310 | }; |
260 | }; |
311 | |
261 | |
312 | typedef struct coro *Coro__State; |
262 | typedef struct coro *Coro__State; |
… | |
… | |
332 | 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 */ |
333 | static CV *cv_coro_run, *cv_coro_terminate; |
283 | static CV *cv_coro_run, *cv_coro_terminate; |
334 | static struct coro *coro_first; |
284 | static struct coro *coro_first; |
335 | #define coro_nready coroapi.nready |
285 | #define coro_nready coroapi.nready |
336 | |
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 | |
337 | /** lowlevel stuff **********************************************************/ |
307 | /** lowlevel stuff **********************************************************/ |
338 | |
308 | |
339 | static SV * |
309 | static SV * |
340 | coro_get_sv (pTHX_ const char *name, int create) |
310 | coro_get_sv (pTHX_ const char *name, int create) |
341 | { |
311 | { |
… | |
… | |
364 | get_hv (name, create); |
334 | get_hv (name, create); |
365 | #endif |
335 | #endif |
366 | return get_hv (name, create); |
336 | return get_hv (name, create); |
367 | } |
337 | } |
368 | |
338 | |
369 | /* may croak */ |
339 | INLINE void |
370 | INLINE CV * |
340 | coro_times_update () |
371 | coro_sv_2cv (pTHX_ SV *sv) |
|
|
372 | { |
341 | { |
373 | HV *st; |
342 | #ifdef coro_clock_gettime |
374 | GV *gvp; |
343 | struct timespec ts; |
375 | CV *cv = sv_2cv (sv, &st, &gvp, 0); |
|
|
376 | |
344 | |
377 | if (!cv) |
345 | ts.tv_sec = ts.tv_nsec = 0; |
378 | croak ("code reference expected"); |
346 | coro_clock_gettime (CORO_CLOCK_THREAD_CPUTIME_ID, &ts); |
|
|
347 | time_cpu [0] = ts.tv_sec; time_cpu [1] = ts.tv_nsec; |
379 | |
348 | |
380 | return cv; |
349 | ts.tv_sec = ts.tv_nsec = 0; |
|
|
350 | coro_clock_gettime (CORO_CLOCK_MONOTONIC, &ts); |
|
|
351 | time_real [0] = ts.tv_sec; time_real [1] = ts.tv_nsec; |
|
|
352 | #else |
|
|
353 | dTHX; |
|
|
354 | UV tv[2]; |
|
|
355 | |
|
|
356 | u2time (aTHX_ tv); |
|
|
357 | time_real [0] = tv [0]; |
|
|
358 | time_real [1] = tv [1] * 1000; |
|
|
359 | #endif |
|
|
360 | } |
|
|
361 | |
|
|
362 | INLINE void |
|
|
363 | coro_times_add (struct coro *c) |
|
|
364 | { |
|
|
365 | c->t_real [1] += time_real [1]; |
|
|
366 | if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } |
|
|
367 | c->t_real [0] += time_real [0]; |
|
|
368 | |
|
|
369 | c->t_cpu [1] += time_cpu [1]; |
|
|
370 | if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } |
|
|
371 | c->t_cpu [0] += time_cpu [0]; |
|
|
372 | } |
|
|
373 | |
|
|
374 | INLINE void |
|
|
375 | coro_times_sub (struct coro *c) |
|
|
376 | { |
|
|
377 | if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } |
|
|
378 | c->t_real [1] -= time_real [1]; |
|
|
379 | c->t_real [0] -= time_real [0]; |
|
|
380 | |
|
|
381 | if (c->t_cpu [1] < time_cpu [1]) { c->t_cpu [1] += 1000000000; --c->t_cpu [0]; } |
|
|
382 | c->t_cpu [1] -= time_cpu [1]; |
|
|
383 | c->t_cpu [0] -= time_cpu [0]; |
381 | } |
384 | } |
382 | |
385 | |
383 | /*****************************************************************************/ |
386 | /*****************************************************************************/ |
384 | /* magic glue */ |
387 | /* magic glue */ |
385 | |
388 | |
… | |
… | |
488 | AV *padlist; |
491 | AV *padlist; |
489 | AV *av = (AV *)mg->mg_obj; |
492 | AV *av = (AV *)mg->mg_obj; |
490 | |
493 | |
491 | /* perl manages to free our internal AV and _then_ call us */ |
494 | /* perl manages to free our internal AV and _then_ call us */ |
492 | if (IN_DESTRUCT) |
495 | if (IN_DESTRUCT) |
493 | return; |
496 | return 0; |
494 | |
497 | |
495 | /* casting is fun. */ |
498 | /* casting is fun. */ |
496 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
499 | while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) |
497 | free_padlist (aTHX_ padlist); |
500 | free_padlist (aTHX_ padlist); |
498 | |
501 | |
… | |
… | |
587 | } |
590 | } |
588 | |
591 | |
589 | slf_frame = c->slf_frame; |
592 | slf_frame = c->slf_frame; |
590 | CORO_THROW = c->except; |
593 | CORO_THROW = c->except; |
591 | |
594 | |
|
|
595 | if (expect_false (enable_times)) |
|
|
596 | { |
|
|
597 | if (expect_false (!times_valid)) |
|
|
598 | coro_times_update (); |
|
|
599 | |
|
|
600 | coro_times_sub (c); |
|
|
601 | } |
|
|
602 | |
592 | if (expect_false (c->on_enter)) |
603 | if (expect_false (c->on_enter)) |
593 | { |
604 | { |
594 | int i; |
605 | int i; |
595 | |
606 | |
596 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
607 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
… | |
… | |
605 | { |
616 | { |
606 | int i; |
617 | int i; |
607 | |
618 | |
608 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
619 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
609 | on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); |
620 | on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); |
|
|
621 | } |
|
|
622 | |
|
|
623 | times_valid = 0; |
|
|
624 | |
|
|
625 | if (expect_false (enable_times)) |
|
|
626 | { |
|
|
627 | coro_times_update (); times_valid = 1; |
|
|
628 | coro_times_add (c); |
610 | } |
629 | } |
611 | |
630 | |
612 | c->except = CORO_THROW; |
631 | c->except = CORO_THROW; |
613 | c->slf_frame = slf_frame; |
632 | c->slf_frame = slf_frame; |
614 | |
633 | |
… | |
… | |
663 | /* we manually unroll here, as usually 2 slots is enough */ |
682 | /* we manually unroll here, as usually 2 slots is enough */ |
664 | if (SLOT_COUNT >= 1) CXINC; |
683 | if (SLOT_COUNT >= 1) CXINC; |
665 | if (SLOT_COUNT >= 2) CXINC; |
684 | if (SLOT_COUNT >= 2) CXINC; |
666 | if (SLOT_COUNT >= 3) CXINC; |
685 | if (SLOT_COUNT >= 3) CXINC; |
667 | { |
686 | { |
668 | int i; |
687 | unsigned int i; |
669 | for (i = 3; i < SLOT_COUNT; ++i) |
688 | for (i = 3; i < SLOT_COUNT; ++i) |
670 | CXINC; |
689 | CXINC; |
671 | } |
690 | } |
672 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
691 | cxstack_ix -= SLOT_COUNT; /* undo allocation */ |
673 | |
692 | |
… | |
… | |
978 | |
997 | |
979 | PL_op = (OP *)&coro_setup_op; |
998 | PL_op = (OP *)&coro_setup_op; |
980 | |
999 | |
981 | /* copy throw, in case it was set before coro_setup */ |
1000 | /* copy throw, in case it was set before coro_setup */ |
982 | CORO_THROW = coro->except; |
1001 | CORO_THROW = coro->except; |
|
|
1002 | |
|
|
1003 | if (expect_false (enable_times)) |
|
|
1004 | { |
|
|
1005 | coro_times_update (); |
|
|
1006 | coro_times_sub (coro); |
|
|
1007 | } |
983 | } |
1008 | } |
984 | |
1009 | |
985 | static void |
1010 | static void |
986 | coro_unwind_stacks (pTHX) |
1011 | coro_unwind_stacks (pTHX) |
987 | { |
1012 | { |
… | |
… | |
1033 | |
1058 | |
1034 | load_perl (aTHX_ current); |
1059 | load_perl (aTHX_ current); |
1035 | } |
1060 | } |
1036 | |
1061 | |
1037 | { |
1062 | { |
1038 | int i; |
1063 | unsigned int i; |
1039 | |
1064 | |
1040 | for (i = 0; i < sizeof (svf) / sizeof (*svf); ++i) |
1065 | for (i = 0; i < sizeof (svf) / sizeof (*svf); ++i) |
1041 | SvREFCNT_dec (svf [i]); |
1066 | SvREFCNT_dec (svf [i]); |
1042 | |
1067 | |
1043 | SvREFCNT_dec (coro->saved_deffh); |
1068 | SvREFCNT_dec (coro->saved_deffh); |
… | |
… | |
1249 | /* 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 */ |
1250 | PL_restartop = PL_op; |
1275 | PL_restartop = PL_op; |
1251 | perl_run (PL_curinterp); |
1276 | perl_run (PL_curinterp); |
1252 | /* |
1277 | /* |
1253 | * 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 |
1254 | * 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. |
1255 | */ |
1281 | */ |
1256 | |
1282 | |
1257 | /* |
1283 | /* |
1258 | * 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 |
1259 | * 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) |
… | |
… | |
1608 | |
1634 | |
1609 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1635 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1610 | TRANSFER (ta, 1); |
1636 | TRANSFER (ta, 1); |
1611 | } |
1637 | } |
1612 | |
1638 | |
1613 | /*****************************************************************************/ |
|
|
1614 | /* gensub: simple closure generation utility */ |
|
|
1615 | |
|
|
1616 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
|
|
1617 | |
|
|
1618 | /* create a closure from XS, returns a code reference */ |
|
|
1619 | /* the arg can be accessed via GENSUB_ARG from the callback */ |
|
|
1620 | /* the callback must use dXSARGS/XSRETURN */ |
|
|
1621 | static SV * |
|
|
1622 | gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
|
|
1623 | { |
|
|
1624 | CV *cv = (CV *)newSV (0); |
|
|
1625 | |
|
|
1626 | sv_upgrade ((SV *)cv, SVt_PVCV); |
|
|
1627 | |
|
|
1628 | CvANON_on (cv); |
|
|
1629 | CvISXSUB_on (cv); |
|
|
1630 | CvXSUB (cv) = xsub; |
|
|
1631 | GENSUB_ARG = arg; |
|
|
1632 | |
|
|
1633 | return newRV_noinc ((SV *)cv); |
|
|
1634 | } |
|
|
1635 | |
|
|
1636 | /** Coro ********************************************************************/ |
1639 | /** Coro ********************************************************************/ |
1637 | |
1640 | |
1638 | INLINE void |
1641 | INLINE void |
1639 | coro_enq (pTHX_ struct coro *coro) |
1642 | coro_enq (pTHX_ struct coro *coro) |
1640 | { |
1643 | { |
… | |
… | |
1665 | } |
1668 | } |
1666 | |
1669 | |
1667 | return 0; |
1670 | return 0; |
1668 | } |
1671 | } |
1669 | |
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 | |
1670 | static int |
1690 | static int |
1671 | api_ready (pTHX_ SV *coro_sv) |
1691 | api_ready (pTHX_ SV *coro_sv) |
1672 | { |
1692 | { |
1673 | struct coro *coro; |
|
|
1674 | SV *sv_hook; |
|
|
1675 | void (*xs_hook)(void); |
|
|
1676 | |
|
|
1677 | coro = SvSTATE (coro_sv); |
1693 | struct coro *coro = SvSTATE (coro_sv); |
1678 | |
1694 | |
1679 | if (coro->flags & CF_READY) |
1695 | if (coro->flags & CF_READY) |
1680 | return 0; |
1696 | return 0; |
1681 | |
1697 | |
1682 | coro->flags |= CF_READY; |
1698 | coro->flags |= CF_READY; |
1683 | |
1699 | |
1684 | sv_hook = coro_nready ? 0 : coro_readyhook; |
|
|
1685 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
|
|
1686 | |
|
|
1687 | coro_enq (aTHX_ coro); |
1700 | coro_enq (aTHX_ coro); |
1688 | ++coro_nready; |
|
|
1689 | |
1701 | |
1690 | if (sv_hook) |
1702 | if (!coro_nready++) |
1691 | { |
1703 | if (coroapi.readyhook) |
1692 | dSP; |
1704 | coroapi.readyhook (); |
1693 | |
|
|
1694 | ENTER; |
|
|
1695 | SAVETMPS; |
|
|
1696 | |
|
|
1697 | PUSHMARK (SP); |
|
|
1698 | PUTBACK; |
|
|
1699 | call_sv (sv_hook, G_VOID | G_DISCARD); |
|
|
1700 | |
|
|
1701 | FREETMPS; |
|
|
1702 | LEAVE; |
|
|
1703 | } |
|
|
1704 | |
|
|
1705 | if (xs_hook) |
|
|
1706 | xs_hook (); |
|
|
1707 | |
1705 | |
1708 | return 1; |
1706 | return 1; |
1709 | } |
1707 | } |
1710 | |
1708 | |
1711 | static int |
1709 | static int |
… | |
… | |
1762 | api_ready (aTHX_ SvRV (sv_idle)); |
1760 | api_ready (aTHX_ SvRV (sv_idle)); |
1763 | --coro_nready; |
1761 | --coro_nready; |
1764 | } |
1762 | } |
1765 | else |
1763 | else |
1766 | { |
1764 | { |
|
|
1765 | /* TODO: deprecated, remove, cannot work reliably *//*D*/ |
1767 | dSP; |
1766 | dSP; |
1768 | |
1767 | |
1769 | ENTER; |
1768 | ENTER; |
1770 | SAVETMPS; |
1769 | SAVETMPS; |
1771 | |
1770 | |
… | |
… | |
1917 | { |
1916 | { |
1918 | int i; |
1917 | int i; |
1919 | HV *hv = (HV *)SvRV (coro_current); |
1918 | HV *hv = (HV *)SvRV (coro_current); |
1920 | AV *av = newAV (); |
1919 | AV *av = newAV (); |
1921 | |
1920 | |
|
|
1921 | /* items are actually not so common, so optimise for this case */ |
|
|
1922 | if (items) |
|
|
1923 | { |
1922 | av_extend (av, items - 1); |
1924 | av_extend (av, items - 1); |
|
|
1925 | |
1923 | for (i = 0; i < items; ++i) |
1926 | for (i = 0; i < items; ++i) |
1924 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
1927 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
|
|
1928 | } |
1925 | |
1929 | |
1926 | 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); |
1927 | |
1931 | |
1928 | 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 */ |
1929 | api_ready (aTHX_ sv_manager); |
1933 | api_ready (aTHX_ sv_manager); |
… | |
… | |
2019 | |
2023 | |
2020 | static void |
2024 | static void |
2021 | coro_rouse_callback (pTHX_ CV *cv) |
2025 | coro_rouse_callback (pTHX_ CV *cv) |
2022 | { |
2026 | { |
2023 | dXSARGS; |
2027 | dXSARGS; |
2024 | SV *data = (SV *)GENSUB_ARG; |
2028 | SV *data = (SV *)S_GENSUB_ARG; |
2025 | |
2029 | |
2026 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
2030 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
2027 | { |
2031 | { |
2028 | /* first call, set args */ |
2032 | /* first call, set args */ |
2029 | SV *coro = SvRV (data); |
2033 | SV *coro = SvRV (data); |
… | |
… | |
2095 | || SvTYPE (SvRV (cb)) != SVt_PVCV |
2099 | || SvTYPE (SvRV (cb)) != SVt_PVCV |
2096 | || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) |
2100 | || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) |
2097 | croak ("Coro::rouse_wait called with illegal callback argument,"); |
2101 | croak ("Coro::rouse_wait called with illegal callback argument,"); |
2098 | |
2102 | |
2099 | { |
2103 | { |
2100 | CV *cv = (CV *)SvRV (cb); /* for GENSUB_ARG */ |
2104 | CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */ |
2101 | SV *data = (SV *)GENSUB_ARG; |
2105 | SV *data = (SV *)S_GENSUB_ARG; |
2102 | |
2106 | |
2103 | frame->data = (void *)data; |
2107 | frame->data = (void *)data; |
2104 | frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule; |
2108 | frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule; |
2105 | frame->check = slf_check_rouse_wait; |
2109 | frame->check = slf_check_rouse_wait; |
2106 | } |
2110 | } |
… | |
… | |
2110 | coro_new_rouse_cb (pTHX) |
2114 | coro_new_rouse_cb (pTHX) |
2111 | { |
2115 | { |
2112 | HV *hv = (HV *)SvRV (coro_current); |
2116 | HV *hv = (HV *)SvRV (coro_current); |
2113 | struct coro *coro = SvSTATE_hv (hv); |
2117 | struct coro *coro = SvSTATE_hv (hv); |
2114 | SV *data = newRV_inc ((SV *)hv); |
2118 | SV *data = newRV_inc ((SV *)hv); |
2115 | SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data); |
2119 | SV *cb = s_gensub (aTHX_ coro_rouse_callback, (void *)data); |
2116 | |
2120 | |
2117 | 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); |
2118 | SvREFCNT_dec (data); /* magicext increases the refcount */ |
2122 | SvREFCNT_dec (data); /* magicext increases the refcount */ |
2119 | |
2123 | |
2120 | SvREFCNT_dec (coro->rouse_cb); |
2124 | SvREFCNT_dec (coro->rouse_cb); |
… | |
… | |
2327 | if (PL_op->op_ppaddr != PL_ppaddr [OP_ENTERSUB] |
2331 | if (PL_op->op_ppaddr != PL_ppaddr [OP_ENTERSUB] |
2328 | && PL_op->op_ppaddr != pp_slf) |
2332 | && PL_op->op_ppaddr != pp_slf) |
2329 | 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"); |
2330 | |
2334 | |
2331 | CvFLAGS (cv) |= CVf_SLF; |
2335 | CvFLAGS (cv) |= CVf_SLF; |
|
|
2336 | CvNODEBUG_on (cv); |
2332 | CvXSUBANY (cv).any_ptr = (void *)init_cb; |
2337 | CvXSUBANY (cv).any_ptr = (void *)init_cb; |
2333 | slf_cv = cv; |
2338 | slf_cv = cv; |
2334 | |
2339 | |
2335 | /* we patch the op, and then re-run the whole call */ |
2340 | /* we patch the op, and then re-run the whole call */ |
2336 | /* 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 */ |
… | |
… | |
2643 | { |
2648 | { |
2644 | if (items >= 2) |
2649 | if (items >= 2) |
2645 | { |
2650 | { |
2646 | /* callback form */ |
2651 | /* callback form */ |
2647 | AV *av = (AV *)SvRV (arg [0]); |
2652 | AV *av = (AV *)SvRV (arg [0]); |
2648 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
2653 | SV *cb_cv = s_get_cv_croak (arg [1]); |
2649 | |
2654 | |
2650 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2655 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2651 | |
2656 | |
2652 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2657 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2653 | coro_semaphore_adjust (aTHX_ av, 0); |
2658 | coro_semaphore_adjust (aTHX_ av, 0); |
… | |
… | |
2713 | { |
2718 | { |
2714 | AV *av = (AV *)SvRV (arg [0]); |
2719 | AV *av = (AV *)SvRV (arg [0]); |
2715 | |
2720 | |
2716 | if (items >= 2) |
2721 | if (items >= 2) |
2717 | { |
2722 | { |
2718 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
2723 | SV *cb_cv = s_get_cv_croak (arg [1]); |
2719 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2724 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2720 | |
2725 | |
2721 | if (SvIVX (AvARRAY (av)[0])) |
2726 | if (SvIVX (AvARRAY (av)[0])) |
2722 | coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ |
2727 | coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ |
2723 | |
2728 | |
… | |
… | |
2758 | |
2763 | |
2759 | static void |
2764 | static void |
2760 | coro_aio_callback (pTHX_ CV *cv) |
2765 | coro_aio_callback (pTHX_ CV *cv) |
2761 | { |
2766 | { |
2762 | dXSARGS; |
2767 | dXSARGS; |
2763 | AV *state = (AV *)GENSUB_ARG; |
2768 | AV *state = (AV *)S_GENSUB_ARG; |
2764 | SV *coro = av_pop (state); |
2769 | SV *coro = av_pop (state); |
2765 | SV *data_sv = newSV (sizeof (struct io_state)); |
2770 | SV *data_sv = newSV (sizeof (struct io_state)); |
2766 | |
2771 | |
2767 | av_extend (state, items - 1); |
2772 | av_extend (state, items - 1); |
2768 | |
2773 | |
… | |
… | |
2882 | |
2887 | |
2883 | for (i = 0; i < items; ++i) |
2888 | for (i = 0; i < items; ++i) |
2884 | PUSHs (arg [i]); |
2889 | PUSHs (arg [i]); |
2885 | |
2890 | |
2886 | /* now push the callback closure */ |
2891 | /* now push the callback closure */ |
2887 | 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)))); |
2888 | |
2893 | |
2889 | /* now call the AIO function - we assume our request is uncancelable */ |
2894 | /* now call the AIO function - we assume our request is uncancelable */ |
2890 | PUTBACK; |
2895 | PUTBACK; |
2891 | call_sv ((SV *)req, G_VOID | G_DISCARD); |
2896 | call_sv ((SV *)req, G_VOID | G_DISCARD); |
2892 | } |
2897 | } |
… | |
… | |
2910 | /*****************************************************************************/ |
2915 | /*****************************************************************************/ |
2911 | |
2916 | |
2912 | #if CORO_CLONE |
2917 | #if CORO_CLONE |
2913 | # include "clone.c" |
2918 | # include "clone.c" |
2914 | #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 | } |
2915 | |
2978 | |
2916 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2979 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2917 | |
2980 | |
2918 | PROTOTYPES: DISABLE |
2981 | PROTOTYPES: DISABLE |
2919 | |
2982 | |
… | |
… | |
2979 | |
3042 | |
2980 | if (!svp) croak ("Time::HiRes is required"); |
3043 | if (!svp) croak ("Time::HiRes is required"); |
2981 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
3044 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
2982 | |
3045 | |
2983 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
3046 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
|
|
3047 | |
|
|
3048 | svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0); |
|
|
3049 | u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp)); |
2984 | } |
3050 | } |
2985 | |
3051 | |
2986 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3052 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
2987 | } |
3053 | } |
2988 | |
3054 | |
2989 | SV * |
3055 | SV * |
2990 | new (char *klass, ...) |
3056 | new (SV *klass, ...) |
2991 | ALIAS: |
3057 | ALIAS: |
2992 | Coro::new = 1 |
3058 | Coro::new = 1 |
2993 | CODE: |
3059 | CODE: |
2994 | { |
3060 | RETVAL = coro_new (aTHX_ ix ? coro_stash : coro_state_stash, &ST (1), items - 1, ix); |
2995 | struct coro *coro; |
3061 | OUTPUT: |
2996 | MAGIC *mg; |
|
|
2997 | HV *hv; |
|
|
2998 | CV *cb; |
|
|
2999 | int i; |
|
|
3000 | |
|
|
3001 | if (items > 1) |
|
|
3002 | { |
|
|
3003 | cb = coro_sv_2cv (aTHX_ ST (1)); |
|
|
3004 | |
|
|
3005 | if (!ix) |
|
|
3006 | { |
|
|
3007 | if (CvISXSUB (cb)) |
|
|
3008 | croak ("Coro::State doesn't support XS functions as coroutine start, caught"); |
|
|
3009 | |
|
|
3010 | if (!CvROOT (cb)) |
|
|
3011 | croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); |
|
|
3012 | } |
|
|
3013 | } |
|
|
3014 | |
|
|
3015 | Newz (0, coro, 1, struct coro); |
|
|
3016 | coro->args = newAV (); |
|
|
3017 | coro->flags = CF_NEW; |
|
|
3018 | |
|
|
3019 | if (coro_first) coro_first->prev = coro; |
|
|
3020 | coro->next = coro_first; |
|
|
3021 | coro_first = coro; |
|
|
3022 | |
|
|
3023 | coro->hv = hv = newHV (); |
|
|
3024 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
|
|
3025 | mg->mg_flags |= MGf_DUP; |
|
|
3026 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
|
|
3027 | |
|
|
3028 | if (items > 1) |
|
|
3029 | { |
|
|
3030 | av_extend (coro->args, items - 1 + ix - 1); |
|
|
3031 | |
|
|
3032 | if (ix) |
|
|
3033 | { |
|
|
3034 | av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); |
|
|
3035 | cb = cv_coro_run; |
|
|
3036 | } |
|
|
3037 | |
|
|
3038 | coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); |
|
|
3039 | |
|
|
3040 | for (i = 2; i < items; i++) |
|
|
3041 | av_push (coro->args, newSVsv (ST (i))); |
|
|
3042 | } |
|
|
3043 | } |
|
|
3044 | OUTPUT: |
|
|
3045 | RETVAL |
3062 | RETVAL |
3046 | |
3063 | |
3047 | void |
3064 | void |
3048 | transfer (...) |
3065 | transfer (...) |
3049 | PROTOTYPE: $$ |
3066 | PROTOTYPE: $$ |
… | |
… | |
3262 | CODE: |
3279 | CODE: |
3263 | coro_state_destroy (aTHX_ self); |
3280 | coro_state_destroy (aTHX_ self); |
3264 | coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */ |
3281 | coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */ |
3265 | |
3282 | |
3266 | |
3283 | |
|
|
3284 | SV * |
|
|
3285 | enable_times (int enabled = enable_times) |
|
|
3286 | CODE: |
|
|
3287 | { |
|
|
3288 | RETVAL = boolSV (enable_times); |
|
|
3289 | |
|
|
3290 | if (enabled != enable_times) |
|
|
3291 | { |
|
|
3292 | enable_times = enabled; |
|
|
3293 | |
|
|
3294 | coro_times_update (); |
|
|
3295 | (enabled ? coro_times_sub : coro_times_add)(SvSTATE (coro_current)); |
|
|
3296 | } |
|
|
3297 | } |
|
|
3298 | OUTPUT: |
|
|
3299 | RETVAL |
|
|
3300 | |
|
|
3301 | void |
|
|
3302 | times (Coro::State self) |
|
|
3303 | PPCODE: |
|
|
3304 | { |
|
|
3305 | struct coro *current = SvSTATE (coro_current); |
|
|
3306 | |
|
|
3307 | if (expect_false (current == self)) |
|
|
3308 | { |
|
|
3309 | coro_times_update (); |
|
|
3310 | coro_times_add (SvSTATE (coro_current)); |
|
|
3311 | } |
|
|
3312 | |
|
|
3313 | EXTEND (SP, 2); |
|
|
3314 | PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9))); |
|
|
3315 | PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); |
|
|
3316 | |
|
|
3317 | if (expect_false (current == self)) |
|
|
3318 | coro_times_sub (SvSTATE (coro_current)); |
|
|
3319 | } |
|
|
3320 | |
3267 | MODULE = Coro::State PACKAGE = Coro |
3321 | MODULE = Coro::State PACKAGE = Coro |
3268 | |
3322 | |
3269 | BOOT: |
3323 | BOOT: |
3270 | { |
3324 | { |
3271 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
3325 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
… | |
… | |
3279 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3333 | sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); |
3280 | |
3334 | |
3281 | 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); |
3282 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3336 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
3283 | 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); |
3284 | 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 */ |
3285 | |
3339 | |
3286 | coro_stash = gv_stashpv ("Coro", TRUE); |
3340 | coro_stash = gv_stashpv ("Coro", TRUE); |
3287 | |
3341 | |
3288 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3342 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); |
3289 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3343 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); |
3290 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
3344 | newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); |
… | |
… | |
3308 | sv_setiv (sv, (IV)&coroapi); |
3362 | sv_setiv (sv, (IV)&coroapi); |
3309 | SvREADONLY_on (sv); |
3363 | SvREADONLY_on (sv); |
3310 | } |
3364 | } |
3311 | } |
3365 | } |
3312 | |
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 | |
3313 | void |
3376 | void |
3314 | terminate (...) |
3377 | terminate (...) |
3315 | CODE: |
3378 | CODE: |
3316 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
3379 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
3317 | |
3380 | |
… | |
… | |
3351 | _set_readyhook (SV *hook) |
3414 | _set_readyhook (SV *hook) |
3352 | PROTOTYPE: $ |
3415 | PROTOTYPE: $ |
3353 | CODE: |
3416 | CODE: |
3354 | SvREFCNT_dec (coro_readyhook); |
3417 | SvREFCNT_dec (coro_readyhook); |
3355 | SvGETMAGIC (hook); |
3418 | SvGETMAGIC (hook); |
|
|
3419 | if (SvOK (hook)) |
|
|
3420 | { |
3356 | 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 | } |
3357 | |
3429 | |
3358 | int |
3430 | int |
3359 | prio (Coro::State coro, int newprio = 0) |
3431 | prio (Coro::State coro, int newprio = 0) |
3360 | PROTOTYPE: $;$ |
3432 | PROTOTYPE: $;$ |
3361 | ALIAS: |
3433 | ALIAS: |
… | |
… | |
3367 | if (items > 1) |
3439 | if (items > 1) |
3368 | { |
3440 | { |
3369 | if (ix) |
3441 | if (ix) |
3370 | newprio = coro->prio - newprio; |
3442 | newprio = coro->prio - newprio; |
3371 | |
3443 | |
3372 | if (newprio < PRIO_MIN) newprio = PRIO_MIN; |
3444 | if (newprio < CORO_PRIO_MIN) newprio = CORO_PRIO_MIN; |
3373 | if (newprio > PRIO_MAX) newprio = PRIO_MAX; |
3445 | if (newprio > CORO_PRIO_MAX) newprio = CORO_PRIO_MAX; |
3374 | |
3446 | |
3375 | coro->prio = newprio; |
3447 | coro->prio = newprio; |
3376 | } |
3448 | } |
3377 | } |
3449 | } |
3378 | OUTPUT: |
3450 | OUTPUT: |
… | |
… | |
3425 | for (i = 1; i < items; ++i) |
3497 | for (i = 1; i < items; ++i) |
3426 | av_push (av, SvREFCNT_inc_NN (ST (i))); |
3498 | av_push (av, SvREFCNT_inc_NN (ST (i))); |
3427 | |
3499 | |
3428 | if ((SV *)hv == &PL_sv_undef) |
3500 | if ((SV *)hv == &PL_sv_undef) |
3429 | { |
3501 | { |
3430 | PUSHMARK (SP); |
3502 | SV *sv = coro_new (aTHX_ coro_stash, (SV **)&cv_pool_handler, 1, 1); |
3431 | EXTEND (SP, 2); |
|
|
3432 | PUSHs (sv_Coro); |
|
|
3433 | PUSHs ((SV *)cv_pool_handler); |
|
|
3434 | PUTBACK; |
|
|
3435 | call_sv ((SV *)cv_coro_state_new, G_SCALAR); |
|
|
3436 | SPAGAIN; |
|
|
3437 | |
|
|
3438 | hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); |
3503 | hv = (HV *)SvREFCNT_inc_NN (SvRV (sv)); |
|
|
3504 | SvREFCNT_dec (sv); |
3439 | } |
3505 | } |
3440 | |
3506 | |
3441 | { |
3507 | { |
3442 | struct coro *coro = SvSTATE_hv (hv); |
3508 | struct coro *coro = SvSTATE_hv (hv); |
3443 | |
3509 | |
… | |
… | |
3477 | CODE: |
3543 | CODE: |
3478 | { |
3544 | { |
3479 | struct coro *coro = SvSTATE_current; |
3545 | struct coro *coro = SvSTATE_current; |
3480 | AV **avp = ix ? &coro->on_leave : &coro->on_enter; |
3546 | AV **avp = ix ? &coro->on_leave : &coro->on_enter; |
3481 | |
3547 | |
3482 | block = (SV *)coro_sv_2cv (aTHX_ block); |
3548 | block = s_get_cv_croak (block); |
3483 | |
3549 | |
3484 | if (!*avp) |
3550 | if (!*avp) |
3485 | *avp = newAV (); |
3551 | *avp = newAV (); |
3486 | |
3552 | |
3487 | av_push (*avp, SvREFCNT_inc (block)); |
3553 | av_push (*avp, SvREFCNT_inc (block)); |
… | |
… | |
3687 | |
3753 | |
3688 | void |
3754 | void |
3689 | _register (char *target, char *proto, SV *req) |
3755 | _register (char *target, char *proto, SV *req) |
3690 | CODE: |
3756 | CODE: |
3691 | { |
3757 | { |
3692 | CV *req_cv = coro_sv_2cv (aTHX_ req); |
3758 | SV *req_cv = s_get_cv_croak (req); |
3693 | /* newXSproto doesn't return the CV on 5.8 */ |
3759 | /* newXSproto doesn't return the CV on 5.8 */ |
3694 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3760 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3695 | sv_setpv ((SV *)slf_cv, proto); |
3761 | sv_setpv ((SV *)slf_cv, proto); |
3696 | 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); |
3697 | } |
3763 | } |
3698 | |
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 | |