ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.300 by root, Wed Nov 19 02:07:48 2008 UTC vs.
Revision 1.313 by root, Thu Nov 20 03:22:59 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines