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

Comparing Coro/Coro/State.xs (file contents):
Revision 1.248 by root, Mon Sep 29 12:40:50 2008 UTC vs.
Revision 1.267 by root, Fri Nov 14 06:29:52 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 sometimes 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}
1554 1659
1555static int 1660static void
1556prepare_cede_notself (pTHX_ struct transfer_args *ta) 1661prepare_cede_notself (pTHX_ struct transfer_args *ta)
1557{ 1662{
1663 SV *prev = SvRV (coro_current);
1664
1558 if (coro_nready) 1665 if (coro_nready)
1559 { 1666 {
1560 SV *prev = SvRV (coro_current);
1561 prepare_schedule (aTHX_ ta); 1667 prepare_schedule (aTHX_ ta);
1562 api_ready (prev); 1668 api_ready (prev);
1669 }
1670 else
1671 ta->prev = ta->next = SvSTATE (prev);
1672}
1673
1674static void
1675api_schedule (void)
1676{
1677 dTHX;
1678 struct transfer_args ta;
1679
1680 prepare_schedule (aTHX_ &ta);
1681 TRANSFER (ta, 1);
1682}
1683
1684static int
1685api_cede (void)
1686{
1687 dTHX;
1688 struct transfer_args ta;
1689
1690 prepare_cede (aTHX_ &ta);
1691
1692 if (expect_true (ta.prev != ta.next))
1693 {
1694 TRANSFER (ta, 1);
1563 return 1; 1695 return 1;
1564 } 1696 }
1565 else 1697 else
1566 return 0; 1698 return 0;
1567} 1699}
1568 1700
1569static void
1570api_schedule (void)
1571{
1572 dTHX;
1573 struct transfer_args ta;
1574
1575 prepare_schedule (aTHX_ &ta);
1576 TRANSFER (ta, 1);
1577}
1578
1579static int 1701static int
1580api_cede (void) 1702api_cede_notself (void)
1581{ 1703{
1704 if (coro_nready)
1705 {
1582 dTHX; 1706 dTHX;
1583 struct transfer_args ta; 1707 struct transfer_args ta;
1584 1708
1585 prepare_cede (aTHX_ &ta); 1709 prepare_cede_notself (aTHX_ &ta);
1586
1587 if (expect_true (ta.prev != ta.next))
1588 {
1589 TRANSFER (ta, 1); 1710 TRANSFER (ta, 1);
1590 return 1; 1711 return 1;
1591 } 1712 }
1592 else 1713 else
1593 return 0; 1714 return 0;
1594} 1715}
1595 1716
1596static int
1597api_cede_notself (void)
1598{
1599 dTHX;
1600 struct transfer_args ta;
1601
1602 if (prepare_cede_notself (aTHX_ &ta))
1603 {
1604 TRANSFER (ta, 1);
1605 return 1;
1606 }
1607 else
1608 return 0;
1609}
1610
1611static void 1717static void
1612api_trace (SV *coro_sv, int flags) 1718api_trace (SV *coro_sv, int flags)
1613{ 1719{
1614 dTHX; 1720 dTHX;
1615 struct coro *coro = SvSTATE (coro_sv); 1721 struct coro *coro = SvSTATE (coro_sv);
1616 1722
1617 if (flags & CC_TRACE) 1723 if (flags & CC_TRACE)
1618 { 1724 {
1619 if (!coro->cctx) 1725 if (!coro->cctx)
1620 coro->cctx = cctx_new (); 1726 coro->cctx = cctx_new_run ();
1621 else if (!(coro->cctx->flags & CC_TRACE)) 1727 else if (!(coro->cctx->flags & CC_TRACE))
1622 croak ("cannot enable tracing on coroutine with custom stack"); 1728 croak ("cannot enable tracing on coroutine with custom stack");
1623 1729
1624 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1730 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1625 } 1731 }
1632 else 1738 else
1633 coro->slot->runops = RUNOPS_DEFAULT; 1739 coro->slot->runops = RUNOPS_DEFAULT;
1634 } 1740 }
1635} 1741}
1636 1742
1743#if 0
1637static int 1744static int
1638coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) 1745coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1639{ 1746{
1640 AV *padlist; 1747 AV *padlist;
1641 AV *av = (AV *)mg->mg_obj; 1748 AV *av = (AV *)mg->mg_obj;
1647 1754
1648static MGVTBL coro_gensub_vtbl = { 1755static MGVTBL coro_gensub_vtbl = {
1649 0, 0, 0, 0, 1756 0, 0, 0, 0,
1650 coro_gensub_free 1757 coro_gensub_free
1651}; 1758};
1759#endif
1652 1760
1653/*****************************************************************************/ 1761/*****************************************************************************/
1654/* PerlIO::cede */ 1762/* PerlIO::cede */
1655 1763
1656typedef struct 1764typedef struct
1723 PerlIOBuf_get_ptr, 1831 PerlIOBuf_get_ptr,
1724 PerlIOBuf_get_cnt, 1832 PerlIOBuf_get_cnt,
1725 PerlIOBuf_set_ptrcnt, 1833 PerlIOBuf_set_ptrcnt,
1726}; 1834};
1727 1835
1836/*****************************************************************************/
1837
1838static const CV *ssl_cv; /* for quick consistency check */
1839
1840static UNOP ssl_restore; /* restore stack as entersub did, for first-re-run */
1841static SV *ssl_arg0;
1842static SV *ssl_arg1;
1843
1844/* this restores the stack in the case we patched the entersub, to */
1845/* recreate the stack frame as perl will on following calls */
1846/* since entersub cleared the stack */
1847static OP *
1848pp_restore (pTHX)
1849{
1850 dSP;
1851
1852 PUSHMARK (SP);
1853
1854 EXTEND (SP, 3);
1855 if (ssl_arg0) PUSHs (sv_2mortal (ssl_arg0)), ssl_arg0 = 0;
1856 if (ssl_arg1) PUSHs (sv_2mortal (ssl_arg1)), ssl_arg1 = 0;
1857 PUSHs ((SV *)CvGV (ssl_cv));
1858
1859 RETURNOP (ssl_restore.op_first);
1860}
1861
1862#define OPpENTERSUB_SSL 15 /* the part of op_private entersub hopefully doesn't use */
1863
1864/* declare prototype */
1865XS(XS_Coro__State__set_stacklevel);
1866
1867/*
1868 * these not obviously related functions are all rolled into one
1869 * function to increase chances that they all will call transfer with the same
1870 * stack offset
1871 */
1872static OP *
1873pp_set_stacklevel (pTHX)
1874{
1875 dSP;
1876 struct transfer_args ta;
1877 SV **arg = PL_stack_base + TOPMARK + 1;
1878 int items = SP - arg; /* args without function object */
1879
1880 /* do a quick consistency check on the "function" object, and if it isn't */
1881 /* for us, divert to the real entersub */
1882 if (SvTYPE (*sp) != SVt_PVGV || CvXSUB (GvCV (*sp)) != XS_Coro__State__set_stacklevel)
1883 return PL_ppaddr[OP_ENTERSUB](aTHX);
1884
1885 /* pop args */
1886 SP = PL_stack_base + POPMARK;
1887
1888 if (!(PL_op->op_flags & OPf_STACKED))
1889 {
1890 /* ampersand-form of call, use @_ instead of stack */
1891 AV *av = GvAV (PL_defgv);
1892 arg = AvARRAY (av);
1893 items = AvFILLp (av) + 1;
1894 }
1895
1896 PUTBACK;
1897 switch (PL_op->op_private & OPpENTERSUB_SSL)
1898 {
1899 case 0:
1900 prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (arg [0]));
1901 break;
1902
1903 case 1:
1904 if (items != 2)
1905 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d.", items);
1906
1907 prepare_transfer (aTHX_ &ta, arg [0], arg [1]);
1908 break;
1909
1910 case 2:
1911 prepare_schedule (aTHX_ &ta);
1912 break;
1913
1914 case 3:
1915 prepare_cede (aTHX_ &ta);
1916 break;
1917
1918 case 4:
1919 prepare_cede_notself (aTHX_ &ta);
1920 break;
1921 }
1922
1923 TRANSFER (ta, 0);
1924 SPAGAIN;
1925
1926skip:
1927 PUTBACK;
1928 SSL_TAIL;
1929 SPAGAIN;
1930 RETURN;
1931}
1932
1933static void
1934coro_ssl_patch (pTHX_ CV *cv, int ix, SV **args, int items)
1935{
1936 assert (("FATAL: ssl call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_set_stacklevel));
1937
1938 assert (("FATAL: ssl call with illegal CV value", CvGV (cv)));
1939 ssl_cv = cv;
1940
1941 /* we patch the op, and then re-run the whole call */
1942 /* we have to put some dummy argument on the stack for this to work */
1943 ssl_restore.op_next = (OP *)&ssl_restore;
1944 ssl_restore.op_type = OP_NULL;
1945 ssl_restore.op_ppaddr = pp_restore;
1946 ssl_restore.op_first = PL_op;
1947
1948 ssl_arg0 = items > 0 ? SvREFCNT_inc (args [0]) : 0;
1949 ssl_arg1 = items > 1 ? SvREFCNT_inc (args [1]) : 0;
1950
1951 PL_op->op_ppaddr = pp_set_stacklevel;
1952 PL_op->op_private = PL_op->op_private & ~OPpENTERSUB_SSL | ix; /* we potentially share our private flags with entersub */
1953
1954 PL_op = (OP *)&ssl_restore;
1955}
1728 1956
1729MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1957MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1730 1958
1731PROTOTYPES: DISABLE 1959PROTOTYPES: DISABLE
1732 1960
1733BOOT: 1961BOOT:
1734{ 1962{
1735#ifdef USE_ITHREADS 1963#ifdef USE_ITHREADS
1736 MUTEX_INIT (&coro_mutex); 1964 MUTEX_INIT (&coro_lock);
1965# if CORO_PTHREAD
1966 coro_thx = PERL_GET_CONTEXT;
1967# endif
1737#endif 1968#endif
1738 BOOT_PAGESIZE; 1969 BOOT_PAGESIZE;
1739 1970
1740 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1971 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1741 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1972 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1804 av_push (coro->args, newSVsv (ST (i))); 2035 av_push (coro->args, newSVsv (ST (i)));
1805} 2036}
1806 OUTPUT: 2037 OUTPUT:
1807 RETVAL 2038 RETVAL
1808 2039
1809# 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
1811# stack offset
1812void 2040void
1813_set_stacklevel (...) 2041_set_stacklevel (...)
1814 ALIAS: 2042 ALIAS:
1815 Coro::State::transfer = 1 2043 Coro::State::transfer = 1
1816 Coro::schedule = 2 2044 Coro::schedule = 2
1817 Coro::cede = 3 2045 Coro::cede = 3
1818 Coro::cede_notself = 4 2046 Coro::cede_notself = 4
1819 CODE: 2047 CODE:
1820{ 2048 coro_ssl_patch (aTHX_ cv, ix, &ST (0), items);
1821 struct transfer_args ta;
1822
1823 PUTBACK;
1824 switch (ix)
1825 {
1826 case 0:
1827 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1828 ta.next = 0;
1829 break;
1830
1831 case 1:
1832 if (items != 2)
1833 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1834
1835 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
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 }
1852 SPAGAIN;
1853
1854 BARRIER;
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}
1860 2049
1861bool 2050bool
1862_destroy (SV *coro_sv) 2051_destroy (SV *coro_sv)
1863 CODE: 2052 CODE:
1864 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 2053 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1872 _exit (code); 2061 _exit (code);
1873 2062
1874int 2063int
1875cctx_stacksize (int new_stacksize = 0) 2064cctx_stacksize (int new_stacksize = 0)
1876 CODE: 2065 CODE:
1877 RETVAL = coro_stacksize; 2066 RETVAL = cctx_stacksize;
1878 if (new_stacksize) 2067 if (new_stacksize)
2068 {
1879 coro_stacksize = new_stacksize; 2069 cctx_stacksize = new_stacksize;
2070 ++cctx_gen;
2071 }
2072 OUTPUT:
2073 RETVAL
2074
2075int
2076cctx_max_idle (int max_idle = 0)
2077 CODE:
2078 RETVAL = cctx_max_idle;
2079 if (max_idle > 1)
2080 cctx_max_idle = max_idle;
1880 OUTPUT: 2081 OUTPUT:
1881 RETVAL 2082 RETVAL
1882 2083
1883int 2084int
1884cctx_count () 2085cctx_count ()
1962 RETVAL = boolSV (coro->flags & ix); 2163 RETVAL = boolSV (coro->flags & ix);
1963 OUTPUT: 2164 OUTPUT:
1964 RETVAL 2165 RETVAL
1965 2166
1966void 2167void
2168throw (Coro::State self, SV *throw = &PL_sv_undef)
2169 PROTOTYPE: $;$
2170 CODE:
2171 SvREFCNT_dec (self->throw);
2172 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2173
2174void
1967api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 2175api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1968 2176
1969SV * 2177SV *
1970has_cctx (Coro::State coro) 2178has_cctx (Coro::State coro)
1971 PROTOTYPE: $ 2179 PROTOTYPE: $
1980 CODE: 2188 CODE:
1981 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; 2189 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1982 OUTPUT: 2190 OUTPUT:
1983 RETVAL 2191 RETVAL
1984 2192
1985IV 2193UV
1986rss (Coro::State coro) 2194rss (Coro::State coro)
1987 PROTOTYPE: $ 2195 PROTOTYPE: $
1988 ALIAS: 2196 ALIAS:
1989 usecount = 1 2197 usecount = 1
1990 CODE: 2198 CODE:
2001 CODE: 2209 CODE:
2002 struct coro *coro = SvSTATE (coro_current); 2210 struct coro *coro = SvSTATE (coro_current);
2003 coro->cctx->idle_sp = 0; 2211 coro->cctx->idle_sp = 0;
2004 2212
2005void 2213void
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) 2214swap_defsv (Coro::State self)
2014 PROTOTYPE: $ 2215 PROTOTYPE: $
2015 ALIAS: 2216 ALIAS:
2016 swap_defav = 1 2217 swap_defav = 1
2017 CODE: 2218 CODE:
2049 2250
2050 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 2251 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
2051 coro_ready[i] = newAV (); 2252 coro_ready[i] = newAV ();
2052 2253
2053 { 2254 {
2054 SV *sv = perl_get_sv ("Coro::API", TRUE); 2255 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2055 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
2056 2256
2057 coroapi.schedule = api_schedule; 2257 coroapi.schedule = api_schedule;
2058 coroapi.cede = api_cede; 2258 coroapi.cede = api_cede;
2059 coroapi.cede_notself = api_cede_notself; 2259 coroapi.cede_notself = api_cede_notself;
2060 coroapi.ready = api_ready; 2260 coroapi.ready = api_ready;
2071void 2271void
2072_set_current (SV *current) 2272_set_current (SV *current)
2073 PROTOTYPE: $ 2273 PROTOTYPE: $
2074 CODE: 2274 CODE:
2075 SvREFCNT_dec (SvRV (coro_current)); 2275 SvREFCNT_dec (SvRV (coro_current));
2076 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); 2276 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
2077 2277
2078void 2278void
2079_set_readyhook (SV *hook) 2279_set_readyhook (SV *hook)
2080 PROTOTYPE: $ 2280 PROTOTYPE: $
2081 CODE: 2281 CODE:
2141 SvREFCNT_dec (old); 2341 SvREFCNT_dec (old);
2142 croak ("\3async_pool terminate\2\n"); 2342 croak ("\3async_pool terminate\2\n");
2143 } 2343 }
2144 2344
2145 SvREFCNT_dec (coro->saved_deffh); 2345 SvREFCNT_dec (coro->saved_deffh);
2146 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2346 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2147 2347
2148 hv_store (hv, "desc", sizeof ("desc") - 1, 2348 hv_store (hv, "desc", sizeof ("desc") - 1,
2149 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 2349 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2150 2350
2151 invoke_av = (AV *)SvRV (invoke); 2351 invoke_av = (AV *)SvRV (invoke);
2155 2355
2156 if (len > 0) 2356 if (len > 0)
2157 { 2357 {
2158 av_fill (defav, len - 1); 2358 av_fill (defav, len - 1);
2159 for (i = 0; i < len; ++i) 2359 for (i = 0; i < len; ++i)
2160 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2360 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
2161 } 2361 }
2162 2362
2163 SvREFCNT_dec (invoke); 2363 SvREFCNT_dec (invoke);
2164} 2364}
2165 2365
2172 sv_setsv (cb, &PL_sv_undef); 2372 sv_setsv (cb, &PL_sv_undef);
2173 2373
2174 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2374 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2175 coro->saved_deffh = 0; 2375 coro->saved_deffh = 0;
2176 2376
2177 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2377 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2178 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2378 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2179 { 2379 {
2180 SV *old = PL_diehook; 2380 SV *old = PL_diehook;
2181 PL_diehook = 0; 2381 PL_diehook = 0;
2182 SvREFCNT_dec (old); 2382 SvREFCNT_dec (old);
2244#endif 2444#endif
2245 2445
2246 2446
2247MODULE = Coro::State PACKAGE = Coro::AIO 2447MODULE = Coro::State PACKAGE = Coro::AIO
2248 2448
2249SV * 2449void
2250_get_state () 2450_get_state (SV *self)
2251 CODE: 2451 PPCODE:
2252{ 2452{
2253 struct io_state *data; 2453 AV *defav = GvAV (PL_defgv);
2254 2454 AV *av = newAV ();
2455 int i;
2255 RETVAL = newSV (sizeof (struct io_state)); 2456 SV *data_sv = newSV (sizeof (struct io_state));
2256 data = (struct io_state *)SvPVX (RETVAL); 2457 struct io_state *data = (struct io_state *)SvPVX (data_sv);
2257 SvCUR_set (RETVAL, sizeof (struct io_state)); 2458 SvCUR_set (data_sv, sizeof (struct io_state));
2258 SvPOK_only (RETVAL); 2459 SvPOK_only (data_sv);
2259 2460
2260 data->errorno = errno; 2461 data->errorno = errno;
2261 data->laststype = PL_laststype; 2462 data->laststype = PL_laststype;
2262 data->laststatval = PL_laststatval; 2463 data->laststatval = PL_laststatval;
2263 data->statcache = PL_statcache; 2464 data->statcache = PL_statcache;
2465
2466 av_extend (av, AvFILLp (defav) + 1 + 1);
2467
2468 for (i = 0; i <= AvFILLp (defav); ++i)
2469 av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i]));
2470
2471 av_push (av, data_sv);
2472
2473 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2474
2475 api_ready (self);
2264} 2476}
2265 OUTPUT:
2266 RETVAL
2267 2477
2268void 2478void
2269_set_state (char *data_) 2479_set_state (SV *state)
2270 PROTOTYPE: $ 2480 PROTOTYPE: $
2271 CODE: 2481 PPCODE:
2272{ 2482{
2273 struct io_state *data = (void *)data_; 2483 AV *av = (AV *)SvRV (state);
2484 struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]);
2485 int i;
2274 2486
2275 errno = data->errorno; 2487 errno = data->errorno;
2276 PL_laststype = data->laststype; 2488 PL_laststype = data->laststype;
2277 PL_laststatval = data->laststatval; 2489 PL_laststatval = data->laststatval;
2278 PL_statcache = data->statcache; 2490 PL_statcache = data->statcache;
2491
2492 EXTEND (SP, AvFILLp (av));
2493 for (i = 0; i < AvFILLp (av); ++i)
2494 PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i])));
2279} 2495}
2280 2496
2281 2497
2282MODULE = Coro::State PACKAGE = Coro::AnyEvent 2498MODULE = Coro::State PACKAGE = Coro::AnyEvent
2283 2499
2312 2528
2313MODULE = Coro::State PACKAGE = PerlIO::cede 2529MODULE = Coro::State PACKAGE = PerlIO::cede
2314 2530
2315BOOT: 2531BOOT:
2316 PerlIO_define_layer (aTHX_ &PerlIO_cede); 2532 PerlIO_define_layer (aTHX_ &PerlIO_cede);
2533

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines