diff options
author | Melanie | 2009-10-27 20:28:12 +0000 |
---|---|---|
committer | Melanie | 2009-10-27 20:28:12 +0000 |
commit | 0d37883bfdb1c8e15187f63f170765c7909a6650 (patch) | |
tree | 25a5d722292d038d1ca314199b29efed54aed426 /OpenSim/Region/ScriptEngine/DotNetEngine/AppDomainManager.cs | |
parent | Merge branch 'master' into vehicles (diff) | |
parent | Remove the rest of SECS. It was never used, except by an experimental version (diff) | |
download | opensim-SC-0d37883bfdb1c8e15187f63f170765c7909a6650.zip opensim-SC-0d37883bfdb1c8e15187f63f170765c7909a6650.tar.gz opensim-SC-0d37883bfdb1c8e15187f63f170765c7909a6650.tar.bz2 opensim-SC-0d37883bfdb1c8e15187f63f170765c7909a6650.tar.xz |
Merge branch 'master' into vehicles
Diffstat (limited to 'OpenSim/Region/ScriptEngine/DotNetEngine/AppDomainManager.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/DotNetEngine/AppDomainManager.cs | 234 |
1 files changed, 0 insertions, 234 deletions
diff --git a/OpenSim/Region/ScriptEngine/DotNetEngine/AppDomainManager.cs b/OpenSim/Region/ScriptEngine/DotNetEngine/AppDomainManager.cs deleted file mode 100644 index 7116512..0000000 --- a/OpenSim/Region/ScriptEngine/DotNetEngine/AppDomainManager.cs +++ /dev/null | |||
@@ -1,234 +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.Collections.Generic; | ||
31 | using System.Reflection; | ||
32 | using System.Security; | ||
33 | using System.Security.Policy; | ||
34 | using System.Security.Permissions; | ||
35 | using OpenSim.Region.ScriptEngine.Interfaces; | ||
36 | using OpenSim.Region.ScriptEngine.Shared.ScriptBase; | ||
37 | using log4net; | ||
38 | |||
39 | namespace OpenSim.Region.ScriptEngine.DotNetEngine | ||
40 | { | ||
41 | public class AppDomainManager | ||
42 | { | ||
43 | // | ||
44 | // This class does AppDomain handling and loading/unloading of | ||
45 | // scripts in it. It is instanced in "ScriptEngine" and controlled | ||
46 | // from "ScriptManager" | ||
47 | // | ||
48 | // 1. Create a new AppDomain if old one is full (or doesn't exist) | ||
49 | // 2. Load scripts into AppDomain | ||
50 | // 3. Unload scripts from AppDomain (stopping them and marking | ||
51 | // them as inactive) | ||
52 | // 4. Unload AppDomain completely when all scripts in it has stopped | ||
53 | // | ||
54 | |||
55 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
56 | |||
57 | private int maxScriptsPerAppDomain = 1; | ||
58 | |||
59 | // Internal list of all AppDomains | ||
60 | private List<AppDomainStructure> appDomains = | ||
61 | new List<AppDomainStructure>(); | ||
62 | |||
63 | // Structure to keep track of data around AppDomain | ||
64 | private class AppDomainStructure | ||
65 | { | ||
66 | // The AppDomain itself | ||
67 | public AppDomain CurrentAppDomain; | ||
68 | |||
69 | // Number of scripts loaded into AppDomain | ||
70 | public int ScriptsLoaded; | ||
71 | |||
72 | // Number of dead scripts | ||
73 | public int ScriptsWaitingUnload; | ||
74 | } | ||
75 | |||
76 | // Current AppDomain | ||
77 | private AppDomainStructure currentAD; | ||
78 | |||
79 | private object getLock = new object(); // Mutex | ||
80 | private object freeLock = new object(); // Mutex | ||
81 | |||
82 | private ScriptEngine m_scriptEngine; | ||
83 | //public AppDomainManager(ScriptEngine scriptEngine) | ||
84 | public AppDomainManager(ScriptEngine scriptEngine) | ||
85 | { | ||
86 | m_scriptEngine = scriptEngine; | ||
87 | ReadConfig(); | ||
88 | } | ||
89 | |||
90 | public void ReadConfig() | ||
91 | { | ||
92 | maxScriptsPerAppDomain = m_scriptEngine.ScriptConfigSource.GetInt( | ||
93 | "ScriptsPerAppDomain", 1); | ||
94 | } | ||
95 | |||
96 | // Find a free AppDomain, creating one if necessary | ||
97 | private AppDomainStructure GetFreeAppDomain() | ||
98 | { | ||
99 | lock (getLock) | ||
100 | { | ||
101 | // Current full? | ||
102 | if (currentAD != null && | ||
103 | currentAD.ScriptsLoaded >= maxScriptsPerAppDomain) | ||
104 | { | ||
105 | // Add it to AppDomains list and empty current | ||
106 | appDomains.Add(currentAD); | ||
107 | currentAD = null; | ||
108 | } | ||
109 | // No current | ||
110 | if (currentAD == null) | ||
111 | { | ||
112 | // Create a new current AppDomain | ||
113 | currentAD = new AppDomainStructure(); | ||
114 | currentAD.CurrentAppDomain = PrepareNewAppDomain(); | ||
115 | } | ||
116 | |||
117 | return currentAD; | ||
118 | } | ||
119 | } | ||
120 | |||
121 | private int AppDomainNameCount; | ||
122 | |||
123 | // Create and prepare a new AppDomain for scripts | ||
124 | private AppDomain PrepareNewAppDomain() | ||
125 | { | ||
126 | // Create and prepare a new AppDomain | ||
127 | AppDomainNameCount++; | ||
128 | |||
129 | // TODO: Currently security match current appdomain | ||
130 | |||
131 | // Construct and initialize settings for a second AppDomain. | ||
132 | AppDomainSetup ads = new AppDomainSetup(); | ||
133 | ads.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory; | ||
134 | ads.DisallowBindingRedirects = true; | ||
135 | ads.DisallowCodeDownload = true; | ||
136 | ads.LoaderOptimization = LoaderOptimization.MultiDomainHost; | ||
137 | ads.ShadowCopyFiles = "false"; // Disable shadowing | ||
138 | ads.ConfigurationFile = | ||
139 | AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; | ||
140 | |||
141 | AppDomain AD = AppDomain.CreateDomain("ScriptAppDomain_" + | ||
142 | AppDomainNameCount, null, ads); | ||
143 | |||
144 | m_log.Info("[" + m_scriptEngine.ScriptEngineName + | ||
145 | "]: AppDomain Loading: " + | ||
146 | AssemblyName.GetAssemblyName( | ||
147 | "OpenSim.Region.ScriptEngine.Shared.dll").ToString()); | ||
148 | AD.Load(AssemblyName.GetAssemblyName( | ||
149 | "OpenSim.Region.ScriptEngine.Shared.dll")); | ||
150 | |||
151 | // Return the new AppDomain | ||
152 | return AD; | ||
153 | } | ||
154 | |||
155 | // Unload appdomains that are full and have only dead scripts | ||
156 | private void UnloadAppDomains() | ||
157 | { | ||
158 | lock (freeLock) | ||
159 | { | ||
160 | // Go through all | ||
161 | foreach (AppDomainStructure ads in new ArrayList(appDomains)) | ||
162 | { | ||
163 | // Don't process current AppDomain | ||
164 | if (ads.CurrentAppDomain != currentAD.CurrentAppDomain) | ||
165 | { | ||
166 | // Not current AppDomain | ||
167 | // Is number of unloaded bigger or equal to number of loaded? | ||
168 | if (ads.ScriptsLoaded <= ads.ScriptsWaitingUnload) | ||
169 | { | ||
170 | // Remove from internal list | ||
171 | appDomains.Remove(ads); | ||
172 | |||
173 | // Unload | ||
174 | AppDomain.Unload(ads.CurrentAppDomain); | ||
175 | } | ||
176 | } | ||
177 | } | ||
178 | } | ||
179 | } | ||
180 | |||
181 | public IScript LoadScript(string FileName, out AppDomain ad) | ||
182 | { | ||
183 | // Find next available AppDomain to put it in | ||
184 | AppDomainStructure FreeAppDomain = GetFreeAppDomain(); | ||
185 | |||
186 | IScript mbrt = (IScript) | ||
187 | FreeAppDomain.CurrentAppDomain.CreateInstanceFromAndUnwrap( | ||
188 | FileName, "SecondLife.Script"); | ||
189 | |||
190 | FreeAppDomain.ScriptsLoaded++; | ||
191 | ad = FreeAppDomain.CurrentAppDomain; | ||
192 | |||
193 | return mbrt; | ||
194 | } | ||
195 | |||
196 | |||
197 | // Increase "dead script" counter for an AppDomain | ||
198 | public void StopScript(AppDomain ad) | ||
199 | { | ||
200 | lock (freeLock) | ||
201 | { | ||
202 | // Check if it is current AppDomain | ||
203 | if (currentAD.CurrentAppDomain == ad) | ||
204 | { | ||
205 | // Yes - increase | ||
206 | currentAD.ScriptsWaitingUnload++; | ||
207 | return; | ||
208 | } | ||
209 | |||
210 | // Lopp through all AppDomains | ||
211 | foreach (AppDomainStructure ads in new ArrayList(appDomains)) | ||
212 | { | ||
213 | if (ads.CurrentAppDomain == ad) | ||
214 | { | ||
215 | // Found it | ||
216 | ads.ScriptsWaitingUnload++; | ||
217 | break; | ||
218 | } | ||
219 | } | ||
220 | } | ||
221 | |||
222 | UnloadAppDomains(); // Outsite lock, has its own GetLock | ||
223 | } | ||
224 | |||
225 | // If set to true then threads and stuff should try | ||
226 | // to make a graceful exit | ||
227 | public bool PleaseShutdown | ||
228 | { | ||
229 | get { return _PleaseShutdown; } | ||
230 | set { _PleaseShutdown = value; } | ||
231 | } | ||
232 | private bool _PleaseShutdown = false; | ||
233 | } | ||
234 | } | ||