| 830 | | |
|---|
| 831 | | /* |
|---|
| 832 | | private static const STATE_STANDINGBY:uint = 0; |
|---|
| 833 | | private static const STATE_RUNNING:uint = 1 << 0; |
|---|
| 834 | | private static const STATE_SLEEPING:uint = 1 << 1; |
|---|
| 835 | | private static const STATE_SUSPENDING:uint = 1 << 2; |
|---|
| 836 | | private static const STATE_TERMINATED:uint = 1 << 3; |
|---|
| 837 | | private static const STATE_ERROR:uint = 1 << 4; |
|---|
| 838 | | |
|---|
| 839 | | private static var _currentThread:Thread = null; |
|---|
| 840 | | internal static var _debug:Debug = null; |
|---|
| 841 | | |
|---|
| 842 | | public static function get currentThread():Thread |
|---|
| 843 | | { |
|---|
| 844 | | return _currentThread; |
|---|
| 845 | | } |
|---|
| 846 | | |
|---|
| 847 | | public function Thread() |
|---|
| 848 | | { |
|---|
| 849 | | _parent = null; |
|---|
| 850 | | _children = []; |
|---|
| 851 | | _willRemove = []; |
|---|
| 852 | | _state = STATE_STANDINGBY; |
|---|
| 853 | | _executeHandler = nullHandler; |
|---|
| 854 | | _waitMonitor = null; |
|---|
| 855 | | _joinMonitor = null; |
|---|
| 856 | | _errorHandler = null; |
|---|
| 857 | | } |
|---|
| 858 | | |
|---|
| 859 | | internal var _parent:Thread; |
|---|
| 860 | | internal var _children:Array; |
|---|
| 861 | | private var _willRemove:Array; |
|---|
| 862 | | private var _state:uint; |
|---|
| 863 | | internal var _executeHandler:Function; |
|---|
| 864 | | internal var _runHandler:Function; |
|---|
| 865 | | private var _waitMonitor:Monitor; |
|---|
| 866 | | private var _joinMonitor:Monitor; |
|---|
| 867 | | private var _errorHandler:ThreadErrorHandler; |
|---|
| 868 | | |
|---|
| 869 | | public function get threadErrorHandler():ThreadErrorHandler |
|---|
| 870 | | { |
|---|
| 871 | | return _errorHandler; |
|---|
| 872 | | } |
|---|
| 873 | | |
|---|
| 874 | | public function set thraedErrorHandler(value:ThreadErrorHandler):void |
|---|
| 875 | | { |
|---|
| 876 | | _errorHandler = value; |
|---|
| 877 | | } |
|---|
| 878 | | |
|---|
| 879 | | public function get isStandingBy():Boolean |
|---|
| 880 | | { |
|---|
| 881 | | return _state == STATE_STANDINGBY; |
|---|
| 882 | | } |
|---|
| 883 | | |
|---|
| 884 | | public function get isRunning():Boolean |
|---|
| 885 | | { |
|---|
| 886 | | return (_state & STATE_RUNNING) != 0; |
|---|
| 887 | | } |
|---|
| 888 | | |
|---|
| 889 | | public function get isSleeping():Boolean |
|---|
| 890 | | { |
|---|
| 891 | | return (_state & STATE_SLEEPING) != 0; |
|---|
| 892 | | } |
|---|
| 893 | | |
|---|
| 894 | | public function get isSuspending():Boolean |
|---|
| 895 | | { |
|---|
| 896 | | return (_state & STATE_SUSPENDING) != 0; |
|---|
| 897 | | } |
|---|
| 898 | | |
|---|
| 899 | | public function get isTerminated():Boolean |
|---|
| 900 | | { |
|---|
| 901 | | return (_state & STATE_TERMINATED) != 0; |
|---|
| 902 | | } |
|---|
| 903 | | |
|---|
| 904 | | public function get isCatchError():Boolean |
|---|
| 905 | | { |
|---|
| 906 | | return (_state & STATE_ERROR) != 0; |
|---|
| 907 | | } |
|---|
| 908 | | |
|---|
| 909 | | internal function addChild(child:Thread):Thread |
|---|
| 910 | | { |
|---|
| 911 | | _children.push(child); |
|---|
| 912 | | |
|---|
| 913 | | return child; |
|---|
| 914 | | } |
|---|
| 915 | | |
|---|
| 916 | | internal function removeChild(child:Thread):Thread |
|---|
| 917 | | { |
|---|
| 918 | | _willRemove.push(child); |
|---|
| 919 | | |
|---|
| 920 | | return child; |
|---|
| 921 | | } |
|---|
| 922 | | |
|---|
| 923 | | public function begin():void |
|---|
| 924 | | { |
|---|
| 925 | | if (isRunning) { |
|---|
| 926 | | return; |
|---|
| 927 | | } |
|---|
| 928 | | |
|---|
| 929 | | (_parent = _currentThread).addChild(this); |
|---|
| 930 | | |
|---|
| 931 | | _runHandler = execute; |
|---|
| 932 | | _executeHandler = beginHandler; |
|---|
| 933 | | } |
|---|
| 934 | | |
|---|
| 935 | | public function terminate():void |
|---|
| 936 | | { |
|---|
| 937 | | if (!isRunning) { |
|---|
| 938 | | return; |
|---|
| 939 | | } |
|---|
| 940 | | |
|---|
| 941 | | _executeHandler = terminateHandler; |
|---|
| 942 | | } |
|---|
| 943 | | |
|---|
| 944 | | public function join():void |
|---|
| 945 | | { |
|---|
| 946 | | if (isTerminated) { |
|---|
| 947 | | return; |
|---|
| 948 | | } |
|---|
| 949 | | |
|---|
| 950 | | if (_joinMonitor == null) { |
|---|
| 951 | | _joinMonitor = new MonitorObject(); |
|---|
| 952 | | } |
|---|
| 953 | | |
|---|
| 954 | | _joinMonitor.wait(); |
|---|
| 955 | | } |
|---|
| 956 | | |
|---|
| 957 | | public function suspend():void |
|---|
| 958 | | { |
|---|
| 959 | | if ((_state & (STATE_SLEEPING | STATE_SUSPENDING)) != 0) { |
|---|
| 960 | | return; |
|---|
| 961 | | } |
|---|
| 962 | | |
|---|
| 963 | | _executeHandler = suspendHandler; |
|---|
| 964 | | } |
|---|
| 965 | | |
|---|
| 966 | | public function resume():void |
|---|
| 967 | | { |
|---|
| 968 | | if (!isSuspending) { |
|---|
| 969 | | return; |
|---|
| 970 | | } |
|---|
| 971 | | |
|---|
| 972 | | _executeHandler = resumeHandler; |
|---|
| 973 | | } |
|---|
| 974 | | |
|---|
| 975 | | public function interruput():void |
|---|
| 976 | | { |
|---|
| 977 | | if (_waitMonitor != null) { |
|---|
| 978 | | _waitMonitor.interrupt(this); |
|---|
| 979 | | } |
|---|
| 980 | | } |
|---|
| 981 | | |
|---|
| 982 | | internal function sleep(monitor:Monitor):void |
|---|
| 983 | | { |
|---|
| 984 | | if ((_state & (STATE_SLEEPING | STATE_SUSPENDING)) != 0) { |
|---|
| 985 | | return; |
|---|
| 986 | | } |
|---|
| 987 | | |
|---|
| 988 | | if (monitor == null || _waitMonitor != null) { |
|---|
| 989 | | throw new Error('Can not wait.'); |
|---|
| 990 | | } |
|---|
| 991 | | |
|---|
| 992 | | _waitMonitor = monitor; |
|---|
| 993 | | _state |= STATE_SLEEPING; |
|---|
| 994 | | _executeHandler = nullHandler; |
|---|
| 995 | | |
|---|
| 996 | | if (_debug != null) { |
|---|
| 997 | | _debug.threadChanged(this); |
|---|
| 998 | | } |
|---|
| 999 | | } |
|---|
| 1000 | | |
|---|
| 1001 | | internal function wakeup(monitor:Monitor):void |
|---|
| 1002 | | { |
|---|
| 1003 | | if (!isSleeping) { |
|---|
| 1004 | | return; |
|---|
| 1005 | | } |
|---|
| 1006 | | |
|---|
| 1007 | | if (monitor == null || _waitMonitor != monitor) { |
|---|
| 1008 | | throw new Error('Can not wakeup.'); |
|---|
| 1009 | | } |
|---|
| 1010 | | |
|---|
| 1011 | | _waitMonitor = null; |
|---|
| 1012 | | _state &= ~STATE_SLEEPING; |
|---|
| 1013 | | _executeHandler = runHandler; |
|---|
| 1014 | | |
|---|
| 1015 | | if (_debug != null) { |
|---|
| 1016 | | _debug.threadChanged(this); |
|---|
| 1017 | | } |
|---|
| 1018 | | } |
|---|
| 1019 | | |
|---|
| 1020 | | internal function wakeupError(e:Error):void |
|---|
| 1021 | | { |
|---|
| 1022 | | if (!isSleeping) { |
|---|
| 1023 | | return; |
|---|
| 1024 | | } |
|---|
| 1025 | | |
|---|
| 1026 | | _waitMonitor = null; |
|---|
| 1027 | | _state &= ~STATE_SLEEPING; |
|---|
| 1028 | | _executeHandler = function():void |
|---|
| 1029 | | { |
|---|
| 1030 | | throw e; |
|---|
| 1031 | | }; |
|---|
| 1032 | | } |
|---|
| 1033 | | |
|---|
| 1034 | | internal function internalExecute():void |
|---|
| 1035 | | { |
|---|
| 1036 | | var children:Array = _children; |
|---|
| 1037 | | var l:uint = children.length; |
|---|
| 1038 | | |
|---|
| 1039 | | var willRemove:Array = _willRemove; |
|---|
| 1040 | | if (willRemove.length > 0) { |
|---|
| 1041 | | for each (var c:Thread in willRemove) { |
|---|
| 1042 | | var index:uint = children.indexOf(c); |
|---|
| 1043 | | if (index != -1) { |
|---|
| 1044 | | children.splice(index, 1); |
|---|
| 1045 | | } |
|---|
| 1046 | | } |
|---|
| 1047 | | willRemove.length = 0; |
|---|
| 1048 | | l = children.length; |
|---|
| 1049 | | } |
|---|
| 1050 | | |
|---|
| 1051 | | for (var i:uint = 0; i < l; ++i) { |
|---|
| 1052 | | try { |
|---|
| 1053 | | Thread(children[i]).internalExecute(); |
|---|
| 1054 | | } |
|---|
| 1055 | | catch (e:Error) { |
|---|
| 1056 | | handleError(e); |
|---|
| 1057 | | } |
|---|
| 1058 | | } |
|---|
| 1059 | | |
|---|
| 1060 | | try { |
|---|
| 1061 | | _currentThread = this; |
|---|
| 1062 | | _executeHandler.apply(this); |
|---|
| 1063 | | } |
|---|
| 1064 | | catch (e2:Error) { |
|---|
| 1065 | | handleError(e2); |
|---|
| 1066 | | } |
|---|
| 1067 | | finally { |
|---|
| 1068 | | _currentThread = null; |
|---|
| 1069 | | } |
|---|
| 1070 | | } |
|---|
| 1071 | | |
|---|
| 1072 | | private function handleError(e:Error):void |
|---|
| 1073 | | { |
|---|
| 1074 | | try { |
|---|
| 1075 | | catchError(_currentThread, e); |
|---|
| 1076 | | } |
|---|
| 1077 | | catch (ee:Error) { |
|---|
| 1078 | | if ((ee = handleByErrorHandler(ee)) != null) { |
|---|
| 1079 | | terminateHandler(); |
|---|
| 1080 | | _state |= STATE_ERROR; |
|---|
| 1081 | | |
|---|
| 1082 | | throw ee; |
|---|
| 1083 | | } |
|---|
| 1084 | | } |
|---|
| 1085 | | } |
|---|
| 1086 | | |
|---|
| 1087 | | private function handleByErrorHandler(e:Error):Error |
|---|
| 1088 | | { |
|---|
| 1089 | | if (_errorHandler != null) { |
|---|
| 1090 | | try { |
|---|
| 1091 | | _errorHandler.handleError(_currentThread, e); |
|---|
| 1092 | | e = null; |
|---|
| 1093 | | } |
|---|
| 1094 | | catch (ee:Error) { |
|---|
| 1095 | | e = ee; |
|---|
| 1096 | | } |
|---|
| 1097 | | } |
|---|
| 1098 | | return e; |
|---|
| 1099 | | } |
|---|
| 1100 | | |
|---|
| 1101 | | internal function nullHandler():void |
|---|
| 1102 | | { |
|---|
| 1103 | | } |
|---|
| 1104 | | |
|---|
| 1105 | | internal function runHandler():void |
|---|
| 1106 | | { |
|---|
| 1107 | | _runHandler.apply(this); |
|---|
| 1108 | | } |
|---|
| 1109 | | |
|---|
| 1110 | | internal function beginHandler():void |
|---|
| 1111 | | { |
|---|
| 1112 | | _state = STATE_RUNNING; |
|---|
| 1113 | | _executeHandler = runHandler; |
|---|
| 1114 | | initialize(); |
|---|
| 1115 | | if (_executeHandler == runHandler) { |
|---|
| 1116 | | runHandler(); |
|---|
| 1117 | | } |
|---|
| 1118 | | |
|---|
| 1119 | | if (_debug != null) { |
|---|
| 1120 | | _debug.threadBegin(); |
|---|
| 1121 | | } |
|---|
| 1122 | | } |
|---|
| 1123 | | |
|---|
| 1124 | | internal function terminateHandler():void |
|---|
| 1125 | | { |
|---|
| 1126 | | var children:Array = _children.slice(); |
|---|
| 1127 | | var l:uint = children.length; |
|---|
| 1128 | | for (var i:uint = 0; i < l; ++i) { |
|---|
| 1129 | | Thread(children[i]).terminateHandler(); |
|---|
| 1130 | | } |
|---|
| 1131 | | |
|---|
| 1132 | | if (_parent != null) { |
|---|
| 1133 | | _parent.removeChild(this); |
|---|
| 1134 | | } |
|---|
| 1135 | | _state = STATE_TERMINATED; |
|---|
| 1136 | | _executeHandler = nullHandler; |
|---|
| 1137 | | finalize(); |
|---|
| 1138 | | |
|---|
| 1139 | | if (_debug != null) { |
|---|
| 1140 | | _debug.threadTerminate(this); |
|---|
| 1141 | | } |
|---|
| 1142 | | |
|---|
| 1143 | | if (_joinMonitor != null) { |
|---|
| 1144 | | _joinMonitor.notifyAll(); |
|---|
| 1145 | | } |
|---|
| 1146 | | } |
|---|
| 1147 | | |
|---|
| 1148 | | internal function suspendHandler():void |
|---|
| 1149 | | { |
|---|
| 1150 | | _state |= STATE_SUSPENDING; |
|---|
| 1151 | | _executeHandler = nullHandler; |
|---|
| 1152 | | } |
|---|
| 1153 | | |
|---|
| 1154 | | internal function resumeHandler():void |
|---|
| 1155 | | { |
|---|
| 1156 | | _state &= ~STATE_SUSPENDING; |
|---|
| 1157 | | _executeHandler = runHandler; |
|---|
| 1158 | | runHandler(); |
|---|
| 1159 | | } |
|---|
| 1160 | | |
|---|
| 1161 | | protected function switchExecuteMethod(method:Function):void |
|---|
| 1162 | | { |
|---|
| 1163 | | _runHandler = method; |
|---|
| 1164 | | } |
|---|
| 1165 | | |
|---|
| 1166 | | protected function initialize():void |
|---|
| 1167 | | { |
|---|
| 1168 | | } |
|---|
| 1169 | | |
|---|
| 1170 | | protected function execute():void |
|---|
| 1171 | | { |
|---|
| 1172 | | } |
|---|
| 1173 | | |
|---|
| 1174 | | protected function finalize():void |
|---|
| 1175 | | { |
|---|
| 1176 | | } |
|---|
| 1177 | | |
|---|
| 1178 | | protected function catchError(thread:Thread, error:Error):void |
|---|
| 1179 | | { |
|---|
| 1180 | | throw error; |
|---|
| 1181 | | } |
|---|
| 1182 | | |
|---|
| 1183 | | public function toString():String |
|---|
| 1184 | | { |
|---|
| 1185 | | return '[Thread]'; |
|---|
| 1186 | | } |
|---|
| 1187 | | |
|---|
| 1188 | | public function getDebugColor():uint |
|---|
| 1189 | | { |
|---|
| 1190 | | if (isCatchError) { |
|---|
| 1191 | | return 0xff0000; |
|---|
| 1192 | | } |
|---|
| 1193 | | if (isSleeping || isSuspending) { |
|---|
| 1194 | | return 0xff9900; |
|---|
| 1195 | | } |
|---|
| 1196 | | if (isRunning) { |
|---|
| 1197 | | return 0x006666; |
|---|
| 1198 | | } |
|---|
| 1199 | | return 0xcccccc; |
|---|
| 1200 | | } |
|---|
| 1201 | | */ |
|---|