… | |
… | |
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 | { |
|
|
720 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
718 | assert (PL_top_env == prev__cctx->top_env);//D |
721 | assert (PL_top_env == prev__cctx->top_env); |
|
|
722 | |
719 | cctx_put (prev__cctx); |
723 | cctx_put (prev__cctx); |
720 | prev->cctx = 0; |
724 | prev->cctx = 0; |
721 | } |
725 | } |
722 | |
726 | |
723 | if (!next->cctx) |
727 | if (!next->cctx) |
… | |
… | |
753 | static void |
757 | static void |
754 | coro_state_destroy (struct coro *coro) |
758 | coro_state_destroy (struct coro *coro) |
755 | { |
759 | { |
756 | if (coro->refcnt--) |
760 | if (coro->refcnt--) |
757 | return; |
761 | return; |
|
|
762 | |
|
|
763 | if (coro->flags & CF_RUNNING) |
|
|
764 | croak ("FATAL: tried to destroy currently running coroutine"); |
758 | |
765 | |
759 | if (coro->mainstack && coro->mainstack != main_mainstack) |
766 | if (coro->mainstack && coro->mainstack != main_mainstack) |
760 | { |
767 | { |
761 | struct coro temp; |
768 | struct coro temp; |
762 | |
769 | |
… | |
… | |
860 | static GV *coro_current, *coro_idle; |
867 | static GV *coro_current, *coro_idle; |
861 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
868 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
862 | static int coro_nready; |
869 | static int coro_nready; |
863 | |
870 | |
864 | static void |
871 | static void |
865 | coro_enq (SV *sv) |
872 | coro_enq (SV *coro_sv) |
866 | { |
873 | { |
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); |
874 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
875 | coro_nready++; |
875 | coro_nready++; |
876 | } |
876 | } |
877 | |
877 | |
878 | static SV * |
878 | static SV * |
879 | coro_deq (int min_prio) |
879 | coro_deq (int min_prio) |
… | |
… | |
901 | |
901 | |
902 | if (SvROK (coro_sv)) |
902 | if (SvROK (coro_sv)) |
903 | coro_sv = SvRV (coro_sv); |
903 | coro_sv = SvRV (coro_sv); |
904 | |
904 | |
905 | coro = SvSTATE (coro_sv); |
905 | coro = SvSTATE (coro_sv); |
|
|
906 | |
906 | if (coro->flags & CF_READY) |
907 | if (coro->flags & CF_READY) |
907 | return 0; |
908 | return 0; |
|
|
909 | |
|
|
910 | if (coro->flags & CF_RUNNING) |
|
|
911 | croak ("Coro::ready called on currently running coroutine"); |
908 | |
912 | |
909 | coro->flags |= CF_READY; |
913 | coro->flags |= CF_READY; |
910 | |
914 | |
911 | LOCK; |
915 | LOCK; |
912 | coro_enq (SvREFCNT_inc (coro_sv)); |
916 | coro_enq (SvREFCNT_inc (coro_sv)); |
… | |
… | |
916 | } |
920 | } |
917 | |
921 | |
918 | static int |
922 | static int |
919 | api_is_ready (SV *coro_sv) |
923 | api_is_ready (SV *coro_sv) |
920 | { |
924 | { |
921 | struct coro *coro; |
|
|
922 | |
|
|
923 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
925 | return !!SvSTATE (coro_sv)->flags & CF_READY; |
924 | } |
926 | } |
925 | |
927 | |
926 | static void |
928 | static void |
927 | prepare_schedule (struct transfer_args *ta) |
929 | prepare_schedule (struct transfer_args *ta) |
928 | { |
930 | { |