tgstation-server 6.0.1
The /tg/station 13 server suite
Loading...
Searching...
No Matches
SemaphoreSlimContext.cs
Go to the documentation of this file.
1using System;
2using System.Threading;
3using System.Threading.Tasks;
4
6{
11 {
18 public static async ValueTask<SemaphoreSlimContext> Lock(SemaphoreSlim semaphore, CancellationToken cancellationToken)
19 {
20 ArgumentNullException.ThrowIfNull(semaphore);
21 await semaphore.WaitAsync(cancellationToken);
22 return new SemaphoreSlimContext(semaphore);
23 }
24
31 public static SemaphoreSlimContext? TryLock(SemaphoreSlim semaphore, out bool locked)
32 {
33 ArgumentNullException.ThrowIfNull(semaphore);
34 locked = semaphore.Wait(TimeSpan.Zero);
35 return locked
36 ? new SemaphoreSlimContext(semaphore)
37 : null;
38 }
39
43 readonly SemaphoreSlim lockedSemaphore;
44
50 {
51 this.lockedSemaphore = lockedSemaphore;
52 }
53
57 public void Dispose() => lockedSemaphore.Release();
58 }
59}
static async ValueTask< SemaphoreSlimContext > Lock(SemaphoreSlim semaphore, CancellationToken cancellationToken)
Asyncronously locks a semaphore .
SemaphoreSlimContext(SemaphoreSlim lockedSemaphore)
Initializes a new instance of the SemaphoreSlimContext class.
readonly SemaphoreSlim lockedSemaphore
The locked SemaphoreSlim.
static ? SemaphoreSlimContext TryLock(SemaphoreSlim semaphore, out bool locked)
Asyncronously attempts to lock a semaphore .
void Dispose()
Release the lock on lockedSemaphore.