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.385 by root, Sat Feb 19 05:42:46 2011 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
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}
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
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)
323{ 376{
1955 2008
1956static void 2009static void
1957coro_call_on_destroy (pTHX_ struct coro *coro) 2010coro_call_on_destroy (pTHX_ struct coro *coro)
1958{ 2011{
1959 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);
1960 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
1961 2013
1962 if (on_destroyp) 2014 if (on_destroyp)
1963 { 2015 {
1964 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;
1965 2019
1966 while (AvFILLp (on_destroy) >= 0) 2020 while (AvFILLp (on_destroy) >= 0)
1967 { 2021 {
1968 dSP; /* don't disturb outer sp */ 2022 dSP; /* don't disturb outer sp */
1969 SV *cb = av_pop (on_destroy); 2023 SV *cb = av_pop (on_destroy);
1971 PUSHMARK (SP); 2025 PUSHMARK (SP);
1972 2026
1973 if (statusp) 2027 if (statusp)
1974 { 2028 {
1975 int i; 2029 int i;
1976 AV *status = (AV *)SvRV (*statusp);
1977 EXTEND (SP, AvFILLp (status) + 1); 2030 EXTEND (SP, AvFILLp (status) + 1);
1978 2031
1979 for (i = 0; i <= AvFILLp (status); ++i) 2032 for (i = 0; i <= AvFILLp (status); ++i)
1980 PUSHs (AvARRAY (status)[i]); 2033 PUSHs (AvARRAY (status)[i]);
1981 } 2034 }
1985 } 2038 }
1986 } 2039 }
1987} 2040}
1988 2041
1989static void 2042static void
1990slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2043coro_set_status (HV *coro_hv, SV **arg, int items)
1991{ 2044{
1992 int i;
1993 HV *hv = (HV *)SvRV (coro_current);
1994 AV *av = newAV (); 2045 AV *av = newAV ();
1995 2046
1996 /* items are actually not so common, so optimise for this case */ 2047 /* items are actually not so common, so optimise for this case */
1997 if (items) 2048 if (items)
1998 { 2049 {
2050 int i;
2051
1999 av_extend (av, items - 1); 2052 av_extend (av, items - 1);
2000 2053
2001 for (i = 0; i < items; ++i) 2054 for (i = 0; i < items; ++i)
2002 av_push (av, SvREFCNT_inc_NN (arg [i])); 2055 av_push (av, SvREFCNT_inc_NN (arg [i]));
2003 } 2056 }
2004 2057
2005 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}
2006 2060
2061static void
2062slf_init_terminate_cancel_common (pTHX_ struct CoroSLF *frame, HV *coro_hv)
2063{
2007 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 */
2008 api_ready (aTHX_ sv_manager); 2065 api_ready (aTHX_ sv_manager);
2009 2066
2010 frame->prepare = prepare_schedule; 2067 frame->prepare = prepare_schedule;
2011 frame->check = slf_check_repeat; 2068 frame->check = slf_check_repeat;
2012 2069
2013 /* as a minor optimisation, we could unwind all stacks here */ 2070 /* as a minor optimisation, we could unwind all stacks here */
2014 /* 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 */
2015 /*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 }
2016} 2110}
2017 2111
2018/*****************************************************************************/ 2112/*****************************************************************************/
2019/* async pool handler */ 2113/* async pool handler */
2020 2114
3109 coroapi.prepare_nop = prepare_nop; 3203 coroapi.prepare_nop = prepare_nop;
3110 coroapi.prepare_schedule = prepare_schedule; 3204 coroapi.prepare_schedule = prepare_schedule;
3111 coroapi.prepare_cede = prepare_cede; 3205 coroapi.prepare_cede = prepare_cede;
3112 coroapi.prepare_cede_notself = prepare_cede_notself; 3206 coroapi.prepare_cede_notself = prepare_cede_notself;
3113 3207
3114 { 3208 time_init (aTHX);
3115 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
3116
3117 if (!svp) croak ("Time::HiRes is required");
3118 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
3119
3120 nvtime = INT2PTR (double (*)(), SvIV (*svp));
3121
3122 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0);
3123 u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp));
3124 }
3125 3209
3126 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3210 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
3127} 3211}
3128 3212
3129SV * 3213SV *
3138void 3222void
3139transfer (...) 3223transfer (...)
3140 PROTOTYPE: $$ 3224 PROTOTYPE: $$
3141 CODE: 3225 CODE:
3142 CORO_EXECUTE_SLF_XS (slf_init_transfer); 3226 CORO_EXECUTE_SLF_XS (slf_init_transfer);
3143
3144bool
3145_destroy (SV *coro_sv)
3146 CODE:
3147 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
3148 OUTPUT:
3149 RETVAL
3150 3227
3151void 3228void
3152_exit (int code) 3229_exit (int code)
3153 PROTOTYPE: $ 3230 PROTOTYPE: $
3154 CODE: 3231 CODE:
3350 3427
3351void 3428void
3352cancel (Coro::State self) 3429cancel (Coro::State self)
3353 CODE: 3430 CODE:
3354 coro_state_destroy (aTHX_ self); 3431 coro_state_destroy (aTHX_ self);
3355 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3356
3357 3432
3358SV * 3433SV *
3359enable_times (int enabled = enable_times) 3434enable_times (int enabled = enable_times)
3360 CODE: 3435 CODE:
3361{ 3436{
3466 api_ready (aTHX_ RETVAL); 3541 api_ready (aTHX_ RETVAL);
3467 OUTPUT: 3542 OUTPUT:
3468 RETVAL 3543 RETVAL
3469 3544
3470void 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
3471terminate (...) 3553terminate (...)
3472 CODE: 3554 CODE:
3473 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);
3474 3561
3475void 3562void
3476schedule (...) 3563schedule (...)
3477 CODE: 3564 CODE:
3478 CORO_EXECUTE_SLF_XS (slf_init_schedule); 3565 CORO_EXECUTE_SLF_XS (slf_init_schedule);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines