ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.130 by root, Mon Dec 25 13:25:55 2006 UTC vs.
Revision 1.139 by root, Sun Jan 14 21:13:41 2007 UTC

93 93
94#define IN_DESTRUCT (PL_main_cv == Nullcv) 94#define IN_DESTRUCT (PL_main_cv == Nullcv)
95 95
96#if __GNUC__ >= 3 96#if __GNUC__ >= 3
97# define attribute(x) __attribute__(x) 97# define attribute(x) __attribute__(x)
98# define BARRIER __asm__ __volatile__ ("" : : : "memory")
98#else 99#else
99# define attribute(x) 100# define attribute(x)
101# define BARRIER
100#endif 102#endif
101 103
102#define NOINLINE attribute ((noinline)) 104#define NOINLINE attribute ((noinline))
103 105
104#include "CoroAPI.h" 106#include "CoroAPI.h"
110#else 112#else
111# define LOCK (void)0 113# define LOCK (void)0
112# define UNLOCK (void)0 114# define UNLOCK (void)0
113#endif 115#endif
114 116
117/* helper storage struct for Coro::AIO */
115struct io_state 118struct io_state
116{ 119{
117 int errorno; 120 int errorno;
118 I32 laststype; 121 I32 laststype;
119 int laststatval; 122 int laststatval;
135 /* the stack */ 138 /* the stack */
136 void *sptr; 139 void *sptr;
137 long ssize; /* positive == mmap, otherwise malloc */ 140 long ssize; /* positive == mmap, otherwise malloc */
138 141
139 /* cpu state */ 142 /* cpu state */
140 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 */
141 JMPENV *top_env; 145 JMPENV *top_env;
142 coro_context cctx; 146 coro_context cctx;
143 147
144 int inuse; 148 int inuse;
145 149
147 int valgrind_id; 151 int valgrind_id;
148#endif 152#endif
149} coro_cctx; 153} coro_cctx;
150 154
151enum { 155enum {
152 CF_RUNNING = 0x0001, /* coroutine is running */ 156 CF_RUNNING = 0x0001, /* coroutine is running */
153 CF_READY = 0x0002, /* coroutine is ready */ 157 CF_READY = 0x0002, /* coroutine is ready */
154 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 */
155}; 160};
156 161
157/* this is a structure representing a perl-level coroutine */ 162/* this is a structure representing a perl-level coroutine */
158struct coro { 163struct coro {
159 /* the c coroutine allocated to this perl coroutine, if any */ 164 /* the c coroutine allocated to this perl coroutine, if any */
180 int prio; 185 int prio;
181}; 186};
182 187
183typedef struct coro *Coro__State; 188typedef struct coro *Coro__State;
184typedef struct coro *Coro__State_or_hashref; 189typedef struct coro *Coro__State_or_hashref;
190
191/** Coro ********************************************************************/
192
193#define PRIO_MAX 3
194#define PRIO_HIGH 1
195#define PRIO_NORMAL 0
196#define PRIO_LOW -1
197#define PRIO_IDLE -3
198#define PRIO_MIN -4
199
200/* for Coro.pm */
201static SV *coro_current;
202static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
203static int coro_nready;
204
205/** lowlevel stuff **********************************************************/
185 206
186static AV * 207static AV *
187coro_clone_padlist (CV *cv) 208coro_clone_padlist (CV *cv)
188{ 209{
189 AV *padlist = CvPADLIST (cv); 210 AV *padlist = CvPADLIST (cv);
298 av_extend (av, AvMAX (av) + 1); 319 av_extend (av, AvMAX (av) + 1);
299 320
300 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 321 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
301} 322}
302 323
324/** load & save, init *******************************************************/
325
303#define SB do { 326#define SB do {
304#define SE } while (0) 327#define SE } while (0)
305 328
306#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE 329#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
307 330
468 491
469 /* free all temporaries */ 492 /* free all temporaries */
470 FREETMPS; 493 FREETMPS;
471 assert (PL_tmps_ix == -1); 494 assert (PL_tmps_ix == -1);
472 495
496 /* unwind all extra stacks */
473 POPSTACK_TO (PL_mainstack); 497 POPSTACK_TO (PL_mainstack);
498
499 /* unwind main stack */
500 dounwind (-1);
474 } 501 }
475 502
476 while (PL_curstackinfo->si_next) 503 while (PL_curstackinfo->si_next)
477 PL_curstackinfo = PL_curstackinfo->si_next; 504 PL_curstackinfo = PL_curstackinfo->si_next;
478 505
494 Safefree (PL_savestack); 521 Safefree (PL_savestack);
495#if !PERL_VERSION_ATLEAST (5,9,0) 522#if !PERL_VERSION_ATLEAST (5,9,0)
496 Safefree (PL_retstack); 523 Safefree (PL_retstack);
497#endif 524#endif
498} 525}
526
527/** coroutine stack handling ************************************************/
499 528
500static void 529static void
501setup_coro (struct coro *coro) 530setup_coro (struct coro *coro)
502{ 531{
503 /* 532 /*
696 ++cctx_idle; 725 ++cctx_idle;
697 cctx->next = cctx_first; 726 cctx->next = cctx_first;
698 cctx_first = cctx; 727 cctx_first = cctx;
699} 728}
700 729
730/** coroutine switching *****************************************************/
731
701/* never call directly, always through the coro_state_transfer global variable */ 732/* never call directly, always through the coro_state_transfer global variable */
702static void NOINLINE 733static void NOINLINE
703transfer (struct coro *prev, struct coro *next) 734transfer (struct coro *prev, struct coro *next)
704{ 735{
705 dSTACKLEVEL; 736 dSTACKLEVEL;
706 737
707 /* sometimes transfer is only called to set idle_sp */ 738 /* sometimes transfer is only called to set idle_sp */
708 if (!next) 739 if (!next)
709 { 740 {
710 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 741 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
711 assert (((coro_cctx *)prev)->top_env = PL_top_env); /* just for the side effetc when assert is enabled */ 742 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
712 } 743 }
713 else if (prev != next) 744 else if (prev != next)
714 { 745 {
715 coro_cctx *prev__cctx; 746 coro_cctx *prev__cctx;
716 747
727 if (!prev->flags & CF_RUNNING) 758 if (!prev->flags & CF_RUNNING)
728 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); 759 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
729 760
730 if (next->flags & CF_RUNNING) 761 if (next->flags & CF_RUNNING)
731 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 762 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
763
764 if (next->flags & CF_DESTROYED)
765 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
732 766
733 prev->flags &= ~CF_RUNNING; 767 prev->flags &= ~CF_RUNNING;
734 next->flags |= CF_RUNNING; 768 next->flags |= CF_RUNNING;
735 769
736 LOCK; 770 LOCK;
757 791
758 /* possibly "free" the cctx */ 792 /* possibly "free" the cctx */
759 if (prev__cctx->idle_sp == STACKLEVEL) 793 if (prev__cctx->idle_sp == STACKLEVEL)
760 { 794 {
761 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 795 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
762 assert (PL_top_env == prev__cctx->top_env); 796 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
763 797
764 prev->cctx = 0; 798 prev->cctx = 0;
765 799
766 cctx_put (prev__cctx); 800 cctx_put (prev__cctx);
767 prev__cctx->inuse = 0; 801 prev__cctx->inuse = 0;
780 PL_top_env = next->cctx->top_env; 814 PL_top_env = next->cctx->top_env;
781 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 815 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
782 } 816 }
783 817
784 free_coro_mortal (); 818 free_coro_mortal ();
785
786 UNLOCK; 819 UNLOCK;
787 } 820 }
788} 821}
789 822
790struct transfer_args 823struct transfer_args
792 struct coro *prev, *next; 825 struct coro *prev, *next;
793}; 826};
794 827
795#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 828#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
796 829
830/** high level stuff ********************************************************/
831
797static void 832static int
798coro_state_destroy (struct coro *coro) 833coro_state_destroy (struct coro *coro)
799{ 834{
800 if (coro->refcnt--) 835 if (coro->flags & CF_DESTROYED)
801 return; 836 return 0;
837
838 coro->flags |= CF_DESTROYED;
839
840 if (coro->flags & CF_READY)
841 {
842 /* reduce nready, as destroying a ready coro effectively unreadies it */
843 /* alternative: look through all ready queues and remove the coro */
844 LOCK;
845 --coro_nready;
846 UNLOCK;
847 }
848 else
849 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
802 850
803 if (coro->mainstack && coro->mainstack != main_mainstack) 851 if (coro->mainstack && coro->mainstack != main_mainstack)
804 { 852 {
853 assert (!(coro->flags & CF_RUNNING));
854
805 struct coro temp; 855 struct coro temp;
806 Zero (&temp, 1, struct coro); 856 Zero (&temp, 1, struct coro);
807 temp.save = CORO_SAVE_ALL; 857 temp.save = CORO_SAVE_ALL;
808 858
809 if (coro->flags & CF_RUNNING) 859 if (coro->flags & CF_RUNNING)
819 coro->mainstack = 0; 869 coro->mainstack = 0;
820 } 870 }
821 871
822 cctx_destroy (coro->cctx); 872 cctx_destroy (coro->cctx);
823 SvREFCNT_dec (coro->args); 873 SvREFCNT_dec (coro->args);
824 Safefree (coro); 874
875 return 1;
825} 876}
826 877
827static int 878static int
828coro_state_clear (pTHX_ SV *sv, MAGIC *mg) 879coro_state_free (pTHX_ SV *sv, MAGIC *mg)
829{ 880{
830 struct coro *coro = (struct coro *)mg->mg_ptr; 881 struct coro *coro = (struct coro *)mg->mg_ptr;
831 mg->mg_ptr = 0; 882 mg->mg_ptr = 0;
832 883
884 if (--coro->refcnt < 0)
885 {
833 coro_state_destroy (coro); 886 coro_state_destroy (coro);
887 Safefree (coro);
888 }
834 889
835 return 0; 890 return 0;
836} 891}
837 892
838static int 893static int
845 return 0; 900 return 0;
846} 901}
847 902
848static MGVTBL coro_state_vtbl = { 903static MGVTBL coro_state_vtbl = {
849 0, 0, 0, 0, 904 0, 0, 0, 0,
850 coro_state_clear, 905 coro_state_free,
851 0, 906 0,
852#ifdef MGf_DUP 907#ifdef MGf_DUP
853 coro_state_dup, 908 coro_state_dup,
854#else 909#else
855# define MGf_DUP 0 910# define MGf_DUP 0
906 return old_save; 961 return old_save;
907} 962}
908 963
909/** Coro ********************************************************************/ 964/** Coro ********************************************************************/
910 965
911#define PRIO_MAX 3
912#define PRIO_HIGH 1
913#define PRIO_NORMAL 0
914#define PRIO_LOW -1
915#define PRIO_IDLE -3
916#define PRIO_MIN -4
917
918/* for Coro.pm */
919static SV *coro_current;
920static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
921static int coro_nready;
922
923static void 966static void
924coro_enq (SV *coro_sv) 967coro_enq (SV *coro_sv)
925{ 968{
926 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 969 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
927 coro_nready++;
928} 970}
929 971
930static SV * 972static SV *
931coro_deq (int min_prio) 973coro_deq (int min_prio)
932{ 974{
936 if (min_prio < 0) 978 if (min_prio < 0)
937 min_prio = 0; 979 min_prio = 0;
938 980
939 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 981 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
940 if (AvFILLp (coro_ready [prio]) >= 0) 982 if (AvFILLp (coro_ready [prio]) >= 0)
941 {
942 coro_nready--;
943 return av_shift (coro_ready [prio]); 983 return av_shift (coro_ready [prio]);
944 }
945 984
946 return 0; 985 return 0;
947} 986}
948 987
949static int 988static int
961 1000
962 coro->flags |= CF_READY; 1001 coro->flags |= CF_READY;
963 1002
964 LOCK; 1003 LOCK;
965 coro_enq (SvREFCNT_inc (coro_sv)); 1004 coro_enq (SvREFCNT_inc (coro_sv));
1005 ++coro_nready;
966 UNLOCK; 1006 UNLOCK;
967 1007
968 return 1; 1008 return 1;
969} 1009}
970 1010
975} 1015}
976 1016
977static void 1017static void
978prepare_schedule (struct transfer_args *ta) 1018prepare_schedule (struct transfer_args *ta)
979{ 1019{
980 SV *prev, *next; 1020 SV *prev_sv, *next_sv;
981 1021
982 for (;;) 1022 for (;;)
983 { 1023 {
984 LOCK; 1024 LOCK;
985 next = coro_deq (PRIO_MIN); 1025 next_sv = coro_deq (PRIO_MIN);
1026
1027 /* nothing to schedule: call the idle handler */
1028 if (!next_sv)
1029 {
1030 UNLOCK;
1031 dSP;
1032
1033 ENTER;
1034 SAVETMPS;
1035
1036 PUSHMARK (SP);
1037 PUTBACK;
1038 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1039
1040 FREETMPS;
1041 LEAVE;
1042 continue;
1043 }
1044
1045 ta->next = SvSTATE (next_sv);
1046
1047 /* cannot transfer to destroyed coros, skip and look for next */
1048 if (ta->next->flags & CF_DESTROYED)
1049 {
1050 UNLOCK;
1051 SvREFCNT_dec (next_sv);
1052 /* coro_nready is already taken care of by destroy */
1053 continue;
1054 }
1055
1056 --coro_nready;
986 UNLOCK; 1057 UNLOCK;
987
988 if (next)
989 break; 1058 break;
990
991 {
992 dSP;
993
994 ENTER;
995 SAVETMPS;
996
997 PUSHMARK (SP);
998 PUTBACK;
999 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1000
1001 FREETMPS;
1002 LEAVE;
1003 } 1059 }
1004 }
1005
1006 prev = SvRV (coro_current);
1007 SvRV_set (coro_current, next);
1008 1060
1009 /* free this only after the transfer */ 1061 /* free this only after the transfer */
1062 prev_sv = SvRV (coro_current);
1063 SvRV_set (coro_current, next_sv);
1064 ta->prev = SvSTATE (prev_sv);
1065
1066 assert (ta->next->flags & CF_READY);
1067 ta->next->flags &= ~CF_READY;
1068
1010 LOCK; 1069 LOCK;
1011 free_coro_mortal (); 1070 free_coro_mortal ();
1071 coro_mortal = prev_sv;
1012 UNLOCK; 1072 UNLOCK;
1013 coro_mortal = prev;
1014
1015 assert (!SvROK(prev));//D
1016 assert (!SvROK(next));//D
1017
1018 ta->prev = SvSTATE (prev);
1019 ta->next = SvSTATE (next);
1020
1021 assert (ta->next->flags & CF_READY);
1022 ta->next->flags &= ~CF_READY;
1023} 1073}
1024 1074
1025static void 1075static void
1026prepare_cede (struct transfer_args *ta) 1076prepare_cede (struct transfer_args *ta)
1027{ 1077{
1028 api_ready (coro_current); 1078 api_ready (coro_current);
1029
1030 prepare_schedule (ta); 1079 prepare_schedule (ta);
1080}
1081
1082static int
1083prepare_cede_notself (struct transfer_args *ta)
1084{
1085 if (coro_nready)
1086 {
1087 SV *prev = SvRV (coro_current);
1088 prepare_schedule (ta);
1089 api_ready (prev);
1090 return 1;
1091 }
1092 else
1093 return 0;
1031} 1094}
1032 1095
1033static void 1096static void
1034api_schedule (void) 1097api_schedule (void)
1035{ 1098{
1037 1100
1038 prepare_schedule (&ta); 1101 prepare_schedule (&ta);
1039 TRANSFER (ta); 1102 TRANSFER (ta);
1040} 1103}
1041 1104
1042static void 1105static int
1043api_cede (void) 1106api_cede (void)
1044{ 1107{
1045 struct transfer_args ta; 1108 struct transfer_args ta;
1046 1109
1047 prepare_cede (&ta); 1110 prepare_cede (&ta);
1111
1112 if (ta.prev != ta.next)
1113 {
1048 TRANSFER (ta); 1114 TRANSFER (ta);
1115 return 1;
1116 }
1117 else
1118 return 0;
1119}
1120
1121static int
1122api_cede_notself (void)
1123{
1124 struct transfer_args ta;
1125
1126 if (prepare_cede_notself (&ta))
1127 {
1128 TRANSFER (ta);
1129 return 1;
1130 }
1131 else
1132 return 0;
1049} 1133}
1050 1134
1051MODULE = Coro::State PACKAGE = Coro::State 1135MODULE = Coro::State PACKAGE = Coro::State
1052 1136
1053PROTOTYPES: DISABLE 1137PROTOTYPES: DISABLE
1109_set_stacklevel (...) 1193_set_stacklevel (...)
1110 ALIAS: 1194 ALIAS:
1111 Coro::State::transfer = 1 1195 Coro::State::transfer = 1
1112 Coro::schedule = 2 1196 Coro::schedule = 2
1113 Coro::cede = 3 1197 Coro::cede = 3
1198 Coro::cede_notself = 4
1114 CODE: 1199 CODE:
1115{ 1200{
1116 struct transfer_args ta; 1201 struct transfer_args ta;
1117 1202
1118 switch (ix) 1203 switch (ix)
1134 break; 1219 break;
1135 1220
1136 case 3: 1221 case 3:
1137 prepare_cede (&ta); 1222 prepare_cede (&ta);
1138 break; 1223 break;
1224
1225 case 4:
1226 if (!prepare_cede_notself (&ta))
1227 XSRETURN_EMPTY;
1228
1229 break;
1139 } 1230 }
1140 1231
1232 BARRIER;
1141 TRANSFER (ta); 1233 TRANSFER (ta);
1142}
1143 1234
1144void 1235 if (GIMME_V != G_VOID && ta.next != ta.prev)
1145_clone_state_from (SV *dst, SV *src) 1236 XSRETURN_YES;
1237}
1238
1239bool
1240_destroy (SV *coro_sv)
1146 CODE: 1241 CODE:
1147{ 1242 RETVAL = coro_state_destroy (SvSTATE (coro_sv));
1148 struct coro *coro_src = SvSTATE (src); 1243 OUTPUT:
1149 1244 RETVAL
1150 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1151
1152 ++coro_src->refcnt;
1153 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1154}
1155 1245
1156void 1246void
1157_exit (code) 1247_exit (code)
1158 int code 1248 int code
1159 PROTOTYPE: $ 1249 PROTOTYPE: $
1196 coro_ready[i] = newAV (); 1286 coro_ready[i] = newAV ();
1197 1287
1198 { 1288 {
1199 SV *sv = perl_get_sv("Coro::API", 1); 1289 SV *sv = perl_get_sv("Coro::API", 1);
1200 1290
1201 coroapi.schedule = api_schedule; 1291 coroapi.schedule = api_schedule;
1202 coroapi.save = api_save; 1292 coroapi.save = api_save;
1203 coroapi.cede = api_cede; 1293 coroapi.cede = api_cede;
1294 coroapi.cede_notself = api_cede_notself;
1204 coroapi.ready = api_ready; 1295 coroapi.ready = api_ready;
1205 coroapi.is_ready = api_is_ready; 1296 coroapi.is_ready = api_is_ready;
1206 coroapi.nready = &coro_nready; 1297 coroapi.nready = &coro_nready;
1207 coroapi.current = coro_current; 1298 coroapi.current = coro_current;
1208 1299
1209 GCoroAPI = &coroapi; 1300 GCoroAPI = &coroapi;
1210 sv_setiv (sv, (IV)&coroapi); 1301 sv_setiv (sv, (IV)&coroapi);
1211 SvREADONLY_on (sv); 1302 SvREADONLY_on (sv);
1212 } 1303 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines