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.293 by root, Tue Nov 18 06:10:03 2008 UTC vs.
Revision 1.298 by root, Tue Nov 18 23:20:41 2008 UTC

120#endif 120#endif
121 121
122/* 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
123 * portable way as possible. */ 123 * portable way as possible. */
124#if __GNUC__ >= 4 124#if __GNUC__ >= 4
125# define dSTACKLEVEL int stacklevel_dummy
125# define dSTACKLEVEL void *stacklevel = __builtin_frame_address (0) 126# define STACKLEVEL __builtin_frame_address (0)
126#else 127#else
127# define dSTACKLEVEL volatile void *stacklevel = (volatile void *)&stacklevel 128# define dSTACKLEVEL volatile void *stacklevel
129# define STACKLEVEL ((void *)&stacklevel)
128#endif 130#endif
129 131
130#define IN_DESTRUCT (PL_main_cv == Nullcv) 132#define IN_DESTRUCT (PL_main_cv == Nullcv)
131 133
132#if __GNUC__ >= 3 134#if __GNUC__ >= 3
143#define expect_true(expr) expect ((expr) != 0, 1) 145#define expect_true(expr) expect ((expr) != 0, 1)
144 146
145#define NOINLINE attribute ((noinline)) 147#define NOINLINE attribute ((noinline))
146 148
147#include "CoroAPI.h" 149#include "CoroAPI.h"
150#define GCoroAPI (&coroapi) /* very sneaky */
148 151
149#ifdef USE_ITHREADS 152#ifdef USE_ITHREADS
150# if CORO_PTHREAD 153# if CORO_PTHREAD
151static void *coro_thx; 154static void *coro_thx;
152# endif 155# endif
254 /* statistics */ 257 /* statistics */
255 int usecount; /* number of transfers to this coro */ 258 int usecount; /* number of transfers to this coro */
256 259
257 /* coro process data */ 260 /* coro process data */
258 int prio; 261 int prio;
259 SV *throw; /* exception to be thrown */ 262 SV *except; /* exception to be thrown */
260 263
261 /* async_pool */ 264 /* async_pool */
262 SV *saved_deffh; 265 SV *saved_deffh;
263 266
264 /* linked list */ 267 /* linked list */
270 273
271/* the following variables are effectively part of the perl context */ 274/* the following variables are effectively part of the perl context */
272/* and get copied between struct coro and these variables */ 275/* and get copied between struct coro and these variables */
273/* the mainr easonw e don't support windows process emulation */ 276/* the mainr easonw e don't support windows process emulation */
274static struct CoroSLF slf_frame; /* the current slf frame */ 277static struct CoroSLF slf_frame; /* the current slf frame */
275static SV *coro_throw;
276 278
277/** Coro ********************************************************************/ 279/** Coro ********************************************************************/
278 280
279#define PRIO_MAX 3 281#define PRIO_MAX 3
280#define PRIO_HIGH 1 282#define PRIO_HIGH 1
509 511
510 PUTBACK; 512 PUTBACK;
511 } 513 }
512 514
513 slf_frame = c->slf_frame; 515 slf_frame = c->slf_frame;
514 coro_throw = c->throw; 516 CORO_THROW = c->except;
515} 517}
516 518
517static void 519static void
518save_perl (pTHX_ Coro__State c) 520save_perl (pTHX_ Coro__State c)
519{ 521{
520 c->throw = coro_throw; 522 c->except = CORO_THROW;
521 c->slf_frame = slf_frame; 523 c->slf_frame = slf_frame;
522 524
523 { 525 {
524 dSP; 526 dSP;
525 I32 cxix = cxstack_ix; 527 I32 cxix = cxstack_ix;
857 { 859 {
858 dSP; 860 dSP;
859 UNOP myop; 861 UNOP myop;
860 862
861 Zero (&myop, 1, UNOP); 863 Zero (&myop, 1, UNOP);
862 myop.op_next = Nullop; 864 myop.op_next = Nullop;
863 myop.op_flags = OPf_WANT_VOID; 865 myop.op_flags = OPf_WANT_VOID;
864 866
865 PUSHMARK (SP); 867 PUSHMARK (SP);
866 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 868 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
867 PUTBACK; 869 PUTBACK;
875 */ 877 */
876 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 */
877 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 */
878 880
879 /* and we have to provide the pp_slf op in any case, so pp_slf can skip it */ 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;
880 coro_setup_op.op_type = OP_CUSTOM; 883 coro_setup_op.op_type = OP_CUSTOM;
881 coro_setup_op.op_ppaddr = pp_slf; 884 coro_setup_op.op_ppaddr = pp_slf;
882 coro_setup_op.op_next = PL_op; 885 /* no flags required, as an init function won't be called */
883 886
884 PL_op = (OP *)&coro_setup_op; 887 PL_op = (OP *)&coro_setup_op;
885 888
886 /* copy throw, in case it was set before coro_setup */ 889 /* copy throw, in case it was set before coro_setup */
887 coro_throw = coro->throw; 890 CORO_THROW = coro->except;
888} 891}
889 892
890static void 893static void
891coro_destruct (pTHX_ struct coro *coro) 894coro_destruct (pTHX_ struct coro *coro)
892{ 895{
916 919
917 SvREFCNT_dec (PL_diehook); 920 SvREFCNT_dec (PL_diehook);
918 SvREFCNT_dec (PL_warnhook); 921 SvREFCNT_dec (PL_warnhook);
919 922
920 SvREFCNT_dec (coro->saved_deffh); 923 SvREFCNT_dec (coro->saved_deffh);
921 SvREFCNT_dec (coro_throw); 924 SvREFCNT_dec (CORO_THROW);
922 925
923 coro_destruct_stacks (aTHX); 926 coro_destruct_stacks (aTHX);
924} 927}
925 928
926INLINE void 929INLINE void
1070static int 1073static int
1071slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) 1074slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame)
1072{ 1075{
1073 *frame = cctx_ssl_frame; 1076 *frame = cctx_ssl_frame;
1074 1077
1075 return 1; /* execute the restored frame - there must be one */ 1078 return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */
1076} 1079}
1077 1080
1078/* initialises PL_top_env and injects a pseudo-slf-call to sett he stacklevel */ 1081/* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */
1079static void NOINLINE 1082static void NOINLINE
1080cctx_prepare (pTHX_ coro_cctx *cctx) 1083cctx_prepare (pTHX_ coro_cctx *cctx)
1081{ 1084{
1082 PL_top_env = &PL_start_env; 1085 PL_top_env = &PL_start_env;
1083 1086
1087 /* we already must be executing an SLF op, there is no other valid way 1090 /* we already must be executing an SLF op, there is no other valid way
1088 * that can lead to creation of a new cctx */ 1091 * that can lead to creation of a new cctx */
1089 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)", 1092 assert (("FATAL: can't prepare slf-less cctx in Coro module (please report)",
1090 slf_frame.prepare && PL_op->op_ppaddr == pp_slf)); 1093 slf_frame.prepare && PL_op->op_ppaddr == pp_slf));
1091 1094
1095 /* we must emulate leaving pp_slf, which is done inside slf_check_set_stacklevel */
1092 cctx_ssl_cctx = cctx; 1096 cctx_ssl_cctx = cctx;
1093 cctx_ssl_frame = slf_frame; 1097 cctx_ssl_frame = slf_frame;
1094 1098
1095 slf_frame.prepare = slf_prepare_set_stacklevel; 1099 slf_frame.prepare = slf_prepare_set_stacklevel;
1096 slf_frame.check = slf_check_set_stacklevel; 1100 slf_frame.check = slf_check_set_stacklevel;
1317 dSTACKLEVEL; 1321 dSTACKLEVEL;
1318 1322
1319 /* sometimes transfer is only called to set idle_sp */ 1323 /* sometimes transfer is only called to set idle_sp */
1320 if (expect_false (!next)) 1324 if (expect_false (!next))
1321 { 1325 {
1322 ((coro_cctx *)prev)->idle_sp = (void *)stacklevel; 1326 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1323 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 */
1324 } 1328 }
1325 else if (expect_true (prev != next)) 1329 else if (expect_true (prev != next))
1326 { 1330 {
1327 coro_cctx *prev__cctx; 1331 coro_cctx *prev__cctx;
1352 1356
1353 prev__cctx = prev->cctx; 1357 prev__cctx = prev->cctx;
1354 1358
1355 /* possibly untie and reuse the cctx */ 1359 /* possibly untie and reuse the cctx */
1356 if (expect_true ( 1360 if (expect_true (
1357 prev__cctx->idle_sp == (void *)stacklevel 1361 prev__cctx->idle_sp == STACKLEVEL
1358 && !(prev__cctx->flags & CC_TRACE) 1362 && !(prev__cctx->flags & CC_TRACE)
1359 && !force_cctx 1363 && !force_cctx
1360 )) 1364 ))
1361 { 1365 {
1362 /* 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 */
1835 } 1839 }
1836 while (slf_frame.check (aTHX_ &slf_frame)); 1840 while (slf_frame.check (aTHX_ &slf_frame));
1837 1841
1838 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 */
1839 1843
1844 /* exception handling */
1845 if (expect_false (CORO_THROW))
1846 {
1847 SV *exception = sv_2mortal (CORO_THROW);
1848
1849 CORO_THROW = 0;
1850 sv_setsv (ERRSV, exception);
1851 croak (0);
1852 }
1853
1840 /* return value handling - mostly like entersub */ 1854 /* return value handling - mostly like entersub */
1841 /* make sure we put something on the stack in scalar context */ 1855 /* make sure we put something on the stack in scalar context */
1842 if (GIMME_V == G_SCALAR) 1856 if (GIMME_V == G_SCALAR)
1843 { 1857 {
1844 dSP; 1858 dSP;
1850 bot [1] = *sp; 1864 bot [1] = *sp;
1851 1865
1852 SP = bot + 1; 1866 SP = bot + 1;
1853 1867
1854 PUTBACK; 1868 PUTBACK;
1855 }
1856
1857 /* exception handling */
1858 if (expect_false (coro_throw))
1859 {
1860 SV *exception = sv_2mortal (coro_throw);
1861
1862 coro_throw = 0;
1863 sv_setsv (ERRSV, exception);
1864 croak (0);
1865 } 1869 }
1866 1870
1867 return NORMAL; 1871 return NORMAL;
1868} 1872}
1869 1873
2031 /* call $sem->adjust (0) to possibly wake up some other waiters */ 2035 /* call $sem->adjust (0) to possibly wake up some other waiters */
2032 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); 2036 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0);
2033} 2037}
2034 2038
2035static int 2039static int
2036slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) 2040slf_check_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, int acquire)
2037{ 2041{
2038 AV *av = (AV *)frame->data; 2042 AV *av = (AV *)frame->data;
2039 SV *count_sv = AvARRAY (av)[0]; 2043 SV *count_sv = AvARRAY (av)[0];
2040 2044
2045 /* if we are about to throw, don't actually acquire the lock, just throw */
2046 if (CORO_THROW)
2047 return 0;
2041 if (SvIVX (count_sv) > 0) 2048 else if (SvIVX (count_sv) > 0)
2042 { 2049 {
2043 SvSTATE_current->on_destroy = 0; 2050 SvSTATE_current->on_destroy = 0;
2051
2052 if (acquire)
2044 SvIVX (count_sv) = SvIVX (count_sv) - 1; 2053 SvIVX (count_sv) = SvIVX (count_sv) - 1;
2054 else
2055 coro_semaphore_adjust (aTHX_ av, 0);
2056
2045 return 0; 2057 return 0;
2046 } 2058 }
2047 else 2059 else
2048 { 2060 {
2049 int i; 2061 int i;
2058 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2070 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2059 return 1; 2071 return 1;
2060 } 2072 }
2061} 2073}
2062 2074
2063static void 2075static int
2076slf_check_semaphore_down (pTHX_ struct CoroSLF *frame)
2077{
2078 return slf_check_semaphore_down_or_wait (aTHX_ frame, 1);
2079}
2080
2081static int
2082slf_check_semaphore_wait (pTHX_ struct CoroSLF *frame)
2083{
2084 return slf_check_semaphore_down_or_wait (aTHX_ frame, 0);
2085}
2086
2087static void
2064slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2088slf_init_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2065{ 2089{
2066 AV *av = (AV *)SvRV (arg [0]); 2090 AV *av = (AV *)SvRV (arg [0]);
2067 2091
2068 if (SvIVX (AvARRAY (av)[0]) > 0) 2092 if (SvIVX (AvARRAY (av)[0]) > 0)
2069 { 2093 {
2070 frame->data = (void *)av; 2094 frame->data = (void *)av;
2071 frame->prepare = prepare_nop; 2095 frame->prepare = prepare_nop;
2072 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2073 } 2096 }
2074 else 2097 else
2075 { 2098 {
2076 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2099 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2077 2100
2078 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); 2101 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av));
2079 frame->prepare = prepare_schedule; 2102 frame->prepare = prepare_schedule;
2080 2103
2081 /* to avoid race conditions when a woken-up coro gets terminated */ 2104 /* to avoid race conditions when a woken-up coro gets terminated */
2082 /* we arrange for a temporary on_destroy that calls adjust (0) */ 2105 /* we arrange for a temporary on_destroy that calls adjust (0) */
2083 assert (!SvSTATE_current->on_destroy);//D
2084 SvSTATE_current->on_destroy = coro_semaphore_on_destroy; 2106 SvSTATE_current->on_destroy = coro_semaphore_on_destroy;
2085 } 2107 }
2108}
2086 2109
2110static void
2111slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2112{
2113 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items);
2087 frame->check = slf_check_semaphore_down; 2114 frame->check = slf_check_semaphore_down;
2115}
2088 2116
2117static void
2118slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2119{
2120 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items);
2121 frame->check = slf_check_semaphore_wait;
2089} 2122}
2090 2123
2091/*****************************************************************************/ 2124/*****************************************************************************/
2092/* gensub: simple closure generation utility */ 2125/* gensub: simple closure generation utility */
2093 2126
2165 2198
2166static int 2199static int
2167slf_check_aio_req (pTHX_ struct CoroSLF *frame) 2200slf_check_aio_req (pTHX_ struct CoroSLF *frame)
2168{ 2201{
2169 AV *state = (AV *)frame->data; 2202 AV *state = (AV *)frame->data;
2203
2204 /* if we are about to throw, return early */
2205 /* this does not cancel the aio request, but at least */
2206 /* it quickly returns */
2207 if (CORO_THROW)
2208 return 0;
2170 2209
2171 /* one element that is an RV? repeat! */ 2210 /* one element that is an RV? repeat! */
2172 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) 2211 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0]))
2173 return 1; 2212 return 1;
2174 2213
2506throw (Coro::State self, SV *throw = &PL_sv_undef) 2545throw (Coro::State self, SV *throw = &PL_sv_undef)
2507 PROTOTYPE: $;$ 2546 PROTOTYPE: $;$
2508 CODE: 2547 CODE:
2509{ 2548{
2510 struct coro *current = SvSTATE_current; 2549 struct coro *current = SvSTATE_current;
2511 SV **throwp = self == current ? &coro_throw : &self->throw; 2550 SV **throwp = self == current ? &CORO_THROW : &self->except;
2512 SvREFCNT_dec (*throwp); 2551 SvREFCNT_dec (*throwp);
2513 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 2552 *throwp = SvOK (throw) ? newSVsv (throw) : 0;
2514} 2553}
2515 2554
2516void 2555void
2605 coroapi.ready = api_ready; 2644 coroapi.ready = api_ready;
2606 coroapi.is_ready = api_is_ready; 2645 coroapi.is_ready = api_is_ready;
2607 coroapi.nready = coro_nready; 2646 coroapi.nready = coro_nready;
2608 coroapi.current = coro_current; 2647 coroapi.current = coro_current;
2609 2648
2610 GCoroAPI = &coroapi; 2649 /*GCoroAPI = &coroapi;*/
2611 sv_setiv (sv, (IV)&coroapi); 2650 sv_setiv (sv, (IV)&coroapi);
2612 SvREADONLY_on (sv); 2651 SvREADONLY_on (sv);
2613 } 2652 }
2614} 2653}
2615 2654
2799 2838
2800void 2839void
2801down (SV *self) 2840down (SV *self)
2802 CODE: 2841 CODE:
2803 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); 2842 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down);
2843
2844void
2845wait (SV *self)
2846 CODE:
2847 CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait);
2804 2848
2805void 2849void
2806try (SV *self) 2850try (SV *self)
2807 PPCODE: 2851 PPCODE:
2808{ 2852{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines