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