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.346 by root, Tue Jun 9 18:56:45 2009 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 **********************************************************/
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
3232 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 3245 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
3233 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 3246 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
3234 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 3247 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
3235 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 3248 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
3236 3249
3237 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
3238 coro_ready[i] = newAV ();
3239
3240 { 3250 {
3241 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3251 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
3242 3252
3243 coroapi.schedule = api_schedule; 3253 coroapi.schedule = api_schedule;
3244 coroapi.schedule_to = api_schedule_to; 3254 coroapi.schedule_to = api_schedule_to;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines