From 5e4d6cab00cb29cd088ab7b62ab13aff103b64cb Mon Sep 17 00:00:00 2001 From: onefang Date: Sun, 19 May 2019 21:24:15 +1000 Subject: Dump OpenSim 0.9.0.1 into it's own branch. --- ThirdParty/SmartThreadPool/CallerThreadContext.cs | 198 +-- ThirdParty/SmartThreadPool/EventWaitHandle.cs | 30 +- .../SmartThreadPool/EventWaitHandleFactory.cs | 6 +- ThirdParty/SmartThreadPool/Interfaces.cs | 444 +++--- ThirdParty/SmartThreadPool/InternalInterfaces.cs | 2 +- ThirdParty/SmartThreadPool/PriorityQueue.cs | 446 +++--- .../SmartThreadPool/STPPerformanceCounter.cs | 606 ++++---- ThirdParty/SmartThreadPool/STPStartInfo.cs | 150 +- .../SmartThreadPool/SmartThreadPool.ThreadEntry.cs | 6 +- ThirdParty/SmartThreadPool/SmartThreadPool.cs | 1643 ++++++++++---------- ThirdParty/SmartThreadPool/WIGStartInfo.cs | 218 +-- ThirdParty/SmartThreadPool/WorkItem.cs | 20 +- ThirdParty/SmartThreadPool/WorkItemFactory.cs | 592 +++---- ThirdParty/SmartThreadPool/WorkItemInfo.cs | 104 +- ThirdParty/SmartThreadPool/WorkItemsGroup.cs | 452 +++--- ThirdParty/SmartThreadPool/WorkItemsGroupBase.cs | 14 +- ThirdParty/SmartThreadPool/WorkItemsQueue.cs | 871 +++++------ 17 files changed, 2904 insertions(+), 2898 deletions(-) (limited to 'ThirdParty') diff --git a/ThirdParty/SmartThreadPool/CallerThreadContext.cs b/ThirdParty/SmartThreadPool/CallerThreadContext.cs index e63add5..925c39b 100644 --- a/ThirdParty/SmartThreadPool/CallerThreadContext.cs +++ b/ThirdParty/SmartThreadPool/CallerThreadContext.cs @@ -13,125 +13,125 @@ namespace Amib.Threading.Internal { #region CallerThreadContext class - /// - /// This class stores the caller call context in order to restore - /// it when the work item is executed in the thread pool environment. - /// - internal class CallerThreadContext - { + /// + /// This class stores the caller call context in order to restore + /// it when the work item is executed in the thread pool environment. + /// + internal class CallerThreadContext + { #region Prepare reflection information - // Cached type information. - private static readonly MethodInfo getLogicalCallContextMethodInfo = - typeof(Thread).GetMethod("GetLogicalCallContext", BindingFlags.Instance | BindingFlags.NonPublic); + // Cached type information. + private static readonly MethodInfo getLogicalCallContextMethodInfo = + typeof(Thread).GetMethod("GetLogicalCallContext", BindingFlags.Instance | BindingFlags.NonPublic); - private static readonly MethodInfo setLogicalCallContextMethodInfo = - typeof(Thread).GetMethod("SetLogicalCallContext", BindingFlags.Instance | BindingFlags.NonPublic); + private static readonly MethodInfo setLogicalCallContextMethodInfo = + typeof(Thread).GetMethod("SetLogicalCallContext", BindingFlags.Instance | BindingFlags.NonPublic); - private static string HttpContextSlotName = GetHttpContextSlotName(); + private static string HttpContextSlotName = GetHttpContextSlotName(); - private static string GetHttpContextSlotName() - { - FieldInfo fi = typeof(HttpContext).GetField("CallContextSlotName", BindingFlags.Static | BindingFlags.NonPublic); + private static string GetHttpContextSlotName() + { + FieldInfo fi = typeof(HttpContext).GetField("CallContextSlotName", BindingFlags.Static | BindingFlags.NonPublic); if (fi != null) { return (string) fi.GetValue(null); } - return "HttpContext"; - } + return "HttpContext"; + } #endregion #region Private fields - private HttpContext _httpContext; - private LogicalCallContext _callContext; + private HttpContext _httpContext; + private LogicalCallContext _callContext; #endregion - /// - /// Constructor - /// - private CallerThreadContext() - { - } - - public bool CapturedCallContext - { - get - { - return (null != _callContext); - } - } - - public bool CapturedHttpContext - { - get - { - return (null != _httpContext); - } - } - - /// - /// Captures the current thread context - /// - /// - public static CallerThreadContext Capture( - bool captureCallContext, - bool captureHttpContext) - { - Debug.Assert(captureCallContext || captureHttpContext); - - CallerThreadContext callerThreadContext = new CallerThreadContext(); - - // TODO: In NET 2.0, redo using the new feature of ExecutionContext class - Capture() - // Capture Call Context - if(captureCallContext && (getLogicalCallContextMethodInfo != null)) - { - callerThreadContext._callContext = (LogicalCallContext)getLogicalCallContextMethodInfo.Invoke(Thread.CurrentThread, null); - if (callerThreadContext._callContext != null) - { - callerThreadContext._callContext = (LogicalCallContext)callerThreadContext._callContext.Clone(); - } - } - - // Capture httpContext - if (captureHttpContext && (null != HttpContext.Current)) - { - callerThreadContext._httpContext = HttpContext.Current; - } - - return callerThreadContext; - } - - /// - /// Applies the thread context stored earlier - /// - /// - public static void Apply(CallerThreadContext callerThreadContext) - { - if (null == callerThreadContext) - { - throw new ArgumentNullException("callerThreadContext"); - } - - // Todo: In NET 2.0, redo using the new feature of ExecutionContext class - Run() - // Restore call context - if ((callerThreadContext._callContext != null) && (setLogicalCallContextMethodInfo != null)) - { - setLogicalCallContextMethodInfo.Invoke(Thread.CurrentThread, new object[] { callerThreadContext._callContext }); - } - - // Restore HttpContext - if (callerThreadContext._httpContext != null) - { + /// + /// Constructor + /// + private CallerThreadContext() + { + } + + public bool CapturedCallContext + { + get + { + return (null != _callContext); + } + } + + public bool CapturedHttpContext + { + get + { + return (null != _httpContext); + } + } + + /// + /// Captures the current thread context + /// + /// + public static CallerThreadContext Capture( + bool captureCallContext, + bool captureHttpContext) + { + Debug.Assert(captureCallContext || captureHttpContext); + + CallerThreadContext callerThreadContext = new CallerThreadContext(); + + // TODO: In NET 2.0, redo using the new feature of ExecutionContext class - Capture() + // Capture Call Context + if(captureCallContext && (getLogicalCallContextMethodInfo != null)) + { + callerThreadContext._callContext = (LogicalCallContext)getLogicalCallContextMethodInfo.Invoke(Thread.CurrentThread, null); + if (callerThreadContext._callContext != null) + { + callerThreadContext._callContext = (LogicalCallContext)callerThreadContext._callContext.Clone(); + } + } + + // Capture httpContext + if (captureHttpContext && (null != HttpContext.Current)) + { + callerThreadContext._httpContext = HttpContext.Current; + } + + return callerThreadContext; + } + + /// + /// Applies the thread context stored earlier + /// + /// + public static void Apply(CallerThreadContext callerThreadContext) + { + if (null == callerThreadContext) + { + throw new ArgumentNullException("callerThreadContext"); + } + + // Todo: In NET 2.0, redo using the new feature of ExecutionContext class - Run() + // Restore call context + if ((callerThreadContext._callContext != null) && (setLogicalCallContextMethodInfo != null)) + { + setLogicalCallContextMethodInfo.Invoke(Thread.CurrentThread, new object[] { callerThreadContext._callContext }); + } + + // Restore HttpContext + if (callerThreadContext._httpContext != null) + { HttpContext.Current = callerThreadContext._httpContext; - //CallContext.SetData(HttpContextSlotName, callerThreadContext._httpContext); - } - } - } + //CallContext.SetData(HttpContextSlotName, callerThreadContext._httpContext); + } + } + } #endregion } diff --git a/ThirdParty/SmartThreadPool/EventWaitHandle.cs b/ThirdParty/SmartThreadPool/EventWaitHandle.cs index 25be07a..b7983cd 100644 --- a/ThirdParty/SmartThreadPool/EventWaitHandle.cs +++ b/ThirdParty/SmartThreadPool/EventWaitHandle.cs @@ -36,34 +36,34 @@ namespace Amib.Threading.Internal return waitHandle.WaitOne(millisecondsTimeout, exitContext); } - private static IntPtr[] PrepareNativeHandles(WaitHandle[] waitHandles) - { - IntPtr[] nativeHandles = new IntPtr[waitHandles.Length]; - for (int i = 0; i < waitHandles.Length; i++) - { + private static IntPtr[] PrepareNativeHandles(WaitHandle[] waitHandles) + { + IntPtr[] nativeHandles = new IntPtr[waitHandles.Length]; + for (int i = 0; i < waitHandles.Length; i++) + { nativeHandles[i] = waitHandles[i].Handle; - } - return nativeHandles; - } + } + return nativeHandles; + } - public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext) - { + public static bool WaitAll(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext) + { uint timeout = millisecondsTimeout < 0 ? INFINITE : (uint)millisecondsTimeout; IntPtr[] nativeHandles = PrepareNativeHandles(waitHandles); - int result = WaitForMultipleObjects((uint)waitHandles.Length, nativeHandles, true, timeout); + int result = WaitForMultipleObjects((uint)waitHandles.Length, nativeHandles, true, timeout); if (result == WAIT_TIMEOUT || result == WAIT_FAILED) { return false; } - return true; - } + return true; + } - public static int WaitAny(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext) + public static int WaitAny(WaitHandle[] waitHandles, int millisecondsTimeout, bool exitContext) { uint timeout = millisecondsTimeout < 0 ? INFINITE : (uint)millisecondsTimeout; @@ -90,7 +90,7 @@ namespace Amib.Threading.Internal return WaitAny(waitHandles, millisecondsTimeout, false); } - + #endregion #region External methods diff --git a/ThirdParty/SmartThreadPool/EventWaitHandleFactory.cs b/ThirdParty/SmartThreadPool/EventWaitHandleFactory.cs index 3c9c849..ece24de 100644 --- a/ThirdParty/SmartThreadPool/EventWaitHandleFactory.cs +++ b/ThirdParty/SmartThreadPool/EventWaitHandleFactory.cs @@ -10,7 +10,7 @@ namespace Amib.Threading.Internal /// /// EventWaitHandleFactory class. /// This is a static class that creates AutoResetEvent and ManualResetEvent objects. - /// In WindowCE the WaitForMultipleObjects API fails to use the Handle property + /// In WindowCE the WaitForMultipleObjects API fails to use the Handle property /// of XxxResetEvent. It can use only handles that were created by the CreateEvent API. /// Consequently this class creates the needed XxxResetEvent and replaces the handle if /// it's a WindowsCE OS. @@ -57,7 +57,7 @@ namespace Amib.Threading.Internal /// The initial state of the event private static void ReplaceEventHandle(WaitHandle waitHandle, bool manualReset, bool initialState) { - // Store the old handle + // Store the old handle IntPtr oldHandle = waitHandle.Handle; // Create a new event @@ -67,7 +67,7 @@ namespace Amib.Threading.Internal waitHandle.Handle = newHandle; // Close the old event - CloseHandle (oldHandle); + CloseHandle (oldHandle); } [DllImport("coredll.dll", SetLastError = true)] diff --git a/ThirdParty/SmartThreadPool/Interfaces.cs b/ThirdParty/SmartThreadPool/Interfaces.cs index 513422f..8cc23a0 100644 --- a/ThirdParty/SmartThreadPool/Interfaces.cs +++ b/ThirdParty/SmartThreadPool/Interfaces.cs @@ -3,18 +3,18 @@ using System.Threading; namespace Amib.Threading { - #region Delegates - - /// - /// A delegate that represents the method to run as the work item - /// - /// A state object for the method to run - public delegate object WorkItemCallback(object state); - - /// - /// A delegate to call after the WorkItemCallback completed - /// - /// The work item result object + #region Delegates + + /// + /// A delegate that represents the method to run as the work item + /// + /// A state object for the method to run + public delegate object WorkItemCallback(object state); + + /// + /// A delegate to call after the WorkItemCallback completed + /// + /// The work item result object public delegate void PostExecuteWorkItemCallback(IWorkItemResult wir); /// @@ -23,56 +23,56 @@ namespace Amib.Threading /// The work item result object public delegate void PostExecuteWorkItemCallback(IWorkItemResult wir); - /// - /// A delegate to call when a WorkItemsGroup becomes idle - /// - /// A reference to the WorkItemsGroup that became idle - public delegate void WorkItemsGroupIdleHandler(IWorkItemsGroup workItemsGroup); + /// + /// A delegate to call when a WorkItemsGroup becomes idle + /// + /// A reference to the WorkItemsGroup that became idle + public delegate void WorkItemsGroupIdleHandler(IWorkItemsGroup workItemsGroup); /// - /// A delegate to call after a thread is created, but before + /// A delegate to call after a thread is created, but before /// it's first use. /// public delegate void ThreadInitializationHandler(); /// - /// A delegate to call when a thread is about to exit, after + /// A delegate to call when a thread is about to exit, after /// it is no longer belong to the pool. /// public delegate void ThreadTerminationHandler(); - #endregion + #endregion - #region WorkItem Priority + #region WorkItem Priority /// /// Defines the availeable priorities of a work item. /// The higher the priority a work item has, the sooner /// it will be executed. /// - public enum WorkItemPriority - { - Lowest, - BelowNormal, - Normal, - AboveNormal, - Highest, - } + public enum WorkItemPriority + { + Lowest, + BelowNormal, + Normal, + AboveNormal, + Highest, + } - #endregion + #endregion - #region IWorkItemsGroup interface + #region IWorkItemsGroup interface - /// - /// IWorkItemsGroup interface + /// + /// IWorkItemsGroup interface /// Created by SmartThreadPool.CreateWorkItemsGroup() - /// - public interface IWorkItemsGroup - { - /// - /// Get/Set the name of the WorkItemsGroup - /// - string Name { get; set; } + /// + public interface IWorkItemsGroup + { + /// + /// Get/Set the name of the WorkItemsGroup + /// + string Name { get; set; } /// /// Get/Set the maximum number of workitem that execute cocurrency on the thread pool @@ -115,14 +115,14 @@ namespace Amib.Threading /// /// Wait for all work item to complete. /// - void WaitForIdle(); + void WaitForIdle(); /// /// Wait for all work item to complete, until timeout expired /// /// How long to wait for the work items to complete /// Returns true if work items completed within the timeout, otherwise false. - bool WaitForIdle(TimeSpan timeout); + bool WaitForIdle(TimeSpan timeout); /// /// Wait for all work item to complete, until timeout expired @@ -150,7 +150,7 @@ namespace Amib.Threading /// Queue a work item /// /// A callback to execute - /// Returns a work item result + /// Returns a work item result IWorkItemResult QueueWorkItem(WorkItemCallback callback); /// @@ -166,7 +166,7 @@ namespace Amib.Threading /// /// A callback to execute /// - /// The context object of the work item. Used for passing arguments to the work item. + /// The context object of the work item. Used for passing arguments to the work item. /// /// Returns a work item result IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state); @@ -176,7 +176,7 @@ namespace Amib.Threading /// /// A callback to execute /// - /// The context object of the work item. Used for passing arguments to the work item. + /// The context object of the work item. Used for passing arguments to the work item. /// /// The work item priority /// Returns a work item result @@ -187,7 +187,7 @@ namespace Amib.Threading /// /// A callback to execute /// - /// The context object of the work item. Used for passing arguments to the work item. + /// The context object of the work item. Used for passing arguments to the work item. /// /// /// A delegate to call after the callback completion @@ -200,7 +200,7 @@ namespace Amib.Threading /// /// A callback to execute /// - /// The context object of the work item. Used for passing arguments to the work item. + /// The context object of the work item. Used for passing arguments to the work item. /// /// /// A delegate to call after the callback completion @@ -214,7 +214,7 @@ namespace Amib.Threading /// /// A callback to execute /// - /// The context object of the work item. Used for passing arguments to the work item. + /// The context object of the work item. Used for passing arguments to the work item. /// /// /// A delegate to call after the callback completion @@ -228,7 +228,7 @@ namespace Amib.Threading /// /// A callback to execute /// - /// The context object of the work item. Used for passing arguments to the work item. + /// The context object of the work item. Used for passing arguments to the work item. /// /// /// A delegate to call after the callback completion @@ -252,7 +252,7 @@ namespace Amib.Threading /// Work item information /// A callback to execute /// - /// The context object of the work item. Used for passing arguments to the work item. + /// The context object of the work item. Used for passing arguments to the work item. /// /// Returns a work item result IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WorkItemCallback callback, object state); @@ -328,72 +328,72 @@ namespace Amib.Threading /// /// Queue a work item. /// - /// Returns a IWorkItemResult<TResult> object. + /// Returns a IWorkItemResult<TResult> object. /// its GetResult() returns a TResult object IWorkItemResult QueueWorkItem(Func func); /// /// Queue a work item. /// - /// Returns a IWorkItemResult<TResult> object. + /// Returns a IWorkItemResult<TResult> object. /// its GetResult() returns a TResult object IWorkItemResult QueueWorkItem(Func func, T arg); /// /// Queue a work item. /// - /// Returns a IWorkItemResult<TResult> object. + /// Returns a IWorkItemResult<TResult> object. /// its GetResult() returns a TResult object IWorkItemResult QueueWorkItem(Func func, T1 arg1, T2 arg2); /// /// Queue a work item. /// - /// Returns a IWorkItemResult<TResult> object. + /// Returns a IWorkItemResult<TResult> object. /// its GetResult() returns a TResult object IWorkItemResult QueueWorkItem(Func func, T1 arg1, T2 arg2, T3 arg3); /// /// Queue a work item. /// - /// Returns a IWorkItemResult<TResult> object. + /// Returns a IWorkItemResult<TResult> object. /// its GetResult() returns a TResult object IWorkItemResult QueueWorkItem(Func func, T1 arg1, T2 arg2, T3 arg3, T4 arg4); #endregion } - #endregion + #endregion - #region CallToPostExecute enumerator + #region CallToPostExecute enumerator - [Flags] - public enum CallToPostExecute - { + [Flags] + public enum CallToPostExecute + { /// /// Never call to the PostExecute call back /// - Never = 0x00, + Never = 0x00, /// /// Call to the PostExecute only when the work item is cancelled /// - WhenWorkItemCanceled = 0x01, + WhenWorkItemCanceled = 0x01, /// /// Call to the PostExecute only when the work item is not cancelled /// - WhenWorkItemNotCanceled = 0x02, + WhenWorkItemNotCanceled = 0x02, /// /// Always call to the PostExecute /// - Always = WhenWorkItemCanceled | WhenWorkItemNotCanceled, - } + Always = WhenWorkItemCanceled | WhenWorkItemNotCanceled, + } - #endregion + #endregion - #region IWorkItemResult interface + #region IWorkItemResult interface /// /// The common interface of IWorkItemResult and IWorkItemResult<T> @@ -421,159 +421,159 @@ namespace Amib.Threading { } - /// + /// /// IWorkItemResult<TResult> interface. /// Created when a Func<TResult> work item is queued. - /// + /// public interface IWorkItemResult : IWaitableResult - { - /// - /// Get the result of the work item. - /// If the work item didn't run yet then the caller waits. - /// - /// The result of the work item + { + /// + /// Get the result of the work item. + /// If the work item didn't run yet then the caller waits. + /// + /// The result of the work item TResult GetResult(); - /// - /// Get the result of the work item. - /// If the work item didn't run yet then the caller waits until timeout. - /// - /// The result of the work item - /// On timeout throws WorkItemTimeoutException + /// + /// Get the result of the work item. + /// If the work item didn't run yet then the caller waits until timeout. + /// + /// The result of the work item + /// On timeout throws WorkItemTimeoutException + TResult GetResult( + int millisecondsTimeout, + bool exitContext); + + /// + /// Get the result of the work item. + /// If the work item didn't run yet then the caller waits until timeout. + /// + /// The result of the work item + /// On timeout throws WorkItemTimeoutException + TResult GetResult( + TimeSpan timeout, + bool exitContext); + + /// + /// Get the result of the work item. + /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled. + /// + /// Timeout in milliseconds, or -1 for infinite + /// + /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. + /// + /// A cancel wait handle to interrupt the blocking if needed + /// The result of the work item + /// On timeout throws WorkItemTimeoutException + /// On cancel throws WorkItemCancelException + TResult GetResult( + int millisecondsTimeout, + bool exitContext, + WaitHandle cancelWaitHandle); + + /// + /// Get the result of the work item. + /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled. + /// + /// The result of the work item + /// On timeout throws WorkItemTimeoutException + /// On cancel throws WorkItemCancelException TResult GetResult( - int millisecondsTimeout, - bool exitContext); - - /// - /// Get the result of the work item. - /// If the work item didn't run yet then the caller waits until timeout. - /// - /// The result of the work item - /// On timeout throws WorkItemTimeoutException - TResult GetResult( - TimeSpan timeout, - bool exitContext); - - /// - /// Get the result of the work item. - /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled. - /// - /// Timeout in milliseconds, or -1 for infinite - /// - /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. - /// - /// A cancel wait handle to interrupt the blocking if needed - /// The result of the work item - /// On timeout throws WorkItemTimeoutException - /// On cancel throws WorkItemCancelException - TResult GetResult( - int millisecondsTimeout, - bool exitContext, - WaitHandle cancelWaitHandle); - - /// - /// Get the result of the work item. - /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled. - /// - /// The result of the work item - /// On timeout throws WorkItemTimeoutException - /// On cancel throws WorkItemCancelException - TResult GetResult( - TimeSpan timeout, - bool exitContext, - WaitHandle cancelWaitHandle); - - /// - /// Get the result of the work item. - /// If the work item didn't run yet then the caller waits. - /// - /// Filled with the exception if one was thrown - /// The result of the work item + TimeSpan timeout, + bool exitContext, + WaitHandle cancelWaitHandle); + + /// + /// Get the result of the work item. + /// If the work item didn't run yet then the caller waits. + /// + /// Filled with the exception if one was thrown + /// The result of the work item TResult GetResult(out Exception e); - /// - /// Get the result of the work item. - /// If the work item didn't run yet then the caller waits until timeout. - /// - /// - /// - /// Filled with the exception if one was thrown - /// The result of the work item - /// On timeout throws WorkItemTimeoutException + /// + /// Get the result of the work item. + /// If the work item didn't run yet then the caller waits until timeout. + /// + /// + /// + /// Filled with the exception if one was thrown + /// The result of the work item + /// On timeout throws WorkItemTimeoutException + TResult GetResult( + int millisecondsTimeout, + bool exitContext, + out Exception e); + + /// + /// Get the result of the work item. + /// If the work item didn't run yet then the caller waits until timeout. + /// + /// + /// Filled with the exception if one was thrown + /// + /// The result of the work item + /// On timeout throws WorkItemTimeoutException + TResult GetResult( + TimeSpan timeout, + bool exitContext, + out Exception e); + + /// + /// Get the result of the work item. + /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled. + /// + /// Timeout in milliseconds, or -1 for infinite + /// + /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. + /// + /// A cancel wait handle to interrupt the blocking if needed + /// Filled with the exception if one was thrown + /// The result of the work item + /// On timeout throws WorkItemTimeoutException + /// On cancel throws WorkItemCancelException + TResult GetResult( + int millisecondsTimeout, + bool exitContext, + WaitHandle cancelWaitHandle, + out Exception e); + + /// + /// Get the result of the work item. + /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled. + /// + /// The result of the work item + /// + /// Filled with the exception if one was thrown + /// + /// + /// On timeout throws WorkItemTimeoutException + /// On cancel throws WorkItemCancelException TResult GetResult( - int millisecondsTimeout, - bool exitContext, - out Exception e); - - /// - /// Get the result of the work item. - /// If the work item didn't run yet then the caller waits until timeout. - /// - /// - /// Filled with the exception if one was thrown - /// - /// The result of the work item - /// On timeout throws WorkItemTimeoutException - TResult GetResult( - TimeSpan timeout, - bool exitContext, - out Exception e); - - /// - /// Get the result of the work item. - /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled. - /// - /// Timeout in milliseconds, or -1 for infinite - /// - /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. - /// - /// A cancel wait handle to interrupt the blocking if needed - /// Filled with the exception if one was thrown - /// The result of the work item - /// On timeout throws WorkItemTimeoutException - /// On cancel throws WorkItemCancelException - TResult GetResult( - int millisecondsTimeout, - bool exitContext, - WaitHandle cancelWaitHandle, - out Exception e); - - /// - /// Get the result of the work item. - /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled. - /// - /// The result of the work item - /// - /// Filled with the exception if one was thrown - /// - /// - /// On timeout throws WorkItemTimeoutException - /// On cancel throws WorkItemCancelException - TResult GetResult( - TimeSpan timeout, - bool exitContext, - WaitHandle cancelWaitHandle, - out Exception e); - - /// - /// Gets an indication whether the asynchronous operation has completed. - /// - bool IsCompleted { get; } - - /// - /// Gets an indication whether the asynchronous operation has been canceled. - /// - bool IsCanceled { get; } - - /// - /// Gets the user-defined object that contains context data + TimeSpan timeout, + bool exitContext, + WaitHandle cancelWaitHandle, + out Exception e); + + /// + /// Gets an indication whether the asynchronous operation has completed. + /// + bool IsCompleted { get; } + + /// + /// Gets an indication whether the asynchronous operation has been canceled. + /// + bool IsCanceled { get; } + + /// + /// Gets the user-defined object that contains context data /// for the work item method. - /// - object State { get; } + /// + object State { get; } - /// + /// /// Same as Cancel(false). - /// + /// bool Cancel(); /// @@ -582,7 +582,7 @@ namespace Amib.Threading /// If the work item is completed, it will remain completed /// If the work item is in progress then the user can check the SmartThreadPool.IsWorkItemCanceled /// property to check if the work item has been cancelled. If the abortExecution is set to true then - /// the Smart Thread Pool will send an AbortException to the running thread to stop the execution + /// the Smart Thread Pool will send an AbortException to the running thread to stop the execution /// of the work item. When an in progress work item is canceled its GetResult will throw WorkItemCancelException. /// If the work item is already cancelled it will remain cancelled /// @@ -590,23 +590,23 @@ namespace Amib.Threading /// Returns true if the work item was not completed, otherwise false. bool Cancel(bool abortExecution); - /// - /// Get the work item's priority - /// - WorkItemPriority WorkItemPriority { get; } + /// + /// Get the work item's priority + /// + WorkItemPriority WorkItemPriority { get; } - /// - /// Return the result, same as GetResult() - /// + /// + /// Return the result, same as GetResult() + /// TResult Result { get; } - /// - /// Returns the exception if occured otherwise returns null. - /// - object Exception { get; } - } + /// + /// Returns the exception if occured otherwise returns null. + /// + object Exception { get; } + } - #endregion + #endregion #region .NET 3.5 diff --git a/ThirdParty/SmartThreadPool/InternalInterfaces.cs b/ThirdParty/SmartThreadPool/InternalInterfaces.cs index 0072e10..3055117 100644 --- a/ThirdParty/SmartThreadPool/InternalInterfaces.cs +++ b/ThirdParty/SmartThreadPool/InternalInterfaces.cs @@ -16,7 +16,7 @@ namespace Amib.Threading.Internal { /// /// This method is intent for internal use. - /// + /// IWorkItemResult GetWorkItemResult(); } diff --git a/ThirdParty/SmartThreadPool/PriorityQueue.cs b/ThirdParty/SmartThreadPool/PriorityQueue.cs index 409c879..3ea6084 100644 --- a/ThirdParty/SmartThreadPool/PriorityQueue.cs +++ b/ThirdParty/SmartThreadPool/PriorityQueue.cs @@ -5,235 +5,235 @@ using System.Diagnostics; namespace Amib.Threading.Internal { - #region PriorityQueue class - - /// - /// PriorityQueue class - /// This class is not thread safe because we use external lock - /// - public sealed class PriorityQueue : IEnumerable - { - #region Private members - - /// - /// The number of queues, there is one for each type of priority - /// - private const int _queuesCount = WorkItemPriority.Highest-WorkItemPriority.Lowest+1; - - /// - /// Work items queues. There is one for each type of priority - /// + #region PriorityQueue class + + /// + /// PriorityQueue class + /// This class is not thread safe because we use external lock + /// + public sealed class PriorityQueue : IEnumerable + { + #region Private members + + /// + /// The number of queues, there is one for each type of priority + /// + private const int _queuesCount = WorkItemPriority.Highest-WorkItemPriority.Lowest+1; + + /// + /// Work items queues. There is one for each type of priority + /// private readonly LinkedList[] _queues = new LinkedList[_queuesCount]; - /// - /// The total number of work items within the queues - /// - private int _workItemsCount; + /// + /// The total number of work items within the queues + /// + private int _workItemsCount; - /// - /// Use with IEnumerable interface - /// - private int _version; + /// + /// Use with IEnumerable interface + /// + private int _version; - #endregion + #endregion - #region Contructor + #region Contructor - public PriorityQueue() - { - for(int i = 0; i < _queues.Length; ++i) - { + public PriorityQueue() + { + for(int i = 0; i < _queues.Length; ++i) + { _queues[i] = new LinkedList(); - } - } - - #endregion - - #region Methods - - /// - /// Enqueue a work item. - /// - /// A work item - public void Enqueue(IHasWorkItemPriority workItem) - { - Debug.Assert(null != workItem); - - int queueIndex = _queuesCount-(int)workItem.WorkItemPriority-1; - Debug.Assert(queueIndex >= 0); - Debug.Assert(queueIndex < _queuesCount); - - _queues[queueIndex].AddLast(workItem); - ++_workItemsCount; - ++_version; - } - - /// - /// Dequeque a work item. - /// - /// Returns the next work item - public IHasWorkItemPriority Dequeue() - { - IHasWorkItemPriority workItem = null; - - if(_workItemsCount > 0) - { - int queueIndex = GetNextNonEmptyQueue(-1); - Debug.Assert(queueIndex >= 0); + } + } + + #endregion + + #region Methods + + /// + /// Enqueue a work item. + /// + /// A work item + public void Enqueue(IHasWorkItemPriority workItem) + { + Debug.Assert(null != workItem); + + int queueIndex = _queuesCount-(int)workItem.WorkItemPriority-1; + Debug.Assert(queueIndex >= 0); + Debug.Assert(queueIndex < _queuesCount); + + _queues[queueIndex].AddLast(workItem); + ++_workItemsCount; + ++_version; + } + + /// + /// Dequeque a work item. + /// + /// Returns the next work item + public IHasWorkItemPriority Dequeue() + { + IHasWorkItemPriority workItem = null; + + if(_workItemsCount > 0) + { + int queueIndex = GetNextNonEmptyQueue(-1); + Debug.Assert(queueIndex >= 0); workItem = _queues[queueIndex].First.Value; - _queues[queueIndex].RemoveFirst(); - Debug.Assert(null != workItem); - --_workItemsCount; - ++_version; - } - - return workItem; - } - - /// - /// Find the next non empty queue starting at queue queueIndex+1 - /// - /// The index-1 to start from - /// - /// The index of the next non empty queue or -1 if all the queues are empty - /// - private int GetNextNonEmptyQueue(int queueIndex) - { - for(int i = queueIndex+1; i < _queuesCount; ++i) - { - if(_queues[i].Count > 0) - { - return i; - } - } - return -1; - } - - /// - /// The number of work items - /// - public int Count - { - get - { - return _workItemsCount; - } - } - - /// - /// Clear all the work items - /// - public void Clear() - { - if (_workItemsCount > 0) - { - foreach(LinkedList queue in _queues) - { - queue.Clear(); - } - _workItemsCount = 0; - ++_version; - } - } - - #endregion - - #region IEnumerable Members - - /// - /// Returns an enumerator to iterate over the work items - /// - /// Returns an enumerator - public IEnumerator GetEnumerator() - { - return new PriorityQueueEnumerator(this); - } - - #endregion - - #region PriorityQueueEnumerator - - /// - /// The class the implements the enumerator - /// - private class PriorityQueueEnumerator : IEnumerator - { - private readonly PriorityQueue _priorityQueue; - private int _version; - private int _queueIndex; - private IEnumerator _enumerator; - - public PriorityQueueEnumerator(PriorityQueue priorityQueue) - { - _priorityQueue = priorityQueue; - _version = _priorityQueue._version; - _queueIndex = _priorityQueue.GetNextNonEmptyQueue(-1); - if (_queueIndex >= 0) - { - _enumerator = _priorityQueue._queues[_queueIndex].GetEnumerator(); - } - else - { - _enumerator = null; - } - } - - #region IEnumerator Members - - public void Reset() - { - _version = _priorityQueue._version; - _queueIndex = _priorityQueue.GetNextNonEmptyQueue(-1); - if (_queueIndex >= 0) - { - _enumerator = _priorityQueue._queues[_queueIndex].GetEnumerator(); - } - else - { - _enumerator = null; - } - } - - public object Current - { - get - { - Debug.Assert(null != _enumerator); - return _enumerator.Current; - } - } - - public bool MoveNext() - { - if (null == _enumerator) - { - return false; - } - - if(_version != _priorityQueue._version) - { - throw new InvalidOperationException("The collection has been modified"); - - } - if (!_enumerator.MoveNext()) - { - _queueIndex = _priorityQueue.GetNextNonEmptyQueue(_queueIndex); - if(-1 == _queueIndex) - { - return false; - } - _enumerator = _priorityQueue._queues[_queueIndex].GetEnumerator(); - _enumerator.MoveNext(); - return true; - } - return true; - } - - #endregion - } - - #endregion - } - - #endregion + _queues[queueIndex].RemoveFirst(); + Debug.Assert(null != workItem); + --_workItemsCount; + ++_version; + } + + return workItem; + } + + /// + /// Find the next non empty queue starting at queue queueIndex+1 + /// + /// The index-1 to start from + /// + /// The index of the next non empty queue or -1 if all the queues are empty + /// + private int GetNextNonEmptyQueue(int queueIndex) + { + for(int i = queueIndex+1; i < _queuesCount; ++i) + { + if(_queues[i].Count > 0) + { + return i; + } + } + return -1; + } + + /// + /// The number of work items + /// + public int Count + { + get + { + return _workItemsCount; + } + } + + /// + /// Clear all the work items + /// + public void Clear() + { + if (_workItemsCount > 0) + { + foreach(LinkedList queue in _queues) + { + queue.Clear(); + } + _workItemsCount = 0; + ++_version; + } + } + + #endregion + + #region IEnumerable Members + + /// + /// Returns an enumerator to iterate over the work items + /// + /// Returns an enumerator + public IEnumerator GetEnumerator() + { + return new PriorityQueueEnumerator(this); + } + + #endregion + + #region PriorityQueueEnumerator + + /// + /// The class the implements the enumerator + /// + private class PriorityQueueEnumerator : IEnumerator + { + private readonly PriorityQueue _priorityQueue; + private int _version; + private int _queueIndex; + private IEnumerator _enumerator; + + public PriorityQueueEnumerator(PriorityQueue priorityQueue) + { + _priorityQueue = priorityQueue; + _version = _priorityQueue._version; + _queueIndex = _priorityQueue.GetNextNonEmptyQueue(-1); + if (_queueIndex >= 0) + { + _enumerator = _priorityQueue._queues[_queueIndex].GetEnumerator(); + } + else + { + _enumerator = null; + } + } + + #region IEnumerator Members + + public void Reset() + { + _version = _priorityQueue._version; + _queueIndex = _priorityQueue.GetNextNonEmptyQueue(-1); + if (_queueIndex >= 0) + { + _enumerator = _priorityQueue._queues[_queueIndex].GetEnumerator(); + } + else + { + _enumerator = null; + } + } + + public object Current + { + get + { + Debug.Assert(null != _enumerator); + return _enumerator.Current; + } + } + + public bool MoveNext() + { + if (null == _enumerator) + { + return false; + } + + if(_version != _priorityQueue._version) + { + throw new InvalidOperationException("The collection has been modified"); + + } + if (!_enumerator.MoveNext()) + { + _queueIndex = _priorityQueue.GetNextNonEmptyQueue(_queueIndex); + if(-1 == _queueIndex) + { + return false; + } + _enumerator = _priorityQueue._queues[_queueIndex].GetEnumerator(); + _enumerator.MoveNext(); + return true; + } + return true; + } + + #endregion + } + + #endregion + } + + #endregion } diff --git a/ThirdParty/SmartThreadPool/STPPerformanceCounter.cs b/ThirdParty/SmartThreadPool/STPPerformanceCounter.cs index 0663d1d..bd68499 100644 --- a/ThirdParty/SmartThreadPool/STPPerformanceCounter.cs +++ b/ThirdParty/SmartThreadPool/STPPerformanceCounter.cs @@ -26,188 +26,188 @@ namespace Amib.Threading.Internal #if !(_WINDOWS_CE) && !(_SILVERLIGHT) && !(WINDOWS_PHONE) internal enum STPPerformanceCounterType - { - // Fields - ActiveThreads = 0, - InUseThreads = 1, - OverheadThreads = 2, - OverheadThreadsPercent = 3, - OverheadThreadsPercentBase = 4, - - WorkItems = 5, - WorkItemsInQueue = 6, - WorkItemsProcessed = 7, - - WorkItemsQueuedPerSecond = 8, - WorkItemsProcessedPerSecond = 9, - - AvgWorkItemWaitTime = 10, - AvgWorkItemWaitTimeBase = 11, - - AvgWorkItemProcessTime = 12, - AvgWorkItemProcessTimeBase = 13, - - WorkItemsGroups = 14, - - LastCounter = 14, - } - - - /// - /// Summary description for STPPerformanceCounter. - /// - internal class STPPerformanceCounter - { - // Fields - private readonly PerformanceCounterType _pcType; - protected string _counterHelp; - protected string _counterName; - - // Methods - public STPPerformanceCounter( - string counterName, - string counterHelp, - PerformanceCounterType pcType) - { - _counterName = counterName; - _counterHelp = counterHelp; - _pcType = pcType; - } - - public void AddCounterToCollection(CounterCreationDataCollection counterData) - { - CounterCreationData counterCreationData = new CounterCreationData( - _counterName, - _counterHelp, - _pcType); - - counterData.Add(counterCreationData); - } - - // Properties - public string Name - { - get - { - return _counterName; - } - } - } - - internal class STPPerformanceCounters - { - // Fields - internal STPPerformanceCounter[] _stpPerformanceCounters; - private static readonly STPPerformanceCounters _instance; - internal const string _stpCategoryHelp = "SmartThreadPool performance counters"; - internal const string _stpCategoryName = "SmartThreadPool"; - - // Methods - static STPPerformanceCounters() - { - _instance = new STPPerformanceCounters(); - } - - private STPPerformanceCounters() - { - STPPerformanceCounter[] stpPerformanceCounters = new STPPerformanceCounter[] - { - new STPPerformanceCounter("Active threads", "The current number of available in the thread pool.", PerformanceCounterType.NumberOfItems32), - new STPPerformanceCounter("In use threads", "The current number of threads that execute a work item.", PerformanceCounterType.NumberOfItems32), - new STPPerformanceCounter("Overhead threads", "The current number of threads that are active, but are not in use.", PerformanceCounterType.NumberOfItems32), - new STPPerformanceCounter("% overhead threads", "The current number of threads that are active, but are not in use in percents.", PerformanceCounterType.RawFraction), - new STPPerformanceCounter("% overhead threads base", "The current number of threads that are active, but are not in use in percents.", PerformanceCounterType.RawBase), - - new STPPerformanceCounter("Work Items", "The number of work items in the Smart Thread Pool. Both queued and processed.", PerformanceCounterType.NumberOfItems32), - new STPPerformanceCounter("Work Items in queue", "The current number of work items in the queue", PerformanceCounterType.NumberOfItems32), - new STPPerformanceCounter("Work Items processed", "The number of work items already processed", PerformanceCounterType.NumberOfItems32), - - new STPPerformanceCounter("Work Items queued/sec", "The number of work items queued per second", PerformanceCounterType.RateOfCountsPerSecond32), - new STPPerformanceCounter("Work Items processed/sec", "The number of work items processed per second", PerformanceCounterType.RateOfCountsPerSecond32), - - new STPPerformanceCounter("Avg. Work Item wait time/sec", "The average time a work item supends in the queue waiting for its turn to execute.", PerformanceCounterType.AverageCount64), - new STPPerformanceCounter("Avg. Work Item wait time base", "The average time a work item supends in the queue waiting for its turn to execute.", PerformanceCounterType.AverageBase), - - new STPPerformanceCounter("Avg. Work Item process time/sec", "The average time it takes to process a work item.", PerformanceCounterType.AverageCount64), - new STPPerformanceCounter("Avg. Work Item process time base", "The average time it takes to process a work item.", PerformanceCounterType.AverageBase), - - new STPPerformanceCounter("Work Items Groups", "The current number of work item groups associated with the Smart Thread Pool.", PerformanceCounterType.NumberOfItems32), - }; - - _stpPerformanceCounters = stpPerformanceCounters; - SetupCategory(); - } - - private void SetupCategory() - { - if (!PerformanceCounterCategory.Exists(_stpCategoryName)) - { - CounterCreationDataCollection counters = new CounterCreationDataCollection(); - - for (int i = 0; i < _stpPerformanceCounters.Length; i++) - { - _stpPerformanceCounters[i].AddCounterToCollection(counters); - } - - PerformanceCounterCategory.Create( - _stpCategoryName, - _stpCategoryHelp, + { + // Fields + ActiveThreads = 0, + InUseThreads = 1, + OverheadThreads = 2, + OverheadThreadsPercent = 3, + OverheadThreadsPercentBase = 4, + + WorkItems = 5, + WorkItemsInQueue = 6, + WorkItemsProcessed = 7, + + WorkItemsQueuedPerSecond = 8, + WorkItemsProcessedPerSecond = 9, + + AvgWorkItemWaitTime = 10, + AvgWorkItemWaitTimeBase = 11, + + AvgWorkItemProcessTime = 12, + AvgWorkItemProcessTimeBase = 13, + + WorkItemsGroups = 14, + + LastCounter = 14, + } + + + /// + /// Summary description for STPPerformanceCounter. + /// + internal class STPPerformanceCounter + { + // Fields + private readonly PerformanceCounterType _pcType; + protected string _counterHelp; + protected string _counterName; + + // Methods + public STPPerformanceCounter( + string counterName, + string counterHelp, + PerformanceCounterType pcType) + { + _counterName = counterName; + _counterHelp = counterHelp; + _pcType = pcType; + } + + public void AddCounterToCollection(CounterCreationDataCollection counterData) + { + CounterCreationData counterCreationData = new CounterCreationData( + _counterName, + _counterHelp, + _pcType); + + counterData.Add(counterCreationData); + } + + // Properties + public string Name + { + get + { + return _counterName; + } + } + } + + internal class STPPerformanceCounters + { + // Fields + internal STPPerformanceCounter[] _stpPerformanceCounters; + private static readonly STPPerformanceCounters _instance; + internal const string _stpCategoryHelp = "SmartThreadPool performance counters"; + internal const string _stpCategoryName = "SmartThreadPool"; + + // Methods + static STPPerformanceCounters() + { + _instance = new STPPerformanceCounters(); + } + + private STPPerformanceCounters() + { + STPPerformanceCounter[] stpPerformanceCounters = new STPPerformanceCounter[] + { + new STPPerformanceCounter("Active threads", "The current number of available in the thread pool.", PerformanceCounterType.NumberOfItems32), + new STPPerformanceCounter("In use threads", "The current number of threads that execute a work item.", PerformanceCounterType.NumberOfItems32), + new STPPerformanceCounter("Overhead threads", "The current number of threads that are active, but are not in use.", PerformanceCounterType.NumberOfItems32), + new STPPerformanceCounter("% overhead threads", "The current number of threads that are active, but are not in use in percents.", PerformanceCounterType.RawFraction), + new STPPerformanceCounter("% overhead threads base", "The current number of threads that are active, but are not in use in percents.", PerformanceCounterType.RawBase), + + new STPPerformanceCounter("Work Items", "The number of work items in the Smart Thread Pool. Both queued and processed.", PerformanceCounterType.NumberOfItems32), + new STPPerformanceCounter("Work Items in queue", "The current number of work items in the queue", PerformanceCounterType.NumberOfItems32), + new STPPerformanceCounter("Work Items processed", "The number of work items already processed", PerformanceCounterType.NumberOfItems32), + + new STPPerformanceCounter("Work Items queued/sec", "The number of work items queued per second", PerformanceCounterType.RateOfCountsPerSecond32), + new STPPerformanceCounter("Work Items processed/sec", "The number of work items processed per second", PerformanceCounterType.RateOfCountsPerSecond32), + + new STPPerformanceCounter("Avg. Work Item wait time/sec", "The average time a work item supends in the queue waiting for its turn to execute.", PerformanceCounterType.AverageCount64), + new STPPerformanceCounter("Avg. Work Item wait time base", "The average time a work item supends in the queue waiting for its turn to execute.", PerformanceCounterType.AverageBase), + + new STPPerformanceCounter("Avg. Work Item process time/sec", "The average time it takes to process a work item.", PerformanceCounterType.AverageCount64), + new STPPerformanceCounter("Avg. Work Item process time base", "The average time it takes to process a work item.", PerformanceCounterType.AverageBase), + + new STPPerformanceCounter("Work Items Groups", "The current number of work item groups associated with the Smart Thread Pool.", PerformanceCounterType.NumberOfItems32), + }; + + _stpPerformanceCounters = stpPerformanceCounters; + SetupCategory(); + } + + private void SetupCategory() + { + if (!PerformanceCounterCategory.Exists(_stpCategoryName)) + { + CounterCreationDataCollection counters = new CounterCreationDataCollection(); + + for (int i = 0; i < _stpPerformanceCounters.Length; i++) + { + _stpPerformanceCounters[i].AddCounterToCollection(counters); + } + + PerformanceCounterCategory.Create( + _stpCategoryName, + _stpCategoryHelp, PerformanceCounterCategoryType.MultiInstance, - counters); - - } - } - - // Properties - public static STPPerformanceCounters Instance - { - get - { - return _instance; - } - } - } - - internal class STPInstancePerformanceCounter : IDisposable - { - // Fields + counters); + + } + } + + // Properties + public static STPPerformanceCounters Instance + { + get + { + return _instance; + } + } + } + + internal class STPInstancePerformanceCounter : IDisposable + { + // Fields private bool _isDisposed; - private PerformanceCounter _pcs; + private PerformanceCounter _pcs; - // Methods - protected STPInstancePerformanceCounter() - { + // Methods + protected STPInstancePerformanceCounter() + { _isDisposed = false; - } - - public STPInstancePerformanceCounter( - string instance, - STPPerformanceCounterType spcType) : this() - { - STPPerformanceCounters counters = STPPerformanceCounters.Instance; - _pcs = new PerformanceCounter( - STPPerformanceCounters._stpCategoryName, - counters._stpPerformanceCounters[(int) spcType].Name, - instance, - false); - _pcs.RawValue = _pcs.RawValue; - } - - - public void Close() - { - if (_pcs != null) - { - _pcs.RemoveInstance(); - _pcs.Close(); - _pcs = null; - } - } - - public void Dispose() - { + } + + public STPInstancePerformanceCounter( + string instance, + STPPerformanceCounterType spcType) : this() + { + STPPerformanceCounters counters = STPPerformanceCounters.Instance; + _pcs = new PerformanceCounter( + STPPerformanceCounters._stpCategoryName, + counters._stpPerformanceCounters[(int) spcType].Name, + instance, + false); + _pcs.RawValue = _pcs.RawValue; + } + + + public void Close() + { + if (_pcs != null) + { + _pcs.RemoveInstance(); + _pcs.Close(); + _pcs = null; + } + } + + public void Dispose() + { Dispose(true); - } + } public virtual void Dispose(bool disposing) { @@ -220,90 +220,90 @@ namespace Amib.Threading.Internal } _isDisposed = true; } - - public virtual void Increment() - { - _pcs.Increment(); - } - - public virtual void IncrementBy(long val) - { - _pcs.IncrementBy(val); - } - - public virtual void Set(long val) - { - _pcs.RawValue = val; - } - } - - internal class STPInstanceNullPerformanceCounter : STPInstancePerformanceCounter - { - // Methods - public override void Increment() {} - public override void IncrementBy(long value) {} - public override void Set(long val) {} - } - - - - internal class STPInstancePerformanceCounters : ISTPInstancePerformanceCounters - { + + public virtual void Increment() + { + _pcs.Increment(); + } + + public virtual void IncrementBy(long val) + { + _pcs.IncrementBy(val); + } + + public virtual void Set(long val) + { + _pcs.RawValue = val; + } + } + + internal class STPInstanceNullPerformanceCounter : STPInstancePerformanceCounter + { + // Methods + public override void Increment() {} + public override void IncrementBy(long value) {} + public override void Set(long val) {} + } + + + + internal class STPInstancePerformanceCounters : ISTPInstancePerformanceCounters + { private bool _isDisposed; - // Fields - private STPInstancePerformanceCounter[] _pcs; - private static readonly STPInstancePerformanceCounter _stpInstanceNullPerformanceCounter; - - // Methods - static STPInstancePerformanceCounters() - { - _stpInstanceNullPerformanceCounter = new STPInstanceNullPerformanceCounter(); - } - - public STPInstancePerformanceCounters(string instance) - { + // Fields + private STPInstancePerformanceCounter[] _pcs; + private static readonly STPInstancePerformanceCounter _stpInstanceNullPerformanceCounter; + + // Methods + static STPInstancePerformanceCounters() + { + _stpInstanceNullPerformanceCounter = new STPInstanceNullPerformanceCounter(); + } + + public STPInstancePerformanceCounters(string instance) + { _isDisposed = false; - _pcs = new STPInstancePerformanceCounter[(int)STPPerformanceCounterType.LastCounter]; + _pcs = new STPInstancePerformanceCounter[(int)STPPerformanceCounterType.LastCounter]; // Call the STPPerformanceCounters.Instance so the static constructor will // intialize the STPPerformanceCounters singleton. - STPPerformanceCounters.Instance.GetHashCode(); - - for (int i = 0; i < _pcs.Length; i++) - { - if (instance != null) - { - _pcs[i] = new STPInstancePerformanceCounter( - instance, - (STPPerformanceCounterType) i); - } - else - { - _pcs[i] = _stpInstanceNullPerformanceCounter; - } - } - } - - - public void Close() - { - if (null != _pcs) - { - for (int i = 0; i < _pcs.Length; i++) - { + STPPerformanceCounters.Instance.GetHashCode(); + + for (int i = 0; i < _pcs.Length; i++) + { + if (instance != null) + { + _pcs[i] = new STPInstancePerformanceCounter( + instance, + (STPPerformanceCounterType) i); + } + else + { + _pcs[i] = _stpInstanceNullPerformanceCounter; + } + } + } + + + public void Close() + { + if (null != _pcs) + { + for (int i = 0; i < _pcs.Length; i++) + { if (null != _pcs[i]) { _pcs[i].Dispose(); } - } - _pcs = null; - } - } + } + _pcs = null; + } + } - public void Dispose() - { + public void Dispose() + { Dispose(true); - } + } public virtual void Dispose(bool disposing) { @@ -316,62 +316,62 @@ namespace Amib.Threading.Internal } _isDisposed = true; } - - private STPInstancePerformanceCounter GetCounter(STPPerformanceCounterType spcType) - { - return _pcs[(int) spcType]; - } - - public void SampleThreads(long activeThreads, long inUseThreads) - { - GetCounter(STPPerformanceCounterType.ActiveThreads).Set(activeThreads); - GetCounter(STPPerformanceCounterType.InUseThreads).Set(inUseThreads); - GetCounter(STPPerformanceCounterType.OverheadThreads).Set(activeThreads-inUseThreads); - - GetCounter(STPPerformanceCounterType.OverheadThreadsPercentBase).Set(activeThreads-inUseThreads); - GetCounter(STPPerformanceCounterType.OverheadThreadsPercent).Set(inUseThreads); - } - - public void SampleWorkItems(long workItemsQueued, long workItemsProcessed) - { - GetCounter(STPPerformanceCounterType.WorkItems).Set(workItemsQueued+workItemsProcessed); - GetCounter(STPPerformanceCounterType.WorkItemsInQueue).Set(workItemsQueued); - GetCounter(STPPerformanceCounterType.WorkItemsProcessed).Set(workItemsProcessed); - - GetCounter(STPPerformanceCounterType.WorkItemsQueuedPerSecond).Set(workItemsQueued); - GetCounter(STPPerformanceCounterType.WorkItemsProcessedPerSecond).Set(workItemsProcessed); - } - - public void SampleWorkItemsWaitTime(TimeSpan workItemWaitTime) - { - GetCounter(STPPerformanceCounterType.AvgWorkItemWaitTime).IncrementBy((long)workItemWaitTime.TotalMilliseconds); - GetCounter(STPPerformanceCounterType.AvgWorkItemWaitTimeBase).Increment(); - } - - public void SampleWorkItemsProcessTime(TimeSpan workItemProcessTime) - { - GetCounter(STPPerformanceCounterType.AvgWorkItemProcessTime).IncrementBy((long)workItemProcessTime.TotalMilliseconds); - GetCounter(STPPerformanceCounterType.AvgWorkItemProcessTimeBase).Increment(); - } + + private STPInstancePerformanceCounter GetCounter(STPPerformanceCounterType spcType) + { + return _pcs[(int) spcType]; + } + + public void SampleThreads(long activeThreads, long inUseThreads) + { + GetCounter(STPPerformanceCounterType.ActiveThreads).Set(activeThreads); + GetCounter(STPPerformanceCounterType.InUseThreads).Set(inUseThreads); + GetCounter(STPPerformanceCounterType.OverheadThreads).Set(activeThreads-inUseThreads); + + GetCounter(STPPerformanceCounterType.OverheadThreadsPercentBase).Set(activeThreads-inUseThreads); + GetCounter(STPPerformanceCounterType.OverheadThreadsPercent).Set(inUseThreads); + } + + public void SampleWorkItems(long workItemsQueued, long workItemsProcessed) + { + GetCounter(STPPerformanceCounterType.WorkItems).Set(workItemsQueued+workItemsProcessed); + GetCounter(STPPerformanceCounterType.WorkItemsInQueue).Set(workItemsQueued); + GetCounter(STPPerformanceCounterType.WorkItemsProcessed).Set(workItemsProcessed); + + GetCounter(STPPerformanceCounterType.WorkItemsQueuedPerSecond).Set(workItemsQueued); + GetCounter(STPPerformanceCounterType.WorkItemsProcessedPerSecond).Set(workItemsProcessed); + } + + public void SampleWorkItemsWaitTime(TimeSpan workItemWaitTime) + { + GetCounter(STPPerformanceCounterType.AvgWorkItemWaitTime).IncrementBy((long)workItemWaitTime.TotalMilliseconds); + GetCounter(STPPerformanceCounterType.AvgWorkItemWaitTimeBase).Increment(); + } + + public void SampleWorkItemsProcessTime(TimeSpan workItemProcessTime) + { + GetCounter(STPPerformanceCounterType.AvgWorkItemProcessTime).IncrementBy((long)workItemProcessTime.TotalMilliseconds); + GetCounter(STPPerformanceCounterType.AvgWorkItemProcessTimeBase).Increment(); + } } #endif internal class NullSTPInstancePerformanceCounters : ISTPInstancePerformanceCounters, ISTPPerformanceCountersReader - { - private static readonly NullSTPInstancePerformanceCounters _instance = new NullSTPInstancePerformanceCounters(); - - public static NullSTPInstancePerformanceCounters Instance - { - get { return _instance; } - } - - public void Close() {} - public void Dispose() {} - - public void SampleThreads(long activeThreads, long inUseThreads) {} - public void SampleWorkItems(long workItemsQueued, long workItemsProcessed) {} - public void SampleWorkItemsWaitTime(TimeSpan workItemWaitTime) {} - public void SampleWorkItemsProcessTime(TimeSpan workItemProcessTime) {} + { + private static readonly NullSTPInstancePerformanceCounters _instance = new NullSTPInstancePerformanceCounters(); + + public static NullSTPInstancePerformanceCounters Instance + { + get { return _instance; } + } + + public void Close() {} + public void Dispose() {} + + public void SampleThreads(long activeThreads, long inUseThreads) {} + public void SampleWorkItems(long workItemsQueued, long workItemsProcessed) {} + public void SampleWorkItemsWaitTime(TimeSpan workItemWaitTime) {} + public void SampleWorkItemsProcessTime(TimeSpan workItemProcessTime) {} public long InUseThreads { get { return 0; } @@ -391,7 +391,7 @@ namespace Amib.Threading.Internal { get { return 0; } } - } + } internal class LocalSTPInstancePerformanceCounters : ISTPInstancePerformanceCounters, ISTPPerformanceCountersReader { diff --git a/ThirdParty/SmartThreadPool/STPStartInfo.cs b/ThirdParty/SmartThreadPool/STPStartInfo.cs index 96fa094..8ea547c 100644 --- a/ThirdParty/SmartThreadPool/STPStartInfo.cs +++ b/ThirdParty/SmartThreadPool/STPStartInfo.cs @@ -3,9 +3,9 @@ using System.Threading; namespace Amib.Threading { - /// - /// Summary description for STPStartInfo. - /// + /// + /// Summary description for STPStartInfo. + /// public class STPStartInfo : WIGStartInfo { private int _idleTimeout = SmartThreadPool.DefaultIdleTimeout; @@ -31,7 +31,7 @@ namespace Amib.Threading _minWorkerThreads = SmartThreadPool.DefaultMinWorkerThreads; } - public STPStartInfo(STPStartInfo stpStartInfo) + public STPStartInfo(STPStartInfo stpStartInfo) : base(stpStartInfo) { _idleTimeout = stpStartInfo.IdleTimeout; @@ -48,63 +48,63 @@ namespace Amib.Threading _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 + + /// + /// 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; + _idleTimeout = value; } - } + } - /// - /// Get/Set the lower limit of threads in the pool. - /// - public virtual int MinWorkerThreads - { - get { return _minWorkerThreads; } - set + /// + /// Get/Set the lower limit of threads in the pool. + /// + public virtual int MinWorkerThreads + { + get { return _minWorkerThreads; } + set { ThrowIfReadOnly(); - _minWorkerThreads = value; + _minWorkerThreads = value; } - } + } - /// - /// Get/Set the upper limit of threads in the pool. - /// - public virtual int MaxWorkerThreads - { - get { return _maxWorkerThreads; } - set + /// + /// Get/Set the upper limit of threads in the pool. + /// + public virtual int MaxWorkerThreads + { + get { return _maxWorkerThreads; } + set { ThrowIfReadOnly(); - _maxWorkerThreads = value; + _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 + /// + /// 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; + _threadPriority = value; } - } + } #endif /// /// Get/Set the thread pool name. Threads will get names depending on this. @@ -118,50 +118,50 @@ namespace Amib.Threading } } - /// - /// 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 + /// + /// 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; + _performanceCounterInstanceName = value; } - } + } /// /// Enable/Disable the local performance counter. - /// This enables the user to get some performance information about the SmartThreadPool + /// 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 { 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; - } - } - - /// + 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 @@ -185,10 +185,10 @@ namespace Amib.Threading ThrowIfReadOnly(); _apartmentState = value; } - } + } #if !(_SILVERLIGHT) && !(WINDOWS_PHONE) - + /// /// Get/Set the max stack size of threads in the thread pool /// diff --git a/ThirdParty/SmartThreadPool/SmartThreadPool.ThreadEntry.cs b/ThirdParty/SmartThreadPool/SmartThreadPool.ThreadEntry.cs index d9502bb..4713332 100644 --- a/ThirdParty/SmartThreadPool/SmartThreadPool.ThreadEntry.cs +++ b/ThirdParty/SmartThreadPool/SmartThreadPool.ThreadEntry.cs @@ -26,15 +26,15 @@ namespace Amib.Threading /// /// A reference from each thread in the thread pool to its SmartThreadPool /// object container. - /// With this variable a thread can know whatever it belongs to a + /// With this variable a thread can know whatever it belongs to a /// SmartThreadPool. /// private readonly SmartThreadPool _associatedSmartThreadPool; /// - /// A reference to the current work item a thread from the thread pool + /// A reference to the current work item a thread from the thread pool /// is executing. - /// + /// public WorkItem CurrentWorkItem { get; set; } public ThreadEntry(SmartThreadPool stp) diff --git a/ThirdParty/SmartThreadPool/SmartThreadPool.cs b/ThirdParty/SmartThreadPool/SmartThreadPool.cs index 615518e..7cc7fbf 100644 --- a/ThirdParty/SmartThreadPool/SmartThreadPool.cs +++ b/ThirdParty/SmartThreadPool/SmartThreadPool.cs @@ -3,7 +3,7 @@ // Smart Thread Pool // 7 Aug 2004 - Initial release // -// 14 Sep 2004 - Bug fixes +// 14 Sep 2004 - Bug fixes // // 15 Oct 2004 - Added new features // - Work items return result. @@ -18,13 +18,13 @@ // - Added finalizers. // - Changed Exceptions so they are serializable. // - Fixed the bug in one of the SmartThreadPool constructors. -// - Changed the SmartThreadPool.WaitAll() so it will support any number of waiters. +// - Changed the SmartThreadPool.WaitAll() so it will support any number of waiters. // The SmartThreadPool.WaitAny() is still limited by the .NET Framework. // - Added PostExecute with options on which cases to call it. // - Added option to dispose of the state objects. // - Added a WaitForIdle() method that waits until the work items queue is empty. // - Added an STPStartInfo class for the initialization of the thread pool. -// - Changed exception handling so if a work item throws an exception it +// - Changed exception handling so if a work item throws an exception it // is rethrown at GetResult(), rather then firing an UnhandledException event. // Note that PostExecute exception are always ignored. // @@ -32,10 +32,10 @@ // - Fixed lost of work items bug // // 3 Jul 2005: Changes. -// - Fixed bug where Enqueue() throws an exception because PopWaiter() returned null, hardly reconstructed. +// - Fixed bug where Enqueue() throws an exception because PopWaiter() returned null, hardly reconstructed. // // 16 Aug 2005: Changes. -// - Fixed bug where the InUseThreads becomes negative when canceling work items. +// - Fixed bug where the InUseThreads becomes negative when canceling work items. // // 31 Jan 2006 - Changes: // - Added work items priority @@ -45,7 +45,7 @@ // - Changed SmartThreadPool.WaitAll() behavior so when it gets empty array // it returns true rather then throwing an exception. // - Added option to start the STP and the WIG as suspended -// - Exception behavior changed, the real exception is returned by an +// - Exception behavior changed, the real exception is returned by an // inner exception // - Added option to keep the Http context of the caller thread. (Thanks to Steven T.) // - Added performance counters @@ -54,17 +54,17 @@ // 13 Feb 2006 - Changes: // - Added a call to the dispose of the Performance Counter so // their won't be a Performance Counter leak. -// - Added exception catch in case the Performance Counters cannot +// - Added exception catch in case the Performance Counters cannot // be created. // // 17 May 2008 - Changes: // - Changed the dispose behavior and removed the Finalizers. // - Enabled the change of the MaxThreads and MinThreads at run time. -// - Enabled the change of the Concurrency of a IWorkItemsGroup at run -// time If the IWorkItemsGroup is a SmartThreadPool then the Concurrency -// refers to the MaxThreads. +// - Enabled the change of the Concurrency of a IWorkItemsGroup at run +// time If the IWorkItemsGroup is a SmartThreadPool then the Concurrency +// refers to the MaxThreads. // - Improved the cancel behavior. -// - Added events for thread creation and termination. +// - Added events for thread creation and termination. // - Fixed the HttpContext context capture. // - Changed internal collections so they use generic collections // - Added IsIdle flag to the SmartThreadPool and IWorkItemsGroup @@ -83,7 +83,7 @@ // // 20 August 2012 - Changes: // - Added set name to threads -// - Fixed the WorkItemsQueue.Dequeue. +// - Fixed the WorkItemsQueue.Dequeue. // Replaced while (!Monitor.TryEnter(this)); with lock(this) { ... } // - Fixed SmartThreadPool.Pipe // - Added IsBackground option to threads @@ -92,7 +92,7 @@ // // 24 August 2012 - Changes: // - Enabled cancel abort after cancel. See: http://smartthreadpool.codeplex.com/discussions/345937 by alecswan -// - Added option to set MaxStackSize of threads +// - Added option to set MaxStackSize of threads #endregion @@ -108,77 +108,77 @@ using Amib.Threading.Internal; namespace Amib.Threading { - #region SmartThreadPool class - /// - /// Smart thread pool class. - /// - public partial class SmartThreadPool : WorkItemsGroupBase, IDisposable - { - #region Public Default Constants - - /// - /// Default minimum number of threads the thread pool contains. (0) - /// - public const int DefaultMinWorkerThreads = 0; - - /// - /// Default maximum number of threads the thread pool contains. (25) - /// - public const int DefaultMaxWorkerThreads = 25; - - /// - /// Default idle timeout in milliseconds. (One minute) - /// - public const int DefaultIdleTimeout = 60*1000; // One minute - - /// - /// Indicate to copy the security context of the caller and then use it in the call. (false) - /// - public const bool DefaultUseCallerCallContext = false; - - /// - /// Indicate to copy the HTTP context of the caller and then use it in the call. (false) - /// - public const bool DefaultUseCallerHttpContext = false; - - /// - /// Indicate to dispose of the state objects if they support the IDispose interface. (false) - /// - public const bool DefaultDisposeOfStateObjects = false; - - /// + #region SmartThreadPool class + /// + /// Smart thread pool class. + /// + public partial class SmartThreadPool : WorkItemsGroupBase, IDisposable + { + #region Public Default Constants + + /// + /// Default minimum number of threads the thread pool contains. (0) + /// + public const int DefaultMinWorkerThreads = 0; + + /// + /// Default maximum number of threads the thread pool contains. (25) + /// + public const int DefaultMaxWorkerThreads = 25; + + /// + /// Default idle timeout in milliseconds. (One minute) + /// + public const int DefaultIdleTimeout = 60*1000; // One minute + + /// + /// Indicate to copy the security context of the caller and then use it in the call. (false) + /// + public const bool DefaultUseCallerCallContext = false; + + /// + /// Indicate to copy the HTTP context of the caller and then use it in the call. (false) + /// + public const bool DefaultUseCallerHttpContext = false; + + /// + /// Indicate to dispose of the state objects if they support the IDispose interface. (false) + /// + public const bool DefaultDisposeOfStateObjects = false; + + /// /// The default option to run the post execute (CallToPostExecute.Always) - /// - public const CallToPostExecute DefaultCallToPostExecute = CallToPostExecute.Always; + /// + public const CallToPostExecute DefaultCallToPostExecute = CallToPostExecute.Always; - /// - /// The default post execute method to run. (None) - /// When null it means not to call it. - /// - public static readonly PostExecuteWorkItemCallback DefaultPostExecuteWorkItemCallback; + /// + /// The default post execute method to run. (None) + /// When null it means not to call it. + /// + public static readonly PostExecuteWorkItemCallback DefaultPostExecuteWorkItemCallback; - /// + /// /// The default work item priority (WorkItemPriority.Normal) - /// - public const WorkItemPriority DefaultWorkItemPriority = WorkItemPriority.Normal; + /// + public const WorkItemPriority DefaultWorkItemPriority = WorkItemPriority.Normal; - /// - /// The default is to work on work items as soon as they arrive - /// and not to wait for the start. (false) - /// - public const bool DefaultStartSuspended = false; + /// + /// The default is to work on work items as soon as they arrive + /// and not to wait for the start. (false) + /// + public const bool DefaultStartSuspended = false; - /// + /// /// The default name to use for the performance counters instance. (null) - /// - public static readonly string DefaultPerformanceCounterInstanceName; + /// + public static readonly string DefaultPerformanceCounterInstanceName; #if !(WINDOWS_PHONE) - /// + /// /// The default thread priority (ThreadPriority.Normal) - /// - public const ThreadPriority DefaultThreadPriority = ThreadPriority.Normal; + /// + public const ThreadPriority DefaultThreadPriority = ThreadPriority.Normal; #endif /// /// The default thread pool name. (SmartThreadPool) @@ -203,37 +203,37 @@ namespace Amib.Threading #if !(_SILVERLIGHT) && !(WINDOWS_PHONE) /// - /// The default apartment state of a thread in the thread pool. - /// The default is ApartmentState.Unknown which means the STP will not + /// The default apartment state of a thread in the thread pool. + /// The default is ApartmentState.Unknown which means the STP will not /// set the apartment of the thread. It will use the .NET default. /// public const ApartmentState DefaultApartmentState = ApartmentState.Unknown; #endif - #endregion + #endregion #region Member Variables - /// - /// Dictionary of all the threads in the thread pool. - /// + /// + /// Dictionary of all the threads in the thread pool. + /// private readonly SynchronizedDictionary _workerThreads = new SynchronizedDictionary(); - /// - /// Queue of work items. - /// - private readonly WorkItemsQueue _workItemsQueue = new WorkItemsQueue(); + /// + /// Queue of work items. + /// + private readonly WorkItemsQueue _workItemsQueue = new WorkItemsQueue(); - /// - /// Count the work items handled. - /// Used by the performance counter. - /// - private int _workItemsProcessed; + /// + /// Count the work items handled. + /// Used by the performance counter. + /// + private int _workItemsProcessed; - /// - /// Number of threads that currently work (not idle). - /// - private int _inUseWorkerThreads; + /// + /// Number of threads that currently work (not idle). + /// + private int _inUseWorkerThreads; /// /// Stores a copy of the original STPStartInfo. @@ -241,51 +241,51 @@ namespace Amib.Threading /// private STPStartInfo _stpStartInfo; - /// - /// Total number of work items that are stored in the work items queue - /// plus the work items that the threads in the pool are working on. - /// - private int _currentWorkItemsCount; + /// + /// Total number of work items that are stored in the work items queue + /// plus the work items that the threads in the pool are working on. + /// + private int _currentWorkItemsCount; - /// - /// Signaled when the thread pool is idle, i.e. no thread is busy - /// and the work items queue is empty - /// - //private ManualResetEvent _isIdleWaitHandle = new ManualResetEvent(true); - private ManualResetEvent _isIdleWaitHandle = EventWaitHandleFactory.CreateManualResetEvent(true); + /// + /// Signaled when the thread pool is idle, i.e. no thread is busy + /// and the work items queue is empty + /// + //private ManualResetEvent _isIdleWaitHandle = new ManualResetEvent(true); + private ManualResetEvent _isIdleWaitHandle = EventWaitHandleFactory.CreateManualResetEvent(true); - /// - /// An event to signal all the threads to quit immediately. - /// - //private ManualResetEvent _shuttingDownEvent = new ManualResetEvent(false); - private ManualResetEvent _shuttingDownEvent = EventWaitHandleFactory.CreateManualResetEvent(false); + /// + /// An event to signal all the threads to quit immediately. + /// + //private ManualResetEvent _shuttingDownEvent = new ManualResetEvent(false); + private ManualResetEvent _shuttingDownEvent = EventWaitHandleFactory.CreateManualResetEvent(false); /// /// A flag to indicate if the Smart Thread Pool is now suspended. /// private bool _isSuspended; - /// - /// A flag to indicate the threads to quit. - /// - private bool _shutdown; - - /// - /// Counts the threads created in the pool. - /// It is used to name the threads. - /// - private int _threadCounter; - - /// - /// Indicate that the SmartThreadPool has been disposed - /// - private bool _isDisposed; - - /// - /// Holds all the WorkItemsGroup instaces that have at least one - /// work item int the SmartThreadPool - /// This variable is used in case of Shutdown - /// + /// + /// A flag to indicate the threads to quit. + /// + private bool _shutdown; + + /// + /// Counts the threads created in the pool. + /// It is used to name the threads. + /// + private int _threadCounter; + + /// + /// Indicate that the SmartThreadPool has been disposed + /// + private bool _isDisposed; + + /// + /// Holds all the WorkItemsGroup instaces that have at least one + /// work item int the SmartThreadPool + /// This variable is used in case of Shutdown + /// private readonly SynchronizedDictionary _workItemsGroups = new SynchronizedDictionary(); /// @@ -305,7 +305,7 @@ namespace Amib.Threading private ISTPInstancePerformanceCounters _localPCs = NullSTPInstancePerformanceCounters.Instance; -#if (WINDOWS_PHONE) +#if (WINDOWS_PHONE) private static readonly Dictionary _threadEntries = new Dictionary(); #elif (_WINDOWS_CE) private static LocalDataStoreSlot _threadEntrySlot = Thread.AllocateDataSlot(); @@ -316,13 +316,13 @@ namespace Amib.Threading #endif /// - /// An event to call after a thread is created, but before + /// An event to call after a thread is created, but before /// it's first use. /// private event ThreadInitializationHandler _onThreadInitialization; /// - /// An event to call when a thread is about to exit, after + /// An event to call when a thread is about to exit, after /// it is no longer belong to the pool. /// private event ThreadTerminationHandler _onThreadTermination; @@ -332,7 +332,7 @@ namespace Amib.Threading #region Per thread properties /// - /// A reference to the current work item a thread from the thread pool + /// A reference to the current work item a thread from the thread pool /// is executing. /// internal static ThreadEntry CurrentThreadEntry @@ -383,78 +383,78 @@ namespace Amib.Threading #region Construction and Finalization /// - /// Constructor - /// - public SmartThreadPool() - { + /// Constructor + /// + public SmartThreadPool() + { _stpStartInfo = new STPStartInfo(); Initialize(); - } - - /// - /// Constructor - /// - /// Idle timeout in milliseconds - public SmartThreadPool(int idleTimeout) - { + } + + /// + /// Constructor + /// + /// Idle timeout in milliseconds + public SmartThreadPool(int idleTimeout) + { _stpStartInfo = new STPStartInfo { IdleTimeout = idleTimeout, }; - Initialize(); - } - - /// - /// Constructor - /// - /// Idle timeout in milliseconds - /// Upper limit of threads in the pool - public SmartThreadPool( - int idleTimeout, - int maxWorkerThreads) - { + Initialize(); + } + + /// + /// Constructor + /// + /// Idle timeout in milliseconds + /// Upper limit of threads in the pool + public SmartThreadPool( + int idleTimeout, + int maxWorkerThreads) + { _stpStartInfo = new STPStartInfo { IdleTimeout = idleTimeout, MaxWorkerThreads = maxWorkerThreads, }; - Initialize(); - } - - /// - /// Constructor - /// - /// Idle timeout in milliseconds - /// Upper limit of threads in the pool - /// Lower limit of threads in the pool - public SmartThreadPool( - int idleTimeout, - int maxWorkerThreads, - int minWorkerThreads) - { + Initialize(); + } + + /// + /// Constructor + /// + /// Idle timeout in milliseconds + /// Upper limit of threads in the pool + /// Lower limit of threads in the pool + public SmartThreadPool( + int idleTimeout, + int maxWorkerThreads, + int minWorkerThreads) + { _stpStartInfo = new STPStartInfo { IdleTimeout = idleTimeout, MaxWorkerThreads = maxWorkerThreads, MinWorkerThreads = minWorkerThreads, }; - Initialize(); - } + Initialize(); + } /// /// Constructor /// /// A SmartThreadPool configuration that overrides the default behavior - public SmartThreadPool(STPStartInfo stpStartInfo) - { - _stpStartInfo = new STPStartInfo(stpStartInfo); - Initialize(); - } - - private void Initialize() - { + public SmartThreadPool(STPStartInfo stpStartInfo) + { + _stpStartInfo = new STPStartInfo(stpStartInfo); + Initialize(); + } + + private void Initialize() + { Name = _stpStartInfo.ThreadPoolName; - ValidateSTPStartInfo(); + ValidateSTPStartInfo(); // _stpStartInfoRW stores a read/write copy of the STPStartInfo. // Actually only MaxWorkerThreads and MinWorkerThreads are overwritten @@ -462,8 +462,8 @@ namespace Amib.Threading _isSuspended = _stpStartInfo.StartSuspended; #if (_WINDOWS_CE) || (_SILVERLIGHT) || (_MONO) || (WINDOWS_PHONE) - if (null != _stpStartInfo.PerformanceCounterInstanceName) - { + if (null != _stpStartInfo.PerformanceCounterInstanceName) + { throw new NotSupportedException("Performance counters are not implemented for Compact Framework/Silverlight/Mono, instead use StpStartInfo.EnableLocalPerformanceCounters"); } #else @@ -486,110 +486,110 @@ namespace Amib.Threading _localPCs = new LocalSTPInstancePerformanceCounters(); } - // If the STP is not started suspended then start the threads. + // If the STP is not started suspended then start the threads. if (!_isSuspended) { StartOptimalNumberOfThreads(); } - } + } - private void StartOptimalNumberOfThreads() - { - int threadsCount = Math.Max(_workItemsQueue.Count, _stpStartInfo.MinWorkerThreads); + private void StartOptimalNumberOfThreads() + { + int threadsCount = Math.Max(_workItemsQueue.Count, _stpStartInfo.MinWorkerThreads); threadsCount = Math.Min(threadsCount, _stpStartInfo.MaxWorkerThreads); threadsCount -= _workerThreads.Count; if (threadsCount > 0) { StartThreads(threadsCount); } - } + } - private void ValidateSTPStartInfo() - { + private void ValidateSTPStartInfo() + { if (_stpStartInfo.MinWorkerThreads < 0) - { - throw new ArgumentOutOfRangeException( - "MinWorkerThreads", "MinWorkerThreads cannot be negative"); - } + { + throw new ArgumentOutOfRangeException( + "MinWorkerThreads", "MinWorkerThreads cannot be negative"); + } if (_stpStartInfo.MaxWorkerThreads <= 0) - { - throw new ArgumentOutOfRangeException( - "MaxWorkerThreads", "MaxWorkerThreads must be greater than zero"); - } + { + throw new ArgumentOutOfRangeException( + "MaxWorkerThreads", "MaxWorkerThreads must be greater than zero"); + } if (_stpStartInfo.MinWorkerThreads > _stpStartInfo.MaxWorkerThreads) - { - throw new ArgumentOutOfRangeException( - "MinWorkerThreads, maxWorkerThreads", - "MaxWorkerThreads must be greater or equal to MinWorkerThreads"); - } - } - - private static void ValidateCallback(Delegate callback) - { - if(callback.GetInvocationList().Length > 1) - { - throw new NotSupportedException("SmartThreadPool doesn't support delegates chains"); - } - } - - #endregion - - #region Thread Processing - - /// - /// Waits on the queue for a work item, shutdown, or timeout. - /// - /// - /// Returns the WaitingCallback or null in case of timeout or shutdown. - /// - private WorkItem Dequeue() - { - WorkItem workItem = + { + throw new ArgumentOutOfRangeException( + "MinWorkerThreads, maxWorkerThreads", + "MaxWorkerThreads must be greater or equal to MinWorkerThreads"); + } + } + + private static void ValidateCallback(Delegate callback) + { + if(callback.GetInvocationList().Length > 1) + { + throw new NotSupportedException("SmartThreadPool doesn't support delegates chains"); + } + } + + #endregion + + #region Thread Processing + + /// + /// Waits on the queue for a work item, shutdown, or timeout. + /// + /// + /// Returns the WaitingCallback or null in case of timeout or shutdown. + /// + private WorkItem Dequeue() + { + WorkItem workItem = _workItemsQueue.DequeueWorkItem(_stpStartInfo.IdleTimeout, _shuttingDownEvent); - return workItem; - } + return workItem; + } - /// - /// Put a new work item in the queue - /// - /// A work item to queue - internal override void Enqueue(WorkItem workItem) - { - // Make sure the workItem is not null - Debug.Assert(null != workItem); + /// + /// Put a new work item in the queue + /// + /// A work item to queue + internal override void Enqueue(WorkItem workItem) + { + // Make sure the workItem is not null + Debug.Assert(null != workItem); - IncrementWorkItemsCount(); + IncrementWorkItemsCount(); workItem.CanceledSmartThreadPool = _canceledSmartThreadPool; - _workItemsQueue.EnqueueWorkItem(workItem); - workItem.WorkItemIsQueued(); - - // If all the threads are busy then try to create a new one - if (_currentWorkItemsCount > _workerThreads.Count) - { - StartThreads(1); - } - } - - private void IncrementWorkItemsCount() - { - _windowsPCs.SampleWorkItems(_workItemsQueue.Count, _workItemsProcessed); + _workItemsQueue.EnqueueWorkItem(workItem); + workItem.WorkItemIsQueued(); + + // If all the threads are busy then try to create a new one + if (_currentWorkItemsCount > _workerThreads.Count) + { + StartThreads(1); + } + } + + private void IncrementWorkItemsCount() + { + _windowsPCs.SampleWorkItems(_workItemsQueue.Count, _workItemsProcessed); _localPCs.SampleWorkItems(_workItemsQueue.Count, _workItemsProcessed); - int count = Interlocked.Increment(ref _currentWorkItemsCount); - //Trace.WriteLine("WorkItemsCount = " + _currentWorkItemsCount.ToString()); - if (count == 1) - { + int count = Interlocked.Increment(ref _currentWorkItemsCount); + //Trace.WriteLine("WorkItemsCount = " + _currentWorkItemsCount.ToString()); + if (count == 1) + { IsIdle = false; _isIdleWaitHandle.Reset(); - } - } + } + } - private void DecrementWorkItemsCount() - { + private void DecrementWorkItemsCount() + { int count = Interlocked.Decrement(ref _currentWorkItemsCount); //Trace.WriteLine("WorkItemsCount = " + _currentWorkItemsCount.ToString()); if (count == 0) @@ -602,81 +602,81 @@ namespace Amib.Threading if (!_shutdown) { - // The counter counts even if the work item was cancelled - _windowsPCs.SampleWorkItems(_workItemsQueue.Count, _workItemsProcessed); + // The counter counts even if the work item was cancelled + _windowsPCs.SampleWorkItems(_workItemsQueue.Count, _workItemsProcessed); _localPCs.SampleWorkItems(_workItemsQueue.Count, _workItemsProcessed); } - } - - internal void RegisterWorkItemsGroup(IWorkItemsGroup workItemsGroup) - { - _workItemsGroups[workItemsGroup] = workItemsGroup; - } - - internal void UnregisterWorkItemsGroup(IWorkItemsGroup workItemsGroup) - { - if (_workItemsGroups.Contains(workItemsGroup)) - { - _workItemsGroups.Remove(workItemsGroup); - } - } - - /// - /// Inform that the current thread is about to quit or quiting. - /// The same thread may call this method more than once. - /// - private void InformCompleted() - { - // There is no need to lock the two methods together - // since only the current thread removes itself - // and the _workerThreads is a synchronized dictionary - if (_workerThreads.Contains(Thread.CurrentThread)) - { - _workerThreads.Remove(Thread.CurrentThread); - _windowsPCs.SampleThreads(_workerThreads.Count, _inUseWorkerThreads); + } + + internal void RegisterWorkItemsGroup(IWorkItemsGroup workItemsGroup) + { + _workItemsGroups[workItemsGroup] = workItemsGroup; + } + + internal void UnregisterWorkItemsGroup(IWorkItemsGroup workItemsGroup) + { + if (_workItemsGroups.Contains(workItemsGroup)) + { + _workItemsGroups.Remove(workItemsGroup); + } + } + + /// + /// Inform that the current thread is about to quit or quiting. + /// The same thread may call this method more than once. + /// + private void InformCompleted() + { + // There is no need to lock the two methods together + // since only the current thread removes itself + // and the _workerThreads is a synchronized dictionary + if (_workerThreads.Contains(Thread.CurrentThread)) + { + _workerThreads.Remove(Thread.CurrentThread); + _windowsPCs.SampleThreads(_workerThreads.Count, _inUseWorkerThreads); _localPCs.SampleThreads(_workerThreads.Count, _inUseWorkerThreads); - } - } - - /// - /// Starts new threads - /// - /// The number of threads to start - private void StartThreads(int threadsCount) - { + } + } + + /// + /// Starts new threads + /// + /// The number of threads to start + private void StartThreads(int threadsCount) + { if (_isSuspended) - { - return; - } - - lock(_workerThreads.SyncRoot) - { - // Don't start threads on shut down - if (_shutdown) - { - return; - } - - for(int i = 0; i < threadsCount; ++i) - { - // Don't create more threads then the upper limit + { + return; + } + + lock(_workerThreads.SyncRoot) + { + // Don't start threads on shut down + if (_shutdown) + { + return; + } + + for(int i = 0; i < threadsCount; ++i) + { + // Don't create more threads then the upper limit if (_workerThreads.Count >= _stpStartInfo.MaxWorkerThreads) - { - return; - } + { + return; + } // Create a new thread #if (_SILVERLIGHT) || (WINDOWS_PHONE) - Thread workerThread = new Thread(ProcessQueuedItems); + Thread workerThread = new Thread(ProcessQueuedItems); #else Thread workerThread = _stpStartInfo.MaxStackSize.HasValue ? new Thread(ProcessQueuedItems, _stpStartInfo.MaxStackSize.Value) : new Thread(ProcessQueuedItems); #endif - // Configure the new thread and start it + // Configure the new thread and start it workerThread.IsBackground = _stpStartInfo.AreThreadsBackground; #if !(_SILVERLIGHT) && !(_WINDOWS_CE) && !(WINDOWS_PHONE) @@ -689,39 +689,40 @@ namespace Amib.Threading #if !(_SILVERLIGHT) && !(WINDOWS_PHONE) workerThread.Priority = _stpStartInfo.ThreadPriority; #endif - workerThread.Start(); workerThread.Name = string.Format("STP:{0}:{1}", Name, _threadCounter); - ++_threadCounter; + workerThread.Start(); + + ++_threadCounter; // Add it to the dictionary and update its creation time. _workerThreads[workerThread] = new ThreadEntry(this); - _windowsPCs.SampleThreads(_workerThreads.Count, _inUseWorkerThreads); + _windowsPCs.SampleThreads(_workerThreads.Count, _inUseWorkerThreads); _localPCs.SampleThreads(_workerThreads.Count, _inUseWorkerThreads); - } - } - } - - /// - /// A worker thread method that processes work items from the work items queue. - /// - private void ProcessQueuedItems() - { + } + } + } + + /// + /// A worker thread method that processes work items from the work items queue. + /// + private void ProcessQueuedItems() + { // Keep the entry of the dictionary as thread's variable to avoid the synchronization locks // of the dictionary. CurrentThreadEntry = _workerThreads[Thread.CurrentThread]; FireOnThreadInitialization(); - try - { - bool bInUseWorkerThreadsWasIncremented = false; + try + { + bool bInUseWorkerThreadsWasIncremented = false; - // Process until shutdown. - while(!_shutdown) - { - // Update the last time this thread was seen alive. - // It's good for debugging. + // Process until shutdown. + while(!_shutdown) + { + // Update the last time this thread was seen alive. + // It's good for debugging. CurrentThreadEntry.IAmAlive(); // The following block handles the when the MaxWorkerThreads has been @@ -743,166 +744,166 @@ namespace Amib.Threading } } - // Wait for a work item, shutdown, or timeout - WorkItem workItem = Dequeue(); + // Wait for a work item, shutdown, or timeout + WorkItem workItem = Dequeue(); - // Update the last time this thread was seen alive. - // It's good for debugging. + // Update the last time this thread was seen alive. + // It's good for debugging. CurrentThreadEntry.IAmAlive(); - // On timeout or shut down. - if (null == workItem) - { - // Double lock for quit. + // On timeout or shut down. + if (null == workItem) + { + // Double lock for quit. if (_workerThreads.Count > _stpStartInfo.MinWorkerThreads) - { - lock(_workerThreads.SyncRoot) - { + { + lock(_workerThreads.SyncRoot) + { if (_workerThreads.Count > _stpStartInfo.MinWorkerThreads) - { - // Inform that the thread is quiting and then quit. - // This method must be called within this lock or else - // more threads will quit and the thread pool will go - // below the lower limit. - InformCompleted(); - break; - } - } - } - } - - // If we didn't quit then skip to the next iteration. - if (null == workItem) - { - continue; - } - - try - { - // Initialize the value to false - bInUseWorkerThreadsWasIncremented = false; + { + // Inform that the thread is quiting and then quit. + // This method must be called within this lock or else + // more threads will quit and the thread pool will go + // below the lower limit. + InformCompleted(); + break; + } + } + } + } + + // If we didn't quit then skip to the next iteration. + if (null == workItem) + { + continue; + } + + try + { + // Initialize the value to false + bInUseWorkerThreadsWasIncremented = false; // Set the Current Work Item of the thread. - // Store the Current Work Item before the workItem.StartingWorkItem() is called, - // so WorkItem.Cancel can work when the work item is between InQueue and InProgress + // Store the Current Work Item before the workItem.StartingWorkItem() is called, + // so WorkItem.Cancel can work when the work item is between InQueue and InProgress // states. - // If the work item has been cancelled BEFORE the workItem.StartingWorkItem() + // If the work item has been cancelled BEFORE the workItem.StartingWorkItem() // (work item is in InQueue state) then workItem.StartingWorkItem() will return false. // If the work item has been cancelled AFTER the workItem.StartingWorkItem() then // (work item is in InProgress state) then the thread will be aborted CurrentThreadEntry.CurrentWorkItem = workItem; - // Change the state of the work item to 'in progress' if possible. - // We do it here so if the work item has been canceled we won't - // increment the _inUseWorkerThreads. - // The cancel mechanism doesn't delete items from the queue, - // it marks the work item as canceled, and when the work item - // is dequeued, we just skip it. - // If the post execute of work item is set to always or to - // call when the work item is canceled then the StartingWorkItem() - // will return true, so the post execute can run. - if (!workItem.StartingWorkItem()) - { - continue; - } - - // Execute the callback. Make sure to accurately - // record how many callbacks are currently executing. - int inUseWorkerThreads = Interlocked.Increment(ref _inUseWorkerThreads); - _windowsPCs.SampleThreads(_workerThreads.Count, inUseWorkerThreads); + // Change the state of the work item to 'in progress' if possible. + // We do it here so if the work item has been canceled we won't + // increment the _inUseWorkerThreads. + // The cancel mechanism doesn't delete items from the queue, + // it marks the work item as canceled, and when the work item + // is dequeued, we just skip it. + // If the post execute of work item is set to always or to + // call when the work item is canceled then the StartingWorkItem() + // will return true, so the post execute can run. + if (!workItem.StartingWorkItem()) + { + continue; + } + + // Execute the callback. Make sure to accurately + // record how many callbacks are currently executing. + int inUseWorkerThreads = Interlocked.Increment(ref _inUseWorkerThreads); + _windowsPCs.SampleThreads(_workerThreads.Count, inUseWorkerThreads); _localPCs.SampleThreads(_workerThreads.Count, inUseWorkerThreads); - // Mark that the _inUseWorkerThreads incremented, so in the finally{} - // statement we will decrement it correctly. - bInUseWorkerThreadsWasIncremented = true; + // Mark that the _inUseWorkerThreads incremented, so in the finally{} + // statement we will decrement it correctly. + bInUseWorkerThreadsWasIncremented = true; workItem.FireWorkItemStarted(); - ExecuteWorkItem(workItem); - } - catch(Exception ex) - { + ExecuteWorkItem(workItem); + } + catch(Exception ex) + { ex.GetHashCode(); - // Do nothing - } - finally - { - workItem.DisposeOfState(); - - // Set the CurrentWorkItem to null, since we - // no longer run user's code. + // Do nothing + } + finally + { + workItem.DisposeOfState(); + + // Set the CurrentWorkItem to null, since we + // no longer run user's code. CurrentThreadEntry.CurrentWorkItem = null; - // Decrement the _inUseWorkerThreads only if we had - // incremented it. Note the cancelled work items don't - // increment _inUseWorkerThreads. - if (bInUseWorkerThreadsWasIncremented) - { - int inUseWorkerThreads = Interlocked.Decrement(ref _inUseWorkerThreads); - _windowsPCs.SampleThreads(_workerThreads.Count, inUseWorkerThreads); + // Decrement the _inUseWorkerThreads only if we had + // incremented it. Note the cancelled work items don't + // increment _inUseWorkerThreads. + if (bInUseWorkerThreadsWasIncremented) + { + int inUseWorkerThreads = Interlocked.Decrement(ref _inUseWorkerThreads); + _windowsPCs.SampleThreads(_workerThreads.Count, inUseWorkerThreads); _localPCs.SampleThreads(_workerThreads.Count, inUseWorkerThreads); - } - - // Notify that the work item has been completed. - // WorkItemsGroup may enqueue their next work item. - workItem.FireWorkItemCompleted(); - - // Decrement the number of work items here so the idle - // ManualResetEvent won't fluctuate. - DecrementWorkItemsCount(); - } - } - } - catch(ThreadAbortException tae) - { + } + + // Notify that the work item has been completed. + // WorkItemsGroup may enqueue their next work item. + workItem.FireWorkItemCompleted(); + + // Decrement the number of work items here so the idle + // ManualResetEvent won't fluctuate. + DecrementWorkItemsCount(); + } + } + } + catch(ThreadAbortException tae) + { tae.GetHashCode(); // Handle the abort exception gracfully. #if !(_WINDOWS_CE) && !(_SILVERLIGHT) && !(WINDOWS_PHONE) - Thread.ResetAbort(); + Thread.ResetAbort(); #endif } - catch(Exception e) - { - Debug.Assert(null != e); - } - finally - { - InformCompleted(); + catch(Exception e) + { + Debug.Assert(null != e); + } + finally + { + InformCompleted(); FireOnThreadTermination(); - } - } + } + } - private void ExecuteWorkItem(WorkItem workItem) - { - _windowsPCs.SampleWorkItemsWaitTime(workItem.WaitingTime); + private void ExecuteWorkItem(WorkItem workItem) + { + _windowsPCs.SampleWorkItemsWaitTime(workItem.WaitingTime); _localPCs.SampleWorkItemsWaitTime(workItem.WaitingTime); - try - { - workItem.Execute(); - } - finally - { - _windowsPCs.SampleWorkItemsProcessTime(workItem.ProcessTime); + try + { + workItem.Execute(); + } + finally + { + _windowsPCs.SampleWorkItemsProcessTime(workItem.ProcessTime); _localPCs.SampleWorkItemsProcessTime(workItem.ProcessTime); - } - } + } + } - #endregion + #endregion - #region Public Methods + #region Public Methods - private void ValidateWaitForIdle() - { + private void ValidateWaitForIdle() + { if (null != CurrentThreadEntry && CurrentThreadEntry.AssociatedSmartThreadPool == this) - { - throw new NotSupportedException( - "WaitForIdle cannot be called from a thread on its SmartThreadPool, it causes a deadlock"); - } - } - - internal static void ValidateWorkItemsGroupWaitForIdle(IWorkItemsGroup workItemsGroup) - { + { + throw new NotSupportedException( + "WaitForIdle cannot be called from a thread on its SmartThreadPool, it causes a deadlock"); + } + } + + internal static void ValidateWorkItemsGroupWaitForIdle(IWorkItemsGroup workItemsGroup) + { if (null == CurrentThreadEntry) { return; @@ -910,334 +911,334 @@ namespace Amib.Threading WorkItem workItem = CurrentThreadEntry.CurrentWorkItem; ValidateWorkItemsGroupWaitForIdleImpl(workItemsGroup, workItem); - if ((null != workItemsGroup) && + if ((null != workItemsGroup) && (null != workItem) && CurrentThreadEntry.CurrentWorkItem.WasQueuedBy(workItemsGroup)) - { - throw new NotSupportedException("WaitForIdle cannot be called from a thread on its SmartThreadPool, it causes a deadlock"); - } - } - - [MethodImpl(MethodImplOptions.NoInlining)] - private static void ValidateWorkItemsGroupWaitForIdleImpl(IWorkItemsGroup workItemsGroup, WorkItem workItem) - { - if ((null != workItemsGroup) && - (null != workItem) && - workItem.WasQueuedBy(workItemsGroup)) - { - throw new NotSupportedException("WaitForIdle cannot be called from a thread on its SmartThreadPool, it causes a deadlock"); - } - } - - /// - /// Force the SmartThreadPool to shutdown - /// - public void Shutdown() - { - Shutdown(true, 0); - } + { + throw new NotSupportedException("WaitForIdle cannot be called from a thread on its SmartThreadPool, it causes a deadlock"); + } + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static void ValidateWorkItemsGroupWaitForIdleImpl(IWorkItemsGroup workItemsGroup, WorkItem workItem) + { + if ((null != workItemsGroup) && + (null != workItem) && + workItem.WasQueuedBy(workItemsGroup)) + { + throw new NotSupportedException("WaitForIdle cannot be called from a thread on its SmartThreadPool, it causes a deadlock"); + } + } + + /// + /// Force the SmartThreadPool to shutdown + /// + public void Shutdown() + { + Shutdown(true, 0); + } /// /// Force the SmartThreadPool to shutdown with timeout /// public void Shutdown(bool forceAbort, TimeSpan timeout) - { - Shutdown(forceAbort, (int)timeout.TotalMilliseconds); - } + { + Shutdown(forceAbort, (int)timeout.TotalMilliseconds); + } - /// - /// Empties the queue of work items and abort the threads in the pool. - /// - public void Shutdown(bool forceAbort, int millisecondsTimeout) - { - ValidateNotDisposed(); + /// + /// Empties the queue of work items and abort the threads in the pool. + /// + public void Shutdown(bool forceAbort, int millisecondsTimeout) + { + ValidateNotDisposed(); - ISTPInstancePerformanceCounters pcs = _windowsPCs; + ISTPInstancePerformanceCounters pcs = _windowsPCs; - if (NullSTPInstancePerformanceCounters.Instance != _windowsPCs) - { - // Set the _pcs to "null" to stop updating the performance - // counters - _windowsPCs = NullSTPInstancePerformanceCounters.Instance; + if (NullSTPInstancePerformanceCounters.Instance != _windowsPCs) + { + // Set the _pcs to "null" to stop updating the performance + // counters + _windowsPCs = NullSTPInstancePerformanceCounters.Instance; pcs.Dispose(); - } + } - Thread [] threads; - lock(_workerThreads.SyncRoot) - { - // Shutdown the work items queue - _workItemsQueue.Dispose(); + Thread [] threads; + lock(_workerThreads.SyncRoot) + { + // Shutdown the work items queue + _workItemsQueue.Dispose(); - // Signal the threads to exit - _shutdown = true; - _shuttingDownEvent.Set(); + // Signal the threads to exit + _shutdown = true; + _shuttingDownEvent.Set(); - // Make a copy of the threads' references in the pool - threads = new Thread [_workerThreads.Count]; - _workerThreads.Keys.CopyTo(threads, 0); - } + // Make a copy of the threads' references in the pool + threads = new Thread [_workerThreads.Count]; + _workerThreads.Keys.CopyTo(threads, 0); + } - int millisecondsLeft = millisecondsTimeout; + int millisecondsLeft = millisecondsTimeout; Stopwatch stopwatch = Stopwatch.StartNew(); //DateTime start = DateTime.UtcNow; - bool waitInfinitely = (Timeout.Infinite == millisecondsTimeout); - bool timeout = false; - - // Each iteration we update the time left for the timeout. - foreach(Thread thread in threads) - { - // Join don't work with negative numbers - if (!waitInfinitely && (millisecondsLeft < 0)) - { - timeout = true; - break; - } - - // Wait for the thread to terminate - bool success = thread.Join(millisecondsLeft); - if(!success) - { - timeout = true; - break; - } - - if(!waitInfinitely) - { - // Update the time left to wait + bool waitInfinitely = (Timeout.Infinite == millisecondsTimeout); + bool timeout = false; + + // Each iteration we update the time left for the timeout. + foreach(Thread thread in threads) + { + // Join don't work with negative numbers + if (!waitInfinitely && (millisecondsLeft < 0)) + { + timeout = true; + break; + } + + // Wait for the thread to terminate + bool success = thread.Join(millisecondsLeft); + if(!success) + { + timeout = true; + break; + } + + if(!waitInfinitely) + { + // Update the time left to wait //TimeSpan ts = DateTime.UtcNow - start; millisecondsLeft = millisecondsTimeout - (int)stopwatch.ElapsedMilliseconds; - } - } - - if (timeout && forceAbort) - { - // Abort the threads in the pool - foreach(Thread thread in threads) - { - - if ((thread != null) + } + } + + if (timeout && forceAbort) + { + // Abort the threads in the pool + foreach(Thread thread in threads) + { + + if ((thread != null) #if !(_WINDOWS_CE) && thread.IsAlive -#endif +#endif ) - { - try - { + { + try + { thread.Abort(); // Shutdown - } - catch(SecurityException e) - { + } + catch(SecurityException e) + { e.GetHashCode(); - } - catch(ThreadStateException ex) - { + } + catch(ThreadStateException ex) + { ex.GetHashCode(); - // In case the thread has been terminated - // after the check if it is alive. - } - } - } - } - } - - /// - /// Wait for all work items to complete - /// + // In case the thread has been terminated + // after the check if it is alive. + } + } + } + } + } + + /// + /// Wait for all work items to complete + /// /// Array of work item result objects - /// - /// true when every work item in workItemResults has completed; otherwise false. - /// - public static bool WaitAll( - IWaitableResult [] waitableResults) - { + /// + /// true when every work item in workItemResults has completed; otherwise false. + /// + public static bool WaitAll( + IWaitableResult [] waitableResults) + { return WaitAll(waitableResults, Timeout.Infinite, true); - } + } - /// - /// Wait for all work items to complete - /// + /// + /// Wait for all work items to complete + /// /// Array of work item result objects - /// The number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely. - /// - /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. - /// - /// - /// true when every work item in workItemResults has completed; otherwise false. - /// - public static bool WaitAll( - IWaitableResult [] waitableResults, - TimeSpan timeout, - bool exitContext) - { + /// The number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely. + /// + /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. + /// + /// + /// true when every work item in workItemResults has completed; otherwise false. + /// + public static bool WaitAll( + IWaitableResult [] waitableResults, + TimeSpan timeout, + bool exitContext) + { return WaitAll(waitableResults, (int)timeout.TotalMilliseconds, exitContext); - } + } - /// - /// Wait for all work items to complete - /// + /// + /// Wait for all work items to complete + /// /// Array of work item result objects - /// The number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely. - /// - /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. - /// - /// A cancel wait handle to interrupt the wait if needed - /// - /// true when every work item in workItemResults has completed; otherwise false. - /// - public static bool WaitAll( - IWaitableResult[] waitableResults, - TimeSpan timeout, - bool exitContext, - WaitHandle cancelWaitHandle) - { + /// The number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely. + /// + /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. + /// + /// A cancel wait handle to interrupt the wait if needed + /// + /// true when every work item in workItemResults has completed; otherwise false. + /// + public static bool WaitAll( + IWaitableResult[] waitableResults, + TimeSpan timeout, + bool exitContext, + WaitHandle cancelWaitHandle) + { return WaitAll(waitableResults, (int)timeout.TotalMilliseconds, exitContext, cancelWaitHandle); - } + } - /// - /// Wait for all work items to complete - /// + /// + /// Wait for all work items to complete + /// /// Array of work item result objects - /// The number of milliseconds to wait, or Timeout.Infinite (-1) to wait indefinitely. - /// - /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. - /// - /// - /// true when every work item in workItemResults has completed; otherwise false. - /// - public static bool WaitAll( - IWaitableResult [] waitableResults, - int millisecondsTimeout, - bool exitContext) - { + /// The number of milliseconds to wait, or Timeout.Infinite (-1) to wait indefinitely. + /// + /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. + /// + /// + /// true when every work item in workItemResults has completed; otherwise false. + /// + public static bool WaitAll( + IWaitableResult [] waitableResults, + int millisecondsTimeout, + bool exitContext) + { return WorkItem.WaitAll(waitableResults, millisecondsTimeout, exitContext, null); - } + } - /// - /// Wait for all work items to complete - /// + /// + /// Wait for all work items to complete + /// /// Array of work item result objects - /// The number of milliseconds to wait, or Timeout.Infinite (-1) to wait indefinitely. - /// - /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. - /// - /// A cancel wait handle to interrupt the wait if needed - /// - /// true when every work item in workItemResults has completed; otherwise false. - /// - public static bool WaitAll( - IWaitableResult[] waitableResults, - int millisecondsTimeout, - bool exitContext, - WaitHandle cancelWaitHandle) - { + /// The number of milliseconds to wait, or Timeout.Infinite (-1) to wait indefinitely. + /// + /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. + /// + /// A cancel wait handle to interrupt the wait if needed + /// + /// true when every work item in workItemResults has completed; otherwise false. + /// + public static bool WaitAll( + IWaitableResult[] waitableResults, + int millisecondsTimeout, + bool exitContext, + WaitHandle cancelWaitHandle) + { return WorkItem.WaitAll(waitableResults, millisecondsTimeout, exitContext, cancelWaitHandle); - } + } - /// - /// Waits for any of the work items in the specified array to complete, cancel, or timeout - /// + /// + /// Waits for any of the work items in the specified array to complete, cancel, or timeout + /// /// Array of work item result objects - /// - /// The array index of the work item result that satisfied the wait, or WaitTimeout if any of the work items has been canceled. - /// - public static int WaitAny( - IWaitableResult [] waitableResults) - { + /// + /// The array index of the work item result that satisfied the wait, or WaitTimeout if any of the work items has been canceled. + /// + public static int WaitAny( + IWaitableResult [] waitableResults) + { return WaitAny(waitableResults, Timeout.Infinite, true); - } + } - /// - /// Waits for any of the work items in the specified array to complete, cancel, or timeout - /// + /// + /// Waits for any of the work items in the specified array to complete, cancel, or timeout + /// /// Array of work item result objects - /// The number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely. - /// - /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. - /// - /// - /// The array index of the work item result that satisfied the wait, or WaitTimeout if no work item result satisfied the wait and a time interval equivalent to millisecondsTimeout has passed or the work item has been canceled. - /// - public static int WaitAny( + /// The number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely. + /// + /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. + /// + /// + /// The array index of the work item result that satisfied the wait, or WaitTimeout if no work item result satisfied the wait and a time interval equivalent to millisecondsTimeout has passed or the work item has been canceled. + /// + public static int WaitAny( IWaitableResult[] waitableResults, - TimeSpan timeout, - bool exitContext) - { + TimeSpan timeout, + bool exitContext) + { return WaitAny(waitableResults, (int)timeout.TotalMilliseconds, exitContext); - } + } - /// - /// Waits for any of the work items in the specified array to complete, cancel, or timeout - /// + /// + /// Waits for any of the work items in the specified array to complete, cancel, or timeout + /// /// Array of work item result objects - /// The number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely. - /// - /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. - /// - /// A cancel wait handle to interrupt the wait if needed - /// - /// The array index of the work item result that satisfied the wait, or WaitTimeout if no work item result satisfied the wait and a time interval equivalent to millisecondsTimeout has passed or the work item has been canceled. - /// - public static int WaitAny( - IWaitableResult [] waitableResults, - TimeSpan timeout, - bool exitContext, - WaitHandle cancelWaitHandle) - { + /// The number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely. + /// + /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. + /// + /// A cancel wait handle to interrupt the wait if needed + /// + /// The array index of the work item result that satisfied the wait, or WaitTimeout if no work item result satisfied the wait and a time interval equivalent to millisecondsTimeout has passed or the work item has been canceled. + /// + public static int WaitAny( + IWaitableResult [] waitableResults, + TimeSpan timeout, + bool exitContext, + WaitHandle cancelWaitHandle) + { return WaitAny(waitableResults, (int)timeout.TotalMilliseconds, exitContext, cancelWaitHandle); - } + } - /// - /// Waits for any of the work items in the specified array to complete, cancel, or timeout - /// + /// + /// Waits for any of the work items in the specified array to complete, cancel, or timeout + /// /// Array of work item result objects - /// The number of milliseconds to wait, or Timeout.Infinite (-1) to wait indefinitely. - /// - /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. - /// - /// - /// The array index of the work item result that satisfied the wait, or WaitTimeout if no work item result satisfied the wait and a time interval equivalent to millisecondsTimeout has passed or the work item has been canceled. - /// - public static int WaitAny( - IWaitableResult [] waitableResults, - int millisecondsTimeout, - bool exitContext) - { + /// The number of milliseconds to wait, or Timeout.Infinite (-1) to wait indefinitely. + /// + /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. + /// + /// + /// The array index of the work item result that satisfied the wait, or WaitTimeout if no work item result satisfied the wait and a time interval equivalent to millisecondsTimeout has passed or the work item has been canceled. + /// + public static int WaitAny( + IWaitableResult [] waitableResults, + int millisecondsTimeout, + bool exitContext) + { return WorkItem.WaitAny(waitableResults, millisecondsTimeout, exitContext, null); - } + } - /// - /// Waits for any of the work items in the specified array to complete, cancel, or timeout - /// + /// + /// Waits for any of the work items in the specified array to complete, cancel, or timeout + /// /// Array of work item result objects - /// The number of milliseconds to wait, or Timeout.Infinite (-1) to wait indefinitely. - /// - /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. - /// - /// A cancel wait handle to interrupt the wait if needed - /// - /// The array index of the work item result that satisfied the wait, or WaitTimeout if no work item result satisfied the wait and a time interval equivalent to millisecondsTimeout has passed or the work item has been canceled. - /// - public static int WaitAny( - IWaitableResult [] waitableResults, - int millisecondsTimeout, - bool exitContext, - WaitHandle cancelWaitHandle) - { + /// The number of milliseconds to wait, or Timeout.Infinite (-1) to wait indefinitely. + /// + /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. + /// + /// A cancel wait handle to interrupt the wait if needed + /// + /// The array index of the work item result that satisfied the wait, or WaitTimeout if no work item result satisfied the wait and a time interval equivalent to millisecondsTimeout has passed or the work item has been canceled. + /// + public static int WaitAny( + IWaitableResult [] waitableResults, + int millisecondsTimeout, + bool exitContext, + WaitHandle cancelWaitHandle) + { return WorkItem.WaitAny(waitableResults, millisecondsTimeout, exitContext, cancelWaitHandle); - } + } /// /// Creates a new WorkItemsGroup. /// /// The number of work items that can be run concurrently /// A reference to the WorkItemsGroup - public IWorkItemsGroup CreateWorkItemsGroup(int concurrency) - { + public IWorkItemsGroup CreateWorkItemsGroup(int concurrency) + { IWorkItemsGroup workItemsGroup = new WorkItemsGroup(this, concurrency, _stpStartInfo); - return workItemsGroup; - } + return workItemsGroup; + } /// /// Creates a new WorkItemsGroup. @@ -1245,11 +1246,11 @@ namespace Amib.Threading /// The number of work items that can be run concurrently /// A WorkItemsGroup configuration that overrides the default behavior /// A reference to the WorkItemsGroup - public IWorkItemsGroup CreateWorkItemsGroup(int concurrency, WIGStartInfo wigStartInfo) - { - IWorkItemsGroup workItemsGroup = new WorkItemsGroup(this, concurrency, wigStartInfo); - return workItemsGroup; - } + public IWorkItemsGroup CreateWorkItemsGroup(int concurrency, WIGStartInfo wigStartInfo) + { + IWorkItemsGroup workItemsGroup = new WorkItemsGroup(this, concurrency, wigStartInfo); + return workItemsGroup; + } #region Fire Thread's Events @@ -1330,22 +1331,22 @@ namespace Amib.Threading } } - - #endregion - #region Properties + #endregion - /// - /// Get/Set the lower limit of threads in the pool. - /// - public int MinThreads - { - get - { - ValidateNotDisposed(); - return _stpStartInfo.MinWorkerThreads; - } + #region Properties + + /// + /// Get/Set the lower limit of threads in the pool. + /// + public int MinThreads + { + get + { + ValidateNotDisposed(); + return _stpStartInfo.MinWorkerThreads; + } set { Debug.Assert(value >= 0); @@ -1357,21 +1358,21 @@ namespace Amib.Threading _stpStartInfo.MinWorkerThreads = value; StartOptimalNumberOfThreads(); } - } - - /// - /// Get/Set the upper limit of threads in the pool. - /// - public int MaxThreads - { - get - { - ValidateNotDisposed(); - return _stpStartInfo.MaxWorkerThreads; - } - - set - { + } + + /// + /// Get/Set the upper limit of threads in the pool. + /// + public int MaxThreads + { + get + { + ValidateNotDisposed(); + return _stpStartInfo.MaxWorkerThreads; + } + + set + { Debug.Assert(value > 0); Debug.Assert(value >= _stpStartInfo.MinWorkerThreads); if (_stpStartInfo.MinWorkerThreads > value) @@ -1380,32 +1381,32 @@ namespace Amib.Threading } _stpStartInfo.MaxWorkerThreads = value; StartOptimalNumberOfThreads(); - } - } - /// - /// Get the number of threads in the thread pool. - /// Should be between the lower and the upper limits. - /// - public int ActiveThreads - { - get - { - ValidateNotDisposed(); - return _workerThreads.Count; - } - } - - /// - /// Get the number of busy (not idle) threads in the thread pool. - /// - public int InUseThreads - { - get - { - ValidateNotDisposed(); - return _inUseWorkerThreads; - } - } + } + } + /// + /// Get the number of threads in the thread pool. + /// Should be between the lower and the upper limits. + /// + public int ActiveThreads + { + get + { + ValidateNotDisposed(); + return _workerThreads.Count; + } + } + + /// + /// Get the number of busy (not idle) threads in the thread pool. + /// + public int InUseThreads + { + get + { + ValidateNotDisposed(); + return _inUseWorkerThreads; + } + } /// /// Returns true if the current running work item has been cancelled. @@ -1419,8 +1420,8 @@ namespace Amib.Threading { return CurrentThreadEntry.CurrentWorkItem.IsCanceled; } - } - + } + /// /// Checks if the work item has been cancelled, and if yes then abort the thread. /// Can be used with Cancel and timeout @@ -1438,16 +1439,16 @@ namespace Amib.Threading /// public STPStartInfo STPStartInfo { - get + get { - return _stpStartInfo.AsReadOnly(); + return _stpStartInfo.AsReadOnly(); } } - public bool IsShuttingdown - { + public bool IsShuttingdown + { get { return _shutdown; } - } + } /// /// Return the local calculated performance counters @@ -1477,13 +1478,16 @@ namespace Amib.Threading _shuttingDownEvent = null; } _workerThreads.Clear(); - + if (null != _isIdleWaitHandle) { _isIdleWaitHandle.Close(); _isIdleWaitHandle = null; } + if (_stpStartInfo.EnableLocalPerformanceCounters) + _localPCs.Dispose(); + _isDisposed = true; } } @@ -1503,22 +1507,22 @@ namespace Amib.Threading /// Get/Set the maximum number of work items that execute cocurrency on the thread pool /// public override int Concurrency - { - get { return MaxThreads; } - set { MaxThreads = value; } - } - - /// - /// Get the number of work items in the queue. - /// - public override int WaitingCallbacks - { - get - { - ValidateNotDisposed(); - return _workItemsQueue.Count; - } - } + { + get { return MaxThreads; } + set { MaxThreads = value; } + } + + /// + /// Get the number of work items in the queue. + /// + public override int WaitingCallbacks + { + get + { + ValidateNotDisposed(); + return _workItemsQueue.Count; + } + } /// /// Get an array with all the state objects of the currently running items. @@ -1538,7 +1542,7 @@ namespace Amib.Threading get { return _stpStartInfo.AsReadOnly(); } } - /// + /// /// Start the thread pool if it was started suspended. /// If it is already running, this method is ignored. /// @@ -1589,7 +1593,7 @@ namespace Amib.Threading } } - /// + /// /// Wait for the thread pool to be idle /// public override bool WaitForIdle(int millisecondsTimeout) @@ -1618,9 +1622,9 @@ namespace Amib.Threading } } - internal override void PreQueueWorkItem() + internal override void PreQueueWorkItem() { - ValidateNotDisposed(); + ValidateNotDisposed(); } #endregion @@ -1672,7 +1676,7 @@ namespace Amib.Threading ManualResetEvent anActionCompleted = new ManualResetEvent(false); ChoiceIndex choiceIndex = new ChoiceIndex(); - + int i = 0; foreach (Action action in actions) { @@ -1681,8 +1685,9 @@ namespace Amib.Threading workItemsGroup.QueueWorkItem(() => { act(); Interlocked.CompareExchange(ref choiceIndex._index, value, -1); anActionCompleted.Set(); }); ++i; } - workItemsGroup.Start(); + workItemsGroup.Start(); anActionCompleted.WaitOne(); + anActionCompleted.Dispose(); return choiceIndex._index; } @@ -1693,7 +1698,7 @@ namespace Amib.Threading /// /// Actions to execute public int Choice(params Action[] actions) - { + { return Choice((IEnumerable)actions); } @@ -1727,6 +1732,6 @@ namespace Amib.Threading Pipe(pipeState, (IEnumerable>)actions); } #endregion - } - #endregion + } + #endregion } diff --git a/ThirdParty/SmartThreadPool/WIGStartInfo.cs b/ThirdParty/SmartThreadPool/WIGStartInfo.cs index 8af195b..756ac1f 100644 --- a/ThirdParty/SmartThreadPool/WIGStartInfo.cs +++ b/ThirdParty/SmartThreadPool/WIGStartInfo.cs @@ -2,11 +2,11 @@ using System; namespace Amib.Threading { - /// - /// Summary description for WIGStartInfo. - /// - public class WIGStartInfo - { + /// + /// Summary description for WIGStartInfo. + /// + public class WIGStartInfo + { private bool _useCallerCallContext; private bool _useCallerHttpContext; private bool _disposeOfStateObjects; @@ -18,10 +18,10 @@ namespace Amib.Threading protected bool _readOnly; - public WIGStartInfo() + public WIGStartInfo() { - _fillStateWithArgs = SmartThreadPool.DefaultFillStateWithArgs; - _workItemPriority = SmartThreadPool.DefaultWorkItemPriority; + _fillStateWithArgs = SmartThreadPool.DefaultFillStateWithArgs; + _workItemPriority = SmartThreadPool.DefaultWorkItemPriority; _startSuspended = SmartThreadPool.DefaultStartSuspended; _postExecuteWorkItemCallback = SmartThreadPool.DefaultPostExecuteWorkItemCallback; _callToPostExecute = SmartThreadPool.DefaultCallToPostExecute; @@ -30,7 +30,7 @@ namespace Amib.Threading _useCallerCallContext = SmartThreadPool.DefaultUseCallerCallContext; } - public WIGStartInfo(WIGStartInfo wigStartInfo) + public WIGStartInfo(WIGStartInfo wigStartInfo) { _useCallerCallContext = wigStartInfo.UseCallerCallContext; _useCallerHttpContext = wigStartInfo.UseCallerHttpContext; @@ -50,122 +50,122 @@ namespace Amib.Threading } } - /// - /// Get/Set if to use the caller's security context - /// - public virtual bool UseCallerCallContext - { - get { return _useCallerCallContext; } - set - { - ThrowIfReadOnly(); - _useCallerCallContext = value; + /// + /// Get/Set if to use the caller's security context + /// + public virtual bool UseCallerCallContext + { + get { return _useCallerCallContext; } + set + { + ThrowIfReadOnly(); + _useCallerCallContext = value; } - } - - - /// - /// Get/Set if to use the caller's HTTP context - /// - public virtual bool UseCallerHttpContext - { - get { return _useCallerHttpContext; } - set - { - ThrowIfReadOnly(); - _useCallerHttpContext = value; + } + + + /// + /// Get/Set if to use the caller's HTTP context + /// + public virtual bool UseCallerHttpContext + { + get { return _useCallerHttpContext; } + set + { + ThrowIfReadOnly(); + _useCallerHttpContext = value; } - } - - - /// - /// Get/Set if to dispose of the state object of a work item - /// - public virtual bool DisposeOfStateObjects - { - get { return _disposeOfStateObjects; } - set - { - ThrowIfReadOnly(); - _disposeOfStateObjects = value; + } + + + /// + /// Get/Set if to dispose of the state object of a work item + /// + public virtual bool DisposeOfStateObjects + { + get { return _disposeOfStateObjects; } + set + { + ThrowIfReadOnly(); + _disposeOfStateObjects = value; } - } - - - /// - /// Get/Set the run the post execute options - /// - public virtual CallToPostExecute CallToPostExecute - { - get { return _callToPostExecute; } - set - { - ThrowIfReadOnly(); - _callToPostExecute = value; + } + + + /// + /// Get/Set the run the post execute options + /// + public virtual CallToPostExecute CallToPostExecute + { + get { return _callToPostExecute; } + set + { + ThrowIfReadOnly(); + _callToPostExecute = value; } - } - - - /// - /// Get/Set the default post execute callback - /// - public virtual PostExecuteWorkItemCallback PostExecuteWorkItemCallback - { - get { return _postExecuteWorkItemCallback; } - set - { - ThrowIfReadOnly(); - _postExecuteWorkItemCallback = value; + } + + + /// + /// Get/Set the default post execute callback + /// + public virtual PostExecuteWorkItemCallback PostExecuteWorkItemCallback + { + get { return _postExecuteWorkItemCallback; } + set + { + ThrowIfReadOnly(); + _postExecuteWorkItemCallback = value; } - } - - - /// - /// Get/Set if the work items execution should be suspended until the Start() - /// method is called. - /// - public virtual bool StartSuspended - { - get { return _startSuspended; } - set - { - ThrowIfReadOnly(); - _startSuspended = value; + } + + + /// + /// Get/Set if the work items execution should be suspended until the Start() + /// method is called. + /// + public virtual bool StartSuspended + { + get { return _startSuspended; } + set + { + ThrowIfReadOnly(); + _startSuspended = value; } - } + } - /// - /// Get/Set the default priority that a work item gets when it is enqueued - /// - public virtual WorkItemPriority WorkItemPriority - { - get { return _workItemPriority; } - set { _workItemPriority = value; } - } + /// + /// Get/Set the default priority that a work item gets when it is enqueued + /// + public virtual WorkItemPriority WorkItemPriority + { + get { return _workItemPriority; } + set { _workItemPriority = value; } + } - /// + /// /// Get/Set the if QueueWorkItem of Action<...>/Func<...> fill the - /// arguments as an object array into the state of the work item. - /// The arguments can be access later by IWorkItemResult.State. - /// - public virtual bool FillStateWithArgs - { - get { return _fillStateWithArgs; } - set - { - ThrowIfReadOnly(); - _fillStateWithArgs = value; + /// arguments as an object array into the state of the work item. + /// The arguments can be access later by IWorkItemResult.State. + /// + public virtual bool FillStateWithArgs + { + get { return _fillStateWithArgs; } + set + { + ThrowIfReadOnly(); + _fillStateWithArgs = value; } - } + } - /// + /// /// Get a readonly version of this WIGStartInfo /// /// Returns a readonly reference to this WIGStartInfoRO public WIGStartInfo AsReadOnly() - { + { return new WIGStartInfo(this) { _readOnly = true }; - } + } } } diff --git a/ThirdParty/SmartThreadPool/WorkItem.cs b/ThirdParty/SmartThreadPool/WorkItem.cs index 185f10c..edb8ac0 100644 --- a/ThirdParty/SmartThreadPool/WorkItem.cs +++ b/ThirdParty/SmartThreadPool/WorkItem.cs @@ -88,7 +88,7 @@ namespace Amib.Threading.Internal private ManualResetEvent _workItemCompleted; /// - /// A reference count to the _workItemCompleted. + /// A reference count to the _workItemCompleted. /// When it reaches to zero _workItemCompleted is Closed /// private int _workItemCompletedRefCount; @@ -114,13 +114,13 @@ namespace Amib.Threading.Internal private event WorkItemStateCallback _workItemCompletedEvent; /// - /// A reference to an object that indicates whatever the + /// A reference to an object that indicates whatever the /// WorkItemsGroup has been canceled /// private CanceledWorkItemsGroup _canceledWorkItemsGroup = CanceledWorkItemsGroup.NotCanceledWorkItemsGroup; /// - /// A reference to an object that indicates whatever the + /// A reference to an object that indicates whatever the /// SmartThreadPool has been canceled /// private CanceledWorkItemsGroup _canceledSmartThreadPool = CanceledWorkItemsGroup.NotCanceledWorkItemsGroup; @@ -197,7 +197,7 @@ namespace Amib.Threading.Internal /// The WorkItemInfo of te workitem /// Callback delegate for the callback. /// State with which to call the callback delegate. - /// + /// /// We assume that the WorkItem object is created within the thread /// that meant to run the callback public WorkItem( @@ -402,7 +402,7 @@ namespace Amib.Threading.Internal { tae.GetHashCode(); // Check if the work item was cancelled - // If we got a ThreadAbortException and the STP is not shutting down, it means the + // If we got a ThreadAbortException and the STP is not shutting down, it means the // work items was cancelled. if (!SmartThreadPool.CurrentThreadEntry.AssociatedSmartThreadPool.IsShuttingdown) { @@ -471,7 +471,7 @@ namespace Amib.Threading.Internal /// Array of work item result objects /// The number of milliseconds to wait, or Timeout.Infinite (-1) to wait indefinitely. /// - /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. + /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. /// /// A cancel wait handle to interrupt the wait if needed /// @@ -553,7 +553,7 @@ namespace Amib.Threading.Internal /// Array of work item result objects /// The number of milliseconds to wait, or Timeout.Infinite (-1) to wait indefinitely. /// - /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. + /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. /// /// A cancel wait handle to interrupt the wait if needed /// @@ -734,7 +734,7 @@ namespace Amib.Threading.Internal // so it already signaled its completion. //signalComplete = true; } - } + } success = true; break; case WorkItemState.Completed: @@ -856,7 +856,7 @@ namespace Amib.Threading.Internal { case 0: // The work item signaled - // Note that the signal could be also as a result of canceling the + // Note that the signal could be also as a result of canceling the // work item (not the get result) break; case 1: @@ -884,7 +884,7 @@ namespace Amib.Threading.Internal } /// - /// A wait handle to wait for completion, cancel, or timeout + /// A wait handle to wait for completion, cancel, or timeout /// private WaitHandle GetWaitHandle() { diff --git a/ThirdParty/SmartThreadPool/WorkItemFactory.cs b/ThirdParty/SmartThreadPool/WorkItemFactory.cs index 16ccd81..471eb20 100644 --- a/ThirdParty/SmartThreadPool/WorkItemFactory.cs +++ b/ThirdParty/SmartThreadPool/WorkItemFactory.cs @@ -2,146 +2,146 @@ using System; namespace Amib.Threading.Internal { - #region WorkItemFactory class - - public class WorkItemFactory - { - /// - /// Create a new work item - /// - /// The WorkItemsGroup of this workitem - /// Work item group start information - /// A callback to execute - /// Returns a work item - public static WorkItem CreateWorkItem( - IWorkItemsGroup workItemsGroup, - WIGStartInfo wigStartInfo, - WorkItemCallback callback) - { - return CreateWorkItem(workItemsGroup, wigStartInfo, callback, null); - } - - /// - /// Create a new work item - /// + #region WorkItemFactory class + + public class WorkItemFactory + { + /// + /// Create a new work item + /// /// The WorkItemsGroup of this workitem /// Work item group start information - /// A callback to execute - /// The priority of the work item - /// Returns a work item - public static WorkItem CreateWorkItem( - IWorkItemsGroup workItemsGroup, - WIGStartInfo wigStartInfo, - WorkItemCallback callback, - WorkItemPriority workItemPriority) - { - return CreateWorkItem(workItemsGroup, wigStartInfo, callback, null, workItemPriority); - } - - /// - /// Create a new work item - /// + /// A callback to execute + /// Returns a work item + public static WorkItem CreateWorkItem( + IWorkItemsGroup workItemsGroup, + WIGStartInfo wigStartInfo, + WorkItemCallback callback) + { + return CreateWorkItem(workItemsGroup, wigStartInfo, callback, null); + } + + /// + /// Create a new work item + /// /// The WorkItemsGroup of this workitem /// Work item group start information - /// Work item info - /// A callback to execute - /// Returns a work item - public static WorkItem CreateWorkItem( - IWorkItemsGroup workItemsGroup, - WIGStartInfo wigStartInfo, - WorkItemInfo workItemInfo, - WorkItemCallback callback) - { - return CreateWorkItem( - workItemsGroup, - wigStartInfo, - workItemInfo, - callback, - null); - } - - /// - /// Create a new work item - /// + /// A callback to execute + /// The priority of the work item + /// Returns a work item + public static WorkItem CreateWorkItem( + IWorkItemsGroup workItemsGroup, + WIGStartInfo wigStartInfo, + WorkItemCallback callback, + WorkItemPriority workItemPriority) + { + return CreateWorkItem(workItemsGroup, wigStartInfo, callback, null, workItemPriority); + } + + /// + /// Create a new work item + /// /// The WorkItemsGroup of this workitem /// Work item group start information - /// A callback to execute - /// - /// The context object of the work item. Used for passing arguments to the work item. - /// - /// Returns a work item - public static WorkItem CreateWorkItem( - IWorkItemsGroup workItemsGroup, - WIGStartInfo wigStartInfo, - WorkItemCallback callback, - object state) - { - ValidateCallback(callback); - - WorkItemInfo workItemInfo = new WorkItemInfo(); - workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext; - workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext; - workItemInfo.PostExecuteWorkItemCallback = wigStartInfo.PostExecuteWorkItemCallback; - workItemInfo.CallToPostExecute = wigStartInfo.CallToPostExecute; - workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; + /// Work item info + /// A callback to execute + /// Returns a work item + public static WorkItem CreateWorkItem( + IWorkItemsGroup workItemsGroup, + WIGStartInfo wigStartInfo, + WorkItemInfo workItemInfo, + WorkItemCallback callback) + { + return CreateWorkItem( + workItemsGroup, + wigStartInfo, + workItemInfo, + callback, + null); + } + + /// + /// Create a new work item + /// + /// The WorkItemsGroup of this workitem + /// Work item group start information + /// A callback to execute + /// + /// The context object of the work item. Used for passing arguments to the work item. + /// + /// Returns a work item + public static WorkItem CreateWorkItem( + IWorkItemsGroup workItemsGroup, + WIGStartInfo wigStartInfo, + WorkItemCallback callback, + object state) + { + ValidateCallback(callback); + + WorkItemInfo workItemInfo = new WorkItemInfo(); + workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext; + workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext; + workItemInfo.PostExecuteWorkItemCallback = wigStartInfo.PostExecuteWorkItemCallback; + workItemInfo.CallToPostExecute = wigStartInfo.CallToPostExecute; + workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; workItemInfo.WorkItemPriority = wigStartInfo.WorkItemPriority; - WorkItem workItem = new WorkItem( - workItemsGroup, - workItemInfo, - callback, - state); - return workItem; - } - - /// - /// Create a new work item - /// - /// The work items group - /// Work item group start information - /// A callback to execute - /// - /// The context object of the work item. Used for passing arguments to the work item. - /// - /// The work item priority - /// Returns a work item - public static WorkItem CreateWorkItem( - IWorkItemsGroup workItemsGroup, - WIGStartInfo wigStartInfo, - WorkItemCallback callback, - object state, - WorkItemPriority workItemPriority) - { - ValidateCallback(callback); - - WorkItemInfo workItemInfo = new WorkItemInfo(); - workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext; - workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext; - workItemInfo.PostExecuteWorkItemCallback = wigStartInfo.PostExecuteWorkItemCallback; - workItemInfo.CallToPostExecute = wigStartInfo.CallToPostExecute; - workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; - workItemInfo.WorkItemPriority = workItemPriority; - - WorkItem workItem = new WorkItem( - workItemsGroup, - workItemInfo, - callback, - state); - - return workItem; - } - - /// - /// Create a new work item - /// + WorkItem workItem = new WorkItem( + workItemsGroup, + workItemInfo, + callback, + state); + return workItem; + } + + /// + /// Create a new work item + /// /// The work items group - /// Work item group start information - /// Work item information - /// A callback to execute - /// - /// The context object of the work item. Used for passing arguments to the work item. - /// - /// Returns a work item + /// Work item group start information + /// A callback to execute + /// + /// The context object of the work item. Used for passing arguments to the work item. + /// + /// The work item priority + /// Returns a work item + public static WorkItem CreateWorkItem( + IWorkItemsGroup workItemsGroup, + WIGStartInfo wigStartInfo, + WorkItemCallback callback, + object state, + WorkItemPriority workItemPriority) + { + ValidateCallback(callback); + + WorkItemInfo workItemInfo = new WorkItemInfo(); + workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext; + workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext; + workItemInfo.PostExecuteWorkItemCallback = wigStartInfo.PostExecuteWorkItemCallback; + workItemInfo.CallToPostExecute = wigStartInfo.CallToPostExecute; + workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; + workItemInfo.WorkItemPriority = workItemPriority; + + WorkItem workItem = new WorkItem( + workItemsGroup, + workItemInfo, + callback, + state); + + return workItem; + } + + /// + /// Create a new work item + /// + /// The work items group + /// Work item group start information + /// Work item information + /// A callback to execute + /// + /// The context object of the work item. Used for passing arguments to the work item. + /// + /// Returns a work item public static WorkItem CreateWorkItem( IWorkItemsGroup workItemsGroup, WIGStartInfo wigStartInfo, @@ -161,183 +161,183 @@ namespace Amib.Threading.Internal return workItem; } - /// - /// Create a new work item - /// + /// + /// Create a new work item + /// /// The work items group - /// Work item group start information - /// A callback to execute - /// - /// The context object of the work item. Used for passing arguments to the work item. - /// - /// - /// A delegate to call after the callback completion - /// - /// Returns a work item - public static WorkItem CreateWorkItem( - IWorkItemsGroup workItemsGroup, - WIGStartInfo wigStartInfo, - WorkItemCallback callback, - object state, - PostExecuteWorkItemCallback postExecuteWorkItemCallback) - { - ValidateCallback(callback); - ValidateCallback(postExecuteWorkItemCallback); - - WorkItemInfo workItemInfo = new WorkItemInfo(); - workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext; - workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext; - workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback; - workItemInfo.CallToPostExecute = wigStartInfo.CallToPostExecute; - workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; + /// Work item group start information + /// A callback to execute + /// + /// The context object of the work item. Used for passing arguments to the work item. + /// + /// + /// A delegate to call after the callback completion + /// + /// Returns a work item + public static WorkItem CreateWorkItem( + IWorkItemsGroup workItemsGroup, + WIGStartInfo wigStartInfo, + WorkItemCallback callback, + object state, + PostExecuteWorkItemCallback postExecuteWorkItemCallback) + { + ValidateCallback(callback); + ValidateCallback(postExecuteWorkItemCallback); + + WorkItemInfo workItemInfo = new WorkItemInfo(); + workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext; + workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext; + workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback; + workItemInfo.CallToPostExecute = wigStartInfo.CallToPostExecute; + workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; workItemInfo.WorkItemPriority = wigStartInfo.WorkItemPriority; - WorkItem workItem = new WorkItem( - workItemsGroup, - workItemInfo, - callback, - state); + WorkItem workItem = new WorkItem( + workItemsGroup, + workItemInfo, + callback, + state); - return workItem; - } + return workItem; + } - /// - /// Create a new work item - /// + /// + /// Create a new work item + /// /// The work items group - /// Work item group start information - /// A callback to execute - /// - /// The context object of the work item. Used for passing arguments to the work item. - /// - /// - /// A delegate to call after the callback completion - /// - /// The work item priority - /// Returns a work item - public static WorkItem CreateWorkItem( - IWorkItemsGroup workItemsGroup, - WIGStartInfo wigStartInfo, - WorkItemCallback callback, - object state, - PostExecuteWorkItemCallback postExecuteWorkItemCallback, - WorkItemPriority workItemPriority) - { - ValidateCallback(callback); - ValidateCallback(postExecuteWorkItemCallback); - - WorkItemInfo workItemInfo = new WorkItemInfo(); - workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext; - workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext; - workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback; - workItemInfo.CallToPostExecute = wigStartInfo.CallToPostExecute; - workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; - workItemInfo.WorkItemPriority = workItemPriority; - - WorkItem workItem = new WorkItem( - workItemsGroup, - workItemInfo, - callback, - state); - - return workItem; - } - - /// - /// Create a new work item - /// + /// Work item group start information + /// A callback to execute + /// + /// The context object of the work item. Used for passing arguments to the work item. + /// + /// + /// A delegate to call after the callback completion + /// + /// The work item priority + /// Returns a work item + public static WorkItem CreateWorkItem( + IWorkItemsGroup workItemsGroup, + WIGStartInfo wigStartInfo, + WorkItemCallback callback, + object state, + PostExecuteWorkItemCallback postExecuteWorkItemCallback, + WorkItemPriority workItemPriority) + { + ValidateCallback(callback); + ValidateCallback(postExecuteWorkItemCallback); + + WorkItemInfo workItemInfo = new WorkItemInfo(); + workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext; + workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext; + workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback; + workItemInfo.CallToPostExecute = wigStartInfo.CallToPostExecute; + workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; + workItemInfo.WorkItemPriority = workItemPriority; + + WorkItem workItem = new WorkItem( + workItemsGroup, + workItemInfo, + callback, + state); + + return workItem; + } + + /// + /// Create a new work item + /// /// The work items group - /// Work item group start information - /// A callback to execute - /// - /// The context object of the work item. Used for passing arguments to the work item. - /// - /// - /// A delegate to call after the callback completion - /// - /// Indicates on which cases to call to the post execute callback - /// Returns a work item - public static WorkItem CreateWorkItem( - IWorkItemsGroup workItemsGroup, - WIGStartInfo wigStartInfo, - WorkItemCallback callback, - object state, - PostExecuteWorkItemCallback postExecuteWorkItemCallback, - CallToPostExecute callToPostExecute) - { - ValidateCallback(callback); - ValidateCallback(postExecuteWorkItemCallback); - - WorkItemInfo workItemInfo = new WorkItemInfo(); - workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext; - workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext; - workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback; - workItemInfo.CallToPostExecute = callToPostExecute; - workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; + /// Work item group start information + /// A callback to execute + /// + /// The context object of the work item. Used for passing arguments to the work item. + /// + /// + /// A delegate to call after the callback completion + /// + /// Indicates on which cases to call to the post execute callback + /// Returns a work item + public static WorkItem CreateWorkItem( + IWorkItemsGroup workItemsGroup, + WIGStartInfo wigStartInfo, + WorkItemCallback callback, + object state, + PostExecuteWorkItemCallback postExecuteWorkItemCallback, + CallToPostExecute callToPostExecute) + { + ValidateCallback(callback); + ValidateCallback(postExecuteWorkItemCallback); + + WorkItemInfo workItemInfo = new WorkItemInfo(); + workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext; + workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext; + workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback; + workItemInfo.CallToPostExecute = callToPostExecute; + workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; workItemInfo.WorkItemPriority = wigStartInfo.WorkItemPriority; - WorkItem workItem = new WorkItem( - workItemsGroup, - workItemInfo, - callback, - state); + WorkItem workItem = new WorkItem( + workItemsGroup, + workItemInfo, + callback, + state); - return workItem; - } + return workItem; + } - /// - /// Create a new work item - /// + /// + /// Create a new work item + /// /// The work items group - /// Work item group start information - /// A callback to execute - /// - /// The context object of the work item. Used for passing arguments to the work item. - /// - /// - /// A delegate to call after the callback completion - /// - /// Indicates on which cases to call to the post execute callback - /// The work item priority - /// Returns a work item - public static WorkItem CreateWorkItem( - IWorkItemsGroup workItemsGroup, - WIGStartInfo wigStartInfo, - WorkItemCallback callback, - object state, - PostExecuteWorkItemCallback postExecuteWorkItemCallback, - CallToPostExecute callToPostExecute, - WorkItemPriority workItemPriority) - { - - ValidateCallback(callback); - ValidateCallback(postExecuteWorkItemCallback); - - WorkItemInfo workItemInfo = new WorkItemInfo(); - workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext; - workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext; - workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback; - workItemInfo.CallToPostExecute = callToPostExecute; - workItemInfo.WorkItemPriority = workItemPriority; - workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; - - WorkItem workItem = new WorkItem( - workItemsGroup, - workItemInfo, - callback, - state); - - return workItem; - } - - private static void ValidateCallback(Delegate callback) - { + /// Work item group start information + /// A callback to execute + /// + /// The context object of the work item. Used for passing arguments to the work item. + /// + /// + /// A delegate to call after the callback completion + /// + /// Indicates on which cases to call to the post execute callback + /// The work item priority + /// Returns a work item + public static WorkItem CreateWorkItem( + IWorkItemsGroup workItemsGroup, + WIGStartInfo wigStartInfo, + WorkItemCallback callback, + object state, + PostExecuteWorkItemCallback postExecuteWorkItemCallback, + CallToPostExecute callToPostExecute, + WorkItemPriority workItemPriority) + { + + ValidateCallback(callback); + ValidateCallback(postExecuteWorkItemCallback); + + WorkItemInfo workItemInfo = new WorkItemInfo(); + workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext; + workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext; + workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback; + workItemInfo.CallToPostExecute = callToPostExecute; + workItemInfo.WorkItemPriority = workItemPriority; + workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; + + WorkItem workItem = new WorkItem( + workItemsGroup, + workItemInfo, + callback, + state); + + return workItem; + } + + private static void ValidateCallback(Delegate callback) + { if (callback != null && callback.GetInvocationList().Length > 1) - { - throw new NotSupportedException("SmartThreadPool doesn't support delegates chains"); - } - } - } + { + throw new NotSupportedException("SmartThreadPool doesn't support delegates chains"); + } + } + } - #endregion + #endregion } diff --git a/ThirdParty/SmartThreadPool/WorkItemInfo.cs b/ThirdParty/SmartThreadPool/WorkItemInfo.cs index 0d7fc85..5be82a2 100644 --- a/ThirdParty/SmartThreadPool/WorkItemInfo.cs +++ b/ThirdParty/SmartThreadPool/WorkItemInfo.cs @@ -1,69 +1,69 @@ namespace Amib.Threading { - #region WorkItemInfo class + #region WorkItemInfo class - /// - /// Summary description for WorkItemInfo. - /// - public class WorkItemInfo - { - public WorkItemInfo() - { - UseCallerCallContext = SmartThreadPool.DefaultUseCallerCallContext; - UseCallerHttpContext = SmartThreadPool.DefaultUseCallerHttpContext; - DisposeOfStateObjects = SmartThreadPool.DefaultDisposeOfStateObjects; - CallToPostExecute = SmartThreadPool.DefaultCallToPostExecute; - PostExecuteWorkItemCallback = SmartThreadPool.DefaultPostExecuteWorkItemCallback; - WorkItemPriority = SmartThreadPool.DefaultWorkItemPriority; - } + /// + /// Summary description for WorkItemInfo. + /// + public class WorkItemInfo + { + public WorkItemInfo() + { + UseCallerCallContext = SmartThreadPool.DefaultUseCallerCallContext; + UseCallerHttpContext = SmartThreadPool.DefaultUseCallerHttpContext; + DisposeOfStateObjects = SmartThreadPool.DefaultDisposeOfStateObjects; + CallToPostExecute = SmartThreadPool.DefaultCallToPostExecute; + PostExecuteWorkItemCallback = SmartThreadPool.DefaultPostExecuteWorkItemCallback; + WorkItemPriority = SmartThreadPool.DefaultWorkItemPriority; + } - public WorkItemInfo(WorkItemInfo workItemInfo) - { - UseCallerCallContext = workItemInfo.UseCallerCallContext; - UseCallerHttpContext = workItemInfo.UseCallerHttpContext; - DisposeOfStateObjects = workItemInfo.DisposeOfStateObjects; - CallToPostExecute = workItemInfo.CallToPostExecute; - PostExecuteWorkItemCallback = workItemInfo.PostExecuteWorkItemCallback; - WorkItemPriority = workItemInfo.WorkItemPriority; + public WorkItemInfo(WorkItemInfo workItemInfo) + { + UseCallerCallContext = workItemInfo.UseCallerCallContext; + UseCallerHttpContext = workItemInfo.UseCallerHttpContext; + DisposeOfStateObjects = workItemInfo.DisposeOfStateObjects; + CallToPostExecute = workItemInfo.CallToPostExecute; + PostExecuteWorkItemCallback = workItemInfo.PostExecuteWorkItemCallback; + WorkItemPriority = workItemInfo.WorkItemPriority; Timeout = workItemInfo.Timeout; - } + } - /// - /// Get/Set if to use the caller's security context - /// - public bool UseCallerCallContext { get; set; } + /// + /// Get/Set if to use the caller's security context + /// + public bool UseCallerCallContext { get; set; } - /// - /// Get/Set if to use the caller's HTTP context - /// - public bool UseCallerHttpContext { get; set; } + /// + /// Get/Set if to use the caller's HTTP context + /// + public bool UseCallerHttpContext { get; set; } - /// - /// Get/Set if to dispose of the state object of a work item - /// - public bool DisposeOfStateObjects { get; set; } + /// + /// Get/Set if to dispose of the state object of a work item + /// + public bool DisposeOfStateObjects { get; set; } - /// - /// Get/Set the run the post execute options - /// + /// + /// Get/Set the run the post execute options + /// public CallToPostExecute CallToPostExecute { get; set; } - /// - /// Get/Set the post execute callback - /// + /// + /// Get/Set the post execute callback + /// public PostExecuteWorkItemCallback PostExecuteWorkItemCallback { get; set; } - /// - /// Get/Set the work item's priority - /// - public WorkItemPriority WorkItemPriority { get; set; } + /// + /// Get/Set the work item's priority + /// + public WorkItemPriority WorkItemPriority { get; set; } - /// - /// Get/Set the work item's timout in milliseconds. + /// + /// Get/Set the work item's timout in milliseconds. /// This is a passive timout. When the timout expires the work item won't be actively aborted! - /// - public long Timeout { get; set; } - } + /// + public long Timeout { get; set; } + } - #endregion + #endregion } diff --git a/ThirdParty/SmartThreadPool/WorkItemsGroup.cs b/ThirdParty/SmartThreadPool/WorkItemsGroup.cs index d9d34ac..d429bc6 100644 --- a/ThirdParty/SmartThreadPool/WorkItemsGroup.cs +++ b/ThirdParty/SmartThreadPool/WorkItemsGroup.cs @@ -6,107 +6,107 @@ using System.Diagnostics; namespace Amib.Threading.Internal { - #region WorkItemsGroup class + #region WorkItemsGroup class - /// - /// Summary description for WorkItemsGroup. - /// - public class WorkItemsGroup : WorkItemsGroupBase - { - #region Private members + /// + /// Summary description for WorkItemsGroup. + /// + public class WorkItemsGroup : WorkItemsGroupBase + { + #region Private members - private readonly object _lock = new object(); + private readonly object _lock = new object(); - /// - /// A reference to the SmartThreadPool instance that created this - /// WorkItemsGroup. - /// - private readonly SmartThreadPool _stp; + /// + /// A reference to the SmartThreadPool instance that created this + /// WorkItemsGroup. + /// + private readonly SmartThreadPool _stp; - /// - /// The OnIdle event - /// - private event WorkItemsGroupIdleHandler _onIdle; + /// + /// The OnIdle event + /// + private event WorkItemsGroupIdleHandler _onIdle; /// /// A flag to indicate if the Work Items Group is now suspended. /// private bool _isSuspended; - /// - /// Defines how many work items of this WorkItemsGroup can run at once. - /// - private int _concurrency; - - /// - /// Priority queue to hold work items before they are passed - /// to the SmartThreadPool. - /// - private readonly PriorityQueue _workItemsQueue; - - /// - /// Indicate how many work items are waiting in the SmartThreadPool - /// queue. - /// This value is used to apply the concurrency. - /// - private int _workItemsInStpQueue; - - /// - /// Indicate how many work items are currently running in the SmartThreadPool. - /// This value is used with the Cancel, to calculate if we can send new - /// work items to the STP. - /// - private int _workItemsExecutingInStp = 0; - - /// - /// WorkItemsGroup start information - /// - private readonly WIGStartInfo _workItemsGroupStartInfo; - - /// - /// Signaled when all of the WorkItemsGroup's work item completed. - /// + /// + /// Defines how many work items of this WorkItemsGroup can run at once. + /// + private int _concurrency; + + /// + /// Priority queue to hold work items before they are passed + /// to the SmartThreadPool. + /// + private readonly PriorityQueue _workItemsQueue; + + /// + /// Indicate how many work items are waiting in the SmartThreadPool + /// queue. + /// This value is used to apply the concurrency. + /// + private int _workItemsInStpQueue; + + /// + /// Indicate how many work items are currently running in the SmartThreadPool. + /// This value is used with the Cancel, to calculate if we can send new + /// work items to the STP. + /// + private int _workItemsExecutingInStp = 0; + + /// + /// WorkItemsGroup start information + /// + private readonly WIGStartInfo _workItemsGroupStartInfo; + + /// + /// Signaled when all of the WorkItemsGroup's work item completed. + /// //private readonly ManualResetEvent _isIdleWaitHandle = new ManualResetEvent(true); private readonly ManualResetEvent _isIdleWaitHandle = EventWaitHandleFactory.CreateManualResetEvent(true); - /// - /// A common object for all the work items that this work items group - /// generate so we can mark them to cancel in O(1) - /// - private CanceledWorkItemsGroup _canceledWorkItemsGroup = new CanceledWorkItemsGroup(); + /// + /// A common object for all the work items that this work items group + /// generate so we can mark them to cancel in O(1) + /// + private CanceledWorkItemsGroup _canceledWorkItemsGroup = new CanceledWorkItemsGroup(); - #endregion + #endregion - #region Construction + #region Construction - public WorkItemsGroup( - SmartThreadPool stp, - int concurrency, - WIGStartInfo wigStartInfo) - { - if (concurrency <= 0) - { - throw new ArgumentOutOfRangeException( + public WorkItemsGroup( + SmartThreadPool stp, + int concurrency, + WIGStartInfo wigStartInfo) + { + if (concurrency <= 0) + { + throw new ArgumentOutOfRangeException( "concurrency", #if !(_WINDOWS_CE) && !(_SILVERLIGHT) && !(WINDOWS_PHONE) concurrency, #endif "concurrency must be greater than zero"); - } - _stp = stp; - _concurrency = concurrency; - _workItemsGroupStartInfo = new WIGStartInfo(wigStartInfo).AsReadOnly(); - _workItemsQueue = new PriorityQueue(); - Name = "WorkItemsGroup"; + } + _stp = stp; + _concurrency = concurrency; + _workItemsGroupStartInfo = new WIGStartInfo(wigStartInfo).AsReadOnly(); + _workItemsQueue = new PriorityQueue(); + Name = "WorkItemsGroup"; - // The _workItemsInStpQueue gets the number of currently executing work items, - // because once a work item is executing, it cannot be cancelled. - _workItemsInStpQueue = _workItemsExecutingInStp; + // The _workItemsInStpQueue gets the number of currently executing work items, + // because once a work item is executing, it cannot be cancelled. + _workItemsInStpQueue = _workItemsExecutingInStp; _isSuspended = _workItemsGroupStartInfo.StartSuspended; - } + } - #endregion + #endregion #region WorkItemsGroupBase Overrides @@ -146,7 +146,7 @@ namespace Amib.Threading.Internal } } - /// + /// /// WorkItemsGroup start information /// public override WIGStartInfo WIGStartInfo @@ -154,38 +154,38 @@ namespace Amib.Threading.Internal get { return _workItemsGroupStartInfo; } } - /// - /// Start the Work Items Group if it was started suspended - /// - public override void Start() - { - // If the Work Items Group already started then quit - if (!_isSuspended) - { - return; - } - _isSuspended = false; - - EnqueueToSTPNextNWorkItem(Math.Min(_workItemsQueue.Count, _concurrency)); - } - - public override void Cancel(bool abortExecution) - { - lock (_lock) - { - _canceledWorkItemsGroup.IsCanceled = true; - _workItemsQueue.Clear(); - _workItemsInStpQueue = 0; - _canceledWorkItemsGroup = new CanceledWorkItemsGroup(); - } - - if (abortExecution) - { - _stp.CancelAbortWorkItemsGroup(this); - } - } - - /// + /// + /// Start the Work Items Group if it was started suspended + /// + public override void Start() + { + // If the Work Items Group already started then quit + if (!_isSuspended) + { + return; + } + _isSuspended = false; + + EnqueueToSTPNextNWorkItem(Math.Min(_workItemsQueue.Count, _concurrency)); + } + + public override void Cancel(bool abortExecution) + { + lock (_lock) + { + _canceledWorkItemsGroup.IsCanceled = true; + _workItemsQueue.Clear(); + _workItemsInStpQueue = 0; + _canceledWorkItemsGroup = new CanceledWorkItemsGroup(); + } + + if (abortExecution) + { + _stp.CancelAbortWorkItemsGroup(this); + } + } + + /// /// Wait for the thread pool to be idle /// public override bool WaitForIdle(int millisecondsTimeout) @@ -194,34 +194,34 @@ namespace Amib.Threading.Internal return STPEventWaitHandle.WaitOne(_isIdleWaitHandle, millisecondsTimeout, false); } - public override event WorkItemsGroupIdleHandler OnIdle - { - add { _onIdle += value; } - remove { _onIdle -= value; } - } + public override event WorkItemsGroupIdleHandler OnIdle + { + add { _onIdle += value; } + remove { _onIdle -= value; } + } - #endregion + #endregion - #region Private methods + #region Private methods - private void RegisterToWorkItemCompletion(IWorkItemResult wir) - { - IInternalWorkItemResult iwir = (IInternalWorkItemResult)wir; - iwir.OnWorkItemStarted += OnWorkItemStartedCallback; - iwir.OnWorkItemCompleted += OnWorkItemCompletedCallback; - } + private void RegisterToWorkItemCompletion(IWorkItemResult wir) + { + IInternalWorkItemResult iwir = (IInternalWorkItemResult)wir; + iwir.OnWorkItemStarted += OnWorkItemStartedCallback; + iwir.OnWorkItemCompleted += OnWorkItemCompletedCallback; + } - public void OnSTPIsStarting() - { + public void OnSTPIsStarting() + { if (_isSuspended) { return; } - + EnqueueToSTPNextNWorkItem(_concurrency); - } + } - public void EnqueueToSTPNextNWorkItem(int count) + public void EnqueueToSTPNextNWorkItem(int count) { for (int i = 0; i < count; ++i) { @@ -229,115 +229,115 @@ namespace Amib.Threading.Internal } } - private object FireOnIdle(object state) - { - FireOnIdleImpl(_onIdle); - return null; - } - - [MethodImpl(MethodImplOptions.NoInlining)] - private void FireOnIdleImpl(WorkItemsGroupIdleHandler onIdle) - { - if(null == onIdle) - { - return; - } - - Delegate[] delegates = onIdle.GetInvocationList(); - foreach(WorkItemsGroupIdleHandler eh in delegates) - { - try - { - eh(this); - } + private object FireOnIdle(object state) + { + FireOnIdleImpl(_onIdle); + return null; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private void FireOnIdleImpl(WorkItemsGroupIdleHandler onIdle) + { + if(null == onIdle) + { + return; + } + + Delegate[] delegates = onIdle.GetInvocationList(); + foreach(WorkItemsGroupIdleHandler eh in delegates) + { + try + { + eh(this); + } catch { } // Suppress exceptions - } - } - - private void OnWorkItemStartedCallback(WorkItem workItem) - { - lock(_lock) - { - ++_workItemsExecutingInStp; - } - } - - private void OnWorkItemCompletedCallback(WorkItem workItem) - { - EnqueueToSTPNextWorkItem(null, true); - } + } + } + + private void OnWorkItemStartedCallback(WorkItem workItem) + { + lock(_lock) + { + ++_workItemsExecutingInStp; + } + } + + private void OnWorkItemCompletedCallback(WorkItem workItem) + { + EnqueueToSTPNextWorkItem(null, true); + } internal override void Enqueue(WorkItem workItem) { EnqueueToSTPNextWorkItem(workItem); } - private void EnqueueToSTPNextWorkItem(WorkItem workItem) - { - EnqueueToSTPNextWorkItem(workItem, false); - } - - private void EnqueueToSTPNextWorkItem(WorkItem workItem, bool decrementWorkItemsInStpQueue) - { - lock(_lock) - { - // Got here from OnWorkItemCompletedCallback() - if (decrementWorkItemsInStpQueue) - { - --_workItemsInStpQueue; - - if(_workItemsInStpQueue < 0) - { - _workItemsInStpQueue = 0; - } - - --_workItemsExecutingInStp; - - if(_workItemsExecutingInStp < 0) - { - _workItemsExecutingInStp = 0; - } - } - - // If the work item is not null then enqueue it - if (null != workItem) - { - workItem.CanceledWorkItemsGroup = _canceledWorkItemsGroup; - - RegisterToWorkItemCompletion(workItem.GetWorkItemResult()); - _workItemsQueue.Enqueue(workItem); - //_stp.IncrementWorkItemsCount(); - - if ((1 == _workItemsQueue.Count) && - (0 == _workItemsInStpQueue)) - { - _stp.RegisterWorkItemsGroup(this); + private void EnqueueToSTPNextWorkItem(WorkItem workItem) + { + EnqueueToSTPNextWorkItem(workItem, false); + } + + private void EnqueueToSTPNextWorkItem(WorkItem workItem, bool decrementWorkItemsInStpQueue) + { + lock(_lock) + { + // Got here from OnWorkItemCompletedCallback() + if (decrementWorkItemsInStpQueue) + { + --_workItemsInStpQueue; + + if(_workItemsInStpQueue < 0) + { + _workItemsInStpQueue = 0; + } + + --_workItemsExecutingInStp; + + if(_workItemsExecutingInStp < 0) + { + _workItemsExecutingInStp = 0; + } + } + + // If the work item is not null then enqueue it + if (null != workItem) + { + workItem.CanceledWorkItemsGroup = _canceledWorkItemsGroup; + + RegisterToWorkItemCompletion(workItem.GetWorkItemResult()); + _workItemsQueue.Enqueue(workItem); + //_stp.IncrementWorkItemsCount(); + + if ((1 == _workItemsQueue.Count) && + (0 == _workItemsInStpQueue)) + { + _stp.RegisterWorkItemsGroup(this); IsIdle = false; _isIdleWaitHandle.Reset(); - } - } - - // If the work items queue of the group is empty than quit - if (0 == _workItemsQueue.Count) - { - if (0 == _workItemsInStpQueue) - { - _stp.UnregisterWorkItemsGroup(this); + } + } + + // If the work items queue of the group is empty than quit + if (0 == _workItemsQueue.Count) + { + if (0 == _workItemsInStpQueue) + { + _stp.UnregisterWorkItemsGroup(this); IsIdle = true; _isIdleWaitHandle.Set(); if (decrementWorkItemsInStpQueue && _onIdle != null && _onIdle.GetInvocationList().Length > 0) { _stp.QueueWorkItem(new WorkItemCallback(FireOnIdle)); } - } - return; - } + } + return; + } if (!_isSuspended) - { - if (_workItemsInStpQueue < _concurrency) - { - WorkItem nextWorkItem = _workItemsQueue.Dequeue() as WorkItem; + { + if (_workItemsInStpQueue < _concurrency) + { + WorkItem nextWorkItem = _workItemsQueue.Dequeue() as WorkItem; try { _stp.Enqueue(nextWorkItem); @@ -348,14 +348,14 @@ namespace Amib.Threading.Internal // The STP has been shutdown } - ++_workItemsInStpQueue; - } - } - } - } + ++_workItemsInStpQueue; + } + } + } + } - #endregion + #endregion } - #endregion + #endregion } diff --git a/ThirdParty/SmartThreadPool/WorkItemsGroupBase.cs b/ThirdParty/SmartThreadPool/WorkItemsGroupBase.cs index 27fae5e..3a5dcc6 100644 --- a/ThirdParty/SmartThreadPool/WorkItemsGroupBase.cs +++ b/ThirdParty/SmartThreadPool/WorkItemsGroupBase.cs @@ -132,7 +132,7 @@ namespace Amib.Threading.Internal /// /// A callback to execute /// - /// The context object of the work item. Used for passing arguments to the work item. + /// The context object of the work item. Used for passing arguments to the work item. /// /// Returns a work item result public IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state) @@ -147,7 +147,7 @@ namespace Amib.Threading.Internal /// /// A callback to execute /// - /// The context object of the work item. Used for passing arguments to the work item. + /// The context object of the work item. Used for passing arguments to the work item. /// /// The work item priority /// Returns a work item result @@ -165,7 +165,7 @@ namespace Amib.Threading.Internal /// Work item information /// A callback to execute /// - /// The context object of the work item. Used for passing arguments to the work item. + /// The context object of the work item. Used for passing arguments to the work item. /// /// Returns a work item result public IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WorkItemCallback callback, object state) @@ -181,7 +181,7 @@ namespace Amib.Threading.Internal /// /// A callback to execute /// - /// The context object of the work item. Used for passing arguments to the work item. + /// The context object of the work item. Used for passing arguments to the work item. /// /// /// A delegate to call after the callback completion @@ -203,7 +203,7 @@ namespace Amib.Threading.Internal /// /// A callback to execute /// - /// The context object of the work item. Used for passing arguments to the work item. + /// The context object of the work item. Used for passing arguments to the work item. /// /// /// A delegate to call after the callback completion @@ -227,7 +227,7 @@ namespace Amib.Threading.Internal /// /// A callback to execute /// - /// The context object of the work item. Used for passing arguments to the work item. + /// The context object of the work item. Used for passing arguments to the work item. /// /// /// A delegate to call after the callback completion @@ -251,7 +251,7 @@ namespace Amib.Threading.Internal /// /// A callback to execute /// - /// The context object of the work item. Used for passing arguments to the work item. + /// The context object of the work item. Used for passing arguments to the work item. /// /// /// A delegate to call after the callback completion diff --git a/ThirdParty/SmartThreadPool/WorkItemsQueue.cs b/ThirdParty/SmartThreadPool/WorkItemsQueue.cs index e0bc916..21403a0 100644 --- a/ThirdParty/SmartThreadPool/WorkItemsQueue.cs +++ b/ThirdParty/SmartThreadPool/WorkItemsQueue.cs @@ -4,29 +4,29 @@ using System.Threading; namespace Amib.Threading.Internal { - #region WorkItemsQueue class - - /// - /// WorkItemsQueue class. - /// - public class WorkItemsQueue : IDisposable - { - #region Member variables - - /// - /// Waiters queue (implemented as stack). - /// - private readonly WaiterEntry _headWaiterEntry = new WaiterEntry(); - - /// - /// Waiters count - /// - private int _waitersCount = 0; - - /// - /// Work items queue - /// - private readonly PriorityQueue _workItems = new PriorityQueue(); + #region WorkItemsQueue class + + /// + /// WorkItemsQueue class. + /// + public class WorkItemsQueue : IDisposable + { + #region Member variables + + /// + /// Waiters queue (implemented as stack). + /// + private readonly WaiterEntry _headWaiterEntry = new WaiterEntry(); + + /// + /// Waiters count + /// + private int _waitersCount = 0; + + /// + /// Work items queue + /// + private readonly PriorityQueue _workItems = new PriorityQueue(); /// /// Indicate that work items are allowed to be queued @@ -34,7 +34,7 @@ namespace Amib.Threading.Internal private bool _isWorkItemsQueueActive = true; -#if (WINDOWS_PHONE) +#if (WINDOWS_PHONE) private static readonly Dictionary _waiterEntries = new Dictionary(); #elif (_WINDOWS_CE) private static LocalDataStoreSlot _waiterEntrySlot = Thread.AllocateDataSlot(); @@ -50,7 +50,7 @@ namespace Amib.Threading.Internal /// private static WaiterEntry CurrentWaiterEntry { -#if (WINDOWS_PHONE) +#if (WINDOWS_PHONE) get { lock (_waiterEntries) @@ -92,60 +92,60 @@ namespace Amib.Threading.Internal } /// - /// A flag that indicates if the WorkItemsQueue has been disposed. - /// + /// A flag that indicates if the WorkItemsQueue has been disposed. + /// private bool _isDisposed = false; - #endregion + #endregion - #region Public properties + #region Public properties /// /// Returns the current number of work items in the queue /// - public int Count - { - get - { + public int Count + { + get + { return _workItems.Count; - } - } + } + } - /// - /// Returns the current number of waiters - /// - public int WaitersCount - { - get - { - return _waitersCount; - } - } + /// + /// Returns the current number of waiters + /// + public int WaitersCount + { + get + { + return _waitersCount; + } + } #endregion #region Public methods - /// - /// Enqueue a work item to the queue. - /// - public bool EnqueueWorkItem(WorkItem workItem) - { - // A work item cannot be null, since null is used in the - // WaitForWorkItem() method to indicate timeout or cancel - if (null == workItem) - { - throw new ArgumentNullException("workItem" , "workItem cannot be null"); - } - - bool enqueue = true; - - // First check if there is a waiter waiting for work item. During - // the check, timed out waiters are ignored. If there is no - // waiter then the work item is queued. - lock(this) - { + /// + /// Enqueue a work item to the queue. + /// + public bool EnqueueWorkItem(WorkItem workItem) + { + // A work item cannot be null, since null is used in the + // WaitForWorkItem() method to indicate timeout or cancel + if (null == workItem) + { + throw new ArgumentNullException("workItem" , "workItem cannot be null"); + } + + bool enqueue = true; + + // First check if there is a waiter waiting for work item. During + // the check, timed out waiters are ignored. If there is no + // waiter then the work item is queued. + lock(this) + { ValidateNotDisposed(); if (!_isWorkItemsQueueActive) @@ -153,55 +153,55 @@ namespace Amib.Threading.Internal return false; } - while(_waitersCount > 0) - { - // Dequeue a waiter. - WaiterEntry waiterEntry = PopWaiter(); - - // Signal the waiter. On success break the loop - if (waiterEntry.Signal(workItem)) - { - enqueue = false; - break; - } - } - - if (enqueue) - { - // Enqueue the work item - _workItems.Enqueue(workItem); - } - } + while(_waitersCount > 0) + { + // Dequeue a waiter. + WaiterEntry waiterEntry = PopWaiter(); + + // Signal the waiter. On success break the loop + if (waiterEntry.Signal(workItem)) + { + enqueue = false; + break; + } + } + + if (enqueue) + { + // Enqueue the work item + _workItems.Enqueue(workItem); + } + } return true; - } - - - /// - /// Waits for a work item or exits on timeout or cancel - /// - /// Timeout in milliseconds - /// Cancel wait handle - /// Returns true if the resource was granted - public WorkItem DequeueWorkItem( - int millisecondsTimeout, - WaitHandle cancelEvent) - { - // This method cause the caller to wait for a work item. - // If there is at least one waiting work item then the - // method returns immidiately with it. - // - // If there are no waiting work items then the caller - // is queued between other waiters for a work item to arrive. - // - // If a work item didn't come within millisecondsTimeout or - // the user canceled the wait by signaling the cancelEvent - // then the method returns null to indicate that the caller - // didn't get a work item. - - WaiterEntry waiterEntry; - WorkItem workItem = null; - lock (this) - { + } + + + /// + /// Waits for a work item or exits on timeout or cancel + /// + /// Timeout in milliseconds + /// Cancel wait handle + /// Returns true if the resource was granted + public WorkItem DequeueWorkItem( + int millisecondsTimeout, + WaitHandle cancelEvent) + { + // This method cause the caller to wait for a work item. + // If there is at least one waiting work item then the + // method returns immidiately with it. + // + // If there are no waiting work items then the caller + // is queued between other waiters for a work item to arrive. + // + // If a work item didn't come within millisecondsTimeout or + // the user canceled the wait by signaling the cancelEvent + // then the method returns null to indicate that the caller + // didn't get a work item. + + WaiterEntry waiterEntry; + WorkItem workItem = null; + lock (this) + { ValidateNotDisposed(); // If there are waiting work items then take one and return. @@ -218,68 +218,68 @@ namespace Amib.Threading.Internal // Put the waiter with the other waiters PushWaiter(waiterEntry); - } + } - // Prepare array of wait handle for the WaitHandle.WaitAny() - WaitHandle [] waitHandles = new WaitHandle[] { - waiterEntry.WaitHandle, - cancelEvent }; + // Prepare array of wait handle for the WaitHandle.WaitAny() + WaitHandle [] waitHandles = new WaitHandle[] { + waiterEntry.WaitHandle, + cancelEvent }; - // Wait for an available resource, cancel event, or timeout. + // Wait for an available resource, cancel event, or timeout. - // During the wait we are supposes to exit the synchronization - // domain. (Placing true as the third argument of the WaitAny()) - // It just doesn't work, I don't know why, so I have two lock(this) - // statments instead of one. + // During the wait we are supposes to exit the synchronization + // domain. (Placing true as the third argument of the WaitAny()) + // It just doesn't work, I don't know why, so I have two lock(this) + // statments instead of one. int index = STPEventWaitHandle.WaitAny( - waitHandles, - millisecondsTimeout, - true); - - lock(this) - { - // success is true if it got a work item. - bool success = (0 == index); - - // The timeout variable is used only for readability. - // (We treat cancel as timeout) - bool timeout = !success; - - // On timeout update the waiterEntry that it is timed out - if (timeout) - { - // The Timeout() fails if the waiter has already been signaled - timeout = waiterEntry.Timeout(); - - // On timeout remove the waiter from the queue. - // Note that the complexity is O(1). - if(timeout) - { - RemoveWaiter(waiterEntry, false); - } - - // Again readability - success = !timeout; - } - - // On success return the work item - if (success) - { - workItem = waiterEntry.WorkItem; - - if (null == workItem) - { - workItem = _workItems.Dequeue() as WorkItem; - } - } - } - // On failure return null. - return workItem; - } + waitHandles, + millisecondsTimeout, + true); + + lock(this) + { + // success is true if it got a work item. + bool success = (0 == index); + + // The timeout variable is used only for readability. + // (We treat cancel as timeout) + bool timeout = !success; + + // On timeout update the waiterEntry that it is timed out + if (timeout) + { + // The Timeout() fails if the waiter has already been signaled + timeout = waiterEntry.Timeout(); + + // On timeout remove the waiter from the queue. + // Note that the complexity is O(1). + if(timeout) + { + RemoveWaiter(waiterEntry, false); + } + + // Again readability + success = !timeout; + } + + // On success return the work item + if (success) + { + workItem = waiterEntry.WorkItem; + + if (null == workItem) + { + workItem = _workItems.Dequeue() as WorkItem; + } + } + } + // On failure return null. + return workItem; + } /// - /// Cleanup the work items queue, hence no more work + /// Cleanup the work items queue, hence no more work /// items are allowed to be queue /// private void Cleanup() @@ -303,19 +303,19 @@ namespace Amib.Threading.Internal // Clear the work items that are already queued _workItems.Clear(); - // Note: - // I don't iterate over the queue and dispose of work items's states, - // since if a work item has a state object that is still in use in the + // Note: + // I don't iterate over the queue and dispose of work items's states, + // since if a work item has a state object that is still in use in the // application then I must not dispose it. // Tell the waiters that they were timed out. // It won't signal them to exit, but to ignore their // next work item. - while(_waitersCount > 0) - { - WaiterEntry waiterEntry = PopWaiter(); - waiterEntry.Timeout(); - } + while(_waitersCount > 0) + { + WaiterEntry waiterEntry = PopWaiter(); + waiterEntry.Timeout(); + } } } @@ -334,275 +334,275 @@ namespace Amib.Threading.Internal } } - #endregion - - #region Private methods - - /// - /// Returns the WaiterEntry of the current thread - /// - /// - /// In order to avoid creation and destuction of WaiterEntry - /// objects each thread has its own WaiterEntry object. - private static WaiterEntry GetThreadWaiterEntry() - { - if (null == CurrentWaiterEntry) - { - CurrentWaiterEntry = new WaiterEntry(); - } - CurrentWaiterEntry.Reset(); - return CurrentWaiterEntry; - } - - #region Waiters stack methods - - /// - /// Push a new waiter into the waiter's stack - /// - /// A waiter to put in the stack - public void PushWaiter(WaiterEntry newWaiterEntry) - { - // Remove the waiter if it is already in the stack and - // update waiter's count as needed - RemoveWaiter(newWaiterEntry, false); - - // If the stack is empty then newWaiterEntry is the new head of the stack - if (null == _headWaiterEntry._nextWaiterEntry) - { - _headWaiterEntry._nextWaiterEntry = newWaiterEntry; - newWaiterEntry._prevWaiterEntry = _headWaiterEntry; - - } - // If the stack is not empty then put newWaiterEntry as the new head - // of the stack. - else - { - // Save the old first waiter entry - WaiterEntry oldFirstWaiterEntry = _headWaiterEntry._nextWaiterEntry; + #endregion + + #region Private methods + + /// + /// Returns the WaiterEntry of the current thread + /// + /// + /// In order to avoid creation and destuction of WaiterEntry + /// objects each thread has its own WaiterEntry object. + private static WaiterEntry GetThreadWaiterEntry() + { + if (null == CurrentWaiterEntry) + { + CurrentWaiterEntry = new WaiterEntry(); + } + CurrentWaiterEntry.Reset(); + return CurrentWaiterEntry; + } + + #region Waiters stack methods + + /// + /// Push a new waiter into the waiter's stack + /// + /// A waiter to put in the stack + public void PushWaiter(WaiterEntry newWaiterEntry) + { + // Remove the waiter if it is already in the stack and + // update waiter's count as needed + RemoveWaiter(newWaiterEntry, false); + + // If the stack is empty then newWaiterEntry is the new head of the stack + if (null == _headWaiterEntry._nextWaiterEntry) + { + _headWaiterEntry._nextWaiterEntry = newWaiterEntry; + newWaiterEntry._prevWaiterEntry = _headWaiterEntry; + + } + // If the stack is not empty then put newWaiterEntry as the new head + // of the stack. + else + { + // Save the old first waiter entry + WaiterEntry oldFirstWaiterEntry = _headWaiterEntry._nextWaiterEntry; // Update the links - _headWaiterEntry._nextWaiterEntry = newWaiterEntry; - newWaiterEntry._nextWaiterEntry = oldFirstWaiterEntry; - newWaiterEntry._prevWaiterEntry = _headWaiterEntry; - oldFirstWaiterEntry._prevWaiterEntry = newWaiterEntry; - } - - // Increment the number of waiters - ++_waitersCount; - } - - /// - /// Pop a waiter from the waiter's stack - /// - /// Returns the first waiter in the stack - private WaiterEntry PopWaiter() - { - // Store the current stack head - WaiterEntry oldFirstWaiterEntry = _headWaiterEntry._nextWaiterEntry; - - // Store the new stack head - WaiterEntry newHeadWaiterEntry = oldFirstWaiterEntry._nextWaiterEntry; - - // Update the old stack head list links and decrement the number - // waiters. - RemoveWaiter(oldFirstWaiterEntry, true); - - // Update the new stack head - _headWaiterEntry._nextWaiterEntry = newHeadWaiterEntry; - if (null != newHeadWaiterEntry) - { - newHeadWaiterEntry._prevWaiterEntry = _headWaiterEntry; - } - - // Return the old stack head - return oldFirstWaiterEntry; - } - - /// - /// Remove a waiter from the stack - /// - /// A waiter entry to remove - /// If true the waiter count is always decremented - private void RemoveWaiter(WaiterEntry waiterEntry, bool popDecrement) - { - // Store the prev entry in the list - WaiterEntry prevWaiterEntry = waiterEntry._prevWaiterEntry; - - // Store the next entry in the list - WaiterEntry nextWaiterEntry = waiterEntry._nextWaiterEntry; - - // A flag to indicate if we need to decrement the waiters count. - // If we got here from PopWaiter then we must decrement. - // If we got here from PushWaiter then we decrement only if - // the waiter was already in the stack. - bool decrementCounter = popDecrement; - - // Null the waiter's entry links - waiterEntry._prevWaiterEntry = null; - waiterEntry._nextWaiterEntry = null; - - // If the waiter entry had a prev link then update it. - // It also means that the waiter is already in the list and we - // need to decrement the waiters count. - if (null != prevWaiterEntry) - { - prevWaiterEntry._nextWaiterEntry = nextWaiterEntry; - decrementCounter = true; - } - - // If the waiter entry had a next link then update it. - // It also means that the waiter is already in the list and we - // need to decrement the waiters count. - if (null != nextWaiterEntry) - { - nextWaiterEntry._prevWaiterEntry = prevWaiterEntry; - decrementCounter = true; - } - - // Decrement the waiters count if needed - if (decrementCounter) - { - --_waitersCount; - } - } - - #endregion - - #endregion - - #region WaiterEntry class - - // A waiter entry in the _waiters queue. - public sealed class WaiterEntry : IDisposable - { - #region Member variables - - /// - /// Event to signal the waiter that it got the work item. - /// - //private AutoResetEvent _waitHandle = new AutoResetEvent(false); + _headWaiterEntry._nextWaiterEntry = newWaiterEntry; + newWaiterEntry._nextWaiterEntry = oldFirstWaiterEntry; + newWaiterEntry._prevWaiterEntry = _headWaiterEntry; + oldFirstWaiterEntry._prevWaiterEntry = newWaiterEntry; + } + + // Increment the number of waiters + ++_waitersCount; + } + + /// + /// Pop a waiter from the waiter's stack + /// + /// Returns the first waiter in the stack + private WaiterEntry PopWaiter() + { + // Store the current stack head + WaiterEntry oldFirstWaiterEntry = _headWaiterEntry._nextWaiterEntry; + + // Store the new stack head + WaiterEntry newHeadWaiterEntry = oldFirstWaiterEntry._nextWaiterEntry; + + // Update the old stack head list links and decrement the number + // waiters. + RemoveWaiter(oldFirstWaiterEntry, true); + + // Update the new stack head + _headWaiterEntry._nextWaiterEntry = newHeadWaiterEntry; + if (null != newHeadWaiterEntry) + { + newHeadWaiterEntry._prevWaiterEntry = _headWaiterEntry; + } + + // Return the old stack head + return oldFirstWaiterEntry; + } + + /// + /// Remove a waiter from the stack + /// + /// A waiter entry to remove + /// If true the waiter count is always decremented + private void RemoveWaiter(WaiterEntry waiterEntry, bool popDecrement) + { + // Store the prev entry in the list + WaiterEntry prevWaiterEntry = waiterEntry._prevWaiterEntry; + + // Store the next entry in the list + WaiterEntry nextWaiterEntry = waiterEntry._nextWaiterEntry; + + // A flag to indicate if we need to decrement the waiters count. + // If we got here from PopWaiter then we must decrement. + // If we got here from PushWaiter then we decrement only if + // the waiter was already in the stack. + bool decrementCounter = popDecrement; + + // Null the waiter's entry links + waiterEntry._prevWaiterEntry = null; + waiterEntry._nextWaiterEntry = null; + + // If the waiter entry had a prev link then update it. + // It also means that the waiter is already in the list and we + // need to decrement the waiters count. + if (null != prevWaiterEntry) + { + prevWaiterEntry._nextWaiterEntry = nextWaiterEntry; + decrementCounter = true; + } + + // If the waiter entry had a next link then update it. + // It also means that the waiter is already in the list and we + // need to decrement the waiters count. + if (null != nextWaiterEntry) + { + nextWaiterEntry._prevWaiterEntry = prevWaiterEntry; + decrementCounter = true; + } + + // Decrement the waiters count if needed + if (decrementCounter) + { + --_waitersCount; + } + } + + #endregion + + #endregion + + #region WaiterEntry class + + // A waiter entry in the _waiters queue. + public sealed class WaiterEntry : IDisposable + { + #region Member variables + + /// + /// Event to signal the waiter that it got the work item. + /// + //private AutoResetEvent _waitHandle = new AutoResetEvent(false); private AutoResetEvent _waitHandle = EventWaitHandleFactory.CreateAutoResetEvent(); - /// - /// Flag to know if this waiter already quited from the queue - /// because of a timeout. - /// - private bool _isTimedout = false; + /// + /// Flag to know if this waiter already quited from the queue + /// because of a timeout. + /// + private bool _isTimedout = false; - /// - /// Flag to know if the waiter was signaled and got a work item. - /// - private bool _isSignaled = false; + /// + /// Flag to know if the waiter was signaled and got a work item. + /// + private bool _isSignaled = false; /// - /// A work item that passed directly to the waiter withou going + /// A work item that passed directly to the waiter withou going /// through the queue /// - private WorkItem _workItem = null; + private WorkItem _workItem = null; private bool _isDisposed = false; - // Linked list members - internal WaiterEntry _nextWaiterEntry = null; - internal WaiterEntry _prevWaiterEntry = null; - - #endregion - - #region Construction - - public WaiterEntry() - { - Reset(); - } - - #endregion - - #region Public methods - - public WaitHandle WaitHandle - { - get { return _waitHandle; } - } - - public WorkItem WorkItem - { - get - { - return _workItem; - } - } - - /// - /// Signal the waiter that it got a work item. - /// - /// Return true on success - /// The method fails if Timeout() preceded its call - public bool Signal(WorkItem workItem) - { - lock(this) - { - if (!_isTimedout) - { - _workItem = workItem; - _isSignaled = true; - _waitHandle.Set(); - return true; - } - } - return false; - } - - /// - /// Mark the wait entry that it has been timed out - /// - /// Return true on success - /// The method fails if Signal() preceded its call - public bool Timeout() - { - lock(this) - { - // Time out can happen only if the waiter wasn't marked as - // signaled - if (!_isSignaled) - { - // We don't remove the waiter from the queue, the DequeueWorkItem + // Linked list members + internal WaiterEntry _nextWaiterEntry = null; + internal WaiterEntry _prevWaiterEntry = null; + + #endregion + + #region Construction + + public WaiterEntry() + { + Reset(); + } + + #endregion + + #region Public methods + + public WaitHandle WaitHandle + { + get { return _waitHandle; } + } + + public WorkItem WorkItem + { + get + { + return _workItem; + } + } + + /// + /// Signal the waiter that it got a work item. + /// + /// Return true on success + /// The method fails if Timeout() preceded its call + public bool Signal(WorkItem workItem) + { + lock(this) + { + if (!_isTimedout) + { + _workItem = workItem; + _isSignaled = true; + _waitHandle.Set(); + return true; + } + } + return false; + } + + /// + /// Mark the wait entry that it has been timed out + /// + /// Return true on success + /// The method fails if Signal() preceded its call + public bool Timeout() + { + lock(this) + { + // Time out can happen only if the waiter wasn't marked as + // signaled + if (!_isSignaled) + { + // We don't remove the waiter from the queue, the DequeueWorkItem // method skips _waiters that were timed out. - _isTimedout = true; - return true; - } - } - return false; - } - - /// - /// Reset the wait entry so it can be used again - /// - public void Reset() - { - _workItem = null; - _isTimedout = false; - _isSignaled = false; - _waitHandle.Reset(); - } - - /// - /// Free resources - /// - public void Close() - { - if (null != _waitHandle) - { - _waitHandle.Close(); - _waitHandle = null; - } - } - - #endregion - - #region IDisposable Members - - public void Dispose() - { + _isTimedout = true; + return true; + } + } + return false; + } + + /// + /// Reset the wait entry so it can be used again + /// + public void Reset() + { + _workItem = null; + _isTimedout = false; + _isSignaled = false; + _waitHandle.Reset(); + } + + /// + /// Free resources + /// + public void Close() + { + if (null != _waitHandle) + { + _waitHandle.Close(); + _waitHandle = null; + } + } + + #endregion + + #region IDisposable Members + + public void Dispose() + { lock (this) { if (!_isDisposed) @@ -613,10 +613,10 @@ namespace Amib.Threading.Internal } } - #endregion - } + #endregion + } - #endregion + #endregion #region IDisposable Members @@ -625,6 +625,7 @@ namespace Amib.Threading.Internal if (!_isDisposed) { Cleanup(); + _headWaiterEntry.Close(); } _isDisposed = true; } @@ -640,6 +641,6 @@ namespace Amib.Threading.Internal #endregion } - #endregion + #endregion } -- cgit v1.1