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.132 by root, Fri Dec 29 13:03:05 2006 UTC vs.
Revision 1.133 by root, Thu Jan 4 20:14:19 2007 UTC

150 int valgrind_id; 150 int valgrind_id;
151#endif 151#endif
152} coro_cctx; 152} coro_cctx;
153 153
154enum { 154enum {
155 CF_RUNNING = 0x0001, /* coroutine is running */ 155 CF_RUNNING = 0x0001, /* coroutine is running */
156 CF_READY = 0x0002, /* coroutine is ready */ 156 CF_READY = 0x0002, /* coroutine is ready */
157 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 */
158}; 159};
159 160
160/* this is a structure representing a perl-level coroutine */ 161/* this is a structure representing a perl-level coroutine */
161struct coro { 162struct coro {
162 /* the c coroutine allocated to this perl coroutine, if any */ 163 /* the c coroutine allocated to this perl coroutine, if any */
730 if (!prev->flags & CF_RUNNING) 731 if (!prev->flags & CF_RUNNING)
731 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");
732 733
733 if (next->flags & CF_RUNNING) 734 if (next->flags & CF_RUNNING)
734 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");
735 739
736 prev->flags &= ~CF_RUNNING; 740 prev->flags &= ~CF_RUNNING;
737 next->flags |= CF_RUNNING; 741 next->flags |= CF_RUNNING;
738 742
739 LOCK; 743 LOCK;
783 PL_top_env = next->cctx->top_env; 787 PL_top_env = next->cctx->top_env;
784 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 788 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
785 } 789 }
786 790
787 free_coro_mortal (); 791 free_coro_mortal ();
788
789 UNLOCK; 792 UNLOCK;
790 } 793 }
791} 794}
792 795
793struct transfer_args 796struct transfer_args
795 struct coro *prev, *next; 798 struct coro *prev, *next;
796}; 799};
797 800
798#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 801#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
799 802
800static void 803static int
801coro_state_destroy (struct coro *coro) 804coro_state_destroy (struct coro *coro)
802{ 805{
803 if (coro->refcnt--) 806 if (coro->refcnt--)
804 return; 807 return 0;
808
809 if (coro->flags & CF_DESTROYED)
810 return 0;
811
812 coro->flags |= CF_DESTROYED;
805 813
806 if (coro->mainstack && coro->mainstack != main_mainstack) 814 if (coro->mainstack && coro->mainstack != main_mainstack)
807 { 815 {
816 assert (!(coro->flags & CF_RUNNING));
817
808 struct coro temp; 818 struct coro temp;
809 Zero (&temp, 1, struct coro); 819 Zero (&temp, 1, struct coro);
810 temp.save = CORO_SAVE_ALL; 820 temp.save = CORO_SAVE_ALL;
811 821
812 if (coro->flags & CF_RUNNING) 822 if (coro->flags & CF_RUNNING)
822 coro->mainstack = 0; 832 coro->mainstack = 0;
823 } 833 }
824 834
825 cctx_destroy (coro->cctx); 835 cctx_destroy (coro->cctx);
826 SvREFCNT_dec (coro->args); 836 SvREFCNT_dec (coro->args);
827 Safefree (coro); 837
838 return 1;
828} 839}
829 840
830static int 841static int
831coro_state_clear (pTHX_ SV *sv, MAGIC *mg) 842coro_state_clear (pTHX_ SV *sv, MAGIC *mg)
832{ 843{
833 struct coro *coro = (struct coro *)mg->mg_ptr; 844 struct coro *coro = (struct coro *)mg->mg_ptr;
834 mg->mg_ptr = 0; 845 mg->mg_ptr = 0;
835 846
836 coro_state_destroy (coro); 847 coro_state_destroy (coro);
848
849 if (!coro->refcnt)
850 Safefree (coro);
837 851
838 return 0; 852 return 0;
839} 853}
840 854
841static int 855static int
978} 992}
979 993
980static void 994static void
981prepare_schedule (struct transfer_args *ta) 995prepare_schedule (struct transfer_args *ta)
982{ 996{
983 SV *prev, *next; 997 SV *prev_sv, *next_sv;
984 998
985 for (;;) 999 for (;;)
986 { 1000 {
987 LOCK; 1001 LOCK;
988 next = coro_deq (PRIO_MIN); 1002 next_sv = coro_deq (PRIO_MIN);
989 UNLOCK; 1003 UNLOCK;
990 1004
1005 /* nothing to schedule: call the idle handler */
991 if (next) 1006 if (!next_sv)
992 break;
993
994 { 1007 {
995 dSP; 1008 dSP;
996 1009
997 ENTER; 1010 ENTER;
998 SAVETMPS; 1011 SAVETMPS;
999 1012
1000 PUSHMARK (SP); 1013 PUSHMARK (SP);
1001 PUTBACK; 1014 PUTBACK;
1002 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); 1015 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1003 1016
1004 FREETMPS; 1017 FREETMPS;
1005 LEAVE; 1018 LEAVE;
1019 continue;
1006 } 1020 }
1007 }
1008 1021
1009 prev = SvRV (coro_current); 1022 ta->next = SvSTATE (next_sv);
1010 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 }
1011 1033
1012 /* 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
1013 LOCK; 1042 LOCK;
1014 free_coro_mortal (); 1043 free_coro_mortal ();
1044 coro_mortal = prev_sv;
1015 UNLOCK; 1045 UNLOCK;
1016 coro_mortal = prev;
1017
1018 assert (!SvROK(prev));//D
1019 assert (!SvROK(next));//D
1020
1021 ta->prev = SvSTATE (prev);
1022 ta->next = SvSTATE (next);
1023
1024 assert (ta->next->flags & CF_READY);
1025 ta->next->flags &= ~CF_READY;
1026} 1046}
1027 1047
1028static void 1048static void
1029prepare_cede (struct transfer_args *ta) 1049prepare_cede (struct transfer_args *ta)
1030{ 1050{
1184 1204
1185 BARRIER; 1205 BARRIER;
1186 TRANSFER (ta); 1206 TRANSFER (ta);
1187} 1207}
1188 1208
1189void 1209bool
1190_clone_state_from (SV *dst, SV *src) 1210_destroy (SV *coro_sv)
1191 CODE: 1211 CODE:
1192{ 1212 RETVAL = coro_state_destroy (SvSTATE (coro_sv));
1193 struct coro *coro_src = SvSTATE (src); 1213 OUTPUT:
1194 1214 RETVAL
1195 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1196
1197 ++coro_src->refcnt;
1198 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1199}
1200 1215
1201void 1216void
1202_exit (code) 1217_exit (code)
1203 int code 1218 int code
1204 PROTOTYPE: $ 1219 PROTOTYPE: $

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines