… | |
… | |
119 | /* cpu state */ |
119 | /* cpu state */ |
120 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
120 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
121 | JMPENV *top_env; |
121 | JMPENV *top_env; |
122 | coro_context cctx; |
122 | coro_context cctx; |
123 | |
123 | |
|
|
124 | int inuse; |
|
|
125 | |
124 | #if USE_VALGRIND |
126 | #if USE_VALGRIND |
125 | int valgrind_id; |
127 | int valgrind_id; |
126 | #endif |
128 | #endif |
127 | } coro_cctx; |
129 | } coro_cctx; |
|
|
130 | |
|
|
131 | enum { |
|
|
132 | CF_RUNNING, /* coroutine is running */ |
|
|
133 | CF_READY, /* coroutine is ready */ |
|
|
134 | }; |
128 | |
135 | |
129 | /* this is a structure representing a perl-level coroutine */ |
136 | /* this is a structure representing a perl-level coroutine */ |
130 | struct coro { |
137 | struct coro { |
131 | /* the c coroutine allocated to this perl coroutine, if any */ |
138 | /* the c coroutine allocated to this perl coroutine, if any */ |
132 | coro_cctx *cctx; |
139 | coro_cctx *cctx; |
133 | |
140 | |
134 | /* data associated with this coroutine (initial args) */ |
141 | /* data associated with this coroutine (initial args) */ |
135 | AV *args; |
142 | AV *args; |
136 | int refcnt; |
143 | int refcnt; |
|
|
144 | int flags; |
137 | |
145 | |
138 | /* optionally saved, might be zero */ |
146 | /* optionally saved, might be zero */ |
139 | AV *defav; |
147 | AV *defav; |
140 | SV *defsv; |
148 | SV *defsv; |
141 | SV *errsv; |
149 | SV *errsv; |
… | |
… | |
665 | if (flags == TRANSFER_SET_STACKLEVEL) |
673 | if (flags == TRANSFER_SET_STACKLEVEL) |
666 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
674 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
667 | else if (prev != next) |
675 | else if (prev != next) |
668 | { |
676 | { |
669 | coro_cctx *prev__cctx; |
677 | coro_cctx *prev__cctx; |
|
|
678 | |
|
|
679 | if (!prev->cctx) |
|
|
680 | { |
|
|
681 | /* create a new empty context */ |
|
|
682 | Newz (0, prev->cctx, 1, coro_cctx); |
|
|
683 | prev->cctx->inuse = 1; |
|
|
684 | prev->flags |= CF_RUNNING; |
|
|
685 | } |
|
|
686 | |
|
|
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 | |
|
|
690 | if (next->flags & CF_RUNNING) |
|
|
691 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
692 | |
|
|
693 | prev->flags &= ~CF_RUNNING; |
|
|
694 | next->flags |= CF_RUNNING; |
670 | |
695 | |
671 | LOCK; |
696 | LOCK; |
672 | |
697 | |
673 | if (next->mainstack) |
698 | if (next->mainstack) |
674 | { |
699 | { |
… | |
… | |
681 | /* need to start coroutine */ |
706 | /* need to start coroutine */ |
682 | /* first get rid of the old state */ |
707 | /* first get rid of the old state */ |
683 | SAVE (prev, -1); |
708 | SAVE (prev, -1); |
684 | /* setup coroutine call */ |
709 | /* setup coroutine call */ |
685 | setup_coro (next); |
710 | setup_coro (next); |
686 | /* need a stack */ |
711 | /* need a new stack */ |
687 | next->cctx = 0; |
712 | assert (!next->stack); |
688 | } |
713 | } |
689 | |
|
|
690 | if (!prev->cctx) |
|
|
691 | /* create a new empty context */ |
|
|
692 | Newz (0, prev->cctx, 1, coro_cctx); |
|
|
693 | |
714 | |
694 | prev__cctx = prev->cctx; |
715 | prev__cctx = prev->cctx; |
695 | |
716 | |
696 | /* possibly "free" the cctx */ |
717 | /* possibly "free" the cctx */ |
697 | if (prev__cctx->idle_sp == STACKLEVEL) |
718 | if (prev__cctx->idle_sp == STACKLEVEL) |
698 | { |
719 | { |
|
|
720 | assert (PL_top_env == prev__cctx->top_env); |
|
|
721 | |
699 | cctx_put (prev__cctx); |
722 | cctx_put (prev__cctx); |
700 | prev->cctx = 0; |
723 | prev->cctx = 0; |
701 | } |
724 | } |
702 | |
725 | |
703 | if (!next->cctx) |
726 | if (!next->cctx) |
704 | next->cctx = cctx_get (); |
727 | next->cctx = cctx_get (); |
705 | |
728 | |
706 | if (prev__cctx != next->cctx) |
729 | if (prev__cctx != next->cctx) |
707 | { |
730 | { |
|
|
731 | assert ( prev__cctx->inuse); |
|
|
732 | assert (!next->cctx->inuse); |
|
|
733 | |
|
|
734 | prev__cctx->inuse = 0; |
|
|
735 | next->cctx->inuse = 1; |
|
|
736 | |
708 | prev__cctx->top_env = PL_top_env; |
737 | prev__cctx->top_env = PL_top_env; |
709 | PL_top_env = next->cctx->top_env; |
738 | PL_top_env = next->cctx->top_env; |
710 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
739 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
711 | } |
740 | } |
712 | |
741 | |
… | |
… | |
727 | static void |
756 | static void |
728 | coro_state_destroy (struct coro *coro) |
757 | coro_state_destroy (struct coro *coro) |
729 | { |
758 | { |
730 | if (coro->refcnt--) |
759 | if (coro->refcnt--) |
731 | return; |
760 | return; |
|
|
761 | |
|
|
762 | if (coro->flags & CF_RUNNING) |
|
|
763 | croak ("FATAL: tried to destroy currently running coroutine"); |
732 | |
764 | |
733 | if (coro->mainstack && coro->mainstack != main_mainstack) |
765 | if (coro->mainstack && coro->mainstack != main_mainstack) |
734 | { |
766 | { |
735 | struct coro temp; |
767 | struct coro temp; |
736 | |
768 | |
… | |
… | |
834 | static GV *coro_current, *coro_idle; |
866 | static GV *coro_current, *coro_idle; |
835 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
867 | static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; |
836 | static int coro_nready; |
868 | static int coro_nready; |
837 | |
869 | |
838 | static void |
870 | static void |
839 | coro_enq (SV *sv) |
871 | coro_enq (SV *coro_sv) |
840 | { |
872 | { |
841 | int prio; |
|
|
842 | |
|
|
843 | if (SvTYPE (sv) != SVt_PVHV) |
|
|
844 | croak ("Coro::ready tried to enqueue something that is not a coroutine"); |
|
|
845 | |
|
|
846 | prio = SvSTATE (sv)->prio; |
|
|
847 | |
|
|
848 | av_push (coro_ready [prio - PRIO_MIN], sv); |
873 | av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); |
849 | coro_nready++; |
874 | coro_nready++; |
850 | } |
875 | } |
851 | |
876 | |
852 | static SV * |
877 | static SV * |
853 | coro_deq (int min_prio) |
878 | coro_deq (int min_prio) |
… | |
… | |
866 | } |
891 | } |
867 | |
892 | |
868 | return 0; |
893 | return 0; |
869 | } |
894 | } |
870 | |
895 | |
871 | static void |
896 | static int |
872 | api_ready (SV *coro) |
897 | api_ready (SV *coro_sv) |
873 | { |
898 | { |
874 | dTHX; |
899 | struct coro *coro; |
875 | |
900 | |
876 | if (SvROK (coro)) |
901 | if (SvROK (coro_sv)) |
877 | coro = SvRV (coro); |
902 | coro_sv = SvRV (coro_sv); |
|
|
903 | |
|
|
904 | coro = SvSTATE (coro_sv); |
|
|
905 | |
|
|
906 | if (coro->flags & CF_READY) |
|
|
907 | return 0; |
|
|
908 | |
|
|
909 | if (coro->flags & CF_RUNNING) |
|
|
910 | croak ("Coro::ready called on currently running coroutine"); |
|
|
911 | |
|
|
912 | coro->flags |= CF_READY; |
878 | |
913 | |
879 | LOCK; |
914 | LOCK; |
880 | coro_enq (SvREFCNT_inc (coro)); |
915 | coro_enq (SvREFCNT_inc (coro_sv)); |
881 | UNLOCK; |
916 | UNLOCK; |
|
|
917 | |
|
|
918 | return 1; |
|
|
919 | } |
|
|
920 | |
|
|
921 | static int |
|
|
922 | api_is_ready (SV *coro_sv) |
|
|
923 | { |
|
|
924 | return !!SvSTATE (coro_sv)->flags & CF_READY; |
882 | } |
925 | } |
883 | |
926 | |
884 | static void |
927 | static void |
885 | prepare_schedule (struct transfer_args *ta) |
928 | prepare_schedule (struct transfer_args *ta) |
886 | { |
929 | { |
… | |
… | |
922 | coro_mortal = prev; |
965 | coro_mortal = prev; |
923 | |
966 | |
924 | ta->prev = SvSTATE (prev); |
967 | ta->prev = SvSTATE (prev); |
925 | ta->next = SvSTATE (next); |
968 | ta->next = SvSTATE (next); |
926 | ta->flags = TRANSFER_SAVE_ALL; |
969 | ta->flags = TRANSFER_SAVE_ALL; |
|
|
970 | |
|
|
971 | ta->next->flags &= ~CF_READY; |
927 | } |
972 | } |
928 | |
973 | |
929 | static void |
974 | static void |
930 | prepare_cede (struct transfer_args *ta) |
975 | prepare_cede (struct transfer_args *ta) |
931 | { |
976 | { |
932 | LOCK; |
977 | api_ready (GvSV (coro_current)); |
933 | coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current)))); |
|
|
934 | UNLOCK; |
|
|
935 | |
978 | |
936 | prepare_schedule (ta); |
979 | prepare_schedule (ta); |
937 | } |
980 | } |
938 | |
981 | |
939 | static void |
982 | static void |
… | |
… | |
1125 | SV *sv = perl_get_sv("Coro::API", 1); |
1168 | SV *sv = perl_get_sv("Coro::API", 1); |
1126 | |
1169 | |
1127 | coroapi.schedule = api_schedule; |
1170 | coroapi.schedule = api_schedule; |
1128 | coroapi.cede = api_cede; |
1171 | coroapi.cede = api_cede; |
1129 | coroapi.ready = api_ready; |
1172 | coroapi.ready = api_ready; |
|
|
1173 | coroapi.is_ready = api_is_ready; |
1130 | coroapi.nready = &coro_nready; |
1174 | coroapi.nready = &coro_nready; |
1131 | coroapi.current = coro_current; |
1175 | coroapi.current = coro_current; |
1132 | |
1176 | |
1133 | GCoroAPI = &coroapi; |
1177 | GCoroAPI = &coroapi; |
1134 | sv_setiv (sv, (IV)&coroapi); |
1178 | sv_setiv (sv, (IV)&coroapi); |
… | |
… | |
1154 | |
1198 | |
1155 | coro->prio = newprio; |
1199 | coro->prio = newprio; |
1156 | } |
1200 | } |
1157 | } |
1201 | } |
1158 | |
1202 | |
1159 | void |
1203 | SV * |
1160 | ready (SV *self) |
1204 | ready (SV *self) |
1161 | PROTOTYPE: $ |
1205 | PROTOTYPE: $ |
1162 | CODE: |
1206 | CODE: |
1163 | api_ready (self); |
1207 | RETVAL = boolSV (api_ready (self)); |
|
|
1208 | OUTPUT: |
|
|
1209 | RETVAL |
|
|
1210 | |
|
|
1211 | SV * |
|
|
1212 | is_ready (SV *self) |
|
|
1213 | PROTOTYPE: $ |
|
|
1214 | CODE: |
|
|
1215 | RETVAL = boolSV (api_is_ready (self)); |
|
|
1216 | OUTPUT: |
|
|
1217 | RETVAL |
1164 | |
1218 | |
1165 | int |
1219 | int |
1166 | nready (...) |
1220 | nready (...) |
1167 | PROTOTYPE: |
1221 | PROTOTYPE: |
1168 | CODE: |
1222 | CODE: |