… | |
… | |
16 | |
16 | |
17 | #ifdef WIN32 |
17 | #ifdef WIN32 |
18 | # undef setjmp |
18 | # undef setjmp |
19 | # undef longjmp |
19 | # undef longjmp |
20 | # undef _exit |
20 | # undef _exit |
21 | # define setjmp _setjmp // deep magic, don't ask |
21 | # define setjmp _setjmp /* deep magic */ |
22 | #else |
22 | #else |
23 | # include <inttypes.h> /* most portable stdint.h */ |
23 | # include <inttypes.h> /* most portable stdint.h */ |
24 | #endif |
24 | #endif |
25 | |
25 | |
26 | #ifdef HAVE_MMAP |
26 | #ifdef HAVE_MMAP |
… | |
… | |
55 | |
55 | |
56 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
56 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
57 | (PERL_REVISION > (a) \ |
57 | (PERL_REVISION > (a) \ |
58 | || (PERL_REVISION == (a) \ |
58 | || (PERL_REVISION == (a) \ |
59 | && (PERL_VERSION > (b) \ |
59 | && (PERL_VERSION > (b) \ |
60 | || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) |
60 | || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c))))) |
61 | |
61 | |
62 | #if !PERL_VERSION_ATLEAST (5,6,0) |
62 | #if !PERL_VERSION_ATLEAST (5,6,0) |
63 | # ifndef PL_ppaddr |
63 | # ifndef PL_ppaddr |
64 | # define PL_ppaddr ppaddr |
64 | # define PL_ppaddr ppaddr |
65 | # endif |
65 | # endif |
… | |
… | |
176 | static HV *hv_sig; /* %SIG */ |
176 | static HV *hv_sig; /* %SIG */ |
177 | |
177 | |
178 | /* async_pool helper stuff */ |
178 | /* async_pool helper stuff */ |
179 | static SV *sv_pool_rss; |
179 | static SV *sv_pool_rss; |
180 | static SV *sv_pool_size; |
180 | static SV *sv_pool_size; |
|
|
181 | static SV *sv_async_pool_idle; |
181 | static AV *av_async_pool; |
182 | static AV *av_async_pool; |
|
|
183 | static SV *sv_Coro; |
|
|
184 | static CV *cv_pool_handler; |
|
|
185 | static CV *cv_coro_new; |
182 | |
186 | |
183 | /* Coro::AnyEvent */ |
187 | /* Coro::AnyEvent */ |
184 | static SV *sv_activity; |
188 | static SV *sv_activity; |
185 | |
189 | |
186 | static struct coro_cctx *cctx_first; |
190 | static struct coro_cctx *cctx_first; |
… | |
… | |
246 | /* state data */ |
250 | /* state data */ |
247 | struct CoroSLF slf_frame; /* saved slf frame */ |
251 | struct CoroSLF slf_frame; /* saved slf frame */ |
248 | AV *mainstack; |
252 | AV *mainstack; |
249 | perl_slots *slot; /* basically the saved sp */ |
253 | perl_slots *slot; /* basically the saved sp */ |
250 | |
254 | |
|
|
255 | CV *startcv; /* the CV to execute */ |
251 | AV *args; /* data associated with this coroutine (initial args) */ |
256 | AV *args; /* data associated with this coroutine (initial args) */ |
252 | int refcnt; /* coroutines are refcounted, yes */ |
257 | int refcnt; /* coroutines are refcounted, yes */ |
253 | int flags; /* CF_ flags */ |
258 | int flags; /* CF_ flags */ |
254 | HV *hv; /* the perl hash associated with this coro, if any */ |
259 | HV *hv; /* the perl hash associated with this coro, if any */ |
255 | void (*on_destroy)(pTHX_ struct coro *coro); |
260 | void (*on_destroy)(pTHX_ struct coro *coro); |
256 | |
261 | |
257 | /* statistics */ |
262 | /* statistics */ |
258 | int usecount; /* number of transfers to this coro */ |
263 | int usecount; /* number of transfers to this coro */ |
259 | |
264 | |
260 | /* coro process data */ |
265 | /* coro process data */ |
261 | int prio; |
266 | int prio; |
262 | SV *except; /* exception to be thrown */ |
267 | SV *except; /* exception to be thrown */ |
|
|
268 | SV *rouse_cb; |
263 | |
269 | |
264 | /* async_pool */ |
270 | /* async_pool */ |
265 | SV *saved_deffh; |
271 | SV *saved_deffh; |
|
|
272 | SV *invoke_cb; |
|
|
273 | AV *invoke_av; |
266 | |
274 | |
267 | /* linked list */ |
275 | /* linked list */ |
268 | struct coro *next, *prev; |
276 | struct coro *next, *prev; |
269 | }; |
277 | }; |
270 | |
278 | |
… | |
… | |
287 | |
295 | |
288 | /* for Coro.pm */ |
296 | /* for Coro.pm */ |
289 | static SV *coro_current; |
297 | static SV *coro_current; |
290 | static SV *coro_readyhook; |
298 | static SV *coro_readyhook; |
291 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
299 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
|
|
300 | static CV *cv_coro_run, *cv_coro_terminate; |
292 | static struct coro *coro_first; |
301 | static struct coro *coro_first; |
293 | #define coro_nready coroapi.nready |
302 | #define coro_nready coroapi.nready |
294 | |
303 | |
295 | /** lowlevel stuff **********************************************************/ |
304 | /** lowlevel stuff **********************************************************/ |
296 | |
305 | |
… | |
… | |
320 | #if PERL_VERSION_ATLEAST (5,10,0) |
329 | #if PERL_VERSION_ATLEAST (5,10,0) |
321 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
330 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
322 | get_hv (name, create); |
331 | get_hv (name, create); |
323 | #endif |
332 | #endif |
324 | return get_hv (name, create); |
333 | return get_hv (name, create); |
|
|
334 | } |
|
|
335 | |
|
|
336 | /* may croak */ |
|
|
337 | INLINE CV * |
|
|
338 | coro_sv_2cv (pTHX_ SV *sv) |
|
|
339 | { |
|
|
340 | HV *st; |
|
|
341 | GV *gvp; |
|
|
342 | return sv_2cv (sv, &st, &gvp, 0); |
325 | } |
343 | } |
326 | |
344 | |
327 | static AV * |
345 | static AV * |
328 | coro_clone_padlist (pTHX_ CV *cv) |
346 | coro_clone_padlist (pTHX_ CV *cv) |
329 | { |
347 | { |
… | |
… | |
448 | else |
466 | else |
449 | { |
467 | { |
450 | #if CORO_PREFER_PERL_FUNCTIONS |
468 | #if CORO_PREFER_PERL_FUNCTIONS |
451 | /* this is probably cleaner? but also slower! */ |
469 | /* this is probably cleaner? but also slower! */ |
452 | /* in practise, it seems to be less stable */ |
470 | /* in practise, it seems to be less stable */ |
453 | CV *cp = Perl_cv_clone (cv); |
471 | CV *cp = Perl_cv_clone (aTHX_ cv); |
454 | CvPADLIST (cv) = CvPADLIST (cp); |
472 | CvPADLIST (cv) = CvPADLIST (cp); |
455 | CvPADLIST (cp) = 0; |
473 | CvPADLIST (cp) = 0; |
456 | SvREFCNT_dec (cp); |
474 | SvREFCNT_dec (cp); |
457 | #else |
475 | #else |
458 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
476 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
… | |
… | |
602 | * of perl.c:init_stacks, except that it uses less memory |
620 | * of perl.c:init_stacks, except that it uses less memory |
603 | * on the (sometimes correct) assumption that coroutines do |
621 | * on the (sometimes correct) assumption that coroutines do |
604 | * not usually need a lot of stackspace. |
622 | * not usually need a lot of stackspace. |
605 | */ |
623 | */ |
606 | #if CORO_PREFER_PERL_FUNCTIONS |
624 | #if CORO_PREFER_PERL_FUNCTIONS |
607 | # define coro_init_stacks init_stacks |
625 | # define coro_init_stacks(thx) init_stacks () |
608 | #else |
626 | #else |
609 | static void |
627 | static void |
610 | coro_init_stacks (pTHX) |
628 | coro_init_stacks (pTHX) |
611 | { |
629 | { |
612 | PL_curstackinfo = new_stackinfo(32, 8); |
630 | PL_curstackinfo = new_stackinfo(32, 8); |
… | |
… | |
675 | #if !PERL_VERSION_ATLEAST (5,10,0) |
693 | #if !PERL_VERSION_ATLEAST (5,10,0) |
676 | Safefree (PL_retstack); |
694 | Safefree (PL_retstack); |
677 | #endif |
695 | #endif |
678 | } |
696 | } |
679 | |
697 | |
|
|
698 | #define CORO_RSS \ |
|
|
699 | rss += sizeof (SYM (curstackinfo)); \ |
|
|
700 | rss += (SYM (curstackinfo->si_cxmax) + 1) * sizeof (PERL_CONTEXT); \ |
|
|
701 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (SYM (curstack))) * sizeof (SV *); \ |
|
|
702 | rss += SYM (tmps_max) * sizeof (SV *); \ |
|
|
703 | rss += (SYM (markstack_max) - SYM (markstack_ptr)) * sizeof (I32); \ |
|
|
704 | rss += SYM (scopestack_max) * sizeof (I32); \ |
|
|
705 | rss += SYM (savestack_max) * sizeof (ANY); |
|
|
706 | |
680 | static size_t |
707 | static size_t |
681 | coro_rss (pTHX_ struct coro *coro) |
708 | coro_rss (pTHX_ struct coro *coro) |
682 | { |
709 | { |
683 | size_t rss = sizeof (*coro); |
710 | size_t rss = sizeof (*coro); |
684 | |
711 | |
685 | if (coro->mainstack) |
712 | if (coro->mainstack) |
686 | { |
713 | { |
687 | perl_slots tmp_slot; |
|
|
688 | perl_slots *slot; |
|
|
689 | |
|
|
690 | if (coro->flags & CF_RUNNING) |
714 | if (coro->flags & CF_RUNNING) |
691 | { |
715 | { |
692 | slot = &tmp_slot; |
716 | #define SYM(sym) PL_ ## sym |
693 | |
717 | CORO_RSS; |
694 | #define VAR(name,type) slot->name = PL_ ## name; |
|
|
695 | # include "state.h" |
|
|
696 | #undef VAR |
718 | #undef SYM |
697 | } |
719 | } |
698 | else |
720 | else |
699 | slot = coro->slot; |
|
|
700 | |
|
|
701 | if (slot) |
|
|
702 | { |
721 | { |
703 | rss += sizeof (slot->curstackinfo); |
722 | #define SYM(sym) coro->slot->sym |
704 | rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
723 | CORO_RSS; |
705 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); |
724 | #undef SYM |
706 | rss += slot->tmps_max * sizeof (SV *); |
|
|
707 | rss += (slot->markstack_max - slot->markstack_ptr) * sizeof (I32); |
|
|
708 | rss += slot->scopestack_max * sizeof (I32); |
|
|
709 | rss += slot->savestack_max * sizeof (ANY); |
|
|
710 | |
|
|
711 | #if !PERL_VERSION_ATLEAST (5,10,0) |
|
|
712 | rss += slot->retstack_max * sizeof (OP *); |
|
|
713 | #endif |
|
|
714 | } |
725 | } |
715 | } |
726 | } |
716 | |
727 | |
717 | return rss; |
728 | return rss; |
718 | } |
729 | } |
… | |
… | |
860 | dSP; |
871 | dSP; |
861 | UNOP myop; |
872 | UNOP myop; |
862 | |
873 | |
863 | Zero (&myop, 1, UNOP); |
874 | Zero (&myop, 1, UNOP); |
864 | myop.op_next = Nullop; |
875 | myop.op_next = Nullop; |
|
|
876 | myop.op_type = OP_ENTERSUB; |
865 | myop.op_flags = OPf_WANT_VOID; |
877 | myop.op_flags = OPf_WANT_VOID; |
866 | |
878 | |
867 | PUSHMARK (SP); |
879 | PUSHMARK (SP); |
868 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
880 | PUSHs ((SV *)coro->startcv); |
869 | PUTBACK; |
881 | PUTBACK; |
870 | PL_op = (OP *)&myop; |
882 | PL_op = (OP *)&myop; |
871 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
883 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
872 | SPAGAIN; |
|
|
873 | } |
884 | } |
874 | |
885 | |
875 | /* this newly created coroutine might be run on an existing cctx which most |
886 | /* this newly created coroutine might be run on an existing cctx which most |
876 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
887 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
877 | */ |
888 | */ |
… | |
… | |
880 | |
891 | |
881 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
892 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
882 | coro_setup_op.op_next = PL_op; |
893 | coro_setup_op.op_next = PL_op; |
883 | coro_setup_op.op_type = OP_CUSTOM; |
894 | coro_setup_op.op_type = OP_CUSTOM; |
884 | coro_setup_op.op_ppaddr = pp_slf; |
895 | coro_setup_op.op_ppaddr = pp_slf; |
885 | /* no flags required, as an init function won't be called */ |
896 | /* no flags etc. required, as an init function won't be called */ |
886 | |
897 | |
887 | PL_op = (OP *)&coro_setup_op; |
898 | PL_op = (OP *)&coro_setup_op; |
888 | |
899 | |
889 | /* copy throw, in case it was set before coro_setup */ |
900 | /* copy throw, in case it was set before coro_setup */ |
890 | CORO_THROW = coro->except; |
901 | CORO_THROW = coro->except; |
… | |
… | |
919 | |
930 | |
920 | SvREFCNT_dec (PL_diehook); |
931 | SvREFCNT_dec (PL_diehook); |
921 | SvREFCNT_dec (PL_warnhook); |
932 | SvREFCNT_dec (PL_warnhook); |
922 | |
933 | |
923 | SvREFCNT_dec (coro->saved_deffh); |
934 | SvREFCNT_dec (coro->saved_deffh); |
924 | SvREFCNT_dec (CORO_THROW); |
935 | SvREFCNT_dec (coro->rouse_cb); |
|
|
936 | SvREFCNT_dec (coro->invoke_cb); |
|
|
937 | SvREFCNT_dec (coro->invoke_av); |
925 | |
938 | |
926 | coro_destruct_stacks (aTHX); |
939 | coro_destruct_stacks (aTHX); |
927 | } |
940 | } |
928 | |
941 | |
929 | INLINE void |
942 | INLINE void |
… | |
… | |
1016 | SAVETMPS; |
1029 | SAVETMPS; |
1017 | EXTEND (SP, 3); |
1030 | EXTEND (SP, 3); |
1018 | PUSHMARK (SP); |
1031 | PUSHMARK (SP); |
1019 | PUSHs (&PL_sv_yes); |
1032 | PUSHs (&PL_sv_yes); |
1020 | PUSHs (fullname); |
1033 | PUSHs (fullname); |
1021 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
1034 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
1022 | PUTBACK; |
1035 | PUTBACK; |
1023 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
1036 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
1024 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1037 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1025 | SPAGAIN; |
1038 | SPAGAIN; |
1026 | FREETMPS; |
1039 | FREETMPS; |
… | |
… | |
1132 | transfer_tail (aTHX); |
1145 | transfer_tail (aTHX); |
1133 | |
1146 | |
1134 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1147 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1135 | PL_restartop = PL_op; |
1148 | PL_restartop = PL_op; |
1136 | perl_run (PL_curinterp); |
1149 | perl_run (PL_curinterp); |
|
|
1150 | /* |
|
|
1151 | * Unfortunately, there is no way to get at the return values of the |
|
|
1152 | * coro body here, as perl_run destroys these |
|
|
1153 | */ |
1137 | |
1154 | |
1138 | /* |
1155 | /* |
1139 | * If perl-run returns we assume exit() was being called or the coro |
1156 | * If perl-run returns we assume exit() was being called or the coro |
1140 | * fell off the end, which seems to be the only valid (non-bug) |
1157 | * fell off the end, which seems to be the only valid (non-bug) |
1141 | * reason for perl_run to return. We try to exit by jumping to the |
1158 | * reason for perl_run to return. We try to exit by jumping to the |
… | |
… | |
1433 | |
1450 | |
1434 | coro->slot = 0; |
1451 | coro->slot = 0; |
1435 | } |
1452 | } |
1436 | |
1453 | |
1437 | cctx_destroy (coro->cctx); |
1454 | cctx_destroy (coro->cctx); |
|
|
1455 | SvREFCNT_dec (coro->startcv); |
1438 | SvREFCNT_dec (coro->args); |
1456 | SvREFCNT_dec (coro->args); |
|
|
1457 | SvREFCNT_dec (CORO_THROW); |
1439 | |
1458 | |
1440 | if (coro->next) coro->next->prev = coro->prev; |
1459 | if (coro->next) coro->next->prev = coro->prev; |
1441 | if (coro->prev) coro->prev->next = coro->next; |
1460 | if (coro->prev) coro->prev->next = coro->next; |
1442 | if (coro == coro_first) coro_first = coro->next; |
1461 | if (coro == coro_first) coro_first = coro->next; |
1443 | |
1462 | |
… | |
… | |
1497 | |
1516 | |
1498 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1517 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1499 | TRANSFER (ta, 1); |
1518 | TRANSFER (ta, 1); |
1500 | } |
1519 | } |
1501 | |
1520 | |
|
|
1521 | /*****************************************************************************/ |
|
|
1522 | /* gensub: simple closure generation utility */ |
|
|
1523 | |
|
|
1524 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
|
|
1525 | |
|
|
1526 | /* create a closure from XS, returns a code reference */ |
|
|
1527 | /* the arg can be accessed via GENSUB_ARG from the callback */ |
|
|
1528 | /* the callback must use dXSARGS/XSRETURN */ |
|
|
1529 | static SV * |
|
|
1530 | gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
|
|
1531 | { |
|
|
1532 | CV *cv = (CV *)newSV (0); |
|
|
1533 | |
|
|
1534 | sv_upgrade ((SV *)cv, SVt_PVCV); |
|
|
1535 | |
|
|
1536 | CvANON_on (cv); |
|
|
1537 | CvISXSUB_on (cv); |
|
|
1538 | CvXSUB (cv) = xsub; |
|
|
1539 | GENSUB_ARG = arg; |
|
|
1540 | |
|
|
1541 | return newRV_noinc ((SV *)cv); |
|
|
1542 | } |
|
|
1543 | |
1502 | /** Coro ********************************************************************/ |
1544 | /** Coro ********************************************************************/ |
1503 | |
1545 | |
1504 | INLINE void |
1546 | INLINE void |
1505 | coro_enq (pTHX_ struct coro *coro) |
1547 | coro_enq (pTHX_ struct coro *coro) |
1506 | { |
1548 | { |
… | |
… | |
1703 | if (coro->flags & CF_RUNNING) |
1745 | if (coro->flags & CF_RUNNING) |
1704 | PL_runops = RUNOPS_DEFAULT; |
1746 | PL_runops = RUNOPS_DEFAULT; |
1705 | else |
1747 | else |
1706 | coro->slot->runops = RUNOPS_DEFAULT; |
1748 | coro->slot->runops = RUNOPS_DEFAULT; |
1707 | } |
1749 | } |
|
|
1750 | } |
|
|
1751 | |
|
|
1752 | /*****************************************************************************/ |
|
|
1753 | /* async pool handler */ |
|
|
1754 | |
|
|
1755 | static int |
|
|
1756 | slf_check_pool_handler (pTHX_ struct CoroSLF *frame) |
|
|
1757 | { |
|
|
1758 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1759 | struct coro *coro = (struct coro *)frame->data; |
|
|
1760 | |
|
|
1761 | if (!coro->invoke_cb) |
|
|
1762 | return 1; /* loop till we have invoke */ |
|
|
1763 | else |
|
|
1764 | { |
|
|
1765 | int i, len; |
|
|
1766 | |
|
|
1767 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
1768 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
1769 | |
|
|
1770 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
|
|
1771 | |
|
|
1772 | len = av_len (coro->invoke_av); |
|
|
1773 | |
|
|
1774 | { |
|
|
1775 | dSP; |
|
|
1776 | XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0; |
|
|
1777 | PUTBACK; |
|
|
1778 | } |
|
|
1779 | |
|
|
1780 | SvREFCNT_dec (GvAV (PL_defgv)); |
|
|
1781 | GvAV (PL_defgv) = coro->invoke_av; |
|
|
1782 | coro->invoke_av = 0; |
|
|
1783 | |
|
|
1784 | return 0; |
|
|
1785 | } |
|
|
1786 | } |
|
|
1787 | |
|
|
1788 | static void |
|
|
1789 | slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1790 | { |
|
|
1791 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1792 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
1793 | |
|
|
1794 | if (expect_true (coro->saved_deffh)) |
|
|
1795 | { |
|
|
1796 | /* subsequent iteration */ |
|
|
1797 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
|
|
1798 | coro->saved_deffh = 0; |
|
|
1799 | |
|
|
1800 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
|
|
1801 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
1802 | { |
|
|
1803 | coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate); |
|
|
1804 | coro->invoke_av = newAV (); |
|
|
1805 | |
|
|
1806 | frame->prepare = prepare_nop; |
|
|
1807 | } |
|
|
1808 | else |
|
|
1809 | { |
|
|
1810 | av_clear (GvAV (PL_defgv)); |
|
|
1811 | hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); |
|
|
1812 | |
|
|
1813 | coro->prio = 0; |
|
|
1814 | |
|
|
1815 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
|
|
1816 | api_trace (aTHX_ coro_current, 0); |
|
|
1817 | |
|
|
1818 | frame->prepare = prepare_schedule; |
|
|
1819 | av_push (av_async_pool, SvREFCNT_inc (hv)); |
|
|
1820 | } |
|
|
1821 | } |
|
|
1822 | else |
|
|
1823 | { |
|
|
1824 | /* first iteration, simply fall through */ |
|
|
1825 | frame->prepare = prepare_nop; |
|
|
1826 | } |
|
|
1827 | |
|
|
1828 | frame->check = slf_check_pool_handler; |
|
|
1829 | frame->data = (void *)coro; |
|
|
1830 | } |
|
|
1831 | |
|
|
1832 | /*****************************************************************************/ |
|
|
1833 | /* rouse callback */ |
|
|
1834 | |
|
|
1835 | #define CORO_MAGIC_type_rouse PERL_MAGIC_ext |
|
|
1836 | |
|
|
1837 | static void |
|
|
1838 | coro_rouse_callback (pTHX_ CV *cv) |
|
|
1839 | { |
|
|
1840 | dXSARGS; |
|
|
1841 | SV *data = (SV *)GENSUB_ARG; |
|
|
1842 | |
|
|
1843 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
|
|
1844 | { |
|
|
1845 | /* first call, set args */ |
|
|
1846 | int i; |
|
|
1847 | AV *av = newAV (); |
|
|
1848 | SV *coro = SvRV (data); |
|
|
1849 | |
|
|
1850 | SvRV_set (data, (SV *)av); |
|
|
1851 | api_ready (aTHX_ coro); |
|
|
1852 | SvREFCNT_dec (coro); |
|
|
1853 | |
|
|
1854 | /* better take a full copy of the arguments */ |
|
|
1855 | while (items--) |
|
|
1856 | av_store (av, items, newSVsv (ST (items))); |
|
|
1857 | } |
|
|
1858 | |
|
|
1859 | XSRETURN_EMPTY; |
|
|
1860 | } |
|
|
1861 | |
|
|
1862 | static int |
|
|
1863 | slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) |
|
|
1864 | { |
|
|
1865 | SV *data = (SV *)frame->data; |
|
|
1866 | |
|
|
1867 | if (CORO_THROW) |
|
|
1868 | return 0; |
|
|
1869 | |
|
|
1870 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
|
|
1871 | return 1; |
|
|
1872 | |
|
|
1873 | /* now push all results on the stack */ |
|
|
1874 | { |
|
|
1875 | dSP; |
|
|
1876 | AV *av = (AV *)SvRV (data); |
|
|
1877 | int i; |
|
|
1878 | |
|
|
1879 | EXTEND (SP, AvFILLp (av) + 1); |
|
|
1880 | for (i = 0; i <= AvFILLp (av); ++i) |
|
|
1881 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
|
|
1882 | |
|
|
1883 | /* we have stolen the elements, so ste length to zero and free */ |
|
|
1884 | AvFILLp (av) = -1; |
|
|
1885 | av_undef (av); |
|
|
1886 | |
|
|
1887 | PUTBACK; |
|
|
1888 | } |
|
|
1889 | |
|
|
1890 | return 0; |
|
|
1891 | } |
|
|
1892 | |
|
|
1893 | static void |
|
|
1894 | slf_init_rouse_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1895 | { |
|
|
1896 | SV *cb; |
|
|
1897 | |
|
|
1898 | if (items) |
|
|
1899 | cb = arg [0]; |
|
|
1900 | else |
|
|
1901 | { |
|
|
1902 | struct coro *coro = SvSTATE_current; |
|
|
1903 | |
|
|
1904 | if (!coro->rouse_cb) |
|
|
1905 | croak ("Coro::rouse_wait called without rouse callback, and no default rouse callback found either,"); |
|
|
1906 | |
|
|
1907 | cb = sv_2mortal (coro->rouse_cb); |
|
|
1908 | coro->rouse_cb = 0; |
|
|
1909 | } |
|
|
1910 | |
|
|
1911 | if (!SvROK (cb) |
|
|
1912 | || SvTYPE (SvRV (cb)) != SVt_PVCV |
|
|
1913 | || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) |
|
|
1914 | croak ("Coro::rouse_wait called with illegal callback argument,"); |
|
|
1915 | |
|
|
1916 | { |
|
|
1917 | CV *cv = (CV *)SvRV (cb); /* for GENSUB_ARG */ |
|
|
1918 | SV *data = (SV *)GENSUB_ARG; |
|
|
1919 | |
|
|
1920 | frame->data = (void *)data; |
|
|
1921 | frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule; |
|
|
1922 | frame->check = slf_check_rouse_wait; |
|
|
1923 | } |
|
|
1924 | } |
|
|
1925 | |
|
|
1926 | static SV * |
|
|
1927 | coro_new_rouse_cb (pTHX) |
|
|
1928 | { |
|
|
1929 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1930 | struct coro *coro = SvSTATE_hv (hv); |
|
|
1931 | SV *data = newRV_inc ((SV *)hv); |
|
|
1932 | SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data); |
|
|
1933 | |
|
|
1934 | sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0); |
|
|
1935 | SvREFCNT_dec (data); /* magicext increases the refcount */ |
|
|
1936 | |
|
|
1937 | SvREFCNT_dec (coro->rouse_cb); |
|
|
1938 | coro->rouse_cb = SvREFCNT_inc_NN (cb); |
|
|
1939 | |
|
|
1940 | return cb; |
1708 | } |
1941 | } |
1709 | |
1942 | |
1710 | /*****************************************************************************/ |
1943 | /*****************************************************************************/ |
1711 | /* schedule-like-function opcode (SLF) */ |
1944 | /* schedule-like-function opcode (SLF) */ |
1712 | |
1945 | |
… | |
… | |
2007 | SV **ary; |
2240 | SV **ary; |
2008 | |
2241 | |
2009 | /* unfortunately, building manually saves memory */ |
2242 | /* unfortunately, building manually saves memory */ |
2010 | Newx (ary, 2, SV *); |
2243 | Newx (ary, 2, SV *); |
2011 | AvALLOC (av) = ary; |
2244 | AvALLOC (av) = ary; |
2012 | AvARRAY (av) = ary; |
2245 | /*AvARRAY (av) = ary;*/ |
|
|
2246 | SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ |
2013 | AvMAX (av) = 1; |
2247 | AvMAX (av) = 1; |
2014 | AvFILLp (av) = 0; |
2248 | AvFILLp (av) = 0; |
2015 | ary [0] = newSViv (count); |
2249 | ary [0] = newSViv (count); |
2016 | |
2250 | |
2017 | return newRV_noinc ((SV *)av); |
2251 | return newRV_noinc ((SV *)av); |
… | |
… | |
2037 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2271 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2038 | AvARRAY (av)[1] = count_sv; |
2272 | AvARRAY (av)[1] = count_sv; |
2039 | cb = av_shift (av); |
2273 | cb = av_shift (av); |
2040 | |
2274 | |
2041 | if (SvOBJECT (cb)) |
2275 | if (SvOBJECT (cb)) |
|
|
2276 | { |
2042 | api_ready (aTHX_ cb); |
2277 | api_ready (aTHX_ cb); |
2043 | else |
2278 | --count; |
2044 | croak ("callbacks not yet supported"); |
2279 | } |
|
|
2280 | else if (SvTYPE (cb) == SVt_PVCV) |
|
|
2281 | { |
|
|
2282 | dSP; |
|
|
2283 | PUSHMARK (SP); |
|
|
2284 | XPUSHs (sv_2mortal (newRV_inc ((SV *)av))); |
|
|
2285 | PUTBACK; |
|
|
2286 | call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR); |
|
|
2287 | } |
2045 | |
2288 | |
2046 | SvREFCNT_dec (cb); |
2289 | SvREFCNT_dec (cb); |
2047 | |
|
|
2048 | --count; |
|
|
2049 | } |
2290 | } |
2050 | } |
2291 | } |
2051 | |
2292 | |
2052 | static void |
2293 | static void |
2053 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
2294 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
… | |
… | |
2135 | } |
2376 | } |
2136 | |
2377 | |
2137 | static void |
2378 | static void |
2138 | slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2379 | slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2139 | { |
2380 | { |
|
|
2381 | if (items >= 2) |
|
|
2382 | { |
|
|
2383 | /* callback form */ |
|
|
2384 | AV *av = (AV *)SvRV (arg [0]); |
|
|
2385 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
|
|
2386 | |
|
|
2387 | av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); |
|
|
2388 | |
|
|
2389 | if (SvIVX (AvARRAY (av)[0]) > 0) |
|
|
2390 | coro_semaphore_adjust (aTHX_ av, 0); |
|
|
2391 | |
|
|
2392 | frame->prepare = prepare_nop; |
|
|
2393 | frame->check = slf_check_nop; |
|
|
2394 | } |
|
|
2395 | else |
|
|
2396 | { |
2140 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
2397 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
2141 | frame->check = slf_check_semaphore_wait; |
2398 | frame->check = slf_check_semaphore_wait; |
|
|
2399 | } |
2142 | } |
2400 | } |
2143 | |
2401 | |
2144 | /* signal */ |
2402 | /* signal */ |
2145 | |
2403 | |
2146 | static void |
2404 | static void |
… | |
… | |
2194 | |
2452 | |
2195 | frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ |
2453 | frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ |
2196 | frame->prepare = prepare_schedule; |
2454 | frame->prepare = prepare_schedule; |
2197 | frame->check = slf_check_signal_wait; |
2455 | frame->check = slf_check_signal_wait; |
2198 | } |
2456 | } |
2199 | } |
|
|
2200 | |
|
|
2201 | /*****************************************************************************/ |
|
|
2202 | /* gensub: simple closure generation utility */ |
|
|
2203 | |
|
|
2204 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
|
|
2205 | |
|
|
2206 | /* create a closure from XS, returns a code reference */ |
|
|
2207 | /* the arg can be accessed via GENSUB_ARG from the callback */ |
|
|
2208 | /* the callback must use dXSARGS/XSRETURN */ |
|
|
2209 | static SV * |
|
|
2210 | gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
|
|
2211 | { |
|
|
2212 | CV *cv = (CV *)newSV (0); |
|
|
2213 | |
|
|
2214 | sv_upgrade ((SV *)cv, SVt_PVCV); |
|
|
2215 | |
|
|
2216 | CvANON_on (cv); |
|
|
2217 | CvISXSUB_on (cv); |
|
|
2218 | CvXSUB (cv) = xsub; |
|
|
2219 | GENSUB_ARG = arg; |
|
|
2220 | |
|
|
2221 | return newRV_noinc ((SV *)cv); |
|
|
2222 | } |
2457 | } |
2223 | |
2458 | |
2224 | /*****************************************************************************/ |
2459 | /*****************************************************************************/ |
2225 | /* Coro::AIO */ |
2460 | /* Coro::AIO */ |
2226 | |
2461 | |
… | |
… | |
2241 | dXSARGS; |
2476 | dXSARGS; |
2242 | AV *state = (AV *)GENSUB_ARG; |
2477 | AV *state = (AV *)GENSUB_ARG; |
2243 | SV *coro = av_pop (state); |
2478 | SV *coro = av_pop (state); |
2244 | SV *data_sv = newSV (sizeof (struct io_state)); |
2479 | SV *data_sv = newSV (sizeof (struct io_state)); |
2245 | |
2480 | |
2246 | av_extend (state, items); |
2481 | av_extend (state, items - 1); |
2247 | |
2482 | |
2248 | sv_upgrade (data_sv, SVt_PV); |
2483 | sv_upgrade (data_sv, SVt_PV); |
2249 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2484 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2250 | SvPOK_only (data_sv); |
2485 | SvPOK_only (data_sv); |
2251 | |
2486 | |
… | |
… | |
2461 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2696 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2462 | } |
2697 | } |
2463 | |
2698 | |
2464 | SV * |
2699 | SV * |
2465 | new (char *klass, ...) |
2700 | new (char *klass, ...) |
|
|
2701 | ALIAS: |
|
|
2702 | Coro::new = 1 |
2466 | CODE: |
2703 | CODE: |
2467 | { |
2704 | { |
2468 | struct coro *coro; |
2705 | struct coro *coro; |
2469 | MAGIC *mg; |
2706 | MAGIC *mg; |
2470 | HV *hv; |
2707 | HV *hv; |
|
|
2708 | CV *cb; |
2471 | int i; |
2709 | int i; |
|
|
2710 | |
|
|
2711 | if (items > 1) |
|
|
2712 | { |
|
|
2713 | cb = coro_sv_2cv (aTHX_ ST (1)); |
|
|
2714 | |
|
|
2715 | if (!ix) |
|
|
2716 | { |
|
|
2717 | if (CvISXSUB (cb)) |
|
|
2718 | croak ("Coro::State doesn't support XS functions as coroutine start, caught"); |
|
|
2719 | |
|
|
2720 | if (!CvROOT (cb)) |
|
|
2721 | croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); |
|
|
2722 | } |
|
|
2723 | } |
2472 | |
2724 | |
2473 | Newz (0, coro, 1, struct coro); |
2725 | Newz (0, coro, 1, struct coro); |
2474 | coro->args = newAV (); |
2726 | coro->args = newAV (); |
2475 | coro->flags = CF_NEW; |
2727 | coro->flags = CF_NEW; |
2476 | |
2728 | |
… | |
… | |
2481 | coro->hv = hv = newHV (); |
2733 | coro->hv = hv = newHV (); |
2482 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2734 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2483 | mg->mg_flags |= MGf_DUP; |
2735 | mg->mg_flags |= MGf_DUP; |
2484 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2736 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2485 | |
2737 | |
|
|
2738 | if (items > 1) |
|
|
2739 | { |
2486 | av_extend (coro->args, items - 1); |
2740 | av_extend (coro->args, items - 1 + ix - 1); |
|
|
2741 | |
|
|
2742 | if (ix) |
|
|
2743 | { |
|
|
2744 | av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); |
|
|
2745 | cb = cv_coro_run; |
|
|
2746 | } |
|
|
2747 | |
|
|
2748 | coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); |
|
|
2749 | |
2487 | for (i = 1; i < items; i++) |
2750 | for (i = 2; i < items; i++) |
2488 | av_push (coro->args, newSVsv (ST (i))); |
2751 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2752 | } |
2489 | } |
2753 | } |
2490 | OUTPUT: |
2754 | OUTPUT: |
2491 | RETVAL |
2755 | RETVAL |
2492 | |
2756 | |
2493 | void |
2757 | void |
… | |
… | |
2691 | |
2955 | |
2692 | BOOT: |
2956 | BOOT: |
2693 | { |
2957 | { |
2694 | int i; |
2958 | int i; |
2695 | |
2959 | |
2696 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
2960 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
2697 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2961 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2698 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2962 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2699 | |
2963 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
|
|
2964 | cv_coro_terminate = get_cv ( "Coro::terminate", GV_ADD); |
2700 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
2965 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); |
2701 | SvREADONLY_on (coro_current); |
2966 | SvREADONLY_on (coro_current); |
|
|
2967 | |
|
|
2968 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
|
|
2969 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
|
|
2970 | cv_pool_handler = get_cv ("Coro::_pool_handler", 0); SvREADONLY_on (cv_pool_handler); |
|
|
2971 | cv_coro_new = get_cv ("Coro::new", 0); SvREADONLY_on (cv_coro_new); |
2702 | |
2972 | |
2703 | coro_stash = gv_stashpv ("Coro", TRUE); |
2973 | coro_stash = gv_stashpv ("Coro", TRUE); |
2704 | |
2974 | |
2705 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
2975 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
2706 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
2976 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
… | |
… | |
2795 | CODE: |
3065 | CODE: |
2796 | RETVAL = coro_nready; |
3066 | RETVAL = coro_nready; |
2797 | OUTPUT: |
3067 | OUTPUT: |
2798 | RETVAL |
3068 | RETVAL |
2799 | |
3069 | |
2800 | # for async_pool speedup |
|
|
2801 | void |
3070 | void |
2802 | _pool_1 (SV *cb) |
3071 | _pool_handler (...) |
2803 | CODE: |
3072 | CODE: |
2804 | { |
3073 | CORO_EXECUTE_SLF_XS (slf_init_pool_handler); |
2805 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2806 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
2807 | AV *defav = GvAV (PL_defgv); |
|
|
2808 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
|
|
2809 | AV *invoke_av; |
|
|
2810 | int i, len; |
|
|
2811 | |
3074 | |
2812 | if (!invoke) |
3075 | void |
|
|
3076 | async_pool (SV *cv, ...) |
|
|
3077 | PROTOTYPE: &@ |
|
|
3078 | PPCODE: |
|
|
3079 | { |
|
|
3080 | HV *hv = (HV *)av_pop (av_async_pool); |
|
|
3081 | AV *av = newAV (); |
|
|
3082 | SV *cb = ST (0); |
|
|
3083 | int i; |
|
|
3084 | |
|
|
3085 | av_extend (av, items - 2); |
|
|
3086 | for (i = 1; i < items; ++i) |
|
|
3087 | av_push (av, SvREFCNT_inc_NN (ST (i))); |
|
|
3088 | |
|
|
3089 | if ((SV *)hv == &PL_sv_undef) |
2813 | { |
3090 | { |
2814 | SV *old = PL_diehook; |
3091 | PUSHMARK (SP); |
2815 | PL_diehook = 0; |
3092 | EXTEND (SP, 2); |
2816 | SvREFCNT_dec (old); |
3093 | PUSHs (sv_Coro); |
2817 | croak ("\3async_pool terminate\2\n"); |
3094 | PUSHs ((SV *)cv_pool_handler); |
|
|
3095 | PUTBACK; |
|
|
3096 | call_sv (cv_coro_new, G_SCALAR); |
|
|
3097 | SPAGAIN; |
|
|
3098 | |
|
|
3099 | hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); |
2818 | } |
3100 | } |
2819 | |
3101 | |
2820 | SvREFCNT_dec (coro->saved_deffh); |
|
|
2821 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
|
|
2822 | |
|
|
2823 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
2824 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
2825 | |
|
|
2826 | invoke_av = (AV *)SvRV (invoke); |
|
|
2827 | len = av_len (invoke_av); |
|
|
2828 | |
|
|
2829 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
|
|
2830 | |
|
|
2831 | if (len > 0) |
|
|
2832 | { |
3102 | { |
2833 | av_fill (defav, len - 1); |
3103 | struct coro *coro = SvSTATE_hv (hv); |
2834 | for (i = 0; i < len; ++i) |
3104 | |
2835 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
3105 | assert (!coro->invoke_cb); |
|
|
3106 | assert (!coro->invoke_av); |
|
|
3107 | coro->invoke_cb = SvREFCNT_inc (cb); |
|
|
3108 | coro->invoke_av = av; |
2836 | } |
3109 | } |
|
|
3110 | |
|
|
3111 | api_ready ((SV *)hv); |
|
|
3112 | |
|
|
3113 | if (GIMME_V != G_VOID) |
|
|
3114 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); |
|
|
3115 | else |
|
|
3116 | SvREFCNT_dec (hv); |
2837 | } |
3117 | } |
|
|
3118 | |
|
|
3119 | SV * |
|
|
3120 | rouse_cb () |
|
|
3121 | PROTOTYPE: |
|
|
3122 | CODE: |
|
|
3123 | RETVAL = coro_new_rouse_cb (aTHX); |
|
|
3124 | OUTPUT: |
|
|
3125 | RETVAL |
2838 | |
3126 | |
2839 | void |
3127 | void |
2840 | _pool_2 (SV *cb) |
3128 | rouse_wait (...) |
|
|
3129 | PROTOTYPE: ;$ |
2841 | CODE: |
3130 | PPCODE: |
2842 | { |
3131 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
2843 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2844 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
2845 | |
|
|
2846 | sv_setsv (cb, &PL_sv_undef); |
|
|
2847 | |
|
|
2848 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
|
|
2849 | coro->saved_deffh = 0; |
|
|
2850 | |
|
|
2851 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
|
|
2852 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
2853 | { |
|
|
2854 | SV *old = PL_diehook; |
|
|
2855 | PL_diehook = 0; |
|
|
2856 | SvREFCNT_dec (old); |
|
|
2857 | croak ("\3async_pool terminate\2\n"); |
|
|
2858 | } |
|
|
2859 | |
|
|
2860 | av_clear (GvAV (PL_defgv)); |
|
|
2861 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
2862 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
|
|
2863 | |
|
|
2864 | coro->prio = 0; |
|
|
2865 | |
|
|
2866 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
|
|
2867 | api_trace (aTHX_ coro_current, 0); |
|
|
2868 | |
|
|
2869 | av_push (av_async_pool, newSVsv (coro_current)); |
|
|
2870 | } |
|
|
2871 | |
3132 | |
2872 | |
3133 | |
2873 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3134 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2874 | |
3135 | |
2875 | BOOT: |
3136 | BOOT: |
… | |
… | |
2909 | adjust = 1 |
3170 | adjust = 1 |
2910 | CODE: |
3171 | CODE: |
2911 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
3172 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
2912 | |
3173 | |
2913 | void |
3174 | void |
2914 | down (SV *self) |
3175 | down (...) |
2915 | CODE: |
3176 | CODE: |
2916 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
3177 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
2917 | |
3178 | |
2918 | void |
3179 | void |
2919 | wait (SV *self) |
3180 | wait (...) |
2920 | CODE: |
3181 | CODE: |
2921 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); |
3182 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); |
2922 | |
3183 | |
2923 | void |
3184 | void |
2924 | try (SV *self) |
3185 | try (SV *self) |
… | |
… | |
2967 | ); |
3228 | ); |
2968 | OUTPUT: |
3229 | OUTPUT: |
2969 | RETVAL |
3230 | RETVAL |
2970 | |
3231 | |
2971 | void |
3232 | void |
2972 | wait (SV *self) |
3233 | wait (...) |
2973 | CODE: |
3234 | CODE: |
2974 | CORO_EXECUTE_SLF_XS (slf_init_signal_wait); |
3235 | CORO_EXECUTE_SLF_XS (slf_init_signal_wait); |
2975 | |
3236 | |
2976 | void |
3237 | void |
2977 | broadcast (SV *self) |
3238 | broadcast (SV *self) |
… | |
… | |
3034 | |
3295 | |
3035 | void |
3296 | void |
3036 | _register (char *target, char *proto, SV *req) |
3297 | _register (char *target, char *proto, SV *req) |
3037 | CODE: |
3298 | CODE: |
3038 | { |
3299 | { |
3039 | HV *st; |
3300 | CV *req_cv = coro_sv_2cv (aTHX_ req); |
3040 | GV *gvp; |
|
|
3041 | CV *req_cv = sv_2cv (req, &st, &gvp, 0); |
|
|
3042 | /* newXSproto doesn't return the CV on 5.8 */ |
3301 | /* newXSproto doesn't return the CV on 5.8 */ |
3043 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3302 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3044 | sv_setpv ((SV *)slf_cv, proto); |
3303 | sv_setpv ((SV *)slf_cv, proto); |
3045 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3304 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3046 | } |
3305 | } |