From 206fb306a7820cf593570e35ddfa8e7c5a10e449 Mon Sep 17 00:00:00 2001 From: Justin Clark-Casey (justincc) Date: Wed, 1 May 2013 19:01:43 +0100 Subject: Update SmartThreadPool to latest version 2.2.3 with a major and minor change. SmartThreadPool code comes from http://www.codeproject.com/Articles/7933/Smart-Thread-Pool This version implements thread abort (via WorkItem.Cancel(true)), threadpool naming, max thread stack, etc. so we no longer need to manually patch those. However, two changes have been made to stock 2.2.3. Major change: WorkItem.Cancel(bool abortExecution) in our version does not succeed if the work item was in progress and thread abort was not specified. This is to match previous behaviour where we handle co-operative termination via another mechanism rather than checking WorkItem.IsCanceled. Minor change: Did not add STP's StopWatch implementation as this is only used WinCE and Silverlight and causes a build clash with System.Diagnostics.StopWatch The reason for updating is to see if this improves http://opensimulator.org/mantis/view.php?id=6557 and http://opensimulator.org/mantis/view.php?id=6586 --- OpenSim/Framework/Util.cs | 20 ++++++++++---------- .../ScriptEngine/Interfaces/IScriptInstance.cs | 2 +- .../ScriptEngine/Shared/Instance/ScriptInstance.cs | 9 ++++++--- OpenSim/Region/ScriptEngine/XEngine/XEngine.cs | 4 ++-- OpenSim/Region/ScriptEngine/XEngine/XWorkItem.cs | 6 +++--- 5 files changed, 22 insertions(+), 19 deletions(-) (limited to 'OpenSim') diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs index bde4673..a3602e9 100644 --- a/OpenSim/Framework/Util.cs +++ b/OpenSim/Framework/Util.cs @@ -1840,7 +1840,7 @@ namespace OpenSim.Framework case FireAndForgetMethod.SmartThreadPool: if (m_ThreadPool == null) InitThreadPool(15); - m_ThreadPool.QueueWorkItem(SmartThreadPoolCallback, new object[] { realCallback, obj }); + m_ThreadPool.QueueWorkItem((cb, o) => cb(o), realCallback, obj); break; case FireAndForgetMethod.Thread: Thread thread = new Thread(delegate(object o) { realCallback(o); }); @@ -1910,15 +1910,15 @@ namespace OpenSim.Framework return sb.ToString(); } - private static object SmartThreadPoolCallback(object o) - { - object[] array = (object[])o; - WaitCallback callback = (WaitCallback)array[0]; - object obj = array[1]; - - callback(obj); - return null; - } +// 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 diff --git a/OpenSim/Region/ScriptEngine/Interfaces/IScriptInstance.cs b/OpenSim/Region/ScriptEngine/Interfaces/IScriptInstance.cs index 35ae44c..b9a217b 100644 --- a/OpenSim/Region/ScriptEngine/Interfaces/IScriptInstance.cs +++ b/OpenSim/Region/ScriptEngine/Interfaces/IScriptInstance.cs @@ -51,7 +51,7 @@ namespace OpenSim.Region.ScriptEngine.Interfaces public interface IScriptWorkItem { bool Cancel(); - void Abort(); + bool Abort(); /// /// Wait for the work item to complete. diff --git a/OpenSim/Region/ScriptEngine/Shared/Instance/ScriptInstance.cs b/OpenSim/Region/ScriptEngine/Shared/Instance/ScriptInstance.cs index 1e6db43..f9d3afc 100644 --- a/OpenSim/Region/ScriptEngine/Shared/Instance/ScriptInstance.cs +++ b/OpenSim/Region/ScriptEngine/Shared/Instance/ScriptInstance.cs @@ -564,9 +564,10 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance public bool Stop(int timeout) { -// m_log.DebugFormat( -// "[SCRIPT INSTANCE]: Stopping script {0} {1} in {2} {3} with timeout {4} {5} {6}", -// ScriptName, ItemID, PrimName, ObjectID, timeout, m_InSelfDelete, DateTime.Now.Ticks); + if (DebugLevel >= 1) + m_log.DebugFormat( + "[SCRIPT INSTANCE]: Stopping script {0} {1} in {2} {3} with timeout {4} {5} {6}", + ScriptName, ItemID, PrimName, ObjectID, timeout, m_InSelfDelete, DateTime.Now.Ticks); IScriptWorkItem workItem; @@ -627,6 +628,8 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance } } + Console.WriteLine("Here9"); + lock (EventQueue) { workItem = m_CurrentWorkItem; diff --git a/OpenSim/Region/ScriptEngine/XEngine/XEngine.cs b/OpenSim/Region/ScriptEngine/XEngine/XEngine.cs index 0d9babb..5804aa8 100644 --- a/OpenSim/Region/ScriptEngine/XEngine/XEngine.cs +++ b/OpenSim/Region/ScriptEngine/XEngine/XEngine.cs @@ -483,7 +483,7 @@ namespace OpenSim.Region.ScriptEngine.XEngine /// /// Basis on which to sort output. Can be null if no sort needs to take place private void HandleScriptsAction( - string[] cmdparams, Action action, Func keySelector) + string[] cmdparams, Action action, System.Func keySelector) { if (!(MainConsole.Instance.ConsoleScene == null || MainConsole.Instance.ConsoleScene == m_Scene)) return; @@ -1517,7 +1517,7 @@ namespace OpenSim.Region.ScriptEngine.XEngine startInfo.MaxWorkerThreads = maxThreads; startInfo.MinWorkerThreads = minThreads; startInfo.ThreadPriority = threadPriority;; - startInfo.StackSize = stackSize; + startInfo.MaxStackSize = stackSize; startInfo.StartSuspended = true; m_ThreadPool = new SmartThreadPool(startInfo); diff --git a/OpenSim/Region/ScriptEngine/XEngine/XWorkItem.cs b/OpenSim/Region/ScriptEngine/XEngine/XWorkItem.cs index 8dd7677..9d9dee1 100644 --- a/OpenSim/Region/ScriptEngine/XEngine/XWorkItem.cs +++ b/OpenSim/Region/ScriptEngine/XEngine/XWorkItem.cs @@ -52,16 +52,16 @@ namespace OpenSim.Region.ScriptEngine.XEngine return wr.Cancel(); } - public void Abort() + public bool Abort() { - wr.Abort(); + return wr.Cancel(true); } public bool Wait(int t) { // We use the integer version of WaitAll because the current version of SmartThreadPool has a bug with the // TimeSpan version. The number of milliseconds in TimeSpan is an int64 so when STP casts it down to an - // int (32-bit) we can end up with bad values. This occurs on Windows though curious not on Mono 2.10.8 + // int (32-bit) we can end up with bad values. This occurs on Windows though curiously not on Mono 2.10.8 // (or very likely other versions of Mono at least up until 3.0.3). return SmartThreadPool.WaitAll(new IWorkItemResult[] {wr}, t, false); } -- cgit v1.1 From 81a90e30c637f84e6ef5d1cf7c188c1f6db12eb3 Mon Sep 17 00:00:00 2001 From: Justin Clark-Casey (justincc) Date: Wed, 1 May 2013 19:29:46 +0100 Subject: Add in-code exaplanation for the change in cancellation signalling in STP 2.2.3. Remove left in Console.WriteLine accidentally inserted in recent 206fb306 --- OpenSim/Region/ScriptEngine/Shared/Instance/ScriptInstance.cs | 2 -- 1 file changed, 2 deletions(-) (limited to 'OpenSim') diff --git a/OpenSim/Region/ScriptEngine/Shared/Instance/ScriptInstance.cs b/OpenSim/Region/ScriptEngine/Shared/Instance/ScriptInstance.cs index f9d3afc..887a317 100644 --- a/OpenSim/Region/ScriptEngine/Shared/Instance/ScriptInstance.cs +++ b/OpenSim/Region/ScriptEngine/Shared/Instance/ScriptInstance.cs @@ -628,8 +628,6 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance } } - Console.WriteLine("Here9"); - lock (EventQueue) { workItem = m_CurrentWorkItem; -- cgit v1.1