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.133 by root, Thu Jan 4 20:14:19 2007 UTC

137 /* the stack */ 137 /* the stack */
138 void *sptr; 138 void *sptr;
139 long ssize; /* positive == mmap, otherwise malloc */ 139 long ssize; /* positive == mmap, otherwise malloc */
140 140
141 /* cpu state */ 141 /* cpu state */
142 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 142 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
143 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */
143 JMPENV *top_env; 144 JMPENV *top_env;
144 coro_context cctx; 145 coro_context cctx;
145 146
146 int inuse; 147 int inuse;
147 148
149 int valgrind_id; 150 int valgrind_id;
150#endif 151#endif
151} coro_cctx; 152} coro_cctx;
152 153
153enum { 154enum {
154 CF_RUNNING = 0x0001, /* coroutine is running */ 155 CF_RUNNING = 0x0001, /* coroutine is running */
155 CF_READY = 0x0002, /* coroutine is ready */ 156 CF_READY = 0x0002, /* coroutine is ready */
156 CF_NEW = 0x0004, /* ahs never been switched to */ 157 CF_NEW = 0x0004, /* has never been switched to */
158 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
157}; 159};
158 160
159/* this is a structure representing a perl-level coroutine */ 161/* this is a structure representing a perl-level coroutine */
160struct coro { 162struct coro {
161 /* the c coroutine allocated to this perl coroutine, if any */ 163 /* the c coroutine allocated to this perl coroutine, if any */
708 710
709 /* sometimes transfer is only called to set idle_sp */ 711 /* sometimes transfer is only called to set idle_sp */
710 if (!next) 712 if (!next)
711 { 713 {
712 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 714 ((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 */ 715 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
714 } 716 }
715 else if (prev != next) 717 else if (prev != next)
716 { 718 {
717 coro_cctx *prev__cctx; 719 coro_cctx *prev__cctx;
718 720
729 if (!prev->flags & CF_RUNNING) 731 if (!prev->flags & CF_RUNNING)
730 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); 732 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
731 733
732 if (next->flags & CF_RUNNING) 734 if (next->flags & CF_RUNNING)
733 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 735 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
736
737 if (next->flags & CF_DESTROYED)
738 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
734 739
735 prev->flags &= ~CF_RUNNING; 740 prev->flags &= ~CF_RUNNING;
736 next->flags |= CF_RUNNING; 741 next->flags |= CF_RUNNING;
737 742
738 LOCK; 743 LOCK;
759 764
760 /* possibly "free" the cctx */ 765 /* possibly "free" the cctx */
761 if (prev__cctx->idle_sp == STACKLEVEL) 766 if (prev__cctx->idle_sp == STACKLEVEL)
762 { 767 {
763 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 768 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
764 assert (PL_top_env == prev__cctx->top_env); 769 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
765 770
766 prev->cctx = 0; 771 prev->cctx = 0;
767 772
768 cctx_put (prev__cctx); 773 cctx_put (prev__cctx);
769 prev__cctx->inuse = 0; 774 prev__cctx->inuse = 0;
782 PL_top_env = next->cctx->top_env; 787 PL_top_env = next->cctx->top_env;
783 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 788 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
784 } 789 }
785 790
786 free_coro_mortal (); 791 free_coro_mortal ();
787
788 UNLOCK; 792 UNLOCK;
789 } 793 }
790} 794}
791 795
792struct transfer_args 796struct transfer_args
794 struct coro *prev, *next; 798 struct coro *prev, *next;
795}; 799};
796 800
797#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 801#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
798 802
799static void 803static int
800coro_state_destroy (struct coro *coro) 804coro_state_destroy (struct coro *coro)
801{ 805{
802 if (coro->refcnt--) 806 if (coro->refcnt--)
803 return; 807 return 0;
808
809 if (coro->flags & CF_DESTROYED)
810 return 0;
811
812 coro->flags |= CF_DESTROYED;
804 813
805 if (coro->mainstack && coro->mainstack != main_mainstack) 814 if (coro->mainstack && coro->mainstack != main_mainstack)
806 { 815 {
816 assert (!(coro->flags & CF_RUNNING));
817
807 struct coro temp; 818 struct coro temp;
808 Zero (&temp, 1, struct coro); 819 Zero (&temp, 1, struct coro);
809 temp.save = CORO_SAVE_ALL; 820 temp.save = CORO_SAVE_ALL;
810 821
811 if (coro->flags & CF_RUNNING) 822 if (coro->flags & CF_RUNNING)
821 coro->mainstack = 0; 832 coro->mainstack = 0;
822 } 833 }
823 834
824 cctx_destroy (coro->cctx); 835 cctx_destroy (coro->cctx);
825 SvREFCNT_dec (coro->args); 836 SvREFCNT_dec (coro->args);
826 Safefree (coro); 837
838 return 1;
827} 839}
828 840
829static int 841static int
830coro_state_clear (pTHX_ SV *sv, MAGIC *mg) 842coro_state_clear (pTHX_ SV *sv, MAGIC *mg)
831{ 843{
832 struct coro *coro = (struct coro *)mg->mg_ptr; 844 struct coro *coro = (struct coro *)mg->mg_ptr;
833 mg->mg_ptr = 0; 845 mg->mg_ptr = 0;
834 846
835 coro_state_destroy (coro); 847 coro_state_destroy (coro);
848
849 if (!coro->refcnt)
850 Safefree (coro);
836 851
837 return 0; 852 return 0;
838} 853}
839 854
840static int 855static int
977} 992}
978 993
979static void 994static void
980prepare_schedule (struct transfer_args *ta) 995prepare_schedule (struct transfer_args *ta)
981{ 996{
982 SV *prev, *next; 997 SV *prev_sv, *next_sv;
983 998
984 for (;;) 999 for (;;)
985 { 1000 {
986 LOCK; 1001 LOCK;
987 next = coro_deq (PRIO_MIN); 1002 next_sv = coro_deq (PRIO_MIN);
988 UNLOCK; 1003 UNLOCK;
989 1004
1005 /* nothing to schedule: call the idle handler */
990 if (next) 1006 if (!next_sv)
991 break;
992
993 { 1007 {
994 dSP; 1008 dSP;
995 1009
996 ENTER; 1010 ENTER;
997 SAVETMPS; 1011 SAVETMPS;
998 1012
999 PUSHMARK (SP); 1013 PUSHMARK (SP);
1000 PUTBACK; 1014 PUTBACK;
1001 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); 1015 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1002 1016
1003 FREETMPS; 1017 FREETMPS;
1004 LEAVE; 1018 LEAVE;
1019 continue;
1005 } 1020 }
1006 }
1007 1021
1008 prev = SvRV (coro_current); 1022 ta->next = SvSTATE (next_sv);
1009 SvRV_set (coro_current, next); 1023
1024 /* cannot transfer to destroyed coros, skip and look for next */
1025 if (ta->next->flags & CF_DESTROYED)
1026 {
1027 SvREFCNT_dec (next_sv);
1028 continue;
1029 }
1030
1031 break;
1032 }
1010 1033
1011 /* free this only after the transfer */ 1034 /* free this only after the transfer */
1035 prev_sv = SvRV (coro_current);
1036 SvRV_set (coro_current, next_sv);
1037 ta->prev = SvSTATE (prev_sv);
1038
1039 assert (ta->next->flags & CF_READY);
1040 ta->next->flags &= ~CF_READY;
1041
1012 LOCK; 1042 LOCK;
1013 free_coro_mortal (); 1043 free_coro_mortal ();
1044 coro_mortal = prev_sv;
1014 UNLOCK; 1045 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} 1046}
1026 1047
1027static void 1048static void
1028prepare_cede (struct transfer_args *ta) 1049prepare_cede (struct transfer_args *ta)
1029{ 1050{
1183 1204
1184 BARRIER; 1205 BARRIER;
1185 TRANSFER (ta); 1206 TRANSFER (ta);
1186} 1207}
1187 1208
1188void 1209bool
1189_clone_state_from (SV *dst, SV *src) 1210_destroy (SV *coro_sv)
1190 CODE: 1211 CODE:
1191{ 1212 RETVAL = coro_state_destroy (SvSTATE (coro_sv));
1192 struct coro *coro_src = SvSTATE (src); 1213 OUTPUT:
1193 1214 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 1215
1200void 1216void
1201_exit (code) 1217_exit (code)
1202 int code 1218 int code
1203 PROTOTYPE: $ 1219 PROTOTYPE: $

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines