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.245 by root, Mon Sep 22 05:40:21 2008 UTC vs.
Revision 1.259 by root, Mon Nov 10 00:02:29 2008 UTC

4#define PERL_EXT 4#define PERL_EXT
5 5
6#include "EXTERN.h" 6#include "EXTERN.h"
7#include "perl.h" 7#include "perl.h"
8#include "XSUB.h" 8#include "XSUB.h"
9#include "perliol.h"
9 10
10#include "patchlevel.h" 11#include "patchlevel.h"
11 12
12#include <stdio.h> 13#include <stdio.h>
13#include <errno.h> 14#include <errno.h>
51#else 52#else
52# define REGISTER_STACK(cctx,start,end) 53# define REGISTER_STACK(cctx,start,end)
53#endif 54#endif
54 55
55/* the maximum number of idle cctx that will be pooled */ 56/* the maximum number of idle cctx that will be pooled */
56#define MAX_IDLE_CCTX 8 57static int cctx_max_idle = 4;
57 58
58#define PERL_VERSION_ATLEAST(a,b,c) \ 59#define PERL_VERSION_ATLEAST(a,b,c) \
59 (PERL_REVISION > (a) \ 60 (PERL_REVISION > (a) \
60 || (PERL_REVISION == (a) \ 61 || (PERL_REVISION == (a) \
61 && (PERL_VERSION > (b) \ 62 && (PERL_VERSION > (b) \
80# ifndef IS_PADCONST 81# ifndef IS_PADCONST
81# define IS_PADCONST(v) 0 82# define IS_PADCONST(v) 0
82# endif 83# endif
83#endif 84#endif
84 85
86/* 5.11 */
87#ifndef CxHASARGS
88# define CxHASARGS(cx) (cx)->blk_sub.hasargs
89#endif
90
91/* 5.10.0 */
92#ifndef SvREFCNT_inc_NN
93# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv)
94#endif
95
85/* 5.8.8 */ 96/* 5.8.8 */
86#ifndef GV_NOTQUAL 97#ifndef GV_NOTQUAL
87# define GV_NOTQUAL 0 98# define GV_NOTQUAL 0
88#endif 99#endif
89#ifndef newSV 100#ifndef newSV
90# define newSV(l) NEWSV(0,l) 101# define newSV(l) NEWSV(0,l)
91#endif
92
93/* 5.11 */
94#ifndef CxHASARGS
95# define CxHASARGS(cx) (cx)->blk_sub.hasargs
96#endif 102#endif
97 103
98/* 5.8.7 */ 104/* 5.8.7 */
99#ifndef SvRV_set 105#ifndef SvRV_set
100# define SvRV_set(s,v) SvRV(s) = (v) 106# define SvRV_set(s,v) SvRV(s) = (v)
136#define NOINLINE attribute ((noinline)) 142#define NOINLINE attribute ((noinline))
137 143
138#include "CoroAPI.h" 144#include "CoroAPI.h"
139 145
140#ifdef USE_ITHREADS 146#ifdef USE_ITHREADS
147
141static perl_mutex coro_mutex; 148static perl_mutex coro_lock;
142# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 149# define LOCK do { MUTEX_LOCK (&coro_lock); } while (0)
143# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 150# define UNLOCK do { MUTEX_UNLOCK (&coro_lock); } while (0)
151# if CORO_PTHREAD
152static void *coro_thx;
153# endif
154
144#else 155#else
156
145# define LOCK (void)0 157# define LOCK (void)0
146# define UNLOCK (void)0 158# define UNLOCK (void)0
159
147#endif 160#endif
161
162# undef LOCK
163# define LOCK (void)0
164# undef UNLOCK
165# define UNLOCK (void)0
148 166
149/* helper storage struct for Coro::AIO */ 167/* helper storage struct for Coro::AIO */
150struct io_state 168struct io_state
151{ 169{
170 AV *res;
152 int errorno; 171 int errorno;
153 I32 laststype; 172 I32 laststype; /* U16 in 5.10.0 */
154 int laststatval; 173 int laststatval;
155 Stat_t statcache; 174 Stat_t statcache;
156}; 175};
157 176
177static double (*nvtime)(); /* so why doesn't it take void? */
178
179static U32 cctx_gen;
158static size_t coro_stacksize = CORO_STACKSIZE; 180static size_t cctx_stacksize = CORO_STACKSIZE;
159static struct CoroAPI coroapi; 181static struct CoroAPI coroapi;
160static AV *main_mainstack; /* used to differentiate between $main and others */ 182static AV *main_mainstack; /* used to differentiate between $main and others */
161static JMPENV *main_top_env; 183static JMPENV *main_top_env;
162static HV *coro_state_stash, *coro_stash; 184static HV *coro_state_stash, *coro_stash;
163static volatile SV *coro_mortal; /* will be freed after next transfer */ 185static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
186static volatile char next_has_throw; /* speedup flag for next->throw check */
164 187
165static GV *irsgv; /* $/ */ 188static GV *irsgv; /* $/ */
166static GV *stdoutgv; /* *STDOUT */ 189static GV *stdoutgv; /* *STDOUT */
167static SV *rv_diehook; 190static SV *rv_diehook;
168static SV *rv_warnhook; 191static SV *rv_warnhook;
200 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 223 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
201 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 224 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
202 JMPENV *top_env; 225 JMPENV *top_env;
203 coro_context cctx; 226 coro_context cctx;
204 227
228 U32 gen;
205#if CORO_USE_VALGRIND 229#if CORO_USE_VALGRIND
206 int valgrind_id; 230 int valgrind_id;
207#endif 231#endif
208 unsigned char flags; 232 unsigned char flags;
209} coro_cctx; 233} coro_cctx;
269#define PRIO_MIN -4 293#define PRIO_MIN -4
270 294
271/* for Coro.pm */ 295/* for Coro.pm */
272static SV *coro_current; 296static SV *coro_current;
273static SV *coro_readyhook; 297static SV *coro_readyhook;
274static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 298static AV *coro_ready [PRIO_MAX - PRIO_MIN + 1];
275static int coro_nready; 299static int coro_nready;
276static struct coro *coro_first; 300static struct coro *coro_first;
277 301
278/** lowlevel stuff **********************************************************/ 302/** lowlevel stuff **********************************************************/
279 303
321 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 345 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
322#endif 346#endif
323 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 347 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
324 --AvFILLp (padlist); 348 --AvFILLp (padlist);
325 349
326 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 350 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE)));
327 av_store (newpadlist, 1, (SV *)newpad); 351 av_store (newpadlist, 1, (SV *)newpad);
328 352
329 return newpadlist; 353 return newpadlist;
330} 354}
331 355
374static MGVTBL coro_cv_vtbl = { 398static MGVTBL coro_cv_vtbl = {
375 0, 0, 0, 0, 399 0, 0, 0, 0,
376 coro_cv_free 400 coro_cv_free
377}; 401};
378 402
379#define CORO_MAGIC(sv,type) \ 403#define CORO_MAGIC(sv, type) \
380 SvMAGIC (sv) \ 404 SvMAGIC (sv) \
381 ? SvMAGIC (sv)->mg_type == type \ 405 ? SvMAGIC (sv)->mg_type == type \
382 ? SvMAGIC (sv) \ 406 ? SvMAGIC (sv) \
383 : mg_find (sv, type) \ 407 : mg_find (sv, type) \
384 : 0 408 : 0
385 409
386#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) 410#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
387#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state) 411#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
388 412
389static struct coro * 413static struct coro *
619 643
620/* 644/*
621 * destroy the stacks, the callchain etc... 645 * destroy the stacks, the callchain etc...
622 */ 646 */
623static void 647static void
624coro_destroy_stacks (pTHX) 648coro_destruct_stacks (pTHX)
625{ 649{
626 while (PL_curstackinfo->si_next) 650 while (PL_curstackinfo->si_next)
627 PL_curstackinfo = PL_curstackinfo->si_next; 651 PL_curstackinfo = PL_curstackinfo->si_next;
628 652
629 while (PL_curstackinfo) 653 while (PL_curstackinfo)
807 GvSV (PL_defgv) = newSV (0); 831 GvSV (PL_defgv) = newSV (0);
808 GvAV (PL_defgv) = coro->args; coro->args = 0; 832 GvAV (PL_defgv) = coro->args; coro->args = 0;
809 GvSV (PL_errgv) = newSV (0); 833 GvSV (PL_errgv) = newSV (0);
810 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 834 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
811 PL_rs = newSVsv (GvSV (irsgv)); 835 PL_rs = newSVsv (GvSV (irsgv));
812 PL_defoutgv = (GV *)SvREFCNT_inc (stdoutgv); 836 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
813 837
814 { 838 {
815 dSP; 839 dSP;
816 LOGOP myop; 840 LOGOP myop;
817 841
834 */ 858 */
835 ENTER; 859 ENTER;
836} 860}
837 861
838static void 862static void
839coro_destroy (pTHX_ struct coro *coro) 863coro_destruct (pTHX_ struct coro *coro)
840{ 864{
841 if (!IN_DESTRUCT) 865 if (!IN_DESTRUCT)
842 { 866 {
843 /* restore all saved variables and stuff */ 867 /* restore all saved variables and stuff */
844 LEAVE_SCOPE (0); 868 LEAVE_SCOPE (0);
866 SvREFCNT_dec (PL_warnhook); 890 SvREFCNT_dec (PL_warnhook);
867 891
868 SvREFCNT_dec (coro->saved_deffh); 892 SvREFCNT_dec (coro->saved_deffh);
869 SvREFCNT_dec (coro->throw); 893 SvREFCNT_dec (coro->throw);
870 894
871 coro_destroy_stacks (aTHX); 895 coro_destruct_stacks (aTHX);
872} 896}
873 897
874static void 898static void
875free_coro_mortal (pTHX) 899free_coro_mortal (pTHX)
876{ 900{
913 : cx->blk_gimme == G_SCALAR ? bot + 1 937 : cx->blk_gimme == G_SCALAR ? bot + 1
914 : bot; 938 : bot;
915 939
916 av_extend (av, top - bot); 940 av_extend (av, top - bot);
917 while (bot < top) 941 while (bot < top)
918 av_push (av, SvREFCNT_inc (*bot++)); 942 av_push (av, SvREFCNT_inc_NN (*bot++));
919 943
920 PL_runops = RUNOPS_DEFAULT; 944 PL_runops = RUNOPS_DEFAULT;
921 ENTER; 945 ENTER;
922 SAVETMPS; 946 SAVETMPS;
923 EXTEND (SP, 3); 947 EXTEND (SP, 3);
1006} 1030}
1007 1031
1008/* inject a fake call to Coro::State::_cctx_init into the execution */ 1032/* inject a fake call to Coro::State::_cctx_init into the execution */
1009/* _cctx_init should be careful, as it could be called at almost any time */ 1033/* _cctx_init should be careful, as it could be called at almost any time */
1010/* during execution of a perl program */ 1034/* during execution of a perl program */
1035/* also initialises PL_top_env */
1011static void NOINLINE 1036static void NOINLINE
1012cctx_prepare (pTHX_ coro_cctx *cctx) 1037cctx_prepare (pTHX_ coro_cctx *cctx)
1013{ 1038{
1014 dSP; 1039 dSP;
1015 LOGOP myop; 1040 LOGOP myop;
1031 PL_op = (OP *)&myop; 1056 PL_op = (OP *)&myop;
1032 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 1057 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
1033 SPAGAIN; 1058 SPAGAIN;
1034} 1059}
1035 1060
1061/* the tail of transfer: execute stuff we can onyl do afetr a transfer */
1062static void
1063transfer_tail (void)
1064{
1065 UNLOCK;
1066
1067 if (expect_false (next_has_throw))
1068 {
1069 struct coro *coro = SvSTATE (coro_current);
1070
1071 if (coro->throw)
1072 {
1073 SV *exception = coro->throw;
1074 coro->throw = 0;
1075 sv_setsv (ERRSV, exception);
1076 croak (0);
1077 }
1078 }
1079}
1080
1036/* 1081/*
1037 * this is a _very_ stripped down perl interpreter ;) 1082 * this is a _very_ stripped down perl interpreter ;)
1038 */ 1083 */
1039static void 1084static void
1040cctx_run (void *arg) 1085cctx_run (void *arg)
1041{ 1086{
1087#ifdef USE_ITHREADS
1088# if CORO_PTHREAD
1089 PERL_SET_CONTEXT (coro_thx);
1090# endif
1091#endif
1092 {
1042 dTHX; 1093 dTHX;
1043 1094
1044 /* cctx_run is the alternative tail of transfer(), so unlock here. */
1045 UNLOCK;
1046
1047 /* we now skip the entersub that lead to transfer() */ 1095 /* we now skip the entersub that lead to transfer () */
1048 PL_op = PL_op->op_next; 1096 PL_op = PL_op->op_next;
1049 1097
1050 /* inject a fake subroutine call to cctx_init */ 1098 /* inject a fake subroutine call to cctx_init */
1051 cctx_prepare (aTHX_ (coro_cctx *)arg); 1099 cctx_prepare (aTHX_ (coro_cctx *)arg);
1052 1100
1101 /* cctx_run is the alternative tail of transfer () */
1102 transfer_tail ();
1103
1053 /* somebody or something will hit me for both perl_run and PL_restartop */ 1104 /* somebody or something will hit me for both perl_run and PL_restartop */
1054 PL_restartop = PL_op; 1105 PL_restartop = PL_op;
1055 perl_run (PL_curinterp); 1106 perl_run (PL_curinterp);
1056 1107
1057 /* 1108 /*
1058 * If perl-run returns we assume exit() was being called or the coro 1109 * If perl-run returns we assume exit() was being called or the coro
1059 * fell off the end, which seems to be the only valid (non-bug) 1110 * fell off the end, which seems to be the only valid (non-bug)
1060 * reason for perl_run to return. We try to exit by jumping to the 1111 * reason for perl_run to return. We try to exit by jumping to the
1061 * bootstrap-time "top" top_env, as we cannot restore the "main" 1112 * bootstrap-time "top" top_env, as we cannot restore the "main"
1062 * coroutine as Coro has no such concept 1113 * coroutine as Coro has no such concept
1063 */ 1114 */
1064 PL_top_env = main_top_env; 1115 PL_top_env = main_top_env;
1065 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1116 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1117 }
1066} 1118}
1067 1119
1068static coro_cctx * 1120static coro_cctx *
1069cctx_new () 1121cctx_new ()
1070{ 1122{
1071 coro_cctx *cctx; 1123 coro_cctx *cctx;
1124
1125 ++cctx_count;
1126 New (0, cctx, 1, coro_cctx);
1127
1128 cctx->gen = cctx_gen;
1129 cctx->flags = 0;
1130 cctx->idle_sp = 0; /* can be accessed by transfer between cctx_run and set_stacklevel */
1131
1132 return cctx;
1133}
1134
1135/* create a new cctx only suitable as source */
1136static coro_cctx *
1137cctx_new_empty ()
1138{
1139 coro_cctx *cctx = cctx_new ();
1140
1141 cctx->sptr = 0;
1142 coro_create (&cctx->cctx, 0, 0, 0, 0);
1143
1144 return cctx;
1145}
1146
1147/* create a new cctx suitable as destination/running a perl interpreter */
1148static coro_cctx *
1149cctx_new_run ()
1150{
1151 coro_cctx *cctx = cctx_new ();
1072 void *stack_start; 1152 void *stack_start;
1073 size_t stack_size; 1153 size_t stack_size;
1074 1154
1075 ++cctx_count;
1076
1077 Newz (0, cctx, 1, coro_cctx);
1078
1079#if HAVE_MMAP 1155#if HAVE_MMAP
1080 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1156 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1081 /* mmap supposedly does allocate-on-write for us */ 1157 /* mmap supposedly does allocate-on-write for us */
1082 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 1158 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
1083 1159
1084 if (cctx->sptr != (void *)-1) 1160 if (cctx->sptr != (void *)-1)
1085 { 1161 {
1091 cctx->flags |= CC_MAPPED; 1167 cctx->flags |= CC_MAPPED;
1092 } 1168 }
1093 else 1169 else
1094#endif 1170#endif
1095 { 1171 {
1096 cctx->ssize = coro_stacksize * (long)sizeof (long); 1172 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1097 New (0, cctx->sptr, coro_stacksize, long); 1173 New (0, cctx->sptr, cctx_stacksize, long);
1098 1174
1099 if (!cctx->sptr) 1175 if (!cctx->sptr)
1100 { 1176 {
1101 perror ("FATAL: unable to allocate stack for coroutine"); 1177 perror ("FATAL: unable to allocate stack for coroutine");
1102 _exit (EXIT_FAILURE); 1178 _exit (EXIT_FAILURE);
1117{ 1193{
1118 if (!cctx) 1194 if (!cctx)
1119 return; 1195 return;
1120 1196
1121 --cctx_count; 1197 --cctx_count;
1198 coro_destroy (&cctx->cctx);
1122 1199
1200 /* coro_transfer creates new, empty cctx's */
1201 if (cctx->sptr)
1202 {
1123#if CORO_USE_VALGRIND 1203#if CORO_USE_VALGRIND
1124 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 1204 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1125#endif 1205#endif
1126 1206
1127#if HAVE_MMAP 1207#if HAVE_MMAP
1128 if (cctx->flags & CC_MAPPED) 1208 if (cctx->flags & CC_MAPPED)
1129 munmap (cctx->sptr, cctx->ssize); 1209 munmap (cctx->sptr, cctx->ssize);
1130 else 1210 else
1131#endif 1211#endif
1132 Safefree (cctx->sptr); 1212 Safefree (cctx->sptr);
1213 }
1133 1214
1134 Safefree (cctx); 1215 Safefree (cctx);
1135} 1216}
1136 1217
1137/* wether this cctx should be destructed */ 1218/* wether this cctx should be destructed */
1138#define CCTX_EXPIRED(cctx) ((cctx)->ssize < coro_stacksize || ((cctx)->flags & CC_NOREUSE)) 1219#define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE))
1139 1220
1140static coro_cctx * 1221static coro_cctx *
1141cctx_get (pTHX) 1222cctx_get (pTHX)
1142{ 1223{
1143 while (expect_true (cctx_first)) 1224 while (expect_true (cctx_first))
1150 return cctx; 1231 return cctx;
1151 1232
1152 cctx_destroy (cctx); 1233 cctx_destroy (cctx);
1153 } 1234 }
1154 1235
1155 return cctx_new (); 1236 return cctx_new_run ();
1156} 1237}
1157 1238
1158static void 1239static void
1159cctx_put (coro_cctx *cctx) 1240cctx_put (coro_cctx *cctx)
1160{ 1241{
1242 assert (("cctx_put called on non-initialised cctx", cctx->sptr));
1243
1161 /* free another cctx if overlimit */ 1244 /* free another cctx if overlimit */
1162 if (expect_false (cctx_idle >= MAX_IDLE_CCTX)) 1245 if (expect_false (cctx_idle >= cctx_max_idle))
1163 { 1246 {
1164 coro_cctx *first = cctx_first; 1247 coro_cctx *first = cctx_first;
1165 cctx_first = first->next; 1248 cctx_first = first->next;
1166 --cctx_idle; 1249 --cctx_idle;
1167 1250
1199/* always use the TRANSFER macro */ 1282/* always use the TRANSFER macro */
1200static void NOINLINE 1283static void NOINLINE
1201transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) 1284transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx)
1202{ 1285{
1203 dSTACKLEVEL; 1286 dSTACKLEVEL;
1204 static volatile int has_throw;
1205 1287
1206 /* sometimes transfer is only called to set idle_sp */ 1288 /* sometimes transfer is only called to set idle_sp */
1207 if (expect_false (!next)) 1289 if (expect_false (!next))
1208 { 1290 {
1209 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 1291 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
1213 { 1295 {
1214 coro_cctx *prev__cctx; 1296 coro_cctx *prev__cctx;
1215 1297
1216 if (expect_false (prev->flags & CF_NEW)) 1298 if (expect_false (prev->flags & CF_NEW))
1217 { 1299 {
1218 /* create a new empty context */ 1300 /* create a new empty/source context */
1219 Newz (0, prev->cctx, 1, coro_cctx); 1301 prev->cctx = cctx_new_empty ();
1220 prev->flags &= ~CF_NEW; 1302 prev->flags &= ~CF_NEW;
1221 prev->flags |= CF_RUNNING; 1303 prev->flags |= CF_RUNNING;
1222 } 1304 }
1223 1305
1224 prev->flags &= ~CF_RUNNING; 1306 prev->flags &= ~CF_RUNNING;
1239 else 1321 else
1240 load_perl (aTHX_ next); 1322 load_perl (aTHX_ next);
1241 1323
1242 prev__cctx = prev->cctx; 1324 prev__cctx = prev->cctx;
1243 1325
1326 if (prev__cctx->idle_sp == STACKLEVEL) asm volatile("");//D
1327
1244 /* possibly "free" the cctx */ 1328 /* possibly "free" the cctx */
1245 if (expect_true ( 1329 if (expect_true (
1246 prev__cctx->idle_sp == STACKLEVEL 1330 prev__cctx->idle_sp == STACKLEVEL
1247 && !(prev__cctx->flags & CC_TRACE) 1331 && !(prev__cctx->flags & CC_TRACE)
1248 && !force_cctx 1332 && !force_cctx
1265 ++next->usecount; 1349 ++next->usecount;
1266 1350
1267 if (expect_true (!next->cctx)) 1351 if (expect_true (!next->cctx))
1268 next->cctx = cctx_get (aTHX); 1352 next->cctx = cctx_get (aTHX);
1269 1353
1270 has_throw = !!next->throw; 1354 next_has_throw = !!next->throw;
1271 1355
1272 if (expect_false (prev__cctx != next->cctx)) 1356 if (expect_false (prev__cctx != next->cctx))
1273 { 1357 {
1274 prev__cctx->top_env = PL_top_env; 1358 prev__cctx->top_env = PL_top_env;
1275 PL_top_env = next->cctx->top_env; 1359 PL_top_env = next->cctx->top_env;
1277 } 1361 }
1278 1362
1279 free_coro_mortal (aTHX); 1363 free_coro_mortal (aTHX);
1280 UNLOCK; 1364 UNLOCK;
1281 1365
1282 if (expect_false (has_throw)) 1366 transfer_tail ();
1283 {
1284 struct coro *coro = SvSTATE (coro_current);
1285
1286 if (coro->throw)
1287 {
1288 SV *exception = coro->throw;
1289 coro->throw = 0;
1290 sv_setsv (ERRSV, exception);
1291 croak (0);
1292 }
1293 }
1294 } 1367 }
1295} 1368}
1296 1369
1297struct transfer_args 1370struct transfer_args
1298{ 1371{
1331 croak ("FATAL: tried to destroy currently running coroutine"); 1404 croak ("FATAL: tried to destroy currently running coroutine");
1332 1405
1333 save_perl (aTHX_ &temp); 1406 save_perl (aTHX_ &temp);
1334 load_perl (aTHX_ coro); 1407 load_perl (aTHX_ coro);
1335 1408
1336 coro_destroy (aTHX_ coro); 1409 coro_destruct (aTHX_ coro);
1337 1410
1338 load_perl (aTHX_ &temp); 1411 load_perl (aTHX_ &temp);
1339 1412
1340 coro->slot = 0; 1413 coro->slot = 0;
1341 } 1414 }
1447 LOCK; 1520 LOCK;
1448 1521
1449 sv_hook = coro_nready ? 0 : coro_readyhook; 1522 sv_hook = coro_nready ? 0 : coro_readyhook;
1450 xs_hook = coro_nready ? 0 : coroapi.readyhook; 1523 xs_hook = coro_nready ? 0 : coroapi.readyhook;
1451 1524
1452 coro_enq (aTHX_ SvREFCNT_inc (coro_sv)); 1525 coro_enq (aTHX_ SvREFCNT_inc_NN (coro_sv));
1453 ++coro_nready; 1526 ++coro_nready;
1454 1527
1455 UNLOCK; 1528 UNLOCK;
1456 1529
1457 if (sv_hook) 1530 if (sv_hook)
1612 struct coro *coro = SvSTATE (coro_sv); 1685 struct coro *coro = SvSTATE (coro_sv);
1613 1686
1614 if (flags & CC_TRACE) 1687 if (flags & CC_TRACE)
1615 { 1688 {
1616 if (!coro->cctx) 1689 if (!coro->cctx)
1617 coro->cctx = cctx_new (); 1690 coro->cctx = cctx_new_run ();
1618 else if (!(coro->cctx->flags & CC_TRACE)) 1691 else if (!(coro->cctx->flags & CC_TRACE))
1619 croak ("cannot enable tracing on coroutine with custom stack"); 1692 croak ("cannot enable tracing on coroutine with custom stack");
1620 1693
1621 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1694 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1622 } 1695 }
1629 else 1702 else
1630 coro->slot->runops = RUNOPS_DEFAULT; 1703 coro->slot->runops = RUNOPS_DEFAULT;
1631 } 1704 }
1632} 1705}
1633 1706
1707#if 0
1634static int 1708static int
1635coro_gensub_free (pTHX_ SV *sv, MAGIC *mg) 1709coro_gensub_free (pTHX_ SV *sv, MAGIC *mg)
1636{ 1710{
1637 AV *padlist; 1711 AV *padlist;
1638 AV *av = (AV *)mg->mg_obj; 1712 AV *av = (AV *)mg->mg_obj;
1644 1718
1645static MGVTBL coro_gensub_vtbl = { 1719static MGVTBL coro_gensub_vtbl = {
1646 0, 0, 0, 0, 1720 0, 0, 0, 0,
1647 coro_gensub_free 1721 coro_gensub_free
1648}; 1722};
1723#endif
1724
1725/*****************************************************************************/
1726/* PerlIO::cede */
1727
1728typedef struct
1729{
1730 PerlIOBuf base;
1731 NV next, every;
1732} PerlIOCede;
1733
1734static IV
1735PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab)
1736{
1737 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1738
1739 self->every = SvCUR (arg) ? SvNV (arg) : 0.01;
1740 self->next = nvtime () + self->every;
1741
1742 return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab);
1743}
1744
1745static SV *
1746PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags)
1747{
1748 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1749
1750 return newSVnv (self->every);
1751}
1752
1753static IV
1754PerlIOCede_flush (pTHX_ PerlIO *f)
1755{
1756 PerlIOCede *self = PerlIOSelf (f, PerlIOCede);
1757 double now = nvtime ();
1758
1759 if (now >= self->next)
1760 {
1761 api_cede ();
1762 self->next = now + self->every;
1763 }
1764
1765 return PerlIOBuf_flush (aTHX_ f);
1766}
1767
1768static PerlIO_funcs PerlIO_cede =
1769{
1770 sizeof(PerlIO_funcs),
1771 "cede",
1772 sizeof(PerlIOCede),
1773 PERLIO_K_DESTRUCT | PERLIO_K_RAW,
1774 PerlIOCede_pushed,
1775 PerlIOBuf_popped,
1776 PerlIOBuf_open,
1777 PerlIOBase_binmode,
1778 PerlIOCede_getarg,
1779 PerlIOBase_fileno,
1780 PerlIOBuf_dup,
1781 PerlIOBuf_read,
1782 PerlIOBuf_unread,
1783 PerlIOBuf_write,
1784 PerlIOBuf_seek,
1785 PerlIOBuf_tell,
1786 PerlIOBuf_close,
1787 PerlIOCede_flush,
1788 PerlIOBuf_fill,
1789 PerlIOBase_eof,
1790 PerlIOBase_error,
1791 PerlIOBase_clearerr,
1792 PerlIOBase_setlinebuf,
1793 PerlIOBuf_get_base,
1794 PerlIOBuf_bufsiz,
1795 PerlIOBuf_get_ptr,
1796 PerlIOBuf_get_cnt,
1797 PerlIOBuf_set_ptrcnt,
1798};
1799
1649 1800
1650MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ 1801MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_
1651 1802
1652PROTOTYPES: DISABLE 1803PROTOTYPES: DISABLE
1653 1804
1654BOOT: 1805BOOT:
1655{ 1806{
1656#ifdef USE_ITHREADS 1807#ifdef USE_ITHREADS
1657 MUTEX_INIT (&coro_mutex); 1808 MUTEX_INIT (&coro_lock);
1809# if CORO_PTHREAD
1810 coro_thx = PERL_GET_CONTEXT;
1811# endif
1658#endif 1812#endif
1659 BOOT_PAGESIZE; 1813 BOOT_PAGESIZE;
1660 1814
1661 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1815 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1662 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1816 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1683 main_top_env = main_top_env->je_prev; 1837 main_top_env = main_top_env->je_prev;
1684 1838
1685 coroapi.ver = CORO_API_VERSION; 1839 coroapi.ver = CORO_API_VERSION;
1686 coroapi.rev = CORO_API_REVISION; 1840 coroapi.rev = CORO_API_REVISION;
1687 coroapi.transfer = api_transfer; 1841 coroapi.transfer = api_transfer;
1842
1843 {
1844 SV **svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
1845
1846 if (!svp) croak ("Time::HiRes is required");
1847 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer");
1848
1849 nvtime = INT2PTR (double (*)(), SvIV (*svp));
1850 }
1688 1851
1689 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1852 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1690} 1853}
1691 1854
1692SV * 1855SV *
1740 ta.next = 0; 1903 ta.next = 0;
1741 break; 1904 break;
1742 1905
1743 case 1: 1906 case 1:
1744 if (items != 2) 1907 if (items != 2)
1745 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items); 1908 croak ("Coro::State::transfer (prev, next) expects two arguments, not %d", items);
1746 1909
1747 prepare_transfer (aTHX_ &ta, ST (0), ST (1)); 1910 prepare_transfer (aTHX_ &ta, ST (0), ST (1));
1748 break; 1911 break;
1749 1912
1750 case 2: 1913 case 2:
1784 _exit (code); 1947 _exit (code);
1785 1948
1786int 1949int
1787cctx_stacksize (int new_stacksize = 0) 1950cctx_stacksize (int new_stacksize = 0)
1788 CODE: 1951 CODE:
1789 RETVAL = coro_stacksize; 1952 RETVAL = cctx_stacksize;
1790 if (new_stacksize) 1953 if (new_stacksize)
1954 {
1791 coro_stacksize = new_stacksize; 1955 cctx_stacksize = new_stacksize;
1956 ++cctx_gen;
1957 }
1958 OUTPUT:
1959 RETVAL
1960
1961int
1962cctx_max_idle (int max_idle = 0)
1963 CODE:
1964 RETVAL = cctx_max_idle;
1965 if (max_idle > 1)
1966 cctx_max_idle = max_idle;
1792 OUTPUT: 1967 OUTPUT:
1793 RETVAL 1968 RETVAL
1794 1969
1795int 1970int
1796cctx_count () 1971cctx_count ()
1892 CODE: 2067 CODE:
1893 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL; 2068 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1894 OUTPUT: 2069 OUTPUT:
1895 RETVAL 2070 RETVAL
1896 2071
1897IV 2072UV
1898rss (Coro::State coro) 2073rss (Coro::State coro)
1899 PROTOTYPE: $ 2074 PROTOTYPE: $
1900 ALIAS: 2075 ALIAS:
1901 usecount = 1 2076 usecount = 1
1902 CODE: 2077 CODE:
1913 CODE: 2088 CODE:
1914 struct coro *coro = SvSTATE (coro_current); 2089 struct coro *coro = SvSTATE (coro_current);
1915 coro->cctx->idle_sp = 0; 2090 coro->cctx->idle_sp = 0;
1916 2091
1917void 2092void
1918throw (Coro::State self, SV *throw = &PL_sv_undef)
1919 PROTOTYPE: $;$
1920 CODE:
1921 SvREFCNT_dec (self->throw);
1922 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
1923
1924void
1925swap_defsv (Coro::State self) 2093swap_defsv (Coro::State self)
1926 PROTOTYPE: $ 2094 PROTOTYPE: $
1927 ALIAS: 2095 ALIAS:
1928 swap_defav = 1 2096 swap_defav = 1
1929 CODE: 2097 CODE:
1983void 2151void
1984_set_current (SV *current) 2152_set_current (SV *current)
1985 PROTOTYPE: $ 2153 PROTOTYPE: $
1986 CODE: 2154 CODE:
1987 SvREFCNT_dec (SvRV (coro_current)); 2155 SvREFCNT_dec (SvRV (coro_current));
1988 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current))); 2156 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
1989 2157
1990void 2158void
1991_set_readyhook (SV *hook) 2159_set_readyhook (SV *hook)
1992 PROTOTYPE: $ 2160 PROTOTYPE: $
1993 CODE: 2161 CODE:
2032 CODE: 2200 CODE:
2033 RETVAL = coro_nready; 2201 RETVAL = coro_nready;
2034 OUTPUT: 2202 OUTPUT:
2035 RETVAL 2203 RETVAL
2036 2204
2205void
2206throw (Coro::State self, SV *throw = &PL_sv_undef)
2207 PROTOTYPE: $;$
2208 CODE:
2209 SvREFCNT_dec (self->throw);
2210 self->throw = SvOK (throw) ? newSVsv (throw) : 0;
2211
2037# for async_pool speedup 2212# for async_pool speedup
2038void 2213void
2039_pool_1 (SV *cb) 2214_pool_1 (SV *cb)
2040 CODE: 2215 CODE:
2041{ 2216{
2053 SvREFCNT_dec (old); 2228 SvREFCNT_dec (old);
2054 croak ("\3async_pool terminate\2\n"); 2229 croak ("\3async_pool terminate\2\n");
2055 } 2230 }
2056 2231
2057 SvREFCNT_dec (coro->saved_deffh); 2232 SvREFCNT_dec (coro->saved_deffh);
2058 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 2233 coro->saved_deffh = SvREFCNT_inc_NN ((SV *)PL_defoutgv);
2059 2234
2060 hv_store (hv, "desc", sizeof ("desc") - 1, 2235 hv_store (hv, "desc", sizeof ("desc") - 1,
2061 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 2236 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
2062 2237
2063 invoke_av = (AV *)SvRV (invoke); 2238 invoke_av = (AV *)SvRV (invoke);
2067 2242
2068 if (len > 0) 2243 if (len > 0)
2069 { 2244 {
2070 av_fill (defav, len - 1); 2245 av_fill (defav, len - 1);
2071 for (i = 0; i < len; ++i) 2246 for (i = 0; i < len; ++i)
2072 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1])); 2247 av_store (defav, i, SvREFCNT_inc_NN (AvARRAY (invoke_av)[i + 1]));
2073 } 2248 }
2074 2249
2075 SvREFCNT_dec (invoke); 2250 SvREFCNT_dec (invoke);
2076} 2251}
2077 2252
2084 sv_setsv (cb, &PL_sv_undef); 2259 sv_setsv (cb, &PL_sv_undef);
2085 2260
2086 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; 2261 SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh;
2087 coro->saved_deffh = 0; 2262 coro->saved_deffh = 0;
2088 2263
2089 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 2264 if (coro_rss (aTHX_ coro) > SvUV (sv_pool_rss)
2090 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 2265 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
2091 { 2266 {
2092 SV *old = PL_diehook; 2267 SV *old = PL_diehook;
2093 PL_diehook = 0; 2268 PL_diehook = 0;
2094 SvREFCNT_dec (old); 2269 SvREFCNT_dec (old);
2156#endif 2331#endif
2157 2332
2158 2333
2159MODULE = Coro::State PACKAGE = Coro::AIO 2334MODULE = Coro::State PACKAGE = Coro::AIO
2160 2335
2161SV * 2336void
2162_get_state () 2337_get_state (SV *self)
2163 CODE: 2338 PPCODE:
2164{ 2339{
2165 struct io_state *data; 2340 AV *defav = GvAV (PL_defgv);
2166 2341 AV *av = newAV ();
2342 int i;
2167 RETVAL = newSV (sizeof (struct io_state)); 2343 SV *data_sv = newSV (sizeof (struct io_state));
2168 data = (struct io_state *)SvPVX (RETVAL); 2344 struct io_state *data = (struct io_state *)SvPVX (data_sv);
2169 SvCUR_set (RETVAL, sizeof (struct io_state)); 2345 SvCUR_set (data_sv, sizeof (struct io_state));
2170 SvPOK_only (RETVAL); 2346 SvPOK_only (data_sv);
2171 2347
2172 data->errorno = errno; 2348 data->errorno = errno;
2173 data->laststype = PL_laststype; 2349 data->laststype = PL_laststype;
2174 data->laststatval = PL_laststatval; 2350 data->laststatval = PL_laststatval;
2175 data->statcache = PL_statcache; 2351 data->statcache = PL_statcache;
2352
2353 av_extend (av, AvFILLp (defav) + 1 + 1);
2354
2355 for (i = 0; i <= AvFILLp (defav); ++i)
2356 av_push (av, SvREFCNT_inc_NN (AvARRAY (defav)[i]));
2357
2358 av_push (av, data_sv);
2359
2360 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
2361
2362 api_ready (self);
2176} 2363}
2177 OUTPUT:
2178 RETVAL
2179 2364
2180void 2365void
2181_set_state (char *data_) 2366_set_state (SV *state)
2182 PROTOTYPE: $ 2367 PROTOTYPE: $
2183 CODE: 2368 PPCODE:
2184{ 2369{
2185 struct io_state *data = (void *)data_; 2370 AV *av = (AV *)SvRV (state);
2371 struct io_state *data = (struct io_state *)SvPVX (AvARRAY (av)[AvFILLp (av)]);
2372 int i;
2186 2373
2187 errno = data->errorno; 2374 errno = data->errorno;
2188 PL_laststype = data->laststype; 2375 PL_laststype = data->laststype;
2189 PL_laststatval = data->laststatval; 2376 PL_laststatval = data->laststatval;
2190 PL_statcache = data->statcache; 2377 PL_statcache = data->statcache;
2378
2379 EXTEND (SP, AvFILLp (av));
2380 for (i = 0; i < AvFILLp (av); ++i)
2381 PUSHs (sv_2mortal (SvREFCNT_inc_NN (AvARRAY (av)[i])));
2191} 2382}
2192 2383
2193 2384
2194MODULE = Coro::State PACKAGE = Coro::AnyEvent 2385MODULE = Coro::State PACKAGE = Coro::AnyEvent
2195 2386
2219 } 2410 }
2220 2411
2221 --incede; 2412 --incede;
2222} 2413}
2223 2414
2415
2416MODULE = Coro::State PACKAGE = PerlIO::cede
2417
2418BOOT:
2419 PerlIO_define_layer (aTHX_ &PerlIO_cede);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines