forked from modelcontextprotocol/csharp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSemaphoreSlimExtensions.cs
More file actions
40 lines (38 loc) · 1.8 KB
/
SemaphoreSlimExtensions.cs
File metadata and controls
40 lines (38 loc) · 1.8 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
35
36
37
38
39
40
namespace ModelContextProtocol;
internal static class SynchronizationExtensions
{
/// <summary>
/// Asynchronously acquires a lock on the semaphore and returns a disposable object that releases the lock when disposed.
/// </summary>
/// <param name="semaphore">The semaphore to acquire a lock on.</param>
/// <param name="cancellationToken">A cancellation token to observe while waiting for the semaphore.</param>
/// <returns>A disposable <see cref="Releaser"/> that releases the semaphore when disposed.</returns>
/// <remarks>
/// This extension method provides a convenient pattern for using a semaphore in asynchronous code,
/// similar to how the `lock` statement is used in synchronous code.
/// </remarks>
/// <exception cref="OperationCanceledException">The <paramref name="cancellationToken"/> was canceled.</exception>
public static async ValueTask<Releaser> LockAsync(this SemaphoreSlim semaphore, CancellationToken cancellationToken = default)
{
await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
return new(semaphore);
}
/// <summary>
/// A disposable struct that releases a semaphore when disposed.
/// </summary>
/// <remarks>
/// This struct is used with the <see cref="LockAsync"/> extension method to provide
/// a using-pattern for semaphore locking, similar to lock statements.
/// </remarks>
public readonly struct Releaser(SemaphoreSlim semaphore) : IDisposable
{
/// <summary>
/// Releases the semaphore.
/// </summary>
/// <remarks>
/// This method is called automatically when the <see cref="Releaser"/> goes out of scope
/// in a using statement or expression.
/// </remarks>
public void Dispose() => semaphore.Release();
}
}