… | |
… | |
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 |
… | |
… | |
98 | # define newSV(l) NEWSV(0,l) |
98 | # define newSV(l) NEWSV(0,l) |
99 | #endif |
99 | #endif |
100 | #ifndef CvISXSUB_on |
100 | #ifndef CvISXSUB_on |
101 | # define CvISXSUB_on(cv) (void)cv |
101 | # define CvISXSUB_on(cv) (void)cv |
102 | #endif |
102 | #endif |
|
|
103 | #ifndef CvISXSUB |
|
|
104 | # define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE) |
|
|
105 | #endif |
103 | |
106 | |
104 | /* 5.8.7 */ |
107 | /* 5.8.7 */ |
105 | #ifndef SvRV_set |
108 | #ifndef SvRV_set |
106 | # define SvRV_set(s,v) SvRV(s) = (v) |
109 | # define SvRV_set(s,v) SvRV(s) = (v) |
107 | #endif |
110 | #endif |
… | |
… | |
176 | static HV *hv_sig; /* %SIG */ |
179 | static HV *hv_sig; /* %SIG */ |
177 | |
180 | |
178 | /* async_pool helper stuff */ |
181 | /* async_pool helper stuff */ |
179 | static SV *sv_pool_rss; |
182 | static SV *sv_pool_rss; |
180 | static SV *sv_pool_size; |
183 | static SV *sv_pool_size; |
|
|
184 | static SV *sv_async_pool_idle; |
181 | static AV *av_async_pool; |
185 | static AV *av_async_pool; |
|
|
186 | static SV *sv_Coro; |
|
|
187 | static CV *cv_pool_handler; |
|
|
188 | static CV *cv_coro_state_new; |
182 | |
189 | |
183 | /* Coro::AnyEvent */ |
190 | /* Coro::AnyEvent */ |
184 | static SV *sv_activity; |
191 | static SV *sv_activity; |
185 | |
192 | |
186 | static struct coro_cctx *cctx_first; |
193 | static struct coro_cctx *cctx_first; |
… | |
… | |
246 | /* state data */ |
253 | /* state data */ |
247 | struct CoroSLF slf_frame; /* saved slf frame */ |
254 | struct CoroSLF slf_frame; /* saved slf frame */ |
248 | AV *mainstack; |
255 | AV *mainstack; |
249 | perl_slots *slot; /* basically the saved sp */ |
256 | perl_slots *slot; /* basically the saved sp */ |
250 | |
257 | |
|
|
258 | CV *startcv; /* the CV to execute */ |
251 | AV *args; /* data associated with this coroutine (initial args) */ |
259 | AV *args; /* data associated with this coroutine (initial args) */ |
252 | int refcnt; /* coroutines are refcounted, yes */ |
260 | int refcnt; /* coroutines are refcounted, yes */ |
253 | int flags; /* CF_ flags */ |
261 | int flags; /* CF_ flags */ |
254 | HV *hv; /* the perl hash associated with this coro, if any */ |
262 | HV *hv; /* the perl hash associated with this coro, if any */ |
255 | void (*on_destroy)(pTHX_ struct coro *coro); |
263 | void (*on_destroy)(pTHX_ struct coro *coro); |
256 | |
264 | |
257 | /* statistics */ |
265 | /* statistics */ |
258 | int usecount; /* number of transfers to this coro */ |
266 | int usecount; /* number of transfers to this coro */ |
259 | |
267 | |
260 | /* coro process data */ |
268 | /* coro process data */ |
261 | int prio; |
269 | int prio; |
262 | SV *except; /* exception to be thrown */ |
270 | SV *except; /* exception to be thrown */ |
|
|
271 | SV *rouse_cb; |
263 | |
272 | |
264 | /* async_pool */ |
273 | /* async_pool */ |
265 | SV *saved_deffh; |
274 | SV *saved_deffh; |
|
|
275 | SV *invoke_cb; |
|
|
276 | AV *invoke_av; |
266 | |
277 | |
267 | /* linked list */ |
278 | /* linked list */ |
268 | struct coro *next, *prev; |
279 | struct coro *next, *prev; |
269 | }; |
280 | }; |
270 | |
281 | |
… | |
… | |
287 | |
298 | |
288 | /* for Coro.pm */ |
299 | /* for Coro.pm */ |
289 | static SV *coro_current; |
300 | static SV *coro_current; |
290 | static SV *coro_readyhook; |
301 | static SV *coro_readyhook; |
291 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
302 | static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; |
|
|
303 | static CV *cv_coro_run, *cv_coro_terminate; |
292 | static struct coro *coro_first; |
304 | static struct coro *coro_first; |
293 | #define coro_nready coroapi.nready |
305 | #define coro_nready coroapi.nready |
294 | |
306 | |
295 | /** lowlevel stuff **********************************************************/ |
307 | /** lowlevel stuff **********************************************************/ |
296 | |
308 | |
… | |
… | |
320 | #if PERL_VERSION_ATLEAST (5,10,0) |
332 | #if PERL_VERSION_ATLEAST (5,10,0) |
321 | /* 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 */ |
322 | get_hv (name, create); |
334 | get_hv (name, create); |
323 | #endif |
335 | #endif |
324 | return get_hv (name, create); |
336 | return get_hv (name, create); |
|
|
337 | } |
|
|
338 | |
|
|
339 | /* may croak */ |
|
|
340 | INLINE CV * |
|
|
341 | coro_sv_2cv (pTHX_ SV *sv) |
|
|
342 | { |
|
|
343 | HV *st; |
|
|
344 | GV *gvp; |
|
|
345 | return sv_2cv (sv, &st, &gvp, 0); |
325 | } |
346 | } |
326 | |
347 | |
327 | static AV * |
348 | static AV * |
328 | coro_clone_padlist (pTHX_ CV *cv) |
349 | coro_clone_padlist (pTHX_ CV *cv) |
329 | { |
350 | { |
… | |
… | |
448 | else |
469 | else |
449 | { |
470 | { |
450 | #if CORO_PREFER_PERL_FUNCTIONS |
471 | #if CORO_PREFER_PERL_FUNCTIONS |
451 | /* this is probably cleaner? but also slower! */ |
472 | /* this is probably cleaner? but also slower! */ |
452 | /* in practise, it seems to be less stable */ |
473 | /* in practise, it seems to be less stable */ |
453 | CV *cp = Perl_cv_clone (cv); |
474 | CV *cp = Perl_cv_clone (aTHX_ cv); |
454 | CvPADLIST (cv) = CvPADLIST (cp); |
475 | CvPADLIST (cv) = CvPADLIST (cp); |
455 | CvPADLIST (cp) = 0; |
476 | CvPADLIST (cp) = 0; |
456 | SvREFCNT_dec (cp); |
477 | SvREFCNT_dec (cp); |
457 | #else |
478 | #else |
458 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
479 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
… | |
… | |
602 | * of perl.c:init_stacks, except that it uses less memory |
623 | * of perl.c:init_stacks, except that it uses less memory |
603 | * on the (sometimes correct) assumption that coroutines do |
624 | * on the (sometimes correct) assumption that coroutines do |
604 | * not usually need a lot of stackspace. |
625 | * not usually need a lot of stackspace. |
605 | */ |
626 | */ |
606 | #if CORO_PREFER_PERL_FUNCTIONS |
627 | #if CORO_PREFER_PERL_FUNCTIONS |
607 | # define coro_init_stacks init_stacks |
628 | # define coro_init_stacks(thx) init_stacks () |
608 | #else |
629 | #else |
609 | static void |
630 | static void |
610 | coro_init_stacks (pTHX) |
631 | coro_init_stacks (pTHX) |
611 | { |
632 | { |
612 | PL_curstackinfo = new_stackinfo(32, 8); |
633 | PL_curstackinfo = new_stackinfo(32, 8); |
… | |
… | |
675 | #if !PERL_VERSION_ATLEAST (5,10,0) |
696 | #if !PERL_VERSION_ATLEAST (5,10,0) |
676 | Safefree (PL_retstack); |
697 | Safefree (PL_retstack); |
677 | #endif |
698 | #endif |
678 | } |
699 | } |
679 | |
700 | |
|
|
701 | #define CORO_RSS \ |
|
|
702 | rss += sizeof (SYM (curstackinfo)); \ |
|
|
703 | rss += (SYM (curstackinfo->si_cxmax) + 1) * sizeof (PERL_CONTEXT); \ |
|
|
704 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (SYM (curstack))) * sizeof (SV *); \ |
|
|
705 | rss += SYM (tmps_max) * sizeof (SV *); \ |
|
|
706 | rss += (SYM (markstack_max) - SYM (markstack_ptr)) * sizeof (I32); \ |
|
|
707 | rss += SYM (scopestack_max) * sizeof (I32); \ |
|
|
708 | rss += SYM (savestack_max) * sizeof (ANY); |
|
|
709 | |
680 | static size_t |
710 | static size_t |
681 | coro_rss (pTHX_ struct coro *coro) |
711 | coro_rss (pTHX_ struct coro *coro) |
682 | { |
712 | { |
683 | size_t rss = sizeof (*coro); |
713 | size_t rss = sizeof (*coro); |
684 | |
714 | |
685 | if (coro->mainstack) |
715 | if (coro->mainstack) |
686 | { |
716 | { |
687 | perl_slots tmp_slot; |
|
|
688 | perl_slots *slot; |
|
|
689 | |
|
|
690 | if (coro->flags & CF_RUNNING) |
717 | if (coro->flags & CF_RUNNING) |
691 | { |
718 | { |
692 | slot = &tmp_slot; |
719 | #define SYM(sym) PL_ ## sym |
693 | |
720 | CORO_RSS; |
694 | #define VAR(name,type) slot->name = PL_ ## name; |
|
|
695 | # include "state.h" |
|
|
696 | #undef VAR |
721 | #undef SYM |
697 | } |
722 | } |
698 | else |
723 | else |
699 | slot = coro->slot; |
|
|
700 | |
|
|
701 | if (slot) |
|
|
702 | { |
724 | { |
703 | rss += sizeof (slot->curstackinfo); |
725 | #define SYM(sym) coro->slot->sym |
704 | rss += (slot->curstackinfo->si_cxmax + 1) * sizeof (PERL_CONTEXT); |
726 | CORO_RSS; |
705 | rss += sizeof (SV) + sizeof (struct xpvav) + (1 + AvMAX (slot->curstack)) * sizeof (SV *); |
727 | #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 | } |
728 | } |
715 | } |
729 | } |
716 | |
730 | |
717 | return rss; |
731 | return rss; |
718 | } |
732 | } |
… | |
… | |
860 | dSP; |
874 | dSP; |
861 | UNOP myop; |
875 | UNOP myop; |
862 | |
876 | |
863 | Zero (&myop, 1, UNOP); |
877 | Zero (&myop, 1, UNOP); |
864 | myop.op_next = Nullop; |
878 | myop.op_next = Nullop; |
|
|
879 | myop.op_type = OP_ENTERSUB; |
865 | myop.op_flags = OPf_WANT_VOID; |
880 | myop.op_flags = OPf_WANT_VOID; |
866 | |
881 | |
867 | PUSHMARK (SP); |
882 | PUSHMARK (SP); |
868 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
883 | PUSHs ((SV *)coro->startcv); |
869 | PUTBACK; |
884 | PUTBACK; |
870 | PL_op = (OP *)&myop; |
885 | PL_op = (OP *)&myop; |
871 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
886 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
872 | SPAGAIN; |
|
|
873 | } |
887 | } |
874 | |
888 | |
875 | /* this newly created coroutine might be run on an existing cctx which most |
889 | /* 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. |
890 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
877 | */ |
891 | */ |
… | |
… | |
880 | |
894 | |
881 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
895 | /* 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; |
896 | coro_setup_op.op_next = PL_op; |
883 | coro_setup_op.op_type = OP_CUSTOM; |
897 | coro_setup_op.op_type = OP_CUSTOM; |
884 | coro_setup_op.op_ppaddr = pp_slf; |
898 | coro_setup_op.op_ppaddr = pp_slf; |
885 | /* no flags required, as an init function won't be called */ |
899 | /* no flags etc. required, as an init function won't be called */ |
886 | |
900 | |
887 | PL_op = (OP *)&coro_setup_op; |
901 | PL_op = (OP *)&coro_setup_op; |
888 | |
902 | |
889 | /* copy throw, in case it was set before coro_setup */ |
903 | /* copy throw, in case it was set before coro_setup */ |
890 | CORO_THROW = coro->except; |
904 | CORO_THROW = coro->except; |
… | |
… | |
919 | |
933 | |
920 | SvREFCNT_dec (PL_diehook); |
934 | SvREFCNT_dec (PL_diehook); |
921 | SvREFCNT_dec (PL_warnhook); |
935 | SvREFCNT_dec (PL_warnhook); |
922 | |
936 | |
923 | SvREFCNT_dec (coro->saved_deffh); |
937 | SvREFCNT_dec (coro->saved_deffh); |
924 | SvREFCNT_dec (CORO_THROW); |
938 | SvREFCNT_dec (coro->rouse_cb); |
|
|
939 | SvREFCNT_dec (coro->invoke_cb); |
|
|
940 | SvREFCNT_dec (coro->invoke_av); |
925 | |
941 | |
926 | coro_destruct_stacks (aTHX); |
942 | coro_destruct_stacks (aTHX); |
927 | } |
943 | } |
928 | |
944 | |
929 | INLINE void |
945 | INLINE void |
… | |
… | |
1016 | SAVETMPS; |
1032 | SAVETMPS; |
1017 | EXTEND (SP, 3); |
1033 | EXTEND (SP, 3); |
1018 | PUSHMARK (SP); |
1034 | PUSHMARK (SP); |
1019 | PUSHs (&PL_sv_yes); |
1035 | PUSHs (&PL_sv_yes); |
1020 | PUSHs (fullname); |
1036 | PUSHs (fullname); |
1021 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
1037 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
1022 | PUTBACK; |
1038 | PUTBACK; |
1023 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
1039 | 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); |
1040 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1025 | SPAGAIN; |
1041 | SPAGAIN; |
1026 | FREETMPS; |
1042 | FREETMPS; |
… | |
… | |
1132 | transfer_tail (aTHX); |
1148 | transfer_tail (aTHX); |
1133 | |
1149 | |
1134 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1150 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1135 | PL_restartop = PL_op; |
1151 | PL_restartop = PL_op; |
1136 | perl_run (PL_curinterp); |
1152 | perl_run (PL_curinterp); |
|
|
1153 | /* |
|
|
1154 | * Unfortunately, there is no way to get at the return values of the |
|
|
1155 | * coro body here, as perl_run destroys these |
|
|
1156 | */ |
1137 | |
1157 | |
1138 | /* |
1158 | /* |
1139 | * If perl-run returns we assume exit() was being called or the coro |
1159 | * 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) |
1160 | * 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 |
1161 | * reason for perl_run to return. We try to exit by jumping to the |
… | |
… | |
1433 | |
1453 | |
1434 | coro->slot = 0; |
1454 | coro->slot = 0; |
1435 | } |
1455 | } |
1436 | |
1456 | |
1437 | cctx_destroy (coro->cctx); |
1457 | cctx_destroy (coro->cctx); |
|
|
1458 | SvREFCNT_dec (coro->startcv); |
1438 | SvREFCNT_dec (coro->args); |
1459 | SvREFCNT_dec (coro->args); |
|
|
1460 | SvREFCNT_dec (CORO_THROW); |
1439 | |
1461 | |
1440 | if (coro->next) coro->next->prev = coro->prev; |
1462 | if (coro->next) coro->next->prev = coro->prev; |
1441 | if (coro->prev) coro->prev->next = coro->next; |
1463 | if (coro->prev) coro->prev->next = coro->next; |
1442 | if (coro == coro_first) coro_first = coro->next; |
1464 | if (coro == coro_first) coro_first = coro->next; |
1443 | |
1465 | |
… | |
… | |
1497 | |
1519 | |
1498 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1520 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1499 | TRANSFER (ta, 1); |
1521 | TRANSFER (ta, 1); |
1500 | } |
1522 | } |
1501 | |
1523 | |
|
|
1524 | /*****************************************************************************/ |
|
|
1525 | /* gensub: simple closure generation utility */ |
|
|
1526 | |
|
|
1527 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
|
|
1528 | |
|
|
1529 | /* create a closure from XS, returns a code reference */ |
|
|
1530 | /* the arg can be accessed via GENSUB_ARG from the callback */ |
|
|
1531 | /* the callback must use dXSARGS/XSRETURN */ |
|
|
1532 | static SV * |
|
|
1533 | gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
|
|
1534 | { |
|
|
1535 | CV *cv = (CV *)newSV (0); |
|
|
1536 | |
|
|
1537 | sv_upgrade ((SV *)cv, SVt_PVCV); |
|
|
1538 | |
|
|
1539 | CvANON_on (cv); |
|
|
1540 | CvISXSUB_on (cv); |
|
|
1541 | CvXSUB (cv) = xsub; |
|
|
1542 | GENSUB_ARG = arg; |
|
|
1543 | |
|
|
1544 | return newRV_noinc ((SV *)cv); |
|
|
1545 | } |
|
|
1546 | |
1502 | /** Coro ********************************************************************/ |
1547 | /** Coro ********************************************************************/ |
1503 | |
1548 | |
1504 | INLINE void |
1549 | INLINE void |
1505 | coro_enq (pTHX_ struct coro *coro) |
1550 | coro_enq (pTHX_ struct coro *coro) |
1506 | { |
1551 | { |
… | |
… | |
1567 | api_is_ready (pTHX_ SV *coro_sv) |
1612 | api_is_ready (pTHX_ SV *coro_sv) |
1568 | { |
1613 | { |
1569 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1614 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1570 | } |
1615 | } |
1571 | |
1616 | |
|
|
1617 | /* expects to own a reference to next->hv */ |
1572 | INLINE void |
1618 | INLINE void |
|
|
1619 | prepare_cede_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
|
|
1620 | { |
|
|
1621 | SV *prev_sv = SvRV (coro_current); |
|
|
1622 | |
|
|
1623 | ta->prev = SvSTATE_hv (prev_sv); |
|
|
1624 | ta->next = next; |
|
|
1625 | |
|
|
1626 | TRANSFER_CHECK (*ta); |
|
|
1627 | |
|
|
1628 | SvRV_set (coro_current, next->hv); |
|
|
1629 | |
|
|
1630 | free_coro_mortal (aTHX); |
|
|
1631 | coro_mortal = prev_sv; |
|
|
1632 | } |
|
|
1633 | |
|
|
1634 | static void |
1573 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1635 | prepare_schedule (pTHX_ struct coro_transfer_args *ta) |
1574 | { |
1636 | { |
1575 | SV *prev_sv, *next_sv; |
|
|
1576 | |
|
|
1577 | for (;;) |
1637 | for (;;) |
1578 | { |
1638 | { |
1579 | next_sv = coro_deq (aTHX); |
1639 | SV *next_sv = coro_deq (aTHX); |
1580 | |
1640 | |
1581 | /* nothing to schedule: call the idle handler */ |
|
|
1582 | if (expect_false (!next_sv)) |
1641 | if (expect_false (!next_sv)) |
1583 | { |
1642 | { |
|
|
1643 | /* nothing to schedule: call the idle handler */ |
1584 | dSP; |
1644 | dSP; |
1585 | |
1645 | |
1586 | ENTER; |
1646 | ENTER; |
1587 | SAVETMPS; |
1647 | SAVETMPS; |
1588 | |
1648 | |
… | |
… | |
1590 | PUTBACK; |
1650 | PUTBACK; |
1591 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
1651 | call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); |
1592 | |
1652 | |
1593 | FREETMPS; |
1653 | FREETMPS; |
1594 | LEAVE; |
1654 | LEAVE; |
1595 | continue; |
|
|
1596 | } |
1655 | } |
1597 | |
1656 | else |
1598 | ta->next = SvSTATE_hv (next_sv); |
|
|
1599 | |
|
|
1600 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1601 | if (expect_false (ta->next->flags & CF_DESTROYED)) |
|
|
1602 | { |
1657 | { |
1603 | SvREFCNT_dec (next_sv); |
1658 | struct coro *next = SvSTATE_hv (next_sv); |
|
|
1659 | |
|
|
1660 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1661 | if (expect_false (next->flags & CF_DESTROYED)) |
1604 | /* coro_nready has already been taken care of by destroy */ |
1662 | SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ |
1605 | continue; |
1663 | else |
|
|
1664 | { |
|
|
1665 | next->flags &= ~CF_READY; |
|
|
1666 | --coro_nready; |
|
|
1667 | |
|
|
1668 | return prepare_cede_to (aTHX_ ta, next); |
|
|
1669 | } |
1606 | } |
1670 | } |
1607 | |
|
|
1608 | --coro_nready; |
|
|
1609 | break; |
|
|
1610 | } |
1671 | } |
1611 | |
|
|
1612 | /* free this only after the transfer */ |
|
|
1613 | prev_sv = SvRV (coro_current); |
|
|
1614 | ta->prev = SvSTATE_hv (prev_sv); |
|
|
1615 | TRANSFER_CHECK (*ta); |
|
|
1616 | assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY)); |
|
|
1617 | ta->next->flags &= ~CF_READY; |
|
|
1618 | SvRV_set (coro_current, next_sv); |
|
|
1619 | |
|
|
1620 | free_coro_mortal (aTHX); |
|
|
1621 | coro_mortal = prev_sv; |
|
|
1622 | } |
1672 | } |
1623 | |
1673 | |
1624 | INLINE void |
1674 | INLINE void |
1625 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1675 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1626 | { |
1676 | { |
… | |
… | |
1703 | if (coro->flags & CF_RUNNING) |
1753 | if (coro->flags & CF_RUNNING) |
1704 | PL_runops = RUNOPS_DEFAULT; |
1754 | PL_runops = RUNOPS_DEFAULT; |
1705 | else |
1755 | else |
1706 | coro->slot->runops = RUNOPS_DEFAULT; |
1756 | coro->slot->runops = RUNOPS_DEFAULT; |
1707 | } |
1757 | } |
|
|
1758 | } |
|
|
1759 | |
|
|
1760 | /*****************************************************************************/ |
|
|
1761 | /* async pool handler */ |
|
|
1762 | |
|
|
1763 | static int |
|
|
1764 | slf_check_pool_handler (pTHX_ struct CoroSLF *frame) |
|
|
1765 | { |
|
|
1766 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1767 | struct coro *coro = (struct coro *)frame->data; |
|
|
1768 | |
|
|
1769 | if (!coro->invoke_cb) |
|
|
1770 | return 1; /* loop till we have invoke */ |
|
|
1771 | else |
|
|
1772 | { |
|
|
1773 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
1774 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
1775 | |
|
|
1776 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
|
|
1777 | |
|
|
1778 | { |
|
|
1779 | dSP; |
|
|
1780 | XPUSHs (sv_2mortal (coro->invoke_cb)); coro->invoke_cb = 0; |
|
|
1781 | PUTBACK; |
|
|
1782 | } |
|
|
1783 | |
|
|
1784 | SvREFCNT_dec (GvAV (PL_defgv)); |
|
|
1785 | GvAV (PL_defgv) = coro->invoke_av; |
|
|
1786 | coro->invoke_av = 0; |
|
|
1787 | |
|
|
1788 | return 0; |
|
|
1789 | } |
|
|
1790 | } |
|
|
1791 | |
|
|
1792 | static void |
|
|
1793 | slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1794 | { |
|
|
1795 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1796 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
1797 | |
|
|
1798 | if (expect_true (coro->saved_deffh)) |
|
|
1799 | { |
|
|
1800 | /* subsequent iteration */ |
|
|
1801 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
|
|
1802 | coro->saved_deffh = 0; |
|
|
1803 | |
|
|
1804 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
|
|
1805 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
1806 | { |
|
|
1807 | coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate); |
|
|
1808 | coro->invoke_av = newAV (); |
|
|
1809 | |
|
|
1810 | frame->prepare = prepare_nop; |
|
|
1811 | } |
|
|
1812 | else |
|
|
1813 | { |
|
|
1814 | av_clear (GvAV (PL_defgv)); |
|
|
1815 | hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); |
|
|
1816 | |
|
|
1817 | coro->prio = 0; |
|
|
1818 | |
|
|
1819 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
|
|
1820 | api_trace (aTHX_ coro_current, 0); |
|
|
1821 | |
|
|
1822 | frame->prepare = prepare_schedule; |
|
|
1823 | av_push (av_async_pool, SvREFCNT_inc (hv)); |
|
|
1824 | } |
|
|
1825 | } |
|
|
1826 | else |
|
|
1827 | { |
|
|
1828 | /* first iteration, simply fall through */ |
|
|
1829 | frame->prepare = prepare_nop; |
|
|
1830 | } |
|
|
1831 | |
|
|
1832 | frame->check = slf_check_pool_handler; |
|
|
1833 | frame->data = (void *)coro; |
|
|
1834 | } |
|
|
1835 | |
|
|
1836 | /*****************************************************************************/ |
|
|
1837 | /* rouse callback */ |
|
|
1838 | |
|
|
1839 | #define CORO_MAGIC_type_rouse PERL_MAGIC_ext |
|
|
1840 | |
|
|
1841 | static void |
|
|
1842 | coro_rouse_callback (pTHX_ CV *cv) |
|
|
1843 | { |
|
|
1844 | dXSARGS; |
|
|
1845 | SV *data = (SV *)GENSUB_ARG; |
|
|
1846 | |
|
|
1847 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
|
|
1848 | { |
|
|
1849 | /* first call, set args */ |
|
|
1850 | AV *av = newAV (); |
|
|
1851 | SV *coro = SvRV (data); |
|
|
1852 | |
|
|
1853 | SvRV_set (data, (SV *)av); |
|
|
1854 | api_ready (aTHX_ coro); |
|
|
1855 | SvREFCNT_dec (coro); |
|
|
1856 | |
|
|
1857 | /* better take a full copy of the arguments */ |
|
|
1858 | while (items--) |
|
|
1859 | av_store (av, items, newSVsv (ST (items))); |
|
|
1860 | } |
|
|
1861 | |
|
|
1862 | XSRETURN_EMPTY; |
|
|
1863 | } |
|
|
1864 | |
|
|
1865 | static int |
|
|
1866 | slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) |
|
|
1867 | { |
|
|
1868 | SV *data = (SV *)frame->data; |
|
|
1869 | |
|
|
1870 | if (CORO_THROW) |
|
|
1871 | return 0; |
|
|
1872 | |
|
|
1873 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
|
|
1874 | return 1; |
|
|
1875 | |
|
|
1876 | /* now push all results on the stack */ |
|
|
1877 | { |
|
|
1878 | dSP; |
|
|
1879 | AV *av = (AV *)SvRV (data); |
|
|
1880 | int i; |
|
|
1881 | |
|
|
1882 | EXTEND (SP, AvFILLp (av) + 1); |
|
|
1883 | for (i = 0; i <= AvFILLp (av); ++i) |
|
|
1884 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
|
|
1885 | |
|
|
1886 | /* we have stolen the elements, so ste length to zero and free */ |
|
|
1887 | AvFILLp (av) = -1; |
|
|
1888 | av_undef (av); |
|
|
1889 | |
|
|
1890 | PUTBACK; |
|
|
1891 | } |
|
|
1892 | |
|
|
1893 | return 0; |
|
|
1894 | } |
|
|
1895 | |
|
|
1896 | static void |
|
|
1897 | slf_init_rouse_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1898 | { |
|
|
1899 | SV *cb; |
|
|
1900 | |
|
|
1901 | if (items) |
|
|
1902 | cb = arg [0]; |
|
|
1903 | else |
|
|
1904 | { |
|
|
1905 | struct coro *coro = SvSTATE_current; |
|
|
1906 | |
|
|
1907 | if (!coro->rouse_cb) |
|
|
1908 | croak ("Coro::rouse_wait called without rouse callback, and no default rouse callback found either,"); |
|
|
1909 | |
|
|
1910 | cb = sv_2mortal (coro->rouse_cb); |
|
|
1911 | coro->rouse_cb = 0; |
|
|
1912 | } |
|
|
1913 | |
|
|
1914 | if (!SvROK (cb) |
|
|
1915 | || SvTYPE (SvRV (cb)) != SVt_PVCV |
|
|
1916 | || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) |
|
|
1917 | croak ("Coro::rouse_wait called with illegal callback argument,"); |
|
|
1918 | |
|
|
1919 | { |
|
|
1920 | CV *cv = (CV *)SvRV (cb); /* for GENSUB_ARG */ |
|
|
1921 | SV *data = (SV *)GENSUB_ARG; |
|
|
1922 | |
|
|
1923 | frame->data = (void *)data; |
|
|
1924 | frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule; |
|
|
1925 | frame->check = slf_check_rouse_wait; |
|
|
1926 | } |
|
|
1927 | } |
|
|
1928 | |
|
|
1929 | static SV * |
|
|
1930 | coro_new_rouse_cb (pTHX) |
|
|
1931 | { |
|
|
1932 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1933 | struct coro *coro = SvSTATE_hv (hv); |
|
|
1934 | SV *data = newRV_inc ((SV *)hv); |
|
|
1935 | SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data); |
|
|
1936 | |
|
|
1937 | sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0); |
|
|
1938 | SvREFCNT_dec (data); /* magicext increases the refcount */ |
|
|
1939 | |
|
|
1940 | SvREFCNT_dec (coro->rouse_cb); |
|
|
1941 | coro->rouse_cb = SvREFCNT_inc_NN (cb); |
|
|
1942 | |
|
|
1943 | return cb; |
1708 | } |
1944 | } |
1709 | |
1945 | |
1710 | /*****************************************************************************/ |
1946 | /*****************************************************************************/ |
1711 | /* schedule-like-function opcode (SLF) */ |
1947 | /* schedule-like-function opcode (SLF) */ |
1712 | |
1948 | |
… | |
… | |
1995 | PerlIOBuf_get_cnt, |
2231 | PerlIOBuf_get_cnt, |
1996 | PerlIOBuf_set_ptrcnt, |
2232 | PerlIOBuf_set_ptrcnt, |
1997 | }; |
2233 | }; |
1998 | |
2234 | |
1999 | /*****************************************************************************/ |
2235 | /*****************************************************************************/ |
2000 | /* Coro::Semaphore */ |
2236 | /* Coro::Semaphore & Coro::Signal */ |
2001 | |
2237 | |
2002 | static SV * |
2238 | static SV * |
2003 | coro_semaphore_new (int count) |
2239 | coro_waitarray_new (pTHX_ int count) |
2004 | { |
2240 | { |
2005 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
2241 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
2006 | AV *av = newAV (); |
2242 | AV *av = newAV (); |
2007 | SV **ary; |
2243 | SV **ary; |
2008 | |
2244 | |
2009 | /* unfortunately, building manually saves memory */ |
2245 | /* unfortunately, building manually saves memory */ |
2010 | Newx (ary, 2, SV *); |
2246 | Newx (ary, 2, SV *); |
2011 | AvALLOC (av) = ary; |
2247 | AvALLOC (av) = ary; |
2012 | AvARRAY (av) = ary; |
2248 | /*AvARRAY (av) = ary;*/ |
|
|
2249 | SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ |
2013 | AvMAX (av) = 1; |
2250 | AvMAX (av) = 1; |
2014 | AvFILLp (av) = 0; |
2251 | AvFILLp (av) = 0; |
2015 | ary [0] = newSViv (count); |
2252 | ary [0] = newSViv (count); |
2016 | |
2253 | |
2017 | return newRV_noinc ((SV *)av); |
2254 | return newRV_noinc ((SV *)av); |
2018 | } |
2255 | } |
|
|
2256 | |
|
|
2257 | /* semaphore */ |
2019 | |
2258 | |
2020 | static void |
2259 | static void |
2021 | coro_semaphore_adjust (pTHX_ AV *av, IV adjust) |
2260 | coro_semaphore_adjust (pTHX_ AV *av, IV adjust) |
2022 | { |
2261 | { |
2023 | SV *count_sv = AvARRAY (av)[0]; |
2262 | SV *count_sv = AvARRAY (av)[0]; |
… | |
… | |
2035 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2274 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2036 | AvARRAY (av)[1] = count_sv; |
2275 | AvARRAY (av)[1] = count_sv; |
2037 | cb = av_shift (av); |
2276 | cb = av_shift (av); |
2038 | |
2277 | |
2039 | if (SvOBJECT (cb)) |
2278 | if (SvOBJECT (cb)) |
|
|
2279 | { |
2040 | api_ready (aTHX_ cb); |
2280 | api_ready (aTHX_ cb); |
2041 | else |
2281 | --count; |
2042 | croak ("callbacks not yet supported"); |
2282 | } |
|
|
2283 | else if (SvTYPE (cb) == SVt_PVCV) |
|
|
2284 | { |
|
|
2285 | dSP; |
|
|
2286 | PUSHMARK (SP); |
|
|
2287 | XPUSHs (sv_2mortal (newRV_inc ((SV *)av))); |
|
|
2288 | PUTBACK; |
|
|
2289 | call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR); |
|
|
2290 | } |
2043 | |
2291 | |
2044 | SvREFCNT_dec (cb); |
2292 | SvREFCNT_dec (cb); |
2045 | |
|
|
2046 | --count; |
|
|
2047 | } |
2293 | } |
2048 | } |
2294 | } |
2049 | |
2295 | |
2050 | static void |
2296 | static void |
2051 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
2297 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
… | |
… | |
2133 | } |
2379 | } |
2134 | |
2380 | |
2135 | static void |
2381 | static void |
2136 | slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2382 | slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2137 | { |
2383 | { |
|
|
2384 | if (items >= 2) |
|
|
2385 | { |
|
|
2386 | /* callback form */ |
|
|
2387 | AV *av = (AV *)SvRV (arg [0]); |
|
|
2388 | CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); |
|
|
2389 | |
|
|
2390 | av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); |
|
|
2391 | |
|
|
2392 | if (SvIVX (AvARRAY (av)[0]) > 0) |
|
|
2393 | coro_semaphore_adjust (aTHX_ av, 0); |
|
|
2394 | |
|
|
2395 | frame->prepare = prepare_nop; |
|
|
2396 | frame->check = slf_check_nop; |
|
|
2397 | } |
|
|
2398 | else |
|
|
2399 | { |
2138 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
2400 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
2139 | frame->check = slf_check_semaphore_wait; |
2401 | frame->check = slf_check_semaphore_wait; |
|
|
2402 | } |
2140 | } |
2403 | } |
2141 | |
2404 | |
2142 | /*****************************************************************************/ |
2405 | /* signal */ |
2143 | /* gensub: simple closure generation utility */ |
|
|
2144 | |
2406 | |
2145 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
2407 | static void |
2146 | |
2408 | coro_signal_wake (pTHX_ AV *av, int count) |
2147 | /* create a closure from XS, returns a code reference */ |
|
|
2148 | /* the arg can be accessed via GENSUB_ARG from the callback */ |
|
|
2149 | /* the callback must use dXSARGS/XSRETURN */ |
|
|
2150 | static SV * |
|
|
2151 | gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
|
|
2152 | { |
2409 | { |
2153 | CV *cv = (CV *)newSV (0); |
2410 | SvIVX (AvARRAY (av)[0]) = 0; |
2154 | |
2411 | |
2155 | sv_upgrade ((SV *)cv, SVt_PVCV); |
2412 | /* now signal count waiters */ |
|
|
2413 | while (count > 0 && AvFILLp (av) > 0) |
|
|
2414 | { |
|
|
2415 | SV *cb; |
2156 | |
2416 | |
2157 | CvANON_on (cv); |
2417 | /* swap first two elements so we can shift a waiter */ |
2158 | CvISXSUB_on (cv); |
2418 | cb = AvARRAY (av)[0]; |
2159 | CvXSUB (cv) = xsub; |
2419 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2160 | GENSUB_ARG = arg; |
2420 | AvARRAY (av)[1] = cb; |
2161 | |
2421 | |
2162 | return newRV_noinc ((SV *)cv); |
2422 | cb = av_shift (av); |
|
|
2423 | |
|
|
2424 | api_ready (aTHX_ cb); |
|
|
2425 | sv_setiv (cb, 0); /* signal waiter */ |
|
|
2426 | SvREFCNT_dec (cb); |
|
|
2427 | |
|
|
2428 | --count; |
|
|
2429 | } |
|
|
2430 | } |
|
|
2431 | |
|
|
2432 | static int |
|
|
2433 | slf_check_signal_wait (pTHX_ struct CoroSLF *frame) |
|
|
2434 | { |
|
|
2435 | /* if we are about to throw, also stop waiting */ |
|
|
2436 | return SvROK ((SV *)frame->data) && !CORO_THROW; |
|
|
2437 | } |
|
|
2438 | |
|
|
2439 | static void |
|
|
2440 | slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2441 | { |
|
|
2442 | AV *av = (AV *)SvRV (arg [0]); |
|
|
2443 | |
|
|
2444 | if (SvIVX (AvARRAY (av)[0])) |
|
|
2445 | { |
|
|
2446 | SvIVX (AvARRAY (av)[0]) = 0; |
|
|
2447 | frame->prepare = prepare_nop; |
|
|
2448 | frame->check = slf_check_nop; |
|
|
2449 | } |
|
|
2450 | else |
|
|
2451 | { |
|
|
2452 | SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */ |
|
|
2453 | |
|
|
2454 | av_push (av, waiter); |
|
|
2455 | |
|
|
2456 | frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ |
|
|
2457 | frame->prepare = prepare_schedule; |
|
|
2458 | frame->check = slf_check_signal_wait; |
|
|
2459 | } |
2163 | } |
2460 | } |
2164 | |
2461 | |
2165 | /*****************************************************************************/ |
2462 | /*****************************************************************************/ |
2166 | /* Coro::AIO */ |
2463 | /* Coro::AIO */ |
2167 | |
2464 | |
… | |
… | |
2182 | dXSARGS; |
2479 | dXSARGS; |
2183 | AV *state = (AV *)GENSUB_ARG; |
2480 | AV *state = (AV *)GENSUB_ARG; |
2184 | SV *coro = av_pop (state); |
2481 | SV *coro = av_pop (state); |
2185 | SV *data_sv = newSV (sizeof (struct io_state)); |
2482 | SV *data_sv = newSV (sizeof (struct io_state)); |
2186 | |
2483 | |
2187 | av_extend (state, items); |
2484 | av_extend (state, items - 1); |
2188 | |
2485 | |
2189 | sv_upgrade (data_sv, SVt_PV); |
2486 | sv_upgrade (data_sv, SVt_PV); |
2190 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2487 | SvCUR_set (data_sv, sizeof (struct io_state)); |
2191 | SvPOK_only (data_sv); |
2488 | SvPOK_only (data_sv); |
2192 | |
2489 | |
… | |
… | |
2402 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2699 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2403 | } |
2700 | } |
2404 | |
2701 | |
2405 | SV * |
2702 | SV * |
2406 | new (char *klass, ...) |
2703 | new (char *klass, ...) |
|
|
2704 | ALIAS: |
|
|
2705 | Coro::new = 1 |
2407 | CODE: |
2706 | CODE: |
2408 | { |
2707 | { |
2409 | struct coro *coro; |
2708 | struct coro *coro; |
2410 | MAGIC *mg; |
2709 | MAGIC *mg; |
2411 | HV *hv; |
2710 | HV *hv; |
|
|
2711 | CV *cb; |
2412 | int i; |
2712 | int i; |
|
|
2713 | |
|
|
2714 | if (items > 1) |
|
|
2715 | { |
|
|
2716 | cb = coro_sv_2cv (aTHX_ ST (1)); |
|
|
2717 | |
|
|
2718 | if (!ix) |
|
|
2719 | { |
|
|
2720 | if (CvISXSUB (cb)) |
|
|
2721 | croak ("Coro::State doesn't support XS functions as coroutine start, caught"); |
|
|
2722 | |
|
|
2723 | if (!CvROOT (cb)) |
|
|
2724 | croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); |
|
|
2725 | } |
|
|
2726 | } |
2413 | |
2727 | |
2414 | Newz (0, coro, 1, struct coro); |
2728 | Newz (0, coro, 1, struct coro); |
2415 | coro->args = newAV (); |
2729 | coro->args = newAV (); |
2416 | coro->flags = CF_NEW; |
2730 | coro->flags = CF_NEW; |
2417 | |
2731 | |
… | |
… | |
2422 | coro->hv = hv = newHV (); |
2736 | coro->hv = hv = newHV (); |
2423 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2737 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2424 | mg->mg_flags |= MGf_DUP; |
2738 | mg->mg_flags |= MGf_DUP; |
2425 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2739 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2426 | |
2740 | |
|
|
2741 | if (items > 1) |
|
|
2742 | { |
2427 | av_extend (coro->args, items - 1); |
2743 | av_extend (coro->args, items - 1 + ix - 1); |
|
|
2744 | |
|
|
2745 | if (ix) |
|
|
2746 | { |
|
|
2747 | av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); |
|
|
2748 | cb = cv_coro_run; |
|
|
2749 | } |
|
|
2750 | |
|
|
2751 | coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); |
|
|
2752 | |
2428 | for (i = 1; i < items; i++) |
2753 | for (i = 2; i < items; i++) |
2429 | av_push (coro->args, newSVsv (ST (i))); |
2754 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2755 | } |
2430 | } |
2756 | } |
2431 | OUTPUT: |
2757 | OUTPUT: |
2432 | RETVAL |
2758 | RETVAL |
2433 | |
2759 | |
2434 | void |
2760 | void |
… | |
… | |
2632 | |
2958 | |
2633 | BOOT: |
2959 | BOOT: |
2634 | { |
2960 | { |
2635 | int i; |
2961 | int i; |
2636 | |
2962 | |
2637 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
2963 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
2638 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2964 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2639 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2965 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2640 | |
2966 | cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); |
|
|
2967 | cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); |
2641 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
2968 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); |
2642 | SvREADONLY_on (coro_current); |
2969 | |
|
|
2970 | sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); |
|
|
2971 | sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); |
|
|
2972 | cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); |
|
|
2973 | cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); |
2643 | |
2974 | |
2644 | coro_stash = gv_stashpv ("Coro", TRUE); |
2975 | coro_stash = gv_stashpv ("Coro", TRUE); |
2645 | |
2976 | |
2646 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
2977 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
2647 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
2978 | newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); |
… | |
… | |
2736 | CODE: |
3067 | CODE: |
2737 | RETVAL = coro_nready; |
3068 | RETVAL = coro_nready; |
2738 | OUTPUT: |
3069 | OUTPUT: |
2739 | RETVAL |
3070 | RETVAL |
2740 | |
3071 | |
2741 | # for async_pool speedup |
|
|
2742 | void |
3072 | void |
2743 | _pool_1 (SV *cb) |
3073 | _pool_handler (...) |
2744 | CODE: |
3074 | CODE: |
2745 | { |
3075 | CORO_EXECUTE_SLF_XS (slf_init_pool_handler); |
2746 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2747 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
2748 | AV *defav = GvAV (PL_defgv); |
|
|
2749 | SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); |
|
|
2750 | AV *invoke_av; |
|
|
2751 | int i, len; |
|
|
2752 | |
3076 | |
2753 | if (!invoke) |
3077 | void |
|
|
3078 | async_pool (SV *cv, ...) |
|
|
3079 | PROTOTYPE: &@ |
|
|
3080 | PPCODE: |
|
|
3081 | { |
|
|
3082 | HV *hv = (HV *)av_pop (av_async_pool); |
|
|
3083 | AV *av = newAV (); |
|
|
3084 | SV *cb = ST (0); |
|
|
3085 | int i; |
|
|
3086 | |
|
|
3087 | av_extend (av, items - 2); |
|
|
3088 | for (i = 1; i < items; ++i) |
|
|
3089 | av_push (av, SvREFCNT_inc_NN (ST (i))); |
|
|
3090 | |
|
|
3091 | if ((SV *)hv == &PL_sv_undef) |
2754 | { |
3092 | { |
2755 | SV *old = PL_diehook; |
3093 | PUSHMARK (SP); |
2756 | PL_diehook = 0; |
3094 | EXTEND (SP, 2); |
2757 | SvREFCNT_dec (old); |
3095 | PUSHs (sv_Coro); |
2758 | croak ("\3async_pool terminate\2\n"); |
3096 | PUSHs ((SV *)cv_pool_handler); |
|
|
3097 | PUTBACK; |
|
|
3098 | call_sv ((SV *)cv_coro_state_new, G_SCALAR); |
|
|
3099 | SPAGAIN; |
|
|
3100 | |
|
|
3101 | hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); |
2759 | } |
3102 | } |
2760 | |
3103 | |
2761 | SvREFCNT_dec (coro->saved_deffh); |
|
|
2762 | coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv); |
|
|
2763 | |
|
|
2764 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
2765 | newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); |
|
|
2766 | |
|
|
2767 | invoke_av = (AV *)SvRV (invoke); |
|
|
2768 | len = av_len (invoke_av); |
|
|
2769 | |
|
|
2770 | sv_setsv (cb, AvARRAY (invoke_av)[0]); |
|
|
2771 | |
|
|
2772 | if (len > 0) |
|
|
2773 | { |
3104 | { |
2774 | av_fill (defav, len - 1); |
3105 | struct coro *coro = SvSTATE_hv (hv); |
2775 | for (i = 0; i < len; ++i) |
3106 | |
2776 | av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1])); |
3107 | assert (!coro->invoke_cb); |
|
|
3108 | assert (!coro->invoke_av); |
|
|
3109 | coro->invoke_cb = SvREFCNT_inc (cb); |
|
|
3110 | coro->invoke_av = av; |
2777 | } |
3111 | } |
|
|
3112 | |
|
|
3113 | api_ready (aTHX_ (SV *)hv); |
|
|
3114 | |
|
|
3115 | if (GIMME_V != G_VOID) |
|
|
3116 | XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); |
|
|
3117 | else |
|
|
3118 | SvREFCNT_dec (hv); |
2778 | } |
3119 | } |
|
|
3120 | |
|
|
3121 | SV * |
|
|
3122 | rouse_cb () |
|
|
3123 | PROTOTYPE: |
|
|
3124 | CODE: |
|
|
3125 | RETVAL = coro_new_rouse_cb (aTHX); |
|
|
3126 | OUTPUT: |
|
|
3127 | RETVAL |
2779 | |
3128 | |
2780 | void |
3129 | void |
2781 | _pool_2 (SV *cb) |
3130 | rouse_wait (...) |
|
|
3131 | PROTOTYPE: ;$ |
2782 | CODE: |
3132 | PPCODE: |
2783 | { |
3133 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
2784 | HV *hv = (HV *)SvRV (coro_current); |
|
|
2785 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
|
|
2786 | |
|
|
2787 | sv_setsv (cb, &PL_sv_undef); |
|
|
2788 | |
|
|
2789 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
|
|
2790 | coro->saved_deffh = 0; |
|
|
2791 | |
|
|
2792 | if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) |
|
|
2793 | || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) |
|
|
2794 | { |
|
|
2795 | SV *old = PL_diehook; |
|
|
2796 | PL_diehook = 0; |
|
|
2797 | SvREFCNT_dec (old); |
|
|
2798 | croak ("\3async_pool terminate\2\n"); |
|
|
2799 | } |
|
|
2800 | |
|
|
2801 | av_clear (GvAV (PL_defgv)); |
|
|
2802 | hv_store (hv, "desc", sizeof ("desc") - 1, |
|
|
2803 | newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); |
|
|
2804 | |
|
|
2805 | coro->prio = 0; |
|
|
2806 | |
|
|
2807 | if (coro->cctx && (coro->cctx->flags & CC_TRACE)) |
|
|
2808 | api_trace (aTHX_ coro_current, 0); |
|
|
2809 | |
|
|
2810 | av_push (av_async_pool, newSVsv (coro_current)); |
|
|
2811 | } |
|
|
2812 | |
3134 | |
2813 | |
3135 | |
2814 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3136 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2815 | |
3137 | |
2816 | BOOT: |
3138 | BOOT: |
… | |
… | |
2821 | |
3143 | |
2822 | SV * |
3144 | SV * |
2823 | new (SV *klass, SV *count = 0) |
3145 | new (SV *klass, SV *count = 0) |
2824 | CODE: |
3146 | CODE: |
2825 | RETVAL = sv_bless ( |
3147 | RETVAL = sv_bless ( |
2826 | coro_semaphore_new (count && SvOK (count) ? SvIV (count) : 1), |
3148 | coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1), |
2827 | GvSTASH (CvGV (cv)) |
3149 | GvSTASH (CvGV (cv)) |
2828 | ); |
3150 | ); |
2829 | OUTPUT: |
3151 | OUTPUT: |
2830 | RETVAL |
3152 | RETVAL |
2831 | |
3153 | |
2832 | # helper for Coro::Channel |
3154 | # helper for Coro::Channel |
2833 | SV * |
3155 | SV * |
2834 | _alloc (int count) |
3156 | _alloc (int count) |
2835 | CODE: |
3157 | CODE: |
2836 | RETVAL = coro_semaphore_new (count); |
3158 | RETVAL = coro_waitarray_new (aTHX_ count); |
2837 | OUTPUT: |
3159 | OUTPUT: |
2838 | RETVAL |
3160 | RETVAL |
2839 | |
3161 | |
2840 | SV * |
3162 | SV * |
2841 | count (SV *self) |
3163 | count (SV *self) |
… | |
… | |
2850 | adjust = 1 |
3172 | adjust = 1 |
2851 | CODE: |
3173 | CODE: |
2852 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
3174 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
2853 | |
3175 | |
2854 | void |
3176 | void |
2855 | down (SV *self) |
3177 | down (...) |
2856 | ALIAS: |
|
|
2857 | Coro::Signal::wait = 0 |
|
|
2858 | CODE: |
3178 | CODE: |
2859 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
3179 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
2860 | |
3180 | |
2861 | void |
3181 | void |
2862 | wait (SV *self) |
3182 | wait (...) |
2863 | CODE: |
3183 | CODE: |
2864 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); |
3184 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); |
2865 | |
3185 | |
2866 | void |
3186 | void |
2867 | try (SV *self) |
3187 | try (SV *self) |
… | |
… | |
2903 | |
3223 | |
2904 | SV * |
3224 | SV * |
2905 | new (SV *klass) |
3225 | new (SV *klass) |
2906 | CODE: |
3226 | CODE: |
2907 | RETVAL = sv_bless ( |
3227 | RETVAL = sv_bless ( |
2908 | coro_semaphore_new (0), |
3228 | coro_waitarray_new (aTHX_ 0), |
2909 | GvSTASH (CvGV (cv)) |
3229 | GvSTASH (CvGV (cv)) |
2910 | ); |
3230 | ); |
2911 | OUTPUT: |
3231 | OUTPUT: |
2912 | RETVAL |
3232 | RETVAL |
2913 | |
3233 | |
2914 | void |
3234 | void |
2915 | broadcast (SV *self, int adjust = 1) |
3235 | wait (...) |
2916 | CODE: |
3236 | CODE: |
|
|
3237 | CORO_EXECUTE_SLF_XS (slf_init_signal_wait); |
|
|
3238 | |
|
|
3239 | void |
|
|
3240 | broadcast (SV *self) |
|
|
3241 | CODE: |
|
|
3242 | { |
|
|
3243 | AV *av = (AV *)SvRV (self); |
|
|
3244 | coro_signal_wake (aTHX_ av, AvFILLp (av)); |
|
|
3245 | } |
|
|
3246 | |
|
|
3247 | void |
|
|
3248 | send (SV *self) |
|
|
3249 | CODE: |
2917 | { |
3250 | { |
2918 | AV *av = (AV *)SvRV (self); |
3251 | AV *av = (AV *)SvRV (self); |
2919 | SvIVX (AvARRAY (av)[0]) = 0; /* not necessary, but gives me fuzzy warm feelings */ |
3252 | |
2920 | coro_semaphore_adjust (aTHX_ av, AvFILLp (av) + 1 - 1); |
3253 | if (AvFILLp (av)) |
|
|
3254 | coro_signal_wake (aTHX_ av, 1); |
|
|
3255 | else |
2921 | SvIVX (AvARRAY (av)[0]) = 0; /* necessary */ |
3256 | SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */ |
2922 | } |
3257 | } |
|
|
3258 | |
|
|
3259 | IV |
|
|
3260 | awaited (SV *self) |
|
|
3261 | CODE: |
|
|
3262 | RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1; |
|
|
3263 | OUTPUT: |
|
|
3264 | RETVAL |
2923 | |
3265 | |
2924 | |
3266 | |
2925 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
3267 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2926 | |
3268 | |
2927 | BOOT: |
3269 | BOOT: |
… | |
… | |
2955 | |
3297 | |
2956 | void |
3298 | void |
2957 | _register (char *target, char *proto, SV *req) |
3299 | _register (char *target, char *proto, SV *req) |
2958 | CODE: |
3300 | CODE: |
2959 | { |
3301 | { |
2960 | HV *st; |
3302 | CV *req_cv = coro_sv_2cv (aTHX_ req); |
2961 | GV *gvp; |
|
|
2962 | CV *req_cv = sv_2cv (req, &st, &gvp, 0); |
|
|
2963 | /* newXSproto doesn't return the CV on 5.8 */ |
3303 | /* newXSproto doesn't return the CV on 5.8 */ |
2964 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3304 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
2965 | sv_setpv ((SV *)slf_cv, proto); |
3305 | sv_setpv ((SV *)slf_cv, proto); |
2966 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3306 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
2967 | } |
3307 | } |