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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines