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.392 by root, Fri Apr 29 15:43:27 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
237 CV *startcv; /* the CV to execute */ 241 CV *startcv; /* the CV to execute */
238 AV *args; /* data associated with this coroutine (initial args) */ 242 AV *args; /* data associated with this coroutine (initial args) */
239 int refcnt; /* coroutines are refcounted, yes */ 243 int refcnt; /* coroutines are refcounted, yes */
240 int flags; /* CF_ flags */ 244 int flags; /* CF_ flags */
241 HV *hv; /* the perl hash associated with this coro, if any */ 245 HV *hv; /* the perl hash associated with this coro, if any */
242 void (*on_destroy)(pTHX_ struct coro *coro); 246 void (*on_destroy)(pTHX_ struct coro *coro); /* for temporary use by xs in critical sections */
243 247
244 /* statistics */ 248 /* statistics */
245 int usecount; /* number of transfers to this coro */ 249 int usecount; /* number of transfers to this coro */
246 250
247 /* coro process data */ 251 /* coro process data */
310 /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */ 314 /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */
311 PL_op->op_flags |= OPf_STACKED; 315 PL_op->op_flags |= OPf_STACKED;
312 PL_op->op_private = 0; 316 PL_op->op_private = 0;
313 return PL_ppaddr [OP_ENTERSUB](aTHX); 317 return PL_ppaddr [OP_ENTERSUB](aTHX);
314} 318}
319
320/** time stuff **************************************************************/
321
322#ifdef HAS_GETTIMEOFDAY
323
324static void
325coro_u2time (pTHX_ UV ret[2])
326{
327 struct timeval tv;
328 gettimeofday (&tv, 0);
329
330 ret [0] = tv.tv_sec;
331 ret [1] = tv.tv_usec;
332}
333
334static double
335coro_nvtime ()
336{
337 struct timeval tv;
338 gettimeofday (&tv, 0);
339
340 return tv.tv_sec + tv.tv_usec * 1e-6;
341}
342
343static void
344time_init (pTHX)
345{
346 nvtime = coro_nvtime;
347 u2time = coro_u2time;
348}
349
350#else
351
352static void
353time_init (pTHX)
354{
355 SV **svp;
356
357 require_pv ("Time/HiRes.pm");
358
359 svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
360
361 if (!svp) croak ("Time::HiRes is required, but missing. Caught");
362 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught");
363
364 nvtime = INT2PTR (double (*)(), SvIV (*svp));
365
366 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0);
367 u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp));
368}
369
370#endif
315 371
316/** lowlevel stuff **********************************************************/ 372/** lowlevel stuff **********************************************************/
317 373
318static SV * 374static SV *
319coro_get_sv (pTHX_ const char *name, int create) 375coro_get_sv (pTHX_ const char *name, int create)
583 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); 639 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
584 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); 640 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
585 641
586#if PERL_VERSION_ATLEAST (5,10,0) 642#if PERL_VERSION_ATLEAST (5,10,0)
587 /* perl 5.10 complicates this _quite_ a bit, but it also is 643 /* perl 5.10 complicates this _quite_ a bit, but it also is
588 * is much faster, so no quarrels here. alternatively, we could 644 * much faster, so no quarrels here. alternatively, we could
589 * sv_upgrade to avoid this. 645 * sv_upgrade to avoid this.
590 */ 646 */
591 { 647 {
592 /* swap sv_u */ 648 /* swap sv_u */
593 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; 649 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
718 { 774 {
719 CV *cv = cx->blk_sub.cv; 775 CV *cv = cx->blk_sub.cv;
720 776
721 if (expect_true (CvDEPTH (cv))) 777 if (expect_true (CvDEPTH (cv)))
722 { 778 {
779 EXTEND (SP, 3);
723 PUSHs ((SV *)CvPADLIST (cv)); 780 PUSHs ((SV *)CvPADLIST (cv));
724 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv))); 781 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv)));
725 PUSHs ((SV *)cv); 782 PUSHs ((SV *)cv);
726 783
727 CvDEPTH (cv) = 0; 784 CvDEPTH (cv) = 0;
1009 PL_comppad_name_fill = 0; 1066 PL_comppad_name_fill = 0;
1010 PL_comppad_name_floor = 0; 1067 PL_comppad_name_floor = 0;
1011 PL_curpm = 0; 1068 PL_curpm = 0;
1012 PL_curpad = 0; 1069 PL_curpad = 0;
1013 PL_localizing = 0; 1070 PL_localizing = 0;
1014 PL_dirty = 0;
1015 PL_restartop = 0; 1071 PL_restartop = 0;
1016#if PERL_VERSION_ATLEAST (5,10,0) 1072#if PERL_VERSION_ATLEAST (5,10,0)
1017 PL_parser = 0; 1073 PL_parser = 0;
1018#endif 1074#endif
1019 PL_hints = 0; 1075 PL_hints = 0;
1135 1191
1136 SvREFCNT_dec (coro->saved_deffh); 1192 SvREFCNT_dec (coro->saved_deffh);
1137 SvREFCNT_dec (coro->rouse_cb); 1193 SvREFCNT_dec (coro->rouse_cb);
1138 SvREFCNT_dec (coro->invoke_cb); 1194 SvREFCNT_dec (coro->invoke_cb);
1139 SvREFCNT_dec (coro->invoke_av); 1195 SvREFCNT_dec (coro->invoke_av);
1140 SvREFCNT_dec (coro->swap_sv);
1141 } 1196 }
1142} 1197}
1143 1198
1144INLINE void 1199INLINE void
1145free_coro_mortal (pTHX) 1200free_coro_mortal (pTHX)
1636 && coro->mainstack != main_mainstack 1691 && coro->mainstack != main_mainstack
1637 && coro->slot 1692 && coro->slot
1638 && !PL_dirty) 1693 && !PL_dirty)
1639 destroy_perl (aTHX_ coro); 1694 destroy_perl (aTHX_ coro);
1640 1695
1696 if (coro->next) coro->next->prev = coro->prev;
1697 if (coro->prev) coro->prev->next = coro->next;
1698 if (coro == coro_first) coro_first = coro->next;
1699
1641 cctx_destroy (coro->cctx); 1700 cctx_destroy (coro->cctx);
1642 SvREFCNT_dec (coro->startcv); 1701 SvREFCNT_dec (coro->startcv);
1643 SvREFCNT_dec (coro->args); 1702 SvREFCNT_dec (coro->args);
1703 SvREFCNT_dec (coro->swap_sv);
1644 SvREFCNT_dec (CORO_THROW); 1704 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 1705
1650 return 1; 1706 return 1;
1651} 1707}
1652 1708
1653static int 1709static int
1823 { 1879 {
1824 /* nothing to schedule: call the idle handler */ 1880 /* nothing to schedule: call the idle handler */
1825 if (SvROK (sv_idle) 1881 if (SvROK (sv_idle)
1826 && SvOBJECT (SvRV (sv_idle))) 1882 && SvOBJECT (SvRV (sv_idle)))
1827 { 1883 {
1884 if (SvRV (sv_idle) == SvRV (coro_current))
1885 croak ("FATAL: $Coro::IDLE blocked itself - did you try to block inside an event loop callback? Caught");
1886
1828 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ 1887 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1829 api_ready (aTHX_ SvRV (sv_idle)); 1888 api_ready (aTHX_ SvRV (sv_idle));
1830 --coro_nready; 1889 --coro_nready;
1831 } 1890 }
1832 else 1891 else
1949 2008
1950static void 2009static void
1951coro_call_on_destroy (pTHX_ struct coro *coro) 2010coro_call_on_destroy (pTHX_ struct coro *coro)
1952{ 2011{
1953 SV **on_destroyp = hv_fetch (coro->hv, "_on_destroy", sizeof ("_on_destroy") - 1, 0); 2012 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 2013
1956 if (on_destroyp) 2014 if (on_destroyp)
1957 { 2015 {
1958 AV *on_destroy = (AV *)SvRV (*on_destroyp); 2016 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
2017 AV *on_destroy = sv_2mortal (SvREFCNT_inc ((AV *)SvRV (*on_destroyp)));
2018 AV *status = statusp ? sv_2mortal (SvREFCNT_inc ((AV *)SvRV (*statusp))) : 0;
1959 2019
1960 while (AvFILLp (on_destroy) >= 0) 2020 while (AvFILLp (on_destroy) >= 0)
1961 { 2021 {
1962 dSP; /* don't disturb outer sp */ 2022 dSP; /* don't disturb outer sp */
1963 SV *cb = av_pop (on_destroy); 2023 SV *cb = av_pop (on_destroy);
1965 PUSHMARK (SP); 2025 PUSHMARK (SP);
1966 2026
1967 if (statusp) 2027 if (statusp)
1968 { 2028 {
1969 int i; 2029 int i;
1970 AV *status = (AV *)SvRV (*statusp);
1971 EXTEND (SP, AvFILLp (status) + 1); 2030 EXTEND (SP, AvFILLp (status) + 1);
1972 2031
1973 for (i = 0; i <= AvFILLp (status); ++i) 2032 for (i = 0; i <= AvFILLp (status); ++i)
1974 PUSHs (AvARRAY (status)[i]); 2033 PUSHs (AvARRAY (status)[i]);
1975 } 2034 }
1979 } 2038 }
1980 } 2039 }
1981} 2040}
1982 2041
1983static void 2042static void
1984slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2043coro_set_status (HV *coro_hv, SV **arg, int items)
1985{ 2044{
1986 int i;
1987 HV *hv = (HV *)SvRV (coro_current);
1988 AV *av = newAV (); 2045 AV *av = newAV ();
1989 2046
1990 /* items are actually not so common, so optimise for this case */ 2047 /* items are actually not so common, so optimise for this case */
1991 if (items) 2048 if (items)
1992 { 2049 {
2050 int i;
2051
1993 av_extend (av, items - 1); 2052 av_extend (av, items - 1);
1994 2053
1995 for (i = 0; i < items; ++i) 2054 for (i = 0; i < items; ++i)
1996 av_push (av, SvREFCNT_inc_NN (arg [i])); 2055 av_push (av, SvREFCNT_inc_NN (arg [i]));
1997 } 2056 }
1998 2057
1999 hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); 2058 hv_store (coro_hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0);
2059}
2000 2060
2061static void
2062slf_init_terminate_cancel_common (pTHX_ struct CoroSLF *frame, HV *coro_hv)
2063{
2001 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 2064 av_push (av_destroy, (SV *)newRV_inc ((SV *)coro_hv)); /* RVinc for perl */
2002 api_ready (aTHX_ sv_manager); 2065 api_ready (aTHX_ sv_manager);
2003 2066
2004 frame->prepare = prepare_schedule; 2067 frame->prepare = prepare_schedule;
2005 frame->check = slf_check_repeat; 2068 frame->check = slf_check_repeat;
2006 2069
2007 /* as a minor optimisation, we could unwind all stacks here */ 2070 /* as a minor optimisation, we could unwind all stacks here */
2008 /* but that puts extra pressure on pp_slf, and is not worth much */ 2071 /* but that puts extra pressure on pp_slf, and is not worth much */
2009 /*coro_unwind_stacks (aTHX);*/ 2072 /*coro_unwind_stacks (aTHX);*/
2073}
2074
2075static void
2076slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2077{
2078 HV *coro_hv = (HV *)SvRV (coro_current);
2079
2080 coro_set_status (coro_hv, arg, items);
2081 slf_init_terminate_cancel_common (frame, coro_hv);
2082}
2083
2084static void
2085slf_init_cancel (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2086{
2087 HV *coro_hv;
2088 struct coro *coro;
2089
2090 if (items <= 0)
2091 croak ("Coro::cancel called without coro object,");
2092
2093 coro = SvSTATE (arg [0]);
2094 coro_hv = coro->hv;
2095
2096 coro_set_status (coro_hv, arg + 1, items - 1);
2097
2098 /* cancelling the current coro is allowed, and equals terminate */
2099 if (coro_hv == (HV *)SvRV (coro_current))
2100 slf_init_terminate_cancel_common (frame, coro_hv);
2101 else
2102 {
2103 /* otherwise we cancel ourselves */
2104 coro_state_destroy (aTHX_ coro);
2105 coro_call_on_destroy (aTHX_ coro);
2106
2107 frame->prepare = prepare_nop;
2108 frame->check = slf_check_nop;
2109 }
2010} 2110}
2011 2111
2012/*****************************************************************************/ 2112/*****************************************************************************/
2013/* async pool handler */ 2113/* async pool handler */
2014 2114
2418 if (PL_op->op_flags & OPf_STACKED) 2518 if (PL_op->op_flags & OPf_STACKED)
2419 { 2519 {
2420 if (items > slf_arga) 2520 if (items > slf_arga)
2421 { 2521 {
2422 slf_arga = items; 2522 slf_arga = items;
2423 free (slf_argv); 2523 Safefree (slf_argv);
2424 slf_argv = malloc (slf_arga * sizeof (SV *)); 2524 New (0, slf_argv, slf_arga, SV *);
2425 } 2525 }
2426 2526
2427 slf_argc = items; 2527 slf_argc = items;
2428 2528
2429 for (i = 0; i < items; ++i) 2529 for (i = 0; i < items; ++i)
2790 { 2890 {
2791 SV *cb_cv = s_get_cv_croak (arg [1]); 2891 SV *cb_cv = s_get_cv_croak (arg [1]);
2792 av_push (av, SvREFCNT_inc_NN (cb_cv)); 2892 av_push (av, SvREFCNT_inc_NN (cb_cv));
2793 2893
2794 if (SvIVX (AvARRAY (av)[0])) 2894 if (SvIVX (AvARRAY (av)[0]))
2795 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ 2895 coro_signal_wake (aTHX_ av, 1); /* must be the only waiter */
2796 2896
2797 frame->prepare = prepare_nop; 2897 frame->prepare = prepare_nop;
2798 frame->check = slf_check_nop; 2898 frame->check = slf_check_nop;
2799 } 2899 }
2800 else if (SvIVX (AvARRAY (av)[0])) 2900 else if (SvIVX (AvARRAY (av)[0]))
2962 /* now call the AIO function - we assume our request is uncancelable */ 3062 /* now call the AIO function - we assume our request is uncancelable */
2963 PUTBACK; 3063 PUTBACK;
2964 call_sv ((SV *)req, G_VOID | G_DISCARD); 3064 call_sv ((SV *)req, G_VOID | G_DISCARD);
2965 } 3065 }
2966 3066
2967 /* now that the requets is going, we loop toll we have a result */ 3067 /* now that the request is going, we loop till we have a result */
2968 frame->data = (void *)state; 3068 frame->data = (void *)state;
2969 frame->prepare = prepare_schedule; 3069 frame->prepare = prepare_schedule;
2970 frame->check = slf_check_aio_req; 3070 frame->check = slf_check_aio_req;
2971} 3071}
2972 3072
3103 coroapi.prepare_nop = prepare_nop; 3203 coroapi.prepare_nop = prepare_nop;
3104 coroapi.prepare_schedule = prepare_schedule; 3204 coroapi.prepare_schedule = prepare_schedule;
3105 coroapi.prepare_cede = prepare_cede; 3205 coroapi.prepare_cede = prepare_cede;
3106 coroapi.prepare_cede_notself = prepare_cede_notself; 3206 coroapi.prepare_cede_notself = prepare_cede_notself;
3107 3207
3108 { 3208 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 3209
3120 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3210 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
3121} 3211}
3122 3212
3123SV * 3213SV *
3132void 3222void
3133transfer (...) 3223transfer (...)
3134 PROTOTYPE: $$ 3224 PROTOTYPE: $$
3135 CODE: 3225 CODE:
3136 CORO_EXECUTE_SLF_XS (slf_init_transfer); 3226 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 3227
3145void 3228void
3146_exit (int code) 3229_exit (int code)
3147 PROTOTYPE: $ 3230 PROTOTYPE: $
3148 CODE: 3231 CODE:
3271 RETVAL = boolSV (coro->flags & ix); 3354 RETVAL = boolSV (coro->flags & ix);
3272 OUTPUT: 3355 OUTPUT:
3273 RETVAL 3356 RETVAL
3274 3357
3275void 3358void
3276throw (Coro::State self, SV *throw = &PL_sv_undef) 3359throw (Coro::State self, SV *exception = &PL_sv_undef)
3277 PROTOTYPE: $;$ 3360 PROTOTYPE: $;$
3278 CODE: 3361 CODE:
3279{ 3362{
3280 struct coro *current = SvSTATE_current; 3363 struct coro *current = SvSTATE_current;
3281 SV **throwp = self == current ? &CORO_THROW : &self->except; 3364 SV **exceptionp = self == current ? &CORO_THROW : &self->except;
3282 SvREFCNT_dec (*throwp); 3365 SvREFCNT_dec (*exceptionp);
3283 SvGETMAGIC (throw); 3366 SvGETMAGIC (exception);
3284 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 3367 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0;
3285} 3368}
3286 3369
3287void 3370void
3288api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3371api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3289 PROTOTYPE: $;$ 3372 PROTOTYPE: $;$
3344 3427
3345void 3428void
3346cancel (Coro::State self) 3429cancel (Coro::State self)
3347 CODE: 3430 CODE:
3348 coro_state_destroy (aTHX_ self); 3431 coro_state_destroy (aTHX_ self);
3349 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3350
3351 3432
3352SV * 3433SV *
3353enable_times (int enabled = enable_times) 3434enable_times (int enabled = enable_times)
3354 CODE: 3435 CODE:
3355{ 3436{
3460 api_ready (aTHX_ RETVAL); 3541 api_ready (aTHX_ RETVAL);
3461 OUTPUT: 3542 OUTPUT:
3462 RETVAL 3543 RETVAL
3463 3544
3464void 3545void
3546_destroy (Coro::State coro)
3547 CODE:
3548 /* used by the manager thread */
3549 coro_state_destroy (aTHX_ coro);
3550 coro_call_on_destroy (aTHX_ coro);
3551
3552void
3465terminate (...) 3553terminate (...)
3466 CODE: 3554 CODE:
3467 CORO_EXECUTE_SLF_XS (slf_init_terminate); 3555 CORO_EXECUTE_SLF_XS (slf_init_terminate);
3556
3557void
3558cancel (...)
3559 CODE:
3560 CORO_EXECUTE_SLF_XS (slf_init_cancel);
3468 3561
3469void 3562void
3470schedule (...) 3563schedule (...)
3471 CODE: 3564 CODE:
3472 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3565 CORO_EXECUTE_SLF_XS (slf_init_schedule);
3869 { 3962 {
3870 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; 3963 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect;
3871 coro_old_pp_sselect = 0; 3964 coro_old_pp_sselect = 0;
3872 } 3965 }
3873 3966
3874

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines