diff options
Diffstat (limited to 'OpenSim/Region/ScriptEngine/DotNetEngine/MaintenanceThread.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/DotNetEngine/MaintenanceThread.cs | 238 |
1 files changed, 0 insertions, 238 deletions
diff --git a/OpenSim/Region/ScriptEngine/DotNetEngine/MaintenanceThread.cs b/OpenSim/Region/ScriptEngine/DotNetEngine/MaintenanceThread.cs deleted file mode 100644 index 87fdf1f..0000000 --- a/OpenSim/Region/ScriptEngine/DotNetEngine/MaintenanceThread.cs +++ /dev/null | |||
@@ -1,238 +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 = Watchdog.StartThread(MaintenanceLoop, "ScriptMaintenanceThread", ThreadPriority.Normal, true); | ||
97 | } | ||
98 | } | ||
99 | |||
100 | /// <summary> | ||
101 | /// Stops maintenance thread | ||
102 | /// </summary> | ||
103 | private void StopMaintenanceThread() | ||
104 | { | ||
105 | #if DEBUG | ||
106 | //m_log.Debug("[" + m_ScriptEngine.ScriptEngineName + "]: StopMaintenanceThread() called"); | ||
107 | #endif | ||
108 | //PleaseShutdown = true; | ||
109 | Thread.Sleep(100); | ||
110 | try | ||
111 | { | ||
112 | if (MaintenanceThreadThread != null && MaintenanceThreadThread.IsAlive) | ||
113 | { | ||
114 | MaintenanceThreadThread.Abort(); | ||
115 | } | ||
116 | } | ||
117 | catch (Exception) | ||
118 | { | ||
119 | //m_log.Error("[" + m_ScriptEngine.ScriptEngineName + "]: Exception stopping maintenence thread: " + ex.ToString()); | ||
120 | } | ||
121 | } | ||
122 | |||
123 | // private ScriptEngine lastScriptEngine; // Keep track of what ScriptEngine instance we are at so we can give exception | ||
124 | /// <summary> | ||
125 | /// A thread should run in this loop and check all running scripts | ||
126 | /// </summary> | ||
127 | public void MaintenanceLoop() | ||
128 | { | ||
129 | //if (m_ScriptEngine.m_EventQueueManager.maxFunctionExecutionTimens < MaintenanceLoopms) | ||
130 | // m_log.Warn("[" + m_ScriptEngine.ScriptEngineName + "]: " + | ||
131 | // "Configuration error: MaxEventExecutionTimeMs is less than MaintenanceLoopms. The Maintenance Loop will only check scripts once per run."); | ||
132 | |||
133 | long Last_maxFunctionExecutionTimens = 0; // DateTime.Now.Ticks; | ||
134 | long Last_ReReadConfigFilens = DateTime.Now.Ticks; | ||
135 | int MaintenanceLoopTicks_ScriptLoadUnload_Count = 0; | ||
136 | int MaintenanceLoopTicks_Other_Count = 0; | ||
137 | bool MaintenanceLoopTicks_ScriptLoadUnload_ResetCount = false; | ||
138 | bool MaintenanceLoopTicks_Other_ResetCount = false; | ||
139 | |||
140 | while (true) | ||
141 | { | ||
142 | try | ||
143 | { | ||
144 | while (true) | ||
145 | { | ||
146 | Thread.Sleep(MaintenanceLoopms); // Sleep before next pass | ||
147 | |||
148 | // Reset counters? | ||
149 | if (MaintenanceLoopTicks_ScriptLoadUnload_ResetCount) | ||
150 | { | ||
151 | MaintenanceLoopTicks_ScriptLoadUnload_ResetCount = false; | ||
152 | MaintenanceLoopTicks_ScriptLoadUnload_Count = 0; | ||
153 | } | ||
154 | if (MaintenanceLoopTicks_Other_ResetCount) | ||
155 | { | ||
156 | MaintenanceLoopTicks_Other_ResetCount = false; | ||
157 | MaintenanceLoopTicks_Other_Count = 0; | ||
158 | } | ||
159 | |||
160 | // Increase our counters | ||
161 | MaintenanceLoopTicks_ScriptLoadUnload_Count++; | ||
162 | MaintenanceLoopTicks_Other_Count++; | ||
163 | |||
164 | foreach (ScriptEngine m_ScriptEngine in new ArrayList(ScriptEngine.ScriptEngines)) | ||
165 | { | ||
166 | // lastScriptEngine = m_ScriptEngine; | ||
167 | // Re-reading config every x seconds | ||
168 | if (MaintenanceLoopTicks_Other_Count >= MaintenanceLoopTicks_Other) | ||
169 | { | ||
170 | MaintenanceLoopTicks_Other_ResetCount = true; | ||
171 | if (m_ScriptEngine.RefreshConfigFilens > 0) | ||
172 | { | ||
173 | // Check if its time to re-read config | ||
174 | if (DateTime.Now.Ticks - Last_ReReadConfigFilens > | ||
175 | m_ScriptEngine.RefreshConfigFilens) | ||
176 | { | ||
177 | //m_log.Debug("Time passed: " + (DateTime.Now.Ticks - Last_ReReadConfigFilens) + ">" + m_ScriptEngine.RefreshConfigFilens); | ||
178 | // Its time to re-read config file | ||
179 | m_ScriptEngine.ReadConfig(); | ||
180 | Last_ReReadConfigFilens = DateTime.Now.Ticks; // Reset time | ||
181 | } | ||
182 | |||
183 | |||
184 | // Adjust number of running script threads if not correct | ||
185 | if (m_ScriptEngine.m_EventQueueManager != null) | ||
186 | m_ScriptEngine.m_EventQueueManager.AdjustNumberOfScriptThreads(); | ||
187 | |||
188 | // Check if any script has exceeded its max execution time | ||
189 | if (EventQueueManager.EnforceMaxExecutionTime) | ||
190 | { | ||
191 | // We are enforcing execution time | ||
192 | if (DateTime.Now.Ticks - Last_maxFunctionExecutionTimens > | ||
193 | EventQueueManager.maxFunctionExecutionTimens) | ||
194 | { | ||
195 | // Its time to check again | ||
196 | m_ScriptEngine.m_EventQueueManager.CheckScriptMaxExecTime(); // Do check | ||
197 | Last_maxFunctionExecutionTimens = DateTime.Now.Ticks; // Reset time | ||
198 | } | ||
199 | } | ||
200 | } | ||
201 | } | ||
202 | if (MaintenanceLoopTicks_ScriptLoadUnload_Count >= MaintenanceLoopTicks_ScriptLoadUnload) | ||
203 | { | ||
204 | MaintenanceLoopTicks_ScriptLoadUnload_ResetCount = true; | ||
205 | // LOAD / UNLOAD SCRIPTS | ||
206 | if (m_ScriptEngine.m_ScriptManager != null) | ||
207 | m_ScriptEngine.m_ScriptManager.DoScriptLoadUnload(); | ||
208 | } | ||
209 | } | ||
210 | |||
211 | Watchdog.UpdateThread(); | ||
212 | } | ||
213 | } | ||
214 | catch(ThreadAbortException) | ||
215 | { | ||
216 | m_log.Error("Thread aborted in MaintenanceLoopThread. If this is during shutdown, please ignore"); | ||
217 | } | ||
218 | catch (Exception ex) | ||
219 | { | ||
220 | m_log.ErrorFormat("Exception in MaintenanceLoopThread. Thread will recover after 5 sec throttle. Exception: {0}", ex.ToString()); | ||
221 | } | ||
222 | } | ||
223 | |||
224 | Watchdog.RemoveThread(); | ||
225 | } | ||
226 | #endregion | ||
227 | |||
228 | ///// <summary> | ||
229 | ///// If set to true then threads and stuff should try to make a graceful exit | ||
230 | ///// </summary> | ||
231 | //public bool PleaseShutdown | ||
232 | //{ | ||
233 | // get { return _PleaseShutdown; } | ||
234 | // set { _PleaseShutdown = value; } | ||
235 | //} | ||
236 | //private bool _PleaseShutdown = false; | ||
237 | } | ||
238 | } | ||