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.130 by root, Mon Dec 25 13:25:55 2006 UTC vs.
Revision 1.135 by root, Fri Jan 5 18:25:51 2007 UTC

93 93
94#define IN_DESTRUCT (PL_main_cv == Nullcv) 94#define IN_DESTRUCT (PL_main_cv == Nullcv)
95 95
96#if __GNUC__ >= 3 96#if __GNUC__ >= 3
97# define attribute(x) __attribute__(x) 97# define attribute(x) __attribute__(x)
98# define BARRIER __asm__ __volatile__ ("" : : : "memory")
98#else 99#else
99# define attribute(x) 100# define attribute(x)
101# define BARRIER
100#endif 102#endif
101 103
102#define NOINLINE attribute ((noinline)) 104#define NOINLINE attribute ((noinline))
103 105
104#include "CoroAPI.h" 106#include "CoroAPI.h"
135 /* the stack */ 137 /* the stack */
136 void *sptr; 138 void *sptr;
137 long ssize; /* positive == mmap, otherwise malloc */ 139 long ssize; /* positive == mmap, otherwise malloc */
138 140
139 /* cpu state */ 141 /* cpu state */
140 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 */
141 JMPENV *top_env; 144 JMPENV *top_env;
142 coro_context cctx; 145 coro_context cctx;
143 146
144 int inuse; 147 int inuse;
145 148
147 int valgrind_id; 150 int valgrind_id;
148#endif 151#endif
149} coro_cctx; 152} coro_cctx;
150 153
151enum { 154enum {
152 CF_RUNNING = 0x0001, /* coroutine is running */ 155 CF_RUNNING = 0x0001, /* coroutine is running */
153 CF_READY = 0x0002, /* coroutine is ready */ 156 CF_READY = 0x0002, /* coroutine is ready */
154 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 */
155}; 159};
156 160
157/* this is a structure representing a perl-level coroutine */ 161/* this is a structure representing a perl-level coroutine */
158struct coro { 162struct coro {
159 /* the c coroutine allocated to this perl coroutine, if any */ 163 /* the c coroutine allocated to this perl coroutine, if any */
468 472
469 /* free all temporaries */ 473 /* free all temporaries */
470 FREETMPS; 474 FREETMPS;
471 assert (PL_tmps_ix == -1); 475 assert (PL_tmps_ix == -1);
472 476
477 /* unwind all extra stacks */
473 POPSTACK_TO (PL_mainstack); 478 POPSTACK_TO (PL_mainstack);
479
480 /* unwind main stack */
481 dounwind (-1);
474 } 482 }
475 483
476 while (PL_curstackinfo->si_next) 484 while (PL_curstackinfo->si_next)
477 PL_curstackinfo = PL_curstackinfo->si_next; 485 PL_curstackinfo = PL_curstackinfo->si_next;
478 486
706 714
707 /* sometimes transfer is only called to set idle_sp */ 715 /* sometimes transfer is only called to set idle_sp */
708 if (!next) 716 if (!next)
709 { 717 {
710 ((coro_cctx *)prev)->idle_sp = STACKLEVEL; 718 ((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 */ 719 assert (((coro_cctx *)prev)->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */
712 } 720 }
713 else if (prev != next) 721 else if (prev != next)
714 { 722 {
715 coro_cctx *prev__cctx; 723 coro_cctx *prev__cctx;
716 724
727 if (!prev->flags & CF_RUNNING) 735 if (!prev->flags & CF_RUNNING)
728 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states"); 736 croak ("Coro::State::transfer called with non-running prev Coro::State, but can only transfer from running states");
729 737
730 if (next->flags & CF_RUNNING) 738 if (next->flags & CF_RUNNING)
731 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states"); 739 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states");
740
741 if (next->flags & CF_DESTROYED)
742 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states");
732 743
733 prev->flags &= ~CF_RUNNING; 744 prev->flags &= ~CF_RUNNING;
734 next->flags |= CF_RUNNING; 745 next->flags |= CF_RUNNING;
735 746
736 LOCK; 747 LOCK;
757 768
758 /* possibly "free" the cctx */ 769 /* possibly "free" the cctx */
759 if (prev__cctx->idle_sp == STACKLEVEL) 770 if (prev__cctx->idle_sp == STACKLEVEL)
760 { 771 {
761 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 772 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
762 assert (PL_top_env == prev__cctx->top_env); 773 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
763 774
764 prev->cctx = 0; 775 prev->cctx = 0;
765 776
766 cctx_put (prev__cctx); 777 cctx_put (prev__cctx);
767 prev__cctx->inuse = 0; 778 prev__cctx->inuse = 0;
780 PL_top_env = next->cctx->top_env; 791 PL_top_env = next->cctx->top_env;
781 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx); 792 coro_transfer (&prev__cctx->cctx, &next->cctx->cctx);
782 } 793 }
783 794
784 free_coro_mortal (); 795 free_coro_mortal ();
785
786 UNLOCK; 796 UNLOCK;
787 } 797 }
788} 798}
789 799
790struct transfer_args 800struct transfer_args
792 struct coro *prev, *next; 802 struct coro *prev, *next;
793}; 803};
794 804
795#define TRANSFER(ta) transfer ((ta).prev, (ta).next) 805#define TRANSFER(ta) transfer ((ta).prev, (ta).next)
796 806
797static void 807static int
798coro_state_destroy (struct coro *coro) 808coro_state_destroy (struct coro *coro)
799{ 809{
800 if (coro->refcnt--) 810 if (coro->flags & CF_DESTROYED)
801 return; 811 return 0;
812
813 coro->flags |= CF_DESTROYED;
802 814
803 if (coro->mainstack && coro->mainstack != main_mainstack) 815 if (coro->mainstack && coro->mainstack != main_mainstack)
804 { 816 {
817 assert (!(coro->flags & CF_RUNNING));
818
805 struct coro temp; 819 struct coro temp;
806 Zero (&temp, 1, struct coro); 820 Zero (&temp, 1, struct coro);
807 temp.save = CORO_SAVE_ALL; 821 temp.save = CORO_SAVE_ALL;
808 822
809 if (coro->flags & CF_RUNNING) 823 if (coro->flags & CF_RUNNING)
819 coro->mainstack = 0; 833 coro->mainstack = 0;
820 } 834 }
821 835
822 cctx_destroy (coro->cctx); 836 cctx_destroy (coro->cctx);
823 SvREFCNT_dec (coro->args); 837 SvREFCNT_dec (coro->args);
824 Safefree (coro); 838
839 return 1;
825} 840}
826 841
827static int 842static int
828coro_state_clear (pTHX_ SV *sv, MAGIC *mg) 843coro_state_free (pTHX_ SV *sv, MAGIC *mg)
829{ 844{
830 struct coro *coro = (struct coro *)mg->mg_ptr; 845 struct coro *coro = (struct coro *)mg->mg_ptr;
831 mg->mg_ptr = 0; 846 mg->mg_ptr = 0;
832 847
848 if (--coro->refcnt < 0)
849 {
833 coro_state_destroy (coro); 850 coro_state_destroy (coro);
851 Safefree (coro);
852 }
834 853
835 return 0; 854 return 0;
836} 855}
837 856
838static int 857static int
845 return 0; 864 return 0;
846} 865}
847 866
848static MGVTBL coro_state_vtbl = { 867static MGVTBL coro_state_vtbl = {
849 0, 0, 0, 0, 868 0, 0, 0, 0,
850 coro_state_clear, 869 coro_state_free,
851 0, 870 0,
852#ifdef MGf_DUP 871#ifdef MGf_DUP
853 coro_state_dup, 872 coro_state_dup,
854#else 873#else
855# define MGf_DUP 0 874# define MGf_DUP 0
975} 994}
976 995
977static void 996static void
978prepare_schedule (struct transfer_args *ta) 997prepare_schedule (struct transfer_args *ta)
979{ 998{
980 SV *prev, *next; 999 SV *prev_sv, *next_sv;
981 1000
982 for (;;) 1001 for (;;)
983 { 1002 {
984 LOCK; 1003 LOCK;
985 next = coro_deq (PRIO_MIN); 1004 next_sv = coro_deq (PRIO_MIN);
986 UNLOCK; 1005 UNLOCK;
987 1006
1007 /* nothing to schedule: call the idle handler */
988 if (next) 1008 if (!next_sv)
989 break;
990
991 { 1009 {
992 dSP; 1010 dSP;
993 1011
994 ENTER; 1012 ENTER;
995 SAVETMPS; 1013 SAVETMPS;
996 1014
997 PUSHMARK (SP); 1015 PUSHMARK (SP);
998 PUTBACK; 1016 PUTBACK;
999 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD); 1017 call_sv (get_sv ("Coro::idle", FALSE), G_DISCARD);
1000 1018
1001 FREETMPS; 1019 FREETMPS;
1002 LEAVE; 1020 LEAVE;
1021 continue;
1003 } 1022 }
1004 }
1005 1023
1006 prev = SvRV (coro_current); 1024 ta->next = SvSTATE (next_sv);
1007 SvRV_set (coro_current, next); 1025
1026 /* cannot transfer to destroyed coros, skip and look for next */
1027 if (ta->next->flags & CF_DESTROYED)
1028 {
1029 SvREFCNT_dec (next_sv);
1030 continue;
1031 }
1032
1033 break;
1034 }
1008 1035
1009 /* free this only after the transfer */ 1036 /* free this only after the transfer */
1037 prev_sv = SvRV (coro_current);
1038 SvRV_set (coro_current, next_sv);
1039 ta->prev = SvSTATE (prev_sv);
1040
1041 assert (ta->next->flags & CF_READY);
1042 ta->next->flags &= ~CF_READY;
1043
1010 LOCK; 1044 LOCK;
1011 free_coro_mortal (); 1045 free_coro_mortal ();
1046 coro_mortal = prev_sv;
1012 UNLOCK; 1047 UNLOCK;
1013 coro_mortal = prev;
1014
1015 assert (!SvROK(prev));//D
1016 assert (!SvROK(next));//D
1017
1018 ta->prev = SvSTATE (prev);
1019 ta->next = SvSTATE (next);
1020
1021 assert (ta->next->flags & CF_READY);
1022 ta->next->flags &= ~CF_READY;
1023} 1048}
1024 1049
1025static void 1050static void
1026prepare_cede (struct transfer_args *ta) 1051prepare_cede (struct transfer_args *ta)
1027{ 1052{
1028 api_ready (coro_current); 1053 api_ready (coro_current);
1029
1030 prepare_schedule (ta); 1054 prepare_schedule (ta);
1055}
1056
1057static int
1058prepare_cede_notself (struct transfer_args *ta)
1059{
1060 if (coro_nready)
1061 {
1062 SV *prev = SvRV (coro_current);
1063 prepare_schedule (ta);
1064 api_ready (prev);
1065 return 1;
1066 }
1067 else
1068 return 0;
1031} 1069}
1032 1070
1033static void 1071static void
1034api_schedule (void) 1072api_schedule (void)
1035{ 1073{
1037 1075
1038 prepare_schedule (&ta); 1076 prepare_schedule (&ta);
1039 TRANSFER (ta); 1077 TRANSFER (ta);
1040} 1078}
1041 1079
1042static void 1080static int
1043api_cede (void) 1081api_cede (void)
1044{ 1082{
1045 struct transfer_args ta; 1083 struct transfer_args ta;
1046 1084
1047 prepare_cede (&ta); 1085 prepare_cede (&ta);
1086
1087 if (ta.prev != ta.next)
1088 {
1048 TRANSFER (ta); 1089 TRANSFER (ta);
1090 return 1;
1091 }
1092 else
1093 return 0;
1094}
1095
1096static int
1097api_cede_notself (void)
1098{
1099 struct transfer_args ta;
1100
1101 if (prepare_cede_notself (&ta))
1102 {
1103 TRANSFER (ta);
1104 return 1;
1105 }
1106 else
1107 return 0;
1049} 1108}
1050 1109
1051MODULE = Coro::State PACKAGE = Coro::State 1110MODULE = Coro::State PACKAGE = Coro::State
1052 1111
1053PROTOTYPES: DISABLE 1112PROTOTYPES: DISABLE
1109_set_stacklevel (...) 1168_set_stacklevel (...)
1110 ALIAS: 1169 ALIAS:
1111 Coro::State::transfer = 1 1170 Coro::State::transfer = 1
1112 Coro::schedule = 2 1171 Coro::schedule = 2
1113 Coro::cede = 3 1172 Coro::cede = 3
1173 Coro::cede_notself = 4
1114 CODE: 1174 CODE:
1115{ 1175{
1116 struct transfer_args ta; 1176 struct transfer_args ta;
1117 1177
1118 switch (ix) 1178 switch (ix)
1134 break; 1194 break;
1135 1195
1136 case 3: 1196 case 3:
1137 prepare_cede (&ta); 1197 prepare_cede (&ta);
1138 break; 1198 break;
1199
1200 case 4:
1201 if (!prepare_cede_notself (&ta))
1202 XSRETURN_EMPTY;
1203
1204 break;
1139 } 1205 }
1140 1206
1207 BARRIER;
1141 TRANSFER (ta); 1208 TRANSFER (ta);
1142}
1143 1209
1144void 1210 if (GIMME_V != G_VOID && ta.next != ta.prev)
1145_clone_state_from (SV *dst, SV *src) 1211 XSRETURN_YES;
1212}
1213
1214bool
1215_destroy (SV *coro_sv)
1146 CODE: 1216 CODE:
1147{ 1217 RETVAL = coro_state_destroy (SvSTATE (coro_sv));
1148 struct coro *coro_src = SvSTATE (src); 1218 OUTPUT:
1149 1219 RETVAL
1150 sv_unmagic (SvRV (dst), PERL_MAGIC_ext);
1151
1152 ++coro_src->refcnt;
1153 sv_magicext (SvRV (dst), 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro_src, 0)->mg_flags |= MGf_DUP;
1154}
1155 1220
1156void 1221void
1157_exit (code) 1222_exit (code)
1158 int code 1223 int code
1159 PROTOTYPE: $ 1224 PROTOTYPE: $
1196 coro_ready[i] = newAV (); 1261 coro_ready[i] = newAV ();
1197 1262
1198 { 1263 {
1199 SV *sv = perl_get_sv("Coro::API", 1); 1264 SV *sv = perl_get_sv("Coro::API", 1);
1200 1265
1201 coroapi.schedule = api_schedule; 1266 coroapi.schedule = api_schedule;
1202 coroapi.save = api_save; 1267 coroapi.save = api_save;
1203 coroapi.cede = api_cede; 1268 coroapi.cede = api_cede;
1269 coroapi.cede_notself = api_cede_notself;
1204 coroapi.ready = api_ready; 1270 coroapi.ready = api_ready;
1205 coroapi.is_ready = api_is_ready; 1271 coroapi.is_ready = api_is_ready;
1206 coroapi.nready = &coro_nready; 1272 coroapi.nready = &coro_nready;
1207 coroapi.current = coro_current; 1273 coroapi.current = coro_current;
1208 1274
1209 GCoroAPI = &coroapi; 1275 GCoroAPI = &coroapi;
1210 sv_setiv (sv, (IV)&coroapi); 1276 sv_setiv (sv, (IV)&coroapi);
1211 SvREADONLY_on (sv); 1277 SvREADONLY_on (sv);
1212 } 1278 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines