├── Tests ├── Usings.cs ├── Utils.cs ├── Tests.csproj ├── SerialQueue.cs └── SerialQueueTasks.cs ├── .editorconfig ├── Benchmark ├── Benchmark.csproj ├── SerialQueueTasksSemaphoreSlim.cs ├── SerialQueueMonitor.cs ├── SerialQueueTasksTplDataflow.cs ├── SerialQueueTasksMonitor.cs ├── SerialQueueSpinLock.cs ├── SerialQueueTasksSpinLock.cs └── Program.cs ├── SerialQueue ├── SerialQueue.csproj ├── SerialQueue.cs └── SerialQueueTasks.cs ├── LICENSE ├── SerialQueue.sln ├── README.md └── .gitignore /Tests/Usings.cs: -------------------------------------------------------------------------------- 1 | global using NUnit.Framework; 2 | -------------------------------------------------------------------------------- /Tests/Utils.cs: -------------------------------------------------------------------------------- 1 | namespace Tests 2 | { 3 | public static class TestUtils 4 | { 5 | public static Task RandomDelay(int first = 0, int second = 1) 6 | { 7 | return Task.Delay(Random.Shared.Next() % 2 == 0 ? first : second); 8 | } 9 | } 10 | } 11 | 12 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | [*.cs] 2 | 3 | # CS4014: Because this call is not awaited, execution of the current method continues before the call is completed 4 | dotnet_diagnostic.CS4014.severity = silent 5 | 6 | # Default severity for analyzer diagnostics with category 'Assertion' 7 | dotnet_analyzer_diagnostic.category-Assertion.severity = none 8 | -------------------------------------------------------------------------------- /Benchmark/Benchmark.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net7.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /Benchmark/SerialQueueTasksSemaphoreSlim.cs: -------------------------------------------------------------------------------- 1 | namespace Threading 2 | { 3 | public class SerialQueueTasksSemaphoreSlim 4 | { 5 | SemaphoreSlim _semaphore = new SemaphoreSlim(1); 6 | 7 | public async Task Enqueue(Action action) 8 | { 9 | await Enqueue(() => { 10 | action(); 11 | return true; 12 | }); 13 | } 14 | 15 | public async Task Enqueue(Func function) 16 | { 17 | await _semaphore.WaitAsync(); 18 | try 19 | { 20 | return function(); 21 | } 22 | finally 23 | { 24 | _semaphore.Release(); 25 | } 26 | } 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /SerialQueue/SerialQueue.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net7.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | 6 11 | 6 12 | 13 | 14 | 15 | TRACE;RELEASE;NET;NET6_0;NETCOREAPP 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /Tests/Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net7.0 5 | enable 6 | enable 7 | 8 | false 9 | true 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 gentlee (Alexander Danilov) 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. 22 | -------------------------------------------------------------------------------- /Tests/SerialQueue.cs: -------------------------------------------------------------------------------- 1 | using Threading; 2 | 3 | namespace Tests 4 | { 5 | [TestFixture] 6 | public class SerialQueueTests 7 | { 8 | [Test] 9 | public void DispatchAsyncFromSingleThread() 10 | { 11 | // Assign 12 | 13 | const int count = 100000; 14 | var queue = new SerialQueue(); 15 | var list = new List(); 16 | var range = Enumerable.Range(0, count); 17 | 18 | // Act 19 | 20 | foreach (var number in range) 21 | { 22 | queue.DispatchAsync(() => list.Add(number)); 23 | } 24 | 25 | queue.DispatchSync(() => { }); 26 | 27 | // Assert 28 | 29 | Assert.True(range.SequenceEqual(list)); 30 | } 31 | 32 | [Test] 33 | public async Task DispatchAsyncFromMultipleThreads() 34 | { 35 | // Assign 36 | 37 | const int count = 100000; 38 | var counter = -123; 39 | var queue = new SerialQueue(); 40 | var list = new List(count); 41 | var tasks = new List(count); 42 | 43 | // Act 44 | 45 | queue.DispatchSync(() => 46 | { 47 | counter = 0; 48 | }); 49 | 50 | for (int i = 0; i < count; i += 1) 51 | { 52 | tasks.Add(Task.Run(() => 53 | { 54 | queue.DispatchAsync(() => 55 | { 56 | list.Add(counter); 57 | counter += 1; 58 | }); 59 | })); 60 | } 61 | 62 | await Task.WhenAll(tasks); 63 | 64 | queue.DispatchSync(() => 65 | { 66 | counter *= 2; 67 | }); 68 | 69 | // Assert 70 | 71 | Assert.AreEqual(count * 2, counter); 72 | Assert.True(list.SequenceEqual(Enumerable.Range(0, count))); 73 | } 74 | } 75 | } 76 | 77 | -------------------------------------------------------------------------------- /SerialQueue.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 25.0.1706.8 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SerialQueue", "SerialQueue\SerialQueue.csproj", "{FEF6A814-C39D-4F14-BA6E-8826E0DD0A19}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{14BB4400-87F5-4FDA-8BF1-1D925154796C}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Benchmark", "Benchmark\Benchmark.csproj", "{9611DBBF-0C76-402C-A125-49FCB49920E5}" 11 | EndProject 12 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{D0276F3C-CEF4-4278-9E81-E19360F73A8A}" 13 | ProjectSection(SolutionItems) = preProject 14 | .editorconfig = .editorconfig 15 | EndProjectSection 16 | EndProject 17 | Global 18 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 19 | Debug|Any CPU = Debug|Any CPU 20 | Release|Any CPU = Release|Any CPU 21 | EndGlobalSection 22 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 23 | {FEF6A814-C39D-4F14-BA6E-8826E0DD0A19}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 24 | {FEF6A814-C39D-4F14-BA6E-8826E0DD0A19}.Debug|Any CPU.Build.0 = Debug|Any CPU 25 | {FEF6A814-C39D-4F14-BA6E-8826E0DD0A19}.Release|Any CPU.ActiveCfg = Release|Any CPU 26 | {FEF6A814-C39D-4F14-BA6E-8826E0DD0A19}.Release|Any CPU.Build.0 = Release|Any CPU 27 | {14BB4400-87F5-4FDA-8BF1-1D925154796C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 28 | {14BB4400-87F5-4FDA-8BF1-1D925154796C}.Debug|Any CPU.Build.0 = Debug|Any CPU 29 | {14BB4400-87F5-4FDA-8BF1-1D925154796C}.Release|Any CPU.ActiveCfg = Release|Any CPU 30 | {14BB4400-87F5-4FDA-8BF1-1D925154796C}.Release|Any CPU.Build.0 = Release|Any CPU 31 | {9611DBBF-0C76-402C-A125-49FCB49920E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 32 | {9611DBBF-0C76-402C-A125-49FCB49920E5}.Debug|Any CPU.Build.0 = Debug|Any CPU 33 | {9611DBBF-0C76-402C-A125-49FCB49920E5}.Release|Any CPU.ActiveCfg = Release|Any CPU 34 | {9611DBBF-0C76-402C-A125-49FCB49920E5}.Release|Any CPU.Build.0 = Release|Any CPU 35 | EndGlobalSection 36 | GlobalSection(SolutionProperties) = preSolution 37 | HideSolutionNode = FALSE 38 | EndGlobalSection 39 | GlobalSection(ExtensibilityGlobals) = postSolution 40 | SolutionGuid = {352439E9-A318-4ABF-A835-6CEAB48B5C00} 41 | EndGlobalSection 42 | GlobalSection(MonoDevelopProperties) = preSolution 43 | Policies = $0 44 | $0.TextStylePolicy = $1 45 | $1.FileWidth = 100 46 | $1.TabsToSpaces = True 47 | $1.EolMarker = Unix 48 | $1.scope = text/x-csharp 49 | $0.CSharpFormattingPolicy = $2 50 | $2.scope = text/x-csharp 51 | EndGlobalSection 52 | EndGlobal 53 | -------------------------------------------------------------------------------- /Benchmark/SerialQueueMonitor.cs: -------------------------------------------------------------------------------- 1 | namespace Threading 2 | { 3 | public class SerialQueueMonitor 4 | { 5 | class LinkedListNode 6 | { 7 | public readonly Action Action; 8 | public LinkedListNode? Next; 9 | 10 | public LinkedListNode(Action action) 11 | { 12 | Action = action; 13 | } 14 | } 15 | 16 | public event Action UnhandledException = delegate { }; 17 | 18 | private LinkedListNode? _queueFirst; 19 | private LinkedListNode? _queueLast; 20 | private bool _isRunning = false; 21 | 22 | public void DispatchSync(Action action) 23 | { 24 | var mre = new ManualResetEvent(false); 25 | DispatchAsync(() => 26 | { 27 | action(); 28 | mre.Set(); 29 | }); 30 | mre.WaitOne(); 31 | } 32 | 33 | public void DispatchAsync(Action action) 34 | { 35 | var newNode = new LinkedListNode(action); 36 | 37 | lock (this) 38 | { 39 | if (_queueFirst == null) 40 | { 41 | _queueFirst = newNode; 42 | _queueLast = newNode; 43 | 44 | if (!_isRunning) 45 | { 46 | _isRunning = true; 47 | ThreadPool.QueueUserWorkItem(Run); 48 | } 49 | } 50 | else 51 | { 52 | _queueLast!.Next = newNode; 53 | _queueLast = newNode; 54 | } 55 | } 56 | } 57 | 58 | private void Run(object? _) 59 | { 60 | while (true) 61 | { 62 | LinkedListNode? firstNode; 63 | 64 | lock(this) 65 | { 66 | if (_queueFirst == null) 67 | { 68 | _isRunning = false; 69 | return; 70 | } 71 | firstNode = _queueFirst; 72 | _queueFirst = null; 73 | _queueLast = null; 74 | } 75 | 76 | while (firstNode != null) 77 | { 78 | var action = firstNode.Action; 79 | firstNode = firstNode.Next; 80 | try 81 | { 82 | action(); 83 | } 84 | catch (Exception error) 85 | { 86 | UnhandledException.Invoke(action, error); 87 | } 88 | } 89 | } 90 | } 91 | } 92 | } 93 | 94 | -------------------------------------------------------------------------------- /Benchmark/SerialQueueTasksTplDataflow.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks.Dataflow; 3 | 4 | namespace Threading.Tasks 5 | { 6 | public class SerialQueueTplDataflow 7 | { 8 | ActionBlock _actionBlock = new ActionBlock( 9 | async action => 10 | { 11 | if (action is Action) 12 | { 13 | (action as Action)!(); 14 | } 15 | else 16 | { 17 | await (action as Func)!(); 18 | } 19 | } 20 | ); 21 | 22 | public Task Enqueue(Action action) 23 | { 24 | var tsc = new TaskCompletionSource(); 25 | _actionBlock.SendAsync(() => 26 | { 27 | try 28 | { 29 | action(); 30 | tsc.SetResult(); 31 | } 32 | catch (Exception e) 33 | { 34 | tsc.SetException(e); 35 | } 36 | }); 37 | return tsc.Task; 38 | } 39 | 40 | public Task Enqueue(Func asyncAction) 41 | { 42 | var tsc = new TaskCompletionSource(); 43 | _actionBlock.SendAsync(async () => 44 | { 45 | try 46 | { 47 | await asyncAction(); 48 | tsc.SetResult(); 49 | } 50 | catch (Exception e) 51 | { 52 | tsc.SetException(e); 53 | } 54 | }); 55 | return tsc.Task; 56 | } 57 | 58 | public Task Enqueue(Func function) 59 | { 60 | var tsc = new TaskCompletionSource(); 61 | _actionBlock.SendAsync(() => 62 | { 63 | try 64 | { 65 | var result = function(); 66 | tsc.SetResult(result); 67 | } 68 | catch (Exception e) 69 | { 70 | tsc.SetException(e); 71 | } 72 | }); 73 | return tsc.Task; 74 | } 75 | 76 | public Task Enqueue(Func> asyncFunction) 77 | { 78 | var tsc = new TaskCompletionSource(); 79 | _actionBlock.SendAsync(async () => 80 | { 81 | try 82 | { 83 | var result = await asyncFunction(); 84 | tsc.SetResult(result); 85 | } 86 | catch (Exception e) 87 | { 88 | tsc.SetException(e); 89 | } 90 | }); 91 | return tsc.Task; 92 | } 93 | } 94 | } 95 | 96 | -------------------------------------------------------------------------------- /Benchmark/SerialQueueTasksMonitor.cs: -------------------------------------------------------------------------------- 1 | namespace Threading 2 | { 3 | public class SerialQueueTasksMonitor 4 | { 5 | readonly WeakReference _lastTask = new(null); 6 | 7 | public Task Enqueue(Action action) 8 | { 9 | lock (this) 10 | { 11 | Task? lastTask; 12 | Task resultTask; 13 | 14 | if (_lastTask.TryGetTarget(out lastTask)) 15 | { 16 | resultTask = lastTask.ContinueWith(_ => action(), TaskContinuationOptions.ExecuteSynchronously); 17 | } 18 | else 19 | { 20 | resultTask = Task.Run(action); 21 | } 22 | 23 | _lastTask.SetTarget(resultTask); 24 | 25 | return resultTask; 26 | } 27 | } 28 | 29 | public Task Enqueue(Func function) 30 | { 31 | lock (this) 32 | { 33 | Task? lastTask; 34 | Task resultTask; 35 | 36 | if (_lastTask.TryGetTarget(out lastTask)) 37 | { 38 | resultTask = lastTask.ContinueWith(_ => function(), TaskContinuationOptions.ExecuteSynchronously); 39 | } 40 | else 41 | { 42 | resultTask = Task.Run(function); 43 | } 44 | 45 | _lastTask.SetTarget(resultTask); 46 | 47 | return resultTask; 48 | } 49 | } 50 | 51 | public Task Enqueue(Func asyncAction) 52 | { 53 | lock (this) 54 | { 55 | Task? lastTask; 56 | Task resultTask; 57 | 58 | if (_lastTask.TryGetTarget(out lastTask)) 59 | { 60 | resultTask = lastTask.ContinueWith(_ => asyncAction(), TaskContinuationOptions.ExecuteSynchronously).Unwrap(); 61 | } 62 | else 63 | { 64 | resultTask = Task.Run(asyncAction); 65 | } 66 | 67 | _lastTask.SetTarget(resultTask); 68 | 69 | return resultTask; 70 | } 71 | } 72 | 73 | public Task Enqueue(Func> asyncFunction) 74 | { 75 | lock (this) 76 | { 77 | Task? lastTask; 78 | Task resultTask; 79 | 80 | if (_lastTask.TryGetTarget(out lastTask)) 81 | { 82 | resultTask = lastTask.ContinueWith(_ => asyncFunction(), TaskContinuationOptions.ExecuteSynchronously).Unwrap(); 83 | } 84 | else 85 | { 86 | resultTask = Task.Run(asyncFunction); 87 | } 88 | 89 | _lastTask.SetTarget(resultTask); 90 | 91 | return resultTask; 92 | } 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /SerialQueue/SerialQueue.cs: -------------------------------------------------------------------------------- 1 | namespace Threading 2 | { 3 | public class SerialQueue 4 | { 5 | class LinkedListNode 6 | { 7 | public readonly Action Action; 8 | public LinkedListNode? Next; 9 | 10 | public LinkedListNode(Action action) 11 | { 12 | Action = action; 13 | } 14 | } 15 | 16 | public event Action UnhandledException = delegate { }; 17 | 18 | private LinkedListNode? _queueFirst; 19 | private LinkedListNode? _queueLast; 20 | private bool _isRunning = false; 21 | 22 | /// 23 | /// Not recommended, use DispatchAsync instead. Queue is made to be used used asynchronously. 24 | /// 25 | public void DispatchSync(Action action) 26 | { 27 | var mre = new ManualResetEvent(false); 28 | DispatchAsync(() => 29 | { 30 | try 31 | { 32 | action(); 33 | } 34 | finally 35 | { 36 | mre.Set(); 37 | } 38 | }); 39 | mre.WaitOne(); 40 | } 41 | 42 | public void DispatchAsync(Action action) 43 | { 44 | var newNode = new LinkedListNode(action); 45 | 46 | lock (this) 47 | { 48 | if (_queueFirst == null) 49 | { 50 | _queueFirst = newNode; 51 | _queueLast = newNode; 52 | 53 | if (!_isRunning) 54 | { 55 | _isRunning = true; 56 | ThreadPool.QueueUserWorkItem(Run); 57 | } 58 | } 59 | else 60 | { 61 | _queueLast!.Next = newNode; 62 | _queueLast = newNode; 63 | } 64 | } 65 | } 66 | 67 | private void Run(object? _) 68 | { 69 | while (true) 70 | { 71 | LinkedListNode? firstNode; 72 | 73 | lock (this) 74 | { 75 | if (_queueFirst == null) 76 | { 77 | _isRunning = false; 78 | return; 79 | } 80 | firstNode = _queueFirst; 81 | _queueFirst = null; 82 | _queueLast = null; 83 | } 84 | 85 | while (firstNode != null) 86 | { 87 | var action = firstNode.Action; 88 | firstNode = firstNode.Next; 89 | try 90 | { 91 | action(); 92 | } 93 | catch (Exception error) 94 | { 95 | UnhandledException.Invoke(action, error); 96 | } 97 | } 98 | } 99 | } 100 | } 101 | } 102 | 103 | -------------------------------------------------------------------------------- /Benchmark/SerialQueueSpinLock.cs: -------------------------------------------------------------------------------- 1 | namespace Threading 2 | { 3 | public class SerialQueueSpinlock 4 | { 5 | class LinkedListNode 6 | { 7 | public readonly Action Action; 8 | public LinkedListNode? Next; 9 | 10 | public LinkedListNode(Action action) 11 | { 12 | Action = action; 13 | } 14 | } 15 | 16 | public event Action UnhandledException = delegate { }; 17 | 18 | private SpinLock _spinLock = new(false); 19 | private LinkedListNode? _queueFirst; 20 | private LinkedListNode? _queueLast; 21 | private bool _isRunning = false; 22 | 23 | public void DispatchSync(Action action) 24 | { 25 | var mre = new ManualResetEvent(false); 26 | DispatchAsync(() => 27 | { 28 | action(); 29 | mre.Set(); 30 | }); 31 | mre.WaitOne(); 32 | } 33 | 34 | public void DispatchAsync(Action action) 35 | { 36 | var newNode = new LinkedListNode(action); 37 | 38 | bool lockTaken = false; 39 | try 40 | { 41 | _spinLock.Enter(ref lockTaken); 42 | 43 | if (_queueFirst == null) 44 | { 45 | _queueFirst = newNode; 46 | _queueLast = newNode; 47 | 48 | if (!_isRunning) 49 | { 50 | _isRunning = true; 51 | ThreadPool.QueueUserWorkItem(Run); 52 | } 53 | } 54 | else 55 | { 56 | _queueLast!.Next = newNode; 57 | _queueLast = newNode; 58 | } 59 | } 60 | finally 61 | { 62 | if (lockTaken) _spinLock.Exit(false); 63 | } 64 | } 65 | 66 | private void Run(object? _) 67 | { 68 | while (true) 69 | { 70 | LinkedListNode? firstNode; 71 | 72 | bool lockTaken = false; 73 | try 74 | { 75 | _spinLock.Enter(ref lockTaken); 76 | if (_queueFirst == null) 77 | { 78 | _isRunning = false; 79 | return; 80 | } 81 | firstNode = _queueFirst; 82 | _queueFirst = null; 83 | _queueLast = null; 84 | } 85 | finally 86 | { 87 | if (lockTaken) _spinLock.Exit(false); 88 | } 89 | 90 | while (firstNode != null) 91 | { 92 | var action = firstNode.Action; 93 | firstNode = firstNode.Next; 94 | try 95 | { 96 | action(); 97 | } 98 | catch (Exception error) 99 | { 100 | UnhandledException.Invoke(action, error); 101 | } 102 | } 103 | } 104 | } 105 | } 106 | } 107 | 108 | -------------------------------------------------------------------------------- /SerialQueue/SerialQueueTasks.cs: -------------------------------------------------------------------------------- 1 | namespace Threading.Tasks 2 | { 3 | public class SerialQueue 4 | { 5 | SpinLock _spinLock = new(false); 6 | readonly WeakReference _lastTask = new(null); 7 | 8 | public Task Enqueue(Action action) 9 | { 10 | bool gotLock = false; 11 | try 12 | { 13 | Task? lastTask; 14 | Task resultTask; 15 | 16 | _spinLock.Enter(ref gotLock); 17 | 18 | if (_lastTask.TryGetTarget(out lastTask)) 19 | { 20 | resultTask = lastTask.ContinueWith(_ => action(), TaskContinuationOptions.ExecuteSynchronously); 21 | } 22 | else 23 | { 24 | resultTask = Task.Run(action); 25 | } 26 | 27 | _lastTask.SetTarget(resultTask); 28 | 29 | return resultTask; 30 | } 31 | finally 32 | { 33 | if (gotLock) _spinLock.Exit(false); 34 | } 35 | } 36 | 37 | public Task Enqueue(Func function) 38 | { 39 | bool gotLock = false; 40 | try 41 | { 42 | Task? lastTask; 43 | Task resultTask; 44 | 45 | _spinLock.Enter(ref gotLock); 46 | 47 | if (_lastTask.TryGetTarget(out lastTask)) 48 | { 49 | resultTask = lastTask.ContinueWith(_ => function(), TaskContinuationOptions.ExecuteSynchronously); 50 | } 51 | else 52 | { 53 | resultTask = Task.Run(function); 54 | } 55 | 56 | _lastTask.SetTarget(resultTask); 57 | 58 | return resultTask; 59 | } 60 | finally 61 | { 62 | if (gotLock) _spinLock.Exit(false); 63 | } 64 | } 65 | 66 | public Task Enqueue(Func asyncAction) 67 | { 68 | bool gotLock = false; 69 | try 70 | { 71 | Task? lastTask; 72 | Task resultTask; 73 | 74 | _spinLock.Enter(ref gotLock); 75 | 76 | if (_lastTask.TryGetTarget(out lastTask)) 77 | { 78 | resultTask = lastTask.ContinueWith(_ => asyncAction(), TaskContinuationOptions.ExecuteSynchronously).Unwrap(); 79 | } 80 | else 81 | { 82 | resultTask = Task.Run(asyncAction); 83 | } 84 | 85 | _lastTask.SetTarget(resultTask); 86 | 87 | return resultTask; 88 | } 89 | finally 90 | { 91 | if (gotLock) _spinLock.Exit(false); 92 | } 93 | } 94 | 95 | public Task Enqueue(Func> asyncFunction) 96 | { 97 | bool gotLock = false; 98 | try 99 | { 100 | Task? lastTask; 101 | Task resultTask; 102 | 103 | _spinLock.Enter(ref gotLock); 104 | 105 | if (_lastTask.TryGetTarget(out lastTask)) 106 | { 107 | resultTask = lastTask.ContinueWith(_ => asyncFunction(), TaskContinuationOptions.ExecuteSynchronously).Unwrap(); 108 | } 109 | else 110 | { 111 | resultTask = Task.Run(asyncFunction); 112 | } 113 | 114 | _lastTask.SetTarget(resultTask); 115 | 116 | return resultTask; 117 | } 118 | finally 119 | { 120 | if (gotLock) _spinLock.Exit(false); 121 | } 122 | } 123 | } 124 | } -------------------------------------------------------------------------------- /Benchmark/SerialQueueTasksSpinLock.cs: -------------------------------------------------------------------------------- 1 | namespace Threading 2 | { 3 | public class SerialQueueTasksSpinLock 4 | { 5 | SpinLock _spinLock = new(false); 6 | readonly WeakReference _lastTask = new(null); 7 | 8 | public Task Enqueue(Action action) 9 | { 10 | bool gotLock = false; 11 | try 12 | { 13 | Task? lastTask; 14 | Task resultTask; 15 | 16 | _spinLock.Enter(ref gotLock); 17 | 18 | if (_lastTask.TryGetTarget(out lastTask)) 19 | { 20 | resultTask = lastTask.ContinueWith(_ => action(), TaskContinuationOptions.ExecuteSynchronously); 21 | } 22 | else 23 | { 24 | resultTask = Task.Run(action); 25 | } 26 | 27 | _lastTask.SetTarget(resultTask); 28 | 29 | return resultTask; 30 | } 31 | finally 32 | { 33 | if (gotLock) _spinLock.Exit(false); 34 | } 35 | } 36 | 37 | public Task Enqueue(Func function) 38 | { 39 | bool gotLock = false; 40 | try 41 | { 42 | Task? lastTask; 43 | Task resultTask; 44 | 45 | _spinLock.Enter(ref gotLock); 46 | 47 | if (_lastTask.TryGetTarget(out lastTask)) 48 | { 49 | resultTask = lastTask.ContinueWith(_ => function(), TaskContinuationOptions.ExecuteSynchronously); 50 | } 51 | else 52 | { 53 | resultTask = Task.Run(function); 54 | } 55 | 56 | _lastTask.SetTarget(resultTask); 57 | 58 | return resultTask; 59 | } 60 | finally 61 | { 62 | if (gotLock) _spinLock.Exit(false); 63 | } 64 | } 65 | 66 | public Task Enqueue(Func asyncAction) 67 | { 68 | bool gotLock = false; 69 | try 70 | { 71 | Task? lastTask; 72 | Task resultTask; 73 | 74 | _spinLock.Enter(ref gotLock); 75 | 76 | if (_lastTask.TryGetTarget(out lastTask)) 77 | { 78 | resultTask = lastTask.ContinueWith(_ => asyncAction(), TaskContinuationOptions.ExecuteSynchronously).Unwrap(); 79 | } 80 | else 81 | { 82 | resultTask = Task.Run(asyncAction); 83 | } 84 | 85 | _lastTask.SetTarget(resultTask); 86 | 87 | return resultTask; 88 | } 89 | finally 90 | { 91 | if (gotLock) _spinLock.Exit(false); 92 | } 93 | } 94 | 95 | public Task Enqueue(Func> asyncFunction) 96 | { 97 | bool gotLock = false; 98 | try 99 | { 100 | Task? lastTask; 101 | Task resultTask; 102 | 103 | _spinLock.Enter(ref gotLock); 104 | 105 | if (_lastTask.TryGetTarget(out lastTask)) 106 | { 107 | resultTask = lastTask.ContinueWith(_ => asyncFunction(), TaskContinuationOptions.ExecuteSynchronously).Unwrap(); 108 | } 109 | else 110 | { 111 | resultTask = Task.Run(asyncFunction); 112 | } 113 | 114 | _lastTask.SetTarget(resultTask); 115 | 116 | return resultTask; 117 | } 118 | finally 119 | { 120 | if (gotLock) _spinLock.Exit(false); 121 | } 122 | } 123 | } 124 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | Donations 🙌 3 | BTC: bc1qs0sq7agz5j30qnqz9m60xj4tt8th6aazgw7kxr
4 | ETH: 0x1D834755b5e889703930AC9b784CB625B3cd833E
5 | USDT(Tron): TPrCq8LxGykQ4as3o1oB8V7x1w2YPU2o5n
6 | TON: EQAtBuFWI3H_LpHfEToil4iYemtfmyzlaJpahM3tFSoxojvV
7 | DOGE: D7GMQdKhKC9ymbT9PtcetSFTQjyPRRfkwT
8 |
9 | 10 | # SerialQueue 11 | 12 | Lightweight, high-performance C# implementations of FIFO serial queues from Apple's GCD, which are often much better to use for synchronization rather than locks - they don't block caller's thread, and rather than creating new threads - they use thread pool. 13 | 14 | Task-based implementation (recommended) is more simple and convenient, while non-task is faster (check benchmark results). 15 | 16 | Covered with tests. 17 | 18 | 👉 Read [my article](https://alexanderdanilov.dev/en/articles/serial-queues) about serial queues. 19 | 20 | ### Table of contents 21 | 22 | - [Interface](https://github.com/gentlee/SerialQueue#interface) 23 | - [Installation](https://github.com/gentlee/SerialQueue#installation) 24 | - [Examples](https://github.com/gentlee/SerialQueue#examples) 25 | - [Benchmark results](https://github.com/gentlee/SerialQueue#benchmark-results) 26 | - [Troubleshooting](https://github.com/gentlee/SerialQueue#troubleshooting) 27 | - [Deadlocks](https://github.com/gentlee/SerialQueue#deadlocks) 28 | 29 | ### Interface 30 | 31 | ```C# 32 | // Task based version (recommended) 33 | // SerialQueue/SerialQueueTasks.cs 34 | class SerialQueue { 35 | Task Enqueue(Action action); 36 | Task Enqueue(Func function); 37 | Task Enqueue(Func asyncAction); 38 | Task Enqueue(Func> asyncFunction); 39 | } 40 | 41 | // Lightweight version 42 | // SerialQueue/SerialQueue.cs 43 | class SerialQueue { 44 | void DispatchSync(Action action); 45 | void DispatchAsync(Action action); 46 | } 47 | ``` 48 | 49 | ### Installation 50 | 51 | Just copy the source code of `SerialQueue/SerialQueueTasks.cs` or `SerialQueue/SerialQueue.cs` file to your project. 52 | 53 | ### Examples 54 | 55 | Task-based queue usage: 56 | ```C# 57 | readonly SerialQueue queue = new SerialQueue(); 58 | 59 | async Task SomeAsyncMethod() 60 | { 61 | await queue.Enqueue(() => { 62 | // synchronized code 63 | }); 64 | } 65 | ``` 66 | 67 | Non-task based example: 68 | ```C# 69 | readonly SerialQueue queue = new SerialQueue(); 70 | 71 | void SomeAsyncMethod() 72 | { 73 | queue.DispatchAsync(() => { 74 | // synchronized code 75 | }); 76 | } 77 | ``` 78 | 79 | Previous examples do the same as the next one with Monitor (lock): 80 | 81 | ```C# 82 | readonly object locker = new object(); 83 | 84 | async Task SomeAsyncMethod() 85 | { 86 | lock(locker) { 87 | // synchronized code 88 | } 89 | } 90 | ``` 91 | 92 | But serial queues are **asynchronous**, **don't block callers threads** while waiting for synced operation to start, evaluate synced operations on **thread pool** and often **perform better**, especially for long synced operations. 93 | 94 | ### Benchmark results 95 | 96 |
97 | Chart 1: Approximate synchronization costs depending on the operation duration (smaller is better). 98 | 99 | ![chart-1](https://github.com/gentlee/SerialQueue/assets/2361140/bab377e6-15a2-4ed2-9db1-621243f30e5b) 100 | 101 |
102 | 103 |
104 | Chart 2: Zoomed in (smaller is better). 105 | 106 | ![chart-2](https://github.com/gentlee/SerialQueue/assets/2361140/a9b52ae0-a455-4e78-a721-81b3146c0db4) 107 | 108 |
109 | 110 |
111 | Chart 3: Zoomed in for the shortest operations (smaller is better). 112 | 113 | ![chart-3](https://github.com/gentlee/SerialQueue/assets/2361140/70e442a5-314a-42cc-9ab8-354b6514f6ae) 114 | 115 |
116 | 117 | - The X axis is the time of the operation to be synchronized, in milliseconds. 118 | - The Y axis shows approximate synchronization costs in processor ticks. 119 | 120 | Synchronization mechanisms: 121 | - **SpinLock**, **Monitor**, **Mutex** - standard synchronization primitives. 122 | - **SemaphoreSlim** is a simplified alternative to Semaphore. 123 | - **TPL Dataflow ActionBlock** - implementation of a queue using TPL Dataflow ActionBlock. 124 | - **SerialQueue (by @borland)** - queue [implementation](https://github.com/borland/SerialQueue) from user @borland. 125 | - **SerialQueue** is a lightweight serial queue implementation from **this repository**. 126 | - **SerialQueue** Tasks is a Task-based serial queue implementation from **this repository**. 127 | 128 | ### Troubleshooting 129 | 130 | #### Deadlocks 131 | 132 | Nesting and awaiting `queue.Enqueue` leads to deadlock in the queue: 133 | 134 | ```C# 135 | var queue = new SerialQueue(); 136 | 137 | await queue.Enqueue(async () => 138 | { 139 | await queue.Enqueue(async () => 140 | { 141 | // This code will never run because it waits until the first task executes, 142 | // and first task awaits while this one finishes. 143 | // Queue is locked. 144 | }); 145 | }); 146 | ``` 147 | This particular case can be fixed by either not awaiting nested Enqueue or not putting nested task to queue at all, because it is already in the queue. 148 | 149 | Overall it is better to implement code not synced first, but later sync it in the upper layer that uses that code, or in a synced wrapper: 150 | 151 | ```C# 152 | // Bad 153 | 154 | async Task Run() 155 | { 156 | await FunctionA(); 157 | await FunctionB(); 158 | await FunctionC(); // deadlock 159 | } 160 | 161 | async Task FunctionA() => await queue.Enqueue(async () => { ... }); 162 | 163 | async Task FunctionB() => await queue.Enqueue(async () => { ... }); 164 | 165 | async Task FunctionC() => await queue.Enqueue(async () => 166 | await FunctionA(); 167 | ... 168 | await FunctionB(); 169 | }); 170 | 171 | // Good 172 | 173 | async Task Run() 174 | { 175 | await queue.Enqueue(FunctionA); 176 | await queue.Enqueue(FunctionB); 177 | await queue.Enqueue(FunctionC); 178 | } 179 | 180 | async Task FunctionA() { ... }; 181 | 182 | async Task FunctionB() { ... }; 183 | 184 | async Task FunctionC() 185 | { 186 | await FunctionA(); 187 | ... 188 | await FunctionB(); 189 | }; 190 | ``` 191 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # globs 2 | Makefile.in 3 | *.userprefs 4 | *.usertasks 5 | config.make 6 | config.status 7 | aclocal.m4 8 | install-sh 9 | autom4te.cache/ 10 | *.tar.gz 11 | tarballs/ 12 | test-results/ 13 | 14 | # Mac bundle stuff 15 | *.dmg 16 | *.app 17 | 18 | # content below from: https://github.com/github/gitignore/blob/main/Global/macOS.gitignore 19 | # General 20 | .DS_Store 21 | .AppleDouble 22 | .LSOverride 23 | 24 | # Icon must end with two \r 25 | Icon 26 | 27 | 28 | # Thumbnails 29 | ._* 30 | 31 | # Files that might appear in the root of a volume 32 | .DocumentRevisions-V100 33 | .fseventsd 34 | .Spotlight-V100 35 | .TemporaryItems 36 | .Trashes 37 | .VolumeIcon.icns 38 | .com.apple.timemachine.donotpresent 39 | 40 | # Directories potentially created on remote AFP share 41 | .AppleDB 42 | .AppleDesktop 43 | Network Trash Folder 44 | Temporary Items 45 | .apdisk 46 | 47 | # content below from: https://github.com/github/gitignore/blob/main/Global/Windows.gitignore 48 | # Windows thumbnail cache files 49 | Thumbs.db 50 | ehthumbs.db 51 | ehthumbs_vista.db 52 | 53 | # Dump file 54 | *.stackdump 55 | 56 | # Folder config file 57 | [Dd]esktop.ini 58 | 59 | # Recycle Bin used on file shares 60 | $RECYCLE.BIN/ 61 | 62 | # Windows Installer files 63 | *.cab 64 | *.msi 65 | *.msix 66 | *.msm 67 | *.msp 68 | 69 | # Windows shortcuts 70 | *.lnk 71 | 72 | # content below from: https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 73 | ## Ignore Visual Studio temporary files, build results, and 74 | ## files generated by popular Visual Studio add-ons. 75 | ## 76 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 77 | 78 | # User-specific files 79 | *.suo 80 | *.user 81 | *.userosscache 82 | *.sln.docstates 83 | 84 | # User-specific files (MonoDevelop/Xamarin Studio) 85 | *.userprefs 86 | 87 | # Build results 88 | [Dd]ebug/ 89 | [Dd]ebugPublic/ 90 | [Rr]elease/ 91 | [Rr]eleases/ 92 | x64/ 93 | x86/ 94 | bld/ 95 | [Bb]in/ 96 | [Oo]bj/ 97 | [Ll]og/ 98 | 99 | # Visual Studio 2015/2017 cache/options directory 100 | .vs/ 101 | # Uncomment if you have tasks that create the project's static files in wwwroot 102 | #wwwroot/ 103 | 104 | # Visual Studio 2017 auto generated files 105 | Generated\ Files/ 106 | 107 | # MSTest test Results 108 | [Tt]est[Rr]esult*/ 109 | [Bb]uild[Ll]og.* 110 | 111 | # NUNIT 112 | *.VisualState.xml 113 | TestResult.xml 114 | 115 | # Build Results of an ATL Project 116 | [Dd]ebugPS/ 117 | [Rr]eleasePS/ 118 | dlldata.c 119 | 120 | # Benchmark Results 121 | BenchmarkDotNet.Artifacts/ 122 | 123 | # .NET Core 124 | project.lock.json 125 | project.fragment.lock.json 126 | artifacts/ 127 | 128 | # StyleCop 129 | StyleCopReport.xml 130 | 131 | # Files built by Visual Studio 132 | *_i.c 133 | *_p.c 134 | *_h.h 135 | *.ilk 136 | *.meta 137 | *.obj 138 | *.iobj 139 | *.pch 140 | *.pdb 141 | *.ipdb 142 | *.pgc 143 | *.pgd 144 | *.rsp 145 | *.sbr 146 | *.tlb 147 | *.tli 148 | *.tlh 149 | *.tmp 150 | *.tmp_proj 151 | *_wpftmp.csproj 152 | *.log 153 | *.vspscc 154 | *.vssscc 155 | .builds 156 | *.pidb 157 | *.svclog 158 | *.scc 159 | 160 | # Chutzpah Test files 161 | _Chutzpah* 162 | 163 | # Visual C++ cache files 164 | ipch/ 165 | *.aps 166 | *.ncb 167 | *.opendb 168 | *.opensdf 169 | *.sdf 170 | *.cachefile 171 | *.VC.db 172 | *.VC.VC.opendb 173 | 174 | # Visual Studio profiler 175 | *.psess 176 | *.vsp 177 | *.vspx 178 | *.sap 179 | 180 | # Visual Studio Trace Files 181 | *.e2e 182 | 183 | # TFS 2012 Local Workspace 184 | $tf/ 185 | 186 | # Guidance Automation Toolkit 187 | *.gpState 188 | 189 | # ReSharper is a .NET coding add-in 190 | _ReSharper*/ 191 | *.[Rr]e[Ss]harper 192 | *.DotSettings.user 193 | 194 | # JustCode is a .NET coding add-in 195 | .JustCode 196 | 197 | # TeamCity is a build add-in 198 | _TeamCity* 199 | 200 | # DotCover is a Code Coverage Tool 201 | *.dotCover 202 | 203 | # AxoCover is a Code Coverage Tool 204 | .axoCover/* 205 | !.axoCover/settings.json 206 | 207 | # Visual Studio code coverage results 208 | *.coverage 209 | *.coveragexml 210 | 211 | # NCrunch 212 | _NCrunch_* 213 | .*crunch*.local.xml 214 | nCrunchTemp_* 215 | 216 | # MightyMoose 217 | *.mm.* 218 | AutoTest.Net/ 219 | 220 | # Web workbench (sass) 221 | .sass-cache/ 222 | 223 | # Installshield output folder 224 | [Ee]xpress/ 225 | 226 | # DocProject is a documentation generator add-in 227 | DocProject/buildhelp/ 228 | DocProject/Help/*.HxT 229 | DocProject/Help/*.HxC 230 | DocProject/Help/*.hhc 231 | DocProject/Help/*.hhk 232 | DocProject/Help/*.hhp 233 | DocProject/Help/Html2 234 | DocProject/Help/html 235 | 236 | # Click-Once directory 237 | publish/ 238 | 239 | # Publish Web Output 240 | *.[Pp]ublish.xml 241 | *.azurePubxml 242 | # Note: Comment the next line if you want to checkin your web deploy settings, 243 | # but database connection strings (with potential passwords) will be unencrypted 244 | *.pubxml 245 | *.publishproj 246 | 247 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 248 | # checkin your Azure Web App publish settings, but sensitive information contained 249 | # in these scripts will be unencrypted 250 | PublishScripts/ 251 | 252 | # NuGet Packages 253 | *.nupkg 254 | # The packages folder can be ignored because of Package Restore 255 | **/[Pp]ackages/* 256 | # except build/, which is used as an MSBuild target. 257 | !**/[Pp]ackages/build/ 258 | # Uncomment if necessary however generally it will be regenerated when needed 259 | #!**/[Pp]ackages/repositories.config 260 | # NuGet v3's project.json files produces more ignorable files 261 | *.nuget.props 262 | *.nuget.targets 263 | 264 | # Microsoft Azure Build Output 265 | csx/ 266 | *.build.csdef 267 | 268 | # Microsoft Azure Emulator 269 | ecf/ 270 | rcf/ 271 | 272 | # Windows Store app package directories and files 273 | AppPackages/ 274 | BundleArtifacts/ 275 | Package.StoreAssociation.xml 276 | _pkginfo.txt 277 | *.appx 278 | 279 | # Visual Studio cache files 280 | # files ending in .cache can be ignored 281 | *.[Cc]ache 282 | # but keep track of directories ending in .cache 283 | !*.[Cc]ache/ 284 | 285 | # Others 286 | ClientBin/ 287 | ~$* 288 | *~ 289 | *.dbmdl 290 | *.dbproj.schemaview 291 | *.jfm 292 | *.pfx 293 | *.publishsettings 294 | orleans.codegen.cs 295 | 296 | # Including strong name files can present a security risk 297 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 298 | #*.snk 299 | 300 | # Since there are multiple workflows, uncomment next line to ignore bower_components 301 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 302 | #bower_components/ 303 | 304 | # RIA/Silverlight projects 305 | Generated_Code/ 306 | 307 | # Backup & report files from converting an old project file 308 | # to a newer Visual Studio version. Backup files are not needed, 309 | # because we have git ;-) 310 | _UpgradeReport_Files/ 311 | Backup*/ 312 | UpgradeLog*.XML 313 | UpgradeLog*.htm 314 | ServiceFabricBackup/ 315 | *.rptproj.bak 316 | 317 | # SQL Server files 318 | *.mdf 319 | *.ldf 320 | *.ndf 321 | 322 | # Business Intelligence projects 323 | *.rdl.data 324 | *.bim.layout 325 | *.bim_*.settings 326 | *.rptproj.rsuser 327 | 328 | # Microsoft Fakes 329 | FakesAssemblies/ 330 | 331 | # GhostDoc plugin setting file 332 | *.GhostDoc.xml 333 | 334 | # Node.js Tools for Visual Studio 335 | .ntvs_analysis.dat 336 | node_modules/ 337 | 338 | # Visual Studio 6 build log 339 | *.plg 340 | 341 | # Visual Studio 6 workspace options file 342 | *.opt 343 | 344 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 345 | *.vbw 346 | 347 | # Visual Studio LightSwitch build output 348 | **/*.HTMLClient/GeneratedArtifacts 349 | **/*.DesktopClient/GeneratedArtifacts 350 | **/*.DesktopClient/ModelManifest.xml 351 | **/*.Server/GeneratedArtifacts 352 | **/*.Server/ModelManifest.xml 353 | _Pvt_Extensions 354 | 355 | # Paket dependency manager 356 | .paket/paket.exe 357 | paket-files/ 358 | 359 | # FAKE - F# Make 360 | .fake/ 361 | 362 | # JetBrains Rider 363 | .idea/ 364 | *.sln.iml 365 | 366 | # CodeRush personal settings 367 | .cr/personal 368 | 369 | # Python Tools for Visual Studio (PTVS) 370 | __pycache__/ 371 | *.pyc 372 | 373 | # Cake - Uncomment if you are using it 374 | # tools/** 375 | # !tools/packages.config 376 | 377 | # Tabs Studio 378 | *.tss 379 | 380 | # Telerik's JustMock configuration file 381 | *.jmconfig 382 | 383 | # BizTalk build output 384 | *.btp.cs 385 | *.btm.cs 386 | *.odx.cs 387 | *.xsd.cs 388 | 389 | # OpenCover UI analysis results 390 | OpenCover/ 391 | 392 | # Azure Stream Analytics local run output 393 | ASALocalRun/ 394 | 395 | # MSBuild Binary and Structured Log 396 | *.binlog 397 | 398 | # NVidia Nsight GPU debugger configuration file 399 | *.nvuser 400 | 401 | # MFractors (Xamarin productivity tool) working folder 402 | .mfractor/ 403 | 404 | # Local History for Visual Studio 405 | .localhistory/ -------------------------------------------------------------------------------- /Benchmark/Program.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | using Threading; 3 | using Threading.Tasks; 4 | 5 | const char DELIMITER = ';'; 6 | 7 | double[] workDurationsNs = GetDurationsNs(); 8 | int[] workIterations = workDurationsNs.Select(GetIterationsForDuration).ToArray(); 9 | var stopwatch = new Stopwatch(); 10 | var workStopwatch = new Stopwatch(); 11 | var benchmarks = new Dictionary> { 12 | // used to calculate synchronization cost for other benchmarks 13 | { "No Sync", noSync }, 14 | 15 | { "SpinLock", spinLock }, 16 | { "Monitor", monitor }, 17 | { "Mutex", mutex }, 18 | { "SemaphoreSlim", serialQueueTasksSemaphoreSlim }, 19 | { "Tpl Dataflow ActionBlock", tplDataflowActionBlock }, 20 | { "SerialQueue (Borland)", serialQueueBorland }, 21 | 22 | // my implementations 23 | { "SerialQueue (Task based, SpinLock)", serialQueueTasksSpinLock }, 24 | { "SerialQueue (Task based, Monitor)", serialQueueTasksMonitor }, 25 | { "SerialQueue (SpinLock)", serialQueueSpinLock }, 26 | { "SerialQueue (Monitor)", serialQueueMonitor }, 27 | }; 28 | 29 | // log work durations & iterations 30 | 31 | Console.WriteLine("Work durations,ms / Iterations \n" + String.Join('\n', workIterations.Select((x, i) => nsToMs(workDurationsNs[i]) + ": " + x))); 32 | 33 | // init results 34 | 35 | double[][] results = new double[benchmarks.Count][]; 36 | for (int i = 0; i < results.Length; i += 1) 37 | { 38 | results[i] = new double[workDurationsNs.Length]; 39 | } 40 | 41 | // run benchmarks 42 | 43 | for (int i = 0; i < benchmarks.Count; i += 1) 44 | { 45 | var (label, benchmark) = benchmarks.ElementAt(i); 46 | var runBenchmark = (int iterations, double workDurationNs) => RunAndGetDuration(() => benchmark(iterations, workDurationNs)); 47 | 48 | // warmup 49 | 50 | await runBenchmark(100_000, 1_000); 51 | GC.Collect(); 52 | 53 | // run benchmark for all work duration values 54 | 55 | for (int j = 0; j < workDurationsNs.Length; j += 1) 56 | { 57 | double workDurationNs = workDurationsNs[j]; 58 | int iterations = workIterations[j]; 59 | 60 | // run benchmark 61 | 62 | var elapsed = await runBenchmark(iterations, workDurationNs); 63 | GC.Collect(); 64 | 65 | // set result and log 66 | 67 | var ticksPerWork = elapsed.Ticks / (double)iterations; 68 | if (i == 0) 69 | { 70 | results[i][j] = ticksPerWork; // log duration for "No Sync" benchmark 71 | } 72 | else 73 | { 74 | results[i][j] = ticksPerWork - results[0][j]; // log diff with "No Sync" duration for other benchmarks 75 | } 76 | Console.WriteLine(DateTime.Now.ToString("HH:mm:ss") + " " + label + " w:" + nsToMs(workDurationNs) + "ms i:" + iterations + " finished in " + nsToSec(elapsed.Ns).ToString("0.###") + "s"); 77 | } 78 | } 79 | 80 | // log headers & results 81 | 82 | Console.WriteLine("Work duration, ms" + DELIMITER + string.Join(DELIMITER, workDurationsNs.Select(nsToMs))); 83 | 84 | for (int i = 0; i < benchmarks.Count; i += 1) 85 | { 86 | var (label, benchmark) = benchmarks.ElementAt(i); 87 | Console.WriteLine(label + DELIMITER + string.Join(DELIMITER, results[i].Select(x => x.ToString("0.##")))); 88 | } 89 | 90 | #region Utils 91 | 92 | void work(double durationNs) 93 | { 94 | workStopwatch!.Restart(); 95 | while (workStopwatch.Elapsed.TotalNanoseconds < durationNs) { } 96 | } 97 | 98 | Task ParallelFor(int count, Action action, bool noParallelism = false) 99 | { 100 | var options = new ParallelOptions(); 101 | if (noParallelism) 102 | { 103 | options.MaxDegreeOfParallelism = 1; 104 | } 105 | var tasksLeft = count; 106 | var tcs = new TaskCompletionSource(); 107 | var callback = () => 108 | { 109 | tasksLeft -= 1; 110 | if (tasksLeft == 0) 111 | { 112 | tcs.SetResult(); 113 | } 114 | }; 115 | Parallel.For(0, count, options, (_, _) => 116 | { 117 | action(callback); 118 | }); 119 | return tcs.Task; 120 | } 121 | 122 | async Task<(long Ticks, double Ns)> RunAndGetDuration(Func action) 123 | { 124 | stopwatch.Restart(); 125 | await action(); 126 | stopwatch.Stop(); 127 | return (stopwatch.ElapsedTicks, stopwatch.Elapsed.TotalNanoseconds); 128 | } 129 | 130 | double[] GetDurationsNs() 131 | { 132 | var enumerator = DurationEnumerator(); 133 | var list = new List(); 134 | while (enumerator.MoveNext()) 135 | list.Add(enumerator.Current); 136 | return list.ToArray(); 137 | 138 | IEnumerator DurationEnumerator() 139 | { 140 | int last = 50; 141 | 142 | while (last < 500_000_000) 143 | { 144 | last = last.ToString()![0] == '5' ? last * 2 : last * 5; 145 | yield return last; 146 | } 147 | } 148 | } 149 | 150 | int GetIterationsForDuration(double durationNs) 151 | { 152 | const int maxIterations = 100_000; 153 | const int minIterations = 5_000; 154 | double maxDurationMins = 5; 155 | double durationMins = nsToSec(durationNs) / 60; 156 | 157 | return Math.Max(minIterations, Math.Min(maxIterations, (int)Math.Round(maxDurationMins / durationMins))); 158 | } 159 | 160 | double nsToSec(double ns) 161 | { 162 | return ns / 1_000_000_000; 163 | } 164 | 165 | double nsToMs(double ns) 166 | { 167 | return ns / 1_000_000; 168 | } 169 | 170 | #endregion 171 | 172 | #region Benchmarks 173 | 174 | Task noSync(int workIterations, double workNs) 175 | { 176 | return ParallelFor(workIterations, (callback) => 177 | { 178 | work(workNs); ; 179 | callback(); 180 | }, true); 181 | } 182 | 183 | Task monitor(int workIterations, double workNs) 184 | { 185 | var locker = new object(); 186 | return ParallelFor(workIterations, (callback) => 187 | { 188 | lock (locker) 189 | { 190 | work(workNs); ; 191 | callback(); 192 | } 193 | }); 194 | } 195 | 196 | Task spinLock(int workIterations, double workNs) 197 | { 198 | var spinLock = new SpinLock(false); 199 | return ParallelFor(workIterations, (callback) => 200 | { 201 | bool gotLock = false; 202 | try 203 | { 204 | spinLock.Enter(ref gotLock); 205 | work(workNs); 206 | callback(); 207 | } 208 | finally 209 | { 210 | if (gotLock) spinLock.Exit(false); 211 | } 212 | }); 213 | } 214 | 215 | Task mutex(int workIterations, double workNs) 216 | { 217 | var mutex = new Mutex(); 218 | return ParallelFor(workIterations, (callback) => 219 | { 220 | mutex.WaitOne(); 221 | try 222 | { 223 | work(workNs); 224 | callback(); 225 | } 226 | finally 227 | { 228 | mutex.ReleaseMutex(); ; 229 | } 230 | }); 231 | } 232 | 233 | Task serialQueueTasksMonitor(int workIterations, double workNs) 234 | { 235 | var serialQueue = new SerialQueueTasksMonitor(); 236 | return ParallelFor(workIterations, (callback) => 237 | { 238 | serialQueue.Enqueue(() => 239 | { 240 | work(workNs); 241 | callback(); 242 | }); 243 | }); 244 | } 245 | 246 | Task serialQueueTasksSpinLock(int workIterations, double workNs) 247 | { 248 | var serialQueue = new SerialQueueTasksSpinLock(); 249 | return ParallelFor(workIterations, (callback) => 250 | { 251 | serialQueue.Enqueue(() => 252 | { 253 | work(workNs); 254 | callback(); 255 | }); 256 | }); 257 | } 258 | 259 | Task serialQueueTasksSemaphoreSlim(int workIterations, double workNs) 260 | { 261 | var serialQueue = new SerialQueueTasksSemaphoreSlim(); 262 | return ParallelFor(workIterations, (callback) => 263 | { 264 | serialQueue.Enqueue(() => 265 | { 266 | work(workNs); 267 | callback(); 268 | }); 269 | }); 270 | } 271 | 272 | Task serialQueueMonitor(int workIterations, double workNs) 273 | { 274 | var serialQueue = new SerialQueueMonitor(); 275 | return ParallelFor(workIterations, (callback) => 276 | { 277 | serialQueue.DispatchAsync(() => 278 | { 279 | work(workNs); 280 | callback(); 281 | }); 282 | }); 283 | } 284 | 285 | Task serialQueueSpinLock(int workIterations, double workNs) 286 | { 287 | var serialQueue = new SerialQueueSpinlock(); 288 | return ParallelFor(workIterations, (callback) => 289 | { 290 | serialQueue.DispatchAsync(() => 291 | { 292 | work(workNs); 293 | callback(); 294 | }); 295 | }); 296 | } 297 | 298 | Task tplDataflowActionBlock(int workIterations, double workNs) 299 | { 300 | var serialQueue = new SerialQueueTplDataflow(); 301 | return ParallelFor(workIterations, (callback) => 302 | { 303 | serialQueue.Enqueue(() => 304 | { 305 | work(workNs); 306 | callback(); 307 | }); 308 | }); 309 | } 310 | 311 | Task serialQueueBorland(int workIterations, double workNs) 312 | { 313 | var serialQueue = new Dispatch.SerialQueue(); 314 | return ParallelFor(workIterations, (callback) => 315 | { 316 | serialQueue.DispatchAsync(() => 317 | { 318 | work(workNs); 319 | callback(); 320 | }); 321 | }); 322 | } 323 | 324 | #endregion 325 | 326 | -------------------------------------------------------------------------------- /Tests/SerialQueueTasks.cs: -------------------------------------------------------------------------------- 1 | using Threading.Tasks; 2 | 3 | namespace Tests.Tasks 4 | { 5 | [TestFixture] 6 | public class SerialQueueTasksTests 7 | { 8 | [Test] 9 | public async Task EnqueueAction() 10 | { 11 | // Assign 12 | 13 | const int count = 100000; 14 | var queue = new SerialQueue(); 15 | var list = new List(); 16 | var tasks = new List(count); 17 | var range = Enumerable.Range(0, count); 18 | 19 | // Act 20 | 21 | foreach (var number in range) 22 | { 23 | tasks.Add(queue.Enqueue(() => list.Add(number))); 24 | } 25 | 26 | await Task.WhenAll(tasks); 27 | 28 | // Assert 29 | 30 | Assert.True(range.SequenceEqual(list)); 31 | } 32 | 33 | [Test] 34 | public async Task EnqueueFunction() 35 | { 36 | // Assign 37 | 38 | const int count = 100000; 39 | var queue = new SerialQueue(); 40 | var tasks = new List>(count); 41 | var range = Enumerable.Range(0, count); 42 | 43 | // Act 44 | 45 | foreach (var number in range) 46 | { 47 | tasks.Add(queue.Enqueue(() => number)); 48 | } 49 | 50 | await Task.WhenAll(tasks); 51 | 52 | // Assert 53 | 54 | Assert.True(tasks.Select(x => x.Result).SequenceEqual(range)); 55 | } 56 | 57 | [Test] 58 | public async Task EnqueueAsyncAction() 59 | { 60 | // Assign 61 | 62 | const int count = 10000; 63 | var queue = new SerialQueue(); 64 | var list = new List(); 65 | var tasks = new List(count); 66 | var range = Enumerable.Range(0, count); 67 | 68 | // Act 69 | 70 | foreach (var number in range) 71 | { 72 | tasks.Add(queue.Enqueue(async () => 73 | { 74 | await TestUtils.RandomDelay(); 75 | list.Add(number); 76 | })); 77 | } 78 | 79 | await Task.WhenAll(tasks); 80 | 81 | // Assert 82 | 83 | Assert.True(range.SequenceEqual(list)); 84 | } 85 | 86 | [Test] 87 | public async Task EnqueueAsyncFunction() 88 | { 89 | // Assign 90 | 91 | const int count = 10000; 92 | var queue = new SerialQueue(); 93 | var tasks = new List>(count); 94 | var range = Enumerable.Range(0, count); 95 | 96 | // Act 97 | 98 | foreach (var number in range) 99 | { 100 | tasks.Add(queue.Enqueue(async () => 101 | { 102 | await TestUtils.RandomDelay(); 103 | return number; 104 | })); 105 | } 106 | 107 | await Task.WhenAll(tasks); 108 | 109 | // Assert 110 | 111 | Assert.True(tasks.Select(x => x.Result).SequenceEqual(range)); 112 | } 113 | 114 | [Test] 115 | public async Task EnqueueMixed() 116 | { 117 | // Assign 118 | 119 | const int count = 10000; 120 | var queue = new SerialQueue(); 121 | var list = new List(); 122 | var tasks = new List(count); 123 | var range = Enumerable.Range(0, count); 124 | 125 | // Act 126 | 127 | foreach (var number in range) 128 | { 129 | var index = number % 4; 130 | if (index == 0) 131 | { 132 | await TestUtils.RandomDelay(); 133 | tasks.Add(queue.Enqueue(() => list.Add(number))); 134 | } 135 | else if (index == 1) 136 | { 137 | await TestUtils.RandomDelay(); 138 | tasks.Add(queue.Enqueue(() => 139 | { 140 | list.Add(number); 141 | return number; 142 | })); 143 | } 144 | else if (index == 2) 145 | { 146 | tasks.Add(queue.Enqueue(async () => 147 | { 148 | await TestUtils.RandomDelay(); 149 | list.Add(number); 150 | })); 151 | } 152 | else 153 | { 154 | tasks.Add(queue.Enqueue(async () => 155 | { 156 | await TestUtils.RandomDelay(); 157 | list.Add(number); 158 | return number; 159 | })); 160 | } 161 | } 162 | 163 | await Task.WhenAll(tasks); 164 | 165 | // Assert 166 | 167 | Assert.True(range.SequenceEqual(list)); 168 | } 169 | 170 | [Test] 171 | public async Task EnqueueFromMultipleThreads() 172 | { 173 | // Assign 174 | 175 | const int count = 10000; 176 | var queue = new SerialQueue(); 177 | var list = new List(); 178 | var tasks = new List(count); 179 | 180 | // Act 181 | 182 | var counter = 0; 183 | for (int i = 0; i < count; i += 1) 184 | { 185 | tasks.Add(Task.Run(async () => 186 | { 187 | await queue.Enqueue(async () => 188 | { 189 | var index = counter; 190 | counter += 1; 191 | await TestUtils.RandomDelay(); 192 | list.Add(index); 193 | }); 194 | })); 195 | } 196 | 197 | await Task.WhenAll(tasks); 198 | 199 | // Assert 200 | 201 | Assert.True(list.SequenceEqual(Enumerable.Range(0, count))); 202 | } 203 | 204 | [Test] 205 | public async Task CatchExceptionFromAction() 206 | { 207 | // Assign 208 | 209 | var queue = new SerialQueue(); 210 | Exception? exception = null; 211 | Action action = () => throw new Exception("Test"); 212 | 213 | // Act 214 | 215 | await queue.Enqueue(() => Thread.Sleep(10)); 216 | try 217 | { 218 | await queue.Enqueue(action); 219 | } 220 | catch (Exception e) 221 | { 222 | exception = e; 223 | } 224 | 225 | // Assert 226 | 227 | Assert.AreEqual("Test", exception?.Message); 228 | } 229 | 230 | [Test] 231 | public async Task CatchExceptionFromAsyncAction() 232 | { 233 | // Assign 234 | 235 | var queue = new SerialQueue(); 236 | var exceptionCatched = false; 237 | 238 | // Act 239 | 240 | await queue.Enqueue(() => Thread.Sleep(10)); 241 | try 242 | { 243 | await queue.Enqueue(async () => 244 | { 245 | await Task.Delay(50); 246 | throw new Exception("Test"); 247 | }); 248 | } 249 | catch (Exception e) 250 | { 251 | if (e.Message == "Test") 252 | { 253 | exceptionCatched = true; 254 | } 255 | } 256 | 257 | // Assert 258 | 259 | Assert.True(exceptionCatched); 260 | } 261 | 262 | 263 | [Test] 264 | public async Task CatchExceptionFromAsyncFunction() 265 | { 266 | // Assign 267 | 268 | var queue = new SerialQueue(); 269 | var exceptionCatched = false; 270 | 271 | // Act 272 | 273 | await queue.Enqueue(() => Thread.Sleep(10)); 274 | try 275 | { 276 | await queue.Enqueue(asyncFunction: async () => 277 | { 278 | await Task.Delay(50); 279 | throw new Exception("Test"); 280 | #pragma warning disable CS0162 // Unreachable code detected 281 | return false; 282 | #pragma warning restore CS0162 // Unreachable code detected 283 | }); 284 | } 285 | catch (Exception e) 286 | { 287 | if (e.Message == "Test") 288 | { 289 | exceptionCatched = true; 290 | } 291 | } 292 | 293 | // Assert 294 | 295 | Assert.True(exceptionCatched); 296 | } 297 | 298 | //[Test] 299 | //public async Task TplIsNotFifo() 300 | //{ 301 | // // Assign 302 | 303 | // const int count = 1000000; 304 | // var list = new List(); 305 | // var tasks = new List(count); 306 | // var range = Enumerable.Range(0, count); 307 | 308 | // // Act 309 | 310 | // foreach (var number in range) 311 | // { 312 | // tasks.Add(Task.Factory.StartNew(() => list.Add(number), TaskCreationOptions.PreferFairness)); 313 | // } 314 | 315 | // await Task.WhenAll(tasks); 316 | 317 | // // Assert 318 | 319 | // Assert.False(range.SequenceEqual(list)); 320 | //} 321 | } 322 | } 323 | 324 | --------------------------------------------------------------------------------