From c5d83e71fb663a7c2fa6ef547c59e9af1b1c857c Mon Sep 17 00:00:00 2001 From: justcoding121 Date: Fri, 13 Apr 2018 19:01:46 -0400 Subject: [PATCH] optimize --- EventHook/Helpers/AsyncQueue.cs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/EventHook/Helpers/AsyncQueue.cs b/EventHook/Helpers/AsyncQueue.cs index 623267f..7a22b69 100644 --- a/EventHook/Helpers/AsyncQueue.cs +++ b/EventHook/Helpers/AsyncQueue.cs @@ -5,8 +5,8 @@ namespace EventHook.Helpers { /// - /// A concurrent queue facilitating async dequeue - /// Since our consumer is always single threaded no locking is needed + /// A concurrent queue facilitating async dequeue with minimal locking + /// Assumes single/multi-threaded producer and a single-threaded consumer /// /// internal class AsyncQueue @@ -25,10 +25,10 @@ internal AsyncQueue(CancellationToken taskCancellationToken) /// /// Keeps any pending Dequeue task to wake up once data arrives /// - TaskCompletionSource dequeueTask; + TaskCompletionSource dequeueTask; /// - /// Assumes a single threaded producer! + /// Supports multi-threaded producers /// /// internal void Enqueue(T value) @@ -39,15 +39,12 @@ internal void Enqueue(T value) if (dequeueTask != null && !dequeueTask.Task.IsCompleted) { - T result; - queue.TryDequeue(out result); - dequeueTask.SetResult(result); + dequeueTask.SetResult(true); } - } /// - /// Assumes a single threaded consumer! + /// Assumes a single-threaded consumer! /// /// internal async Task DequeueAsync() @@ -60,11 +57,12 @@ internal async Task DequeueAsync() return result; } - dequeueTask = new TaskCompletionSource(); + dequeueTask = new TaskCompletionSource(); taskCancellationToken.Register(() => dequeueTask.TrySetCanceled()); - result = await dequeueTask.Task; + await dequeueTask.Task; dequeueTask = null; + queue.TryDequeue(out result); return result; }