aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/DotNetEngine/AppDomainManager.cs
diff options
context:
space:
mode:
authorTedd Hansen2008-01-12 14:30:22 +0000
committerTedd Hansen2008-01-12 14:30:22 +0000
commitbacbade369a5244f9bcc611488b59f3bd4c8a564 (patch)
tree2cd909eff401066a69dba96615cbf736fdd73be5 /OpenSim/Region/ScriptEngine/DotNetEngine/AppDomainManager.cs
parent* Trying something to see if it helps teleports and border crossings (diff)
downloadopensim-SC_OLD-bacbade369a5244f9bcc611488b59f3bd4c8a564.zip
opensim-SC_OLD-bacbade369a5244f9bcc611488b59f3bd4c8a564.tar.gz
opensim-SC_OLD-bacbade369a5244f9bcc611488b59f3bd4c8a564.tar.bz2
opensim-SC_OLD-bacbade369a5244f9bcc611488b59f3bd4c8a564.tar.xz
Major reorganizing of DotNetEngine. Moved common script engine parts to ScriptEngine.Common, only .Net-specific code in DotNetEngine. AppDomains, event handling, event execution queue and multithreading, script load/unload queue, etc has been moved to ScriptEngine.Common.
Loads of things has been put into interfaces instead of the specific class. We are now one step closer to ScriptServer, and its very easy to implement new script languages. Just a few lines required to make them a OpenSim script module with all its glory.
Diffstat (limited to 'OpenSim/Region/ScriptEngine/DotNetEngine/AppDomainManager.cs')
-rw-r--r--OpenSim/Region/ScriptEngine/DotNetEngine/AppDomainManager.cs238
1 files changed, 0 insertions, 238 deletions
diff --git a/OpenSim/Region/ScriptEngine/DotNetEngine/AppDomainManager.cs b/OpenSim/Region/ScriptEngine/DotNetEngine/AppDomainManager.cs
deleted file mode 100644
index 66153a7..0000000
--- a/OpenSim/Region/ScriptEngine/DotNetEngine/AppDomainManager.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 OpenSim 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
29using System;
30using System.Collections;
31using System.Collections.Generic;
32using System.Reflection;
33using OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL;
34
35namespace OpenSim.Region.ScriptEngine.DotNetEngine
36{
37 public class AppDomainManager
38 {
39
40 //
41 // This class does AppDomain handling and loading/unloading of scripts in it.
42 // It is instanced in "ScriptEngine" and controlled from "ScriptManager"
43 //
44 // 1. Create a new AppDomain if old one is full (or doesn't exist)
45 // 2. Load scripts into AppDomain
46 // 3. Unload scripts from AppDomain (stopping them and marking them as inactive)
47 // 4. Unload AppDomain completely when all scripts in it has stopped
48 //
49
50
51 private int maxScriptsPerAppDomain = 1;
52
53 /// <summary>
54 /// Internal list of all AppDomains
55 /// </summary>
56 private List<AppDomainStructure> appDomains = new List<AppDomainStructure>();
57
58 /// <summary>
59 /// Structure to keep track of data around AppDomain
60 /// </summary>
61 private class AppDomainStructure
62 {
63 /// <summary>
64 /// The AppDomain itself
65 /// </summary>
66 public AppDomain CurrentAppDomain;
67
68 /// <summary>
69 /// Number of scripts loaded into AppDomain
70 /// </summary>
71 public int ScriptsLoaded;
72
73 /// <summary>
74 /// Number of dead scripts
75 /// </summary>
76 public int ScriptsWaitingUnload;
77 }
78
79 /// <summary>
80 /// Current AppDomain
81 /// </summary>
82 private AppDomainStructure currentAD;
83
84 private object getLock = new object(); // Mutex
85 private object freeLock = new object(); // Mutex
86
87 //private ScriptEngine m_scriptEngine;
88 //public AppDomainManager(ScriptEngine scriptEngine)
89 public AppDomainManager()
90 {
91 //m_scriptEngine = scriptEngine;
92 }
93
94 /// <summary>
95 /// Find a free AppDomain, creating one if necessary
96 /// </summary>
97 /// <returns>Free AppDomain</returns>
98 private AppDomainStructure GetFreeAppDomain()
99 {
100 Console.WriteLine("Finding free AppDomain");
101 lock (getLock)
102 {
103 // Current full?
104 if (currentAD != null && currentAD.ScriptsLoaded >= maxScriptsPerAppDomain)
105 {
106 // Add it to AppDomains list and empty current
107 appDomains.Add(currentAD);
108 currentAD = null;
109 }
110 // No current
111 if (currentAD == null)
112 {
113 // Create a new current AppDomain
114 currentAD = new AppDomainStructure();
115 currentAD.CurrentAppDomain = PrepareNewAppDomain();
116 }
117
118 Console.WriteLine("Scripts loaded in this Appdomain: " + currentAD.ScriptsLoaded);
119 return currentAD;
120 } // lock
121 }
122
123 private int AppDomainNameCount;
124
125 /// <summary>
126 /// Create and prepare a new AppDomain for scripts
127 /// </summary>
128 /// <returns>The new AppDomain</returns>
129 private AppDomain PrepareNewAppDomain()
130 {
131 // Create and prepare a new AppDomain
132 AppDomainNameCount++;
133 // TODO: Currently security match current appdomain
134
135 // Construct and initialize settings for a second AppDomain.
136 AppDomainSetup ads = new AppDomainSetup();
137 ads.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
138 ads.DisallowBindingRedirects = false;
139 ads.DisallowCodeDownload = true;
140 ads.LoaderOptimization = LoaderOptimization.MultiDomain; // Sounds good ;)
141 ads.ShadowCopyFiles = "true"; // Enabled shadowing
142 ads.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
143
144 AppDomain AD = AppDomain.CreateDomain("ScriptAppDomain_" + AppDomainNameCount, null, ads);
145 Console.WriteLine("Loading: " +
146 AssemblyName.GetAssemblyName("OpenSim.Region.ScriptEngine.Common.dll").ToString());
147 AD.Load(AssemblyName.GetAssemblyName("OpenSim.Region.ScriptEngine.Common.dll"));
148
149 // Return the new AppDomain
150 return AD;
151 }
152
153 /// <summary>
154 /// Unload appdomains that are full and have only dead scripts
155 /// </summary>
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 Console.WriteLine("Found empty AppDomain, unloading");
171 // Remove from internal list
172 appDomains.Remove(ads);
173#if DEBUG
174 long m = GC.GetTotalMemory(true);
175#endif
176 // Unload
177 AppDomain.Unload(ads.CurrentAppDomain);
178#if DEBUG
179 Console.WriteLine("AppDomain unload freed " + (m - GC.GetTotalMemory(true)) +
180 " bytes of memory");
181#endif
182 }
183 }
184 } // foreach
185 } // lock
186 }
187
188
189 public LSL_BaseClass LoadScript(string FileName)
190 {
191 // Find next available AppDomain to put it in
192 AppDomainStructure FreeAppDomain = GetFreeAppDomain();
193
194 Console.WriteLine("Loading into AppDomain: " + FileName);
195 LSL_BaseClass mbrt =
196 (LSL_BaseClass)
197 FreeAppDomain.CurrentAppDomain.CreateInstanceFromAndUnwrap(FileName, "SecondLife.Script");
198 //Console.WriteLine("ScriptEngine AppDomainManager: is proxy={0}", RemotingServices.IsTransparentProxy(mbrt));
199 FreeAppDomain.ScriptsLoaded++;
200
201 return mbrt;
202 }
203
204
205 /// <summary>
206 /// Increase "dead script" counter for an AppDomain
207 /// </summary>
208 /// <param name="ad"></param>
209 //[Obsolete("Needs fixing, needs a real purpose in life!!!")]
210 public void StopScript(AppDomain ad)
211 {
212 lock (freeLock)
213 {
214 Console.WriteLine("Stopping script in AppDomain");
215 // Check if it is current AppDomain
216 if (currentAD.CurrentAppDomain == ad)
217 {
218 // Yes - increase
219 currentAD.ScriptsWaitingUnload++;
220 return;
221 }
222
223 // Lopp through all AppDomains
224 foreach (AppDomainStructure ads in new ArrayList(appDomains))
225 {
226 if (ads.CurrentAppDomain == ad)
227 {
228 // Found it
229 ads.ScriptsWaitingUnload++;
230 break;
231 }
232 } // foreach
233 } // lock
234
235 UnloadAppDomains(); // Outsite lock, has its own GetLock
236 }
237 }
238} \ No newline at end of file