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.117 by root, Fri Dec 1 19:41:06 2006 UTC vs.
Revision 1.127 by root, Tue Dec 12 13:56:45 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)
713 prev->flags &= ~CF_RUNNING; 733 prev->flags &= ~CF_RUNNING;
714 next->flags |= CF_RUNNING; 734 next->flags |= CF_RUNNING;
715 735
716 LOCK; 736 LOCK;
717 737
718 if (next->mainstack) 738 if (next->flags & CF_NEW)
719 { 739 {
720 /* coroutine already started */ 740 /* need to start coroutine */
721 SAVE (prev, flags); 741 next->flags &= ~CF_NEW;
722 LOAD (next); 742 /* first get rid of the old state */
743 save_perl (prev);
744 /* setup coroutine call */
745 setup_coro (next);
746 /* need a new stack */
747 assert (!next->cctx);
723 } 748 }
724 else 749 else
725 { 750 {
726 /* need to start coroutine */ 751 /* coroutine already started */
727 assert (next->flags & CF_NEW); 752 save_perl (prev);
728 next->flags &= ~CF_NEW; 753 load_perl (next);
729 /* first get rid of the old state */
730 SAVE (prev, -1);
731 /* setup coroutine call */
732 setup_coro (next);
733 /* need a new stack */
734 assert (!next->stack);
735 } 754 }
736 755
737 prev__cctx = prev->cctx; 756 prev__cctx = prev->cctx;
738 757
739 /* possibly "free" the cctx */ 758 /* possibly "free" the cctx */
740 if (prev__cctx->idle_sp == STACKLEVEL && 0) 759 if (prev__cctx->idle_sp == STACKLEVEL)
741 { 760 {
742 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 761 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
743 assert (PL_top_env == prev__cctx->top_env); 762 assert (PL_top_env == prev__cctx->top_env);
744 763
745 prev->cctx = 0; 764 prev->cctx = 0;
769} 788}
770 789
771struct transfer_args 790struct transfer_args
772{ 791{
773 struct coro *prev, *next; 792 struct coro *prev, *next;
774 int flags;
775}; 793};
776 794
777#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) 795#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
778 796
779static void 797static void
780coro_state_destroy (struct coro *coro) 798coro_state_destroy (struct coro *coro)
781{ 799{
782 if (coro->refcnt--) 800 if (coro->refcnt--)
783 return; 801 return;
784 802
785 if (coro->mainstack && coro->mainstack != main_mainstack) 803 if (coro->mainstack && coro->mainstack != main_mainstack)
786 { 804 {
787 struct coro temp; 805 struct coro temp;
806 Zero (&temp, 1, struct coro);
807 temp.save = CORO_SAVE_ALL;
788 808
789 if (coro->flags & CF_RUNNING) 809 if (coro->flags & CF_RUNNING)
790 croak ("FATAL: tried to destroy currently running coroutine"); 810 croak ("FATAL: tried to destroy currently running coroutine");
791 811
792 SAVE ((&temp), TRANSFER_SAVE_ALL); 812 save_perl (&temp);
793 LOAD (coro); 813 load_perl (coro);
794 814
795 coro_destroy_stacks (); 815 coro_destroy_stacks ();
796 816
797 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 817 load_perl (&temp); /* this will get rid of defsv etc.. */
798 818
799 coro->mainstack = 0; 819 coro->mainstack = 0;
800 } 820 }
801 821
802 cctx_destroy (coro->cctx); 822 cctx_destroy (coro->cctx);
857 assert (mg->mg_type == PERL_MAGIC_ext); 877 assert (mg->mg_type == PERL_MAGIC_ext);
858 return (struct coro *)mg->mg_ptr; 878 return (struct coro *)mg->mg_ptr;
859} 879}
860 880
861static void 881static void
862prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) 882prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv)
863{ 883{
864 ta->prev = SvSTATE (prev); 884 ta->prev = SvSTATE (prev_sv);
865 ta->next = SvSTATE (next); 885 ta->next = SvSTATE (next_sv);
866 ta->flags = flags;
867} 886}
868 887
869static void 888static void
870api_transfer (SV *prev, SV *next, int flags) 889api_transfer (SV *prev_sv, SV *next_sv)
871{ 890{
872 struct transfer_args ta; 891 struct transfer_args ta;
873 892
874 prepare_transfer (&ta, prev, next, flags); 893 prepare_transfer (&ta, prev_sv, next_sv);
875 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;
876} 907}
877 908
878/** Coro ********************************************************************/ 909/** Coro ********************************************************************/
879 910
880#define PRIO_MAX 3 911#define PRIO_MAX 3
989 assert (!SvROK(prev));//D 1020 assert (!SvROK(prev));//D
990 assert (!SvROK(next));//D 1021 assert (!SvROK(next));//D
991 1022
992 ta->prev = SvSTATE (prev); 1023 ta->prev = SvSTATE (prev);
993 ta->next = SvSTATE (next); 1024 ta->next = SvSTATE (next);
994 ta->flags = TRANSFER_SAVE_ALL;
995 1025
996 assert (ta->flags & CF_READY); 1026 assert (ta->next->flags & CF_READY);
997 ta->next->flags &= ~CF_READY; 1027 ta->next->flags &= ~CF_READY;
998} 1028}
999 1029
1000static void 1030static void
1001prepare_cede (struct transfer_args *ta) 1031prepare_cede (struct transfer_args *ta)
1034#endif 1064#endif
1035 BOOT_PAGESIZE; 1065 BOOT_PAGESIZE;
1036 1066
1037 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1067 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1038 1068
1039 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 1069 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1040 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 1070 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1041 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 1071 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1072 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1073 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1042 1074
1043 main_mainstack = PL_mainstack; 1075 main_mainstack = PL_mainstack;
1044 1076
1045 coroapi.ver = CORO_API_VERSION; 1077 coroapi.ver = CORO_API_VERSION;
1046 coroapi.transfer = api_transfer; 1078 coroapi.transfer = api_transfer;
1056 HV *hv; 1088 HV *hv;
1057 int i; 1089 int i;
1058 1090
1059 Newz (0, coro, 1, struct coro); 1091 Newz (0, coro, 1, struct coro);
1060 coro->args = newAV (); 1092 coro->args = newAV ();
1093 coro->save = CORO_SAVE_ALL;
1061 coro->flags = CF_NEW; 1094 coro->flags = CF_NEW;
1062 1095
1063 hv = newHV (); 1096 hv = newHV ();
1064 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1097 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1065 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1098 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1066 1099
1067 for (i = 1; i < items; i++) 1100 for (i = 1; i < items; i++)
1068 av_push (coro->args, newSVsv (ST (i))); 1101 av_push (coro->args, newSVsv (ST (i)));
1069} 1102}
1070 OUTPUT: 1103 OUTPUT:
1104 RETVAL
1105
1106int
1107save (SV *coro, int new_save = -1)
1108 CODE:
1109 RETVAL = api_save (coro, new_save);
1110 OUTPUT:
1071 RETVAL 1111 RETVAL
1072 1112
1073void 1113void
1074_set_stacklevel (...) 1114_set_stacklevel (...)
1075 ALIAS: 1115 ALIAS:
1076 Coro::State::transfer = 1 1116 Coro::State::transfer = 1
1077 Coro::schedule = 2 1117 Coro::schedule = 2
1078 Coro::cede = 3 1118 Coro::cede = 3
1079 Coro::Cont::yield = 4
1080 CODE: 1119 CODE:
1081{ 1120{
1082 struct transfer_args ta; 1121 struct transfer_args ta;
1083 1122
1084 switch (ix) 1123 switch (ix)
1085 { 1124 {
1086 case 0: 1125 case 0:
1087 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); 1126 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1088 ta.next = 0; 1127 ta.next = 0;
1089 ta.flags = TRANSFER_SET_STACKLEVEL;
1090 break; 1128 break;
1091 1129
1092 case 1: 1130 case 1:
1093 if (items != 3) 1131 if (items != 2)
1094 croak ("Coro::State::transfer (prev,next,flags) expects three arguments, not %d", items); 1132 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1095 1133
1096 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); 1134 prepare_transfer (&ta, ST (0), ST (1));
1097 break; 1135 break;
1098 1136
1099 case 2: 1137 case 2:
1100 prepare_schedule (&ta); 1138 prepare_schedule (&ta);
1101 break; 1139 break;
1102 1140
1103 case 3: 1141 case 3:
1104 prepare_cede (&ta); 1142 prepare_cede (&ta);
1105 break; 1143 break;
1106
1107 case 4:
1108 {
1109 SV *yieldstack;
1110 SV *sv;
1111 AV *defav = GvAV (PL_defgv);
1112
1113 yieldstack = *hv_fetch (
1114 (HV *)SvRV (coro_current),
1115 "yieldstack", sizeof ("yieldstack") - 1,
1116 0
1117 );
1118
1119 /* set up @_ -- ugly */
1120 av_clear (defav);
1121 av_fill (defav, items - 1);
1122 while (items--)
1123 av_store (defav, items, SvREFCNT_inc (ST(items)));
1124
1125 sv = av_pop ((AV *)SvRV (yieldstack));
1126 ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1127 ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1128 ta.flags = 0;
1129 SvREFCNT_dec (sv);
1130 }
1131 break;
1132
1133 } 1144 }
1134 1145
1135 TRANSFER (ta); 1146 TRANSFER (ta);
1136} 1147}
1137 1148
1191 1202
1192 { 1203 {
1193 SV *sv = perl_get_sv("Coro::API", 1); 1204 SV *sv = perl_get_sv("Coro::API", 1);
1194 1205
1195 coroapi.schedule = api_schedule; 1206 coroapi.schedule = api_schedule;
1207 coroapi.save = api_save;
1196 coroapi.cede = api_cede; 1208 coroapi.cede = api_cede;
1197 coroapi.ready = api_ready; 1209 coroapi.ready = api_ready;
1198 coroapi.is_ready = api_is_ready; 1210 coroapi.is_ready = api_is_ready;
1199 coroapi.nready = &coro_nready; 1211 coroapi.nready = &coro_nready;
1200 coroapi.current = coro_current; 1212 coroapi.current = coro_current;
1202 GCoroAPI = &coroapi; 1214 GCoroAPI = &coroapi;
1203 sv_setiv (sv, (IV)&coroapi); 1215 sv_setiv (sv, (IV)&coroapi);
1204 SvREADONLY_on (sv); 1216 SvREADONLY_on (sv);
1205 } 1217 }
1206} 1218}
1219
1220void
1221_set_current (SV *current)
1222 PROTOTYPE: $
1223 CODE:
1224 SvREFCNT_dec (SvRV (coro_current));
1225 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1207 1226
1208int 1227int
1209prio (Coro::State coro, int newprio = 0) 1228prio (Coro::State coro, int newprio = 0)
1210 ALIAS: 1229 ALIAS:
1211 nice = 1 1230 nice = 1
1247 CODE: 1266 CODE:
1248 RETVAL = coro_nready; 1267 RETVAL = coro_nready;
1249 OUTPUT: 1268 OUTPUT:
1250 RETVAL 1269 RETVAL
1251 1270
1252void
1253_set_current (SV *current)
1254 PROTOTYPE: $
1255 CODE:
1256 SvREFCNT_dec (SvRV (coro_current));
1257 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1258
1259MODULE = Coro::State PACKAGE = Coro::AIO 1271MODULE = Coro::State PACKAGE = Coro::AIO
1260 1272
1261SV * 1273SV *
1262_get_state () 1274_get_state ()
1263 CODE: 1275 CODE:
1264{ 1276{
1265 struct { 1277 struct io_state *data;
1266 int errorno;
1267 int laststype;
1268 int laststatval;
1269 Stat_t statcache;
1270 } data;
1271 1278
1279 RETVAL = newSV (sizeof (struct io_state));
1280 data = (struct io_state *)SvPVX (RETVAL);
1281 SvCUR_set (RETVAL, sizeof (struct io_state));
1282 SvPOK_only (RETVAL);
1283
1272 data.errorno = errno; 1284 data->errorno = errno;
1273 data.laststype = PL_laststype; 1285 data->laststype = PL_laststype;
1274 data.laststatval = PL_laststatval; 1286 data->laststatval = PL_laststatval;
1275 data.statcache = PL_statcache; 1287 data->statcache = PL_statcache;
1276
1277 RETVAL = newSVpvn ((char *)&data, sizeof data);
1278} 1288}
1279 OUTPUT: 1289 OUTPUT:
1280 RETVAL 1290 RETVAL
1281 1291
1282void 1292void
1283_set_state (char *data_) 1293_set_state (char *data_)
1284 PROTOTYPE: $ 1294 PROTOTYPE: $
1285 CODE: 1295 CODE:
1286{ 1296{
1287 struct { 1297 struct io_state *data = (void *)data_;
1288 int errorno;
1289 int laststype;
1290 int laststatval;
1291 Stat_t statcache;
1292 } *data = (void *)data_;
1293 1298
1294 errno = data->errorno; 1299 errno = data->errorno;
1295 PL_laststype = data->laststype; 1300 PL_laststype = data->laststype;
1296 PL_laststatval = data->laststatval; 1301 PL_laststatval = data->laststatval;
1297 PL_statcache = data->statcache; 1302 PL_statcache = data->statcache;
1298} 1303}
1304

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines