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.386 by root, Sat Feb 19 06:51:23 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
319/** time stuff **************************************************************/ 320/** time stuff **************************************************************/
320 321
321#ifdef HAS_GETTIMEOFDAY 322#ifdef HAS_GETTIMEOFDAY
322 323
323static void 324static void
324coro_u2time (aTHX_ UV ret[2]) 325coro_u2time (pTHX_ UV ret[2])
325{ 326{
326 struct timeval tv; 327 struct timeval tv;
327 gettimeofday (&tv, 0); 328 gettimeofday (&tv, 0);
328 329
329 ret [0] = tv.tv_sec; 330 ret [0] = tv.tv_sec;
338 339
339 return tv.tv_sec + tv.tv_usec * 1e-6; 340 return tv.tv_sec + tv.tv_usec * 1e-6;
340} 341}
341 342
342static void 343static void
343time_init (void) 344time_init (pTHX)
344{ 345{
345 nvtime = coro_nvtime; 346 nvtime = coro_nvtime;
346 u2time = coro_u2time; 347 u2time = coro_u2time;
347} 348}
348 349
349#else 350#else
350 351
351static void 352static void
352time_init (void) 353time_init (pTHX)
353{ 354{
354 SV **svp; 355 SV **svp;
355 356
356 require_pv ("Time::HiRes"); 357 require_pv ("Time/HiRes.pm");
357 358
358 svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); 359 svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
359 360
360 if (!svp) croak ("Time::HiRes is required"); 361 if (!svp) croak ("Time::HiRes is required, but missing. Caught");
361 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); 362 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught");
362 363
363 nvtime = INT2PTR (double (*)(), SvIV (*svp)); 364 nvtime = INT2PTR (double (*)(), SvIV (*svp));
364 365
365 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0); 366 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0);
366 u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp)); 367 u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp));
2007 2008
2008static void 2009static void
2009coro_call_on_destroy (pTHX_ struct coro *coro) 2010coro_call_on_destroy (pTHX_ struct coro *coro)
2010{ 2011{
2011 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);
2012 SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0);
2013 2013
2014 if (on_destroyp) 2014 if (on_destroyp)
2015 { 2015 {
2016 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;
2017 2019
2018 while (AvFILLp (on_destroy) >= 0) 2020 while (AvFILLp (on_destroy) >= 0)
2019 { 2021 {
2020 dSP; /* don't disturb outer sp */ 2022 dSP; /* don't disturb outer sp */
2021 SV *cb = av_pop (on_destroy); 2023 SV *cb = av_pop (on_destroy);
2023 PUSHMARK (SP); 2025 PUSHMARK (SP);
2024 2026
2025 if (statusp) 2027 if (statusp)
2026 { 2028 {
2027 int i; 2029 int i;
2028 AV *status = (AV *)SvRV (*statusp);
2029 EXTEND (SP, AvFILLp (status) + 1); 2030 EXTEND (SP, AvFILLp (status) + 1);
2030 2031
2031 for (i = 0; i <= AvFILLp (status); ++i) 2032 for (i = 0; i <= AvFILLp (status); ++i)
2032 PUSHs (AvARRAY (status)[i]); 2033 PUSHs (AvARRAY (status)[i]);
2033 } 2034 }
2037 } 2038 }
2038 } 2039 }
2039} 2040}
2040 2041
2041static void 2042static void
2042slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2043coro_set_status (HV *coro_hv, SV **arg, int items)
2043{ 2044{
2044 int i;
2045 HV *hv = (HV *)SvRV (coro_current);
2046 AV *av = newAV (); 2045 AV *av = newAV ();
2047 2046
2048 /* items are actually not so common, so optimise for this case */ 2047 /* items are actually not so common, so optimise for this case */
2049 if (items) 2048 if (items)
2050 { 2049 {
2050 int i;
2051
2051 av_extend (av, items - 1); 2052 av_extend (av, items - 1);
2052 2053
2053 for (i = 0; i < items; ++i) 2054 for (i = 0; i < items; ++i)
2054 av_push (av, SvREFCNT_inc_NN (arg [i])); 2055 av_push (av, SvREFCNT_inc_NN (arg [i]));
2055 } 2056 }
2056 2057
2057 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}
2058 2060
2061static void
2062slf_init_terminate_cancel_common (pTHX_ struct CoroSLF *frame, HV *coro_hv)
2063{
2059 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 */
2060 api_ready (aTHX_ sv_manager); 2065 api_ready (aTHX_ sv_manager);
2061 2066
2062 frame->prepare = prepare_schedule; 2067 frame->prepare = prepare_schedule;
2063 frame->check = slf_check_repeat; 2068 frame->check = slf_check_repeat;
2064 2069
2065 /* as a minor optimisation, we could unwind all stacks here */ 2070 /* as a minor optimisation, we could unwind all stacks here */
2066 /* 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 */
2067 /*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 }
2068} 2110}
2069 2111
2070/*****************************************************************************/ 2112/*****************************************************************************/
2071/* async pool handler */ 2113/* async pool handler */
2072 2114
3161 coroapi.prepare_nop = prepare_nop; 3203 coroapi.prepare_nop = prepare_nop;
3162 coroapi.prepare_schedule = prepare_schedule; 3204 coroapi.prepare_schedule = prepare_schedule;
3163 coroapi.prepare_cede = prepare_cede; 3205 coroapi.prepare_cede = prepare_cede;
3164 coroapi.prepare_cede_notself = prepare_cede_notself; 3206 coroapi.prepare_cede_notself = prepare_cede_notself;
3165 3207
3166 time_init (); 3208 time_init (aTHX);
3167 3209
3168 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3210 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
3169} 3211}
3170 3212
3171SV * 3213SV *
3180void 3222void
3181transfer (...) 3223transfer (...)
3182 PROTOTYPE: $$ 3224 PROTOTYPE: $$
3183 CODE: 3225 CODE:
3184 CORO_EXECUTE_SLF_XS (slf_init_transfer); 3226 CORO_EXECUTE_SLF_XS (slf_init_transfer);
3185
3186bool
3187_destroy (SV *coro_sv)
3188 CODE:
3189 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
3190 OUTPUT:
3191 RETVAL
3192 3227
3193void 3228void
3194_exit (int code) 3229_exit (int code)
3195 PROTOTYPE: $ 3230 PROTOTYPE: $
3196 CODE: 3231 CODE:
3392 3427
3393void 3428void
3394cancel (Coro::State self) 3429cancel (Coro::State self)
3395 CODE: 3430 CODE:
3396 coro_state_destroy (aTHX_ self); 3431 coro_state_destroy (aTHX_ self);
3397 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3398
3399 3432
3400SV * 3433SV *
3401enable_times (int enabled = enable_times) 3434enable_times (int enabled = enable_times)
3402 CODE: 3435 CODE:
3403{ 3436{
3508 api_ready (aTHX_ RETVAL); 3541 api_ready (aTHX_ RETVAL);
3509 OUTPUT: 3542 OUTPUT:
3510 RETVAL 3543 RETVAL
3511 3544
3512void 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
3513terminate (...) 3553terminate (...)
3514 CODE: 3554 CODE:
3515 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);
3516 3561
3517void 3562void
3518schedule (...) 3563schedule (...)
3519 CODE: 3564 CODE:
3520 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