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.378 by root, Sat Dec 12 01:30:26 2009 UTC vs.
Revision 1.387 by root, Mon Feb 21 13:38:04 2011 UTC

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 */
310 /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */ 313 /* entersub is an UNOP, select a LISTOP... keep your fingers crossed */
311 PL_op->op_flags |= OPf_STACKED; 314 PL_op->op_flags |= OPf_STACKED;
312 PL_op->op_private = 0; 315 PL_op->op_private = 0;
313 return PL_ppaddr [OP_ENTERSUB](aTHX); 316 return PL_ppaddr [OP_ENTERSUB](aTHX);
314} 317}
318
319/** time stuff **************************************************************/
320
321#ifdef HAS_GETTIMEOFDAY
322
323static void
324coro_u2time (pTHX_ UV ret[2])
325{
326 struct timeval tv;
327 gettimeofday (&tv, 0);
328
329 ret [0] = tv.tv_sec;
330 ret [1] = tv.tv_usec;
331}
332
333static double
334coro_nvtime ()
335{
336 struct timeval tv;
337 gettimeofday (&tv, 0);
338
339 return tv.tv_sec + tv.tv_usec * 1e-6;
340}
341
342static void
343time_init (void)
344{
345 nvtime = coro_nvtime;
346 u2time = coro_u2time;
347}
348
349#else
350
351static void
352time_init (void)
353{
354 SV **svp;
355
356 require_pv ("Time::HiRes");
357
358 svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
359
360 if (!svp) croak ("Time::HiRes is required");
361 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
362
363 nvtime = INT2PTR (double (*)(), SvIV (*svp));
364
365 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0);
366 u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp));
367}
368
369#endif
315 370
316/** lowlevel stuff **********************************************************/ 371/** lowlevel stuff **********************************************************/
317 372
318static SV * 373static SV *
319coro_get_sv (pTHX_ const char *name, int create) 374coro_get_sv (pTHX_ const char *name, int create)
583 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep); 638 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
584 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep); 639 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
585 640
586#if PERL_VERSION_ATLEAST (5,10,0) 641#if PERL_VERSION_ATLEAST (5,10,0)
587 /* perl 5.10 complicates this _quite_ a bit, but it also is 642 /* perl 5.10 complicates this _quite_ a bit, but it also is
588 * is much faster, so no quarrels here. alternatively, we could 643 * much faster, so no quarrels here. alternatively, we could
589 * sv_upgrade to avoid this. 644 * sv_upgrade to avoid this.
590 */ 645 */
591 { 646 {
592 /* swap sv_u */ 647 /* swap sv_u */
593 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u; 648 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
1010 PL_comppad_name_fill = 0; 1065 PL_comppad_name_fill = 0;
1011 PL_comppad_name_floor = 0; 1066 PL_comppad_name_floor = 0;
1012 PL_curpm = 0; 1067 PL_curpm = 0;
1013 PL_curpad = 0; 1068 PL_curpad = 0;
1014 PL_localizing = 0; 1069 PL_localizing = 0;
1015 PL_dirty = 0;
1016 PL_restartop = 0; 1070 PL_restartop = 0;
1017#if PERL_VERSION_ATLEAST (5,10,0) 1071#if PERL_VERSION_ATLEAST (5,10,0)
1018 PL_parser = 0; 1072 PL_parser = 0;
1019#endif 1073#endif
1020 PL_hints = 0; 1074 PL_hints = 0;
2422 if (PL_op->op_flags & OPf_STACKED) 2476 if (PL_op->op_flags & OPf_STACKED)
2423 { 2477 {
2424 if (items > slf_arga) 2478 if (items > slf_arga)
2425 { 2479 {
2426 slf_arga = items; 2480 slf_arga = items;
2427 free (slf_argv); 2481 Safefree (slf_argv);
2428 slf_argv = malloc (slf_arga * sizeof (SV *)); 2482 New (0, slf_argv, slf_arga, SV *);
2429 } 2483 }
2430 2484
2431 slf_argc = items; 2485 slf_argc = items;
2432 2486
2433 for (i = 0; i < items; ++i) 2487 for (i = 0; i < items; ++i)
2794 { 2848 {
2795 SV *cb_cv = s_get_cv_croak (arg [1]); 2849 SV *cb_cv = s_get_cv_croak (arg [1]);
2796 av_push (av, SvREFCNT_inc_NN (cb_cv)); 2850 av_push (av, SvREFCNT_inc_NN (cb_cv));
2797 2851
2798 if (SvIVX (AvARRAY (av)[0])) 2852 if (SvIVX (AvARRAY (av)[0]))
2799 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ 2853 coro_signal_wake (aTHX_ av, 1); /* must be the only waiter */
2800 2854
2801 frame->prepare = prepare_nop; 2855 frame->prepare = prepare_nop;
2802 frame->check = slf_check_nop; 2856 frame->check = slf_check_nop;
2803 } 2857 }
2804 else if (SvIVX (AvARRAY (av)[0])) 2858 else if (SvIVX (AvARRAY (av)[0]))
2966 /* now call the AIO function - we assume our request is uncancelable */ 3020 /* now call the AIO function - we assume our request is uncancelable */
2967 PUTBACK; 3021 PUTBACK;
2968 call_sv ((SV *)req, G_VOID | G_DISCARD); 3022 call_sv ((SV *)req, G_VOID | G_DISCARD);
2969 } 3023 }
2970 3024
2971 /* now that the requets is going, we loop toll we have a result */ 3025 /* now that the request is going, we loop till we have a result */
2972 frame->data = (void *)state; 3026 frame->data = (void *)state;
2973 frame->prepare = prepare_schedule; 3027 frame->prepare = prepare_schedule;
2974 frame->check = slf_check_aio_req; 3028 frame->check = slf_check_aio_req;
2975} 3029}
2976 3030
3107 coroapi.prepare_nop = prepare_nop; 3161 coroapi.prepare_nop = prepare_nop;
3108 coroapi.prepare_schedule = prepare_schedule; 3162 coroapi.prepare_schedule = prepare_schedule;
3109 coroapi.prepare_cede = prepare_cede; 3163 coroapi.prepare_cede = prepare_cede;
3110 coroapi.prepare_cede_notself = prepare_cede_notself; 3164 coroapi.prepare_cede_notself = prepare_cede_notself;
3111 3165
3112 { 3166 time_init ();
3113 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
3114
3115 if (!svp) croak ("Time::HiRes is required");
3116 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
3117
3118 nvtime = INT2PTR (double (*)(), SvIV (*svp));
3119
3120 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0);
3121 u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp));
3122 }
3123 3167
3124 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3168 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
3125} 3169}
3126 3170
3127SV * 3171SV *
3275 RETVAL = boolSV (coro->flags & ix); 3319 RETVAL = boolSV (coro->flags & ix);
3276 OUTPUT: 3320 OUTPUT:
3277 RETVAL 3321 RETVAL
3278 3322
3279void 3323void
3280throw (Coro::State self, SV *throw = &PL_sv_undef) 3324throw (Coro::State self, SV *exception = &PL_sv_undef)
3281 PROTOTYPE: $;$ 3325 PROTOTYPE: $;$
3282 CODE: 3326 CODE:
3283{ 3327{
3284 struct coro *current = SvSTATE_current; 3328 struct coro *current = SvSTATE_current;
3285 SV **throwp = self == current ? &CORO_THROW : &self->except; 3329 SV **exceptionp = self == current ? &CORO_THROW : &self->except;
3286 SvREFCNT_dec (*throwp); 3330 SvREFCNT_dec (*exceptionp);
3287 SvGETMAGIC (throw); 3331 SvGETMAGIC (exception);
3288 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 3332 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0;
3289} 3333}
3290 3334
3291void 3335void
3292api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3336api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3293 PROTOTYPE: $;$ 3337 PROTOTYPE: $;$
3873 { 3917 {
3874 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; 3918 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect;
3875 coro_old_pp_sselect = 0; 3919 coro_old_pp_sselect = 0;
3876 } 3920 }
3877 3921
3878

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines