├── UnitTest ├── Usings.cs ├── AssemblyInfo.cs ├── UnitTest.csproj ├── DeferredActionTimerTest.cs ├── ConcurrentSetTest.cs └── InterlockedFlagTest.cs ├── icon.png ├── PowerThreadPool.publickey ├── PowerThreadPool ├── Constants │ ├── CanWatch.cs │ ├── CanCancel.cs │ ├── CanDispose.cs │ ├── WatchStates.cs │ ├── WorkHeldStates.cs │ ├── CanForceStop.cs │ ├── CanCreateNewWorker.cs │ ├── WorkStealability.cs │ ├── CanDeleteRedundantWorker.cs │ ├── DependencyStatus.cs │ ├── PoolStates.cs │ ├── WorkerStates.cs │ └── CanGetWork.cs ├── Works │ ├── WorkItemBase.cs │ ├── AsyncWorkInfo.cs │ ├── WorkFunc.cs │ ├── WorkAction.cs │ ├── WorkIDGuid.cs │ ├── WorkIDLong.cs │ ├── WorkIDString.cs │ ├── WorkBase.cs │ └── WorkID.cs ├── Exceptions │ ├── CycleDetectedException.cs │ ├── WorkRejectedException.cs │ ├── WorkStopException.cs │ └── WorkExceptionBase.cs ├── EventArguments │ ├── WorkCanceledEventArgs.cs │ ├── WorkStartedEventArgs.cs │ ├── WorkTimedOutEventArgs.cs │ ├── WorkRejectedEventArgs.cs │ ├── WorkStoppedEventArgs.cs │ ├── WorkDiscardedEventArgs.cs │ ├── RunningTimerElapsedEventArgs.cs │ ├── RunningWorkerCountChangedEventArgs.cs │ ├── WorkEndedEventArgs.cs │ ├── PoolIdledEventArgs.cs │ ├── WorkEventArgsBase.cs │ └── ErrorOccurredEventArgs.cs ├── Core │ ├── WorkerContext.cs │ ├── PowerThreadPool.Group.cs │ └── PowerThreadPool.QueueWorkItemAsync.cs ├── Options │ ├── WorkOption.cs │ ├── TimeoutOption.cs │ ├── RunningTimerOption.cs │ ├── RejectOption.cs │ ├── RetryOption.cs │ ├── DestroyThreadOption.cs │ ├── WorkOptionOfT.cs │ └── PowerPoolOption.cs ├── Helpers │ ├── Asynchronous │ │ ├── ITaskCompletionSource.cs │ │ ├── TaskCompletionSourceBox.cs │ │ ├── AsyncManualResetEvent.cs │ │ ├── PowerPoolSynchronizationContext.cs │ │ └── PowerPoolSynchronizationContextOfT.cs │ ├── ConcurrentStealablePriorityCollectionHelper.cs │ ├── HitChecker.cs │ ├── StatusPingPongChecker.cs │ ├── LockFree │ │ ├── Spinner.cs │ │ ├── WorkGuard.cs │ │ └── InterlockedFlag.cs │ ├── Timers │ │ └── DeferredActionTimer.cs │ └── DelegateHelper.cs ├── Collections │ ├── Comparer │ │ └── DescendingIntComparer.cs │ ├── IStealablePriorityCollection.cs │ ├── ConcurrentSet.cs │ ├── ConcurrentStealablePriorityStack.cs │ ├── ConcurrentStealablePriorityQueue.cs │ ├── ConcurrentStealablePriorityDeque.cs │ └── ConcurrentObservableCollection.cs ├── Results │ ├── RetryInfo.cs │ ├── ExecuteResult.cs │ └── ExecuteResultBase.cs └── PowerThreadPool.csproj ├── PowerThreadPoolTest ├── App.xaml.cs ├── App.xaml ├── AssemblyInfo.cs ├── PowerThreadPoolTest.csproj ├── MainWindow.xaml └── MainWindow.xaml.cs ├── coverage.runsettings ├── Benchmark ├── Program.cs ├── Benchmark.csproj ├── BenchmarkAsyncWork.cs ├── BenchmarkSyncWork.cs ├── BenchmarkSyncShortWork.cs └── BenchmarkAsyncShortWork.cs ├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md └── workflows │ ├── build_sign_publish.yml │ └── test.yml ├── Deque ├── Deque.projitems ├── Deque.shproj ├── Utility.cs └── Deque.cs ├── SECURITY.md ├── LICENSE ├── .all-contributorsrc ├── PowerThreadPool.sln └── .gitignore /UnitTest/Usings.cs: -------------------------------------------------------------------------------- 1 | global using Xunit; 2 | -------------------------------------------------------------------------------- /icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZjzMisaka/PowerThreadPool/HEAD/icon.png -------------------------------------------------------------------------------- /UnitTest/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | [assembly: CollectionBehavior(DisableTestParallelization = true)] 2 | -------------------------------------------------------------------------------- /PowerThreadPool.publickey: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZjzMisaka/PowerThreadPool/HEAD/PowerThreadPool.publickey -------------------------------------------------------------------------------- /PowerThreadPool/Constants/CanWatch.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.Constants 2 | { 3 | internal enum CanWatch 4 | { 5 | Allowed = 0, 6 | NotAllowed = 1, 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /PowerThreadPool/Works/WorkItemBase.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.Works 2 | { 3 | public abstract class WorkItemBase 4 | { 5 | internal WorkID ID { get; set; } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /PowerThreadPool/Constants/CanCancel.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.Constants 2 | { 3 | internal enum CanCancel 4 | { 5 | Allowed = 0, 6 | NotAllowed = 1, 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /PowerThreadPool/Constants/CanDispose.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.Constants 2 | { 3 | internal enum CanDispose 4 | { 5 | Allowed = 0, 6 | NotAllowed = 1, 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /PowerThreadPool/Constants/WatchStates.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.Constants 2 | { 3 | internal enum WatchStates 4 | { 5 | Idle = 0, 6 | Watching = 1, 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /PowerThreadPool/Constants/WorkHeldStates.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.Constants 2 | { 3 | internal enum WorkHeldStates 4 | { 5 | NotHeld = 0, 6 | Held = 1 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /PowerThreadPool/Constants/CanForceStop.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.Constants 2 | { 3 | internal enum CanForceStop 4 | { 5 | Allowed = 0, 6 | NotAllowed = 1, 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /PowerThreadPool/Constants/CanCreateNewWorker.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.Constants 2 | { 3 | internal enum CanCreateNewWorker 4 | { 5 | Allowed = 0, 6 | NotAllowed = 1, 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /PowerThreadPool/Constants/WorkStealability.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.Constants 2 | { 3 | internal enum WorkStealability 4 | { 5 | Allowed = 0, 6 | NotAllowed = 1, 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /PowerThreadPool/Constants/CanDeleteRedundantWorker.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.Constants 2 | { 3 | internal enum CanDeleteRedundantWorker 4 | { 5 | Allowed = 0, 6 | NotAllowed = 1, 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /PowerThreadPool/Constants/DependencyStatus.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.Constants 2 | { 3 | internal enum DependencyStatus 4 | { 5 | Normal = 0, 6 | Solved = 1, 7 | Failed = 2, 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /PowerThreadPool/Constants/PoolStates.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.Constants 2 | { 3 | internal enum PoolStates 4 | { 5 | NotRunning = 0, 6 | IdleChecked = 1, 7 | Running = 2, 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /PowerThreadPool/Constants/WorkerStates.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.Constants 2 | { 3 | internal enum WorkerStates 4 | { 5 | Idle = 0, 6 | Running = 1, 7 | ToBeDisposed = 2, 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /PowerThreadPool/Exceptions/CycleDetectedException.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.Exceptions 2 | { 3 | public class CycleDetectedException : WorkExceptionBase 4 | { 5 | public CycleDetectedException() { } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /PowerThreadPool/Exceptions/WorkRejectedException.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.Exceptions 2 | { 3 | public class WorkRejectedException : WorkExceptionBase 4 | { 5 | public WorkRejectedException() { } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /PowerThreadPool/EventArguments/WorkCanceledEventArgs.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.EventArguments 2 | { 3 | public class WorkCanceledEventArgs : WorkEventArgsBase 4 | { 5 | public WorkCanceledEventArgs() { } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /PowerThreadPool/EventArguments/WorkStartedEventArgs.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.EventArguments 2 | { 3 | public class WorkStartedEventArgs : WorkEventArgsBase 4 | { 5 | public WorkStartedEventArgs() { } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /PowerThreadPool/EventArguments/WorkTimedOutEventArgs.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.EventArguments 2 | { 3 | public class WorkTimedOutEventArgs : WorkEventArgsBase 4 | { 5 | public WorkTimedOutEventArgs() { } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /PowerThreadPool/Core/WorkerContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace PowerThreadPool 4 | { 5 | internal static class WorkerContext 6 | { 7 | [ThreadStatic] 8 | internal static Worker s_current; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /PowerThreadPool/Exceptions/WorkStopException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace PowerThreadPool.Exceptions 4 | { 5 | internal class WorkStopException : Exception 6 | { 7 | public WorkStopException() { } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /PowerThreadPool/Constants/CanGetWork.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.Constants 2 | { 3 | internal enum CanGetWork 4 | { 5 | Allowed = 0, 6 | NotAllowed = 1, 7 | ToBeDisabled = 2, 8 | Disabled = -1, 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /PowerThreadPool/Options/WorkOption.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.Options 2 | { 3 | public class WorkOption : WorkOption 4 | { 5 | internal static new WorkOption DefaultInstance { get; set; } = new WorkOption 6 | { 7 | IsDefaultInstance = true 8 | }; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /PowerThreadPool/Helpers/Asynchronous/ITaskCompletionSource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace PowerThreadPool.Helpers.Asynchronous 5 | { 6 | internal interface ITaskCompletionSource 7 | { 8 | Task Task { get; } 9 | void SetResult(object result); 10 | void SetException(Exception ex); 11 | void SetCanceled(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /PowerThreadPool/Exceptions/WorkExceptionBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using PowerThreadPool.Works; 3 | 4 | namespace PowerThreadPool.Exceptions 5 | { 6 | public class WorkExceptionBase : Exception 7 | { 8 | public WorkExceptionBase() { } 9 | 10 | /// 11 | /// work id 12 | /// 13 | public WorkID ID { get; internal set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /PowerThreadPool/EventArguments/WorkRejectedEventArgs.cs: -------------------------------------------------------------------------------- 1 | using PowerThreadPool.Options; 2 | 3 | namespace PowerThreadPool.EventArguments 4 | { 5 | public class WorkRejectedEventArgs : WorkEventArgsBase 6 | { 7 | public WorkRejectedEventArgs(RejectType rejectType) 8 | { 9 | RejectType = rejectType; 10 | } 11 | 12 | public RejectType RejectType { get; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /PowerThreadPool/EventArguments/WorkStoppedEventArgs.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.EventArguments 2 | { 3 | public class WorkStoppedEventArgs : WorkEventArgsBase 4 | { 5 | public WorkStoppedEventArgs() { } 6 | 7 | /// 8 | /// Indicating whether the work was stopped forcefully. 9 | /// 10 | public bool ForceStop { get; internal set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /PowerThreadPool/EventArguments/WorkDiscardedEventArgs.cs: -------------------------------------------------------------------------------- 1 | using PowerThreadPool.Options; 2 | 3 | namespace PowerThreadPool.EventArguments 4 | { 5 | public class WorkDiscardedEventArgs : WorkEventArgsBase 6 | { 7 | public WorkDiscardedEventArgs(RejectType rejectType) 8 | { 9 | RejectType = rejectType; 10 | } 11 | 12 | public RejectType RejectType { get; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /PowerThreadPoolTest/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Configuration; 4 | using System.Data; 5 | using System.Linq; 6 | using System.Threading.Tasks; 7 | using System.Windows; 8 | 9 | namespace PowerThreadPoolTest 10 | { 11 | /// 12 | /// Interaction logic for App.xaml 13 | /// 14 | public partial class App : Application 15 | { 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /PowerThreadPoolTest/App.xaml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /coverage.runsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | cobertura 8 | **/Deque/*.cs 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /PowerThreadPool/Options/TimeoutOption.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.Options 2 | { 3 | public class TimeoutOption 4 | { 5 | /// 6 | /// The maximum amount of time (ms). 7 | /// 8 | public int Duration { get; set; } 9 | 10 | /// 11 | /// If forceStop is true, Thread.Interrupt() will be called. 12 | /// 13 | public bool ForceStop { get; set; } = false; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /PowerThreadPool/Collections/Comparer/DescendingIntComparer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace PowerThreadPool.Collections.Comparer 4 | { 5 | internal class DescendingIntComparer : IComparer 6 | { 7 | internal static DescendingIntComparer Instance { get; } = new DescendingIntComparer(); 8 | private DescendingIntComparer() { } 9 | 10 | public int Compare(int x, int y) 11 | { 12 | return y.CompareTo(x); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Benchmark/Program.cs: -------------------------------------------------------------------------------- 1 | using BenchmarkDotNet.Running; 2 | 3 | namespace Benchmark 4 | { 5 | internal class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | BenchmarkRunner.Run(); 10 | BenchmarkRunner.Run(); 11 | BenchmarkRunner.Run(); 12 | BenchmarkRunner.Run(); 13 | Console.WriteLine("OK"); 14 | Console.ReadLine(); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /PowerThreadPool/Works/AsyncWorkInfo.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.Works 2 | { 3 | internal sealed class AsyncWorkInfo 4 | { 5 | internal WorkID AsyncWorkID { get; set; } 6 | 7 | internal WorkID BaseAsyncWorkID { get; set; } 8 | 9 | internal bool AllowEventsAndCallback { get; set; } = true; 10 | 11 | private volatile bool _asyncDone; 12 | internal bool AsyncDone 13 | { 14 | get => _asyncDone; 15 | set => _asyncDone = value; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /PowerThreadPool/Options/RunningTimerOption.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using PowerThreadPool.EventArguments; 3 | 4 | namespace PowerThreadPool.Options 5 | { 6 | public class RunningTimerOption 7 | { 8 | /// 9 | /// The time, in milliseconds, between events. 10 | /// 11 | public double Interval { get; set; } = 1000; 12 | 13 | /// 14 | /// Occurs when the interval elapses, but only if the thread pool is in the Running state. 15 | /// 16 | public Action Elapsed { get; set; } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: "[BUG]" 5 | labels: bug 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 16 | **Expected behavior** 17 | A clear and concise description of what you expected to happen. 18 | 19 | **Desktop (please complete the following information):** 20 | - OS: [e.g. iOS] 21 | - Version [e.g. v1.0.0] 22 | 23 | **Additional context** 24 | Add any other context about the problem here. 25 | -------------------------------------------------------------------------------- /PowerThreadPoolTest/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Windows; 2 | 3 | [assembly: ThemeInfo( 4 | ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located 5 | //(used if a resource is not found in the page, 6 | // or application resource dictionaries) 7 | ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located 8 | //(used if a resource is not found in the page, 9 | // app, or any theme specific resource dictionaries) 10 | )] 11 | -------------------------------------------------------------------------------- /PowerThreadPool/Works/WorkFunc.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using PowerThreadPool.Options; 3 | 4 | namespace PowerThreadPool.Works 5 | { 6 | internal class WorkFunc : Work 7 | { 8 | private Func _function; 9 | 10 | internal WorkFunc(PowerPool powerPool, WorkID id, Func function, WorkOption option, AsyncWorkInfo asyncWorkInfo) : base(powerPool, id, option, asyncWorkInfo) 11 | { 12 | _function = function; 13 | } 14 | 15 | internal override object Execute() 16 | { 17 | ++_executeCount; 18 | return _function(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /PowerThreadPool/Options/RejectOption.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.Options 2 | { 3 | public enum RejectType 4 | { 5 | AbortPolicy, 6 | CallerRunsPolicy, 7 | DiscardPolicy, 8 | DiscardOldestPolicy, 9 | } 10 | 11 | public class RejectOption 12 | { 13 | /// 14 | /// Thread queue limit. 15 | /// If the queue is full, the reject policy will be used. 16 | /// 17 | public int ThreadQueueLimit { get; set; } 18 | 19 | /// 20 | /// Reject type. 21 | /// 22 | public RejectType RejectType { get; set; } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /PowerThreadPool/Works/WorkAction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using PowerThreadPool.Options; 3 | 4 | namespace PowerThreadPool.Works 5 | { 6 | internal class WorkAction : Work 7 | { 8 | private Action _action; 9 | 10 | internal WorkAction(PowerPool powerPool, WorkID id, Action action, WorkOption option, AsyncWorkInfo asyncWorkInfo) : base(powerPool, id, option, asyncWorkInfo) 11 | { 12 | _action = action; 13 | } 14 | 15 | internal override object Execute() 16 | { 17 | ++_executeCount; 18 | _action(); 19 | return null; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /PowerThreadPool/Helpers/Asynchronous/TaskCompletionSourceBox.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace PowerThreadPool.Helpers.Asynchronous 5 | { 6 | internal class TaskCompletionSourceBox : ITaskCompletionSource 7 | { 8 | private readonly TaskCompletionSource _tcs = new TaskCompletionSource(); 9 | 10 | public Task Task => _tcs.Task; 11 | public void SetResult(object result) => _tcs.SetResult((T)result); 12 | public void SetException(Exception ex) => _tcs.SetException(ex); 13 | public void SetCanceled() => _tcs.SetCanceled(); 14 | 15 | public Task TypedTask => _tcs.Task; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: enhancement 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context about the feature request here. 21 | -------------------------------------------------------------------------------- /PowerThreadPoolTest/PowerThreadPoolTest.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | WinExe 5 | net8.0-windows7.0 6 | enable 7 | true 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /PowerThreadPool/Works/WorkIDGuid.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace PowerThreadPool.Works 4 | { 5 | public sealed class WorkIDGuid : 6 | WorkID 7 | { 8 | private readonly Guid _guid; 9 | private readonly int _hash; 10 | 11 | internal WorkIDGuid(Guid g) 12 | { 13 | _guid = g; 14 | _hash = ComputeHash(WorkIdKind.Guid, 0L, g, null); 15 | } 16 | 17 | internal override WorkIdKind Kind => WorkIdKind.Guid; 18 | 19 | internal override long Long => default; 20 | 21 | internal override Guid Guid => _guid; 22 | 23 | internal override string String => default; 24 | 25 | internal override int Hash => _hash; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /PowerThreadPool/Works/WorkIDLong.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace PowerThreadPool.Works 4 | { 5 | public sealed class WorkIDLong : 6 | WorkID 7 | { 8 | private readonly long _long; 9 | private readonly int _hash; 10 | 11 | internal WorkIDLong(long l) 12 | { 13 | _long = l; 14 | _hash = ComputeHash(WorkIdKind.Long, l, default, null); 15 | } 16 | 17 | internal override WorkIdKind Kind => WorkIdKind.Long; 18 | 19 | internal override long Long => _long; 20 | 21 | internal override Guid Guid => default; 22 | 23 | internal override string String => default; 24 | 25 | internal override int Hash => _hash; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /PowerThreadPool/Works/WorkIDString.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace PowerThreadPool.Works 4 | { 5 | public sealed class WorkIDString : 6 | WorkID 7 | { 8 | private readonly string _string; 9 | private readonly int _hash; 10 | 11 | internal WorkIDString(string s) 12 | { 13 | _string = s; 14 | _hash = ComputeHash(WorkIdKind.String, 0L, default, s); 15 | } 16 | 17 | internal override WorkIdKind Kind => WorkIdKind.String; 18 | 19 | internal override long Long => default; 20 | 21 | internal override Guid Guid => default; 22 | 23 | internal override string String => _string; 24 | 25 | internal override int Hash => _hash; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Deque/Deque.projitems: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath) 5 | true 6 | 70448bca-9802-46c0-9aa8-17f518a8e7b2 7 | 8 | 9 | Deque 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /PowerThreadPool/EventArguments/RunningTimerElapsedEventArgs.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace PowerThreadPool.EventArguments 4 | { 5 | public class RunningTimerElapsedEventArgs 6 | { 7 | public RunningTimerElapsedEventArgs() { } 8 | 9 | private DateTime _signalTime; 10 | /// 11 | /// The date/time when the System.Timers.Timer.Elapsed event was raised. 12 | /// 13 | public DateTime SignalTime 14 | { 15 | get => _signalTime.ToLocalTime(); 16 | internal set => _signalTime = value; 17 | } 18 | 19 | /// 20 | /// Pool runtime duration. 21 | /// 22 | public TimeSpan RuntimeDuration { get; internal set; } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Benchmark/Benchmark.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | enable 7 | disable 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /PowerThreadPool/EventArguments/RunningWorkerCountChangedEventArgs.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace PowerThreadPool.EventArguments 4 | { 5 | public class RunningWorkerCountChangedEventArgs : EventArgs 6 | { 7 | public RunningWorkerCountChangedEventArgs() { } 8 | 9 | private int _previousCount = 0; 10 | /// 11 | /// previous count. 12 | /// 13 | public int PreviousCount 14 | { 15 | get => _previousCount; 16 | internal set => _previousCount = value; 17 | } 18 | 19 | private int _nowCount = 0; 20 | /// 21 | /// now count. 22 | /// 23 | public int NowCount 24 | { 25 | get => _nowCount; 26 | internal set 27 | { 28 | _nowCount = value; 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /PowerThreadPool/Options/RetryOption.cs: -------------------------------------------------------------------------------- 1 | namespace PowerThreadPool.Options 2 | { 3 | public enum RetryBehavior 4 | { 5 | ImmediateRetry, 6 | Requeue 7 | } 8 | 9 | public enum RetryPolicy 10 | { 11 | Limited, 12 | Unlimited 13 | } 14 | 15 | public class RetryOption 16 | { 17 | /// 18 | /// ImmediateRetry or Requeue. 19 | /// 20 | public RetryBehavior RetryBehavior { get; set; } = RetryBehavior.ImmediateRetry; 21 | 22 | /// 23 | /// Unlimited or Limited. 24 | /// 25 | public RetryPolicy RetryPolicy { get; set; } = RetryPolicy.Limited; 26 | 27 | /// 28 | /// Max retry count. 29 | /// Enable if RetryPolicy is Limited. 30 | /// 31 | public int MaxRetryCount { get; set; } = 3; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /PowerThreadPool/Helpers/ConcurrentStealablePriorityCollectionHelper.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace PowerThreadPool.Helpers 4 | { 5 | internal class ConcurrentStealablePriorityCollectionHelper 6 | { 7 | internal static List InsertPriorityDescending(List oldList, int priority) 8 | { 9 | var newList = new List(oldList.Count + 1); 10 | bool inserted = false; 11 | for (int i = 0; i < oldList.Count; ++i) 12 | { 13 | int p = oldList[i]; 14 | if (!inserted && priority > p) 15 | { 16 | newList.Add(priority); 17 | inserted = true; 18 | } 19 | newList.Add(p); 20 | } 21 | if (!inserted) 22 | { 23 | newList.Add(priority); 24 | } 25 | return newList; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /PowerThreadPool/EventArguments/WorkEndedEventArgs.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using PowerThreadPool.Results; 3 | 4 | namespace PowerThreadPool.EventArguments 5 | { 6 | public class WorkEndedEventArgs : WorkEventArgsBase 7 | { 8 | public WorkEndedEventArgs() { } 9 | 10 | /// 11 | /// The result of the work. 12 | /// 13 | public object Result { get; internal set; } 14 | 15 | /// 16 | /// Indicates whether the work was successful. 17 | /// 18 | public bool Succeed { get; internal set; } 19 | 20 | /// 21 | /// The exception that occurred if the work failed due to an uncaught exception. 22 | /// 23 | public Exception Exception { get; internal set; } 24 | 25 | /// 26 | /// The retry information of the work. 27 | /// 28 | public RetryInfo RetryInfo { get; internal set; } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Supported Versions 4 | 5 | Upgrading to the latest version is always recommended. Fixes for previous vulnerabilities will be applied to the new version. 6 | 7 | | Version | Supported | 8 | | ------- | ------------------ | 9 | | Latest | :white_check_mark: | 10 | | Older | :x: | 11 | 12 | ### How to Report 13 | - **Issue Tracker**: Open an [issue](https://github.com/ZjzMisaka/PowerThreadPool/issues). 14 | - **Email**: Send an email to [zjzmisaka@outlook.com](mailto:zjzmisaka@outlook.com). 15 | - **Discord**: Join our [discord](https://discord.gg/drhvZqvXTd). 16 | 17 | ### What to Expect After Reporting 18 | 19 | 1. **Acknowledgement**: An acknowledgement of the report will be sent within three days of receiving it. 20 | 2. **Assessment**: An initial assessment will be conducted to determine the severity and scope of the vulnerability. 21 | 3. **Collaboration**: Assistance in submitting patches or fixes for the vulnerability is encouraged and appreciated. 22 | -------------------------------------------------------------------------------- /PowerThreadPool/EventArguments/PoolIdledEventArgs.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace PowerThreadPool.EventArguments 4 | { 5 | public class PoolIdledEventArgs : EventArgs 6 | { 7 | public PoolIdledEventArgs() { } 8 | 9 | private DateTime _startDateTime; 10 | /// 11 | /// Start datetime. 12 | /// 13 | public DateTime StartDateTime 14 | { 15 | get => _startDateTime.ToLocalTime(); 16 | internal set => _startDateTime = value; 17 | } 18 | 19 | private DateTime _endDateTime; 20 | /// 21 | /// End datetime. 22 | /// 23 | public DateTime EndDateTime 24 | { 25 | get => _endDateTime.ToLocalTime(); 26 | internal set => _endDateTime = value; 27 | } 28 | 29 | /// 30 | /// Runtime duration of the thread pool. 31 | /// 32 | public TimeSpan RuntimeDuration => EndDateTime - StartDateTime; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Deque/Deque.shproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 70448bca-9802-46c0-9aa8-17f518a8e7b2 5 | 14.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Zhang Junzhi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /PowerThreadPool/Results/RetryInfo.cs: -------------------------------------------------------------------------------- 1 | using PowerThreadPool.Options; 2 | 3 | namespace PowerThreadPool.Results 4 | { 5 | public class RetryInfo 6 | { 7 | /// 8 | /// Current retry count. 9 | /// 10 | public int CurrentRetryCount { get; internal set; } 11 | 12 | /// 13 | /// Max retry count. 14 | /// Same as RetryOption.MaxRetryCount. 15 | /// 16 | public int MaxRetryCount { get; internal set; } 17 | 18 | /// 19 | /// Unlimited or Limited. 20 | /// Same as RetryOption.RetryPolicy. 21 | /// 22 | public RetryPolicy RetryPolicy { get; internal set; } 23 | 24 | private bool _stopRetry = false; 25 | /// 26 | /// If set to true, even if the retry count is not full or unlimited, subsequent retries will be aborted. 27 | /// 28 | public bool StopRetry 29 | { 30 | get 31 | { 32 | return _stopRetry; 33 | } 34 | set 35 | { 36 | if (value == true) 37 | { 38 | _stopRetry = value; 39 | } 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /PowerThreadPoolTest/MainWindow.xaml: -------------------------------------------------------------------------------- 1 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 |