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.287 by root, Mon Nov 17 07:03:12 2008 UTC vs.
Revision 1.295 by root, Tue Nov 18 10:35:01 2008 UTC

95# define GV_NOTQUAL 0 95# define GV_NOTQUAL 0
96#endif 96#endif
97#ifndef newSV 97#ifndef newSV
98# define newSV(l) NEWSV(0,l) 98# define newSV(l) NEWSV(0,l)
99#endif 99#endif
100#ifndef CvISXSUB_on
101# define CvISXSUB_on(cv) (void)cv
102#endif
100 103
101/* 5.8.7 */ 104/* 5.8.7 */
102#ifndef SvRV_set 105#ifndef SvRV_set
103# define SvRV_set(s,v) SvRV(s) = (v) 106# define SvRV_set(s,v) SvRV(s) = (v)
104#endif 107#endif
117#endif 120#endif
118 121
119/* The next macros try to return the current stack pointer, in an as 122/* The next macros try to return the current stack pointer, in an as
120 * portable way as possible. */ 123 * portable way as possible. */
121#if __GNUC__ >= 4 124#if __GNUC__ >= 4
125# define dSTACKLEVEL int stacklevel_dummy
122# define dSTACKLEVEL void *stacklevel = __builtin_frame_address (0) 126# define STACKLEVEL __builtin_frame_address (0)
123#else 127#else
124# define dSTACKLEVEL volatile void *stacklevel = (volatile void *)&stacklevel 128# define dSTACKLEVEL volatile void *stacklevel
129# define STACKLEVEL ((void *)&stacklevel)
125#endif 130#endif
126 131
127#define IN_DESTRUCT (PL_main_cv == Nullcv) 132#define IN_DESTRUCT (PL_main_cv == Nullcv)
128 133
129#if __GNUC__ >= 3 134#if __GNUC__ >= 3
148static void *coro_thx; 153static void *coro_thx;
149# endif 154# endif
150#endif 155#endif
151 156
152static double (*nvtime)(); /* so why doesn't it take void? */ 157static double (*nvtime)(); /* so why doesn't it take void? */
158
159/* we hijack an hopefully unused CV flag for our purposes */
160#define CVf_SLF 0x4000
161static OP *pp_slf (pTHX);
153 162
154static U32 cctx_gen; 163static U32 cctx_gen;
155static size_t cctx_stacksize = CORO_STACKSIZE; 164static size_t cctx_stacksize = CORO_STACKSIZE;
156static struct CoroAPI coroapi; 165static struct CoroAPI coroapi;
157static AV *main_mainstack; /* used to differentiate between $main and others */ 166static AV *main_mainstack; /* used to differentiate between $main and others */
811slf_check_nop (pTHX_ struct CoroSLF *frame) 820slf_check_nop (pTHX_ struct CoroSLF *frame)
812{ 821{
813 return 0; 822 return 0;
814} 823}
815 824
825static UNOP coro_setup_op;
826
816static void NOINLINE /* noinline to keep it out of the transfer fast path */ 827static void NOINLINE /* noinline to keep it out of the transfer fast path */
817coro_setup (pTHX_ struct coro *coro) 828coro_setup (pTHX_ struct coro *coro)
818{ 829{
819 /* 830 /*
820 * emulate part of the perl startup here. 831 * emulate part of the perl startup here.
848 { 859 {
849 dSP; 860 dSP;
850 UNOP myop; 861 UNOP myop;
851 862
852 Zero (&myop, 1, UNOP); 863 Zero (&myop, 1, UNOP);
853 myop.op_next = Nullop; 864 myop.op_next = Nullop;
854 myop.op_flags = OPf_WANT_VOID; 865 myop.op_flags = OPf_WANT_VOID;
855 866
856 PUSHMARK (SP); 867 PUSHMARK (SP);
857 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 868 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
858 PUTBACK; 869 PUTBACK;
865 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here. 876 * likely was suspended in pp_slf, so we have to emulate entering pp_slf here.
866 */ 877 */
867 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */ 878 slf_frame.prepare = prepare_nop; /* provide a nop function for an eventual pp_slf */
868 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */ 879 slf_frame.check = slf_check_nop; /* signal pp_slf to not repeat */
869 880
881 /* 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;
883 coro_setup_op.op_type = OP_CUSTOM;
884 coro_setup_op.op_ppaddr = pp_slf;
885 /* no flags required, as an init function won't be called */
886
887 PL_op = (OP *)&coro_setup_op;
888
889 /* copy throw, in case it was set before coro_setup */
870 coro_throw = coro->throw; 890 coro_throw = coro->throw;
871} 891}
872 892
873static void 893static void
874coro_destruct (pTHX_ struct coro *coro) 894coro_destruct (pTHX_ struct coro *coro)
1038 1058
1039 TAINT_NOT; 1059 TAINT_NOT;
1040 return 0; 1060 return 0;
1041} 1061}
1042 1062
1063static struct coro_cctx *cctx_ssl_cctx;
1064static struct CoroSLF cctx_ssl_frame;
1065
1043static void 1066static void
1044prepare_set_stacklevel (struct coro_transfer_args *ta, struct coro_cctx *cctx) 1067slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1045{ 1068{
1046 ta->prev = (struct coro *)cctx; 1069 ta->prev = (struct coro *)cctx_ssl_cctx;
1047 ta->next = 0; 1070 ta->next = 0;
1048} 1071}
1049 1072
1050/* inject a fake call to Coro::State::_cctx_init into the execution */ 1073static int
1051/* _cctx_init should be careful, as it could be called at almost any time */ 1074slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1052/* during execution of a perl program */ 1075{
1053/* also initialises PL_top_env */ 1076 *frame = cctx_ssl_frame;
1077
1078 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1079}
1080
1081/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1054static void NOINLINE 1082static void NOINLINE
1055cctx_prepare (pTHX_ coro_cctx *cctx) 1083cctx_prepare (pTHX_ coro_cctx *cctx)
1056{ 1084{
1057 dSP;
1058 UNOP myop;
1059
1060 PL_top_env = &PL_start_env; 1085 PL_top_env = &PL_start_env;
1061 1086
1062 if (cctx->flags & CC_TRACE) 1087 if (cctx->flags & CC_TRACE)
1063 PL_runops = runops_trace; 1088 PL_runops = runops_trace;
1064 1089
1065 Zero (&myop, 1, UNOP); 1090 /* we already must be executing an SLF op, there is no other valid way
1066 myop.op_next = PL_op; 1091 * that can lead to creation of a new cctx */
1067 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 1092 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1093 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1068 1094
1069 PUSHMARK (SP); 1095 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */
1070 EXTEND (SP, 2); 1096 cctx_ssl_cctx = cctx;
1071 PUSHs (sv_2mortal (newSViv ((IV)cctx))); 1097 cctx_ssl_frame = slf_frame;
1072 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 1098
1073 PUTBACK; 1099 slf_frame.prepare = slf_prepare_set_stacklevel;
1074 PL_op = (OP *)&myop; 1100 slf_frame.check = slf_check_set_stacklevel;
1075 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1076 SPAGAIN;
1077} 1101}
1078 1102
1079/* the tail of transfer: execute stuff we can only do after a transfer */ 1103/* the tail of transfer: execute stuff we can only do after a transfer */
1080INLINE void 1104INLINE void
1081transfer_tail (pTHX) 1105transfer_tail (pTHX)
1297 dSTACKLEVEL; 1321 dSTACKLEVEL;
1298 1322
1299 /* sometimes transfer is only called to set idle_sp */ 1323 /* sometimes transfer is only called to set idle_sp */
1300 if (expect_false (!next)) 1324 if (expect_false (!next))
1301 { 1325 {
1302 ((coro_cctx *)prev)->idle_sp = (void *)stacklevel; 1326 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1303 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ 1327 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
1304 } 1328 }
1305 else if (expect_true (prev != next)) 1329 else if (expect_true (prev != next))
1306 { 1330 {
1307 coro_cctx *prev__cctx; 1331 coro_cctx *prev__cctx;
1332 1356
1333 prev__cctx = prev->cctx; 1357 prev__cctx = prev->cctx;
1334 1358
1335 /* possibly untie and reuse the cctx */ 1359 /* possibly untie and reuse the cctx */
1336 if (expect_true ( 1360 if (expect_true (
1337 prev__cctx->idle_sp == (void *)stacklevel 1361 prev__cctx->idle_sp == STACKLEVEL
1338 && !(prev__cctx->flags & CC_TRACE) 1362 && !(prev__cctx->flags & CC_TRACE)
1339 && !force_cctx 1363 && !force_cctx
1340 )) 1364 ))
1341 { 1365 {
1342 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1366 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1525 ENTER; 1549 ENTER;
1526 SAVETMPS; 1550 SAVETMPS;
1527 1551
1528 PUSHMARK (SP); 1552 PUSHMARK (SP);
1529 PUTBACK; 1553 PUTBACK;
1530 call_sv (sv_hook, G_DISCARD); 1554 call_sv (sv_hook, G_VOID | G_DISCARD);
1531 SPAGAIN;
1532 1555
1533 FREETMPS; 1556 FREETMPS;
1534 LEAVE; 1557 LEAVE;
1535 } 1558 }
1536 1559
1563 ENTER; 1586 ENTER;
1564 SAVETMPS; 1587 SAVETMPS;
1565 1588
1566 PUSHMARK (SP); 1589 PUSHMARK (SP);
1567 PUTBACK; 1590 PUTBACK;
1568 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); 1591 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD);
1569 SPAGAIN;
1570 1592
1571 FREETMPS; 1593 FREETMPS;
1572 LEAVE; 1594 LEAVE;
1573 continue; 1595 continue;
1574 } 1596 }
1714 1736
1715 RETURNOP (slf_restore.op_first); 1737 RETURNOP (slf_restore.op_first);
1716} 1738}
1717 1739
1718static void 1740static void
1719slf_prepare_set_stacklevel (pTHX_ struct coro_transfer_args *ta)
1720{
1721 prepare_set_stacklevel (ta, (struct coro_cctx *)slf_frame.data);
1722}
1723
1724static void
1725slf_init_set_stacklevel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1726{
1727 assert (("FATAL: set_stacklevel needs the coro cctx as sole argument", items == 1));
1728
1729 frame->prepare = slf_prepare_set_stacklevel;
1730 frame->check = slf_check_nop;
1731 frame->data = (void *)SvIV (arg [0]);
1732}
1733
1734static void
1735slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta) 1741slf_prepare_transfer (pTHX_ struct coro_transfer_args *ta)
1736{ 1742{
1737 SV **arg = (SV **)slf_frame.data; 1743 SV **arg = (SV **)slf_frame.data;
1738 1744
1739 prepare_transfer (aTHX_ ta, arg [0], arg [1]); 1745 prepare_transfer (aTHX_ ta, arg [0], arg [1]);
1768slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 1774slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
1769{ 1775{
1770 frame->prepare = prepare_cede_notself; 1776 frame->prepare = prepare_cede_notself;
1771 frame->check = slf_check_nop; 1777 frame->check = slf_check_nop;
1772} 1778}
1773
1774/* we hijack an hopefully unused CV flag for our purposes */
1775#define CVf_SLF 0x4000
1776 1779
1777/* 1780/*
1778 * these not obviously related functions are all rolled into one 1781 * these not obviously related functions are all rolled into one
1779 * function to increase chances that they all will call transfer with the same 1782 * function to increase chances that they all will call transfer with the same
1780 * stack offset 1783 * stack offset
1837 while (slf_frame.check (aTHX_ &slf_frame)); 1840 while (slf_frame.check (aTHX_ &slf_frame));
1838 1841
1839 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ 1842 slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */
1840 1843
1841 /* return value handling - mostly like entersub */ 1844 /* return value handling - mostly like entersub */
1842 {
1843 dSP;
1844 SV **bot = PL_stack_base + checkmark;
1845 int gimme = GIMME_V;
1846
1847 /* make sure we put something on the stack in scalar context */ 1845 /* make sure we put something on the stack in scalar context */
1848 if (gimme == G_SCALAR) 1846 if (GIMME_V == G_SCALAR)
1849 { 1847 {
1850 if (sp == bot) 1848 dSP;
1851 XPUSHs (&PL_sv_undef); 1849 SV **bot = PL_stack_base + checkmark;
1852 1850
1851 if (sp == bot) /* too few, push undef */
1852 bot [1] = &PL_sv_undef;
1853 else if (sp != bot + 1) /* too many, take last one */
1854 bot [1] = *sp;
1855
1853 SP = bot + 1; 1856 SP = bot + 1;
1854 }
1855 1857
1856 PUTBACK; 1858 PUTBACK;
1857 } 1859 }
1858 1860
1859 /* exception handling */ 1861 /* exception handling */
1860 if (expect_false (coro_throw)) 1862 if (expect_false (coro_throw))
1861 { 1863 {
1862 SV *exception = sv_2mortal (coro_throw); 1864 SV *exception = sv_2mortal (coro_throw);
2038slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) 2040slf_check_semaphore_down (pTHX_ struct CoroSLF *frame)
2039{ 2041{
2040 AV *av = (AV *)frame->data; 2042 AV *av = (AV *)frame->data;
2041 SV *count_sv = AvARRAY (av)[0]; 2043 SV *count_sv = AvARRAY (av)[0];
2042 2044
2045 /* if we are about to throw, don't actually acquire the lock, just throw */
2046 if (coro_throw && 0)//D
2047 return 0;
2043 if (SvIVX (count_sv) > 0) 2048 else if (SvIVX (count_sv) > 0)
2044 { 2049 {
2045 SvSTATE_current->on_destroy = 0; 2050 SvSTATE_current->on_destroy = 0;
2046 SvIVX (count_sv) = SvIVX (count_sv) - 1; 2051 SvIVX (count_sv) = SvIVX (count_sv) - 1;
2047 return 0; 2052 return 0;
2048 } 2053 }
2069 2074
2070 if (SvIVX (AvARRAY (av)[0]) > 0) 2075 if (SvIVX (AvARRAY (av)[0]) > 0)
2071 { 2076 {
2072 frame->data = (void *)av; 2077 frame->data = (void *)av;
2073 frame->prepare = prepare_nop; 2078 frame->prepare = prepare_nop;
2074 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2075 } 2079 }
2076 else 2080 else
2077 { 2081 {
2078 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2082 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2079 2083
2080 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); 2084 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av));
2081 frame->prepare = prepare_schedule; 2085 frame->prepare = prepare_schedule;
2082 2086
2083 /* to avoid race conditions when a woken-up coro gets terminated */ 2087 /* to avoid race conditions when a woken-up coro gets terminated */
2084 /* we arrange for a temporary on_destroy that calls adjust (0) */ 2088 /* we arrange for a temporary on_destroy that calls adjust (0) */
2085 assert (!SvSTATE_current->on_destroy);//D
2086 SvSTATE_current->on_destroy = coro_semaphore_on_destroy; 2089 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2087 } 2090 }
2088 2091
2089 frame->check = slf_check_semaphore_down; 2092 frame->check = slf_check_semaphore_down;
2090 2093
2247 2250
2248 for (i = 0; i < items; ++i) 2251 for (i = 0; i < items; ++i)
2249 PUSHs (arg [i]); 2252 PUSHs (arg [i]);
2250 2253
2251 /* now push the callback closure */ 2254 /* now push the callback closure */
2252 PUSHs (sv_2mortal (gensub (coro_aio_callback, (void *)SvREFCNT_inc_NN ((SV *)state)))); 2255 PUSHs (sv_2mortal (gensub (aTHX_ coro_aio_callback, (void *)SvREFCNT_inc_NN ((SV *)state))));
2253 2256
2254 /* now call the AIO function - we assume our request is uncancelable */ 2257 /* now call the AIO function - we assume our request is uncancelable */
2255 PUTBACK; 2258 PUTBACK;
2256 call_sv ((SV *)req, G_VOID | G_DISCARD); 2259 call_sv ((SV *)req, G_VOID | G_DISCARD);
2257 } 2260 }
2263} 2266}
2264 2267
2265static void 2268static void
2266coro_aio_req_xs (pTHX_ CV *cv) 2269coro_aio_req_xs (pTHX_ CV *cv)
2267{ 2270{
2268 dVAR;
2269 dXSARGS; 2271 dXSARGS;
2270 2272
2271 CORO_EXECUTE_SLF_XS (slf_init_aio_req); 2273 CORO_EXECUTE_SLF_XS (slf_init_aio_req);
2272 2274
2273 XSRETURN_EMPTY; 2275 XSRETURN_EMPTY;
2374 for (i = 1; i < items; i++) 2376 for (i = 1; i < items; i++)
2375 av_push (coro->args, newSVsv (ST (i))); 2377 av_push (coro->args, newSVsv (ST (i)));
2376} 2378}
2377 OUTPUT: 2379 OUTPUT:
2378 RETVAL 2380 RETVAL
2379
2380void
2381_set_stacklevel (...)
2382 CODE:
2383 CORO_EXECUTE_SLF_XS (slf_init_set_stacklevel);
2384 2381
2385void 2382void
2386transfer (...) 2383transfer (...)
2387 PROTOTYPE: $$ 2384 PROTOTYPE: $$
2388 CODE: 2385 CODE:
2774new (SV *klass, SV *count_ = 0) 2771new (SV *klass, SV *count_ = 0)
2775 CODE: 2772 CODE:
2776{ 2773{
2777 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ 2774 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */
2778 AV *av = newAV (); 2775 AV *av = newAV ();
2776 SV **ary;
2777
2778 /* unfortunately, building manually saves memory */
2779 Newx (ary, 2, SV *);
2780 AvALLOC (av) = ary;
2781 AvARRAY (av) = ary;
2782 AvMAX (av) = 1;
2783 AvFILLp (av) = 0;
2779 av_push (av, newSViv (count_ && SvOK (count_) ? SvIV (count_) : 1)); 2784 ary [0] = newSViv (count_ && SvOK (count_) ? SvIV (count_) : 1);
2785
2780 RETVAL = sv_bless (newRV_noinc ((SV *)av), GvSTASH (CvGV (cv))); 2786 RETVAL = sv_bless (newRV_noinc ((SV *)av), GvSTASH (CvGV (cv)));
2781} 2787}
2782 OUTPUT: 2788 OUTPUT:
2783 RETVAL 2789 RETVAL
2784 2790
2830 else 2836 else
2831 { 2837 {
2832 int i; 2838 int i;
2833 EXTEND (SP, AvFILLp (av) + 1 - 1); 2839 EXTEND (SP, AvFILLp (av) + 1 - 1);
2834 for (i = 1; i <= AvFILLp (av); ++i) 2840 for (i = 1; i <= AvFILLp (av); ++i)
2835 PUSHs (newSVsv (AvARRAY (av)[i])); 2841 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
2836 } 2842 }
2837} 2843}
2838 2844
2839 2845
2840MODULE = Coro::State PACKAGE = Coro::AnyEvent 2846MODULE = Coro::State PACKAGE = Coro::AnyEvent
2857 sv_setsv (sv_activity, &PL_sv_undef); 2863 sv_setsv (sv_activity, &PL_sv_undef);
2858 if (coro_nready >= incede) 2864 if (coro_nready >= incede)
2859 { 2865 {
2860 PUSHMARK (SP); 2866 PUSHMARK (SP);
2861 PUTBACK; 2867 PUTBACK;
2862 call_pv ("Coro::AnyEvent::_activity", G_DISCARD | G_EVAL); 2868 call_pv ("Coro::AnyEvent::_activity", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2863 SPAGAIN;
2864 } 2869 }
2865 2870
2866 --incede; 2871 --incede;
2867} 2872}
2868 2873
2874 CODE: 2879 CODE:
2875{ 2880{
2876 HV *st; 2881 HV *st;
2877 GV *gvp; 2882 GV *gvp;
2878 CV *req_cv = sv_2cv (req, &st, &gvp, 0); 2883 CV *req_cv = sv_2cv (req, &st, &gvp, 0);
2884 /* newXSproto doesn't return the CV on 5.8 */
2879 CV *slf_cv = newXSproto (target, coro_aio_req_xs, __FILE__, proto); 2885 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
2886 sv_setpv ((SV *)slf_cv, proto);
2880 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 2887 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
2881} 2888}
2882 2889

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines