AppDomainManager.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using OpenSim.Region.ScriptEngine.Interfaces;
  32. using OpenSim.Region.ScriptEngine.Shared.ScriptBase;
  33. namespace OpenSim.Region.ScriptEngine.DotNetEngine
  34. {
  35. public class AppDomainManager
  36. {
  37. //
  38. // This class does AppDomain handling and loading/unloading of
  39. // scripts in it. It is instanced in "ScriptEngine" and controlled
  40. // from "ScriptManager"
  41. //
  42. // 1. Create a new AppDomain if old one is full (or doesn't exist)
  43. // 2. Load scripts into AppDomain
  44. // 3. Unload scripts from AppDomain (stopping them and marking
  45. // them as inactive)
  46. // 4. Unload AppDomain completely when all scripts in it has stopped
  47. //
  48. private int maxScriptsPerAppDomain = 1;
  49. // Internal list of all AppDomains
  50. private List<AppDomainStructure> appDomains =
  51. new List<AppDomainStructure>();
  52. // Structure to keep track of data around AppDomain
  53. private class AppDomainStructure
  54. {
  55. // The AppDomain itself
  56. public AppDomain CurrentAppDomain;
  57. // Number of scripts loaded into AppDomain
  58. public int ScriptsLoaded;
  59. // Number of dead scripts
  60. public int ScriptsWaitingUnload;
  61. }
  62. // Current AppDomain
  63. private AppDomainStructure currentAD;
  64. private object getLock = new object(); // Mutex
  65. private object freeLock = new object(); // Mutex
  66. private ScriptEngine m_scriptEngine;
  67. //public AppDomainManager(ScriptEngine scriptEngine)
  68. public AppDomainManager(ScriptEngine scriptEngine)
  69. {
  70. m_scriptEngine = scriptEngine;
  71. ReadConfig();
  72. }
  73. public void ReadConfig()
  74. {
  75. maxScriptsPerAppDomain = m_scriptEngine.ScriptConfigSource.GetInt(
  76. "ScriptsPerAppDomain", 1);
  77. }
  78. // Find a free AppDomain, creating one if necessary
  79. private AppDomainStructure GetFreeAppDomain()
  80. {
  81. lock (getLock)
  82. {
  83. // Current full?
  84. if (currentAD != null &&
  85. currentAD.ScriptsLoaded >= maxScriptsPerAppDomain)
  86. {
  87. // Add it to AppDomains list and empty current
  88. appDomains.Add(currentAD);
  89. currentAD = null;
  90. }
  91. // No current
  92. if (currentAD == null)
  93. {
  94. // Create a new current AppDomain
  95. currentAD = new AppDomainStructure();
  96. currentAD.CurrentAppDomain = PrepareNewAppDomain();
  97. }
  98. return currentAD;
  99. }
  100. }
  101. private int AppDomainNameCount;
  102. // Create and prepare a new AppDomain for scripts
  103. private AppDomain PrepareNewAppDomain()
  104. {
  105. // Create and prepare a new AppDomain
  106. AppDomainNameCount++;
  107. // TODO: Currently security match current appdomain
  108. // Construct and initialize settings for a second AppDomain.
  109. AppDomainSetup ads = new AppDomainSetup();
  110. ads.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
  111. ads.DisallowBindingRedirects = true;
  112. ads.DisallowCodeDownload = true;
  113. ads.LoaderOptimization = LoaderOptimization.MultiDomainHost;
  114. ads.ShadowCopyFiles = "false"; // Disable shadowing
  115. ads.ConfigurationFile =
  116. AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
  117. AppDomain AD = AppDomain.CreateDomain("ScriptAppDomain_" +
  118. AppDomainNameCount, null, ads);
  119. m_scriptEngine.Log.Info("[" + m_scriptEngine.ScriptEngineName +
  120. "]: AppDomain Loading: " +
  121. AssemblyName.GetAssemblyName(
  122. "OpenSim.Region.ScriptEngine.Shared.dll").ToString());
  123. AD.Load(AssemblyName.GetAssemblyName(
  124. "OpenSim.Region.ScriptEngine.Shared.dll"));
  125. // Return the new AppDomain
  126. return AD;
  127. }
  128. // Unload appdomains that are full and have only dead scripts
  129. private void UnloadAppDomains()
  130. {
  131. lock (freeLock)
  132. {
  133. // Go through all
  134. foreach (AppDomainStructure ads in new ArrayList(appDomains))
  135. {
  136. // Don't process current AppDomain
  137. if (ads.CurrentAppDomain != currentAD.CurrentAppDomain)
  138. {
  139. // Not current AppDomain
  140. // Is number of unloaded bigger or equal to number of loaded?
  141. if (ads.ScriptsLoaded <= ads.ScriptsWaitingUnload)
  142. {
  143. // Remove from internal list
  144. appDomains.Remove(ads);
  145. // Unload
  146. AppDomain.Unload(ads.CurrentAppDomain);
  147. }
  148. }
  149. }
  150. }
  151. }
  152. public IScript LoadScript(string FileName, out AppDomain ad)
  153. {
  154. // Find next available AppDomain to put it in
  155. AppDomainStructure FreeAppDomain = GetFreeAppDomain();
  156. IScript mbrt = (IScript)
  157. FreeAppDomain.CurrentAppDomain.CreateInstanceFromAndUnwrap(
  158. FileName, "SecondLife.Script");
  159. FreeAppDomain.ScriptsLoaded++;
  160. ad = FreeAppDomain.CurrentAppDomain;
  161. return mbrt;
  162. }
  163. // Increase "dead script" counter for an AppDomain
  164. public void StopScript(AppDomain ad)
  165. {
  166. lock (freeLock)
  167. {
  168. // Check if it is current AppDomain
  169. if (currentAD.CurrentAppDomain == ad)
  170. {
  171. // Yes - increase
  172. currentAD.ScriptsWaitingUnload++;
  173. return;
  174. }
  175. // Lopp through all AppDomains
  176. foreach (AppDomainStructure ads in new ArrayList(appDomains))
  177. {
  178. if (ads.CurrentAppDomain == ad)
  179. {
  180. // Found it
  181. ads.ScriptsWaitingUnload++;
  182. break;
  183. }
  184. }
  185. }
  186. UnloadAppDomains(); // Outsite lock, has its own GetLock
  187. }
  188. // If set to true then threads and stuff should try
  189. // to make a graceful exit
  190. public bool PleaseShutdown
  191. {
  192. get { return _PleaseShutdown; }
  193. set { _PleaseShutdown = value; }
  194. }
  195. private bool _PleaseShutdown = false;
  196. }
  197. }