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.164 by root, Tue Sep 25 01:47:23 2007 UTC vs.
Revision 1.165 by root, Wed Sep 26 19:26:48 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_TRACE = 0x02,
148 CC_NOREUSE = 0x04, /* throw this away after tracing */
149};
150
145/* this is a structure representing a c-level coroutine */ 151/* this is a structure representing a c-level coroutine */
146typedef struct coro_cctx { 152typedef struct coro_cctx {
147 struct coro_cctx *next; 153 struct coro_cctx *next;
148 154
149 /* the stack */ 155 /* the stack */
157 coro_context cctx; 163 coro_context cctx;
158 164
159#if CORO_USE_VALGRIND 165#if CORO_USE_VALGRIND
160 int valgrind_id; 166 int valgrind_id;
161#endif 167#endif
162 char inuse, mapped; 168 unsigned char flags;
163} coro_cctx; 169} coro_cctx;
164 170
165enum { 171enum {
166 CF_RUNNING = 0x0001, /* coroutine is running */ 172 CF_RUNNING = 0x0001, /* coroutine is running */
167 CF_READY = 0x0002, /* coroutine is ready */ 173 CF_READY = 0x0002, /* coroutine is ready */
624 SvREFCNT_dec (coro_mortal); 630 SvREFCNT_dec (coro_mortal);
625 coro_mortal = 0; 631 coro_mortal = 0;
626 } 632 }
627} 633}
628 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
629/* 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 */
630/* _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 */
631/* during execution of a perl program */ 678/* during execution of a perl program */
632static void NOINLINE 679static void NOINLINE
633prepare_cctx (pTHX_ coro_cctx *cctx) 680prepare_cctx (pTHX_ coro_cctx *cctx)
634{ 681{
635 dSP; 682 dSP;
636 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;
637 689
638 Zero (&myop, 1, LOGOP); 690 Zero (&myop, 1, LOGOP);
639 myop.op_next = PL_op; 691 myop.op_next = PL_op;
640 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 692 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
641 693
658 dTHX; 710 dTHX;
659 711
660 /* coro_run is the alternative tail of transfer(), so unlock here. */ 712 /* coro_run is the alternative tail of transfer(), so unlock here. */
661 UNLOCK; 713 UNLOCK;
662 714
663 PL_top_env = &PL_start_env; 715 /* we now skip the entersub that lead to transfer() */
716 PL_op = PL_op->op_next;
664 717
665 /* inject a fake subroutine call to cctx_init */ 718 /* inject a fake subroutine call to cctx_init */
666 prepare_cctx (aTHX_ (coro_cctx *)arg); 719 prepare_cctx (aTHX_ (coro_cctx *)arg);
667 720
668 /* 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 */
702# if CORO_STACKGUARD 755# if CORO_STACKGUARD
703 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 756 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
704# endif 757# endif
705 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 758 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
706 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 759 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
707 cctx->mapped = 1; 760 cctx->flags |= CC_MAPPED;
708 } 761 }
709 else 762 else
710#endif 763#endif
711 { 764 {
712 cctx->ssize = coro_stacksize * (long)sizeof (long); 765 cctx->ssize = coro_stacksize * (long)sizeof (long);
739#if CORO_USE_VALGRIND 792#if CORO_USE_VALGRIND
740 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 793 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
741#endif 794#endif
742 795
743#if HAVE_MMAP 796#if HAVE_MMAP
744 if (cctx->mapped) 797 if (cctx->flags & CC_MAPPED)
745 munmap (cctx->sptr, cctx->ssize); 798 munmap (cctx->sptr, cctx->ssize);
746 else 799 else
747#endif 800#endif
748 Safefree (cctx->sptr); 801 Safefree (cctx->sptr);
749 802
757 { 810 {
758 coro_cctx *cctx = cctx_first; 811 coro_cctx *cctx = cctx_first;
759 cctx_first = cctx->next; 812 cctx_first = cctx->next;
760 --cctx_idle; 813 --cctx_idle;
761 814
762 if (cctx->ssize >= coro_stacksize) 815 if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE))
763 return cctx; 816 return cctx;
764 817
765 cctx_destroy (cctx); 818 cctx_destroy (cctx);
766 } 819 }
767 820
768 PL_op = PL_op->op_next;
769 return cctx_new (); 821 return cctx_new ();
770} 822}
771 823
772static void 824static void
773cctx_put (coro_cctx *cctx) 825cctx_put (coro_cctx *cctx)
777 { 829 {
778 coro_cctx *first = cctx_first; 830 coro_cctx *first = cctx_first;
779 cctx_first = first->next; 831 cctx_first = first->next;
780 --cctx_idle; 832 --cctx_idle;
781 833
782 assert (!first->inuse);
783 cctx_destroy (first); 834 cctx_destroy (first);
784 } 835 }
785 836
786 ++cctx_idle; 837 ++cctx_idle;
787 cctx->next = cctx_first; 838 cctx->next = cctx_first;
808 859
809 if (prev->flags & CF_NEW) 860 if (prev->flags & CF_NEW)
810 { 861 {
811 /* create a new empty context */ 862 /* create a new empty context */
812 Newz (0, prev->cctx, 1, coro_cctx); 863 Newz (0, prev->cctx, 1, coro_cctx);
813 prev->cctx->inuse = 1;
814 prev->flags &= ~CF_NEW; 864 prev->flags &= ~CF_NEW;
815 prev->flags |= CF_RUNNING; 865 prev->flags |= CF_RUNNING;
816 } 866 }
817 867
818 /*TODO: must not croak here */ 868 /*TODO: must not croak here */
836 next->flags &= ~CF_NEW; 886 next->flags &= ~CF_NEW;
837 /* first get rid of the old state */ 887 /* first get rid of the old state */
838 save_perl (aTHX_ prev); 888 save_perl (aTHX_ prev);
839 /* setup coroutine call */ 889 /* setup coroutine call */
840 setup_coro (aTHX_ next); 890 setup_coro (aTHX_ next);
841 /* need a new stack */
842 assert (!next->cctx);
843 } 891 }
844 else 892 else
845 { 893 {
846 /* coroutine already started */ 894 /* coroutine already started */
847 save_perl (aTHX_ prev); 895 save_perl (aTHX_ prev);
857 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));
858 906
859 prev->cctx = 0; 907 prev->cctx = 0;
860 908
861 cctx_put (prev__cctx); 909 cctx_put (prev__cctx);
862 prev__cctx->inuse = 0;
863 } 910 }
864 911
865 if (!next->cctx) 912 if (!next->cctx)
866 {
867 next->cctx = cctx_get (aTHX); 913 next->cctx = cctx_get (aTHX);
868 assert (!next->cctx->inuse);
869 next->cctx->inuse = 1;
870 }
871 914
872 if (prev__cctx != next->cctx) 915 if (prev__cctx != next->cctx)
873 { 916 {
874 prev__cctx->top_env = PL_top_env; 917 prev__cctx->top_env = PL_top_env;
875 PL_top_env = next->cctx->top_env; 918 PL_top_env = next->cctx->top_env;
1433 CODE: 1476 CODE:
1434 RETVAL = boolSV (coro->flags & ix); 1477 RETVAL = boolSV (coro->flags & ix);
1435 OUTPUT: 1478 OUTPUT:
1436 RETVAL 1479 RETVAL
1437 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
1438SV * 1502SV *
1439has_stack (Coro::State coro) 1503has_stack (Coro::State coro)
1440 PROTOTYPE: $ 1504 PROTOTYPE: $
1441 CODE: 1505 CODE:
1442 RETVAL = boolSV (!!coro->cctx); 1506 RETVAL = boolSV (!!coro->cctx);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines