Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed writing pages in the same thread that does EnqueuePage #2453

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions LiteDB/Engine/Disk/DiskWriterQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace LiteDB.Engine
{
using LiteDB.Utils.Extensions;

/// <summary>
/// Implement disk write queue and async writer thread - used only for write on LOG file
/// [ThreadSafe]
Expand All @@ -23,7 +25,7 @@ internal class DiskWriterQueue : IDisposable

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

private Exception _exception = null; // store last exception in async running task
Expand Down Expand Up @@ -100,7 +102,7 @@ private async Task ExecuteQueue()

_stream.FlushToDisk();

await _queueHasItems.WaitAsync();
await _queueHasItems.WaitHandle.WaitAsync().ConfigureAwait(false);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions LiteDB/LiteDB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<AssemblyOriginatorKeyFile Condition="'$(Configuration)' == 'Release'">LiteDB.snk</AssemblyOriginatorKeyFile>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<!--
== All variables ==
HAVE_APP_DOMAIN
Expand Down Expand Up @@ -64,8 +64,9 @@
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.3'">
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.5.1" />
<PackageReference Include="System.Security.Cryptography.Algorithms" Version="4.3.1" />
<PackageReference Include="System.Threading.ThreadPool" Version="4.3.0" />
</ItemGroup>

<!-- End References -->

</Project>
35 changes: 0 additions & 35 deletions LiteDB/Utils/AsyncManualResetEvent.cs

This file was deleted.

33 changes: 33 additions & 0 deletions LiteDB/Utils/Extensions/WaitHandleExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace LiteDB.Utils.Extensions
{
using System;
using System.Threading;
using System.Threading.Tasks;

internal static class WaitHandleExtensions
{
public static async Task<bool> WaitAsync(this WaitHandle handle)
{
var tcs = new TaskCompletionSource<bool>();

using (new ThreadPoolRegistration(handle, tcs))
{
return await tcs.Task.ConfigureAwait(false);
}
}

private sealed class ThreadPoolRegistration : IDisposable
{
private readonly RegisteredWaitHandle _registeredWaitHandle;

public ThreadPoolRegistration(WaitHandle handle, TaskCompletionSource<bool> tcs)
{
_registeredWaitHandle = ThreadPool.RegisterWaitForSingleObject(handle,
(state, timedOut) => ((TaskCompletionSource<bool>)state).TrySetResult(!timedOut), tcs,
Timeout.InfiniteTimeSpan, executeOnlyOnce: true);
}

void IDisposable.Dispose() => _registeredWaitHandle.Unregister(null);
}
}
}