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