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.336 by root, Thu Dec 4 17:29:40 2008 UTC vs.
Revision 1.349 by root, Wed Jun 10 00:47:24 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
285 /* async_pool */ 289 /* async_pool */
286 SV *saved_deffh; 290 SV *saved_deffh;
287 SV *invoke_cb; 291 SV *invoke_cb;
288 AV *invoke_av; 292 AV *invoke_av;
289 293
294 /* on_enter/on_leave */
295 AV *on_enter;
296 AV *on_leave;
297
290 /* linked list */ 298 /* linked list */
291 struct coro *next, *prev; 299 struct coro *next, *prev;
292}; 300};
293 301
294typedef struct coro *Coro__State; 302typedef struct coro *Coro__State;
309#define PRIO_MIN -4 317#define PRIO_MIN -4
310 318
311/* for Coro.pm */ 319/* for Coro.pm */
312static SV *coro_current; 320static SV *coro_current;
313static SV *coro_readyhook; 321static SV *coro_readyhook;
314static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1]; 322static struct coro *coro_ready [PRIO_MAX - PRIO_MIN + 1][2]; /* head|tail */
315static CV *cv_coro_run, *cv_coro_terminate; 323static CV *cv_coro_run, *cv_coro_terminate;
316static struct coro *coro_first; 324static struct coro *coro_first;
317#define coro_nready coroapi.nready 325#define coro_nready coroapi.nready
318 326
319/** lowlevel stuff **********************************************************/ 327/** lowlevel stuff **********************************************************/
352INLINE CV * 360INLINE CV *
353coro_sv_2cv (pTHX_ SV *sv) 361coro_sv_2cv (pTHX_ SV *sv)
354{ 362{
355 HV *st; 363 HV *st;
356 GV *gvp; 364 GV *gvp;
357 return sv_2cv (sv, &st, &gvp, 0); 365 CV *cv = sv_2cv (sv, &st, &gvp, 0);
366
367 if (!cv)
368 croak ("code reference expected");
369
370 return cv;
358} 371}
359 372
360/*****************************************************************************/ 373/*****************************************************************************/
361/* magic glue */ 374/* magic glue */
362 375
521} 534}
522 535
523/** load & save, init *******************************************************/ 536/** load & save, init *******************************************************/
524 537
525static void 538static void
539on_enterleave_call (pTHX_ SV *cb);
540
541static void
526load_perl (pTHX_ Coro__State c) 542load_perl (pTHX_ Coro__State c)
527{ 543{
528 perl_slots *slot = c->slot; 544 perl_slots *slot = c->slot;
529 c->slot = 0; 545 c->slot = 0;
530 546
556 PUTBACK; 572 PUTBACK;
557 } 573 }
558 574
559 slf_frame = c->slf_frame; 575 slf_frame = c->slf_frame;
560 CORO_THROW = c->except; 576 CORO_THROW = c->except;
577
578 if (expect_false (c->on_enter))
579 {
580 int i;
581
582 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
583 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
584 }
561} 585}
562 586
563static void 587static void
564save_perl (pTHX_ Coro__State c) 588save_perl (pTHX_ Coro__State c)
565{ 589{
590 if (expect_false (c->on_leave))
591 {
592 int i;
593
594 for (i = AvFILLp (c->on_leave); i >= 0; --i)
595 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]);
596 }
597
566 c->except = CORO_THROW; 598 c->except = CORO_THROW;
567 c->slf_frame = slf_frame; 599 c->slf_frame = slf_frame;
568 600
569 { 601 {
570 dSP; 602 dSP;
769#endif 801#endif
770 802
771/* 803/*
772 * This overrides the default magic get method of %SIG elements. 804 * This overrides the default magic get method of %SIG elements.
773 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 805 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
774 * and instead of tryign to save and restore the hash elements, we just provide 806 * and instead of trying to save and restore the hash elements, we just provide
775 * readback here. 807 * readback here.
776 * We only do this when the hook is != 0, as they are often set to 0 temporarily,
777 * not expecting this to actually change the hook. This is a potential problem
778 * when a schedule happens then, but we ignore this.
779 */ 808 */
780static int 809static int
781coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 810coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
782{ 811{
783 const char *s = MgPV_nolen_const (mg); 812 const char *s = MgPV_nolen_const (mg);
836 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 865 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
837 866
838 if (svp) 867 if (svp)
839 { 868 {
840 SV *old = *svp; 869 SV *old = *svp;
841 *svp = newSVsv (sv); 870 *svp = SvOK (sv) ? newSVsv (sv) : 0;
842 SvREFCNT_dec (old); 871 SvREFCNT_dec (old);
843 return 0; 872 return 0;
844 } 873 }
845 } 874 }
846 875
938 /* copy throw, in case it was set before coro_setup */ 967 /* copy throw, in case it was set before coro_setup */
939 CORO_THROW = coro->except; 968 CORO_THROW = coro->except;
940} 969}
941 970
942static void 971static void
943coro_destruct_perl (pTHX_ struct coro *coro) 972coro_unwind_stacks (pTHX)
944{ 973{
945 if (!IN_DESTRUCT) 974 if (!IN_DESTRUCT)
946 { 975 {
947 /* restore all saved variables and stuff */ 976 /* restore all saved variables and stuff */
948 LEAVE_SCOPE (0); 977 LEAVE_SCOPE (0);
956 POPSTACK_TO (PL_mainstack); 985 POPSTACK_TO (PL_mainstack);
957 986
958 /* unwind main stack */ 987 /* unwind main stack */
959 dounwind (-1); 988 dounwind (-1);
960 } 989 }
990}
991
992static void
993coro_destruct_perl (pTHX_ struct coro *coro)
994{
995 coro_unwind_stacks (aTHX);
961 996
962 SvREFCNT_dec (GvSV (PL_defgv)); 997 SvREFCNT_dec (GvSV (PL_defgv));
963 SvREFCNT_dec (GvAV (PL_defgv)); 998 SvREFCNT_dec (GvAV (PL_defgv));
964 SvREFCNT_dec (GvSV (PL_errgv)); 999 SvREFCNT_dec (GvSV (PL_errgv));
965 SvREFCNT_dec (PL_defoutgv); 1000 SvREFCNT_dec (PL_defoutgv);
1351 /* TODO: throwing up here is considered harmful */ 1386 /* TODO: throwing up here is considered harmful */
1352 1387
1353 if (expect_true (prev != next)) 1388 if (expect_true (prev != next))
1354 { 1389 {
1355 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1390 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1356 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,");
1357 1392
1358 if (expect_false (next->flags & CF_RUNNING))
1359 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1360
1361 if (expect_false (next->flags & CF_DESTROYED)) 1393 if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED)))
1362 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,");
1363 1395
1364#if !PERL_VERSION_ATLEAST (5,10,0) 1396#if !PERL_VERSION_ATLEAST (5,10,0)
1365 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1397 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1366 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,");
1367#endif 1399#endif
1455coro_state_destroy (pTHX_ struct coro *coro) 1487coro_state_destroy (pTHX_ struct coro *coro)
1456{ 1488{
1457 if (coro->flags & CF_DESTROYED) 1489 if (coro->flags & CF_DESTROYED)
1458 return 0; 1490 return 0;
1459 1491
1460 if (coro->on_destroy) 1492 if (coro->on_destroy && !PL_dirty)
1461 coro->on_destroy (aTHX_ coro); 1493 coro->on_destroy (aTHX_ coro);
1462 1494
1463 coro->flags |= CF_DESTROYED; 1495 coro->flags |= CF_DESTROYED;
1464 1496
1465 if (coro->flags & CF_READY) 1497 if (coro->flags & CF_READY)
1474 if (coro->mainstack 1506 if (coro->mainstack
1475 && coro->mainstack != main_mainstack 1507 && coro->mainstack != main_mainstack
1476 && coro->slot 1508 && coro->slot
1477 && !PL_dirty) 1509 && !PL_dirty)
1478 { 1510 {
1479 struct coro temp; 1511 struct coro *current = SvSTATE_current;
1480 1512
1481 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack)); 1513 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1482 1514
1483 save_perl (aTHX_ &temp); 1515 save_perl (aTHX_ current);
1484 load_perl (aTHX_ coro); 1516 load_perl (aTHX_ coro);
1485 1517
1486 coro_destruct_perl (aTHX_ coro); 1518 coro_destruct_perl (aTHX_ coro);
1487 1519
1488 load_perl (aTHX_ &temp); 1520 load_perl (aTHX_ current);
1489 1521
1490 coro->slot = 0; 1522 coro->slot = 0;
1491 } 1523 }
1492 1524
1493 cctx_destroy (coro->cctx); 1525 cctx_destroy (coro->cctx);
1583/** Coro ********************************************************************/ 1615/** Coro ********************************************************************/
1584 1616
1585INLINE void 1617INLINE void
1586coro_enq (pTHX_ struct coro *coro) 1618coro_enq (pTHX_ struct coro *coro)
1587{ 1619{
1588 av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); 1620 struct coro **ready = coro_ready [coro->prio - PRIO_MIN];
1589}
1590 1621
1591INLINE 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 *
1592coro_deq (pTHX) 1630coro_deq (pTHX)
1593{ 1631{
1594 int prio; 1632 int prio;
1595 1633
1596 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) 1634 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; )
1597 if (AvFILLp (coro_ready [prio]) >= 0) 1635 {
1598 return av_shift (coro_ready [prio]); 1636 struct coro **ready = coro_ready [prio];
1637
1638 if (ready [0])
1639 {
1640 struct coro *coro = ready [0];
1641 ready [0] = coro->next_ready;
1642 return coro;
1643 }
1644 }
1599 1645
1600 return 0; 1646 return 0;
1601} 1647}
1602 1648
1603static int 1649static int
1667static void 1713static void
1668prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1714prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1669{ 1715{
1670 for (;;) 1716 for (;;)
1671 { 1717 {
1672 SV *next_sv = coro_deq (aTHX); 1718 struct coro *next = coro_deq (aTHX);
1673 1719
1674 if (expect_true (next_sv)) 1720 if (expect_true (next))
1675 { 1721 {
1676 struct coro *next = SvSTATE_hv (next_sv);
1677
1678 /* cannot transfer to destroyed coros, skip and look for next */ 1722 /* cannot transfer to destroyed coros, skip and look for next */
1679 if (expect_false (next->flags & CF_DESTROYED)) 1723 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1680 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ 1724 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */
1681 else 1725 else
1682 { 1726 {
1683 next->flags &= ~CF_READY; 1727 next->flags &= ~CF_READY;
1684 --coro_nready; 1728 --coro_nready;
1685 1729
1863 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 1907 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1864 api_ready (aTHX_ sv_manager); 1908 api_ready (aTHX_ sv_manager);
1865 1909
1866 frame->prepare = prepare_schedule; 1910 frame->prepare = prepare_schedule;
1867 frame->check = slf_check_repeat; 1911 frame->check = slf_check_repeat;
1912
1913 /* as a minor optimisation, we could unwind all stacks here */
1914 /* but that puts extra pressure on pp_slf, and is not worth much */
1915 /*coro_unwind_stacks (aTHX);*/
1868} 1916}
1869 1917
1870/*****************************************************************************/ 1918/*****************************************************************************/
1871/* async pool handler */ 1919/* async pool handler */
1872 1920
1955 SV *data = (SV *)GENSUB_ARG; 2003 SV *data = (SV *)GENSUB_ARG;
1956 2004
1957 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2005 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1958 { 2006 {
1959 /* first call, set args */ 2007 /* first call, set args */
2008 SV *coro = SvRV (data);
1960 AV *av = newAV (); 2009 AV *av = newAV ();
1961 SV *coro = SvRV (data);
1962 2010
1963 SvRV_set (data, (SV *)av); 2011 SvRV_set (data, (SV *)av);
1964 api_ready (aTHX_ coro);
1965 SvREFCNT_dec (coro);
1966 2012
1967 /* better take a full copy of the arguments */ 2013 /* better take a full copy of the arguments */
1968 while (items--) 2014 while (items--)
1969 av_store (av, items, newSVsv (ST (items))); 2015 av_store (av, items, newSVsv (ST (items)));
2016
2017 api_ready (aTHX_ coro);
2018 SvREFCNT_dec (coro);
1970 } 2019 }
1971 2020
1972 XSRETURN_EMPTY; 2021 XSRETURN_EMPTY;
1973} 2022}
1974 2023
1991 2040
1992 EXTEND (SP, AvFILLp (av) + 1); 2041 EXTEND (SP, AvFILLp (av) + 1);
1993 for (i = 0; i <= AvFILLp (av); ++i) 2042 for (i = 0; i <= AvFILLp (av); ++i)
1994 PUSHs (sv_2mortal (AvARRAY (av)[i])); 2043 PUSHs (sv_2mortal (AvARRAY (av)[i]));
1995 2044
1996 /* we have stolen the elements, so ste length to zero and free */ 2045 /* we have stolen the elements, so set length to zero and free */
1997 AvFILLp (av) = -1; 2046 AvFILLp (av) = -1;
1998 av_undef (av); 2047 av_undef (av);
1999 2048
2000 PUTBACK; 2049 PUTBACK;
2001 } 2050 }
2291 2340
2292 PL_op->op_ppaddr = pp_slf; 2341 PL_op->op_ppaddr = pp_slf;
2293 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ 2342 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2294 2343
2295 PL_op = (OP *)&slf_restore; 2344 PL_op = (OP *)&slf_restore;
2345}
2346
2347/*****************************************************************************/
2348/* dynamic wind */
2349
2350static void
2351on_enterleave_call (pTHX_ SV *cb)
2352{
2353 dSP;
2354
2355 PUSHSTACK;
2356
2357 PUSHMARK (SP);
2358 PUTBACK;
2359 call_sv (cb, G_VOID | G_DISCARD);
2360 SPAGAIN;
2361
2362 POPSTACK;
2363}
2364
2365static SV *
2366coro_avp_pop_and_free (pTHX_ AV **avp)
2367{
2368 AV *av = *avp;
2369 SV *res = av_pop (av);
2370
2371 if (AvFILLp (av) < 0)
2372 {
2373 *avp = 0;
2374 SvREFCNT_dec (av);
2375 }
2376
2377 return res;
2378}
2379
2380static void
2381coro_pop_on_enter (pTHX_ void *coro)
2382{
2383 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter);
2384 SvREFCNT_dec (cb);
2385}
2386
2387static void
2388coro_pop_on_leave (pTHX_ void *coro)
2389{
2390 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave);
2391 on_enterleave_call (aTHX_ sv_2mortal (cb));
2296} 2392}
2297 2393
2298/*****************************************************************************/ 2394/*****************************************************************************/
2299/* PerlIO::cede */ 2395/* PerlIO::cede */
2300 2396
2998 eval = 1 3094 eval = 1
2999 CODE: 3095 CODE:
3000{ 3096{
3001 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3097 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
3002 { 3098 {
3003 struct coro temp; 3099 struct coro *current = SvSTATE_current;
3004 3100
3005 if (!(coro->flags & CF_RUNNING)) 3101 if (current != coro)
3006 { 3102 {
3007 PUTBACK; 3103 PUTBACK;
3008 save_perl (aTHX_ &temp); 3104 save_perl (aTHX_ current);
3009 load_perl (aTHX_ coro); 3105 load_perl (aTHX_ coro);
3106 SPAGAIN;
3010 } 3107 }
3011 3108
3012 {
3013 dSP;
3014 ENTER;
3015 SAVETMPS;
3016 PUTBACK;
3017 PUSHSTACK; 3109 PUSHSTACK;
3110
3018 PUSHMARK (SP); 3111 PUSHMARK (SP);
3112 PUTBACK;
3019 3113
3020 if (ix) 3114 if (ix)
3021 eval_sv (coderef, 0); 3115 eval_sv (coderef, 0);
3022 else 3116 else
3023 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3117 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
3024 3118
3025 POPSTACK; 3119 POPSTACK;
3026 SPAGAIN; 3120 SPAGAIN;
3027 FREETMPS;
3028 LEAVE;
3029 PUTBACK;
3030 }
3031 3121
3032 if (!(coro->flags & CF_RUNNING)) 3122 if (current != coro)
3033 { 3123 {
3124 PUTBACK;
3034 save_perl (aTHX_ coro); 3125 save_perl (aTHX_ coro);
3035 load_perl (aTHX_ &temp); 3126 load_perl (aTHX_ current);
3036 SPAGAIN; 3127 SPAGAIN;
3037 } 3128 }
3038 } 3129 }
3039} 3130}
3040 3131
3044 ALIAS: 3135 ALIAS:
3045 is_ready = CF_READY 3136 is_ready = CF_READY
3046 is_running = CF_RUNNING 3137 is_running = CF_RUNNING
3047 is_new = CF_NEW 3138 is_new = CF_NEW
3048 is_destroyed = CF_DESTROYED 3139 is_destroyed = CF_DESTROYED
3140 is_suspended = CF_SUSPENDED
3049 CODE: 3141 CODE:
3050 RETVAL = boolSV (coro->flags & ix); 3142 RETVAL = boolSV (coro->flags & ix);
3051 OUTPUT: 3143 OUTPUT:
3052 RETVAL 3144 RETVAL
3053 3145
3118 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3210 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
3119 3211
3120 SV *tmp = *src; *src = *dst; *dst = tmp; 3212 SV *tmp = *src; *src = *dst; *dst = tmp;
3121 } 3213 }
3122 3214
3215void
3216cancel (Coro::State self)
3217 CODE:
3218 coro_state_destroy (aTHX_ self);
3219 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3220
3123 3221
3124MODULE = Coro::State PACKAGE = Coro 3222MODULE = Coro::State PACKAGE = Coro
3125 3223
3126BOOT: 3224BOOT:
3127{ 3225{
3128 int i;
3129
3130 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3226 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3131 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3227 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3132 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD); 3228 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3133 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3229 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3134 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3230 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3149 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 3245 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
3150 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 3246 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
3151 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 3247 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
3152 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 3248 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
3153 3249
3154 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
3155 coro_ready[i] = newAV ();
3156
3157 { 3250 {
3158 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3251 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
3159 3252
3160 coroapi.schedule = api_schedule; 3253 coroapi.schedule = api_schedule;
3161 coroapi.schedule_to = api_schedule_to; 3254 coroapi.schedule_to = api_schedule_to;
3201cede_notself (...) 3294cede_notself (...)
3202 CODE: 3295 CODE:
3203 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3296 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3204 3297
3205void 3298void
3206_cancel (Coro::State self)
3207 CODE:
3208 coro_state_destroy (aTHX_ self);
3209 coro_call_on_destroy (aTHX_ self);
3210
3211void
3212_set_current (SV *current) 3299_set_current (SV *current)
3213 PROTOTYPE: $ 3300 PROTOTYPE: $
3214 CODE: 3301 CODE:
3215 SvREFCNT_dec (SvRV (coro_current)); 3302 SvREFCNT_dec (SvRV (coro_current));
3216 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); 3303 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
3258 PROTOTYPE: 3345 PROTOTYPE:
3259 CODE: 3346 CODE:
3260 RETVAL = coro_nready; 3347 RETVAL = coro_nready;
3261 OUTPUT: 3348 OUTPUT:
3262 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;
3263 3362
3264void 3363void
3265_pool_handler (...) 3364_pool_handler (...)
3266 CODE: 3365 CODE:
3267 CORO_EXECUTE_SLF_XS (slf_init_pool_handler); 3366 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
3322rouse_wait (...) 3421rouse_wait (...)
3323 PROTOTYPE: ;$ 3422 PROTOTYPE: ;$
3324 PPCODE: 3423 PPCODE:
3325 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3424 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3326 3425
3426void
3427on_enter (SV *block)
3428 ALIAS:
3429 on_leave = 1
3430 PROTOTYPE: &
3431 CODE:
3432{
3433 struct coro *coro = SvSTATE_current;
3434 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3435
3436 block = (SV *)coro_sv_2cv (aTHX_ block);
3437
3438 if (!*avp)
3439 *avp = newAV ();
3440
3441 av_push (*avp, SvREFCNT_inc (block));
3442
3443 if (!ix)
3444 on_enterleave_call (aTHX_ block);
3445
3446 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3447 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3448 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3449}
3450
3327 3451
3328MODULE = Coro::State PACKAGE = PerlIO::cede 3452MODULE = Coro::State PACKAGE = PerlIO::cede
3329 3453
3330BOOT: 3454BOOT:
3331 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3455 PerlIO_define_layer (aTHX_ &PerlIO_cede);
3341 GvSTASH (CvGV (cv)) 3465 GvSTASH (CvGV (cv))
3342 ); 3466 );
3343 OUTPUT: 3467 OUTPUT:
3344 RETVAL 3468 RETVAL
3345 3469
3346# helper for Coro::Channel 3470# helper for Coro::Channel and others
3347SV * 3471SV *
3348_alloc (int count) 3472_alloc (int count)
3349 CODE: 3473 CODE:
3350 RETVAL = coro_waitarray_new (aTHX_ count); 3474 RETVAL = coro_waitarray_new (aTHX_ count);
3351 OUTPUT: 3475 OUTPUT:
3409 for (i = 1; i <= wcount; ++i) 3533 for (i = 1; i <= wcount; ++i)
3410 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3534 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
3411 } 3535 }
3412} 3536}
3413 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
3414MODULE = Coro::State PACKAGE = Coro::Signal 3554MODULE = Coro::State PACKAGE = Coro::Signal
3415 3555
3416SV * 3556SV *
3417new (SV *klass) 3557new (SV *klass)
3418 CODE: 3558 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines