aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/Common/ScriptEngineBase/AppDomainManager.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Region/ScriptEngine/Common/ScriptEngineBase/AppDomainManager.cs (renamed from OpenSim/Region/ScriptEngine/DotNetEngine/AppDomainManager.cs)474
1 files changed, 237 insertions, 237 deletions
diff --git a/OpenSim/Region/ScriptEngine/DotNetEngine/AppDomainManager.cs b/OpenSim/Region/ScriptEngine/Common/ScriptEngineBase/AppDomainManager.cs
index 66153a7..4eea69a 100644
--- a/OpenSim/Region/ScriptEngine/DotNetEngine/AppDomainManager.cs
+++ b/OpenSim/Region/ScriptEngine/Common/ScriptEngineBase/AppDomainManager.cs
@@ -1,238 +1,238 @@
1/* 1/*
2* Copyright (c) Contributors, http://opensimulator.org/ 2* Copyright (c) Contributors, http://opensimulator.org/
3* See CONTRIBUTORS.TXT for a full list of copyright holders. 3* See CONTRIBUTORS.TXT for a full list of copyright holders.
4* 4*
5* Redistribution and use in source and binary forms, with or without 5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions are met: 6* modification, are permitted provided that the following conditions are met:
7* * Redistributions of source code must retain the above copyright 7* * Redistributions of source code must retain the above copyright
8* notice, this list of conditions and the following disclaimer. 8* notice, this list of conditions and the following disclaimer.
9* * Redistributions in binary form must reproduce the above copyright 9* * Redistributions in binary form must reproduce the above copyright
10* notice, this list of conditions and the following disclaimer in the 10* notice, this list of conditions and the following disclaimer in the
11* documentation and/or other materials provided with the distribution. 11* documentation and/or other materials provided with the distribution.
12* * Neither the name of the OpenSim Project nor the 12* * Neither the name of the OpenSim Project nor the
13* names of its contributors may be used to endorse or promote products 13* names of its contributors may be used to endorse or promote products
14* derived from this software without specific prior written permission. 14* derived from this software without specific prior written permission.
15* 15*
16* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS AS IS AND ANY 16* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS AS IS AND ANY
17* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY 19* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 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 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 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 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. 25* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26* 26*
27*/ 27*/
28 28
29using System; 29using System;
30using System.Collections; 30using System.Collections;
31using System.Collections.Generic; 31using System.Collections.Generic;
32using System.Reflection; 32using System.Reflection;
33using OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL; 33using OpenSim.Region.ScriptEngine.Common;
34 34
35namespace OpenSim.Region.ScriptEngine.DotNetEngine 35namespace OpenSim.Region.ScriptEngine.Common.ScriptEngineBase
36{ 36{
37 public class AppDomainManager 37 public class AppDomainManager
38 { 38 {
39 39
40 // 40 //
41 // This class does AppDomain handling and loading/unloading of scripts in it. 41 // This class does AppDomain handling and loading/unloading of scripts in it.
42 // It is instanced in "ScriptEngine" and controlled from "ScriptManager" 42 // It is instanced in "ScriptEngine" and controlled from "ScriptManager"
43 // 43 //
44 // 1. Create a new AppDomain if old one is full (or doesn't exist) 44 // 1. Create a new AppDomain if old one is full (or doesn't exist)
45 // 2. Load scripts into AppDomain 45 // 2. Load scripts into AppDomain
46 // 3. Unload scripts from AppDomain (stopping them and marking them as inactive) 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 47 // 4. Unload AppDomain completely when all scripts in it has stopped
48 // 48 //
49 49
50 50
51 private int maxScriptsPerAppDomain = 1; 51 private int maxScriptsPerAppDomain = 1;
52 52
53 /// <summary> 53 /// <summary>
54 /// Internal list of all AppDomains 54 /// Internal list of all AppDomains
55 /// </summary> 55 /// </summary>
56 private List<AppDomainStructure> appDomains = new List<AppDomainStructure>(); 56 private List<AppDomainStructure> appDomains = new List<AppDomainStructure>();
57 57
58 /// <summary> 58 /// <summary>
59 /// Structure to keep track of data around AppDomain 59 /// Structure to keep track of data around AppDomain
60 /// </summary> 60 /// </summary>
61 private class AppDomainStructure 61 private class AppDomainStructure
62 { 62 {
63 /// <summary> 63 /// <summary>
64 /// The AppDomain itself 64 /// The AppDomain itself
65 /// </summary> 65 /// </summary>
66 public AppDomain CurrentAppDomain; 66 public AppDomain CurrentAppDomain;
67 67
68 /// <summary> 68 /// <summary>
69 /// Number of scripts loaded into AppDomain 69 /// Number of scripts loaded into AppDomain
70 /// </summary> 70 /// </summary>
71 public int ScriptsLoaded; 71 public int ScriptsLoaded;
72 72
73 /// <summary> 73 /// <summary>
74 /// Number of dead scripts 74 /// Number of dead scripts
75 /// </summary> 75 /// </summary>
76 public int ScriptsWaitingUnload; 76 public int ScriptsWaitingUnload;
77 } 77 }
78 78
79 /// <summary> 79 /// <summary>
80 /// Current AppDomain 80 /// Current AppDomain
81 /// </summary> 81 /// </summary>
82 private AppDomainStructure currentAD; 82 private AppDomainStructure currentAD;
83 83
84 private object getLock = new object(); // Mutex 84 private object getLock = new object(); // Mutex
85 private object freeLock = new object(); // Mutex 85 private object freeLock = new object(); // Mutex
86 86
87 //private ScriptEngine m_scriptEngine; 87 //private ScriptEngine m_scriptEngine;
88 //public AppDomainManager(ScriptEngine scriptEngine) 88 //public AppDomainManager(ScriptEngine scriptEngine)
89 public AppDomainManager() 89 public AppDomainManager()
90 { 90 {
91 //m_scriptEngine = scriptEngine; 91 //m_scriptEngine = scriptEngine;
92 } 92 }
93 93
94 /// <summary> 94 /// <summary>
95 /// Find a free AppDomain, creating one if necessary 95 /// Find a free AppDomain, creating one if necessary
96 /// </summary> 96 /// </summary>
97 /// <returns>Free AppDomain</returns> 97 /// <returns>Free AppDomain</returns>
98 private AppDomainStructure GetFreeAppDomain() 98 private AppDomainStructure GetFreeAppDomain()
99 { 99 {
100 Console.WriteLine("Finding free AppDomain"); 100 Console.WriteLine("Finding free AppDomain");
101 lock (getLock) 101 lock (getLock)
102 { 102 {
103 // Current full? 103 // Current full?
104 if (currentAD != null && currentAD.ScriptsLoaded >= maxScriptsPerAppDomain) 104 if (currentAD != null && currentAD.ScriptsLoaded >= maxScriptsPerAppDomain)
105 { 105 {
106 // Add it to AppDomains list and empty current 106 // Add it to AppDomains list and empty current
107 appDomains.Add(currentAD); 107 appDomains.Add(currentAD);
108 currentAD = null; 108 currentAD = null;
109 } 109 }
110 // No current 110 // No current
111 if (currentAD == null) 111 if (currentAD == null)
112 { 112 {
113 // Create a new current AppDomain 113 // Create a new current AppDomain
114 currentAD = new AppDomainStructure(); 114 currentAD = new AppDomainStructure();
115 currentAD.CurrentAppDomain = PrepareNewAppDomain(); 115 currentAD.CurrentAppDomain = PrepareNewAppDomain();
116 } 116 }
117 117
118 Console.WriteLine("Scripts loaded in this Appdomain: " + currentAD.ScriptsLoaded); 118 Console.WriteLine("Scripts loaded in this Appdomain: " + currentAD.ScriptsLoaded);
119 return currentAD; 119 return currentAD;
120 } // lock 120 } // lock
121 } 121 }
122 122
123 private int AppDomainNameCount; 123 private int AppDomainNameCount;
124 124
125 /// <summary> 125 /// <summary>
126 /// Create and prepare a new AppDomain for scripts 126 /// Create and prepare a new AppDomain for scripts
127 /// </summary> 127 /// </summary>
128 /// <returns>The new AppDomain</returns> 128 /// <returns>The new AppDomain</returns>
129 private AppDomain PrepareNewAppDomain() 129 private AppDomain PrepareNewAppDomain()
130 { 130 {
131 // Create and prepare a new AppDomain 131 // Create and prepare a new AppDomain
132 AppDomainNameCount++; 132 AppDomainNameCount++;
133 // TODO: Currently security match current appdomain 133 // TODO: Currently security match current appdomain
134 134
135 // Construct and initialize settings for a second AppDomain. 135 // Construct and initialize settings for a second AppDomain.
136 AppDomainSetup ads = new AppDomainSetup(); 136 AppDomainSetup ads = new AppDomainSetup();
137 ads.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory; 137 ads.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
138 ads.DisallowBindingRedirects = false; 138 ads.DisallowBindingRedirects = false;
139 ads.DisallowCodeDownload = true; 139 ads.DisallowCodeDownload = true;
140 ads.LoaderOptimization = LoaderOptimization.MultiDomain; // Sounds good ;) 140 ads.LoaderOptimization = LoaderOptimization.MultiDomain; // Sounds good ;)
141 ads.ShadowCopyFiles = "true"; // Enabled shadowing 141 ads.ShadowCopyFiles = "true"; // Enabled shadowing
142 ads.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; 142 ads.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
143 143
144 AppDomain AD = AppDomain.CreateDomain("ScriptAppDomain_" + AppDomainNameCount, null, ads); 144 AppDomain AD = AppDomain.CreateDomain("ScriptAppDomain_" + AppDomainNameCount, null, ads);
145 Console.WriteLine("Loading: " + 145 Console.WriteLine("Loading: " +
146 AssemblyName.GetAssemblyName("OpenSim.Region.ScriptEngine.Common.dll").ToString()); 146 AssemblyName.GetAssemblyName("OpenSim.Region.ScriptEngine.Common.dll").ToString());
147 AD.Load(AssemblyName.GetAssemblyName("OpenSim.Region.ScriptEngine.Common.dll")); 147 AD.Load(AssemblyName.GetAssemblyName("OpenSim.Region.ScriptEngine.Common.dll"));
148 148
149 // Return the new AppDomain 149 // Return the new AppDomain
150 return AD; 150 return AD;
151 } 151 }
152 152
153 /// <summary> 153 /// <summary>
154 /// Unload appdomains that are full and have only dead scripts 154 /// Unload appdomains that are full and have only dead scripts
155 /// </summary> 155 /// </summary>
156 private void UnloadAppDomains() 156 private void UnloadAppDomains()
157 { 157 {
158 lock (freeLock) 158 lock (freeLock)
159 { 159 {
160 // Go through all 160 // Go through all
161 foreach (AppDomainStructure ads in new ArrayList(appDomains)) 161 foreach (AppDomainStructure ads in new ArrayList(appDomains))
162 { 162 {
163 // Don't process current AppDomain 163 // Don't process current AppDomain
164 if (ads.CurrentAppDomain != currentAD.CurrentAppDomain) 164 if (ads.CurrentAppDomain != currentAD.CurrentAppDomain)
165 { 165 {
166 // Not current AppDomain 166 // Not current AppDomain
167 // Is number of unloaded bigger or equal to number of loaded? 167 // Is number of unloaded bigger or equal to number of loaded?
168 if (ads.ScriptsLoaded <= ads.ScriptsWaitingUnload) 168 if (ads.ScriptsLoaded <= ads.ScriptsWaitingUnload)
169 { 169 {
170 Console.WriteLine("Found empty AppDomain, unloading"); 170 Console.WriteLine("Found empty AppDomain, unloading");
171 // Remove from internal list 171 // Remove from internal list
172 appDomains.Remove(ads); 172 appDomains.Remove(ads);
173#if DEBUG 173#if DEBUG
174 long m = GC.GetTotalMemory(true); 174 long m = GC.GetTotalMemory(true);
175#endif 175#endif
176 // Unload 176 // Unload
177 AppDomain.Unload(ads.CurrentAppDomain); 177 AppDomain.Unload(ads.CurrentAppDomain);
178#if DEBUG 178#if DEBUG
179 Console.WriteLine("AppDomain unload freed " + (m - GC.GetTotalMemory(true)) + 179 Console.WriteLine("AppDomain unload freed " + (m - GC.GetTotalMemory(true)) +
180 " bytes of memory"); 180 " bytes of memory");
181#endif 181#endif
182 } 182 }
183 } 183 }
184 } // foreach 184 } // foreach
185 } // lock 185 } // lock
186 } 186 }
187 187
188 188
189 public LSL_BaseClass LoadScript(string FileName) 189 public IScript LoadScript(string FileName)
190 { 190 {
191 // Find next available AppDomain to put it in 191 // Find next available AppDomain to put it in
192 AppDomainStructure FreeAppDomain = GetFreeAppDomain(); 192 AppDomainStructure FreeAppDomain = GetFreeAppDomain();
193 193
194 Console.WriteLine("Loading into AppDomain: " + FileName); 194 Console.WriteLine("Loading into AppDomain: " + FileName);
195 LSL_BaseClass mbrt = 195 IScript mbrt =
196 (LSL_BaseClass) 196 (IScript)
197 FreeAppDomain.CurrentAppDomain.CreateInstanceFromAndUnwrap(FileName, "SecondLife.Script"); 197 FreeAppDomain.CurrentAppDomain.CreateInstanceFromAndUnwrap(FileName, "SecondLife.Script");
198 //Console.WriteLine("ScriptEngine AppDomainManager: is proxy={0}", RemotingServices.IsTransparentProxy(mbrt)); 198 //Console.WriteLine("ScriptEngine AppDomainManager: is proxy={0}", RemotingServices.IsTransparentProxy(mbrt));
199 FreeAppDomain.ScriptsLoaded++; 199 FreeAppDomain.ScriptsLoaded++;
200 200
201 return mbrt; 201 return mbrt;
202 } 202 }
203 203
204 204
205 /// <summary> 205 /// <summary>
206 /// Increase "dead script" counter for an AppDomain 206 /// Increase "dead script" counter for an AppDomain
207 /// </summary> 207 /// </summary>
208 /// <param name="ad"></param> 208 /// <param name="ad"></param>
209 //[Obsolete("Needs fixing, needs a real purpose in life!!!")] 209 //[Obsolete("Needs fixing, needs a real purpose in life!!!")]
210 public void StopScript(AppDomain ad) 210 public void StopScript(AppDomain ad)
211 { 211 {
212 lock (freeLock) 212 lock (freeLock)
213 { 213 {
214 Console.WriteLine("Stopping script in AppDomain"); 214 Console.WriteLine("Stopping script in AppDomain");
215 // Check if it is current AppDomain 215 // Check if it is current AppDomain
216 if (currentAD.CurrentAppDomain == ad) 216 if (currentAD.CurrentAppDomain == ad)
217 { 217 {
218 // Yes - increase 218 // Yes - increase
219 currentAD.ScriptsWaitingUnload++; 219 currentAD.ScriptsWaitingUnload++;
220 return; 220 return;
221 } 221 }
222 222
223 // Lopp through all AppDomains 223 // Lopp through all AppDomains
224 foreach (AppDomainStructure ads in new ArrayList(appDomains)) 224 foreach (AppDomainStructure ads in new ArrayList(appDomains))
225 { 225 {
226 if (ads.CurrentAppDomain == ad) 226 if (ads.CurrentAppDomain == ad)
227 { 227 {
228 // Found it 228 // Found it
229 ads.ScriptsWaitingUnload++; 229 ads.ScriptsWaitingUnload++;
230 break; 230 break;
231 } 231 }
232 } // foreach 232 } // foreach
233 } // lock 233 } // lock
234 234
235 UnloadAppDomains(); // Outsite lock, has its own GetLock 235 UnloadAppDomains(); // Outsite lock, has its own GetLock
236 } 236 }
237 } 237 }
238} \ No newline at end of file 238} \ No newline at end of file