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.381 by root, Sat Oct 23 09:28:50 2010 UTC vs.
Revision 1.392 by root, Fri Apr 29 15:43:27 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
240 CV *startcv; /* the CV to execute */ 241 CV *startcv; /* the CV to execute */
241 AV *args; /* data associated with this coroutine (initial args) */ 242 AV *args; /* data associated with this coroutine (initial args) */
242 int refcnt; /* coroutines are refcounted, yes */ 243 int refcnt; /* coroutines are refcounted, yes */
243 int flags; /* CF_ flags */ 244 int flags; /* CF_ flags */
244 HV *hv; /* the perl hash associated with this coro, if any */ 245 HV *hv; /* the perl hash associated with this coro, if any */
245 void (*on_destroy)(pTHX_ struct coro *coro); 246 void (*on_destroy)(pTHX_ struct coro *coro); /* for temporary use by xs in critical sections */
246 247
247 /* statistics */ 248 /* statistics */
248 int usecount; /* number of transfers to this coro */ 249 int usecount; /* number of transfers to this coro */
249 250
250 /* coro process data */ 251 /* coro process data */
313 /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */ 314 /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */
314 PL_op->op_flags |= OPf_STACKED; 315 PL_op->op_flags |= OPf_STACKED;
315 PL_op->op_private = 0; 316 PL_op->op_private = 0;
316 return PL_ppaddr [OP_ENTERSUB](aTHX); 317 return PL_ppaddr [OP_ENTERSUB](aTHX);
317} 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
318 371
319/** lowlevel stuff **********************************************************/ 372/** lowlevel stuff **********************************************************/
320 373
321static SV * 374static SV *
322coro_get_sv (pTHX_ const char *name, int create) 375coro_get_sv (pTHX_ const char *name, int create)
586 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); 639 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
587 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); 640 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
588 641
589#if PERL_VERSION_ATLEAST (5,10,0) 642#if PERL_VERSION_ATLEAST (5,10,0)
590 /* 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
591 * is much faster, so no quarrels here. alternatively, we could 644 * much faster, so no quarrels here. alternatively, we could
592 * sv_upgrade to avoid this. 645 * sv_upgrade to avoid this.
593 */ 646 */
594 { 647 {
595 /* swap sv_u */ 648 /* swap sv_u */
596 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;
1013 PL_comppad_name_fill = 0; 1066 PL_comppad_name_fill = 0;
1014 PL_comppad_name_floor = 0; 1067 PL_comppad_name_floor = 0;
1015 PL_curpm = 0; 1068 PL_curpm = 0;
1016 PL_curpad = 0; 1069 PL_curpad = 0;
1017 PL_localizing = 0; 1070 PL_localizing = 0;
1018 PL_dirty = 0;
1019 PL_restartop = 0; 1071 PL_restartop = 0;
1020#if PERL_VERSION_ATLEAST (5,10,0) 1072#if PERL_VERSION_ATLEAST (5,10,0)
1021 PL_parser = 0; 1073 PL_parser = 0;
1022#endif 1074#endif
1023 PL_hints = 0; 1075 PL_hints = 0;
1956 2008
1957static void 2009static void
1958coro_call_on_destroy (pTHX_ struct coro *coro) 2010coro_call_on_destroy (pTHX_ struct coro *coro)
1959{ 2011{
1960 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);
1961 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
1962 2013
1963 if (on_destroyp) 2014 if (on_destroyp)
1964 { 2015 {
1965 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;
1966 2019
1967 while (AvFILLp (on_destroy) >= 0) 2020 while (AvFILLp (on_destroy) >= 0)
1968 { 2021 {
1969 dSP; /* don't disturb outer sp */ 2022 dSP; /* don't disturb outer sp */
1970 SV *cb = av_pop (on_destroy); 2023 SV *cb = av_pop (on_destroy);
1972 PUSHMARK (SP); 2025 PUSHMARK (SP);
1973 2026
1974 if (statusp) 2027 if (statusp)
1975 { 2028 {
1976 int i; 2029 int i;
1977 AV *status = (AV *)SvRV (*statusp);
1978 EXTEND (SP, AvFILLp (status) + 1); 2030 EXTEND (SP, AvFILLp (status) + 1);
1979 2031
1980 for (i = 0; i <= AvFILLp (status); ++i) 2032 for (i = 0; i <= AvFILLp (status); ++i)
1981 PUSHs (AvARRAY (status)[i]); 2033 PUSHs (AvARRAY (status)[i]);
1982 } 2034 }
1986 } 2038 }
1987 } 2039 }
1988} 2040}
1989 2041
1990static void 2042static void
1991slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2043coro_set_status (HV *coro_hv, SV **arg, int items)
1992{ 2044{
1993 int i;
1994 HV *hv = (HV *)SvRV (coro_current);
1995 AV *av = newAV (); 2045 AV *av = newAV ();
1996 2046
1997 /* items are actually not so common, so optimise for this case */ 2047 /* items are actually not so common, so optimise for this case */
1998 if (items) 2048 if (items)
1999 { 2049 {
2050 int i;
2051
2000 av_extend (av, items - 1); 2052 av_extend (av, items - 1);
2001 2053
2002 for (i = 0; i < items; ++i) 2054 for (i = 0; i < items; ++i)
2003 av_push (av, SvREFCNT_inc_NN (arg [i])); 2055 av_push (av, SvREFCNT_inc_NN (arg [i]));
2004 } 2056 }
2005 2057
2006 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}
2007 2060
2061static void
2062slf_init_terminate_cancel_common (pTHX_ struct CoroSLF *frame, HV *coro_hv)
2063{
2008 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 */
2009 api_ready (aTHX_ sv_manager); 2065 api_ready (aTHX_ sv_manager);
2010 2066
2011 frame->prepare = prepare_schedule; 2067 frame->prepare = prepare_schedule;
2012 frame->check = slf_check_repeat; 2068 frame->check = slf_check_repeat;
2013 2069
2014 /* as a minor optimisation, we could unwind all stacks here */ 2070 /* as a minor optimisation, we could unwind all stacks here */
2015 /* 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 */
2016 /*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 }
2017} 2110}
2018 2111
2019/*****************************************************************************/ 2112/*****************************************************************************/
2020/* async pool handler */ 2113/* async pool handler */
2021 2114
3110 coroapi.prepare_nop = prepare_nop; 3203 coroapi.prepare_nop = prepare_nop;
3111 coroapi.prepare_schedule = prepare_schedule; 3204 coroapi.prepare_schedule = prepare_schedule;
3112 coroapi.prepare_cede = prepare_cede; 3205 coroapi.prepare_cede = prepare_cede;
3113 coroapi.prepare_cede_notself = prepare_cede_notself; 3206 coroapi.prepare_cede_notself = prepare_cede_notself;
3114 3207
3115 { 3208 time_init (aTHX);
3116 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
3117
3118 if (!svp) croak ("Time::HiRes is required");
3119 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
3120
3121 nvtime = INT2PTR (double (*)(), SvIV (*svp));
3122
3123 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0);
3124 u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp));
3125 }
3126 3209
3127 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3210 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
3128} 3211}
3129 3212
3130SV * 3213SV *
3139void 3222void
3140transfer (...) 3223transfer (...)
3141 PROTOTYPE: $$ 3224 PROTOTYPE: $$
3142 CODE: 3225 CODE:
3143 CORO_EXECUTE_SLF_XS (slf_init_transfer); 3226 CORO_EXECUTE_SLF_XS (slf_init_transfer);
3144
3145bool
3146_destroy (SV *coro_sv)
3147 CODE:
3148 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
3149 OUTPUT:
3150 RETVAL
3151 3227
3152void 3228void
3153_exit (int code) 3229_exit (int code)
3154 PROTOTYPE: $ 3230 PROTOTYPE: $
3155 CODE: 3231 CODE:
3351 3427
3352void 3428void
3353cancel (Coro::State self) 3429cancel (Coro::State self)
3354 CODE: 3430 CODE:
3355 coro_state_destroy (aTHX_ self); 3431 coro_state_destroy (aTHX_ self);
3356 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3357
3358 3432
3359SV * 3433SV *
3360enable_times (int enabled = enable_times) 3434enable_times (int enabled = enable_times)
3361 CODE: 3435 CODE:
3362{ 3436{
3467 api_ready (aTHX_ RETVAL); 3541 api_ready (aTHX_ RETVAL);
3468 OUTPUT: 3542 OUTPUT:
3469 RETVAL 3543 RETVAL
3470 3544
3471void 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
3472terminate (...) 3553terminate (...)
3473 CODE: 3554 CODE:
3474 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);
3475 3561
3476void 3562void
3477schedule (...) 3563schedule (...)
3478 CODE: 3564 CODE:
3479 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3565 CORO_EXECUTE_SLF_XS (slf_init_schedule);
3876 { 3962 {
3877 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; 3963 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect;
3878 coro_old_pp_sselect = 0; 3964 coro_old_pp_sselect = 0;
3879 } 3965 }
3880 3966
3881

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines