… | |
… | |
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 | |
|
|
324 | static void |
|
|
325 | coro_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 | |
|
|
334 | static double |
|
|
335 | coro_nvtime () |
|
|
336 | { |
|
|
337 | struct timeval tv; |
|
|
338 | gettimeofday (&tv, 0); |
|
|
339 | |
|
|
340 | return tv.tv_sec + tv.tv_usec * 1e-6; |
|
|
341 | } |
|
|
342 | |
|
|
343 | static void |
|
|
344 | time_init (pTHX) |
|
|
345 | { |
|
|
346 | nvtime = coro_nvtime; |
|
|
347 | u2time = coro_u2time; |
|
|
348 | } |
|
|
349 | |
|
|
350 | #else |
|
|
351 | |
|
|
352 | static void |
|
|
353 | time_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 | |
321 | static SV * |
374 | static SV * |
322 | coro_get_sv (pTHX_ const char *name, int create) |
375 | coro_get_sv (pTHX_ const char *name, int create) |
323 | { |
376 | { |
… | |
… | |
1955 | |
2008 | |
1956 | static void |
2009 | static void |
1957 | coro_call_on_destroy (pTHX_ struct coro *coro) |
2010 | coro_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 | |
1989 | static void |
2042 | static void |
1990 | slf_init_terminate (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2043 | coro_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 | |
|
|
2061 | static void |
|
|
2062 | slf_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 | |
|
|
2075 | static void |
|
|
2076 | slf_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 | |
|
|
2084 | static void |
|
|
2085 | slf_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 | |
3129 | SV * |
3213 | SV * |
… | |
… | |
3138 | void |
3222 | void |
3139 | transfer (...) |
3223 | transfer (...) |
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 | |
|
|
3144 | bool |
|
|
3145 | _destroy (SV *coro_sv) |
|
|
3146 | CODE: |
|
|
3147 | RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); |
|
|
3148 | OUTPUT: |
|
|
3149 | RETVAL |
|
|
3150 | |
3227 | |
3151 | void |
3228 | void |
3152 | _exit (int code) |
3229 | _exit (int code) |
3153 | PROTOTYPE: $ |
3230 | PROTOTYPE: $ |
3154 | CODE: |
3231 | CODE: |
… | |
… | |
3350 | |
3427 | |
3351 | void |
3428 | void |
3352 | cancel (Coro::State self) |
3429 | cancel (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 | |
3358 | SV * |
3433 | SV * |
3359 | enable_times (int enabled = enable_times) |
3434 | enable_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 | |
3470 | void |
3545 | void |
|
|
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 | |
|
|
3552 | void |
3471 | terminate (...) |
3553 | terminate (...) |
3472 | CODE: |
3554 | CODE: |
3473 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
3555 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
|
|
3556 | |
|
|
3557 | void |
|
|
3558 | cancel (...) |
|
|
3559 | CODE: |
|
|
3560 | CORO_EXECUTE_SLF_XS (slf_init_cancel); |
3474 | |
3561 | |
3475 | void |
3562 | void |
3476 | schedule (...) |
3563 | schedule (...) |
3477 | CODE: |
3564 | CODE: |
3478 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |
3565 | CORO_EXECUTE_SLF_XS (slf_init_schedule); |