|
|
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 |
… | |
… | |
237 | CV *startcv; /* the CV to execute */ |
240 | CV *startcv; /* the CV to execute */ |
238 | AV *args; /* data associated with this coroutine (initial args) */ |
241 | AV *args; /* data associated with this coroutine (initial args) */ |
239 | int refcnt; /* coroutines are refcounted, yes */ |
242 | int refcnt; /* coroutines are refcounted, yes */ |
240 | int flags; /* CF_ flags */ |
243 | int flags; /* CF_ flags */ |
241 | HV *hv; /* the perl hash associated with this coro, if any */ |
244 | HV *hv; /* the perl hash associated with this coro, if any */ |
242 | void (*on_destroy)(pTHX_ struct coro *coro); |
245 | void (*on_destroy)(pTHX_ struct coro *coro); /* for temporary use by xs in critical sections */ |
243 | |
246 | |
244 | /* statistics */ |
247 | /* statistics */ |
245 | int usecount; /* number of transfers to this coro */ |
248 | int usecount; /* number of transfers to this coro */ |
246 | |
249 | |
247 | /* coro process data */ |
250 | /* coro process data */ |
… | |
… | |
583 | SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); |
586 | SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); |
584 | SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); |
587 | SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); |
585 | |
588 | |
586 | #if PERL_VERSION_ATLEAST (5,10,0) |
589 | #if PERL_VERSION_ATLEAST (5,10,0) |
587 | /* perl 5.10 complicates this _quite_ a bit, but it also is |
590 | /* perl 5.10 complicates this _quite_ a bit, but it also is |
588 | * is much faster, so no quarrels here. alternatively, we could |
591 | * much faster, so no quarrels here. alternatively, we could |
589 | * sv_upgrade to avoid this. |
592 | * sv_upgrade to avoid this. |
590 | */ |
593 | */ |
591 | { |
594 | { |
592 | /* swap sv_u */ |
595 | /* swap sv_u */ |
593 | tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; |
596 | tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; |
… | |
… | |
718 | { |
721 | { |
719 | CV *cv = cx->blk_sub.cv; |
722 | CV *cv = cx->blk_sub.cv; |
720 | |
723 | |
721 | if (expect_true (CvDEPTH (cv))) |
724 | if (expect_true (CvDEPTH (cv))) |
722 | { |
725 | { |
|
|
726 | EXTEND (SP, 3); |
723 | PUSHs ((SV *)CvPADLIST (cv)); |
727 | PUSHs ((SV *)CvPADLIST (cv)); |
724 | PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv))); |
728 | PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv))); |
725 | PUSHs ((SV *)cv); |
729 | PUSHs ((SV *)cv); |
726 | |
730 | |
727 | CvDEPTH (cv) = 0; |
731 | CvDEPTH (cv) = 0; |
… | |
… | |
1009 | PL_comppad_name_fill = 0; |
1013 | PL_comppad_name_fill = 0; |
1010 | PL_comppad_name_floor = 0; |
1014 | PL_comppad_name_floor = 0; |
1011 | PL_curpm = 0; |
1015 | PL_curpm = 0; |
1012 | PL_curpad = 0; |
1016 | PL_curpad = 0; |
1013 | PL_localizing = 0; |
1017 | PL_localizing = 0; |
1014 | PL_dirty = 0; |
|
|
1015 | PL_restartop = 0; |
1018 | PL_restartop = 0; |
1016 | #if PERL_VERSION_ATLEAST (5,10,0) |
1019 | #if PERL_VERSION_ATLEAST (5,10,0) |
1017 | PL_parser = 0; |
1020 | PL_parser = 0; |
1018 | #endif |
1021 | #endif |
1019 | PL_hints = 0; |
1022 | PL_hints = 0; |
… | |
… | |
1135 | |
1138 | |
1136 | SvREFCNT_dec (coro->saved_deffh); |
1139 | SvREFCNT_dec (coro->saved_deffh); |
1137 | SvREFCNT_dec (coro->rouse_cb); |
1140 | SvREFCNT_dec (coro->rouse_cb); |
1138 | SvREFCNT_dec (coro->invoke_cb); |
1141 | SvREFCNT_dec (coro->invoke_cb); |
1139 | SvREFCNT_dec (coro->invoke_av); |
1142 | SvREFCNT_dec (coro->invoke_av); |
1140 | SvREFCNT_dec (coro->swap_sv); |
|
|
1141 | } |
1143 | } |
1142 | } |
1144 | } |
1143 | |
1145 | |
1144 | INLINE void |
1146 | INLINE void |
1145 | free_coro_mortal (pTHX) |
1147 | free_coro_mortal (pTHX) |
… | |
… | |
1636 | && coro->mainstack != main_mainstack |
1638 | && coro->mainstack != main_mainstack |
1637 | && coro->slot |
1639 | && coro->slot |
1638 | && !PL_dirty) |
1640 | && !PL_dirty) |
1639 | destroy_perl (aTHX_ coro); |
1641 | destroy_perl (aTHX_ coro); |
1640 | |
1642 | |
|
|
1643 | if (coro->next) coro->next->prev = coro->prev; |
|
|
1644 | if (coro->prev) coro->prev->next = coro->next; |
|
|
1645 | if (coro == coro_first) coro_first = coro->next; |
|
|
1646 | |
1641 | cctx_destroy (coro->cctx); |
1647 | cctx_destroy (coro->cctx); |
1642 | SvREFCNT_dec (coro->startcv); |
1648 | SvREFCNT_dec (coro->startcv); |
1643 | SvREFCNT_dec (coro->args); |
1649 | SvREFCNT_dec (coro->args); |
|
|
1650 | SvREFCNT_dec (coro->swap_sv); |
1644 | SvREFCNT_dec (CORO_THROW); |
1651 | SvREFCNT_dec (CORO_THROW); |
1645 | |
|
|
1646 | if (coro->next) coro->next->prev = coro->prev; |
|
|
1647 | if (coro->prev) coro->prev->next = coro->next; |
|
|
1648 | if (coro == coro_first) coro_first = coro->next; |
|
|
1649 | |
1652 | |
1650 | return 1; |
1653 | return 1; |
1651 | } |
1654 | } |
1652 | |
1655 | |
1653 | static int |
1656 | static int |
… | |
… | |
1823 | { |
1826 | { |
1824 | /* nothing to schedule: call the idle handler */ |
1827 | /* nothing to schedule: call the idle handler */ |
1825 | if (SvROK (sv_idle) |
1828 | if (SvROK (sv_idle) |
1826 | && SvOBJECT (SvRV (sv_idle))) |
1829 | && SvOBJECT (SvRV (sv_idle))) |
1827 | { |
1830 | { |
|
|
1831 | if (SvRV (sv_idle) == SvRV (coro_current)) |
|
|
1832 | croak ("FATAL: $Coro::IDLE blocked itself - did you try to block inside an event loop callback? Caught"); |
|
|
1833 | |
1828 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
1834 | ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ |
1829 | api_ready (aTHX_ SvRV (sv_idle)); |
1835 | api_ready (aTHX_ SvRV (sv_idle)); |
1830 | --coro_nready; |
1836 | --coro_nready; |
1831 | } |
1837 | } |
1832 | else |
1838 | else |
… | |
… | |
2418 | if (PL_op->op_flags & OPf_STACKED) |
2424 | if (PL_op->op_flags & OPf_STACKED) |
2419 | { |
2425 | { |
2420 | if (items > slf_arga) |
2426 | if (items > slf_arga) |
2421 | { |
2427 | { |
2422 | slf_arga = items; |
2428 | slf_arga = items; |
2423 | free (slf_argv); |
2429 | Safefree (slf_argv); |
2424 | slf_argv = malloc (slf_arga * sizeof (SV *)); |
2430 | New (0, slf_argv, slf_arga, SV *); |
2425 | } |
2431 | } |
2426 | |
2432 | |
2427 | slf_argc = items; |
2433 | slf_argc = items; |
2428 | |
2434 | |
2429 | for (i = 0; i < items; ++i) |
2435 | for (i = 0; i < items; ++i) |
… | |
… | |
2790 | { |
2796 | { |
2791 | SV *cb_cv = s_get_cv_croak (arg [1]); |
2797 | SV *cb_cv = s_get_cv_croak (arg [1]); |
2792 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2798 | av_push (av, SvREFCNT_inc_NN (cb_cv)); |
2793 | |
2799 | |
2794 | if (SvIVX (AvARRAY (av)[0])) |
2800 | if (SvIVX (AvARRAY (av)[0])) |
2795 | coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ |
2801 | coro_signal_wake (aTHX_ av, 1); /* must be the only waiter */ |
2796 | |
2802 | |
2797 | frame->prepare = prepare_nop; |
2803 | frame->prepare = prepare_nop; |
2798 | frame->check = slf_check_nop; |
2804 | frame->check = slf_check_nop; |
2799 | } |
2805 | } |
2800 | else if (SvIVX (AvARRAY (av)[0])) |
2806 | else if (SvIVX (AvARRAY (av)[0])) |
… | |
… | |
2962 | /* now call the AIO function - we assume our request is uncancelable */ |
2968 | /* now call the AIO function - we assume our request is uncancelable */ |
2963 | PUTBACK; |
2969 | PUTBACK; |
2964 | call_sv ((SV *)req, G_VOID | G_DISCARD); |
2970 | call_sv ((SV *)req, G_VOID | G_DISCARD); |
2965 | } |
2971 | } |
2966 | |
2972 | |
2967 | /* now that the requets is going, we loop toll we have a result */ |
2973 | /* now that the request is going, we loop till we have a result */ |
2968 | frame->data = (void *)state; |
2974 | frame->data = (void *)state; |
2969 | frame->prepare = prepare_schedule; |
2975 | frame->prepare = prepare_schedule; |
2970 | frame->check = slf_check_aio_req; |
2976 | frame->check = slf_check_aio_req; |
2971 | } |
2977 | } |
2972 | |
2978 | |
… | |
… | |
3271 | RETVAL = boolSV (coro->flags & ix); |
3277 | RETVAL = boolSV (coro->flags & ix); |
3272 | OUTPUT: |
3278 | OUTPUT: |
3273 | RETVAL |
3279 | RETVAL |
3274 | |
3280 | |
3275 | void |
3281 | void |
3276 | throw (Coro::State self, SV *throw = &PL_sv_undef) |
3282 | throw (Coro::State self, SV *exception = &PL_sv_undef) |
3277 | PROTOTYPE: $;$ |
3283 | PROTOTYPE: $;$ |
3278 | CODE: |
3284 | CODE: |
3279 | { |
3285 | { |
3280 | struct coro *current = SvSTATE_current; |
3286 | struct coro *current = SvSTATE_current; |
3281 | SV **throwp = self == current ? &CORO_THROW : &self->except; |
3287 | SV **exceptionp = self == current ? &CORO_THROW : &self->except; |
3282 | SvREFCNT_dec (*throwp); |
3288 | SvREFCNT_dec (*exceptionp); |
3283 | SvGETMAGIC (throw); |
3289 | SvGETMAGIC (exception); |
3284 | *throwp = SvOK (throw) ? newSVsv (throw) : 0; |
3290 | *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; |
3285 | } |
3291 | } |
3286 | |
3292 | |
3287 | void |
3293 | void |
3288 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3294 | api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) |
3289 | PROTOTYPE: $;$ |
3295 | PROTOTYPE: $;$ |
… | |
… | |
3396 | SWAP_SVS (current); |
3402 | SWAP_SVS (current); |
3397 | |
3403 | |
3398 | if (!coro->swap_sv) |
3404 | if (!coro->swap_sv) |
3399 | coro->swap_sv = newAV (); |
3405 | coro->swap_sv = newAV (); |
3400 | |
3406 | |
3401 | av_push (coro->swap_sv, SvREFCNT_inc_NN (sv)); |
3407 | av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (sv ))); |
3402 | av_push (coro->swap_sv, SvREFCNT_inc_NN (swapsv)); |
3408 | av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (swapsv))); |
3403 | |
3409 | |
3404 | if (current == coro) |
3410 | if (current == coro) |
3405 | SWAP_SVS (current); |
3411 | SWAP_SVS (current); |
3406 | } |
3412 | } |
3407 | |
3413 | |
… | |
… | |
3869 | { |
3875 | { |
3870 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
3876 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
3871 | coro_old_pp_sselect = 0; |
3877 | coro_old_pp_sselect = 0; |
3872 | } |
3878 | } |
3873 | |
3879 | |
3874 | |
|
|