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.156 by root, Fri Sep 21 01:10:55 2007 UTC vs.
Revision 1.165 by root, Wed Sep 26 19:26:48 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 */
395 /* 405 /*
396 * the worst thing you can imagine happens first - we have to save 406 * the worst thing you can imagine happens first - we have to save
397 * (and reinitialize) all cv's in the whole callchain :( 407 * (and reinitialize) all cv's in the whole callchain :(
398 */ 408 */
399 409
400 EXTEND (SP, 3 + 1);
401 PUSHs (Nullsv); 410 XPUSHs (Nullsv);
402 /* this loop was inspired by pp_caller */ 411 /* this loop was inspired by pp_caller */
403 for (;;) 412 for (;;)
404 { 413 {
405 while (cxix >= 0) 414 while (cxix >= 0)
406 { 415 {
425 434
426 if (top_si->si_type == PERLSI_MAIN) 435 if (top_si->si_type == PERLSI_MAIN)
427 break; 436 break;
428 437
429 top_si = top_si->si_prev; 438 top_si = top_si->si_prev;
430 ccstk = top_si->si_cxstack; 439 ccstk = top_si->si_cxstack;
431 cxix = top_si->si_cxix; 440 cxix = top_si->si_cxix;
432 } 441 }
433 442
434 PUTBACK; 443 PUTBACK;
435 } 444 }
436 445
621 SvREFCNT_dec (coro_mortal); 630 SvREFCNT_dec (coro_mortal);
622 coro_mortal = 0; 631 coro_mortal = 0;
623 } 632 }
624} 633}
625 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
626/* 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 */
627/* _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 */
628/* during execution of a perl program */ 678/* during execution of a perl program */
629static void NOINLINE 679static void NOINLINE
630prepare_cctx (pTHX_ coro_cctx *cctx) 680prepare_cctx (pTHX_ coro_cctx *cctx)
631{ 681{
632 dSP; 682 dSP;
633 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;
634 689
635 Zero (&myop, 1, LOGOP); 690 Zero (&myop, 1, LOGOP);
636 myop.op_next = PL_op; 691 myop.op_next = PL_op;
637 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 692 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
638 693
655 dTHX; 710 dTHX;
656 711
657 /* coro_run is the alternative tail of transfer(), so unlock here. */ 712 /* coro_run is the alternative tail of transfer(), so unlock here. */
658 UNLOCK; 713 UNLOCK;
659 714
660 PL_top_env = &PL_start_env; 715 /* we now skip the entersub that lead to transfer() */
716 PL_op = PL_op->op_next;
661 717
662 /* inject a fake subroutine call to cctx_init */ 718 /* inject a fake subroutine call to cctx_init */
663 prepare_cctx (aTHX_ (coro_cctx *)arg); 719 prepare_cctx (aTHX_ (coro_cctx *)arg);
664 720
665 /* 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 */
699# if CORO_STACKGUARD 755# if CORO_STACKGUARD
700 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 756 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
701# endif 757# endif
702 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 758 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
703 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 759 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
704 cctx->mapped = 1; 760 cctx->flags |= CC_MAPPED;
705 } 761 }
706 else 762 else
707#endif 763#endif
708 { 764 {
709 cctx->ssize = coro_stacksize * (long)sizeof (long); 765 cctx->ssize = coro_stacksize * (long)sizeof (long);
736#if CORO_USE_VALGRIND 792#if CORO_USE_VALGRIND
737 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 793 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
738#endif 794#endif
739 795
740#if HAVE_MMAP 796#if HAVE_MMAP
741 if (cctx->mapped) 797 if (cctx->flags & CC_MAPPED)
742 munmap (cctx->sptr, cctx->ssize); 798 munmap (cctx->sptr, cctx->ssize);
743 else 799 else
744#endif 800#endif
745 Safefree (cctx->sptr); 801 Safefree (cctx->sptr);
746 802
754 { 810 {
755 coro_cctx *cctx = cctx_first; 811 coro_cctx *cctx = cctx_first;
756 cctx_first = cctx->next; 812 cctx_first = cctx->next;
757 --cctx_idle; 813 --cctx_idle;
758 814
759 if (cctx->ssize >= coro_stacksize) 815 if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE))
760 return cctx; 816 return cctx;
761 817
762 cctx_destroy (cctx); 818 cctx_destroy (cctx);
763 } 819 }
764 820
765 PL_op = PL_op->op_next;
766 return cctx_new (); 821 return cctx_new ();
767} 822}
768 823
769static void 824static void
770cctx_put (coro_cctx *cctx) 825cctx_put (coro_cctx *cctx)
774 { 829 {
775 coro_cctx *first = cctx_first; 830 coro_cctx *first = cctx_first;
776 cctx_first = first->next; 831 cctx_first = first->next;
777 --cctx_idle; 832 --cctx_idle;
778 833
779 assert (!first->inuse);
780 cctx_destroy (first); 834 cctx_destroy (first);
781 } 835 }
782 836
783 ++cctx_idle; 837 ++cctx_idle;
784 cctx->next = cctx_first; 838 cctx->next = cctx_first;
805 859
806 if (prev->flags & CF_NEW) 860 if (prev->flags & CF_NEW)
807 { 861 {
808 /* create a new empty context */ 862 /* create a new empty context */
809 Newz (0, prev->cctx, 1, coro_cctx); 863 Newz (0, prev->cctx, 1, coro_cctx);
810 prev->cctx->inuse = 1;
811 prev->flags &= ~CF_NEW; 864 prev->flags &= ~CF_NEW;
812 prev->flags |= CF_RUNNING; 865 prev->flags |= CF_RUNNING;
813 } 866 }
814 867
815 /*TODO: must not croak here */ 868 /*TODO: must not croak here */
833 next->flags &= ~CF_NEW; 886 next->flags &= ~CF_NEW;
834 /* first get rid of the old state */ 887 /* first get rid of the old state */
835 save_perl (aTHX_ prev); 888 save_perl (aTHX_ prev);
836 /* setup coroutine call */ 889 /* setup coroutine call */
837 setup_coro (aTHX_ next); 890 setup_coro (aTHX_ next);
838 /* need a new stack */
839 assert (!next->cctx);
840 } 891 }
841 else 892 else
842 { 893 {
843 /* coroutine already started */ 894 /* coroutine already started */
844 save_perl (aTHX_ prev); 895 save_perl (aTHX_ prev);
854 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));
855 906
856 prev->cctx = 0; 907 prev->cctx = 0;
857 908
858 cctx_put (prev__cctx); 909 cctx_put (prev__cctx);
859 prev__cctx->inuse = 0;
860 } 910 }
861 911
862 if (!next->cctx) 912 if (!next->cctx)
863 {
864 next->cctx = cctx_get (aTHX); 913 next->cctx = cctx_get (aTHX);
865 assert (!next->cctx->inuse);
866 next->cctx->inuse = 1;
867 }
868 914
869 if (prev__cctx != next->cctx) 915 if (prev__cctx != next->cctx)
870 { 916 {
871 prev__cctx->top_env = PL_top_env; 917 prev__cctx->top_env = PL_top_env;
872 PL_top_env = next->cctx->top_env; 918 PL_top_env = next->cctx->top_env;
1376 if (coro->hv) 1422 if (coro->hv)
1377 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); 1423 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1378} 1424}
1379 1425
1380void 1426void
1381_eval (Coro::State coro, SV *coderef) 1427call (Coro::State coro, SV *coderef)
1428 ALIAS:
1429 eval = 1
1382 CODE: 1430 CODE:
1383{ 1431{
1384 if (coro->mainstack) 1432 if (coro->mainstack)
1385 { 1433 {
1386 struct coro temp; 1434 struct coro temp;
1397 dSP; 1445 dSP;
1398 ENTER; 1446 ENTER;
1399 SAVETMPS; 1447 SAVETMPS;
1400 PUSHMARK (SP); 1448 PUSHMARK (SP);
1401 PUTBACK; 1449 PUTBACK;
1450 if (ix)
1451 eval_sv (coderef, 0);
1452 else
1402 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1453 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1403 SPAGAIN; 1454 SPAGAIN;
1404 FREETMPS; 1455 FREETMPS;
1405 LEAVE; 1456 LEAVE;
1406 PUTBACK; 1457 PUTBACK;
1407 } 1458 }
1425 CODE: 1476 CODE:
1426 RETVAL = boolSV (coro->flags & ix); 1477 RETVAL = boolSV (coro->flags & ix);
1427 OUTPUT: 1478 OUTPUT:
1428 RETVAL 1479 RETVAL
1429 1480
1481void
1482trace (Coro::State coro, int enable = 0)
1483 CODE:
1484 if (enable)
1485 {
1486 if (coro->flags & CF_RUNNING)
1487 croak ("cannot enable tracing on running coroutine");
1488
1489 if (coro->cctx)
1490 croak ("cannot enable tracing on coroutine with custom stack");
1491
1492 coro->cctx = cctx_new ();
1493 coro->cctx->flags |= CC_TRACE;
1494 }
1495 else
1496 if (coro->cctx && coro->cctx->flags & CC_TRACE)
1497 {
1498 coro->cctx->flags &= ~CC_TRACE;
1499 coro->cctx->flags |= CC_NOREUSE;
1500 }
1501
1502SV *
1503has_stack (Coro::State coro)
1504 PROTOTYPE: $
1505 CODE:
1506 RETVAL = boolSV (!!coro->cctx);
1507 OUTPUT:
1508 RETVAL
1509
1430IV 1510IV
1431rss (Coro::State coro) 1511rss (Coro::State coro)
1432 PROTOTYPE: $ 1512 PROTOTYPE: $
1433 CODE: 1513 CODE:
1434 RETVAL = coro_rss (coro); 1514 RETVAL = coro_rss (coro);
1440 1520
1441BOOT: 1521BOOT:
1442{ 1522{
1443 int i; 1523 int i;
1444 1524
1525 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE);
1526 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE);
1527 av_async_pool = get_av ("Coro::async_pool", TRUE);
1528
1529 coro_current = get_sv ("Coro::current", FALSE);
1530 SvREADONLY_on (coro_current);
1531
1445 coro_stash = gv_stashpv ("Coro", TRUE); 1532 coro_stash = gv_stashpv ("Coro", TRUE);
1446 1533
1447 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1534 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1448 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1535 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1449 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1536 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1450 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1537 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1451 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1538 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1452 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1539 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1453
1454 coro_current = get_sv ("Coro::current", FALSE);
1455 SvREADONLY_on (coro_current);
1456 1540
1457 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1541 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1458 coro_ready[i] = newAV (); 1542 coro_ready[i] = newAV ();
1459 1543
1460 { 1544 {
1518 CODE: 1602 CODE:
1519 RETVAL = coro_nready; 1603 RETVAL = coro_nready;
1520 OUTPUT: 1604 OUTPUT:
1521 RETVAL 1605 RETVAL
1522 1606
1607# for async_pool speedup
1608void
1609_pool_1 (SV *cb)
1610 CODE:
1611{
1612 int i, len;
1613 HV *hv = (HV *)SvRV (coro_current);
1614 AV *defav = GvAV (PL_defgv);
1615 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1616 AV *invoke_av;
1617
1618 if (!invoke)
1619 croak ("\3terminate\2\n");
1620
1621 hv_store (hv, "desc", sizeof ("desc") - 1,
1622 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1623
1624 invoke_av = (AV *)SvRV (invoke);
1625 len = av_len (invoke_av);
1626
1627 sv_setsv (cb, AvARRAY (invoke_av)[0]);
1628
1629 if (len > 0)
1630 {
1631 av_fill (defav, len - 1);
1632 for (i = 0; i < len; ++i)
1633 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1]));
1634 }
1635
1636 SvREFCNT_dec (invoke);
1637}
1638
1639void
1640_pool_2 (SV *cb)
1641 CODE:
1642{
1643 struct coro *coro = SvSTATE (coro_current);
1644
1645 sv_setsv (cb, &PL_sv_undef);
1646
1647 if (coro_rss (coro) > SvIV (sv_pool_rss)
1648 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1649 croak ("\3terminate\2\n");
1650
1651 av_clear (GvAV (PL_defgv));
1652 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1653 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1654 coro->save = CORO_SAVE_DEF;
1655 coro->prio = 0;
1656 av_push (av_async_pool, newSVsv (coro_current));
1657}
1658
1659
1523MODULE = Coro::State PACKAGE = Coro::AIO 1660MODULE = Coro::State PACKAGE = Coro::AIO
1524 1661
1525SV * 1662SV *
1526_get_state () 1663_get_state ()
1527 CODE: 1664 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines