… | |
… | |
283 | static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ |
283 | static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ |
284 | static CV *cv_coro_run, *cv_coro_terminate; |
284 | static CV *cv_coro_run, *cv_coro_terminate; |
285 | static struct coro *coro_first; |
285 | static struct coro *coro_first; |
286 | #define coro_nready coroapi.nready |
286 | #define coro_nready coroapi.nready |
287 | |
287 | |
|
|
288 | /** Coro::Select ************************************************************/ |
|
|
289 | |
|
|
290 | static OP *(*coro_old_pp_sselect) (pTHX); |
|
|
291 | static SV *coro_select_select; |
|
|
292 | |
|
|
293 | /* horrible hack, but if it works... */ |
|
|
294 | static OP * |
|
|
295 | coro_pp_sselect (aTHX) |
|
|
296 | { |
|
|
297 | dSP; |
|
|
298 | PUSHMARK (SP - 4); /* fake argument list */ |
|
|
299 | XPUSHs (coro_select_select); |
|
|
300 | PUTBACK; |
|
|
301 | |
|
|
302 | /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */ |
|
|
303 | PL_op->op_flags |= OPf_STACKED; |
|
|
304 | PL_op->op_private = 0; |
|
|
305 | return PL_ppaddr [OP_ENTERSUB](aTHX); |
|
|
306 | } |
|
|
307 | |
288 | /** lowlevel stuff **********************************************************/ |
308 | /** lowlevel stuff **********************************************************/ |
289 | |
309 | |
290 | static SV * |
310 | static SV * |
291 | coro_get_sv (pTHX_ const char *name, int create) |
311 | coro_get_sv (pTHX_ const char *name, int create) |
292 | { |
312 | { |
… | |
… | |
1255 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1275 | /* somebody or something will hit me for both perl_run and PL_restartop */ |
1256 | PL_restartop = PL_op; |
1276 | PL_restartop = PL_op; |
1257 | perl_run (PL_curinterp); |
1277 | perl_run (PL_curinterp); |
1258 | /* |
1278 | /* |
1259 | * Unfortunately, there is no way to get at the return values of the |
1279 | * Unfortunately, there is no way to get at the return values of the |
1260 | * coro body here, as perl_run destroys these |
1280 | * coro body here, as perl_run destroys these. Likewise, we cannot catch |
|
|
1281 | * runtime errors here, as this is just a random interpreter, not a thread. |
1261 | */ |
1282 | */ |
1262 | |
1283 | |
1263 | /* |
1284 | /* |
1264 | * If perl-run returns we assume exit() was being called or the coro |
1285 | * If perl-run returns we assume exit() was being called or the coro |
1265 | * fell off the end, which seems to be the only valid (non-bug) |
1286 | * fell off the end, which seems to be the only valid (non-bug) |
… | |
… | |
1648 | } |
1669 | } |
1649 | |
1670 | |
1650 | return 0; |
1671 | return 0; |
1651 | } |
1672 | } |
1652 | |
1673 | |
|
|
1674 | static void |
|
|
1675 | invoke_sv_ready_hook_helper (void) |
|
|
1676 | { |
|
|
1677 | dTHX; |
|
|
1678 | dSP; |
|
|
1679 | |
|
|
1680 | ENTER; |
|
|
1681 | SAVETMPS; |
|
|
1682 | |
|
|
1683 | PUSHMARK (SP); |
|
|
1684 | PUTBACK; |
|
|
1685 | call_sv (coro_readyhook, G_VOID | G_DISCARD); |
|
|
1686 | |
|
|
1687 | FREETMPS; |
|
|
1688 | LEAVE; |
|
|
1689 | } |
|
|
1690 | |
1653 | static int |
1691 | static int |
1654 | api_ready (pTHX_ SV *coro_sv) |
1692 | api_ready (pTHX_ SV *coro_sv) |
1655 | { |
1693 | { |
1656 | struct coro *coro; |
|
|
1657 | SV *sv_hook; |
|
|
1658 | void (*xs_hook)(void); |
|
|
1659 | |
|
|
1660 | coro = SvSTATE (coro_sv); |
1694 | struct coro *coro = SvSTATE (coro_sv); |
1661 | |
1695 | |
1662 | if (coro->flags & CF_READY) |
1696 | if (coro->flags & CF_READY) |
1663 | return 0; |
1697 | return 0; |
1664 | |
1698 | |
1665 | coro->flags |= CF_READY; |
1699 | coro->flags |= CF_READY; |
1666 | |
1700 | |
1667 | sv_hook = coro_nready ? 0 : coro_readyhook; |
|
|
1668 | xs_hook = coro_nready ? 0 : coroapi.readyhook; |
|
|
1669 | |
|
|
1670 | coro_enq (aTHX_ coro); |
1701 | coro_enq (aTHX_ coro); |
1671 | ++coro_nready; |
|
|
1672 | |
1702 | |
1673 | if (sv_hook) |
1703 | if (!coro_nready++) |
1674 | { |
1704 | if (coroapi.readyhook) |
1675 | dSP; |
1705 | coroapi.readyhook (); |
1676 | |
|
|
1677 | ENTER; |
|
|
1678 | SAVETMPS; |
|
|
1679 | |
|
|
1680 | PUSHMARK (SP); |
|
|
1681 | PUTBACK; |
|
|
1682 | call_sv (sv_hook, G_VOID | G_DISCARD); |
|
|
1683 | |
|
|
1684 | FREETMPS; |
|
|
1685 | LEAVE; |
|
|
1686 | } |
|
|
1687 | |
|
|
1688 | if (xs_hook) |
|
|
1689 | xs_hook (); |
|
|
1690 | |
1706 | |
1691 | return 1; |
1707 | return 1; |
1692 | } |
1708 | } |
1693 | |
1709 | |
1694 | static int |
1710 | static int |
… | |
… | |
1900 | { |
1916 | { |
1901 | int i; |
1917 | int i; |
1902 | HV *hv = (HV *)SvRV (coro_current); |
1918 | HV *hv = (HV *)SvRV (coro_current); |
1903 | AV *av = newAV (); |
1919 | AV *av = newAV (); |
1904 | |
1920 | |
|
|
1921 | /* items are actually not so common, so optimise for this case */ |
|
|
1922 | if (items) |
|
|
1923 | { |
1905 | av_extend (av, items - 1); |
1924 | av_extend (av, items - 1); |
|
|
1925 | |
1906 | for (i = 0; i < items; ++i) |
1926 | for (i = 0; i < items; ++i) |
1907 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
1927 | av_push (av, SvREFCNT_inc_NN (arg [i])); |
|
|
1928 | } |
1908 | |
1929 | |
1909 | hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); |
1930 | hv_store (hv, "_status", sizeof ("_status") - 1, newRV_noinc ((SV *)av), 0); |
1910 | |
1931 | |
1911 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
1932 | av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ |
1912 | api_ready (aTHX_ sv_manager); |
1933 | api_ready (aTHX_ sv_manager); |
… | |
… | |
2894 | |
2915 | |
2895 | #if CORO_CLONE |
2916 | #if CORO_CLONE |
2896 | # include "clone.c" |
2917 | # include "clone.c" |
2897 | #endif |
2918 | #endif |
2898 | |
2919 | |
|
|
2920 | /*****************************************************************************/ |
|
|
2921 | |
|
|
2922 | static SV * |
|
|
2923 | coro_new (HV *stash, SV **argv, int argc, int is_coro) |
|
|
2924 | { |
|
|
2925 | SV *coro_sv; |
|
|
2926 | struct coro *coro; |
|
|
2927 | MAGIC *mg; |
|
|
2928 | HV *hv; |
|
|
2929 | SV *cb; |
|
|
2930 | int i; |
|
|
2931 | |
|
|
2932 | if (argc > 0) |
|
|
2933 | { |
|
|
2934 | cb = s_get_cv_croak (argv [0]); |
|
|
2935 | |
|
|
2936 | if (!is_coro) |
|
|
2937 | { |
|
|
2938 | if (CvISXSUB (cb)) |
|
|
2939 | croak ("Coro::State doesn't support XS functions as coroutine start, caught"); |
|
|
2940 | |
|
|
2941 | if (!CvROOT (cb)) |
|
|
2942 | croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); |
|
|
2943 | } |
|
|
2944 | } |
|
|
2945 | |
|
|
2946 | Newz (0, coro, 1, struct coro); |
|
|
2947 | coro->args = newAV (); |
|
|
2948 | coro->flags = CF_NEW; |
|
|
2949 | |
|
|
2950 | if (coro_first) coro_first->prev = coro; |
|
|
2951 | coro->next = coro_first; |
|
|
2952 | coro_first = coro; |
|
|
2953 | |
|
|
2954 | coro->hv = hv = newHV (); |
|
|
2955 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
|
|
2956 | mg->mg_flags |= MGf_DUP; |
|
|
2957 | coro_sv = sv_bless (newRV_noinc ((SV *)hv), stash); |
|
|
2958 | |
|
|
2959 | if (argc > 0) |
|
|
2960 | { |
|
|
2961 | av_extend (coro->args, argc + is_coro - 1); |
|
|
2962 | |
|
|
2963 | if (is_coro) |
|
|
2964 | { |
|
|
2965 | av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); |
|
|
2966 | cb = (SV *)cv_coro_run; |
|
|
2967 | } |
|
|
2968 | |
|
|
2969 | coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); |
|
|
2970 | |
|
|
2971 | for (i = 1; i < argc; i++) |
|
|
2972 | av_push (coro->args, newSVsv (argv [i])); |
|
|
2973 | } |
|
|
2974 | |
|
|
2975 | return coro_sv; |
|
|
2976 | } |
|
|
2977 | |
2899 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2978 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
2900 | |
2979 | |
2901 | PROTOTYPES: DISABLE |
2980 | PROTOTYPES: DISABLE |
2902 | |
2981 | |
2903 | BOOT: |
2982 | BOOT: |
… | |
… | |
2971 | |
3050 | |
2972 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
3051 | assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); |
2973 | } |
3052 | } |
2974 | |
3053 | |
2975 | SV * |
3054 | SV * |
2976 | new (char *klass, ...) |
3055 | new (SV *klass, ...) |
2977 | ALIAS: |
3056 | ALIAS: |
2978 | Coro::new = 1 |
3057 | Coro::new = 1 |
2979 | CODE: |
3058 | CODE: |
2980 | { |
3059 | RETVAL = coro_new (ix ? coro_stash : coro_state_stash, &ST (1), items - 1, ix); |
2981 | struct coro *coro; |
3060 | OUTPUT: |
2982 | MAGIC *mg; |
|
|
2983 | HV *hv; |
|
|
2984 | SV *cb; |
|
|
2985 | int i; |
|
|
2986 | |
|
|
2987 | if (items > 1) |
|
|
2988 | { |
|
|
2989 | cb = s_get_cv_croak (ST (1)); |
|
|
2990 | |
|
|
2991 | if (!ix) |
|
|
2992 | { |
|
|
2993 | if (CvISXSUB (cb)) |
|
|
2994 | croak ("Coro::State doesn't support XS functions as coroutine start, caught"); |
|
|
2995 | |
|
|
2996 | if (!CvROOT (cb)) |
|
|
2997 | croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught"); |
|
|
2998 | } |
|
|
2999 | } |
|
|
3000 | |
|
|
3001 | Newz (0, coro, 1, struct coro); |
|
|
3002 | coro->args = newAV (); |
|
|
3003 | coro->flags = CF_NEW; |
|
|
3004 | |
|
|
3005 | if (coro_first) coro_first->prev = coro; |
|
|
3006 | coro->next = coro_first; |
|
|
3007 | coro_first = coro; |
|
|
3008 | |
|
|
3009 | coro->hv = hv = newHV (); |
|
|
3010 | mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); |
|
|
3011 | mg->mg_flags |= MGf_DUP; |
|
|
3012 | RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); |
|
|
3013 | |
|
|
3014 | if (items > 1) |
|
|
3015 | { |
|
|
3016 | av_extend (coro->args, items - 1 + ix - 1); |
|
|
3017 | |
|
|
3018 | if (ix) |
|
|
3019 | { |
|
|
3020 | av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb)); |
|
|
3021 | cb = (SV *)cv_coro_run; |
|
|
3022 | } |
|
|
3023 | |
|
|
3024 | coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb); |
|
|
3025 | |
|
|
3026 | for (i = 2; i < items; i++) |
|
|
3027 | av_push (coro->args, newSVsv (ST (i))); |
|
|
3028 | } |
|
|
3029 | } |
|
|
3030 | OUTPUT: |
|
|
3031 | RETVAL |
3061 | RETVAL |
3032 | |
3062 | |
3033 | void |
3063 | void |
3034 | transfer (...) |
3064 | transfer (...) |
3035 | PROTOTYPE: $$ |
3065 | PROTOTYPE: $$ |
… | |
… | |
3331 | sv_setiv (sv, (IV)&coroapi); |
3361 | sv_setiv (sv, (IV)&coroapi); |
3332 | SvREADONLY_on (sv); |
3362 | SvREADONLY_on (sv); |
3333 | } |
3363 | } |
3334 | } |
3364 | } |
3335 | |
3365 | |
|
|
3366 | SV * |
|
|
3367 | async (...) |
|
|
3368 | PROTOTYPE: &@ |
|
|
3369 | CODE: |
|
|
3370 | RETVAL = coro_new (coro_stash, &ST (0), items, 1); |
|
|
3371 | api_ready (RETVAL); |
|
|
3372 | OUTPUT: |
|
|
3373 | RETVAL |
|
|
3374 | |
3336 | void |
3375 | void |
3337 | terminate (...) |
3376 | terminate (...) |
3338 | CODE: |
3377 | CODE: |
3339 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
3378 | CORO_EXECUTE_SLF_XS (slf_init_terminate); |
3340 | |
3379 | |
… | |
… | |
3374 | _set_readyhook (SV *hook) |
3413 | _set_readyhook (SV *hook) |
3375 | PROTOTYPE: $ |
3414 | PROTOTYPE: $ |
3376 | CODE: |
3415 | CODE: |
3377 | SvREFCNT_dec (coro_readyhook); |
3416 | SvREFCNT_dec (coro_readyhook); |
3378 | SvGETMAGIC (hook); |
3417 | SvGETMAGIC (hook); |
|
|
3418 | if (SvOK (hook)) |
|
|
3419 | { |
3379 | coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; |
3420 | coro_readyhook = newSVsv (hook); |
|
|
3421 | CORO_READYHOOK = invoke_sv_ready_hook_helper; |
|
|
3422 | } |
|
|
3423 | else |
|
|
3424 | { |
|
|
3425 | coro_readyhook = 0; |
|
|
3426 | CORO_READYHOOK = 0; |
|
|
3427 | } |
3380 | |
3428 | |
3381 | int |
3429 | int |
3382 | prio (Coro::State coro, int newprio = 0) |
3430 | prio (Coro::State coro, int newprio = 0) |
3383 | PROTOTYPE: $;$ |
3431 | PROTOTYPE: $;$ |
3384 | ALIAS: |
3432 | ALIAS: |
… | |
… | |
3717 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3765 | CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); |
3718 | sv_setpv ((SV *)slf_cv, proto); |
3766 | sv_setpv ((SV *)slf_cv, proto); |
3719 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3767 | sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); |
3720 | } |
3768 | } |
3721 | |
3769 | |
|
|
3770 | MODULE = Coro::State PACKAGE = Coro::Select |
|
|
3771 | |
|
|
3772 | void |
|
|
3773 | patch_pp_sselect () |
|
|
3774 | CODE: |
|
|
3775 | if (!coro_old_pp_sselect) |
|
|
3776 | { |
|
|
3777 | coro_select_select = (SV *)get_cv ("Coro::Select::select", 0); |
|
|
3778 | coro_old_pp_sselect = PL_ppaddr [OP_SSELECT]; |
|
|
3779 | PL_ppaddr [OP_SSELECT] = coro_pp_sselect; |
|
|
3780 | } |
|
|
3781 | |
|
|
3782 | void |
|
|
3783 | unpatch_pp_sselect () |
|
|
3784 | CODE: |
|
|
3785 | if (coro_old_pp_sselect) |
|
|
3786 | { |
|
|
3787 | PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; |
|
|
3788 | coro_old_pp_sselect = 0; |
|
|
3789 | } |
|
|
3790 | |
|
|
3791 | |