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.157 by root, Fri Sep 21 02:01:13 2007 UTC vs.
Revision 1.166 by root, Wed Sep 26 21:11:28 2007 UTC

88/* prefer perl internal functions over our own? */ 88/* prefer perl internal functions over our own? */
89#ifndef CORO_PREFER_PERL_FUNCTIONS 89#ifndef CORO_PREFER_PERL_FUNCTIONS
90# define CORO_PREFER_PERL_FUNCTIONS 0 90# define CORO_PREFER_PERL_FUNCTIONS 0
91#endif 91#endif
92 92
93/* The next macro should declare a variable stacklevel that contains and approximation 93/* The next macros try to return the current stack pointer, in an as
94 * to the current C stack pointer. Its property is that it changes with each call 94 * portable way as possible. */
95 * and should be unique. */
96#define dSTACKLEVEL int stacklevel 95#define dSTACKLEVEL volatile char stacklevel
97#define STACKLEVEL ((void *)&stacklevel) 96#define STACKLEVEL ((void *)&stacklevel)
98 97
99#define IN_DESTRUCT (PL_main_cv == Nullcv) 98#define IN_DESTRUCT (PL_main_cv == Nullcv)
100 99
101#if __GNUC__ >= 3 100#if __GNUC__ >= 3
133static AV *main_mainstack; /* used to differentiate between $main and others */ 132static AV *main_mainstack; /* used to differentiate between $main and others */
134static JMPENV *main_top_env; 133static JMPENV *main_top_env;
135static HV *coro_state_stash, *coro_stash; 134static HV *coro_state_stash, *coro_stash;
136static SV *coro_mortal; /* will be freed after next transfer */ 135static SV *coro_mortal; /* will be freed after next transfer */
137 136
137/* async_pool helper stuff */
138static SV *sv_pool_rss;
139static SV *sv_pool_size;
140static AV *av_async_pool;
141
138static struct coro_cctx *cctx_first; 142static struct coro_cctx *cctx_first;
139static int cctx_count, cctx_idle; 143static int cctx_count, cctx_idle;
144
145enum {
146 CC_MAPPED = 0x01,
147 CC_TRACE = 0x02,
148 CC_NOREUSE = 0x04, /* throw this away after tracing */
149};
140 150
141/* this is a structure representing a c-level coroutine */ 151/* this is a structure representing a c-level coroutine */
142typedef struct coro_cctx { 152typedef struct coro_cctx {
143 struct coro_cctx *next; 153 struct coro_cctx *next;
144 154
153 coro_context cctx; 163 coro_context cctx;
154 164
155#if CORO_USE_VALGRIND 165#if CORO_USE_VALGRIND
156 int valgrind_id; 166 int valgrind_id;
157#endif 167#endif
158 char inuse, mapped; 168 unsigned char flags;
159} coro_cctx; 169} coro_cctx;
160 170
161enum { 171enum {
162 CF_RUNNING = 0x0001, /* coroutine is running */ 172 CF_RUNNING = 0x0001, /* coroutine is running */
163 CF_READY = 0x0002, /* coroutine is ready */ 173 CF_READY = 0x0002, /* coroutine is ready */
424 434
425 if (top_si->si_type == PERLSI_MAIN) 435 if (top_si->si_type == PERLSI_MAIN)
426 break; 436 break;
427 437
428 top_si = top_si->si_prev; 438 top_si = top_si->si_prev;
429 ccstk = top_si->si_cxstack; 439 ccstk = top_si->si_cxstack;
430 cxix = top_si->si_cxix; 440 cxix = top_si->si_cxix;
431 } 441 }
432 442
433 PUTBACK; 443 PUTBACK;
434 } 444 }
435 445
620 SvREFCNT_dec (coro_mortal); 630 SvREFCNT_dec (coro_mortal);
621 coro_mortal = 0; 631 coro_mortal = 0;
622 } 632 }
623} 633}
624 634
635static void
636do_trace (pTHX)
637{
638 if (PL_curcop != &PL_compiling)
639 {
640 runops_proc_t old_runops = PL_runops;
641 dSP;
642 ENTER;
643 SAVETMPS;
644 PUSHMARK (SP);
645 PUTBACK;
646 PL_runops = RUNOPS_DEFAULT;
647 call_pv ("Coro::_do_trace", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD | G_NOARGS);
648 PL_runops = old_runops;
649 SPAGAIN;
650 FREETMPS;
651 LEAVE;
652 PUTBACK;
653 }
654}
655
656static int
657runops_coro (pTHX)
658{
659 COP *oldcop = 0;
660
661 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
662 {
663 PERL_ASYNC_CHECK ();
664
665 if (oldcop != PL_curcop)
666 {
667 oldcop = PL_curcop;
668 do_trace (aTHX);
669 }
670 }
671
672 TAINT_NOT;
673 return 0;
674}
675
625/* inject a fake call to Coro::State::_cctx_init into the execution */ 676/* inject a fake call to Coro::State::_cctx_init into the execution */
626/* _cctx_init should be careful, as it could be called at almost any time */ 677/* _cctx_init should be careful, as it could be called at almost any time */
627/* during execution of a perl program */ 678/* during execution of a perl program */
628static void NOINLINE 679static void NOINLINE
629prepare_cctx (pTHX_ coro_cctx *cctx) 680prepare_cctx (pTHX_ coro_cctx *cctx)
630{ 681{
631 dSP; 682 dSP;
632 LOGOP myop; 683 LOGOP myop;
684
685 PL_top_env = &PL_start_env;
686
687 if (cctx->flags & CC_TRACE)
688 PL_runops = runops_coro;
633 689
634 Zero (&myop, 1, LOGOP); 690 Zero (&myop, 1, LOGOP);
635 myop.op_next = PL_op; 691 myop.op_next = PL_op;
636 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 692 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
637 693
654 dTHX; 710 dTHX;
655 711
656 /* coro_run is the alternative tail of transfer(), so unlock here. */ 712 /* coro_run is the alternative tail of transfer(), so unlock here. */
657 UNLOCK; 713 UNLOCK;
658 714
659 PL_top_env = &PL_start_env; 715 /* we now skip the entersub that lead to transfer() */
716 PL_op = PL_op->op_next;
660 717
661 /* inject a fake subroutine call to cctx_init */ 718 /* inject a fake subroutine call to cctx_init */
662 prepare_cctx (aTHX_ (coro_cctx *)arg); 719 prepare_cctx (aTHX_ (coro_cctx *)arg);
663 720
664 /* somebody or something will hit me for both perl_run and PL_restartop */ 721 /* somebody or something will hit me for both perl_run and PL_restartop */
698# if CORO_STACKGUARD 755# if CORO_STACKGUARD
699 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 756 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
700# endif 757# endif
701 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 758 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
702 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 759 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
703 cctx->mapped = 1; 760 cctx->flags |= CC_MAPPED;
704 } 761 }
705 else 762 else
706#endif 763#endif
707 { 764 {
708 cctx->ssize = coro_stacksize * (long)sizeof (long); 765 cctx->ssize = coro_stacksize * (long)sizeof (long);
735#if CORO_USE_VALGRIND 792#if CORO_USE_VALGRIND
736 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 793 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
737#endif 794#endif
738 795
739#if HAVE_MMAP 796#if HAVE_MMAP
740 if (cctx->mapped) 797 if (cctx->flags & CC_MAPPED)
741 munmap (cctx->sptr, cctx->ssize); 798 munmap (cctx->sptr, cctx->ssize);
742 else 799 else
743#endif 800#endif
744 Safefree (cctx->sptr); 801 Safefree (cctx->sptr);
745 802
753 { 810 {
754 coro_cctx *cctx = cctx_first; 811 coro_cctx *cctx = cctx_first;
755 cctx_first = cctx->next; 812 cctx_first = cctx->next;
756 --cctx_idle; 813 --cctx_idle;
757 814
758 if (cctx->ssize >= coro_stacksize) 815 if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE))
759 return cctx; 816 return cctx;
760 817
761 cctx_destroy (cctx); 818 cctx_destroy (cctx);
762 } 819 }
763 820
764 PL_op = PL_op->op_next;
765 return cctx_new (); 821 return cctx_new ();
766} 822}
767 823
768static void 824static void
769cctx_put (coro_cctx *cctx) 825cctx_put (coro_cctx *cctx)
773 { 829 {
774 coro_cctx *first = cctx_first; 830 coro_cctx *first = cctx_first;
775 cctx_first = first->next; 831 cctx_first = first->next;
776 --cctx_idle; 832 --cctx_idle;
777 833
778 assert (!first->inuse);
779 cctx_destroy (first); 834 cctx_destroy (first);
780 } 835 }
781 836
782 ++cctx_idle; 837 ++cctx_idle;
783 cctx->next = cctx_first; 838 cctx->next = cctx_first;
804 859
805 if (prev->flags & CF_NEW) 860 if (prev->flags & CF_NEW)
806 { 861 {
807 /* create a new empty context */ 862 /* create a new empty context */
808 Newz (0, prev->cctx, 1, coro_cctx); 863 Newz (0, prev->cctx, 1, coro_cctx);
809 prev->cctx->inuse = 1;
810 prev->flags &= ~CF_NEW; 864 prev->flags &= ~CF_NEW;
811 prev->flags |= CF_RUNNING; 865 prev->flags |= CF_RUNNING;
812 } 866 }
813 867
814 /*TODO: must not croak here */ 868 /*TODO: must not croak here */
832 next->flags &= ~CF_NEW; 886 next->flags &= ~CF_NEW;
833 /* first get rid of the old state */ 887 /* first get rid of the old state */
834 save_perl (aTHX_ prev); 888 save_perl (aTHX_ prev);
835 /* setup coroutine call */ 889 /* setup coroutine call */
836 setup_coro (aTHX_ next); 890 setup_coro (aTHX_ next);
837 /* need a new stack */
838 assert (!next->cctx);
839 } 891 }
840 else 892 else
841 { 893 {
842 /* coroutine already started */ 894 /* coroutine already started */
843 save_perl (aTHX_ prev); 895 save_perl (aTHX_ prev);
853 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 905 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
854 906
855 prev->cctx = 0; 907 prev->cctx = 0;
856 908
857 cctx_put (prev__cctx); 909 cctx_put (prev__cctx);
858 prev__cctx->inuse = 0;
859 } 910 }
860 911
861 if (!next->cctx) 912 if (!next->cctx)
862 {
863 next->cctx = cctx_get (aTHX); 913 next->cctx = cctx_get (aTHX);
864 assert (!next->cctx->inuse);
865 next->cctx->inuse = 1;
866 }
867 914
868 if (prev__cctx != next->cctx) 915 if (prev__cctx != next->cctx)
869 { 916 {
870 prev__cctx->top_env = PL_top_env; 917 prev__cctx->top_env = PL_top_env;
871 PL_top_env = next->cctx->top_env; 918 PL_top_env = next->cctx->top_env;
1375 if (coro->hv) 1422 if (coro->hv)
1376 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); 1423 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1377} 1424}
1378 1425
1379void 1426void
1380_eval (Coro::State coro, SV *coderef) 1427call (Coro::State coro, SV *coderef)
1428 ALIAS:
1429 eval = 1
1381 CODE: 1430 CODE:
1382{ 1431{
1383 if (coro->mainstack) 1432 if (coro->mainstack)
1384 { 1433 {
1385 struct coro temp; 1434 struct coro temp;
1396 dSP; 1445 dSP;
1397 ENTER; 1446 ENTER;
1398 SAVETMPS; 1447 SAVETMPS;
1399 PUSHMARK (SP); 1448 PUSHMARK (SP);
1400 PUTBACK; 1449 PUTBACK;
1450 if (ix)
1451 eval_sv (coderef, 0);
1452 else
1401 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1453 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1402 SPAGAIN; 1454 SPAGAIN;
1403 FREETMPS; 1455 FREETMPS;
1404 LEAVE; 1456 LEAVE;
1405 PUTBACK; 1457 PUTBACK;
1406 } 1458 }
1424 CODE: 1476 CODE:
1425 RETVAL = boolSV (coro->flags & ix); 1477 RETVAL = boolSV (coro->flags & ix);
1426 OUTPUT: 1478 OUTPUT:
1427 RETVAL 1479 RETVAL
1428 1480
1481void
1482trace (Coro::State coro, int enable = 0)
1483 CODE:
1484 if (enable)
1485 {
1486 if (coro->cctx && coro->cctx->flags & CC_TRACE)
1487 XSRETURN_EMPTY;
1488
1489 if (coro->flags & CF_RUNNING)
1490 croak ("cannot enable tracing on running coroutine");
1491
1492 if (coro->cctx)
1493 croak ("cannot enable tracing on coroutine with custom stack");
1494
1495 coro->cctx = cctx_new ();
1496 coro->cctx->flags |= CC_TRACE;
1497 }
1498 else
1499 if (coro->cctx && coro->cctx->flags & CC_TRACE)
1500 {
1501 coro->cctx->flags &= ~CC_TRACE;
1502 coro->cctx->flags |= CC_NOREUSE;
1503 }
1504
1505SV *
1506has_stack (Coro::State coro)
1507 PROTOTYPE: $
1508 CODE:
1509 RETVAL = boolSV (!!coro->cctx);
1510 OUTPUT:
1511 RETVAL
1512
1429IV 1513IV
1430rss (Coro::State coro) 1514rss (Coro::State coro)
1431 PROTOTYPE: $ 1515 PROTOTYPE: $
1432 CODE: 1516 CODE:
1433 RETVAL = coro_rss (coro); 1517 RETVAL = coro_rss (coro);
1439 1523
1440BOOT: 1524BOOT:
1441{ 1525{
1442 int i; 1526 int i;
1443 1527
1528 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE);
1529 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE);
1530 av_async_pool = get_av ("Coro::async_pool", TRUE);
1531
1532 coro_current = get_sv ("Coro::current", FALSE);
1533 SvREADONLY_on (coro_current);
1534
1444 coro_stash = gv_stashpv ("Coro", TRUE); 1535 coro_stash = gv_stashpv ("Coro", TRUE);
1445 1536
1446 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1537 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1447 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1538 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1448 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1539 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1449 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1540 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1450 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1541 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1451 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1542 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1452
1453 coro_current = get_sv ("Coro::current", FALSE);
1454 SvREADONLY_on (coro_current);
1455 1543
1456 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1544 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1457 coro_ready[i] = newAV (); 1545 coro_ready[i] = newAV ();
1458 1546
1459 { 1547 {
1517 CODE: 1605 CODE:
1518 RETVAL = coro_nready; 1606 RETVAL = coro_nready;
1519 OUTPUT: 1607 OUTPUT:
1520 RETVAL 1608 RETVAL
1521 1609
1610# for async_pool speedup
1611void
1612_pool_1 (SV *cb)
1613 CODE:
1614{
1615 int i, len;
1616 HV *hv = (HV *)SvRV (coro_current);
1617 AV *defav = GvAV (PL_defgv);
1618 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1619 AV *invoke_av;
1620
1621 if (!invoke)
1622 croak ("\3terminate\2\n");
1623
1624 hv_store (hv, "desc", sizeof ("desc") - 1,
1625 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1626
1627 invoke_av = (AV *)SvRV (invoke);
1628 len = av_len (invoke_av);
1629
1630 sv_setsv (cb, AvARRAY (invoke_av)[0]);
1631
1632 if (len > 0)
1633 {
1634 av_fill (defav, len - 1);
1635 for (i = 0; i < len; ++i)
1636 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1]));
1637 }
1638
1639 SvREFCNT_dec (invoke);
1640}
1641
1642void
1643_pool_2 (SV *cb)
1644 CODE:
1645{
1646 struct coro *coro = SvSTATE (coro_current);
1647
1648 sv_setsv (cb, &PL_sv_undef);
1649
1650 if (coro_rss (coro) > SvIV (sv_pool_rss)
1651 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1652 croak ("\3terminate\2\n");
1653
1654 av_clear (GvAV (PL_defgv));
1655 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1656 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1657 coro->save = CORO_SAVE_DEF;
1658 coro->prio = 0;
1659 av_push (av_async_pool, newSVsv (coro_current));
1660}
1661
1662
1522MODULE = Coro::State PACKAGE = Coro::AIO 1663MODULE = Coro::State PACKAGE = Coro::AIO
1523 1664
1524SV * 1665SV *
1525_get_state () 1666_get_state ()
1526 CODE: 1667 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines