diff options
Diffstat (limited to 'OpenSim/Region/ScriptEngine/DotNetEngine/MaintenanceThread.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/DotNetEngine/MaintenanceThread.cs | 241 |
1 files changed, 0 insertions, 241 deletions
diff --git a/OpenSim/Region/ScriptEngine/DotNetEngine/MaintenanceThread.cs b/OpenSim/Region/ScriptEngine/DotNetEngine/MaintenanceThread.cs deleted file mode 100644 index 7ffdb1a..0000000 --- a/OpenSim/Region/ScriptEngine/DotNetEngine/MaintenanceThread.cs +++ /dev/null | |||
@@ -1,241 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSimulator Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.Collections; | ||
30 | using System.Reflection; | ||
31 | using System.Threading; | ||
32 | using log4net; | ||
33 | using OpenSim.Framework; | ||
34 | |||
35 | namespace OpenSim.Region.ScriptEngine.DotNetEngine | ||
36 | { | ||
37 | /// <summary> | ||
38 | /// This class does maintenance on script engine. | ||
39 | /// </summary> | ||
40 | public class MaintenanceThread | ||
41 | { | ||
42 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
43 | |||
44 | //public ScriptEngine m_ScriptEngine; | ||
45 | private int MaintenanceLoopms; | ||
46 | private int MaintenanceLoopTicks_ScriptLoadUnload; | ||
47 | private int MaintenanceLoopTicks_Other; | ||
48 | |||
49 | |||
50 | public MaintenanceThread() | ||
51 | { | ||
52 | //m_ScriptEngine = _ScriptEngine; | ||
53 | |||
54 | ReadConfig(); | ||
55 | |||
56 | // Start maintenance thread | ||
57 | StartMaintenanceThread(); | ||
58 | } | ||
59 | |||
60 | ~MaintenanceThread() | ||
61 | { | ||
62 | StopMaintenanceThread(); | ||
63 | } | ||
64 | |||
65 | public void ReadConfig() | ||
66 | { | ||
67 | // Bad hack, but we need a m_ScriptEngine :) | ||
68 | lock (ScriptEngine.ScriptEngines) | ||
69 | { | ||
70 | foreach (ScriptEngine m_ScriptEngine in ScriptEngine.ScriptEngines) | ||
71 | { | ||
72 | MaintenanceLoopms = m_ScriptEngine.ScriptConfigSource.GetInt("MaintenanceLoopms", 50); | ||
73 | MaintenanceLoopTicks_ScriptLoadUnload = | ||
74 | m_ScriptEngine.ScriptConfigSource.GetInt("MaintenanceLoopTicks_ScriptLoadUnload", 1); | ||
75 | MaintenanceLoopTicks_Other = | ||
76 | m_ScriptEngine.ScriptConfigSource.GetInt("MaintenanceLoopTicks_Other", 10); | ||
77 | |||
78 | return; | ||
79 | } | ||
80 | } | ||
81 | } | ||
82 | |||
83 | #region " Maintenance thread " | ||
84 | /// <summary> | ||
85 | /// Maintenance thread. Enforcing max execution time for example. | ||
86 | /// </summary> | ||
87 | public Thread MaintenanceThreadThread; | ||
88 | |||
89 | /// <summary> | ||
90 | /// Starts maintenance thread | ||
91 | /// </summary> | ||
92 | private void StartMaintenanceThread() | ||
93 | { | ||
94 | if (MaintenanceThreadThread == null) | ||
95 | { | ||
96 | MaintenanceThreadThread = new Thread(MaintenanceLoop); | ||
97 | MaintenanceThreadThread.Name = "ScriptMaintenanceThread"; | ||
98 | MaintenanceThreadThread.IsBackground = true; | ||
99 | MaintenanceThreadThread.Start(); | ||
100 | } | ||
101 | } | ||
102 | |||
103 | /// <summary> | ||
104 | /// Stops maintenance thread | ||
105 | /// </summary> | ||
106 | private void StopMaintenanceThread() | ||
107 | { | ||
108 | #if DEBUG | ||
109 | //m_log.Debug("[" + m_ScriptEngine.ScriptEngineName + "]: StopMaintenanceThread() called"); | ||
110 | #endif | ||
111 | //PleaseShutdown = true; | ||
112 | Thread.Sleep(100); | ||
113 | try | ||
114 | { | ||
115 | if (MaintenanceThreadThread != null && MaintenanceThreadThread.IsAlive) | ||
116 | { | ||
117 | MaintenanceThreadThread.Abort(); | ||
118 | } | ||
119 | } | ||
120 | catch (Exception) | ||
121 | { | ||
122 | //m_log.Error("[" + m_ScriptEngine.ScriptEngineName + "]: Exception stopping maintenence thread: " + ex.ToString()); | ||
123 | } | ||
124 | } | ||
125 | |||
126 | // private ScriptEngine lastScriptEngine; // Keep track of what ScriptEngine instance we are at so we can give exception | ||
127 | /// <summary> | ||
128 | /// A thread should run in this loop and check all running scripts | ||
129 | /// </summary> | ||
130 | public void MaintenanceLoop() | ||
131 | { | ||
132 | //if (m_ScriptEngine.m_EventQueueManager.maxFunctionExecutionTimens < MaintenanceLoopms) | ||
133 | // m_log.Warn("[" + m_ScriptEngine.ScriptEngineName + "]: " + | ||
134 | // "Configuration error: MaxEventExecutionTimeMs is less than MaintenanceLoopms. The Maintenance Loop will only check scripts once per run."); | ||
135 | |||
136 | long Last_maxFunctionExecutionTimens = 0; // DateTime.Now.Ticks; | ||
137 | long Last_ReReadConfigFilens = DateTime.Now.Ticks; | ||
138 | int MaintenanceLoopTicks_ScriptLoadUnload_Count = 0; | ||
139 | int MaintenanceLoopTicks_Other_Count = 0; | ||
140 | bool MaintenanceLoopTicks_ScriptLoadUnload_ResetCount = false; | ||
141 | bool MaintenanceLoopTicks_Other_ResetCount = false; | ||
142 | |||
143 | while (true) | ||
144 | { | ||
145 | try | ||
146 | { | ||
147 | while (true) | ||
148 | { | ||
149 | Thread.Sleep(MaintenanceLoopms); // Sleep before next pass | ||
150 | |||
151 | // Reset counters? | ||
152 | if (MaintenanceLoopTicks_ScriptLoadUnload_ResetCount) | ||
153 | { | ||
154 | MaintenanceLoopTicks_ScriptLoadUnload_ResetCount = false; | ||
155 | MaintenanceLoopTicks_ScriptLoadUnload_Count = 0; | ||
156 | } | ||
157 | if (MaintenanceLoopTicks_Other_ResetCount) | ||
158 | { | ||
159 | MaintenanceLoopTicks_Other_ResetCount = false; | ||
160 | MaintenanceLoopTicks_Other_Count = 0; | ||
161 | } | ||
162 | |||
163 | // Increase our counters | ||
164 | MaintenanceLoopTicks_ScriptLoadUnload_Count++; | ||
165 | MaintenanceLoopTicks_Other_Count++; | ||
166 | |||
167 | |||
168 | //lock (ScriptEngine.ScriptEngines) | ||
169 | //{ | ||
170 | foreach (ScriptEngine m_ScriptEngine in new ArrayList(ScriptEngine.ScriptEngines)) | ||
171 | { | ||
172 | // lastScriptEngine = m_ScriptEngine; | ||
173 | // Re-reading config every x seconds | ||
174 | if (MaintenanceLoopTicks_Other_Count >= MaintenanceLoopTicks_Other) | ||
175 | { | ||
176 | MaintenanceLoopTicks_Other_ResetCount = true; | ||
177 | if (m_ScriptEngine.RefreshConfigFilens > 0) | ||
178 | { | ||
179 | // Check if its time to re-read config | ||
180 | if (DateTime.Now.Ticks - Last_ReReadConfigFilens > | ||
181 | m_ScriptEngine.RefreshConfigFilens) | ||
182 | { | ||
183 | //m_log.Debug("Time passed: " + (DateTime.Now.Ticks - Last_ReReadConfigFilens) + ">" + m_ScriptEngine.RefreshConfigFilens); | ||
184 | // Its time to re-read config file | ||
185 | m_ScriptEngine.ReadConfig(); | ||
186 | Last_ReReadConfigFilens = DateTime.Now.Ticks; // Reset time | ||
187 | } | ||
188 | |||
189 | |||
190 | // Adjust number of running script threads if not correct | ||
191 | if (m_ScriptEngine.m_EventQueueManager != null) | ||
192 | m_ScriptEngine.m_EventQueueManager.AdjustNumberOfScriptThreads(); | ||
193 | |||
194 | // Check if any script has exceeded its max execution time | ||
195 | if (EventQueueManager.EnforceMaxExecutionTime) | ||
196 | { | ||
197 | // We are enforcing execution time | ||
198 | if (DateTime.Now.Ticks - Last_maxFunctionExecutionTimens > | ||
199 | EventQueueManager.maxFunctionExecutionTimens) | ||
200 | { | ||
201 | // Its time to check again | ||
202 | m_ScriptEngine.m_EventQueueManager.CheckScriptMaxExecTime(); // Do check | ||
203 | Last_maxFunctionExecutionTimens = DateTime.Now.Ticks; // Reset time | ||
204 | } | ||
205 | } | ||
206 | } | ||
207 | } | ||
208 | if (MaintenanceLoopTicks_ScriptLoadUnload_Count >= MaintenanceLoopTicks_ScriptLoadUnload) | ||
209 | { | ||
210 | MaintenanceLoopTicks_ScriptLoadUnload_ResetCount = true; | ||
211 | // LOAD / UNLOAD SCRIPTS | ||
212 | if (m_ScriptEngine.m_ScriptManager != null) | ||
213 | m_ScriptEngine.m_ScriptManager.DoScriptLoadUnload(); | ||
214 | } | ||
215 | } | ||
216 | //} | ||
217 | } | ||
218 | } | ||
219 | catch(ThreadAbortException) | ||
220 | { | ||
221 | m_log.Error("Thread aborted in MaintenanceLoopThread. If this is during shutdown, please ignore"); | ||
222 | } | ||
223 | catch (Exception ex) | ||
224 | { | ||
225 | m_log.ErrorFormat("Exception in MaintenanceLoopThread. Thread will recover after 5 sec throttle. Exception: {0}", ex.ToString()); | ||
226 | } | ||
227 | } | ||
228 | } | ||
229 | #endregion | ||
230 | |||
231 | ///// <summary> | ||
232 | ///// If set to true then threads and stuff should try to make a graceful exit | ||
233 | ///// </summary> | ||
234 | //public bool PleaseShutdown | ||
235 | //{ | ||
236 | // get { return _PleaseShutdown; } | ||
237 | // set { _PleaseShutdown = value; } | ||
238 | //} | ||
239 | //private bool _PleaseShutdown = false; | ||
240 | } | ||
241 | } | ||