|
|
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 | |
|
|
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."); |
|
|
362 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
|
|
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 | |
318 | static SV * |
374 | static SV * |
319 | coro_get_sv (pTHX_ const char *name, int create) |
375 | coro_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; |
… | |
… | |
1010 | PL_comppad_name_fill = 0; |
1066 | PL_comppad_name_fill = 0; |
1011 | PL_comppad_name_floor = 0; |
1067 | PL_comppad_name_floor = 0; |
1012 | PL_curpm = 0; |
1068 | PL_curpm = 0; |
1013 | PL_curpad = 0; |
1069 | PL_curpad = 0; |
1014 | PL_localizing = 0; |
1070 | PL_localizing = 0; |
1015 | PL_dirty = 0; |
|
|
1016 | PL_restartop = 0; |
1071 | PL_restartop = 0; |
1017 | #if PERL_VERSION_ATLEAST (5,10,0) |
1072 | #if PERL_VERSION_ATLEAST (5,10,0) |
1018 | PL_parser = 0; |
1073 | PL_parser = 0; |
1019 | #endif |
1074 | #endif |
1020 | PL_hints = 0; |
1075 | PL_hints = 0; |
… | |
… | |
1824 | { |
1879 | { |
1825 | /* nothing to schedule: call the idle handler */ |
1880 | /* nothing to schedule: call the idle handler */ |
1826 | if (SvROK (sv_idle) |
1881 | if (SvROK (sv_idle) |
1827 | && SvOBJECT (SvRV (sv_idle))) |
1882 | && SvOBJECT (SvRV (sv_idle))) |
1828 | { |
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 | |
1829 | ++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 */ |
1830 | api_ready (aTHX_ SvRV (sv_idle)); |
1888 | api_ready (aTHX_ SvRV (sv_idle)); |
1831 | --coro_nready; |
1889 | --coro_nready; |
1832 | } |
1890 | } |
1833 | else |
1891 | else |
… | |
… | |
1950 | |
2008 | |
1951 | static void |
2009 | static void |
1952 | coro_call_on_destroy (pTHX_ struct coro *coro) |
2010 | coro_call_on_destroy (pTHX_ struct coro *coro) |
1953 | { |
2011 | { |
1954 | 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); |
1955 | SV **statusp = hv_fetch (coro->hv, "_status", sizeof ("_status") - 1, 0); |
|
|
1956 | |
2013 | |
1957 | if (on_destroyp) |
2014 | if (on_destroyp) |
1958 | { |
2015 | { |
1959 | 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; |
1960 | |
2019 | |
1961 | while (AvFILLp (on_destroy) >= 0) |
2020 | while (AvFILLp (on_destroy) >= 0) |
1962 | { |
2021 | { |
1963 | dSP; /* don't disturb outer sp */ |
2022 | dSP; /* don't disturb outer sp */ |
1964 | SV *cb = av_pop (on_destroy); |
2023 | SV *cb = av_pop (on_destroy); |
… | |
… | |
1966 | PUSHMARK (SP); |
2025 | PUSHMARK (SP); |
1967 | |
2026 | |
1968 | if (statusp) |
2027 | if (statusp) |
1969 | { |
2028 | { |
1970 | int i; |
2029 | int i; |
1971 | AV *status = (AV *)SvRV (*statusp); |
|
|
1972 | EXTEND (SP, AvFILLp (status) + 1); |
2030 | EXTEND (SP, AvFILLp (status) + 1); |
1973 | |
2031 | |
1974 | for (i = 0; i <= AvFILLp (status); ++i) |
2032 | for (i = 0; i <= AvFILLp (status); ++i) |
1975 | PUSHs (AvARRAY (status)[i]); |
2033 | PUSHs (AvARRAY (status)[i]); |
1976 | } |
2034 | } |
… | |
… | |
2419 | if (PL_op->op_flags & OPf_STACKED) |
2477 | if (PL_op->op_flags & OPf_STACKED) |
2420 | { |
2478 | { |
2421 | if (items > slf_arga) |
2479 | if (items > slf_arga) |
2422 | { |
2480 | { |
2423 | slf_arga = items; |
2481 | slf_arga = items; |
2424 | free (slf_argv); |
2482 | Safefree (slf_argv); |
2425 | slf_argv = malloc (slf_arga * sizeof (SV *)); |
2483 | New (0, slf_argv, slf_arga, SV *); |
2426 | } |
2484 | } |
2427 | |
2485 | |
2428 | slf_argc = items; |
2486 | slf_argc = items; |
2429 | |
2487 | |
2430 | for (i = 0; i < items; ++i) |
2488 | for (i = 0; i < items; ++i) |
… | |
… | |
2791 | { |
2849 | { |
2792 | SV *cb_cv = s_get_cv_croak (arg [1]); |
2850 | SV *cb_cv = s_get_cv_croak (arg [1]); |
2793 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2851 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2794 | |
2852 | |
2795 | if (SvIVX (AvARRAY (av)[0])) |
2853 | if (SvIVX (AvARRAY (av)[0])) |
2796 | coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ |
2854 | coro_signal_wake (aTHX_ av, 1); /* must be the only waiter */ |
2797 | |
2855 | |
2798 | frame->prepare = prepare_nop; |
2856 | frame->prepare = prepare_nop; |
2799 | frame->check = slf_check_nop; |
2857 | frame->check = slf_check_nop; |
2800 | } |
2858 | } |
2801 | else if (SvIVX (AvARRAY (av)[0])) |
2859 | else if (SvIVX (AvARRAY (av)[0])) |
… | |
… | |
2963 | /* now call the AIO function - we assume our request is uncancelable */ |
3021 | /* now call the AIO function - we assume our request is uncancelable */ |
2964 | PUTBACK; |
3022 | PUTBACK; |
2965 | call_sv ((SV *)req, G_VOID | G_DISCARD); |
3023 | call_sv ((SV *)req, G_VOID | G_DISCARD); |
2966 | } |
3024 | } |
2967 | |
3025 | |
2968 | /* now that the requets is going, we loop toll we have a result */ |
3026 | /* now that the request is going, we loop till we have a result */ |
2969 | frame->data = (void *)state; |
3027 | frame->data = (void *)state; |
2970 | frame->prepare = prepare_schedule; |
3028 | frame->prepare = prepare_schedule; |
2971 | frame->check = slf_check_aio_req; |
3029 | frame->check = slf_check_aio_req; |
2972 | } |
3030 | } |
2973 | |
3031 | |
… | |
… | |
3104 | coroapi.prepare_nop = prepare_nop; |
3162 | coroapi.prepare_nop = prepare_nop; |
3105 | coroapi.prepare_schedule = prepare_schedule; |
3163 | coroapi.prepare_schedule = prepare_schedule; |
3106 | coroapi.prepare_cede = prepare_cede; |
3164 | coroapi.prepare_cede = prepare_cede; |
3107 | coroapi.prepare_cede_notself = prepare_cede_notself; |
3165 | coroapi.prepare_cede_notself = prepare_cede_notself; |
3108 | |
3166 | |
3109 | { |
3167 | time_init (aTHX); |
3110 | SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); |
|
|
3111 | |
|
|
3112 | if (!svp) croak ("Time::HiRes is required"); |
|
|
3113 | if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer"); |
|
|
3114 | |
|
|
3115 | nvtime = INT2PTR (double (*)(), SvIV (*svp)); |
|
|
3116 | |
|
|
3117 | svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0); |
|
|
3118 | u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp)); |
|
|
3119 | } |
|
|
3120 | |
3168 | |
3121 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3169 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3122 | } |
3170 | } |
3123 | |
3171 | |
3124 | SV * |
3172 | SV * |
… | |
… | |
3272 | RETVAL = boolSV (coro->flags & ix); |
3320 | RETVAL = boolSV (coro->flags & ix); |
3273 | OUTPUT: |
3321 | OUTPUT: |
3274 | RETVAL |
3322 | RETVAL |
3275 | |
3323 | |
3276 | void |
3324 | void |
3277 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
3325 | throw (Coro::State self, SV *exception = &PL_sv_undef) |
3278 | PROTOTYPE: $;$ |
3326 | PROTOTYPE: $;$ |
3279 | CODE: |
3327 | CODE: |
3280 | { |
3328 | { |
3281 | struct coro *current = SvSTATE_current; |
3329 | struct coro *current = SvSTATE_current; |
3282 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
3330 | SV **exceptionp = self == current ? &CORO_THROW : &self->except; |
3283 | SvREFCNT_dec (*throwp); |
3331 | SvREFCNT_dec (*exceptionp); |
3284 | SvGETMAGIC (throw); |
3332 | SvGETMAGIC (exception); |
3285 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
3333 | *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; |
3286 | } |
3334 | } |
3287 | |
3335 | |
3288 | void |
3336 | void |
3289 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3337 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3290 | PROTOTYPE: $;$ |
3338 | PROTOTYPE: $;$ |
… | |
… | |
3346 | void |
3394 | void |
3347 | cancel (Coro::State self) |
3395 | cancel (Coro::State self) |
3348 | CODE: |
3396 | CODE: |
3349 | coro_state_destroy (aTHX_ self); |
3397 | coro_state_destroy (aTHX_ self); |
3350 | coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */ |
3398 | coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */ |
3351 | |
|
|
3352 | |
3399 | |
3353 | SV * |
3400 | SV * |
3354 | enable_times (int enabled = enable_times) |
3401 | enable_times (int enabled = enable_times) |
3355 | CODE: |
3402 | CODE: |
3356 | { |
3403 | { |
… | |
… | |
3870 | { |
3917 | { |
3871 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
3918 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
3872 | coro_old_pp_sselect = 0; |
3919 | coro_old_pp_sselect = 0; |
3873 | } |
3920 | } |
3874 | |
3921 | |
3875 | |
|
|