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.119 by root, Sat Dec 2 15:41:28 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#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
15
16#define PERL_VERSION_ATLEAST(a,b,c) \
17 (PERL_REVISION > (a) \
18 || (PERL_REVISION == (a) \
19 && (PERL_VERSION > (b) \
20 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c)))))
21
22#if !PERL_VERSION_ATLEAST (5,6,0)
23# ifndef PL_ppaddr
24# define PL_ppaddr ppaddr
25# endif
26# ifndef call_sv
27# define call_sv perl_call_sv
28# endif
29# ifndef get_sv
30# define get_sv perl_get_sv
31# endif
32# ifndef get_cv
33# define get_cv perl_get_cv
34# endif
35# ifndef IS_PADGV
36# define IS_PADGV(v) 0
37# endif
38# ifndef IS_PADCONST
39# define IS_PADCONST(v) 0
40# endif
41#endif
42
43#include <stdio.h> 9#include <stdio.h>
44#include <errno.h> 10#include <errno.h>
45 11#include <assert.h>
46#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
47# undef STACKGUARD
48#endif
49
50#ifndef STACKGUARD
51# define STACKGUARD 0
52#endif
53 12
54#ifdef HAVE_MMAP 13#ifdef HAVE_MMAP
55# include <unistd.h> 14# include <unistd.h>
56# include <sys/mman.h> 15# include <sys/mman.h>
57# ifndef MAP_ANONYMOUS 16# ifndef MAP_ANONYMOUS
72#else 31#else
73# define PAGESIZE 0 32# define PAGESIZE 0
74# define BOOT_PAGESIZE (void)0 33# define BOOT_PAGESIZE (void)0
75#endif 34#endif
76 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
77/* 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
78 * 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
79 * and should be unique. */ 90 * and should be unique. */
80#define dSTACKLEVEL int stacklevel 91#define dSTACKLEVEL int stacklevel
81#define STACKLEVEL ((void *)&stacklevel) 92#define STACKLEVEL ((void *)&stacklevel)
89#endif 100#endif
90 101
91#define NOINLINE attribute ((noinline)) 102#define NOINLINE attribute ((noinline))
92 103
93#include "CoroAPI.h" 104#include "CoroAPI.h"
94
95#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
96 105
97#ifdef USE_ITHREADS 106#ifdef USE_ITHREADS
98static perl_mutex coro_mutex; 107static perl_mutex coro_mutex;
99# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 108# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
100# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 109# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
101#else 110#else
102# define LOCK (void)0 111# define LOCK (void)0
103# define UNLOCK (void)0 112# define UNLOCK (void)0
104#endif 113#endif
105 114
115struct io_state
116{
117 int errorno;
118 I32 laststype;
119 int laststatval;
120 Stat_t statcache;
121};
122
106static struct CoroAPI coroapi; 123static struct CoroAPI coroapi;
107static AV *main_mainstack; /* used to differentiate between $main and others */ 124static AV *main_mainstack; /* used to differentiate between $main and others */
108static HV *coro_state_stash, *coro_stash; 125static HV *coro_state_stash, *coro_stash;
109static SV *coro_mortal; /* will be freed after next transfer */ 126static SV *coro_mortal; /* will be freed after next transfer */
110 127
143 coro_cctx *cctx; 160 coro_cctx *cctx;
144 161
145 /* data associated with this coroutine (initial args) */ 162 /* data associated with this coroutine (initial args) */
146 AV *args; 163 AV *args;
147 int refcnt; 164 int refcnt;
148 int flags; 165 int save; /* CORO_SAVE flags */
166 int flags; /* CF_ flags */
149 167
150 /* optionally saved, might be zero */ 168 /* optionally saved, might be zero */
151 AV *defav; 169 AV *defav; /* @_ */
152 SV *defsv; 170 SV *defsv; /* $_ */
153 SV *errsv; 171 SV *errsv; /* $@ */
172 SV *irssv; /* $/ */
173 SV *irssv_sv; /* real $/ cache */
154 174
155#define VAR(name,type) type name; 175#define VAR(name,type) type name;
156# include "state.h" 176# include "state.h"
157#undef VAR 177#undef VAR
158 178
244 264
245 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) 265 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
246 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 266 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
247 else 267 else
248 { 268 {
249#if 0 269#if PREFER_PERL_FUNCTIONS
250 /* this is probably cleaner, but also slower? */ 270 /* this is probably cleaner, but also slower? */
251 CV *cp = Perl_cv_clone (cv); 271 CV *cp = Perl_cv_clone (cv);
252 CvPADLIST (cv) = CvPADLIST (cp); 272 CvPADLIST (cv) = CvPADLIST (cp);
253 CvPADLIST (cp) = 0; 273 CvPADLIST (cp) = 0;
254 SvREFCNT_dec (cp); 274 SvREFCNT_dec (cp);
281} 301}
282 302
283#define SB do { 303#define SB do {
284#define SE } while (0) 304#define SE } while (0)
285 305
286#define LOAD(state) load_state ((state))
287#define SAVE(state,flags) save_state ((state),(flags))
288
289#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
290 307
291static void 308static void
292load_state (Coro__State c) 309load_perl (Coro__State c)
293{ 310{
294#define VAR(name,type) PL_ ## name = c->name; 311#define VAR(name,type) PL_ ## name = c->name;
295# include "state.h" 312# include "state.h"
296#undef VAR 313#undef VAR
297 314
298 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 315 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
299 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 316 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
300 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 }
301 329
302 { 330 {
303 dSP; 331 dSP;
304 CV *cv; 332 CV *cv;
305 333
314 PUTBACK; 342 PUTBACK;
315 } 343 }
316} 344}
317 345
318static void 346static void
319save_state (Coro__State c, int flags) 347save_perl (Coro__State c)
320{ 348{
321 { 349 {
322 dSP; 350 dSP;
323 I32 cxix = cxstack_ix; 351 I32 cxix = cxstack_ix;
324 PERL_CONTEXT *ccstk = cxstack; 352 PERL_CONTEXT *ccstk = cxstack;
327 /* 355 /*
328 * 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
329 * (and reinitialize) all cv's in the whole callchain :( 357 * (and reinitialize) all cv's in the whole callchain :(
330 */ 358 */
331 359
360 EXTEND (SP, 3 + 1);
332 PUSHs (Nullsv); 361 PUSHs (Nullsv);
333 /* this loop was inspired by pp_caller */ 362 /* this loop was inspired by pp_caller */
334 for (;;) 363 for (;;)
335 { 364 {
336 while (cxix >= 0) 365 while (cxix >= 0)
342 CV *cv = cx->blk_sub.cv; 371 CV *cv = cx->blk_sub.cv;
343 372
344 if (CvDEPTH (cv)) 373 if (CvDEPTH (cv))
345 { 374 {
346 EXTEND (SP, 3); 375 EXTEND (SP, 3);
347
348 PUSHs ((SV *)CvPADLIST (cv)); 376 PUSHs ((SV *)CvPADLIST (cv));
349 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 377 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
350 PUSHs ((SV *)cv); 378 PUSHs ((SV *)cv);
351 379
352 CvDEPTH (cv) = 0; 380 CvDEPTH (cv) = 0;
353 get_padlist (cv); 381 get_padlist (cv);
354 } 382 }
355 } 383 }
356#ifdef CXt_FORMAT
357 else if (CxTYPE (cx) == CXt_FORMAT)
358 {
359 /* I never used formats, so how should I know how these are implemented? */
360 /* my bold guess is as a simple, plain sub... */
361 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
362 }
363#endif
364 } 384 }
365 385
366 if (top_si->si_type == PERLSI_MAIN) 386 if (top_si->si_type == PERLSI_MAIN)
367 break; 387 break;
368 388
372 } 392 }
373 393
374 PUTBACK; 394 PUTBACK;
375 } 395 }
376 396
377 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;
378 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 398 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
379 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;
380 401
381#define VAR(name,type)c->name = PL_ ## name; 402#define VAR(name,type)c->name = PL_ ## name;
382# include "state.h" 403# include "state.h"
383#undef VAR 404#undef VAR
384} 405}
387 * allocate various perl stacks. This is an exact copy 408 * allocate various perl stacks. This is an exact copy
388 * of perl.c:init_stacks, except that it uses less memory 409 * of perl.c:init_stacks, except that it uses less memory
389 * on the (sometimes correct) assumption that coroutines do 410 * on the (sometimes correct) assumption that coroutines do
390 * not usually need a lot of stackspace. 411 * not usually need a lot of stackspace.
391 */ 412 */
413#if PREFER_PERL_FUNCTIONS
414# define coro_init_stacks init_stacks
415#else
392static void 416static void
393coro_init_stacks () 417coro_init_stacks ()
394{ 418{
395 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT)); 419 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
396 PL_curstackinfo->si_type = PERLSI_MAIN; 420 PL_curstackinfo->si_type = PERLSI_MAIN;
426 New(54,PL_retstack,16,OP*); 450 New(54,PL_retstack,16,OP*);
427 PL_retstack_ix = 0; 451 PL_retstack_ix = 0;
428 PL_retstack_max = 16; 452 PL_retstack_max = 16;
429#endif 453#endif
430} 454}
455#endif
431 456
432/* 457/*
433 * destroy the stacks, the callchain etc... 458 * destroy the stacks, the callchain etc...
434 */ 459 */
435static void 460static void
436coro_destroy_stacks () 461coro_destroy_stacks ()
437{ 462{
438 if (!IN_DESTRUCT) 463 if (!IN_DESTRUCT)
439 { 464 {
440 /* is this ugly, I ask? */ 465 /* restore all saved variables and stuff */
441 LEAVE_SCOPE (0); 466 LEAVE_SCOPE (0);
467 assert (PL_tmps_floor == -1);
442 468
443 /* sure it is, but more important: is it correct?? :/ */ 469 /* free all temporaries */
444 FREETMPS; 470 FREETMPS;
471 assert (PL_tmps_ix == -1);
445 472
446 /*POPSTACK_TO (PL_mainstack);*//*D*//*use*/ 473 POPSTACK_TO (PL_mainstack);
447 } 474 }
448 475
449 while (PL_curstackinfo->si_next) 476 while (PL_curstackinfo->si_next)
450 PL_curstackinfo = PL_curstackinfo->si_next; 477 PL_curstackinfo = PL_curstackinfo->si_next;
451 478
452 while (PL_curstackinfo) 479 while (PL_curstackinfo)
453 { 480 {
454 PERL_SI *p = PL_curstackinfo->si_prev; 481 PERL_SI *p = PL_curstackinfo->si_prev;
455 482
456 { /*D*//*remove*/
457 dSP;
458 SWITCHSTACK (PL_curstack, PL_curstackinfo->si_stack);
459 PUTBACK; /* possibly superfluous */
460 }
461
462 if (!IN_DESTRUCT) 483 if (!IN_DESTRUCT)
463 {
464 dounwind (-1);/*D*//*remove*/
465 SvREFCNT_dec (PL_curstackinfo->si_stack); 484 SvREFCNT_dec (PL_curstackinfo->si_stack);
466 }
467 485
468 Safefree (PL_curstackinfo->si_cxstack); 486 Safefree (PL_curstackinfo->si_cxstack);
469 Safefree (PL_curstackinfo); 487 Safefree (PL_curstackinfo);
470 PL_curstackinfo = p; 488 PL_curstackinfo = p;
471 } 489 }
497 515
498 { 516 {
499 dSP; 517 dSP;
500 LOGOP myop; 518 LOGOP myop;
501 519
502 /* I have no idea why this is needed, but it is */
503 PUSHMARK (SP);
504
505 SvREFCNT_dec (GvAV (PL_defgv)); 520 SvREFCNT_dec (GvAV (PL_defgv));
506 GvAV (PL_defgv) = coro->args; coro->args = 0; 521 GvAV (PL_defgv) = coro->args; coro->args = 0;
507 522
508 Zero (&myop, 1, LOGOP); 523 Zero (&myop, 1, LOGOP);
509 myop.op_next = Nullop; 524 myop.op_next = Nullop;
510 myop.op_flags = OPf_WANT_VOID; 525 myop.op_flags = OPf_WANT_VOID;
511 526
512 PL_op = (OP *)&myop;
513
514 PUSHMARK (SP); 527 PUSHMARK (SP);
515 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 528 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
516 PUTBACK; 529 PUTBACK;
530 PL_op = (OP *)&myop;
517 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 531 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
518 SPAGAIN; 532 SPAGAIN;
519 } 533 }
520 534
521 ENTER; /* necessary e.g. for dounwind */ 535 ENTER; /* necessary e.g. for dounwind */
529 SvREFCNT_dec (coro_mortal); 543 SvREFCNT_dec (coro_mortal);
530 coro_mortal = 0; 544 coro_mortal = 0;
531 } 545 }
532} 546}
533 547
548/* inject a fake call to Coro::State::_cctx_init into the execution */
534static void NOINLINE 549static void NOINLINE
535prepare_cctx (coro_cctx *cctx) 550prepare_cctx (coro_cctx *cctx)
536{ 551{
537 dSP; 552 dSP;
538 LOGOP myop; 553 LOGOP myop;
539 554
540 Zero (&myop, 1, LOGOP); 555 Zero (&myop, 1, LOGOP);
541 myop.op_next = PL_op; 556 myop.op_next = PL_op;
542 myop.op_flags = OPf_WANT_VOID; 557 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
543
544 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
545 558
546 PUSHMARK (SP); 559 PUSHMARK (SP);
560 EXTEND (SP, 2);
561 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx))));
547 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 562 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
548 PUTBACK; 563 PUTBACK;
564 PL_op = (OP *)&myop;
549 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 565 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
550 SPAGAIN; 566 SPAGAIN;
551} 567}
552 568
553static void 569static void
554coro_run (void *arg) 570coro_run (void *arg)
555{ 571{
556 /* coro_run is the alternative epilogue of transfer() */ 572 /* coro_run is the alternative tail of transfer(), so unlock here. */
557 UNLOCK; 573 UNLOCK;
558 574
559 /* 575 /*
560 * this is a _very_ stripped down perl interpreter ;) 576 * this is a _very_ stripped down perl interpreter ;)
561 */ 577 */
563 579
564 /* inject call to cctx_init */ 580 /* inject call to cctx_init */
565 prepare_cctx ((coro_cctx *)arg); 581 prepare_cctx ((coro_cctx *)arg);
566 582
567 /* 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;
568 perl_run (PL_curinterp); 585 perl_run (PL_curinterp);
569 586
570 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); 587 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
571 abort (); 588 abort ();
572} 589}
576{ 593{
577 coro_cctx *cctx; 594 coro_cctx *cctx;
578 595
579 ++cctx_count; 596 ++cctx_count;
580 597
581 New (0, cctx, 1, coro_cctx); 598 Newz (0, cctx, 1, coro_cctx);
582 599
583#if HAVE_MMAP 600#if HAVE_MMAP
584 601
585 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 602 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
586 /* mmap suppsedly does allocate-on-write for us */ 603 /* mmap supposedly does allocate-on-write for us */
587 cctx->sptr = mmap (0, cctx->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);
588 605
589 if (cctx->sptr == (void *)-1) 606 if (cctx->sptr == (void *)-1)
590 { 607 {
591 perror ("FATAL: unable to mmap stack for coroutine"); 608 perror ("FATAL: unable to mmap stack for coroutine");
681 cctx_first = cctx; 698 cctx_first = cctx;
682} 699}
683 700
684/* never call directly, always through the coro_state_transfer global variable */ 701/* never call directly, always through the coro_state_transfer global variable */
685static void NOINLINE 702static void NOINLINE
686transfer (struct coro *prev, struct coro *next, int flags) 703transfer (struct coro *prev, struct coro *next)
687{ 704{
688 dSTACKLEVEL; 705 dSTACKLEVEL;
689 706
690 /* sometimes transfer is only called to set idle_sp */ 707 /* sometimes transfer is only called to set idle_sp */
691 if (flags == TRANSFER_SET_STACKLEVEL) 708 if (!next)
709 {
692 ((coro_cctx *)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 }
693 else if (prev != next) 713 else if (prev != next)
694 { 714 {
695 coro_cctx *prev__cctx; 715 coro_cctx *prev__cctx;
696 716
697 if (prev->flags & CF_NEW) 717 if (prev->flags & CF_NEW)
718 if (next->flags & CF_NEW) 738 if (next->flags & CF_NEW)
719 { 739 {
720 /* need to start coroutine */ 740 /* need to start coroutine */
721 next->flags &= ~CF_NEW; 741 next->flags &= ~CF_NEW;
722 /* first get rid of the old state */ 742 /* first get rid of the old state */
723 SAVE (prev, -1); 743 save_perl (prev);
724 /* setup coroutine call */ 744 /* setup coroutine call */
725 setup_coro (next); 745 setup_coro (next);
726 /* need a new stack */ 746 /* need a new stack */
727 assert (!next->stack); 747 assert (!next->cctx);
728 } 748 }
729 else 749 else
730 { 750 {
731 /* coroutine already started */ 751 /* coroutine already started */
732 SAVE (prev, flags); 752 save_perl (prev);
733 LOAD (next); 753 load_perl (next);
734 } 754 }
735 755
736 prev__cctx = prev->cctx; 756 prev__cctx = prev->cctx;
737 757
738 /* possibly "free" the cctx */ 758 /* possibly "free" the cctx */
768} 788}
769 789
770struct transfer_args 790struct transfer_args
771{ 791{
772 struct coro *prev, *next; 792 struct coro *prev, *next;
773 int flags;
774}; 793};
775 794
776#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) 795#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
777 796
778static void 797static void
779coro_state_destroy (struct coro *coro) 798coro_state_destroy (struct coro *coro)
780{ 799{
781 if (coro->refcnt--) 800 if (coro->refcnt--)
782 return; 801 return;
783 802
784 if (coro->mainstack && coro->mainstack != main_mainstack) 803 if (coro->mainstack && coro->mainstack != main_mainstack)
785 { 804 {
786 struct coro temp; 805 struct coro temp;
806 Zero (&temp, 1, struct coro);
807 temp.save = CORO_SAVE_ALL;
787 808
788 if (coro->flags & CF_RUNNING) 809 if (coro->flags & CF_RUNNING)
789 croak ("FATAL: tried to destroy currently running coroutine"); 810 croak ("FATAL: tried to destroy currently running coroutine");
790 811
791 SAVE ((&temp), TRANSFER_SAVE_ALL); 812 save_perl (&temp);
792 LOAD (coro); 813 load_perl (coro);
793 814
794 coro_destroy_stacks (); 815 coro_destroy_stacks ();
795 816
796 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 817 load_perl (&temp); /* this will get rid of defsv etc.. */
797 818
798 coro->mainstack = 0; 819 coro->mainstack = 0;
799 } 820 }
800 821
801 cctx_destroy (coro->cctx); 822 cctx_destroy (coro->cctx);
856 assert (mg->mg_type == PERL_MAGIC_ext); 877 assert (mg->mg_type == PERL_MAGIC_ext);
857 return (struct coro *)mg->mg_ptr; 878 return (struct coro *)mg->mg_ptr;
858} 879}
859 880
860static void 881static void
861prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) 882prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv)
862{ 883{
863 ta->prev = SvSTATE (prev); 884 ta->prev = SvSTATE (prev_sv);
864 ta->next = SvSTATE (next); 885 ta->next = SvSTATE (next_sv);
865 ta->flags = flags;
866} 886}
867 887
868static void 888static void
869api_transfer (SV *prev, SV *next, int flags) 889api_transfer (SV *prev_sv, SV *next_sv)
870{ 890{
871 struct transfer_args ta; 891 struct transfer_args ta;
872 892
873 prepare_transfer (&ta, prev, next, flags); 893 prepare_transfer (&ta, prev_sv, next_sv);
874 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;
875} 907}
876 908
877/** Coro ********************************************************************/ 909/** Coro ********************************************************************/
878 910
879#define PRIO_MAX 3 911#define PRIO_MAX 3
925 coro = SvSTATE (coro_sv); 957 coro = SvSTATE (coro_sv);
926 958
927 if (coro->flags & CF_READY) 959 if (coro->flags & CF_READY)
928 return 0; 960 return 0;
929 961
930#if 0 /* this is actually harmless */
931 if (coro->flags & CF_RUNNING)
932 croak ("Coro::ready called on currently running coroutine");
933#endif
934
935 coro->flags |= CF_READY; 962 coro->flags |= CF_READY;
936 963
937 LOCK; 964 LOCK;
938 coro_enq (SvREFCNT_inc (coro_sv)); 965 coro_enq (SvREFCNT_inc (coro_sv));
939 UNLOCK; 966 UNLOCK;
942} 969}
943 970
944static int 971static int
945api_is_ready (SV *coro_sv) 972api_is_ready (SV *coro_sv)
946{ 973{
947 return !!SvSTATE (coro_sv)->flags & CF_READY; 974 return !!(SvSTATE (coro_sv)->flags & CF_READY);
948} 975}
949 976
950static void 977static void
951prepare_schedule (struct transfer_args *ta) 978prepare_schedule (struct transfer_args *ta)
952{ 979{
988 assert (!SvROK(prev));//D 1015 assert (!SvROK(prev));//D
989 assert (!SvROK(next));//D 1016 assert (!SvROK(next));//D
990 1017
991 ta->prev = SvSTATE (prev); 1018 ta->prev = SvSTATE (prev);
992 ta->next = SvSTATE (next); 1019 ta->next = SvSTATE (next);
993 ta->flags = TRANSFER_SAVE_ALL;
994 1020
995 assert (ta->flags & CF_READY); 1021 assert (ta->next->flags & CF_READY);
996 ta->next->flags &= ~CF_READY; 1022 ta->next->flags &= ~CF_READY;
997} 1023}
998 1024
999static void 1025static void
1000prepare_cede (struct transfer_args *ta) 1026prepare_cede (struct transfer_args *ta)
1033#endif 1059#endif
1034 BOOT_PAGESIZE; 1060 BOOT_PAGESIZE;
1035 1061
1036 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1062 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1037 1063
1038 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 1064 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1039 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 1065 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1040 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));
1041 1069
1042 main_mainstack = PL_mainstack; 1070 main_mainstack = PL_mainstack;
1043 1071
1044 coroapi.ver = CORO_API_VERSION; 1072 coroapi.ver = CORO_API_VERSION;
1045 coroapi.transfer = api_transfer; 1073 coroapi.transfer = api_transfer;
1055 HV *hv; 1083 HV *hv;
1056 int i; 1084 int i;
1057 1085
1058 Newz (0, coro, 1, struct coro); 1086 Newz (0, coro, 1, struct coro);
1059 coro->args = newAV (); 1087 coro->args = newAV ();
1088 coro->save = CORO_SAVE_ALL;
1060 coro->flags = CF_NEW; 1089 coro->flags = CF_NEW;
1061 1090
1062 hv = newHV (); 1091 hv = newHV ();
1063 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;
1064 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1093 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1065 1094
1066 for (i = 1; i < items; i++) 1095 for (i = 1; i < items; i++)
1067 av_push (coro->args, newSVsv (ST (i))); 1096 av_push (coro->args, newSVsv (ST (i)));
1068} 1097}
1069 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:
1070 RETVAL 1106 RETVAL
1071 1107
1072void 1108void
1073_set_stacklevel (...) 1109_set_stacklevel (...)
1074 ALIAS: 1110 ALIAS:
1082 switch (ix) 1118 switch (ix)
1083 { 1119 {
1084 case 0: 1120 case 0:
1085 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); 1121 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1086 ta.next = 0; 1122 ta.next = 0;
1087 ta.flags = TRANSFER_SET_STACKLEVEL;
1088 break; 1123 break;
1089 1124
1090 case 1: 1125 case 1:
1091 if (items != 3) 1126 if (items != 2)
1092 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);
1093 1128
1094 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); 1129 prepare_transfer (&ta, ST (0), ST (1));
1095 break; 1130 break;
1096 1131
1097 case 2: 1132 case 2:
1098 prepare_schedule (&ta); 1133 prepare_schedule (&ta);
1099 break; 1134 break;
1162 1197
1163 { 1198 {
1164 SV *sv = perl_get_sv("Coro::API", 1); 1199 SV *sv = perl_get_sv("Coro::API", 1);
1165 1200
1166 coroapi.schedule = api_schedule; 1201 coroapi.schedule = api_schedule;
1202 coroapi.save = api_save;
1167 coroapi.cede = api_cede; 1203 coroapi.cede = api_cede;
1168 coroapi.ready = api_ready; 1204 coroapi.ready = api_ready;
1169 coroapi.is_ready = api_is_ready; 1205 coroapi.is_ready = api_is_ready;
1170 coroapi.nready = &coro_nready; 1206 coroapi.nready = &coro_nready;
1171 coroapi.current = coro_current; 1207 coroapi.current = coro_current;
1174 sv_setiv (sv, (IV)&coroapi); 1210 sv_setiv (sv, (IV)&coroapi);
1175 SvREADONLY_on (sv); 1211 SvREADONLY_on (sv);
1176 } 1212 }
1177} 1213}
1178 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)));
1221
1179int 1222int
1180prio (Coro::State coro, int newprio = 0) 1223prio (Coro::State coro, int newprio = 0)
1181 ALIAS: 1224 ALIAS:
1182 nice = 1 1225 nice = 1
1183 CODE: 1226 CODE:
1185 RETVAL = coro->prio; 1228 RETVAL = coro->prio;
1186 1229
1187 if (items > 1) 1230 if (items > 1)
1188 { 1231 {
1189 if (ix) 1232 if (ix)
1190 newprio += coro->prio; 1233 newprio = coro->prio - newprio;
1191 1234
1192 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 1235 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1193 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 1236 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1194 1237
1195 coro->prio = newprio; 1238 coro->prio = newprio;
1196 } 1239 }
1197} 1240}
1241 OUTPUT:
1242 RETVAL
1198 1243
1199SV * 1244SV *
1200ready (SV *self) 1245ready (SV *self)
1201 PROTOTYPE: $ 1246 PROTOTYPE: $
1202 CODE: 1247 CODE:
1218 CODE: 1263 CODE:
1219 RETVAL = coro_nready; 1264 RETVAL = coro_nready;
1220 OUTPUT: 1265 OUTPUT:
1221 RETVAL 1266 RETVAL
1222 1267
1223void
1224_set_current (SV *current)
1225 PROTOTYPE: $
1226 CODE:
1227 SvREFCNT_dec (SvRV (coro_current));
1228 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1229
1230MODULE = Coro::State PACKAGE = Coro::AIO 1268MODULE = Coro::State PACKAGE = Coro::AIO
1231 1269
1232SV * 1270SV *
1233_get_state () 1271_get_state ()
1234 CODE: 1272 CODE:
1235{ 1273{
1236 struct { 1274 struct io_state *data;
1237 int errorno;
1238 int laststype;
1239 int laststatval;
1240 Stat_t statcache;
1241 } data;
1242 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
1243 data.errorno = errno; 1281 data->errorno = errno;
1244 data.laststype = PL_laststype; 1282 data->laststype = PL_laststype;
1245 data.laststatval = PL_laststatval; 1283 data->laststatval = PL_laststatval;
1246 data.statcache = PL_statcache; 1284 data->statcache = PL_statcache;
1247
1248 RETVAL = newSVpvn ((char *)&data, sizeof data);
1249} 1285}
1250 OUTPUT: 1286 OUTPUT:
1251 RETVAL 1287 RETVAL
1252 1288
1253void 1289void
1254_set_state (char *data_) 1290_set_state (char *data_)
1255 PROTOTYPE: $ 1291 PROTOTYPE: $
1256 CODE: 1292 CODE:
1257{ 1293{
1258 struct { 1294 struct io_state *data = (void *)data_;
1259 int errorno;
1260 int laststype;
1261 int laststatval;
1262 Stat_t statcache;
1263 } *data = (void *)data_;
1264 1295
1265 errno = data->errorno; 1296 errno = data->errorno;
1266 PL_laststype = data->laststype; 1297 PL_laststype = data->laststype;
1267 PL_laststatval = data->laststatval; 1298 PL_laststatval = data->laststatval;
1268 PL_statcache = data->statcache; 1299 PL_statcache = data->statcache;
1269} 1300}
1301

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines