diff options
author | Tedd Hansen | 2008-02-01 22:18:55 +0000 |
---|---|---|
committer | Tedd Hansen | 2008-02-01 22:18:55 +0000 |
commit | a6726b0c9de4ef875348f9dcbf21a9c93bde8a09 (patch) | |
tree | bcf0fbbe7306172e2933f59799813f84ed5a621f /OpenSim/Region/ScriptEngine/Common/ScriptEngineBase/MaintenanceThread.cs | |
parent | Thank you, Kinoc for the ChatModule.cs updates. (diff) | |
download | opensim-SC-a6726b0c9de4ef875348f9dcbf21a9c93bde8a09.zip opensim-SC-a6726b0c9de4ef875348f9dcbf21a9c93bde8a09.tar.gz opensim-SC-a6726b0c9de4ef875348f9dcbf21a9c93bde8a09.tar.bz2 opensim-SC-a6726b0c9de4ef875348f9dcbf21a9c93bde8a09.tar.xz |
SCRIPT SUPPORT IS STILL BROKEN.
Bugfix: Scripts exceeding max and set to be killed were not killed, only removed.
Added ability to re-read configuration while OpenSim is running
All regions now sharing one MaintenanceThread
New MaintenanceThread:
- checks for script execution timeout
- re-reads config
- starts/stops threads if thread active count becomes too high/low compared to config
Speed increase on event execution:
- Reuse of try{}catch{} blocks
- Time calculation on event execution
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Common/ScriptEngineBase/MaintenanceThread.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/Common/ScriptEngineBase/MaintenanceThread.cs | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/Common/ScriptEngineBase/MaintenanceThread.cs b/OpenSim/Region/ScriptEngine/Common/ScriptEngineBase/MaintenanceThread.cs new file mode 100644 index 0000000..9536291 --- /dev/null +++ b/OpenSim/Region/ScriptEngine/Common/ScriptEngineBase/MaintenanceThread.cs | |||
@@ -0,0 +1,127 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Text; | ||
4 | using System.Threading; | ||
5 | |||
6 | namespace OpenSim.Region.ScriptEngine.Common.ScriptEngineBase | ||
7 | { | ||
8 | /// <summary> | ||
9 | /// This class does maintenance on script engine. | ||
10 | /// </summary> | ||
11 | public class MaintenanceThread | ||
12 | { | ||
13 | public ScriptEngine m_ScriptEngine; | ||
14 | private int MaintenanceLoopms; | ||
15 | |||
16 | public MaintenanceThread(ScriptEngine _ScriptEngine) | ||
17 | { | ||
18 | m_ScriptEngine = _ScriptEngine; | ||
19 | |||
20 | ReadConfig(); | ||
21 | |||
22 | // Start maintenance thread | ||
23 | StartMaintenanceThread(); | ||
24 | } | ||
25 | |||
26 | ~MaintenanceThread() | ||
27 | { | ||
28 | StopMaintenanceThread(); | ||
29 | } | ||
30 | |||
31 | private void ReadConfig() | ||
32 | { | ||
33 | MaintenanceLoopms = m_ScriptEngine.ScriptConfigSource.GetInt("MaintenanceLoopms", 50); | ||
34 | } | ||
35 | |||
36 | |||
37 | #region " Maintenance thread " | ||
38 | /// <summary> | ||
39 | /// Maintenance thread. Enforcing max execution time for example. | ||
40 | /// </summary> | ||
41 | public static Thread MaintenanceThreadThread; | ||
42 | |||
43 | /// <summary> | ||
44 | /// Starts maintenance thread | ||
45 | /// </summary> | ||
46 | private void StartMaintenanceThread() | ||
47 | { | ||
48 | StopMaintenanceThread(); | ||
49 | |||
50 | MaintenanceThreadThread = new Thread(MaintenanceLoop); | ||
51 | MaintenanceThreadThread.Name = "ScriptMaintenanceThread"; | ||
52 | MaintenanceThreadThread.IsBackground = true; | ||
53 | MaintenanceThreadThread.Start(); | ||
54 | } | ||
55 | |||
56 | /// <summary> | ||
57 | /// Stops maintenance thread | ||
58 | /// </summary> | ||
59 | private void StopMaintenanceThread() | ||
60 | { | ||
61 | try | ||
62 | { | ||
63 | if (MaintenanceThreadThread != null) | ||
64 | { | ||
65 | if (MaintenanceThreadThread.IsAlive) | ||
66 | { | ||
67 | MaintenanceThreadThread.Abort(); | ||
68 | } | ||
69 | } | ||
70 | } | ||
71 | catch (Exception ex) | ||
72 | { | ||
73 | m_ScriptEngine.Log.Error("EventQueueManager", "Exception stopping maintenence thread: " + ex.ToString()); | ||
74 | } | ||
75 | |||
76 | } | ||
77 | |||
78 | /// <summary> | ||
79 | /// A thread should run in this loop and check all running scripts | ||
80 | /// </summary> | ||
81 | public void MaintenanceLoop() | ||
82 | { | ||
83 | try | ||
84 | { | ||
85 | long Last_maxFunctionExecutionTimens = 0;// DateTime.Now.Ticks; | ||
86 | long Last_ReReadConfigFilens = DateTime.Now.Ticks; | ||
87 | while (true) | ||
88 | { | ||
89 | System.Threading.Thread.Sleep(MaintenanceLoopms); // Sleep | ||
90 | |||
91 | // Re-reading config every x seconds? | ||
92 | if (m_ScriptEngine.ReReadConfigFileSeconds > 0) | ||
93 | { | ||
94 | // Check if its time to re-read config | ||
95 | if (DateTime.Now.Ticks - Last_ReReadConfigFilens > m_ScriptEngine.ReReadConfigFilens) | ||
96 | { | ||
97 | // Its time to re-read config file | ||
98 | m_ScriptEngine.ConfigSource.Reload(); // Re-read config | ||
99 | Last_ReReadConfigFilens = DateTime.Now.Ticks; // Reset time | ||
100 | } | ||
101 | } | ||
102 | |||
103 | // Adjust number of running script threads if not correct | ||
104 | if (m_ScriptEngine.m_EventQueueManager.eventQueueThreads.Count != m_ScriptEngine.m_EventQueueManager.numberOfThreads) | ||
105 | { | ||
106 | m_ScriptEngine.m_EventQueueManager.AdjustNumberOfScriptThreads(); | ||
107 | } | ||
108 | |||
109 | |||
110 | // Check if any script has exceeded its max execution time | ||
111 | if (m_ScriptEngine.m_EventQueueManager.EnforceMaxExecutionTime) | ||
112 | { | ||
113 | if (DateTime.Now.Ticks - Last_maxFunctionExecutionTimens > m_ScriptEngine.m_EventQueueManager.maxFunctionExecutionTimens) | ||
114 | { | ||
115 | m_ScriptEngine.m_EventQueueManager.CheckScriptMaxExecTime(); // Do check | ||
116 | Last_maxFunctionExecutionTimens = DateTime.Now.Ticks; // Reset time | ||
117 | } | ||
118 | } | ||
119 | } | ||
120 | } | ||
121 | catch (ThreadAbortException tae) | ||
122 | { | ||
123 | } | ||
124 | } | ||
125 | #endregion | ||
126 | } | ||
127 | } | ||