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.334 by root, Fri Nov 28 23:30:55 2008 UTC vs.
Revision 1.351 by root, Sat Jun 20 08:58:25 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}
961 991
962 SvREFCNT_dec (GvSV (PL_defgv)); 992static void
963 SvREFCNT_dec (GvAV (PL_defgv)); 993coro_destruct_perl (pTHX_ struct coro *coro)
964 SvREFCNT_dec (GvSV (PL_errgv)); 994{
965 SvREFCNT_dec (PL_defoutgv); 995 SV *svf [9];
966 SvREFCNT_dec (PL_rs);
967 SvREFCNT_dec (GvSV (irsgv));
968 SvREFCNT_dec (GvHV (PL_hintgv));
969 996
970 SvREFCNT_dec (PL_diehook);
971 SvREFCNT_dec (PL_warnhook);
972 997 {
998 struct coro *current = SvSTATE_current;
999
1000 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1001
1002 save_perl (aTHX_ current);
1003 load_perl (aTHX_ coro);
1004 coro_unwind_stacks (aTHX);
1005 coro_destruct_stacks (aTHX);
1006
1007 // now save some sv's to be free'd later
1008 svf [0] = GvSV (PL_defgv);
1009 svf [1] = (SV *)GvAV (PL_defgv);
1010 svf [2] = GvSV (PL_errgv);
1011 svf [3] = (SV *)PL_defoutgv;
1012 svf [4] = PL_rs;
1013 svf [5] = GvSV (irsgv);
1014 svf [6] = (SV *)GvHV (PL_hintgv);
1015 svf [7] = PL_diehook;
1016 svf [8] = PL_warnhook;
1017 assert (9 == sizeof (svf) / sizeof (*svf));
1018
1019 load_perl (aTHX_ current);
1020 }
1021
1022 {
1023 int i;
1024
1025 for (i = 0; i < sizeof (svf) / sizeof (*svf); ++i)
1026 SvREFCNT_dec (svf [i]);
1027
973 SvREFCNT_dec (coro->saved_deffh); 1028 SvREFCNT_dec (coro->saved_deffh);
974 SvREFCNT_dec (coro->rouse_cb); 1029 SvREFCNT_dec (coro->rouse_cb);
975 SvREFCNT_dec (coro->invoke_cb); 1030 SvREFCNT_dec (coro->invoke_cb);
976 SvREFCNT_dec (coro->invoke_av); 1031 SvREFCNT_dec (coro->invoke_av);
977 1032 }
978 coro_destruct_stacks (aTHX);
979} 1033}
980 1034
981INLINE void 1035INLINE void
982free_coro_mortal (pTHX) 1036free_coro_mortal (pTHX)
983{ 1037{
1277cctx_destroy (coro_cctx *cctx) 1331cctx_destroy (coro_cctx *cctx)
1278{ 1332{
1279 if (!cctx) 1333 if (!cctx)
1280 return; 1334 return;
1281 1335
1282 assert (cctx != cctx_current);//D temporary 1336 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1283 1337
1284 --cctx_count; 1338 --cctx_count;
1285 coro_destroy (&cctx->cctx); 1339 coro_destroy (&cctx->cctx);
1286 1340
1287 /* coro_transfer creates new, empty cctx's */ 1341 /* coro_transfer creates new, empty cctx's */
1351 /* TODO: throwing up here is considered harmful */ 1405 /* TODO: throwing up here is considered harmful */
1352 1406
1353 if (expect_true (prev != next)) 1407 if (expect_true (prev != next))
1354 { 1408 {
1355 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1409 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,"); 1410 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,");
1357 1411
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)) 1412 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,"); 1413 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,");
1363 1414
1364#if !PERL_VERSION_ATLEAST (5,10,0) 1415#if !PERL_VERSION_ATLEAST (5,10,0)
1365 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1416 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,"); 1417 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
1367#endif 1418#endif
1405 coro_setup (aTHX_ next); 1456 coro_setup (aTHX_ next);
1406 } 1457 }
1407 else 1458 else
1408 load_perl (aTHX_ next); 1459 load_perl (aTHX_ next);
1409 1460
1410 assert (!prev->cctx);//D temporary
1411
1412 /* possibly untie and reuse the cctx */ 1461 /* possibly untie and reuse the cctx */
1413 if (expect_true ( 1462 if (expect_true (
1414 cctx_current->idle_sp == STACKLEVEL 1463 cctx_current->idle_sp == STACKLEVEL
1415 && !(cctx_current->flags & CC_TRACE) 1464 && !(cctx_current->flags & CC_TRACE)
1416 && !force_cctx 1465 && !force_cctx
1457coro_state_destroy (pTHX_ struct coro *coro) 1506coro_state_destroy (pTHX_ struct coro *coro)
1458{ 1507{
1459 if (coro->flags & CF_DESTROYED) 1508 if (coro->flags & CF_DESTROYED)
1460 return 0; 1509 return 0;
1461 1510
1462 if (coro->on_destroy) 1511 if (coro->on_destroy && !PL_dirty)
1463 coro->on_destroy (aTHX_ coro); 1512 coro->on_destroy (aTHX_ coro);
1464 1513
1465 coro->flags |= CF_DESTROYED; 1514 coro->flags |= CF_DESTROYED;
1466 1515
1467 if (coro->flags & CF_READY) 1516 if (coro->flags & CF_READY)
1473 else 1522 else
1474 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ 1523 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
1475 1524
1476 if (coro->mainstack 1525 if (coro->mainstack
1477 && coro->mainstack != main_mainstack 1526 && coro->mainstack != main_mainstack
1527 && coro->slot
1478 && !PL_dirty) 1528 && !PL_dirty)
1479 {
1480 struct coro temp;
1481
1482 assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING)));
1483
1484 save_perl (aTHX_ &temp);
1485 load_perl (aTHX_ coro);
1486
1487 coro_destruct_perl (aTHX_ coro); 1529 coro_destruct_perl (aTHX_ coro);
1488
1489 load_perl (aTHX_ &temp);
1490
1491 coro->slot = 0;
1492 }
1493 1530
1494 cctx_destroy (coro->cctx); 1531 cctx_destroy (coro->cctx);
1495 SvREFCNT_dec (coro->startcv); 1532 SvREFCNT_dec (coro->startcv);
1496 SvREFCNT_dec (coro->args); 1533 SvREFCNT_dec (coro->args);
1497 SvREFCNT_dec (CORO_THROW); 1534 SvREFCNT_dec (CORO_THROW);
1584/** Coro ********************************************************************/ 1621/** Coro ********************************************************************/
1585 1622
1586INLINE void 1623INLINE void
1587coro_enq (pTHX_ struct coro *coro) 1624coro_enq (pTHX_ struct coro *coro)
1588{ 1625{
1589 av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); 1626 struct coro **ready = coro_ready [coro->prio - PRIO_MIN];
1590}
1591 1627
1592INLINE SV * 1628 SvREFCNT_inc_NN (coro->hv);
1629
1630 coro->next_ready = 0;
1631 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro;
1632 ready [1] = coro;
1633}
1634
1635INLINE struct coro *
1593coro_deq (pTHX) 1636coro_deq (pTHX)
1594{ 1637{
1595 int prio; 1638 int prio;
1596 1639
1597 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) 1640 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; )
1598 if (AvFILLp (coro_ready [prio]) >= 0) 1641 {
1599 return av_shift (coro_ready [prio]); 1642 struct coro **ready = coro_ready [prio];
1643
1644 if (ready [0])
1645 {
1646 struct coro *coro = ready [0];
1647 ready [0] = coro->next_ready;
1648 return coro;
1649 }
1650 }
1600 1651
1601 return 0; 1652 return 0;
1602} 1653}
1603 1654
1604static int 1655static int
1668static void 1719static void
1669prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1720prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1670{ 1721{
1671 for (;;) 1722 for (;;)
1672 { 1723 {
1673 SV *next_sv = coro_deq (aTHX); 1724 struct coro *next = coro_deq (aTHX);
1674 1725
1675 if (expect_true (next_sv)) 1726 if (expect_true (next))
1676 { 1727 {
1677 struct coro *next = SvSTATE_hv (next_sv);
1678
1679 /* cannot transfer to destroyed coros, skip and look for next */ 1728 /* cannot transfer to destroyed coros, skip and look for next */
1680 if (expect_false (next->flags & CF_DESTROYED)) 1729 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1681 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ 1730 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */
1682 else 1731 else
1683 { 1732 {
1684 next->flags &= ~CF_READY; 1733 next->flags &= ~CF_READY;
1685 --coro_nready; 1734 --coro_nready;
1686 1735
1864 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 1913 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1865 api_ready (aTHX_ sv_manager); 1914 api_ready (aTHX_ sv_manager);
1866 1915
1867 frame->prepare = prepare_schedule; 1916 frame->prepare = prepare_schedule;
1868 frame->check = slf_check_repeat; 1917 frame->check = slf_check_repeat;
1918
1919 /* as a minor optimisation, we could unwind all stacks here */
1920 /* but that puts extra pressure on pp_slf, and is not worth much */
1921 /*coro_unwind_stacks (aTHX);*/
1869} 1922}
1870 1923
1871/*****************************************************************************/ 1924/*****************************************************************************/
1872/* async pool handler */ 1925/* async pool handler */
1873 1926
1956 SV *data = (SV *)GENSUB_ARG; 2009 SV *data = (SV *)GENSUB_ARG;
1957 2010
1958 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2011 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1959 { 2012 {
1960 /* first call, set args */ 2013 /* first call, set args */
2014 SV *coro = SvRV (data);
1961 AV *av = newAV (); 2015 AV *av = newAV ();
1962 SV *coro = SvRV (data);
1963 2016
1964 SvRV_set (data, (SV *)av); 2017 SvRV_set (data, (SV *)av);
1965 api_ready (aTHX_ coro);
1966 SvREFCNT_dec (coro);
1967 2018
1968 /* better take a full copy of the arguments */ 2019 /* better take a full copy of the arguments */
1969 while (items--) 2020 while (items--)
1970 av_store (av, items, newSVsv (ST (items))); 2021 av_store (av, items, newSVsv (ST (items)));
2022
2023 api_ready (aTHX_ coro);
2024 SvREFCNT_dec (coro);
1971 } 2025 }
1972 2026
1973 XSRETURN_EMPTY; 2027 XSRETURN_EMPTY;
1974} 2028}
1975 2029
1992 2046
1993 EXTEND (SP, AvFILLp (av) + 1); 2047 EXTEND (SP, AvFILLp (av) + 1);
1994 for (i = 0; i <= AvFILLp (av); ++i) 2048 for (i = 0; i <= AvFILLp (av); ++i)
1995 PUSHs (sv_2mortal (AvARRAY (av)[i])); 2049 PUSHs (sv_2mortal (AvARRAY (av)[i]));
1996 2050
1997 /* we have stolen the elements, so ste length to zero and free */ 2051 /* we have stolen the elements, so set length to zero and free */
1998 AvFILLp (av) = -1; 2052 AvFILLp (av) = -1;
1999 av_undef (av); 2053 av_undef (av);
2000 2054
2001 PUTBACK; 2055 PUTBACK;
2002 } 2056 }
2292 2346
2293 PL_op->op_ppaddr = pp_slf; 2347 PL_op->op_ppaddr = pp_slf;
2294 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ 2348 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2295 2349
2296 PL_op = (OP *)&slf_restore; 2350 PL_op = (OP *)&slf_restore;
2351}
2352
2353/*****************************************************************************/
2354/* dynamic wind */
2355
2356static void
2357on_enterleave_call (pTHX_ SV *cb)
2358{
2359 dSP;
2360
2361 PUSHSTACK;
2362
2363 PUSHMARK (SP);
2364 PUTBACK;
2365 call_sv (cb, G_VOID | G_DISCARD);
2366 SPAGAIN;
2367
2368 POPSTACK;
2369}
2370
2371static SV *
2372coro_avp_pop_and_free (pTHX_ AV **avp)
2373{
2374 AV *av = *avp;
2375 SV *res = av_pop (av);
2376
2377 if (AvFILLp (av) < 0)
2378 {
2379 *avp = 0;
2380 SvREFCNT_dec (av);
2381 }
2382
2383 return res;
2384}
2385
2386static void
2387coro_pop_on_enter (pTHX_ void *coro)
2388{
2389 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter);
2390 SvREFCNT_dec (cb);
2391}
2392
2393static void
2394coro_pop_on_leave (pTHX_ void *coro)
2395{
2396 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave);
2397 on_enterleave_call (aTHX_ sv_2mortal (cb));
2297} 2398}
2298 2399
2299/*****************************************************************************/ 2400/*****************************************************************************/
2300/* PerlIO::cede */ 2401/* PerlIO::cede */
2301 2402
2999 eval = 1 3100 eval = 1
3000 CODE: 3101 CODE:
3001{ 3102{
3002 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3103 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
3003 { 3104 {
3004 struct coro temp; 3105 struct coro *current = SvSTATE_current;
3005 3106
3006 if (!(coro->flags & CF_RUNNING)) 3107 if (current != coro)
3007 { 3108 {
3008 PUTBACK; 3109 PUTBACK;
3009 save_perl (aTHX_ &temp); 3110 save_perl (aTHX_ current);
3010 load_perl (aTHX_ coro); 3111 load_perl (aTHX_ coro);
3112 SPAGAIN;
3011 } 3113 }
3012 3114
3013 {
3014 dSP;
3015 ENTER;
3016 SAVETMPS;
3017 PUTBACK;
3018 PUSHSTACK; 3115 PUSHSTACK;
3116
3019 PUSHMARK (SP); 3117 PUSHMARK (SP);
3118 PUTBACK;
3020 3119
3021 if (ix) 3120 if (ix)
3022 eval_sv (coderef, 0); 3121 eval_sv (coderef, 0);
3023 else 3122 else
3024 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3123 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
3025 3124
3026 POPSTACK; 3125 POPSTACK;
3027 SPAGAIN; 3126 SPAGAIN;
3028 FREETMPS;
3029 LEAVE;
3030 PUTBACK;
3031 }
3032 3127
3033 if (!(coro->flags & CF_RUNNING)) 3128 if (current != coro)
3034 { 3129 {
3130 PUTBACK;
3035 save_perl (aTHX_ coro); 3131 save_perl (aTHX_ coro);
3036 load_perl (aTHX_ &temp); 3132 load_perl (aTHX_ current);
3037 SPAGAIN; 3133 SPAGAIN;
3038 } 3134 }
3039 } 3135 }
3040} 3136}
3041 3137
3045 ALIAS: 3141 ALIAS:
3046 is_ready = CF_READY 3142 is_ready = CF_READY
3047 is_running = CF_RUNNING 3143 is_running = CF_RUNNING
3048 is_new = CF_NEW 3144 is_new = CF_NEW
3049 is_destroyed = CF_DESTROYED 3145 is_destroyed = CF_DESTROYED
3146 is_suspended = CF_SUSPENDED
3050 CODE: 3147 CODE:
3051 RETVAL = boolSV (coro->flags & ix); 3148 RETVAL = boolSV (coro->flags & ix);
3052 OUTPUT: 3149 OUTPUT:
3053 RETVAL 3150 RETVAL
3054 3151
3119 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3216 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
3120 3217
3121 SV *tmp = *src; *src = *dst; *dst = tmp; 3218 SV *tmp = *src; *src = *dst; *dst = tmp;
3122 } 3219 }
3123 3220
3221void
3222cancel (Coro::State self)
3223 CODE:
3224 coro_state_destroy (aTHX_ self);
3225 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3226
3124 3227
3125MODULE = Coro::State PACKAGE = Coro 3228MODULE = Coro::State PACKAGE = Coro
3126 3229
3127BOOT: 3230BOOT:
3128{ 3231{
3129 int i;
3130
3131 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3232 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3132 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3233 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3133 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3234 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3134 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3235 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3135 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3236 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3136 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3237 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3137 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3238 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3138 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3239 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3149 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3250 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
3150 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 3251 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
3151 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 3252 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
3152 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 3253 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
3153 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 3254 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
3154
3155 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
3156 coro_ready[i] = newAV ();
3157 3255
3158 { 3256 {
3159 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3257 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
3160 3258
3161 coroapi.schedule = api_schedule; 3259 coroapi.schedule = api_schedule;
3202cede_notself (...) 3300cede_notself (...)
3203 CODE: 3301 CODE:
3204 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3302 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3205 3303
3206void 3304void
3207_cancel (Coro::State self)
3208 CODE:
3209 coro_state_destroy (aTHX_ self);
3210 coro_call_on_destroy (aTHX_ self);
3211
3212void
3213_set_current (SV *current) 3305_set_current (SV *current)
3214 PROTOTYPE: $ 3306 PROTOTYPE: $
3215 CODE: 3307 CODE:
3216 SvREFCNT_dec (SvRV (coro_current)); 3308 SvREFCNT_dec (SvRV (coro_current));
3217 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); 3309 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
3259 PROTOTYPE: 3351 PROTOTYPE:
3260 CODE: 3352 CODE:
3261 RETVAL = coro_nready; 3353 RETVAL = coro_nready;
3262 OUTPUT: 3354 OUTPUT:
3263 RETVAL 3355 RETVAL
3356
3357void
3358suspend (Coro::State self)
3359 PROTOTYPE: $
3360 CODE:
3361 self->flags |= CF_SUSPENDED;
3362
3363void
3364resume (Coro::State self)
3365 PROTOTYPE: $
3366 CODE:
3367 self->flags &= ~CF_SUSPENDED;
3264 3368
3265void 3369void
3266_pool_handler (...) 3370_pool_handler (...)
3267 CODE: 3371 CODE:
3268 CORO_EXECUTE_SLF_XS (slf_init_pool_handler); 3372 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
3323rouse_wait (...) 3427rouse_wait (...)
3324 PROTOTYPE: ;$ 3428 PROTOTYPE: ;$
3325 PPCODE: 3429 PPCODE:
3326 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3430 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3327 3431
3432void
3433on_enter (SV *block)
3434 ALIAS:
3435 on_leave = 1
3436 PROTOTYPE: &
3437 CODE:
3438{
3439 struct coro *coro = SvSTATE_current;
3440 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3441
3442 block = (SV *)coro_sv_2cv (aTHX_ block);
3443
3444 if (!*avp)
3445 *avp = newAV ();
3446
3447 av_push (*avp, SvREFCNT_inc (block));
3448
3449 if (!ix)
3450 on_enterleave_call (aTHX_ block);
3451
3452 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3453 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3454 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3455}
3456
3328 3457
3329MODULE = Coro::State PACKAGE = PerlIO::cede 3458MODULE = Coro::State PACKAGE = PerlIO::cede
3330 3459
3331BOOT: 3460BOOT:
3332 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3461 PerlIO_define_layer (aTHX_ &PerlIO_cede);
3342 GvSTASH (CvGV (cv)) 3471 GvSTASH (CvGV (cv))
3343 ); 3472 );
3344 OUTPUT: 3473 OUTPUT:
3345 RETVAL 3474 RETVAL
3346 3475
3347# helper for Coro::Channel 3476# helper for Coro::Channel and others
3348SV * 3477SV *
3349_alloc (int count) 3478_alloc (int count)
3350 CODE: 3479 CODE:
3351 RETVAL = coro_waitarray_new (aTHX_ count); 3480 RETVAL = coro_waitarray_new (aTHX_ count);
3352 OUTPUT: 3481 OUTPUT:
3410 for (i = 1; i <= wcount; ++i) 3539 for (i = 1; i <= wcount; ++i)
3411 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3540 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
3412 } 3541 }
3413} 3542}
3414 3543
3544MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
3545
3546void
3547_may_delete (SV *sem, int count, int extra_refs)
3548 PPCODE:
3549{
3550 AV *av = (AV *)SvRV (sem);
3551
3552 if (SvREFCNT ((SV *)av) == 1 + extra_refs
3553 && AvFILLp (av) == 0 /* no waiters, just count */
3554 && SvIV (AvARRAY (av)[0]) == count)
3555 XSRETURN_YES;
3556
3557 XSRETURN_NO;
3558}
3559
3415MODULE = Coro::State PACKAGE = Coro::Signal 3560MODULE = Coro::State PACKAGE = Coro::Signal
3416 3561
3417SV * 3562SV *
3418new (SV *klass) 3563new (SV *klass)
3419 CODE: 3564 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines