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 --- ThirdParty/SmartThreadPool/STPStartInfo.cs | 325 +++++++++++++++++++---------- 1 file changed, 212 insertions(+), 113 deletions(-) (limited to 'ThirdParty/SmartThreadPool/STPStartInfo.cs') diff --git a/ThirdParty/SmartThreadPool/STPStartInfo.cs b/ThirdParty/SmartThreadPool/STPStartInfo.cs index fa9ceb4..2ec8dc6 100644 --- a/ThirdParty/SmartThreadPool/STPStartInfo.cs +++ b/ThirdParty/SmartThreadPool/STPStartInfo.cs @@ -1,113 +1,212 @@ -// Ami Bar -// amibar@gmail.com - -using System.Threading; - -namespace Amib.Threading -{ - /// - /// Summary description for STPStartInfo. - /// - public class STPStartInfo : WIGStartInfo - { - /// - /// Idle timeout in milliseconds. - /// If a thread is idle for _idleTimeout milliseconds then - /// it may quit. - /// - private int _idleTimeout; - - /// - /// The lower limit of threads in the pool. - /// - private int _minWorkerThreads; - - /// - /// The upper limit of threads in the pool. - /// - private int _maxWorkerThreads; - - /// - /// The priority of the threads in the pool - /// - private ThreadPriority _threadPriority; - - /// - /// The thread pool name. Threads will get names depending on this. - /// - private string _threadPoolName; - - /// - /// If this field is not null then the performance counters are enabled - /// and use the string as the name of the instance. - /// - private string _pcInstanceName; - - private int _stackSize; - - public STPStartInfo() : base() - { - _idleTimeout = SmartThreadPool.DefaultIdleTimeout; - _minWorkerThreads = SmartThreadPool.DefaultMinWorkerThreads; - _maxWorkerThreads = SmartThreadPool.DefaultMaxWorkerThreads; - _threadPriority = SmartThreadPool.DefaultThreadPriority; - _threadPoolName = SmartThreadPool.DefaultThreadPoolName; - _pcInstanceName = SmartThreadPool.DefaultPerformanceCounterInstanceName; - _stackSize = SmartThreadPool.DefaultStackSize; - } - - public STPStartInfo(STPStartInfo stpStartInfo) : base(stpStartInfo) - { - _idleTimeout = stpStartInfo._idleTimeout; - _minWorkerThreads = stpStartInfo._minWorkerThreads; - _maxWorkerThreads = stpStartInfo._maxWorkerThreads; - _threadPriority = stpStartInfo._threadPriority; - _threadPoolName = stpStartInfo._threadPoolName; - _pcInstanceName = stpStartInfo._pcInstanceName; - _stackSize = stpStartInfo._stackSize; - } - - public int IdleTimeout - { - get { return _idleTimeout; } - set { _idleTimeout = value; } - } - - public int MinWorkerThreads - { - get { return _minWorkerThreads; } - set { _minWorkerThreads = value; } - } - - public int MaxWorkerThreads - { - get { return _maxWorkerThreads; } - set { _maxWorkerThreads = value; } - } - - public ThreadPriority ThreadPriority - { - get { return _threadPriority; } - set { _threadPriority = value; } - } - - public virtual string ThreadPoolName - { - get { return _threadPoolName; } - set { _threadPoolName = value; } - } - - - public string PerformanceCounterInstanceName - { - get { return _pcInstanceName; } - set { _pcInstanceName = value; } - } - - public int StackSize - { - get { return _stackSize; } - set { _stackSize = value; } - } - } -} +using System; +using System.Threading; + +namespace Amib.Threading +{ + /// + /// Summary description for STPStartInfo. + /// + public class STPStartInfo : WIGStartInfo + { + private int _idleTimeout = SmartThreadPool.DefaultIdleTimeout; + private int _minWorkerThreads = SmartThreadPool.DefaultMinWorkerThreads; + private int _maxWorkerThreads = SmartThreadPool.DefaultMaxWorkerThreads; +#if !(WINDOWS_PHONE) + private ThreadPriority _threadPriority = SmartThreadPool.DefaultThreadPriority; +#endif + private string _performanceCounterInstanceName = SmartThreadPool.DefaultPerformanceCounterInstanceName; + private bool _areThreadsBackground = SmartThreadPool.DefaultAreThreadsBackground; + private bool _enableLocalPerformanceCounters; + private string _threadPoolName = SmartThreadPool.DefaultThreadPoolName; + private int? _maxStackSize = SmartThreadPool.DefaultMaxStackSize; + + public STPStartInfo() + { + _performanceCounterInstanceName = SmartThreadPool.DefaultPerformanceCounterInstanceName; +#if !(WINDOWS_PHONE) + _threadPriority = SmartThreadPool.DefaultThreadPriority; +#endif + _maxWorkerThreads = SmartThreadPool.DefaultMaxWorkerThreads; + _idleTimeout = SmartThreadPool.DefaultIdleTimeout; + _minWorkerThreads = SmartThreadPool.DefaultMinWorkerThreads; + } + + public STPStartInfo(STPStartInfo stpStartInfo) + : base(stpStartInfo) + { + _idleTimeout = stpStartInfo.IdleTimeout; + _minWorkerThreads = stpStartInfo.MinWorkerThreads; + _maxWorkerThreads = stpStartInfo.MaxWorkerThreads; +#if !(WINDOWS_PHONE) + _threadPriority = stpStartInfo.ThreadPriority; +#endif + _performanceCounterInstanceName = stpStartInfo.PerformanceCounterInstanceName; + _enableLocalPerformanceCounters = stpStartInfo._enableLocalPerformanceCounters; + _threadPoolName = stpStartInfo._threadPoolName; + _areThreadsBackground = stpStartInfo.AreThreadsBackground; +#if !(_SILVERLIGHT) && !(WINDOWS_PHONE) + _apartmentState = stpStartInfo._apartmentState; +#endif + } + + /// + /// Get/Set the idle timeout in milliseconds. + /// If a thread is idle (starved) longer than IdleTimeout then it may quit. + /// + public virtual int IdleTimeout + { + get { return _idleTimeout; } + set + { + ThrowIfReadOnly(); + _idleTimeout = value; + } + } + + + /// + /// Get/Set the lower limit of threads in the pool. + /// + public virtual int MinWorkerThreads + { + get { return _minWorkerThreads; } + set + { + ThrowIfReadOnly(); + _minWorkerThreads = value; + } + } + + + /// + /// Get/Set the upper limit of threads in the pool. + /// + public virtual int MaxWorkerThreads + { + get { return _maxWorkerThreads; } + set + { + ThrowIfReadOnly(); + _maxWorkerThreads = value; + } + } + +#if !(WINDOWS_PHONE) + /// + /// Get/Set the scheduling priority of the threads in the pool. + /// The Os handles the scheduling. + /// + public virtual ThreadPriority ThreadPriority + { + get { return _threadPriority; } + set + { + ThrowIfReadOnly(); + _threadPriority = value; + } + } +#endif + /// + /// Get/Set the thread pool name. Threads will get names depending on this. + /// + public virtual string ThreadPoolName { + get { return _threadPoolName; } + set + { + ThrowIfReadOnly (); + _threadPoolName = value; + } + } + + /// + /// Get/Set the performance counter instance name of this SmartThreadPool + /// The default is null which indicate not to use performance counters at all. + /// + public virtual string PerformanceCounterInstanceName + { + get { return _performanceCounterInstanceName; } + set + { + ThrowIfReadOnly(); + _performanceCounterInstanceName = value; + } + } + + /// + /// Enable/Disable the local performance counter. + /// This enables the user to get some performance information about the SmartThreadPool + /// without using Windows performance counters. (Useful on WindowsCE, Silverlight, etc.) + /// The default is false. + /// + public virtual bool EnableLocalPerformanceCounters + { + get { return _enableLocalPerformanceCounters; } + set + { + ThrowIfReadOnly(); + _enableLocalPerformanceCounters = value; + } + } + + /// + /// Get/Set backgroundness of thread in thread pool. + /// + public virtual bool AreThreadsBackground + { + get { return _areThreadsBackground; } + set + { + ThrowIfReadOnly (); + _areThreadsBackground = value; + } + } + + /// + /// Get a readonly version of this STPStartInfo. + /// + /// Returns a readonly reference to this STPStartInfo + public new STPStartInfo AsReadOnly() + { + return new STPStartInfo(this) { _readOnly = true }; + } + +#if !(_SILVERLIGHT) && !(WINDOWS_PHONE) + + private ApartmentState _apartmentState = SmartThreadPool.DefaultApartmentState; + + /// + /// Get/Set the apartment state of threads in the thread pool + /// + public ApartmentState ApartmentState + { + get { return _apartmentState; } + set + { + ThrowIfReadOnly(); + _apartmentState = value; + } + } + +#if !(_SILVERLIGHT) && !(WINDOWS_PHONE) + + /// + /// Get/Set the max stack size of threads in the thread pool + /// + public int? MaxStackSize + { + get { return _maxStackSize; } + set + { + ThrowIfReadOnly(); + if (value.HasValue && value.Value < 0) + { + throw new ArgumentOutOfRangeException("value", "Value must be greater than 0."); + } + _maxStackSize = value; + } + } +#endif + +#endif + } +} -- cgit v1.1