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.363 by root, Tue Jul 14 00:55:10 2009 UTC vs.
Revision 1.373 by root, Thu Oct 1 23:51:33 2009 UTC

142static SV *sv_pool_size; 142static SV *sv_pool_size;
143static SV *sv_async_pool_idle; /* description string */ 143static SV *sv_async_pool_idle; /* description string */
144static AV *av_async_pool; /* idle pool */ 144static AV *av_async_pool; /* idle pool */
145static SV *sv_Coro; /* class string */ 145static SV *sv_Coro; /* class string */
146static CV *cv_pool_handler; 146static CV *cv_pool_handler;
147static CV *cv_coro_state_new;
148 147
149/* Coro::AnyEvent */ 148/* Coro::AnyEvent */
150static SV *sv_activity; 149static SV *sv_activity;
151 150
152/* enable processtime/realtime profiling */ 151/* enable processtime/realtime profiling */
282static SV *coro_readyhook; 281static SV *coro_readyhook;
283static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */ 282static struct coro *coro_ready [CORO_PRIO_MAX - CORO_PRIO_MIN + 1][2]; /* head|tail */
284static CV *cv_coro_run, *cv_coro_terminate; 283static CV *cv_coro_run, *cv_coro_terminate;
285static struct coro *coro_first; 284static struct coro *coro_first;
286#define coro_nready coroapi.nready 285#define coro_nready coroapi.nready
286
287/** Coro::Select ************************************************************/
288
289static OP *(*coro_old_pp_sselect) (pTHX);
290static SV *coro_select_select;
291
292/* horrible hack, but if it works... */
293static OP *
294coro_pp_sselect (pTHX)
295{
296 dSP;
297 PUSHMARK (SP - 4); /* fake argument list */
298 XPUSHs (coro_select_select);
299 PUTBACK;
300
301 /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */
302 PL_op->op_flags |= OPf_STACKED;
303 PL_op->op_private = 0;
304 return PL_ppaddr [OP_ENTERSUB](aTHX);
305}
287 306
288/** lowlevel stuff **********************************************************/ 307/** lowlevel stuff **********************************************************/
289 308
290static SV * 309static SV *
291coro_get_sv (pTHX_ const char *name, int create) 310coro_get_sv (pTHX_ const char *name, int create)
1255 /* somebody or something will hit me for both perl_run and PL_restartop */ 1274 /* somebody or something will hit me for both perl_run and PL_restartop */
1256 PL_restartop = PL_op; 1275 PL_restartop = PL_op;
1257 perl_run (PL_curinterp); 1276 perl_run (PL_curinterp);
1258 /* 1277 /*
1259 * Unfortunately, there is no way to get at the return values of the 1278 * Unfortunately, there is no way to get at the return values of the
1260 * coro body here, as perl_run destroys these 1279 * coro body here, as perl_run destroys these. Likewise, we cannot catch
1280 * runtime errors here, as this is just a random interpreter, not a thread.
1261 */ 1281 */
1262 1282
1263 /* 1283 /*
1264 * If perl-run returns we assume exit() was being called or the coro 1284 * 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) 1285 * fell off the end, which seems to be the only valid (non-bug)
1648 } 1668 }
1649 1669
1650 return 0; 1670 return 0;
1651} 1671}
1652 1672
1673static void
1674invoke_sv_ready_hook_helper (void)
1675{
1676 dTHX;
1677 dSP;
1678
1679 ENTER;
1680 SAVETMPS;
1681
1682 PUSHMARK (SP);
1683 PUTBACK;
1684 call_sv (coro_readyhook, G_VOID | G_DISCARD);
1685
1686 FREETMPS;
1687 LEAVE;
1688}
1689
1653static int 1690static int
1654api_ready (pTHX_ SV *coro_sv) 1691api_ready (pTHX_ SV *coro_sv)
1655{ 1692{
1656 struct coro *coro;
1657 SV *sv_hook;
1658 void (*xs_hook)(void);
1659
1660 coro = SvSTATE (coro_sv); 1693 struct coro *coro = SvSTATE (coro_sv);
1661 1694
1662 if (coro->flags & CF_READY) 1695 if (coro->flags & CF_READY)
1663 return 0; 1696 return 0;
1664 1697
1665 coro->flags |= CF_READY; 1698 coro->flags |= CF_READY;
1666 1699
1667 sv_hook = coro_nready ? 0 : coro_readyhook;
1668 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1669
1670 coro_enq (aTHX_ coro); 1700 coro_enq (aTHX_ coro);
1671 ++coro_nready;
1672 1701
1673 if (sv_hook) 1702 if (!coro_nready++)
1674 { 1703 if (coroapi.readyhook)
1675 dSP; 1704 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 1705
1691 return 1; 1706 return 1;
1692} 1707}
1693 1708
1694static int 1709static int
1745 api_ready (aTHX_ SvRV (sv_idle)); 1760 api_ready (aTHX_ SvRV (sv_idle));
1746 --coro_nready; 1761 --coro_nready;
1747 } 1762 }
1748 else 1763 else
1749 { 1764 {
1765 /* TODO: deprecated, remove, cannot work reliably *//*D*/
1750 dSP; 1766 dSP;
1751 1767
1752 ENTER; 1768 ENTER;
1753 SAVETMPS; 1769 SAVETMPS;
1754 1770
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
2922static SV *
2923coro_new (pTHX_ 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
2899MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2978MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2900 2979
2901PROTOTYPES: DISABLE 2980PROTOTYPES: DISABLE
2902 2981
2903BOOT: 2982BOOT:
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
2975SV * 3054SV *
2976new (char *klass, ...) 3055new (SV *klass, ...)
2977 ALIAS: 3056 ALIAS:
2978 Coro::new = 1 3057 Coro::new = 1
2979 CODE: 3058 CODE:
2980{ 3059 RETVAL = coro_new (aTHX_ 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
3033void 3063void
3034transfer (...) 3064transfer (...)
3035 PROTOTYPE: $$ 3065 PROTOTYPE: $$
3302 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE); 3332 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3303 3333
3304 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3334 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3305 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3335 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3306 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3336 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3307 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); 3337 CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */
3308 3338
3309 coro_stash = gv_stashpv ("Coro", TRUE); 3339 coro_stash = gv_stashpv ("Coro", TRUE);
3310 3340
3311 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); 3341 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX));
3312 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); 3342 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH));
3313 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); 3343 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL));
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
3366SV *
3367async (...)
3368 PROTOTYPE: &@
3369 CODE:
3370 RETVAL = coro_new (aTHX_ coro_stash, &ST (0), items, 1);
3371 api_ready (aTHX_ RETVAL);
3372 OUTPUT:
3373 RETVAL
3374
3336void 3375void
3337terminate (...) 3376terminate (...)
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
3381int 3429int
3382prio (Coro::State coro, int newprio = 0) 3430prio (Coro::State coro, int newprio = 0)
3383 PROTOTYPE: $;$ 3431 PROTOTYPE: $;$
3384 ALIAS: 3432 ALIAS:
3448 for (i = 1; i < items; ++i) 3496 for (i = 1; i < items; ++i)
3449 av_push (av, SvREFCNT_inc_NN (ST (i))); 3497 av_push (av, SvREFCNT_inc_NN (ST (i)));
3450 3498
3451 if ((SV *)hv == &PL_sv_undef) 3499 if ((SV *)hv == &PL_sv_undef)
3452 { 3500 {
3453 PUSHMARK (SP); 3501 SV *sv = coro_new (aTHX_ coro_stash, (SV **)&cv_pool_handler, 1, 1);
3454 EXTEND (SP, 2);
3455 PUSHs (sv_Coro);
3456 PUSHs ((SV *)cv_pool_handler);
3457 PUTBACK;
3458 call_sv ((SV *)cv_coro_state_new, G_SCALAR);
3459 SPAGAIN;
3460
3461 hv = (HV *)SvREFCNT_inc_NN (SvRV (POPs)); 3502 hv = (HV *)SvREFCNT_inc_NN (SvRV (sv));
3503 SvREFCNT_dec (sv);
3462 } 3504 }
3463 3505
3464 { 3506 {
3465 struct coro *coro = SvSTATE_hv (hv); 3507 struct coro *coro = SvSTATE_hv (hv);
3466 3508
3717 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); 3759 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
3718 sv_setpv ((SV *)slf_cv, proto); 3760 sv_setpv ((SV *)slf_cv, proto);
3719 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 3761 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
3720} 3762}
3721 3763
3764MODULE = Coro::State PACKAGE = Coro::Select
3765
3766void
3767patch_pp_sselect ()
3768 CODE:
3769 if (!coro_old_pp_sselect)
3770 {
3771 coro_select_select = (SV *)get_cv ("Coro::Select::select", 0);
3772 coro_old_pp_sselect = PL_ppaddr [OP_SSELECT];
3773 PL_ppaddr [OP_SSELECT] = coro_pp_sselect;
3774 }
3775
3776void
3777unpatch_pp_sselect ()
3778 CODE:
3779 if (coro_old_pp_sselect)
3780 {
3781 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect;
3782 coro_old_pp_sselect = 0;
3783 }
3784
3785

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines