Skip to content

Commit

Permalink
Simplified DiskWriterQueue with blocking concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
ltetak committed Jan 25, 2024
1 parent 13ab9ef commit 27ea600
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions LiteDB/Engine/Disk/DiskWriterQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ internal class DiskWriterQueue : IDisposable
private bool _shouldClose = false;

private readonly ConcurrentQueue<PageBuffer> _queue = new ConcurrentQueue<PageBuffer>();
private readonly object _queueSync = new object();
private readonly ManualResetEventSlim _queueHasItems = new ManualResetEventSlim(false);
private readonly ManualResetEventSlim _queueIsEmpty = new ManualResetEventSlim(true);

Expand All @@ -42,12 +41,9 @@ public DiskWriterQueue(Stream stream)
public void EnqueuePage(PageBuffer page)
{
ENSURE(page.Origin == FileOrigin.Log, "async writer must use only for Log file");
lock (_queueSync)
{
_queueIsEmpty.Reset();
_queue.Enqueue(page);
_queueHasItems.Set();
}
_queueIsEmpty.Reset();
_queue.Enqueue(page);
_queueHasItems.Set();
}

/// <summary>
Expand All @@ -73,19 +69,13 @@ private void ExecuteQueue()
else
{
TryFlushStream();

lock (_queueSync)
{
if (_queue.Count > 0) continue;
_queueIsEmpty.Set();
}

_queueIsEmpty.Set();
_queueHasItems.Wait();
if (_shouldClose) return;
}
}
}

private void TryFlushStream()
{
try
Expand Down

0 comments on commit 27ea600

Please sign in to comment.