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.111 by root, Thu Nov 30 18:21:14 2006 UTC vs.
Revision 1.113 by root, Thu Nov 30 20:54:24 2006 UTC

682 Newz (0, prev->cctx, 1, coro_cctx); 682 Newz (0, prev->cctx, 1, coro_cctx);
683 prev->cctx->inuse = 1; 683 prev->cctx->inuse = 1;
684 prev->flags |= CF_RUNNING; 684 prev->flags |= CF_RUNNING;
685 } 685 }
686 686
687 assert ( prev->flags & CF_RUNNING); 687 if (!prev->flags & CF_RUNNING)
688 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
689
688 assert (!next->flags & CF_RUNNING); 690 if (next->flags & CF_RUNNING)
691 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
689 692
690 prev->flags &= ~CF_RUNNING; 693 prev->flags &= ~CF_RUNNING;
691 next->flags |= CF_RUNNING; 694 next->flags |= CF_RUNNING;
692 695
693 LOCK; 696 LOCK;
703 /* need to start coroutine */ 706 /* need to start coroutine */
704 /* first get rid of the old state */ 707 /* first get rid of the old state */
705 SAVE (prev, -1); 708 SAVE (prev, -1);
706 /* setup coroutine call */ 709 /* setup coroutine call */
707 setup_coro (next); 710 setup_coro (next);
708 /* need a stack */ 711 /* need a new stack */
709 assert (!next->stack); 712 assert (!next->stack);
710 next->cctx = 0;
711 } 713 }
712 714
713 prev__cctx = prev->cctx; 715 prev__cctx = prev->cctx;
714 716
715 /* possibly "free" the cctx */ 717 /* possibly "free" the cctx */
716 if (prev__cctx->idle_sp == STACKLEVEL) 718 if (prev__cctx->idle_sp == STACKLEVEL)
717 { 719 {
718 assert (PL_top_env == prev__cctx->top_env);//D 720 assert (PL_top_env == prev__cctx->top_env);
721
719 cctx_put (prev__cctx); 722 cctx_put (prev__cctx);
720 prev->cctx = 0; 723 prev->cctx = 0;
721 } 724 }
725 else if (prev__cctx->idle_sp) //D
726 fprintf (stderr, "coro %p has own stack %p-%p\n", prev__cctx, prev__cctx->idle_sp, STACKLEVEL);//D
722 727
723 if (!next->cctx) 728 if (!next->cctx)
724 next->cctx = cctx_get (); 729 next->cctx = cctx_get ();
725 730
726 if (prev__cctx != next->cctx) 731 if (prev__cctx != next->cctx)
753static void 758static void
754coro_state_destroy (struct coro *coro) 759coro_state_destroy (struct coro *coro)
755{ 760{
756 if (coro->refcnt--) 761 if (coro->refcnt--)
757 return; 762 return;
763
764 if (coro->flags & CF_RUNNING)
765 croak ("FATAL: tried to destroy currently running coroutine");
758 766
759 if (coro->mainstack && coro->mainstack != main_mainstack) 767 if (coro->mainstack && coro->mainstack != main_mainstack)
760 { 768 {
761 struct coro temp; 769 struct coro temp;
762 770
860static GV *coro_current, *coro_idle; 868static GV *coro_current, *coro_idle;
861static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 869static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
862static int coro_nready; 870static int coro_nready;
863 871
864static void 872static void
865coro_enq (SV *sv) 873coro_enq (SV *coro_sv)
866{ 874{
867 int prio;
868
869 if (SvTYPE (sv) != SVt_PVHV)
870 croak ("Coro::ready tried to enqueue something that is not a coroutine");
871
872 prio = SvSTATE (sv)->prio;
873
874 av_push (coro_ready [prio - PRIO_MIN], sv); 875 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
875 coro_nready++; 876 coro_nready++;
876} 877}
877 878
878static SV * 879static SV *
879coro_deq (int min_prio) 880coro_deq (int min_prio)
901 902
902 if (SvROK (coro_sv)) 903 if (SvROK (coro_sv))
903 coro_sv = SvRV (coro_sv); 904 coro_sv = SvRV (coro_sv);
904 905
905 coro = SvSTATE (coro_sv); 906 coro = SvSTATE (coro_sv);
907
906 if (coro->flags & CF_READY) 908 if (coro->flags & CF_READY)
907 return 0; 909 return 0;
910
911 if (coro->flags & CF_RUNNING)
912 croak ("Coro::ready called on currently running coroutine");
908 913
909 coro->flags |= CF_READY; 914 coro->flags |= CF_READY;
910 915
911 LOCK; 916 LOCK;
912 coro_enq (SvREFCNT_inc (coro_sv)); 917 coro_enq (SvREFCNT_inc (coro_sv));
916} 921}
917 922
918static int 923static int
919api_is_ready (SV *coro_sv) 924api_is_ready (SV *coro_sv)
920{ 925{
921 struct coro *coro;
922
923 return !!(SvSTATE (coro_sv)->flags & CF_READY); 926 return !!SvSTATE (coro_sv)->flags & CF_READY;
924} 927}
925 928
926static void 929static void
927prepare_schedule (struct transfer_args *ta) 930prepare_schedule (struct transfer_args *ta)
928{ 931{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines