-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Async DiskWriterQueue implementation
- Loading branch information
Showing
2 changed files
with
47 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace LiteDB | ||
{ | ||
/// <summary> | ||
/// Async implementation of ManualResetEvent | ||
/// https://devblogs.microsoft.com/pfxteam/building-async-coordination-primitives-part-1-asyncmanualresetevent/ | ||
/// </summary> | ||
internal class AsyncManualResetEvent | ||
{ | ||
private volatile TaskCompletionSource<bool> _tcs = new TaskCompletionSource<bool>(); | ||
|
||
public Task WaitAsync() | ||
{ | ||
return _tcs.Task; | ||
} | ||
|
||
public void Set() | ||
{ | ||
_tcs.TrySetResult(true); | ||
} | ||
|
||
public void Reset() | ||
{ | ||
while (true) | ||
{ | ||
var tcs = _tcs; | ||
if (!tcs.Task.IsCompleted || | ||
Interlocked.CompareExchange(ref _tcs, new TaskCompletionSource<bool>(), tcs) == tcs) | ||
return; | ||
} | ||
} | ||
} | ||
} |