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.352 by root, Sat Jun 20 08:58:50 2009 UTC vs.
Revision 1.357 by root, Sat Jun 27 14:09:28 2009 UTC

137 137
138#define IN_DESTRUCT PL_dirty 138#define IN_DESTRUCT PL_dirty
139 139
140#if __GNUC__ >= 3 140#if __GNUC__ >= 3
141# define attribute(x) __attribute__(x) 141# define attribute(x) __attribute__(x)
142# define expect(expr,value) __builtin_expect ((expr),(value)) 142# define expect(expr,value) __builtin_expect ((expr), (value))
143# define INLINE static inline 143# define INLINE static inline
144#else 144#else
145# define attribute(x) 145# define attribute(x)
146# define expect(expr,value) (expr) 146# define expect(expr,value) (expr)
147# define INLINE static 147# define INLINE static
615 { 615 {
616 while (expect_true (cxix >= 0)) 616 while (expect_true (cxix >= 0))
617 { 617 {
618 PERL_CONTEXT *cx = &ccstk[cxix--]; 618 PERL_CONTEXT *cx = &ccstk[cxix--];
619 619
620 if (expect_true (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)) 620 if (expect_true (CxTYPE (cx) == CXt_SUB) || expect_false (CxTYPE (cx) == CXt_FORMAT))
621 { 621 {
622 CV *cv = cx->blk_sub.cv; 622 CV *cv = cx->blk_sub.cv;
623 623
624 if (expect_true (CvDEPTH (cv))) 624 if (expect_true (CvDEPTH (cv)))
625 { 625 {
2477/* Coro::Semaphore & Coro::Signal */ 2477/* Coro::Semaphore & Coro::Signal */
2478 2478
2479static SV * 2479static SV *
2480coro_waitarray_new (pTHX_ int count) 2480coro_waitarray_new (pTHX_ int count)
2481{ 2481{
2482 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ 2482 /* a waitarray=semaphore contains a counter IV in $sem->[0] and any waiters after that */
2483 AV *av = newAV (); 2483 AV *av = newAV ();
2484 SV **ary; 2484 SV **ary;
2485 2485
2486 /* unfortunately, building manually saves memory */ 2486 /* unfortunately, building manually saves memory */
2487 Newx (ary, 2, SV *); 2487 Newx (ary, 2, SV *);
2631 { 2631 {
2632 /* callback form */ 2632 /* callback form */
2633 AV *av = (AV *)SvRV (arg [0]); 2633 AV *av = (AV *)SvRV (arg [0]);
2634 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); 2634 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2635 2635
2636 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); 2636 av_push (av, SvREFCNT_inc_NN (cb_cv));
2637 2637
2638 if (SvIVX (AvARRAY (av)[0]) > 0) 2638 if (SvIVX (AvARRAY (av)[0]) > 0)
2639 coro_semaphore_adjust (aTHX_ av, 0); 2639 coro_semaphore_adjust (aTHX_ av, 0);
2640 2640
2641 frame->prepare = prepare_nop; 2641 frame->prepare = prepare_nop;
2665 AvARRAY (av)[0] = AvARRAY (av)[1]; 2665 AvARRAY (av)[0] = AvARRAY (av)[1];
2666 AvARRAY (av)[1] = cb; 2666 AvARRAY (av)[1] = cb;
2667 2667
2668 cb = av_shift (av); 2668 cb = av_shift (av);
2669 2669
2670 if (SvTYPE (cb) == SVt_PVCV)
2671 {
2672 dSP;
2673 PUSHMARK (SP);
2674 XPUSHs (sv_2mortal (newRV_inc ((SV *)av)));
2675 PUTBACK;
2676 call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR);
2677 }
2678 else
2679 {
2670 api_ready (aTHX_ cb); 2680 api_ready (aTHX_ cb);
2671 sv_setiv (cb, 0); /* signal waiter */ 2681 sv_setiv (cb, 0); /* signal waiter */
2682 }
2683
2672 SvREFCNT_dec (cb); 2684 SvREFCNT_dec (cb);
2673 2685
2674 --count; 2686 --count;
2675 } 2687 }
2676} 2688}
2685static void 2697static void
2686slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2698slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2687{ 2699{
2688 AV *av = (AV *)SvRV (arg [0]); 2700 AV *av = (AV *)SvRV (arg [0]);
2689 2701
2702 if (items >= 2)
2703 {
2704 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2705 av_push (av, SvREFCNT_inc_NN (cb_cv));
2706
2690 if (SvIVX (AvARRAY (av)[0])) 2707 if (SvIVX (AvARRAY (av)[0]))
2708 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */
2709
2710 frame->prepare = prepare_nop;
2711 frame->check = slf_check_nop;
2712 }
2713 else if (SvIVX (AvARRAY (av)[0]))
2691 { 2714 {
2692 SvIVX (AvARRAY (av)[0]) = 0; 2715 SvIVX (AvARRAY (av)[0]) = 0;
2693 frame->prepare = prepare_nop; 2716 frame->prepare = prepare_nop;
2694 frame->check = slf_check_nop; 2717 frame->check = slf_check_nop;
2695 } 2718 }
2696 else 2719 else
2697 { 2720 {
2698 SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */ 2721 SV *waiter = newSVsv (coro_current); /* owned by signal av */
2699 2722
2700 av_push (av, waiter); 2723 av_push (av, waiter);
2701 2724
2702 frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ 2725 frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */
2703 frame->prepare = prepare_schedule; 2726 frame->prepare = prepare_schedule;
3156 CODE: 3179 CODE:
3157{ 3180{
3158 struct coro *current = SvSTATE_current; 3181 struct coro *current = SvSTATE_current;
3159 SV **throwp = self == current ? &CORO_THROW : &self->except; 3182 SV **throwp = self == current ? &CORO_THROW : &self->except;
3160 SvREFCNT_dec (*throwp); 3183 SvREFCNT_dec (*throwp);
3184 SvGETMAGIC (throw);
3161 *throwp = SvOK (throw) ? newSVsv (throw) : 0; 3185 *throwp = SvOK (throw) ? newSVsv (throw) : 0;
3162} 3186}
3163 3187
3164void 3188void
3165api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3189api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3312void 3336void
3313_set_readyhook (SV *hook) 3337_set_readyhook (SV *hook)
3314 PROTOTYPE: $ 3338 PROTOTYPE: $
3315 CODE: 3339 CODE:
3316 SvREFCNT_dec (coro_readyhook); 3340 SvREFCNT_dec (coro_readyhook);
3341 SvGETMAGIC (hook);
3317 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0; 3342 coro_readyhook = SvOK (hook) ? newSVsv (hook) : 0;
3318 3343
3319int 3344int
3320prio (Coro::State coro, int newprio = 0) 3345prio (Coro::State coro, int newprio = 0)
3321 PROTOTYPE: $;$ 3346 PROTOTYPE: $;$
3465MODULE = Coro::State PACKAGE = Coro::Semaphore 3490MODULE = Coro::State PACKAGE = Coro::Semaphore
3466 3491
3467SV * 3492SV *
3468new (SV *klass, SV *count = 0) 3493new (SV *klass, SV *count = 0)
3469 CODE: 3494 CODE:
3495{
3496 int semcnt = 1;
3497
3498 if (count)
3499 {
3500 SvGETMAGIC (count);
3501
3502 if (SvOK (count))
3503 semcnt = SvIV (count);
3504 }
3505
3470 RETVAL = sv_bless ( 3506 RETVAL = sv_bless (
3471 coro_waitarray_new (aTHX_ count && SvOK (count) ? SvIV (count) : 1), 3507 coro_waitarray_new (aTHX_ semcnt),
3472 GvSTASH (CvGV (cv)) 3508 GvSTASH (CvGV (cv))
3473 ); 3509 );
3510}
3474 OUTPUT: 3511 OUTPUT:
3475 RETVAL 3512 RETVAL
3476 3513
3477# helper for Coro::Channel and others 3514# helper for Coro::Channel and others
3478SV * 3515SV *

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines