From 2f394b7e7ebf991c7a70f93bf251d26d8043aaa2 Mon Sep 17 00:00:00 2001 From: John Hurliman Date: Thu, 22 Oct 2009 01:30:12 -0700 Subject: * Allow SmartThreadPool to be initialized without setting max stack size (like the original implementation) * Only initialize Util's SmartThreadPool if it is actually being used * No longer initializing Util's SmartThreadPool with a custom max stack size. From MSDN: "Avoid using this constructor overload. The default stack size used by the Thread(ThreadStart) constructor overload is the recommended stack size for threads." --- OpenSim/Framework/Util.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'OpenSim/Framework/Util.cs') diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index d09bd6d..167e34d 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs @@ -69,8 +69,6 @@ namespace OpenSim.Framework /// public class Util { - private static SmartThreadPool m_ThreadPool = null; - private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); private static uint nextXferID = 5000; @@ -79,6 +77,9 @@ namespace OpenSim.Framework private static string regexInvalidFileChars = "[" + new String(Path.GetInvalidFileNameChars()) + "]"; private static string regexInvalidPathChars = "[" + new String(Path.GetInvalidPathChars()) + "]"; private static object XferLock = new object(); + /// Thread pool used for Util.FireAndForget if + /// FireAndForgetMethod.SmartThreadPool is used + private static SmartThreadPool m_ThreadPool; // Unix-epoch starts at January 1st 1970, 00:00:00 UTC. And all our times in the server are (or at least should be) in UTC. private static readonly DateTime unixEpoch = @@ -1319,8 +1320,11 @@ namespace OpenSim.Framework FireAndForget(callback, null); } - public static void SetMaxThreads(int maxThreads) + public static void InitThreadPool(int maxThreads) { + if (maxThreads < 2) + throw new ArgumentOutOfRangeException("maxThreads", "maxThreads must be greater than 2"); + if (m_ThreadPool != null) return; @@ -1328,9 +1332,7 @@ namespace OpenSim.Framework startInfo.IdleTimeout = 2000; // 2 seconds startInfo.MaxWorkerThreads = maxThreads; startInfo.MinWorkerThreads = 2; - startInfo.StackSize = 524288; startInfo.ThreadPriority = ThreadPriority.Normal; - startInfo.StartSuspended = false; m_ThreadPool = new SmartThreadPool(startInfo); -- cgit v1.1 From 6ca4b0f36622833688136e9ace7d5545063293ba Mon Sep 17 00:00:00 2001 From: John Hurliman Date: Thu, 22 Oct 2009 10:37:11 -0700 Subject: * Added a check if Util.m_ThreadPool is null before trying to use it, and if so initialize it to sane defaults * Simplified the InitThreadPool() function --- OpenSim/Framework/Util.cs | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) (limited to 'OpenSim/Framework/Util.cs') diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index 167e34d..a18a827 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs @@ -1324,18 +1324,10 @@ namespace OpenSim.Framework { if (maxThreads < 2) throw new ArgumentOutOfRangeException("maxThreads", "maxThreads must be greater than 2"); - if (m_ThreadPool != null) - return; - - STPStartInfo startInfo = new STPStartInfo(); - startInfo.IdleTimeout = 2000; // 2 seconds - startInfo.MaxWorkerThreads = maxThreads; - startInfo.MinWorkerThreads = 2; - startInfo.ThreadPriority = ThreadPriority.Normal; - startInfo.StartSuspended = false; + throw new InvalidOperationException("SmartThreadPool is already initialized"); - m_ThreadPool = new SmartThreadPool(startInfo); + m_ThreadPool = new SmartThreadPool(2000, maxThreads, 2); } public static void FireAndForget(System.Threading.WaitCallback callback, object obj) @@ -1343,20 +1335,22 @@ namespace OpenSim.Framework switch (FireAndForgetMethod) { case FireAndForgetMethod.UnsafeQueueUserWorkItem: - System.Threading.ThreadPool.UnsafeQueueUserWorkItem(callback, obj); + ThreadPool.UnsafeQueueUserWorkItem(callback, obj); break; case FireAndForgetMethod.QueueUserWorkItem: - System.Threading.ThreadPool.QueueUserWorkItem(callback, obj); + ThreadPool.QueueUserWorkItem(callback, obj); break; case FireAndForgetMethod.BeginInvoke: FireAndForgetWrapper wrapper = Singleton.GetInstance(); wrapper.FireAndForget(callback, obj); break; case FireAndForgetMethod.SmartThreadPool: + if (m_ThreadPool != null) + m_ThreadPool = new SmartThreadPool(2000, 15, 2); m_ThreadPool.QueueWorkItem(delegate(object o) { callback(o); return null; }, obj); break; case FireAndForgetMethod.Thread: - System.Threading.Thread thread = new System.Threading.Thread(delegate(object o) { callback(o); }); + Thread thread = new Thread(delegate(object o) { callback(o); }); thread.Start(obj); break; default: -- cgit v1.1 From 36b0e5e1d3112212ef988a8b2e7c10284c7e9276 Mon Sep 17 00:00:00 2001 From: John Hurliman Date: Thu, 22 Oct 2009 11:07:23 -0700 Subject: Terrible typo in the previous commit! --- OpenSim/Framework/Util.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'OpenSim/Framework/Util.cs') diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index a18a827..b96367a 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs @@ -1345,7 +1345,7 @@ namespace OpenSim.Framework wrapper.FireAndForget(callback, obj); break; case FireAndForgetMethod.SmartThreadPool: - if (m_ThreadPool != null) + if (m_ThreadPool == null) m_ThreadPool = new SmartThreadPool(2000, 15, 2); m_ThreadPool.QueueWorkItem(delegate(object o) { callback(o); return null; }, obj); break; -- cgit v1.1 From 52a4534f7fe9e7b044a54f5a794391b54a1edb94 Mon Sep 17 00:00:00 2001 From: John Hurliman Date: Fri, 23 Oct 2009 13:45:18 -0700 Subject: * Change the way Util.FireAndForget() calls SmartThreadPool to avoid using a delegate (which STP appears to hold on to). This removes the slow leak I was seeing when using async_call_method=SmartThreadPool and stabilizes allocated memory for an idle OpenSim instance --- OpenSim/Framework/Util.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'OpenSim/Framework/Util.cs') diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index b96367a..10f38ab 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs @@ -1347,7 +1347,7 @@ namespace OpenSim.Framework case FireAndForgetMethod.SmartThreadPool: if (m_ThreadPool == null) m_ThreadPool = new SmartThreadPool(2000, 15, 2); - m_ThreadPool.QueueWorkItem(delegate(object o) { callback(o); return null; }, obj); + m_ThreadPool.QueueWorkItem(SmartThreadPoolCallback, new object[] { callback, obj }); break; case FireAndForgetMethod.Thread: Thread thread = new Thread(delegate(object o) { callback(o); }); @@ -1358,6 +1358,16 @@ namespace OpenSim.Framework } } + private static object SmartThreadPoolCallback(object o) + { + object[] array = (object[])o; + WaitCallback callback = (WaitCallback)array[0]; + object obj = array[1]; + + callback(obj); + return null; + } + #endregion FireAndForget Threading Pattern } } -- cgit v1.1