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.251 by root, Thu Oct 30 09:44:31 2008 UTC vs.
Revision 1.266 by root, Fri Nov 14 03:26:22 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) \
126 123
127#define IN_DESTRUCT (PL_main_cv == Nullcv) 124#define IN_DESTRUCT (PL_main_cv == Nullcv)
128 125
129#if __GNUC__ >= 3 126#if __GNUC__ >= 3
130# define attribute(x) __attribute__(x) 127# define attribute(x) __attribute__(x)
131# define BARRIER __asm__ __volatile__ ("" : : : "memory")
132# define expect(expr,value) __builtin_expect ((expr),(value)) 128# define expect(expr,value) __builtin_expect ((expr),(value))
129# define INLINE static inline
133#else 130#else
134# define attribute(x) 131# define attribute(x)
135# define BARRIER
136# define expect(expr,value) (expr) 132# define expect(expr,value) (expr)
133# define INLINE static
137#endif 134#endif
138 135
139#define expect_false(expr) expect ((expr) != 0, 0) 136#define expect_false(expr) expect ((expr) != 0, 0)
140#define expect_true(expr) expect ((expr) != 0, 1) 137#define expect_true(expr) expect ((expr) != 0, 1)
141 138
142#define NOINLINE attribute ((noinline)) 139#define NOINLINE attribute ((noinline))
143 140
144#include "CoroAPI.h" 141#include "CoroAPI.h"
145 142
146#ifdef USE_ITHREADS 143#ifdef USE_ITHREADS
144
147static perl_mutex coro_mutex; 145static perl_mutex coro_lock;
148# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 146# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0)
149# 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
150#else 152#else
153
151# define LOCK (void)0 154# define LOCK (void)0
152# define UNLOCK (void)0 155# define UNLOCK (void)0
156
153#endif 157#endif
158
159# undef LOCK
160# define LOCK (void)0
161# undef UNLOCK
162# define UNLOCK (void)0
154 163
155/* helper storage struct for Coro::AIO */ 164/* helper storage struct for Coro::AIO */
156struct io_state 165struct io_state
157{ 166{
158 AV *res; 167 AV *res;
159 int errorno; 168 int errorno;
160 I32 laststype; 169 I32 laststype; /* U16 in 5.10.0 */
161 int laststatval; 170 int laststatval;
162 Stat_t statcache; 171 Stat_t statcache;
163}; 172};
164 173
165static double (*nvtime)(); /* so why doesn't it take void? */ 174static double (*nvtime)(); /* so why doesn't it take void? */
166 175
176static U32 cctx_gen;
167static size_t coro_stacksize = CORO_STACKSIZE; 177static size_t cctx_stacksize = CORO_STACKSIZE;
168static struct CoroAPI coroapi; 178static struct CoroAPI coroapi;
169static AV *main_mainstack; /* used to differentiate between $main and others */ 179static AV *main_mainstack; /* used to differentiate between $main and others */
170static JMPENV *main_top_env; 180static JMPENV *main_top_env;
171static HV *coro_state_stash, *coro_stash; 181static HV *coro_state_stash, *coro_stash;
172static 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};
173 189
174static GV *irsgv; /* $/ */ 190static GV *irsgv; /* $/ */
175static GV *stdoutgv; /* *STDOUT */ 191static GV *stdoutgv; /* *STDOUT */
176static SV *rv_diehook; 192static SV *rv_diehook;
177static SV *rv_warnhook; 193static SV *rv_warnhook;
209 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 225 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
210 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 */
211 JMPENV *top_env; 227 JMPENV *top_env;
212 coro_context cctx; 228 coro_context cctx;
213 229
230 U32 gen;
214#if CORO_USE_VALGRIND 231#if CORO_USE_VALGRIND
215 int valgrind_id; 232 int valgrind_id;
216#endif 233#endif
217 unsigned char flags; 234 unsigned char flags;
218} coro_cctx; 235} coro_cctx;
278#define PRIO_MIN -4 295#define PRIO_MIN -4
279 296
280/* for Coro.pm */ 297/* for Coro.pm */
281static SV *coro_current; 298static SV *coro_current;
282static SV *coro_readyhook; 299static SV *coro_readyhook;
283static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 300static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
284static int coro_nready; 301static int coro_nready;
285static struct coro *coro_first; 302static struct coro *coro_first;
286 303
287/** lowlevel stuff **********************************************************/ 304/** lowlevel stuff **********************************************************/
288 305
383static MGVTBL coro_cv_vtbl = { 400static MGVTBL coro_cv_vtbl = {
384 0, 0, 0, 0, 401 0, 0, 0, 0,
385 coro_cv_free 402 coro_cv_free
386}; 403};
387 404
388#define CORO_MAGIC(sv,type) \ 405#define CORO_MAGIC(sv, type) \
389 SvMAGIC (sv) \ 406 SvMAGIC (sv) \
390 ? SvMAGIC (sv)->mg_type == type \ 407 ? SvMAGIC (sv)->mg_type == type \
391 ? SvMAGIC (sv) \ 408 ? SvMAGIC (sv) \
392 : mg_find (sv, type) \ 409 : mg_find (sv, type) \
393 : 0 410 : 0
394 411
395#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)
396#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)
397 414
398static struct coro * 415INLINE struct coro *
399SvSTATE_ (pTHX_ SV *coro) 416SvSTATE_ (pTHX_ SV *coro)
400{ 417{
401 HV *stash; 418 HV *stash;
402 MAGIC *mg; 419 MAGIC *mg;
403 420
628 645
629/* 646/*
630 * destroy the stacks, the callchain etc... 647 * destroy the stacks, the callchain etc...
631 */ 648 */
632static void 649static void
633coro_destroy_stacks (pTHX) 650coro_destruct_stacks (pTHX)
634{ 651{
635 while (PL_curstackinfo->si_next) 652 while (PL_curstackinfo->si_next)
636 PL_curstackinfo = PL_curstackinfo->si_next; 653 PL_curstackinfo = PL_curstackinfo->si_next;
637 654
638 while (PL_curstackinfo) 655 while (PL_curstackinfo)
692#endif 709#endif
693 } 710 }
694 } 711 }
695 712
696 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 SV **bot = SP;
728
729 int gimme = GIMME_V;
730
731 /* make sure we put something on the stack in scalar context */
732 if (gimme == G_SCALAR)
733 {
734 if (sp == bot)
735 XPUSHs (&PL_sv_undef);
736
737 SP = bot + 1;
738 }
739
740 PUTBACK;
697} 741}
698 742
699/** coroutine stack handling ************************************************/ 743/** coroutine stack handling ************************************************/
700 744
701static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); 745static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
820 PL_rs = newSVsv (GvSV (irsgv)); 864 PL_rs = newSVsv (GvSV (irsgv));
821 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 865 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
822 866
823 { 867 {
824 dSP; 868 dSP;
825 LOGOP myop; 869 UNOP myop;
826 870
827 Zero (&myop, 1, LOGOP); 871 Zero (&myop, 1, UNOP);
828 myop.op_next = Nullop; 872 myop.op_next = Nullop;
829 myop.op_flags = OPf_WANT_VOID; 873 myop.op_flags = OPf_WANT_VOID;
830 874
831 PUSHMARK (SP); 875 PUSHMARK (SP);
832 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 876 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
835 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 879 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
836 SPAGAIN; 880 SPAGAIN;
837 } 881 }
838 882
839 /* this newly created coroutine might be run on an existing cctx which most 883 /* this newly created coroutine might be run on an existing cctx which most
840 * likely was suspended in set_stacklevel, called from entersub. 884 * likely was suspended in set_stacklevel, called from pp_set_stacklevel,
841 * set_stacklevl doesn't do anything on return, but entersub does LEAVE, 885 * so we have to emulate entering pp_set_stacklevel here.
842 * so we ENTER here for symmetry
843 */ 886 */
844 ENTER; 887 SSL_HEAD;
845} 888}
846 889
847static void 890static void
848coro_destroy (pTHX_ struct coro *coro) 891coro_destruct (pTHX_ struct coro *coro)
849{ 892{
850 if (!IN_DESTRUCT) 893 if (!IN_DESTRUCT)
851 { 894 {
852 /* restore all saved variables and stuff */ 895 /* restore all saved variables and stuff */
853 LEAVE_SCOPE (0); 896 LEAVE_SCOPE (0);
875 SvREFCNT_dec (PL_warnhook); 918 SvREFCNT_dec (PL_warnhook);
876 919
877 SvREFCNT_dec (coro->saved_deffh); 920 SvREFCNT_dec (coro->saved_deffh);
878 SvREFCNT_dec (coro->throw); 921 SvREFCNT_dec (coro->throw);
879 922
880 coro_destroy_stacks (aTHX); 923 coro_destruct_stacks (aTHX);
881} 924}
882 925
883static void 926INLINE void
884free_coro_mortal (pTHX) 927free_coro_mortal (pTHX)
885{ 928{
886 if (expect_true (coro_mortal)) 929 if (expect_true (coro_mortal))
887 { 930 {
888 SvREFCNT_dec (coro_mortal); 931 SvREFCNT_dec (coro_mortal);
1012 1055
1013 TAINT_NOT; 1056 TAINT_NOT;
1014 return 0; 1057 return 0;
1015} 1058}
1016 1059
1060static void
1061prepare_set_stacklevel (struct transfer_args *ta, struct coro_cctx *cctx)
1062{
1063 ta->prev = (struct coro *)cctx;
1064 ta->next = 0;
1065}
1066
1017/* inject a fake call to Coro::State::_cctx_init into the execution */ 1067/* inject a fake call to Coro::State::_cctx_init into the execution */
1018/* _cctx_init should be careful, as it could be called at almost any time */ 1068/* _cctx_init should be careful, as it could be called at almost any time */
1019/* during execution of a perl program */ 1069/* during execution of a perl program */
1070/* also initialises PL_top_env */
1020static void NOINLINE 1071static void NOINLINE
1021cctx_prepare (pTHX_ coro_cctx *cctx) 1072cctx_prepare (pTHX_ coro_cctx *cctx)
1022{ 1073{
1023 dSP; 1074 dSP;
1024 LOGOP myop; 1075 UNOP myop;
1025 1076
1026 PL_top_env = &PL_start_env; 1077 PL_top_env = &PL_start_env;
1027 1078
1028 if (cctx->flags & CC_TRACE) 1079 if (cctx->flags & CC_TRACE)
1029 PL_runops = runops_trace; 1080 PL_runops = runops_trace;
1030 1081
1031 Zero (&myop, 1, LOGOP); 1082 Zero (&myop, 1, UNOP);
1032 myop.op_next = PL_op; 1083 myop.op_next = PL_op;
1033 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 1084 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
1034 1085
1035 PUSHMARK (SP); 1086 PUSHMARK (SP);
1036 EXTEND (SP, 2); 1087 EXTEND (SP, 2);
1037 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); 1088 PUSHs (sv_2mortal (newSViv ((IV)cctx)));
1038 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 1089 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
1039 PUTBACK; 1090 PUTBACK;
1040 PL_op = (OP *)&myop; 1091 PL_op = (OP *)&myop;
1041 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 1092 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1042 SPAGAIN; 1093 SPAGAIN;
1043} 1094}
1044 1095
1096/* the tail of transfer: execute stuff we can only do after a transfer */
1097INLINE void
1098transfer_tail (pTHX)
1099{
1100 struct coro *next = (struct coro *)transfer_next;
1101 assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */
1102 assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next));
1103
1104 free_coro_mortal (aTHX);
1105 UNLOCK;
1106
1107 if (expect_false (next->throw))
1108 {
1109 SV *exception = sv_2mortal (next->throw);
1110
1111 next->throw = 0;
1112 sv_setsv (ERRSV, exception);
1113 croak (0);
1114 }
1115}
1116
1045/* 1117/*
1046 * this is a _very_ stripped down perl interpreter ;) 1118 * this is a _very_ stripped down perl interpreter ;)
1047 */ 1119 */
1048static void 1120static void
1049cctx_run (void *arg) 1121cctx_run (void *arg)
1050{ 1122{
1123#ifdef USE_ITHREADS
1124# if CORO_PTHREAD
1125 PERL_SET_CONTEXT (coro_thx);
1126# endif
1127#endif
1128 {
1051 dTHX; 1129 dTHX;
1052 1130
1053 /* cctx_run is the alternative tail of transfer(), so unlock here. */ 1131 /* we are the alternative tail to pp_set_stacklevel */
1054 UNLOCK; 1132 /* so do the same things here */
1133 SSL_TAIL;
1055 1134
1056 /* we now skip the entersub that lead to transfer() */ 1135 /* we now skip the op that did lead to transfer() */
1057 PL_op = PL_op->op_next; 1136 PL_op = PL_op->op_next;
1058 1137
1059 /* inject a fake subroutine call to cctx_init */ 1138 /* inject a fake subroutine call to cctx_init */
1060 cctx_prepare (aTHX_ (coro_cctx *)arg); 1139 cctx_prepare (aTHX_ (coro_cctx *)arg);
1061 1140
1141 /* cctx_run is the alternative tail of transfer() */
1142 transfer_tail (aTHX);
1143
1062 /* somebody or something will hit me for both perl_run and PL_restartop */ 1144 /* somebody or something will hit me for both perl_run and PL_restartop */
1063 PL_restartop = PL_op; 1145 PL_restartop = PL_op;
1064 perl_run (PL_curinterp); 1146 perl_run (PL_curinterp);
1065 1147
1066 /* 1148 /*
1067 * If perl-run returns we assume exit() was being called or the coro 1149 * If perl-run returns we assume exit() was being called or the coro
1068 * fell off the end, which seems to be the only valid (non-bug) 1150 * fell off the end, which seems to be the only valid (non-bug)
1069 * reason for perl_run to return. We try to exit by jumping to the 1151 * reason for perl_run to return. We try to exit by jumping to the
1070 * bootstrap-time "top" top_env, as we cannot restore the "main" 1152 * bootstrap-time "top" top_env, as we cannot restore the "main"
1071 * coroutine as Coro has no such concept 1153 * coroutine as Coro has no such concept
1072 */ 1154 */
1073 PL_top_env = main_top_env; 1155 PL_top_env = main_top_env;
1074 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1156 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1157 }
1075} 1158}
1076 1159
1077static coro_cctx * 1160static coro_cctx *
1078cctx_new () 1161cctx_new ()
1079{ 1162{
1080 coro_cctx *cctx; 1163 coro_cctx *cctx;
1164
1165 ++cctx_count;
1166 New (0, cctx, 1, coro_cctx);
1167
1168 cctx->gen = cctx_gen;
1169 cctx->flags = 0;
1170 cctx->idle_sp = 0; /* can be accessed by transfer between cctx_run and set_stacklevel, on throw */
1171
1172 return cctx;
1173}
1174
1175/* create a new cctx only suitable as source */
1176static coro_cctx *
1177cctx_new_empty ()
1178{
1179 coro_cctx *cctx = cctx_new ();
1180
1181 cctx->sptr = 0;
1182 coro_create (&cctx->cctx, 0, 0, 0, 0);
1183
1184 return cctx;
1185}
1186
1187/* create a new cctx suitable as destination/running a perl interpreter */
1188static coro_cctx *
1189cctx_new_run ()
1190{
1191 coro_cctx *cctx = cctx_new ();
1081 void *stack_start; 1192 void *stack_start;
1082 size_t stack_size; 1193 size_t stack_size;
1083 1194
1084 ++cctx_count;
1085
1086 Newz (0, cctx, 1, coro_cctx);
1087
1088#if HAVE_MMAP 1195#if HAVE_MMAP
1089 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1196 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1090 /* mmap supposedly does allocate-on-write for us */ 1197 /* mmap supposedly does allocate-on-write for us */
1091 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1198 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
1092 1199
1093 if (cctx->sptr != (void *)-1) 1200 if (cctx->sptr != (void *)-1)
1094 { 1201 {
1095# if CORO_STACKGUARD 1202 #if CORO_STACKGUARD
1096 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1203 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1097# endif 1204 #endif
1098 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 1205 stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE;
1099 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 1206 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
1100 cctx->flags |= CC_MAPPED; 1207 cctx->flags |= CC_MAPPED;
1101 } 1208 }
1102 else 1209 else
1103#endif 1210#endif
1104 { 1211 {
1105 cctx->ssize = coro_stacksize * (long)sizeof (long); 1212 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1106 New (0, cctx->sptr, coro_stacksize, long); 1213 New (0, cctx->sptr, cctx_stacksize, long);
1107 1214
1108 if (!cctx->sptr) 1215 if (!cctx->sptr)
1109 { 1216 {
1110 perror ("FATAL: unable to allocate stack for coroutine"); 1217 perror ("FATAL: unable to allocate stack for coroutine, exiting.");
1111 _exit (EXIT_FAILURE); 1218 _exit (EXIT_FAILURE);
1112 } 1219 }
1113 1220
1114 stack_start = cctx->sptr; 1221 stack_start = cctx->sptr;
1115 stack_size = cctx->ssize; 1222 stack_size = cctx->ssize;
1116 } 1223 }
1117 1224
1118 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); 1225 #if CORO_USE_VALGRIND
1226 cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size);
1227 #endif
1228
1119 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); 1229 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size);
1120 1230
1121 return cctx; 1231 return cctx;
1122} 1232}
1123 1233
1126{ 1236{
1127 if (!cctx) 1237 if (!cctx)
1128 return; 1238 return;
1129 1239
1130 --cctx_count; 1240 --cctx_count;
1241 coro_destroy (&cctx->cctx);
1131 1242
1243 /* coro_transfer creates new, empty cctx's */
1244 if (cctx->sptr)
1245 {
1132#if CORO_USE_VALGRIND 1246 #if CORO_USE_VALGRIND
1133 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1247 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1134#endif 1248 #endif
1135 1249
1136#if HAVE_MMAP 1250#if HAVE_MMAP
1137 if (cctx->flags & CC_MAPPED) 1251 if (cctx->flags & CC_MAPPED)
1138 munmap (cctx->sptr, cctx->ssize); 1252 munmap (cctx->sptr, cctx->ssize);
1139 else 1253 else
1140#endif 1254#endif
1141 Safefree (cctx->sptr); 1255 Safefree (cctx->sptr);
1256 }
1142 1257
1143 Safefree (cctx); 1258 Safefree (cctx);
1144} 1259}
1145 1260
1146/* wether this cctx should be destructed */ 1261/* wether this cctx should be destructed */
1147#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) 1262#define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE))
1148 1263
1149static coro_cctx * 1264static coro_cctx *
1150cctx_get (pTHX) 1265cctx_get (pTHX)
1151{ 1266{
1152 while (expect_true (cctx_first)) 1267 while (expect_true (cctx_first))
1159 return cctx; 1274 return cctx;
1160 1275
1161 cctx_destroy (cctx); 1276 cctx_destroy (cctx);
1162 } 1277 }
1163 1278
1164 return cctx_new (); 1279 return cctx_new_run ();
1165} 1280}
1166 1281
1167static void 1282static void
1168cctx_put (coro_cctx *cctx) 1283cctx_put (coro_cctx *cctx)
1169{ 1284{
1285 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr));
1286
1170 /* free another cctx if overlimit */ 1287 /* free another cctx if overlimit */
1171 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1288 if (expect_false (cctx_idle >= cctx_max_idle))
1172 { 1289 {
1173 coro_cctx *first = cctx_first; 1290 coro_cctx *first = cctx_first;
1174 cctx_first = first->next; 1291 cctx_first = first->next;
1175 --cctx_idle; 1292 --cctx_idle;
1176 1293
1208/* always use the TRANSFER macro */ 1325/* always use the TRANSFER macro */
1209static void NOINLINE 1326static void NOINLINE
1210transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1327transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1211{ 1328{
1212 dSTACKLEVEL; 1329 dSTACKLEVEL;
1213 static volatile int has_throw;
1214 1330
1215 /* sometimes transfer is only called to set idle_sp */ 1331 /* sometimes transfer is only called to set idle_sp */
1216 if (expect_false (!next)) 1332 if (expect_false (!next))
1217 { 1333 {
1218 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1334 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1222 { 1338 {
1223 coro_cctx *prev__cctx; 1339 coro_cctx *prev__cctx;
1224 1340
1225 if (expect_false (prev->flags & CF_NEW)) 1341 if (expect_false (prev->flags & CF_NEW))
1226 { 1342 {
1227 /* create a new empty context */ 1343 /* create a new empty/source context */
1228 Newz (0, prev->cctx, 1, coro_cctx); 1344 prev->cctx = cctx_new_empty ();
1229 prev->flags &= ~CF_NEW; 1345 prev->flags &= ~CF_NEW;
1230 prev->flags |= CF_RUNNING; 1346 prev->flags |= CF_RUNNING;
1231 } 1347 }
1232 1348
1233 prev->flags &= ~CF_RUNNING; 1349 prev->flags &= ~CF_RUNNING;
1248 else 1364 else
1249 load_perl (aTHX_ next); 1365 load_perl (aTHX_ next);
1250 1366
1251 prev__cctx = prev->cctx; 1367 prev__cctx = prev->cctx;
1252 1368
1253 /* possibly "free" the cctx */ 1369 /* possibly untie and reuse the cctx */
1254 if (expect_true ( 1370 if (expect_true (
1255 prev__cctx->idle_sp == STACKLEVEL 1371 prev__cctx->idle_sp == STACKLEVEL
1256 && !(prev__cctx->flags & CC_TRACE) 1372 && !(prev__cctx->flags & CC_TRACE)
1257 && !force_cctx 1373 && !force_cctx
1258 )) 1374 ))
1259 { 1375 {
1260 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1376 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
1261 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 1377 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te));
1262 1378
1263 prev->cctx = 0; 1379 prev->cctx = 0;
1264 1380
1265 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1381 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1266 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1382 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1274 ++next->usecount; 1390 ++next->usecount;
1275 1391
1276 if (expect_true (!next->cctx)) 1392 if (expect_true (!next->cctx))
1277 next->cctx = cctx_get (aTHX); 1393 next->cctx = cctx_get (aTHX);
1278 1394
1279 has_throw = !!next->throw; 1395 assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next));
1396 transfer_next = next;
1280 1397
1281 if (expect_false (prev__cctx != next->cctx)) 1398 if (expect_false (prev__cctx != next->cctx))
1282 { 1399 {
1283 prev__cctx->top_env = PL_top_env; 1400 prev__cctx->top_env = PL_top_env;
1284 PL_top_env = next->cctx->top_env; 1401 PL_top_env = next->cctx->top_env;
1285 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1402 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
1286 } 1403 }
1287 1404
1288 free_coro_mortal (aTHX); 1405 transfer_tail (aTHX);
1289 UNLOCK;
1290
1291 if (expect_false (has_throw))
1292 {
1293 struct coro *coro = SvSTATE (coro_current);
1294
1295 if (coro->throw)
1296 {
1297 SV *exception = coro->throw;
1298 coro->throw = 0;
1299 sv_setsv (ERRSV, exception);
1300 croak (0);
1301 }
1302 }
1303 } 1406 }
1304} 1407}
1305
1306struct transfer_args
1307{
1308 struct coro *prev, *next;
1309};
1310 1408
1311#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) 1409#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx))
1312#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) 1410#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1313 1411
1314/** high level stuff ********************************************************/ 1412/** high level stuff ********************************************************/
1340 croak ("FATAL: tried to destroy currently running coroutine"); 1438 croak ("FATAL: tried to destroy currently running coroutine");
1341 1439
1342 save_perl (aTHX_ &temp); 1440 save_perl (aTHX_ &temp);
1343 load_perl (aTHX_ coro); 1441 load_perl (aTHX_ coro);
1344 1442
1345 coro_destroy (aTHX_ coro); 1443 coro_destruct (aTHX_ coro);
1346 1444
1347 load_perl (aTHX_ &temp); 1445 load_perl (aTHX_ &temp);
1348 1446
1349 coro->slot = 0; 1447 coro->slot = 0;
1350 } 1448 }
1487 1585
1488static int 1586static int
1489api_is_ready (SV *coro_sv) 1587api_is_ready (SV *coro_sv)
1490{ 1588{
1491 dTHX; 1589 dTHX;
1590
1492 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1591 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1493} 1592}
1494 1593
1495static void 1594INLINE void
1496prepare_schedule (pTHX_ struct transfer_args *ta) 1595prepare_schedule (pTHX_ struct transfer_args *ta)
1497{ 1596{
1498 SV *prev_sv, *next_sv; 1597 SV *prev_sv, *next_sv;
1499 1598
1500 for (;;) 1599 for (;;)
1526 /* cannot transfer to destroyed coros, skip and look for next */ 1625 /* cannot transfer to destroyed coros, skip and look for next */
1527 if (expect_false (ta->next->flags & CF_DESTROYED)) 1626 if (expect_false (ta->next->flags & CF_DESTROYED))
1528 { 1627 {
1529 UNLOCK; 1628 UNLOCK;
1530 SvREFCNT_dec (next_sv); 1629 SvREFCNT_dec (next_sv);
1531 /* coro_nready is already taken care of by destroy */ 1630 /* coro_nready has already been taken care of by destroy */
1532 continue; 1631 continue;
1533 } 1632 }
1534 1633
1535 --coro_nready; 1634 --coro_nready;
1536 UNLOCK; 1635 UNLOCK;
1539 1638
1540 /* free this only after the transfer */ 1639 /* free this only after the transfer */
1541 prev_sv = SvRV (coro_current); 1640 prev_sv = SvRV (coro_current);
1542 ta->prev = SvSTATE (prev_sv); 1641 ta->prev = SvSTATE (prev_sv);
1543 TRANSFER_CHECK (*ta); 1642 TRANSFER_CHECK (*ta);
1544 assert (ta->next->flags & CF_READY); 1643 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY));
1545 ta->next->flags &= ~CF_READY; 1644 ta->next->flags &= ~CF_READY;
1546 SvRV_set (coro_current, next_sv); 1645 SvRV_set (coro_current, next_sv);
1547 1646
1548 LOCK; 1647 LOCK;
1549 free_coro_mortal (aTHX); 1648 free_coro_mortal (aTHX);
1550 coro_mortal = prev_sv; 1649 coro_mortal = prev_sv;
1551 UNLOCK; 1650 UNLOCK;
1552} 1651}
1553 1652
1554static void 1653INLINE void
1555prepare_cede (pTHX_ struct transfer_args *ta) 1654prepare_cede (pTHX_ struct transfer_args *ta)
1556{ 1655{
1557 api_ready (coro_current); 1656 api_ready (coro_current);
1558 prepare_schedule (aTHX_ ta); 1657 prepare_schedule (aTHX_ ta);
1559} 1658}
1621 struct coro *coro = SvSTATE (coro_sv); 1720 struct coro *coro = SvSTATE (coro_sv);
1622 1721
1623 if (flags & CC_TRACE) 1722 if (flags & CC_TRACE)
1624 { 1723 {
1625 if (!coro->cctx) 1724 if (!coro->cctx)
1626 coro->cctx = cctx_new (); 1725 coro->cctx = cctx_new_run ();
1627 else if (!(coro->cctx->flags & CC_TRACE)) 1726 else if (!(coro->cctx->flags & CC_TRACE))
1628 croak ("cannot enable tracing on coroutine with custom stack"); 1727 croak ("cannot enable tracing on coroutine with custom stack");
1629 1728
1630 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1729 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1631 } 1730 }
1638 else 1737 else
1639 coro->slot->runops = RUNOPS_DEFAULT; 1738 coro->slot->runops = RUNOPS_DEFAULT;
1640 } 1739 }
1641} 1740}
1642 1741
1742#if 0
1643static int 1743static int
1644coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) 1744coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1645{ 1745{
1646 AV *padlist; 1746 AV *padlist;
1647 AV *av = (AV *)mg->mg_obj; 1747 AV *av = (AV *)mg->mg_obj;
1653 1753
1654static MGVTBL coro_gensub_vtbl = { 1754static MGVTBL coro_gensub_vtbl = {
1655 0, 0, 0, 0, 1755 0, 0, 0, 0,
1656 coro_gensub_free 1756 coro_gensub_free
1657}; 1757};
1758#endif
1658 1759
1659/*****************************************************************************/ 1760/*****************************************************************************/
1660/* PerlIO::cede */ 1761/* PerlIO::cede */
1661 1762
1662typedef struct 1763typedef struct
1729 PerlIOBuf_get_ptr, 1830 PerlIOBuf_get_ptr,
1730 PerlIOBuf_get_cnt, 1831 PerlIOBuf_get_cnt,
1731 PerlIOBuf_set_ptrcnt, 1832 PerlIOBuf_set_ptrcnt,
1732}; 1833};
1733 1834
1835/*****************************************************************************/
1836
1837static const CV *ssl_cv; /* for quick consistency check */
1838
1839static UNOP ssl_restore; /* restore stack as entersub did, for first-re-run */
1840static SV *ssl_arg0;
1841static SV *ssl_arg1;
1842
1843/* this restores the stack in the case we patched the entersub, to */
1844/* recreate the stack frame as perl will on following calls */
1845/* since entersub cleared the stack */
1846static OP *
1847pp_restore (pTHX)
1848{
1849 dSP;
1850
1851 PUSHMARK (SP);
1852
1853 EXTEND (SP, 3);
1854 if (ssl_arg0) PUSHs (sv_2mortal (ssl_arg0)), ssl_arg0 = 0;
1855 if (ssl_arg1) PUSHs (sv_2mortal (ssl_arg1)), ssl_arg1 = 0;
1856 PUSHs ((SV *)CvGV (ssl_cv));
1857
1858 RETURNOP (ssl_restore.op_first);
1859}
1860
1861/* declare prototype */
1862XS(XS_Coro__State__set_stacklevel);
1863
1864#define OPpENTERSUB_SSL 15
1865
1866static OP *
1867pp_set_stacklevel (pTHX)
1868{
1869 dSP;
1870 struct transfer_args ta;
1871 SV **arg = PL_stack_base + TOPMARK + 1;
1872 int items = SP - arg; /* args without function object */
1873
1874 /* do a quick consistency check on the "function" object, and if it isn't */
1875 /* for us, divert to the real entersub */
1876 if (SvTYPE (*sp) != SVt_PVGV || CvXSUB (GvCV (*sp)) != XS_Coro__State__set_stacklevel)
1877 return PL_ppaddr[OP_ENTERSUB](aTHX);
1878
1879 /* pop args */
1880 SP = PL_stack_base + POPMARK;
1881
1882 if (!(PL_op->op_flags & OPf_STACKED))
1883 {
1884 /* ampersand-form of call, use @_ instead of stack */
1885 AV *av = GvAV (PL_defgv);
1886 arg = AvARRAY (av);
1887 items = AvFILLp (av) + 1;
1888 }
1889
1890 PUTBACK;
1891 switch (PL_op->op_private & OPpENTERSUB_SSL)
1892 {
1893 case 0:
1894 prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (arg [0]));
1895 break;
1896
1897 case 1:
1898 if (items != 2)
1899 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d.", items);
1900
1901 prepare_transfer (aTHX_ &ta, arg [0], arg [1]);
1902 break;
1903
1904 case 2:
1905 prepare_schedule (aTHX_ &ta);
1906 break;
1907
1908 case 3:
1909 prepare_cede (aTHX_ &ta);
1910 break;
1911
1912 case 4:
1913 if (!prepare_cede_notself (aTHX_ &ta))
1914 goto skip;
1915
1916 break;
1917 }
1918
1919 TRANSFER (ta, 0);
1920 SPAGAIN;
1921
1922skip:
1923 PUTBACK;
1924 SSL_TAIL;
1925 SPAGAIN;
1926 RETURN;
1927}
1734 1928
1735MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1929MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1736 1930
1737PROTOTYPES: DISABLE 1931PROTOTYPES: DISABLE
1738
1739BOOT:
1740{
1741#ifdef USE_ITHREADS
1742 MUTEX_INIT (&coro_mutex);
1743#endif
1744 BOOT_PAGESIZE;
1745
1746 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1747 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1748
1749 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1750 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1751 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr;
1752
1753 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1754 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1755 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1756
1757 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1758
1759 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1760 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1761 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1762 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1763
1764 main_mainstack = PL_mainstack;
1765 main_top_env = PL_top_env;
1766
1767 while (main_top_env->je_prev)
1768 main_top_env = main_top_env->je_prev;
1769
1770 coroapi.ver = CORO_API_VERSION;
1771 coroapi.rev = CORO_API_REVISION;
1772 coroapi.transfer = api_transfer;
1773
1774 {
1775 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1776
1777 if (!svp) croak ("Time::HiRes is required");
1778 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1779
1780 nvtime = INT2PTR (double (*)(), SvIV (*svp));
1781 }
1782
1783 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1784}
1785
1786SV *
1787new (char *klass, ...)
1788 CODE:
1789{
1790 struct coro *coro;
1791 MAGIC *mg;
1792 HV *hv;
1793 int i;
1794
1795 Newz (0, coro, 1, struct coro);
1796 coro->args = newAV ();
1797 coro->flags = CF_NEW;
1798
1799 if (coro_first) coro_first->prev = coro;
1800 coro->next = coro_first;
1801 coro_first = coro;
1802
1803 coro->hv = hv = newHV ();
1804 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
1805 mg->mg_flags |= MGf_DUP;
1806 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1807
1808 av_extend (coro->args, items - 1);
1809 for (i = 1; i < items; i++)
1810 av_push (coro->args, newSVsv (ST (i)));
1811}
1812 OUTPUT:
1813 RETVAL
1814 1932
1815# these not obviously related functions are all rolled into the same xs 1933# these not obviously related functions are all rolled into the same xs
1816# function to increase chances that they all will call transfer with the same 1934# function to increase chances that they all will call transfer with the same
1817# stack offset 1935# stack offset
1818void 1936void
1822 Coro::schedule = 2 1940 Coro::schedule = 2
1823 Coro::cede = 3 1941 Coro::cede = 3
1824 Coro::cede_notself = 4 1942 Coro::cede_notself = 4
1825 CODE: 1943 CODE:
1826{ 1944{
1827 struct transfer_args ta; 1945 assert (("FATAL: ssl call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_set_stacklevel));
1828 1946
1829 PUTBACK; 1947 assert (("FATAL: ssl call with illegal CV value", CvGV (cv)));
1830 switch (ix) 1948 ssl_cv = cv;
1949
1950 /* we patch the op, and then re-run the whole call */
1951 /* we have to put some dummy argument on the stack for this to work */
1952 ssl_restore.op_next = (OP *)&ssl_restore;
1953 ssl_restore.op_type = OP_NULL;
1954 ssl_restore.op_ppaddr = pp_restore;
1955 ssl_restore.op_first = PL_op;
1956
1957 ssl_arg0 = items > 0 ? SvREFCNT_inc (ST (0)) : 0;
1958 ssl_arg1 = items > 1 ? SvREFCNT_inc (ST (1)) : 0;
1959
1960 PL_op->op_ppaddr = pp_set_stacklevel;
1961 PL_op->op_private = PL_op->op_private & ~OPpENTERSUB_SSL | ix; /* we potentially share our private flags with entersub */
1962
1963 PL_op = (OP *)&ssl_restore;
1964}
1965
1966BOOT:
1967{
1968#ifdef USE_ITHREADS
1969 MUTEX_INIT (&coro_lock);
1970# if CORO_PTHREAD
1971 coro_thx = PERL_GET_CONTEXT;
1972# endif
1973#endif
1974 BOOT_PAGESIZE;
1975
1976 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1977 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1978
1979 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1980 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1981 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr;
1982
1983 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1984 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1985 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1986
1987 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1988
1989 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1990 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1991 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1992 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1993
1994 main_mainstack = PL_mainstack;
1995 main_top_env = PL_top_env;
1996
1997 while (main_top_env->je_prev)
1998 main_top_env = main_top_env->je_prev;
1999
2000 coroapi.ver = CORO_API_VERSION;
2001 coroapi.rev = CORO_API_REVISION;
2002 coroapi.transfer = api_transfer;
2003
1831 { 2004 {
1832 case 0: 2005 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1833 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1834 ta.next = 0;
1835 break;
1836 2006
1837 case 1: 2007 if (!svp) croak ("Time::HiRes is required");
1838 if (items != 2) 2008 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1839 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1840 2009
1841 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 2010 nvtime = INT2PTR (double (*)(), SvIV (*svp));
1842 break;
1843
1844 case 2:
1845 prepare_schedule (aTHX_ &ta);
1846 break;
1847
1848 case 3:
1849 prepare_cede (aTHX_ &ta);
1850 break;
1851
1852 case 4:
1853 if (!prepare_cede_notself (aTHX_ &ta))
1854 XSRETURN_EMPTY;
1855
1856 break;
1857 } 2011 }
1858 SPAGAIN;
1859 2012
1860 BARRIER; 2013 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1861 PUTBACK;
1862 TRANSFER (ta, 0);
1863 SPAGAIN; /* might be the sp of a different coroutine now */
1864 /* be extra careful not to ever do anything after TRANSFER */
1865} 2014}
2015
2016SV *
2017new (char *klass, ...)
2018 CODE:
2019{
2020 struct coro *coro;
2021 MAGIC *mg;
2022 HV *hv;
2023 int i;
2024
2025 Newz (0, coro, 1, struct coro);
2026 coro->args = newAV ();
2027 coro->flags = CF_NEW;
2028
2029 if (coro_first) coro_first->prev = coro;
2030 coro->next = coro_first;
2031 coro_first = coro;
2032
2033 coro->hv = hv = newHV ();
2034 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
2035 mg->mg_flags |= MGf_DUP;
2036 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2037
2038 av_extend (coro->args, items - 1);
2039 for (i = 1; i < items; i++)
2040 av_push (coro->args, newSVsv (ST (i)));
2041}
2042 OUTPUT:
2043 RETVAL
1866 2044
1867bool 2045bool
1868_destroy (SV *coro_sv) 2046_destroy (SV *coro_sv)
1869 CODE: 2047 CODE:
1870 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 2048 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1878 _exit (code); 2056 _exit (code);
1879 2057
1880int 2058int
1881cctx_stacksize (int new_stacksize = 0) 2059cctx_stacksize (int new_stacksize = 0)
1882 CODE: 2060 CODE:
1883 RETVAL = coro_stacksize; 2061 RETVAL = cctx_stacksize;
1884 if (new_stacksize) 2062 if (new_stacksize)
2063 {
1885 coro_stacksize = new_stacksize; 2064 cctx_stacksize = new_stacksize;
2065 ++cctx_gen;
2066 }
2067 OUTPUT:
2068 RETVAL
2069
2070int
2071cctx_max_idle (int max_idle = 0)
2072 CODE:
2073 RETVAL = cctx_max_idle;
2074 if (max_idle > 1)
2075 cctx_max_idle = max_idle;
1886 OUTPUT: 2076 OUTPUT:
1887 RETVAL 2077 RETVAL
1888 2078
1889int 2079int
1890cctx_count () 2080cctx_count ()
1968 RETVAL = boolSV (coro->flags & ix); 2158 RETVAL = boolSV (coro->flags & ix);
1969 OUTPUT: 2159 OUTPUT:
1970 RETVAL 2160 RETVAL
1971 2161
1972void 2162void
2163throw (Coro::State self, SV *throw = &PL_sv_undef)
2164 PROTOTYPE: $;$
2165 CODE:
2166 SvREFCNT_dec (self->throw);
2167 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2168
2169void
1973api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 2170api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1974 2171
1975SV * 2172SV *
1976has_cctx (Coro::State coro) 2173has_cctx (Coro::State coro)
1977 PROTOTYPE: $ 2174 PROTOTYPE: $
1986 CODE: 2183 CODE:
1987 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; 2184 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1988 OUTPUT: 2185 OUTPUT:
1989 RETVAL 2186 RETVAL
1990 2187
1991IV 2188UV
1992rss (Coro::State coro) 2189rss (Coro::State coro)
1993 PROTOTYPE: $ 2190 PROTOTYPE: $
1994 ALIAS: 2191 ALIAS:
1995 usecount = 1 2192 usecount = 1
1996 CODE: 2193 CODE:
2007 CODE: 2204 CODE:
2008 struct coro *coro = SvSTATE (coro_current); 2205 struct coro *coro = SvSTATE (coro_current);
2009 coro->cctx->idle_sp = 0; 2206 coro->cctx->idle_sp = 0;
2010 2207
2011void 2208void
2012throw (Coro::State self, SV *throw = &PL_sv_undef)
2013 PROTOTYPE: $;$
2014 CODE:
2015 SvREFCNT_dec (self->throw);
2016 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2017
2018void
2019swap_defsv (Coro::State self) 2209swap_defsv (Coro::State self)
2020 PROTOTYPE: $ 2210 PROTOTYPE: $
2021 ALIAS: 2211 ALIAS:
2022 swap_defav = 1 2212 swap_defav = 1
2023 CODE: 2213 CODE:
2055 2245
2056 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 2246 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
2057 coro_ready[i] = newAV (); 2247 coro_ready[i] = newAV ();
2058 2248
2059 { 2249 {
2060 SV *sv = perl_get_sv ("Coro::API", TRUE); 2250 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2061 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
2062 2251
2063 coroapi.schedule = api_schedule; 2252 coroapi.schedule = api_schedule;
2064 coroapi.cede = api_cede; 2253 coroapi.cede = api_cede;
2065 coroapi.cede_notself = api_cede_notself; 2254 coroapi.cede_notself = api_cede_notself;
2066 coroapi.ready = api_ready; 2255 coroapi.ready = api_ready;
2178 sv_setsv (cb, &PL_sv_undef); 2367 sv_setsv (cb, &PL_sv_undef);
2179 2368
2180 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2369 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2181 coro->saved_deffh = 0; 2370 coro->saved_deffh = 0;
2182 2371
2183 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2372 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2184 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2373 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2185 { 2374 {
2186 SV *old = PL_diehook; 2375 SV *old = PL_diehook;
2187 PL_diehook = 0; 2376 PL_diehook = 0;
2188 SvREFCNT_dec (old); 2377 SvREFCNT_dec (old);
2334 2523
2335MODULE = Coro::State PACKAGE = PerlIO::cede 2524MODULE = Coro::State PACKAGE = PerlIO::cede
2336 2525
2337BOOT: 2526BOOT:
2338 PerlIO_define_layer (aTHX_ &PerlIO_cede); 2527 PerlIO_define_layer (aTHX_ &PerlIO_cede);
2528

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines