… | |
… | |
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 |
151 | static void *coro_thx; |
154 | static 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 */ |
274 | static struct CoroSLF slf_frame; /* the current slf frame */ |
277 | static struct CoroSLF slf_frame; /* the current slf frame */ |
275 | static 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 | |
517 | static void |
519 | static void |
518 | save_perl (pTHX_ Coro__State c) |
520 | save_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 | |
890 | static void |
893 | static void |
891 | coro_destruct (pTHX_ struct coro *coro) |
894 | coro_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 | |
926 | INLINE void |
929 | INLINE void |
… | |
… | |
1070 | static int |
1073 | static int |
1071 | slf_check_set_stacklevel (pTHX_ struct CoroSLF *frame) |
1074 | slf_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 */ |
1079 | static void NOINLINE |
1082 | static void NOINLINE |
1080 | cctx_prepare (pTHX_ coro_cctx *cctx) |
1083 | cctx_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 | |
… | |
… | |
1991 | PerlIOBuf_get_cnt, |
1995 | PerlIOBuf_get_cnt, |
1992 | PerlIOBuf_set_ptrcnt, |
1996 | PerlIOBuf_set_ptrcnt, |
1993 | }; |
1997 | }; |
1994 | |
1998 | |
1995 | /*****************************************************************************/ |
1999 | /*****************************************************************************/ |
|
|
2000 | /* Coro::Semaphore & Coro::Signal */ |
|
|
2001 | |
|
|
2002 | static SV * |
|
|
2003 | coro_waitarray_new (pTHX_ int count) |
|
|
2004 | { |
|
|
2005 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
|
|
2006 | AV *av = newAV (); |
|
|
2007 | SV **ary; |
|
|
2008 | |
|
|
2009 | /* unfortunately, building manually saves memory */ |
|
|
2010 | Newx (ary, 2, SV *); |
|
|
2011 | AvALLOC (av) = ary; |
|
|
2012 | AvARRAY (av) = ary; |
|
|
2013 | AvMAX (av) = 1; |
|
|
2014 | AvFILLp (av) = 0; |
|
|
2015 | ary [0] = newSViv (count); |
|
|
2016 | |
|
|
2017 | return newRV_noinc ((SV *)av); |
|
|
2018 | } |
|
|
2019 | |
1996 | /* Coro::Semaphore */ |
2020 | /* semaphore */ |
1997 | |
2021 | |
1998 | static void |
2022 | static void |
1999 | coro_semaphore_adjust (pTHX_ AV *av, IV adjust) |
2023 | coro_semaphore_adjust (pTHX_ AV *av, IV adjust) |
2000 | { |
2024 | { |
2001 | SV *count_sv = AvARRAY (av)[0]; |
2025 | SV *count_sv = AvARRAY (av)[0]; |
… | |
… | |
2031 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
2055 | /* call $sem->adjust (0) to possibly wake up some other waiters */ |
2032 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
2056 | coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); |
2033 | } |
2057 | } |
2034 | |
2058 | |
2035 | static int |
2059 | static int |
2036 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
2060 | slf_check_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, int acquire) |
2037 | { |
2061 | { |
2038 | AV *av = (AV *)frame->data; |
2062 | AV *av = (AV *)frame->data; |
2039 | SV *count_sv = AvARRAY (av)[0]; |
2063 | SV *count_sv = AvARRAY (av)[0]; |
2040 | |
2064 | |
|
|
2065 | /* if we are about to throw, don't actually acquire the lock, just throw */ |
|
|
2066 | if (CORO_THROW) |
|
|
2067 | return 0; |
2041 | if (SvIVX (count_sv) > 0) |
2068 | else if (SvIVX (count_sv) > 0) |
2042 | { |
2069 | { |
2043 | SvSTATE_current->on_destroy = 0; |
2070 | SvSTATE_current->on_destroy = 0; |
|
|
2071 | |
|
|
2072 | if (acquire) |
2044 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
2073 | SvIVX (count_sv) = SvIVX (count_sv) - 1; |
|
|
2074 | else |
|
|
2075 | coro_semaphore_adjust (aTHX_ av, 0); |
|
|
2076 | |
2045 | return 0; |
2077 | return 0; |
2046 | } |
2078 | } |
2047 | else |
2079 | else |
2048 | { |
2080 | { |
2049 | int i; |
2081 | int i; |
… | |
… | |
2058 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2090 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2059 | return 1; |
2091 | return 1; |
2060 | } |
2092 | } |
2061 | } |
2093 | } |
2062 | |
2094 | |
2063 | static void |
2095 | static int |
|
|
2096 | slf_check_semaphore_down (pTHX_ struct CoroSLF *frame) |
|
|
2097 | { |
|
|
2098 | return slf_check_semaphore_down_or_wait (aTHX_ frame, 1); |
|
|
2099 | } |
|
|
2100 | |
|
|
2101 | static int |
|
|
2102 | slf_check_semaphore_wait (pTHX_ struct CoroSLF *frame) |
|
|
2103 | { |
|
|
2104 | return slf_check_semaphore_down_or_wait (aTHX_ frame, 0); |
|
|
2105 | } |
|
|
2106 | |
|
|
2107 | static void |
2064 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2108 | slf_init_semaphore_down_or_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2065 | { |
2109 | { |
2066 | AV *av = (AV *)SvRV (arg [0]); |
2110 | AV *av = (AV *)SvRV (arg [0]); |
2067 | |
2111 | |
2068 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2112 | if (SvIVX (AvARRAY (av)[0]) > 0) |
2069 | { |
2113 | { |
2070 | frame->data = (void *)av; |
2114 | frame->data = (void *)av; |
2071 | frame->prepare = prepare_nop; |
2115 | frame->prepare = prepare_nop; |
2072 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
|
|
2073 | } |
2116 | } |
2074 | else |
2117 | else |
2075 | { |
2118 | { |
2076 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2119 | av_push (av, SvREFCNT_inc (SvRV (coro_current))); |
2077 | |
2120 | |
2078 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2121 | frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); |
2079 | frame->prepare = prepare_schedule; |
2122 | frame->prepare = prepare_schedule; |
2080 | |
2123 | |
2081 | /* to avoid race conditions when a woken-up coro gets terminated */ |
2124 | /* to avoid race conditions when a woken-up coro gets terminated */ |
2082 | /* we arrange for a temporary on_destroy that calls adjust (0) */ |
2125 | /* 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; |
2126 | SvSTATE_current->on_destroy = coro_semaphore_on_destroy; |
2085 | } |
2127 | } |
|
|
2128 | } |
2086 | |
2129 | |
|
|
2130 | static void |
|
|
2131 | slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2132 | { |
|
|
2133 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
2087 | frame->check = slf_check_semaphore_down; |
2134 | frame->check = slf_check_semaphore_down; |
|
|
2135 | } |
2088 | |
2136 | |
|
|
2137 | static void |
|
|
2138 | slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2139 | { |
|
|
2140 | slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); |
|
|
2141 | frame->check = slf_check_semaphore_wait; |
|
|
2142 | } |
|
|
2143 | |
|
|
2144 | /* signal */ |
|
|
2145 | |
|
|
2146 | static void |
|
|
2147 | coro_signal_wake (pTHX_ AV *av, int count) |
|
|
2148 | { |
|
|
2149 | SvIVX (AvARRAY (av)[0]) = 0; |
|
|
2150 | |
|
|
2151 | /* now signal count waiters */ |
|
|
2152 | while (count > 0 && AvFILLp (av) > 0) |
|
|
2153 | { |
|
|
2154 | SV *cb; |
|
|
2155 | |
|
|
2156 | /* swap first two elements so we can shift a waiter */ |
|
|
2157 | cb = AvARRAY (av)[0]; |
|
|
2158 | AvARRAY (av)[0] = AvARRAY (av)[1]; |
|
|
2159 | AvARRAY (av)[1] = cb; |
|
|
2160 | |
|
|
2161 | cb = av_shift (av); |
|
|
2162 | |
|
|
2163 | api_ready (aTHX_ cb); |
|
|
2164 | sv_setiv (cb, 0); /* signal waiter */ |
|
|
2165 | SvREFCNT_dec (cb); |
|
|
2166 | |
|
|
2167 | --count; |
|
|
2168 | } |
|
|
2169 | } |
|
|
2170 | |
|
|
2171 | static int |
|
|
2172 | slf_check_signal_wait (pTHX_ struct CoroSLF *frame) |
|
|
2173 | { |
|
|
2174 | /* if we are about to throw, also stop waiting */ |
|
|
2175 | return SvROK ((SV *)frame->data) && !CORO_THROW; |
|
|
2176 | } |
|
|
2177 | |
|
|
2178 | static void |
|
|
2179 | slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
|
|
2180 | { |
|
|
2181 | AV *av = (AV *)SvRV (arg [0]); |
|
|
2182 | |
|
|
2183 | if (SvIVX (AvARRAY (av)[0])) |
|
|
2184 | { |
|
|
2185 | SvIVX (AvARRAY (av)[0]) = 0; |
|
|
2186 | frame->prepare = prepare_nop; |
|
|
2187 | frame->check = slf_check_nop; |
|
|
2188 | } |
|
|
2189 | else |
|
|
2190 | { |
|
|
2191 | SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */ |
|
|
2192 | |
|
|
2193 | av_push (av, waiter); |
|
|
2194 | |
|
|
2195 | frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ |
|
|
2196 | frame->prepare = prepare_schedule; |
|
|
2197 | frame->check = slf_check_signal_wait; |
|
|
2198 | } |
2089 | } |
2199 | } |
2090 | |
2200 | |
2091 | /*****************************************************************************/ |
2201 | /*****************************************************************************/ |
2092 | /* gensub: simple closure generation utility */ |
2202 | /* gensub: simple closure generation utility */ |
2093 | |
2203 | |
… | |
… | |
2165 | |
2275 | |
2166 | static int |
2276 | static int |
2167 | slf_check_aio_req (pTHX_ struct CoroSLF *frame) |
2277 | slf_check_aio_req (pTHX_ struct CoroSLF *frame) |
2168 | { |
2278 | { |
2169 | AV *state = (AV *)frame->data; |
2279 | AV *state = (AV *)frame->data; |
|
|
2280 | |
|
|
2281 | /* if we are about to throw, return early */ |
|
|
2282 | /* this does not cancel the aio request, but at least */ |
|
|
2283 | /* it quickly returns */ |
|
|
2284 | if (CORO_THROW) |
|
|
2285 | return 0; |
2170 | |
2286 | |
2171 | /* one element that is an RV? repeat! */ |
2287 | /* one element that is an RV? repeat! */ |
2172 | if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) |
2288 | if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) |
2173 | return 1; |
2289 | return 1; |
2174 | |
2290 | |
… | |
… | |
2506 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
2622 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
2507 | PROTOTYPE: $;$ |
2623 | PROTOTYPE: $;$ |
2508 | CODE: |
2624 | CODE: |
2509 | { |
2625 | { |
2510 | struct coro *current = SvSTATE_current; |
2626 | struct coro *current = SvSTATE_current; |
2511 | SV **throwp = self == current ? &coro_throw : &self->throw; |
2627 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
2512 | SvREFCNT_dec (*throwp); |
2628 | SvREFCNT_dec (*throwp); |
2513 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
2629 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
2514 | } |
2630 | } |
2515 | |
2631 | |
2516 | void |
2632 | void |
… | |
… | |
2605 | coroapi.ready = api_ready; |
2721 | coroapi.ready = api_ready; |
2606 | coroapi.is_ready = api_is_ready; |
2722 | coroapi.is_ready = api_is_ready; |
2607 | coroapi.nready = coro_nready; |
2723 | coroapi.nready = coro_nready; |
2608 | coroapi.current = coro_current; |
2724 | coroapi.current = coro_current; |
2609 | |
2725 | |
2610 | GCoroAPI = &coroapi; |
2726 | /*GCoroAPI = &coroapi;*/ |
2611 | sv_setiv (sv, (IV)&coroapi); |
2727 | sv_setiv (sv, (IV)&coroapi); |
2612 | SvREADONLY_on (sv); |
2728 | SvREADONLY_on (sv); |
2613 | } |
2729 | } |
2614 | } |
2730 | } |
2615 | |
2731 | |
… | |
… | |
2761 | |
2877 | |
2762 | |
2878 | |
2763 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
2879 | MODULE = Coro::State PACKAGE = Coro::Semaphore |
2764 | |
2880 | |
2765 | SV * |
2881 | SV * |
2766 | new (SV *klass, SV *count_ = 0) |
2882 | new (SV *klass, SV *count = 0) |
2767 | CODE: |
2883 | CODE: |
2768 | { |
2884 | RETVAL = sv_bless ( |
2769 | /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ |
2885 | coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1), |
2770 | AV *av = newAV (); |
2886 | GvSTASH (CvGV (cv)) |
2771 | SV **ary; |
2887 | ); |
|
|
2888 | OUTPUT: |
|
|
2889 | RETVAL |
2772 | |
2890 | |
2773 | /* unfortunately, building manually saves memory */ |
2891 | # helper for Coro::Channel |
2774 | Newx (ary, 2, SV *); |
2892 | SV * |
2775 | AvALLOC (av) = ary; |
2893 | _alloc (int count) |
2776 | AvARRAY (av) = ary; |
2894 | CODE: |
2777 | AvMAX (av) = 1; |
2895 | RETVAL = coro_waitarray_new (aTHX_ count); |
2778 | AvFILLp (av) = 0; |
|
|
2779 | ary [0] = newSViv (count_ && SvOK (count_) ? SvIV (count_) : 1); |
|
|
2780 | |
|
|
2781 | RETVAL = sv_bless (newRV_noinc ((SV *)av), GvSTASH (CvGV (cv))); |
|
|
2782 | } |
|
|
2783 | OUTPUT: |
2896 | OUTPUT: |
2784 | RETVAL |
2897 | RETVAL |
2785 | |
2898 | |
2786 | SV * |
2899 | SV * |
2787 | count (SV *self) |
2900 | count (SV *self) |
… | |
… | |
2799 | |
2912 | |
2800 | void |
2913 | void |
2801 | down (SV *self) |
2914 | down (SV *self) |
2802 | CODE: |
2915 | CODE: |
2803 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
2916 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); |
|
|
2917 | |
|
|
2918 | void |
|
|
2919 | wait (SV *self) |
|
|
2920 | CODE: |
|
|
2921 | CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); |
2804 | |
2922 | |
2805 | void |
2923 | void |
2806 | try (SV *self) |
2924 | try (SV *self) |
2807 | PPCODE: |
2925 | PPCODE: |
2808 | { |
2926 | { |
… | |
… | |
2820 | XSRETURN_NO; |
2938 | XSRETURN_NO; |
2821 | } |
2939 | } |
2822 | |
2940 | |
2823 | void |
2941 | void |
2824 | waiters (SV *self) |
2942 | waiters (SV *self) |
2825 | CODE: |
2943 | PPCODE: |
2826 | { |
2944 | { |
2827 | AV *av = (AV *)SvRV (self); |
2945 | AV *av = (AV *)SvRV (self); |
|
|
2946 | int wcount = AvFILLp (av) + 1 - 1; |
2828 | |
2947 | |
2829 | if (GIMME_V == G_SCALAR) |
2948 | if (GIMME_V == G_SCALAR) |
2830 | XPUSHs (sv_2mortal (newSVsv (AvARRAY (av)[0]))); |
2949 | XPUSHs (sv_2mortal (newSViv (wcount))); |
2831 | else |
2950 | else |
2832 | { |
2951 | { |
2833 | int i; |
2952 | int i; |
2834 | EXTEND (SP, AvFILLp (av) + 1 - 1); |
2953 | EXTEND (SP, wcount); |
2835 | for (i = 1; i <= AvFILLp (av); ++i) |
2954 | for (i = 1; i <= wcount; ++i) |
2836 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
2955 | PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); |
2837 | } |
2956 | } |
2838 | } |
2957 | } |
|
|
2958 | |
|
|
2959 | MODULE = Coro::State PACKAGE = Coro::Signal |
|
|
2960 | |
|
|
2961 | SV * |
|
|
2962 | new (SV *klass) |
|
|
2963 | CODE: |
|
|
2964 | RETVAL = sv_bless ( |
|
|
2965 | coro_waitarray_new (aTHX_ 0), |
|
|
2966 | GvSTASH (CvGV (cv)) |
|
|
2967 | ); |
|
|
2968 | OUTPUT: |
|
|
2969 | RETVAL |
|
|
2970 | |
|
|
2971 | void |
|
|
2972 | wait (SV *self) |
|
|
2973 | CODE: |
|
|
2974 | CORO_EXECUTE_SLF_XS (slf_init_signal_wait); |
|
|
2975 | |
|
|
2976 | void |
|
|
2977 | broadcast (SV *self) |
|
|
2978 | CODE: |
|
|
2979 | { |
|
|
2980 | AV *av = (AV *)SvRV (self); |
|
|
2981 | coro_signal_wake (aTHX_ av, AvFILLp (av)); |
|
|
2982 | } |
|
|
2983 | |
|
|
2984 | void |
|
|
2985 | send (SV *self) |
|
|
2986 | CODE: |
|
|
2987 | { |
|
|
2988 | AV *av = (AV *)SvRV (self); |
|
|
2989 | |
|
|
2990 | if (AvFILLp (av)) |
|
|
2991 | coro_signal_wake (aTHX_ av, 1); |
|
|
2992 | else |
|
|
2993 | SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */ |
|
|
2994 | } |
|
|
2995 | |
|
|
2996 | IV |
|
|
2997 | awaited (SV *self) |
|
|
2998 | CODE: |
|
|
2999 | RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1; |
|
|
3000 | OUTPUT: |
|
|
3001 | RETVAL |
2839 | |
3002 | |
2840 | |
3003 | |
2841 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
3004 | MODULE = Coro::State PACKAGE = Coro::AnyEvent |
2842 | |
3005 | |
2843 | BOOT: |
3006 | BOOT: |