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.264 by root, Fri Nov 14 02:29:09 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)
700#ifndef MgPV_nolen_const 723#ifndef MgPV_nolen_const
701#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \ 724#define MgPV_nolen_const(mg) (((((int)(mg)->mg_len)) == HEf_SVKEY) ? \
702 SvPV_nolen((SV*)((mg)->mg_ptr)) : \ 725 SvPV_nolen((SV*)((mg)->mg_ptr)) : \
703 (const char*)(mg)->mg_ptr) 726 (const char*)(mg)->mg_ptr)
704#endif 727#endif
728
729/* we sometimes need to create the effect of entersub calling us */
730#define SSL_HEAD (void)0
731/* we somtimes need to create the effect of leaving via entersub */
732#define SSL_TAIL (void)0
705 733
706/* 734/*
707 * This overrides the default magic get method of %SIG elements. 735 * This overrides the default magic get method of %SIG elements.
708 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 736 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
709 * and instead of tryign to save and restore the hash elements, we just provide 737 * and instead of tryign to save and restore the hash elements, we just provide
810 GvSV (PL_defgv) = newSV (0); 838 GvSV (PL_defgv) = newSV (0);
811 GvAV (PL_defgv) = coro->args; coro->args = 0; 839 GvAV (PL_defgv) = coro->args; coro->args = 0;
812 GvSV (PL_errgv) = newSV (0); 840 GvSV (PL_errgv) = newSV (0);
813 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 841 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
814 PL_rs = newSVsv (GvSV (irsgv)); 842 PL_rs = newSVsv (GvSV (irsgv));
815 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); 843 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
816 844
817 { 845 {
818 dSP; 846 dSP;
819 LOGOP myop; 847 UNOP myop;
820 848
821 Zero (&myop, 1, LOGOP); 849 Zero (&myop, 1, UNOP);
822 myop.op_next = Nullop; 850 myop.op_next = Nullop;
823 myop.op_flags = OPf_WANT_VOID; 851 myop.op_flags = OPf_WANT_VOID;
824 852
825 PUSHMARK (SP); 853 PUSHMARK (SP);
826 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv)))); 854 XPUSHs (sv_2mortal (av_shift (GvAV (PL_defgv))));
830 SPAGAIN; 858 SPAGAIN;
831 } 859 }
832 860
833 /* this newly created coroutine might be run on an existing cctx which most 861 /* this newly created coroutine might be run on an existing cctx which most
834 * likely was suspended in set_stacklevel, called from entersub. 862 * likely was suspended in set_stacklevel, called from entersub.
835 * set_stacklevl doesn't do anything on return, but entersub does LEAVE, 863 * set_stacklevel doesn't do anything on return, but entersub does LEAVE,
836 * so we ENTER here for symmetry 864 * so we ENTER here for symmetry.
837 */ 865 */
838 ENTER; 866 SSL_HEAD;
839} 867}
840 868
841static void 869static void
842coro_destroy (pTHX_ struct coro *coro) 870coro_destruct (pTHX_ struct coro *coro)
843{ 871{
844 if (!IN_DESTRUCT) 872 if (!IN_DESTRUCT)
845 { 873 {
846 /* restore all saved variables and stuff */ 874 /* restore all saved variables and stuff */
847 LEAVE_SCOPE (0); 875 LEAVE_SCOPE (0);
869 SvREFCNT_dec (PL_warnhook); 897 SvREFCNT_dec (PL_warnhook);
870 898
871 SvREFCNT_dec (coro->saved_deffh); 899 SvREFCNT_dec (coro->saved_deffh);
872 SvREFCNT_dec (coro->throw); 900 SvREFCNT_dec (coro->throw);
873 901
874 coro_destroy_stacks (aTHX); 902 coro_destruct_stacks (aTHX);
875} 903}
876 904
877static void 905INLINE void
878free_coro_mortal (pTHX) 906free_coro_mortal (pTHX)
879{ 907{
880 if (expect_true (coro_mortal)) 908 if (expect_true (coro_mortal))
881 { 909 {
882 SvREFCNT_dec (coro_mortal); 910 SvREFCNT_dec (coro_mortal);
916 : cx->blk_gimme == G_SCALAR ? bot + 1 944 : cx->blk_gimme == G_SCALAR ? bot + 1
917 : bot; 945 : bot;
918 946
919 av_extend (av, top - bot); 947 av_extend (av, top - bot);
920 while (bot < top) 948 while (bot < top)
921 av_push (av, SvREFCNT_inc (*bot++)); 949 av_push (av, SvREFCNT_inc_NN (*bot++));
922 950
923 PL_runops = RUNOPS_DEFAULT; 951 PL_runops = RUNOPS_DEFAULT;
924 ENTER; 952 ENTER;
925 SAVETMPS; 953 SAVETMPS;
926 EXTEND (SP, 3); 954 EXTEND (SP, 3);
1006 1034
1007 TAINT_NOT; 1035 TAINT_NOT;
1008 return 0; 1036 return 0;
1009} 1037}
1010 1038
1039static void
1040prepare_set_stacklevel (struct transfer_args *ta, struct coro_cctx *cctx)
1041{
1042 ta->prev = (struct coro *)cctx;
1043 ta->next = 0;
1044}
1045
1011/* inject a fake call to Coro::State::_cctx_init into the execution */ 1046/* 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 */ 1047/* _cctx_init should be careful, as it could be called at almost any time */
1013/* during execution of a perl program */ 1048/* during execution of a perl program */
1049/* also initialises PL_top_env */
1014static void NOINLINE 1050static void NOINLINE
1015cctx_prepare (pTHX_ coro_cctx *cctx) 1051cctx_prepare (pTHX_ coro_cctx *cctx)
1016{ 1052{
1017 dSP; 1053 dSP;
1018 LOGOP myop; 1054 UNOP myop;
1019 1055
1020 PL_top_env = &PL_start_env; 1056 PL_top_env = &PL_start_env;
1021 1057
1022 if (cctx->flags & CC_TRACE) 1058 if (cctx->flags & CC_TRACE)
1023 PL_runops = runops_trace; 1059 PL_runops = runops_trace;
1024 1060
1025 Zero (&myop, 1, LOGOP); 1061 Zero (&myop, 1, UNOP);
1026 myop.op_next = PL_op; 1062 myop.op_next = PL_op;
1027 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 1063 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
1028 1064
1029 PUSHMARK (SP); 1065 PUSHMARK (SP);
1030 EXTEND (SP, 2); 1066 EXTEND (SP, 2);
1031 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx)))); 1067 PUSHs (sv_2mortal (newSViv ((IV)cctx)));
1032 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 1068 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
1033 PUTBACK; 1069 PUTBACK;
1034 PL_op = (OP *)&myop; 1070 PL_op = (OP *)&myop;
1035 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 1071 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1036 SPAGAIN; 1072 SPAGAIN;
1037} 1073}
1038 1074
1075/* the tail of transfer: execute stuff we can only do after a transfer */
1076INLINE void
1077transfer_tail (pTHX)
1078{
1079 struct coro *next = (struct coro *)transfer_next;
1080 assert (!(transfer_next = 0)); /* just used for the side effect when asserts are enabled */
1081 assert (("FATAL: next coroutine was zero in transfer_tail (please report)", next));
1082
1083 free_coro_mortal (aTHX);
1084 UNLOCK;
1085
1086 if (expect_false (next->throw))
1087 {
1088 SV *exception = sv_2mortal (next->throw);
1089
1090 next->throw = 0;
1091 sv_setsv (ERRSV, exception);
1092 croak (0);
1093 }
1094}
1095
1039/* 1096/*
1040 * this is a _very_ stripped down perl interpreter ;) 1097 * this is a _very_ stripped down perl interpreter ;)
1041 */ 1098 */
1042static void 1099static void
1043cctx_run (void *arg) 1100cctx_run (void *arg)
1044{ 1101{
1102#ifdef USE_ITHREADS
1103# if CORO_PTHREAD
1104 PERL_SET_CONTEXT (coro_thx);
1105# endif
1106#endif
1107 {
1045 dTHX; 1108 dTHX;
1046 1109
1047 /* cctx_run is the alternative tail of transfer(), so unlock here. */ 1110 /* entersub called ENTER, but we never 'returned', undo that here */
1048 UNLOCK; 1111 SSL_TAIL;
1049 1112
1050 /* we now skip the entersub that lead to transfer() */ 1113 /* we now skip the op that did lead to transfer() */
1051 PL_op = PL_op->op_next; 1114 PL_op = PL_op->op_next;
1052 1115
1053 /* inject a fake subroutine call to cctx_init */ 1116 /* inject a fake subroutine call to cctx_init */
1054 cctx_prepare (aTHX_ (coro_cctx *)arg); 1117 cctx_prepare (aTHX_ (coro_cctx *)arg);
1055 1118
1119 /* cctx_run is the alternative tail of transfer() */
1120 transfer_tail (aTHX);
1121
1056 /* somebody or something will hit me for both perl_run and PL_restartop */ 1122 /* somebody or something will hit me for both perl_run and PL_restartop */
1057 PL_restartop = PL_op; 1123 PL_restartop = PL_op;
1058 perl_run (PL_curinterp); 1124 perl_run (PL_curinterp);
1059 1125
1060 /* 1126 /*
1061 * If perl-run returns we assume exit() was being called or the coro 1127 * 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) 1128 * 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 1129 * 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" 1130 * bootstrap-time "top" top_env, as we cannot restore the "main"
1065 * coroutine as Coro has no such concept 1131 * coroutine as Coro has no such concept
1066 */ 1132 */
1067 PL_top_env = main_top_env; 1133 PL_top_env = main_top_env;
1068 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1134 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1135 }
1069} 1136}
1070 1137
1071static coro_cctx * 1138static coro_cctx *
1072cctx_new () 1139cctx_new ()
1073{ 1140{
1074 coro_cctx *cctx; 1141 coro_cctx *cctx;
1142
1143 ++cctx_count;
1144 New (0, cctx, 1, coro_cctx);
1145
1146 cctx->gen = cctx_gen;
1147 cctx->flags = 0;
1148 cctx->idle_sp = 0; /* can be accessed by transfer between cctx_run and set_stacklevel, on throw */
1149
1150 return cctx;
1151}
1152
1153/* create a new cctx only suitable as source */
1154static coro_cctx *
1155cctx_new_empty ()
1156{
1157 coro_cctx *cctx = cctx_new ();
1158
1159 cctx->sptr = 0;
1160 coro_create (&cctx->cctx, 0, 0, 0, 0);
1161
1162 return cctx;
1163}
1164
1165/* create a new cctx suitable as destination/running a perl interpreter */
1166static coro_cctx *
1167cctx_new_run ()
1168{
1169 coro_cctx *cctx = cctx_new ();
1075 void *stack_start; 1170 void *stack_start;
1076 size_t stack_size; 1171 size_t stack_size;
1077 1172
1078 ++cctx_count;
1079
1080 Newz (0, cctx, 1, coro_cctx);
1081
1082#if HAVE_MMAP 1173#if HAVE_MMAP
1083 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1174 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1084 /* mmap supposedly does allocate-on-write for us */ 1175 /* 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); 1176 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
1086 1177
1087 if (cctx->sptr != (void *)-1) 1178 if (cctx->sptr != (void *)-1)
1088 { 1179 {
1089# if CORO_STACKGUARD 1180 #if CORO_STACKGUARD
1090 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1181 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1091# endif 1182 #endif
1092 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 1183 stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE;
1093 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 1184 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
1094 cctx->flags |= CC_MAPPED; 1185 cctx->flags |= CC_MAPPED;
1095 } 1186 }
1096 else 1187 else
1097#endif 1188#endif
1098 { 1189 {
1099 cctx->ssize = coro_stacksize * (long)sizeof (long); 1190 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1100 New (0, cctx->sptr, coro_stacksize, long); 1191 New (0, cctx->sptr, cctx_stacksize, long);
1101 1192
1102 if (!cctx->sptr) 1193 if (!cctx->sptr)
1103 { 1194 {
1104 perror ("FATAL: unable to allocate stack for coroutine"); 1195 perror ("FATAL: unable to allocate stack for coroutine, exiting.");
1105 _exit (EXIT_FAILURE); 1196 _exit (EXIT_FAILURE);
1106 } 1197 }
1107 1198
1108 stack_start = cctx->sptr; 1199 stack_start = cctx->sptr;
1109 stack_size = cctx->ssize; 1200 stack_size = cctx->ssize;
1110 } 1201 }
1111 1202
1112 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); 1203 #if CORO_USE_VALGRIND
1204 cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size);
1205 #endif
1206
1113 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); 1207 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size);
1114 1208
1115 return cctx; 1209 return cctx;
1116} 1210}
1117 1211
1120{ 1214{
1121 if (!cctx) 1215 if (!cctx)
1122 return; 1216 return;
1123 1217
1124 --cctx_count; 1218 --cctx_count;
1219 coro_destroy (&cctx->cctx);
1125 1220
1221 /* coro_transfer creates new, empty cctx's */
1222 if (cctx->sptr)
1223 {
1126#if CORO_USE_VALGRIND 1224 #if CORO_USE_VALGRIND
1127 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1225 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1128#endif 1226 #endif
1129 1227
1130#if HAVE_MMAP 1228#if HAVE_MMAP
1131 if (cctx->flags & CC_MAPPED) 1229 if (cctx->flags & CC_MAPPED)
1132 munmap (cctx->sptr, cctx->ssize); 1230 munmap (cctx->sptr, cctx->ssize);
1133 else 1231 else
1134#endif 1232#endif
1135 Safefree (cctx->sptr); 1233 Safefree (cctx->sptr);
1234 }
1136 1235
1137 Safefree (cctx); 1236 Safefree (cctx);
1138} 1237}
1139 1238
1140/* wether this cctx should be destructed */ 1239/* wether this cctx should be destructed */
1141#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) 1240#define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE))
1142 1241
1143static coro_cctx * 1242static coro_cctx *
1144cctx_get (pTHX) 1243cctx_get (pTHX)
1145{ 1244{
1146 while (expect_true (cctx_first)) 1245 while (expect_true (cctx_first))
1153 return cctx; 1252 return cctx;
1154 1253
1155 cctx_destroy (cctx); 1254 cctx_destroy (cctx);
1156 } 1255 }
1157 1256
1158 return cctx_new (); 1257 return cctx_new_run ();
1159} 1258}
1160 1259
1161static void 1260static void
1162cctx_put (coro_cctx *cctx) 1261cctx_put (coro_cctx *cctx)
1163{ 1262{
1263 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr));
1264
1164 /* free another cctx if overlimit */ 1265 /* free another cctx if overlimit */
1165 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1266 if (expect_false (cctx_idle >= cctx_max_idle))
1166 { 1267 {
1167 coro_cctx *first = cctx_first; 1268 coro_cctx *first = cctx_first;
1168 cctx_first = first->next; 1269 cctx_first = first->next;
1169 --cctx_idle; 1270 --cctx_idle;
1170 1271
1202/* always use the TRANSFER macro */ 1303/* always use the TRANSFER macro */
1203static void NOINLINE 1304static void NOINLINE
1204transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1305transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1205{ 1306{
1206 dSTACKLEVEL; 1307 dSTACKLEVEL;
1207 static volatile int has_throw;
1208 1308
1209 /* sometimes transfer is only called to set idle_sp */ 1309 /* sometimes transfer is only called to set idle_sp */
1210 if (expect_false (!next)) 1310 if (expect_false (!next))
1211 { 1311 {
1212 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1312 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1216 { 1316 {
1217 coro_cctx *prev__cctx; 1317 coro_cctx *prev__cctx;
1218 1318
1219 if (expect_false (prev->flags & CF_NEW)) 1319 if (expect_false (prev->flags & CF_NEW))
1220 { 1320 {
1221 /* create a new empty context */ 1321 /* create a new empty/source context */
1222 Newz (0, prev->cctx, 1, coro_cctx); 1322 prev->cctx = cctx_new_empty ();
1223 prev->flags &= ~CF_NEW; 1323 prev->flags &= ~CF_NEW;
1224 prev->flags |= CF_RUNNING; 1324 prev->flags |= CF_RUNNING;
1225 } 1325 }
1226 1326
1227 prev->flags &= ~CF_RUNNING; 1327 prev->flags &= ~CF_RUNNING;
1242 else 1342 else
1243 load_perl (aTHX_ next); 1343 load_perl (aTHX_ next);
1244 1344
1245 prev__cctx = prev->cctx; 1345 prev__cctx = prev->cctx;
1246 1346
1247 /* possibly "free" the cctx */ 1347 /* possibly untie and reuse the cctx */
1248 if (expect_true ( 1348 if (expect_true (
1249 prev__cctx->idle_sp == STACKLEVEL 1349 prev__cctx->idle_sp == STACKLEVEL
1250 && !(prev__cctx->flags & CC_TRACE) 1350 && !(prev__cctx->flags & CC_TRACE)
1251 && !force_cctx 1351 && !force_cctx
1252 )) 1352 ))
1253 { 1353 {
1254 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1354 /* 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)); 1355 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == prev__cctx->idle_te));
1256 1356
1257 prev->cctx = 0; 1357 prev->cctx = 0;
1258 1358
1259 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1359 /* 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 */ 1360 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1268 ++next->usecount; 1368 ++next->usecount;
1269 1369
1270 if (expect_true (!next->cctx)) 1370 if (expect_true (!next->cctx))
1271 next->cctx = cctx_get (aTHX); 1371 next->cctx = cctx_get (aTHX);
1272 1372
1273 has_throw = !!next->throw; 1373 assert (("FATAL: transfer_next already nonzero in Coro (please report)", !transfer_next));
1374 transfer_next = next;
1274 1375
1275 if (expect_false (prev__cctx != next->cctx)) 1376 if (expect_false (prev__cctx != next->cctx))
1276 { 1377 {
1277 prev__cctx->top_env = PL_top_env; 1378 prev__cctx->top_env = PL_top_env;
1278 PL_top_env = next->cctx->top_env; 1379 PL_top_env = next->cctx->top_env;
1279 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 1380 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
1280 } 1381 }
1281 1382
1282 free_coro_mortal (aTHX); 1383 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 } 1384 }
1298} 1385}
1299
1300struct transfer_args
1301{
1302 struct coro *prev, *next;
1303};
1304 1386
1305#define TRANSFER(ta, force_cctx) transfer (aTHX_ (ta).prev, (ta).next, (force_cctx)) 1387#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) 1388#define TRANSFER_CHECK(ta) transfer_check (aTHX_ (ta).prev, (ta).next)
1307 1389
1308/** high level stuff ********************************************************/ 1390/** high level stuff ********************************************************/
1334 croak ("FATAL: tried to destroy currently running coroutine"); 1416 croak ("FATAL: tried to destroy currently running coroutine");
1335 1417
1336 save_perl (aTHX_ &temp); 1418 save_perl (aTHX_ &temp);
1337 load_perl (aTHX_ coro); 1419 load_perl (aTHX_ coro);
1338 1420
1339 coro_destroy (aTHX_ coro); 1421 coro_destruct (aTHX_ coro);
1340 1422
1341 load_perl (aTHX_ &temp); 1423 load_perl (aTHX_ &temp);
1342 1424
1343 coro->slot = 0; 1425 coro->slot = 0;
1344 } 1426 }
1450 LOCK; 1532 LOCK;
1451 1533
1452 sv_hook = coro_nready ? 0 : coro_readyhook; 1534 sv_hook = coro_nready ? 0 : coro_readyhook;
1453 xs_hook = coro_nready ? 0 : coroapi.readyhook; 1535 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1454 1536
1455 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1537 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1456 ++coro_nready; 1538 ++coro_nready;
1457 1539
1458 UNLOCK; 1540 UNLOCK;
1459 1541
1460 if (sv_hook) 1542 if (sv_hook)
1481 1563
1482static int 1564static int
1483api_is_ready (SV *coro_sv) 1565api_is_ready (SV *coro_sv)
1484{ 1566{
1485 dTHX; 1567 dTHX;
1568
1486 return !!(SvSTATE (coro_sv)->flags & CF_READY); 1569 return !!(SvSTATE (coro_sv)->flags & CF_READY);
1487} 1570}
1488 1571
1489static void 1572INLINE void
1490prepare_schedule (pTHX_ struct transfer_args *ta) 1573prepare_schedule (pTHX_ struct transfer_args *ta)
1491{ 1574{
1492 SV *prev_sv, *next_sv; 1575 SV *prev_sv, *next_sv;
1493 1576
1494 for (;;) 1577 for (;;)
1520 /* cannot transfer to destroyed coros, skip and look for next */ 1603 /* cannot transfer to destroyed coros, skip and look for next */
1521 if (expect_false (ta->next->flags & CF_DESTROYED)) 1604 if (expect_false (ta->next->flags & CF_DESTROYED))
1522 { 1605 {
1523 UNLOCK; 1606 UNLOCK;
1524 SvREFCNT_dec (next_sv); 1607 SvREFCNT_dec (next_sv);
1525 /* coro_nready is already taken care of by destroy */ 1608 /* coro_nready has already been taken care of by destroy */
1526 continue; 1609 continue;
1527 } 1610 }
1528 1611
1529 --coro_nready; 1612 --coro_nready;
1530 UNLOCK; 1613 UNLOCK;
1533 1616
1534 /* free this only after the transfer */ 1617 /* free this only after the transfer */
1535 prev_sv = SvRV (coro_current); 1618 prev_sv = SvRV (coro_current);
1536 ta->prev = SvSTATE (prev_sv); 1619 ta->prev = SvSTATE (prev_sv);
1537 TRANSFER_CHECK (*ta); 1620 TRANSFER_CHECK (*ta);
1538 assert (ta->next->flags & CF_READY); 1621 assert (("FATAL: next coroutine isn't marked as ready in Coro (please report)", ta->next->flags & CF_READY));
1539 ta->next->flags &= ~CF_READY; 1622 ta->next->flags &= ~CF_READY;
1540 SvRV_set (coro_current, next_sv); 1623 SvRV_set (coro_current, next_sv);
1541 1624
1542 LOCK; 1625 LOCK;
1543 free_coro_mortal (aTHX); 1626 free_coro_mortal (aTHX);
1544 coro_mortal = prev_sv; 1627 coro_mortal = prev_sv;
1545 UNLOCK; 1628 UNLOCK;
1546} 1629}
1547 1630
1548static void 1631INLINE void
1549prepare_cede (pTHX_ struct transfer_args *ta) 1632prepare_cede (pTHX_ struct transfer_args *ta)
1550{ 1633{
1551 api_ready (coro_current); 1634 api_ready (coro_current);
1552 prepare_schedule (aTHX_ ta); 1635 prepare_schedule (aTHX_ ta);
1553} 1636}
1615 struct coro *coro = SvSTATE (coro_sv); 1698 struct coro *coro = SvSTATE (coro_sv);
1616 1699
1617 if (flags & CC_TRACE) 1700 if (flags & CC_TRACE)
1618 { 1701 {
1619 if (!coro->cctx) 1702 if (!coro->cctx)
1620 coro->cctx = cctx_new (); 1703 coro->cctx = cctx_new_run ();
1621 else if (!(coro->cctx->flags & CC_TRACE)) 1704 else if (!(coro->cctx->flags & CC_TRACE))
1622 croak ("cannot enable tracing on coroutine with custom stack"); 1705 croak ("cannot enable tracing on coroutine with custom stack");
1623 1706
1624 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1707 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1625 } 1708 }
1632 else 1715 else
1633 coro->slot->runops = RUNOPS_DEFAULT; 1716 coro->slot->runops = RUNOPS_DEFAULT;
1634 } 1717 }
1635} 1718}
1636 1719
1720#if 0
1637static int 1721static int
1638coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) 1722coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1639{ 1723{
1640 AV *padlist; 1724 AV *padlist;
1641 AV *av = (AV *)mg->mg_obj; 1725 AV *av = (AV *)mg->mg_obj;
1647 1731
1648static MGVTBL coro_gensub_vtbl = { 1732static MGVTBL coro_gensub_vtbl = {
1649 0, 0, 0, 0, 1733 0, 0, 0, 0,
1650 coro_gensub_free 1734 coro_gensub_free
1651}; 1735};
1736#endif
1652 1737
1653/*****************************************************************************/ 1738/*****************************************************************************/
1654/* PerlIO::cede */ 1739/* PerlIO::cede */
1655 1740
1656typedef struct 1741typedef struct
1662static IV 1747static IV
1663PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) 1748PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1664{ 1749{
1665 PerlIOCede *self = PerlIOSelf (f, PerlIOCede); 1750 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1666 1751
1667 self->every = SvNV (arg); 1752 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
1668 self->next = nvtime () + self->every; 1753 self->next = nvtime () + self->every;
1669 1754
1670 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); 1755 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
1671} 1756}
1672 1757
1688 { 1773 {
1689 api_cede (); 1774 api_cede ();
1690 self->next = now + self->every; 1775 self->next = now + self->every;
1691 } 1776 }
1692 1777
1693 return PerlIOBuf_flush (f); 1778 return PerlIOBuf_flush (aTHX_ f);
1694} 1779}
1695 1780
1696static PerlIO_funcs PerlIO_cede = 1781static PerlIO_funcs PerlIO_cede =
1697{ 1782{
1698 sizeof(PerlIO_funcs), 1783 sizeof(PerlIO_funcs),
1723 PerlIOBuf_get_ptr, 1808 PerlIOBuf_get_ptr,
1724 PerlIOBuf_get_cnt, 1809 PerlIOBuf_get_cnt,
1725 PerlIOBuf_set_ptrcnt, 1810 PerlIOBuf_set_ptrcnt,
1726}; 1811};
1727 1812
1813/*****************************************************************************/
1814
1815static const CV *ssl_cv; /* for quick consistency check */
1816
1817static UNOP ssl_restore; /* restore stack as entersub did, for first-re-run */
1818static SV *ssl_arg0;
1819static SV *ssl_arg1;
1820
1821/* this restores the stack in the case we patched the entersub, to */
1822/* recreate the stack frame as perl will on following calls */
1823/* since entersub cleared the stack */
1824static OP *
1825pp_restore (pTHX)
1826{
1827 dSP;
1828
1829 PUSHMARK (SP);
1830
1831 EXTEND (SP, 3);
1832 if (ssl_arg0) PUSHs (sv_2mortal (ssl_arg0)), ssl_arg0 = 0;
1833 if (ssl_arg1) PUSHs (sv_2mortal (ssl_arg1)), ssl_arg1 = 0;
1834 PUSHs ((SV *)CvGV (ssl_cv));
1835
1836 RETURNOP (ssl_restore.op_first);
1837}
1838
1839/* declare prototype */
1840XS(XS_Coro__State__set_stacklevel);
1841
1842static OP *
1843pp_set_stacklevel (pTHX)
1844{
1845 dSP;
1846 struct transfer_args ta;
1847 SV **arg = PL_stack_base + TOPMARK + 1;
1848 int items = SP - arg; /* args without function object */
1849
1850 /* do a quick consistency check on the "function" object, and if it isn't */
1851 /* for us, divert to the real entersub */
1852 if (SvTYPE (*sp) != SVt_PVGV || CvXSUB (GvCV (*sp)) != XS_Coro__State__set_stacklevel)
1853 return PL_ppaddr[OP_ENTERSUB](aTHX);
1854
1855 /* pop args */
1856 SP = PL_stack_base + POPMARK;
1857
1858 if (!(PL_op->op_flags & OPf_STACKED))
1859 {
1860 /* ampersand-form of call, use @_ instead of stack */
1861 AV *av = GvAV (PL_defgv);
1862 arg = AvARRAY (av);
1863 items = AvFILLp (av) + 1;
1864 }
1865
1866 PUTBACK;
1867 switch (PL_op->op_private & 7)
1868 {
1869 case 0:
1870 prepare_set_stacklevel (&ta, (struct coro_cctx *)SvIV (arg [0]));
1871 break;
1872
1873 case 1:
1874 if (items != 2)
1875 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d.", items);
1876
1877 prepare_transfer (aTHX_ &ta, arg [0], arg [1]);
1878 break;
1879
1880 case 2:
1881 prepare_schedule (aTHX_ &ta);
1882 break;
1883
1884 case 3:
1885 prepare_cede (aTHX_ &ta);
1886 break;
1887
1888 case 4:
1889 if (!prepare_cede_notself (aTHX_ &ta))
1890 RETURN;
1891
1892 break;
1893 }
1894
1895 TRANSFER (ta, 0);
1896 SPAGAIN;
1897
1898skip:
1899
1900 RETURN;
1901}
1728 1902
1729MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1903MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1730 1904
1731PROTOTYPES: DISABLE 1905PROTOTYPES: 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 1906
1809# these not obviously related functions are all rolled into the same xs 1907# 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 1908# function to increase chances that they all will call transfer with the same
1811# stack offset 1909# stack offset
1812void 1910void
1816 Coro::schedule = 2 1914 Coro::schedule = 2
1817 Coro::cede = 3 1915 Coro::cede = 3
1818 Coro::cede_notself = 4 1916 Coro::cede_notself = 4
1819 CODE: 1917 CODE:
1820{ 1918{
1821 struct transfer_args ta; 1919 assert (("FATAL: ssl call recursion in Coro module (please report)", PL_op->op_ppaddr != pp_set_stacklevel));
1822 1920
1823 PUTBACK; 1921 /* we patch the op, and then re-run the whole call */
1824 switch (ix) 1922 /* we have to put some dummy argument on the stack for this to work */
1923 /* TODO: walk back the opcode chain (but how?), nuke the pp_gv etc. */
1924 ssl_restore.op_next = (OP *)&ssl_restore;
1925 ssl_restore.op_type = OP_NULL;
1926 ssl_restore.op_ppaddr = pp_restore;
1927 ssl_restore.op_first = PL_op;
1928
1929 ssl_arg0 = items > 0 ? SvREFCNT_inc (ST (0)) : 0;
1930 ssl_arg1 = items > 1 ? SvREFCNT_inc (ST (1)) : 0;
1931
1932 PL_op->op_ppaddr = pp_set_stacklevel;
1933 PL_op->op_private = PL_op->op_private & ~7 | ix; /* we potentially share our private flags with entersub */
1934
1935 PL_op = (OP *)&ssl_restore;
1936}
1937
1938BOOT:
1939{
1940#ifdef USE_ITHREADS
1941 MUTEX_INIT (&coro_lock);
1942# if CORO_PTHREAD
1943 coro_thx = PERL_GET_CONTEXT;
1944# endif
1945#endif
1946 BOOT_PAGESIZE;
1947
1948 ssl_cv = get_cv ("Coro::State::_set_stacklevel", 0);
1949
1950 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1951 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1952
1953 orig_sigelem_get = PL_vtbl_sigelem.svt_get; PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1954 orig_sigelem_set = PL_vtbl_sigelem.svt_set; PL_vtbl_sigelem.svt_set = coro_sigelem_set;
1955 orig_sigelem_clr = PL_vtbl_sigelem.svt_clear; PL_vtbl_sigelem.svt_clear = coro_sigelem_clr;
1956
1957 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1958 rv_diehook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1959 rv_warnhook = newRV_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1960
1961 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1962
1963 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1964 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1965 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1966 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1967
1968 main_mainstack = PL_mainstack;
1969 main_top_env = PL_top_env;
1970
1971 while (main_top_env->je_prev)
1972 main_top_env = main_top_env->je_prev;
1973
1974 coroapi.ver = CORO_API_VERSION;
1975 coroapi.rev = CORO_API_REVISION;
1976 coroapi.transfer = api_transfer;
1977
1825 { 1978 {
1826 case 0: 1979 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 1980
1831 case 1: 1981 if (!svp) croak ("Time::HiRes is required");
1832 if (items != 2) 1982 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 1983
1835 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 1984 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 } 1985 }
1852 SPAGAIN;
1853 1986
1854 BARRIER; 1987 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} 1988}
1989
1990SV *
1991new (char *klass, ...)
1992 CODE:
1993{
1994 struct coro *coro;
1995 MAGIC *mg;
1996 HV *hv;
1997 int i;
1998
1999 Newz (0, coro, 1, struct coro);
2000 coro->args = newAV ();
2001 coro->flags = CF_NEW;
2002
2003 if (coro_first) coro_first->prev = coro;
2004 coro->next = coro_first;
2005 coro_first = coro;
2006
2007 coro->hv = hv = newHV ();
2008 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
2009 mg->mg_flags |= MGf_DUP;
2010 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
2011
2012 av_extend (coro->args, items - 1);
2013 for (i = 1; i < items; i++)
2014 av_push (coro->args, newSVsv (ST (i)));
2015}
2016 OUTPUT:
2017 RETVAL
1860 2018
1861bool 2019bool
1862_destroy (SV *coro_sv) 2020_destroy (SV *coro_sv)
1863 CODE: 2021 CODE:
1864 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv)); 2022 RETVAL = coro_state_destroy (aTHX_ SvSTATE (coro_sv));
1872 _exit (code); 2030 _exit (code);
1873 2031
1874int 2032int
1875cctx_stacksize (int new_stacksize = 0) 2033cctx_stacksize (int new_stacksize = 0)
1876 CODE: 2034 CODE:
1877 RETVAL = coro_stacksize; 2035 RETVAL = cctx_stacksize;
1878 if (new_stacksize) 2036 if (new_stacksize)
2037 {
1879 coro_stacksize = new_stacksize; 2038 cctx_stacksize = new_stacksize;
2039 ++cctx_gen;
2040 }
2041 OUTPUT:
2042 RETVAL
2043
2044int
2045cctx_max_idle (int max_idle = 0)
2046 CODE:
2047 RETVAL = cctx_max_idle;
2048 if (max_idle > 1)
2049 cctx_max_idle = max_idle;
1880 OUTPUT: 2050 OUTPUT:
1881 RETVAL 2051 RETVAL
1882 2052
1883int 2053int
1884cctx_count () 2054cctx_count ()
1962 RETVAL = boolSV (coro->flags & ix); 2132 RETVAL = boolSV (coro->flags & ix);
1963 OUTPUT: 2133 OUTPUT:
1964 RETVAL 2134 RETVAL
1965 2135
1966void 2136void
2137throw (Coro::State self, SV *throw = &PL_sv_undef)
2138 PROTOTYPE: $;$
2139 CODE:
2140 SvREFCNT_dec (self->throw);
2141 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2142
2143void
1967api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 2144api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
1968 2145
1969SV * 2146SV *
1970has_cctx (Coro::State coro) 2147has_cctx (Coro::State coro)
1971 PROTOTYPE: $ 2148 PROTOTYPE: $
1980 CODE: 2157 CODE:
1981 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; 2158 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1982 OUTPUT: 2159 OUTPUT:
1983 RETVAL 2160 RETVAL
1984 2161
1985IV 2162UV
1986rss (Coro::State coro) 2163rss (Coro::State coro)
1987 PROTOTYPE: $ 2164 PROTOTYPE: $
1988 ALIAS: 2165 ALIAS:
1989 usecount = 1 2166 usecount = 1
1990 CODE: 2167 CODE:
2001 CODE: 2178 CODE:
2002 struct coro *coro = SvSTATE (coro_current); 2179 struct coro *coro = SvSTATE (coro_current);
2003 coro->cctx->idle_sp = 0; 2180 coro->cctx->idle_sp = 0;
2004 2181
2005void 2182void
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) 2183swap_defsv (Coro::State self)
2014 PROTOTYPE: $ 2184 PROTOTYPE: $
2015 ALIAS: 2185 ALIAS:
2016 swap_defav = 1 2186 swap_defav = 1
2017 CODE: 2187 CODE:
2049 2219
2050 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 2220 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
2051 coro_ready[i] = newAV (); 2221 coro_ready[i] = newAV ();
2052 2222
2053 { 2223 {
2054 SV *sv = perl_get_sv ("Coro::API", TRUE); 2224 SV *sv = coro_get_sv (aTHX_ "Coro::API", TRUE);
2055 perl_get_sv ("Coro::API", TRUE); /* silence 5.10 warning */
2056 2225
2057 coroapi.schedule = api_schedule; 2226 coroapi.schedule = api_schedule;
2058 coroapi.cede = api_cede; 2227 coroapi.cede = api_cede;
2059 coroapi.cede_notself = api_cede_notself; 2228 coroapi.cede_notself = api_cede_notself;
2060 coroapi.ready = api_ready; 2229 coroapi.ready = api_ready;
2071void 2240void
2072_set_current (SV *current) 2241_set_current (SV *current)
2073 PROTOTYPE: $ 2242 PROTOTYPE: $
2074 CODE: 2243 CODE:
2075 SvREFCNT_dec (SvRV (coro_current)); 2244 SvREFCNT_dec (SvRV (coro_current));
2076 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); 2245 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
2077 2246
2078void 2247void
2079_set_readyhook (SV *hook) 2248_set_readyhook (SV *hook)
2080 PROTOTYPE: $ 2249 PROTOTYPE: $
2081 CODE: 2250 CODE:
2141 SvREFCNT_dec (old); 2310 SvREFCNT_dec (old);
2142 croak ("\3async_pool terminate\2\n"); 2311 croak ("\3async_pool terminate\2\n");
2143 } 2312 }
2144 2313
2145 SvREFCNT_dec (coro->saved_deffh); 2314 SvREFCNT_dec (coro->saved_deffh);
2146 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2315 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2147 2316
2148 hv_store (hv, "desc", sizeof ("desc") - 1, 2317 hv_store (hv, "desc", sizeof ("desc") - 1,
2149 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 2318 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2150 2319
2151 invoke_av = (AV *)SvRV (invoke); 2320 invoke_av = (AV *)SvRV (invoke);
2155 2324
2156 if (len > 0) 2325 if (len > 0)
2157 { 2326 {
2158 av_fill (defav, len - 1); 2327 av_fill (defav, len - 1);
2159 for (i = 0; i < len; ++i) 2328 for (i = 0; i < len; ++i)
2160 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2329 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
2161 } 2330 }
2162 2331
2163 SvREFCNT_dec (invoke); 2332 SvREFCNT_dec (invoke);
2164} 2333}
2165 2334
2172 sv_setsv (cb, &PL_sv_undef); 2341 sv_setsv (cb, &PL_sv_undef);
2173 2342
2174 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2343 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2175 coro->saved_deffh = 0; 2344 coro->saved_deffh = 0;
2176 2345
2177 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2346 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2178 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2347 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2179 { 2348 {
2180 SV *old = PL_diehook; 2349 SV *old = PL_diehook;
2181 PL_diehook = 0; 2350 PL_diehook = 0;
2182 SvREFCNT_dec (old); 2351 SvREFCNT_dec (old);
2244#endif 2413#endif
2245 2414
2246 2415
2247MODULE = Coro::State PACKAGE = Coro::AIO 2416MODULE = Coro::State PACKAGE = Coro::AIO
2248 2417
2249SV * 2418void
2250_get_state () 2419_get_state (SV *self)
2251 CODE: 2420 PPCODE:
2252{ 2421{
2253 struct io_state *data; 2422 AV *defav = GvAV (PL_defgv);
2254 2423 AV *av = newAV ();
2424 int i;
2255 RETVAL = newSV (sizeof (struct io_state)); 2425 SV *data_sv = newSV (sizeof (struct io_state));
2256 data = (struct io_state *)SvPVX (RETVAL); 2426 struct io_state *data = (struct io_state *)SvPVX (data_sv);
2257 SvCUR_set (RETVAL, sizeof (struct io_state)); 2427 SvCUR_set (data_sv, sizeof (struct io_state));
2258 SvPOK_only (RETVAL); 2428 SvPOK_only (data_sv);
2259 2429
2260 data->errorno = errno; 2430 data->errorno = errno;
2261 data->laststype = PL_laststype; 2431 data->laststype = PL_laststype;
2262 data->laststatval = PL_laststatval; 2432 data->laststatval = PL_laststatval;
2263 data->statcache = PL_statcache; 2433 data->statcache = PL_statcache;
2434
2435 av_extend (av, AvFILLp (defav) + 1 + 1);
2436
2437 for (i = 0; i <= AvFILLp (defav); ++i)
2438 av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i]));
2439
2440 av_push (av, data_sv);
2441
2442 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2443
2444 api_ready (self);
2264} 2445}
2265 OUTPUT:
2266 RETVAL
2267 2446
2268void 2447void
2269_set_state (char *data_) 2448_set_state (SV *state)
2270 PROTOTYPE: $ 2449 PROTOTYPE: $
2271 CODE: 2450 PPCODE:
2272{ 2451{
2273 struct io_state *data = (void *)data_; 2452 AV *av = (AV *)SvRV (state);
2453 struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]);
2454 int i;
2274 2455
2275 errno = data->errorno; 2456 errno = data->errorno;
2276 PL_laststype = data->laststype; 2457 PL_laststype = data->laststype;
2277 PL_laststatval = data->laststatval; 2458 PL_laststatval = data->laststatval;
2278 PL_statcache = data->statcache; 2459 PL_statcache = data->statcache;
2460
2461 EXTEND (SP, AvFILLp (av));
2462 for (i = 0; i < AvFILLp (av); ++i)
2463 PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i])));
2279} 2464}
2280 2465
2281 2466
2282MODULE = Coro::State PACKAGE = Coro::AnyEvent 2467MODULE = Coro::State PACKAGE = Coro::AnyEvent
2283 2468
2312 2497
2313MODULE = Coro::State PACKAGE = PerlIO::cede 2498MODULE = Coro::State PACKAGE = PerlIO::cede
2314 2499
2315BOOT: 2500BOOT:
2316 PerlIO_define_layer (aTHX_ &PerlIO_cede); 2501 PerlIO_define_layer (aTHX_ &PerlIO_cede);
2502

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines