-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSemaphoreManager.cs
More file actions
34 lines (26 loc) · 1.19 KB
/
Copy pathSemaphoreManager.cs
File metadata and controls
34 lines (26 loc) · 1.19 KB
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
using Semaphoria.Extensions;
namespace Semaphoria
{
public class SemaphoreManager : ISemaphoreManager
{
private readonly Dictionary<string, SemaphoreSlim> _semaphores = [];
public int NumSlots { get; set; } = 1;
public async Task WaitAsync(string semaphoreName, CancellationToken cancellationToken = default) =>
await Get(semaphoreName).WaitAsync(cancellationToken);
public void Wait(string semaphoreName) => Get(semaphoreName).Wait();
public void Release(string semaphoreName) => Get(semaphoreName).Release();
public int CurrentCount(string semaphoreName) => Get(semaphoreName).CurrentCount;
// CurrentCount is the number of slots left. NOT the number of slots in use.
public bool IsFull(string semaphoreName) => Get(semaphoreName).IsFull();
public SemaphoreSlim Get(string semaphoreName)
{
if (_semaphores.TryGetValue(semaphoreName, out SemaphoreSlim? semaphore))
{
return semaphore;
}
semaphore = new SemaphoreSlim(NumSlots, NumSlots);
_semaphores.Add(semaphoreName, semaphore);
return semaphore;
}
}
}