… | |
… | |
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 |
… | |
… | |
246 | /* state data */ |
246 | /* state data */ |
247 | struct CoroSLF slf_frame; /* saved slf frame */ |
247 | struct CoroSLF slf_frame; /* saved slf frame */ |
248 | AV *mainstack; |
248 | AV *mainstack; |
249 | perl_slots *slot; /* basically the saved sp */ |
249 | perl_slots *slot; /* basically the saved sp */ |
250 | |
250 | |
|
|
251 | CV *startcv; /* the CV to execute */ |
251 | AV *args; /* data associated with this coroutine (initial args) */ |
252 | AV *args; /* data associated with this coroutine (initial args) */ |
252 | int refcnt; /* coroutines are refcounted, yes */ |
253 | int refcnt; /* coroutines are refcounted, yes */ |
253 | int flags; /* CF_ flags */ |
254 | int flags; /* CF_ flags */ |
254 | HV *hv; /* the perl hash associated with this coro, if any */ |
255 | HV *hv; /* the perl hash associated with this coro, if any */ |
255 | void (*on_destroy)(pTHX_ struct coro *coro); |
256 | void (*on_destroy)(pTHX_ struct coro *coro); |
256 | |
257 | |
257 | /* statistics */ |
258 | /* statistics */ |
258 | int usecount; /* number of transfers to this coro */ |
259 | int usecount; /* number of transfers to this coro */ |
259 | |
260 | |
260 | /* coro process data */ |
261 | /* coro process data */ |
261 | int prio; |
262 | int prio; |
262 | SV *except; /* exception to be thrown */ |
263 | SV *except; /* exception to be thrown */ |
|
|
264 | SV *rouse_cb; |
263 | |
265 | |
264 | /* async_pool */ |
266 | /* async_pool */ |
265 | SV *saved_deffh; |
267 | SV *saved_deffh; |
266 | |
268 | |
267 | /* linked list */ |
269 | /* linked list */ |
… | |
… | |
320 | #if PERL_VERSION_ATLEAST (5,10,0) |
322 | #if PERL_VERSION_ATLEAST (5,10,0) |
321 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
323 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
322 | get_hv (name, create); |
324 | get_hv (name, create); |
323 | #endif |
325 | #endif |
324 | return get_hv (name, create); |
326 | return get_hv (name, create); |
|
|
327 | } |
|
|
328 | |
|
|
329 | /* may croak */ |
|
|
330 | INLINE CV * |
|
|
331 | coro_sv_2cv (SV *sv) |
|
|
332 | { |
|
|
333 | HV *st; |
|
|
334 | GV *gvp; |
|
|
335 | return sv_2cv (sv, &st, &gvp, 0); |
325 | } |
336 | } |
326 | |
337 | |
327 | static AV * |
338 | static AV * |
328 | coro_clone_padlist (pTHX_ CV *cv) |
339 | coro_clone_padlist (pTHX_ CV *cv) |
329 | { |
340 | { |
… | |
… | |
448 | else |
459 | else |
449 | { |
460 | { |
450 | #if CORO_PREFER_PERL_FUNCTIONS |
461 | #if CORO_PREFER_PERL_FUNCTIONS |
451 | /* this is probably cleaner? but also slower! */ |
462 | /* this is probably cleaner? but also slower! */ |
452 | /* in practise, it seems to be less stable */ |
463 | /* in practise, it seems to be less stable */ |
453 | CV *cp = Perl_cv_clone (cv); |
464 | CV *cp = Perl_cv_clone (aTHX_ cv); |
454 | CvPADLIST (cv) = CvPADLIST (cp); |
465 | CvPADLIST (cv) = CvPADLIST (cp); |
455 | CvPADLIST (cp) = 0; |
466 | CvPADLIST (cp) = 0; |
456 | SvREFCNT_dec (cp); |
467 | SvREFCNT_dec (cp); |
457 | #else |
468 | #else |
458 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
469 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
… | |
… | |
602 | * of perl.c:init_stacks, except that it uses less memory |
613 | * of perl.c:init_stacks, except that it uses less memory |
603 | * on the (sometimes correct) assumption that coroutines do |
614 | * on the (sometimes correct) assumption that coroutines do |
604 | * not usually need a lot of stackspace. |
615 | * not usually need a lot of stackspace. |
605 | */ |
616 | */ |
606 | #if CORO_PREFER_PERL_FUNCTIONS |
617 | #if CORO_PREFER_PERL_FUNCTIONS |
607 | # define coro_init_stacks init_stacks |
618 | # define coro_init_stacks(thx) init_stacks () |
608 | #else |
619 | #else |
609 | static void |
620 | static void |
610 | coro_init_stacks (pTHX) |
621 | coro_init_stacks (pTHX) |
611 | { |
622 | { |
612 | PL_curstackinfo = new_stackinfo(32, 8); |
623 | PL_curstackinfo = new_stackinfo(32, 8); |
… | |
… | |
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); |
925 | |
936 | |
926 | coro_destruct_stacks (aTHX); |
937 | coro_destruct_stacks (aTHX); |
927 | } |
938 | } |
928 | |
939 | |
929 | INLINE void |
940 | INLINE void |
… | |
… | |
1016 | SAVETMPS; |
1027 | SAVETMPS; |
1017 | EXTEND (SP, 3); |
1028 | EXTEND (SP, 3); |
1018 | PUSHMARK (SP); |
1029 | PUSHMARK (SP); |
1019 | PUSHs (&PL_sv_yes); |
1030 | PUSHs (&PL_sv_yes); |
1020 | PUSHs (fullname); |
1031 | PUSHs (fullname); |
1021 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
1032 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
1022 | PUTBACK; |
1033 | PUTBACK; |
1023 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
1034 | 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); |
1035 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1025 | SPAGAIN; |
1036 | SPAGAIN; |
1026 | FREETMPS; |
1037 | FREETMPS; |
… | |
… | |
1132 | transfer_tail (aTHX); |
1143 | transfer_tail (aTHX); |
1133 | |
1144 | |
1134 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1145 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1135 | PL_restartop = PL_op; |
1146 | PL_restartop = PL_op; |
1136 | perl_run (PL_curinterp); |
1147 | perl_run (PL_curinterp); |
|
|
1148 | /* |
|
|
1149 | * Unfortunately, there is no way to get at the return values of the |
|
|
1150 | * coro body here, as perl_run destroys these |
|
|
1151 | */ |
1137 | |
1152 | |
1138 | /* |
1153 | /* |
1139 | * If perl-run returns we assume exit() was being called or the coro |
1154 | * 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) |
1155 | * 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 |
1156 | * reason for perl_run to return. We try to exit by jumping to the |
… | |
… | |
1433 | |
1448 | |
1434 | coro->slot = 0; |
1449 | coro->slot = 0; |
1435 | } |
1450 | } |
1436 | |
1451 | |
1437 | cctx_destroy (coro->cctx); |
1452 | cctx_destroy (coro->cctx); |
|
|
1453 | SvREFCNT_dec (coro->startcv); |
1438 | SvREFCNT_dec (coro->args); |
1454 | SvREFCNT_dec (coro->args); |
|
|
1455 | SvREFCNT_dec (CORO_THROW); |
1439 | |
1456 | |
1440 | if (coro->next) coro->next->prev = coro->prev; |
1457 | if (coro->next) coro->next->prev = coro->prev; |
1441 | if (coro->prev) coro->prev->next = coro->next; |
1458 | if (coro->prev) coro->prev->next = coro->next; |
1442 | if (coro == coro_first) coro_first = coro->next; |
1459 | if (coro == coro_first) coro_first = coro->next; |
1443 | |
1460 | |
… | |
… | |
1497 | |
1514 | |
1498 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1515 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1499 | TRANSFER (ta, 1); |
1516 | TRANSFER (ta, 1); |
1500 | } |
1517 | } |
1501 | |
1518 | |
|
|
1519 | /*****************************************************************************/ |
|
|
1520 | /* gensub: simple closure generation utility */ |
|
|
1521 | |
|
|
1522 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
|
|
1523 | |
|
|
1524 | /* create a closure from XS, returns a code reference */ |
|
|
1525 | /* the arg can be accessed via GENSUB_ARG from the callback */ |
|
|
1526 | /* the callback must use dXSARGS/XSRETURN */ |
|
|
1527 | static SV * |
|
|
1528 | gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
|
|
1529 | { |
|
|
1530 | CV *cv = (CV *)newSV (0); |
|
|
1531 | |
|
|
1532 | sv_upgrade ((SV *)cv, SVt_PVCV); |
|
|
1533 | |
|
|
1534 | CvANON_on (cv); |
|
|
1535 | CvISXSUB_on (cv); |
|
|
1536 | CvXSUB (cv) = xsub; |
|
|
1537 | GENSUB_ARG = arg; |
|
|
1538 | |
|
|
1539 | return newRV_noinc ((SV *)cv); |
|
|
1540 | } |
|
|
1541 | |
1502 | /** Coro ********************************************************************/ |
1542 | /** Coro ********************************************************************/ |
1503 | |
1543 | |
1504 | INLINE void |
1544 | INLINE void |
1505 | coro_enq (pTHX_ struct coro *coro) |
1545 | coro_enq (pTHX_ struct coro *coro) |
1506 | { |
1546 | { |
… | |
… | |
1703 | if (coro->flags & CF_RUNNING) |
1743 | if (coro->flags & CF_RUNNING) |
1704 | PL_runops = RUNOPS_DEFAULT; |
1744 | PL_runops = RUNOPS_DEFAULT; |
1705 | else |
1745 | else |
1706 | coro->slot->runops = RUNOPS_DEFAULT; |
1746 | coro->slot->runops = RUNOPS_DEFAULT; |
1707 | } |
1747 | } |
|
|
1748 | } |
|
|
1749 | |
|
|
1750 | /*****************************************************************************/ |
|
|
1751 | /* rouse callback */ |
|
|
1752 | |
|
|
1753 | #define CORO_MAGIC_type_rouse PERL_MAGIC_ext |
|
|
1754 | |
|
|
1755 | static void |
|
|
1756 | coro_rouse_callback (pTHX_ CV *cv) |
|
|
1757 | { |
|
|
1758 | dXSARGS; |
|
|
1759 | SV *data = (SV *)GENSUB_ARG; |
|
|
1760 | |
|
|
1761 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
|
|
1762 | { |
|
|
1763 | /* first call, set args */ |
|
|
1764 | int i; |
|
|
1765 | AV *av = newAV (); |
|
|
1766 | SV *coro = SvRV (data); |
|
|
1767 | |
|
|
1768 | SvRV_set (data, (SV *)av); |
|
|
1769 | api_ready (aTHX_ coro); |
|
|
1770 | SvREFCNT_dec (coro); |
|
|
1771 | |
|
|
1772 | /* better take a full copy of the arguments */ |
|
|
1773 | while (items--) |
|
|
1774 | av_store (av, items, newSVsv (ST (items))); |
|
|
1775 | } |
|
|
1776 | |
|
|
1777 | XSRETURN_EMPTY; |
|
|
1778 | } |
|
|
1779 | |
|
|
1780 | static int |
|
|
1781 | slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) |
|
|
1782 | { |
|
|
1783 | SV *data = (SV *)frame->data; |
|
|
1784 | |
|
|
1785 | if (CORO_THROW) |
|
|
1786 | return 0; |
|
|
1787 | |
|
|
1788 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
|
|
1789 | return 1; |
|
|
1790 | |
|
|
1791 | /* now push all results on the stack */ |
|
|
1792 | { |
|
|
1793 | dSP; |
|
|
1794 | AV *av = (AV *)SvRV (data); |
|
|
1795 | int i; |
|
|
1796 | |
|
|
1797 | EXTEND (SP, AvFILLp (av) + 1); |
|
|
1798 | for (i = 0; i <= AvFILLp (av); ++i) |
|
|
1799 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
|
|
1800 | |
|
|
1801 | /* we have stolen the elements, so ste length to zero and free */ |
|
|
1802 | AvFILLp (av) = -1; |
|
|
1803 | av_undef (av); |
|
|
1804 | |
|
|
1805 | PUTBACK; |
|
|
1806 | } |
|
|
1807 | |
|
|
1808 | return 0; |
|
|
1809 | } |
|
|
1810 | |
|
|
1811 | static void |
|
|
1812 | slf_init_rouse_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1813 | { |
|
|
1814 | SV *cb; |
|
|
1815 | |
|
|
1816 | if (items) |
|
|
1817 | cb = arg [0]; |
|
|
1818 | else |
|
|
1819 | { |
|
|
1820 | struct coro *coro = SvSTATE_current; |
|
|
1821 | |
|
|
1822 | if (!coro->rouse_cb) |
|
|
1823 | croak ("Coro::rouse_wait called without rouse callback, and no default rouse callback found either,"); |
|
|
1824 | |
|
|
1825 | cb = sv_2mortal (coro->rouse_cb); |
|
|
1826 | coro->rouse_cb = 0; |
|
|
1827 | } |
|
|
1828 | |
|
|
1829 | if (!SvROK (cb) |
|
|
1830 | || SvTYPE (SvRV (cb)) != SVt_PVCV |
|
|
1831 | || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) |
|
|
1832 | croak ("Coro::rouse_wait called with illegal callback argument,"); |
|
|
1833 | |
|
|
1834 | { |
|
|
1835 | CV *cv = (CV *)SvRV (cb); /* for GENSUB_ARG */ |
|
|
1836 | SV *data = (SV *)GENSUB_ARG; |
|
|
1837 | |
|
|
1838 | frame->data = (void *)data; |
|
|
1839 | frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule; |
|
|
1840 | frame->check = slf_check_rouse_wait; |
|
|
1841 | } |
|
|
1842 | } |
|
|
1843 | |
|
|
1844 | static SV * |
|
|
1845 | coro_new_rouse_cb (pTHX) |
|
|
1846 | { |
|
|
1847 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1848 | struct coro *coro = SvSTATE_hv (hv); |
|
|
1849 | SV *data = newRV_inc ((SV *)hv); |
|
|
1850 | SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data); |
|
|
1851 | |
|
|
1852 | sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0); |
|
|
1853 | SvREFCNT_dec (data); /* magicext increases the refcount */ |
|
|
1854 | |
|
|
1855 | SvREFCNT_dec (coro->rouse_cb); |
|
|
1856 | coro->rouse_cb = SvREFCNT_inc_NN (cb); |
|
|
1857 | |
|
|
1858 | return cb; |
1708 | } |
1859 | } |
1709 | |
1860 | |
1710 | /*****************************************************************************/ |
1861 | /*****************************************************************************/ |
1711 | /* schedule-like-function opcode (SLF) */ |
1862 | /* schedule-like-function opcode (SLF) */ |
1712 | |
1863 | |
… | |
… | |
2007 | SV **ary; |
2158 | SV **ary; |
2008 | |
2159 | |
2009 | /* unfortunately, building manually saves memory */ |
2160 | /* unfortunately, building manually saves memory */ |
2010 | Newx (ary, 2, SV *); |
2161 | Newx (ary, 2, SV *); |
2011 | AvALLOC (av) = ary; |
2162 | AvALLOC (av) = ary; |
2012 | AvARRAY (av) = ary; |
2163 | /*AvARRAY (av) = ary;*/ |
|
|
2164 | SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ |
2013 | AvMAX (av) = 1; |
2165 | AvMAX (av) = 1; |
2014 | AvFILLp (av) = 0; |
2166 | AvFILLp (av) = 0; |
2015 | ary [0] = newSViv (count); |
2167 | ary [0] = newSViv (count); |
2016 | |
2168 | |
2017 | return newRV_noinc ((SV *)av); |
2169 | return newRV_noinc ((SV *)av); |
… | |
… | |
2037 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2189 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2038 | AvARRAY (av)[1] = count_sv; |
2190 | AvARRAY (av)[1] = count_sv; |
2039 | cb = av_shift (av); |
2191 | cb = av_shift (av); |
2040 | |
2192 | |
2041 | if (SvOBJECT (cb)) |
2193 | if (SvOBJECT (cb)) |
|
|
2194 | { |
2042 | api_ready (aTHX_ cb); |
2195 | api_ready (aTHX_ cb); |
2043 | else |
2196 | --count; |
2044 | croak ("callbacks not yet supported"); |
2197 | } |
|
|
2198 | else if (SvTYPE (cb) == SVt_PVCV) |
|
|
2199 | { |
|
|
2200 | dSP; |
|
|
2201 | PUSHMARK (SP); |
|
|
2202 | XPUSHs (sv_2mortal (newRV_inc ((SV *)av))); |
|
|
2203 | PUTBACK; |
|
|
2204 | call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR); |
|
|
2205 | } |
2045 | |
2206 | |
2046 | SvREFCNT_dec (cb); |
2207 | SvREFCNT_dec (cb); |
2047 | |
|
|
2048 | --count; |
|
|
2049 | } |
2208 | } |
2050 | } |
2209 | } |
2051 | |
2210 | |
2052 | static void |
2211 | static void |
2053 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
2212 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
… | |
… | |
2135 | } |
2294 | } |
2136 | |
2295 | |
2137 | static void |
2296 | static void |
2138 | slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2297 | slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2139 | { |
2298 | { |
|
|
2299 | if (items >= 2) |
|
|
2300 | { |
|
|
2301 | /* callback form */ |
|
|
2302 | AV *av = (AV *)SvRV (arg [0]); |
|
|
2303 | CV *cb_cv = coro_sv_2cv (arg [1]); |
|
|
2304 | |
|
|
2305 | av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); |
|
|
2306 | |
|
|
2307 | if (SvIVX (AvARRAY (av)[0]) > 0) |
|
|
2308 | coro_semaphore_adjust (aTHX_ av, 0); |
|
|
2309 | |
|
|
2310 | frame->prepare = prepare_nop; |
|
|
2311 | frame->check = slf_check_nop; |
|
|
2312 | } |
|
|
2313 | else |
|
|
2314 | { |
2140 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
2315 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
2141 | frame->check = slf_check_semaphore_wait; |
2316 | frame->check = slf_check_semaphore_wait; |
|
|
2317 | } |
2142 | } |
2318 | } |
2143 | |
2319 | |
2144 | /* signal */ |
2320 | /* signal */ |
2145 | |
2321 | |
2146 | static void |
2322 | static void |
… | |
… | |
2194 | |
2370 | |
2195 | frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ |
2371 | frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ |
2196 | frame->prepare = prepare_schedule; |
2372 | frame->prepare = prepare_schedule; |
2197 | frame->check = slf_check_signal_wait; |
2373 | frame->check = slf_check_signal_wait; |
2198 | } |
2374 | } |
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 | } |
2375 | } |
2223 | |
2376 | |
2224 | /*****************************************************************************/ |
2377 | /*****************************************************************************/ |
2225 | /* Coro::AIO */ |
2378 | /* Coro::AIO */ |
2226 | |
2379 | |
… | |
… | |
2466 | CODE: |
2619 | CODE: |
2467 | { |
2620 | { |
2468 | struct coro *coro; |
2621 | struct coro *coro; |
2469 | MAGIC *mg; |
2622 | MAGIC *mg; |
2470 | HV *hv; |
2623 | HV *hv; |
|
|
2624 | CV *cb; |
2471 | int i; |
2625 | int i; |
|
|
2626 | |
|
|
2627 | if (items > 1) |
|
|
2628 | { |
|
|
2629 | cb = coro_sv_2cv (ST (1)); |
|
|
2630 | |
|
|
2631 | if (CvISXSUB (cb)) |
|
|
2632 | croak ("Coro::State doesn't support XS functions as coroutine start, caught"); |
|
|
2633 | |
|
|
2634 | if (!CvROOT (cb)) |
|
|
2635 | croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); |
|
|
2636 | } |
2472 | |
2637 | |
2473 | Newz (0, coro, 1, struct coro); |
2638 | Newz (0, coro, 1, struct coro); |
2474 | coro->args = newAV (); |
2639 | coro->args = newAV (); |
2475 | coro->flags = CF_NEW; |
2640 | coro->flags = CF_NEW; |
2476 | |
2641 | |
… | |
… | |
2481 | coro->hv = hv = newHV (); |
2646 | coro->hv = hv = newHV (); |
2482 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2647 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2483 | mg->mg_flags |= MGf_DUP; |
2648 | mg->mg_flags |= MGf_DUP; |
2484 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2649 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2485 | |
2650 | |
|
|
2651 | if (items > 1) |
|
|
2652 | { |
|
|
2653 | coro->startcv = SvREFCNT_inc_NN (cb); |
|
|
2654 | |
2486 | av_extend (coro->args, items - 1); |
2655 | av_extend (coro->args, items - 1); |
2487 | for (i = 1; i < items; i++) |
2656 | for (i = 2; i < items; i++) |
2488 | av_push (coro->args, newSVsv (ST (i))); |
2657 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2658 | } |
2489 | } |
2659 | } |
2490 | OUTPUT: |
2660 | OUTPUT: |
2491 | RETVAL |
2661 | RETVAL |
2492 | |
2662 | |
2493 | void |
2663 | void |
… | |
… | |
2867 | api_trace (aTHX_ coro_current, 0); |
3037 | api_trace (aTHX_ coro_current, 0); |
2868 | |
3038 | |
2869 | av_push (av_async_pool, newSVsv (coro_current)); |
3039 | av_push (av_async_pool, newSVsv (coro_current)); |
2870 | } |
3040 | } |
2871 | |
3041 | |
|
|
3042 | SV * |
|
|
3043 | rouse_cb () |
|
|
3044 | PROTOTYPE: |
|
|
3045 | CODE: |
|
|
3046 | RETVAL = coro_new_rouse_cb (aTHX); |
|
|
3047 | OUTPUT: |
|
|
3048 | RETVAL |
|
|
3049 | |
|
|
3050 | void |
|
|
3051 | rouse_wait (...) |
|
|
3052 | PROTOTYPE: ;$ |
|
|
3053 | PPCODE: |
|
|
3054 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
|
|
3055 | |
2872 | |
3056 | |
2873 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3057 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2874 | |
3058 | |
2875 | BOOT: |
3059 | BOOT: |
2876 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
3060 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
… | |
… | |
2909 | adjust = 1 |
3093 | adjust = 1 |
2910 | CODE: |
3094 | CODE: |
2911 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
3095 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
2912 | |
3096 | |
2913 | void |
3097 | void |
2914 | down (SV *self) |
3098 | down (...) |
2915 | CODE: |
3099 | CODE: |
2916 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
3100 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
2917 | |
3101 | |
2918 | void |
3102 | void |
2919 | wait (SV *self) |
3103 | wait (...) |
2920 | CODE: |
3104 | CODE: |
2921 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); |
3105 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); |
2922 | |
3106 | |
2923 | void |
3107 | void |
2924 | try (SV *self) |
3108 | try (SV *self) |
… | |
… | |
2967 | ); |
3151 | ); |
2968 | OUTPUT: |
3152 | OUTPUT: |
2969 | RETVAL |
3153 | RETVAL |
2970 | |
3154 | |
2971 | void |
3155 | void |
2972 | wait (SV *self) |
3156 | wait (...) |
2973 | CODE: |
3157 | CODE: |
2974 | CORO_EXECUTE_SLF_XS (slf_init_signal_wait); |
3158 | CORO_EXECUTE_SLF_XS (slf_init_signal_wait); |
2975 | |
3159 | |
2976 | void |
3160 | void |
2977 | broadcast (SV *self) |
3161 | broadcast (SV *self) |
… | |
… | |
3034 | |
3218 | |
3035 | void |
3219 | void |
3036 | _register (char *target, char *proto, SV *req) |
3220 | _register (char *target, char *proto, SV *req) |
3037 | CODE: |
3221 | CODE: |
3038 | { |
3222 | { |
3039 | HV *st; |
|
|
3040 | GV *gvp; |
|
|
3041 | CV *req_cv = sv_2cv (req, &st, &gvp, 0); |
3223 | CV *req_cv = coro_sv_2cv (req); |
3042 | /* newXSproto doesn't return the CV on 5.8 */ |
3224 | /* newXSproto doesn't return the CV on 5.8 */ |
3043 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3225 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3044 | sv_setpv ((SV *)slf_cv, proto); |
3226 | sv_setpv ((SV *)slf_cv, proto); |
3045 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3227 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3046 | } |
3228 | } |