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.378 by root, Sat Dec 12 01:30:26 2009 UTC vs.
Revision 1.398 by root, Sat May 7 14:11:10 2011 UTC

1/* this works around a bug in mingw32 providing a non-working setjmp */
2#define USE_NO_MINGW_SETJMP_TWO_ARGS
3
1#define NDEBUG 1 4#define NDEBUG 1
2 5
3#include "libcoro/coro.c" 6#include "libcoro/coro.c"
4 7
5#define PERL_NO_GET_CONTEXT 8#define PERL_NO_GET_CONTEXT
18 21
19#ifndef SVs_PADSTALE 22#ifndef SVs_PADSTALE
20# define SVs_PADSTALE 0 23# define SVs_PADSTALE 0
21#endif 24#endif
22 25
23#ifdef WIN32 26#if defined(_WIN32)
27# undef HAS_GETTIMEOFDAY
24# undef setjmp 28# undef setjmp
25# undef longjmp 29# undef longjmp
26# undef _exit 30# undef _exit
27# define setjmp _setjmp /* deep magic */ 31# define setjmp _setjmp /* deep magic */
28#else 32#else
122static void (*u2time)(pTHX_ UV ret[2]); 126static void (*u2time)(pTHX_ UV ret[2]);
123 127
124/* we hijack an hopefully unused CV flag for our purposes */ 128/* we hijack an hopefully unused CV flag for our purposes */
125#define CVf_SLF 0x4000 129#define CVf_SLF 0x4000
126static OP *pp_slf (pTHX); 130static OP *pp_slf (pTHX);
131static void slf_destroy (pTHX_ struct coro *coro);
127 132
128static U32 cctx_gen; 133static U32 cctx_gen;
129static size_t cctx_stacksize = CORO_STACKSIZE; 134static size_t cctx_stacksize = CORO_STACKSIZE;
130static struct CoroAPI coroapi; 135static struct CoroAPI coroapi;
131static AV *main_mainstack; /* used to differentiate between $main and others */ 136static AV *main_mainstack; /* used to differentiate between $main and others */
192 int valgrind_id; 197 int valgrind_id;
193#endif 198#endif
194 unsigned char flags; 199 unsigned char flags;
195} coro_cctx; 200} coro_cctx;
196 201
197coro_cctx *cctx_current; /* the currently running cctx */ 202static coro_cctx *cctx_current; /* the currently running cctx */
198 203
199/*****************************************************************************/ 204/*****************************************************************************/
200 205
201enum { 206enum {
202 CF_RUNNING = 0x0001, /* coroutine is running */ 207 CF_RUNNING = 0x0001, /* coroutine is running */
203 CF_READY = 0x0002, /* coroutine is ready */ 208 CF_READY = 0x0002, /* coroutine is ready */
204 CF_NEW = 0x0004, /* has never been switched to */ 209 CF_NEW = 0x0004, /* has never been switched to */
205 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 210 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
206 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) */
207}; 213};
208 214
209/* 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 */
210typedef struct 216typedef struct
211{ 217{
217#define VAR(name,type) type name; 223#define VAR(name,type) type name;
218# include "state.h" 224# include "state.h"
219#undef VAR 225#undef VAR
220} perl_slots; 226} perl_slots;
221 227
228// how many context stack entries do we need for perl_slots
222#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))
223 230
224/* this is a structure representing a perl-level coroutine */ 231/* this is a structure representing a perl-level coroutine */
225struct coro { 232struct coro {
226 /* the C coroutine allocated to this perl coroutine, if any */ 233 /* the C coroutine allocated to this perl coroutine, if any */
237 CV *startcv; /* the CV to execute */ 244 CV *startcv; /* the CV to execute */
238 AV *args; /* data associated with this coroutine (initial args) */ 245 AV *args; /* data associated with this coroutine (initial args) */
239 int refcnt; /* coroutines are refcounted, yes */ 246 int refcnt; /* coroutines are refcounted, yes */
240 int flags; /* CF_ flags */ 247 int flags; /* CF_ flags */
241 HV *hv; /* the perl hash associated with this coro, if any */ 248 HV *hv; /* the perl hash associated with this coro, if any */
242 void (*on_destroy)(pTHX_ struct coro *coro);
243 249
244 /* statistics */ 250 /* statistics */
245 int usecount; /* number of transfers to this coro */ 251 int usecount; /* number of transfers to this coro */
246 252
247 /* coro process data */ 253 /* coro process data */
271typedef struct coro *Coro__State; 277typedef struct coro *Coro__State;
272typedef struct coro *Coro__State_or_hashref; 278typedef struct coro *Coro__State_or_hashref;
273 279
274/* the following variables are effectively part of the perl context */ 280/* the following variables are effectively part of the perl context */
275/* and get copied between struct coro and these variables */ 281/* and get copied between struct coro and these variables */
276/* the mainr easonw e don't support windows process emulation */ 282/* the main reason we don't support windows process emulation */
277static struct CoroSLF slf_frame; /* the current slf frame */ 283static struct CoroSLF slf_frame; /* the current slf frame */
278 284
279/** Coro ********************************************************************/ 285/** Coro ********************************************************************/
280 286
281#define CORO_PRIO_MAX 3 287#define CORO_PRIO_MAX 3
287 293
288/* for Coro.pm */ 294/* for Coro.pm */
289static SV *coro_current; 295static SV *coro_current;
290static SV *coro_readyhook; 296static SV *coro_readyhook;
291static 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 */
292static CV *cv_coro_run, *cv_coro_terminate; 298static CV *cv_coro_run;
293static struct coro *coro_first; 299static struct coro *coro_first;
294#define coro_nready coroapi.nready 300#define coro_nready coroapi.nready
295 301
296/** Coro::Select ************************************************************/ 302/** Coro::Select ************************************************************/
297 303
310 /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */ 316 /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */
311 PL_op->op_flags |= OPf_STACKED; 317 PL_op->op_flags |= OPf_STACKED;
312 PL_op->op_private = 0; 318 PL_op->op_private = 0;
313 return PL_ppaddr [OP_ENTERSUB](aTHX); 319 return PL_ppaddr [OP_ENTERSUB](aTHX);
314} 320}
321
322/** time stuff **************************************************************/
323
324#ifdef HAS_GETTIMEOFDAY
325
326static void
327coro_u2time (pTHX_ UV ret[2])
328{
329 struct timeval tv;
330 gettimeofday (&tv, 0);
331
332 ret [0] = tv.tv_sec;
333 ret [1] = tv.tv_usec;
334}
335
336static double
337coro_nvtime ()
338{
339 struct timeval tv;
340 gettimeofday (&tv, 0);
341
342 return tv.tv_sec + tv.tv_usec * 1e-6;
343}
344
345static void
346time_init (pTHX)
347{
348 nvtime = coro_nvtime;
349 u2time = coro_u2time;
350}
351
352#else
353
354static void
355time_init (pTHX)
356{
357 SV **svp;
358
359 require_pv ("Time/HiRes.pm");
360
361 svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
362
363 if (!svp) croak ("Time::HiRes is required, but missing. Caught");
364 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught");
365
366 nvtime = INT2PTR (double (*)(), SvIV (*svp));
367
368 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0);
369 u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp));
370}
371
372#endif
315 373
316/** lowlevel stuff **********************************************************/ 374/** lowlevel stuff **********************************************************/
317 375
318static SV * 376static SV *
319coro_get_sv (pTHX_ const char *name, int create) 377coro_get_sv (pTHX_ const char *name, int create)
583 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); 641 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
584 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); 642 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
585 643
586#if PERL_VERSION_ATLEAST (5,10,0) 644#if PERL_VERSION_ATLEAST (5,10,0)
587 /* perl 5.10 complicates this _quite_ a bit, but it also is 645 /* perl 5.10 complicates this _quite_ a bit, but it also is
588 * is much faster, so no quarrels here. alternatively, we could 646 * much faster, so no quarrels here. alternatively, we could
589 * sv_upgrade to avoid this. 647 * sv_upgrade to avoid this.
590 */ 648 */
591 { 649 {
592 /* swap sv_u */ 650 /* swap sv_u */
593 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; 651 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
741 799
742 PUTBACK; 800 PUTBACK;
743 } 801 }
744 802
745 /* allocate some space on the context stack for our purposes */ 803 /* allocate some space on the context stack for our purposes */
746 /* we manually unroll here, as usually 2 slots is enough */ 804 if (expect_false (cxstack_ix + SLOT_COUNT >= cxstack_max))
747 if (SLOT_COUNT >= 1) CXINC;
748 if (SLOT_COUNT >= 2) CXINC;
749 if (SLOT_COUNT >= 3) CXINC;
750 { 805 {
751 unsigned int i; 806 unsigned int i;
807
752 for (i = 3; i < SLOT_COUNT; ++i) 808 for (i = 0; i < SLOT_COUNT; ++i)
753 CXINC; 809 CXINC;
754 } 810
755 cxstack_ix -= SLOT_COUNT; /* undo allocation */ 811 cxstack_ix -= SLOT_COUNT; /* undo allocation */
812 }
756 813
757 c->mainstack = PL_mainstack; 814 c->mainstack = PL_mainstack;
758 815
759 { 816 {
760 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 817 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
781# define coro_init_stacks(thx) init_stacks () 838# define coro_init_stacks(thx) init_stacks ()
782#else 839#else
783static void 840static void
784coro_init_stacks (pTHX) 841coro_init_stacks (pTHX)
785{ 842{
786 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() */
787 PL_curstackinfo->si_type = PERLSI_MAIN; 844 PL_curstackinfo->si_type = PERLSI_MAIN;
788 PL_curstack = PL_curstackinfo->si_stack; 845 PL_curstack = PL_curstackinfo->si_stack;
789 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 846 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
790 847
791 PL_stack_base = AvARRAY(PL_curstack); 848 PL_stack_base = AvARRAY(PL_curstack);
1010 PL_comppad_name_fill = 0; 1067 PL_comppad_name_fill = 0;
1011 PL_comppad_name_floor = 0; 1068 PL_comppad_name_floor = 0;
1012 PL_curpm = 0; 1069 PL_curpm = 0;
1013 PL_curpad = 0; 1070 PL_curpad = 0;
1014 PL_localizing = 0; 1071 PL_localizing = 0;
1015 PL_dirty = 0;
1016 PL_restartop = 0; 1072 PL_restartop = 0;
1017#if PERL_VERSION_ATLEAST (5,10,0) 1073#if PERL_VERSION_ATLEAST (5,10,0)
1018 PL_parser = 0; 1074 PL_parser = 0;
1019#endif 1075#endif
1020 PL_hints = 0; 1076 PL_hints = 0;
1098destroy_perl (pTHX_ struct coro *coro) 1154destroy_perl (pTHX_ struct coro *coro)
1099{ 1155{
1100 SV *svf [9]; 1156 SV *svf [9];
1101 1157
1102 { 1158 {
1159 SV *old_current = SvRV (coro_current);
1103 struct coro *current = SvSTATE_current; 1160 struct coro *current = SvSTATE (old_current);
1104 1161
1105 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));
1106 1163
1107 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
1108 load_perl (aTHX_ coro); 1169 load_perl (aTHX_ coro);
1109 1170
1110 coro_unwind_stacks (aTHX); 1171 coro_unwind_stacks (aTHX);
1111 coro_destruct_stacks (aTHX); 1172 coro_destruct_stacks (aTHX);
1112 1173
1122 svf [5] = GvSV (irsgv); 1183 svf [5] = GvSV (irsgv);
1123 svf [6] = (SV *)GvHV (PL_hintgv); 1184 svf [6] = (SV *)GvHV (PL_hintgv);
1124 svf [7] = PL_diehook; 1185 svf [7] = PL_diehook;
1125 svf [8] = PL_warnhook; 1186 svf [8] = PL_warnhook;
1126 assert (9 == sizeof (svf) / sizeof (*svf)); 1187 assert (9 == sizeof (svf) / sizeof (*svf));
1188
1189 SvRV_set (coro_current, old_current);
1127 1190
1128 load_perl (aTHX_ current); 1191 load_perl (aTHX_ current);
1129 } 1192 }
1130 1193
1131 { 1194 {
1441cctx_destroy (coro_cctx *cctx) 1504cctx_destroy (coro_cctx *cctx)
1442{ 1505{
1443 if (!cctx) 1506 if (!cctx)
1444 return; 1507 return;
1445 1508
1446 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary? 1509 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));
1447 1510
1448 --cctx_count; 1511 --cctx_count;
1449 coro_destroy (&cctx->cctx); 1512 coro_destroy (&cctx->cctx);
1450 1513
1451 /* coro_transfer creates new, empty cctx's */ 1514 /* coro_transfer creates new, empty cctx's */
1610#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))
1611#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) 1674#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1612 1675
1613/** high level stuff ********************************************************/ 1676/** high level stuff ********************************************************/
1614 1677
1615static int 1678static void
1616coro_state_destroy (pTHX_ struct coro *coro) 1679coro_state_destroy (pTHX_ struct coro *coro)
1617{ 1680{
1618 if (coro->flags & CF_DESTROYED) 1681 if (coro->flags & CF_DESTROYED)
1619 return 0; 1682 return;
1620 1683
1621 if (coro->on_destroy && !PL_dirty)
1622 coro->on_destroy (aTHX_ coro); 1684 slf_destroy (aTHX_ coro);
1623 1685
1624 coro->flags |= CF_DESTROYED; 1686 coro->flags |= CF_DESTROYED;
1625 1687
1626 if (coro->flags & CF_READY) 1688 if (coro->flags & CF_READY)
1627 { 1689 {
1645 cctx_destroy (coro->cctx); 1707 cctx_destroy (coro->cctx);
1646 SvREFCNT_dec (coro->startcv); 1708 SvREFCNT_dec (coro->startcv);
1647 SvREFCNT_dec (coro->args); 1709 SvREFCNT_dec (coro->args);
1648 SvREFCNT_dec (coro->swap_sv); 1710 SvREFCNT_dec (coro->swap_sv);
1649 SvREFCNT_dec (CORO_THROW); 1711 SvREFCNT_dec (CORO_THROW);
1650
1651 return 1;
1652} 1712}
1653 1713
1654static int 1714static int
1655coro_state_free (pTHX_ SV *sv, MAGIC *mg) 1715coro_state_free (pTHX_ SV *sv, MAGIC *mg)
1656{ 1716{
1953 2013
1954static void 2014static void
1955coro_call_on_destroy (pTHX_ struct coro *coro) 2015coro_call_on_destroy (pTHX_ struct coro *coro)
1956{ 2016{
1957 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);
1958 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
1959 2018
1960 if (on_destroyp) 2019 if (on_destroyp)
1961 { 2020 {
1962 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;
1963 2024
1964 while (AvFILLp (on_destroy) >= 0) 2025 while (AvFILLp (on_destroy) >= 0)
1965 { 2026 {
1966 dSP; /* don't disturb outer sp */ 2027 dSP; /* don't disturb outer sp */
1967 SV *cb = av_pop (on_destroy); 2028 SV *cb = av_pop (on_destroy);
1969 PUSHMARK (SP); 2030 PUSHMARK (SP);
1970 2031
1971 if (statusp) 2032 if (statusp)
1972 { 2033 {
1973 int i; 2034 int i;
1974 AV *status = (AV *)SvRV (*statusp);
1975 EXTEND (SP, AvFILLp (status) + 1); 2035 EXTEND (SP, AvFILLp (status) + 1);
1976 2036
1977 for (i = 0; i <= AvFILLp (status); ++i) 2037 for (i = 0; i <= AvFILLp (status); ++i)
1978 PUSHs (AvARRAY (status)[i]); 2038 PUSHs (AvARRAY (status)[i]);
1979 } 2039 }
1983 } 2043 }
1984 } 2044 }
1985} 2045}
1986 2046
1987static void 2047static void
1988slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2048coro_set_status (pTHX_ HV *coro_hv, SV **arg, int items)
1989{ 2049{
1990 int i;
1991 HV *hv = (HV *)SvRV (coro_current);
1992 AV *av = newAV (); 2050 AV *av = newAV ();
1993 2051
1994 /* items are actually not so common, so optimise for this case */ 2052 /* items are actually not so common, so optimise for this case */
1995 if (items) 2053 if (items)
1996 { 2054 {
2055 int i;
2056
1997 av_extend (av, items - 1); 2057 av_extend (av, items - 1);
1998 2058
1999 for (i = 0; i < items; ++i) 2059 for (i = 0; i < items; ++i)
2000 av_push (av, SvREFCNT_inc_NN (arg [i])); 2060 av_push (av, SvREFCNT_inc_NN (arg [i]));
2001 } 2061 }
2002 2062
2003 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}
2004 2065
2066static void
2067slf_init_terminate_cancel_common (pTHX_ struct CoroSLF *frame, HV *coro_hv)
2068{
2005 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 */
2006 api_ready (aTHX_ sv_manager); 2070 api_ready (aTHX_ sv_manager);
2007 2071
2008 frame->prepare = prepare_schedule; 2072 frame->prepare = prepare_schedule;
2009 frame->check = slf_check_repeat; 2073 frame->check = slf_check_repeat;
2010 2074
2011 /* as a minor optimisation, we could unwind all stacks here */ 2075 /* as a minor optimisation, we could unwind all stacks here */
2012 /* 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 */
2013 /*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;
2014} 2185}
2015 2186
2016/*****************************************************************************/ 2187/*****************************************************************************/
2017/* async pool handler */ 2188/* async pool handler */
2018 2189
2058 coro->saved_deffh = 0; 2229 coro->saved_deffh = 0;
2059 2230
2060 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) 2231 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2061 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2232 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2062 { 2233 {
2063 coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate); 2234 slf_init_terminate_cancel_common (aTHX_ frame, hv);
2064 coro->invoke_av = newAV (); 2235 return;
2065
2066 frame->prepare = prepare_nop;
2067 } 2236 }
2068 else 2237 else
2069 { 2238 {
2070 av_clear (GvAV (PL_defgv)); 2239 av_clear (GvAV (PL_defgv));
2071 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);
2294static void 2463static void
2295slf_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)
2296{ 2465{
2297 frame->prepare = prepare_cede_notself; 2466 frame->prepare = prepare_cede_notself;
2298 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;
2299} 2485}
2300 2486
2301/* 2487/*
2302 * these not obviously related functions are all rolled into one 2488 * these not obviously related functions are all rolled into one
2303 * 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
2422 if (PL_op->op_flags & OPf_STACKED) 2608 if (PL_op->op_flags & OPf_STACKED)
2423 { 2609 {
2424 if (items > slf_arga) 2610 if (items > slf_arga)
2425 { 2611 {
2426 slf_arga = items; 2612 slf_arga = items;
2427 free (slf_argv); 2613 Safefree (slf_argv);
2428 slf_argv = malloc (slf_arga * sizeof (SV *)); 2614 New (0, slf_argv, slf_arga, SV *);
2429 } 2615 }
2430 2616
2431 slf_argc = items; 2617 slf_argc = items;
2432 2618
2433 for (i = 0; i < items; ++i) 2619 for (i = 0; i < items; ++i)
2629 SvREFCNT_dec (cb); 2815 SvREFCNT_dec (cb);
2630 } 2816 }
2631} 2817}
2632 2818
2633static void 2819static void
2634coro_semaphore_on_destroy (pTHX_ struct coro *coro) 2820coro_semaphore_destroy (pTHX_ struct coro *coro)
2635{ 2821{
2636 /* call $sem->adjust (0) to possibly wake up some other waiters */ 2822 /* call $sem->adjust (0) to possibly wake up some other waiters */
2637 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); 2823 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0);
2638} 2824}
2639 2825
2646 /* 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 */
2647 if (CORO_THROW) 2833 if (CORO_THROW)
2648 return 0; 2834 return 0;
2649 else if (SvIVX (count_sv) > 0) 2835 else if (SvIVX (count_sv) > 0)
2650 { 2836 {
2651 SvSTATE_current->on_destroy = 0; 2837 frame->destroy = 0;
2652 2838
2653 if (acquire) 2839 if (acquire)
2654 SvIVX (count_sv) = SvIVX (count_sv) - 1; 2840 SvIVX (count_sv) = SvIVX (count_sv) - 1;
2655 else 2841 else
2656 coro_semaphore_adjust (aTHX_ av, 0); 2842 coro_semaphore_adjust (aTHX_ av, 0);
2699 { 2885 {
2700 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2886 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2701 2887
2702 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); 2888 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av));
2703 frame->prepare = prepare_schedule; 2889 frame->prepare = prepare_schedule;
2704
2705 /* to avoid race conditions when a woken-up coro gets terminated */ 2890 /* to avoid race conditions when a woken-up coro gets terminated */
2706 /* we arrange for a temporary on_destroy that calls adjust (0) */ 2891 /* we arrange for a temporary on_destroy that calls adjust (0) */
2707 SvSTATE_current->on_destroy = coro_semaphore_on_destroy; 2892 frame->destroy = coro_semaphore_destroy;
2708 } 2893 }
2709} 2894}
2710 2895
2711static void 2896static void
2712slf_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)
2794 { 2979 {
2795 SV *cb_cv = s_get_cv_croak (arg [1]); 2980 SV *cb_cv = s_get_cv_croak (arg [1]);
2796 av_push (av, SvREFCNT_inc_NN (cb_cv)); 2981 av_push (av, SvREFCNT_inc_NN (cb_cv));
2797 2982
2798 if (SvIVX (AvARRAY (av)[0])) 2983 if (SvIVX (AvARRAY (av)[0]))
2799 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ 2984 coro_signal_wake (aTHX_ av, 1); /* must be the only waiter */
2800 2985
2801 frame->prepare = prepare_nop; 2986 frame->prepare = prepare_nop;
2802 frame->check = slf_check_nop; 2987 frame->check = slf_check_nop;
2803 } 2988 }
2804 else if (SvIVX (AvARRAY (av)[0])) 2989 else if (SvIVX (AvARRAY (av)[0]))
2966 /* now call the AIO function - we assume our request is uncancelable */ 3151 /* now call the AIO function - we assume our request is uncancelable */
2967 PUTBACK; 3152 PUTBACK;
2968 call_sv ((SV *)req, G_VOID | G_DISCARD); 3153 call_sv ((SV *)req, G_VOID | G_DISCARD);
2969 } 3154 }
2970 3155
2971 /* now that the requets is going, we loop toll we have a result */ 3156 /* now that the request is going, we loop till we have a result */
2972 frame->data = (void *)state; 3157 frame->data = (void *)state;
2973 frame->prepare = prepare_schedule; 3158 frame->prepare = prepare_schedule;
2974 frame->check = slf_check_aio_req; 3159 frame->check = slf_check_aio_req;
2975} 3160}
2976 3161
3107 coroapi.prepare_nop = prepare_nop; 3292 coroapi.prepare_nop = prepare_nop;
3108 coroapi.prepare_schedule = prepare_schedule; 3293 coroapi.prepare_schedule = prepare_schedule;
3109 coroapi.prepare_cede = prepare_cede; 3294 coroapi.prepare_cede = prepare_cede;
3110 coroapi.prepare_cede_notself = prepare_cede_notself; 3295 coroapi.prepare_cede_notself = prepare_cede_notself;
3111 3296
3112 { 3297 time_init (aTHX);
3113 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
3114
3115 if (!svp) croak ("Time::HiRes is required");
3116 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
3117
3118 nvtime = INT2PTR (double (*)(), SvIV (*svp));
3119
3120 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0);
3121 u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp));
3122 }
3123 3298
3124 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3299 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
3125} 3300}
3126 3301
3127SV * 3302SV *
3136void 3311void
3137transfer (...) 3312transfer (...)
3138 PROTOTYPE: $$ 3313 PROTOTYPE: $$
3139 CODE: 3314 CODE:
3140 CORO_EXECUTE_SLF_XS (slf_init_transfer); 3315 CORO_EXECUTE_SLF_XS (slf_init_transfer);
3141
3142bool
3143_destroy (SV *coro_sv)
3144 CODE:
3145 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
3146 OUTPUT:
3147 RETVAL
3148 3316
3149void 3317void
3150_exit (int code) 3318_exit (int code)
3151 PROTOTYPE: $ 3319 PROTOTYPE: $
3152 CODE: 3320 CODE:
3228 CODE: 3396 CODE:
3229{ 3397{
3230 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3398 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
3231 { 3399 {
3232 struct coro *current = SvSTATE_current; 3400 struct coro *current = SvSTATE_current;
3401 struct CoroSLF slf_save;
3233 3402
3234 if (current != coro) 3403 if (current != coro)
3235 { 3404 {
3236 PUTBACK; 3405 PUTBACK;
3237 save_perl (aTHX_ current); 3406 save_perl (aTHX_ current);
3238 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;
3239 SPAGAIN; 3417 SPAGAIN;
3240 } 3418 }
3241 3419
3242 PUSHSTACK; 3420 PUSHSTACK;
3243 3421
3253 SPAGAIN; 3431 SPAGAIN;
3254 3432
3255 if (current != coro) 3433 if (current != coro)
3256 { 3434 {
3257 PUTBACK; 3435 PUTBACK;
3436 slf_frame = slf_save;
3258 save_perl (aTHX_ coro); 3437 save_perl (aTHX_ coro);
3259 load_perl (aTHX_ current); 3438 load_perl (aTHX_ current);
3260 SPAGAIN; 3439 SPAGAIN;
3261 } 3440 }
3262 } 3441 }
3275 RETVAL = boolSV (coro->flags & ix); 3454 RETVAL = boolSV (coro->flags & ix);
3276 OUTPUT: 3455 OUTPUT:
3277 RETVAL 3456 RETVAL
3278 3457
3279void 3458void
3280throw (Coro::State self, SV *throw = &PL_sv_undef) 3459throw (Coro::State self, SV *exception = &PL_sv_undef)
3281 PROTOTYPE: $;$ 3460 PROTOTYPE: $;$
3282 CODE: 3461 CODE:
3283{ 3462{
3284 struct coro *current = SvSTATE_current; 3463 struct coro *current = SvSTATE_current;
3285 SV **throwp = self == current ? &CORO_THROW : &self->except; 3464 SV **exceptionp = self == current ? &CORO_THROW : &self->except;
3286 SvREFCNT_dec (*throwp); 3465 SvREFCNT_dec (*exceptionp);
3287 SvGETMAGIC (throw); 3466 SvGETMAGIC (exception);
3288 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 3467 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0;
3289} 3468}
3290 3469
3291void 3470void
3292api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3471api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3293 PROTOTYPE: $;$ 3472 PROTOTYPE: $;$
3348 3527
3349void 3528void
3350cancel (Coro::State self) 3529cancel (Coro::State self)
3351 CODE: 3530 CODE:
3352 coro_state_destroy (aTHX_ self); 3531 coro_state_destroy (aTHX_ self);
3353 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3354
3355 3532
3356SV * 3533SV *
3357enable_times (int enabled = enable_times) 3534enable_times (int enabled = enable_times)
3358 CODE: 3535 CODE:
3359{ 3536{
3415BOOT: 3592BOOT:
3416{ 3593{
3417 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3594 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3418 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3595 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3419 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); 3596 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3420 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3421 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);
3422 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3598 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3423 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3599 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3424 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3600 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3425 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); 3601 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3464 api_ready (aTHX_ RETVAL); 3640 api_ready (aTHX_ RETVAL);
3465 OUTPUT: 3641 OUTPUT:
3466 RETVAL 3642 RETVAL
3467 3643
3468void 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
3469terminate (...) 3652terminate (...)
3470 CODE: 3653 CODE:
3471 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
3472 3664
3473void 3665void
3474schedule (...) 3666schedule (...)
3475 CODE: 3667 CODE:
3476 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3668 CORO_EXECUTE_SLF_XS (slf_init_schedule);
3873 { 4065 {
3874 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; 4066 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect;
3875 coro_old_pp_sselect = 0; 4067 coro_old_pp_sselect = 0;
3876 } 4068 }
3877 4069
3878

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines