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.127 by root, Tue Dec 12 13:56:45 2006 UTC vs.
Revision 1.144 by root, Sun Mar 4 11:45:23 2007 UTC

31#else 31#else
32# define PAGESIZE 0 32# define PAGESIZE 0
33# define BOOT_PAGESIZE (void)0 33# define BOOT_PAGESIZE (void)0
34#endif 34#endif
35 35
36#if USE_VALGRIND 36#if CORO_USE_VALGRIND
37# include <valgrind/valgrind.h> 37# include <valgrind/valgrind.h>
38# define REGISTER_STACK(cctx,start,end) (cctx)->valgrind_id = VALGRIND_STACK_REGISTER ((start), (end))
39#else
40# define REGISTER_STACK(cctx,start,end)
38#endif 41#endif
39 42
40/* the maximum number of idle cctx that will be pooled */ 43/* the maximum number of idle cctx that will be pooled */
41#define MAX_IDLE_CCTX 8 44#define MAX_IDLE_CCTX 8
42 45
71#ifndef SvRV_set 74#ifndef SvRV_set
72# define SvRV_set(s,v) SvRV(s) = (v) 75# define SvRV_set(s,v) SvRV(s) = (v)
73#endif 76#endif
74 77
75#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64 78#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
76# undef STACKGUARD 79# undef CORO_STACKGUARD
77#endif 80#endif
78 81
79#ifndef STACKGUARD 82#ifndef CORO_STACKGUARD
80# define STACKGUARD 0 83# define CORO_STACKGUARD 0
81#endif 84#endif
82 85
83/* prefer perl internal functions over our own? */ 86/* prefer perl internal functions over our own? */
84#ifndef PREFER_PERL_FUNCTIONS 87#ifndef CORO_PREFER_PERL_FUNCTIONS
85# define PREFER_PERL_FUNCTIONS 0 88# define CORO_PREFER_PERL_FUNCTIONS 0
86#endif 89#endif
87 90
88/* The next macro should declare a variable stacklevel that contains and approximation 91/* The next macro should declare a variable stacklevel that contains and approximation
89 * to the current C stack pointer. Its property is that it changes with each call 92 * to the current C stack pointer. Its property is that it changes with each call
90 * and should be unique. */ 93 * and should be unique. */
93 96
94#define IN_DESTRUCT (PL_main_cv == Nullcv) 97#define IN_DESTRUCT (PL_main_cv == Nullcv)
95 98
96#if __GNUC__ >= 3 99#if __GNUC__ >= 3
97# define attribute(x) __attribute__(x) 100# define attribute(x) __attribute__(x)
101# define BARRIER __asm__ __volatile__ ("" : : : "memory")
98#else 102#else
99# define attribute(x) 103# define attribute(x)
104# define BARRIER
100#endif 105#endif
101 106
102#define NOINLINE attribute ((noinline)) 107#define NOINLINE attribute ((noinline))
103 108
104#include "CoroAPI.h" 109#include "CoroAPI.h"
110#else 115#else
111# define LOCK (void)0 116# define LOCK (void)0
112# define UNLOCK (void)0 117# define UNLOCK (void)0
113#endif 118#endif
114 119
120/* helper storage struct for Coro::AIO */
115struct io_state 121struct io_state
116{ 122{
117 int errorno; 123 int errorno;
118 I32 laststype; 124 I32 laststype;
119 int laststatval; 125 int laststatval;
132typedef struct coro_cctx { 138typedef struct coro_cctx {
133 struct coro_cctx *next; 139 struct coro_cctx *next;
134 140
135 /* the stack */ 141 /* the stack */
136 void *sptr; 142 void *sptr;
137 long ssize; /* positive == mmap, otherwise malloc */ 143 ssize_t ssize; /* positive == mmap, otherwise malloc */
138 144
139 /* cpu state */ 145 /* cpu state */
140 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 146 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
147 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
141 JMPENV *top_env; 148 JMPENV *top_env;
142 coro_context cctx; 149 coro_context cctx;
143 150
144 int inuse; 151 int inuse;
145 152
146#if USE_VALGRIND 153#if CORO_USE_VALGRIND
147 int valgrind_id; 154 int valgrind_id;
148#endif 155#endif
149} coro_cctx; 156} coro_cctx;
150 157
151enum { 158enum {
152 CF_RUNNING = 0x0001, /* coroutine is running */ 159 CF_RUNNING = 0x0001, /* coroutine is running */
153 CF_READY = 0x0002, /* coroutine is ready */ 160 CF_READY = 0x0002, /* coroutine is ready */
154 CF_NEW = 0x0004, /* ahs never been switched to */ 161 CF_NEW = 0x0004, /* has never been switched to */
162 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
155}; 163};
156 164
157/* this is a structure representing a perl-level coroutine */ 165/* this is a structure representing a perl-level coroutine */
158struct coro { 166struct coro {
159 /* the c coroutine allocated to this perl coroutine, if any */ 167 /* the c coroutine allocated to this perl coroutine, if any */
181}; 189};
182 190
183typedef struct coro *Coro__State; 191typedef struct coro *Coro__State;
184typedef struct coro *Coro__State_or_hashref; 192typedef struct coro *Coro__State_or_hashref;
185 193
194/** Coro ********************************************************************/
195
196#define PRIO_MAX 3
197#define PRIO_HIGH 1
198#define PRIO_NORMAL 0
199#define PRIO_LOW -1
200#define PRIO_IDLE -3
201#define PRIO_MIN -4
202
203/* for Coro.pm */
204static SV *coro_current;
205static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
206static int coro_nready;
207
208/** lowlevel stuff **********************************************************/
209
186static AV * 210static AV *
187coro_clone_padlist (CV *cv) 211coro_clone_padlist (CV *cv)
188{ 212{
189 AV *padlist = CvPADLIST (cv); 213 AV *padlist = CvPADLIST (cv);
190 AV *newpadlist, *newpad; 214 AV *newpadlist, *newpad;
264 288
265 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) 289 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
266 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 290 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
267 else 291 else
268 { 292 {
269#if PREFER_PERL_FUNCTIONS 293#if CORO_PREFER_PERL_FUNCTIONS
270 /* this is probably cleaner, but also slower? */ 294 /* this is probably cleaner, but also slower? */
271 CV *cp = Perl_cv_clone (cv); 295 CV *cp = Perl_cv_clone (cv);
272 CvPADLIST (cv) = CvPADLIST (cp); 296 CvPADLIST (cv) = CvPADLIST (cp);
273 CvPADLIST (cp) = 0; 297 CvPADLIST (cp) = 0;
274 SvREFCNT_dec (cp); 298 SvREFCNT_dec (cp);
297 if (AvFILLp (av) >= AvMAX (av)) 321 if (AvFILLp (av) >= AvMAX (av))
298 av_extend (av, AvMAX (av) + 1); 322 av_extend (av, AvMAX (av) + 1);
299 323
300 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 324 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
301} 325}
326
327/** load & save, init *******************************************************/
302 328
303#define SB do { 329#define SB do {
304#define SE } while (0) 330#define SE } while (0)
305 331
306#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE 332#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
339 CvPADLIST (cv) = (AV *)POPs; 365 CvPADLIST (cv) = (AV *)POPs;
340 } 366 }
341 367
342 PUTBACK; 368 PUTBACK;
343 } 369 }
370 assert (!PL_comppad || AvARRAY (PL_comppad));//D
344} 371}
345 372
346static void 373static void
347save_perl (Coro__State c) 374save_perl (Coro__State c)
348{ 375{
376 assert (!PL_comppad || AvARRAY (PL_comppad));//D
349 { 377 {
350 dSP; 378 dSP;
351 I32 cxix = cxstack_ix; 379 I32 cxix = cxstack_ix;
352 PERL_CONTEXT *ccstk = cxstack; 380 PERL_CONTEXT *ccstk = cxstack;
353 PERL_SI *top_si = PL_curstackinfo; 381 PERL_SI *top_si = PL_curstackinfo;
408 * allocate various perl stacks. This is an exact copy 436 * allocate various perl stacks. This is an exact copy
409 * of perl.c:init_stacks, except that it uses less memory 437 * of perl.c:init_stacks, except that it uses less memory
410 * on the (sometimes correct) assumption that coroutines do 438 * on the (sometimes correct) assumption that coroutines do
411 * not usually need a lot of stackspace. 439 * not usually need a lot of stackspace.
412 */ 440 */
413#if PREFER_PERL_FUNCTIONS 441#if CORO_PREFER_PERL_FUNCTIONS
414# define coro_init_stacks init_stacks 442# define coro_init_stacks init_stacks
415#else 443#else
416static void 444static void
417coro_init_stacks () 445coro_init_stacks ()
418{ 446{
468 496
469 /* free all temporaries */ 497 /* free all temporaries */
470 FREETMPS; 498 FREETMPS;
471 assert (PL_tmps_ix == -1); 499 assert (PL_tmps_ix == -1);
472 500
501 /* unwind all extra stacks */
473 POPSTACK_TO (PL_mainstack); 502 POPSTACK_TO (PL_mainstack);
503
504 /* unwind main stack */
505 dounwind (-1);
474 } 506 }
475 507
476 while (PL_curstackinfo->si_next) 508 while (PL_curstackinfo->si_next)
477 PL_curstackinfo = PL_curstackinfo->si_next; 509 PL_curstackinfo = PL_curstackinfo->si_next;
478 510
495#if !PERL_VERSION_ATLEAST (5,9,0) 527#if !PERL_VERSION_ATLEAST (5,9,0)
496 Safefree (PL_retstack); 528 Safefree (PL_retstack);
497#endif 529#endif
498} 530}
499 531
532/** coroutine stack handling ************************************************/
533
500static void 534static void
501setup_coro (struct coro *coro) 535setup_coro (struct coro *coro)
502{ 536{
503 /* 537 /*
504 * emulate part of the perl startup here. 538 * emulate part of the perl startup here.
506 540
507 coro_init_stacks (); 541 coro_init_stacks ();
508 542
509 PL_curcop = &PL_compiling; 543 PL_curcop = &PL_compiling;
510 PL_in_eval = EVAL_NULL; 544 PL_in_eval = EVAL_NULL;
545 PL_comppad = 0;
511 PL_curpm = 0; 546 PL_curpm = 0;
512 PL_localizing = 0; 547 PL_localizing = 0;
513 PL_dirty = 0; 548 PL_dirty = 0;
514 PL_restartop = 0; 549 PL_restartop = 0;
515 550
597 632
598 Newz (0, cctx, 1, coro_cctx); 633 Newz (0, cctx, 1, coro_cctx);
599 634
600#if HAVE_MMAP 635#if HAVE_MMAP
601 636
602 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 637 cctx->ssize = ((CORO_STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
603 /* mmap supposedly does allocate-on-write for us */ 638 /* mmap supposedly does allocate-on-write for us */
604 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 639 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
605 640
606 if (cctx->sptr == (void *)-1) 641 if (cctx->sptr != (void *)-1)
607 {
608 perror ("FATAL: unable to mmap stack for coroutine");
609 _exit (EXIT_FAILURE);
610 } 642 {
611
612# if STACKGUARD 643# if CORO_STACKGUARD
613 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 644 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
614# endif 645# endif
615 646 REGISTER_STACK (
616#else 647 cctx,
617
618 cctx->ssize = STACKSIZE * (long)sizeof (long);
619 New (0, cctx->sptr, STACKSIZE, long);
620
621 if (!cctx->sptr)
622 {
623 perror ("FATAL: unable to malloc stack for coroutine");
624 _exit (EXIT_FAILURE);
625 }
626
627#endif
628
629#if USE_VALGRIND
630 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
631 STACKGUARD * PAGESIZE + (char *)cctx->sptr, 648 CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr,
632 cctx->ssize + (char *)cctx->sptr 649 cctx->ssize + (char *)cctx->sptr
633 ); 650 );
634#endif
635 651
636 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); 652 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
653 }
654 else
655#endif
656 {
657 cctx->ssize = -CORO_STACKSIZE * (long)sizeof (long);
658 New (0, cctx->sptr, CORO_STACKSIZE, long);
659
660 if (!cctx->sptr)
661 {
662 perror ("FATAL: unable to allocate stack for coroutine");
663 _exit (EXIT_FAILURE);
664 }
665
666 REGISTER_STACK (
667 cctx,
668 (char *)cctx->sptr,
669 (char *)cctx->sptr - cctx->ssize
670 );
671
672 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, -cctx->ssize);
673 }
637 674
638 return cctx; 675 return cctx;
639} 676}
640 677
641static void 678static void
644 if (!cctx) 681 if (!cctx)
645 return; 682 return;
646 683
647 --cctx_count; 684 --cctx_count;
648 685
649#if USE_VALGRIND 686#if CORO_USE_VALGRIND
650 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 687 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
651#endif 688#endif
652 689
653#if HAVE_MMAP 690#if HAVE_MMAP
691 if (cctx->ssize > 0)
654 munmap (cctx->sptr, cctx->ssize); 692 munmap (cctx->sptr, cctx->ssize);
655#else 693 else
694#endif
656 Safefree (cctx->sptr); 695 Safefree (cctx->sptr);
657#endif
658 696
659 Safefree (cctx); 697 Safefree (cctx);
660} 698}
661 699
662static coro_cctx * 700static coro_cctx *
696 ++cctx_idle; 734 ++cctx_idle;
697 cctx->next = cctx_first; 735 cctx->next = cctx_first;
698 cctx_first = cctx; 736 cctx_first = cctx;
699} 737}
700 738
739/** coroutine switching *****************************************************/
740
701/* never call directly, always through the coro_state_transfer global variable */ 741/* never call directly, always through the coro_state_transfer global variable */
702static void NOINLINE 742static void NOINLINE
703transfer (struct coro *prev, struct coro *next) 743transfer (struct coro *prev, struct coro *next)
704{ 744{
705 dSTACKLEVEL; 745 dSTACKLEVEL;
706 746
707 /* sometimes transfer is only called to set idle_sp */ 747 /* sometimes transfer is only called to set idle_sp */
708 if (!next) 748 if (!next)
709 { 749 {
710 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 750 ((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 */ 751 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
712 } 752 }
713 else if (prev != next) 753 else if (prev != next)
714 { 754 {
715 coro_cctx *prev__cctx; 755 coro_cctx *prev__cctx;
716 756
727 if (!prev->flags & CF_RUNNING) 767 if (!prev->flags & CF_RUNNING)
728 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); 768 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
729 769
730 if (next->flags & CF_RUNNING) 770 if (next->flags & CF_RUNNING)
731 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 771 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
772
773 if (next->flags & CF_DESTROYED)
774 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
732 775
733 prev->flags &= ~CF_RUNNING; 776 prev->flags &= ~CF_RUNNING;
734 next->flags |= CF_RUNNING; 777 next->flags |= CF_RUNNING;
735 778
736 LOCK; 779 LOCK;
757 800
758 /* possibly "free" the cctx */ 801 /* possibly "free" the cctx */
759 if (prev__cctx->idle_sp == STACKLEVEL) 802 if (prev__cctx->idle_sp == STACKLEVEL)
760 { 803 {
761 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 804 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
762 assert (PL_top_env == prev__cctx->top_env); 805 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
763 806
764 prev->cctx = 0; 807 prev->cctx = 0;
765 808
766 cctx_put (prev__cctx); 809 cctx_put (prev__cctx);
767 prev__cctx->inuse = 0; 810 prev__cctx->inuse = 0;
780 PL_top_env = next->cctx->top_env; 823 PL_top_env = next->cctx->top_env;
781 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 824 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
782 } 825 }
783 826
784 free_coro_mortal (); 827 free_coro_mortal ();
785
786 UNLOCK; 828 UNLOCK;
787 } 829 }
788} 830}
789 831
790struct transfer_args 832struct transfer_args
792 struct coro *prev, *next; 834 struct coro *prev, *next;
793}; 835};
794 836
795#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 837#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
796 838
839/** high level stuff ********************************************************/
840
797static void 841static int
798coro_state_destroy (struct coro *coro) 842coro_state_destroy (struct coro *coro)
799{ 843{
800 if (coro->refcnt--) 844 if (coro->flags & CF_DESTROYED)
801 return; 845 return 0;
846
847 coro->flags |= CF_DESTROYED;
848
849 if (coro->flags & CF_READY)
850 {
851 /* reduce nready, as destroying a ready coro effectively unreadies it */
852 /* alternative: look through all ready queues and remove the coro */
853 LOCK;
854 --coro_nready;
855 UNLOCK;
856 }
857 else
858 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
802 859
803 if (coro->mainstack && coro->mainstack != main_mainstack) 860 if (coro->mainstack && coro->mainstack != main_mainstack)
804 { 861 {
805 struct coro temp; 862 struct coro temp;
863
864 assert (!(coro->flags & CF_RUNNING));
865
806 Zero (&temp, 1, struct coro); 866 Zero (&temp, 1, struct coro);
807 temp.save = CORO_SAVE_ALL; 867 temp.save = CORO_SAVE_ALL;
808 868
809 if (coro->flags & CF_RUNNING) 869 if (coro->flags & CF_RUNNING)
810 croak ("FATAL: tried to destroy currently running coroutine"); 870 croak ("FATAL: tried to destroy currently running coroutine");
819 coro->mainstack = 0; 879 coro->mainstack = 0;
820 } 880 }
821 881
822 cctx_destroy (coro->cctx); 882 cctx_destroy (coro->cctx);
823 SvREFCNT_dec (coro->args); 883 SvREFCNT_dec (coro->args);
824 Safefree (coro); 884
885 return 1;
825} 886}
826 887
827static int 888static int
828coro_state_clear (pTHX_ SV *sv, MAGIC *mg) 889coro_state_free (pTHX_ SV *sv, MAGIC *mg)
829{ 890{
830 struct coro *coro = (struct coro *)mg->mg_ptr; 891 struct coro *coro = (struct coro *)mg->mg_ptr;
831 mg->mg_ptr = 0; 892 mg->mg_ptr = 0;
832 893
894 if (--coro->refcnt < 0)
895 {
833 coro_state_destroy (coro); 896 coro_state_destroy (coro);
897 Safefree (coro);
898 }
834 899
835 return 0; 900 return 0;
836} 901}
837 902
838static int 903static int
845 return 0; 910 return 0;
846} 911}
847 912
848static MGVTBL coro_state_vtbl = { 913static MGVTBL coro_state_vtbl = {
849 0, 0, 0, 0, 914 0, 0, 0, 0,
850 coro_state_clear, 915 coro_state_free,
851 0, 916 0,
852#ifdef MGf_DUP 917#ifdef MGf_DUP
853 coro_state_dup, 918 coro_state_dup,
854#else 919#else
855# define MGf_DUP 0 920# define MGf_DUP 0
871 /* very slow, but rare, check */ 936 /* very slow, but rare, check */
872 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) 937 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
873 croak ("Coro::State object required"); 938 croak ("Coro::State object required");
874 } 939 }
875 940
876 mg = SvMAGIC (coro); 941 mg = CORO_MAGIC (coro);
877 assert (mg->mg_type == PERL_MAGIC_ext);
878 return (struct coro *)mg->mg_ptr; 942 return (struct coro *)mg->mg_ptr;
879} 943}
880 944
881static void 945static void
882prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) 946prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv)
906 return old_save; 970 return old_save;
907} 971}
908 972
909/** Coro ********************************************************************/ 973/** Coro ********************************************************************/
910 974
911#define PRIO_MAX 3
912#define PRIO_HIGH 1
913#define PRIO_NORMAL 0
914#define PRIO_LOW -1
915#define PRIO_IDLE -3
916#define PRIO_MIN -4
917
918/* for Coro.pm */
919static SV *coro_current;
920static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
921static int coro_nready;
922
923static void 975static void
924coro_enq (SV *coro_sv) 976coro_enq (SV *coro_sv)
925{ 977{
926 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 978 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
927 coro_nready++;
928} 979}
929 980
930static SV * 981static SV *
931coro_deq (int min_prio) 982coro_deq (int min_prio)
932{ 983{
936 if (min_prio < 0) 987 if (min_prio < 0)
937 min_prio = 0; 988 min_prio = 0;
938 989
939 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 990 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
940 if (AvFILLp (coro_ready [prio]) >= 0) 991 if (AvFILLp (coro_ready [prio]) >= 0)
941 {
942 coro_nready--;
943 return av_shift (coro_ready [prio]); 992 return av_shift (coro_ready [prio]);
944 }
945 993
946 return 0; 994 return 0;
947} 995}
948 996
949static int 997static int
957 coro = SvSTATE (coro_sv); 1005 coro = SvSTATE (coro_sv);
958 1006
959 if (coro->flags & CF_READY) 1007 if (coro->flags & CF_READY)
960 return 0; 1008 return 0;
961 1009
962#if 0 /* this is actually harmless */
963 if (coro->flags & CF_RUNNING)
964 croak ("Coro::ready called on currently running coroutine");
965#endif
966
967 coro->flags |= CF_READY; 1010 coro->flags |= CF_READY;
968 1011
969 LOCK; 1012 LOCK;
970 coro_enq (SvREFCNT_inc (coro_sv)); 1013 coro_enq (SvREFCNT_inc (coro_sv));
1014 ++coro_nready;
971 UNLOCK; 1015 UNLOCK;
972 1016
973 return 1; 1017 return 1;
974} 1018}
975 1019
976static int 1020static int
977api_is_ready (SV *coro_sv) 1021api_is_ready (SV *coro_sv)
978{ 1022{
979 return !!SvSTATE (coro_sv)->flags & CF_READY; 1023 return !!(SvSTATE (coro_sv)->flags & CF_READY);
980} 1024}
981 1025
982static void 1026static void
983prepare_schedule (struct transfer_args *ta) 1027prepare_schedule (struct transfer_args *ta)
984{ 1028{
985 SV *prev, *next; 1029 SV *prev_sv, *next_sv;
986 1030
987 for (;;) 1031 for (;;)
988 { 1032 {
989 LOCK; 1033 LOCK;
990 next = coro_deq (PRIO_MIN); 1034 next_sv = coro_deq (PRIO_MIN);
1035
1036 /* nothing to schedule: call the idle handler */
1037 if (!next_sv)
1038 {
1039 dSP;
1040 UNLOCK;
1041
1042 ENTER;
1043 SAVETMPS;
1044
1045 PUSHMARK (SP);
1046 PUTBACK;
1047 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1048
1049 FREETMPS;
1050 LEAVE;
1051 continue;
1052 }
1053
1054 ta->next = SvSTATE (next_sv);
1055
1056 /* cannot transfer to destroyed coros, skip and look for next */
1057 if (ta->next->flags & CF_DESTROYED)
1058 {
1059 UNLOCK;
1060 SvREFCNT_dec (next_sv);
1061 /* coro_nready is already taken care of by destroy */
1062 continue;
1063 }
1064
1065 --coro_nready;
991 UNLOCK; 1066 UNLOCK;
992
993 if (next)
994 break; 1067 break;
995
996 {
997 dSP;
998
999 ENTER;
1000 SAVETMPS;
1001
1002 PUSHMARK (SP);
1003 PUTBACK;
1004 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1005
1006 FREETMPS;
1007 LEAVE;
1008 } 1068 }
1009 }
1010
1011 prev = SvRV (coro_current);
1012 SvRV_set (coro_current, next);
1013 1069
1014 /* free this only after the transfer */ 1070 /* free this only after the transfer */
1071 prev_sv = SvRV (coro_current);
1072 SvRV_set (coro_current, next_sv);
1073 ta->prev = SvSTATE (prev_sv);
1074
1075 assert (ta->next->flags & CF_READY);
1076 ta->next->flags &= ~CF_READY;
1077
1015 LOCK; 1078 LOCK;
1016 free_coro_mortal (); 1079 free_coro_mortal ();
1080 coro_mortal = prev_sv;
1017 UNLOCK; 1081 UNLOCK;
1018 coro_mortal = prev;
1019
1020 assert (!SvROK(prev));//D
1021 assert (!SvROK(next));//D
1022
1023 ta->prev = SvSTATE (prev);
1024 ta->next = SvSTATE (next);
1025
1026 assert (ta->next->flags & CF_READY);
1027 ta->next->flags &= ~CF_READY;
1028} 1082}
1029 1083
1030static void 1084static void
1031prepare_cede (struct transfer_args *ta) 1085prepare_cede (struct transfer_args *ta)
1032{ 1086{
1033 api_ready (coro_current); 1087 api_ready (coro_current);
1034
1035 prepare_schedule (ta); 1088 prepare_schedule (ta);
1089}
1090
1091static int
1092prepare_cede_notself (struct transfer_args *ta)
1093{
1094 if (coro_nready)
1095 {
1096 SV *prev = SvRV (coro_current);
1097 prepare_schedule (ta);
1098 api_ready (prev);
1099 return 1;
1100 }
1101 else
1102 return 0;
1036} 1103}
1037 1104
1038static void 1105static void
1039api_schedule (void) 1106api_schedule (void)
1040{ 1107{
1042 1109
1043 prepare_schedule (&ta); 1110 prepare_schedule (&ta);
1044 TRANSFER (ta); 1111 TRANSFER (ta);
1045} 1112}
1046 1113
1047static void 1114static int
1048api_cede (void) 1115api_cede (void)
1049{ 1116{
1050 struct transfer_args ta; 1117 struct transfer_args ta;
1051 1118
1052 prepare_cede (&ta); 1119 prepare_cede (&ta);
1120
1121 if (ta.prev != ta.next)
1122 {
1053 TRANSFER (ta); 1123 TRANSFER (ta);
1124 return 1;
1125 }
1126 else
1127 return 0;
1128}
1129
1130static int
1131api_cede_notself (void)
1132{
1133 struct transfer_args ta;
1134
1135 if (prepare_cede_notself (&ta))
1136 {
1137 TRANSFER (ta);
1138 return 1;
1139 }
1140 else
1141 return 0;
1054} 1142}
1055 1143
1056MODULE = Coro::State PACKAGE = Coro::State 1144MODULE = Coro::State PACKAGE = Coro::State
1057 1145
1058PROTOTYPES: DISABLE 1146PROTOTYPES: DISABLE
1114_set_stacklevel (...) 1202_set_stacklevel (...)
1115 ALIAS: 1203 ALIAS:
1116 Coro::State::transfer = 1 1204 Coro::State::transfer = 1
1117 Coro::schedule = 2 1205 Coro::schedule = 2
1118 Coro::cede = 3 1206 Coro::cede = 3
1207 Coro::cede_notself = 4
1119 CODE: 1208 CODE:
1120{ 1209{
1121 struct transfer_args ta; 1210 struct transfer_args ta;
1122 1211
1123 switch (ix) 1212 switch (ix)
1139 break; 1228 break;
1140 1229
1141 case 3: 1230 case 3:
1142 prepare_cede (&ta); 1231 prepare_cede (&ta);
1143 break; 1232 break;
1233
1234 case 4:
1235 if (!prepare_cede_notself (&ta))
1236 XSRETURN_EMPTY;
1237
1238 break;
1144 } 1239 }
1145 1240
1241 BARRIER;
1146 TRANSFER (ta); 1242 TRANSFER (ta);
1147}
1148 1243
1149void 1244 if (GIMME_V != G_VOID && ta.next != ta.prev)
1150_clone_state_from (SV *dst, SV *src) 1245 XSRETURN_YES;
1246}
1247
1248bool
1249_destroy (SV *coro_sv)
1151 CODE: 1250 CODE:
1152{ 1251 RETVAL = coro_state_destroy (SvSTATE (coro_sv));
1153 struct coro *coro_src = SvSTATE (src); 1252 OUTPUT:
1154 1253 RETVAL
1155 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1156
1157 ++coro_src->refcnt;
1158 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1159}
1160 1254
1161void 1255void
1162_exit (code) 1256_exit (code)
1163 int code 1257 int code
1164 PROTOTYPE: $ 1258 PROTOTYPE: $
1201 coro_ready[i] = newAV (); 1295 coro_ready[i] = newAV ();
1202 1296
1203 { 1297 {
1204 SV *sv = perl_get_sv("Coro::API", 1); 1298 SV *sv = perl_get_sv("Coro::API", 1);
1205 1299
1206 coroapi.schedule = api_schedule; 1300 coroapi.schedule = api_schedule;
1207 coroapi.save = api_save; 1301 coroapi.save = api_save;
1208 coroapi.cede = api_cede; 1302 coroapi.cede = api_cede;
1303 coroapi.cede_notself = api_cede_notself;
1209 coroapi.ready = api_ready; 1304 coroapi.ready = api_ready;
1210 coroapi.is_ready = api_is_ready; 1305 coroapi.is_ready = api_is_ready;
1211 coroapi.nready = &coro_nready; 1306 coroapi.nready = &coro_nready;
1212 coroapi.current = coro_current; 1307 coroapi.current = coro_current;
1213 1308
1214 GCoroAPI = &coroapi; 1309 GCoroAPI = &coroapi;
1215 sv_setiv (sv, (IV)&coroapi); 1310 sv_setiv (sv, (IV)&coroapi);
1216 SvREADONLY_on (sv); 1311 SvREADONLY_on (sv);
1217 } 1312 }
1233 RETVAL = coro->prio; 1328 RETVAL = coro->prio;
1234 1329
1235 if (items > 1) 1330 if (items > 1)
1236 { 1331 {
1237 if (ix) 1332 if (ix)
1238 newprio += coro->prio; 1333 newprio = coro->prio - newprio;
1239 1334
1240 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 1335 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1241 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 1336 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1242 1337
1243 coro->prio = newprio; 1338 coro->prio = newprio;
1244 } 1339 }
1245} 1340}
1341 OUTPUT:
1342 RETVAL
1246 1343
1247SV * 1344SV *
1248ready (SV *self) 1345ready (SV *self)
1249 PROTOTYPE: $ 1346 PROTOTYPE: $
1250 CODE: 1347 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines