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.374 by root, Fri Oct 2 19:55:59 2009 UTC vs.
Revision 1.386 by root, Sat Feb 19 06:51:23 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 (aTHX_ 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;
718 { 773 {
719 CV *cv = cx->blk_sub.cv; 774 CV *cv = cx->blk_sub.cv;
720 775
721 if (expect_true (CvDEPTH (cv))) 776 if (expect_true (CvDEPTH (cv)))
722 { 777 {
778 EXTEND (SP, 3);
723 PUSHs ((SV *)CvPADLIST (cv)); 779 PUSHs ((SV *)CvPADLIST (cv));
724 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv))); 780 PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv)));
725 PUSHs ((SV *)cv); 781 PUSHs ((SV *)cv);
726 782
727 CvDEPTH (cv) = 0; 783 CvDEPTH (cv) = 0;
1009 PL_comppad_name_fill = 0; 1065 PL_comppad_name_fill = 0;
1010 PL_comppad_name_floor = 0; 1066 PL_comppad_name_floor = 0;
1011 PL_curpm = 0; 1067 PL_curpm = 0;
1012 PL_curpad = 0; 1068 PL_curpad = 0;
1013 PL_localizing = 0; 1069 PL_localizing = 0;
1014 PL_dirty = 0;
1015 PL_restartop = 0; 1070 PL_restartop = 0;
1016#if PERL_VERSION_ATLEAST (5,10,0) 1071#if PERL_VERSION_ATLEAST (5,10,0)
1017 PL_parser = 0; 1072 PL_parser = 0;
1018#endif 1073#endif
1019 PL_hints = 0; 1074 PL_hints = 0;
1135 1190
1136 SvREFCNT_dec (coro->saved_deffh); 1191 SvREFCNT_dec (coro->saved_deffh);
1137 SvREFCNT_dec (coro->rouse_cb); 1192 SvREFCNT_dec (coro->rouse_cb);
1138 SvREFCNT_dec (coro->invoke_cb); 1193 SvREFCNT_dec (coro->invoke_cb);
1139 SvREFCNT_dec (coro->invoke_av); 1194 SvREFCNT_dec (coro->invoke_av);
1140 SvREFCNT_dec (coro->swap_sv);
1141 } 1195 }
1142} 1196}
1143 1197
1144INLINE void 1198INLINE void
1145free_coro_mortal (pTHX) 1199free_coro_mortal (pTHX)
1636 && coro->mainstack != main_mainstack 1690 && coro->mainstack != main_mainstack
1637 && coro->slot 1691 && coro->slot
1638 && !PL_dirty) 1692 && !PL_dirty)
1639 destroy_perl (aTHX_ coro); 1693 destroy_perl (aTHX_ coro);
1640 1694
1695 if (coro->next) coro->next->prev = coro->prev;
1696 if (coro->prev) coro->prev->next = coro->next;
1697 if (coro == coro_first) coro_first = coro->next;
1698
1641 cctx_destroy (coro->cctx); 1699 cctx_destroy (coro->cctx);
1642 SvREFCNT_dec (coro->startcv); 1700 SvREFCNT_dec (coro->startcv);
1643 SvREFCNT_dec (coro->args); 1701 SvREFCNT_dec (coro->args);
1702 SvREFCNT_dec (coro->swap_sv);
1644 SvREFCNT_dec (CORO_THROW); 1703 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 1704
1650 return 1; 1705 return 1;
1651} 1706}
1652 1707
1653static int 1708static int
1823 { 1878 {
1824 /* nothing to schedule: call the idle handler */ 1879 /* nothing to schedule: call the idle handler */
1825 if (SvROK (sv_idle) 1880 if (SvROK (sv_idle)
1826 && SvOBJECT (SvRV (sv_idle))) 1881 && SvOBJECT (SvRV (sv_idle)))
1827 { 1882 {
1883 if (SvRV (sv_idle) == SvRV (coro_current))
1884 croak ("FATAL: $Coro::IDLE blocked itself - did you try to block inside an event loop callback? Caught");
1885
1828 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ 1886 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1829 api_ready (aTHX_ SvRV (sv_idle)); 1887 api_ready (aTHX_ SvRV (sv_idle));
1830 --coro_nready; 1888 --coro_nready;
1831 } 1889 }
1832 else 1890 else
2418 if (PL_op->op_flags & OPf_STACKED) 2476 if (PL_op->op_flags & OPf_STACKED)
2419 { 2477 {
2420 if (items > slf_arga) 2478 if (items > slf_arga)
2421 { 2479 {
2422 slf_arga = items; 2480 slf_arga = items;
2423 free (slf_argv); 2481 Safefree (slf_argv);
2424 slf_argv = malloc (slf_arga * sizeof (SV *)); 2482 New (0, slf_argv, slf_arga, SV *);
2425 } 2483 }
2426 2484
2427 slf_argc = items; 2485 slf_argc = items;
2428 2486
2429 for (i = 0; i < items; ++i) 2487 for (i = 0; i < items; ++i)
2790 { 2848 {
2791 SV *cb_cv = s_get_cv_croak (arg [1]); 2849 SV *cb_cv = s_get_cv_croak (arg [1]);
2792 av_push (av, SvREFCNT_inc_NN (cb_cv)); 2850 av_push (av, SvREFCNT_inc_NN (cb_cv));
2793 2851
2794 if (SvIVX (AvARRAY (av)[0])) 2852 if (SvIVX (AvARRAY (av)[0]))
2795 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */ 2853 coro_signal_wake (aTHX_ av, 1); /* must be the only waiter */
2796 2854
2797 frame->prepare = prepare_nop; 2855 frame->prepare = prepare_nop;
2798 frame->check = slf_check_nop; 2856 frame->check = slf_check_nop;
2799 } 2857 }
2800 else if (SvIVX (AvARRAY (av)[0])) 2858 else if (SvIVX (AvARRAY (av)[0]))
2962 /* now call the AIO function - we assume our request is uncancelable */ 3020 /* now call the AIO function - we assume our request is uncancelable */
2963 PUTBACK; 3021 PUTBACK;
2964 call_sv ((SV *)req, G_VOID | G_DISCARD); 3022 call_sv ((SV *)req, G_VOID | G_DISCARD);
2965 } 3023 }
2966 3024
2967 /* 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 */
2968 frame->data = (void *)state; 3026 frame->data = (void *)state;
2969 frame->prepare = prepare_schedule; 3027 frame->prepare = prepare_schedule;
2970 frame->check = slf_check_aio_req; 3028 frame->check = slf_check_aio_req;
2971} 3029}
2972 3030
3103 coroapi.prepare_nop = prepare_nop; 3161 coroapi.prepare_nop = prepare_nop;
3104 coroapi.prepare_schedule = prepare_schedule; 3162 coroapi.prepare_schedule = prepare_schedule;
3105 coroapi.prepare_cede = prepare_cede; 3163 coroapi.prepare_cede = prepare_cede;
3106 coroapi.prepare_cede_notself = prepare_cede_notself; 3164 coroapi.prepare_cede_notself = prepare_cede_notself;
3107 3165
3108 { 3166 time_init ();
3109 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
3110
3111 if (!svp) croak ("Time::HiRes is required");
3112 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
3113
3114 nvtime = INT2PTR (double (*)(), SvIV (*svp));
3115
3116 svp = hv_fetch (PL_modglobal, "Time::U2time", 12, 0);
3117 u2time = INT2PTR (void (*)(pTHX_ UV ret[2]), SvIV (*svp));
3118 }
3119 3167
3120 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL)); 3168 assert (("PRIO_NORMAL must be 0", !CORO_PRIO_NORMAL));
3121} 3169}
3122 3170
3123SV * 3171SV *
3271 RETVAL = boolSV (coro->flags & ix); 3319 RETVAL = boolSV (coro->flags & ix);
3272 OUTPUT: 3320 OUTPUT:
3273 RETVAL 3321 RETVAL
3274 3322
3275void 3323void
3276throw (Coro::State self, SV *throw = &PL_sv_undef) 3324throw (Coro::State self, SV *exception = &PL_sv_undef)
3277 PROTOTYPE: $;$ 3325 PROTOTYPE: $;$
3278 CODE: 3326 CODE:
3279{ 3327{
3280 struct coro *current = SvSTATE_current; 3328 struct coro *current = SvSTATE_current;
3281 SV **throwp = self == current ? &CORO_THROW : &self->except; 3329 SV **exceptionp = self == current ? &CORO_THROW : &self->except;
3282 SvREFCNT_dec (*throwp); 3330 SvREFCNT_dec (*exceptionp);
3283 SvGETMAGIC (throw); 3331 SvGETMAGIC (exception);
3284 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 3332 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0;
3285} 3333}
3286 3334
3287void 3335void
3288api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3336api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3289 PROTOTYPE: $;$ 3337 PROTOTYPE: $;$
3396 SWAP_SVS (current); 3444 SWAP_SVS (current);
3397 3445
3398 if (!coro->swap_sv) 3446 if (!coro->swap_sv)
3399 coro->swap_sv = newAV (); 3447 coro->swap_sv = newAV ();
3400 3448
3401 av_push (coro->swap_sv, SvREFCNT_inc_NN (sv)); 3449 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (sv )));
3402 av_push (coro->swap_sv, SvREFCNT_inc_NN (swapsv)); 3450 av_push (coro->swap_sv, SvREFCNT_inc_NN (SvRV (swapsv)));
3403 3451
3404 if (current == coro) 3452 if (current == coro)
3405 SWAP_SVS (current); 3453 SWAP_SVS (current);
3406} 3454}
3407 3455
3869 { 3917 {
3870 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; 3918 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect;
3871 coro_old_pp_sselect = 0; 3919 coro_old_pp_sselect = 0;
3872 } 3920 }
3873 3921
3874

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines