aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Grid/ScriptEngine/DotNetEngine/AppDomainManager.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Grid/ScriptEngine/DotNetEngine/AppDomainManager.cs')
-rw-r--r--OpenSim/Grid/ScriptEngine/DotNetEngine/AppDomainManager.cs223
1 files changed, 0 insertions, 223 deletions
diff --git a/OpenSim/Grid/ScriptEngine/DotNetEngine/AppDomainManager.cs b/OpenSim/Grid/ScriptEngine/DotNetEngine/AppDomainManager.cs
deleted file mode 100644
index c5b5e95..0000000
--- a/OpenSim/Grid/ScriptEngine/DotNetEngine/AppDomainManager.cs
+++ /dev/null
@@ -1,223 +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
28using System;
29using System.Collections;
30using System.Collections.Generic;
31using System.Reflection;
32using OpenSim.Grid.ScriptEngine.DotNetEngine.Compiler.LSL;
33
34namespace OpenSim.Grid.ScriptEngine.DotNetEngine
35{
36 public class AppDomainManager
37 {
38 private int maxScriptsPerAppDomain = 1;
39
40 /// <summary>
41 /// Internal list of all AppDomains
42 /// </summary>
43 private List<AppDomainStructure> appDomains = new List<AppDomainStructure>();
44
45 /// <summary>
46 /// Structure to keep track of data around AppDomain
47 /// </summary>
48 private class AppDomainStructure
49 {
50 /// <summary>
51 /// The AppDomain itself
52 /// </summary>
53 public AppDomain CurrentAppDomain;
54
55 /// <summary>
56 /// Number of scripts loaded into AppDomain
57 /// </summary>
58 public int ScriptsLoaded;
59
60 /// <summary>
61 /// Number of dead scripts
62 /// </summary>
63 public int ScriptsWaitingUnload;
64 }
65
66 /// <summary>
67 /// Current AppDomain
68 /// </summary>
69 private AppDomainStructure currentAD;
70
71 private object getLock = new object(); // Mutex
72 private object freeLock = new object(); // Mutex
73
74 //private ScriptEngine m_scriptEngine;
75 //public AppDomainManager(ScriptEngine scriptEngine)
76 public AppDomainManager()
77 {
78 //m_scriptEngine = scriptEngine;
79 }
80
81 /// <summary>
82 /// Find a free AppDomain, creating one if necessary
83 /// </summary>
84 /// <returns>Free AppDomain</returns>
85 private AppDomainStructure GetFreeAppDomain()
86 {
87 Console.WriteLine("Finding free AppDomain");
88 lock (getLock)
89 {
90 // Current full?
91 if (currentAD != null && currentAD.ScriptsLoaded >= maxScriptsPerAppDomain)
92 {
93 // Add it to AppDomains list and empty current
94 appDomains.Add(currentAD);
95 currentAD = null;
96 }
97 // No current
98 if (currentAD == null)
99 {
100 // Create a new current AppDomain
101 currentAD = new AppDomainStructure();
102 currentAD.CurrentAppDomain = PrepareNewAppDomain();
103 }
104
105 Console.WriteLine("Scripts loaded in this Appdomain: " + currentAD.ScriptsLoaded);
106 return currentAD;
107 }
108 }
109
110 private int AppDomainNameCount;
111
112 /// <summary>
113 /// Create and prepare a new AppDomain for scripts
114 /// </summary>
115 /// <returns>The new AppDomain</returns>
116 private AppDomain PrepareNewAppDomain()
117 {
118 // Create and prepare a new AppDomain
119 AppDomainNameCount++;
120 // TODO: Currently security match current appdomain
121
122 // Construct and initialize settings for a second AppDomain.
123 AppDomainSetup ads = new AppDomainSetup();
124 ads.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
125 ads.DisallowBindingRedirects = false;
126 ads.DisallowCodeDownload = true;
127 ads.LoaderOptimization = LoaderOptimization.MultiDomain; // Sounds good ;)
128 ads.ShadowCopyFiles = "true"; // Enabled shadowing
129 ads.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
130
131 AppDomain AD = AppDomain.CreateDomain("ScriptAppDomain_" + AppDomainNameCount, null, ads);
132 Console.WriteLine("Loading: " +
133 AssemblyName.GetAssemblyName("OpenSim.Region.ScriptEngine.Common.dll").ToString());
134 AD.Load(AssemblyName.GetAssemblyName("OpenSim.Region.ScriptEngine.Common.dll"));
135
136 // Return the new AppDomain
137 return AD;
138 }
139
140 /// <summary>
141 /// Unload appdomains that are full and have only dead scripts
142 /// </summary>
143 private void UnloadAppDomains()
144 {
145 lock (freeLock)
146 {
147 // Go through all
148 foreach (AppDomainStructure ads in new ArrayList(appDomains))
149 {
150 // Don't process current AppDomain
151 if (ads.CurrentAppDomain != currentAD.CurrentAppDomain)
152 {
153 // Not current AppDomain
154 // Is number of unloaded bigger or equal to number of loaded?
155 if (ads.ScriptsLoaded <= ads.ScriptsWaitingUnload)
156 {
157 Console.WriteLine("Found empty AppDomain, unloading");
158 // Remove from internal list
159 appDomains.Remove(ads);
160#if DEBUG
161 long m = GC.GetTotalMemory(true);
162#endif
163 // Unload
164 AppDomain.Unload(ads.CurrentAppDomain);
165#if DEBUG
166 Console.WriteLine("AppDomain unload freed " + (m - GC.GetTotalMemory(true)) +
167 " bytes of memory");
168#endif
169 }
170 }
171 }
172 }
173 }
174
175 public LSL_BaseClass LoadScript(string FileName)
176 {
177 // Find next available AppDomain to put it in
178 AppDomainStructure FreeAppDomain = GetFreeAppDomain();
179
180 Console.WriteLine("Loading into AppDomain: " + FileName);
181 LSL_BaseClass mbrt =
182 (LSL_BaseClass)
183 FreeAppDomain.CurrentAppDomain.CreateInstanceFromAndUnwrap(FileName, "SecondLife.Script");
184 //Console.WriteLine("ScriptEngine AppDomainManager: is proxy={0}", RemotingServices.IsTransparentProxy(mbrt));
185 FreeAppDomain.ScriptsLoaded++;
186
187 return mbrt;
188 }
189
190 /// <summary>
191 /// Increase "dead script" counter for an AppDomain
192 /// </summary>
193 /// <param name="ad"></param>
194 //[Obsolete("Needs fixing, needs a real purpose in life!!!")]
195 public void StopScript(AppDomain ad)
196 {
197 lock (freeLock)
198 {
199 Console.WriteLine("Stopping script in AppDomain");
200 // Check if it is current AppDomain
201 if (currentAD.CurrentAppDomain == ad)
202 {
203 // Yes - increase
204 currentAD.ScriptsWaitingUnload++;
205 return;
206 }
207
208 // Lopp through all AppDomains
209 foreach (AppDomainStructure ads in new ArrayList(appDomains))
210 {
211 if (ads.CurrentAppDomain == ad)
212 {
213 // Found it
214 ads.ScriptsWaitingUnload++;
215 break;
216 }
217 }
218 }
219
220 UnloadAppDomains(); // Outsite lock, has its own GetLock
221 }
222 }
223}