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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines