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.131 by root, Fri Dec 29 11:37:49 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. */
112#else 115#else
113# define LOCK (void)0 116# define LOCK (void)0
114# define UNLOCK (void)0 117# define UNLOCK (void)0
115#endif 118#endif
116 119
120/* helper storage struct for Coro::AIO */
117struct io_state 121struct io_state
118{ 122{
119 int errorno; 123 int errorno;
120 I32 laststype; 124 I32 laststype;
121 int laststatval; 125 int laststatval;
134typedef struct coro_cctx { 138typedef struct coro_cctx {
135 struct coro_cctx *next; 139 struct coro_cctx *next;
136 140
137 /* the stack */ 141 /* the stack */
138 void *sptr; 142 void *sptr;
139 long ssize; /* positive == mmap, otherwise malloc */ 143 ssize_t ssize; /* positive == mmap, otherwise malloc */
140 144
141 /* cpu state */ 145 /* cpu state */
142 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 */
143 JMPENV *top_env; 148 JMPENV *top_env;
144 coro_context cctx; 149 coro_context cctx;
145 150
146 int inuse; 151 int inuse;
147 152
148#if USE_VALGRIND 153#if CORO_USE_VALGRIND
149 int valgrind_id; 154 int valgrind_id;
150#endif 155#endif
151} coro_cctx; 156} coro_cctx;
152 157
153enum { 158enum {
154 CF_RUNNING = 0x0001, /* coroutine is running */ 159 CF_RUNNING = 0x0001, /* coroutine is running */
155 CF_READY = 0x0002, /* coroutine is ready */ 160 CF_READY = 0x0002, /* coroutine is ready */
156 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 */
157}; 163};
158 164
159/* this is a structure representing a perl-level coroutine */ 165/* this is a structure representing a perl-level coroutine */
160struct coro { 166struct coro {
161 /* the c coroutine allocated to this perl coroutine, if any */ 167 /* the c coroutine allocated to this perl coroutine, if any */
183}; 189};
184 190
185typedef struct coro *Coro__State; 191typedef struct coro *Coro__State;
186typedef struct coro *Coro__State_or_hashref; 192typedef struct coro *Coro__State_or_hashref;
187 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
188static AV * 210static AV *
189coro_clone_padlist (CV *cv) 211coro_clone_padlist (CV *cv)
190{ 212{
191 AV *padlist = CvPADLIST (cv); 213 AV *padlist = CvPADLIST (cv);
192 AV *newpadlist, *newpad; 214 AV *newpadlist, *newpad;
266 288
267 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0) 289 if (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)
268 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 290 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
269 else 291 else
270 { 292 {
271#if PREFER_PERL_FUNCTIONS 293#if CORO_PREFER_PERL_FUNCTIONS
272 /* this is probably cleaner, but also slower? */ 294 /* this is probably cleaner, but also slower? */
273 CV *cp = Perl_cv_clone (cv); 295 CV *cp = Perl_cv_clone (cv);
274 CvPADLIST (cv) = CvPADLIST (cp); 296 CvPADLIST (cv) = CvPADLIST (cp);
275 CvPADLIST (cp) = 0; 297 CvPADLIST (cp) = 0;
276 SvREFCNT_dec (cp); 298 SvREFCNT_dec (cp);
299 if (AvFILLp (av) >= AvMAX (av)) 321 if (AvFILLp (av) >= AvMAX (av))
300 av_extend (av, AvMAX (av) + 1); 322 av_extend (av, AvMAX (av) + 1);
301 323
302 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 324 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
303} 325}
326
327/** load & save, init *******************************************************/
304 328
305#define SB do { 329#define SB do {
306#define SE } while (0) 330#define SE } while (0)
307 331
308#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
341 CvPADLIST (cv) = (AV *)POPs; 365 CvPADLIST (cv) = (AV *)POPs;
342 } 366 }
343 367
344 PUTBACK; 368 PUTBACK;
345 } 369 }
370 assert (!PL_comppad || AvARRAY (PL_comppad));//D
346} 371}
347 372
348static void 373static void
349save_perl (Coro__State c) 374save_perl (Coro__State c)
350{ 375{
376 assert (!PL_comppad || AvARRAY (PL_comppad));//D
351 { 377 {
352 dSP; 378 dSP;
353 I32 cxix = cxstack_ix; 379 I32 cxix = cxstack_ix;
354 PERL_CONTEXT *ccstk = cxstack; 380 PERL_CONTEXT *ccstk = cxstack;
355 PERL_SI *top_si = PL_curstackinfo; 381 PERL_SI *top_si = PL_curstackinfo;
410 * allocate various perl stacks. This is an exact copy 436 * allocate various perl stacks. This is an exact copy
411 * of perl.c:init_stacks, except that it uses less memory 437 * of perl.c:init_stacks, except that it uses less memory
412 * on the (sometimes correct) assumption that coroutines do 438 * on the (sometimes correct) assumption that coroutines do
413 * not usually need a lot of stackspace. 439 * not usually need a lot of stackspace.
414 */ 440 */
415#if PREFER_PERL_FUNCTIONS 441#if CORO_PREFER_PERL_FUNCTIONS
416# define coro_init_stacks init_stacks 442# define coro_init_stacks init_stacks
417#else 443#else
418static void 444static void
419coro_init_stacks () 445coro_init_stacks ()
420{ 446{
470 496
471 /* free all temporaries */ 497 /* free all temporaries */
472 FREETMPS; 498 FREETMPS;
473 assert (PL_tmps_ix == -1); 499 assert (PL_tmps_ix == -1);
474 500
501 /* unwind all extra stacks */
475 POPSTACK_TO (PL_mainstack); 502 POPSTACK_TO (PL_mainstack);
503
504 /* unwind main stack */
505 dounwind (-1);
476 } 506 }
477 507
478 while (PL_curstackinfo->si_next) 508 while (PL_curstackinfo->si_next)
479 PL_curstackinfo = PL_curstackinfo->si_next; 509 PL_curstackinfo = PL_curstackinfo->si_next;
480 510
497#if !PERL_VERSION_ATLEAST (5,9,0) 527#if !PERL_VERSION_ATLEAST (5,9,0)
498 Safefree (PL_retstack); 528 Safefree (PL_retstack);
499#endif 529#endif
500} 530}
501 531
532/** coroutine stack handling ************************************************/
533
502static void 534static void
503setup_coro (struct coro *coro) 535setup_coro (struct coro *coro)
504{ 536{
505 /* 537 /*
506 * emulate part of the perl startup here. 538 * emulate part of the perl startup here.
508 540
509 coro_init_stacks (); 541 coro_init_stacks ();
510 542
511 PL_curcop = &PL_compiling; 543 PL_curcop = &PL_compiling;
512 PL_in_eval = EVAL_NULL; 544 PL_in_eval = EVAL_NULL;
545 PL_comppad = 0;
513 PL_curpm = 0; 546 PL_curpm = 0;
514 PL_localizing = 0; 547 PL_localizing = 0;
515 PL_dirty = 0; 548 PL_dirty = 0;
516 PL_restartop = 0; 549 PL_restartop = 0;
517 550
599 632
600 Newz (0, cctx, 1, coro_cctx); 633 Newz (0, cctx, 1, coro_cctx);
601 634
602#if HAVE_MMAP 635#if HAVE_MMAP
603 636
604 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 637 cctx->ssize = ((CORO_STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
605 /* mmap supposedly does allocate-on-write for us */ 638 /* mmap supposedly does allocate-on-write for us */
606 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);
607 640
608 if (cctx->sptr == (void *)-1) 641 if (cctx->sptr != (void *)-1)
609 {
610 perror ("FATAL: unable to mmap stack for coroutine");
611 _exit (EXIT_FAILURE);
612 } 642 {
613
614# if STACKGUARD 643# if CORO_STACKGUARD
615 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 644 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
616# endif 645# endif
617 646 REGISTER_STACK (
618#else 647 cctx,
619
620 cctx->ssize = STACKSIZE * (long)sizeof (long);
621 New (0, cctx->sptr, STACKSIZE, long);
622
623 if (!cctx->sptr)
624 {
625 perror ("FATAL: unable to malloc stack for coroutine");
626 _exit (EXIT_FAILURE);
627 }
628
629#endif
630
631#if USE_VALGRIND
632 cctx->valgrind_id = VALGRIND_STACK_REGISTER (
633 STACKGUARD * PAGESIZE + (char *)cctx->sptr, 648 CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr,
634 cctx->ssize + (char *)cctx->sptr 649 cctx->ssize + (char *)cctx->sptr
635 ); 650 );
636#endif
637 651
638 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 }
639 674
640 return cctx; 675 return cctx;
641} 676}
642 677
643static void 678static void
646 if (!cctx) 681 if (!cctx)
647 return; 682 return;
648 683
649 --cctx_count; 684 --cctx_count;
650 685
651#if USE_VALGRIND 686#if CORO_USE_VALGRIND
652 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 687 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
653#endif 688#endif
654 689
655#if HAVE_MMAP 690#if HAVE_MMAP
691 if (cctx->ssize > 0)
656 munmap (cctx->sptr, cctx->ssize); 692 munmap (cctx->sptr, cctx->ssize);
657#else 693 else
694#endif
658 Safefree (cctx->sptr); 695 Safefree (cctx->sptr);
659#endif
660 696
661 Safefree (cctx); 697 Safefree (cctx);
662} 698}
663 699
664static coro_cctx * 700static coro_cctx *
698 ++cctx_idle; 734 ++cctx_idle;
699 cctx->next = cctx_first; 735 cctx->next = cctx_first;
700 cctx_first = cctx; 736 cctx_first = cctx;
701} 737}
702 738
739/** coroutine switching *****************************************************/
740
703/* never call directly, always through the coro_state_transfer global variable */ 741/* never call directly, always through the coro_state_transfer global variable */
704static void NOINLINE 742static void NOINLINE
705transfer (struct coro *prev, struct coro *next) 743transfer (struct coro *prev, struct coro *next)
706{ 744{
707 dSTACKLEVEL; 745 dSTACKLEVEL;
708 746
709 /* sometimes transfer is only called to set idle_sp */ 747 /* sometimes transfer is only called to set idle_sp */
710 if (!next) 748 if (!next)
711 { 749 {
712 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 750 ((coro_cctx *)prev)->idle_sp = STACKLEVEL;
713 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 */
714 } 752 }
715 else if (prev != next) 753 else if (prev != next)
716 { 754 {
717 coro_cctx *prev__cctx; 755 coro_cctx *prev__cctx;
718 756
729 if (!prev->flags & CF_RUNNING) 767 if (!prev->flags & CF_RUNNING)
730 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");
731 769
732 if (next->flags & CF_RUNNING) 770 if (next->flags & CF_RUNNING)
733 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");
734 775
735 prev->flags &= ~CF_RUNNING; 776 prev->flags &= ~CF_RUNNING;
736 next->flags |= CF_RUNNING; 777 next->flags |= CF_RUNNING;
737 778
738 LOCK; 779 LOCK;
759 800
760 /* possibly "free" the cctx */ 801 /* possibly "free" the cctx */
761 if (prev__cctx->idle_sp == STACKLEVEL) 802 if (prev__cctx->idle_sp == STACKLEVEL)
762 { 803 {
763 /* 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 */
764 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));
765 806
766 prev->cctx = 0; 807 prev->cctx = 0;
767 808
768 cctx_put (prev__cctx); 809 cctx_put (prev__cctx);
769 prev__cctx->inuse = 0; 810 prev__cctx->inuse = 0;
782 PL_top_env = next->cctx->top_env; 823 PL_top_env = next->cctx->top_env;
783 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 824 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
784 } 825 }
785 826
786 free_coro_mortal (); 827 free_coro_mortal ();
787
788 UNLOCK; 828 UNLOCK;
789 } 829 }
790} 830}
791 831
792struct transfer_args 832struct transfer_args
794 struct coro *prev, *next; 834 struct coro *prev, *next;
795}; 835};
796 836
797#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 837#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
798 838
839/** high level stuff ********************************************************/
840
799static void 841static int
800coro_state_destroy (struct coro *coro) 842coro_state_destroy (struct coro *coro)
801{ 843{
802 if (coro->refcnt--) 844 if (coro->flags & CF_DESTROYED)
803 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 */
804 859
805 if (coro->mainstack && coro->mainstack != main_mainstack) 860 if (coro->mainstack && coro->mainstack != main_mainstack)
806 { 861 {
807 struct coro temp; 862 struct coro temp;
863
864 assert (!(coro->flags & CF_RUNNING));
865
808 Zero (&temp, 1, struct coro); 866 Zero (&temp, 1, struct coro);
809 temp.save = CORO_SAVE_ALL; 867 temp.save = CORO_SAVE_ALL;
810 868
811 if (coro->flags & CF_RUNNING) 869 if (coro->flags & CF_RUNNING)
812 croak ("FATAL: tried to destroy currently running coroutine"); 870 croak ("FATAL: tried to destroy currently running coroutine");
821 coro->mainstack = 0; 879 coro->mainstack = 0;
822 } 880 }
823 881
824 cctx_destroy (coro->cctx); 882 cctx_destroy (coro->cctx);
825 SvREFCNT_dec (coro->args); 883 SvREFCNT_dec (coro->args);
826 Safefree (coro); 884
885 return 1;
827} 886}
828 887
829static int 888static int
830coro_state_clear (pTHX_ SV *sv, MAGIC *mg) 889coro_state_free (pTHX_ SV *sv, MAGIC *mg)
831{ 890{
832 struct coro *coro = (struct coro *)mg->mg_ptr; 891 struct coro *coro = (struct coro *)mg->mg_ptr;
833 mg->mg_ptr = 0; 892 mg->mg_ptr = 0;
834 893
894 if (--coro->refcnt < 0)
895 {
835 coro_state_destroy (coro); 896 coro_state_destroy (coro);
897 Safefree (coro);
898 }
836 899
837 return 0; 900 return 0;
838} 901}
839 902
840static int 903static int
847 return 0; 910 return 0;
848} 911}
849 912
850static MGVTBL coro_state_vtbl = { 913static MGVTBL coro_state_vtbl = {
851 0, 0, 0, 0, 914 0, 0, 0, 0,
852 coro_state_clear, 915 coro_state_free,
853 0, 916 0,
854#ifdef MGf_DUP 917#ifdef MGf_DUP
855 coro_state_dup, 918 coro_state_dup,
856#else 919#else
857# define MGf_DUP 0 920# define MGf_DUP 0
873 /* very slow, but rare, check */ 936 /* very slow, but rare, check */
874 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) 937 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
875 croak ("Coro::State object required"); 938 croak ("Coro::State object required");
876 } 939 }
877 940
878 mg = SvMAGIC (coro); 941 mg = CORO_MAGIC (coro);
879 assert (mg->mg_type == PERL_MAGIC_ext);
880 return (struct coro *)mg->mg_ptr; 942 return (struct coro *)mg->mg_ptr;
881} 943}
882 944
883static void 945static void
884prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv) 946prepare_transfer (struct transfer_args *ta, SV *prev_sv, SV *next_sv)
908 return old_save; 970 return old_save;
909} 971}
910 972
911/** Coro ********************************************************************/ 973/** Coro ********************************************************************/
912 974
913#define PRIO_MAX 3
914#define PRIO_HIGH 1
915#define PRIO_NORMAL 0
916#define PRIO_LOW -1
917#define PRIO_IDLE -3
918#define PRIO_MIN -4
919
920/* for Coro.pm */
921static SV *coro_current;
922static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
923static int coro_nready;
924
925static void 975static void
926coro_enq (SV *coro_sv) 976coro_enq (SV *coro_sv)
927{ 977{
928 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);
929 coro_nready++;
930} 979}
931 980
932static SV * 981static SV *
933coro_deq (int min_prio) 982coro_deq (int min_prio)
934{ 983{
938 if (min_prio < 0) 987 if (min_prio < 0)
939 min_prio = 0; 988 min_prio = 0;
940 989
941 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 990 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
942 if (AvFILLp (coro_ready [prio]) >= 0) 991 if (AvFILLp (coro_ready [prio]) >= 0)
943 {
944 coro_nready--;
945 return av_shift (coro_ready [prio]); 992 return av_shift (coro_ready [prio]);
946 }
947 993
948 return 0; 994 return 0;
949} 995}
950 996
951static int 997static int
963 1009
964 coro->flags |= CF_READY; 1010 coro->flags |= CF_READY;
965 1011
966 LOCK; 1012 LOCK;
967 coro_enq (SvREFCNT_inc (coro_sv)); 1013 coro_enq (SvREFCNT_inc (coro_sv));
1014 ++coro_nready;
968 UNLOCK; 1015 UNLOCK;
969 1016
970 return 1; 1017 return 1;
971} 1018}
972 1019
977} 1024}
978 1025
979static void 1026static void
980prepare_schedule (struct transfer_args *ta) 1027prepare_schedule (struct transfer_args *ta)
981{ 1028{
982 SV *prev, *next; 1029 SV *prev_sv, *next_sv;
983 1030
984 for (;;) 1031 for (;;)
985 { 1032 {
986 LOCK; 1033 LOCK;
987 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;
988 UNLOCK; 1066 UNLOCK;
989
990 if (next)
991 break; 1067 break;
992
993 {
994 dSP;
995
996 ENTER;
997 SAVETMPS;
998
999 PUSHMARK (SP);
1000 PUTBACK;
1001 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1002
1003 FREETMPS;
1004 LEAVE;
1005 } 1068 }
1006 }
1007
1008 prev = SvRV (coro_current);
1009 SvRV_set (coro_current, next);
1010 1069
1011 /* 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
1012 LOCK; 1078 LOCK;
1013 free_coro_mortal (); 1079 free_coro_mortal ();
1080 coro_mortal = prev_sv;
1014 UNLOCK; 1081 UNLOCK;
1015 coro_mortal = prev;
1016
1017 assert (!SvROK(prev));//D
1018 assert (!SvROK(next));//D
1019
1020 ta->prev = SvSTATE (prev);
1021 ta->next = SvSTATE (next);
1022
1023 assert (ta->next->flags & CF_READY);
1024 ta->next->flags &= ~CF_READY;
1025} 1082}
1026 1083
1027static void 1084static void
1028prepare_cede (struct transfer_args *ta) 1085prepare_cede (struct transfer_args *ta)
1029{ 1086{
1181 break; 1238 break;
1182 } 1239 }
1183 1240
1184 BARRIER; 1241 BARRIER;
1185 TRANSFER (ta); 1242 TRANSFER (ta);
1186}
1187 1243
1188void 1244 if (GIMME_V != G_VOID && ta.next != ta.prev)
1189_clone_state_from (SV *dst, SV *src) 1245 XSRETURN_YES;
1246}
1247
1248bool
1249_destroy (SV *coro_sv)
1190 CODE: 1250 CODE:
1191{ 1251 RETVAL = coro_state_destroy (SvSTATE (coro_sv));
1192 struct coro *coro_src = SvSTATE (src); 1252 OUTPUT:
1193 1253 RETVAL
1194 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1195
1196 ++coro_src->refcnt;
1197 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1198}
1199 1254
1200void 1255void
1201_exit (code) 1256_exit (code)
1202 int code 1257 int code
1203 PROTOTYPE: $ 1258 PROTOTYPE: $

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines