mirror of
https://github.com/opensim/opensim.git
synced 2026-08-05 00:46:02 +08:00
DNE code cleanups
This commit is contained in:
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// Internal list of all AppDomains
|
||||
/// </summary>
|
||||
private List<AppDomainStructure> appDomains = new List<AppDomainStructure>();
|
||||
// Internal list of all AppDomains
|
||||
private List<AppDomainStructure> appDomains =
|
||||
new List<AppDomainStructure>();
|
||||
|
||||
/// <summary>
|
||||
/// Structure to keep track of data around AppDomain
|
||||
/// </summary>
|
||||
// Structure to keep track of data around AppDomain
|
||||
private class AppDomainStructure
|
||||
{
|
||||
/// <summary>
|
||||
/// The AppDomain itself
|
||||
/// </summary>
|
||||
// The AppDomain itself
|
||||
public AppDomain CurrentAppDomain;
|
||||
|
||||
/// <summary>
|
||||
/// Number of scripts loaded into AppDomain
|
||||
/// </summary>
|
||||
// Number of scripts loaded into AppDomain
|
||||
public int ScriptsLoaded;
|
||||
|
||||
/// <summary>
|
||||
/// Number of dead scripts
|
||||
/// </summary>
|
||||
// Number of dead scripts
|
||||
public int ScriptsWaitingUnload;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Current AppDomain
|
||||
/// </summary>
|
||||
// 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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find a free AppDomain, creating one if necessary
|
||||
/// </summary>
|
||||
/// <returns>Free AppDomain</returns>
|
||||
// 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;
|
||||
|
||||
/// <summary>
|
||||
/// Create and prepare a new AppDomain for scripts
|
||||
/// </summary>
|
||||
/// <returns>The new AppDomain</returns>
|
||||
// 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unload appdomains that are full and have only dead scripts
|
||||
/// </summary>
|
||||
// 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
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Increase "dead script" counter for an AppDomain
|
||||
/// </summary>
|
||||
/// <param name="ad"></param>
|
||||
//[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
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If set to true then threads and stuff should try to make a graceful exit
|
||||
/// </summary>
|
||||
// If set to true then threads and stuff should try
|
||||
// to make a graceful exit
|
||||
public bool PleaseShutdown
|
||||
{
|
||||
get { return _PleaseShutdown; }
|
||||
|
||||
Reference in New Issue
Block a user