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.248 by root, Mon Sep 29 12:40:50 2008 UTC vs.
Revision 1.261 by root, Mon Nov 10 05:09:30 2008 UTC

52#else 52#else
53# define REGISTER_STACK(cctx,start,end) 53# define REGISTER_STACK(cctx,start,end)
54#endif 54#endif
55 55
56/* the maximum number of idle cctx that will be pooled */ 56/* the maximum number of idle cctx that will be pooled */
57#define MAX_IDLE_CCTX 8 57static int cctx_max_idle = 4;
58 58
59#define PERL_VERSION_ATLEAST(a,b,c) \ 59#define PERL_VERSION_ATLEAST(a,b,c) \
60 (PERL_REVISION > (a) \ 60 (PERL_REVISION > (a) \
61 || (PERL_REVISION == (a) \ 61 || (PERL_REVISION == (a) \
62 && (PERL_VERSION > (b) \ 62 && (PERL_VERSION > (b) \
81# ifndef IS_PADCONST 81# ifndef IS_PADCONST
82# define IS_PADCONST(v) 0 82# define IS_PADCONST(v) 0
83# endif 83# endif
84#endif 84#endif
85 85
86/* 5.11 */
87#ifndef CxHASARGS
88# define CxHASARGS(cx) (cx)->blk_sub.hasargs
89#endif
90
91/* 5.10.0 */
92#ifndef SvREFCNT_inc_NN
93# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv)
94#endif
95
86/* 5.8.8 */ 96/* 5.8.8 */
87#ifndef GV_NOTQUAL 97#ifndef GV_NOTQUAL
88# define GV_NOTQUAL 0 98# define GV_NOTQUAL 0
89#endif 99#endif
90#ifndef newSV 100#ifndef newSV
91# define newSV(l) NEWSV(0,l) 101# define newSV(l) NEWSV(0,l)
92#endif
93
94/* 5.11 */
95#ifndef CxHASARGS
96# define CxHASARGS(cx) (cx)->blk_sub.hasargs
97#endif 102#endif
98 103
99/* 5.8.7 */ 104/* 5.8.7 */
100#ifndef SvRV_set 105#ifndef SvRV_set
101# define SvRV_set(s,v) SvRV(s) = (v) 106# define SvRV_set(s,v) SvRV(s) = (v)
137#define NOINLINE attribute ((noinline)) 142#define NOINLINE attribute ((noinline))
138 143
139#include "CoroAPI.h" 144#include "CoroAPI.h"
140 145
141#ifdef USE_ITHREADS 146#ifdef USE_ITHREADS
147
142static perl_mutex coro_mutex; 148static perl_mutex coro_lock;
143# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 149# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0)
144# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 150# define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0)
151# if CORO_PTHREAD
152static void *coro_thx;
153# endif
154
145#else 155#else
156
146# define LOCK (void)0 157# define LOCK (void)0
147# define UNLOCK (void)0 158# define UNLOCK (void)0
159
148#endif 160#endif
161
162# undef LOCK
163# define LOCK (void)0
164# undef UNLOCK
165# define UNLOCK (void)0
149 166
150/* helper storage struct for Coro::AIO */ 167/* helper storage struct for Coro::AIO */
151struct io_state 168struct io_state
152{ 169{
170 AV *res;
153 int errorno; 171 int errorno;
154 I32 laststype; 172 I32 laststype; /* U16 in 5.10.0 */
155 int laststatval; 173 int laststatval;
156 Stat_t statcache; 174 Stat_t statcache;
157}; 175};
158 176
159static double (*nvtime)(); /* so why doesn't it take void? */ 177static double (*nvtime)(); /* so why doesn't it take void? */
160 178
179static U32 cctx_gen;
161static size_t coro_stacksize = CORO_STACKSIZE; 180static size_t cctx_stacksize = CORO_STACKSIZE;
162static struct CoroAPI coroapi; 181static struct CoroAPI coroapi;
163static AV *main_mainstack; /* used to differentiate between $main and others */ 182static AV *main_mainstack; /* used to differentiate between $main and others */
164static JMPENV *main_top_env; 183static JMPENV *main_top_env;
165static HV *coro_state_stash, *coro_stash; 184static HV *coro_state_stash, *coro_stash;
166static volatile SV *coro_mortal; /* will be freed after next transfer */ 185static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
186static volatile struct coro *transfer_next;
187
188struct transfer_args
189{
190 struct coro *prev, *next;
191};
167 192
168static GV *irsgv; /* $/ */ 193static GV *irsgv; /* $/ */
169static GV *stdoutgv; /* *STDOUT */ 194static GV *stdoutgv; /* *STDOUT */
170static SV *rv_diehook; 195static SV *rv_diehook;
171static SV *rv_warnhook; 196static SV *rv_warnhook;
203 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 228 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
204 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 229 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
205 JMPENV *top_env; 230 JMPENV *top_env;
206 coro_context cctx; 231 coro_context cctx;
207 232
233 U32 gen;
208#if CORO_USE_VALGRIND 234#if CORO_USE_VALGRIND
209 int valgrind_id; 235 int valgrind_id;
210#endif 236#endif
211 unsigned char flags; 237 unsigned char flags;
212} coro_cctx; 238} coro_cctx;
272#define PRIO_MIN -4 298#define PRIO_MIN -4
273 299
274/* for Coro.pm */ 300/* for Coro.pm */
275static SV *coro_current; 301static SV *coro_current;
276static SV *coro_readyhook; 302static SV *coro_readyhook;
277static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 303static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
278static int coro_nready; 304static int coro_nready;
279static struct coro *coro_first; 305static struct coro *coro_first;
280 306
281/** lowlevel stuff **********************************************************/ 307/** lowlevel stuff **********************************************************/
282 308
324 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 350 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
325#endif 351#endif
326 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 352 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
327 --AvFILLp (padlist); 353 --AvFILLp (padlist);
328 354
329 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 355 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE)));
330 av_store (newpadlist, 1, (SV *)newpad); 356 av_store (newpadlist, 1, (SV *)newpad);
331 357
332 return newpadlist; 358 return newpadlist;
333} 359}
334 360
377static MGVTBL coro_cv_vtbl = { 403static MGVTBL coro_cv_vtbl = {
378 0, 0, 0, 0, 404 0, 0, 0, 0,
379 coro_cv_free 405 coro_cv_free
380}; 406};
381 407
382#define CORO_MAGIC(sv,type) \ 408#define CORO_MAGIC(sv, type) \
383 SvMAGIC (sv) \ 409 SvMAGIC (sv) \
384 ? SvMAGIC (sv)->mg_type == type \ 410 ? SvMAGIC (sv)->mg_type == type \
385 ? SvMAGIC (sv) \ 411 ? SvMAGIC (sv) \
386 : mg_find (sv, type) \ 412 : mg_find (sv, type) \
387 : 0 413 : 0
388 414
389#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 415#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
390#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) 416#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
391 417
392static struct coro * 418static struct coro *
622 648
623/* 649/*
624 * destroy the stacks, the callchain etc... 650 * destroy the stacks, the callchain etc...
625 */ 651 */
626static void 652static void
627coro_destroy_stacks (pTHX) 653coro_destruct_stacks (pTHX)
628{ 654{
629 while (PL_curstackinfo->si_next) 655 while (PL_curstackinfo->si_next)
630 PL_curstackinfo = PL_curstackinfo->si_next; 656 PL_curstackinfo = PL_curstackinfo->si_next;
631 657
632 while (PL_curstackinfo) 658 while (PL_curstackinfo)
810 GvSV (PL_defgv) = newSV (0); 836 GvSV (PL_defgv) = newSV (0);
811 GvAV (PL_defgv) = coro->args; coro->args = 0; 837 GvAV (PL_defgv) = coro->args; coro->args = 0;
812 GvSV (PL_errgv) = newSV (0); 838 GvSV (PL_errgv) = newSV (0);
813 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 839 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
814 PL_rs = newSVsv (GvSV (irsgv)); 840 PL_rs = newSVsv (GvSV (irsgv));
815 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); 841 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
816 842
817 { 843 {
818 dSP; 844 dSP;
819 LOGOP myop; 845 UNOP myop;
820 846
821 Zero (&myop, 1, LOGOP); 847 Zero (&myop, 1, UNOP);
822 myop.op_next = Nullop; 848 myop.op_next = Nullop;
823 myop.op_flags = OPf_WANT_VOID; 849 myop.op_flags = OPf_WANT_VOID;
824 850
825 PUSHMARK (SP); 851 PUSHMARK (SP);
826 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 852 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
830 SPAGAIN; 856 SPAGAIN;
831 } 857 }
832 858
833 /* this newly created coroutine might be run on an existing cctx which most 859 /* this newly created coroutine might be run on an existing cctx which most
834 * likely was suspended in set_stacklevel, called from entersub. 860 * likely was suspended in set_stacklevel, called from entersub.
835 * set_stacklevl doesn't do anything on return, but entersub does LEAVE, 861 * set_stacklevel doesn't do anything on return, but entersub does LEAVE,
836 * so we ENTER here for symmetry 862 * so we ENTER here for symmetry.
837 */ 863 */
838 ENTER; 864 ENTER;
839} 865}
840 866
841static void 867static void
842coro_destroy (pTHX_ struct coro *coro) 868coro_destruct (pTHX_ struct coro *coro)
843{ 869{
844 if (!IN_DESTRUCT) 870 if (!IN_DESTRUCT)
845 { 871 {
846 /* restore all saved variables and stuff */ 872 /* restore all saved variables and stuff */
847 LEAVE_SCOPE (0); 873 LEAVE_SCOPE (0);
869 SvREFCNT_dec (PL_warnhook); 895 SvREFCNT_dec (PL_warnhook);
870 896
871 SvREFCNT_dec (coro->saved_deffh); 897 SvREFCNT_dec (coro->saved_deffh);
872 SvREFCNT_dec (coro->throw); 898 SvREFCNT_dec (coro->throw);
873 899
874 coro_destroy_stacks (aTHX); 900 coro_destruct_stacks (aTHX);
875} 901}
876 902
877static void 903static void
878free_coro_mortal (pTHX) 904free_coro_mortal (pTHX)
879{ 905{
916 : cx->blk_gimme == G_SCALAR ? bot + 1 942 : cx->blk_gimme == G_SCALAR ? bot + 1
917 : bot; 943 : bot;
918 944
919 av_extend (av, top - bot); 945 av_extend (av, top - bot);
920 while (bot < top) 946 while (bot < top)
921 av_push (av, SvREFCNT_inc (*bot++)); 947 av_push (av, SvREFCNT_inc_NN (*bot++));
922 948
923 PL_runops = RUNOPS_DEFAULT; 949 PL_runops = RUNOPS_DEFAULT;
924 ENTER; 950 ENTER;
925 SAVETMPS; 951 SAVETMPS;
926 EXTEND (SP, 3); 952 EXTEND (SP, 3);
1006 1032
1007 TAINT_NOT; 1033 TAINT_NOT;
1008 return 0; 1034 return 0;
1009} 1035}
1010 1036
1037static void
1038prepare_set_stacklevel (struct transfer_args *ta, struct coro_cctx *cctx)
1039{
1040 ta->prev = (struct coro *)cctx;
1041 ta->next = 0;
1042}
1043
1011/* inject a fake call to Coro::State::_cctx_init into the execution */ 1044/* inject a fake call to Coro::State::_cctx_init into the execution */
1012/* _cctx_init should be careful, as it could be called at almost any time */ 1045/* _cctx_init should be careful, as it could be called at almost any time */
1013/* during execution of a perl program */ 1046/* during execution of a perl program */
1047/* also initialises PL_top_env */
1014static void NOINLINE 1048static void NOINLINE
1015cctx_prepare (pTHX_ coro_cctx *cctx) 1049cctx_prepare (pTHX_ coro_cctx *cctx)
1016{ 1050{
1017 dSP; 1051 dSP;
1018 LOGOP myop; 1052 UNOP myop;
1019 1053
1020 PL_top_env = &PL_start_env; 1054 PL_top_env = &PL_start_env;
1021 1055
1022 if (cctx->flags & CC_TRACE) 1056 if (cctx->flags & CC_TRACE)
1023 PL_runops = runops_trace; 1057 PL_runops = runops_trace;
1024 1058
1025 Zero (&myop, 1, LOGOP); 1059 Zero (&myop, 1, UNOP);
1026 myop.op_next = PL_op; 1060 myop.op_next = PL_op;
1027 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 1061 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
1028 1062
1029 PUSHMARK (SP); 1063 PUSHMARK (SP);
1030 EXTEND (SP, 2); 1064 EXTEND (SP, 2);
1031 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); 1065 PUSHs (sv_2mortal (newSViv ((IV)cctx)));
1032 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 1066 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
1033 PUTBACK; 1067 PUTBACK;
1034 PL_op = (OP *)&myop; 1068 PL_op = (OP *)&myop;
1035 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 1069 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1036 SPAGAIN; 1070 SPAGAIN;
1037} 1071}
1038 1072
1073/* the tail of transfer: execute stuff we can onyl do afetr a transfer */
1074static void
1075transfer_tail (void)
1076{
1077 struct coro *next = (struct coro *)transfer_next;
1078 transfer_next = 0; //D for temporary assertion in transfer
1079 assert (("FATAL ERROR: internal error 1067 in Coro module, please report", next));//D
1080
1081 free_coro_mortal (aTHX);
1082 UNLOCK;
1083
1084 if (expect_false (next->throw))
1085 {
1086 SV *exception = sv_2mortal (next->throw);
1087
1088 next->throw = 0;
1089 sv_setsv (ERRSV, exception);
1090 croak (0);
1091 }
1092}
1093
1039/* 1094/*
1040 * this is a _very_ stripped down perl interpreter ;) 1095 * this is a _very_ stripped down perl interpreter ;)
1041 */ 1096 */
1042static void 1097static void
1043cctx_run (void *arg) 1098cctx_run (void *arg)
1044{ 1099{
1100#ifdef USE_ITHREADS
1101# if CORO_PTHREAD
1102 PERL_SET_CONTEXT (coro_thx);
1103# endif
1104#endif
1105 {
1045 dTHX; 1106 dTHX;
1046 1107
1047 /* cctx_run is the alternative tail of transfer(), so unlock here. */ 1108 /* entersub called ENTER, but we never 'returned', undo that here */
1048 UNLOCK; 1109 LEAVE;
1049 1110
1050 /* we now skip the entersub that lead to transfer() */ 1111 /* we now skip the entersub that did lead to transfer() */
1051 PL_op = PL_op->op_next; 1112 PL_op = PL_op->op_next;
1052 1113
1053 /* inject a fake subroutine call to cctx_init */ 1114 /* inject a fake subroutine call to cctx_init */
1054 cctx_prepare (aTHX_ (coro_cctx *)arg); 1115 cctx_prepare (aTHX_ (coro_cctx *)arg);
1055 1116
1117 /* cctx_run is the alternative tail of transfer() */
1118 transfer_tail ();
1119
1056 /* somebody or something will hit me for both perl_run and PL_restartop */ 1120 /* somebody or something will hit me for both perl_run and PL_restartop */
1057 PL_restartop = PL_op; 1121 PL_restartop = PL_op;
1058 perl_run (PL_curinterp); 1122 perl_run (PL_curinterp);
1059 1123
1060 /* 1124 /*
1061 * If perl-run returns we assume exit() was being called or the coro 1125 * If perl-run returns we assume exit() was being called or the coro
1062 * fell off the end, which seems to be the only valid (non-bug) 1126 * fell off the end, which seems to be the only valid (non-bug)
1063 * reason for perl_run to return. We try to exit by jumping to the 1127 * reason for perl_run to return. We try to exit by jumping to the
1064 * bootstrap-time "top" top_env, as we cannot restore the "main" 1128 * bootstrap-time "top" top_env, as we cannot restore the "main"
1065 * coroutine as Coro has no such concept 1129 * coroutine as Coro has no such concept
1066 */ 1130 */
1067 PL_top_env = main_top_env; 1131 PL_top_env = main_top_env;
1068 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1132 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1133 }
1069} 1134}
1070 1135
1071static coro_cctx * 1136static coro_cctx *
1072cctx_new () 1137cctx_new ()
1073{ 1138{
1074 coro_cctx *cctx; 1139 coro_cctx *cctx;
1140
1141 ++cctx_count;
1142 New (0, cctx, 1, coro_cctx);
1143
1144 cctx->gen = cctx_gen;
1145 cctx->flags = 0;
1146 cctx->idle_sp = 0; /* can be accessed by transfer between cctx_run and set_stacklevel, on throw */
1147
1148 return cctx;
1149}
1150
1151/* create a new cctx only suitable as source */
1152static coro_cctx *
1153cctx_new_empty ()
1154{
1155 coro_cctx *cctx = cctx_new ();
1156
1157 cctx->sptr = 0;
1158 coro_create (&cctx->cctx, 0, 0, 0, 0);
1159
1160 return cctx;
1161}
1162
1163/* create a new cctx suitable as destination/running a perl interpreter */
1164static coro_cctx *
1165cctx_new_run ()
1166{
1167 coro_cctx *cctx = cctx_new ();
1075 void *stack_start; 1168 void *stack_start;
1076 size_t stack_size; 1169 size_t stack_size;
1077 1170
1078 ++cctx_count;
1079
1080 Newz (0, cctx, 1, coro_cctx);
1081
1082#if HAVE_MMAP 1171#if HAVE_MMAP
1083 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1172 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1084 /* mmap supposedly does allocate-on-write for us */ 1173 /* mmap supposedly does allocate-on-write for us */
1085 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1174 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
1086 1175
1087 if (cctx->sptr != (void *)-1) 1176 if (cctx->sptr != (void *)-1)
1088 { 1177 {
1094 cctx->flags |= CC_MAPPED; 1183 cctx->flags |= CC_MAPPED;
1095 } 1184 }
1096 else 1185 else
1097#endif 1186#endif
1098 { 1187 {
1099 cctx->ssize = coro_stacksize * (long)sizeof (long); 1188 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1100 New (0, cctx->sptr, coro_stacksize, long); 1189 New (0, cctx->sptr, cctx_stacksize, long);
1101 1190
1102 if (!cctx->sptr) 1191 if (!cctx->sptr)
1103 { 1192 {
1104 perror ("FATAL: unable to allocate stack for coroutine"); 1193 perror ("FATAL: unable to allocate stack for coroutine");
1105 _exit (EXIT_FAILURE); 1194 _exit (EXIT_FAILURE);
1120{ 1209{
1121 if (!cctx) 1210 if (!cctx)
1122 return; 1211 return;
1123 1212
1124 --cctx_count; 1213 --cctx_count;
1214 coro_destroy (&cctx->cctx);
1125 1215
1216 /* coro_transfer creates new, empty cctx's */
1217 if (cctx->sptr)
1218 {
1126#if CORO_USE_VALGRIND 1219#if CORO_USE_VALGRIND
1127 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1220 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1128#endif 1221#endif
1129 1222
1130#if HAVE_MMAP 1223#if HAVE_MMAP
1131 if (cctx->flags & CC_MAPPED) 1224 if (cctx->flags & CC_MAPPED)
1132 munmap (cctx->sptr, cctx->ssize); 1225 munmap (cctx->sptr, cctx->ssize);
1133 else 1226 else
1134#endif 1227#endif
1135 Safefree (cctx->sptr); 1228 Safefree (cctx->sptr);
1229 }
1136 1230
1137 Safefree (cctx); 1231 Safefree (cctx);
1138} 1232}
1139 1233
1140/* wether this cctx should be destructed */ 1234/* wether this cctx should be destructed */
1141#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) 1235#define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE))
1142 1236
1143static coro_cctx * 1237static coro_cctx *
1144cctx_get (pTHX) 1238cctx_get (pTHX)
1145{ 1239{
1146 while (expect_true (cctx_first)) 1240 while (expect_true (cctx_first))
1153 return cctx; 1247 return cctx;
1154 1248
1155 cctx_destroy (cctx); 1249 cctx_destroy (cctx);
1156 } 1250 }
1157 1251
1158 return cctx_new (); 1252 return cctx_new_run ();
1159} 1253}
1160 1254
1161static void 1255static void
1162cctx_put (coro_cctx *cctx) 1256cctx_put (coro_cctx *cctx)
1163{ 1257{
1258 assert (("cctx_put called on non-initialised cctx", cctx->sptr));
1259
1164 /* free another cctx if overlimit */ 1260 /* free another cctx if overlimit */
1165 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1261 if (expect_false (cctx_idle >= cctx_max_idle))
1166 { 1262 {
1167 coro_cctx *first = cctx_first; 1263 coro_cctx *first = cctx_first;
1168 cctx_first = first->next; 1264 cctx_first = first->next;
1169 --cctx_idle; 1265 --cctx_idle;
1170 1266
1202/* always use the TRANSFER macro */ 1298/* always use the TRANSFER macro */
1203static void NOINLINE 1299static void NOINLINE
1204transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1300transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1205{ 1301{
1206 dSTACKLEVEL; 1302 dSTACKLEVEL;
1207 static volatile int has_throw;
1208 1303
1209 /* sometimes transfer is only called to set idle_sp */ 1304 /* sometimes transfer is only called to set idle_sp */
1210 if (expect_false (!next)) 1305 if (expect_false (!next))
1211 { 1306 {
1212 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1307 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1216 { 1311 {
1217 coro_cctx *prev__cctx; 1312 coro_cctx *prev__cctx;
1218 1313
1219 if (expect_false (prev->flags & CF_NEW)) 1314 if (expect_false (prev->flags & CF_NEW))
1220 { 1315 {
1221 /* create a new empty context */ 1316 /* create a new empty/source context */
1222 Newz (0, prev->cctx, 1, coro_cctx); 1317 prev->cctx = cctx_new_empty ();
1223 prev->flags &= ~CF_NEW; 1318 prev->flags &= ~CF_NEW;
1224 prev->flags |= CF_RUNNING; 1319 prev->flags |= CF_RUNNING;
1225 } 1320 }
1226 1321
1227 prev->flags &= ~CF_RUNNING; 1322 prev->flags &= ~CF_RUNNING;
1242 else 1337 else
1243 load_perl (aTHX_ next); 1338 load_perl (aTHX_ next);
1244 1339
1245 prev__cctx = prev->cctx; 1340 prev__cctx = prev->cctx;
1246 1341
1247 /* possibly "free" the cctx */ 1342 /* possibly untie and reuse the cctx */
1248 if (expect_true ( 1343 if (expect_true (
1249 prev__cctx->idle_sp == STACKLEVEL 1344 prev__cctx->idle_sp == STACKLEVEL
1250 && !(prev__cctx->flags & CC_TRACE) 1345 && !(prev__cctx->flags & CC_TRACE)
1251 && !force_cctx 1346 && !force_cctx
1252 )) 1347 ))
1268 ++next->usecount; 1363 ++next->usecount;
1269 1364
1270 if (expect_true (!next->cctx)) 1365 if (expect_true (!next->cctx))
1271 next->cctx = cctx_get (aTHX); 1366 next->cctx = cctx_get (aTHX);
1272 1367
1273 has_throw = !!next->throw; 1368 assert (("FATAL ERROR: internal error 1352 in Coro, please report", !transfer_next));//D
1369 transfer_next = next;
1274 1370
1275 if (expect_false (prev__cctx != next->cctx)) 1371 if (expect_false (prev__cctx != next->cctx))
1276 { 1372 {
1277 prev__cctx->top_env = PL_top_env; 1373 prev__cctx->top_env = PL_top_env;
1278 PL_top_env = next->cctx->top_env; 1374 PL_top_env = next->cctx->top_env;
1279 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1375 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
1280 } 1376 }
1281 1377
1282 free_coro_mortal (aTHX); 1378 transfer_tail ();
1283 UNLOCK;
1284
1285 if (expect_false (has_throw))
1286 {
1287 struct coro *coro = SvSTATE (coro_current);
1288
1289 if (coro->throw)
1290 {
1291 SV *exception = coro->throw;
1292 coro->throw = 0;
1293 sv_setsv (ERRSV, exception);
1294 croak (0);
1295 }
1296 }
1297 } 1379 }
1298} 1380}
1299
1300struct transfer_args
1301{
1302 struct coro *prev, *next;
1303};
1304 1381
1305#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) 1382#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx))
1306#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) 1383#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1307 1384
1308/** high level stuff ********************************************************/ 1385/** high level stuff ********************************************************/
1334 croak ("FATAL: tried to destroy currently running coroutine"); 1411 croak ("FATAL: tried to destroy currently running coroutine");
1335 1412
1336 save_perl (aTHX_ &temp); 1413 save_perl (aTHX_ &temp);
1337 load_perl (aTHX_ coro); 1414 load_perl (aTHX_ coro);
1338 1415
1339 coro_destroy (aTHX_ coro); 1416 coro_destruct (aTHX_ coro);
1340 1417
1341 load_perl (aTHX_ &temp); 1418 load_perl (aTHX_ &temp);
1342 1419
1343 coro->slot = 0; 1420 coro->slot = 0;
1344 } 1421 }
1450 LOCK; 1527 LOCK;
1451 1528
1452 sv_hook = coro_nready ? 0 : coro_readyhook; 1529 sv_hook = coro_nready ? 0 : coro_readyhook;
1453 xs_hook = coro_nready ? 0 : coroapi.readyhook; 1530 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1454 1531
1455 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1532 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1456 ++coro_nready; 1533 ++coro_nready;
1457 1534
1458 UNLOCK; 1535 UNLOCK;
1459 1536
1460 if (sv_hook) 1537 if (sv_hook)
1520 /* cannot transfer to destroyed coros, skip and look for next */ 1597 /* cannot transfer to destroyed coros, skip and look for next */
1521 if (expect_false (ta->next->flags & CF_DESTROYED)) 1598 if (expect_false (ta->next->flags & CF_DESTROYED))
1522 { 1599 {
1523 UNLOCK; 1600 UNLOCK;
1524 SvREFCNT_dec (next_sv); 1601 SvREFCNT_dec (next_sv);
1525 /* coro_nready is already taken care of by destroy */ 1602 /* coro_nready has already been taken care of by destroy */
1526 continue; 1603 continue;
1527 } 1604 }
1528 1605
1529 --coro_nready; 1606 --coro_nready;
1530 UNLOCK; 1607 UNLOCK;
1615 struct coro *coro = SvSTATE (coro_sv); 1692 struct coro *coro = SvSTATE (coro_sv);
1616 1693
1617 if (flags & CC_TRACE) 1694 if (flags & CC_TRACE)
1618 { 1695 {
1619 if (!coro->cctx) 1696 if (!coro->cctx)
1620 coro->cctx = cctx_new (); 1697 coro->cctx = cctx_new_run ();
1621 else if (!(coro->cctx->flags & CC_TRACE)) 1698 else if (!(coro->cctx->flags & CC_TRACE))
1622 croak ("cannot enable tracing on coroutine with custom stack"); 1699 croak ("cannot enable tracing on coroutine with custom stack");
1623 1700
1624 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1701 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1625 } 1702 }
1632 else 1709 else
1633 coro->slot->runops = RUNOPS_DEFAULT; 1710 coro->slot->runops = RUNOPS_DEFAULT;
1634 } 1711 }
1635} 1712}
1636 1713
1714#if 0
1637static int 1715static int
1638coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) 1716coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1639{ 1717{
1640 AV *padlist; 1718 AV *padlist;
1641 AV *av = (AV *)mg->mg_obj; 1719 AV *av = (AV *)mg->mg_obj;
1647 1725
1648static MGVTBL coro_gensub_vtbl = { 1726static MGVTBL coro_gensub_vtbl = {
1649 0, 0, 0, 0, 1727 0, 0, 0, 0,
1650 coro_gensub_free 1728 coro_gensub_free
1651}; 1729};
1730#endif
1652 1731
1653/*****************************************************************************/ 1732/*****************************************************************************/
1654/* PerlIO::cede */ 1733/* PerlIO::cede */
1655 1734
1656typedef struct 1735typedef struct
1731PROTOTYPES: DISABLE 1810PROTOTYPES: DISABLE
1732 1811
1733BOOT: 1812BOOT:
1734{ 1813{
1735#ifdef USE_ITHREADS 1814#ifdef USE_ITHREADS
1736 MUTEX_INIT (&coro_mutex); 1815 MUTEX_INIT (&coro_lock);
1816# if CORO_PTHREAD
1817 coro_thx = PERL_GET_CONTEXT;
1818# endif
1737#endif 1819#endif
1738 BOOT_PAGESIZE; 1820 BOOT_PAGESIZE;
1739 1821
1740 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1822 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1741 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1823 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1822 1904
1823 PUTBACK; 1905 PUTBACK;
1824 switch (ix) 1906 switch (ix)
1825 { 1907 {
1826 case 0: 1908 case 0:
1827 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); 1909 prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (ST (0)));
1828 ta.next = 0;
1829 break; 1910 break;
1830 1911
1831 case 1: 1912 case 1:
1832 if (items != 2) 1913 if (items != 2)
1833 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); 1914 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items);
1834 1915
1835 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 1916 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1836 break; 1917 break;
1837 1918
1838 case 2: 1919 case 2:
1872 _exit (code); 1953 _exit (code);
1873 1954
1874int 1955int
1875cctx_stacksize (int new_stacksize = 0) 1956cctx_stacksize (int new_stacksize = 0)
1876 CODE: 1957 CODE:
1877 RETVAL = coro_stacksize; 1958 RETVAL = cctx_stacksize;
1878 if (new_stacksize) 1959 if (new_stacksize)
1960 {
1879 coro_stacksize = new_stacksize; 1961 cctx_stacksize = new_stacksize;
1962 ++cctx_gen;
1963 }
1964 OUTPUT:
1965 RETVAL
1966
1967int
1968cctx_max_idle (int max_idle = 0)
1969 CODE:
1970 RETVAL = cctx_max_idle;
1971 if (max_idle > 1)
1972 cctx_max_idle = max_idle;
1880 OUTPUT: 1973 OUTPUT:
1881 RETVAL 1974 RETVAL
1882 1975
1883int 1976int
1884cctx_count () 1977cctx_count ()
1962 RETVAL = boolSV (coro->flags & ix); 2055 RETVAL = boolSV (coro->flags & ix);
1963 OUTPUT: 2056 OUTPUT:
1964 RETVAL 2057 RETVAL
1965 2058
1966void 2059void
2060throw (Coro::State self, SV *throw = &PL_sv_undef)
2061 PROTOTYPE: $;$
2062 CODE:
2063 SvREFCNT_dec (self->throw);
2064 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2065
2066void
1967api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 2067api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1968 2068
1969SV * 2069SV *
1970has_cctx (Coro::State coro) 2070has_cctx (Coro::State coro)
1971 PROTOTYPE: $ 2071 PROTOTYPE: $
1980 CODE: 2080 CODE:
1981 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; 2081 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1982 OUTPUT: 2082 OUTPUT:
1983 RETVAL 2083 RETVAL
1984 2084
1985IV 2085UV
1986rss (Coro::State coro) 2086rss (Coro::State coro)
1987 PROTOTYPE: $ 2087 PROTOTYPE: $
1988 ALIAS: 2088 ALIAS:
1989 usecount = 1 2089 usecount = 1
1990 CODE: 2090 CODE:
2001 CODE: 2101 CODE:
2002 struct coro *coro = SvSTATE (coro_current); 2102 struct coro *coro = SvSTATE (coro_current);
2003 coro->cctx->idle_sp = 0; 2103 coro->cctx->idle_sp = 0;
2004 2104
2005void 2105void
2006throw (Coro::State self, SV *throw = &PL_sv_undef)
2007 PROTOTYPE: $;$
2008 CODE:
2009 SvREFCNT_dec (self->throw);
2010 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2011
2012void
2013swap_defsv (Coro::State self) 2106swap_defsv (Coro::State self)
2014 PROTOTYPE: $ 2107 PROTOTYPE: $
2015 ALIAS: 2108 ALIAS:
2016 swap_defav = 1 2109 swap_defav = 1
2017 CODE: 2110 CODE:
2071void 2164void
2072_set_current (SV *current) 2165_set_current (SV *current)
2073 PROTOTYPE: $ 2166 PROTOTYPE: $
2074 CODE: 2167 CODE:
2075 SvREFCNT_dec (SvRV (coro_current)); 2168 SvREFCNT_dec (SvRV (coro_current));
2076 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); 2169 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
2077 2170
2078void 2171void
2079_set_readyhook (SV *hook) 2172_set_readyhook (SV *hook)
2080 PROTOTYPE: $ 2173 PROTOTYPE: $
2081 CODE: 2174 CODE:
2141 SvREFCNT_dec (old); 2234 SvREFCNT_dec (old);
2142 croak ("\3async_pool terminate\2\n"); 2235 croak ("\3async_pool terminate\2\n");
2143 } 2236 }
2144 2237
2145 SvREFCNT_dec (coro->saved_deffh); 2238 SvREFCNT_dec (coro->saved_deffh);
2146 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2239 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2147 2240
2148 hv_store (hv, "desc", sizeof ("desc") - 1, 2241 hv_store (hv, "desc", sizeof ("desc") - 1,
2149 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 2242 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2150 2243
2151 invoke_av = (AV *)SvRV (invoke); 2244 invoke_av = (AV *)SvRV (invoke);
2155 2248
2156 if (len > 0) 2249 if (len > 0)
2157 { 2250 {
2158 av_fill (defav, len - 1); 2251 av_fill (defav, len - 1);
2159 for (i = 0; i < len; ++i) 2252 for (i = 0; i < len; ++i)
2160 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2253 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
2161 } 2254 }
2162 2255
2163 SvREFCNT_dec (invoke); 2256 SvREFCNT_dec (invoke);
2164} 2257}
2165 2258
2172 sv_setsv (cb, &PL_sv_undef); 2265 sv_setsv (cb, &PL_sv_undef);
2173 2266
2174 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2267 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2175 coro->saved_deffh = 0; 2268 coro->saved_deffh = 0;
2176 2269
2177 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2270 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2178 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2271 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2179 { 2272 {
2180 SV *old = PL_diehook; 2273 SV *old = PL_diehook;
2181 PL_diehook = 0; 2274 PL_diehook = 0;
2182 SvREFCNT_dec (old); 2275 SvREFCNT_dec (old);
2244#endif 2337#endif
2245 2338
2246 2339
2247MODULE = Coro::State PACKAGE = Coro::AIO 2340MODULE = Coro::State PACKAGE = Coro::AIO
2248 2341
2249SV * 2342void
2250_get_state () 2343_get_state (SV *self)
2251 CODE: 2344 PPCODE:
2252{ 2345{
2253 struct io_state *data; 2346 AV *defav = GvAV (PL_defgv);
2254 2347 AV *av = newAV ();
2348 int i;
2255 RETVAL = newSV (sizeof (struct io_state)); 2349 SV *data_sv = newSV (sizeof (struct io_state));
2256 data = (struct io_state *)SvPVX (RETVAL); 2350 struct io_state *data = (struct io_state *)SvPVX (data_sv);
2257 SvCUR_set (RETVAL, sizeof (struct io_state)); 2351 SvCUR_set (data_sv, sizeof (struct io_state));
2258 SvPOK_only (RETVAL); 2352 SvPOK_only (data_sv);
2259 2353
2260 data->errorno = errno; 2354 data->errorno = errno;
2261 data->laststype = PL_laststype; 2355 data->laststype = PL_laststype;
2262 data->laststatval = PL_laststatval; 2356 data->laststatval = PL_laststatval;
2263 data->statcache = PL_statcache; 2357 data->statcache = PL_statcache;
2358
2359 av_extend (av, AvFILLp (defav) + 1 + 1);
2360
2361 for (i = 0; i <= AvFILLp (defav); ++i)
2362 av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i]));
2363
2364 av_push (av, data_sv);
2365
2366 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2367
2368 api_ready (self);
2264} 2369}
2265 OUTPUT:
2266 RETVAL
2267 2370
2268void 2371void
2269_set_state (char *data_) 2372_set_state (SV *state)
2270 PROTOTYPE: $ 2373 PROTOTYPE: $
2271 CODE: 2374 PPCODE:
2272{ 2375{
2273 struct io_state *data = (void *)data_; 2376 AV *av = (AV *)SvRV (state);
2377 struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]);
2378 int i;
2274 2379
2275 errno = data->errorno; 2380 errno = data->errorno;
2276 PL_laststype = data->laststype; 2381 PL_laststype = data->laststype;
2277 PL_laststatval = data->laststatval; 2382 PL_laststatval = data->laststatval;
2278 PL_statcache = data->statcache; 2383 PL_statcache = data->statcache;
2384
2385 EXTEND (SP, AvFILLp (av));
2386 for (i = 0; i < AvFILLp (av); ++i)
2387 PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i])));
2279} 2388}
2280 2389
2281 2390
2282MODULE = Coro::State PACKAGE = Coro::AnyEvent 2391MODULE = Coro::State PACKAGE = Coro::AnyEvent
2283 2392

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines