-
-
Notifications
You must be signed in to change notification settings - Fork 230
Expand file tree
/
Copy pathSignal.cs
More file actions
36 lines (30 loc) · 910 Bytes
/
Signal.cs
File metadata and controls
36 lines (30 loc) · 910 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
namespace Sentry.Internal;
internal class Signal : IDisposable
{
private readonly Lock _lock = new();
private readonly SemaphoreSlim _semaphore = new(0, 1);
public Signal(bool isReleasedInitially = false)
{
if (isReleasedInitially)
{
Release();
}
}
public void Release()
{
// Make sure the semaphore does not go above 1
lock (_lock)
{
if (_semaphore.CurrentCount >= 1)
{
return;
}
_semaphore.Release();
}
}
// It's synchronized only on Release, not to go above 1. The type itself is thread-safe.
// ReSharper disable once InconsistentlySynchronizedField
public Task WaitAsync(CancellationToken cancellationToken = default) =>
_semaphore.WaitAsync(cancellationToken);
public void Dispose() => _semaphore.Dispose();
}