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.375 by root, Fri Oct 2 19:58:02 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;
718 { 776 {
719 CV *cv = cx->blk_sub.cv; 777 CV *cv = cx->blk_sub.cv;
720 778
721 if (expect_true (CvDEPTH (cv))) 779 if (expect_true (CvDEPTH (cv)))
722 { 780 {
781 EXTEND (SP, 3);
723 PUSHs ((SV *)CvPADLIST (cv)); 782 PUSHs ((SV *)CvPADLIST (cv));
724 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv))); 783 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv)));
725 PUSHs ((SV *)cv); 784 PUSHs ((SV *)cv);
726 785
727 CvDEPTH (cv) = 0; 786 CvDEPTH (cv) = 0;
740 799
741 PUTBACK; 800 PUTBACK;
742 } 801 }
743 802
744 /* allocate some space on the context stack for our purposes */ 803 /* allocate some space on the context stack for our purposes */
745 /* we manually unroll here, as usually 2 slots is enough */ 804 if (expect_false (cxstack_ix + SLOT_COUNT >= cxstack_max))
746 if (SLOT_COUNT >= 1) CXINC;
747 if (SLOT_COUNT >= 2) CXINC;
748 if (SLOT_COUNT >= 3) CXINC;
749 { 805 {
750 unsigned int i; 806 unsigned int i;
807
751 for (i = 3; i < SLOT_COUNT; ++i) 808 for (i = 0; i < SLOT_COUNT; ++i)
752 CXINC; 809 CXINC;
753 } 810
754 cxstack_ix -= SLOT_COUNT; /* undo allocation */ 811 cxstack_ix -= SLOT_COUNT; /* undo allocation */
812 }
755 813
756 c->mainstack = PL_mainstack; 814 c->mainstack = PL_mainstack;
757 815
758 { 816 {
759 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 817 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
780# define coro_init_stacks(thx) init_stacks () 838# define coro_init_stacks(thx) init_stacks ()
781#else 839#else
782static void 840static void
783coro_init_stacks (pTHX) 841coro_init_stacks (pTHX)
784{ 842{
785 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() */
786 PL_curstackinfo->si_type = PERLSI_MAIN; 844 PL_curstackinfo->si_type = PERLSI_MAIN;
787 PL_curstack = PL_curstackinfo->si_stack; 845 PL_curstack = PL_curstackinfo->si_stack;
788 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 846 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
789 847
790 PL_stack_base = AvARRAY(PL_curstack); 848 PL_stack_base = AvARRAY(PL_curstack);
1009 PL_comppad_name_fill = 0; 1067 PL_comppad_name_fill = 0;
1010 PL_comppad_name_floor = 0; 1068 PL_comppad_name_floor = 0;
1011 PL_curpm = 0; 1069 PL_curpm = 0;
1012 PL_curpad = 0; 1070 PL_curpad = 0;
1013 PL_localizing = 0; 1071 PL_localizing = 0;
1014 PL_dirty = 0;
1015 PL_restartop = 0; 1072 PL_restartop = 0;
1016#if PERL_VERSION_ATLEAST (5,10,0) 1073#if PERL_VERSION_ATLEAST (5,10,0)
1017 PL_parser = 0; 1074 PL_parser = 0;
1018#endif 1075#endif
1019 PL_hints = 0; 1076 PL_hints = 0;
1097destroy_perl (pTHX_ struct coro *coro) 1154destroy_perl (pTHX_ struct coro *coro)
1098{ 1155{
1099 SV *svf [9]; 1156 SV *svf [9];
1100 1157
1101 { 1158 {
1159 SV *old_current = SvRV (coro_current);
1102 struct coro *current = SvSTATE_current; 1160 struct coro *current = SvSTATE (old_current);
1103 1161
1104 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));
1105 1163
1106 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
1107 load_perl (aTHX_ coro); 1169 load_perl (aTHX_ coro);
1108 1170
1109 coro_unwind_stacks (aTHX); 1171 coro_unwind_stacks (aTHX);
1110 coro_destruct_stacks (aTHX); 1172 coro_destruct_stacks (aTHX);
1111 1173
1122 svf [6] = (SV *)GvHV (PL_hintgv); 1184 svf [6] = (SV *)GvHV (PL_hintgv);
1123 svf [7] = PL_diehook; 1185 svf [7] = PL_diehook;
1124 svf [8] = PL_warnhook; 1186 svf [8] = PL_warnhook;
1125 assert (9 == sizeof (svf) / sizeof (*svf)); 1187 assert (9 == sizeof (svf) / sizeof (*svf));
1126 1188
1189 SvRV_set (coro_current, old_current);
1190
1127 load_perl (aTHX_ current); 1191 load_perl (aTHX_ current);
1128 } 1192 }
1129 1193
1130 { 1194 {
1131 unsigned int i; 1195 unsigned int i;
1135 1199
1136 SvREFCNT_dec (coro->saved_deffh); 1200 SvREFCNT_dec (coro->saved_deffh);
1137 SvREFCNT_dec (coro->rouse_cb); 1201 SvREFCNT_dec (coro->rouse_cb);
1138 SvREFCNT_dec (coro->invoke_cb); 1202 SvREFCNT_dec (coro->invoke_cb);
1139 SvREFCNT_dec (coro->invoke_av); 1203 SvREFCNT_dec (coro->invoke_av);
1140 SvREFCNT_dec (coro->swap_sv);
1141 } 1204 }
1142} 1205}
1143 1206
1144INLINE void 1207INLINE void
1145free_coro_mortal (pTHX) 1208free_coro_mortal (pTHX)
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 {
1636 && coro->mainstack != main_mainstack 1698 && coro->mainstack != main_mainstack
1637 && coro->slot 1699 && coro->slot
1638 && !PL_dirty) 1700 && !PL_dirty)
1639 destroy_perl (aTHX_ coro); 1701 destroy_perl (aTHX_ coro);
1640 1702
1703 if (coro->next) coro->next->prev = coro->prev;
1704 if (coro->prev) coro->prev->next = coro->next;
1705 if (coro == coro_first) coro_first = coro->next;
1706
1641 cctx_destroy (coro->cctx); 1707 cctx_destroy (coro->cctx);
1642 SvREFCNT_dec (coro->startcv); 1708 SvREFCNT_dec (coro->startcv);
1643 SvREFCNT_dec (coro->args); 1709 SvREFCNT_dec (coro->args);
1710 SvREFCNT_dec (coro->swap_sv);
1644 SvREFCNT_dec (CORO_THROW); 1711 SvREFCNT_dec (CORO_THROW);
1645
1646 if (coro->next) coro->next->prev = coro->prev;
1647 if (coro->prev) coro->prev->next = coro->next;
1648 if (coro == coro_first) coro_first = coro->next;
1649
1650 return 1;
1651} 1712}
1652 1713
1653static int 1714static int
1654coro_state_free (pTHX_ SV *sv, MAGIC *mg) 1715coro_state_free (pTHX_ SV *sv, MAGIC *mg)
1655{ 1716{
1823 { 1884 {
1824 /* nothing to schedule: call the idle handler */ 1885 /* nothing to schedule: call the idle handler */
1825 if (SvROK (sv_idle) 1886 if (SvROK (sv_idle)
1826 && SvOBJECT (SvRV (sv_idle))) 1887 && SvOBJECT (SvRV (sv_idle)))
1827 { 1888 {
1889 if (SvRV (sv_idle) == SvRV (coro_current))
1890 croak ("FATAL: $Coro::IDLE blocked itself - did you try to block inside an event loop callback? Caught");
1891
1828 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ 1892 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1829 api_ready (aTHX_ SvRV (sv_idle)); 1893 api_ready (aTHX_ SvRV (sv_idle));
1830 --coro_nready; 1894 --coro_nready;
1831 } 1895 }
1832 else 1896 else
1949 2013
1950static void 2014static void
1951coro_call_on_destroy (pTHX_ struct coro *coro) 2015coro_call_on_destroy (pTHX_ struct coro *coro)
1952{ 2016{
1953 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);
1954 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
1955 2018
1956 if (on_destroyp) 2019 if (on_destroyp)
1957 { 2020 {
1958 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;
1959 2024
1960 while (AvFILLp (on_destroy) >= 0) 2025 while (AvFILLp (on_destroy) >= 0)
1961 { 2026 {
1962 dSP; /* don't disturb outer sp */ 2027 dSP; /* don't disturb outer sp */
1963 SV *cb = av_pop (on_destroy); 2028 SV *cb = av_pop (on_destroy);
1965 PUSHMARK (SP); 2030 PUSHMARK (SP);
1966 2031
1967 if (statusp) 2032 if (statusp)
1968 { 2033 {
1969 int i; 2034 int i;
1970 AV *status = (AV *)SvRV (*statusp);
1971 EXTEND (SP, AvFILLp (status) + 1); 2035 EXTEND (SP, AvFILLp (status) + 1);
1972 2036
1973 for (i = 0; i <= AvFILLp (status); ++i) 2037 for (i = 0; i <= AvFILLp (status); ++i)
1974 PUSHs (AvARRAY (status)[i]); 2038 PUSHs (AvARRAY (status)[i]);
1975 } 2039 }
1979 } 2043 }
1980 } 2044 }
1981} 2045}
1982 2046
1983static void 2047static void
1984slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2048coro_set_status (pTHX_ HV *coro_hv, SV **arg, int items)
1985{ 2049{
1986 int i;
1987 HV *hv = (HV *)SvRV (coro_current);
1988 AV *av = newAV (); 2050 AV *av = newAV ();
1989 2051
1990 /* items are actually not so common, so optimise for this case */ 2052 /* items are actually not so common, so optimise for this case */
1991 if (items) 2053 if (items)
1992 { 2054 {
2055 int i;
2056
1993 av_extend (av, items - 1); 2057 av_extend (av, items - 1);
1994 2058
1995 for (i = 0; i < items; ++i) 2059 for (i = 0; i < items; ++i)
1996 av_push (av, SvREFCNT_inc_NN (arg [i])); 2060 av_push (av, SvREFCNT_inc_NN (arg [i]));
1997 } 2061 }
1998 2062
1999 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}
2000 2065
2066static void
2067slf_init_terminate_cancel_common (pTHX_ struct CoroSLF *frame, HV *coro_hv)
2068{
2001 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 */
2002 api_ready (aTHX_ sv_manager); 2070 api_ready (aTHX_ sv_manager);
2003 2071
2004 frame->prepare = prepare_schedule; 2072 frame->prepare = prepare_schedule;
2005 frame->check = slf_check_repeat; 2073 frame->check = slf_check_repeat;
2006 2074
2007 /* as a minor optimisation, we could unwind all stacks here */ 2075 /* as a minor optimisation, we could unwind all stacks here */
2008 /* 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 */
2009 /*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;
2010} 2185}
2011 2186
2012/*****************************************************************************/ 2187/*****************************************************************************/
2013/* async pool handler */ 2188/* async pool handler */
2014 2189
2054 coro->saved_deffh = 0; 2229 coro->saved_deffh = 0;
2055 2230
2056 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss) 2231 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2057 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2232 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2058 { 2233 {
2059 coro->invoke_cb = SvREFCNT_inc_NN ((SV *)cv_coro_terminate); 2234 slf_init_terminate_cancel_common (aTHX_ frame, hv);
2060 coro->invoke_av = newAV (); 2235 return;
2061
2062 frame->prepare = prepare_nop;
2063 } 2236 }
2064 else 2237 else
2065 { 2238 {
2066 av_clear (GvAV (PL_defgv)); 2239 av_clear (GvAV (PL_defgv));
2067 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);
2290static void 2463static void
2291slf_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)
2292{ 2465{
2293 frame->prepare = prepare_cede_notself; 2466 frame->prepare = prepare_cede_notself;
2294 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;
2295} 2485}
2296 2486
2297/* 2487/*
2298 * these not obviously related functions are all rolled into one 2488 * these not obviously related functions are all rolled into one
2299 * 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
2418 if (PL_op->op_flags & OPf_STACKED) 2608 if (PL_op->op_flags & OPf_STACKED)
2419 { 2609 {
2420 if (items > slf_arga) 2610 if (items > slf_arga)
2421 { 2611 {
2422 slf_arga = items; 2612 slf_arga = items;
2423 free (slf_argv); 2613 Safefree (slf_argv);
2424 slf_argv = malloc (slf_arga * sizeof (SV *)); 2614 New (0, slf_argv, slf_arga, SV *);
2425 } 2615 }
2426 2616
2427 slf_argc = items; 2617 slf_argc = items;
2428 2618
2429 for (i = 0; i < items; ++i) 2619 for (i = 0; i < items; ++i)
2625 SvREFCNT_dec (cb); 2815 SvREFCNT_dec (cb);
2626 } 2816 }
2627} 2817}
2628 2818
2629static void 2819static void
2630coro_semaphore_on_destroy (pTHX_ struct coro *coro) 2820coro_semaphore_destroy (pTHX_ struct coro *coro)
2631{ 2821{
2632 /* call $sem->adjust (0) to possibly wake up some other waiters */ 2822 /* call $sem->adjust (0) to possibly wake up some other waiters */
2633 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0); 2823 coro_semaphore_adjust (aTHX_ (AV *)coro->slf_frame.data, 0);
2634} 2824}
2635 2825
2642 /* 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 */
2643 if (CORO_THROW) 2833 if (CORO_THROW)
2644 return 0; 2834 return 0;
2645 else if (SvIVX (count_sv) > 0) 2835 else if (SvIVX (count_sv) > 0)
2646 { 2836 {
2647 SvSTATE_current->on_destroy = 0; 2837 frame->destroy = 0;
2648 2838
2649 if (acquire) 2839 if (acquire)
2650 SvIVX (count_sv) = SvIVX (count_sv) - 1; 2840 SvIVX (count_sv) = SvIVX (count_sv) - 1;
2651 else 2841 else
2652 coro_semaphore_adjust (aTHX_ av, 0); 2842 coro_semaphore_adjust (aTHX_ av, 0);
2695 { 2885 {
2696 av_push (av, SvREFCNT_inc (SvRV (coro_current))); 2886 av_push (av, SvREFCNT_inc (SvRV (coro_current)));
2697 2887
2698 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av)); 2888 frame->data = (void *)sv_2mortal (SvREFCNT_inc ((SV *)av));
2699 frame->prepare = prepare_schedule; 2889 frame->prepare = prepare_schedule;
2700
2701 /* to avoid race conditions when a woken-up coro gets terminated */ 2890 /* to avoid race conditions when a woken-up coro gets terminated */
2702 /* we arrange for a temporary on_destroy that calls adjust (0) */ 2891 /* we arrange for a temporary on_destroy that calls adjust (0) */
2703 SvSTATE_current->on_destroy = coro_semaphore_on_destroy; 2892 frame->destroy = coro_semaphore_destroy;
2704 } 2893 }
2705} 2894}
2706 2895
2707static void 2896static void
2708slf_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)
2790 { 2979 {
2791 SV *cb_cv = s_get_cv_croak (arg [1]); 2980 SV *cb_cv = s_get_cv_croak (arg [1]);
2792 av_push (av, SvREFCNT_inc_NN (cb_cv)); 2981 av_push (av, SvREFCNT_inc_NN (cb_cv));
2793 2982
2794 if (SvIVX (AvARRAY (av)[0])) 2983 if (SvIVX (AvARRAY (av)[0]))
2795 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ 2984 coro_signal_wake (aTHX_ av, 1); /* must be the only waiter */
2796 2985
2797 frame->prepare = prepare_nop; 2986 frame->prepare = prepare_nop;
2798 frame->check = slf_check_nop; 2987 frame->check = slf_check_nop;
2799 } 2988 }
2800 else if (SvIVX (AvARRAY (av)[0])) 2989 else if (SvIVX (AvARRAY (av)[0]))
2962 /* now call the AIO function - we assume our request is uncancelable */ 3151 /* now call the AIO function - we assume our request is uncancelable */
2963 PUTBACK; 3152 PUTBACK;
2964 call_sv ((SV *)req, G_VOID | G_DISCARD); 3153 call_sv ((SV *)req, G_VOID | G_DISCARD);
2965 } 3154 }
2966 3155
2967 /* 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 */
2968 frame->data = (void *)state; 3157 frame->data = (void *)state;
2969 frame->prepare = prepare_schedule; 3158 frame->prepare = prepare_schedule;
2970 frame->check = slf_check_aio_req; 3159 frame->check = slf_check_aio_req;
2971} 3160}
2972 3161
3103 coroapi.prepare_nop = prepare_nop; 3292 coroapi.prepare_nop = prepare_nop;
3104 coroapi.prepare_schedule = prepare_schedule; 3293 coroapi.prepare_schedule = prepare_schedule;
3105 coroapi.prepare_cede = prepare_cede; 3294 coroapi.prepare_cede = prepare_cede;
3106 coroapi.prepare_cede_notself = prepare_cede_notself; 3295 coroapi.prepare_cede_notself = prepare_cede_notself;
3107 3296
3108 { 3297 time_init (aTHX);
3109 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
3110
3111 if (!svp) croak ("Time::HiRes is required");
3112 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
3113
3114 nvtime = INT2PTR (double (*)(), SvIV (*svp));
3115
3116 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0);
3117 u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp));
3118 }
3119 3298
3120 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3299 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
3121} 3300}
3122 3301
3123SV * 3302SV *
3132void 3311void
3133transfer (...) 3312transfer (...)
3134 PROTOTYPE: $$ 3313 PROTOTYPE: $$
3135 CODE: 3314 CODE:
3136 CORO_EXECUTE_SLF_XS (slf_init_transfer); 3315 CORO_EXECUTE_SLF_XS (slf_init_transfer);
3137
3138bool
3139_destroy (SV *coro_sv)
3140 CODE:
3141 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
3142 OUTPUT:
3143 RETVAL
3144 3316
3145void 3317void
3146_exit (int code) 3318_exit (int code)
3147 PROTOTYPE: $ 3319 PROTOTYPE: $
3148 CODE: 3320 CODE:
3224 CODE: 3396 CODE:
3225{ 3397{
3226 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3398 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
3227 { 3399 {
3228 struct coro *current = SvSTATE_current; 3400 struct coro *current = SvSTATE_current;
3401 struct CoroSLF slf_save;
3229 3402
3230 if (current != coro) 3403 if (current != coro)
3231 { 3404 {
3232 PUTBACK; 3405 PUTBACK;
3233 save_perl (aTHX_ current); 3406 save_perl (aTHX_ current);
3234 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;
3235 SPAGAIN; 3417 SPAGAIN;
3236 } 3418 }
3237 3419
3238 PUSHSTACK; 3420 PUSHSTACK;
3239 3421
3249 SPAGAIN; 3431 SPAGAIN;
3250 3432
3251 if (current != coro) 3433 if (current != coro)
3252 { 3434 {
3253 PUTBACK; 3435 PUTBACK;
3436 slf_frame = slf_save;
3254 save_perl (aTHX_ coro); 3437 save_perl (aTHX_ coro);
3255 load_perl (aTHX_ current); 3438 load_perl (aTHX_ current);
3256 SPAGAIN; 3439 SPAGAIN;
3257 } 3440 }
3258 } 3441 }
3271 RETVAL = boolSV (coro->flags & ix); 3454 RETVAL = boolSV (coro->flags & ix);
3272 OUTPUT: 3455 OUTPUT:
3273 RETVAL 3456 RETVAL
3274 3457
3275void 3458void
3276throw (Coro::State self, SV *throw = &PL_sv_undef) 3459throw (Coro::State self, SV *exception = &PL_sv_undef)
3277 PROTOTYPE: $;$ 3460 PROTOTYPE: $;$
3278 CODE: 3461 CODE:
3279{ 3462{
3280 struct coro *current = SvSTATE_current; 3463 struct coro *current = SvSTATE_current;
3281 SV **throwp = self == current ? &CORO_THROW : &self->except; 3464 SV **exceptionp = self == current ? &CORO_THROW : &self->except;
3282 SvREFCNT_dec (*throwp); 3465 SvREFCNT_dec (*exceptionp);
3283 SvGETMAGIC (throw); 3466 SvGETMAGIC (exception);
3284 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 3467 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0;
3285} 3468}
3286 3469
3287void 3470void
3288api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3471api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3289 PROTOTYPE: $;$ 3472 PROTOTYPE: $;$
3344 3527
3345void 3528void
3346cancel (Coro::State self) 3529cancel (Coro::State self)
3347 CODE: 3530 CODE:
3348 coro_state_destroy (aTHX_ self); 3531 coro_state_destroy (aTHX_ self);
3349 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3350
3351 3532
3352SV * 3533SV *
3353enable_times (int enabled = enable_times) 3534enable_times (int enabled = enable_times)
3354 CODE: 3535 CODE:
3355{ 3536{
3411BOOT: 3592BOOT:
3412{ 3593{
3413 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3594 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3414 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3595 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3415 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); 3596 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3416 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3417 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);
3418 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3598 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3419 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3599 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3420 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3600 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3421 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); 3601 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3460 api_ready (aTHX_ RETVAL); 3640 api_ready (aTHX_ RETVAL);
3461 OUTPUT: 3641 OUTPUT:
3462 RETVAL 3642 RETVAL
3463 3643
3464void 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
3465terminate (...) 3652terminate (...)
3466 CODE: 3653 CODE:
3467 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
3468 3664
3469void 3665void
3470schedule (...) 3666schedule (...)
3471 CODE: 3667 CODE:
3472 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3668 CORO_EXECUTE_SLF_XS (slf_init_schedule);
3869 { 4065 {
3870 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; 4066 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect;
3871 coro_old_pp_sselect = 0; 4067 coro_old_pp_sselect = 0;
3872 } 4068 }
3873 4069
3874

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines