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.367 by root, Mon Aug 3 09:19:57 2009 UTC vs.
Revision 1.368 by root, Thu Aug 13 02:35:41 2009 UTC

1916{ 1916{
1917 int i; 1917 int i;
1918 HV *hv = (HV *)SvRV (coro_current); 1918 HV *hv = (HV *)SvRV (coro_current);
1919 AV *av = newAV (); 1919 AV *av = newAV ();
1920 1920
1921 /* items are actually not so common, so optimise for this case */
1922 if (items)
1923 {
1921 av_extend (av, items - 1); 1924 av_extend (av, items - 1);
1925
1922 for (i = 0; i < items; ++i) 1926 for (i = 0; i < items; ++i)
1923 av_push (av, SvREFCNT_inc_NN (arg [i])); 1927 av_push (av, SvREFCNT_inc_NN (arg [i]));
1928 }
1924 1929
1925 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);
1926 1931
1927 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 */
1928 api_ready (aTHX_ sv_manager); 1933 api_ready (aTHX_ sv_manager);
2910 2915
2911#if CORO_CLONE 2916#if CORO_CLONE
2912# include "clone.c" 2917# include "clone.c"
2913#endif 2918#endif
2914 2919
2920/*****************************************************************************/
2921
2922static SV *
2923coro_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
2915MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 2978MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
2916 2979
2917PROTOTYPES: DISABLE 2980PROTOTYPES: DISABLE
2918 2981
2919BOOT: 2982BOOT:
2987 3050
2988 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3051 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
2989} 3052}
2990 3053
2991SV * 3054SV *
2992new (char *klass, ...) 3055new (SV *klass, ...)
2993 ALIAS: 3056 ALIAS:
2994 Coro::new = 1 3057 Coro::new = 1
2995 CODE: 3058 CODE:
2996{ 3059 RETVAL = coro_new (ix ? coro_stash : coro_state_stash, &ST (1), items - 1, ix);
2997 struct coro *coro; 3060 OUTPUT:
2998 MAGIC *mg;
2999 HV *hv;
3000 SV *cb;
3001 int i;
3002
3003 if (items > 1)
3004 {
3005 cb = s_get_cv_croak (ST (1));
3006
3007 if (!ix)
3008 {
3009 if (CvISXSUB (cb))
3010 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
3011
3012 if (!CvROOT (cb))
3013 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
3014 }
3015 }
3016
3017 Newz (0, coro, 1, struct coro);
3018 coro->args = newAV ();
3019 coro->flags = CF_NEW;
3020
3021 if (coro_first) coro_first->prev = coro;
3022 coro->next = coro_first;
3023 coro_first = coro;
3024
3025 coro->hv = hv = newHV ();
3026 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
3027 mg->mg_flags |= MGf_DUP;
3028 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
3029
3030 if (items > 1)
3031 {
3032 av_extend (coro->args, items - 1 + ix - 1);
3033
3034 if (ix)
3035 {
3036 av_push (coro->args, SvREFCNT_inc_NN ((SV *)cb));
3037 cb = (SV *)cv_coro_run;
3038 }
3039
3040 coro->startcv = (CV *)SvREFCNT_inc_NN ((SV *)cb);
3041
3042 for (i = 2; i < items; i++)
3043 av_push (coro->args, newSVsv (ST (i)));
3044 }
3045}
3046 OUTPUT:
3047 RETVAL 3061 RETVAL
3048 3062
3049void 3063void
3050transfer (...) 3064transfer (...)
3051 PROTOTYPE: $$ 3065 PROTOTYPE: $$
3346 /*GCoroAPI = &coroapi;*/ 3360 /*GCoroAPI = &coroapi;*/
3347 sv_setiv (sv, (IV)&coroapi); 3361 sv_setiv (sv, (IV)&coroapi);
3348 SvREADONLY_on (sv); 3362 SvREADONLY_on (sv);
3349 } 3363 }
3350} 3364}
3365
3366SV *
3367async (...)
3368 PROTOTYPE: &@
3369 CODE:
3370 RETVAL = coro_new (coro_stash, &ST (0), items, 1);
3371 api_ready (RETVAL);
3372 OUTPUT:
3373 RETVAL
3351 3374
3352void 3375void
3353terminate (...) 3376terminate (...)
3354 CODE: 3377 CODE:
3355 CORO_EXECUTE_SLF_XS (slf_init_terminate); 3378 CORO_EXECUTE_SLF_XS (slf_init_terminate);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines