… | |
… | |
137 | /* the stack */ |
137 | /* the stack */ |
138 | void *sptr; |
138 | void *sptr; |
139 | long ssize; /* positive == mmap, otherwise malloc */ |
139 | long ssize; /* positive == mmap, otherwise malloc */ |
140 | |
140 | |
141 | /* cpu state */ |
141 | /* cpu state */ |
142 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
142 | void *idle_sp; /* sp of top-level transfer/schedule/cede call */ |
|
|
143 | JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ |
143 | JMPENV *top_env; |
144 | JMPENV *top_env; |
144 | coro_context cctx; |
145 | coro_context cctx; |
145 | |
146 | |
146 | int inuse; |
147 | int inuse; |
147 | |
148 | |
… | |
… | |
149 | int valgrind_id; |
150 | int valgrind_id; |
150 | #endif |
151 | #endif |
151 | } coro_cctx; |
152 | } coro_cctx; |
152 | |
153 | |
153 | enum { |
154 | enum { |
154 | CF_RUNNING = 0x0001, /* coroutine is running */ |
155 | CF_RUNNING = 0x0001, /* coroutine is running */ |
155 | CF_READY = 0x0002, /* coroutine is ready */ |
156 | CF_READY = 0x0002, /* coroutine is ready */ |
156 | CF_NEW = 0x0004, /* ahs never been switched to */ |
157 | CF_NEW = 0x0004, /* has never been switched to */ |
|
|
158 | CF_DESTROYED = 0x0008, /* coroutine data has been freed */ |
157 | }; |
159 | }; |
158 | |
160 | |
159 | /* this is a structure representing a perl-level coroutine */ |
161 | /* this is a structure representing a perl-level coroutine */ |
160 | struct coro { |
162 | struct coro { |
161 | /* the c coroutine allocated to this perl coroutine, if any */ |
163 | /* the c coroutine allocated to this perl coroutine, if any */ |
… | |
… | |
470 | |
472 | |
471 | /* free all temporaries */ |
473 | /* free all temporaries */ |
472 | FREETMPS; |
474 | FREETMPS; |
473 | assert (PL_tmps_ix == -1); |
475 | assert (PL_tmps_ix == -1); |
474 | |
476 | |
|
|
477 | /* unwind all extra stacks */ |
475 | POPSTACK_TO (PL_mainstack); |
478 | POPSTACK_TO (PL_mainstack); |
|
|
479 | |
|
|
480 | /* unwind main stack */ |
|
|
481 | dounwind (-1); |
476 | } |
482 | } |
477 | |
483 | |
478 | while (PL_curstackinfo->si_next) |
484 | while (PL_curstackinfo->si_next) |
479 | PL_curstackinfo = PL_curstackinfo->si_next; |
485 | PL_curstackinfo = PL_curstackinfo->si_next; |
480 | |
486 | |
… | |
… | |
708 | |
714 | |
709 | /* sometimes transfer is only called to set idle_sp */ |
715 | /* sometimes transfer is only called to set idle_sp */ |
710 | if (!next) |
716 | if (!next) |
711 | { |
717 | { |
712 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
718 | ((coro_cctx *)prev)->idle_sp = STACKLEVEL; |
713 | assert (((coro_cctx *)prev)->top_env = PL_top_env); /* just for the side effetc when assert is enabled */ |
719 | assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
714 | } |
720 | } |
715 | else if (prev != next) |
721 | else if (prev != next) |
716 | { |
722 | { |
717 | coro_cctx *prev__cctx; |
723 | coro_cctx *prev__cctx; |
718 | |
724 | |
… | |
… | |
729 | if (!prev->flags & CF_RUNNING) |
735 | if (!prev->flags & CF_RUNNING) |
730 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
736 | croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); |
731 | |
737 | |
732 | if (next->flags & CF_RUNNING) |
738 | if (next->flags & CF_RUNNING) |
733 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
739 | croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); |
|
|
740 | |
|
|
741 | if (next->flags & CF_DESTROYED) |
|
|
742 | croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states"); |
734 | |
743 | |
735 | prev->flags &= ~CF_RUNNING; |
744 | prev->flags &= ~CF_RUNNING; |
736 | next->flags |= CF_RUNNING; |
745 | next->flags |= CF_RUNNING; |
737 | |
746 | |
738 | LOCK; |
747 | LOCK; |
… | |
… | |
759 | |
768 | |
760 | /* possibly "free" the cctx */ |
769 | /* possibly "free" the cctx */ |
761 | if (prev__cctx->idle_sp == STACKLEVEL) |
770 | if (prev__cctx->idle_sp == STACKLEVEL) |
762 | { |
771 | { |
763 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
772 | /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ |
764 | assert (PL_top_env == prev__cctx->top_env); |
773 | assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); |
765 | |
774 | |
766 | prev->cctx = 0; |
775 | prev->cctx = 0; |
767 | |
776 | |
768 | cctx_put (prev__cctx); |
777 | cctx_put (prev__cctx); |
769 | prev__cctx->inuse = 0; |
778 | prev__cctx->inuse = 0; |
… | |
… | |
782 | PL_top_env = next->cctx->top_env; |
791 | PL_top_env = next->cctx->top_env; |
783 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
792 | coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); |
784 | } |
793 | } |
785 | |
794 | |
786 | free_coro_mortal (); |
795 | free_coro_mortal (); |
787 | |
|
|
788 | UNLOCK; |
796 | UNLOCK; |
789 | } |
797 | } |
790 | } |
798 | } |
791 | |
799 | |
792 | struct transfer_args |
800 | struct transfer_args |
… | |
… | |
794 | struct coro *prev, *next; |
802 | struct coro *prev, *next; |
795 | }; |
803 | }; |
796 | |
804 | |
797 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
805 | #define TRANSFER(ta) transfer ((ta).prev, (ta).next) |
798 | |
806 | |
799 | static void |
807 | static int |
800 | coro_state_destroy (struct coro *coro) |
808 | coro_state_destroy (struct coro *coro) |
801 | { |
809 | { |
802 | if (coro->refcnt--) |
810 | if (coro->flags & CF_DESTROYED) |
803 | return; |
811 | return 0; |
|
|
812 | |
|
|
813 | coro->flags |= CF_DESTROYED; |
804 | |
814 | |
805 | if (coro->mainstack && coro->mainstack != main_mainstack) |
815 | if (coro->mainstack && coro->mainstack != main_mainstack) |
806 | { |
816 | { |
|
|
817 | assert (!(coro->flags & CF_RUNNING)); |
|
|
818 | |
807 | struct coro temp; |
819 | struct coro temp; |
808 | Zero (&temp, 1, struct coro); |
820 | Zero (&temp, 1, struct coro); |
809 | temp.save = CORO_SAVE_ALL; |
821 | temp.save = CORO_SAVE_ALL; |
810 | |
822 | |
811 | if (coro->flags & CF_RUNNING) |
823 | if (coro->flags & CF_RUNNING) |
… | |
… | |
821 | coro->mainstack = 0; |
833 | coro->mainstack = 0; |
822 | } |
834 | } |
823 | |
835 | |
824 | cctx_destroy (coro->cctx); |
836 | cctx_destroy (coro->cctx); |
825 | SvREFCNT_dec (coro->args); |
837 | SvREFCNT_dec (coro->args); |
826 | Safefree (coro); |
838 | |
|
|
839 | return 1; |
827 | } |
840 | } |
828 | |
841 | |
829 | static int |
842 | static int |
830 | coro_state_clear (pTHX_ SV *sv, MAGIC *mg) |
843 | coro_state_free (pTHX_ SV *sv, MAGIC *mg) |
831 | { |
844 | { |
832 | struct coro *coro = (struct coro *)mg->mg_ptr; |
845 | struct coro *coro = (struct coro *)mg->mg_ptr; |
833 | mg->mg_ptr = 0; |
846 | mg->mg_ptr = 0; |
834 | |
847 | |
|
|
848 | if (--coro->refcnt < 0) |
|
|
849 | { |
835 | coro_state_destroy (coro); |
850 | coro_state_destroy (coro); |
|
|
851 | Safefree (coro); |
|
|
852 | } |
836 | |
853 | |
837 | return 0; |
854 | return 0; |
838 | } |
855 | } |
839 | |
856 | |
840 | static int |
857 | static int |
… | |
… | |
847 | return 0; |
864 | return 0; |
848 | } |
865 | } |
849 | |
866 | |
850 | static MGVTBL coro_state_vtbl = { |
867 | static MGVTBL coro_state_vtbl = { |
851 | 0, 0, 0, 0, |
868 | 0, 0, 0, 0, |
852 | coro_state_clear, |
869 | coro_state_free, |
853 | 0, |
870 | 0, |
854 | #ifdef MGf_DUP |
871 | #ifdef MGf_DUP |
855 | coro_state_dup, |
872 | coro_state_dup, |
856 | #else |
873 | #else |
857 | # define MGf_DUP 0 |
874 | # define MGf_DUP 0 |
… | |
… | |
977 | } |
994 | } |
978 | |
995 | |
979 | static void |
996 | static void |
980 | prepare_schedule (struct transfer_args *ta) |
997 | prepare_schedule (struct transfer_args *ta) |
981 | { |
998 | { |
982 | SV *prev, *next; |
999 | SV *prev_sv, *next_sv; |
983 | |
1000 | |
984 | for (;;) |
1001 | for (;;) |
985 | { |
1002 | { |
986 | LOCK; |
1003 | LOCK; |
987 | next = coro_deq (PRIO_MIN); |
1004 | next_sv = coro_deq (PRIO_MIN); |
988 | UNLOCK; |
1005 | UNLOCK; |
989 | |
1006 | |
|
|
1007 | /* nothing to schedule: call the idle handler */ |
990 | if (next) |
1008 | if (!next_sv) |
991 | break; |
|
|
992 | |
|
|
993 | { |
1009 | { |
994 | dSP; |
1010 | dSP; |
995 | |
1011 | |
996 | ENTER; |
1012 | ENTER; |
997 | SAVETMPS; |
1013 | SAVETMPS; |
998 | |
1014 | |
999 | PUSHMARK (SP); |
1015 | PUSHMARK (SP); |
1000 | PUTBACK; |
1016 | PUTBACK; |
1001 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
1017 | call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); |
1002 | |
1018 | |
1003 | FREETMPS; |
1019 | FREETMPS; |
1004 | LEAVE; |
1020 | LEAVE; |
|
|
1021 | continue; |
1005 | } |
1022 | } |
1006 | } |
|
|
1007 | |
1023 | |
1008 | prev = SvRV (coro_current); |
1024 | ta->next = SvSTATE (next_sv); |
1009 | SvRV_set (coro_current, next); |
1025 | |
|
|
1026 | /* cannot transfer to destroyed coros, skip and look for next */ |
|
|
1027 | if (ta->next->flags & CF_DESTROYED) |
|
|
1028 | { |
|
|
1029 | SvREFCNT_dec (next_sv); |
|
|
1030 | continue; |
|
|
1031 | } |
|
|
1032 | |
|
|
1033 | break; |
|
|
1034 | } |
1010 | |
1035 | |
1011 | /* free this only after the transfer */ |
1036 | /* free this only after the transfer */ |
|
|
1037 | prev_sv = SvRV (coro_current); |
|
|
1038 | SvRV_set (coro_current, next_sv); |
|
|
1039 | ta->prev = SvSTATE (prev_sv); |
|
|
1040 | |
|
|
1041 | assert (ta->next->flags & CF_READY); |
|
|
1042 | ta->next->flags &= ~CF_READY; |
|
|
1043 | |
1012 | LOCK; |
1044 | LOCK; |
1013 | free_coro_mortal (); |
1045 | free_coro_mortal (); |
|
|
1046 | coro_mortal = prev_sv; |
1014 | UNLOCK; |
1047 | UNLOCK; |
1015 | coro_mortal = prev; |
|
|
1016 | |
|
|
1017 | assert (!SvROK(prev));//D |
|
|
1018 | assert (!SvROK(next));//D |
|
|
1019 | |
|
|
1020 | ta->prev = SvSTATE (prev); |
|
|
1021 | ta->next = SvSTATE (next); |
|
|
1022 | |
|
|
1023 | assert (ta->next->flags & CF_READY); |
|
|
1024 | ta->next->flags &= ~CF_READY; |
|
|
1025 | } |
1048 | } |
1026 | |
1049 | |
1027 | static void |
1050 | static void |
1028 | prepare_cede (struct transfer_args *ta) |
1051 | prepare_cede (struct transfer_args *ta) |
1029 | { |
1052 | { |
… | |
… | |
1181 | break; |
1204 | break; |
1182 | } |
1205 | } |
1183 | |
1206 | |
1184 | BARRIER; |
1207 | BARRIER; |
1185 | TRANSFER (ta); |
1208 | TRANSFER (ta); |
1186 | } |
|
|
1187 | |
1209 | |
1188 | void |
1210 | if (GIMME_V != G_VOID && ta.next != ta.prev) |
1189 | _clone_state_from (SV *dst, SV *src) |
1211 | XSRETURN_YES; |
|
|
1212 | } |
|
|
1213 | |
|
|
1214 | bool |
|
|
1215 | _destroy (SV *coro_sv) |
1190 | CODE: |
1216 | CODE: |
1191 | { |
1217 | RETVAL = coro_state_destroy (SvSTATE (coro_sv)); |
1192 | struct coro *coro_src = SvSTATE (src); |
1218 | OUTPUT: |
1193 | |
1219 | RETVAL |
1194 | sv_unmagic (SvRV (dst), PERL_MAGIC_ext); |
|
|
1195 | |
|
|
1196 | ++coro_src->refcnt; |
|
|
1197 | sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; |
|
|
1198 | } |
|
|
1199 | |
1220 | |
1200 | void |
1221 | void |
1201 | _exit (code) |
1222 | _exit (code) |
1202 | int code |
1223 | int code |
1203 | PROTOTYPE: $ |
1224 | PROTOTYPE: $ |