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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines