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.114 by root, Thu Nov 30 21:22:45 2006 UTC vs.
Revision 1.125 by root, Mon Dec 11 20:54:45 2006 UTC

7#include "patchlevel.h" 7#include "patchlevel.h"
8 8
9#if USE_VALGRIND 9#if USE_VALGRIND
10# include <valgrind/valgrind.h> 10# include <valgrind/valgrind.h>
11#endif 11#endif
12
13/* the maximum number of idle cctx that will be pooled */
14#define MAX_IDLE_CCTX 8
12 15
13#define PERL_VERSION_ATLEAST(a,b,c) \ 16#define PERL_VERSION_ATLEAST(a,b,c) \
14 (PERL_REVISION > (a) \ 17 (PERL_REVISION > (a) \
15 || (PERL_REVISION == (a) \ 18 || (PERL_REVISION == (a) \
16 && (PERL_VERSION > (b) \ 19 && (PERL_VERSION > (b) \
35# ifndef IS_PADCONST 38# ifndef IS_PADCONST
36# define IS_PADCONST(v) 0 39# define IS_PADCONST(v) 0
37# endif 40# endif
38#endif 41#endif
39 42
43/* 5.8.7 */
44#ifndef SvRV_set
45# define SvRV_set(s,v) SvRV(s) = (v)
46#endif
47
40#include <stdio.h> 48#include <stdio.h>
41#include <errno.h> 49#include <errno.h>
50#include <assert.h>
42 51
43#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 52#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
44# undef STACKGUARD 53# undef STACKGUARD
45#endif 54#endif
46 55
87 96
88#define NOINLINE attribute ((noinline)) 97#define NOINLINE attribute ((noinline))
89 98
90#include "CoroAPI.h" 99#include "CoroAPI.h"
91 100
92#define TRANSFER_SET_STACKLEVEL 0x8bfbfbfb /* magic cookie */
93
94#ifdef USE_ITHREADS 101#ifdef USE_ITHREADS
95static perl_mutex coro_mutex; 102static perl_mutex coro_mutex;
96# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0) 103# define LOCK do { MUTEX_LOCK (&coro_mutex); } while (0)
97# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0) 104# define UNLOCK do { MUTEX_UNLOCK (&coro_mutex); } while (0)
98#else 105#else
99# define LOCK (void)0 106# define LOCK (void)0
100# define UNLOCK (void)0 107# define UNLOCK (void)0
101#endif 108#endif
102 109
110struct io_state
111{
112 int errorno;
113 I32 laststype;
114 int laststatval;
115 Stat_t statcache;
116};
117
103static struct CoroAPI coroapi; 118static struct CoroAPI coroapi;
104static AV *main_mainstack; /* used to differentiate between $main and others */ 119static AV *main_mainstack; /* used to differentiate between $main and others */
105static HV *coro_state_stash, *coro_stash; 120static HV *coro_state_stash, *coro_stash;
106static SV *coro_mortal; /* will be freed after next transfer */ 121static SV *coro_mortal; /* will be freed after next transfer */
107 122
127 int valgrind_id; 142 int valgrind_id;
128#endif 143#endif
129} coro_cctx; 144} coro_cctx;
130 145
131enum { 146enum {
132 CF_RUNNING, /* coroutine is running */ 147 CF_RUNNING = 0x0001, /* coroutine is running */
133 CF_READY, /* coroutine is ready */ 148 CF_READY = 0x0002, /* coroutine is ready */
149 CF_NEW = 0x0004, /* ahs never been switched to */
134}; 150};
135 151
136/* this is a structure representing a perl-level coroutine */ 152/* this is a structure representing a perl-level coroutine */
137struct coro { 153struct coro {
138 /* the c coroutine allocated to this perl coroutine, if any */ 154 /* the c coroutine allocated to this perl coroutine, if any */
139 coro_cctx *cctx; 155 coro_cctx *cctx;
140 156
141 /* data associated with this coroutine (initial args) */ 157 /* data associated with this coroutine (initial args) */
142 AV *args; 158 AV *args;
143 int refcnt; 159 int refcnt;
144 int flags; 160 int save; /* CORO_SAVE flags */
161 int flags; /* CF_ flags */
145 162
146 /* optionally saved, might be zero */ 163 /* optionally saved, might be zero */
147 AV *defav; 164 AV *defav; /* @_ */
148 SV *defsv; 165 SV *defsv; /* $_ */
149 SV *errsv; 166 SV *errsv; /* $@ */
167 SV *irssv; /* $/ */
168 SV *irssv_sv; /* real $/ cache */
150 169
151#define VAR(name,type) type name; 170#define VAR(name,type) type name;
152# include "state.h" 171# include "state.h"
153#undef VAR 172#undef VAR
154 173
277} 296}
278 297
279#define SB do { 298#define SB do {
280#define SE } while (0) 299#define SE } while (0)
281 300
282#define LOAD(state) load_state((state));
283#define SAVE(state,flags) save_state((state),(flags));
284
285#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 301#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
286 302
287static void 303static void
288load_state(Coro__State c) 304load_perl (Coro__State c)
289{ 305{
290#define VAR(name,type) PL_ ## name = c->name; 306#define VAR(name,type) PL_ ## name = c->name;
291# include "state.h" 307# include "state.h"
292#undef VAR 308#undef VAR
293 309
294 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav); 310 if (c->defav) REPLACE_SV (GvAV (PL_defgv), c->defav);
295 if (c->defsv) REPLACE_SV (DEFSV , c->defsv); 311 if (c->defsv) REPLACE_SV (DEFSV , c->defsv);
296 if (c->errsv) REPLACE_SV (ERRSV , c->errsv); 312 if (c->errsv) REPLACE_SV (ERRSV , c->errsv);
313 if (c->irssv)
314 {
315 if (c->irssv == PL_rs || sv_eq (PL_rs, c->irssv))
316 SvREFCNT_dec (c->irssv);
317 else
318 {
319 REPLACE_SV (PL_rs, c->irssv);
320 if (!c->irssv_sv) c->irssv_sv = get_sv ("/", 0);
321 sv_setsv (c->irssv_sv, PL_rs);
322 }
323 }
297 324
298 { 325 {
299 dSP; 326 dSP;
300 CV *cv; 327 CV *cv;
301 328
310 PUTBACK; 337 PUTBACK;
311 } 338 }
312} 339}
313 340
314static void 341static void
315save_state(Coro__State c, int flags) 342save_perl (Coro__State c)
316{ 343{
317 { 344 {
318 dSP; 345 dSP;
319 I32 cxix = cxstack_ix; 346 I32 cxix = cxstack_ix;
320 PERL_CONTEXT *ccstk = cxstack; 347 PERL_CONTEXT *ccstk = cxstack;
331 { 358 {
332 while (cxix >= 0) 359 while (cxix >= 0)
333 { 360 {
334 PERL_CONTEXT *cx = &ccstk[cxix--]; 361 PERL_CONTEXT *cx = &ccstk[cxix--];
335 362
336 if (CxTYPE(cx) == CXt_SUB) 363 if (CxTYPE (cx) == CXt_SUB)
337 { 364 {
338 CV *cv = cx->blk_sub.cv; 365 CV *cv = cx->blk_sub.cv;
339 366
340 if (CvDEPTH (cv)) 367 if (CvDEPTH (cv))
341 { 368 {
342 EXTEND (SP, 3); 369 EXTEND (SP, 3);
343 370
344 PUSHs ((SV *)CvPADLIST(cv)); 371 PUSHs ((SV *)CvPADLIST (cv));
345 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 372 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
346 PUSHs ((SV *)cv); 373 PUSHs ((SV *)cv);
347 374
348 CvDEPTH (cv) = 0; 375 CvDEPTH (cv) = 0;
349 get_padlist (cv); 376 get_padlist (cv);
350 } 377 }
351 } 378 }
352#ifdef CXt_FORMAT 379#ifdef CXt_FORMAT
353 else if (CxTYPE(cx) == CXt_FORMAT) 380 else if (CxTYPE (cx) == CXt_FORMAT)
354 { 381 {
355 /* I never used formats, so how should I know how these are implemented? */ 382 /* I never used formats, so how should I know how these are implemented? */
356 /* my bold guess is as a simple, plain sub... */ 383 /* my bold guess is as a simple, plain sub... */
357 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); 384 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
358 } 385 }
368 } 395 }
369 396
370 PUTBACK; 397 PUTBACK;
371 } 398 }
372 399
373 c->defav = flags & TRANSFER_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0; 400 c->defav = c->save & CORO_SAVE_DEFAV ? (AV *)SvREFCNT_inc (GvAV (PL_defgv)) : 0;
374 c->defsv = flags & TRANSFER_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0; 401 c->defsv = c->save & CORO_SAVE_DEFSV ? SvREFCNT_inc (DEFSV) : 0;
375 c->errsv = flags & TRANSFER_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0; 402 c->errsv = c->save & CORO_SAVE_ERRSV ? SvREFCNT_inc (ERRSV) : 0;
403 c->irssv = c->save & CORO_SAVE_IRSSV ? SvREFCNT_inc (PL_rs) : 0;
376 404
377#define VAR(name,type)c->name = PL_ ## name; 405#define VAR(name,type)c->name = PL_ ## name;
378# include "state.h" 406# include "state.h"
379#undef VAR 407#undef VAR
380} 408}
386 * not usually need a lot of stackspace. 414 * not usually need a lot of stackspace.
387 */ 415 */
388static void 416static void
389coro_init_stacks () 417coro_init_stacks ()
390{ 418{
391 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 419 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
392 PL_curstackinfo->si_type = PERLSI_MAIN; 420 PL_curstackinfo->si_type = PERLSI_MAIN;
393 PL_curstack = PL_curstackinfo->si_stack; 421 PL_curstack = PL_curstackinfo->si_stack;
394 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 422 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
395 423
396 PL_stack_base = AvARRAY(PL_curstack); 424 PL_stack_base = AvARRAY(PL_curstack);
397 PL_stack_sp = PL_stack_base; 425 PL_stack_sp = PL_stack_base;
398 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 426 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
399 427
400 New(50,PL_tmps_stack,96,SV*); 428 New(50,PL_tmps_stack,128,SV*);
401 PL_tmps_floor = -1; 429 PL_tmps_floor = -1;
402 PL_tmps_ix = -1; 430 PL_tmps_ix = -1;
403 PL_tmps_max = 96; 431 PL_tmps_max = 128;
404 432
405 New(54,PL_markstack,16,I32); 433 New(54,PL_markstack,32,I32);
406 PL_markstack_ptr = PL_markstack; 434 PL_markstack_ptr = PL_markstack;
407 PL_markstack_max = PL_markstack + 16; 435 PL_markstack_max = PL_markstack + 32;
408 436
409#ifdef SET_MARK_OFFSET 437#ifdef SET_MARK_OFFSET
410 SET_MARK_OFFSET; 438 SET_MARK_OFFSET;
411#endif 439#endif
412 440
413 New(54,PL_scopestack,16,I32); 441 New(54,PL_scopestack,32,I32);
414 PL_scopestack_ix = 0; 442 PL_scopestack_ix = 0;
415 PL_scopestack_max = 16; 443 PL_scopestack_max = 32;
416 444
417 New(54,PL_savestack,96,ANY); 445 New(54,PL_savestack,64,ANY);
418 PL_savestack_ix = 0; 446 PL_savestack_ix = 0;
419 PL_savestack_max = 96; 447 PL_savestack_max = 64;
420 448
421#if !PERL_VERSION_ATLEAST (5,9,0) 449#if !PERL_VERSION_ATLEAST (5,9,0)
422 New(54,PL_retstack,8,OP*); 450 New(54,PL_retstack,16,OP*);
423 PL_retstack_ix = 0; 451 PL_retstack_ix = 0;
424 PL_retstack_max = 8; 452 PL_retstack_max = 16;
425#endif 453#endif
426} 454}
427 455
428/* 456/*
429 * destroy the stacks, the callchain etc... 457 * destroy the stacks, the callchain etc...
482 * emulate part of the perl startup here. 510 * emulate part of the perl startup here.
483 */ 511 */
484 512
485 coro_init_stacks (); 513 coro_init_stacks ();
486 514
487 PL_curcop = 0; 515 PL_curcop = &PL_compiling;
488 PL_in_eval = 0; 516 PL_in_eval = EVAL_NULL;
489 PL_curpm = 0; 517 PL_curpm = 0;
518 PL_localizing = 0;
519 PL_dirty = 0;
520 PL_restartop = 0;
490 521
491 { 522 {
492 dSP; 523 dSP;
493 LOGOP myop; 524 LOGOP myop;
494 525
495 /* I have no idea why this is needed, but it is */
496 PUSHMARK (SP);
497
498 SvREFCNT_dec (GvAV (PL_defgv)); 526 SvREFCNT_dec (GvAV (PL_defgv));
499 GvAV (PL_defgv) = coro->args; coro->args = 0; 527 GvAV (PL_defgv) = coro->args; coro->args = 0;
500 528
501 Zero (&myop, 1, LOGOP); 529 Zero (&myop, 1, LOGOP);
502 myop.op_next = Nullop; 530 myop.op_next = Nullop;
503 myop.op_flags = OPf_WANT_VOID; 531 myop.op_flags = OPf_WANT_VOID;
504 532
505 PL_op = (OP *)&myop;
506
507 PUSHMARK (SP); 533 PUSHMARK (SP);
508 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 534 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
509 PUTBACK; 535 PUTBACK;
536 PL_op = (OP *)&myop;
510 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 537 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
511 SPAGAIN; 538 SPAGAIN;
512
513 ENTER; /* necessary e.g. for dounwind */
514 } 539 }
540
541 ENTER; /* necessary e.g. for dounwind */
515} 542}
516 543
517static void 544static void
518free_coro_mortal () 545free_coro_mortal ()
519{ 546{
522 SvREFCNT_dec (coro_mortal); 549 SvREFCNT_dec (coro_mortal);
523 coro_mortal = 0; 550 coro_mortal = 0;
524 } 551 }
525} 552}
526 553
554/* inject a fake call to Coro::State::_cctx_init into the execution */
527static void NOINLINE 555static void NOINLINE
528prepare_cctx (coro_cctx *cctx) 556prepare_cctx (coro_cctx *cctx)
529{ 557{
530 dSP; 558 dSP;
531 LOGOP myop; 559 LOGOP myop;
532 560
533 Zero (&myop, 1, LOGOP); 561 Zero (&myop, 1, LOGOP);
534 myop.op_next = PL_op; 562 myop.op_next = PL_op;
535 myop.op_flags = OPf_WANT_VOID; 563 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
536
537 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
538 564
539 PUSHMARK (SP); 565 PUSHMARK (SP);
566 EXTEND (SP, 2);
567 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx))));
540 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 568 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
541 PUTBACK; 569 PUTBACK;
570 PL_op = (OP *)&myop;
542 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 571 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
543 SPAGAIN; 572 SPAGAIN;
544} 573}
545 574
546static void 575static void
547coro_run (void *arg) 576coro_run (void *arg)
548{ 577{
549 /* coro_run is the alternative epilogue of transfer() */ 578 /* coro_run is the alternative tail of transfer(), so unlock here. */
550 UNLOCK; 579 UNLOCK;
551 580
552 /* 581 /*
553 * this is a _very_ stripped down perl interpreter ;) 582 * this is a _very_ stripped down perl interpreter ;)
554 */ 583 */
556 585
557 /* inject call to cctx_init */ 586 /* inject call to cctx_init */
558 prepare_cctx ((coro_cctx *)arg); 587 prepare_cctx ((coro_cctx *)arg);
559 588
560 /* somebody will hit me for both perl_run and PL_restartop */ 589 /* somebody will hit me for both perl_run and PL_restartop */
590 PL_restartop = PL_op;
561 perl_run (PL_curinterp); 591 perl_run (PL_curinterp);
562 592
563 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); 593 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
564 abort (); 594 abort ();
565} 595}
569{ 599{
570 coro_cctx *cctx; 600 coro_cctx *cctx;
571 601
572 ++cctx_count; 602 ++cctx_count;
573 603
574 New (0, cctx, 1, coro_cctx); 604 Newz (0, cctx, 1, coro_cctx);
575 605
576#if HAVE_MMAP 606#if HAVE_MMAP
577 607
578 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 608 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
579 /* mmap suppsedly does allocate-on-write for us */ 609 /* mmap supposedly does allocate-on-write for us */
580 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 610 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
581 611
582 if (cctx->sptr == (void *)-1) 612 if (cctx->sptr == (void *)-1)
583 { 613 {
584 perror ("FATAL: unable to mmap stack for coroutine"); 614 perror ("FATAL: unable to mmap stack for coroutine");
613 643
614 return cctx; 644 return cctx;
615} 645}
616 646
617static void 647static void
618cctx_free (coro_cctx *cctx) 648cctx_destroy (coro_cctx *cctx)
619{ 649{
620 if (!cctx) 650 if (!cctx)
621 return; 651 return;
622 652
623 --cctx_count; 653 --cctx_count;
640{ 670{
641 coro_cctx *cctx; 671 coro_cctx *cctx;
642 672
643 if (cctx_first) 673 if (cctx_first)
644 { 674 {
645 --cctx_idle;
646 cctx = cctx_first; 675 cctx = cctx_first;
647 cctx_first = cctx->next; 676 cctx_first = cctx->next;
677 --cctx_idle;
648 } 678 }
649 else 679 else
650 { 680 {
651 cctx = cctx_new (); 681 cctx = cctx_new ();
652 PL_op = PL_op->op_next; 682 PL_op = PL_op->op_next;
656} 686}
657 687
658static void 688static void
659cctx_put (coro_cctx *cctx) 689cctx_put (coro_cctx *cctx)
660{ 690{
691 /* free another cctx if overlimit */
692 if (cctx_idle >= MAX_IDLE_CCTX)
693 {
694 coro_cctx *first = cctx_first;
695 cctx_first = first->next;
696 --cctx_idle;
697
698 assert (!first->inuse);
699 cctx_destroy (first);
700 }
701
661 ++cctx_idle; 702 ++cctx_idle;
662 cctx->next = cctx_first; 703 cctx->next = cctx_first;
663 cctx_first = cctx; 704 cctx_first = cctx;
664} 705}
665 706
666/* never call directly, always through the coro_state_transfer global variable */ 707/* never call directly, always through the coro_state_transfer global variable */
667static void NOINLINE 708static void NOINLINE
668transfer (struct coro *prev, struct coro *next, int flags) 709transfer (struct coro *prev, struct coro *next)
669{ 710{
670 dSTACKLEVEL; 711 dSTACKLEVEL;
671 712
672 /* sometimes transfer is only called to set idle_sp */ 713 /* sometimes transfer is only called to set idle_sp */
673 if (flags == TRANSFER_SET_STACKLEVEL) 714 if (!next)
715 {
674 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 716 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
717 assert (((coro_cctx *)prev)->top_env = PL_top_env); /* just for the side effetc when assert is enabled */
718 }
675 else if (prev != next) 719 else if (prev != next)
676 { 720 {
677 coro_cctx *prev__cctx; 721 coro_cctx *prev__cctx;
678 722
679 if (!prev->cctx) 723 if (prev->flags & CF_NEW)
680 { 724 {
681 /* create a new empty context */ 725 /* create a new empty context */
682 Newz (0, prev->cctx, 1, coro_cctx); 726 Newz (0, prev->cctx, 1, coro_cctx);
683 prev->cctx->inuse = 1; 727 prev->cctx->inuse = 1;
728 prev->flags &= ~CF_NEW;
684 prev->flags |= CF_RUNNING; 729 prev->flags |= CF_RUNNING;
685 } 730 }
686 731
732 /*TODO: must not croak here */
687 if (!prev->flags & CF_RUNNING) 733 if (!prev->flags & CF_RUNNING)
688 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); 734 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
689 735
690 if (next->flags & CF_RUNNING) 736 if (next->flags & CF_RUNNING)
691 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 737 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
693 prev->flags &= ~CF_RUNNING; 739 prev->flags &= ~CF_RUNNING;
694 next->flags |= CF_RUNNING; 740 next->flags |= CF_RUNNING;
695 741
696 LOCK; 742 LOCK;
697 743
698 if (next->mainstack) 744 if (next->flags & CF_NEW)
699 { 745 {
700 /* coroutine already started */ 746 /* need to start coroutine */
701 SAVE (prev, flags); 747 next->flags &= ~CF_NEW;
702 LOAD (next); 748 /* first get rid of the old state */
749 save_perl (prev);
750 /* setup coroutine call */
751 setup_coro (next);
752 /* need a new stack */
753 assert (!next->cctx);
703 } 754 }
704 else 755 else
705 { 756 {
706 /* need to start coroutine */ 757 /* coroutine already started */
707 /* first get rid of the old state */ 758 save_perl (prev);
708 SAVE (prev, -1); 759 load_perl (next);
709 /* setup coroutine call */
710 setup_coro (next);
711 /* need a new stack */
712 assert (!next->stack);
713 } 760 }
714 761
715 prev__cctx = prev->cctx; 762 prev__cctx = prev->cctx;
716 763
717 /* possibly "free" the cctx */ 764 /* possibly "free" the cctx */
718 if (prev__cctx->idle_sp == STACKLEVEL) 765 if (prev__cctx->idle_sp == STACKLEVEL)
719 { 766 {
720 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 767 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
721 assert (PL_top_env == prev__cctx->top_env); 768 assert (PL_top_env == prev__cctx->top_env);
722 769
770 prev->cctx = 0;
771
723 cctx_put (prev__cctx); 772 cctx_put (prev__cctx);
724 prev->cctx = 0; 773 prev__cctx->inuse = 0;
725 } 774 }
726 775
727 if (!next->cctx) 776 if (!next->cctx)
777 {
728 next->cctx = cctx_get (); 778 next->cctx = cctx_get ();
779 assert (!next->cctx->inuse);
780 next->cctx->inuse = 1;
781 }
729 782
730 if (prev__cctx != next->cctx) 783 if (prev__cctx != next->cctx)
731 { 784 {
732 assert ( prev__cctx->inuse);
733 assert (!next->cctx->inuse);
734
735 prev__cctx->inuse = 0;
736 next->cctx->inuse = 1;
737
738 prev__cctx->top_env = PL_top_env; 785 prev__cctx->top_env = PL_top_env;
739 PL_top_env = next->cctx->top_env; 786 PL_top_env = next->cctx->top_env;
740 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 787 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
741 } 788 }
742 789
747} 794}
748 795
749struct transfer_args 796struct transfer_args
750{ 797{
751 struct coro *prev, *next; 798 struct coro *prev, *next;
752 int flags;
753}; 799};
754 800
755#define TRANSFER(ta) transfer ((ta).prev, (ta).next, (ta).flags) 801#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
756 802
757static void 803static void
758coro_state_destroy (struct coro *coro) 804coro_state_destroy (struct coro *coro)
759{ 805{
760 if (coro->refcnt--) 806 if (coro->refcnt--)
761 return; 807 return;
762 808
763 if (coro->flags & CF_RUNNING)
764 croak ("FATAL: tried to destroy currently running coroutine");
765
766 if (coro->mainstack && coro->mainstack != main_mainstack) 809 if (coro->mainstack && coro->mainstack != main_mainstack)
767 { 810 {
768 struct coro temp; 811 struct coro temp;
812 Zero (&temp, 1, struct coro);
813 temp.save = CORO_SAVE_ALL;
769 814
770 SAVE ((&temp), TRANSFER_SAVE_ALL); 815 if (coro->flags & CF_RUNNING)
771 LOAD (coro); 816 croak ("FATAL: tried to destroy currently running coroutine");
817
818 save_perl (&temp);
819 load_perl (coro);
772 820
773 coro_destroy_stacks (); 821 coro_destroy_stacks ();
774 822
775 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 823 load_perl (&temp); /* this will get rid of defsv etc.. */
776 824
777 coro->mainstack = 0; 825 coro->mainstack = 0;
778 } 826 }
779 827
780 cctx_free (coro->cctx); 828 cctx_destroy (coro->cctx);
781 SvREFCNT_dec (coro->args); 829 SvREFCNT_dec (coro->args);
782 Safefree (coro); 830 Safefree (coro);
783} 831}
784 832
785static int 833static int
835 assert (mg->mg_type == PERL_MAGIC_ext); 883 assert (mg->mg_type == PERL_MAGIC_ext);
836 return (struct coro *)mg->mg_ptr; 884 return (struct coro *)mg->mg_ptr;
837} 885}
838 886
839static void 887static void
840prepare_transfer (struct transfer_args *ta, SV *prev, SV *next, int flags) 888prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv)
841{ 889{
842 ta->prev = SvSTATE (prev); 890 ta->prev = SvSTATE (prev_sv);
843 ta->next = SvSTATE (next); 891 ta->next = SvSTATE (next_sv);
844 ta->flags = flags;
845} 892}
846 893
847static void 894static void
848api_transfer (SV *prev, SV *next, int flags) 895api_transfer (SV *prev_sv, SV *next_sv)
849{ 896{
850 dTHX;
851 struct transfer_args ta; 897 struct transfer_args ta;
852 898
853 prepare_transfer (&ta, prev, next, flags); 899 prepare_transfer (&ta, prev_sv, next_sv);
854 TRANSFER (ta); 900 TRANSFER (ta);
901}
902
903static int
904api_save (SV *coro_sv, int new_save)
905{
906 struct coro *coro = SvSTATE (coro_sv);
907 int old_save = coro->save;
908
909 if (new_save >= 0)
910 coro->save = new_save;
911
912 return old_save;
855} 913}
856 914
857/** Coro ********************************************************************/ 915/** Coro ********************************************************************/
858 916
859#define PRIO_MAX 3 917#define PRIO_MAX 3
862#define PRIO_LOW -1 920#define PRIO_LOW -1
863#define PRIO_IDLE -3 921#define PRIO_IDLE -3
864#define PRIO_MIN -4 922#define PRIO_MIN -4
865 923
866/* for Coro.pm */ 924/* for Coro.pm */
867static GV *coro_current, *coro_idle; 925static SV *coro_current;
868static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 926static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
869static int coro_nready; 927static int coro_nready;
870 928
871static void 929static void
872coro_enq (SV *coro_sv) 930coro_enq (SV *coro_sv)
905 coro = SvSTATE (coro_sv); 963 coro = SvSTATE (coro_sv);
906 964
907 if (coro->flags & CF_READY) 965 if (coro->flags & CF_READY)
908 return 0; 966 return 0;
909 967
968#if 0 /* this is actually harmless */
910 if (coro->flags & CF_RUNNING) 969 if (coro->flags & CF_RUNNING)
911 croak ("Coro::ready called on currently running coroutine"); 970 croak ("Coro::ready called on currently running coroutine");
971#endif
912 972
913 coro->flags |= CF_READY; 973 coro->flags |= CF_READY;
914 974
915 LOCK; 975 LOCK;
916 coro_enq (SvREFCNT_inc (coro_sv)); 976 coro_enq (SvREFCNT_inc (coro_sv));
926} 986}
927 987
928static void 988static void
929prepare_schedule (struct transfer_args *ta) 989prepare_schedule (struct transfer_args *ta)
930{ 990{
931 SV *current, *prev, *next; 991 SV *prev, *next;
932
933 current = GvSV (coro_current);
934 992
935 for (;;) 993 for (;;)
936 { 994 {
937 LOCK; 995 LOCK;
938 next = coro_deq (PRIO_MIN); 996 next = coro_deq (PRIO_MIN);
947 ENTER; 1005 ENTER;
948 SAVETMPS; 1006 SAVETMPS;
949 1007
950 PUSHMARK (SP); 1008 PUSHMARK (SP);
951 PUTBACK; 1009 PUTBACK;
952 call_sv (GvSV (coro_idle), G_DISCARD); 1010 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
953 1011
954 FREETMPS; 1012 FREETMPS;
955 LEAVE; 1013 LEAVE;
956 } 1014 }
957 } 1015 }
958 1016
959 prev = SvRV (current); 1017 prev = SvRV (coro_current);
960 SvRV (current) = next; 1018 SvRV_set (coro_current, next);
961 1019
962 /* free this only after the transfer */ 1020 /* free this only after the transfer */
963 LOCK; 1021 LOCK;
964 free_coro_mortal (); 1022 free_coro_mortal ();
965 UNLOCK; 1023 UNLOCK;
966 coro_mortal = prev; 1024 coro_mortal = prev;
967 1025
1026 assert (!SvROK(prev));//D
1027 assert (!SvROK(next));//D
1028
968 ta->prev = SvSTATE (prev); 1029 ta->prev = SvSTATE (prev);
969 ta->next = SvSTATE (next); 1030 ta->next = SvSTATE (next);
970 ta->flags = TRANSFER_SAVE_ALL;
971 1031
1032 assert (ta->next->flags & CF_READY);
972 ta->next->flags &= ~CF_READY; 1033 ta->next->flags &= ~CF_READY;
973} 1034}
974 1035
975static void 1036static void
976prepare_cede (struct transfer_args *ta) 1037prepare_cede (struct transfer_args *ta)
977{ 1038{
978 api_ready (GvSV (coro_current)); 1039 api_ready (coro_current);
979 1040
980 prepare_schedule (ta); 1041 prepare_schedule (ta);
981} 1042}
982 1043
983static void 1044static void
984api_schedule (void) 1045api_schedule (void)
985{ 1046{
986 dTHX;
987 struct transfer_args ta; 1047 struct transfer_args ta;
988 1048
989 prepare_schedule (&ta); 1049 prepare_schedule (&ta);
990 TRANSFER (ta); 1050 TRANSFER (ta);
991} 1051}
992 1052
993static void 1053static void
994api_cede (void) 1054api_cede (void)
995{ 1055{
996 dTHX;
997 struct transfer_args ta; 1056 struct transfer_args ta;
998 1057
999 prepare_cede (&ta); 1058 prepare_cede (&ta);
1000 TRANSFER (ta); 1059 TRANSFER (ta);
1001} 1060}
1011#endif 1070#endif
1012 BOOT_PAGESIZE; 1071 BOOT_PAGESIZE;
1013 1072
1014 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1073 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1015 1074
1016 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (TRANSFER_SAVE_DEFAV)); 1075 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1017 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (TRANSFER_SAVE_DEFSV)); 1076 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1018 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (TRANSFER_SAVE_ERRSV)); 1077 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1078 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1079 newCONSTSUB (coro_state_stash, "SAVE_ALL", newSViv (CORO_SAVE_ALL));
1019 1080
1020 main_mainstack = PL_mainstack; 1081 main_mainstack = PL_mainstack;
1021 1082
1022 coroapi.ver = CORO_API_VERSION; 1083 coroapi.ver = CORO_API_VERSION;
1023 coroapi.transfer = api_transfer; 1084 coroapi.transfer = api_transfer;
1033 HV *hv; 1094 HV *hv;
1034 int i; 1095 int i;
1035 1096
1036 Newz (0, coro, 1, struct coro); 1097 Newz (0, coro, 1, struct coro);
1037 coro->args = newAV (); 1098 coro->args = newAV ();
1099 coro->save = CORO_SAVE_ALL;
1100 coro->flags = CF_NEW;
1038 1101
1039 hv = newHV (); 1102 hv = newHV ();
1040 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1103 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1041 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1104 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1042 1105
1043 for (i = 1; i < items; i++) 1106 for (i = 1; i < items; i++)
1044 av_push (coro->args, newSVsv (ST (i))); 1107 av_push (coro->args, newSVsv (ST (i)));
1045} 1108}
1046 OUTPUT: 1109 OUTPUT:
1110 RETVAL
1111
1112int
1113save (SV *coro, int new_save = -1)
1114 CODE:
1115 RETVAL = api_save (coro, new_save);
1116 OUTPUT:
1047 RETVAL 1117 RETVAL
1048 1118
1049void 1119void
1050_set_stacklevel (...) 1120_set_stacklevel (...)
1051 ALIAS: 1121 ALIAS:
1052 Coro::State::transfer = 1 1122 Coro::State::transfer = 1
1053 Coro::schedule = 2 1123 Coro::schedule = 2
1054 Coro::cede = 3 1124 Coro::cede = 3
1055 Coro::Cont::yield = 4
1056 CODE: 1125 CODE:
1057{ 1126{
1058 struct transfer_args ta; 1127 struct transfer_args ta;
1059 1128
1060 switch (ix) 1129 switch (ix)
1061 { 1130 {
1062 case 0: 1131 case 0:
1063 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0))); 1132 ta.prev = (struct coro *)INT2PTR (coro_cctx *, SvIV (ST (0)));
1064 ta.next = 0; 1133 ta.next = 0;
1065 ta.flags = TRANSFER_SET_STACKLEVEL;
1066 break; 1134 break;
1067 1135
1068 case 1: 1136 case 1:
1069 if (items != 3) 1137 if (items != 2)
1070 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); 1138 croak ("Coro::State::transfer (prev,next) expects two arguments, not %d", items);
1071 1139
1072 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); 1140 prepare_transfer (&ta, ST (0), ST (1));
1073 break; 1141 break;
1074 1142
1075 case 2: 1143 case 2:
1076 prepare_schedule (&ta); 1144 prepare_schedule (&ta);
1077 break; 1145 break;
1078 1146
1079 case 3: 1147 case 3:
1080 prepare_cede (&ta); 1148 prepare_cede (&ta);
1081 break; 1149 break;
1082
1083 case 4:
1084 {
1085 SV *yieldstack;
1086 SV *sv;
1087 AV *defav = GvAV (PL_defgv);
1088
1089 yieldstack = *hv_fetch (
1090 (HV *)SvRV (GvSV (coro_current)),
1091 "yieldstack", sizeof ("yieldstack") - 1,
1092 0
1093 );
1094
1095 /* set up @_ -- ugly */
1096 av_clear (defav);
1097 av_fill (defav, items - 1);
1098 while (items--)
1099 av_store (defav, items, SvREFCNT_inc (ST(items)));
1100
1101 sv = av_pop ((AV *)SvRV (yieldstack));
1102 ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1103 ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1104 ta.flags = 0;
1105 SvREFCNT_dec (sv);
1106 }
1107 break;
1108
1109 } 1150 }
1110 1151
1111 TRANSFER (ta); 1152 TRANSFER (ta);
1112} 1153}
1113 1154
1157 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1198 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1158 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1199 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1159 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1200 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1160 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1201 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1161 1202
1162 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); 1203 coro_current = get_sv ("Coro::current", FALSE);
1163 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); 1204 SvREADONLY_on (coro_current);
1164 1205
1165 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1206 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1166 coro_ready[i] = newAV (); 1207 coro_ready[i] = newAV ();
1167 1208
1168 { 1209 {
1169 SV *sv = perl_get_sv("Coro::API", 1); 1210 SV *sv = perl_get_sv("Coro::API", 1);
1170 1211
1171 coroapi.schedule = api_schedule; 1212 coroapi.schedule = api_schedule;
1213 coroapi.save = api_save;
1172 coroapi.cede = api_cede; 1214 coroapi.cede = api_cede;
1173 coroapi.ready = api_ready; 1215 coroapi.ready = api_ready;
1174 coroapi.is_ready = api_is_ready; 1216 coroapi.is_ready = api_is_ready;
1175 coroapi.nready = &coro_nready; 1217 coroapi.nready = &coro_nready;
1176 coroapi.current = coro_current; 1218 coroapi.current = coro_current;
1178 GCoroAPI = &coroapi; 1220 GCoroAPI = &coroapi;
1179 sv_setiv (sv, (IV)&coroapi); 1221 sv_setiv (sv, (IV)&coroapi);
1180 SvREADONLY_on (sv); 1222 SvREADONLY_on (sv);
1181 } 1223 }
1182} 1224}
1225
1226void
1227_set_current (SV *current)
1228 PROTOTYPE: $
1229 CODE:
1230 SvREFCNT_dec (SvRV (coro_current));
1231 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1183 1232
1184int 1233int
1185prio (Coro::State coro, int newprio = 0) 1234prio (Coro::State coro, int newprio = 0)
1186 ALIAS: 1235 ALIAS:
1187 nice = 1 1236 nice = 1
1229 1278
1230SV * 1279SV *
1231_get_state () 1280_get_state ()
1232 CODE: 1281 CODE:
1233{ 1282{
1234 struct { 1283 struct io_state *data;
1235 int errorno;
1236 int laststype;
1237 int laststatval;
1238 Stat_t statcache;
1239 } data;
1240 1284
1285 RETVAL = newSV (sizeof (struct io_state));
1286 data = (struct io_state *)SvPVX (RETVAL);
1287 SvCUR_set (RETVAL, sizeof (struct io_state));
1288 SvPOK_only (RETVAL);
1289
1241 data.errorno = errno; 1290 data->errorno = errno;
1242 data.laststype = PL_laststype; 1291 data->laststype = PL_laststype;
1243 data.laststatval = PL_laststatval; 1292 data->laststatval = PL_laststatval;
1244 data.statcache = PL_statcache; 1293 data->statcache = PL_statcache;
1245
1246 RETVAL = newSVpvn ((char *)&data, sizeof data);
1247} 1294}
1248 OUTPUT: 1295 OUTPUT:
1249 RETVAL 1296 RETVAL
1250 1297
1251void 1298void
1252_set_state (char *data_) 1299_set_state (char *data_)
1253 PROTOTYPE: $ 1300 PROTOTYPE: $
1254 CODE: 1301 CODE:
1255{ 1302{
1256 struct { 1303 struct io_state *data = (void *)data_;
1257 int errorno;
1258 int laststype;
1259 int laststatval;
1260 Stat_t statcache;
1261 } *data = (void *)data_;
1262 1304
1263 errno = data->errorno; 1305 errno = data->errorno;
1264 PL_laststype = data->laststype; 1306 PL_laststype = data->laststype;
1265 PL_laststatval = data->laststatval; 1307 PL_laststatval = data->laststatval;
1266 PL_statcache = data->statcache; 1308 PL_statcache = data->statcache;
1267} 1309}
1310

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines