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.249 by root, Tue Sep 30 17:12:35 2008 UTC vs.
Revision 1.265 by root, Fri Nov 14 02:42:26 2008 UTC

46# define BOOT_PAGESIZE (void)0 46# define BOOT_PAGESIZE (void)0
47#endif 47#endif
48 48
49#if CORO_USE_VALGRIND 49#if CORO_USE_VALGRIND
50# include <valgrind/valgrind.h> 50# include <valgrind/valgrind.h>
51# define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end))
52#else
53# define REGISTER_STACK(cctx,start,end)
54#endif 51#endif
55 52
56/* the maximum number of idle cctx that will be pooled */ 53/* the maximum number of idle cctx that will be pooled */
57#define MAX_IDLE_CCTX 8 54static int cctx_max_idle = 4;
58 55
59#define PERL_VERSION_ATLEAST(a,b,c) \ 56#define PERL_VERSION_ATLEAST(a,b,c) \
60 (PERL_REVISION > (a) \ 57 (PERL_REVISION > (a) \
61 || (PERL_REVISION == (a) \ 58 || (PERL_REVISION == (a) \
62 && (PERL_VERSION > (b) \ 59 && (PERL_VERSION > (b) \
81# ifndef IS_PADCONST 78# ifndef IS_PADCONST
82# define IS_PADCONST(v) 0 79# define IS_PADCONST(v) 0
83# endif 80# endif
84#endif 81#endif
85 82
83/* 5.11 */
84#ifndef CxHASARGS
85# define CxHASARGS(cx) (cx)->blk_sub.hasargs
86#endif
87
88/* 5.10.0 */
89#ifndef SvREFCNT_inc_NN
90# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv)
91#endif
92
86/* 5.8.8 */ 93/* 5.8.8 */
87#ifndef GV_NOTQUAL 94#ifndef GV_NOTQUAL
88# define GV_NOTQUAL 0 95# define GV_NOTQUAL 0
89#endif 96#endif
90#ifndef newSV 97#ifndef newSV
91# define newSV(l) NEWSV(0,l) 98# 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 99#endif
98 100
99/* 5.8.7 */ 101/* 5.8.7 */
100#ifndef SvRV_set 102#ifndef SvRV_set
101# define SvRV_set(s,v) SvRV(s) = (v) 103# define SvRV_set(s,v) SvRV(s) = (v)
121 123
122#define IN_DESTRUCT (PL_main_cv == Nullcv) 124#define IN_DESTRUCT (PL_main_cv == Nullcv)
123 125
124#if __GNUC__ >= 3 126#if __GNUC__ >= 3
125# define attribute(x) __attribute__(x) 127# define attribute(x) __attribute__(x)
126# define BARRIER __asm__ __volatile__ ("" : : : "memory")
127# define expect(expr,value) __builtin_expect ((expr),(value)) 128# define expect(expr,value) __builtin_expect ((expr),(value))
129# define INLINE static inline
128#else 130#else
129# define attribute(x) 131# define attribute(x)
130# define BARRIER
131# define expect(expr,value) (expr) 132# define expect(expr,value) (expr)
133# define INLINE static
132#endif 134#endif
133 135
134#define expect_false(expr) expect ((expr) != 0, 0) 136#define expect_false(expr) expect ((expr) != 0, 0)
135#define expect_true(expr) expect ((expr) != 0, 1) 137#define expect_true(expr) expect ((expr) != 0, 1)
136 138
137#define NOINLINE attribute ((noinline)) 139#define NOINLINE attribute ((noinline))
138 140
139#include "CoroAPI.h" 141#include "CoroAPI.h"
140 142
141#ifdef USE_ITHREADS 143#ifdef USE_ITHREADS
144
142static perl_mutex coro_mutex; 145static perl_mutex coro_lock;
143# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 146# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0)
144# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 147# define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0)
148# if CORO_PTHREAD
149static void *coro_thx;
150# endif
151
145#else 152#else
153
146# define LOCK (void)0 154# define LOCK (void)0
147# define UNLOCK (void)0 155# define UNLOCK (void)0
156
148#endif 157#endif
158
159# undef LOCK
160# define LOCK (void)0
161# undef UNLOCK
162# define UNLOCK (void)0
149 163
150/* helper storage struct for Coro::AIO */ 164/* helper storage struct for Coro::AIO */
151struct io_state 165struct io_state
152{ 166{
153 AV *res; 167 AV *res;
154 int errorno; 168 int errorno;
155 I32 laststype; 169 I32 laststype; /* U16 in 5.10.0 */
156 int laststatval; 170 int laststatval;
157 Stat_t statcache; 171 Stat_t statcache;
158}; 172};
159 173
160static double (*nvtime)(); /* so why doesn't it take void? */ 174static double (*nvtime)(); /* so why doesn't it take void? */
161 175
176static U32 cctx_gen;
162static size_t coro_stacksize = CORO_STACKSIZE; 177static size_t cctx_stacksize = CORO_STACKSIZE;
163static struct CoroAPI coroapi; 178static struct CoroAPI coroapi;
164static AV *main_mainstack; /* used to differentiate between $main and others */ 179static AV *main_mainstack; /* used to differentiate between $main and others */
165static JMPENV *main_top_env; 180static JMPENV *main_top_env;
166static HV *coro_state_stash, *coro_stash; 181static HV *coro_state_stash, *coro_stash;
167static volatile SV *coro_mortal; /* will be freed after next transfer */ 182static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
183static volatile struct coro *transfer_next;
184
185struct transfer_args
186{
187 struct coro *prev, *next;
188};
168 189
169static GV *irsgv; /* $/ */ 190static GV *irsgv; /* $/ */
170static GV *stdoutgv; /* *STDOUT */ 191static GV *stdoutgv; /* *STDOUT */
171static SV *rv_diehook; 192static SV *rv_diehook;
172static SV *rv_warnhook; 193static SV *rv_warnhook;
204 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 225 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
205 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 226 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
206 JMPENV *top_env; 227 JMPENV *top_env;
207 coro_context cctx; 228 coro_context cctx;
208 229
230 U32 gen;
209#if CORO_USE_VALGRIND 231#if CORO_USE_VALGRIND
210 int valgrind_id; 232 int valgrind_id;
211#endif 233#endif
212 unsigned char flags; 234 unsigned char flags;
213} coro_cctx; 235} coro_cctx;
273#define PRIO_MIN -4 295#define PRIO_MIN -4
274 296
275/* for Coro.pm */ 297/* for Coro.pm */
276static SV *coro_current; 298static SV *coro_current;
277static SV *coro_readyhook; 299static SV *coro_readyhook;
278static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 300static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
279static int coro_nready; 301static int coro_nready;
280static struct coro *coro_first; 302static struct coro *coro_first;
281 303
282/** lowlevel stuff **********************************************************/ 304/** lowlevel stuff **********************************************************/
283 305
378static MGVTBL coro_cv_vtbl = { 400static MGVTBL coro_cv_vtbl = {
379 0, 0, 0, 0, 401 0, 0, 0, 0,
380 coro_cv_free 402 coro_cv_free
381}; 403};
382 404
383#define CORO_MAGIC(sv,type) \ 405#define CORO_MAGIC(sv, type) \
384 SvMAGIC (sv) \ 406 SvMAGIC (sv) \
385 ? SvMAGIC (sv)->mg_type == type \ 407 ? SvMAGIC (sv)->mg_type == type \
386 ? SvMAGIC (sv) \ 408 ? SvMAGIC (sv) \
387 : mg_find (sv, type) \ 409 : mg_find (sv, type) \
388 : 0 410 : 0
389 411
390#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 412#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
391#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) 413#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
392 414
393static struct coro * 415INLINE struct coro *
394SvSTATE_ (pTHX_ SV *coro) 416SvSTATE_ (pTHX_ SV *coro)
395{ 417{
396 HV *stash; 418 HV *stash;
397 MAGIC *mg; 419 MAGIC *mg;
398 420
623 645
624/* 646/*
625 * destroy the stacks, the callchain etc... 647 * destroy the stacks, the callchain etc...
626 */ 648 */
627static void 649static void
628coro_destroy_stacks (pTHX) 650coro_destruct_stacks (pTHX)
629{ 651{
630 while (PL_curstackinfo->si_next) 652 while (PL_curstackinfo->si_next)
631 PL_curstackinfo = PL_curstackinfo->si_next; 653 PL_curstackinfo = PL_curstackinfo->si_next;
632 654
633 while (PL_curstackinfo) 655 while (PL_curstackinfo)
687#endif 709#endif
688 } 710 }
689 } 711 }
690 712
691 return rss; 713 return rss;
714}
715
716/** set stacklevel support **************************************************/
717
718/* we sometimes need to create the effect of pp_set_stacklevel calling us */
719#define SSL_HEAD (void)0
720/* we somtimes need to create the effect of leaving via pp_set_stacklevel */
721#define SSL_TAIL set_stacklevel_tail (aTHX)
722
723INLINE void
724set_stacklevel_tail (pTHX)
725{
726 dSP;
727 int gimme = GIMME_V;
728
729 if (gimme == G_SCALAR)
730 XPUSHs (&PL_sv_undef);
731
732 PUTBACK;
692} 733}
693 734
694/** coroutine stack handling ************************************************/ 735/** coroutine stack handling ************************************************/
695 736
696static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); 737static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
815 PL_rs = newSVsv (GvSV (irsgv)); 856 PL_rs = newSVsv (GvSV (irsgv));
816 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 857 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
817 858
818 { 859 {
819 dSP; 860 dSP;
820 LOGOP myop; 861 UNOP myop;
821 862
822 Zero (&myop, 1, LOGOP); 863 Zero (&myop, 1, UNOP);
823 myop.op_next = Nullop; 864 myop.op_next = Nullop;
824 myop.op_flags = OPf_WANT_VOID; 865 myop.op_flags = OPf_WANT_VOID;
825 866
826 PUSHMARK (SP); 867 PUSHMARK (SP);
827 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 868 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
830 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 871 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
831 SPAGAIN; 872 SPAGAIN;
832 } 873 }
833 874
834 /* this newly created coroutine might be run on an existing cctx which most 875 /* this newly created coroutine might be run on an existing cctx which most
835 * likely was suspended in set_stacklevel, called from entersub. 876 * likely was suspended in set_stacklevel, called from pp_set_stacklevel,
836 * set_stacklevl doesn't do anything on return, but entersub does LEAVE, 877 * so we have to emulate entering pp_set_stacklevel here.
837 * so we ENTER here for symmetry
838 */ 878 */
839 ENTER; 879 SSL_HEAD;
840} 880}
841 881
842static void 882static void
843coro_destroy (pTHX_ struct coro *coro) 883coro_destruct (pTHX_ struct coro *coro)
844{ 884{
845 if (!IN_DESTRUCT) 885 if (!IN_DESTRUCT)
846 { 886 {
847 /* restore all saved variables and stuff */ 887 /* restore all saved variables and stuff */
848 LEAVE_SCOPE (0); 888 LEAVE_SCOPE (0);
870 SvREFCNT_dec (PL_warnhook); 910 SvREFCNT_dec (PL_warnhook);
871 911
872 SvREFCNT_dec (coro->saved_deffh); 912 SvREFCNT_dec (coro->saved_deffh);
873 SvREFCNT_dec (coro->throw); 913 SvREFCNT_dec (coro->throw);
874 914
875 coro_destroy_stacks (aTHX); 915 coro_destruct_stacks (aTHX);
876} 916}
877 917
878static void 918INLINE void
879free_coro_mortal (pTHX) 919free_coro_mortal (pTHX)
880{ 920{
881 if (expect_true (coro_mortal)) 921 if (expect_true (coro_mortal))
882 { 922 {
883 SvREFCNT_dec (coro_mortal); 923 SvREFCNT_dec (coro_mortal);
1007 1047
1008 TAINT_NOT; 1048 TAINT_NOT;
1009 return 0; 1049 return 0;
1010} 1050}
1011 1051
1052static void
1053prepare_set_stacklevel (struct transfer_args *ta, struct coro_cctx *cctx)
1054{
1055 ta->prev = (struct coro *)cctx;
1056 ta->next = 0;
1057}
1058
1012/* inject a fake call to Coro::State::_cctx_init into the execution */ 1059/* inject a fake call to Coro::State::_cctx_init into the execution */
1013/* _cctx_init should be careful, as it could be called at almost any time */ 1060/* _cctx_init should be careful, as it could be called at almost any time */
1014/* during execution of a perl program */ 1061/* during execution of a perl program */
1062/* also initialises PL_top_env */
1015static void NOINLINE 1063static void NOINLINE
1016cctx_prepare (pTHX_ coro_cctx *cctx) 1064cctx_prepare (pTHX_ coro_cctx *cctx)
1017{ 1065{
1018 dSP; 1066 dSP;
1019 LOGOP myop; 1067 UNOP myop;
1020 1068
1021 PL_top_env = &PL_start_env; 1069 PL_top_env = &PL_start_env;
1022 1070
1023 if (cctx->flags & CC_TRACE) 1071 if (cctx->flags & CC_TRACE)
1024 PL_runops = runops_trace; 1072 PL_runops = runops_trace;
1025 1073
1026 Zero (&myop, 1, LOGOP); 1074 Zero (&myop, 1, UNOP);
1027 myop.op_next = PL_op; 1075 myop.op_next = PL_op;
1028 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 1076 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
1029 1077
1030 PUSHMARK (SP); 1078 PUSHMARK (SP);
1031 EXTEND (SP, 2); 1079 EXTEND (SP, 2);
1032 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); 1080 PUSHs (sv_2mortal (newSViv ((IV)cctx)));
1033 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 1081 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
1034 PUTBACK; 1082 PUTBACK;
1035 PL_op = (OP *)&myop; 1083 PL_op = (OP *)&myop;
1036 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 1084 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1037 SPAGAIN; 1085 SPAGAIN;
1038} 1086}
1039 1087
1088/* the tail of transfer: execute stuff we can only do after a transfer */
1089INLINE void
1090transfer_tail (pTHX)
1091{
1092 struct coro *next = (struct coro *)transfer_next;
1093 assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */
1094 assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next));
1095
1096 free_coro_mortal (aTHX);
1097 UNLOCK;
1098
1099 if (expect_false (next->throw))
1100 {
1101 SV *exception = sv_2mortal (next->throw);
1102
1103 next->throw = 0;
1104 sv_setsv (ERRSV, exception);
1105 croak (0);
1106 }
1107}
1108
1040/* 1109/*
1041 * this is a _very_ stripped down perl interpreter ;) 1110 * this is a _very_ stripped down perl interpreter ;)
1042 */ 1111 */
1043static void 1112static void
1044cctx_run (void *arg) 1113cctx_run (void *arg)
1045{ 1114{
1115#ifdef USE_ITHREADS
1116# if CORO_PTHREAD
1117 PERL_SET_CONTEXT (coro_thx);
1118# endif
1119#endif
1120 {
1046 dTHX; 1121 dTHX;
1047 1122
1048 /* cctx_run is the alternative tail of transfer(), so unlock here. */ 1123 /* we are the alternative tail to pp_set_stacklevel */
1049 UNLOCK; 1124 /* so do the same things here */
1125 SSL_TAIL;
1050 1126
1051 /* we now skip the entersub that lead to transfer() */ 1127 /* we now skip the op that did lead to transfer() */
1052 PL_op = PL_op->op_next; 1128 PL_op = PL_op->op_next;
1053 1129
1054 /* inject a fake subroutine call to cctx_init */ 1130 /* inject a fake subroutine call to cctx_init */
1055 cctx_prepare (aTHX_ (coro_cctx *)arg); 1131 cctx_prepare (aTHX_ (coro_cctx *)arg);
1056 1132
1133 /* cctx_run is the alternative tail of transfer() */
1134 transfer_tail (aTHX);
1135
1057 /* somebody or something will hit me for both perl_run and PL_restartop */ 1136 /* somebody or something will hit me for both perl_run and PL_restartop */
1058 PL_restartop = PL_op; 1137 PL_restartop = PL_op;
1059 perl_run (PL_curinterp); 1138 perl_run (PL_curinterp);
1060 1139
1061 /* 1140 /*
1062 * If perl-run returns we assume exit() was being called or the coro 1141 * If perl-run returns we assume exit() was being called or the coro
1063 * fell off the end, which seems to be the only valid (non-bug) 1142 * fell off the end, which seems to be the only valid (non-bug)
1064 * reason for perl_run to return. We try to exit by jumping to the 1143 * reason for perl_run to return. We try to exit by jumping to the
1065 * bootstrap-time "top" top_env, as we cannot restore the "main" 1144 * bootstrap-time "top" top_env, as we cannot restore the "main"
1066 * coroutine as Coro has no such concept 1145 * coroutine as Coro has no such concept
1067 */ 1146 */
1068 PL_top_env = main_top_env; 1147 PL_top_env = main_top_env;
1069 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1148 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1149 }
1070} 1150}
1071 1151
1072static coro_cctx * 1152static coro_cctx *
1073cctx_new () 1153cctx_new ()
1074{ 1154{
1075 coro_cctx *cctx; 1155 coro_cctx *cctx;
1156
1157 ++cctx_count;
1158 New (0, cctx, 1, coro_cctx);
1159
1160 cctx->gen = cctx_gen;
1161 cctx->flags = 0;
1162 cctx->idle_sp = 0; /* can be accessed by transfer between cctx_run and set_stacklevel, on throw */
1163
1164 return cctx;
1165}
1166
1167/* create a new cctx only suitable as source */
1168static coro_cctx *
1169cctx_new_empty ()
1170{
1171 coro_cctx *cctx = cctx_new ();
1172
1173 cctx->sptr = 0;
1174 coro_create (&cctx->cctx, 0, 0, 0, 0);
1175
1176 return cctx;
1177}
1178
1179/* create a new cctx suitable as destination/running a perl interpreter */
1180static coro_cctx *
1181cctx_new_run ()
1182{
1183 coro_cctx *cctx = cctx_new ();
1076 void *stack_start; 1184 void *stack_start;
1077 size_t stack_size; 1185 size_t stack_size;
1078 1186
1079 ++cctx_count;
1080
1081 Newz (0, cctx, 1, coro_cctx);
1082
1083#if HAVE_MMAP 1187#if HAVE_MMAP
1084 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1188 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1085 /* mmap supposedly does allocate-on-write for us */ 1189 /* mmap supposedly does allocate-on-write for us */
1086 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1190 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
1087 1191
1088 if (cctx->sptr != (void *)-1) 1192 if (cctx->sptr != (void *)-1)
1089 { 1193 {
1090# if CORO_STACKGUARD 1194 #if CORO_STACKGUARD
1091 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1195 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1092# endif 1196 #endif
1093 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 1197 stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE;
1094 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 1198 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
1095 cctx->flags |= CC_MAPPED; 1199 cctx->flags |= CC_MAPPED;
1096 } 1200 }
1097 else 1201 else
1098#endif 1202#endif
1099 { 1203 {
1100 cctx->ssize = coro_stacksize * (long)sizeof (long); 1204 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1101 New (0, cctx->sptr, coro_stacksize, long); 1205 New (0, cctx->sptr, cctx_stacksize, long);
1102 1206
1103 if (!cctx->sptr) 1207 if (!cctx->sptr)
1104 { 1208 {
1105 perror ("FATAL: unable to allocate stack for coroutine"); 1209 perror ("FATAL: unable to allocate stack for coroutine, exiting.");
1106 _exit (EXIT_FAILURE); 1210 _exit (EXIT_FAILURE);
1107 } 1211 }
1108 1212
1109 stack_start = cctx->sptr; 1213 stack_start = cctx->sptr;
1110 stack_size = cctx->ssize; 1214 stack_size = cctx->ssize;
1111 } 1215 }
1112 1216
1113 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); 1217 #if CORO_USE_VALGRIND
1218 cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size);
1219 #endif
1220
1114 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); 1221 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size);
1115 1222
1116 return cctx; 1223 return cctx;
1117} 1224}
1118 1225
1121{ 1228{
1122 if (!cctx) 1229 if (!cctx)
1123 return; 1230 return;
1124 1231
1125 --cctx_count; 1232 --cctx_count;
1233 coro_destroy (&cctx->cctx);
1126 1234
1235 /* coro_transfer creates new, empty cctx's */
1236 if (cctx->sptr)
1237 {
1127#if CORO_USE_VALGRIND 1238 #if CORO_USE_VALGRIND
1128 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1239 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1129#endif 1240 #endif
1130 1241
1131#if HAVE_MMAP 1242#if HAVE_MMAP
1132 if (cctx->flags & CC_MAPPED) 1243 if (cctx->flags & CC_MAPPED)
1133 munmap (cctx->sptr, cctx->ssize); 1244 munmap (cctx->sptr, cctx->ssize);
1134 else 1245 else
1135#endif 1246#endif
1136 Safefree (cctx->sptr); 1247 Safefree (cctx->sptr);
1248 }
1137 1249
1138 Safefree (cctx); 1250 Safefree (cctx);
1139} 1251}
1140 1252
1141/* wether this cctx should be destructed */ 1253/* wether this cctx should be destructed */
1142#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) 1254#define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE))
1143 1255
1144static coro_cctx * 1256static coro_cctx *
1145cctx_get (pTHX) 1257cctx_get (pTHX)
1146{ 1258{
1147 while (expect_true (cctx_first)) 1259 while (expect_true (cctx_first))
1154 return cctx; 1266 return cctx;
1155 1267
1156 cctx_destroy (cctx); 1268 cctx_destroy (cctx);
1157 } 1269 }
1158 1270
1159 return cctx_new (); 1271 return cctx_new_run ();
1160} 1272}
1161 1273
1162static void 1274static void
1163cctx_put (coro_cctx *cctx) 1275cctx_put (coro_cctx *cctx)
1164{ 1276{
1277 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr));
1278
1165 /* free another cctx if overlimit */ 1279 /* free another cctx if overlimit */
1166 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1280 if (expect_false (cctx_idle >= cctx_max_idle))
1167 { 1281 {
1168 coro_cctx *first = cctx_first; 1282 coro_cctx *first = cctx_first;
1169 cctx_first = first->next; 1283 cctx_first = first->next;
1170 --cctx_idle; 1284 --cctx_idle;
1171 1285
1203/* always use the TRANSFER macro */ 1317/* always use the TRANSFER macro */
1204static void NOINLINE 1318static void NOINLINE
1205transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1319transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1206{ 1320{
1207 dSTACKLEVEL; 1321 dSTACKLEVEL;
1208 static volatile int has_throw;
1209 1322
1210 /* sometimes transfer is only called to set idle_sp */ 1323 /* sometimes transfer is only called to set idle_sp */
1211 if (expect_false (!next)) 1324 if (expect_false (!next))
1212 { 1325 {
1213 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1326 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1217 { 1330 {
1218 coro_cctx *prev__cctx; 1331 coro_cctx *prev__cctx;
1219 1332
1220 if (expect_false (prev->flags & CF_NEW)) 1333 if (expect_false (prev->flags & CF_NEW))
1221 { 1334 {
1222 /* create a new empty context */ 1335 /* create a new empty/source context */
1223 Newz (0, prev->cctx, 1, coro_cctx); 1336 prev->cctx = cctx_new_empty ();
1224 prev->flags &= ~CF_NEW; 1337 prev->flags &= ~CF_NEW;
1225 prev->flags |= CF_RUNNING; 1338 prev->flags |= CF_RUNNING;
1226 } 1339 }
1227 1340
1228 prev->flags &= ~CF_RUNNING; 1341 prev->flags &= ~CF_RUNNING;
1243 else 1356 else
1244 load_perl (aTHX_ next); 1357 load_perl (aTHX_ next);
1245 1358
1246 prev__cctx = prev->cctx; 1359 prev__cctx = prev->cctx;
1247 1360
1248 /* possibly "free" the cctx */ 1361 /* possibly untie and reuse the cctx */
1249 if (expect_true ( 1362 if (expect_true (
1250 prev__cctx->idle_sp == STACKLEVEL 1363 prev__cctx->idle_sp == STACKLEVEL
1251 && !(prev__cctx->flags & CC_TRACE) 1364 && !(prev__cctx->flags & CC_TRACE)
1252 && !force_cctx 1365 && !force_cctx
1253 )) 1366 ))
1254 { 1367 {
1255 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1368 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
1256 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 1369 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te));
1257 1370
1258 prev->cctx = 0; 1371 prev->cctx = 0;
1259 1372
1260 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1373 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1261 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1374 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1269 ++next->usecount; 1382 ++next->usecount;
1270 1383
1271 if (expect_true (!next->cctx)) 1384 if (expect_true (!next->cctx))
1272 next->cctx = cctx_get (aTHX); 1385 next->cctx = cctx_get (aTHX);
1273 1386
1274 has_throw = !!next->throw; 1387 assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next));
1388 transfer_next = next;
1275 1389
1276 if (expect_false (prev__cctx != next->cctx)) 1390 if (expect_false (prev__cctx != next->cctx))
1277 { 1391 {
1278 prev__cctx->top_env = PL_top_env; 1392 prev__cctx->top_env = PL_top_env;
1279 PL_top_env = next->cctx->top_env; 1393 PL_top_env = next->cctx->top_env;
1280 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1394 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
1281 } 1395 }
1282 1396
1283 free_coro_mortal (aTHX); 1397 transfer_tail (aTHX);
1284 UNLOCK;
1285
1286 if (expect_false (has_throw))
1287 {
1288 struct coro *coro = SvSTATE (coro_current);
1289
1290 if (coro->throw)
1291 {
1292 SV *exception = coro->throw;
1293 coro->throw = 0;
1294 sv_setsv (ERRSV, exception);
1295 croak (0);
1296 }
1297 }
1298 } 1398 }
1299} 1399}
1300
1301struct transfer_args
1302{
1303 struct coro *prev, *next;
1304};
1305 1400
1306#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) 1401#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx))
1307#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) 1402#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1308 1403
1309/** high level stuff ********************************************************/ 1404/** high level stuff ********************************************************/
1335 croak ("FATAL: tried to destroy currently running coroutine"); 1430 croak ("FATAL: tried to destroy currently running coroutine");
1336 1431
1337 save_perl (aTHX_ &temp); 1432 save_perl (aTHX_ &temp);
1338 load_perl (aTHX_ coro); 1433 load_perl (aTHX_ coro);
1339 1434
1340 coro_destroy (aTHX_ coro); 1435 coro_destruct (aTHX_ coro);
1341 1436
1342 load_perl (aTHX_ &temp); 1437 load_perl (aTHX_ &temp);
1343 1438
1344 coro->slot = 0; 1439 coro->slot = 0;
1345 } 1440 }
1482 1577
1483static int 1578static int
1484api_is_ready (SV *coro_sv) 1579api_is_ready (SV *coro_sv)
1485{ 1580{
1486 dTHX; 1581 dTHX;
1582
1487 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1583 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1488} 1584}
1489 1585
1490static void 1586INLINE void
1491prepare_schedule (pTHX_ struct transfer_args *ta) 1587prepare_schedule (pTHX_ struct transfer_args *ta)
1492{ 1588{
1493 SV *prev_sv, *next_sv; 1589 SV *prev_sv, *next_sv;
1494 1590
1495 for (;;) 1591 for (;;)
1521 /* cannot transfer to destroyed coros, skip and look for next */ 1617 /* cannot transfer to destroyed coros, skip and look for next */
1522 if (expect_false (ta->next->flags & CF_DESTROYED)) 1618 if (expect_false (ta->next->flags & CF_DESTROYED))
1523 { 1619 {
1524 UNLOCK; 1620 UNLOCK;
1525 SvREFCNT_dec (next_sv); 1621 SvREFCNT_dec (next_sv);
1526 /* coro_nready is already taken care of by destroy */ 1622 /* coro_nready has already been taken care of by destroy */
1527 continue; 1623 continue;
1528 } 1624 }
1529 1625
1530 --coro_nready; 1626 --coro_nready;
1531 UNLOCK; 1627 UNLOCK;
1534 1630
1535 /* free this only after the transfer */ 1631 /* free this only after the transfer */
1536 prev_sv = SvRV (coro_current); 1632 prev_sv = SvRV (coro_current);
1537 ta->prev = SvSTATE (prev_sv); 1633 ta->prev = SvSTATE (prev_sv);
1538 TRANSFER_CHECK (*ta); 1634 TRANSFER_CHECK (*ta);
1539 assert (ta->next->flags & CF_READY); 1635 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY));
1540 ta->next->flags &= ~CF_READY; 1636 ta->next->flags &= ~CF_READY;
1541 SvRV_set (coro_current, next_sv); 1637 SvRV_set (coro_current, next_sv);
1542 1638
1543 LOCK; 1639 LOCK;
1544 free_coro_mortal (aTHX); 1640 free_coro_mortal (aTHX);
1545 coro_mortal = prev_sv; 1641 coro_mortal = prev_sv;
1546 UNLOCK; 1642 UNLOCK;
1547} 1643}
1548 1644
1549static void 1645INLINE void
1550prepare_cede (pTHX_ struct transfer_args *ta) 1646prepare_cede (pTHX_ struct transfer_args *ta)
1551{ 1647{
1552 api_ready (coro_current); 1648 api_ready (coro_current);
1553 prepare_schedule (aTHX_ ta); 1649 prepare_schedule (aTHX_ ta);
1554} 1650}
1616 struct coro *coro = SvSTATE (coro_sv); 1712 struct coro *coro = SvSTATE (coro_sv);
1617 1713
1618 if (flags & CC_TRACE) 1714 if (flags & CC_TRACE)
1619 { 1715 {
1620 if (!coro->cctx) 1716 if (!coro->cctx)
1621 coro->cctx = cctx_new (); 1717 coro->cctx = cctx_new_run ();
1622 else if (!(coro->cctx->flags & CC_TRACE)) 1718 else if (!(coro->cctx->flags & CC_TRACE))
1623 croak ("cannot enable tracing on coroutine with custom stack"); 1719 croak ("cannot enable tracing on coroutine with custom stack");
1624 1720
1625 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1721 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1626 } 1722 }
1633 else 1729 else
1634 coro->slot->runops = RUNOPS_DEFAULT; 1730 coro->slot->runops = RUNOPS_DEFAULT;
1635 } 1731 }
1636} 1732}
1637 1733
1734#if 0
1638static int 1735static int
1639coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) 1736coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1640{ 1737{
1641 AV *padlist; 1738 AV *padlist;
1642 AV *av = (AV *)mg->mg_obj; 1739 AV *av = (AV *)mg->mg_obj;
1648 1745
1649static MGVTBL coro_gensub_vtbl = { 1746static MGVTBL coro_gensub_vtbl = {
1650 0, 0, 0, 0, 1747 0, 0, 0, 0,
1651 coro_gensub_free 1748 coro_gensub_free
1652}; 1749};
1750#endif
1653 1751
1654/*****************************************************************************/ 1752/*****************************************************************************/
1655/* PerlIO::cede */ 1753/* PerlIO::cede */
1656 1754
1657typedef struct 1755typedef struct
1724 PerlIOBuf_get_ptr, 1822 PerlIOBuf_get_ptr,
1725 PerlIOBuf_get_cnt, 1823 PerlIOBuf_get_cnt,
1726 PerlIOBuf_set_ptrcnt, 1824 PerlIOBuf_set_ptrcnt,
1727}; 1825};
1728 1826
1827/*****************************************************************************/
1828
1829static const CV *ssl_cv; /* for quick consistency check */
1830
1831static UNOP ssl_restore; /* restore stack as entersub did, for first-re-run */
1832static SV *ssl_arg0;
1833static SV *ssl_arg1;
1834
1835/* this restores the stack in the case we patched the entersub, to */
1836/* recreate the stack frame as perl will on following calls */
1837/* since entersub cleared the stack */
1838static OP *
1839pp_restore (pTHX)
1840{
1841 dSP;
1842
1843 PUSHMARK (SP);
1844
1845 EXTEND (SP, 3);
1846 if (ssl_arg0) PUSHs (sv_2mortal (ssl_arg0)), ssl_arg0 = 0;
1847 if (ssl_arg1) PUSHs (sv_2mortal (ssl_arg1)), ssl_arg1 = 0;
1848 PUSHs ((SV *)CvGV (ssl_cv));
1849
1850 RETURNOP (ssl_restore.op_first);
1851}
1852
1853/* declare prototype */
1854XS(XS_Coro__State__set_stacklevel);
1855
1856static OP *
1857pp_set_stacklevel (pTHX)
1858{
1859 dSP;
1860 struct transfer_args ta;
1861 SV **arg = PL_stack_base + TOPMARK + 1;
1862 int items = SP - arg; /* args without function object */
1863
1864 /* do a quick consistency check on the "function" object, and if it isn't */
1865 /* for us, divert to the real entersub */
1866 if (SvTYPE (*sp) != SVt_PVGV || CvXSUB (GvCV (*sp)) != XS_Coro__State__set_stacklevel)
1867 return PL_ppaddr[OP_ENTERSUB](aTHX);
1868
1869 /* pop args */
1870 SP = PL_stack_base + POPMARK;
1871
1872 if (!(PL_op->op_flags & OPf_STACKED))
1873 {
1874 /* ampersand-form of call, use @_ instead of stack */
1875 AV *av = GvAV (PL_defgv);
1876 arg = AvARRAY (av);
1877 items = AvFILLp (av) + 1;
1878 }
1879
1880 PUTBACK;
1881 switch (PL_op->op_private & 7)
1882 {
1883 case 0:
1884 prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (arg [0]));
1885 break;
1886
1887 case 1:
1888 if (items != 2)
1889 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d.", items);
1890
1891 prepare_transfer (aTHX_ &ta, arg [0], arg [1]);
1892 break;
1893
1894 case 2:
1895 prepare_schedule (aTHX_ &ta);
1896 break;
1897
1898 case 3:
1899 prepare_cede (aTHX_ &ta);
1900 break;
1901
1902 case 4:
1903 if (!prepare_cede_notself (aTHX_ &ta))
1904 goto skip;
1905
1906 break;
1907 }
1908
1909 TRANSFER (ta, 0);
1910 SPAGAIN;
1911
1912skip:
1913 PUTBACK;
1914 SSL_TAIL;
1915 SPAGAIN;
1916 RETURN;
1917}
1729 1918
1730MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1919MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1731 1920
1732PROTOTYPES: DISABLE 1921PROTOTYPES: DISABLE
1733
1734BOOT:
1735{
1736#ifdef USE_ITHREADS
1737 MUTEX_INIT (&coro_mutex);
1738#endif
1739 BOOT_PAGESIZE;
1740
1741 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1742 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1743
1744 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1745 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1746 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr;
1747
1748 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1749 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1750 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1751
1752 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1753
1754 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1755 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1756 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1757 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1758
1759 main_mainstack = PL_mainstack;
1760 main_top_env = PL_top_env;
1761
1762 while (main_top_env->je_prev)
1763 main_top_env = main_top_env->je_prev;
1764
1765 coroapi.ver = CORO_API_VERSION;
1766 coroapi.rev = CORO_API_REVISION;
1767 coroapi.transfer = api_transfer;
1768
1769 {
1770 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1771
1772 if (!svp) croak ("Time::HiRes is required");
1773 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1774
1775 nvtime = INT2PTR (double (*)(), SvIV (*svp));
1776 }
1777
1778 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1779}
1780
1781SV *
1782new (char *klass, ...)
1783 CODE:
1784{
1785 struct coro *coro;
1786 MAGIC *mg;
1787 HV *hv;
1788 int i;
1789
1790 Newz (0, coro, 1, struct coro);
1791 coro->args = newAV ();
1792 coro->flags = CF_NEW;
1793
1794 if (coro_first) coro_first->prev = coro;
1795 coro->next = coro_first;
1796 coro_first = coro;
1797
1798 coro->hv = hv = newHV ();
1799 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
1800 mg->mg_flags |= MGf_DUP;
1801 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1802
1803 av_extend (coro->args, items - 1);
1804 for (i = 1; i < items; i++)
1805 av_push (coro->args, newSVsv (ST (i)));
1806}
1807 OUTPUT:
1808 RETVAL
1809 1922
1810# these not obviously related functions are all rolled into the same xs 1923# these not obviously related functions are all rolled into the same xs
1811# function to increase chances that they all will call transfer with the same 1924# function to increase chances that they all will call transfer with the same
1812# stack offset 1925# stack offset
1813void 1926void
1817 Coro::schedule = 2 1930 Coro::schedule = 2
1818 Coro::cede = 3 1931 Coro::cede = 3
1819 Coro::cede_notself = 4 1932 Coro::cede_notself = 4
1820 CODE: 1933 CODE:
1821{ 1934{
1822 struct transfer_args ta; 1935 assert (("FATAL: ssl call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_set_stacklevel));
1823 1936
1824 PUTBACK; 1937 /* we patch the op, and then re-run the whole call */
1825 switch (ix) 1938 /* we have to put some dummy argument on the stack for this to work */
1939 /* TODO: walk back the opcode chain (but how?), nuke the pp_gv etc. */
1940 ssl_restore.op_next = (OP *)&ssl_restore;
1941 ssl_restore.op_type = OP_NULL;
1942 ssl_restore.op_ppaddr = pp_restore;
1943 ssl_restore.op_first = PL_op;
1944
1945 ssl_arg0 = items > 0 ? SvREFCNT_inc (ST (0)) : 0;
1946 ssl_arg1 = items > 1 ? SvREFCNT_inc (ST (1)) : 0;
1947
1948 PL_op->op_ppaddr = pp_set_stacklevel;
1949 PL_op->op_private = PL_op->op_private & ~7 | ix; /* we potentially share our private flags with entersub */
1950
1951 PL_op = (OP *)&ssl_restore;
1952}
1953
1954BOOT:
1955{
1956#ifdef USE_ITHREADS
1957 MUTEX_INIT (&coro_lock);
1958# if CORO_PTHREAD
1959 coro_thx = PERL_GET_CONTEXT;
1960# endif
1961#endif
1962 BOOT_PAGESIZE;
1963
1964 ssl_cv = get_cv ("Coro::State::_set_stacklevel", 0);
1965
1966 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1967 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1968
1969 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1970 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1971 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr;
1972
1973 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1974 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1975 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1976
1977 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1978
1979 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1980 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1981 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1982 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1983
1984 main_mainstack = PL_mainstack;
1985 main_top_env = PL_top_env;
1986
1987 while (main_top_env->je_prev)
1988 main_top_env = main_top_env->je_prev;
1989
1990 coroapi.ver = CORO_API_VERSION;
1991 coroapi.rev = CORO_API_REVISION;
1992 coroapi.transfer = api_transfer;
1993
1826 { 1994 {
1827 case 0: 1995 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1828 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1829 ta.next = 0;
1830 break;
1831 1996
1832 case 1: 1997 if (!svp) croak ("Time::HiRes is required");
1833 if (items != 2) 1998 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1834 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1835 1999
1836 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 2000 nvtime = INT2PTR (double (*)(), SvIV (*svp));
1837 break;
1838
1839 case 2:
1840 prepare_schedule (aTHX_ &ta);
1841 break;
1842
1843 case 3:
1844 prepare_cede (aTHX_ &ta);
1845 break;
1846
1847 case 4:
1848 if (!prepare_cede_notself (aTHX_ &ta))
1849 XSRETURN_EMPTY;
1850
1851 break;
1852 } 2001 }
1853 SPAGAIN;
1854 2002
1855 BARRIER; 2003 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1856 PUTBACK;
1857 TRANSFER (ta, 0);
1858 SPAGAIN; /* might be the sp of a different coroutine now */
1859 /* be extra careful not to ever do anything after TRANSFER */
1860} 2004}
2005
2006SV *
2007new (char *klass, ...)
2008 CODE:
2009{
2010 struct coro *coro;
2011 MAGIC *mg;
2012 HV *hv;
2013 int i;
2014
2015 Newz (0, coro, 1, struct coro);
2016 coro->args = newAV ();
2017 coro->flags = CF_NEW;
2018
2019 if (coro_first) coro_first->prev = coro;
2020 coro->next = coro_first;
2021 coro_first = coro;
2022
2023 coro->hv = hv = newHV ();
2024 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
2025 mg->mg_flags |= MGf_DUP;
2026 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2027
2028 av_extend (coro->args, items - 1);
2029 for (i = 1; i < items; i++)
2030 av_push (coro->args, newSVsv (ST (i)));
2031}
2032 OUTPUT:
2033 RETVAL
1861 2034
1862bool 2035bool
1863_destroy (SV *coro_sv) 2036_destroy (SV *coro_sv)
1864 CODE: 2037 CODE:
1865 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 2038 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1873 _exit (code); 2046 _exit (code);
1874 2047
1875int 2048int
1876cctx_stacksize (int new_stacksize = 0) 2049cctx_stacksize (int new_stacksize = 0)
1877 CODE: 2050 CODE:
1878 RETVAL = coro_stacksize; 2051 RETVAL = cctx_stacksize;
1879 if (new_stacksize) 2052 if (new_stacksize)
2053 {
1880 coro_stacksize = new_stacksize; 2054 cctx_stacksize = new_stacksize;
2055 ++cctx_gen;
2056 }
2057 OUTPUT:
2058 RETVAL
2059
2060int
2061cctx_max_idle (int max_idle = 0)
2062 CODE:
2063 RETVAL = cctx_max_idle;
2064 if (max_idle > 1)
2065 cctx_max_idle = max_idle;
1881 OUTPUT: 2066 OUTPUT:
1882 RETVAL 2067 RETVAL
1883 2068
1884int 2069int
1885cctx_count () 2070cctx_count ()
1963 RETVAL = boolSV (coro->flags & ix); 2148 RETVAL = boolSV (coro->flags & ix);
1964 OUTPUT: 2149 OUTPUT:
1965 RETVAL 2150 RETVAL
1966 2151
1967void 2152void
2153throw (Coro::State self, SV *throw = &PL_sv_undef)
2154 PROTOTYPE: $;$
2155 CODE:
2156 SvREFCNT_dec (self->throw);
2157 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2158
2159void
1968api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 2160api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1969 2161
1970SV * 2162SV *
1971has_cctx (Coro::State coro) 2163has_cctx (Coro::State coro)
1972 PROTOTYPE: $ 2164 PROTOTYPE: $
1981 CODE: 2173 CODE:
1982 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; 2174 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1983 OUTPUT: 2175 OUTPUT:
1984 RETVAL 2176 RETVAL
1985 2177
1986IV 2178UV
1987rss (Coro::State coro) 2179rss (Coro::State coro)
1988 PROTOTYPE: $ 2180 PROTOTYPE: $
1989 ALIAS: 2181 ALIAS:
1990 usecount = 1 2182 usecount = 1
1991 CODE: 2183 CODE:
2002 CODE: 2194 CODE:
2003 struct coro *coro = SvSTATE (coro_current); 2195 struct coro *coro = SvSTATE (coro_current);
2004 coro->cctx->idle_sp = 0; 2196 coro->cctx->idle_sp = 0;
2005 2197
2006void 2198void
2007throw (Coro::State self, SV *throw = &PL_sv_undef)
2008 PROTOTYPE: $;$
2009 CODE:
2010 SvREFCNT_dec (self->throw);
2011 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2012
2013void
2014swap_defsv (Coro::State self) 2199swap_defsv (Coro::State self)
2015 PROTOTYPE: $ 2200 PROTOTYPE: $
2016 ALIAS: 2201 ALIAS:
2017 swap_defav = 1 2202 swap_defav = 1
2018 CODE: 2203 CODE:
2050 2235
2051 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 2236 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
2052 coro_ready[i] = newAV (); 2237 coro_ready[i] = newAV ();
2053 2238
2054 { 2239 {
2055 SV *sv = perl_get_sv ("Coro::API", TRUE); 2240 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2056 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
2057 2241
2058 coroapi.schedule = api_schedule; 2242 coroapi.schedule = api_schedule;
2059 coroapi.cede = api_cede; 2243 coroapi.cede = api_cede;
2060 coroapi.cede_notself = api_cede_notself; 2244 coroapi.cede_notself = api_cede_notself;
2061 coroapi.ready = api_ready; 2245 coroapi.ready = api_ready;
2173 sv_setsv (cb, &PL_sv_undef); 2357 sv_setsv (cb, &PL_sv_undef);
2174 2358
2175 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2359 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2176 coro->saved_deffh = 0; 2360 coro->saved_deffh = 0;
2177 2361
2178 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2362 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2179 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2363 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2180 { 2364 {
2181 SV *old = PL_diehook; 2365 SV *old = PL_diehook;
2182 PL_diehook = 0; 2366 PL_diehook = 0;
2183 SvREFCNT_dec (old); 2367 SvREFCNT_dec (old);
2329 2513
2330MODULE = Coro::State PACKAGE = PerlIO::cede 2514MODULE = Coro::State PACKAGE = PerlIO::cede
2331 2515
2332BOOT: 2516BOOT:
2333 PerlIO_define_layer (aTHX_ &PerlIO_cede); 2517 PerlIO_define_layer (aTHX_ &PerlIO_cede);
2518

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines