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

238enum { 238enum {
239 CF_RUNNING = 0x0001, /* coroutine is running */ 239 CF_RUNNING = 0x0001, /* coroutine is running */
240 CF_READY = 0x0002, /* coroutine is ready */ 240 CF_READY = 0x0002, /* coroutine is ready */
241 CF_NEW = 0x0004, /* has never been switched to */ 241 CF_NEW = 0x0004, /* has never been switched to */
242 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 242 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
243 CF_SUSPENDED = 0x0010, /* coroutine can't be scheduled */
243}; 244};
244 245
245/* the structure where most of the perl state is stored, overlaid on the cxstack */ 246/* the structure where most of the perl state is stored, overlaid on the cxstack */
246typedef struct 247typedef struct
247{ 248{
260/* this is a structure representing a perl-level coroutine */ 261/* this is a structure representing a perl-level coroutine */
261struct coro { 262struct coro {
262 /* the C coroutine allocated to this perl coroutine, if any */ 263 /* the C coroutine allocated to this perl coroutine, if any */
263 coro_cctx *cctx; 264 coro_cctx *cctx;
264 265
266 /* ready queue */
267 struct coro *next_ready;
268
265 /* state data */ 269 /* state data */
266 struct CoroSLF slf_frame; /* saved slf frame */ 270 struct CoroSLF slf_frame; /* saved slf frame */
267 AV *mainstack; 271 AV *mainstack;
268 perl_slots *slot; /* basically the saved sp */ 272 perl_slots *slot; /* basically the saved sp */
269 273
313#define PRIO_MIN -4 317#define PRIO_MIN -4
314 318
315/* for Coro.pm */ 319/* for Coro.pm */
316static SV *coro_current; 320static SV *coro_current;
317static SV *coro_readyhook; 321static SV *coro_readyhook;
318static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 322static struct coro *coro_ready [PRIO_MAX - PRIO_MIN + 1][2]; /* head|tail */
319static CV *cv_coro_run, *cv_coro_terminate; 323static CV *cv_coro_run, *cv_coro_terminate;
320static struct coro *coro_first; 324static struct coro *coro_first;
321#define coro_nready coroapi.nready 325#define coro_nready coroapi.nready
322 326
323/** lowlevel stuff **********************************************************/ 327/** lowlevel stuff **********************************************************/
1382 /* TODO: throwing up here is considered harmful */ 1386 /* TODO: throwing up here is considered harmful */
1383 1387
1384 if (expect_true (prev != next)) 1388 if (expect_true (prev != next))
1385 { 1389 {
1386 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1390 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1387 croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,"); 1391 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,");
1388 1392
1389 if (expect_false (next->flags & CF_RUNNING))
1390 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1391
1392 if (expect_false (next->flags & CF_DESTROYED)) 1393 if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED)))
1393 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states,"); 1394 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,");
1394 1395
1395#if !PERL_VERSION_ATLEAST (5,10,0) 1396#if !PERL_VERSION_ATLEAST (5,10,0)
1396 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1397 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1397 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); 1398 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
1398#endif 1399#endif
1486coro_state_destroy (pTHX_ struct coro *coro) 1487coro_state_destroy (pTHX_ struct coro *coro)
1487{ 1488{
1488 if (coro->flags & CF_DESTROYED) 1489 if (coro->flags & CF_DESTROYED)
1489 return 0; 1490 return 0;
1490 1491
1491 if (coro->on_destroy) 1492 if (coro->on_destroy && !PL_dirty)
1492 coro->on_destroy (aTHX_ coro); 1493 coro->on_destroy (aTHX_ coro);
1493 1494
1494 coro->flags |= CF_DESTROYED; 1495 coro->flags |= CF_DESTROYED;
1495 1496
1496 if (coro->flags & CF_READY) 1497 if (coro->flags & CF_READY)
1614/** Coro ********************************************************************/ 1615/** Coro ********************************************************************/
1615 1616
1616INLINE void 1617INLINE void
1617coro_enq (pTHX_ struct coro *coro) 1618coro_enq (pTHX_ struct coro *coro)
1618{ 1619{
1619 av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); 1620 struct coro **ready = coro_ready [coro->prio - PRIO_MIN];
1620}
1621 1621
1622INLINE 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 *
1623coro_deq (pTHX) 1630coro_deq (pTHX)
1624{ 1631{
1625 int prio; 1632 int prio;
1626 1633
1627 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) 1634 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; )
1628 if (AvFILLp (coro_ready [prio]) >= 0) 1635 {
1629 return av_shift (coro_ready [prio]); 1636 struct coro **ready = coro_ready [prio];
1630 1637
1631 return 0; 1638 if (ready [0])
1639 {
1640 struct coro *coro = ready [0];
1641 ready [0] = coro->next_ready;
1642 return coro;
1643 }
1644 }
1632} 1645}
1633 1646
1634static int 1647static int
1635api_ready (pTHX_ SV *coro_sv) 1648api_ready (pTHX_ SV *coro_sv)
1636{ 1649{
1698static void 1711static void
1699prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1712prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1700{ 1713{
1701 for (;;) 1714 for (;;)
1702 { 1715 {
1703 SV *next_sv = coro_deq (aTHX); 1716 struct coro *next = coro_deq (aTHX);
1704 1717
1705 if (expect_true (next_sv)) 1718 if (expect_true (next))
1706 { 1719 {
1707 struct coro *next = SvSTATE_hv (next_sv);
1708
1709 /* cannot transfer to destroyed coros, skip and look for next */ 1720 /* cannot transfer to destroyed coros, skip and look for next */
1710 if (expect_false (next->flags & CF_DESTROYED)) 1721 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1711 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 */
1712 else 1723 else
1713 { 1724 {
1714 next->flags &= ~CF_READY; 1725 next->flags &= ~CF_READY;
1715 --coro_nready; 1726 --coro_nready;
1716 1727
1990 SV *data = (SV *)GENSUB_ARG; 2001 SV *data = (SV *)GENSUB_ARG;
1991 2002
1992 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2003 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1993 { 2004 {
1994 /* first call, set args */ 2005 /* first call, set args */
2006 SV *coro = SvRV (data);
1995 AV *av = newAV (); 2007 AV *av = newAV ();
1996 SV *coro = SvRV (data);
1997 2008
1998 SvRV_set (data, (SV *)av); 2009 SvRV_set (data, (SV *)av);
1999 api_ready (aTHX_ coro);
2000 SvREFCNT_dec (coro);
2001 2010
2002 /* better take a full copy of the arguments */ 2011 /* better take a full copy of the arguments */
2003 while (items--) 2012 while (items--)
2004 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);
2005 } 2017 }
2006 2018
2007 XSRETURN_EMPTY; 2019 XSRETURN_EMPTY;
2008} 2020}
2009 2021
2026 2038
2027 EXTEND (SP, AvFILLp (av) + 1); 2039 EXTEND (SP, AvFILLp (av) + 1);
2028 for (i = 0; i <= AvFILLp (av); ++i) 2040 for (i = 0; i <= AvFILLp (av); ++i)
2029 PUSHs (sv_2mortal (AvARRAY (av)[i])); 2041 PUSHs (sv_2mortal (AvARRAY (av)[i]));
2030 2042
2031 /* 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 */
2032 AvFILLp (av) = -1; 2044 AvFILLp (av) = -1;
2033 av_undef (av); 2045 av_undef (av);
2034 2046
2035 PUTBACK; 2047 PUTBACK;
2036 } 2048 }
3100 if (ix) 3112 if (ix)
3101 eval_sv (coderef, 0); 3113 eval_sv (coderef, 0);
3102 else 3114 else
3103 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3115 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
3104 3116
3117 POPSTACK;
3105 SPAGAIN; 3118 SPAGAIN;
3106 POPSTACK;
3107 3119
3108 if (current != coro) 3120 if (current != coro)
3109 { 3121 {
3110 PUTBACK; 3122 PUTBACK;
3111 save_perl (aTHX_ coro); 3123 save_perl (aTHX_ coro);
3121 ALIAS: 3133 ALIAS:
3122 is_ready = CF_READY 3134 is_ready = CF_READY
3123 is_running = CF_RUNNING 3135 is_running = CF_RUNNING
3124 is_new = CF_NEW 3136 is_new = CF_NEW
3125 is_destroyed = CF_DESTROYED 3137 is_destroyed = CF_DESTROYED
3138 is_suspended = CF_SUSPENDED
3126 CODE: 3139 CODE:
3127 RETVAL = boolSV (coro->flags & ix); 3140 RETVAL = boolSV (coro->flags & ix);
3128 OUTPUT: 3141 OUTPUT:
3129 RETVAL 3142 RETVAL
3130 3143
3195 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3208 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
3196 3209
3197 SV *tmp = *src; *src = *dst; *dst = tmp; 3210 SV *tmp = *src; *src = *dst; *dst = tmp;
3198 } 3211 }
3199 3212
3213void
3214cancel (Coro::State self)
3215 CODE:
3216 coro_state_destroy (aTHX_ self);
3217 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3218
3200 3219
3201MODULE = Coro::State PACKAGE = Coro 3220MODULE = Coro::State PACKAGE = Coro
3202 3221
3203BOOT: 3222BOOT:
3204{ 3223{
3226 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 3245 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
3227 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 3246 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
3228 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 3247 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
3229 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 3248 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
3230 3249
3231 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
3232 coro_ready[i] = newAV ();
3233
3234 { 3250 {
3235 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3251 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
3236 3252
3237 coroapi.schedule = api_schedule; 3253 coroapi.schedule = api_schedule;
3238 coroapi.schedule_to = api_schedule_to; 3254 coroapi.schedule_to = api_schedule_to;
3278cede_notself (...) 3294cede_notself (...)
3279 CODE: 3295 CODE:
3280 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3296 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3281 3297
3282void 3298void
3283_cancel (Coro::State self)
3284 CODE:
3285 coro_state_destroy (aTHX_ self);
3286 coro_call_on_destroy (aTHX_ self);
3287
3288void
3289_set_current (SV *current) 3299_set_current (SV *current)
3290 PROTOTYPE: $ 3300 PROTOTYPE: $
3291 CODE: 3301 CODE:
3292 SvREFCNT_dec (SvRV (coro_current)); 3302 SvREFCNT_dec (SvRV (coro_current));
3293 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); 3303 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
3335 PROTOTYPE: 3345 PROTOTYPE:
3336 CODE: 3346 CODE:
3337 RETVAL = coro_nready; 3347 RETVAL = coro_nready;
3338 OUTPUT: 3348 OUTPUT:
3339 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;
3340 3362
3341void 3363void
3342_pool_handler (...) 3364_pool_handler (...)
3343 CODE: 3365 CODE:
3344 CORO_EXECUTE_SLF_XS (slf_init_pool_handler); 3366 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
3419 av_push (*avp, SvREFCNT_inc (block)); 3441 av_push (*avp, SvREFCNT_inc (block));
3420 3442
3421 if (!ix) 3443 if (!ix)
3422 on_enterleave_call (aTHX_ block); 3444 on_enterleave_call (aTHX_ block);
3423 3445
3424 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */ 3446 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3425 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);
3426 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around xs calls */ 3448 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3427} 3449}
3428 3450
3429 3451
3430MODULE = Coro::State PACKAGE = PerlIO::cede 3452MODULE = Coro::State PACKAGE = PerlIO::cede
3431 3453
3443 GvSTASH (CvGV (cv)) 3465 GvSTASH (CvGV (cv))
3444 ); 3466 );
3445 OUTPUT: 3467 OUTPUT:
3446 RETVAL 3468 RETVAL
3447 3469
3448# helper for Coro::Channel 3470# helper for Coro::Channel and others
3449SV * 3471SV *
3450_alloc (int count) 3472_alloc (int count)
3451 CODE: 3473 CODE:
3452 RETVAL = coro_waitarray_new (aTHX_ count); 3474 RETVAL = coro_waitarray_new (aTHX_ count);
3453 OUTPUT: 3475 OUTPUT:
3511 for (i = 1; i <= wcount; ++i) 3533 for (i = 1; i <= wcount; ++i)
3512 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3534 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
3513 } 3535 }
3514} 3536}
3515 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
3516MODULE = Coro::State PACKAGE = Coro::Signal 3554MODULE = Coro::State PACKAGE = Coro::Signal
3517 3555
3518SV * 3556SV *
3519new (SV *klass) 3557new (SV *klass)
3520 CODE: 3558 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines