… | |
… | |
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 |
… | |
… | |
165 | static size_t cctx_stacksize = CORO_STACKSIZE; |
165 | static size_t cctx_stacksize = CORO_STACKSIZE; |
166 | static struct CoroAPI coroapi; |
166 | static struct CoroAPI coroapi; |
167 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
167 | static AV *main_mainstack; /* used to differentiate between $main and others */ |
168 | static JMPENV *main_top_env; |
168 | static JMPENV *main_top_env; |
169 | static HV *coro_state_stash, *coro_stash; |
169 | static HV *coro_state_stash, *coro_stash; |
|
|
170 | static CV *coro_run_cv; |
170 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
171 | static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ |
171 | |
172 | |
172 | static GV *irsgv; /* $/ */ |
173 | static GV *irsgv; /* $/ */ |
173 | static GV *stdoutgv; /* *STDOUT */ |
174 | static GV *stdoutgv; /* *STDOUT */ |
174 | static SV *rv_diehook; |
175 | static SV *rv_diehook; |
… | |
… | |
246 | /* state data */ |
247 | /* state data */ |
247 | struct CoroSLF slf_frame; /* saved slf frame */ |
248 | struct CoroSLF slf_frame; /* saved slf frame */ |
248 | AV *mainstack; |
249 | AV *mainstack; |
249 | perl_slots *slot; /* basically the saved sp */ |
250 | perl_slots *slot; /* basically the saved sp */ |
250 | |
251 | |
|
|
252 | CV *startcv; /* the CV to execute */ |
251 | AV *args; /* data associated with this coroutine (initial args) */ |
253 | AV *args; /* data associated with this coroutine (initial args) */ |
252 | int refcnt; /* coroutines are refcounted, yes */ |
254 | int refcnt; /* coroutines are refcounted, yes */ |
253 | int flags; /* CF_ flags */ |
255 | int flags; /* CF_ flags */ |
254 | HV *hv; /* the perl hash associated with this coro, if any */ |
256 | HV *hv; /* the perl hash associated with this coro, if any */ |
255 | void (*on_destroy)(pTHX_ struct coro *coro); |
257 | void (*on_destroy)(pTHX_ struct coro *coro); |
256 | |
258 | |
257 | /* statistics */ |
259 | /* statistics */ |
258 | int usecount; /* number of transfers to this coro */ |
260 | int usecount; /* number of transfers to this coro */ |
259 | |
261 | |
260 | /* coro process data */ |
262 | /* coro process data */ |
261 | int prio; |
263 | int prio; |
262 | SV *except; /* exception to be thrown */ |
264 | SV *except; /* exception to be thrown */ |
|
|
265 | SV *rouse_cb; |
263 | |
266 | |
264 | /* async_pool */ |
267 | /* async_pool */ |
265 | SV *saved_deffh; |
268 | SV *saved_deffh; |
266 | |
269 | |
267 | /* linked list */ |
270 | /* linked list */ |
… | |
… | |
320 | #if PERL_VERSION_ATLEAST (5,10,0) |
323 | #if PERL_VERSION_ATLEAST (5,10,0) |
321 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
324 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
322 | get_hv (name, create); |
325 | get_hv (name, create); |
323 | #endif |
326 | #endif |
324 | return get_hv (name, create); |
327 | return get_hv (name, create); |
|
|
328 | } |
|
|
329 | |
|
|
330 | /* may croak */ |
|
|
331 | INLINE CV * |
|
|
332 | coro_sv_2cv (SV *sv) |
|
|
333 | { |
|
|
334 | HV *st; |
|
|
335 | GV *gvp; |
|
|
336 | return sv_2cv (sv, &st, &gvp, 0); |
325 | } |
337 | } |
326 | |
338 | |
327 | static AV * |
339 | static AV * |
328 | coro_clone_padlist (pTHX_ CV *cv) |
340 | coro_clone_padlist (pTHX_ CV *cv) |
329 | { |
341 | { |
… | |
… | |
448 | else |
460 | else |
449 | { |
461 | { |
450 | #if CORO_PREFER_PERL_FUNCTIONS |
462 | #if CORO_PREFER_PERL_FUNCTIONS |
451 | /* this is probably cleaner? but also slower! */ |
463 | /* this is probably cleaner? but also slower! */ |
452 | /* in practise, it seems to be less stable */ |
464 | /* in practise, it seems to be less stable */ |
453 | CV *cp = Perl_cv_clone (cv); |
465 | CV *cp = Perl_cv_clone (aTHX_ cv); |
454 | CvPADLIST (cv) = CvPADLIST (cp); |
466 | CvPADLIST (cv) = CvPADLIST (cp); |
455 | CvPADLIST (cp) = 0; |
467 | CvPADLIST (cp) = 0; |
456 | SvREFCNT_dec (cp); |
468 | SvREFCNT_dec (cp); |
457 | #else |
469 | #else |
458 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
470 | CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); |
… | |
… | |
602 | * of perl.c:init_stacks, except that it uses less memory |
614 | * of perl.c:init_stacks, except that it uses less memory |
603 | * on the (sometimes correct) assumption that coroutines do |
615 | * on the (sometimes correct) assumption that coroutines do |
604 | * not usually need a lot of stackspace. |
616 | * not usually need a lot of stackspace. |
605 | */ |
617 | */ |
606 | #if CORO_PREFER_PERL_FUNCTIONS |
618 | #if CORO_PREFER_PERL_FUNCTIONS |
607 | # define coro_init_stacks init_stacks |
619 | # define coro_init_stacks(thx) init_stacks () |
608 | #else |
620 | #else |
609 | static void |
621 | static void |
610 | coro_init_stacks (pTHX) |
622 | coro_init_stacks (pTHX) |
611 | { |
623 | { |
612 | PL_curstackinfo = new_stackinfo(32, 8); |
624 | PL_curstackinfo = new_stackinfo(32, 8); |
… | |
… | |
860 | dSP; |
872 | dSP; |
861 | UNOP myop; |
873 | UNOP myop; |
862 | |
874 | |
863 | Zero (&myop, 1, UNOP); |
875 | Zero (&myop, 1, UNOP); |
864 | myop.op_next = Nullop; |
876 | myop.op_next = Nullop; |
|
|
877 | myop.op_type = OP_ENTERSUB; |
865 | myop.op_flags = OPf_WANT_VOID; |
878 | myop.op_flags = OPf_WANT_VOID; |
866 | |
879 | |
867 | PUSHMARK (SP); |
880 | PUSHMARK (SP); |
868 | XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); |
881 | PUSHs ((SV *)coro->startcv); |
869 | PUTBACK; |
882 | PUTBACK; |
870 | PL_op = (OP *)&myop; |
883 | PL_op = (OP *)&myop; |
871 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
884 | PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); |
872 | SPAGAIN; |
|
|
873 | } |
885 | } |
874 | |
886 | |
875 | /* this newly created coroutine might be run on an existing cctx which most |
887 | /* 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. |
888 | * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. |
877 | */ |
889 | */ |
… | |
… | |
880 | |
892 | |
881 | /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ |
893 | /* 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; |
894 | coro_setup_op.op_next = PL_op; |
883 | coro_setup_op.op_type = OP_CUSTOM; |
895 | coro_setup_op.op_type = OP_CUSTOM; |
884 | coro_setup_op.op_ppaddr = pp_slf; |
896 | coro_setup_op.op_ppaddr = pp_slf; |
885 | /* no flags required, as an init function won't be called */ |
897 | /* no flags etc. required, as an init function won't be called */ |
886 | |
898 | |
887 | PL_op = (OP *)&coro_setup_op; |
899 | PL_op = (OP *)&coro_setup_op; |
888 | |
900 | |
889 | /* copy throw, in case it was set before coro_setup */ |
901 | /* copy throw, in case it was set before coro_setup */ |
890 | CORO_THROW = coro->except; |
902 | CORO_THROW = coro->except; |
… | |
… | |
919 | |
931 | |
920 | SvREFCNT_dec (PL_diehook); |
932 | SvREFCNT_dec (PL_diehook); |
921 | SvREFCNT_dec (PL_warnhook); |
933 | SvREFCNT_dec (PL_warnhook); |
922 | |
934 | |
923 | SvREFCNT_dec (coro->saved_deffh); |
935 | SvREFCNT_dec (coro->saved_deffh); |
924 | SvREFCNT_dec (CORO_THROW); |
936 | SvREFCNT_dec (coro->rouse_cb); |
925 | |
937 | |
926 | coro_destruct_stacks (aTHX); |
938 | coro_destruct_stacks (aTHX); |
927 | } |
939 | } |
928 | |
940 | |
929 | INLINE void |
941 | INLINE void |
… | |
… | |
1016 | SAVETMPS; |
1028 | SAVETMPS; |
1017 | EXTEND (SP, 3); |
1029 | EXTEND (SP, 3); |
1018 | PUSHMARK (SP); |
1030 | PUSHMARK (SP); |
1019 | PUSHs (&PL_sv_yes); |
1031 | PUSHs (&PL_sv_yes); |
1020 | PUSHs (fullname); |
1032 | PUSHs (fullname); |
1021 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
1033 | PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); |
1022 | PUTBACK; |
1034 | PUTBACK; |
1023 | cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); |
1035 | 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); |
1036 | if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); |
1025 | SPAGAIN; |
1037 | SPAGAIN; |
1026 | FREETMPS; |
1038 | FREETMPS; |
… | |
… | |
1132 | transfer_tail (aTHX); |
1144 | transfer_tail (aTHX); |
1133 | |
1145 | |
1134 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1146 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1135 | PL_restartop = PL_op; |
1147 | PL_restartop = PL_op; |
1136 | perl_run (PL_curinterp); |
1148 | perl_run (PL_curinterp); |
|
|
1149 | /* |
|
|
1150 | * Unfortunately, there is no way to get at the return values of the |
|
|
1151 | * coro body here, as perl_run destroys these |
|
|
1152 | */ |
1137 | |
1153 | |
1138 | /* |
1154 | /* |
1139 | * If perl-run returns we assume exit() was being called or the coro |
1155 | * 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) |
1156 | * 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 |
1157 | * reason for perl_run to return. We try to exit by jumping to the |
… | |
… | |
1433 | |
1449 | |
1434 | coro->slot = 0; |
1450 | coro->slot = 0; |
1435 | } |
1451 | } |
1436 | |
1452 | |
1437 | cctx_destroy (coro->cctx); |
1453 | cctx_destroy (coro->cctx); |
|
|
1454 | SvREFCNT_dec (coro->startcv); |
1438 | SvREFCNT_dec (coro->args); |
1455 | SvREFCNT_dec (coro->args); |
|
|
1456 | SvREFCNT_dec (CORO_THROW); |
1439 | |
1457 | |
1440 | if (coro->next) coro->next->prev = coro->prev; |
1458 | if (coro->next) coro->next->prev = coro->prev; |
1441 | if (coro->prev) coro->prev->next = coro->next; |
1459 | if (coro->prev) coro->prev->next = coro->next; |
1442 | if (coro == coro_first) coro_first = coro->next; |
1460 | if (coro == coro_first) coro_first = coro->next; |
1443 | |
1461 | |
… | |
… | |
1497 | |
1515 | |
1498 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1516 | prepare_transfer (aTHX_ &ta, prev_sv, next_sv); |
1499 | TRANSFER (ta, 1); |
1517 | TRANSFER (ta, 1); |
1500 | } |
1518 | } |
1501 | |
1519 | |
|
|
1520 | /*****************************************************************************/ |
|
|
1521 | /* gensub: simple closure generation utility */ |
|
|
1522 | |
|
|
1523 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
|
|
1524 | |
|
|
1525 | /* create a closure from XS, returns a code reference */ |
|
|
1526 | /* the arg can be accessed via GENSUB_ARG from the callback */ |
|
|
1527 | /* the callback must use dXSARGS/XSRETURN */ |
|
|
1528 | static SV * |
|
|
1529 | gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
|
|
1530 | { |
|
|
1531 | CV *cv = (CV *)newSV (0); |
|
|
1532 | |
|
|
1533 | sv_upgrade ((SV *)cv, SVt_PVCV); |
|
|
1534 | |
|
|
1535 | CvANON_on (cv); |
|
|
1536 | CvISXSUB_on (cv); |
|
|
1537 | CvXSUB (cv) = xsub; |
|
|
1538 | GENSUB_ARG = arg; |
|
|
1539 | |
|
|
1540 | return newRV_noinc ((SV *)cv); |
|
|
1541 | } |
|
|
1542 | |
1502 | /** Coro ********************************************************************/ |
1543 | /** Coro ********************************************************************/ |
1503 | |
1544 | |
1504 | INLINE void |
1545 | INLINE void |
1505 | coro_enq (pTHX_ struct coro *coro) |
1546 | coro_enq (pTHX_ struct coro *coro) |
1506 | { |
1547 | { |
… | |
… | |
1703 | if (coro->flags & CF_RUNNING) |
1744 | if (coro->flags & CF_RUNNING) |
1704 | PL_runops = RUNOPS_DEFAULT; |
1745 | PL_runops = RUNOPS_DEFAULT; |
1705 | else |
1746 | else |
1706 | coro->slot->runops = RUNOPS_DEFAULT; |
1747 | coro->slot->runops = RUNOPS_DEFAULT; |
1707 | } |
1748 | } |
|
|
1749 | } |
|
|
1750 | |
|
|
1751 | /*****************************************************************************/ |
|
|
1752 | /* rouse callback */ |
|
|
1753 | |
|
|
1754 | #define CORO_MAGIC_type_rouse PERL_MAGIC_ext |
|
|
1755 | |
|
|
1756 | static void |
|
|
1757 | coro_rouse_callback (pTHX_ CV *cv) |
|
|
1758 | { |
|
|
1759 | dXSARGS; |
|
|
1760 | SV *data = (SV *)GENSUB_ARG; |
|
|
1761 | |
|
|
1762 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
|
|
1763 | { |
|
|
1764 | /* first call, set args */ |
|
|
1765 | int i; |
|
|
1766 | AV *av = newAV (); |
|
|
1767 | SV *coro = SvRV (data); |
|
|
1768 | |
|
|
1769 | SvRV_set (data, (SV *)av); |
|
|
1770 | api_ready (aTHX_ coro); |
|
|
1771 | SvREFCNT_dec (coro); |
|
|
1772 | |
|
|
1773 | /* better take a full copy of the arguments */ |
|
|
1774 | while (items--) |
|
|
1775 | av_store (av, items, newSVsv (ST (items))); |
|
|
1776 | } |
|
|
1777 | |
|
|
1778 | XSRETURN_EMPTY; |
|
|
1779 | } |
|
|
1780 | |
|
|
1781 | static int |
|
|
1782 | slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) |
|
|
1783 | { |
|
|
1784 | SV *data = (SV *)frame->data; |
|
|
1785 | |
|
|
1786 | if (CORO_THROW) |
|
|
1787 | return 0; |
|
|
1788 | |
|
|
1789 | if (SvTYPE (SvRV (data)) != SVt_PVAV) |
|
|
1790 | return 1; |
|
|
1791 | |
|
|
1792 | /* now push all results on the stack */ |
|
|
1793 | { |
|
|
1794 | dSP; |
|
|
1795 | AV *av = (AV *)SvRV (data); |
|
|
1796 | int i; |
|
|
1797 | |
|
|
1798 | EXTEND (SP, AvFILLp (av) + 1); |
|
|
1799 | for (i = 0; i <= AvFILLp (av); ++i) |
|
|
1800 | PUSHs (sv_2mortal (AvARRAY (av)[i])); |
|
|
1801 | |
|
|
1802 | /* we have stolen the elements, so ste length to zero and free */ |
|
|
1803 | AvFILLp (av) = -1; |
|
|
1804 | av_undef (av); |
|
|
1805 | |
|
|
1806 | PUTBACK; |
|
|
1807 | } |
|
|
1808 | |
|
|
1809 | return 0; |
|
|
1810 | } |
|
|
1811 | |
|
|
1812 | static void |
|
|
1813 | slf_init_rouse_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
1814 | { |
|
|
1815 | SV *cb; |
|
|
1816 | |
|
|
1817 | if (items) |
|
|
1818 | cb = arg [0]; |
|
|
1819 | else |
|
|
1820 | { |
|
|
1821 | struct coro *coro = SvSTATE_current; |
|
|
1822 | |
|
|
1823 | if (!coro->rouse_cb) |
|
|
1824 | croak ("Coro::rouse_wait called without rouse callback, and no default rouse callback found either,"); |
|
|
1825 | |
|
|
1826 | cb = sv_2mortal (coro->rouse_cb); |
|
|
1827 | coro->rouse_cb = 0; |
|
|
1828 | } |
|
|
1829 | |
|
|
1830 | if (!SvROK (cb) |
|
|
1831 | || SvTYPE (SvRV (cb)) != SVt_PVCV |
|
|
1832 | || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) |
|
|
1833 | croak ("Coro::rouse_wait called with illegal callback argument,"); |
|
|
1834 | |
|
|
1835 | { |
|
|
1836 | CV *cv = (CV *)SvRV (cb); /* for GENSUB_ARG */ |
|
|
1837 | SV *data = (SV *)GENSUB_ARG; |
|
|
1838 | |
|
|
1839 | frame->data = (void *)data; |
|
|
1840 | frame->prepare = SvTYPE (SvRV (data)) == SVt_PVAV ? prepare_nop : prepare_schedule; |
|
|
1841 | frame->check = slf_check_rouse_wait; |
|
|
1842 | } |
|
|
1843 | } |
|
|
1844 | |
|
|
1845 | static SV * |
|
|
1846 | coro_new_rouse_cb (pTHX) |
|
|
1847 | { |
|
|
1848 | HV *hv = (HV *)SvRV (coro_current); |
|
|
1849 | struct coro *coro = SvSTATE_hv (hv); |
|
|
1850 | SV *data = newRV_inc ((SV *)hv); |
|
|
1851 | SV *cb = gensub (aTHX_ coro_rouse_callback, (void *)data); |
|
|
1852 | |
|
|
1853 | sv_magicext (SvRV (cb), data, CORO_MAGIC_type_rouse, 0, 0, 0); |
|
|
1854 | SvREFCNT_dec (data); /* magicext increases the refcount */ |
|
|
1855 | |
|
|
1856 | SvREFCNT_dec (coro->rouse_cb); |
|
|
1857 | coro->rouse_cb = SvREFCNT_inc_NN (cb); |
|
|
1858 | |
|
|
1859 | return cb; |
1708 | } |
1860 | } |
1709 | |
1861 | |
1710 | /*****************************************************************************/ |
1862 | /*****************************************************************************/ |
1711 | /* schedule-like-function opcode (SLF) */ |
1863 | /* schedule-like-function opcode (SLF) */ |
1712 | |
1864 | |
… | |
… | |
1995 | PerlIOBuf_get_cnt, |
2147 | PerlIOBuf_get_cnt, |
1996 | PerlIOBuf_set_ptrcnt, |
2148 | PerlIOBuf_set_ptrcnt, |
1997 | }; |
2149 | }; |
1998 | |
2150 | |
1999 | /*****************************************************************************/ |
2151 | /*****************************************************************************/ |
2000 | /* Coro::Semaphore */ |
2152 | /* Coro::Semaphore & Coro::Signal */ |
2001 | |
2153 | |
2002 | static SV * |
2154 | static SV * |
2003 | coro_semaphore_new (int count) |
2155 | coro_waitarray_new (pTHX_ int count) |
2004 | { |
2156 | { |
2005 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
2157 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
2006 | AV *av = newAV (); |
2158 | AV *av = newAV (); |
2007 | SV **ary; |
2159 | SV **ary; |
2008 | |
2160 | |
2009 | /* unfortunately, building manually saves memory */ |
2161 | /* unfortunately, building manually saves memory */ |
2010 | Newx (ary, 2, SV *); |
2162 | Newx (ary, 2, SV *); |
2011 | AvALLOC (av) = ary; |
2163 | AvALLOC (av) = ary; |
2012 | AvARRAY (av) = ary; |
2164 | /*AvARRAY (av) = ary;*/ |
|
|
2165 | SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ |
2013 | AvMAX (av) = 1; |
2166 | AvMAX (av) = 1; |
2014 | AvFILLp (av) = 0; |
2167 | AvFILLp (av) = 0; |
2015 | ary [0] = newSViv (count); |
2168 | ary [0] = newSViv (count); |
2016 | |
2169 | |
2017 | return newRV_noinc ((SV *)av); |
2170 | return newRV_noinc ((SV *)av); |
2018 | } |
2171 | } |
|
|
2172 | |
|
|
2173 | /* semaphore */ |
2019 | |
2174 | |
2020 | static void |
2175 | static void |
2021 | coro_semaphore_adjust (pTHX_ AV *av, IV adjust) |
2176 | coro_semaphore_adjust (pTHX_ AV *av, IV adjust) |
2022 | { |
2177 | { |
2023 | SV *count_sv = AvARRAY (av)[0]; |
2178 | SV *count_sv = AvARRAY (av)[0]; |
… | |
… | |
2035 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2190 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2036 | AvARRAY (av)[1] = count_sv; |
2191 | AvARRAY (av)[1] = count_sv; |
2037 | cb = av_shift (av); |
2192 | cb = av_shift (av); |
2038 | |
2193 | |
2039 | if (SvOBJECT (cb)) |
2194 | if (SvOBJECT (cb)) |
|
|
2195 | { |
2040 | api_ready (aTHX_ cb); |
2196 | api_ready (aTHX_ cb); |
2041 | else |
2197 | --count; |
2042 | croak ("callbacks not yet supported"); |
2198 | } |
|
|
2199 | else if (SvTYPE (cb) == SVt_PVCV) |
|
|
2200 | { |
|
|
2201 | dSP; |
|
|
2202 | PUSHMARK (SP); |
|
|
2203 | XPUSHs (sv_2mortal (newRV_inc ((SV *)av))); |
|
|
2204 | PUTBACK; |
|
|
2205 | call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR); |
|
|
2206 | } |
2043 | |
2207 | |
2044 | SvREFCNT_dec (cb); |
2208 | SvREFCNT_dec (cb); |
2045 | |
|
|
2046 | --count; |
|
|
2047 | } |
2209 | } |
2048 | } |
2210 | } |
2049 | |
2211 | |
2050 | static void |
2212 | static void |
2051 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
2213 | coro_semaphore_on_destroy (pTHX_ struct coro *coro) |
… | |
… | |
2133 | } |
2295 | } |
2134 | |
2296 | |
2135 | static void |
2297 | static void |
2136 | slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2298 | slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2137 | { |
2299 | { |
|
|
2300 | if (items >= 2) |
|
|
2301 | { |
|
|
2302 | /* callback form */ |
|
|
2303 | AV *av = (AV *)SvRV (arg [0]); |
|
|
2304 | CV *cb_cv = coro_sv_2cv (arg [1]); |
|
|
2305 | |
|
|
2306 | av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); |
|
|
2307 | |
|
|
2308 | if (SvIVX (AvARRAY (av)[0]) > 0) |
|
|
2309 | coro_semaphore_adjust (aTHX_ av, 0); |
|
|
2310 | |
|
|
2311 | frame->prepare = prepare_nop; |
|
|
2312 | frame->check = slf_check_nop; |
|
|
2313 | } |
|
|
2314 | else |
|
|
2315 | { |
2138 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
2316 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
2139 | frame->check = slf_check_semaphore_wait; |
2317 | frame->check = slf_check_semaphore_wait; |
|
|
2318 | } |
2140 | } |
2319 | } |
2141 | |
2320 | |
2142 | /*****************************************************************************/ |
2321 | /* signal */ |
2143 | /* gensub: simple closure generation utility */ |
|
|
2144 | |
2322 | |
2145 | #define GENSUB_ARG CvXSUBANY (cv).any_ptr |
2323 | static void |
2146 | |
2324 | 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 | { |
2325 | { |
2153 | CV *cv = (CV *)newSV (0); |
2326 | SvIVX (AvARRAY (av)[0]) = 0; |
2154 | |
2327 | |
2155 | sv_upgrade ((SV *)cv, SVt_PVCV); |
2328 | /* now signal count waiters */ |
|
|
2329 | while (count > 0 && AvFILLp (av) > 0) |
|
|
2330 | { |
|
|
2331 | SV *cb; |
2156 | |
2332 | |
2157 | CvANON_on (cv); |
2333 | /* swap first two elements so we can shift a waiter */ |
2158 | CvISXSUB_on (cv); |
2334 | cb = AvARRAY (av)[0]; |
2159 | CvXSUB (cv) = xsub; |
2335 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
2160 | GENSUB_ARG = arg; |
2336 | AvARRAY (av)[1] = cb; |
2161 | |
2337 | |
2162 | return newRV_noinc ((SV *)cv); |
2338 | cb = av_shift (av); |
|
|
2339 | |
|
|
2340 | api_ready (aTHX_ cb); |
|
|
2341 | sv_setiv (cb, 0); /* signal waiter */ |
|
|
2342 | SvREFCNT_dec (cb); |
|
|
2343 | |
|
|
2344 | --count; |
|
|
2345 | } |
|
|
2346 | } |
|
|
2347 | |
|
|
2348 | static int |
|
|
2349 | slf_check_signal_wait (pTHX_ struct CoroSLF *frame) |
|
|
2350 | { |
|
|
2351 | /* if we are about to throw, also stop waiting */ |
|
|
2352 | return SvROK ((SV *)frame->data) && !CORO_THROW; |
|
|
2353 | } |
|
|
2354 | |
|
|
2355 | static void |
|
|
2356 | slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2357 | { |
|
|
2358 | AV *av = (AV *)SvRV (arg [0]); |
|
|
2359 | |
|
|
2360 | if (SvIVX (AvARRAY (av)[0])) |
|
|
2361 | { |
|
|
2362 | SvIVX (AvARRAY (av)[0]) = 0; |
|
|
2363 | frame->prepare = prepare_nop; |
|
|
2364 | frame->check = slf_check_nop; |
|
|
2365 | } |
|
|
2366 | else |
|
|
2367 | { |
|
|
2368 | SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */ |
|
|
2369 | |
|
|
2370 | av_push (av, waiter); |
|
|
2371 | |
|
|
2372 | frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ |
|
|
2373 | frame->prepare = prepare_schedule; |
|
|
2374 | frame->check = slf_check_signal_wait; |
|
|
2375 | } |
2163 | } |
2376 | } |
2164 | |
2377 | |
2165 | /*****************************************************************************/ |
2378 | /*****************************************************************************/ |
2166 | /* Coro::AIO */ |
2379 | /* Coro::AIO */ |
2167 | |
2380 | |
… | |
… | |
2402 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2615 | assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); |
2403 | } |
2616 | } |
2404 | |
2617 | |
2405 | SV * |
2618 | SV * |
2406 | new (char *klass, ...) |
2619 | new (char *klass, ...) |
|
|
2620 | ALIAS: |
|
|
2621 | Coro::new = 1 |
2407 | CODE: |
2622 | CODE: |
2408 | { |
2623 | { |
2409 | struct coro *coro; |
2624 | struct coro *coro; |
2410 | MAGIC *mg; |
2625 | MAGIC *mg; |
2411 | HV *hv; |
2626 | HV *hv; |
|
|
2627 | CV *cb; |
2412 | int i; |
2628 | int i; |
|
|
2629 | |
|
|
2630 | if (items > 1) |
|
|
2631 | { |
|
|
2632 | cb = coro_sv_2cv (ST (1)); |
|
|
2633 | |
|
|
2634 | if (CvISXSUB (cb)) |
|
|
2635 | croak ("Coro::State doesn't support XS functions as coroutine start, caught"); |
|
|
2636 | |
|
|
2637 | if (!CvROOT (cb)) |
|
|
2638 | croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); |
|
|
2639 | } |
2413 | |
2640 | |
2414 | Newz (0, coro, 1, struct coro); |
2641 | Newz (0, coro, 1, struct coro); |
2415 | coro->args = newAV (); |
2642 | coro->args = newAV (); |
2416 | coro->flags = CF_NEW; |
2643 | coro->flags = CF_NEW; |
2417 | |
2644 | |
… | |
… | |
2422 | coro->hv = hv = newHV (); |
2649 | coro->hv = hv = newHV (); |
2423 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2650 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
2424 | mg->mg_flags |= MGf_DUP; |
2651 | mg->mg_flags |= MGf_DUP; |
2425 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2652 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
2426 | |
2653 | |
|
|
2654 | if (items > 1) |
|
|
2655 | { |
2427 | av_extend (coro->args, items - 1); |
2656 | av_extend (coro->args, items - 1 + ix); |
|
|
2657 | |
|
|
2658 | if (ix) |
|
|
2659 | { |
|
|
2660 | av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); |
|
|
2661 | cb = coro_run_cv; |
|
|
2662 | } |
|
|
2663 | |
|
|
2664 | coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); |
|
|
2665 | |
2428 | for (i = 1; i < items; i++) |
2666 | for (i = 2; i < items; i++) |
2429 | av_push (coro->args, newSVsv (ST (i))); |
2667 | av_push (coro->args, newSVsv (ST (i))); |
|
|
2668 | } |
2430 | } |
2669 | } |
2431 | OUTPUT: |
2670 | OUTPUT: |
2432 | RETVAL |
2671 | RETVAL |
2433 | |
2672 | |
2434 | void |
2673 | void |
… | |
… | |
2635 | int i; |
2874 | int i; |
2636 | |
2875 | |
2637 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
2876 | av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); |
2638 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2877 | sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); |
2639 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2878 | sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); |
2640 | |
2879 | coro_run_cv = get_cv (aTHX_ "Coro::_terminate", GV_ADD); |
2641 | coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE); |
2880 | coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); |
2642 | SvREADONLY_on (coro_current); |
2881 | SvREADONLY_on (coro_current); |
2643 | |
2882 | |
2644 | coro_stash = gv_stashpv ("Coro", TRUE); |
2883 | coro_stash = gv_stashpv ("Coro", TRUE); |
2645 | |
2884 | |
2646 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
2885 | newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); |
… | |
… | |
2808 | api_trace (aTHX_ coro_current, 0); |
3047 | api_trace (aTHX_ coro_current, 0); |
2809 | |
3048 | |
2810 | av_push (av_async_pool, newSVsv (coro_current)); |
3049 | av_push (av_async_pool, newSVsv (coro_current)); |
2811 | } |
3050 | } |
2812 | |
3051 | |
|
|
3052 | SV * |
|
|
3053 | rouse_cb () |
|
|
3054 | PROTOTYPE: |
|
|
3055 | CODE: |
|
|
3056 | RETVAL = coro_new_rouse_cb (aTHX); |
|
|
3057 | OUTPUT: |
|
|
3058 | RETVAL |
|
|
3059 | |
|
|
3060 | void |
|
|
3061 | rouse_wait (...) |
|
|
3062 | PROTOTYPE: ;$ |
|
|
3063 | PPCODE: |
|
|
3064 | CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); |
|
|
3065 | |
2813 | |
3066 | |
2814 | MODULE = Coro::State PACKAGE = PerlIO::cede |
3067 | MODULE = Coro::State PACKAGE = PerlIO::cede |
2815 | |
3068 | |
2816 | BOOT: |
3069 | BOOT: |
2817 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
3070 | PerlIO_define_layer (aTHX_ &PerlIO_cede); |
… | |
… | |
2821 | |
3074 | |
2822 | SV * |
3075 | SV * |
2823 | new (SV *klass, SV *count = 0) |
3076 | new (SV *klass, SV *count = 0) |
2824 | CODE: |
3077 | CODE: |
2825 | RETVAL = sv_bless ( |
3078 | RETVAL = sv_bless ( |
2826 | coro_semaphore_new (count && SvOK (count) ? SvIV (count) : 1), |
3079 | coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1), |
2827 | GvSTASH (CvGV (cv)) |
3080 | GvSTASH (CvGV (cv)) |
2828 | ); |
3081 | ); |
2829 | OUTPUT: |
3082 | OUTPUT: |
2830 | RETVAL |
3083 | RETVAL |
2831 | |
3084 | |
2832 | # helper for Coro::Channel |
3085 | # helper for Coro::Channel |
2833 | SV * |
3086 | SV * |
2834 | _alloc (int count) |
3087 | _alloc (int count) |
2835 | CODE: |
3088 | CODE: |
2836 | RETVAL = coro_semaphore_new (count); |
3089 | RETVAL = coro_waitarray_new (aTHX_ count); |
2837 | OUTPUT: |
3090 | OUTPUT: |
2838 | RETVAL |
3091 | RETVAL |
2839 | |
3092 | |
2840 | SV * |
3093 | SV * |
2841 | count (SV *self) |
3094 | count (SV *self) |
… | |
… | |
2850 | adjust = 1 |
3103 | adjust = 1 |
2851 | CODE: |
3104 | CODE: |
2852 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
3105 | coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); |
2853 | |
3106 | |
2854 | void |
3107 | void |
2855 | down (SV *self) |
3108 | down (...) |
2856 | ALIAS: |
|
|
2857 | Coro::Signal::wait = 0 |
|
|
2858 | CODE: |
3109 | CODE: |
2859 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
3110 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
2860 | |
3111 | |
2861 | void |
3112 | void |
2862 | wait (SV *self) |
3113 | wait (...) |
2863 | CODE: |
3114 | CODE: |
2864 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); |
3115 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); |
2865 | |
3116 | |
2866 | void |
3117 | void |
2867 | try (SV *self) |
3118 | try (SV *self) |
… | |
… | |
2903 | |
3154 | |
2904 | SV * |
3155 | SV * |
2905 | new (SV *klass) |
3156 | new (SV *klass) |
2906 | CODE: |
3157 | CODE: |
2907 | RETVAL = sv_bless ( |
3158 | RETVAL = sv_bless ( |
2908 | coro_semaphore_new (0), |
3159 | coro_waitarray_new (aTHX_ 0), |
2909 | GvSTASH (CvGV (cv)) |
3160 | GvSTASH (CvGV (cv)) |
2910 | ); |
3161 | ); |
2911 | OUTPUT: |
3162 | OUTPUT: |
2912 | RETVAL |
3163 | RETVAL |
2913 | |
3164 | |
2914 | void |
3165 | void |
2915 | broadcast (SV *self, int adjust = 1) |
3166 | wait (...) |
2916 | CODE: |
3167 | CODE: |
|
|
3168 | CORO_EXECUTE_SLF_XS (slf_init_signal_wait); |
|
|
3169 | |
|
|
3170 | void |
|
|
3171 | broadcast (SV *self) |
|
|
3172 | CODE: |
|
|
3173 | { |
|
|
3174 | AV *av = (AV *)SvRV (self); |
|
|
3175 | coro_signal_wake (aTHX_ av, AvFILLp (av)); |
|
|
3176 | } |
|
|
3177 | |
|
|
3178 | void |
|
|
3179 | send (SV *self) |
|
|
3180 | CODE: |
2917 | { |
3181 | { |
2918 | AV *av = (AV *)SvRV (self); |
3182 | AV *av = (AV *)SvRV (self); |
2919 | SvIVX (AvARRAY (av)[0]) = 0; /* not necessary, but gives me fuzzy warm feelings */ |
3183 | |
2920 | coro_semaphore_adjust (aTHX_ av, AvFILLp (av) + 1 - 1); |
3184 | if (AvFILLp (av)) |
|
|
3185 | coro_signal_wake (aTHX_ av, 1); |
|
|
3186 | else |
2921 | SvIVX (AvARRAY (av)[0]) = 0; /* necessary */ |
3187 | SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */ |
2922 | } |
3188 | } |
|
|
3189 | |
|
|
3190 | IV |
|
|
3191 | awaited (SV *self) |
|
|
3192 | CODE: |
|
|
3193 | RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1; |
|
|
3194 | OUTPUT: |
|
|
3195 | RETVAL |
2923 | |
3196 | |
2924 | |
3197 | |
2925 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
3198 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2926 | |
3199 | |
2927 | BOOT: |
3200 | BOOT: |
… | |
… | |
2955 | |
3228 | |
2956 | void |
3229 | void |
2957 | _register (char *target, char *proto, SV *req) |
3230 | _register (char *target, char *proto, SV *req) |
2958 | CODE: |
3231 | CODE: |
2959 | { |
3232 | { |
2960 | HV *st; |
|
|
2961 | GV *gvp; |
|
|
2962 | CV *req_cv = sv_2cv (req, &st, &gvp, 0); |
3233 | CV *req_cv = coro_sv_2cv (req); |
2963 | /* newXSproto doesn't return the CV on 5.8 */ |
3234 | /* newXSproto doesn't return the CV on 5.8 */ |
2964 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3235 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
2965 | sv_setpv ((SV *)slf_cv, proto); |
3236 | sv_setpv ((SV *)slf_cv, proto); |
2966 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3237 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
2967 | } |
3238 | } |