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.141 by root, Mon Jan 22 18:45:17 2007 UTC

33# define BOOT_PAGESIZE (void)0 33# define BOOT_PAGESIZE (void)0
34#endif 34#endif
35 35
36#if USE_VALGRIND 36#if 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
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
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 */
180 int prio; 188 int prio;
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;
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 **********************************************************/
185 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);
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}
302 326
327/** load & save, init *******************************************************/
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
307 333
468 494
469 /* free all temporaries */ 495 /* free all temporaries */
470 FREETMPS; 496 FREETMPS;
471 assert (PL_tmps_ix == -1); 497 assert (PL_tmps_ix == -1);
472 498
499 /* unwind all extra stacks */
473 POPSTACK_TO (PL_mainstack); 500 POPSTACK_TO (PL_mainstack);
501
502 /* unwind main stack */
503 dounwind (-1);
474 } 504 }
475 505
476 while (PL_curstackinfo->si_next) 506 while (PL_curstackinfo->si_next)
477 PL_curstackinfo = PL_curstackinfo->si_next; 507 PL_curstackinfo = PL_curstackinfo->si_next;
478 508
494 Safefree (PL_savestack); 524 Safefree (PL_savestack);
495#if !PERL_VERSION_ATLEAST (5,9,0) 525#if !PERL_VERSION_ATLEAST (5,9,0)
496 Safefree (PL_retstack); 526 Safefree (PL_retstack);
497#endif 527#endif
498} 528}
529
530/** coroutine stack handling ************************************************/
499 531
500static void 532static void
501setup_coro (struct coro *coro) 533setup_coro (struct coro *coro)
502{ 534{
503 /* 535 /*
601 633
602 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE; 634 cctx->ssize = ((STACKSIZE * sizeof (long) + PAGESIZE - 1) / PAGESIZE + STACKGUARD) * PAGESIZE;
603 /* mmap supposedly does allocate-on-write for us */ 635 /* 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); 636 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
605 637
606 if (cctx->sptr == (void *)-1) 638 if (cctx->sptr != (void *)-1)
607 {
608 perror ("FATAL: unable to mmap stack for coroutine");
609 _exit (EXIT_FAILURE);
610 } 639 {
611
612# if STACKGUARD 640# if STACKGUARD
613 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE); 641 mprotect (cctx->sptr, STACKGUARD * PAGESIZE, PROT_NONE);
614# endif 642# endif
615 643 REGISTER_STACK (
616#else 644 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, 645 STACKGUARD * PAGESIZE + (char *)cctx->sptr,
632 cctx->ssize + (char *)cctx->sptr 646 cctx->ssize + (char *)cctx->sptr
633 ); 647 );
634#endif
635 648
636 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize); 649 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, cctx->ssize);
650 }
651 else
652#endif
653 {
654 cctx->ssize = -STACKSIZE * (long)sizeof (long);
655 New (0, cctx->sptr, STACKSIZE, long);
656
657 if (!cctx->sptr)
658 {
659 perror ("FATAL: unable to allocate stack for coroutine");
660 _exit (EXIT_FAILURE);
661 }
662
663 REGISTER_STACK (
664 cctx,
665 (char *)cctx->sptr,
666 (char *)cctx->sptr - cctx->ssize
667 );
668
669 coro_create (&cctx->cctx, coro_run, (void *)cctx, cctx->sptr, -cctx->ssize);
670 }
637 671
638 return cctx; 672 return cctx;
639} 673}
640 674
641static void 675static void
649#if USE_VALGRIND 683#if USE_VALGRIND
650 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 684 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
651#endif 685#endif
652 686
653#if HAVE_MMAP 687#if HAVE_MMAP
688 if (cctx->ssize > 0)
654 munmap (cctx->sptr, cctx->ssize); 689 munmap (cctx->sptr, cctx->ssize);
655#else 690 else
691#endif
656 Safefree (cctx->sptr); 692 Safefree (cctx->sptr);
657#endif
658 693
659 Safefree (cctx); 694 Safefree (cctx);
660} 695}
661 696
662static coro_cctx * 697static coro_cctx *
696 ++cctx_idle; 731 ++cctx_idle;
697 cctx->next = cctx_first; 732 cctx->next = cctx_first;
698 cctx_first = cctx; 733 cctx_first = cctx;
699} 734}
700 735
736/** coroutine switching *****************************************************/
737
701/* never call directly, always through the coro_state_transfer global variable */ 738/* never call directly, always through the coro_state_transfer global variable */
702static void NOINLINE 739static void NOINLINE
703transfer (struct coro *prev, struct coro *next) 740transfer (struct coro *prev, struct coro *next)
704{ 741{
705 dSTACKLEVEL; 742 dSTACKLEVEL;
706 743
707 /* sometimes transfer is only called to set idle_sp */ 744 /* sometimes transfer is only called to set idle_sp */
708 if (!next) 745 if (!next)
709 { 746 {
710 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 747 ((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 */ 748 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
712 } 749 }
713 else if (prev != next) 750 else if (prev != next)
714 { 751 {
715 coro_cctx *prev__cctx; 752 coro_cctx *prev__cctx;
716 753
727 if (!prev->flags & CF_RUNNING) 764 if (!prev->flags & CF_RUNNING)
728 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); 765 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
729 766
730 if (next->flags & CF_RUNNING) 767 if (next->flags & CF_RUNNING)
731 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 768 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
769
770 if (next->flags & CF_DESTROYED)
771 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
732 772
733 prev->flags &= ~CF_RUNNING; 773 prev->flags &= ~CF_RUNNING;
734 next->flags |= CF_RUNNING; 774 next->flags |= CF_RUNNING;
735 775
736 LOCK; 776 LOCK;
757 797
758 /* possibly "free" the cctx */ 798 /* possibly "free" the cctx */
759 if (prev__cctx->idle_sp == STACKLEVEL) 799 if (prev__cctx->idle_sp == STACKLEVEL)
760 { 800 {
761 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 801 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
762 assert (PL_top_env == prev__cctx->top_env); 802 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
763 803
764 prev->cctx = 0; 804 prev->cctx = 0;
765 805
766 cctx_put (prev__cctx); 806 cctx_put (prev__cctx);
767 prev__cctx->inuse = 0; 807 prev__cctx->inuse = 0;
780 PL_top_env = next->cctx->top_env; 820 PL_top_env = next->cctx->top_env;
781 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 821 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
782 } 822 }
783 823
784 free_coro_mortal (); 824 free_coro_mortal ();
785
786 UNLOCK; 825 UNLOCK;
787 } 826 }
788} 827}
789 828
790struct transfer_args 829struct transfer_args
792 struct coro *prev, *next; 831 struct coro *prev, *next;
793}; 832};
794 833
795#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 834#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
796 835
836/** high level stuff ********************************************************/
837
797static void 838static int
798coro_state_destroy (struct coro *coro) 839coro_state_destroy (struct coro *coro)
799{ 840{
800 if (coro->refcnt--) 841 if (coro->flags & CF_DESTROYED)
801 return; 842 return 0;
843
844 coro->flags |= CF_DESTROYED;
845
846 if (coro->flags & CF_READY)
847 {
848 /* reduce nready, as destroying a ready coro effectively unreadies it */
849 /* alternative: look through all ready queues and remove the coro */
850 LOCK;
851 --coro_nready;
852 UNLOCK;
853 }
854 else
855 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
802 856
803 if (coro->mainstack && coro->mainstack != main_mainstack) 857 if (coro->mainstack && coro->mainstack != main_mainstack)
804 { 858 {
805 struct coro temp; 859 struct coro temp;
860
861 assert (!(coro->flags & CF_RUNNING));
862
806 Zero (&temp, 1, struct coro); 863 Zero (&temp, 1, struct coro);
807 temp.save = CORO_SAVE_ALL; 864 temp.save = CORO_SAVE_ALL;
808 865
809 if (coro->flags & CF_RUNNING) 866 if (coro->flags & CF_RUNNING)
810 croak ("FATAL: tried to destroy currently running coroutine"); 867 croak ("FATAL: tried to destroy currently running coroutine");
819 coro->mainstack = 0; 876 coro->mainstack = 0;
820 } 877 }
821 878
822 cctx_destroy (coro->cctx); 879 cctx_destroy (coro->cctx);
823 SvREFCNT_dec (coro->args); 880 SvREFCNT_dec (coro->args);
824 Safefree (coro); 881
882 return 1;
825} 883}
826 884
827static int 885static int
828coro_state_clear (pTHX_ SV *sv, MAGIC *mg) 886coro_state_free (pTHX_ SV *sv, MAGIC *mg)
829{ 887{
830 struct coro *coro = (struct coro *)mg->mg_ptr; 888 struct coro *coro = (struct coro *)mg->mg_ptr;
831 mg->mg_ptr = 0; 889 mg->mg_ptr = 0;
832 890
891 if (--coro->refcnt < 0)
892 {
833 coro_state_destroy (coro); 893 coro_state_destroy (coro);
894 Safefree (coro);
895 }
834 896
835 return 0; 897 return 0;
836} 898}
837 899
838static int 900static int
845 return 0; 907 return 0;
846} 908}
847 909
848static MGVTBL coro_state_vtbl = { 910static MGVTBL coro_state_vtbl = {
849 0, 0, 0, 0, 911 0, 0, 0, 0,
850 coro_state_clear, 912 coro_state_free,
851 0, 913 0,
852#ifdef MGf_DUP 914#ifdef MGf_DUP
853 coro_state_dup, 915 coro_state_dup,
854#else 916#else
855# define MGf_DUP 0 917# define MGf_DUP 0
906 return old_save; 968 return old_save;
907} 969}
908 970
909/** Coro ********************************************************************/ 971/** Coro ********************************************************************/
910 972
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 973static void
924coro_enq (SV *coro_sv) 974coro_enq (SV *coro_sv)
925{ 975{
926 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 976 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
927 coro_nready++;
928} 977}
929 978
930static SV * 979static SV *
931coro_deq (int min_prio) 980coro_deq (int min_prio)
932{ 981{
936 if (min_prio < 0) 985 if (min_prio < 0)
937 min_prio = 0; 986 min_prio = 0;
938 987
939 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 988 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
940 if (AvFILLp (coro_ready [prio]) >= 0) 989 if (AvFILLp (coro_ready [prio]) >= 0)
941 {
942 coro_nready--;
943 return av_shift (coro_ready [prio]); 990 return av_shift (coro_ready [prio]);
944 }
945 991
946 return 0; 992 return 0;
947} 993}
948 994
949static int 995static int
957 coro = SvSTATE (coro_sv); 1003 coro = SvSTATE (coro_sv);
958 1004
959 if (coro->flags & CF_READY) 1005 if (coro->flags & CF_READY)
960 return 0; 1006 return 0;
961 1007
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; 1008 coro->flags |= CF_READY;
968 1009
969 LOCK; 1010 LOCK;
970 coro_enq (SvREFCNT_inc (coro_sv)); 1011 coro_enq (SvREFCNT_inc (coro_sv));
1012 ++coro_nready;
971 UNLOCK; 1013 UNLOCK;
972 1014
973 return 1; 1015 return 1;
974} 1016}
975 1017
976static int 1018static int
977api_is_ready (SV *coro_sv) 1019api_is_ready (SV *coro_sv)
978{ 1020{
979 return !!SvSTATE (coro_sv)->flags & CF_READY; 1021 return !!(SvSTATE (coro_sv)->flags & CF_READY);
980} 1022}
981 1023
982static void 1024static void
983prepare_schedule (struct transfer_args *ta) 1025prepare_schedule (struct transfer_args *ta)
984{ 1026{
985 SV *prev, *next; 1027 SV *prev_sv, *next_sv;
986 1028
987 for (;;) 1029 for (;;)
988 { 1030 {
989 LOCK; 1031 LOCK;
990 next = coro_deq (PRIO_MIN); 1032 next_sv = coro_deq (PRIO_MIN);
1033
1034 /* nothing to schedule: call the idle handler */
1035 if (!next_sv)
1036 {
1037 dSP;
1038 UNLOCK;
1039
1040 ENTER;
1041 SAVETMPS;
1042
1043 PUSHMARK (SP);
1044 PUTBACK;
1045 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1046
1047 FREETMPS;
1048 LEAVE;
1049 continue;
1050 }
1051
1052 ta->next = SvSTATE (next_sv);
1053
1054 /* cannot transfer to destroyed coros, skip and look for next */
1055 if (ta->next->flags & CF_DESTROYED)
1056 {
1057 UNLOCK;
1058 SvREFCNT_dec (next_sv);
1059 /* coro_nready is already taken care of by destroy */
1060 continue;
1061 }
1062
1063 --coro_nready;
991 UNLOCK; 1064 UNLOCK;
992
993 if (next)
994 break; 1065 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 } 1066 }
1009 }
1010
1011 prev = SvRV (coro_current);
1012 SvRV_set (coro_current, next);
1013 1067
1014 /* free this only after the transfer */ 1068 /* free this only after the transfer */
1069 prev_sv = SvRV (coro_current);
1070 SvRV_set (coro_current, next_sv);
1071 ta->prev = SvSTATE (prev_sv);
1072
1073 assert (ta->next->flags & CF_READY);
1074 ta->next->flags &= ~CF_READY;
1075
1015 LOCK; 1076 LOCK;
1016 free_coro_mortal (); 1077 free_coro_mortal ();
1078 coro_mortal = prev_sv;
1017 UNLOCK; 1079 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} 1080}
1029 1081
1030static void 1082static void
1031prepare_cede (struct transfer_args *ta) 1083prepare_cede (struct transfer_args *ta)
1032{ 1084{
1033 api_ready (coro_current); 1085 api_ready (coro_current);
1034
1035 prepare_schedule (ta); 1086 prepare_schedule (ta);
1087}
1088
1089static int
1090prepare_cede_notself (struct transfer_args *ta)
1091{
1092 if (coro_nready)
1093 {
1094 SV *prev = SvRV (coro_current);
1095 prepare_schedule (ta);
1096 api_ready (prev);
1097 return 1;
1098 }
1099 else
1100 return 0;
1036} 1101}
1037 1102
1038static void 1103static void
1039api_schedule (void) 1104api_schedule (void)
1040{ 1105{
1042 1107
1043 prepare_schedule (&ta); 1108 prepare_schedule (&ta);
1044 TRANSFER (ta); 1109 TRANSFER (ta);
1045} 1110}
1046 1111
1047static void 1112static int
1048api_cede (void) 1113api_cede (void)
1049{ 1114{
1050 struct transfer_args ta; 1115 struct transfer_args ta;
1051 1116
1052 prepare_cede (&ta); 1117 prepare_cede (&ta);
1118
1119 if (ta.prev != ta.next)
1120 {
1053 TRANSFER (ta); 1121 TRANSFER (ta);
1122 return 1;
1123 }
1124 else
1125 return 0;
1126}
1127
1128static int
1129api_cede_notself (void)
1130{
1131 struct transfer_args ta;
1132
1133 if (prepare_cede_notself (&ta))
1134 {
1135 TRANSFER (ta);
1136 return 1;
1137 }
1138 else
1139 return 0;
1054} 1140}
1055 1141
1056MODULE = Coro::State PACKAGE = Coro::State 1142MODULE = Coro::State PACKAGE = Coro::State
1057 1143
1058PROTOTYPES: DISABLE 1144PROTOTYPES: DISABLE
1114_set_stacklevel (...) 1200_set_stacklevel (...)
1115 ALIAS: 1201 ALIAS:
1116 Coro::State::transfer = 1 1202 Coro::State::transfer = 1
1117 Coro::schedule = 2 1203 Coro::schedule = 2
1118 Coro::cede = 3 1204 Coro::cede = 3
1205 Coro::cede_notself = 4
1119 CODE: 1206 CODE:
1120{ 1207{
1121 struct transfer_args ta; 1208 struct transfer_args ta;
1122 1209
1123 switch (ix) 1210 switch (ix)
1139 break; 1226 break;
1140 1227
1141 case 3: 1228 case 3:
1142 prepare_cede (&ta); 1229 prepare_cede (&ta);
1143 break; 1230 break;
1231
1232 case 4:
1233 if (!prepare_cede_notself (&ta))
1234 XSRETURN_EMPTY;
1235
1236 break;
1144 } 1237 }
1145 1238
1239 BARRIER;
1146 TRANSFER (ta); 1240 TRANSFER (ta);
1147}
1148 1241
1149void 1242 if (GIMME_V != G_VOID && ta.next != ta.prev)
1150_clone_state_from (SV *dst, SV *src) 1243 XSRETURN_YES;
1244}
1245
1246bool
1247_destroy (SV *coro_sv)
1151 CODE: 1248 CODE:
1152{ 1249 RETVAL = coro_state_destroy (SvSTATE (coro_sv));
1153 struct coro *coro_src = SvSTATE (src); 1250 OUTPUT:
1154 1251 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 1252
1161void 1253void
1162_exit (code) 1254_exit (code)
1163 int code 1255 int code
1164 PROTOTYPE: $ 1256 PROTOTYPE: $
1201 coro_ready[i] = newAV (); 1293 coro_ready[i] = newAV ();
1202 1294
1203 { 1295 {
1204 SV *sv = perl_get_sv("Coro::API", 1); 1296 SV *sv = perl_get_sv("Coro::API", 1);
1205 1297
1206 coroapi.schedule = api_schedule; 1298 coroapi.schedule = api_schedule;
1207 coroapi.save = api_save; 1299 coroapi.save = api_save;
1208 coroapi.cede = api_cede; 1300 coroapi.cede = api_cede;
1301 coroapi.cede_notself = api_cede_notself;
1209 coroapi.ready = api_ready; 1302 coroapi.ready = api_ready;
1210 coroapi.is_ready = api_is_ready; 1303 coroapi.is_ready = api_is_ready;
1211 coroapi.nready = &coro_nready; 1304 coroapi.nready = &coro_nready;
1212 coroapi.current = coro_current; 1305 coroapi.current = coro_current;
1213 1306
1214 GCoroAPI = &coroapi; 1307 GCoroAPI = &coroapi;
1215 sv_setiv (sv, (IV)&coroapi); 1308 sv_setiv (sv, (IV)&coroapi);
1216 SvREADONLY_on (sv); 1309 SvREADONLY_on (sv);
1217 } 1310 }
1233 RETVAL = coro->prio; 1326 RETVAL = coro->prio;
1234 1327
1235 if (items > 1) 1328 if (items > 1)
1236 { 1329 {
1237 if (ix) 1330 if (ix)
1238 newprio += coro->prio; 1331 newprio = coro->prio - newprio;
1239 1332
1240 if (newprio < PRIO_MIN) newprio = PRIO_MIN; 1333 if (newprio < PRIO_MIN) newprio = PRIO_MIN;
1241 if (newprio > PRIO_MAX) newprio = PRIO_MAX; 1334 if (newprio > PRIO_MAX) newprio = PRIO_MAX;
1242 1335
1243 coro->prio = newprio; 1336 coro->prio = newprio;
1244 } 1337 }
1245} 1338}
1339 OUTPUT:
1340 RETVAL
1246 1341
1247SV * 1342SV *
1248ready (SV *self) 1343ready (SV *self)
1249 PROTOTYPE: $ 1344 PROTOTYPE: $
1250 CODE: 1345 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines