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.166 by root, Wed Sep 26 21:11:28 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_TRACE = 0x02,
148 CC_NOREUSE = 0x04, /* throw this away after tracing */
149};
139 150
140/* this is a structure representing a c-level coroutine */ 151/* this is a structure representing a c-level coroutine */
141typedef struct coro_cctx { 152typedef struct coro_cctx {
142 struct coro_cctx *next; 153 struct coro_cctx *next;
143 154
152 coro_context cctx; 163 coro_context cctx;
153 164
154#if CORO_USE_VALGRIND 165#if CORO_USE_VALGRIND
155 int valgrind_id; 166 int valgrind_id;
156#endif 167#endif
157 char inuse, mapped; 168 unsigned char flags;
158} coro_cctx; 169} coro_cctx;
159 170
160enum { 171enum {
161 CF_RUNNING = 0x0001, /* coroutine is running */ 172 CF_RUNNING = 0x0001, /* coroutine is running */
162 CF_READY = 0x0002, /* coroutine is ready */ 173 CF_READY = 0x0002, /* coroutine is ready */
619 SvREFCNT_dec (coro_mortal); 630 SvREFCNT_dec (coro_mortal);
620 coro_mortal = 0; 631 coro_mortal = 0;
621 } 632 }
622} 633}
623 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
624/* 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 */
625/* _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 */
626/* during execution of a perl program */ 678/* during execution of a perl program */
627static void NOINLINE 679static void NOINLINE
628prepare_cctx (pTHX_ coro_cctx *cctx) 680prepare_cctx (pTHX_ coro_cctx *cctx)
629{ 681{
630 dSP; 682 dSP;
631 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;
632 689
633 Zero (&myop, 1, LOGOP); 690 Zero (&myop, 1, LOGOP);
634 myop.op_next = PL_op; 691 myop.op_next = PL_op;
635 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 692 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
636 693
653 dTHX; 710 dTHX;
654 711
655 /* coro_run is the alternative tail of transfer(), so unlock here. */ 712 /* coro_run is the alternative tail of transfer(), so unlock here. */
656 UNLOCK; 713 UNLOCK;
657 714
658 PL_top_env = &PL_start_env; 715 /* we now skip the entersub that lead to transfer() */
716 PL_op = PL_op->op_next;
659 717
660 /* inject a fake subroutine call to cctx_init */ 718 /* inject a fake subroutine call to cctx_init */
661 prepare_cctx (aTHX_ (coro_cctx *)arg); 719 prepare_cctx (aTHX_ (coro_cctx *)arg);
662 720
663 /* 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 */
697# if CORO_STACKGUARD 755# if CORO_STACKGUARD
698 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 756 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
699# endif 757# endif
700 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 758 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
701 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 759 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
702 cctx->mapped = 1; 760 cctx->flags |= CC_MAPPED;
703 } 761 }
704 else 762 else
705#endif 763#endif
706 { 764 {
707 cctx->ssize = coro_stacksize * (long)sizeof (long); 765 cctx->ssize = coro_stacksize * (long)sizeof (long);
734#if CORO_USE_VALGRIND 792#if CORO_USE_VALGRIND
735 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 793 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
736#endif 794#endif
737 795
738#if HAVE_MMAP 796#if HAVE_MMAP
739 if (cctx->mapped) 797 if (cctx->flags & CC_MAPPED)
740 munmap (cctx->sptr, cctx->ssize); 798 munmap (cctx->sptr, cctx->ssize);
741 else 799 else
742#endif 800#endif
743 Safefree (cctx->sptr); 801 Safefree (cctx->sptr);
744 802
752 { 810 {
753 coro_cctx *cctx = cctx_first; 811 coro_cctx *cctx = cctx_first;
754 cctx_first = cctx->next; 812 cctx_first = cctx->next;
755 --cctx_idle; 813 --cctx_idle;
756 814
757 if (cctx->ssize >= coro_stacksize) 815 if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE))
758 return cctx; 816 return cctx;
759 817
760 cctx_destroy (cctx); 818 cctx_destroy (cctx);
761 } 819 }
762 820
763 PL_op = PL_op->op_next;
764 return cctx_new (); 821 return cctx_new ();
765} 822}
766 823
767static void 824static void
768cctx_put (coro_cctx *cctx) 825cctx_put (coro_cctx *cctx)
772 { 829 {
773 coro_cctx *first = cctx_first; 830 coro_cctx *first = cctx_first;
774 cctx_first = first->next; 831 cctx_first = first->next;
775 --cctx_idle; 832 --cctx_idle;
776 833
777 assert (!first->inuse);
778 cctx_destroy (first); 834 cctx_destroy (first);
779 } 835 }
780 836
781 ++cctx_idle; 837 ++cctx_idle;
782 cctx->next = cctx_first; 838 cctx->next = cctx_first;
803 859
804 if (prev->flags & CF_NEW) 860 if (prev->flags & CF_NEW)
805 { 861 {
806 /* create a new empty context */ 862 /* create a new empty context */
807 Newz (0, prev->cctx, 1, coro_cctx); 863 Newz (0, prev->cctx, 1, coro_cctx);
808 prev->cctx->inuse = 1;
809 prev->flags &= ~CF_NEW; 864 prev->flags &= ~CF_NEW;
810 prev->flags |= CF_RUNNING; 865 prev->flags |= CF_RUNNING;
811 } 866 }
812 867
813 /*TODO: must not croak here */ 868 /*TODO: must not croak here */
831 next->flags &= ~CF_NEW; 886 next->flags &= ~CF_NEW;
832 /* first get rid of the old state */ 887 /* first get rid of the old state */
833 save_perl (aTHX_ prev); 888 save_perl (aTHX_ prev);
834 /* setup coroutine call */ 889 /* setup coroutine call */
835 setup_coro (aTHX_ next); 890 setup_coro (aTHX_ next);
836 /* need a new stack */
837 assert (!next->cctx);
838 } 891 }
839 else 892 else
840 { 893 {
841 /* coroutine already started */ 894 /* coroutine already started */
842 save_perl (aTHX_ prev); 895 save_perl (aTHX_ prev);
852 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));
853 906
854 prev->cctx = 0; 907 prev->cctx = 0;
855 908
856 cctx_put (prev__cctx); 909 cctx_put (prev__cctx);
857 prev__cctx->inuse = 0;
858 } 910 }
859 911
860 if (!next->cctx) 912 if (!next->cctx)
861 {
862 next->cctx = cctx_get (aTHX); 913 next->cctx = cctx_get (aTHX);
863 assert (!next->cctx->inuse);
864 next->cctx->inuse = 1;
865 }
866 914
867 if (prev__cctx != next->cctx) 915 if (prev__cctx != next->cctx)
868 { 916 {
869 prev__cctx->top_env = PL_top_env; 917 prev__cctx->top_env = PL_top_env;
870 PL_top_env = next->cctx->top_env; 918 PL_top_env = next->cctx->top_env;
1374 if (coro->hv) 1422 if (coro->hv)
1375 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); 1423 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1376} 1424}
1377 1425
1378void 1426void
1379_eval (Coro::State coro, SV *coderef) 1427call (Coro::State coro, SV *coderef)
1428 ALIAS:
1429 eval = 1
1380 CODE: 1430 CODE:
1381{ 1431{
1382 if (coro->mainstack) 1432 if (coro->mainstack)
1383 { 1433 {
1384 struct coro temp; 1434 struct coro temp;
1395 dSP; 1445 dSP;
1396 ENTER; 1446 ENTER;
1397 SAVETMPS; 1447 SAVETMPS;
1398 PUSHMARK (SP); 1448 PUSHMARK (SP);
1399 PUTBACK; 1449 PUTBACK;
1450 if (ix)
1451 eval_sv (coderef, 0);
1452 else
1400 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1453 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1401 SPAGAIN; 1454 SPAGAIN;
1402 FREETMPS; 1455 FREETMPS;
1403 LEAVE; 1456 LEAVE;
1404 PUTBACK; 1457 PUTBACK;
1405 } 1458 }
1423 CODE: 1476 CODE:
1424 RETVAL = boolSV (coro->flags & ix); 1477 RETVAL = boolSV (coro->flags & ix);
1425 OUTPUT: 1478 OUTPUT:
1426 RETVAL 1479 RETVAL
1427 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
1428IV 1513IV
1429rss (Coro::State coro) 1514rss (Coro::State coro)
1430 PROTOTYPE: $ 1515 PROTOTYPE: $
1431 CODE: 1516 CODE:
1432 RETVAL = coro_rss (coro); 1517 RETVAL = coro_rss (coro);
1438 1523
1439BOOT: 1524BOOT:
1440{ 1525{
1441 int i; 1526 int i;
1442 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
1443 coro_stash = gv_stashpv ("Coro", TRUE); 1535 coro_stash = gv_stashpv ("Coro", TRUE);
1444 1536
1445 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1537 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1446 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1538 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1447 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1539 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1448 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1540 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1449 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1541 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1450 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1542 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1451
1452 coro_current = get_sv ("Coro::current", FALSE);
1453 SvREADONLY_on (coro_current);
1454 1543
1455 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1544 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1456 coro_ready[i] = newAV (); 1545 coro_ready[i] = newAV ();
1457 1546
1458 { 1547 {
1516 CODE: 1605 CODE:
1517 RETVAL = coro_nready; 1606 RETVAL = coro_nready;
1518 OUTPUT: 1607 OUTPUT:
1519 RETVAL 1608 RETVAL
1520 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
1521MODULE = Coro::State PACKAGE = Coro::AIO 1663MODULE = Coro::State PACKAGE = Coro::AIO
1522 1664
1523SV * 1665SV *
1524_get_state () 1666_get_state ()
1525 CODE: 1667 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines