From 34cfd710ce2f80e11012a219af73100f559306df Mon Sep 17 00:00:00 2001 From: Melanie Thielker Date: Fri, 26 Sep 2008 17:01:33 +0000 Subject: DNE code cleanups --- .../ScriptEngine/DotNetEngine/AppDomainManager.cs | 106 ++++++++------------- 1 file changed, 39 insertions(+), 67 deletions(-) (limited to 'OpenSim/Region/ScriptEngine/DotNetEngine/AppDomainManager.cs') diff --git a/OpenSim/Region/ScriptEngine/DotNetEngine/AppDomainManager.cs b/OpenSim/Region/ScriptEngine/DotNetEngine/AppDomainManager.cs index 5a77d39..da461e3 100644 --- a/OpenSim/Region/ScriptEngine/DotNetEngine/AppDomainManager.cs +++ b/OpenSim/Region/ScriptEngine/DotNetEngine/AppDomainManager.cs @@ -37,46 +37,37 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine public class AppDomainManager : iScriptEngineFunctionModule { // - // This class does AppDomain handling and loading/unloading of scripts in it. - // It is instanced in "ScriptEngine" and controlled from "ScriptManager" + // This class does AppDomain handling and loading/unloading of + // scripts in it. It is instanced in "ScriptEngine" and controlled + // from "ScriptManager" // // 1. Create a new AppDomain if old one is full (or doesn't exist) // 2. Load scripts into AppDomain - // 3. Unload scripts from AppDomain (stopping them and marking them as inactive) + // 3. Unload scripts from AppDomain (stopping them and marking + // them as inactive) // 4. Unload AppDomain completely when all scripts in it has stopped // private int maxScriptsPerAppDomain = 1; - /// - /// Internal list of all AppDomains - /// - private List appDomains = new List(); + // Internal list of all AppDomains + private List appDomains = + new List(); - /// - /// Structure to keep track of data around AppDomain - /// + // Structure to keep track of data around AppDomain private class AppDomainStructure { - /// - /// The AppDomain itself - /// + // The AppDomain itself public AppDomain CurrentAppDomain; - /// - /// Number of scripts loaded into AppDomain - /// + // Number of scripts loaded into AppDomain public int ScriptsLoaded; - /// - /// Number of dead scripts - /// + // Number of dead scripts public int ScriptsWaitingUnload; } - /// - /// Current AppDomain - /// + // Current AppDomain private AppDomainStructure currentAD; private object getLock = new object(); // Mutex @@ -92,20 +83,18 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine public void ReadConfig() { - maxScriptsPerAppDomain = m_scriptEngine.ScriptConfigSource.GetInt("ScriptsPerAppDomain", 1); + maxScriptsPerAppDomain = m_scriptEngine.ScriptConfigSource.GetInt( + "ScriptsPerAppDomain", 1); } - /// - /// Find a free AppDomain, creating one if necessary - /// - /// Free AppDomain + // Find a free AppDomain, creating one if necessary private AppDomainStructure GetFreeAppDomain() { - // Console.WriteLine("Finding free AppDomain"); lock (getLock) { // Current full? - if (currentAD != null && currentAD.ScriptsLoaded >= maxScriptsPerAppDomain) + if (currentAD != null && + currentAD.ScriptsLoaded >= maxScriptsPerAppDomain) { // Add it to AppDomains list and empty current appDomains.Add(currentAD); @@ -119,21 +108,18 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine currentAD.CurrentAppDomain = PrepareNewAppDomain(); } - // Console.WriteLine("Scripts loaded in this Appdomain: " + currentAD.ScriptsLoaded); return currentAD; } } private int AppDomainNameCount; - /// - /// Create and prepare a new AppDomain for scripts - /// - /// The new AppDomain + // Create and prepare a new AppDomain for scripts private AppDomain PrepareNewAppDomain() { // Create and prepare a new AppDomain AppDomainNameCount++; + // TODO: Currently security match current appdomain // Construct and initialize settings for a second AppDomain. @@ -143,21 +129,24 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine ads.DisallowCodeDownload = true; ads.LoaderOptimization = LoaderOptimization.MultiDomainHost; ads.ShadowCopyFiles = "false"; // Disable shadowing - ads.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; + ads.ConfigurationFile = + AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; - AppDomain AD = AppDomain.CreateDomain("ScriptAppDomain_" + AppDomainNameCount, null, ads); - m_scriptEngine.Log.Info("[" + m_scriptEngine.ScriptEngineName + "]: AppDomain Loading: " + - AssemblyName.GetAssemblyName("OpenSim.Region.ScriptEngine.Shared.dll").ToString()); - AD.Load(AssemblyName.GetAssemblyName("OpenSim.Region.ScriptEngine.Shared.dll")); + AppDomain AD = AppDomain.CreateDomain("ScriptAppDomain_" + + AppDomainNameCount, null, ads); + m_scriptEngine.Log.Info("[" + m_scriptEngine.ScriptEngineName + + "]: AppDomain Loading: " + + AssemblyName.GetAssemblyName( + "OpenSim.Region.ScriptEngine.Shared.dll").ToString()); + AD.Load(AssemblyName.GetAssemblyName( + "OpenSim.Region.ScriptEngine.Shared.dll")); // Return the new AppDomain return AD; } - /// - /// Unload appdomains that are full and have only dead scripts - /// + // Unload appdomains that are full and have only dead scripts private void UnloadAppDomains() { lock (freeLock) @@ -174,15 +163,9 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine { // Remove from internal list appDomains.Remove(ads); -//#if DEBUG - //Console.WriteLine("Found empty AppDomain, unloading"); - //long m = GC.GetTotalMemory(true); // This force a garbage collect that freezes some windows plateforms -//#endif + // Unload AppDomain.Unload(ads.CurrentAppDomain); -//#if DEBUG - //m_scriptEngine.Log.Info("[" + m_scriptEngine.ScriptEngineName + "]: AppDomain unload freed " + (m - GC.GetTotalMemory(true)) + " bytes of memory"); -//#endif } } } @@ -194,13 +177,10 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine // Find next available AppDomain to put it in AppDomainStructure FreeAppDomain = GetFreeAppDomain(); -#if DEBUG - m_scriptEngine.Log.Info("[" + m_scriptEngine.ScriptEngineName + "]: Loading into AppDomain: " + FileName); -#endif - IScript mbrt = - (IScript) - FreeAppDomain.CurrentAppDomain.CreateInstanceFromAndUnwrap(FileName, "SecondLife.Script"); - //Console.WriteLine("ScriptEngine AppDomainManager: is proxy={0}", RemotingServices.IsTransparentProxy(mbrt)); + IScript mbrt = (IScript) + FreeAppDomain.CurrentAppDomain.CreateInstanceFromAndUnwrap( + FileName, "SecondLife.Script"); + FreeAppDomain.ScriptsLoaded++; ad = FreeAppDomain.CurrentAppDomain; @@ -208,18 +188,11 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine } - /// - /// Increase "dead script" counter for an AppDomain - /// - /// - //[Obsolete("Needs fixing, needs a real purpose in life!!!")] + // Increase "dead script" counter for an AppDomain public void StopScript(AppDomain ad) { lock (freeLock) { -#if DEBUG - m_scriptEngine.Log.Info("[" + m_scriptEngine.ScriptEngineName + "]: Stopping script in AppDomain"); -#endif // Check if it is current AppDomain if (currentAD.CurrentAppDomain == ad) { @@ -243,9 +216,8 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine UnloadAppDomains(); // Outsite lock, has its own GetLock } - /// - /// If set to true then threads and stuff should try to make a graceful exit - /// + // If set to true then threads and stuff should try + // to make a graceful exit public bool PleaseShutdown { get { return _PleaseShutdown; } -- cgit v1.1