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.105 by root, Mon Nov 27 02:01:33 2006 UTC vs.
Revision 1.132 by root, Fri Dec 29 13:03:05 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines