ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.109 by root, Mon Nov 27 18:45:29 2006 UTC vs.
Revision 1.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
119 /* cpu state */ 130 /* cpu state */
120 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 131 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
121 JMPENV *top_env; 132 JMPENV *top_env;
122 coro_context cctx; 133 coro_context cctx;
123 134
135 int inuse;
136
124#if USE_VALGRIND 137#if USE_VALGRIND
125 int valgrind_id; 138 int valgrind_id;
126#endif 139#endif
127} coro_cctx; 140} coro_cctx;
141
142enum {
143 CF_RUNNING = 0x0001, /* coroutine is running */
144 CF_READY = 0x0002, /* coroutine is ready */
145 CF_NEW = 0x0004, /* ahs never been switched to */
146};
128 147
129/* this is a structure representing a perl-level coroutine */ 148/* this is a structure representing a perl-level coroutine */
130struct coro { 149struct coro {
131 /* the c coroutine allocated to this perl coroutine, if any */ 150 /* the c coroutine allocated to this perl coroutine, if any */
132 coro_cctx *cctx; 151 coro_cctx *cctx;
133 152
134 /* data associated with this coroutine (initial args) */ 153 /* data associated with this coroutine (initial args) */
135 AV *args; 154 AV *args;
136 int refcnt; 155 int refcnt;
156 int flags;
137 157
138 /* optionally saved, might be zero */ 158 /* optionally saved, might be zero */
139 AV *defav; 159 AV *defav;
140 SV *defsv; 160 SV *defsv;
141 SV *errsv; 161 SV *errsv;
269} 289}
270 290
271#define SB do { 291#define SB do {
272#define SE } while (0) 292#define SE } while (0)
273 293
274#define LOAD(state) load_state((state)); 294#define LOAD(state) load_state ((state))
275#define SAVE(state,flags) save_state((state),(flags)); 295#define SAVE(state,flags) save_state ((state),(flags))
276 296
277#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
278 298
279static void 299static void
280load_state(Coro__State c) 300load_state (Coro__State c)
281{ 301{
282#define VAR(name,type) PL_ ## name = c->name; 302#define VAR(name,type) PL_ ## name = c->name;
283# include "state.h" 303# include "state.h"
284#undef VAR 304#undef VAR
285 305
302 PUTBACK; 322 PUTBACK;
303 } 323 }
304} 324}
305 325
306static void 326static void
307save_state(Coro__State c, int flags) 327save_state (Coro__State c, int flags)
308{ 328{
309 { 329 {
310 dSP; 330 dSP;
311 I32 cxix = cxstack_ix; 331 I32 cxix = cxstack_ix;
312 PERL_CONTEXT *ccstk = cxstack; 332 PERL_CONTEXT *ccstk = cxstack;
323 { 343 {
324 while (cxix >= 0) 344 while (cxix >= 0)
325 { 345 {
326 PERL_CONTEXT *cx = &ccstk[cxix--]; 346 PERL_CONTEXT *cx = &ccstk[cxix--];
327 347
328 if (CxTYPE(cx) == CXt_SUB) 348 if (CxTYPE (cx) == CXt_SUB)
329 { 349 {
330 CV *cv = cx->blk_sub.cv; 350 CV *cv = cx->blk_sub.cv;
331 351
332 if (CvDEPTH (cv)) 352 if (CvDEPTH (cv))
333 { 353 {
334 EXTEND (SP, 3); 354 EXTEND (SP, 3);
335 355
336 PUSHs ((SV *)CvPADLIST(cv)); 356 PUSHs ((SV *)CvPADLIST (cv));
337 PUSHs (INT2PTR (SV *, CvDEPTH (cv))); 357 PUSHs (INT2PTR (SV *, CvDEPTH (cv)));
338 PUSHs ((SV *)cv); 358 PUSHs ((SV *)cv);
339 359
340 CvDEPTH (cv) = 0; 360 CvDEPTH (cv) = 0;
341 get_padlist (cv); 361 get_padlist (cv);
342 } 362 }
343 } 363 }
344#ifdef CXt_FORMAT 364#ifdef CXt_FORMAT
345 else if (CxTYPE(cx) == CXt_FORMAT) 365 else if (CxTYPE (cx) == CXt_FORMAT)
346 { 366 {
347 /* 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? */
348 /* my bold guess is as a simple, plain sub... */ 368 /* my bold guess is as a simple, plain sub... */
349 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");
350 } 370 }
378 * not usually need a lot of stackspace. 398 * not usually need a lot of stackspace.
379 */ 399 */
380static void 400static void
381coro_init_stacks () 401coro_init_stacks ()
382{ 402{
383 PL_curstackinfo = new_stackinfo(96, 1024/sizeof(PERL_CONTEXT) - 1); 403 PL_curstackinfo = new_stackinfo(128, 1024/sizeof(PERL_CONTEXT));
384 PL_curstackinfo->si_type = PERLSI_MAIN; 404 PL_curstackinfo->si_type = PERLSI_MAIN;
385 PL_curstack = PL_curstackinfo->si_stack; 405 PL_curstack = PL_curstackinfo->si_stack;
386 PL_mainstack = PL_curstack; /* remember in case we switch stacks */ 406 PL_mainstack = PL_curstack; /* remember in case we switch stacks */
387 407
388 PL_stack_base = AvARRAY(PL_curstack); 408 PL_stack_base = AvARRAY(PL_curstack);
389 PL_stack_sp = PL_stack_base; 409 PL_stack_sp = PL_stack_base;
390 PL_stack_max = PL_stack_base + AvMAX(PL_curstack); 410 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
391 411
392 New(50,PL_tmps_stack,96,SV*); 412 New(50,PL_tmps_stack,128,SV*);
393 PL_tmps_floor = -1; 413 PL_tmps_floor = -1;
394 PL_tmps_ix = -1; 414 PL_tmps_ix = -1;
395 PL_tmps_max = 96; 415 PL_tmps_max = 128;
396 416
397 New(54,PL_markstack,16,I32); 417 New(54,PL_markstack,32,I32);
398 PL_markstack_ptr = PL_markstack; 418 PL_markstack_ptr = PL_markstack;
399 PL_markstack_max = PL_markstack + 16; 419 PL_markstack_max = PL_markstack + 32;
400 420
401#ifdef SET_MARK_OFFSET 421#ifdef SET_MARK_OFFSET
402 SET_MARK_OFFSET; 422 SET_MARK_OFFSET;
403#endif 423#endif
404 424
405 New(54,PL_scopestack,16,I32); 425 New(54,PL_scopestack,32,I32);
406 PL_scopestack_ix = 0; 426 PL_scopestack_ix = 0;
407 PL_scopestack_max = 16; 427 PL_scopestack_max = 32;
408 428
409 New(54,PL_savestack,96,ANY); 429 New(54,PL_savestack,64,ANY);
410 PL_savestack_ix = 0; 430 PL_savestack_ix = 0;
411 PL_savestack_max = 96; 431 PL_savestack_max = 64;
412 432
413#if !PERL_VERSION_ATLEAST (5,9,0) 433#if !PERL_VERSION_ATLEAST (5,9,0)
414 New(54,PL_retstack,8,OP*); 434 New(54,PL_retstack,16,OP*);
415 PL_retstack_ix = 0; 435 PL_retstack_ix = 0;
416 PL_retstack_max = 8; 436 PL_retstack_max = 16;
417#endif 437#endif
418} 438}
419 439
420/* 440/*
421 * destroy the stacks, the callchain etc... 441 * destroy the stacks, the callchain etc...
474 * emulate part of the perl startup here. 494 * emulate part of the perl startup here.
475 */ 495 */
476 496
477 coro_init_stacks (); 497 coro_init_stacks ();
478 498
479 PL_curcop = 0; 499 PL_curcop = &PL_compiling;
480 PL_in_eval = 0; 500 PL_in_eval = EVAL_NULL;
481 PL_curpm = 0; 501 PL_curpm = 0;
502 PL_localizing = 0;
503 PL_dirty = 0;
504 PL_restartop = 0;
482 505
483 { 506 {
484 dSP; 507 dSP;
485 LOGOP myop; 508 LOGOP myop;
486 509
487 /* I have no idea why this is needed, but it is */
488 PUSHMARK (SP);
489
490 SvREFCNT_dec (GvAV (PL_defgv)); 510 SvREFCNT_dec (GvAV (PL_defgv));
491 GvAV (PL_defgv) = coro->args; coro->args = 0; 511 GvAV (PL_defgv) = coro->args; coro->args = 0;
492 512
493 Zero (&myop, 1, LOGOP); 513 Zero (&myop, 1, LOGOP);
494 myop.op_next = Nullop; 514 myop.op_next = Nullop;
495 myop.op_flags = OPf_WANT_VOID; 515 myop.op_flags = OPf_WANT_VOID;
496 516
497 PL_op = (OP *)&myop;
498
499 PUSHMARK (SP); 517 PUSHMARK (SP);
500 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE)); 518 XPUSHs ((SV *)get_cv ("Coro::State::_coro_init", FALSE));
501 PUTBACK; 519 PUTBACK;
520 PL_op = (OP *)&myop;
502 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 521 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
503 SPAGAIN; 522 SPAGAIN;
504
505 ENTER; /* necessary e.g. for dounwind */
506 } 523 }
524
525 ENTER; /* necessary e.g. for dounwind */
507} 526}
508 527
509static void 528static void
510free_coro_mortal () 529free_coro_mortal ()
511{ 530{
514 SvREFCNT_dec (coro_mortal); 533 SvREFCNT_dec (coro_mortal);
515 coro_mortal = 0; 534 coro_mortal = 0;
516 } 535 }
517} 536}
518 537
538/* inject a fake call to Coro::State::_cctx_init into the execution */
519static void NOINLINE 539static void NOINLINE
520prepare_cctx (coro_cctx *cctx) 540prepare_cctx (coro_cctx *cctx)
521{ 541{
522 dSP; 542 dSP;
523 LOGOP myop; 543 LOGOP myop;
524 544
525 Zero (&myop, 1, LOGOP); 545 Zero (&myop, 1, LOGOP);
526 myop.op_next = PL_op; 546 myop.op_next = PL_op;
527 myop.op_flags = OPf_WANT_VOID; 547 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
528
529 sv_setiv (get_sv ("Coro::State::_cctx", FALSE), PTR2IV (cctx));
530 548
531 PUSHMARK (SP); 549 PUSHMARK (SP);
550 EXTEND (SP, 2);
551 PUSHs (sv_2mortal (newSViv (PTR2IV (cctx))));
532 XPUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE)); 552 PUSHs ((SV *)get_cv ("Coro::State::_cctx_init", FALSE));
533 PUTBACK; 553 PUTBACK;
554 PL_op = (OP *)&myop;
534 PL_restartop = PL_ppaddr[OP_ENTERSUB](aTHX); 555 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
535 SPAGAIN; 556 SPAGAIN;
536} 557}
537 558
538static void 559static void
539coro_run (void *arg) 560coro_run (void *arg)
540{ 561{
541 /* coro_run is the alternative epilogue of transfer() */ 562 /* coro_run is the alternative tail of transfer(), so unlock here. */
542 UNLOCK; 563 UNLOCK;
543 564
544 /* 565 /*
545 * this is a _very_ stripped down perl interpreter ;) 566 * this is a _very_ stripped down perl interpreter ;)
546 */ 567 */
548 569
549 /* inject call to cctx_init */ 570 /* inject call to cctx_init */
550 prepare_cctx ((coro_cctx *)arg); 571 prepare_cctx ((coro_cctx *)arg);
551 572
552 /* 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;
553 perl_run (PL_curinterp); 575 perl_run (PL_curinterp);
554 576
555 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);
556 abort (); 578 abort ();
557} 579}
566 New (0, cctx, 1, coro_cctx); 588 New (0, cctx, 1, coro_cctx);
567 589
568#if HAVE_MMAP 590#if HAVE_MMAP
569 591
570 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 592 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
571 /* mmap suppsedly does allocate-on-write for us */ 593 /* mmap supposedly does allocate-on-write for us */
572 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 594 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
573 595
574 if (cctx->sptr == (void *)-1) 596 if (cctx->sptr == (void *)-1)
575 { 597 {
576 perror ("FATAL: unable to mmap stack for coroutine"); 598 perror ("FATAL: unable to mmap stack for coroutine");
605 627
606 return cctx; 628 return cctx;
607} 629}
608 630
609static void 631static void
610cctx_free (coro_cctx *cctx) 632cctx_destroy (coro_cctx *cctx)
611{ 633{
612 if (!cctx) 634 if (!cctx)
613 return; 635 return;
614 636
615 --cctx_count; 637 --cctx_count;
632{ 654{
633 coro_cctx *cctx; 655 coro_cctx *cctx;
634 656
635 if (cctx_first) 657 if (cctx_first)
636 { 658 {
637 --cctx_idle;
638 cctx = cctx_first; 659 cctx = cctx_first;
639 cctx_first = cctx->next; 660 cctx_first = cctx->next;
661 --cctx_idle;
640 } 662 }
641 else 663 else
642 { 664 {
643 cctx = cctx_new (); 665 cctx = cctx_new ();
644 PL_op = PL_op->op_next; 666 PL_op = PL_op->op_next;
648} 670}
649 671
650static void 672static void
651cctx_put (coro_cctx *cctx) 673cctx_put (coro_cctx *cctx)
652{ 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
653 ++cctx_idle; 686 ++cctx_idle;
654 cctx->next = cctx_first; 687 cctx->next = cctx_first;
655 cctx_first = cctx; 688 cctx_first = cctx;
656} 689}
657 690
666 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 699 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
667 else if (prev != next) 700 else if (prev != next)
668 { 701 {
669 coro_cctx *prev__cctx; 702 coro_cctx *prev__cctx;
670 703
704 if (prev->flags & CF_NEW)
705 {
706 /* create a new empty context */
707 Newz (0, prev->cctx, 1, coro_cctx);
708 prev->cctx->inuse = 1;
709 prev->flags &= ~CF_NEW;
710 prev->flags |= CF_RUNNING;
711 }
712
713 /*TODO: must not croak here */
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
717 if (next->flags & CF_RUNNING)
718 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
719
720 prev->flags &= ~CF_RUNNING;
721 next->flags |= CF_RUNNING;
722
671 LOCK; 723 LOCK;
672 724
673 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
674 { 737 {
675 /* coroutine already started */ 738 /* coroutine already started */
676 SAVE (prev, flags); 739 SAVE (prev, flags);
677 LOAD (next); 740 LOAD (next);
678 } 741 }
679 else
680 {
681 /* need to start coroutine */
682 /* first get rid of the old state */
683 SAVE (prev, -1);
684 /* setup coroutine call */
685 setup_coro (next);
686 /* need a stack */
687 next->cctx = 0;
688 }
689
690 if (!prev->cctx)
691 /* create a new empty context */
692 Newz (0, prev->cctx, 1, coro_cctx);
693 742
694 prev__cctx = prev->cctx; 743 prev__cctx = prev->cctx;
695 744
696 /* possibly "free" the cctx */ 745 /* possibly "free" the cctx */
697 if (prev__cctx->idle_sp == STACKLEVEL) 746 if (prev__cctx->idle_sp == STACKLEVEL)
698 { 747 {
748 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
749 assert (PL_top_env == prev__cctx->top_env);
750
751 prev->cctx = 0;
752
699 cctx_put (prev__cctx); 753 cctx_put (prev__cctx);
700 prev->cctx = 0; 754 prev__cctx->inuse = 0;
701 } 755 }
702 756
703 if (!next->cctx) 757 if (!next->cctx)
758 {
704 next->cctx = cctx_get (); 759 next->cctx = cctx_get ();
760 assert (!next->cctx->inuse);
761 next->cctx->inuse = 1;
762 }
705 763
706 if (prev__cctx != next->cctx) 764 if (prev__cctx != next->cctx)
707 { 765 {
708 prev__cctx->top_env = PL_top_env; 766 prev__cctx->top_env = PL_top_env;
709 PL_top_env = next->cctx->top_env; 767 PL_top_env = next->cctx->top_env;
732 790
733 if (coro->mainstack && coro->mainstack != main_mainstack) 791 if (coro->mainstack && coro->mainstack != main_mainstack)
734 { 792 {
735 struct coro temp; 793 struct coro temp;
736 794
795 if (coro->flags & CF_RUNNING)
796 croak ("FATAL: tried to destroy currently running coroutine");
797
737 SAVE ((&temp), TRANSFER_SAVE_ALL); 798 SAVE ((&temp), TRANSFER_SAVE_ALL);
738 LOAD (coro); 799 LOAD (coro);
739 800
740 coro_destroy_stacks (); 801 coro_destroy_stacks ();
741 802
742 LOAD ((&temp)); /* this will get rid of defsv etc.. */ 803 LOAD ((&temp)); /* this will get rid of defsv etc.. */
743 804
744 coro->mainstack = 0; 805 coro->mainstack = 0;
745 } 806 }
746 807
747 cctx_free (coro->cctx); 808 cctx_destroy (coro->cctx);
748 SvREFCNT_dec (coro->args); 809 SvREFCNT_dec (coro->args);
749 Safefree (coro); 810 Safefree (coro);
750} 811}
751 812
752static int 813static int
812} 873}
813 874
814static void 875static void
815api_transfer (SV *prev, SV *next, int flags) 876api_transfer (SV *prev, SV *next, int flags)
816{ 877{
817 dTHX;
818 struct transfer_args ta; 878 struct transfer_args ta;
819 879
820 prepare_transfer (&ta, prev, next, flags); 880 prepare_transfer (&ta, prev, next, flags);
821 TRANSFER (ta); 881 TRANSFER (ta);
822} 882}
829#define PRIO_LOW -1 889#define PRIO_LOW -1
830#define PRIO_IDLE -3 890#define PRIO_IDLE -3
831#define PRIO_MIN -4 891#define PRIO_MIN -4
832 892
833/* for Coro.pm */ 893/* for Coro.pm */
834static GV *coro_current, *coro_idle; 894static SV *coro_current;
835static AV *coro_ready [PRIO_MAX-PRIO_MIN+1]; 895static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
836static int coro_nready; 896static int coro_nready;
837 897
838static void 898static void
839coro_enq (SV *sv) 899coro_enq (SV *coro_sv)
840{ 900{
841 int prio;
842
843 if (SvTYPE (sv) != SVt_PVHV)
844 croak ("Coro::ready tried to enqueue something that is not a coroutine");
845
846 prio = SvSTATE (sv)->prio;
847
848 av_push (coro_ready [prio - PRIO_MIN], sv); 901 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
849 coro_nready++; 902 coro_nready++;
850} 903}
851 904
852static SV * 905static SV *
853coro_deq (int min_prio) 906coro_deq (int min_prio)
866 } 919 }
867 920
868 return 0; 921 return 0;
869} 922}
870 923
871static void 924static int
872api_ready (SV *coro) 925api_ready (SV *coro_sv)
873{ 926{
874 dTHX; 927 struct coro *coro;
875 928
876 if (SvROK (coro)) 929 if (SvROK (coro_sv))
877 coro = SvRV (coro); 930 coro_sv = SvRV (coro_sv);
931
932 coro = SvSTATE (coro_sv);
933
934 if (coro->flags & CF_READY)
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
941
942 coro->flags |= CF_READY;
878 943
879 LOCK; 944 LOCK;
880 coro_enq (SvREFCNT_inc (coro)); 945 coro_enq (SvREFCNT_inc (coro_sv));
881 UNLOCK; 946 UNLOCK;
947
948 return 1;
949}
950
951static int
952api_is_ready (SV *coro_sv)
953{
954 return !!SvSTATE (coro_sv)->flags & CF_READY;
882} 955}
883 956
884static void 957static void
885prepare_schedule (struct transfer_args *ta) 958prepare_schedule (struct transfer_args *ta)
886{ 959{
887 SV *current, *prev, *next; 960 SV *prev, *next;
888
889 current = GvSV (coro_current);
890 961
891 for (;;) 962 for (;;)
892 { 963 {
893 LOCK; 964 LOCK;
894 next = coro_deq (PRIO_MIN); 965 next = coro_deq (PRIO_MIN);
903 ENTER; 974 ENTER;
904 SAVETMPS; 975 SAVETMPS;
905 976
906 PUSHMARK (SP); 977 PUSHMARK (SP);
907 PUTBACK; 978 PUTBACK;
908 call_sv (GvSV (coro_idle), G_DISCARD); 979 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
909 980
910 FREETMPS; 981 FREETMPS;
911 LEAVE; 982 LEAVE;
912 } 983 }
913 } 984 }
914 985
915 prev = SvRV (current); 986 prev = SvRV (coro_current);
916 SvRV (current) = next; 987 SvRV_set (coro_current, next);
917 988
918 /* free this only after the transfer */ 989 /* free this only after the transfer */
919 LOCK; 990 LOCK;
920 free_coro_mortal (); 991 free_coro_mortal ();
921 UNLOCK; 992 UNLOCK;
922 coro_mortal = prev; 993 coro_mortal = prev;
923 994
995 assert (!SvROK(prev));//D
996 assert (!SvROK(next));//D
997
924 ta->prev = SvSTATE (prev); 998 ta->prev = SvSTATE (prev);
925 ta->next = SvSTATE (next); 999 ta->next = SvSTATE (next);
926 ta->flags = TRANSFER_SAVE_ALL; 1000 ta->flags = TRANSFER_SAVE_ALL;
1001
1002 assert (ta->flags & CF_READY);
1003 ta->next->flags &= ~CF_READY;
927} 1004}
928 1005
929static void 1006static void
930prepare_cede (struct transfer_args *ta) 1007prepare_cede (struct transfer_args *ta)
931{ 1008{
932 LOCK; 1009 api_ready (coro_current);
933 coro_enq (SvREFCNT_inc (SvRV (GvSV (coro_current))));
934 UNLOCK;
935 1010
936 prepare_schedule (ta); 1011 prepare_schedule (ta);
937} 1012}
938 1013
939static void 1014static void
940api_schedule (void) 1015api_schedule (void)
941{ 1016{
942 dTHX;
943 struct transfer_args ta; 1017 struct transfer_args ta;
944 1018
945 prepare_schedule (&ta); 1019 prepare_schedule (&ta);
946 TRANSFER (ta); 1020 TRANSFER (ta);
947} 1021}
948 1022
949static void 1023static void
950api_cede (void) 1024api_cede (void)
951{ 1025{
952 dTHX;
953 struct transfer_args ta; 1026 struct transfer_args ta;
954 1027
955 prepare_cede (&ta); 1028 prepare_cede (&ta);
956 TRANSFER (ta); 1029 TRANSFER (ta);
957} 1030}
989 HV *hv; 1062 HV *hv;
990 int i; 1063 int i;
991 1064
992 Newz (0, coro, 1, struct coro); 1065 Newz (0, coro, 1, struct coro);
993 coro->args = newAV (); 1066 coro->args = newAV ();
1067 coro->flags = CF_NEW;
994 1068
995 hv = newHV (); 1069 hv = newHV ();
996 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;
997 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1071 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
998 1072
1006_set_stacklevel (...) 1080_set_stacklevel (...)
1007 ALIAS: 1081 ALIAS:
1008 Coro::State::transfer = 1 1082 Coro::State::transfer = 1
1009 Coro::schedule = 2 1083 Coro::schedule = 2
1010 Coro::cede = 3 1084 Coro::cede = 3
1011 Coro::Cont::yield = 4
1012 CODE: 1085 CODE:
1013{ 1086{
1014 struct transfer_args ta; 1087 struct transfer_args ta;
1015 1088
1016 switch (ix) 1089 switch (ix)
1021 ta.flags = TRANSFER_SET_STACKLEVEL; 1094 ta.flags = TRANSFER_SET_STACKLEVEL;
1022 break; 1095 break;
1023 1096
1024 case 1: 1097 case 1:
1025 if (items != 3) 1098 if (items != 3)
1026 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);
1027 1100
1028 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2))); 1101 prepare_transfer (&ta, ST (0), ST (1), SvIV (ST (2)));
1029 break; 1102 break;
1030 1103
1031 case 2: 1104 case 2:
1033 break; 1106 break;
1034 1107
1035 case 3: 1108 case 3:
1036 prepare_cede (&ta); 1109 prepare_cede (&ta);
1037 break; 1110 break;
1038
1039 case 4:
1040 {
1041 SV *yieldstack;
1042 SV *sv;
1043 AV *defav = GvAV (PL_defgv);
1044
1045 yieldstack = *hv_fetch (
1046 (HV *)SvRV (GvSV (coro_current)),
1047 "yieldstack", sizeof ("yieldstack") - 1,
1048 0
1049 );
1050
1051 /* set up @_ -- ugly */
1052 av_clear (defav);
1053 av_fill (defav, items - 1);
1054 while (items--)
1055 av_store (defav, items, SvREFCNT_inc (ST(items)));
1056
1057 sv = av_pop ((AV *)SvRV (yieldstack));
1058 ta.prev = SvSTATE (*av_fetch ((AV *)SvRV (sv), 0, 0));
1059 ta.next = SvSTATE (*av_fetch ((AV *)SvRV (sv), 1, 0));
1060 ta.flags = 0;
1061 SvREFCNT_dec (sv);
1062 }
1063 break;
1064
1065 } 1111 }
1066 1112
1067 TRANSFER (ta); 1113 TRANSFER (ta);
1068} 1114}
1069 1115
1113 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1159 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1114 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1160 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1115 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1161 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1116 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1162 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1117 1163
1118 coro_current = gv_fetchpv ("Coro::current", TRUE, SVt_PV); 1164 coro_current = get_sv ("Coro::current", FALSE);
1119 coro_idle = gv_fetchpv ("Coro::idle" , TRUE, SVt_PV); 1165 SvREADONLY_on (coro_current);
1120 1166
1121 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1167 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1122 coro_ready[i] = newAV (); 1168 coro_ready[i] = newAV ();
1123 1169
1124 { 1170 {
1125 SV *sv = perl_get_sv("Coro::API", 1); 1171 SV *sv = perl_get_sv("Coro::API", 1);
1126 1172
1127 coroapi.schedule = api_schedule; 1173 coroapi.schedule = api_schedule;
1128 coroapi.cede = api_cede; 1174 coroapi.cede = api_cede;
1129 coroapi.ready = api_ready; 1175 coroapi.ready = api_ready;
1176 coroapi.is_ready = api_is_ready;
1130 coroapi.nready = &coro_nready; 1177 coroapi.nready = &coro_nready;
1131 coroapi.current = coro_current; 1178 coroapi.current = coro_current;
1132 1179
1133 GCoroAPI = &coroapi; 1180 GCoroAPI = &coroapi;
1134 sv_setiv (sv, (IV)&coroapi); 1181 sv_setiv (sv, (IV)&coroapi);
1154 1201
1155 coro->prio = newprio; 1202 coro->prio = newprio;
1156 } 1203 }
1157} 1204}
1158 1205
1159void 1206SV *
1160ready (SV *self) 1207ready (SV *self)
1161 PROTOTYPE: $ 1208 PROTOTYPE: $
1162 CODE: 1209 CODE:
1163 api_ready (self); 1210 RETVAL = boolSV (api_ready (self));
1211 OUTPUT:
1212 RETVAL
1213
1214SV *
1215is_ready (SV *self)
1216 PROTOTYPE: $
1217 CODE:
1218 RETVAL = boolSV (api_is_ready (self));
1219 OUTPUT:
1220 RETVAL
1164 1221
1165int 1222int
1166nready (...) 1223nready (...)
1167 PROTOTYPE: 1224 PROTOTYPE:
1168 CODE: 1225 CODE:
1169 RETVAL = coro_nready; 1226 RETVAL = coro_nready;
1170 OUTPUT: 1227 OUTPUT:
1171 RETVAL 1228 RETVAL
1172 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
1173MODULE = Coro::State PACKAGE = Coro::AIO 1237MODULE = Coro::State PACKAGE = Coro::AIO
1174 1238
1175SV * 1239SV *
1176_get_state () 1240_get_state ()
1177 CODE: 1241 CODE:
1178{ 1242{
1179 struct { 1243 struct io_state *data;
1180 int errorno;
1181 int laststype;
1182 int laststatval;
1183 Stat_t statcache;
1184 } data;
1185 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
1186 data.errorno = errno; 1250 data->errorno = errno;
1187 data.laststype = PL_laststype; 1251 data->laststype = PL_laststype;
1188 data.laststatval = PL_laststatval; 1252 data->laststatval = PL_laststatval;
1189 data.statcache = PL_statcache; 1253 data->statcache = PL_statcache;
1190
1191 RETVAL = newSVpvn ((char *)&data, sizeof data);
1192} 1254}
1193 OUTPUT: 1255 OUTPUT:
1194 RETVAL 1256 RETVAL
1195 1257
1196void 1258void
1197_set_state (char *data_) 1259_set_state (char *data_)
1198 PROTOTYPE: $ 1260 PROTOTYPE: $
1199 CODE: 1261 CODE:
1200{ 1262{
1201 struct { 1263 struct io_state *data = (void *)data_;
1202 int errorno;
1203 int laststype;
1204 int laststatval;
1205 Stat_t statcache;
1206 } *data = (void *)data_;
1207 1264
1208 errno = data->errorno; 1265 errno = data->errorno;
1209 PL_laststype = data->laststype; 1266 PL_laststype = data->laststype;
1210 PL_laststatval = data->laststatval; 1267 PL_laststatval = data->laststatval;
1211 PL_statcache = data->statcache; 1268 PL_statcache = data->statcache;
1212} 1269}
1270

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines