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.158 by root, Fri Sep 21 02:23:48 2007 UTC vs.
Revision 1.168 by root, Thu Sep 27 16:10:48 2007 UTC

132static AV *main_mainstack; /* used to differentiate between $main and others */ 132static AV *main_mainstack; /* used to differentiate between $main and others */
133static JMPENV *main_top_env; 133static JMPENV *main_top_env;
134static HV *coro_state_stash, *coro_stash; 134static HV *coro_state_stash, *coro_stash;
135static SV *coro_mortal; /* will be freed after next transfer */ 135static SV *coro_mortal; /* will be freed after next transfer */
136 136
137/* async_pool helper stuff */
138static SV *sv_pool_rss;
139static SV *sv_pool_size;
140static AV *av_async_pool;
141
137static struct coro_cctx *cctx_first; 142static struct coro_cctx *cctx_first;
138static int cctx_count, cctx_idle; 143static int cctx_count, cctx_idle;
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};
139 153
140/* this is a structure representing a c-level coroutine */ 154/* this is a structure representing a c-level coroutine */
141typedef struct coro_cctx { 155typedef struct coro_cctx {
142 struct coro_cctx *next; 156 struct coro_cctx *next;
143 157
152 coro_context cctx; 166 coro_context cctx;
153 167
154#if CORO_USE_VALGRIND 168#if CORO_USE_VALGRIND
155 int valgrind_id; 169 int valgrind_id;
156#endif 170#endif
157 char inuse, mapped; 171 unsigned char flags;
158} coro_cctx; 172} coro_cctx;
159 173
160enum { 174enum {
161 CF_RUNNING = 0x0001, /* coroutine is running */ 175 CF_RUNNING = 0x0001, /* coroutine is running */
162 CF_READY = 0x0002, /* coroutine is ready */ 176 CF_READY = 0x0002, /* coroutine is ready */
619 SvREFCNT_dec (coro_mortal); 633 SvREFCNT_dec (coro_mortal);
620 coro_mortal = 0; 634 coro_mortal = 0;
621 } 635 }
622} 636}
623 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
624/* 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 */
625/* _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 */
626/* during execution of a perl program */ 744/* during execution of a perl program */
627static void NOINLINE 745static void NOINLINE
628prepare_cctx (pTHX_ coro_cctx *cctx) 746prepare_cctx (pTHX_ coro_cctx *cctx)
629{ 747{
630 dSP; 748 dSP;
631 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;
632 755
633 Zero (&myop, 1, LOGOP); 756 Zero (&myop, 1, LOGOP);
634 myop.op_next = PL_op; 757 myop.op_next = PL_op;
635 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 758 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
636 759
653 dTHX; 776 dTHX;
654 777
655 /* coro_run is the alternative tail of transfer(), so unlock here. */ 778 /* coro_run is the alternative tail of transfer(), so unlock here. */
656 UNLOCK; 779 UNLOCK;
657 780
658 PL_top_env = &PL_start_env; 781 /* we now skip the entersub that lead to transfer() */
782 PL_op = PL_op->op_next;
659 783
660 /* inject a fake subroutine call to cctx_init */ 784 /* inject a fake subroutine call to cctx_init */
661 prepare_cctx (aTHX_ (coro_cctx *)arg); 785 prepare_cctx (aTHX_ (coro_cctx *)arg);
662 786
663 /* 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 */
697# if CORO_STACKGUARD 821# if CORO_STACKGUARD
698 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 822 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
699# endif 823# endif
700 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 824 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
701 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 825 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
702 cctx->mapped = 1; 826 cctx->flags |= CC_MAPPED;
703 } 827 }
704 else 828 else
705#endif 829#endif
706 { 830 {
707 cctx->ssize = coro_stacksize * (long)sizeof (long); 831 cctx->ssize = coro_stacksize * (long)sizeof (long);
734#if CORO_USE_VALGRIND 858#if CORO_USE_VALGRIND
735 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 859 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
736#endif 860#endif
737 861
738#if HAVE_MMAP 862#if HAVE_MMAP
739 if (cctx->mapped) 863 if (cctx->flags & CC_MAPPED)
740 munmap (cctx->sptr, cctx->ssize); 864 munmap (cctx->sptr, cctx->ssize);
741 else 865 else
742#endif 866#endif
743 Safefree (cctx->sptr); 867 Safefree (cctx->sptr);
744 868
752 { 876 {
753 coro_cctx *cctx = cctx_first; 877 coro_cctx *cctx = cctx_first;
754 cctx_first = cctx->next; 878 cctx_first = cctx->next;
755 --cctx_idle; 879 --cctx_idle;
756 880
757 if (cctx->ssize >= coro_stacksize) 881 if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE))
758 return cctx; 882 return cctx;
759 883
760 cctx_destroy (cctx); 884 cctx_destroy (cctx);
761 } 885 }
762 886
763 PL_op = PL_op->op_next;
764 return cctx_new (); 887 return cctx_new ();
765} 888}
766 889
767static void 890static void
768cctx_put (coro_cctx *cctx) 891cctx_put (coro_cctx *cctx)
772 { 895 {
773 coro_cctx *first = cctx_first; 896 coro_cctx *first = cctx_first;
774 cctx_first = first->next; 897 cctx_first = first->next;
775 --cctx_idle; 898 --cctx_idle;
776 899
777 assert (!first->inuse);
778 cctx_destroy (first); 900 cctx_destroy (first);
779 } 901 }
780 902
781 ++cctx_idle; 903 ++cctx_idle;
782 cctx->next = cctx_first; 904 cctx->next = cctx_first;
803 925
804 if (prev->flags & CF_NEW) 926 if (prev->flags & CF_NEW)
805 { 927 {
806 /* create a new empty context */ 928 /* create a new empty context */
807 Newz (0, prev->cctx, 1, coro_cctx); 929 Newz (0, prev->cctx, 1, coro_cctx);
808 prev->cctx->inuse = 1;
809 prev->flags &= ~CF_NEW; 930 prev->flags &= ~CF_NEW;
810 prev->flags |= CF_RUNNING; 931 prev->flags |= CF_RUNNING;
811 } 932 }
812 933
813 /*TODO: must not croak here */ 934 /*TODO: must not croak here */
831 next->flags &= ~CF_NEW; 952 next->flags &= ~CF_NEW;
832 /* first get rid of the old state */ 953 /* first get rid of the old state */
833 save_perl (aTHX_ prev); 954 save_perl (aTHX_ prev);
834 /* setup coroutine call */ 955 /* setup coroutine call */
835 setup_coro (aTHX_ next); 956 setup_coro (aTHX_ next);
836 /* need a new stack */
837 assert (!next->cctx);
838 } 957 }
839 else 958 else
840 { 959 {
841 /* coroutine already started */ 960 /* coroutine already started */
842 save_perl (aTHX_ prev); 961 save_perl (aTHX_ prev);
844 } 963 }
845 964
846 prev__cctx = prev->cctx; 965 prev__cctx = prev->cctx;
847 966
848 /* possibly "free" the cctx */ 967 /* possibly "free" the cctx */
849 if (prev__cctx->idle_sp == STACKLEVEL) 968 if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))
850 { 969 {
851 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 970 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
852 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));
853 972
854 prev->cctx = 0; 973 prev->cctx = 0;
855 974
856 cctx_put (prev__cctx); 975 cctx_put (prev__cctx);
857 prev__cctx->inuse = 0;
858 } 976 }
859 977
860 if (!next->cctx) 978 if (!next->cctx)
861 {
862 next->cctx = cctx_get (aTHX); 979 next->cctx = cctx_get (aTHX);
863 assert (!next->cctx->inuse);
864 next->cctx->inuse = 1;
865 }
866 980
867 if (prev__cctx != next->cctx) 981 if (prev__cctx != next->cctx)
868 { 982 {
869 prev__cctx->top_env = PL_top_env; 983 prev__cctx->top_env = PL_top_env;
870 PL_top_env = next->cctx->top_env; 984 PL_top_env = next->cctx->top_env;
1214#endif 1328#endif
1215 BOOT_PAGESIZE; 1329 BOOT_PAGESIZE;
1216 1330
1217 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1331 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1218 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
1219 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1338 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1220 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1339 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1221 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1340 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1222 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); 1341 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1223 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); 1342 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH));
1374 if (coro->hv) 1493 if (coro->hv)
1375 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); 1494 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1376} 1495}
1377 1496
1378void 1497void
1379_eval (Coro::State coro, SV *coderef) 1498call (Coro::State coro, SV *coderef)
1499 ALIAS:
1500 eval = 1
1380 CODE: 1501 CODE:
1381{ 1502{
1382 if (coro->mainstack) 1503 if (coro->mainstack)
1383 { 1504 {
1384 struct coro temp; 1505 struct coro temp;
1395 dSP; 1516 dSP;
1396 ENTER; 1517 ENTER;
1397 SAVETMPS; 1518 SAVETMPS;
1398 PUSHMARK (SP); 1519 PUSHMARK (SP);
1399 PUTBACK; 1520 PUTBACK;
1521 if (ix)
1522 eval_sv (coderef, 0);
1523 else
1400 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1524 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1401 SPAGAIN; 1525 SPAGAIN;
1402 FREETMPS; 1526 FREETMPS;
1403 LEAVE; 1527 LEAVE;
1404 PUTBACK; 1528 PUTBACK;
1405 } 1529 }
1423 CODE: 1547 CODE:
1424 RETVAL = boolSV (coro->flags & ix); 1548 RETVAL = boolSV (coro->flags & ix);
1425 OUTPUT: 1549 OUTPUT:
1426 RETVAL 1550 RETVAL
1427 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 if (coro->flags & CF_RUNNING)
1571 PL_runops = RUNOPS_DEFAULT;
1572 else
1573 coro->runops = RUNOPS_DEFAULT;
1574 }
1575
1576SV *
1577has_stack (Coro::State coro)
1578 PROTOTYPE: $
1579 CODE:
1580 RETVAL = boolSV (!!coro->cctx);
1581 OUTPUT:
1582 RETVAL
1583
1584int
1585is_traced (Coro::State coro)
1586 PROTOTYPE: $
1587 CODE:
1588 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1589 OUTPUT:
1590 RETVAL
1591
1428IV 1592IV
1429rss (Coro::State coro) 1593rss (Coro::State coro)
1430 PROTOTYPE: $ 1594 PROTOTYPE: $
1431 CODE: 1595 CODE:
1432 RETVAL = coro_rss (coro); 1596 RETVAL = coro_rss (coro);
1438 1602
1439BOOT: 1603BOOT:
1440{ 1604{
1441 int i; 1605 int i;
1442 1606
1607 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE);
1608 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE);
1609 av_async_pool = get_av ("Coro::async_pool", TRUE);
1610
1611 coro_current = get_sv ("Coro::current", FALSE);
1612 SvREADONLY_on (coro_current);
1613
1443 coro_stash = gv_stashpv ("Coro", TRUE); 1614 coro_stash = gv_stashpv ("Coro", TRUE);
1444 1615
1445 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1616 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1446 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1617 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1447 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1618 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1448 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1619 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1449 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1620 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1450 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1621 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1451
1452 coro_current = get_sv ("Coro::current", FALSE);
1453 SvREADONLY_on (coro_current);
1454 1622
1455 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1623 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1456 coro_ready[i] = newAV (); 1624 coro_ready[i] = newAV ();
1457 1625
1458 { 1626 {
1516 CODE: 1684 CODE:
1517 RETVAL = coro_nready; 1685 RETVAL = coro_nready;
1518 OUTPUT: 1686 OUTPUT:
1519 RETVAL 1687 RETVAL
1520 1688
1689# for async_pool speedup
1690void
1691_pool_1 (SV *cb)
1692 CODE:
1693{
1694 int i, len;
1695 HV *hv = (HV *)SvRV (coro_current);
1696 AV *defav = GvAV (PL_defgv);
1697 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1698 AV *invoke_av;
1699
1700 if (!invoke)
1701 croak ("\3terminate\2\n");
1702
1703 hv_store (hv, "desc", sizeof ("desc") - 1,
1704 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1705
1706 invoke_av = (AV *)SvRV (invoke);
1707 len = av_len (invoke_av);
1708
1709 sv_setsv (cb, AvARRAY (invoke_av)[0]);
1710
1711 if (len > 0)
1712 {
1713 av_fill (defav, len - 1);
1714 for (i = 0; i < len; ++i)
1715 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1]));
1716 }
1717
1718 SvREFCNT_dec (invoke);
1719}
1720
1721void
1722_pool_2 (SV *cb)
1723 CODE:
1724{
1725 struct coro *coro = SvSTATE (coro_current);
1726
1727 sv_setsv (cb, &PL_sv_undef);
1728
1729 if (coro_rss (coro) > SvIV (sv_pool_rss)
1730 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1731 croak ("\3terminate\2\n");
1732
1733 av_clear (GvAV (PL_defgv));
1734 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1735 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1736 coro->save = CORO_SAVE_DEF;
1737 coro->prio = 0;
1738 av_push (av_async_pool, newSVsv (coro_current));
1739}
1740
1741
1521MODULE = Coro::State PACKAGE = Coro::AIO 1742MODULE = Coro::State PACKAGE = Coro::AIO
1522 1743
1523SV * 1744SV *
1524_get_state () 1745_get_state ()
1525 CODE: 1746 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines