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.386 by root, Sat Feb 19 06:51:23 2011 UTC vs.
Revision 1.398 by root, Sat May 7 14:11:10 2011 UTC

21 21
22#ifndef SVs_PADSTALE 22#ifndef SVs_PADSTALE
23# define SVs_PADSTALE 0 23# define SVs_PADSTALE 0
24#endif 24#endif
25 25
26#ifdef WIN32 26#if defined(_WIN32)
27# undef HAS_GETTIMEOFDAY
27# undef setjmp 28# undef setjmp
28# undef longjmp 29# undef longjmp
29# undef _exit 30# undef _exit
30# define setjmp _setjmp /* deep magic */ 31# define setjmp _setjmp /* deep magic */
31#else 32#else
125static void (*u2time)(pTHX_ UV ret[2]); 126static void (*u2time)(pTHX_ UV ret[2]);
126 127
127/* we hijack an hopefully unused CV flag for our purposes */ 128/* we hijack an hopefully unused CV flag for our purposes */
128#define CVf_SLF 0x4000 129#define CVf_SLF 0x4000
129static OP *pp_slf (pTHX); 130static OP *pp_slf (pTHX);
131static void slf_destroy (pTHX_ struct coro *coro);
130 132
131static U32 cctx_gen; 133static U32 cctx_gen;
132static size_t cctx_stacksize = CORO_STACKSIZE; 134static size_t cctx_stacksize = CORO_STACKSIZE;
133static struct CoroAPI coroapi; 135static struct CoroAPI coroapi;
134static AV *main_mainstack; /* used to differentiate between $main and others */ 136static AV *main_mainstack; /* used to differentiate between $main and others */
195 int valgrind_id; 197 int valgrind_id;
196#endif 198#endif
197 unsigned char flags; 199 unsigned char flags;
198} coro_cctx; 200} coro_cctx;
199 201
200coro_cctx *cctx_current; /* the currently running cctx */ 202static coro_cctx *cctx_current; /* the currently running cctx */
201 203
202/*****************************************************************************/ 204/*****************************************************************************/
203 205
204enum { 206enum {
205 CF_RUNNING = 0x0001, /* coroutine is running */ 207 CF_RUNNING = 0x0001, /* coroutine is running */
206 CF_READY = 0x0002, /* coroutine is ready */ 208 CF_READY = 0x0002, /* coroutine is ready */
207 CF_NEW = 0x0004, /* has never been switched to */ 209 CF_NEW = 0x0004, /* has never been switched to */
208 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 210 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
209 CF_SUSPENDED = 0x0010, /* coroutine can't be scheduled */ 211 CF_SUSPENDED = 0x0010, /* coroutine can't be scheduled */
212 CF_NOCANCEL = 0x0020, /* cannot cancel, set slf_frame.data to 1 (hackish) */
210}; 213};
211 214
212/* the structure where most of the perl state is stored, overlaid on the cxstack */ 215/* the structure where most of the perl state is stored, overlaid on the cxstack */
213typedef struct 216typedef struct
214{ 217{
220#define VAR(name,type) type name; 223#define VAR(name,type) type name;
221# include "state.h" 224# include "state.h"
222#undef VAR 225#undef VAR
223} perl_slots; 226} perl_slots;
224 227
228// how many context stack entries do we need for perl_slots
225#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) 229#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT))
226 230
227/* this is a structure representing a perl-level coroutine */ 231/* this is a structure representing a perl-level coroutine */
228struct coro { 232struct coro {
229 /* the C coroutine allocated to this perl coroutine, if any */ 233 /* the C coroutine allocated to this perl coroutine, if any */
240 CV *startcv; /* the CV to execute */ 244 CV *startcv; /* the CV to execute */
241 AV *args; /* data associated with this coroutine (initial args) */ 245 AV *args; /* data associated with this coroutine (initial args) */
242 int refcnt; /* coroutines are refcounted, yes */ 246 int refcnt; /* coroutines are refcounted, yes */
243 int flags; /* CF_ flags */ 247 int flags; /* CF_ flags */
244 HV *hv; /* the perl hash associated with this coro, if any */ 248 HV *hv; /* the perl hash associated with this coro, if any */
245 void (*on_destroy)(pTHX_ struct coro *coro); /* for temporary use by xs in critical sections */
246 249
247 /* statistics */ 250 /* statistics */
248 int usecount; /* number of transfers to this coro */ 251 int usecount; /* number of transfers to this coro */
249 252
250 /* coro process data */ 253 /* coro process data */
274typedef struct coro *Coro__State; 277typedef struct coro *Coro__State;
275typedef struct coro *Coro__State_or_hashref; 278typedef struct coro *Coro__State_or_hashref;
276 279
277/* the following variables are effectively part of the perl context */ 280/* the following variables are effectively part of the perl context */
278/* and get copied between struct coro and these variables */ 281/* and get copied between struct coro and these variables */
279/* the mainr easonw e don't support windows process emulation */ 282/* the main reason we don't support windows process emulation */
280static struct CoroSLF slf_frame; /* the current slf frame */ 283static struct CoroSLF slf_frame; /* the current slf frame */
281 284
282/** Coro ********************************************************************/ 285/** Coro ********************************************************************/
283 286
284#define CORO_PRIO_MAX 3 287#define CORO_PRIO_MAX 3
290 293
291/* for Coro.pm */ 294/* for Coro.pm */
292static SV *coro_current; 295static SV *coro_current;
293static SV *coro_readyhook; 296static SV *coro_readyhook;
294static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ 297static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */
295static CV *cv_coro_run, *cv_coro_terminate; 298static CV *cv_coro_run;
296static struct coro *coro_first; 299static struct coro *coro_first;
297#define coro_nready coroapi.nready 300#define coro_nready coroapi.nready
298 301
299/** Coro::Select ************************************************************/ 302/** Coro::Select ************************************************************/
300 303
319/** time stuff **************************************************************/ 322/** time stuff **************************************************************/
320 323
321#ifdef HAS_GETTIMEOFDAY 324#ifdef HAS_GETTIMEOFDAY
322 325
323static void 326static void
324coro_u2time (aTHX_ UV ret[2]) 327coro_u2time (pTHX_ UV ret[2])
325{ 328{
326 struct timeval tv; 329 struct timeval tv;
327 gettimeofday (&tv, 0); 330 gettimeofday (&tv, 0);
328 331
329 ret [0] = tv.tv_sec; 332 ret [0] = tv.tv_sec;
338 341
339 return tv.tv_sec + tv.tv_usec * 1e-6; 342 return tv.tv_sec + tv.tv_usec * 1e-6;
340} 343}
341 344
342static void 345static void
343time_init (void) 346time_init (pTHX)
344{ 347{
345 nvtime = coro_nvtime; 348 nvtime = coro_nvtime;
346 u2time = coro_u2time; 349 u2time = coro_u2time;
347} 350}
348 351
349#else 352#else
350 353
351static void 354static void
352time_init (void) 355time_init (pTHX)
353{ 356{
354 SV **svp; 357 SV **svp;
355 358
356 require_pv ("Time::HiRes"); 359 require_pv ("Time/HiRes.pm");
357 360
358 svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); 361 svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
359 362
360 if (!svp) croak ("Time::HiRes is required"); 363 if (!svp) croak ("Time::HiRes is required, but missing. Caught");
361 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); 364 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught");
362 365
363 nvtime = INT2PTR (double (*)(), SvIV (*svp)); 366 nvtime = INT2PTR (double (*)(), SvIV (*svp));
364 367
365 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0); 368 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0);
366 u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp)); 369 u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp));
796 799
797 PUTBACK; 800 PUTBACK;
798 } 801 }
799 802
800 /* allocate some space on the context stack for our purposes */ 803 /* allocate some space on the context stack for our purposes */
801 /* we manually unroll here, as usually 2 slots is enough */ 804 if (expect_false (cxstack_ix + SLOT_COUNT >= cxstack_max))
802 if (SLOT_COUNT >= 1) CXINC;
803 if (SLOT_COUNT >= 2) CXINC;
804 if (SLOT_COUNT >= 3) CXINC;
805 { 805 {
806 unsigned int i; 806 unsigned int i;
807
807 for (i = 3; i < SLOT_COUNT; ++i) 808 for (i = 0; i < SLOT_COUNT; ++i)
808 CXINC; 809 CXINC;
809 } 810
810 cxstack_ix -= SLOT_COUNT; /* undo allocation */ 811 cxstack_ix -= SLOT_COUNT; /* undo allocation */
812 }
811 813
812 c->mainstack = PL_mainstack; 814 c->mainstack = PL_mainstack;
813 815
814 { 816 {
815 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 817 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
836# define coro_init_stacks(thx) init_stacks () 838# define coro_init_stacks(thx) init_stacks ()
837#else 839#else
838static void 840static void
839coro_init_stacks (pTHX) 841coro_init_stacks (pTHX)
840{ 842{
841 PL_curstackinfo = new_stackinfo(32, 8); 843 PL_curstackinfo = new_stackinfo(32, 4 + SLOT_COUNT); /* 3 is minimum due to perl rounding down in scope.c:GROW() */
842 PL_curstackinfo->si_type = PERLSI_MAIN; 844 PL_curstackinfo->si_type = PERLSI_MAIN;
843 PL_curstack = PL_curstackinfo->si_stack; 845 PL_curstack = PL_curstackinfo->si_stack;
844 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 846 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
845 847
846 PL_stack_base = AvARRAY(PL_curstack); 848 PL_stack_base = AvARRAY(PL_curstack);
1152destroy_perl (pTHX_ struct coro *coro) 1154destroy_perl (pTHX_ struct coro *coro)
1153{ 1155{
1154 SV *svf [9]; 1156 SV *svf [9];
1155 1157
1156 { 1158 {
1159 SV *old_current = SvRV (coro_current);
1157 struct coro *current = SvSTATE_current; 1160 struct coro *current = SvSTATE (old_current);
1158 1161
1159 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); 1162 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1160 1163
1161 save_perl (aTHX_ current); 1164 save_perl (aTHX_ current);
1165
1166 /* this will cause transfer_check to croak on block*/
1167 SvRV_set (coro_current, (SV *)coro->hv);
1168
1162 load_perl (aTHX_ coro); 1169 load_perl (aTHX_ coro);
1163 1170
1164 coro_unwind_stacks (aTHX); 1171 coro_unwind_stacks (aTHX);
1165 coro_destruct_stacks (aTHX); 1172 coro_destruct_stacks (aTHX);
1166 1173
1176 svf [5] = GvSV (irsgv); 1183 svf [5] = GvSV (irsgv);
1177 svf [6] = (SV *)GvHV (PL_hintgv); 1184 svf [6] = (SV *)GvHV (PL_hintgv);
1178 svf [7] = PL_diehook; 1185 svf [7] = PL_diehook;
1179 svf [8] = PL_warnhook; 1186 svf [8] = PL_warnhook;
1180 assert (9 == sizeof (svf) / sizeof (*svf)); 1187 assert (9 == sizeof (svf) / sizeof (*svf));
1188
1189 SvRV_set (coro_current, old_current);
1181 1190
1182 load_perl (aTHX_ current); 1191 load_perl (aTHX_ current);
1183 } 1192 }
1184 1193
1185 { 1194 {
1495cctx_destroy (coro_cctx *cctx) 1504cctx_destroy (coro_cctx *cctx)
1496{ 1505{
1497 if (!cctx) 1506 if (!cctx)
1498 return; 1507 return;
1499 1508
1500 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary? 1509 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));
1501 1510
1502 --cctx_count; 1511 --cctx_count;
1503 coro_destroy (&cctx->cctx); 1512 coro_destroy (&cctx->cctx);
1504 1513
1505 /* coro_transfer creates new, empty cctx's */ 1514 /* coro_transfer creates new, empty cctx's */
1664#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) 1673#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx))
1665#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) 1674#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1666 1675
1667/** high level stuff ********************************************************/ 1676/** high level stuff ********************************************************/
1668 1677
1669static int 1678static void
1670coro_state_destroy (pTHX_ struct coro *coro) 1679coro_state_destroy (pTHX_ struct coro *coro)
1671{ 1680{
1672 if (coro->flags & CF_DESTROYED) 1681 if (coro->flags & CF_DESTROYED)
1673 return 0; 1682 return;
1674 1683
1675 if (coro->on_destroy && !PL_dirty)
1676 coro->on_destroy (aTHX_ coro); 1684 slf_destroy (aTHX_ coro);
1677 1685
1678 coro->flags |= CF_DESTROYED; 1686 coro->flags |= CF_DESTROYED;
1679 1687
1680 if (coro->flags & CF_READY) 1688 if (coro->flags & CF_READY)
1681 { 1689 {
1699 cctx_destroy (coro->cctx); 1707 cctx_destroy (coro->cctx);
1700 SvREFCNT_dec (coro->startcv); 1708 SvREFCNT_dec (coro->startcv);
1701 SvREFCNT_dec (coro->args); 1709 SvREFCNT_dec (coro->args);
1702 SvREFCNT_dec (coro->swap_sv); 1710 SvREFCNT_dec (coro->swap_sv);
1703 SvREFCNT_dec (CORO_THROW); 1711 SvREFCNT_dec (CORO_THROW);
1704
1705 return 1;
1706} 1712}
1707 1713
1708static int 1714static int
1709coro_state_free (pTHX_ SV *sv, MAGIC *mg) 1715coro_state_free (pTHX_ SV *sv, MAGIC *mg)
1710{ 1716{
2007 2013
2008static void 2014static void
2009coro_call_on_destroy (pTHX_ struct coro *coro) 2015coro_call_on_destroy (pTHX_ struct coro *coro)
2010{ 2016{
2011 SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0); 2017 SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0);
2012 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
2013 2018
2014 if (on_destroyp) 2019 if (on_destroyp)
2015 { 2020 {
2016 AV *on_destroy = (AV *)SvRV (*on_destroyp); 2021 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
2022 AV *on_destroy = sv_2mortal (SvREFCNT_inc ((AV *)SvRV (*on_destroyp)));
2023 AV *status = statusp ? sv_2mortal (SvREFCNT_inc ((AV *)SvRV (*statusp))) : 0;
2017 2024
2018 while (AvFILLp (on_destroy) >= 0) 2025 while (AvFILLp (on_destroy) >= 0)
2019 { 2026 {
2020 dSP; /* don't disturb outer sp */ 2027 dSP; /* don't disturb outer sp */
2021 SV *cb = av_pop (on_destroy); 2028 SV *cb = av_pop (on_destroy);
2023 PUSHMARK (SP); 2030 PUSHMARK (SP);
2024 2031
2025 if (statusp) 2032 if (statusp)
2026 { 2033 {
2027 int i; 2034 int i;
2028 AV *status = (AV *)SvRV (*statusp);
2029 EXTEND (SP, AvFILLp (status) + 1); 2035 EXTEND (SP, AvFILLp (status) + 1);
2030 2036
2031 for (i = 0; i <= AvFILLp (status); ++i) 2037 for (i = 0; i <= AvFILLp (status); ++i)
2032 PUSHs (AvARRAY (status)[i]); 2038 PUSHs (AvARRAY (status)[i]);
2033 } 2039 }
2037 } 2043 }
2038 } 2044 }
2039} 2045}
2040 2046
2041static void 2047static void
2042slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2048coro_set_status (pTHX_ HV *coro_hv, SV **arg, int items)
2043{ 2049{
2044 int i;
2045 HV *hv = (HV *)SvRV (coro_current);
2046 AV *av = newAV (); 2050 AV *av = newAV ();
2047 2051
2048 /* items are actually not so common, so optimise for this case */ 2052 /* items are actually not so common, so optimise for this case */
2049 if (items) 2053 if (items)
2050 { 2054 {
2055 int i;
2056
2051 av_extend (av, items - 1); 2057 av_extend (av, items - 1);
2052 2058
2053 for (i = 0; i < items; ++i) 2059 for (i = 0; i < items; ++i)
2054 av_push (av, SvREFCNT_inc_NN (arg [i])); 2060 av_push (av, SvREFCNT_inc_NN (arg [i]));
2055 } 2061 }
2056 2062
2057 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); 2063 hv_store (coro_hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0);
2064}
2058 2065
2066static void
2067slf_init_terminate_cancel_common (pTHX_ struct CoroSLF *frame, HV *coro_hv)
2068{
2059 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 2069 av_push (av_destroy, (SV *)newRV_inc ((SV *)coro_hv)); /* RVinc for perl */
2060 api_ready (aTHX_ sv_manager); 2070 api_ready (aTHX_ sv_manager);
2061 2071
2062 frame->prepare = prepare_schedule; 2072 frame->prepare = prepare_schedule;
2063 frame->check = slf_check_repeat; 2073 frame->check = slf_check_repeat;
2064 2074
2065 /* as a minor optimisation, we could unwind all stacks here */ 2075 /* as a minor optimisation, we could unwind all stacks here */
2066 /* but that puts extra pressure on pp_slf, and is not worth much */ 2076 /* but that puts extra pressure on pp_slf, and is not worth much */
2067 /*coro_unwind_stacks (aTHX);*/ 2077 /*coro_unwind_stacks (aTHX);*/
2078}
2079
2080static void
2081slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2082{
2083 HV *coro_hv = (HV *)SvRV (coro_current);
2084
2085 coro_set_status (aTHX_ coro_hv, arg, items);
2086 slf_init_terminate_cancel_common (aTHX_ frame, coro_hv);
2087}
2088
2089static void
2090slf_init_cancel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2091{
2092 HV *coro_hv;
2093 struct coro *coro;
2094
2095 if (items <= 0)
2096 croak ("Coro::cancel called without coro object,");
2097
2098 coro = SvSTATE (arg [0]);
2099 coro_hv = coro->hv;
2100
2101 coro_set_status (aTHX_ coro_hv, arg + 1, items - 1);
2102
2103 if (expect_false (coro->flags & CF_NOCANCEL))
2104 {
2105 /* coro currently busy cancelling something, so just notify it */
2106 coro->slf_frame.data = (void *)coro;
2107
2108 frame->prepare = prepare_nop;
2109 frame->check = slf_check_nop;
2110 }
2111 else if (coro_hv == (HV *)SvRV (coro_current))
2112 {
2113 /* cancelling the current coro is allowed, and equals terminate */
2114 slf_init_terminate_cancel_common (aTHX_ frame, coro_hv);
2115 }
2116 else
2117 {
2118 struct coro *self = SvSTATE_current;
2119
2120 /* otherwise we cancel directly, purely for speed reasons
2121 * unfortunately, this requires some magic trickery, as
2122 * somebody else could cancel us, so we have to fight the cancellation.
2123 * this is ugly, and hopefully fully worth the extra speed.
2124 * besides, I can't get the slow-but-safe version working...
2125 */
2126 slf_frame.data = 0;
2127 self->flags |= CF_NOCANCEL;
2128
2129 coro_state_destroy (aTHX_ coro);
2130 coro_call_on_destroy (aTHX_ coro);
2131
2132 self->flags &= ~CF_NOCANCEL;
2133
2134 if (slf_frame.data)
2135 {
2136 /* while we were busy we have been cancelled, so terminate */
2137 slf_init_terminate_cancel_common (aTHX_ frame, self->hv);
2138 }
2139 else
2140 {
2141 frame->prepare = prepare_nop;
2142 frame->check = slf_check_nop;
2143 }
2144 }
2145}
2146
2147static int
2148slf_check_safe_cancel (pTHX_ struct CoroSLF *frame)
2149{
2150 frame->prepare = 0;
2151 coro_unwind_stacks (aTHX);
2152
2153 slf_init_terminate_cancel_common (aTHX_ frame, (HV *)SvRV (coro_current));
2154
2155 return 1;
2156}
2157
2158static int
2159safe_cancel (pTHX_ struct coro *coro, SV **arg, int items)
2160{
2161 if (coro->cctx)
2162 croak ("coro inside C callback, unable to cancel at this time, caught");
2163
2164 if (coro->flags & CF_NEW)
2165 {
2166 coro_set_status (aTHX_ coro->hv, arg, items);
2167 coro_state_destroy (aTHX_ coro);
2168 coro_call_on_destroy (aTHX_ coro);
2169 }
2170 else
2171 {
2172 if (!coro->slf_frame.prepare)
2173 croak ("coro outside an SLF function, unable to cancel at this time, caught");
2174
2175 slf_destroy (aTHX_ coro);
2176
2177 coro_set_status (aTHX_ coro->hv, arg, items);
2178 coro->slf_frame.prepare = prepare_nop;
2179 coro->slf_frame.check = slf_check_safe_cancel;
2180
2181 api_ready (aTHX_ coro->hv);
2182 }
2183
2184 return 1;
2068} 2185}
2069 2186
2070/*****************************************************************************/ 2187/*****************************************************************************/
2071/* async pool handler */ 2188/* async pool handler */
2072 2189
2112 coro->saved_deffh = 0; 2229 coro->saved_deffh = 0;
2113 2230
2114 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) 2231 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2115 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2232 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2116 { 2233 {
2117 coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate); 2234 slf_init_terminate_cancel_common (aTHX_ frame, hv);
2118 coro->invoke_av = newAV (); 2235 return;
2119
2120 frame->prepare = prepare_nop;
2121 } 2236 }
2122 else 2237 else
2123 { 2238 {
2124 av_clear (GvAV (PL_defgv)); 2239 av_clear (GvAV (PL_defgv));
2125 hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0); 2240 hv_store (hv, "desc", sizeof ("desc") - 1, SvREFCNT_inc_NN (sv_async_pool_idle), 0);
2348static void 2463static void
2349slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2464slf_init_cede_notself (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2350{ 2465{
2351 frame->prepare = prepare_cede_notself; 2466 frame->prepare = prepare_cede_notself;
2352 frame->check = slf_check_nop; 2467 frame->check = slf_check_nop;
2468}
2469
2470/* "undo"/cancel a running slf call - used when cancelling a coro, mainly */
2471static void
2472slf_destroy (pTHX_ struct coro *coro)
2473{
2474 /* this callback is reserved for slf functions needing to do cleanup */
2475 if (coro->slf_frame.destroy && coro->slf_frame.prepare && !PL_dirty)
2476 coro->slf_frame.destroy (aTHX_ coro);
2477
2478 /*
2479 * The on_destroy above most likely is from an SLF call.
2480 * Since by definition the SLF call will not finish when we destroy
2481 * the coro, we will have to force-finish it here, otherwise
2482 * cleanup functions cannot call SLF functions.
2483 */
2484 coro->slf_frame.prepare = 0;
2353} 2485}
2354 2486
2355/* 2487/*
2356 * these not obviously related functions are all rolled into one 2488 * these not obviously related functions are all rolled into one
2357 * function to increase chances that they all will call transfer with the same 2489 * function to increase chances that they all will call transfer with the same
2683 SvREFCNT_dec (cb); 2815 SvREFCNT_dec (cb);
2684 } 2816 }
2685} 2817}
2686 2818
2687static void 2819static void
2688coro_semaphore_on_destroy (pTHX_ struct coro *coro) 2820coro_semaphore_destroy (pTHX_ struct coro *coro)
2689{ 2821{
2690 /* call $sem->adjust (0) to possibly wake up some other waiters */ 2822 /* call $sem->adjust (0) to possibly wake up some other waiters */
2691 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); 2823 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0);
2692} 2824}
2693 2825
2700 /* if we are about to throw, don't actually acquire the lock, just throw */ 2832 /* if we are about to throw, don't actually acquire the lock, just throw */
2701 if (CORO_THROW) 2833 if (CORO_THROW)
2702 return 0; 2834 return 0;
2703 else if (SvIVX (count_sv) > 0) 2835 else if (SvIVX (count_sv) > 0)
2704 { 2836 {
2705 SvSTATE_current->on_destroy = 0; 2837 frame->destroy = 0;
2706 2838
2707 if (acquire) 2839 if (acquire)
2708 SvIVX (count_sv) = SvIVX (count_sv) - 1; 2840 SvIVX (count_sv) = SvIVX (count_sv) - 1;
2709 else 2841 else
2710 coro_semaphore_adjust (aTHX_ av, 0); 2842 coro_semaphore_adjust (aTHX_ av, 0);
2753 { 2885 {
2754 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2886 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2755 2887
2756 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); 2888 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av));
2757 frame->prepare = prepare_schedule; 2889 frame->prepare = prepare_schedule;
2758
2759 /* to avoid race conditions when a woken-up coro gets terminated */ 2890 /* to avoid race conditions when a woken-up coro gets terminated */
2760 /* we arrange for a temporary on_destroy that calls adjust (0) */ 2891 /* we arrange for a temporary on_destroy that calls adjust (0) */
2761 SvSTATE_current->on_destroy = coro_semaphore_on_destroy; 2892 frame->destroy = coro_semaphore_destroy;
2762 } 2893 }
2763} 2894}
2764 2895
2765static void 2896static void
2766slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2897slf_init_semaphore_down (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
3161 coroapi.prepare_nop = prepare_nop; 3292 coroapi.prepare_nop = prepare_nop;
3162 coroapi.prepare_schedule = prepare_schedule; 3293 coroapi.prepare_schedule = prepare_schedule;
3163 coroapi.prepare_cede = prepare_cede; 3294 coroapi.prepare_cede = prepare_cede;
3164 coroapi.prepare_cede_notself = prepare_cede_notself; 3295 coroapi.prepare_cede_notself = prepare_cede_notself;
3165 3296
3166 time_init (); 3297 time_init (aTHX);
3167 3298
3168 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3299 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
3169} 3300}
3170 3301
3171SV * 3302SV *
3180void 3311void
3181transfer (...) 3312transfer (...)
3182 PROTOTYPE: $$ 3313 PROTOTYPE: $$
3183 CODE: 3314 CODE:
3184 CORO_EXECUTE_SLF_XS (slf_init_transfer); 3315 CORO_EXECUTE_SLF_XS (slf_init_transfer);
3185
3186bool
3187_destroy (SV *coro_sv)
3188 CODE:
3189 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
3190 OUTPUT:
3191 RETVAL
3192 3316
3193void 3317void
3194_exit (int code) 3318_exit (int code)
3195 PROTOTYPE: $ 3319 PROTOTYPE: $
3196 CODE: 3320 CODE:
3272 CODE: 3396 CODE:
3273{ 3397{
3274 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3398 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
3275 { 3399 {
3276 struct coro *current = SvSTATE_current; 3400 struct coro *current = SvSTATE_current;
3401 struct CoroSLF slf_save;
3277 3402
3278 if (current != coro) 3403 if (current != coro)
3279 { 3404 {
3280 PUTBACK; 3405 PUTBACK;
3281 save_perl (aTHX_ current); 3406 save_perl (aTHX_ current);
3282 load_perl (aTHX_ coro); 3407 load_perl (aTHX_ coro);
3408 /* the coro is most likely in an active SLF call.
3409 * while not strictly required (the code we execute is
3410 * not allowed to call any SLF functions), it's cleaner
3411 * to reinitialise the slf_frame and restore it later.
3412 * This might one day allow us to actually do SLF calls
3413 * from code executed here.
3414 */
3415 slf_save = slf_frame;
3416 slf_frame.prepare = 0;
3283 SPAGAIN; 3417 SPAGAIN;
3284 } 3418 }
3285 3419
3286 PUSHSTACK; 3420 PUSHSTACK;
3287 3421
3297 SPAGAIN; 3431 SPAGAIN;
3298 3432
3299 if (current != coro) 3433 if (current != coro)
3300 { 3434 {
3301 PUTBACK; 3435 PUTBACK;
3436 slf_frame = slf_save;
3302 save_perl (aTHX_ coro); 3437 save_perl (aTHX_ coro);
3303 load_perl (aTHX_ current); 3438 load_perl (aTHX_ current);
3304 SPAGAIN; 3439 SPAGAIN;
3305 } 3440 }
3306 } 3441 }
3392 3527
3393void 3528void
3394cancel (Coro::State self) 3529cancel (Coro::State self)
3395 CODE: 3530 CODE:
3396 coro_state_destroy (aTHX_ self); 3531 coro_state_destroy (aTHX_ self);
3397 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3398
3399 3532
3400SV * 3533SV *
3401enable_times (int enabled = enable_times) 3534enable_times (int enabled = enable_times)
3402 CODE: 3535 CODE:
3403{ 3536{
3459BOOT: 3592BOOT:
3460{ 3593{
3461 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3594 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3462 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3595 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3463 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); 3596 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3464 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3465 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3597 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3466 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3598 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3467 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3599 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3468 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3600 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3469 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); 3601 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3508 api_ready (aTHX_ RETVAL); 3640 api_ready (aTHX_ RETVAL);
3509 OUTPUT: 3641 OUTPUT:
3510 RETVAL 3642 RETVAL
3511 3643
3512void 3644void
3645_destroy (Coro::State coro)
3646 CODE:
3647 /* used by the manager thread */
3648 coro_state_destroy (aTHX_ coro);
3649 coro_call_on_destroy (aTHX_ coro);
3650
3651void
3513terminate (...) 3652terminate (...)
3514 CODE: 3653 CODE:
3515 CORO_EXECUTE_SLF_XS (slf_init_terminate); 3654 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3655
3656void
3657cancel (...)
3658 CODE:
3659 CORO_EXECUTE_SLF_XS (slf_init_cancel);
3660
3661int
3662safe_cancel (Coro::State self, ...)
3663 C_ARGS: aTHX_ self, &ST (1), items - 1
3516 3664
3517void 3665void
3518schedule (...) 3666schedule (...)
3519 CODE: 3667 CODE:
3520 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3668 CORO_EXECUTE_SLF_XS (slf_init_schedule);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines