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.96 by root, Sun Nov 26 18:25:11 2006 UTC vs.
Revision 1.126 by root, Tue Dec 12 04:19:56 2006 UTC

1#define PERL_NO_GET_CONTEXT
2
3#include "libcoro/coro.c" 1#include "libcoro/coro.c"
4 2
5#include "EXTERN.h" 3#include "EXTERN.h"
6#include "perl.h" 4#include "perl.h"
7#include "XSUB.h" 5#include "XSUB.h"
8 6
9#include "patchlevel.h" 7#include "patchlevel.h"
10 8
11#if PERL_VERSION < 6 9#if USE_VALGRIND
10# include <valgrind/valgrind.h>
11#endif
12
13/* the maximum number of idle cctx that will be pooled */
14#define MAX_IDLE_CCTX 8
15
16#define PERL_VERSION_ATLEAST(a,b,c) \
17 (PERL_REVISION > (a) \
18 || (PERL_REVISION == (a) \
19 && (PERL_VERSION > (b) \
20 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c)))))
21
22#if !PERL_VERSION_ATLEAST (5,6,0)
12# ifndef PL_ppaddr 23# ifndef PL_ppaddr
13# define PL_ppaddr ppaddr 24# define PL_ppaddr ppaddr
14# endif 25# endif
15# ifndef call_sv 26# ifndef call_sv
16# define call_sv perl_call_sv 27# define call_sv perl_call_sv
27# ifndef IS_PADCONST 38# ifndef IS_PADCONST
28# define IS_PADCONST(v) 0 39# define IS_PADCONST(v) 0
29# endif 40# endif
30#endif 41#endif
31 42
43/* 5.8.7 */
44#ifndef SvRV_set
45# define SvRV_set(s,v) SvRV(s) = (v)
46#endif
47
32#include <stdio.h> 48#include <stdio.h>
33#include <errno.h> 49#include <errno.h>
50#include <assert.h>
34 51
35#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 52#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
36# undef STACKGUARD 53# undef STACKGUARD
37#endif 54#endif
38 55
54# ifndef PAGESIZE 71# ifndef PAGESIZE
55# define PAGESIZE pagesize 72# define PAGESIZE pagesize
56# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE) 73# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE)
57static long pagesize; 74static long pagesize;
58# else 75# else
59# define BOOT_PAGESIZE 76# define BOOT_PAGESIZE (void)0
60# endif 77# endif
78#else
79# define PAGESIZE 0
80# define BOOT_PAGESIZE (void)0
61#endif 81#endif
62 82
63/* The next macro should declare a variable stacklevel that contains and approximation 83/* The next macro should declare a variable stacklevel that contains and approximation
64 * to the current C stack pointer. Its property is that it changes with each call 84 * to the current C stack pointer. Its property is that it changes with each call
65 * and should be unique. */ 85 * and should be unique. */
66#define dSTACKLEVEL int stacklevel 86#define dSTACKLEVEL int stacklevel
67#define STACKLEVEL ((void *)&stacklevel) 87#define STACKLEVEL ((void *)&stacklevel)
68 88
69#define IN_DESTRUCT (PL_main_cv == Nullcv) 89#define IN_DESTRUCT (PL_main_cv == Nullcv)
70 90
91#if __GNUC__ >= 3
92# define attribute(x) __attribute__(x)
93#else
94# define attribute(x)
95#endif
96
97#define NOINLINE attribute ((noinline))
98
71#include "CoroAPI.h" 99#include "CoroAPI.h"
72
73#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
74 100
75#ifdef USE_ITHREADS 101#ifdef USE_ITHREADS
76static perl_mutex coro_mutex; 102static perl_mutex coro_mutex;
77# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 103# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
78# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 104# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
79#else 105#else
80# define LOCK (void)0 106# define LOCK (void)0
81# define UNLOCK (void)0 107# define UNLOCK (void)0
82#endif 108#endif
83 109
110struct io_state
111{
112 int errorno;
113 I32 laststype;
114 int laststatval;
115 Stat_t statcache;
116};
117
84static struct CoroAPI coroapi; 118static struct CoroAPI coroapi;
85static AV *main_mainstack; /* used to differentiate between $main and others */ 119static AV *main_mainstack; /* used to differentiate between $main and others */
86static HV *coro_state_stash, *coro_stash; 120static HV *coro_state_stash, *coro_stash;
87static SV *coro_mortal; /* will be freed after next transfer */ 121static SV *coro_mortal; /* will be freed after next transfer */
88 122
123static struct coro_cctx *cctx_first;
124static int cctx_count, cctx_idle;
125
89/* this is a structure representing a c-level coroutine */ 126/* this is a structure representing a c-level coroutine */
90typedef struct coro_stack { 127typedef struct coro_cctx {
91 struct coro_stack *next; 128 struct coro_cctx *next;
92 129
93 /* the stack */ 130 /* the stack */
94 void *sptr; 131 void *sptr;
95 long ssize; /* positive == mmap, otherwise malloc */ 132 long ssize; /* positive == mmap, otherwise malloc */
96 133
97 /* cpu state */ 134 /* cpu state */
98 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 135 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
99 JMPENV *top_env; 136 JMPENV *top_env;
100 coro_context cctx; 137 coro_context cctx;
101} coro_stack; 138
139 int inuse;
140
141#if USE_VALGRIND
142 int valgrind_id;
143#endif
144} coro_cctx;
145
146enum {
147 CF_RUNNING = 0x0001, /* coroutine is running */
148 CF_READY = 0x0002, /* coroutine is ready */
149 CF_NEW = 0x0004, /* ahs never been switched to */
150};
102 151
103/* this is a structure representing a perl-level coroutine */ 152/* this is a structure representing a perl-level coroutine */
104struct coro { 153struct coro {
105 /* the c coroutine allocated to this perl coroutine, if any */ 154 /* the c coroutine allocated to this perl coroutine, if any */
106 coro_stack *stack; 155 coro_cctx *cctx;
107 156
108 /* data associated with this coroutine (initial args) */ 157 /* data associated with this coroutine (initial args) */
109 AV *args; 158 AV *args;
110 int refcnt; 159 int refcnt;
160 int save; /* CORO_SAVE flags */
161 int flags; /* CF_ flags */
111 162
112 /* optionally saved, might be zero */ 163 /* optionally saved, might be zero */
113 AV *defav; 164 AV *defav; /* @_ */
114 SV *defsv; 165 SV *defsv; /* $_ */
115 SV *errsv; 166 SV *errsv; /* $@ */
167 SV *irssv; /* $/ */
168 SV *irssv_sv; /* real $/ cache */
116 169
117 /* saved global state not related to stacks */ 170#define VAR(name,type) type name;
118 U8 dowarn; 171# include "state.h"
119 I32 in_eval; 172#undef VAR
120
121 /* the stacks and related info (callchain etc..) */
122 PERL_SI *curstackinfo;
123 AV *curstack;
124 AV *mainstack;
125 SV **stack_sp;
126 OP *op;
127 SV **curpad;
128 AV *comppad;
129 CV *compcv;
130 SV **stack_base;
131 SV **stack_max;
132 SV **tmps_stack;
133 I32 tmps_floor;
134 I32 tmps_ix;
135 I32 tmps_max;
136 I32 *markstack;
137 I32 *markstack_ptr;
138 I32 *markstack_max;
139 I32 *scopestack;
140 I32 scopestack_ix;
141 I32 scopestack_max;
142 ANY *savestack;
143 I32 savestack_ix;
144 I32 savestack_max;
145 OP **retstack;
146 I32 retstack_ix;
147 I32 retstack_max;
148 PMOP *curpm;
149 COP *curcop;
150 173
151 /* coro process data */ 174 /* coro process data */
152 int prio; 175 int prio;
153}; 176};
154 177
155typedef struct coro *Coro__State; 178typedef struct coro *Coro__State;
156typedef struct coro *Coro__State_or_hashref; 179typedef struct coro *Coro__State_or_hashref;
157 180
158static AV * 181static AV *
159coro_clone_padlist (pTHX_ CV *cv) 182coro_clone_padlist (CV *cv)
160{ 183{
161 AV *padlist = CvPADLIST (cv); 184 AV *padlist = CvPADLIST (cv);
162 AV *newpadlist, *newpad; 185 AV *newpadlist, *newpad;
163 186
164 newpadlist = newAV (); 187 newpadlist = newAV ();
165 AvREAL_off (newpadlist); 188 AvREAL_off (newpadlist);
166#if PERL_VERSION < 9 189#if PERL_VERSION_ATLEAST (5,9,0)
190 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
191#else
167 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 192 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
168#else
169 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
170#endif 193#endif
171 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 194 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
172 --AvFILLp (padlist); 195 --AvFILLp (padlist);
173 196
174 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE))); 197 av_store (newpadlist, 0, SvREFCNT_inc (*av_fetch (padlist, 0, FALSE)));
176 199
177 return newpadlist; 200 return newpadlist;
178} 201}
179 202
180static void 203static void
181free_padlist (pTHX_ AV *padlist) 204free_padlist (AV *padlist)
182{ 205{
183 /* may be during global destruction */ 206 /* may be during global destruction */
184 if (SvREFCNT (padlist)) 207 if (SvREFCNT (padlist))
185 { 208 {
186 I32 i = AvFILLp (padlist); 209 I32 i = AvFILLp (padlist);
207 AV *padlist; 230 AV *padlist;
208 AV *av = (AV *)mg->mg_obj; 231 AV *av = (AV *)mg->mg_obj;
209 232
210 /* casting is fun. */ 233 /* casting is fun. */
211 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 234 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
212 free_padlist (aTHX_ padlist); 235 free_padlist (padlist);
213 236
214 SvREFCNT_dec (av); 237 SvREFCNT_dec (av);
215 238
216 return 0; 239 return 0;
217} 240}
218 241
219#define PERL_MAGIC_coro PERL_MAGIC_ext 242#define PERL_MAGIC_coro PERL_MAGIC_ext
220 243
221static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 244static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free};
222 245
246#define CORO_MAGIC(cv) \
247 SvMAGIC (cv) \
248 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
249 ? SvMAGIC (cv) \
250 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
251 : 0
252
223/* the next two functions merely cache the padlists */ 253/* the next two functions merely cache the padlists */
224static void 254static void
225get_padlist (pTHX_ CV *cv) 255get_padlist (CV *cv)
226{ 256{
227 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 257 MAGIC *mg = CORO_MAGIC (cv);
258 AV *av;
228 259
229 if (mg && AvFILLp ((AV *)mg->mg_obj) >= 0) 260 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
230 CvPADLIST (cv) = (AV *)av_pop ((AV *)mg->mg_obj); 261 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
231 else 262 else
232 { 263 {
233#if 0 264#if 0
234 /* this is probably cleaner, but also slower? */ 265 /* this is probably cleaner, but also slower? */
235 CV *cp = Perl_cv_clone (aTHX_ cv); 266 CV *cp = Perl_cv_clone (cv);
236 CvPADLIST (cv) = CvPADLIST (cp); 267 CvPADLIST (cv) = CvPADLIST (cp);
237 CvPADLIST (cp) = 0; 268 CvPADLIST (cp) = 0;
238 SvREFCNT_dec (cp); 269 SvREFCNT_dec (cp);
239#else 270#else
240 CvPADLIST (cv) = coro_clone_padlist (aTHX_ cv); 271 CvPADLIST (cv) = coro_clone_padlist (cv);
241#endif 272#endif
242 } 273 }
243} 274}
244 275
245static void 276static void
246put_padlist (pTHX_ CV *cv) 277put_padlist (CV *cv)
247{ 278{
248 MAGIC *mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 279 MAGIC *mg = CORO_MAGIC (cv);
280 AV *av;
249 281
250 if (!mg) 282 if (!mg)
251 { 283 {
252 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0); 284 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
253 mg = mg_find ((SV *)cv, PERL_MAGIC_coro); 285 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
254 mg->mg_virtual = &vtbl_coro; 286 mg->mg_virtual = &vtbl_coro;
255 mg->mg_obj = (SV *)newAV (); 287 mg->mg_obj = (SV *)newAV ();
256 } 288 }
257 289
258 av_push ((AV *)mg->mg_obj, (SV *)CvPADLIST (cv)); 290 av = (AV *)mg->mg_obj;
291
292 if (AvFILLp (av) >= AvMAX (av))
293 av_extend (av, AvMAX (av) + 1);
294
295 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
259} 296}
260 297
261#define SB do { 298#define SB do {
262#define SE } while (0) 299#define SE } while (0)
263 300
264#define LOAD(state) load_state(aTHX_ (state));
265#define SAVE(state,flags) save_state(aTHX_ (state),(flags));
266
267#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 301#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
268 302
269static void 303static void
270load_state(pTHX_ Coro__State c) 304load_perl (Coro__State c)
271{ 305{
272 PL_dowarn = c->dowarn; 306#define VAR(name,type) PL_ ## name = c->name;
273 PL_in_eval = c->in_eval; 307# include "state.h"
274 308#undef VAR
275 PL_curstackinfo = c->curstackinfo;
276 PL_curstack = c->curstack;
277 PL_mainstack = c->mainstack;
278 PL_stack_sp = c->stack_sp;
279 PL_op = c->op;
280 PL_curpad = c->curpad;
281 PL_comppad = c->comppad;
282 PL_compcv = c->compcv;
283 PL_stack_base = c->stack_base;
284 PL_stack_max = c->stack_max;
285 PL_tmps_stack = c->tmps_stack;
286 PL_tmps_floor = c->tmps_floor;
287 PL_tmps_ix = c->tmps_ix;
288 PL_tmps_max = c->tmps_max;
289 PL_markstack = c->markstack;
290 PL_markstack_ptr = c->markstack_ptr;
291 PL_markstack_max = c->markstack_max;
292 PL_scopestack = c->scopestack;
293 PL_scopestack_ix = c->scopestack_ix;
294 PL_scopestack_max = c->scopestack_max;
295 PL_savestack = c->savestack;
296 PL_savestack_ix = c->savestack_ix;
297 PL_savestack_max = c->savestack_max;
298#if PERL_VERSION < 9
299 PL_retstack = c->retstack;
300 PL_retstack_ix = c->retstack_ix;
301 PL_retstack_max = c->retstack_max;
302#endif
303 PL_curpm = c->curpm;
304 PL_curcop = c->curcop;
305 309
306 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 310 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
307 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 311 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
308 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 312 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
313 if (c->irssv)
314 {
315 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
316 SvREFCNT_dec (c->irssv);
317 else
318 {
319 REPLACE_SV (PL_rs, c->irssv);
320 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
321 sv_setsv (c->irssv_sv, PL_rs);
322 }
323 }
309 324
310 { 325 {
311 dSP; 326 dSP;
312 CV *cv; 327 CV *cv;
313 328
314 /* now do the ugly restore mess */ 329 /* now do the ugly restore mess */
315 while ((cv = (CV *)POPs)) 330 while ((cv = (CV *)POPs))
316 { 331 {
317 AV *padlist = (AV *)POPs;
318
319 if (padlist)
320 {
321 put_padlist (aTHX_ cv); /* mark this padlist as available */ 332 put_padlist (cv); /* mark this padlist as available */
322 CvPADLIST(cv) = padlist; 333 CvDEPTH (cv) = PTR2IV (POPs);
323 } 334 CvPADLIST (cv) = (AV *)POPs;
324
325 ++CvDEPTH(cv);
326 } 335 }
327 336
328 PUTBACK; 337 PUTBACK;
329 } 338 }
330} 339}
331 340
332static void 341static void
333save_state(pTHX_ Coro__State c, int flags) 342save_perl (Coro__State c)
334{ 343{
335 { 344 {
336 dSP; 345 dSP;
337 I32 cxix = cxstack_ix; 346 I32 cxix = cxstack_ix;
338 PERL_CONTEXT *ccstk = cxstack; 347 PERL_CONTEXT *ccstk = cxstack;
341 /* 350 /*
342 * the worst thing you can imagine happens first - we have to save 351 * the worst thing you can imagine happens first - we have to save
343 * (and reinitialize) all cv's in the whole callchain :( 352 * (and reinitialize) all cv's in the whole callchain :(
344 */ 353 */
345 354
355 EXTEND (SP, 3 + 1);
346 PUSHs (Nullsv); 356 PUSHs (Nullsv);
347 /* this loop was inspired by pp_caller */ 357 /* this loop was inspired by pp_caller */
348 for (;;) 358 for (;;)
349 { 359 {
350 while (cxix >= 0) 360 while (cxix >= 0)
351 { 361 {
352 PERL_CONTEXT *cx = &ccstk[cxix--]; 362 PERL_CONTEXT *cx = &ccstk[cxix--];
353 363
354 if (CxTYPE(cx) == CXt_SUB) 364 if (CxTYPE (cx) == CXt_SUB)
355 { 365 {
356 CV *cv = cx->blk_sub.cv; 366 CV *cv = cx->blk_sub.cv;
367
357 if (CvDEPTH(cv)) 368 if (CvDEPTH (cv))
358 { 369 {
359 EXTEND (SP, CvDEPTH(cv)*2); 370 EXTEND (SP, 3);
360
361 while (--CvDEPTH(cv))
362 {
363 /* this tells the restore code to increment CvDEPTH */
364 PUSHs (Nullsv);
365 PUSHs ((SV *)cv);
366 }
367
368 PUSHs ((SV *)CvPADLIST(cv)); 371 PUSHs ((SV *)CvPADLIST (cv));
372 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
369 PUSHs ((SV *)cv); 373 PUSHs ((SV *)cv);
370 374
375 CvDEPTH (cv) = 0;
371 get_padlist (aTHX_ cv); 376 get_padlist (cv);
372 } 377 }
373 } 378 }
374#ifdef CXt_FORMAT
375 else if (CxTYPE(cx) == CXt_FORMAT)
376 {
377 /* I never used formats, so how should I know how these are implemented? */
378 /* my bold guess is as a simple, plain sub... */
379 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
380 }
381#endif
382 } 379 }
383 380
384 if (top_si->si_type == PERLSI_MAIN) 381 if (top_si->si_type == PERLSI_MAIN)
385 break; 382 break;
386 383
390 } 387 }
391 388
392 PUTBACK; 389 PUTBACK;
393 } 390 }
394 391
395 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 392 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
396 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 393 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
397 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 394 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
395 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0;
398 396
399 c->dowarn = PL_dowarn; 397#define VAR(name,type)c->name = PL_ ## name;
400 c->in_eval = PL_in_eval; 398# include "state.h"
401 399#undef VAR
402 c->curstackinfo = PL_curstackinfo;
403 c->curstack = PL_curstack;
404 c->mainstack = PL_mainstack;
405 c->stack_sp = PL_stack_sp;
406 c->op = PL_op;
407 c->curpad = PL_curpad;
408 c->comppad = PL_comppad;
409 c->compcv = PL_compcv;
410 c->stack_base = PL_stack_base;
411 c->stack_max = PL_stack_max;
412 c->tmps_stack = PL_tmps_stack;
413 c->tmps_floor = PL_tmps_floor;
414 c->tmps_ix = PL_tmps_ix;
415 c->tmps_max = PL_tmps_max;
416 c->markstack = PL_markstack;
417 c->markstack_ptr = PL_markstack_ptr;
418 c->markstack_max = PL_markstack_max;
419 c->scopestack = PL_scopestack;
420 c->scopestack_ix = PL_scopestack_ix;
421 c->scopestack_max = PL_scopestack_max;
422 c->savestack = PL_savestack;
423 c->savestack_ix = PL_savestack_ix;
424 c->savestack_max = PL_savestack_max;
425#if PERL_VERSION < 9
426 c->retstack = PL_retstack;
427 c->retstack_ix = PL_retstack_ix;
428 c->retstack_max = PL_retstack_max;
429#endif
430 c->curpm = PL_curpm;
431 c->curcop = PL_curcop;
432} 400}
433 401
434/* 402/*
435 * allocate various perl stacks. This is an exact copy 403 * allocate various perl stacks. This is an exact copy
436 * of perl.c:init_stacks, except that it uses less memory 404 * of perl.c:init_stacks, except that it uses less memory
437 * on the (sometimes correct) assumption that coroutines do 405 * on the (sometimes correct) assumption that coroutines do
438 * not usually need a lot of stackspace. 406 * not usually need a lot of stackspace.
439 */ 407 */
408#if USE_PERL_INIT_STACKS
409# define coro_init_stacks init_stacks
410#else
411
440static void 412static void
441coro_init_stacks (pTHX) 413coro_init_stacks ()
442{ 414{
443 LOCK;
444
445 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 415 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
446 PL_curstackinfo->si_type = PERLSI_MAIN; 416 PL_curstackinfo->si_type = PERLSI_MAIN;
447 PL_curstack = PL_curstackinfo->si_stack; 417 PL_curstack = PL_curstackinfo->si_stack;
448 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 418 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
449 419
450 PL_stack_base = AvARRAY(PL_curstack); 420 PL_stack_base = AvARRAY(PL_curstack);
451 PL_stack_sp = PL_stack_base; 421 PL_stack_sp = PL_stack_base;
452 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 422 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
453 423
454 New(50,PL_tmps_stack,96,SV*); 424 New(50,PL_tmps_stack,128,SV*);
455 PL_tmps_floor = -1; 425 PL_tmps_floor = -1;
456 PL_tmps_ix = -1; 426 PL_tmps_ix = -1;
457 PL_tmps_max = 96; 427 PL_tmps_max = 128;
458 428
459 New(54,PL_markstack,16,I32); 429 New(54,PL_markstack,32,I32);
460 PL_markstack_ptr = PL_markstack; 430 PL_markstack_ptr = PL_markstack;
461 PL_markstack_max = PL_markstack + 16; 431 PL_markstack_max = PL_markstack + 32;
462 432
463#ifdef SET_MARK_OFFSET 433#ifdef SET_MARK_OFFSET
464 SET_MARK_OFFSET; 434 SET_MARK_OFFSET;
465#endif 435#endif
466 436
467 New(54,PL_scopestack,16,I32); 437 New(54,PL_scopestack,32,I32);
468 PL_scopestack_ix = 0; 438 PL_scopestack_ix = 0;
469 PL_scopestack_max = 16; 439 PL_scopestack_max = 32;
470 440
471 New(54,PL_savestack,96,ANY); 441 New(54,PL_savestack,64,ANY);
472 PL_savestack_ix = 0; 442 PL_savestack_ix = 0;
473 PL_savestack_max = 96; 443 PL_savestack_max = 64;
474 444
475#if PERL_VERSION < 9 445#if !PERL_VERSION_ATLEAST (5,9,0)
476 New(54,PL_retstack,8,OP*); 446 New(54,PL_retstack,16,OP*);
477 PL_retstack_ix = 0; 447 PL_retstack_ix = 0;
478 PL_retstack_max = 8; 448 PL_retstack_max = 16;
479#endif 449#endif
480
481 UNLOCK;
482} 450}
483 451
484/* 452/*
485 * destroy the stacks, the callchain etc... 453 * destroy the stacks, the callchain etc...
486 */ 454 */
487static void 455static void
488destroy_stacks(pTHX) 456coro_destroy_stacks ()
489{ 457{
490 if (!IN_DESTRUCT) 458 if (!IN_DESTRUCT)
491 { 459 {
492 /* is this ugly, I ask? */ 460 /* restore all saved variables and stuff */
493 LEAVE_SCOPE (0); 461 LEAVE_SCOPE (0);
462 assert (PL_tmps_floor == -1);
494 463
495 /* sure it is, but more important: is it correct?? :/ */ 464 /* free all temporaries */
496 FREETMPS; 465 FREETMPS;
466 assert (PL_tmps_ix == -1);
497 467
498 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ 468 POPSTACK_TO (PL_mainstack);
499 } 469 }
500 470
501 while (PL_curstackinfo->si_next) 471 while (PL_curstackinfo->si_next)
502 PL_curstackinfo = PL_curstackinfo->si_next; 472 PL_curstackinfo = PL_curstackinfo->si_next;
503 473
504 while (PL_curstackinfo) 474 while (PL_curstackinfo)
505 { 475 {
506 PERL_SI *p = PL_curstackinfo->si_prev; 476 PERL_SI *p = PL_curstackinfo->si_prev;
507 477
508 { /*D*//*remove*/
509 dSP;
510 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
511 PUTBACK; /* possibly superfluous */
512 }
513
514 if (!IN_DESTRUCT) 478 if (!IN_DESTRUCT)
515 {
516 dounwind (-1);/*D*//*remove*/
517 SvREFCNT_dec (PL_curstackinfo->si_stack); 479 SvREFCNT_dec (PL_curstackinfo->si_stack);
518 }
519 480
520 Safefree (PL_curstackinfo->si_cxstack); 481 Safefree (PL_curstackinfo->si_cxstack);
521 Safefree (PL_curstackinfo); 482 Safefree (PL_curstackinfo);
522 PL_curstackinfo = p; 483 PL_curstackinfo = p;
523 } 484 }
524 485
525 Safefree (PL_tmps_stack); 486 Safefree (PL_tmps_stack);
526 Safefree (PL_markstack); 487 Safefree (PL_markstack);
527 Safefree (PL_scopestack); 488 Safefree (PL_scopestack);
528 Safefree (PL_savestack); 489 Safefree (PL_savestack);
529#if PERL_VERSION < 9 490#if !PERL_VERSION_ATLEAST (5,9,0)
530 Safefree (PL_retstack); 491 Safefree (PL_retstack);
531#endif 492#endif
532} 493}
494#endif
533 495
534static void 496static void
535setup_coro (struct coro *coro) 497setup_coro (struct coro *coro)
536{ 498{
537 /* 499 /*
538 * emulate part of the perl startup here. 500 * emulate part of the perl startup here.
539 */ 501 */
540 dTHX;
541 dSP;
542 UNOP myop;
543 SV *sub_init = (SV *)get_cv ("Coro::State::coro_init", FALSE);
544 502
545 coro_init_stacks (aTHX); 503 coro_init_stacks ();
546 /*PL_curcop = 0;*/ 504
547 /*PL_in_eval = PL_in_eval;*/ /* inherit */ 505 PL_curcop = &PL_compiling;
506 PL_in_eval = EVAL_NULL;
507 PL_curpm = 0;
508 PL_localizing = 0;
509 PL_dirty = 0;
510 PL_restartop = 0;
511
512 {
513 dSP;
514 LOGOP myop;
515
548 SvREFCNT_dec (GvAV (PL_defgv)); 516 SvREFCNT_dec (GvAV (PL_defgv));
549 GvAV (PL_defgv) = coro->args; coro->args = 0; 517 GvAV (PL_defgv) = coro->args; coro->args = 0;
550 518
551 SPAGAIN;
552
553 Zero (&myop, 1, UNOP); 519 Zero (&myop, 1, LOGOP);
554 myop.op_next = Nullop; 520 myop.op_next = Nullop;
555 myop.op_flags = OPf_WANT_VOID; 521 myop.op_flags = OPf_WANT_VOID;
556 522
523 PUSHMARK (SP);
524 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
525 PUTBACK;
557 PL_op = (OP *)&myop; 526 PL_op = (OP *)&myop;
558
559 PUSHMARK(SP);
560 XPUSHs (sub_init);
561 PUTBACK;
562 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 527 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
563 SPAGAIN; 528 SPAGAIN;
529 }
564 530
565 ENTER; /* necessary e.g. for dounwind */ 531 ENTER; /* necessary e.g. for dounwind */
566} 532}
567 533
568static void 534static void
573 SvREFCNT_dec (coro_mortal); 539 SvREFCNT_dec (coro_mortal);
574 coro_mortal = 0; 540 coro_mortal = 0;
575 } 541 }
576} 542}
577 543
544/* inject a fake call to Coro::State::_cctx_init into the execution */
545static void NOINLINE
546prepare_cctx (coro_cctx *cctx)
547{
548 dSP;
549 LOGOP myop;
550
551 Zero (&myop, 1, LOGOP);
552 myop.op_next = PL_op;
553 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
554
555 PUSHMARK (SP);
556 EXTEND (SP, 2);
557 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx))));
558 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
559 PUTBACK;
560 PL_op = (OP *)&myop;
561 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
562 SPAGAIN;
563}
564
578static void 565static void
579coro_run (void *arg) 566coro_run (void *arg)
580{ 567{
568 /* coro_run is the alternative tail of transfer(), so unlock here. */
569 UNLOCK;
570
581 /* 571 /*
582 * this is a _very_ stripped down perl interpreter ;) 572 * this is a _very_ stripped down perl interpreter ;)
583 */ 573 */
584 dTHX;
585 int ret;
586
587 UNLOCK;
588
589 PL_top_env = &PL_start_env; 574 PL_top_env = &PL_start_env;
590 575
591 sv_setiv (get_sv ("Coro::State::cctx_stack", FALSE), PTR2IV ((coro_stack *)arg)); 576 /* inject call to cctx_init */
592 sv_setiv (get_sv ("Coro::State::cctx_restartop", FALSE), PTR2IV (PL_op)); 577 prepare_cctx ((coro_cctx *)arg);
593
594 /* continue at cctx_init, without entersub */
595 PL_restartop = CvSTART (get_cv ("Coro::State::cctx_init", FALSE));
596 578
597 /* somebody will hit me for both perl_run and PL_restartop */ 579 /* somebody will hit me for both perl_run and PL_restartop */
598 ret = perl_run (aTHX_ PERL_GET_CONTEXT); 580 PL_restartop = PL_op;
599 printf ("ret %d\n", ret);//D 581 perl_run (PL_curinterp);
600 582
601 fputs ("FATAL: C coroutine fell over the edge of the world, aborting.\n", stderr); 583 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
602 abort (); 584 abort ();
603} 585}
604 586
605static coro_stack * 587static coro_cctx *
606stack_new () 588cctx_new ()
607{ 589{
608 coro_stack *stack; 590 coro_cctx *cctx;
609 591
592 ++cctx_count;
593
610 New (0, stack, 1, coro_stack); 594 Newz (0, cctx, 1, coro_cctx);
611 595
612#if HAVE_MMAP 596#if HAVE_MMAP
613 597
614 stack->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 598 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
615 /* mmap suppsedly does allocate-on-write for us */ 599 /* mmap supposedly does allocate-on-write for us */
616 stack->sptr = mmap (0, stack->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 600 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
617 601
618 if (stack->sptr == (void *)-1) 602 if (cctx->sptr == (void *)-1)
619 { 603 {
620 perror ("FATAL: unable to mmap stack for coroutine"); 604 perror ("FATAL: unable to mmap stack for coroutine");
621 _exit (EXIT_FAILURE); 605 _exit (EXIT_FAILURE);
622 } 606 }
623 607
624# if STACKGUARD 608# if STACKGUARD
625 mprotect (stack->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 609 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
626# endif 610# endif
627 611
628#else 612#else
629 613
630 stack->ssize = STACKSIZE * (long)sizeof (long); 614 cctx->ssize = STACKSIZE * (long)sizeof (long);
631 New (0, stack->sptr, STACKSIZE, long); 615 New (0, cctx->sptr, STACKSIZE, long);
632 616
633 if (!stack->sptr) 617 if (!cctx->sptr)
634 { 618 {
635 perror (stderr, "FATAL: unable to malloc stack for coroutine"); 619 perror ("FATAL: unable to malloc stack for coroutine");
636 _exit (EXIT_FAILURE); 620 _exit (EXIT_FAILURE);
637 } 621 }
638 622
639#endif 623#endif
640 624
625#if USE_VALGRIND
626 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
627 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
628 cctx->ssize + (char *)cctx->sptr
629 );
630#endif
631
641 coro_create (&stack->cctx, coro_run, (void *)stack, stack->sptr, stack->ssize); 632 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
642 633
643 return stack; 634 return cctx;
644} 635}
645 636
646static void 637static void
647stack_free (coro_stack *stack) 638cctx_destroy (coro_cctx *cctx)
648{ 639{
649 if (!stack) 640 if (!cctx)
650 return; 641 return;
651 642
643 --cctx_count;
644
645#if USE_VALGRIND
646 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
647#endif
648
652#if HAVE_MMAP 649#if HAVE_MMAP
653 munmap (stack->sptr, stack->ssize); 650 munmap (cctx->sptr, cctx->ssize);
654#else 651#else
655 Safefree (stack->sptr); 652 Safefree (cctx->sptr);
656#endif 653#endif
657 654
658 Safefree (stack); 655 Safefree (cctx);
659} 656}
660 657
661static coro_stack *stack_first;
662
663static coro_stack * 658static coro_cctx *
664stack_get () 659cctx_get ()
665{ 660{
666 coro_stack *stack; 661 coro_cctx *cctx;
667 662
668 if (stack_first) 663 if (cctx_first)
669 { 664 {
670 stack = stack_first; 665 cctx = cctx_first;
671 stack_first = stack->next; 666 cctx_first = cctx->next;
667 --cctx_idle;
672 } 668 }
673 else 669 else
674 { 670 {
675 stack = stack_new (); 671 cctx = cctx_new ();
676 PL_op = PL_op->op_next; 672 PL_op = PL_op->op_next;
677 } 673 }
678 674
679 return stack; 675 return cctx;
680} 676}
681 677
682static void 678static void
683stack_put (coro_stack *stack) 679cctx_put (coro_cctx *cctx)
684{ 680{
685 stack->next = stack_first; 681 /* free another cctx if overlimit */
686 stack_first = stack; 682 if (cctx_idle >= MAX_IDLE_CCTX)
683 {
684 coro_cctx *first = cctx_first;
685 cctx_first = first->next;
686 --cctx_idle;
687
688 assert (!first->inuse);
689 cctx_destroy (first);
690 }
691
692 ++cctx_idle;
693 cctx->next = cctx_first;
694 cctx_first = cctx;
687} 695}
688 696
689/* never call directly, always through the coro_state_transfer global variable */ 697/* never call directly, always through the coro_state_transfer global variable */
690static void 698static void NOINLINE
691transfer_impl (pTHX_ struct coro *prev, struct coro *next, int flags) 699transfer (struct coro *prev, struct coro *next)
692{ 700{
693 dSTACKLEVEL; 701 dSTACKLEVEL;
694 702
695 /* sometimes transfer is only called to set idle_sp */ 703 /* sometimes transfer is only called to set idle_sp */
696 if (flags == TRANSFER_SET_STACKLEVEL) 704 if (!next)
705 {
697 ((coro_stack *)prev)->idle_sp = STACKLEVEL; 706 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
707 assert (((coro_cctx *)prev)->top_env = PL_top_env); /* just for the side effetc when assert is enabled */
708 }
698 else if (prev != next) 709 else if (prev != next)
699 { 710 {
700 coro_stack *prev__stack; 711 coro_cctx *prev__cctx;
712
713 if (prev->flags & CF_NEW)
714 {
715 /* create a new empty context */
716 Newz (0, prev->cctx, 1, coro_cctx);
717 prev->cctx->inuse = 1;
718 prev->flags &= ~CF_NEW;
719 prev->flags |= CF_RUNNING;
720 }
721
722 /*TODO: must not croak here */
723 if (!prev->flags & CF_RUNNING)
724 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
725
726 if (next->flags & CF_RUNNING)
727 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
728
729 prev->flags &= ~CF_RUNNING;
730 next->flags |= CF_RUNNING;
701 731
702 LOCK; 732 LOCK;
703 733
704 if (next->mainstack) 734 if (next->flags & CF_NEW)
705 { 735 {
706 /* coroutine already started */ 736 /* need to start coroutine */
707 SAVE (prev, flags); 737 next->flags &= ~CF_NEW;
708 LOAD (next); 738 /* first get rid of the old state */
739 save_perl (prev);
740 /* setup coroutine call */
741 setup_coro (next);
742 /* need a new stack */
743 assert (!next->cctx);
709 } 744 }
710 else 745 else
711 { 746 {
712 /* need to start coroutine */ 747 /* coroutine already started */
713 /* first get rid of the old state */ 748 save_perl (prev);
714 SAVE (prev, -1); 749 load_perl (next);
715 /* setup coroutine call */
716 setup_coro (next);
717 /* need a stack */
718 next->stack = 0;
719 } 750 }
720 751
721 if (!prev->stack)
722 /* create a new empty context */
723 Newz (0, prev->stack, 1, coro_stack);
724
725 prev__stack = prev->stack; 752 prev__cctx = prev->cctx;
726 753
727 /* possibly "free" the stack */ 754 /* possibly "free" the cctx */
728 if (prev__stack->idle_sp == STACKLEVEL) 755 if (prev__cctx->idle_sp == STACKLEVEL)
729 { 756 {
730 stack_put (prev__stack); 757 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
758 assert (PL_top_env == prev__cctx->top_env);
759
731 prev->stack = 0; 760 prev->cctx = 0;
761
762 cctx_put (prev__cctx);
763 prev__cctx->inuse = 0;
732 } 764 }
733 765
734 if (!next->stack) 766 if (!next->cctx)
735 next->stack = stack_get ();
736
737 if (prev__stack != next->stack)
738 { 767 {
768 next->cctx = cctx_get ();
769 assert (!next->cctx->inuse);
770 next->cctx->inuse = 1;
771 }
772
773 if (prev__cctx != next->cctx)
774 {
739 prev__stack->top_env = PL_top_env; 775 prev__cctx->top_env = PL_top_env;
740 PL_top_env = next->stack->top_env; 776 PL_top_env = next->cctx->top_env;
741 coro_transfer (&prev__stack->cctx, &next->stack->cctx); 777 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
742 } 778 }
743 779
744 free_coro_mortal (); 780 free_coro_mortal ();
745 781
746 UNLOCK; 782 UNLOCK;
747 } 783 }
748} 784}
749 785
750/* use this function pointer to call the above function */
751/* this is done to increase chances of the compiler not inlining the call */
752/* not static to make it even harder for the compiler (and theoretically impossible in most cases */
753void (*coro_state_transfer)(pTHX_ struct coro *prev, struct coro *next, int flags) = transfer_impl;
754
755struct transfer_args 786struct transfer_args
756{ 787{
757 struct coro *prev, *next; 788 struct coro *prev, *next;
758 int flags;
759}; 789};
760 790
761#define TRANSFER(ta) coro_state_transfer ((ta).prev, (ta).next, (ta).flags) 791#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
762 792
763static void 793static void
764coro_state_destroy (struct coro *coro) 794coro_state_destroy (struct coro *coro)
765{ 795{
766 if (coro->refcnt--) 796 if (coro->refcnt--)
767 return; 797 return;
768 798
769 if (coro->mainstack && coro->mainstack != main_mainstack) 799 if (coro->mainstack && coro->mainstack != main_mainstack)
770 { 800 {
771 struct coro temp; 801 struct coro temp;
802 Zero (&temp, 1, struct coro);
803 temp.save = CORO_SAVE_ALL;
772 804
773 SAVE (aTHX_ (&temp), TRANSFER_SAVE_ALL); 805 if (coro->flags & CF_RUNNING)
774 LOAD (aTHX_ coro); 806 croak ("FATAL: tried to destroy currently running coroutine");
775 807
808 save_perl (&temp);
809 load_perl (coro);
810
776 destroy_stacks (aTHX); 811 coro_destroy_stacks ();
777 812
778 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 813 load_perl (&temp); /* this will get rid of defsv etc.. */
779 814
780 coro->mainstack = 0; 815 coro->mainstack = 0;
781 } 816 }
782 817
783 stack_free (coro->stack); 818 cctx_destroy (coro->cctx);
784 SvREFCNT_dec (coro->args); 819 SvREFCNT_dec (coro->args);
785 Safefree (coro); 820 Safefree (coro);
786} 821}
787 822
788static int 823static int
789coro_state_clear (SV *sv, MAGIC *mg) 824coro_state_clear (pTHX_ SV *sv, MAGIC *mg)
790{ 825{
791 struct coro *coro = (struct coro *)mg->mg_ptr; 826 struct coro *coro = (struct coro *)mg->mg_ptr;
792 mg->mg_ptr = 0; 827 mg->mg_ptr = 0;
793 828
794 coro_state_destroy (coro); 829 coro_state_destroy (coro);
795 830
796 return 0; 831 return 0;
797} 832}
798 833
799static int 834static int
800coro_state_dup (MAGIC *mg, CLONE_PARAMS *params) 835coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params)
801{ 836{
802 struct coro *coro = (struct coro *)mg->mg_ptr; 837 struct coro *coro = (struct coro *)mg->mg_ptr;
803 838
804 ++coro->refcnt; 839 ++coro->refcnt;
805 840
806 return 0; 841 return 0;
807} 842}
808 843
809static MGVTBL coro_state_vtbl = { 0, 0, 0, 0, coro_state_clear, 0, coro_state_dup, 0 }; 844static MGVTBL coro_state_vtbl = {
845 0, 0, 0, 0,
846 coro_state_clear,
847 0,
848#ifdef MGf_DUP
849 coro_state_dup,
850#else
851# define MGf_DUP 0
852#endif
853};
810 854
811static struct coro * 855static struct coro *
812SvSTATE (SV *coro) 856SvSTATE (SV *coro)
813{ 857{
814 HV *stash; 858 HV *stash;
829 assert (mg->mg_type == PERL_MAGIC_ext); 873 assert (mg->mg_type == PERL_MAGIC_ext);
830 return (struct coro *)mg->mg_ptr; 874 return (struct coro *)mg->mg_ptr;
831} 875}
832 876
833static void 877static void
834prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev, SV *next, int flags) 878prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv)
835{ 879{
836 ta->prev = SvSTATE (prev); 880 ta->prev = SvSTATE (prev_sv);
837 ta->next = SvSTATE (next); 881 ta->next = SvSTATE (next_sv);
838 ta->flags = flags;
839} 882}
840 883
841static void 884static void
842api_transfer (SV *prev, SV *next, int flags) 885api_transfer (SV *prev_sv, SV *next_sv)
843{ 886{
844 dTHX;
845 struct transfer_args ta; 887 struct transfer_args ta;
846 888
847 prepare_transfer (aTHX_ &ta, prev, next, flags); 889 prepare_transfer (&ta, prev_sv, next_sv);
848 TRANSFER (ta); 890 TRANSFER (ta);
891}
892
893static int
894api_save (SV *coro_sv, int new_save)
895{
896 struct coro *coro = SvSTATE (coro_sv);
897 int old_save = coro->save;
898
899 if (new_save >= 0)
900 coro->save = new_save;
901
902 return old_save;
849} 903}
850 904
851/** Coro ********************************************************************/ 905/** Coro ********************************************************************/
852 906
853#define PRIO_MAX 3 907#define PRIO_MAX 3
856#define PRIO_LOW -1 910#define PRIO_LOW -1
857#define PRIO_IDLE -3 911#define PRIO_IDLE -3
858#define PRIO_MIN -4 912#define PRIO_MIN -4
859 913
860/* for Coro.pm */ 914/* for Coro.pm */
861static GV *coro_current, *coro_idle; 915static SV *coro_current;
862static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 916static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
863static int coro_nready; 917static int coro_nready;
864 918
865static void 919static void
866coro_enq (pTHX_ SV *sv) 920coro_enq (SV *coro_sv)
867{ 921{
868 int prio;
869
870 if (SvTYPE (sv) != SVt_PVHV)
871 croak ("Coro::ready tried to enqueue something that is not a coroutine");
872
873 prio = SvSTATE (sv)->prio;
874
875 av_push (coro_ready [prio - PRIO_MIN], sv); 922 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
876 coro_nready++; 923 coro_nready++;
877} 924}
878 925
879static SV * 926static SV *
880coro_deq (pTHX_ int min_prio) 927coro_deq (int min_prio)
881{ 928{
882 int prio = PRIO_MAX - PRIO_MIN; 929 int prio = PRIO_MAX - PRIO_MIN;
883 930
884 min_prio -= PRIO_MIN; 931 min_prio -= PRIO_MIN;
885 if (min_prio < 0) 932 if (min_prio < 0)
893 } 940 }
894 941
895 return 0; 942 return 0;
896} 943}
897 944
898static void 945static int
899api_ready (SV *coro) 946api_ready (SV *coro_sv)
900{ 947{
901 dTHX; 948 struct coro *coro;
902 949
903 if (SvROK (coro)) 950 if (SvROK (coro_sv))
904 coro = SvRV (coro); 951 coro_sv = SvRV (coro_sv);
952
953 coro = SvSTATE (coro_sv);
954
955 if (coro->flags & CF_READY)
956 return 0;
957
958#if 0 /* this is actually harmless */
959 if (coro->flags & CF_RUNNING)
960 croak ("Coro::ready called on currently running coroutine");
961#endif
962
963 coro->flags |= CF_READY;
905 964
906 LOCK; 965 LOCK;
907 coro_enq (aTHX_ SvREFCNT_inc (coro)); 966 coro_enq (SvREFCNT_inc (coro_sv));
908 UNLOCK; 967 UNLOCK;
909}
910 968
969 return 1;
970}
971
911static void 972static int
973api_is_ready (SV *coro_sv)
974{
975 return !!SvSTATE (coro_sv)->flags & CF_READY;
976}
977
978static void
912prepare_schedule (aTHX_ struct transfer_args *ta) 979prepare_schedule (struct transfer_args *ta)
913{ 980{
914 SV *current, *prev, *next; 981 SV *prev, *next;
915
916 LOCK;
917
918 current = GvSV (coro_current);
919 982
920 for (;;) 983 for (;;)
921 { 984 {
922 LOCK; 985 LOCK;
923
924 next = coro_deq (aTHX_ PRIO_MIN); 986 next = coro_deq (PRIO_MIN);
987 UNLOCK;
925 988
926 if (next) 989 if (next)
927 break; 990 break;
928 991
929 UNLOCK;
930
931 { 992 {
932 dSP; 993 dSP;
933 994
934 ENTER; 995 ENTER;
935 SAVETMPS; 996 SAVETMPS;
936 997
937 PUSHMARK (SP); 998 PUSHMARK (SP);
938 PUTBACK; 999 PUTBACK;
939 call_sv (GvSV (coro_idle), G_DISCARD); 1000 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
940 1001
941 FREETMPS; 1002 FREETMPS;
942 LEAVE; 1003 LEAVE;
943 } 1004 }
944 } 1005 }
945 1006
946 prev = SvRV (current); 1007 prev = SvRV (coro_current);
947 SvRV (current) = next; 1008 SvRV_set (coro_current, next);
948 1009
949 /* free this only after the transfer */ 1010 /* free this only after the transfer */
1011 LOCK;
950 free_coro_mortal (); 1012 free_coro_mortal ();
1013 UNLOCK;
951 coro_mortal = prev; 1014 coro_mortal = prev;
952 1015
1016 assert (!SvROK(prev));//D
1017 assert (!SvROK(next));//D
1018
953 ta->prev = SvSTATE (prev); 1019 ta->prev = SvSTATE (prev);
954 ta->next = SvSTATE (next); 1020 ta->next = SvSTATE (next);
955 ta->flags = TRANSFER_SAVE_ALL;
956 1021
957 UNLOCK; 1022 assert (ta->next->flags & CF_READY);
1023 ta->next->flags &= ~CF_READY;
958} 1024}
959 1025
960static void 1026static void
961prepare_cede (aTHX_ struct transfer_args *ta) 1027prepare_cede (struct transfer_args *ta)
962{ 1028{
963 LOCK; 1029 api_ready (coro_current);
964 coro_enq (aTHX_ SvREFCNT_inc (SvRV (GvSV (coro_current))));
965 UNLOCK;
966 1030
967 prepare_schedule (ta); 1031 prepare_schedule (ta);
968} 1032}
969 1033
970static void 1034static void
971api_schedule (void) 1035api_schedule (void)
972{ 1036{
973 dTHX;
974 struct transfer_args ta; 1037 struct transfer_args ta;
975 1038
976 prepare_schedule (&ta); 1039 prepare_schedule (&ta);
977 TRANSFER (ta); 1040 TRANSFER (ta);
978} 1041}
979 1042
980static void 1043static void
981api_cede (void) 1044api_cede (void)
982{ 1045{
983 dTHX;
984 struct transfer_args ta; 1046 struct transfer_args ta;
985 1047
986 prepare_cede (&ta); 1048 prepare_cede (&ta);
987 TRANSFER (ta); 1049 TRANSFER (ta);
988} 1050}
998#endif 1060#endif
999 BOOT_PAGESIZE; 1061 BOOT_PAGESIZE;
1000 1062
1001 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1063 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1002 1064
1003 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 1065 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1004 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 1066 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1005 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 1067 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1068 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1069 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1006 1070
1007 main_mainstack = PL_mainstack; 1071 main_mainstack = PL_mainstack;
1008 1072
1009 coroapi.ver = CORO_API_VERSION; 1073 coroapi.ver = CORO_API_VERSION;
1010 coroapi.transfer = api_transfer; 1074 coroapi.transfer = api_transfer;
1020 HV *hv; 1084 HV *hv;
1021 int i; 1085 int i;
1022 1086
1023 Newz (0, coro, 1, struct coro); 1087 Newz (0, coro, 1, struct coro);
1024 coro->args = newAV (); 1088 coro->args = newAV ();
1089 coro->save = CORO_SAVE_ALL;
1090 coro->flags = CF_NEW;
1025 1091
1026 hv = newHV (); 1092 hv = newHV ();
1027 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1093 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1028 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1094 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1029 1095
1030 for (i = 1; i < items; i++) 1096 for (i = 1; i < items; i++)
1031 av_push (coro->args, newSVsv (ST (i))); 1097 av_push (coro->args, newSVsv (ST (i)));
1032
1033 /*coro->mainstack = 0; *//*actual work is done inside transfer */
1034 /*coro->stack = 0;*/
1035} 1098}
1036 OUTPUT: 1099 OUTPUT:
1100 RETVAL
1101
1102int
1103save (SV *coro, int new_save = -1)
1104 CODE:
1105 RETVAL = api_save (coro, new_save);
1106 OUTPUT:
1037 RETVAL 1107 RETVAL
1038 1108
1039void 1109void
1040_set_stacklevel (...) 1110_set_stacklevel (...)
1041 ALIAS: 1111 ALIAS:
1042 Coro::State::transfer = 1 1112 Coro::State::transfer = 1
1043 Coro::schedule = 2 1113 Coro::schedule = 2
1044 Coro::cede = 3 1114 Coro::cede = 3
1045 Coro::Cont::yield = 4
1046 CODE: 1115 CODE:
1047{ 1116{
1048 struct transfer_args ta; 1117 struct transfer_args ta;
1049 1118
1050 switch (ix) 1119 switch (ix)
1051 { 1120 {
1052 case 0: 1121 case 0:
1053 ta.prev = (struct coro *)INT2PTR (coro_stack *, SvIV (ST (0))); 1122 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1054 ta.next = 0; 1123 ta.next = 0;
1055 ta.flags = TRANSFER_SET_STACKLEVEL;
1056 break; 1124 break;
1057 1125
1058 case 1: 1126 case 1:
1059 if (items != 3) 1127 if (items != 2)
1060 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); 1128 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1061 1129
1062 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); 1130 prepare_transfer (&ta, ST (0), ST (1));
1063 break; 1131 break;
1064 1132
1065 case 2: 1133 case 2:
1066 prepare_schedule (&ta); 1134 prepare_schedule (&ta);
1067 break; 1135 break;
1068 1136
1069 case 3: 1137 case 3:
1070 prepare_cede (&ta); 1138 prepare_cede (&ta);
1071 break; 1139 break;
1072
1073 case 4:
1074 {
1075 SV *yieldstack;
1076 SV *sv;
1077 AV *defav = GvAV (PL_defgv);
1078
1079 yieldstack = *hv_fetch (
1080 (HV *)SvRV (GvSV (coro_current)),
1081 "yieldstack", sizeof ("yieldstack") - 1,
1082 0
1083 );
1084
1085 /* set up @_ -- ugly */
1086 av_clear (defav);
1087 av_fill (defav, items - 1);
1088 while (items--)
1089 av_store (defav, items, SvREFCNT_inc (ST(items)));
1090
1091 sv = av_pop ((AV *)SvRV (yieldstack));
1092 ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1093 ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1094 ta.flags = 0;
1095 SvREFCNT_dec (sv);
1096 }
1097 break;
1098
1099 } 1140 }
1100 1141
1101 TRANSFER (ta); 1142 TRANSFER (ta);
1102} 1143}
1103 1144
1110 sv_unmagic (SvRV (dst), PERL_MAGIC_ext); 1151 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1111 1152
1112 ++coro_src->refcnt; 1153 ++coro_src->refcnt;
1113 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP; 1154 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1114} 1155}
1115
1116void
1117_nonlocal_goto (IV nextop)
1118 CODE:
1119 /* uuh, somebody will kill me again for this */
1120 PL_op->op_next = INT2PTR (OP *, nextop);
1121 1156
1122void 1157void
1123_exit (code) 1158_exit (code)
1124 int code 1159 int code
1125 PROTOTYPE: $ 1160 PROTOTYPE: $
1126 CODE: 1161 CODE:
1127 _exit (code); 1162 _exit (code);
1128 1163
1164int
1165cctx_count ()
1166 CODE:
1167 RETVAL = cctx_count;
1168 OUTPUT:
1169 RETVAL
1170
1171int
1172cctx_idle ()
1173 CODE:
1174 RETVAL = cctx_idle;
1175 OUTPUT:
1176 RETVAL
1177
1129MODULE = Coro::State PACKAGE = Coro 1178MODULE = Coro::State PACKAGE = Coro
1130 1179
1131BOOT: 1180BOOT:
1132{ 1181{
1133 int i; 1182 int i;
1139 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1188 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1140 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1189 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1141 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1190 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1142 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1191 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1143 1192
1144 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); 1193 coro_current = get_sv ("Coro::current", FALSE);
1145 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); 1194 SvREADONLY_on (coro_current);
1146 1195
1147 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1196 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1148 coro_ready[i] = newAV (); 1197 coro_ready[i] = newAV ();
1149 1198
1150 { 1199 {
1151 SV *sv = perl_get_sv("Coro::API", 1); 1200 SV *sv = perl_get_sv("Coro::API", 1);
1152 1201
1153 coroapi.schedule = api_schedule; 1202 coroapi.schedule = api_schedule;
1203 coroapi.save = api_save;
1154 coroapi.cede = api_cede; 1204 coroapi.cede = api_cede;
1155 coroapi.ready = api_ready; 1205 coroapi.ready = api_ready;
1206 coroapi.is_ready = api_is_ready;
1156 coroapi.nready = &coro_nready; 1207 coroapi.nready = &coro_nready;
1157 coroapi.current = coro_current; 1208 coroapi.current = coro_current;
1158 1209
1159 GCoroAPI = &coroapi; 1210 GCoroAPI = &coroapi;
1160 sv_setiv (sv, (IV)&coroapi); 1211 sv_setiv (sv, (IV)&coroapi);
1161 SvREADONLY_on (sv); 1212 SvREADONLY_on (sv);
1162 } 1213 }
1163} 1214}
1215
1216void
1217_set_current (SV *current)
1218 PROTOTYPE: $
1219 CODE:
1220 SvREFCNT_dec (SvRV (coro_current));
1221 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1164 1222
1165int 1223int
1166prio (Coro::State coro, int newprio = 0) 1224prio (Coro::State coro, int newprio = 0)
1167 ALIAS: 1225 ALIAS:
1168 nice = 1 1226 nice = 1
1180 1238
1181 coro->prio = newprio; 1239 coro->prio = newprio;
1182 } 1240 }
1183} 1241}
1184 1242
1185void 1243SV *
1186ready (SV *self) 1244ready (SV *self)
1187 PROTOTYPE: $ 1245 PROTOTYPE: $
1188 CODE: 1246 CODE:
1189 api_ready (self); 1247 RETVAL = boolSV (api_ready (self));
1248 OUTPUT:
1249 RETVAL
1250
1251SV *
1252is_ready (SV *self)
1253 PROTOTYPE: $
1254 CODE:
1255 RETVAL = boolSV (api_is_ready (self));
1256 OUTPUT:
1257 RETVAL
1190 1258
1191int 1259int
1192nready (...) 1260nready (...)
1193 PROTOTYPE: 1261 PROTOTYPE:
1194 CODE: 1262 CODE:
1200 1268
1201SV * 1269SV *
1202_get_state () 1270_get_state ()
1203 CODE: 1271 CODE:
1204{ 1272{
1205 struct { 1273 struct io_state *data;
1206 int errorno;
1207 int laststype;
1208 int laststatval;
1209 Stat_t statcache;
1210 } data;
1211 1274
1275 RETVAL = newSV (sizeof (struct io_state));
1276 data = (struct io_state *)SvPVX (RETVAL);
1277 SvCUR_set (RETVAL, sizeof (struct io_state));
1278 SvPOK_only (RETVAL);
1279
1212 data.errorno = errno; 1280 data->errorno = errno;
1213 data.laststype = PL_laststype; 1281 data->laststype = PL_laststype;
1214 data.laststatval = PL_laststatval; 1282 data->laststatval = PL_laststatval;
1215 data.statcache = PL_statcache; 1283 data->statcache = PL_statcache;
1216
1217 RETVAL = newSVpvn ((char *)&data, sizeof data);
1218} 1284}
1219 OUTPUT: 1285 OUTPUT:
1220 RETVAL 1286 RETVAL
1221 1287
1222void 1288void
1223_set_state (char *data_) 1289_set_state (char *data_)
1224 PROTOTYPE: $ 1290 PROTOTYPE: $
1225 CODE: 1291 CODE:
1226{ 1292{
1227 struct { 1293 struct io_state *data = (void *)data_;
1228 int errorno;
1229 int laststype;
1230 int laststatval;
1231 Stat_t statcache;
1232 } *data = (void *)data_;
1233 1294
1234 errno = data->errorno; 1295 errno = data->errorno;
1235 PL_laststype = data->laststype; 1296 PL_laststype = data->laststype;
1236 PL_laststatval = data->laststatval; 1297 PL_laststatval = data->laststatval;
1237 PL_statcache = data->statcache; 1298 PL_statcache = data->statcache;
1238} 1299}
1300

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines