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.332 by root, Thu Nov 27 12:20:31 2008 UTC vs.
Revision 1.355 by root, Wed Jun 24 02:52:38 2009 UTC

137 137
138#define IN_DESTRUCT PL_dirty 138#define IN_DESTRUCT PL_dirty
139 139
140#if __GNUC__ >= 3 140#if __GNUC__ >= 3
141# define attribute(x) __attribute__(x) 141# define attribute(x) __attribute__(x)
142# define expect(expr,value) __builtin_expect ((expr),(value)) 142# define expect(expr,value) __builtin_expect ((expr), (value))
143# define INLINE static inline 143# define INLINE static inline
144#else 144#else
145# define attribute(x) 145# define attribute(x)
146# define expect(expr,value) (expr) 146# define expect(expr,value) (expr)
147# define INLINE static 147# define INLINE static
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;
583 { 615 {
584 while (expect_true (cxix >= 0)) 616 while (expect_true (cxix >= 0))
585 { 617 {
586 PERL_CONTEXT *cx = &ccstk[cxix--]; 618 PERL_CONTEXT *cx = &ccstk[cxix--];
587 619
588 if (expect_true (CxTYPE (cx) == CXt_SUB || CxTYPE (cx) == CXt_FORMAT)) 620 if (expect_true (CxTYPE (cx) == CXt_SUB) || expect_false (CxTYPE (cx) == CXt_FORMAT))
589 { 621 {
590 CV *cv = cx->blk_sub.cv; 622 CV *cv = cx->blk_sub.cv;
591 623
592 if (expect_true (CvDEPTH (cv))) 624 if (expect_true (CvDEPTH (cv)))
593 { 625 {
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 (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
1005 coro_unwind_stacks (aTHX);
1006 coro_destruct_stacks (aTHX);
1007
1008 // now save some sv's to be free'd later
1009 svf [0] = GvSV (PL_defgv);
1010 svf [1] = (SV *)GvAV (PL_defgv);
1011 svf [2] = GvSV (PL_errgv);
1012 svf [3] = (SV *)PL_defoutgv;
1013 svf [4] = PL_rs;
1014 svf [5] = GvSV (irsgv);
1015 svf [6] = (SV *)GvHV (PL_hintgv);
1016 svf [7] = PL_diehook;
1017 svf [8] = PL_warnhook;
1018 assert (9 == sizeof (svf) / sizeof (*svf));
1019
1020 load_perl (aTHX_ current);
1021 }
1022
1023 {
1024 int i;
1025
1026 for (i = 0; i < sizeof (svf) / sizeof (*svf); ++i)
1027 SvREFCNT_dec (svf [i]);
1028
973 SvREFCNT_dec (coro->saved_deffh); 1029 SvREFCNT_dec (coro->saved_deffh);
974 SvREFCNT_dec (coro->rouse_cb); 1030 SvREFCNT_dec (coro->rouse_cb);
975 SvREFCNT_dec (coro->invoke_cb); 1031 SvREFCNT_dec (coro->invoke_cb);
976 SvREFCNT_dec (coro->invoke_av); 1032 SvREFCNT_dec (coro->invoke_av);
977 1033 }
978 coro_destruct_stacks (aTHX);
979} 1034}
980 1035
981INLINE void 1036INLINE void
982free_coro_mortal (pTHX) 1037free_coro_mortal (pTHX)
983{ 1038{
1277cctx_destroy (coro_cctx *cctx) 1332cctx_destroy (coro_cctx *cctx)
1278{ 1333{
1279 if (!cctx) 1334 if (!cctx)
1280 return; 1335 return;
1281 1336
1282 assert (cctx != cctx_current);//D temporary 1337 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1283 1338
1284 --cctx_count; 1339 --cctx_count;
1285 coro_destroy (&cctx->cctx); 1340 coro_destroy (&cctx->cctx);
1286 1341
1287 /* coro_transfer creates new, empty cctx's */ 1342 /* coro_transfer creates new, empty cctx's */
1351 /* TODO: throwing up here is considered harmful */ 1406 /* TODO: throwing up here is considered harmful */
1352 1407
1353 if (expect_true (prev != next)) 1408 if (expect_true (prev != next))
1354 { 1409 {
1355 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1410 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,"); 1411 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,");
1357 1412
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)) 1413 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,"); 1414 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,");
1363 1415
1364#if !PERL_VERSION_ATLEAST (5,10,0) 1416#if !PERL_VERSION_ATLEAST (5,10,0)
1365 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1417 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,"); 1418 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
1367#endif 1419#endif
1405 coro_setup (aTHX_ next); 1457 coro_setup (aTHX_ next);
1406 } 1458 }
1407 else 1459 else
1408 load_perl (aTHX_ next); 1460 load_perl (aTHX_ next);
1409 1461
1410 assert (!prev->cctx);//D temporary
1411
1412 /* possibly untie and reuse the cctx */ 1462 /* possibly untie and reuse the cctx */
1413 if (expect_true ( 1463 if (expect_true (
1414 cctx_current->idle_sp == STACKLEVEL 1464 cctx_current->idle_sp == STACKLEVEL
1415 && !(cctx_current->flags & CC_TRACE) 1465 && !(cctx_current->flags & CC_TRACE)
1416 && !force_cctx 1466 && !force_cctx
1457coro_state_destroy (pTHX_ struct coro *coro) 1507coro_state_destroy (pTHX_ struct coro *coro)
1458{ 1508{
1459 if (coro->flags & CF_DESTROYED) 1509 if (coro->flags & CF_DESTROYED)
1460 return 0; 1510 return 0;
1461 1511
1462 if (coro->on_destroy) 1512 if (coro->on_destroy && !PL_dirty)
1463 coro->on_destroy (aTHX_ coro); 1513 coro->on_destroy (aTHX_ coro);
1464 1514
1465 coro->flags |= CF_DESTROYED; 1515 coro->flags |= CF_DESTROYED;
1466 1516
1467 if (coro->flags & CF_READY) 1517 if (coro->flags & CF_READY)
1471 --coro_nready; 1521 --coro_nready;
1472 } 1522 }
1473 else 1523 else
1474 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ 1524 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
1475 1525
1476 if (coro->mainstack && coro->mainstack != main_mainstack) 1526 if (coro->mainstack
1477 { 1527 && coro->mainstack != main_mainstack
1478 struct coro temp; 1528 && coro->slot
1479 1529 && !PL_dirty)
1480 assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING)));
1481
1482 save_perl (aTHX_ &temp);
1483 load_perl (aTHX_ coro);
1484
1485 coro_destruct (aTHX_ coro); 1530 coro_destruct_perl (aTHX_ coro);
1486
1487 load_perl (aTHX_ &temp);
1488
1489 coro->slot = 0;
1490 }
1491 1531
1492 cctx_destroy (coro->cctx); 1532 cctx_destroy (coro->cctx);
1493 SvREFCNT_dec (coro->startcv); 1533 SvREFCNT_dec (coro->startcv);
1494 SvREFCNT_dec (coro->args); 1534 SvREFCNT_dec (coro->args);
1495 SvREFCNT_dec (CORO_THROW); 1535 SvREFCNT_dec (CORO_THROW);
1582/** Coro ********************************************************************/ 1622/** Coro ********************************************************************/
1583 1623
1584INLINE void 1624INLINE void
1585coro_enq (pTHX_ struct coro *coro) 1625coro_enq (pTHX_ struct coro *coro)
1586{ 1626{
1587 av_push (coro_ready [coro->prio - PRIO_MIN], SvREFCNT_inc_NN (coro->hv)); 1627 struct coro **ready = coro_ready [coro->prio - PRIO_MIN];
1588}
1589 1628
1590INLINE SV * 1629 SvREFCNT_inc_NN (coro->hv);
1630
1631 coro->next_ready = 0;
1632 *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro;
1633 ready [1] = coro;
1634}
1635
1636INLINE struct coro *
1591coro_deq (pTHX) 1637coro_deq (pTHX)
1592{ 1638{
1593 int prio; 1639 int prio;
1594 1640
1595 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; ) 1641 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= 0; )
1596 if (AvFILLp (coro_ready [prio]) >= 0) 1642 {
1597 return av_shift (coro_ready [prio]); 1643 struct coro **ready = coro_ready [prio];
1644
1645 if (ready [0])
1646 {
1647 struct coro *coro = ready [0];
1648 ready [0] = coro->next_ready;
1649 return coro;
1650 }
1651 }
1598 1652
1599 return 0; 1653 return 0;
1600} 1654}
1601 1655
1602static int 1656static int
1666static void 1720static void
1667prepare_schedule (pTHX_ struct coro_transfer_args *ta) 1721prepare_schedule (pTHX_ struct coro_transfer_args *ta)
1668{ 1722{
1669 for (;;) 1723 for (;;)
1670 { 1724 {
1671 SV *next_sv = coro_deq (aTHX); 1725 struct coro *next = coro_deq (aTHX);
1672 1726
1673 if (expect_true (next_sv)) 1727 if (expect_true (next))
1674 { 1728 {
1675 struct coro *next = SvSTATE_hv (next_sv);
1676
1677 /* cannot transfer to destroyed coros, skip and look for next */ 1729 /* cannot transfer to destroyed coros, skip and look for next */
1678 if (expect_false (next->flags & CF_DESTROYED)) 1730 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1679 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ 1731 SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */
1680 else 1732 else
1681 { 1733 {
1682 next->flags &= ~CF_READY; 1734 next->flags &= ~CF_READY;
1683 --coro_nready; 1735 --coro_nready;
1684 1736
1862 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 1914 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1863 api_ready (aTHX_ sv_manager); 1915 api_ready (aTHX_ sv_manager);
1864 1916
1865 frame->prepare = prepare_schedule; 1917 frame->prepare = prepare_schedule;
1866 frame->check = slf_check_repeat; 1918 frame->check = slf_check_repeat;
1919
1920 /* as a minor optimisation, we could unwind all stacks here */
1921 /* but that puts extra pressure on pp_slf, and is not worth much */
1922 /*coro_unwind_stacks (aTHX);*/
1867} 1923}
1868 1924
1869/*****************************************************************************/ 1925/*****************************************************************************/
1870/* async pool handler */ 1926/* async pool handler */
1871 1927
1954 SV *data = (SV *)GENSUB_ARG; 2010 SV *data = (SV *)GENSUB_ARG;
1955 2011
1956 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2012 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1957 { 2013 {
1958 /* first call, set args */ 2014 /* first call, set args */
2015 SV *coro = SvRV (data);
1959 AV *av = newAV (); 2016 AV *av = newAV ();
1960 SV *coro = SvRV (data);
1961 2017
1962 SvRV_set (data, (SV *)av); 2018 SvRV_set (data, (SV *)av);
1963 api_ready (aTHX_ coro);
1964 SvREFCNT_dec (coro);
1965 2019
1966 /* better take a full copy of the arguments */ 2020 /* better take a full copy of the arguments */
1967 while (items--) 2021 while (items--)
1968 av_store (av, items, newSVsv (ST (items))); 2022 av_store (av, items, newSVsv (ST (items)));
2023
2024 api_ready (aTHX_ coro);
2025 SvREFCNT_dec (coro);
1969 } 2026 }
1970 2027
1971 XSRETURN_EMPTY; 2028 XSRETURN_EMPTY;
1972} 2029}
1973 2030
1990 2047
1991 EXTEND (SP, AvFILLp (av) + 1); 2048 EXTEND (SP, AvFILLp (av) + 1);
1992 for (i = 0; i <= AvFILLp (av); ++i) 2049 for (i = 0; i <= AvFILLp (av); ++i)
1993 PUSHs (sv_2mortal (AvARRAY (av)[i])); 2050 PUSHs (sv_2mortal (AvARRAY (av)[i]));
1994 2051
1995 /* we have stolen the elements, so ste length to zero and free */ 2052 /* we have stolen the elements, so set length to zero and free */
1996 AvFILLp (av) = -1; 2053 AvFILLp (av) = -1;
1997 av_undef (av); 2054 av_undef (av);
1998 2055
1999 PUTBACK; 2056 PUTBACK;
2000 } 2057 }
2290 2347
2291 PL_op->op_ppaddr = pp_slf; 2348 PL_op->op_ppaddr = pp_slf;
2292 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ 2349 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2293 2350
2294 PL_op = (OP *)&slf_restore; 2351 PL_op = (OP *)&slf_restore;
2352}
2353
2354/*****************************************************************************/
2355/* dynamic wind */
2356
2357static void
2358on_enterleave_call (pTHX_ SV *cb)
2359{
2360 dSP;
2361
2362 PUSHSTACK;
2363
2364 PUSHMARK (SP);
2365 PUTBACK;
2366 call_sv (cb, G_VOID | G_DISCARD);
2367 SPAGAIN;
2368
2369 POPSTACK;
2370}
2371
2372static SV *
2373coro_avp_pop_and_free (pTHX_ AV **avp)
2374{
2375 AV *av = *avp;
2376 SV *res = av_pop (av);
2377
2378 if (AvFILLp (av) < 0)
2379 {
2380 *avp = 0;
2381 SvREFCNT_dec (av);
2382 }
2383
2384 return res;
2385}
2386
2387static void
2388coro_pop_on_enter (pTHX_ void *coro)
2389{
2390 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter);
2391 SvREFCNT_dec (cb);
2392}
2393
2394static void
2395coro_pop_on_leave (pTHX_ void *coro)
2396{
2397 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave);
2398 on_enterleave_call (aTHX_ sv_2mortal (cb));
2295} 2399}
2296 2400
2297/*****************************************************************************/ 2401/*****************************************************************************/
2298/* PerlIO::cede */ 2402/* PerlIO::cede */
2299 2403
2373/* Coro::Semaphore & Coro::Signal */ 2477/* Coro::Semaphore & Coro::Signal */
2374 2478
2375static SV * 2479static SV *
2376coro_waitarray_new (pTHX_ int count) 2480coro_waitarray_new (pTHX_ int count)
2377{ 2481{
2378 /* a semaphore contains a counter IV in $sem->[0] and any waiters after that */ 2482 /* a waitarray=semaphore contains a counter IV in $sem->[0] and any waiters after that */
2379 AV *av = newAV (); 2483 AV *av = newAV ();
2380 SV **ary; 2484 SV **ary;
2381 2485
2382 /* unfortunately, building manually saves memory */ 2486 /* unfortunately, building manually saves memory */
2383 Newx (ary, 2, SV *); 2487 Newx (ary, 2, SV *);
2527 { 2631 {
2528 /* callback form */ 2632 /* callback form */
2529 AV *av = (AV *)SvRV (arg [0]); 2633 AV *av = (AV *)SvRV (arg [0]);
2530 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]); 2634 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2531 2635
2532 av_push (av, (SV *)SvREFCNT_inc_NN (cb_cv)); 2636 av_push (av, SvREFCNT_inc_NN (cb_cv));
2533 2637
2534 if (SvIVX (AvARRAY (av)[0]) > 0) 2638 if (SvIVX (AvARRAY (av)[0]) > 0)
2535 coro_semaphore_adjust (aTHX_ av, 0); 2639 coro_semaphore_adjust (aTHX_ av, 0);
2536 2640
2537 frame->prepare = prepare_nop; 2641 frame->prepare = prepare_nop;
2561 AvARRAY (av)[0] = AvARRAY (av)[1]; 2665 AvARRAY (av)[0] = AvARRAY (av)[1];
2562 AvARRAY (av)[1] = cb; 2666 AvARRAY (av)[1] = cb;
2563 2667
2564 cb = av_shift (av); 2668 cb = av_shift (av);
2565 2669
2670 if (SvTYPE (cb) == SVt_PVCV)
2671 {
2672 dSP;
2673 PUSHMARK (SP);
2674 XPUSHs (sv_2mortal (newRV_inc ((SV *)av)));
2675 PUTBACK;
2676 call_sv (cb, G_VOID | G_DISCARD | G_EVAL | G_KEEPERR);
2677 }
2678 else
2679 {
2566 api_ready (aTHX_ cb); 2680 api_ready (aTHX_ cb);
2567 sv_setiv (cb, 0); /* signal waiter */ 2681 sv_setiv (cb, 0); /* signal waiter */
2682 }
2683
2568 SvREFCNT_dec (cb); 2684 SvREFCNT_dec (cb);
2569 2685
2570 --count; 2686 --count;
2571 } 2687 }
2572} 2688}
2581static void 2697static void
2582slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) 2698slf_init_signal_wait (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items)
2583{ 2699{
2584 AV *av = (AV *)SvRV (arg [0]); 2700 AV *av = (AV *)SvRV (arg [0]);
2585 2701
2702 if (items >= 2)
2703 {
2704 CV *cb_cv = coro_sv_2cv (aTHX_ arg [1]);
2705 av_push (av, SvREFCNT_inc_NN (cb_cv));
2706
2586 if (SvIVX (AvARRAY (av)[0])) 2707 if (SvIVX (AvARRAY (av)[0]))
2708 coro_signal_wake (aTHX_ av, 1); /* ust be the only waiter */
2709
2710 frame->prepare = prepare_nop;
2711 frame->check = slf_check_nop;
2712 }
2713 else if (SvIVX (AvARRAY (av)[0]))
2587 { 2714 {
2588 SvIVX (AvARRAY (av)[0]) = 0; 2715 SvIVX (AvARRAY (av)[0]) = 0;
2589 frame->prepare = prepare_nop; 2716 frame->prepare = prepare_nop;
2590 frame->check = slf_check_nop; 2717 frame->check = slf_check_nop;
2591 } 2718 }
2592 else 2719 else
2593 { 2720 {
2594 SV *waiter = newRV_inc (SvRV (coro_current)); /* owned by signal av */ 2721 SV *waiter = newSVsv (coro_current); /* owned by signal av */
2595 2722
2596 av_push (av, waiter); 2723 av_push (av, waiter);
2597 2724
2598 frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */ 2725 frame->data = (void *)sv_2mortal (SvREFCNT_inc_NN (waiter)); /* owned by process */
2599 frame->prepare = prepare_schedule; 2726 frame->prepare = prepare_schedule;
2925SV * 3052SV *
2926clone (Coro::State coro) 3053clone (Coro::State coro)
2927 CODE: 3054 CODE:
2928{ 3055{
2929#if CORO_CLONE 3056#if CORO_CLONE
2930 struct coro *ncoro = coro_clone (coro); 3057 struct coro *ncoro = coro_clone (aTHX_ coro);
2931 MAGIC *mg; 3058 MAGIC *mg;
2932 /* TODO: too much duplication */ 3059 /* TODO: too much duplication */
2933 ncoro->hv = newHV (); 3060 ncoro->hv = newHV ();
2934 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); 3061 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2935 mg->mg_flags |= MGf_DUP; 3062 mg->mg_flags |= MGf_DUP;
2997 eval = 1 3124 eval = 1
2998 CODE: 3125 CODE:
2999{ 3126{
3000 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3127 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
3001 { 3128 {
3002 struct coro temp; 3129 struct coro *current = SvSTATE_current;
3003 3130
3004 if (!(coro->flags & CF_RUNNING)) 3131 if (current != coro)
3005 { 3132 {
3006 PUTBACK; 3133 PUTBACK;
3007 save_perl (aTHX_ &temp); 3134 save_perl (aTHX_ current);
3008 load_perl (aTHX_ coro); 3135 load_perl (aTHX_ coro);
3136 SPAGAIN;
3009 } 3137 }
3010 3138
3011 {
3012 dSP;
3013 ENTER;
3014 SAVETMPS;
3015 PUTBACK;
3016 PUSHSTACK; 3139 PUSHSTACK;
3140
3017 PUSHMARK (SP); 3141 PUSHMARK (SP);
3142 PUTBACK;
3018 3143
3019 if (ix) 3144 if (ix)
3020 eval_sv (coderef, 0); 3145 eval_sv (coderef, 0);
3021 else 3146 else
3022 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3147 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
3023 3148
3024 POPSTACK; 3149 POPSTACK;
3025 SPAGAIN; 3150 SPAGAIN;
3026 FREETMPS;
3027 LEAVE;
3028 PUTBACK;
3029 }
3030 3151
3031 if (!(coro->flags & CF_RUNNING)) 3152 if (current != coro)
3032 { 3153 {
3154 PUTBACK;
3033 save_perl (aTHX_ coro); 3155 save_perl (aTHX_ coro);
3034 load_perl (aTHX_ &temp); 3156 load_perl (aTHX_ current);
3035 SPAGAIN; 3157 SPAGAIN;
3036 } 3158 }
3037 } 3159 }
3038} 3160}
3039 3161
3043 ALIAS: 3165 ALIAS:
3044 is_ready = CF_READY 3166 is_ready = CF_READY
3045 is_running = CF_RUNNING 3167 is_running = CF_RUNNING
3046 is_new = CF_NEW 3168 is_new = CF_NEW
3047 is_destroyed = CF_DESTROYED 3169 is_destroyed = CF_DESTROYED
3170 is_suspended = CF_SUSPENDED
3048 CODE: 3171 CODE:
3049 RETVAL = boolSV (coro->flags & ix); 3172 RETVAL = boolSV (coro->flags & ix);
3050 OUTPUT: 3173 OUTPUT:
3051 RETVAL 3174 RETVAL
3052 3175
3117 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3240 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
3118 3241
3119 SV *tmp = *src; *src = *dst; *dst = tmp; 3242 SV *tmp = *src; *src = *dst; *dst = tmp;
3120 } 3243 }
3121 3244
3245void
3246cancel (Coro::State self)
3247 CODE:
3248 coro_state_destroy (aTHX_ self);
3249 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3250
3122 3251
3123MODULE = Coro::State PACKAGE = Coro 3252MODULE = Coro::State PACKAGE = Coro
3124 3253
3125BOOT: 3254BOOT:
3126{ 3255{
3127 int i;
3128
3129 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3256 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3130 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3257 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3131 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3258 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3132 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3259 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3133 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3260 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3134 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3261 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3135 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3262 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3136 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3263 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3147 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 3274 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
3148 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 3275 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
3149 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 3276 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
3150 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 3277 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
3151 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 3278 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
3152
3153 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
3154 coro_ready[i] = newAV ();
3155 3279
3156 { 3280 {
3157 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE); 3281 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
3158 3282
3159 coroapi.schedule = api_schedule; 3283 coroapi.schedule = api_schedule;
3200cede_notself (...) 3324cede_notself (...)
3201 CODE: 3325 CODE:
3202 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3326 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3203 3327
3204void 3328void
3205_cancel (Coro::State self)
3206 CODE:
3207 coro_state_destroy (aTHX_ self);
3208 coro_call_on_destroy (aTHX_ self);
3209
3210void
3211_set_current (SV *current) 3329_set_current (SV *current)
3212 PROTOTYPE: $ 3330 PROTOTYPE: $
3213 CODE: 3331 CODE:
3214 SvREFCNT_dec (SvRV (coro_current)); 3332 SvREFCNT_dec (SvRV (coro_current));
3215 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); 3333 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
3257 PROTOTYPE: 3375 PROTOTYPE:
3258 CODE: 3376 CODE:
3259 RETVAL = coro_nready; 3377 RETVAL = coro_nready;
3260 OUTPUT: 3378 OUTPUT:
3261 RETVAL 3379 RETVAL
3380
3381void
3382suspend (Coro::State self)
3383 PROTOTYPE: $
3384 CODE:
3385 self->flags |= CF_SUSPENDED;
3386
3387void
3388resume (Coro::State self)
3389 PROTOTYPE: $
3390 CODE:
3391 self->flags &= ~CF_SUSPENDED;
3262 3392
3263void 3393void
3264_pool_handler (...) 3394_pool_handler (...)
3265 CODE: 3395 CODE:
3266 CORO_EXECUTE_SLF_XS (slf_init_pool_handler); 3396 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
3321rouse_wait (...) 3451rouse_wait (...)
3322 PROTOTYPE: ;$ 3452 PROTOTYPE: ;$
3323 PPCODE: 3453 PPCODE:
3324 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3454 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3325 3455
3456void
3457on_enter (SV *block)
3458 ALIAS:
3459 on_leave = 1
3460 PROTOTYPE: &
3461 CODE:
3462{
3463 struct coro *coro = SvSTATE_current;
3464 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3465
3466 block = (SV *)coro_sv_2cv (aTHX_ block);
3467
3468 if (!*avp)
3469 *avp = newAV ();
3470
3471 av_push (*avp, SvREFCNT_inc (block));
3472
3473 if (!ix)
3474 on_enterleave_call (aTHX_ block);
3475
3476 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3477 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3478 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3479}
3480
3326 3481
3327MODULE = Coro::State PACKAGE = PerlIO::cede 3482MODULE = Coro::State PACKAGE = PerlIO::cede
3328 3483
3329BOOT: 3484BOOT:
3330 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3485 PerlIO_define_layer (aTHX_ &PerlIO_cede);
3340 GvSTASH (CvGV (cv)) 3495 GvSTASH (CvGV (cv))
3341 ); 3496 );
3342 OUTPUT: 3497 OUTPUT:
3343 RETVAL 3498 RETVAL
3344 3499
3345# helper for Coro::Channel 3500# helper for Coro::Channel and others
3346SV * 3501SV *
3347_alloc (int count) 3502_alloc (int count)
3348 CODE: 3503 CODE:
3349 RETVAL = coro_waitarray_new (aTHX_ count); 3504 RETVAL = coro_waitarray_new (aTHX_ count);
3350 OUTPUT: 3505 OUTPUT:
3408 for (i = 1; i <= wcount; ++i) 3563 for (i = 1; i <= wcount; ++i)
3409 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3564 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
3410 } 3565 }
3411} 3566}
3412 3567
3568MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
3569
3570void
3571_may_delete (SV *sem, int count, int extra_refs)
3572 PPCODE:
3573{
3574 AV *av = (AV *)SvRV (sem);
3575
3576 if (SvREFCNT ((SV *)av) == 1 + extra_refs
3577 && AvFILLp (av) == 0 /* no waiters, just count */
3578 && SvIV (AvARRAY (av)[0]) == count)
3579 XSRETURN_YES;
3580
3581 XSRETURN_NO;
3582}
3583
3413MODULE = Coro::State PACKAGE = Coro::Signal 3584MODULE = Coro::State PACKAGE = Coro::Signal
3414 3585
3415SV * 3586SV *
3416new (SV *klass) 3587new (SV *klass)
3417 CODE: 3588 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines