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.246 by root, Wed Sep 24 21:20:05 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) \
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{
167 AV *res;
153 int errorno; 168 int errorno;
154 I32 laststype; 169 I32 laststype; /* U16 in 5.10.0 */
155 int laststatval; 170 int laststatval;
156 Stat_t statcache; 171 Stat_t statcache;
157}; 172};
158 173
159static double (*nvtime)(); /* so why doesn't it take void? */ 174static double (*nvtime)(); /* so why doesn't it take void? */
160 175
176static U32 cctx_gen;
161static size_t coro_stacksize = CORO_STACKSIZE; 177static size_t cctx_stacksize = CORO_STACKSIZE;
162static struct CoroAPI coroapi; 178static struct CoroAPI coroapi;
163static AV *main_mainstack; /* used to differentiate between $main and others */ 179static AV *main_mainstack; /* used to differentiate between $main and others */
164static JMPENV *main_top_env; 180static JMPENV *main_top_env;
165static HV *coro_state_stash, *coro_stash; 181static HV *coro_state_stash, *coro_stash;
166static 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};
167 189
168static GV *irsgv; /* $/ */ 190static GV *irsgv; /* $/ */
169static GV *stdoutgv; /* *STDOUT */ 191static GV *stdoutgv; /* *STDOUT */
170static SV *rv_diehook; 192static SV *rv_diehook;
171static SV *rv_warnhook; 193static SV *rv_warnhook;
203 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 225 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 */ 226 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
205 JMPENV *top_env; 227 JMPENV *top_env;
206 coro_context cctx; 228 coro_context cctx;
207 229
230 U32 gen;
208#if CORO_USE_VALGRIND 231#if CORO_USE_VALGRIND
209 int valgrind_id; 232 int valgrind_id;
210#endif 233#endif
211 unsigned char flags; 234 unsigned char flags;
212} coro_cctx; 235} coro_cctx;
272#define PRIO_MIN -4 295#define PRIO_MIN -4
273 296
274/* for Coro.pm */ 297/* for Coro.pm */
275static SV *coro_current; 298static SV *coro_current;
276static SV *coro_readyhook; 299static SV *coro_readyhook;
277static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 300static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
278static int coro_nready; 301static int coro_nready;
279static struct coro *coro_first; 302static struct coro *coro_first;
280 303
281/** lowlevel stuff **********************************************************/ 304/** lowlevel stuff **********************************************************/
282 305
324 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 347 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
325#endif 348#endif
326 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 349 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
327 --AvFILLp (padlist); 350 --AvFILLp (padlist);
328 351
329 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 352 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE)));
330 av_store (newpadlist, 1, (SV *)newpad); 353 av_store (newpadlist, 1, (SV *)newpad);
331 354
332 return newpadlist; 355 return newpadlist;
333} 356}
334 357
377static MGVTBL coro_cv_vtbl = { 400static MGVTBL coro_cv_vtbl = {
378 0, 0, 0, 0, 401 0, 0, 0, 0,
379 coro_cv_free 402 coro_cv_free
380}; 403};
381 404
382#define CORO_MAGIC(sv,type) \ 405#define CORO_MAGIC(sv, type) \
383 SvMAGIC (sv) \ 406 SvMAGIC (sv) \
384 ? SvMAGIC (sv)->mg_type == type \ 407 ? SvMAGIC (sv)->mg_type == type \
385 ? SvMAGIC (sv) \ 408 ? SvMAGIC (sv) \
386 : mg_find (sv, type) \ 409 : mg_find (sv, type) \
387 : 0 410 : 0
388 411
389#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)
390#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)
391 414
392static struct coro * 415INLINE struct coro *
393SvSTATE_ (pTHX_ SV *coro) 416SvSTATE_ (pTHX_ SV *coro)
394{ 417{
395 HV *stash; 418 HV *stash;
396 MAGIC *mg; 419 MAGIC *mg;
397 420
622 645
623/* 646/*
624 * destroy the stacks, the callchain etc... 647 * destroy the stacks, the callchain etc...
625 */ 648 */
626static void 649static void
627coro_destroy_stacks (pTHX) 650coro_destruct_stacks (pTHX)
628{ 651{
629 while (PL_curstackinfo->si_next) 652 while (PL_curstackinfo->si_next)
630 PL_curstackinfo = PL_curstackinfo->si_next; 653 PL_curstackinfo = PL_curstackinfo->si_next;
631 654
632 while (PL_curstackinfo) 655 while (PL_curstackinfo)
686#endif 709#endif
687 } 710 }
688 } 711 }
689 712
690 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;
691} 741}
692 742
693/** coroutine stack handling ************************************************/ 743/** coroutine stack handling ************************************************/
694 744
695static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg); 745static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
810 GvSV (PL_defgv) = newSV (0); 860 GvSV (PL_defgv) = newSV (0);
811 GvAV (PL_defgv) = coro->args; coro->args = 0; 861 GvAV (PL_defgv) = coro->args; coro->args = 0;
812 GvSV (PL_errgv) = newSV (0); 862 GvSV (PL_errgv) = newSV (0);
813 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 863 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
814 PL_rs = newSVsv (GvSV (irsgv)); 864 PL_rs = newSVsv (GvSV (irsgv));
815 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); 865 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
816 866
817 { 867 {
818 dSP; 868 dSP;
819 LOGOP myop; 869 UNOP myop;
820 870
821 Zero (&myop, 1, LOGOP); 871 Zero (&myop, 1, UNOP);
822 myop.op_next = Nullop; 872 myop.op_next = Nullop;
823 myop.op_flags = OPf_WANT_VOID; 873 myop.op_flags = OPf_WANT_VOID;
824 874
825 PUSHMARK (SP); 875 PUSHMARK (SP);
826 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 876 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
829 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 879 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
830 SPAGAIN; 880 SPAGAIN;
831 } 881 }
832 882
833 /* 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
834 * likely was suspended in set_stacklevel, called from entersub. 884 * likely was suspended in set_stacklevel, called from pp_set_stacklevel,
835 * set_stacklevl doesn't do anything on return, but entersub does LEAVE, 885 * so we have to emulate entering pp_set_stacklevel here.
836 * so we ENTER here for symmetry
837 */ 886 */
838 ENTER; 887 SSL_HEAD;
839} 888}
840 889
841static void 890static void
842coro_destroy (pTHX_ struct coro *coro) 891coro_destruct (pTHX_ struct coro *coro)
843{ 892{
844 if (!IN_DESTRUCT) 893 if (!IN_DESTRUCT)
845 { 894 {
846 /* restore all saved variables and stuff */ 895 /* restore all saved variables and stuff */
847 LEAVE_SCOPE (0); 896 LEAVE_SCOPE (0);
869 SvREFCNT_dec (PL_warnhook); 918 SvREFCNT_dec (PL_warnhook);
870 919
871 SvREFCNT_dec (coro->saved_deffh); 920 SvREFCNT_dec (coro->saved_deffh);
872 SvREFCNT_dec (coro->throw); 921 SvREFCNT_dec (coro->throw);
873 922
874 coro_destroy_stacks (aTHX); 923 coro_destruct_stacks (aTHX);
875} 924}
876 925
877static void 926INLINE void
878free_coro_mortal (pTHX) 927free_coro_mortal (pTHX)
879{ 928{
880 if (expect_true (coro_mortal)) 929 if (expect_true (coro_mortal))
881 { 930 {
882 SvREFCNT_dec (coro_mortal); 931 SvREFCNT_dec (coro_mortal);
916 : cx->blk_gimme == G_SCALAR ? bot + 1 965 : cx->blk_gimme == G_SCALAR ? bot + 1
917 : bot; 966 : bot;
918 967
919 av_extend (av, top - bot); 968 av_extend (av, top - bot);
920 while (bot < top) 969 while (bot < top)
921 av_push (av, SvREFCNT_inc (*bot++)); 970 av_push (av, SvREFCNT_inc_NN (*bot++));
922 971
923 PL_runops = RUNOPS_DEFAULT; 972 PL_runops = RUNOPS_DEFAULT;
924 ENTER; 973 ENTER;
925 SAVETMPS; 974 SAVETMPS;
926 EXTEND (SP, 3); 975 EXTEND (SP, 3);
1006 1055
1007 TAINT_NOT; 1056 TAINT_NOT;
1008 return 0; 1057 return 0;
1009} 1058}
1010 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
1011/* 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 */
1012/* _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 */
1013/* during execution of a perl program */ 1069/* during execution of a perl program */
1070/* also initialises PL_top_env */
1014static void NOINLINE 1071static void NOINLINE
1015cctx_prepare (pTHX_ coro_cctx *cctx) 1072cctx_prepare (pTHX_ coro_cctx *cctx)
1016{ 1073{
1017 dSP; 1074 dSP;
1018 LOGOP myop; 1075 UNOP myop;
1019 1076
1020 PL_top_env = &PL_start_env; 1077 PL_top_env = &PL_start_env;
1021 1078
1022 if (cctx->flags & CC_TRACE) 1079 if (cctx->flags & CC_TRACE)
1023 PL_runops = runops_trace; 1080 PL_runops = runops_trace;
1024 1081
1025 Zero (&myop, 1, LOGOP); 1082 Zero (&myop, 1, UNOP);
1026 myop.op_next = PL_op; 1083 myop.op_next = PL_op;
1027 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 1084 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
1028 1085
1029 PUSHMARK (SP); 1086 PUSHMARK (SP);
1030 EXTEND (SP, 2); 1087 EXTEND (SP, 2);
1031 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); 1088 PUSHs (sv_2mortal (newSViv ((IV)cctx)));
1032 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 1089 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
1033 PUTBACK; 1090 PUTBACK;
1034 PL_op = (OP *)&myop; 1091 PL_op = (OP *)&myop;
1035 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 1092 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1036 SPAGAIN; 1093 SPAGAIN;
1037} 1094}
1038 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
1039/* 1117/*
1040 * this is a _very_ stripped down perl interpreter ;) 1118 * this is a _very_ stripped down perl interpreter ;)
1041 */ 1119 */
1042static void 1120static void
1043cctx_run (void *arg) 1121cctx_run (void *arg)
1044{ 1122{
1123#ifdef USE_ITHREADS
1124# if CORO_PTHREAD
1125 PERL_SET_CONTEXT (coro_thx);
1126# endif
1127#endif
1128 {
1045 dTHX; 1129 dTHX;
1046 1130
1047 /* cctx_run is the alternative tail of transfer(), so unlock here. */ 1131 /* we are the alternative tail to pp_set_stacklevel */
1048 UNLOCK; 1132 /* so do the same things here */
1133 SSL_TAIL;
1049 1134
1050 /* we now skip the entersub that lead to transfer() */ 1135 /* we now skip the op that did lead to transfer() */
1051 PL_op = PL_op->op_next; 1136 PL_op = PL_op->op_next;
1052 1137
1053 /* inject a fake subroutine call to cctx_init */ 1138 /* inject a fake subroutine call to cctx_init */
1054 cctx_prepare (aTHX_ (coro_cctx *)arg); 1139 cctx_prepare (aTHX_ (coro_cctx *)arg);
1055 1140
1141 /* cctx_run is the alternative tail of transfer() */
1142 transfer_tail (aTHX);
1143
1056 /* 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 */
1057 PL_restartop = PL_op; 1145 PL_restartop = PL_op;
1058 perl_run (PL_curinterp); 1146 perl_run (PL_curinterp);
1059 1147
1060 /* 1148 /*
1061 * 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
1062 * 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)
1063 * 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
1064 * bootstrap-time "top" top_env, as we cannot restore the "main" 1152 * bootstrap-time "top" top_env, as we cannot restore the "main"
1065 * coroutine as Coro has no such concept 1153 * coroutine as Coro has no such concept
1066 */ 1154 */
1067 PL_top_env = main_top_env; 1155 PL_top_env = main_top_env;
1068 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 }
1069} 1158}
1070 1159
1071static coro_cctx * 1160static coro_cctx *
1072cctx_new () 1161cctx_new ()
1073{ 1162{
1074 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 ();
1075 void *stack_start; 1192 void *stack_start;
1076 size_t stack_size; 1193 size_t stack_size;
1077 1194
1078 ++cctx_count;
1079
1080 Newz (0, cctx, 1, coro_cctx);
1081
1082#if HAVE_MMAP 1195#if HAVE_MMAP
1083 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;
1084 /* mmap supposedly does allocate-on-write for us */ 1197 /* 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); 1198 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
1086 1199
1087 if (cctx->sptr != (void *)-1) 1200 if (cctx->sptr != (void *)-1)
1088 { 1201 {
1089# if CORO_STACKGUARD 1202 #if CORO_STACKGUARD
1090 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1203 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1091# endif 1204 #endif
1092 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 1205 stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE;
1093 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 1206 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
1094 cctx->flags |= CC_MAPPED; 1207 cctx->flags |= CC_MAPPED;
1095 } 1208 }
1096 else 1209 else
1097#endif 1210#endif
1098 { 1211 {
1099 cctx->ssize = coro_stacksize * (long)sizeof (long); 1212 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1100 New (0, cctx->sptr, coro_stacksize, long); 1213 New (0, cctx->sptr, cctx_stacksize, long);
1101 1214
1102 if (!cctx->sptr) 1215 if (!cctx->sptr)
1103 { 1216 {
1104 perror ("FATAL: unable to allocate stack for coroutine"); 1217 perror ("FATAL: unable to allocate stack for coroutine, exiting.");
1105 _exit (EXIT_FAILURE); 1218 _exit (EXIT_FAILURE);
1106 } 1219 }
1107 1220
1108 stack_start = cctx->sptr; 1221 stack_start = cctx->sptr;
1109 stack_size = cctx->ssize; 1222 stack_size = cctx->ssize;
1110 } 1223 }
1111 1224
1112 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
1113 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);
1114 1230
1115 return cctx; 1231 return cctx;
1116} 1232}
1117 1233
1120{ 1236{
1121 if (!cctx) 1237 if (!cctx)
1122 return; 1238 return;
1123 1239
1124 --cctx_count; 1240 --cctx_count;
1241 coro_destroy (&cctx->cctx);
1125 1242
1243 /* coro_transfer creates new, empty cctx's */
1244 if (cctx->sptr)
1245 {
1126#if CORO_USE_VALGRIND 1246 #if CORO_USE_VALGRIND
1127 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1247 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1128#endif 1248 #endif
1129 1249
1130#if HAVE_MMAP 1250#if HAVE_MMAP
1131 if (cctx->flags & CC_MAPPED) 1251 if (cctx->flags & CC_MAPPED)
1132 munmap (cctx->sptr, cctx->ssize); 1252 munmap (cctx->sptr, cctx->ssize);
1133 else 1253 else
1134#endif 1254#endif
1135 Safefree (cctx->sptr); 1255 Safefree (cctx->sptr);
1256 }
1136 1257
1137 Safefree (cctx); 1258 Safefree (cctx);
1138} 1259}
1139 1260
1140/* wether this cctx should be destructed */ 1261/* wether this cctx should be destructed */
1141#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))
1142 1263
1143static coro_cctx * 1264static coro_cctx *
1144cctx_get (pTHX) 1265cctx_get (pTHX)
1145{ 1266{
1146 while (expect_true (cctx_first)) 1267 while (expect_true (cctx_first))
1153 return cctx; 1274 return cctx;
1154 1275
1155 cctx_destroy (cctx); 1276 cctx_destroy (cctx);
1156 } 1277 }
1157 1278
1158 return cctx_new (); 1279 return cctx_new_run ();
1159} 1280}
1160 1281
1161static void 1282static void
1162cctx_put (coro_cctx *cctx) 1283cctx_put (coro_cctx *cctx)
1163{ 1284{
1285 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr));
1286
1164 /* free another cctx if overlimit */ 1287 /* free another cctx if overlimit */
1165 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1288 if (expect_false (cctx_idle >= cctx_max_idle))
1166 { 1289 {
1167 coro_cctx *first = cctx_first; 1290 coro_cctx *first = cctx_first;
1168 cctx_first = first->next; 1291 cctx_first = first->next;
1169 --cctx_idle; 1292 --cctx_idle;
1170 1293
1202/* always use the TRANSFER macro */ 1325/* always use the TRANSFER macro */
1203static void NOINLINE 1326static void NOINLINE
1204transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1327transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1205{ 1328{
1206 dSTACKLEVEL; 1329 dSTACKLEVEL;
1207 static volatile int has_throw;
1208 1330
1209 /* sometimes transfer is only called to set idle_sp */ 1331 /* sometimes transfer is only called to set idle_sp */
1210 if (expect_false (!next)) 1332 if (expect_false (!next))
1211 { 1333 {
1212 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1334 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1216 { 1338 {
1217 coro_cctx *prev__cctx; 1339 coro_cctx *prev__cctx;
1218 1340
1219 if (expect_false (prev->flags & CF_NEW)) 1341 if (expect_false (prev->flags & CF_NEW))
1220 { 1342 {
1221 /* create a new empty context */ 1343 /* create a new empty/source context */
1222 Newz (0, prev->cctx, 1, coro_cctx); 1344 prev->cctx = cctx_new_empty ();
1223 prev->flags &= ~CF_NEW; 1345 prev->flags &= ~CF_NEW;
1224 prev->flags |= CF_RUNNING; 1346 prev->flags |= CF_RUNNING;
1225 } 1347 }
1226 1348
1227 prev->flags &= ~CF_RUNNING; 1349 prev->flags &= ~CF_RUNNING;
1242 else 1364 else
1243 load_perl (aTHX_ next); 1365 load_perl (aTHX_ next);
1244 1366
1245 prev__cctx = prev->cctx; 1367 prev__cctx = prev->cctx;
1246 1368
1247 /* possibly "free" the cctx */ 1369 /* possibly untie and reuse the cctx */
1248 if (expect_true ( 1370 if (expect_true (
1249 prev__cctx->idle_sp == STACKLEVEL 1371 prev__cctx->idle_sp == STACKLEVEL
1250 && !(prev__cctx->flags & CC_TRACE) 1372 && !(prev__cctx->flags & CC_TRACE)
1251 && !force_cctx 1373 && !force_cctx
1252 )) 1374 ))
1253 { 1375 {
1254 /* 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 */
1255 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));
1256 1378
1257 prev->cctx = 0; 1379 prev->cctx = 0;
1258 1380
1259 /* 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 */
1260 /* 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 */
1268 ++next->usecount; 1390 ++next->usecount;
1269 1391
1270 if (expect_true (!next->cctx)) 1392 if (expect_true (!next->cctx))
1271 next->cctx = cctx_get (aTHX); 1393 next->cctx = cctx_get (aTHX);
1272 1394
1273 has_throw = !!next->throw; 1395 assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next));
1396 transfer_next = next;
1274 1397
1275 if (expect_false (prev__cctx != next->cctx)) 1398 if (expect_false (prev__cctx != next->cctx))
1276 { 1399 {
1277 prev__cctx->top_env = PL_top_env; 1400 prev__cctx->top_env = PL_top_env;
1278 PL_top_env = next->cctx->top_env; 1401 PL_top_env = next->cctx->top_env;
1279 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1402 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
1280 } 1403 }
1281 1404
1282 free_coro_mortal (aTHX); 1405 transfer_tail (aTHX);
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 } 1406 }
1298} 1407}
1299
1300struct transfer_args
1301{
1302 struct coro *prev, *next;
1303};
1304 1408
1305#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))
1306#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next) 1410#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1307 1411
1308/** high level stuff ********************************************************/ 1412/** high level stuff ********************************************************/
1334 croak ("FATAL: tried to destroy currently running coroutine"); 1438 croak ("FATAL: tried to destroy currently running coroutine");
1335 1439
1336 save_perl (aTHX_ &temp); 1440 save_perl (aTHX_ &temp);
1337 load_perl (aTHX_ coro); 1441 load_perl (aTHX_ coro);
1338 1442
1339 coro_destroy (aTHX_ coro); 1443 coro_destruct (aTHX_ coro);
1340 1444
1341 load_perl (aTHX_ &temp); 1445 load_perl (aTHX_ &temp);
1342 1446
1343 coro->slot = 0; 1447 coro->slot = 0;
1344 } 1448 }
1450 LOCK; 1554 LOCK;
1451 1555
1452 sv_hook = coro_nready ? 0 : coro_readyhook; 1556 sv_hook = coro_nready ? 0 : coro_readyhook;
1453 xs_hook = coro_nready ? 0 : coroapi.readyhook; 1557 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1454 1558
1455 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1559 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1456 ++coro_nready; 1560 ++coro_nready;
1457 1561
1458 UNLOCK; 1562 UNLOCK;
1459 1563
1460 if (sv_hook) 1564 if (sv_hook)
1481 1585
1482static int 1586static int
1483api_is_ready (SV *coro_sv) 1587api_is_ready (SV *coro_sv)
1484{ 1588{
1485 dTHX; 1589 dTHX;
1590
1486 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1591 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1487} 1592}
1488 1593
1489static void 1594INLINE void
1490prepare_schedule (pTHX_ struct transfer_args *ta) 1595prepare_schedule (pTHX_ struct transfer_args *ta)
1491{ 1596{
1492 SV *prev_sv, *next_sv; 1597 SV *prev_sv, *next_sv;
1493 1598
1494 for (;;) 1599 for (;;)
1520 /* cannot transfer to destroyed coros, skip and look for next */ 1625 /* cannot transfer to destroyed coros, skip and look for next */
1521 if (expect_false (ta->next->flags & CF_DESTROYED)) 1626 if (expect_false (ta->next->flags & CF_DESTROYED))
1522 { 1627 {
1523 UNLOCK; 1628 UNLOCK;
1524 SvREFCNT_dec (next_sv); 1629 SvREFCNT_dec (next_sv);
1525 /* coro_nready is already taken care of by destroy */ 1630 /* coro_nready has already been taken care of by destroy */
1526 continue; 1631 continue;
1527 } 1632 }
1528 1633
1529 --coro_nready; 1634 --coro_nready;
1530 UNLOCK; 1635 UNLOCK;
1533 1638
1534 /* free this only after the transfer */ 1639 /* free this only after the transfer */
1535 prev_sv = SvRV (coro_current); 1640 prev_sv = SvRV (coro_current);
1536 ta->prev = SvSTATE (prev_sv); 1641 ta->prev = SvSTATE (prev_sv);
1537 TRANSFER_CHECK (*ta); 1642 TRANSFER_CHECK (*ta);
1538 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));
1539 ta->next->flags &= ~CF_READY; 1644 ta->next->flags &= ~CF_READY;
1540 SvRV_set (coro_current, next_sv); 1645 SvRV_set (coro_current, next_sv);
1541 1646
1542 LOCK; 1647 LOCK;
1543 free_coro_mortal (aTHX); 1648 free_coro_mortal (aTHX);
1544 coro_mortal = prev_sv; 1649 coro_mortal = prev_sv;
1545 UNLOCK; 1650 UNLOCK;
1546} 1651}
1547 1652
1548static void 1653INLINE void
1549prepare_cede (pTHX_ struct transfer_args *ta) 1654prepare_cede (pTHX_ struct transfer_args *ta)
1550{ 1655{
1551 api_ready (coro_current); 1656 api_ready (coro_current);
1552 prepare_schedule (aTHX_ ta); 1657 prepare_schedule (aTHX_ ta);
1553} 1658}
1615 struct coro *coro = SvSTATE (coro_sv); 1720 struct coro *coro = SvSTATE (coro_sv);
1616 1721
1617 if (flags & CC_TRACE) 1722 if (flags & CC_TRACE)
1618 { 1723 {
1619 if (!coro->cctx) 1724 if (!coro->cctx)
1620 coro->cctx = cctx_new (); 1725 coro->cctx = cctx_new_run ();
1621 else if (!(coro->cctx->flags & CC_TRACE)) 1726 else if (!(coro->cctx->flags & CC_TRACE))
1622 croak ("cannot enable tracing on coroutine with custom stack"); 1727 croak ("cannot enable tracing on coroutine with custom stack");
1623 1728
1624 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1729 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1625 } 1730 }
1632 else 1737 else
1633 coro->slot->runops = RUNOPS_DEFAULT; 1738 coro->slot->runops = RUNOPS_DEFAULT;
1634 } 1739 }
1635} 1740}
1636 1741
1742#if 0
1637static int 1743static int
1638coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) 1744coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1639{ 1745{
1640 AV *padlist; 1746 AV *padlist;
1641 AV *av = (AV *)mg->mg_obj; 1747 AV *av = (AV *)mg->mg_obj;
1647 1753
1648static MGVTBL coro_gensub_vtbl = { 1754static MGVTBL coro_gensub_vtbl = {
1649 0, 0, 0, 0, 1755 0, 0, 0, 0,
1650 coro_gensub_free 1756 coro_gensub_free
1651}; 1757};
1758#endif
1652 1759
1653/*****************************************************************************/ 1760/*****************************************************************************/
1654/* PerlIO::cede */ 1761/* PerlIO::cede */
1655 1762
1656typedef struct 1763typedef struct
1662static IV 1769static IV
1663PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) 1770PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1664{ 1771{
1665 PerlIOCede *self = PerlIOSelf (f, PerlIOCede); 1772 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1666 1773
1667 self->every = SvNV (arg); 1774 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
1668 self->next = nvtime () + self->every; 1775 self->next = nvtime () + self->every;
1669 1776
1670 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); 1777 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
1671} 1778}
1672 1779
1688 { 1795 {
1689 api_cede (); 1796 api_cede ();
1690 self->next = now + self->every; 1797 self->next = now + self->every;
1691 } 1798 }
1692 1799
1693 return PerlIOBuf_flush (f); 1800 return PerlIOBuf_flush (aTHX_ f);
1694} 1801}
1695 1802
1696static PerlIO_funcs PerlIO_cede = 1803static PerlIO_funcs PerlIO_cede =
1697{ 1804{
1698 sizeof(PerlIO_funcs), 1805 sizeof(PerlIO_funcs),
1723 PerlIOBuf_get_ptr, 1830 PerlIOBuf_get_ptr,
1724 PerlIOBuf_get_cnt, 1831 PerlIOBuf_get_cnt,
1725 PerlIOBuf_set_ptrcnt, 1832 PerlIOBuf_set_ptrcnt,
1726}; 1833};
1727 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}
1728 1928
1729MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1929MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1730 1930
1731PROTOTYPES: DISABLE 1931PROTOTYPES: DISABLE
1732
1733BOOT:
1734{
1735#ifdef USE_ITHREADS
1736 MUTEX_INIT (&coro_mutex);
1737#endif
1738 BOOT_PAGESIZE;
1739
1740 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1741 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1742
1743 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1744 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1745 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr;
1746
1747 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1748 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1749 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1750
1751 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1752
1753 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1754 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1755 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1756 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1757
1758 main_mainstack = PL_mainstack;
1759 main_top_env = PL_top_env;
1760
1761 while (main_top_env->je_prev)
1762 main_top_env = main_top_env->je_prev;
1763
1764 coroapi.ver = CORO_API_VERSION;
1765 coroapi.rev = CORO_API_REVISION;
1766 coroapi.transfer = api_transfer;
1767
1768 {
1769 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1770
1771 if (!svp) croak ("Time::HiRes is required");
1772 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1773
1774 nvtime = INT2PTR (double (*)(), SvIV (*svp));
1775 }
1776
1777 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1778}
1779
1780SV *
1781new (char *klass, ...)
1782 CODE:
1783{
1784 struct coro *coro;
1785 MAGIC *mg;
1786 HV *hv;
1787 int i;
1788
1789 Newz (0, coro, 1, struct coro);
1790 coro->args = newAV ();
1791 coro->flags = CF_NEW;
1792
1793 if (coro_first) coro_first->prev = coro;
1794 coro->next = coro_first;
1795 coro_first = coro;
1796
1797 coro->hv = hv = newHV ();
1798 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
1799 mg->mg_flags |= MGf_DUP;
1800 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1801
1802 av_extend (coro->args, items - 1);
1803 for (i = 1; i < items; i++)
1804 av_push (coro->args, newSVsv (ST (i)));
1805}
1806 OUTPUT:
1807 RETVAL
1808 1932
1809# 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
1810# 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
1811# stack offset 1935# stack offset
1812void 1936void
1816 Coro::schedule = 2 1940 Coro::schedule = 2
1817 Coro::cede = 3 1941 Coro::cede = 3
1818 Coro::cede_notself = 4 1942 Coro::cede_notself = 4
1819 CODE: 1943 CODE:
1820{ 1944{
1821 struct transfer_args ta; 1945 assert (("FATAL: ssl call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_set_stacklevel));
1822 1946
1823 PUTBACK; 1947 assert (("FATAL: ssl call with illegal CV value", CvGV (cv)));
1824 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
1825 { 2004 {
1826 case 0: 2005 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1827 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1828 ta.next = 0;
1829 break;
1830 2006
1831 case 1: 2007 if (!svp) croak ("Time::HiRes is required");
1832 if (items != 2) 2008 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1833 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1834 2009
1835 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 2010 nvtime = INT2PTR (double (*)(), SvIV (*svp));
1836 break;
1837
1838 case 2:
1839 prepare_schedule (aTHX_ &ta);
1840 break;
1841
1842 case 3:
1843 prepare_cede (aTHX_ &ta);
1844 break;
1845
1846 case 4:
1847 if (!prepare_cede_notself (aTHX_ &ta))
1848 XSRETURN_EMPTY;
1849
1850 break;
1851 } 2011 }
1852 SPAGAIN;
1853 2012
1854 BARRIER; 2013 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1855 PUTBACK;
1856 TRANSFER (ta, 0);
1857 SPAGAIN; /* might be the sp of a different coroutine now */
1858 /* be extra careful not to ever do anything after TRANSFER */
1859} 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
1860 2044
1861bool 2045bool
1862_destroy (SV *coro_sv) 2046_destroy (SV *coro_sv)
1863 CODE: 2047 CODE:
1864 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 2048 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1872 _exit (code); 2056 _exit (code);
1873 2057
1874int 2058int
1875cctx_stacksize (int new_stacksize = 0) 2059cctx_stacksize (int new_stacksize = 0)
1876 CODE: 2060 CODE:
1877 RETVAL = coro_stacksize; 2061 RETVAL = cctx_stacksize;
1878 if (new_stacksize) 2062 if (new_stacksize)
2063 {
1879 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;
1880 OUTPUT: 2076 OUTPUT:
1881 RETVAL 2077 RETVAL
1882 2078
1883int 2079int
1884cctx_count () 2080cctx_count ()
1962 RETVAL = boolSV (coro->flags & ix); 2158 RETVAL = boolSV (coro->flags & ix);
1963 OUTPUT: 2159 OUTPUT:
1964 RETVAL 2160 RETVAL
1965 2161
1966void 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
1967api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 2170api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1968 2171
1969SV * 2172SV *
1970has_cctx (Coro::State coro) 2173has_cctx (Coro::State coro)
1971 PROTOTYPE: $ 2174 PROTOTYPE: $
1980 CODE: 2183 CODE:
1981 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; 2184 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1982 OUTPUT: 2185 OUTPUT:
1983 RETVAL 2186 RETVAL
1984 2187
1985IV 2188UV
1986rss (Coro::State coro) 2189rss (Coro::State coro)
1987 PROTOTYPE: $ 2190 PROTOTYPE: $
1988 ALIAS: 2191 ALIAS:
1989 usecount = 1 2192 usecount = 1
1990 CODE: 2193 CODE:
2001 CODE: 2204 CODE:
2002 struct coro *coro = SvSTATE (coro_current); 2205 struct coro *coro = SvSTATE (coro_current);
2003 coro->cctx->idle_sp = 0; 2206 coro->cctx->idle_sp = 0;
2004 2207
2005void 2208void
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) 2209swap_defsv (Coro::State self)
2014 PROTOTYPE: $ 2210 PROTOTYPE: $
2015 ALIAS: 2211 ALIAS:
2016 swap_defav = 1 2212 swap_defav = 1
2017 CODE: 2213 CODE:
2049 2245
2050 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 2246 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
2051 coro_ready[i] = newAV (); 2247 coro_ready[i] = newAV ();
2052 2248
2053 { 2249 {
2054 SV *sv = perl_get_sv ("Coro::API", TRUE); 2250 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2055 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
2056 2251
2057 coroapi.schedule = api_schedule; 2252 coroapi.schedule = api_schedule;
2058 coroapi.cede = api_cede; 2253 coroapi.cede = api_cede;
2059 coroapi.cede_notself = api_cede_notself; 2254 coroapi.cede_notself = api_cede_notself;
2060 coroapi.ready = api_ready; 2255 coroapi.ready = api_ready;
2071void 2266void
2072_set_current (SV *current) 2267_set_current (SV *current)
2073 PROTOTYPE: $ 2268 PROTOTYPE: $
2074 CODE: 2269 CODE:
2075 SvREFCNT_dec (SvRV (coro_current)); 2270 SvREFCNT_dec (SvRV (coro_current));
2076 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); 2271 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
2077 2272
2078void 2273void
2079_set_readyhook (SV *hook) 2274_set_readyhook (SV *hook)
2080 PROTOTYPE: $ 2275 PROTOTYPE: $
2081 CODE: 2276 CODE:
2141 SvREFCNT_dec (old); 2336 SvREFCNT_dec (old);
2142 croak ("\3async_pool terminate\2\n"); 2337 croak ("\3async_pool terminate\2\n");
2143 } 2338 }
2144 2339
2145 SvREFCNT_dec (coro->saved_deffh); 2340 SvREFCNT_dec (coro->saved_deffh);
2146 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2341 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2147 2342
2148 hv_store (hv, "desc", sizeof ("desc") - 1, 2343 hv_store (hv, "desc", sizeof ("desc") - 1,
2149 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 2344 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2150 2345
2151 invoke_av = (AV *)SvRV (invoke); 2346 invoke_av = (AV *)SvRV (invoke);
2155 2350
2156 if (len > 0) 2351 if (len > 0)
2157 { 2352 {
2158 av_fill (defav, len - 1); 2353 av_fill (defav, len - 1);
2159 for (i = 0; i < len; ++i) 2354 for (i = 0; i < len; ++i)
2160 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2355 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
2161 } 2356 }
2162 2357
2163 SvREFCNT_dec (invoke); 2358 SvREFCNT_dec (invoke);
2164} 2359}
2165 2360
2172 sv_setsv (cb, &PL_sv_undef); 2367 sv_setsv (cb, &PL_sv_undef);
2173 2368
2174 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2369 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2175 coro->saved_deffh = 0; 2370 coro->saved_deffh = 0;
2176 2371
2177 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2372 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2178 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2373 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2179 { 2374 {
2180 SV *old = PL_diehook; 2375 SV *old = PL_diehook;
2181 PL_diehook = 0; 2376 PL_diehook = 0;
2182 SvREFCNT_dec (old); 2377 SvREFCNT_dec (old);
2244#endif 2439#endif
2245 2440
2246 2441
2247MODULE = Coro::State PACKAGE = Coro::AIO 2442MODULE = Coro::State PACKAGE = Coro::AIO
2248 2443
2249SV * 2444void
2250_get_state () 2445_get_state (SV *self)
2251 CODE: 2446 PPCODE:
2252{ 2447{
2253 struct io_state *data; 2448 AV *defav = GvAV (PL_defgv);
2254 2449 AV *av = newAV ();
2450 int i;
2255 RETVAL = newSV (sizeof (struct io_state)); 2451 SV *data_sv = newSV (sizeof (struct io_state));
2256 data = (struct io_state *)SvPVX (RETVAL); 2452 struct io_state *data = (struct io_state *)SvPVX (data_sv);
2257 SvCUR_set (RETVAL, sizeof (struct io_state)); 2453 SvCUR_set (data_sv, sizeof (struct io_state));
2258 SvPOK_only (RETVAL); 2454 SvPOK_only (data_sv);
2259 2455
2260 data->errorno = errno; 2456 data->errorno = errno;
2261 data->laststype = PL_laststype; 2457 data->laststype = PL_laststype;
2262 data->laststatval = PL_laststatval; 2458 data->laststatval = PL_laststatval;
2263 data->statcache = PL_statcache; 2459 data->statcache = PL_statcache;
2460
2461 av_extend (av, AvFILLp (defav) + 1 + 1);
2462
2463 for (i = 0; i <= AvFILLp (defav); ++i)
2464 av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i]));
2465
2466 av_push (av, data_sv);
2467
2468 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2469
2470 api_ready (self);
2264} 2471}
2265 OUTPUT:
2266 RETVAL
2267 2472
2268void 2473void
2269_set_state (char *data_) 2474_set_state (SV *state)
2270 PROTOTYPE: $ 2475 PROTOTYPE: $
2271 CODE: 2476 PPCODE:
2272{ 2477{
2273 struct io_state *data = (void *)data_; 2478 AV *av = (AV *)SvRV (state);
2479 struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]);
2480 int i;
2274 2481
2275 errno = data->errorno; 2482 errno = data->errorno;
2276 PL_laststype = data->laststype; 2483 PL_laststype = data->laststype;
2277 PL_laststatval = data->laststatval; 2484 PL_laststatval = data->laststatval;
2278 PL_statcache = data->statcache; 2485 PL_statcache = data->statcache;
2486
2487 EXTEND (SP, AvFILLp (av));
2488 for (i = 0; i < AvFILLp (av); ++i)
2489 PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i])));
2279} 2490}
2280 2491
2281 2492
2282MODULE = Coro::State PACKAGE = Coro::AnyEvent 2493MODULE = Coro::State PACKAGE = Coro::AnyEvent
2283 2494
2312 2523
2313MODULE = Coro::State PACKAGE = PerlIO::cede 2524MODULE = Coro::State PACKAGE = PerlIO::cede
2314 2525
2315BOOT: 2526BOOT:
2316 PerlIO_define_layer (aTHX_ &PerlIO_cede); 2527 PerlIO_define_layer (aTHX_ &PerlIO_cede);
2528

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines