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.303 by root, Wed Nov 19 05:52:42 2008 UTC vs.
Revision 1.307 by root, Wed Nov 19 14:34:46 2008 UTC

246 /* state data */ 246 /* state data */
247 struct CoroSLF slf_frame; /* saved slf frame */ 247 struct CoroSLF slf_frame; /* saved slf frame */
248 AV *mainstack; 248 AV *mainstack;
249 perl_slots *slot; /* basically the saved sp */ 249 perl_slots *slot; /* basically the saved sp */
250 250
251 CV *startcv; /* the CV to execute */
251 AV *args; /* data associated with this coroutine (initial args) */ 252 AV *args; /* data associated with this coroutine (initial args) */
252 int refcnt; /* coroutines are refcounted, yes */ 253 int refcnt; /* coroutines are refcounted, yes */
253 int flags; /* CF_ flags */ 254 int flags; /* CF_ flags */
254 HV *hv; /* the perl hash associated with this coro, if any */ 255 HV *hv; /* the perl hash associated with this coro, if any */
255 void (*on_destroy)(pTHX_ struct coro *coro); 256 void (*on_destroy)(pTHX_ struct coro *coro);
256 257
257 /* statistics */ 258 /* statistics */
258 int usecount; /* number of transfers to this coro */ 259 int usecount; /* number of transfers to this coro */
259 260
321#if PERL_VERSION_ATLEAST (5,10,0) 322#if PERL_VERSION_ATLEAST (5,10,0)
322 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 323 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
323 get_hv (name, create); 324 get_hv (name, create);
324#endif 325#endif
325 return get_hv (name, create); 326 return get_hv (name, create);
327}
328
329/* may croak */
330INLINE CV *
331coro_sv_2cv (SV *sv)
332{
333 HV *st;
334 GV *gvp;
335 return sv_2cv (sv, &st, &gvp, 0);
326} 336}
327 337
328static AV * 338static AV *
329coro_clone_padlist (pTHX_ CV *cv) 339coro_clone_padlist (pTHX_ CV *cv)
330{ 340{
449 else 459 else
450 { 460 {
451#if CORO_PREFER_PERL_FUNCTIONS 461#if CORO_PREFER_PERL_FUNCTIONS
452 /* this is probably cleaner? but also slower! */ 462 /* this is probably cleaner? but also slower! */
453 /* in practise, it seems to be less stable */ 463 /* in practise, it seems to be less stable */
454 CV *cp = Perl_cv_clone (cv); 464 CV *cp = Perl_cv_clone (aTHX_ cv);
455 CvPADLIST (cv) = CvPADLIST (cp); 465 CvPADLIST (cv) = CvPADLIST (cp);
456 CvPADLIST (cp) = 0; 466 CvPADLIST (cp) = 0;
457 SvREFCNT_dec (cp); 467 SvREFCNT_dec (cp);
458#else 468#else
459 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 469 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv);
603 * of perl.c:init_stacks, except that it uses less memory 613 * of perl.c:init_stacks, except that it uses less memory
604 * on the (sometimes correct) assumption that coroutines do 614 * on the (sometimes correct) assumption that coroutines do
605 * not usually need a lot of stackspace. 615 * not usually need a lot of stackspace.
606 */ 616 */
607#if CORO_PREFER_PERL_FUNCTIONS 617#if CORO_PREFER_PERL_FUNCTIONS
608# define coro_init_stacks init_stacks 618# define coro_init_stacks(thx) init_stacks ()
609#else 619#else
610static void 620static void
611coro_init_stacks (pTHX) 621coro_init_stacks (pTHX)
612{ 622{
613 PL_curstackinfo = new_stackinfo(32, 8); 623 PL_curstackinfo = new_stackinfo(32, 8);
1018 SAVETMPS; 1028 SAVETMPS;
1019 EXTEND (SP, 3); 1029 EXTEND (SP, 3);
1020 PUSHMARK (SP); 1030 PUSHMARK (SP);
1021 PUSHs (&PL_sv_yes); 1031 PUSHs (&PL_sv_yes);
1022 PUSHs (fullname); 1032 PUSHs (fullname);
1023 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); 1033 PUSHs (CxHASARGS (cx) ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
1024 PUTBACK; 1034 PUTBACK;
1025 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); 1035 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
1026 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1036 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1027 SPAGAIN; 1037 SPAGAIN;
1028 FREETMPS; 1038 FREETMPS;
1435 1445
1436 coro->slot = 0; 1446 coro->slot = 0;
1437 } 1447 }
1438 1448
1439 cctx_destroy (coro->cctx); 1449 cctx_destroy (coro->cctx);
1450 SvREFCNT_dec (coro->startcv);
1440 SvREFCNT_dec (coro->args); 1451 SvREFCNT_dec (coro->args);
1441 1452
1442 if (coro->next) coro->next->prev = coro->prev; 1453 if (coro->next) coro->next->prev = coro->prev;
1443 if (coro->prev) coro->prev->next = coro->next; 1454 if (coro->prev) coro->prev->next = coro->next;
1444 if (coro == coro_first) coro_first = coro->next; 1455 if (coro == coro_first) coro_first = coro->next;
2143 SV **ary; 2154 SV **ary;
2144 2155
2145 /* unfortunately, building manually saves memory */ 2156 /* unfortunately, building manually saves memory */
2146 Newx (ary, 2, SV *); 2157 Newx (ary, 2, SV *);
2147 AvALLOC (av) = ary; 2158 AvALLOC (av) = ary;
2148 AvARRAY (av) = ary; 2159 /*AvARRAY (av) = ary;*/
2160 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */
2149 AvMAX (av) = 1; 2161 AvMAX (av) = 1;
2150 AvFILLp (av) = 0; 2162 AvFILLp (av) = 0;
2151 ary [0] = newSViv (count); 2163 ary [0] = newSViv (count);
2152 2164
2153 return newRV_noinc ((SV *)av); 2165 return newRV_noinc ((SV *)av);
2173 AvARRAY (av)[0] = AvARRAY (av)[1]; 2185 AvARRAY (av)[0] = AvARRAY (av)[1];
2174 AvARRAY (av)[1] = count_sv; 2186 AvARRAY (av)[1] = count_sv;
2175 cb = av_shift (av); 2187 cb = av_shift (av);
2176 2188
2177 if (SvOBJECT (cb)) 2189 if (SvOBJECT (cb))
2190 {
2178 api_ready (aTHX_ cb); 2191 api_ready (aTHX_ cb);
2179 else 2192 --count;
2180 croak ("callbacks not yet supported"); 2193 }
2194 else if (SvTYPE (cb) == SVt_PVCV)
2195 {
2196 dSP;
2197 PUSHMARK (SP);
2198 XPUSHs (sv_2mortal (newRV_inc ((SV *)av)));
2199 PUTBACK;
2200 call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR);
2201 }
2181 2202
2182 SvREFCNT_dec (cb); 2203 SvREFCNT_dec (cb);
2183
2184 --count;
2185 } 2204 }
2186} 2205}
2187 2206
2188static void 2207static void
2189coro_semaphore_on_destroy (pTHX_ struct coro *coro) 2208coro_semaphore_on_destroy (pTHX_ struct coro *coro)
2271} 2290}
2272 2291
2273static void 2292static void
2274slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2293slf_init_semaphore_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2275{ 2294{
2295 if (items >= 2)
2296 {
2297 /* callback form */
2298 AV *av = (AV *)SvRV (arg [0]);
2299 CV *cb_cv = coro_sv_2cv (arg [1]);
2300
2301 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv));
2302
2303 if (SvIVX (AvARRAY (av)[0]) > 0)
2304 coro_semaphore_adjust (aTHX_ av, 0);
2305
2306 frame->prepare = prepare_nop;
2307 frame->check = slf_check_nop;
2308 }
2309 else
2310 {
2276 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items); 2311 slf_init_semaphore_down_or_wait (aTHX_ frame, cv, arg, items);
2277 frame->check = slf_check_semaphore_wait; 2312 frame->check = slf_check_semaphore_wait;
2313 }
2278} 2314}
2279 2315
2280/* signal */ 2316/* signal */
2281 2317
2282static void 2318static void
2579 CODE: 2615 CODE:
2580{ 2616{
2581 struct coro *coro; 2617 struct coro *coro;
2582 MAGIC *mg; 2618 MAGIC *mg;
2583 HV *hv; 2619 HV *hv;
2620 CV *cb;
2584 int i; 2621 int i;
2622
2623 if (items > 1)
2624 {
2625 cb = coro_sv_2cv (ST (1));
2626
2627 if (CvISXSUB (cb))
2628 croak ("Coro::State doesn't support XS functions as coroutine start, caught");
2629
2630 if (!CvROOT (cb))
2631 croak ("Coro::State doesn't support autoloaded or undefined functions as coroutine start, caught");
2632 }
2585 2633
2586 Newz (0, coro, 1, struct coro); 2634 Newz (0, coro, 1, struct coro);
2587 coro->args = newAV (); 2635 coro->args = newAV ();
2588 coro->flags = CF_NEW; 2636 coro->flags = CF_NEW;
2589 2637
2594 coro->hv = hv = newHV (); 2642 coro->hv = hv = newHV ();
2595 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0); 2643 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
2596 mg->mg_flags |= MGf_DUP; 2644 mg->mg_flags |= MGf_DUP;
2597 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 2645 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2598 2646
2647 if (items > 1)
2648 {
2649 coro->startcv = SvREFCNT_inc_NN (cb);
2650
2599 av_extend (coro->args, items - 1); 2651 av_extend (coro->args, items - 1);
2652 av_push (coro->args, SvREFCNT_inc (cb));
2600 for (i = 1; i < items; i++) 2653 for (i = 2; i < items; i++)
2601 av_push (coro->args, newSVsv (ST (i))); 2654 av_push (coro->args, newSVsv (ST (i)));
2655 }
2602} 2656}
2603 OUTPUT: 2657 OUTPUT:
2604 RETVAL 2658 RETVAL
2605 2659
2606void 2660void
2989 RETVAL = coro_new_rouse_cb (aTHX); 3043 RETVAL = coro_new_rouse_cb (aTHX);
2990 OUTPUT: 3044 OUTPUT:
2991 RETVAL 3045 RETVAL
2992 3046
2993void 3047void
2994rouse_wait (SV *cb = 0) 3048rouse_wait (...)
2995 PROTOTYPE: ;$ 3049 PROTOTYPE: ;$
2996 PPCODE: 3050 PPCODE:
2997 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3051 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
2998 3052
2999 3053
3036 adjust = 1 3090 adjust = 1
3037 CODE: 3091 CODE:
3038 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1); 3092 coro_semaphore_adjust (aTHX_ (AV *)SvRV (self), ix ? adjust : 1);
3039 3093
3040void 3094void
3041down (SV *self) 3095down (...)
3042 CODE: 3096 CODE:
3043 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down); 3097 CORO_EXECUTE_SLF_XS (slf_init_semaphore_down);
3044 3098
3045void 3099void
3046wait (SV *self) 3100wait (...)
3047 CODE: 3101 CODE:
3048 CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait); 3102 CORO_EXECUTE_SLF_XS (slf_init_semaphore_wait);
3049 3103
3050void 3104void
3051try (SV *self) 3105try (SV *self)
3094 ); 3148 );
3095 OUTPUT: 3149 OUTPUT:
3096 RETVAL 3150 RETVAL
3097 3151
3098void 3152void
3099wait (SV *self) 3153wait (...)
3100 CODE: 3154 CODE:
3101 CORO_EXECUTE_SLF_XS (slf_init_signal_wait); 3155 CORO_EXECUTE_SLF_XS (slf_init_signal_wait);
3102 3156
3103void 3157void
3104broadcast (SV *self) 3158broadcast (SV *self)
3161 3215
3162void 3216void
3163_register (char *target, char *proto, SV *req) 3217_register (char *target, char *proto, SV *req)
3164 CODE: 3218 CODE:
3165{ 3219{
3166 HV *st;
3167 GV *gvp;
3168 CV *req_cv = sv_2cv (req, &st, &gvp, 0); 3220 CV *req_cv = coro_sv_2cv (req);
3169 /* newXSproto doesn't return the CV on 5.8 */ 3221 /* newXSproto doesn't return the CV on 5.8 */
3170 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__); 3222 CV *slf_cv = newXS (target, coro_aio_req_xs, __FILE__);
3171 sv_setpv ((SV *)slf_cv, proto); 3223 sv_setpv ((SV *)slf_cv, proto);
3172 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0); 3224 sv_magicext ((SV *)slf_cv, (SV *)req_cv, CORO_MAGIC_type_aio, 0, 0, 0);
3173} 3225}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines