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.160 by root, Sat Sep 22 22:39:15 2007 UTC vs.
Revision 1.167 by root, Thu Sep 27 15:52:31 2007 UTC

140static AV *av_async_pool; 140static AV *av_async_pool;
141 141
142static struct coro_cctx *cctx_first; 142static struct coro_cctx *cctx_first;
143static int cctx_count, cctx_idle; 143static int cctx_count, cctx_idle;
144 144
145enum {
146 CC_MAPPED = 0x01,
147 CC_NOREUSE = 0x02, /* throw this away after tracing */
148 CC_TRACE = 0x04,
149 CC_TRACE_SUB = 0x08, /* trace sub calls */
150 CC_TRACE_LINE = 0x10, /* trace each statement */
151 CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE,
152};
153
145/* this is a structure representing a c-level coroutine */ 154/* this is a structure representing a c-level coroutine */
146typedef struct coro_cctx { 155typedef struct coro_cctx {
147 struct coro_cctx *next; 156 struct coro_cctx *next;
148 157
149 /* the stack */ 158 /* the stack */
157 coro_context cctx; 166 coro_context cctx;
158 167
159#if CORO_USE_VALGRIND 168#if CORO_USE_VALGRIND
160 int valgrind_id; 169 int valgrind_id;
161#endif 170#endif
162 char inuse, mapped; 171 unsigned char flags;
163} coro_cctx; 172} coro_cctx;
164 173
165enum { 174enum {
166 CF_RUNNING = 0x0001, /* coroutine is running */ 175 CF_RUNNING = 0x0001, /* coroutine is running */
167 CF_READY = 0x0002, /* coroutine is ready */ 176 CF_READY = 0x0002, /* coroutine is ready */
624 SvREFCNT_dec (coro_mortal); 633 SvREFCNT_dec (coro_mortal);
625 coro_mortal = 0; 634 coro_mortal = 0;
626 } 635 }
627} 636}
628 637
638static int
639runops_coro (pTHX)
640{
641 COP *oldcop = 0;
642 int oldcxix = -2;
643
644 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
645 {
646 PERL_ASYNC_CHECK ();
647
648 if (PL_op->op_type == OP_LEAVESUB)
649 {
650 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
651 SV **bot, **top;
652 AV *av = newAV (); /* return values */
653 runops_proc_t old_runops = PL_runops;
654 dSP;
655 ENTER;
656 SAVETMPS;
657 EXTEND (SP, 3);
658 PL_runops = RUNOPS_DEFAULT;
659
660 GV *gv = CvGV (cx->blk_sub.cv);
661 SV *fullname = sv_2mortal (newSV (0));
662 if (isGV (gv))
663 gv_efullname3 (fullname, gv, 0);
664
665 bot = PL_stack_base + cx->blk_oldsp + 1;
666 top = cx->blk_gimme == G_ARRAY ? SP + 1
667 : cx->blk_gimme == G_SCALAR ? bot + 1
668 : bot;
669
670 while (bot < top)
671 av_push (av, SvREFCNT_inc (*bot++));
672
673 PUSHMARK (SP);
674 PUSHs (&PL_sv_no);
675 PUSHs (fullname);
676 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
677 PUTBACK;
678 call_pv ("Coro::_do_trace_sub", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
679 SPAGAIN;
680
681 FREETMPS;
682 LEAVE;
683 PL_runops = old_runops;
684 }
685
686 if (oldcop != PL_curcop)
687 {
688 oldcop = PL_curcop;
689
690 if (PL_curcop != &PL_compiling)
691 {
692 runops_proc_t old_runops = PL_runops;
693 dSP;
694 ENTER;
695 SAVETMPS;
696 EXTEND (SP, 3);
697 PL_runops = RUNOPS_DEFAULT;
698
699 if (oldcxix != cxstack_ix)
700 {
701 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
702
703 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
704 {
705 GV *gv = CvGV (cx->blk_sub.cv);
706 SV *fullname = sv_2mortal (newSV (0));
707 if (isGV (gv))
708 gv_efullname3 (fullname, gv, 0);
709
710 PUSHMARK (SP);
711 PUSHs (&PL_sv_yes);
712 PUSHs (fullname);
713 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
714 PUTBACK;
715 call_pv ("Coro::_do_trace_sub", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
716 SPAGAIN;
717 }
718
719 oldcxix = cxstack_ix;
720 }
721
722 if (0) {
723 PUSHMARK (SP);
724 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
725 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop))));
726 PUTBACK;
727 call_pv ("Coro::_do_trace_line", G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
728 SPAGAIN;
729 }
730
731 FREETMPS;
732 LEAVE;
733 PL_runops = old_runops;
734 }
735 }
736 }
737
738 TAINT_NOT;
739 return 0;
740}
741
629/* inject a fake call to Coro::State::_cctx_init into the execution */ 742/* inject a fake call to Coro::State::_cctx_init into the execution */
630/* _cctx_init should be careful, as it could be called at almost any time */ 743/* _cctx_init should be careful, as it could be called at almost any time */
631/* during execution of a perl program */ 744/* during execution of a perl program */
632static void NOINLINE 745static void NOINLINE
633prepare_cctx (pTHX_ coro_cctx *cctx) 746prepare_cctx (pTHX_ coro_cctx *cctx)
634{ 747{
635 dSP; 748 dSP;
636 LOGOP myop; 749 LOGOP myop;
750
751 PL_top_env = &PL_start_env;
752
753 if (cctx->flags & CC_TRACE)
754 PL_runops = runops_coro;
637 755
638 Zero (&myop, 1, LOGOP); 756 Zero (&myop, 1, LOGOP);
639 myop.op_next = PL_op; 757 myop.op_next = PL_op;
640 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 758 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
641 759
658 dTHX; 776 dTHX;
659 777
660 /* coro_run is the alternative tail of transfer(), so unlock here. */ 778 /* coro_run is the alternative tail of transfer(), so unlock here. */
661 UNLOCK; 779 UNLOCK;
662 780
663 PL_top_env = &PL_start_env; 781 /* we now skip the entersub that lead to transfer() */
782 PL_op = PL_op->op_next;
664 783
665 /* inject a fake subroutine call to cctx_init */ 784 /* inject a fake subroutine call to cctx_init */
666 prepare_cctx (aTHX_ (coro_cctx *)arg); 785 prepare_cctx (aTHX_ (coro_cctx *)arg);
667 786
668 /* somebody or something will hit me for both perl_run and PL_restartop */ 787 /* somebody or something will hit me for both perl_run and PL_restartop */
702# if CORO_STACKGUARD 821# if CORO_STACKGUARD
703 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 822 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
704# endif 823# endif
705 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 824 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
706 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 825 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
707 cctx->mapped = 1; 826 cctx->flags |= CC_MAPPED;
708 } 827 }
709 else 828 else
710#endif 829#endif
711 { 830 {
712 cctx->ssize = coro_stacksize * (long)sizeof (long); 831 cctx->ssize = coro_stacksize * (long)sizeof (long);
739#if CORO_USE_VALGRIND 858#if CORO_USE_VALGRIND
740 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 859 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
741#endif 860#endif
742 861
743#if HAVE_MMAP 862#if HAVE_MMAP
744 if (cctx->mapped) 863 if (cctx->flags & CC_MAPPED)
745 munmap (cctx->sptr, cctx->ssize); 864 munmap (cctx->sptr, cctx->ssize);
746 else 865 else
747#endif 866#endif
748 Safefree (cctx->sptr); 867 Safefree (cctx->sptr);
749 868
757 { 876 {
758 coro_cctx *cctx = cctx_first; 877 coro_cctx *cctx = cctx_first;
759 cctx_first = cctx->next; 878 cctx_first = cctx->next;
760 --cctx_idle; 879 --cctx_idle;
761 880
762 if (cctx->ssize >= coro_stacksize) 881 if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE))
763 return cctx; 882 return cctx;
764 883
765 cctx_destroy (cctx); 884 cctx_destroy (cctx);
766 } 885 }
767 886
768 PL_op = PL_op->op_next;
769 return cctx_new (); 887 return cctx_new ();
770} 888}
771 889
772static void 890static void
773cctx_put (coro_cctx *cctx) 891cctx_put (coro_cctx *cctx)
777 { 895 {
778 coro_cctx *first = cctx_first; 896 coro_cctx *first = cctx_first;
779 cctx_first = first->next; 897 cctx_first = first->next;
780 --cctx_idle; 898 --cctx_idle;
781 899
782 assert (!first->inuse);
783 cctx_destroy (first); 900 cctx_destroy (first);
784 } 901 }
785 902
786 ++cctx_idle; 903 ++cctx_idle;
787 cctx->next = cctx_first; 904 cctx->next = cctx_first;
808 925
809 if (prev->flags & CF_NEW) 926 if (prev->flags & CF_NEW)
810 { 927 {
811 /* create a new empty context */ 928 /* create a new empty context */
812 Newz (0, prev->cctx, 1, coro_cctx); 929 Newz (0, prev->cctx, 1, coro_cctx);
813 prev->cctx->inuse = 1;
814 prev->flags &= ~CF_NEW; 930 prev->flags &= ~CF_NEW;
815 prev->flags |= CF_RUNNING; 931 prev->flags |= CF_RUNNING;
816 } 932 }
817 933
818 /*TODO: must not croak here */ 934 /*TODO: must not croak here */
836 next->flags &= ~CF_NEW; 952 next->flags &= ~CF_NEW;
837 /* first get rid of the old state */ 953 /* first get rid of the old state */
838 save_perl (aTHX_ prev); 954 save_perl (aTHX_ prev);
839 /* setup coroutine call */ 955 /* setup coroutine call */
840 setup_coro (aTHX_ next); 956 setup_coro (aTHX_ next);
841 /* need a new stack */
842 assert (!next->cctx);
843 } 957 }
844 else 958 else
845 { 959 {
846 /* coroutine already started */ 960 /* coroutine already started */
847 save_perl (aTHX_ prev); 961 save_perl (aTHX_ prev);
857 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 971 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
858 972
859 prev->cctx = 0; 973 prev->cctx = 0;
860 974
861 cctx_put (prev__cctx); 975 cctx_put (prev__cctx);
862 prev__cctx->inuse = 0;
863 } 976 }
864 977
865 if (!next->cctx) 978 if (!next->cctx)
866 {
867 next->cctx = cctx_get (aTHX); 979 next->cctx = cctx_get (aTHX);
868 assert (!next->cctx->inuse);
869 next->cctx->inuse = 1;
870 }
871 980
872 if (prev__cctx != next->cctx) 981 if (prev__cctx != next->cctx)
873 { 982 {
874 prev__cctx->top_env = PL_top_env; 983 prev__cctx->top_env = PL_top_env;
875 PL_top_env = next->cctx->top_env; 984 PL_top_env = next->cctx->top_env;
1219#endif 1328#endif
1220 BOOT_PAGESIZE; 1329 BOOT_PAGESIZE;
1221 1330
1222 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1331 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1223 1332
1333 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1334 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1335 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1336 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1337
1224 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1338 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1225 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1339 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1226 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1340 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1227 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); 1341 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1228 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); 1342 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH));
1379 if (coro->hv) 1493 if (coro->hv)
1380 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); 1494 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1381} 1495}
1382 1496
1383void 1497void
1384_eval (Coro::State coro, SV *coderef) 1498call (Coro::State coro, SV *coderef)
1499 ALIAS:
1500 eval = 1
1385 CODE: 1501 CODE:
1386{ 1502{
1387 if (coro->mainstack) 1503 if (coro->mainstack)
1388 { 1504 {
1389 struct coro temp; 1505 struct coro temp;
1400 dSP; 1516 dSP;
1401 ENTER; 1517 ENTER;
1402 SAVETMPS; 1518 SAVETMPS;
1403 PUSHMARK (SP); 1519 PUSHMARK (SP);
1404 PUTBACK; 1520 PUTBACK;
1521 if (ix)
1522 eval_sv (coderef, 0);
1523 else
1405 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1524 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1406 SPAGAIN; 1525 SPAGAIN;
1407 FREETMPS; 1526 FREETMPS;
1408 LEAVE; 1527 LEAVE;
1409 PUTBACK; 1528 PUTBACK;
1410 } 1529 }
1428 CODE: 1547 CODE:
1429 RETVAL = boolSV (coro->flags & ix); 1548 RETVAL = boolSV (coro->flags & ix);
1430 OUTPUT: 1549 OUTPUT:
1431 RETVAL 1550 RETVAL
1432 1551
1552void
1553trace (Coro::State coro, int flags = CC_TRACE | CC_TRACE_SUB)
1554 CODE:
1555 if (flags & CC_TRACE)
1556 {
1557 if (!coro->cctx)
1558 coro->cctx = cctx_new ();
1559 else if (!(coro->cctx->flags & CC_TRACE))
1560 croak ("cannot enable tracing on coroutine with custom stack");
1561
1562 coro->cctx->flags |= flags & (CC_TRACE | CC_TRACE_ALL);
1563 }
1564 else
1565 if (coro->cctx && coro->cctx->flags & CC_TRACE)
1566 {
1567 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL);
1568 coro->cctx->flags |= CC_NOREUSE;
1569 }
1570
1571SV *
1572has_stack (Coro::State coro)
1573 PROTOTYPE: $
1574 CODE:
1575 RETVAL = boolSV (!!coro->cctx);
1576 OUTPUT:
1577 RETVAL
1578
1579int
1580is_traced (Coro::State coro)
1581 PROTOTYPE: $
1582 CODE:
1583 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1584 OUTPUT:
1585 RETVAL
1586
1433IV 1587IV
1434rss (Coro::State coro) 1588rss (Coro::State coro)
1435 PROTOTYPE: $ 1589 PROTOTYPE: $
1436 CODE: 1590 CODE:
1437 RETVAL = coro_rss (coro); 1591 RETVAL = coro_rss (coro);
1526 RETVAL = coro_nready; 1680 RETVAL = coro_nready;
1527 OUTPUT: 1681 OUTPUT:
1528 RETVAL 1682 RETVAL
1529 1683
1530# for async_pool speedup 1684# for async_pool speedup
1531SV * 1685void
1532_pool_1 (...) 1686_pool_1 (SV *cb)
1533 CODE: 1687 CODE:
1534{ 1688{
1535 int i, len; 1689 int i, len;
1536 HV *hv = (HV *)SvRV (coro_current); 1690 HV *hv = (HV *)SvRV (coro_current);
1537 AV *defav = GvAV (PL_defgv); 1691 AV *defav = GvAV (PL_defgv);
1538 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); 1692 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1539 AV *invoke_av; 1693 AV *invoke_av;
1540 1694
1541 if (!invoke) 1695 if (!invoke)
1542 XSRETURN_EMPTY; 1696 croak ("\3terminate\2\n");
1543 1697
1544 hv_store (hv, "desc", sizeof ("desc") - 1, 1698 hv_store (hv, "desc", sizeof ("desc") - 1,
1545 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 1699 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1546 1700
1547 invoke_av = (AV *)SvRV (invoke); 1701 invoke_av = (AV *)SvRV (invoke);
1548 len = av_len (invoke_av); 1702 len = av_len (invoke_av);
1549 1703
1550 RETVAL = SvREFCNT_inc (AvARRAY (invoke_av)[0]); 1704 sv_setsv (cb, AvARRAY (invoke_av)[0]);
1551 1705
1552 if (len > 0) 1706 if (len > 0)
1553 { 1707 {
1554 av_clear (defav);
1555 av_extend (defav, len); 1708 av_fill (defav, len - 1);
1556 for (i = 0; i < len; ++i) 1709 for (i = 0; i < len; ++i)
1557 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 1710 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1]));
1558 } 1711 }
1559 1712
1560 SvREFCNT_dec (invoke); 1713 SvREFCNT_dec (invoke);
1561} 1714}
1562 OUTPUT:
1563 RETVAL
1564 1715
1565void 1716void
1566_pool_2 (...) 1717_pool_2 (SV *cb)
1567 CODE: 1718 CODE:
1568{ 1719{
1569 struct coro *coro = SvSTATE (coro_current); 1720 struct coro *coro = SvSTATE (coro_current);
1721
1722 sv_setsv (cb, &PL_sv_undef);
1570 1723
1571 if (coro_rss (coro) > SvIV (sv_pool_rss) 1724 if (coro_rss (coro) > SvIV (sv_pool_rss)
1572 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 1725 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1573 XSRETURN_YES; 1726 croak ("\3terminate\2\n");
1574 1727
1575 av_clear (GvAV (PL_defgv)); 1728 av_clear (GvAV (PL_defgv));
1576 hv_store (SvRV (coro_current), "desc", sizeof ("desc") - 1, 1729 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1577 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); 1730 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1578 coro->save = CORO_SAVE_DEF; 1731 coro->save = CORO_SAVE_DEF;
1579 coro->prio = 0; 1732 coro->prio = 0;
1580 av_push (av_async_pool, newSVsv (coro_current)); 1733 av_push (av_async_pool, newSVsv (coro_current));
1581
1582 XSRETURN_NO;
1583} 1734}
1584 1735
1585 1736
1586MODULE = Coro::State PACKAGE = Coro::AIO 1737MODULE = Coro::State PACKAGE = Coro::AIO
1587 1738

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines