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.111 by root, Thu Nov 30 18:21:14 2006 UTC vs.
Revision 1.121 by root, Sat Dec 2 20:40:25 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) \
98#else 101#else
99# define LOCK (void)0 102# define LOCK (void)0
100# define UNLOCK (void)0 103# define UNLOCK (void)0
101#endif 104#endif
102 105
106struct io_state
107{
108 int errorno;
109 I32 laststype;
110 int laststatval;
111 Stat_t statcache;
112};
113
103static struct CoroAPI coroapi; 114static struct CoroAPI coroapi;
104static AV *main_mainstack; /* used to differentiate between $main and others */ 115static AV *main_mainstack; /* used to differentiate between $main and others */
105static HV *coro_state_stash, *coro_stash; 116static HV *coro_state_stash, *coro_stash;
106static SV *coro_mortal; /* will be freed after next transfer */ 117static SV *coro_mortal; /* will be freed after next transfer */
107 118
127 int valgrind_id; 138 int valgrind_id;
128#endif 139#endif
129} coro_cctx; 140} coro_cctx;
130 141
131enum { 142enum {
132 CF_RUNNING, /* coroutine is running */ 143 CF_RUNNING = 0x0001, /* coroutine is running */
133 CF_READY, /* coroutine is ready */ 144 CF_READY = 0x0002, /* coroutine is ready */
145 CF_NEW = 0x0004, /* ahs never been switched to */
134}; 146};
135 147
136/* this is a structure representing a perl-level coroutine */ 148/* this is a structure representing a perl-level coroutine */
137struct coro { 149struct coro {
138 /* the c coroutine allocated to this perl coroutine, if any */ 150 /* the c coroutine allocated to this perl coroutine, if any */
277} 289}
278 290
279#define SB do { 291#define SB do {
280#define SE } while (0) 292#define SE } while (0)
281 293
282#define LOAD(state) load_state((state)); 294#define LOAD(state) load_state ((state))
283#define SAVE(state,flags) save_state((state),(flags)); 295#define SAVE(state,flags) save_state ((state),(flags))
284 296
285#define REPLACE_SV(sv,val) SB SvREFCNT_dec(sv); (sv) = (val); (val) = 0; SE 297#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
286 298
287static void 299static void
288load_state(Coro__State c) 300load_state (Coro__State c)
289{ 301{
290#define VAR(name,type) PL_ ## name = c->name; 302#define VAR(name,type) PL_ ## name = c->name;
291# include "state.h" 303# include "state.h"
292#undef VAR 304#undef VAR
293 305
310 PUTBACK; 322 PUTBACK;
311 } 323 }
312} 324}
313 325
314static void 326static void
315save_state(Coro__State c, int flags) 327save_state (Coro__State c, int flags)
316{ 328{
317 { 329 {
318 dSP; 330 dSP;
319 I32 cxix = cxstack_ix; 331 I32 cxix = cxstack_ix;
320 PERL_CONTEXT *ccstk = cxstack; 332 PERL_CONTEXT *ccstk = cxstack;
331 { 343 {
332 while (cxix >= 0) 344 while (cxix >= 0)
333 { 345 {
334 PERL_CONTEXT *cx = &ccstk[cxix--]; 346 PERL_CONTEXT *cx = &ccstk[cxix--];
335 347
336 if (CxTYPE(cx) == CXt_SUB) 348 if (CxTYPE (cx) == CXt_SUB)
337 { 349 {
338 CV *cv = cx->blk_sub.cv; 350 CV *cv = cx->blk_sub.cv;
339 351
340 if (CvDEPTH (cv)) 352 if (CvDEPTH (cv))
341 { 353 {
342 EXTEND (SP, 3); 354 EXTEND (SP, 3);
343 355
344 PUSHs ((SV *)CvPADLIST(cv)); 356 PUSHs ((SV *)CvPADLIST (cv));
345 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 357 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
346 PUSHs ((SV *)cv); 358 PUSHs ((SV *)cv);
347 359
348 CvDEPTH (cv) = 0; 360 CvDEPTH (cv) = 0;
349 get_padlist (cv); 361 get_padlist (cv);
350 } 362 }
351 } 363 }
352#ifdef CXt_FORMAT 364#ifdef CXt_FORMAT
353 else if (CxTYPE(cx) == CXt_FORMAT) 365 else if (CxTYPE (cx) == CXt_FORMAT)
354 { 366 {
355 /* I never used formats, so how should I know how these are implemented? */ 367 /* I never used formats, so how should I know how these are implemented? */
356 /* my bold guess is as a simple, plain sub... */ 368 /* my bold guess is as a simple, plain sub... */
357 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats"); 369 croak ("CXt_FORMAT not yet handled. Don't switch coroutines from within formats");
358 } 370 }
386 * not usually need a lot of stackspace. 398 * not usually need a lot of stackspace.
387 */ 399 */
388static void 400static void
389coro_init_stacks () 401coro_init_stacks ()
390{ 402{
391 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 403 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
392 PL_curstackinfo->si_type = PERLSI_MAIN; 404 PL_curstackinfo->si_type = PERLSI_MAIN;
393 PL_curstack = PL_curstackinfo->si_stack; 405 PL_curstack = PL_curstackinfo->si_stack;
394 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 406 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
395 407
396 PL_stack_base = AvARRAY(PL_curstack); 408 PL_stack_base = AvARRAY(PL_curstack);
397 PL_stack_sp = PL_stack_base; 409 PL_stack_sp = PL_stack_base;
398 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 410 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
399 411
400 New(50,PL_tmps_stack,96,SV*); 412 New(50,PL_tmps_stack,128,SV*);
401 PL_tmps_floor = -1; 413 PL_tmps_floor = -1;
402 PL_tmps_ix = -1; 414 PL_tmps_ix = -1;
403 PL_tmps_max = 96; 415 PL_tmps_max = 128;
404 416
405 New(54,PL_markstack,16,I32); 417 New(54,PL_markstack,32,I32);
406 PL_markstack_ptr = PL_markstack; 418 PL_markstack_ptr = PL_markstack;
407 PL_markstack_max = PL_markstack + 16; 419 PL_markstack_max = PL_markstack + 32;
408 420
409#ifdef SET_MARK_OFFSET 421#ifdef SET_MARK_OFFSET
410 SET_MARK_OFFSET; 422 SET_MARK_OFFSET;
411#endif 423#endif
412 424
413 New(54,PL_scopestack,16,I32); 425 New(54,PL_scopestack,32,I32);
414 PL_scopestack_ix = 0; 426 PL_scopestack_ix = 0;
415 PL_scopestack_max = 16; 427 PL_scopestack_max = 32;
416 428
417 New(54,PL_savestack,96,ANY); 429 New(54,PL_savestack,64,ANY);
418 PL_savestack_ix = 0; 430 PL_savestack_ix = 0;
419 PL_savestack_max = 96; 431 PL_savestack_max = 64;
420 432
421#if !PERL_VERSION_ATLEAST (5,9,0) 433#if !PERL_VERSION_ATLEAST (5,9,0)
422 New(54,PL_retstack,8,OP*); 434 New(54,PL_retstack,16,OP*);
423 PL_retstack_ix = 0; 435 PL_retstack_ix = 0;
424 PL_retstack_max = 8; 436 PL_retstack_max = 16;
425#endif 437#endif
426} 438}
427 439
428/* 440/*
429 * destroy the stacks, the callchain etc... 441 * destroy the stacks, the callchain etc...
482 * emulate part of the perl startup here. 494 * emulate part of the perl startup here.
483 */ 495 */
484 496
485 coro_init_stacks (); 497 coro_init_stacks ();
486 498
487 PL_curcop = 0; 499 PL_curcop = &PL_compiling;
488 PL_in_eval = 0; 500 PL_in_eval = EVAL_NULL;
489 PL_curpm = 0; 501 PL_curpm = 0;
502 PL_localizing = 0;
503 PL_dirty = 0;
504 PL_restartop = 0;
490 505
491 { 506 {
492 dSP; 507 dSP;
493 LOGOP myop; 508 LOGOP myop;
494 509
495 /* I have no idea why this is needed, but it is */
496 PUSHMARK (SP);
497
498 SvREFCNT_dec (GvAV (PL_defgv)); 510 SvREFCNT_dec (GvAV (PL_defgv));
499 GvAV (PL_defgv) = coro->args; coro->args = 0; 511 GvAV (PL_defgv) = coro->args; coro->args = 0;
500 512
501 Zero (&myop, 1, LOGOP); 513 Zero (&myop, 1, LOGOP);
502 myop.op_next = Nullop; 514 myop.op_next = Nullop;
503 myop.op_flags = OPf_WANT_VOID; 515 myop.op_flags = OPf_WANT_VOID;
504 516
505 PL_op = (OP *)&myop;
506
507 PUSHMARK (SP); 517 PUSHMARK (SP);
508 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 518 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
509 PUTBACK; 519 PUTBACK;
520 PL_op = (OP *)&myop;
510 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 521 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
511 SPAGAIN; 522 SPAGAIN;
512
513 ENTER; /* necessary e.g. for dounwind */
514 } 523 }
524
525 ENTER; /* necessary e.g. for dounwind */
515} 526}
516 527
517static void 528static void
518free_coro_mortal () 529free_coro_mortal ()
519{ 530{
522 SvREFCNT_dec (coro_mortal); 533 SvREFCNT_dec (coro_mortal);
523 coro_mortal = 0; 534 coro_mortal = 0;
524 } 535 }
525} 536}
526 537
538/* inject a fake call to Coro::State::_cctx_init into the execution */
527static void NOINLINE 539static void NOINLINE
528prepare_cctx (coro_cctx *cctx) 540prepare_cctx (coro_cctx *cctx)
529{ 541{
530 dSP; 542 dSP;
531 LOGOP myop; 543 LOGOP myop;
532 544
533 Zero (&myop, 1, LOGOP); 545 Zero (&myop, 1, LOGOP);
534 myop.op_next = PL_op; 546 myop.op_next = PL_op;
535 myop.op_flags = OPf_WANT_VOID; 547 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
536
537 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
538 548
539 PUSHMARK (SP); 549 PUSHMARK (SP);
550 EXTEND (SP, 2);
551 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx))));
540 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 552 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
541 PUTBACK; 553 PUTBACK;
554 PL_op = (OP *)&myop;
542 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 555 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
543 SPAGAIN; 556 SPAGAIN;
544} 557}
545 558
546static void 559static void
547coro_run (void *arg) 560coro_run (void *arg)
548{ 561{
549 /* coro_run is the alternative epilogue of transfer() */ 562 /* coro_run is the alternative tail of transfer(), so unlock here. */
550 UNLOCK; 563 UNLOCK;
551 564
552 /* 565 /*
553 * this is a _very_ stripped down perl interpreter ;) 566 * this is a _very_ stripped down perl interpreter ;)
554 */ 567 */
556 569
557 /* inject call to cctx_init */ 570 /* inject call to cctx_init */
558 prepare_cctx ((coro_cctx *)arg); 571 prepare_cctx ((coro_cctx *)arg);
559 572
560 /* somebody will hit me for both perl_run and PL_restartop */ 573 /* somebody will hit me for both perl_run and PL_restartop */
574 PL_restartop = PL_op;
561 perl_run (PL_curinterp); 575 perl_run (PL_curinterp);
562 576
563 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr); 577 fputs ("FATAL: C coroutine fell over the edge of the world, aborting. Did you call exit in a coroutine?\n", stderr);
564 abort (); 578 abort ();
565} 579}
574 New (0, cctx, 1, coro_cctx); 588 New (0, cctx, 1, coro_cctx);
575 589
576#if HAVE_MMAP 590#if HAVE_MMAP
577 591
578 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 592 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
579 /* mmap suppsedly does allocate-on-write for us */ 593 /* 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); 594 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
581 595
582 if (cctx->sptr == (void *)-1) 596 if (cctx->sptr == (void *)-1)
583 { 597 {
584 perror ("FATAL: unable to mmap stack for coroutine"); 598 perror ("FATAL: unable to mmap stack for coroutine");
613 627
614 return cctx; 628 return cctx;
615} 629}
616 630
617static void 631static void
618cctx_free (coro_cctx *cctx) 632cctx_destroy (coro_cctx *cctx)
619{ 633{
620 if (!cctx) 634 if (!cctx)
621 return; 635 return;
622 636
623 --cctx_count; 637 --cctx_count;
640{ 654{
641 coro_cctx *cctx; 655 coro_cctx *cctx;
642 656
643 if (cctx_first) 657 if (cctx_first)
644 { 658 {
645 --cctx_idle;
646 cctx = cctx_first; 659 cctx = cctx_first;
647 cctx_first = cctx->next; 660 cctx_first = cctx->next;
661 --cctx_idle;
648 } 662 }
649 else 663 else
650 { 664 {
651 cctx = cctx_new (); 665 cctx = cctx_new ();
652 PL_op = PL_op->op_next; 666 PL_op = PL_op->op_next;
656} 670}
657 671
658static void 672static void
659cctx_put (coro_cctx *cctx) 673cctx_put (coro_cctx *cctx)
660{ 674{
675 /* free another cctx if overlimit */
676 if (cctx_idle >= MAX_IDLE_CCTX)
677 {
678 coro_cctx *first = cctx_first;
679 cctx_first = first->next;
680 --cctx_idle;
681
682 assert (!first->inuse);
683 cctx_destroy (first);
684 }
685
661 ++cctx_idle; 686 ++cctx_idle;
662 cctx->next = cctx_first; 687 cctx->next = cctx_first;
663 cctx_first = cctx; 688 cctx_first = cctx;
664} 689}
665 690
674 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 699 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
675 else if (prev != next) 700 else if (prev != next)
676 { 701 {
677 coro_cctx *prev__cctx; 702 coro_cctx *prev__cctx;
678 703
679 if (!prev->cctx) 704 if (prev->flags & CF_NEW)
680 { 705 {
681 /* create a new empty context */ 706 /* create a new empty context */
682 Newz (0, prev->cctx, 1, coro_cctx); 707 Newz (0, prev->cctx, 1, coro_cctx);
683 prev->cctx->inuse = 1; 708 prev->cctx->inuse = 1;
709 prev->flags &= ~CF_NEW;
684 prev->flags |= CF_RUNNING; 710 prev->flags |= CF_RUNNING;
685 } 711 }
686 712
713 /*TODO: must not croak here */
687 assert ( prev->flags & CF_RUNNING); 714 if (!prev->flags & CF_RUNNING)
715 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
716
688 assert (!next->flags & CF_RUNNING); 717 if (next->flags & CF_RUNNING)
718 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
689 719
690 prev->flags &= ~CF_RUNNING; 720 prev->flags &= ~CF_RUNNING;
691 next->flags |= CF_RUNNING; 721 next->flags |= CF_RUNNING;
692 722
693 LOCK; 723 LOCK;
694 724
695 if (next->mainstack) 725 if (next->flags & CF_NEW)
726 {
727 /* need to start coroutine */
728 next->flags &= ~CF_NEW;
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 }
736 else
696 { 737 {
697 /* coroutine already started */ 738 /* coroutine already started */
698 SAVE (prev, flags); 739 SAVE (prev, flags);
699 LOAD (next); 740 LOAD (next);
700 } 741 }
701 else
702 {
703 /* need to start coroutine */
704 /* first get rid of the old state */
705 SAVE (prev, -1);
706 /* setup coroutine call */
707 setup_coro (next);
708 /* need a stack */
709 assert (!next->stack);
710 next->cctx = 0;
711 }
712 742
713 prev__cctx = prev->cctx; 743 prev__cctx = prev->cctx;
714 744
715 /* possibly "free" the cctx */ 745 /* possibly "free" the cctx */
716 if (prev__cctx->idle_sp == STACKLEVEL) 746 if (prev__cctx->idle_sp == STACKLEVEL)
717 { 747 {
748 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
718 assert (PL_top_env == prev__cctx->top_env);//D 749 assert (PL_top_env == prev__cctx->top_env);
750
751 prev->cctx = 0;
752
719 cctx_put (prev__cctx); 753 cctx_put (prev__cctx);
720 prev->cctx = 0; 754 prev__cctx->inuse = 0;
721 } 755 }
722 756
723 if (!next->cctx) 757 if (!next->cctx)
758 {
724 next->cctx = cctx_get (); 759 next->cctx = cctx_get ();
760 assert (!next->cctx->inuse);
761 next->cctx->inuse = 1;
762 }
725 763
726 if (prev__cctx != next->cctx) 764 if (prev__cctx != next->cctx)
727 { 765 {
728 assert ( prev__cctx->inuse);
729 assert (!next->cctx->inuse);
730
731 prev__cctx->inuse = 0;
732 next->cctx->inuse = 1;
733
734 prev__cctx->top_env = PL_top_env; 766 prev__cctx->top_env = PL_top_env;
735 PL_top_env = next->cctx->top_env; 767 PL_top_env = next->cctx->top_env;
736 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 768 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
737 } 769 }
738 770
758 790
759 if (coro->mainstack && coro->mainstack != main_mainstack) 791 if (coro->mainstack && coro->mainstack != main_mainstack)
760 { 792 {
761 struct coro temp; 793 struct coro temp;
762 794
795 if (coro->flags & CF_RUNNING)
796 croak ("FATAL: tried to destroy currently running coroutine");
797
763 SAVE ((&temp), TRANSFER_SAVE_ALL); 798 SAVE ((&temp), TRANSFER_SAVE_ALL);
764 LOAD (coro); 799 LOAD (coro);
765 800
766 coro_destroy_stacks (); 801 coro_destroy_stacks ();
767 802
768 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 803 LOAD ((&temp)); /* this will get rid of defsv etc.. */
769 804
770 coro->mainstack = 0; 805 coro->mainstack = 0;
771 } 806 }
772 807
773 cctx_free (coro->cctx); 808 cctx_destroy (coro->cctx);
774 SvREFCNT_dec (coro->args); 809 SvREFCNT_dec (coro->args);
775 Safefree (coro); 810 Safefree (coro);
776} 811}
777 812
778static int 813static int
838} 873}
839 874
840static void 875static void
841api_transfer (SV *prev, SV *next, int flags) 876api_transfer (SV *prev, SV *next, int flags)
842{ 877{
843 dTHX;
844 struct transfer_args ta; 878 struct transfer_args ta;
845 879
846 prepare_transfer (&ta, prev, next, flags); 880 prepare_transfer (&ta, prev, next, flags);
847 TRANSFER (ta); 881 TRANSFER (ta);
848} 882}
855#define PRIO_LOW -1 889#define PRIO_LOW -1
856#define PRIO_IDLE -3 890#define PRIO_IDLE -3
857#define PRIO_MIN -4 891#define PRIO_MIN -4
858 892
859/* for Coro.pm */ 893/* for Coro.pm */
860static GV *coro_current, *coro_idle; 894static SV *coro_current;
861static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 895static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
862static int coro_nready; 896static int coro_nready;
863 897
864static void 898static void
865coro_enq (SV *sv) 899coro_enq (SV *coro_sv)
866{ 900{
867 int prio;
868
869 if (SvTYPE (sv) != SVt_PVHV)
870 croak ("Coro::ready tried to enqueue something that is not a coroutine");
871
872 prio = SvSTATE (sv)->prio;
873
874 av_push (coro_ready [prio - PRIO_MIN], sv); 901 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
875 coro_nready++; 902 coro_nready++;
876} 903}
877 904
878static SV * 905static SV *
879coro_deq (int min_prio) 906coro_deq (int min_prio)
901 928
902 if (SvROK (coro_sv)) 929 if (SvROK (coro_sv))
903 coro_sv = SvRV (coro_sv); 930 coro_sv = SvRV (coro_sv);
904 931
905 coro = SvSTATE (coro_sv); 932 coro = SvSTATE (coro_sv);
933
906 if (coro->flags & CF_READY) 934 if (coro->flags & CF_READY)
907 return 0; 935 return 0;
936
937#if 0 /* this is actually harmless */
938 if (coro->flags & CF_RUNNING)
939 croak ("Coro::ready called on currently running coroutine");
940#endif
908 941
909 coro->flags |= CF_READY; 942 coro->flags |= CF_READY;
910 943
911 LOCK; 944 LOCK;
912 coro_enq (SvREFCNT_inc (coro_sv)); 945 coro_enq (SvREFCNT_inc (coro_sv));
916} 949}
917 950
918static int 951static int
919api_is_ready (SV *coro_sv) 952api_is_ready (SV *coro_sv)
920{ 953{
921 struct coro *coro;
922
923 return !!(SvSTATE (coro_sv)->flags & CF_READY); 954 return !!SvSTATE (coro_sv)->flags & CF_READY;
924} 955}
925 956
926static void 957static void
927prepare_schedule (struct transfer_args *ta) 958prepare_schedule (struct transfer_args *ta)
928{ 959{
929 SV *current, *prev, *next; 960 SV *prev, *next;
930
931 current = GvSV (coro_current);
932 961
933 for (;;) 962 for (;;)
934 { 963 {
935 LOCK; 964 LOCK;
936 next = coro_deq (PRIO_MIN); 965 next = coro_deq (PRIO_MIN);
945 ENTER; 974 ENTER;
946 SAVETMPS; 975 SAVETMPS;
947 976
948 PUSHMARK (SP); 977 PUSHMARK (SP);
949 PUTBACK; 978 PUTBACK;
950 call_sv (GvSV (coro_idle), G_DISCARD); 979 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
951 980
952 FREETMPS; 981 FREETMPS;
953 LEAVE; 982 LEAVE;
954 } 983 }
955 } 984 }
956 985
957 prev = SvRV (current); 986 prev = SvRV (coro_current);
958 SvRV (current) = next; 987 SvRV_set (coro_current, next);
959 988
960 /* free this only after the transfer */ 989 /* free this only after the transfer */
961 LOCK; 990 LOCK;
962 free_coro_mortal (); 991 free_coro_mortal ();
963 UNLOCK; 992 UNLOCK;
964 coro_mortal = prev; 993 coro_mortal = prev;
965 994
995 assert (!SvROK(prev));//D
996 assert (!SvROK(next));//D
997
966 ta->prev = SvSTATE (prev); 998 ta->prev = SvSTATE (prev);
967 ta->next = SvSTATE (next); 999 ta->next = SvSTATE (next);
968 ta->flags = TRANSFER_SAVE_ALL; 1000 ta->flags = TRANSFER_SAVE_ALL;
969 1001
1002 assert (ta->flags & CF_READY);
970 ta->next->flags &= ~CF_READY; 1003 ta->next->flags &= ~CF_READY;
971} 1004}
972 1005
973static void 1006static void
974prepare_cede (struct transfer_args *ta) 1007prepare_cede (struct transfer_args *ta)
975{ 1008{
976 api_ready (GvSV (coro_current)); 1009 api_ready (coro_current);
977 1010
978 prepare_schedule (ta); 1011 prepare_schedule (ta);
979} 1012}
980 1013
981static void 1014static void
982api_schedule (void) 1015api_schedule (void)
983{ 1016{
984 dTHX;
985 struct transfer_args ta; 1017 struct transfer_args ta;
986 1018
987 prepare_schedule (&ta); 1019 prepare_schedule (&ta);
988 TRANSFER (ta); 1020 TRANSFER (ta);
989} 1021}
990 1022
991static void 1023static void
992api_cede (void) 1024api_cede (void)
993{ 1025{
994 dTHX;
995 struct transfer_args ta; 1026 struct transfer_args ta;
996 1027
997 prepare_cede (&ta); 1028 prepare_cede (&ta);
998 TRANSFER (ta); 1029 TRANSFER (ta);
999} 1030}
1031 HV *hv; 1062 HV *hv;
1032 int i; 1063 int i;
1033 1064
1034 Newz (0, coro, 1, struct coro); 1065 Newz (0, coro, 1, struct coro);
1035 coro->args = newAV (); 1066 coro->args = newAV ();
1067 coro->flags = CF_NEW;
1036 1068
1037 hv = newHV (); 1069 hv = newHV ();
1038 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1070 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP;
1039 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1071 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1040 1072
1048_set_stacklevel (...) 1080_set_stacklevel (...)
1049 ALIAS: 1081 ALIAS:
1050 Coro::State::transfer = 1 1082 Coro::State::transfer = 1
1051 Coro::schedule = 2 1083 Coro::schedule = 2
1052 Coro::cede = 3 1084 Coro::cede = 3
1053 Coro::Cont::yield = 4
1054 CODE: 1085 CODE:
1055{ 1086{
1056 struct transfer_args ta; 1087 struct transfer_args ta;
1057 1088
1058 switch (ix) 1089 switch (ix)
1063 ta.flags = TRANSFER_SET_STACKLEVEL; 1094 ta.flags = TRANSFER_SET_STACKLEVEL;
1064 break; 1095 break;
1065 1096
1066 case 1: 1097 case 1:
1067 if (items != 3) 1098 if (items != 3)
1068 croak ("Coro::State::transfer(prev,next,flags) expects three arguments, not %d", items); 1099 croak ("Coro::State::transfer (prev,next,flags) expects three arguments, not %d", items);
1069 1100
1070 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); 1101 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2)));
1071 break; 1102 break;
1072 1103
1073 case 2: 1104 case 2:
1075 break; 1106 break;
1076 1107
1077 case 3: 1108 case 3:
1078 prepare_cede (&ta); 1109 prepare_cede (&ta);
1079 break; 1110 break;
1080
1081 case 4:
1082 {
1083 SV *yieldstack;
1084 SV *sv;
1085 AV *defav = GvAV (PL_defgv);
1086
1087 yieldstack = *hv_fetch (
1088 (HV *)SvRV (GvSV (coro_current)),
1089 "yieldstack", sizeof ("yieldstack") - 1,
1090 0
1091 );
1092
1093 /* set up @_ -- ugly */
1094 av_clear (defav);
1095 av_fill (defav, items - 1);
1096 while (items--)
1097 av_store (defav, items, SvREFCNT_inc (ST(items)));
1098
1099 sv = av_pop ((AV *)SvRV (yieldstack));
1100 ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1101 ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1102 ta.flags = 0;
1103 SvREFCNT_dec (sv);
1104 }
1105 break;
1106
1107 } 1111 }
1108 1112
1109 TRANSFER (ta); 1113 TRANSFER (ta);
1110} 1114}
1111 1115
1155 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1159 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1156 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1160 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1157 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1161 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1158 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1162 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1159 1163
1160 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); 1164 coro_current = get_sv ("Coro::current", FALSE);
1161 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); 1165 SvREADONLY_on (coro_current);
1162 1166
1163 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1167 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1164 coro_ready[i] = newAV (); 1168 coro_ready[i] = newAV ();
1165 1169
1166 { 1170 {
1221 CODE: 1225 CODE:
1222 RETVAL = coro_nready; 1226 RETVAL = coro_nready;
1223 OUTPUT: 1227 OUTPUT:
1224 RETVAL 1228 RETVAL
1225 1229
1230void
1231_set_current (SV *current)
1232 PROTOTYPE: $
1233 CODE:
1234 SvREFCNT_dec (SvRV (coro_current));
1235 SvRV_set (coro_current, SvREFCNT_inc (SvRV (current)));
1236
1226MODULE = Coro::State PACKAGE = Coro::AIO 1237MODULE = Coro::State PACKAGE = Coro::AIO
1227 1238
1228SV * 1239SV *
1229_get_state () 1240_get_state ()
1230 CODE: 1241 CODE:
1231{ 1242{
1232 struct { 1243 struct io_state *data;
1233 int errorno;
1234 int laststype;
1235 int laststatval;
1236 Stat_t statcache;
1237 } data;
1238 1244
1245 RETVAL = newSV (sizeof (struct io_state));
1246 data = (struct io_state *)SvPVX (RETVAL);
1247 SvCUR_set (RETVAL, sizeof (struct io_state));
1248 SvPOK_only (RETVAL);
1249
1239 data.errorno = errno; 1250 data->errorno = errno;
1240 data.laststype = PL_laststype; 1251 data->laststype = PL_laststype;
1241 data.laststatval = PL_laststatval; 1252 data->laststatval = PL_laststatval;
1242 data.statcache = PL_statcache; 1253 data->statcache = PL_statcache;
1243
1244 RETVAL = newSVpvn ((char *)&data, sizeof data);
1245} 1254}
1246 OUTPUT: 1255 OUTPUT:
1247 RETVAL 1256 RETVAL
1248 1257
1249void 1258void
1250_set_state (char *data_) 1259_set_state (char *data_)
1251 PROTOTYPE: $ 1260 PROTOTYPE: $
1252 CODE: 1261 CODE:
1253{ 1262{
1254 struct { 1263 struct io_state *data = (void *)data_;
1255 int errorno;
1256 int laststype;
1257 int laststatval;
1258 Stat_t statcache;
1259 } *data = (void *)data_;
1260 1264
1261 errno = data->errorno; 1265 errno = data->errorno;
1262 PL_laststype = data->laststype; 1266 PL_laststype = data->laststype;
1263 PL_laststatval = data->laststatval; 1267 PL_laststatval = data->laststatval;
1264 PL_statcache = data->statcache; 1268 PL_statcache = data->statcache;
1265} 1269}
1270

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines