Skip to content

Commit

Permalink
chore: Adding comments for AsyncManualResetEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
hez2010 committed Apr 14, 2024
1 parent 1332846 commit d513828
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/GZCTF/Utils/AsyncManualResetEvent.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
namespace GZCTF.Utils;

/// <summary>
/// Asynchronous manual reset event
/// This class is similar to <see cref="ManualResetEvent"/> but asynchronous and non-blocking
/// </summary>
public sealed class AsyncManualResetEvent
{
private volatile TaskCompletionSource<bool> _tcs = new();

/// <summary>
/// Wait for the event to be signaled
/// </summary>
/// <param name="cancellationToken">Cancellation token</param>
public async Task WaitAsync(CancellationToken cancellationToken = default)
{
var tcs = _tcs;
Expand All @@ -21,6 +29,12 @@ private async Task<bool> Delay(int milliseconds)
return false;
}

/// <summary>
/// Wait for the event to be signaled
/// </summary>
/// <param name="milliseconds">Timeout</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Returns false if timeout</returns>
public async Task<bool> WaitAsync(int milliseconds, CancellationToken cancellationToken = default)
{
var tcs = _tcs;
Expand All @@ -32,6 +46,9 @@ public async Task<bool> WaitAsync(int milliseconds, CancellationToken cancellati
return await await Task.WhenAny(tcs.Task, cancelTcs.Task, Delay(milliseconds));
}

/// <summary>
/// Set the event to signaled
/// </summary>
public void Set()
{
var tcs = _tcs;
Expand All @@ -40,6 +57,9 @@ public void Set()
tcs.Task.Wait();
}

/// <summary>
/// Reset the event to non-signaled
/// </summary>
public void Reset()
{
var newTcs = new TaskCompletionSource<bool>();
Expand Down

0 comments on commit d513828

Please sign in to comment.