Add ModelContextProtocol.Core package (#428)

This commit is contained in:
Stephen Halter 2025-06-02 15:16:45 -07:00 • committed by GitHub
commit 703b0fe50b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,40 +0,0 @@
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();
}
}