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.137 by root, Sun Jan 14 20:47:53 2007 UTC

112#else 112#else
113# define LOCK (void)0 113# define LOCK (void)0
114# define UNLOCK (void)0 114# define UNLOCK (void)0
115#endif 115#endif
116 116
117/* helper storage struct for Coro::AIO */
117struct io_state 118struct io_state
118{ 119{
119 int errorno; 120 int errorno;
120 I32 laststype; 121 I32 laststype;
121 int laststatval; 122 int laststatval;
137 /* the stack */ 138 /* the stack */
138 void *sptr; 139 void *sptr;
139 long ssize; /* positive == mmap, otherwise malloc */ 140 long ssize; /* positive == mmap, otherwise malloc */
140 141
141 /* cpu state */ 142 /* cpu state */
142 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 143 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
144 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
143 JMPENV *top_env; 145 JMPENV *top_env;
144 coro_context cctx; 146 coro_context cctx;
145 147
146 int inuse; 148 int inuse;
147 149
149 int valgrind_id; 151 int valgrind_id;
150#endif 152#endif
151} coro_cctx; 153} coro_cctx;
152 154
153enum { 155enum {
154 CF_RUNNING = 0x0001, /* coroutine is running */ 156 CF_RUNNING = 0x0001, /* coroutine is running */
155 CF_READY = 0x0002, /* coroutine is ready */ 157 CF_READY = 0x0002, /* coroutine is ready */
156 CF_NEW = 0x0004, /* ahs never been switched to */ 158 CF_NEW = 0x0004, /* has never been switched to */
159 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
157}; 160};
158 161
159/* this is a structure representing a perl-level coroutine */ 162/* this is a structure representing a perl-level coroutine */
160struct coro { 163struct coro {
161 /* the c coroutine allocated to this perl coroutine, if any */ 164 /* the c coroutine allocated to this perl coroutine, if any */
182 int prio; 185 int prio;
183}; 186};
184 187
185typedef struct coro *Coro__State; 188typedef struct coro *Coro__State;
186typedef struct coro *Coro__State_or_hashref; 189typedef struct coro *Coro__State_or_hashref;
190
191/** Coro ********************************************************************/
192
193#define PRIO_MAX 3
194#define PRIO_HIGH 1
195#define PRIO_NORMAL 0
196#define PRIO_LOW -1
197#define PRIO_IDLE -3
198#define PRIO_MIN -4
199
200/* for Coro.pm */
201static SV *coro_current;
202static AV *coro_ready [PRIO_MAX-PRIO_MIN+1];
203static int coro_nready;
204
205/** lowlevel stuff **********************************************************/
187 206
188static AV * 207static AV *
189coro_clone_padlist (CV *cv) 208coro_clone_padlist (CV *cv)
190{ 209{
191 AV *padlist = CvPADLIST (cv); 210 AV *padlist = CvPADLIST (cv);
300 av_extend (av, AvMAX (av) + 1); 319 av_extend (av, AvMAX (av) + 1);
301 320
302 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 321 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
303} 322}
304 323
324/** load & save, init *******************************************************/
325
305#define SB do { 326#define SB do {
306#define SE } while (0) 327#define SE } while (0)
307 328
308#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE 329#define REPLACE_SV(sv,val) SB SvREFCNT_dec (sv); (sv) = (val); (val) = 0; SE
309 330
470 491
471 /* free all temporaries */ 492 /* free all temporaries */
472 FREETMPS; 493 FREETMPS;
473 assert (PL_tmps_ix == -1); 494 assert (PL_tmps_ix == -1);
474 495
496 /* unwind all extra stacks */
475 POPSTACK_TO (PL_mainstack); 497 POPSTACK_TO (PL_mainstack);
498
499 /* unwind main stack */
500 dounwind (-1);
476 } 501 }
477 502
478 while (PL_curstackinfo->si_next) 503 while (PL_curstackinfo->si_next)
479 PL_curstackinfo = PL_curstackinfo->si_next; 504 PL_curstackinfo = PL_curstackinfo->si_next;
480 505
496 Safefree (PL_savestack); 521 Safefree (PL_savestack);
497#if !PERL_VERSION_ATLEAST (5,9,0) 522#if !PERL_VERSION_ATLEAST (5,9,0)
498 Safefree (PL_retstack); 523 Safefree (PL_retstack);
499#endif 524#endif
500} 525}
526
527/** coroutine stack handling ************************************************/
501 528
502static void 529static void
503setup_coro (struct coro *coro) 530setup_coro (struct coro *coro)
504{ 531{
505 /* 532 /*
698 ++cctx_idle; 725 ++cctx_idle;
699 cctx->next = cctx_first; 726 cctx->next = cctx_first;
700 cctx_first = cctx; 727 cctx_first = cctx;
701} 728}
702 729
730/** coroutine switching *****************************************************/
731
703/* never call directly, always through the coro_state_transfer global variable */ 732/* never call directly, always through the coro_state_transfer global variable */
704static void NOINLINE 733static void NOINLINE
705transfer (struct coro *prev, struct coro *next) 734transfer (struct coro *prev, struct coro *next)
706{ 735{
707 dSTACKLEVEL; 736 dSTACKLEVEL;
708 737
709 /* sometimes transfer is only called to set idle_sp */ 738 /* sometimes transfer is only called to set idle_sp */
710 if (!next) 739 if (!next)
711 { 740 {
712 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 741 ((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 */ 742 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
714 } 743 }
715 else if (prev != next) 744 else if (prev != next)
716 { 745 {
717 coro_cctx *prev__cctx; 746 coro_cctx *prev__cctx;
718 747
729 if (!prev->flags & CF_RUNNING) 758 if (!prev->flags & CF_RUNNING)
730 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); 759 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
731 760
732 if (next->flags & CF_RUNNING) 761 if (next->flags & CF_RUNNING)
733 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 762 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
763
764 if (next->flags & CF_DESTROYED)
765 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
734 766
735 prev->flags &= ~CF_RUNNING; 767 prev->flags &= ~CF_RUNNING;
736 next->flags |= CF_RUNNING; 768 next->flags |= CF_RUNNING;
737 769
738 LOCK; 770 LOCK;
759 791
760 /* possibly "free" the cctx */ 792 /* possibly "free" the cctx */
761 if (prev__cctx->idle_sp == STACKLEVEL) 793 if (prev__cctx->idle_sp == STACKLEVEL)
762 { 794 {
763 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 795 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
764 assert (PL_top_env == prev__cctx->top_env); 796 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
765 797
766 prev->cctx = 0; 798 prev->cctx = 0;
767 799
768 cctx_put (prev__cctx); 800 cctx_put (prev__cctx);
769 prev__cctx->inuse = 0; 801 prev__cctx->inuse = 0;
782 PL_top_env = next->cctx->top_env; 814 PL_top_env = next->cctx->top_env;
783 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 815 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
784 } 816 }
785 817
786 free_coro_mortal (); 818 free_coro_mortal ();
787
788 UNLOCK; 819 UNLOCK;
789 } 820 }
790} 821}
791 822
792struct transfer_args 823struct transfer_args
794 struct coro *prev, *next; 825 struct coro *prev, *next;
795}; 826};
796 827
797#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 828#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
798 829
830/** high level stuff ********************************************************/
831
799static void 832static int
800coro_state_destroy (struct coro *coro) 833coro_state_destroy (struct coro *coro)
801{ 834{
802 if (coro->refcnt--) 835 if (coro->flags & CF_DESTROYED)
803 return; 836 return 0;
837
838 coro->flags |= CF_DESTROYED;
839
840 if (coro->flags & CF_READY)
841 {
842 /* reduce nready, as destroying a ready coro effectively unreadies it */
843 /* alternative: look through all ready queues and remove it */
844 LOCK;
845 --coro_nready;
846 UNLOCK;
847 }
848 else
849 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
804 850
805 if (coro->mainstack && coro->mainstack != main_mainstack) 851 if (coro->mainstack && coro->mainstack != main_mainstack)
806 { 852 {
853 assert (!(coro->flags & CF_RUNNING));
854
807 struct coro temp; 855 struct coro temp;
808 Zero (&temp, 1, struct coro); 856 Zero (&temp, 1, struct coro);
809 temp.save = CORO_SAVE_ALL; 857 temp.save = CORO_SAVE_ALL;
810 858
811 if (coro->flags & CF_RUNNING) 859 if (coro->flags & CF_RUNNING)
821 coro->mainstack = 0; 869 coro->mainstack = 0;
822 } 870 }
823 871
824 cctx_destroy (coro->cctx); 872 cctx_destroy (coro->cctx);
825 SvREFCNT_dec (coro->args); 873 SvREFCNT_dec (coro->args);
826 Safefree (coro); 874
875 return 1;
827} 876}
828 877
829static int 878static int
830coro_state_clear (pTHX_ SV *sv, MAGIC *mg) 879coro_state_free (pTHX_ SV *sv, MAGIC *mg)
831{ 880{
832 struct coro *coro = (struct coro *)mg->mg_ptr; 881 struct coro *coro = (struct coro *)mg->mg_ptr;
833 mg->mg_ptr = 0; 882 mg->mg_ptr = 0;
834 883
884 if (--coro->refcnt < 0)
885 {
835 coro_state_destroy (coro); 886 coro_state_destroy (coro);
887 Safefree (coro);
888 }
836 889
837 return 0; 890 return 0;
838} 891}
839 892
840static int 893static int
847 return 0; 900 return 0;
848} 901}
849 902
850static MGVTBL coro_state_vtbl = { 903static MGVTBL coro_state_vtbl = {
851 0, 0, 0, 0, 904 0, 0, 0, 0,
852 coro_state_clear, 905 coro_state_free,
853 0, 906 0,
854#ifdef MGf_DUP 907#ifdef MGf_DUP
855 coro_state_dup, 908 coro_state_dup,
856#else 909#else
857# define MGf_DUP 0 910# define MGf_DUP 0
908 return old_save; 961 return old_save;
909} 962}
910 963
911/** Coro ********************************************************************/ 964/** Coro ********************************************************************/
912 965
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 966static void
926coro_enq (SV *coro_sv) 967coro_enq (SV *coro_sv)
927{ 968{
928 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv); 969 av_push (coro_ready [SvSTATE (coro_sv)->prio - PRIO_MIN], coro_sv);
929 coro_nready++;
930} 970}
931 971
932static SV * 972static SV *
933coro_deq (int min_prio) 973coro_deq (int min_prio)
934{ 974{
938 if (min_prio < 0) 978 if (min_prio < 0)
939 min_prio = 0; 979 min_prio = 0;
940 980
941 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; ) 981 for (prio = PRIO_MAX - PRIO_MIN + 1; --prio >= min_prio; )
942 if (AvFILLp (coro_ready [prio]) >= 0) 982 if (AvFILLp (coro_ready [prio]) >= 0)
943 {
944 coro_nready--;
945 return av_shift (coro_ready [prio]); 983 return av_shift (coro_ready [prio]);
946 }
947 984
948 return 0; 985 return 0;
949} 986}
950 987
951static int 988static int
963 1000
964 coro->flags |= CF_READY; 1001 coro->flags |= CF_READY;
965 1002
966 LOCK; 1003 LOCK;
967 coro_enq (SvREFCNT_inc (coro_sv)); 1004 coro_enq (SvREFCNT_inc (coro_sv));
1005 ++coro_nready;
968 UNLOCK; 1006 UNLOCK;
969 1007
970 return 1; 1008 return 1;
971} 1009}
972 1010
977} 1015}
978 1016
979static void 1017static void
980prepare_schedule (struct transfer_args *ta) 1018prepare_schedule (struct transfer_args *ta)
981{ 1019{
982 SV *prev, *next; 1020 SV *prev_sv, *next_sv;
983 1021
984 for (;;) 1022 for (;;)
985 { 1023 {
986 LOCK; 1024 LOCK;
987 next = coro_deq (PRIO_MIN); 1025 next_sv = coro_deq (PRIO_MIN);
988 UNLOCK; 1026 UNLOCK;
989 1027
1028 /* nothing to schedule: call the idle handler */
990 if (next) 1029 if (!next_sv)
991 break;
992
993 { 1030 {
994 dSP; 1031 dSP;
995 1032
996 ENTER; 1033 ENTER;
997 SAVETMPS; 1034 SAVETMPS;
998 1035
999 PUSHMARK (SP); 1036 PUSHMARK (SP);
1000 PUTBACK; 1037 PUTBACK;
1001 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); 1038 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1002 1039
1003 FREETMPS; 1040 FREETMPS;
1004 LEAVE; 1041 LEAVE;
1042 continue;
1005 } 1043 }
1006 }
1007 1044
1008 prev = SvRV (coro_current); 1045 ta->next = SvSTATE (next_sv);
1009 SvRV_set (coro_current, next); 1046
1047 /* cannot transfer to destroyed coros, skip and look for next */
1048 if (ta->next->flags & CF_DESTROYED)
1049 {
1050 SvREFCNT_dec (next_sv);
1051 /* coro_nready is already taken care of by destroy */
1052 continue;
1053 }
1054
1055 LOCK;
1056 --coro_nready;
1057 UNLOCK;
1058 break;
1059 }
1010 1060
1011 /* free this only after the transfer */ 1061 /* free this only after the transfer */
1062 prev_sv = SvRV (coro_current);
1063 SvRV_set (coro_current, next_sv);
1064 ta->prev = SvSTATE (prev_sv);
1065
1066 assert (ta->next->flags & CF_READY);
1067 ta->next->flags &= ~CF_READY;
1068
1012 LOCK; 1069 LOCK;
1013 free_coro_mortal (); 1070 free_coro_mortal ();
1071 coro_mortal = prev_sv;
1014 UNLOCK; 1072 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} 1073}
1026 1074
1027static void 1075static void
1028prepare_cede (struct transfer_args *ta) 1076prepare_cede (struct transfer_args *ta)
1029{ 1077{
1181 break; 1229 break;
1182 } 1230 }
1183 1231
1184 BARRIER; 1232 BARRIER;
1185 TRANSFER (ta); 1233 TRANSFER (ta);
1186}
1187 1234
1188void 1235 if (GIMME_V != G_VOID && ta.next != ta.prev)
1189_clone_state_from (SV *dst, SV *src) 1236 XSRETURN_YES;
1237}
1238
1239bool
1240_destroy (SV *coro_sv)
1190 CODE: 1241 CODE:
1191{ 1242 RETVAL = coro_state_destroy (SvSTATE (coro_sv));
1192 struct coro *coro_src = SvSTATE (src); 1243 OUTPUT:
1193 1244 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 1245
1200void 1246void
1201_exit (code) 1247_exit (code)
1202 int code 1248 int code
1203 PROTOTYPE: $ 1249 PROTOTYPE: $

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines