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.341 by root, Mon Dec 15 15:03:31 2008 UTC vs.
Revision 1.347 by root, Wed Jun 10 00:00:02 2009 UTC

261/* this is a structure representing a perl-level coroutine */ 261/* this is a structure representing a perl-level coroutine */
262struct coro { 262struct coro {
263 /* the C coroutine allocated to this perl coroutine, if any */ 263 /* the C coroutine allocated to this perl coroutine, if any */
264 coro_cctx *cctx; 264 coro_cctx *cctx;
265 265
266 /* ready queue */
267 struct coro *next_ready;
268
266 /* state data */ 269 /* state data */
267 struct CoroSLF slf_frame; /* saved slf frame */ 270 struct CoroSLF slf_frame; /* saved slf frame */
268 AV *mainstack; 271 AV *mainstack;
269 perl_slots *slot; /* basically the saved sp */ 272 perl_slots *slot; /* basically the saved sp */
270 273
314#define PRIO_MIN -4 317#define PRIO_MIN -4
315 318
316/* for Coro.pm */ 319/* for Coro.pm */
317static SV *coro_current; 320static SV *coro_current;
318static SV *coro_readyhook; 321static SV *coro_readyhook;
319static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 322static struct coro *coro_ready [PRIO_MAX - PRIO_MIN + 1][2]; /* head|tail */
320static CV *cv_coro_run, *cv_coro_terminate; 323static CV *cv_coro_run, *cv_coro_terminate;
321static struct coro *coro_first; 324static struct coro *coro_first;
322#define coro_nready coroapi.nready 325#define coro_nready coroapi.nready
323 326
324/** lowlevel stuff **********************************************************/ 327/** lowlevel stuff **********************************************************/
1484coro_state_destroy (pTHX_ struct coro *coro) 1487coro_state_destroy (pTHX_ struct coro *coro)
1485{ 1488{
1486 if (coro->flags & CF_DESTROYED) 1489 if (coro->flags & CF_DESTROYED)
1487 return 0; 1490 return 0;
1488 1491
1489 if (coro->on_destroy) 1492 if (coro->on_destroy && !PL_dirty)
1490 coro->on_destroy (aTHX_ coro); 1493 coro->on_destroy (aTHX_ coro);
1491 1494
1492 coro->flags |= CF_DESTROYED; 1495 coro->flags |= CF_DESTROYED;
1493 1496
1494 if (coro->flags & CF_READY) 1497 if (coro->flags & CF_READY)
1612/** Coro ********************************************************************/ 1615/** Coro ********************************************************************/
1613 1616
1614INLINE void 1617INLINE void
1615coro_enq (pTHX_ struct coro *coro) 1618coro_enq (pTHX_ struct coro *coro)
1616{ 1619{
1617 av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); 1620 struct coro **ready = coro_ready [coro->prio - PRIO_MIN];
1618}
1619 1621
1620INLINE SV * 1622 SvREFCNT_inc_NN (coro->hv);
1623
1624 coro->next_ready = 0;
1625 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro;
1626 ready [1] = coro;
1627}
1628
1629INLINE struct coro *
1621coro_deq (pTHX) 1630coro_deq (pTHX)
1622{ 1631{
1623 int prio; 1632 int prio;
1624 1633
1625 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) 1634 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; )
1626 if (AvFILLp (coro_ready [prio]) >= 0) 1635 {
1627 return av_shift (coro_ready [prio]); 1636 struct coro **ready = coro_ready [prio];
1628 1637
1629 return 0; 1638 if (ready [0])
1639 {
1640 struct coro *coro = ready [0];
1641 ready [0] = coro->next_ready;
1642 return coro;
1643 }
1644 }
1630} 1645}
1631 1646
1632static int 1647static int
1633api_ready (pTHX_ SV *coro_sv) 1648api_ready (pTHX_ SV *coro_sv)
1634{ 1649{
1696static void 1711static void
1697prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1712prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1698{ 1713{
1699 for (;;) 1714 for (;;)
1700 { 1715 {
1701 SV *next_sv = coro_deq (aTHX); 1716 struct coro *next = coro_deq (aTHX);
1702 1717
1703 if (expect_true (next_sv)) 1718 if (expect_true (next))
1704 { 1719 {
1705 struct coro *next = SvSTATE_hv (next_sv);
1706
1707 /* cannot transfer to destroyed coros, skip and look for next */ 1720 /* cannot transfer to destroyed coros, skip and look for next */
1708 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED))) 1721 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1709 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ 1722 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */
1710 else 1723 else
1711 { 1724 {
1712 next->flags &= ~CF_READY; 1725 next->flags &= ~CF_READY;
1713 --coro_nready; 1726 --coro_nready;
1714 1727
1988 SV *data = (SV *)GENSUB_ARG; 2001 SV *data = (SV *)GENSUB_ARG;
1989 2002
1990 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2003 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1991 { 2004 {
1992 /* first call, set args */ 2005 /* first call, set args */
2006 SV *coro = SvRV (data);
1993 AV *av = newAV (); 2007 AV *av = newAV ();
1994 SV *coro = SvRV (data);
1995 2008
1996 SvRV_set (data, (SV *)av); 2009 SvRV_set (data, (SV *)av);
1997 api_ready (aTHX_ coro);
1998 SvREFCNT_dec (coro);
1999 2010
2000 /* better take a full copy of the arguments */ 2011 /* better take a full copy of the arguments */
2001 while (items--) 2012 while (items--)
2002 av_store (av, items, newSVsv (ST (items))); 2013 av_store (av, items, newSVsv (ST (items)));
2014
2015 api_ready (aTHX_ coro);
2016 SvREFCNT_dec (coro);
2003 } 2017 }
2004 2018
2005 XSRETURN_EMPTY; 2019 XSRETURN_EMPTY;
2006} 2020}
2007 2021
2024 2038
2025 EXTEND (SP, AvFILLp (av) + 1); 2039 EXTEND (SP, AvFILLp (av) + 1);
2026 for (i = 0; i <= AvFILLp (av); ++i) 2040 for (i = 0; i <= AvFILLp (av); ++i)
2027 PUSHs (sv_2mortal (AvARRAY (av)[i])); 2041 PUSHs (sv_2mortal (AvARRAY (av)[i]));
2028 2042
2029 /* we have stolen the elements, so ste length to zero and free */ 2043 /* we have stolen the elements, so set length to zero and free */
2030 AvFILLp (av) = -1; 2044 AvFILLp (av) = -1;
2031 av_undef (av); 2045 av_undef (av);
2032 2046
2033 PUTBACK; 2047 PUTBACK;
2034 } 2048 }
3098 if (ix) 3112 if (ix)
3099 eval_sv (coderef, 0); 3113 eval_sv (coderef, 0);
3100 else 3114 else
3101 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3115 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
3102 3116
3117 POPSTACK;
3103 SPAGAIN; 3118 SPAGAIN;
3104 POPSTACK;
3105 3119
3106 if (current != coro) 3120 if (current != coro)
3107 { 3121 {
3108 PUTBACK; 3122 PUTBACK;
3109 save_perl (aTHX_ coro); 3123 save_perl (aTHX_ coro);
3231 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 3245 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
3232 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 3246 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
3233 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 3247 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
3234 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 3248 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
3235 3249
3236 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
3237 coro_ready[i] = newAV ();
3238
3239 { 3250 {
3240 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3251 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
3241 3252
3242 coroapi.schedule = api_schedule; 3253 coroapi.schedule = api_schedule;
3243 coroapi.schedule_to = api_schedule_to; 3254 coroapi.schedule_to = api_schedule_to;
3334 PROTOTYPE: 3345 PROTOTYPE:
3335 CODE: 3346 CODE:
3336 RETVAL = coro_nready; 3347 RETVAL = coro_nready;
3337 OUTPUT: 3348 OUTPUT:
3338 RETVAL 3349 RETVAL
3350
3351void
3352suspend (Coro::State self)
3353 PROTOTYPE: $
3354 CODE:
3355 self->flags |= CF_SUSPENDED;
3356
3357void
3358resume (Coro::State self)
3359 PROTOTYPE: $
3360 CODE:
3361 self->flags &= ~CF_SUSPENDED;
3339 3362
3340void 3363void
3341_pool_handler (...) 3364_pool_handler (...)
3342 CODE: 3365 CODE:
3343 CORO_EXECUTE_SLF_XS (slf_init_pool_handler); 3366 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
3418 av_push (*avp, SvREFCNT_inc (block)); 3441 av_push (*avp, SvREFCNT_inc (block));
3419 3442
3420 if (!ix) 3443 if (!ix)
3421 on_enterleave_call (aTHX_ block); 3444 on_enterleave_call (aTHX_ block);
3422 3445
3423 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */ 3446 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3424 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro); 3447 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3425 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */ 3448 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3426} 3449}
3427 3450
3428 3451
3429MODULE = Coro::State PACKAGE = PerlIO::cede 3452MODULE = Coro::State PACKAGE = PerlIO::cede
3430 3453
3442 GvSTASH (CvGV (cv)) 3465 GvSTASH (CvGV (cv))
3443 ); 3466 );
3444 OUTPUT: 3467 OUTPUT:
3445 RETVAL 3468 RETVAL
3446 3469
3447# helper for Coro::Channel 3470# helper for Coro::Channel and others
3448SV * 3471SV *
3449_alloc (int count) 3472_alloc (int count)
3450 CODE: 3473 CODE:
3451 RETVAL = coro_waitarray_new (aTHX_ count); 3474 RETVAL = coro_waitarray_new (aTHX_ count);
3452 OUTPUT: 3475 OUTPUT:
3510 for (i = 1; i <= wcount; ++i) 3533 for (i = 1; i <= wcount; ++i)
3511 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3534 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
3512 } 3535 }
3513} 3536}
3514 3537
3538MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
3539
3540void
3541_may_delete (SV *sem, int count, int extra_refs)
3542 PPCODE:
3543{
3544 AV *av = (AV *)SvRV (sem);
3545
3546 if (SvREFCNT ((SV *)av) == 1 + extra_refs
3547 && AvFILLp (av) == 0 /* no waiters, just count */
3548 && SvIV (AvARRAY (av)[0]) == count)
3549 XSRETURN_YES;
3550
3551 XSRETURN_NO;
3552}
3553
3515MODULE = Coro::State PACKAGE = Coro::Signal 3554MODULE = Coro::State PACKAGE = Coro::Signal
3516 3555
3517SV * 3556SV *
3518new (SV *klass) 3557new (SV *klass)
3519 CODE: 3558 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines