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.103 by root, Mon Nov 27 01:33:30 2006 UTC vs.
Revision 1.131 by root, Fri Dec 29 11:37:49 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines