pvNugsSemaphoreNc9Abstractions 9.0.1
dotnet add package pvNugsSemaphoreNc9Abstractions --version 9.0.1
NuGet\Install-Package pvNugsSemaphoreNc9Abstractions -Version 9.0.1
<PackageReference Include="pvNugsSemaphoreNc9Abstractions" Version="9.0.1" />
<PackageVersion Include="pvNugsSemaphoreNc9Abstractions" Version="9.0.1" />
<PackageReference Include="pvNugsSemaphoreNc9Abstractions" />
paket add pvNugsSemaphoreNc9Abstractions --version 9.0.1
#r "nuget: pvNugsSemaphoreNc9Abstractions, 9.0.1"
#:package pvNugsSemaphoreNc9Abstractions@9.0.1
#addin nuget:?package=pvNugsSemaphoreNc9Abstractions&version=9.0.1
#tool nuget:?package=pvNugsSemaphoreNc9Abstractions&version=9.0.1
โณ pvNugsSemaphoreNc9Abstractions
Distributed Semaphore (Mutex) Abstractions for .NET
โจ Features
- ๐ Distributed, named semaphore (mutex) abstraction
- ๐ท๏ธ Identifies owners and supports timeouts
- โฐ Expiry, force-release, and state machine support
- ๐งฉ Designed for dependency injection and extensibility
- ๐งช Testable and mockable interfaces
๐ฆ Installation
# .NET CLI
dotnet add package pvNugsSemaphoreNc9Abstractions
# or Package Manager
Install-Package pvNugsSemaphoreNc9Abstractions
๐ฆ State Machine
The semaphore state machine is represented by the SemaphoreStatusEnu
enum:
State | Description |
---|---|
๐ข Acquired |
The semaphore is successfully acquired and exclusive access is granted. |
๐ก OwnedBySomeoneElse |
The semaphore is currently held by another requester and cannot be acquired. |
๐ด ForcedAcquired |
The semaphore was forcefully acquired (stolen) by the requester because the previous lock timed out and expired. |
๐งฉ Main Interfaces
IPvNugsSemaphoreService
Provides distributed semaphore management for coordinating access to shared resources across processes.
public interface IPvNugsSemaphoreService
{
Task<IPvNugsSemaphoreInfo> AcquireSemaphoreAsync(
string name,
string requester,
TimeSpan timeout,
CancellationToken ct = default
);
Task TouchSemaphoreAsync(
string name,
CancellationToken ct = default
);
Task ReleaseSemaphoreAsync(
string name,
CancellationToken ct = default
);
Task<IPvNugsSemaphoreInfo?> GetSemaphoreAsync(
string name,
string requester,
CancellationToken ct = default
);
Task<T> IsolateWorkAsync<T>(
string semaphoreName,
string requester,
TimeSpan timeout,
Func<Task<T>> workAsync,
Action<string>? notify = null,
int sleepBetweenAttemptsInSeconds = 15,
CancellationToken ct = default
);
Task IsolateWorkAsync(
string semaphoreName,
string requester,
TimeSpan timeout,
Func<Task> workAsync,
Action<string>? notify = null,
int sleepBetweenAttemptsInSeconds = 15,
CancellationToken ct = default
);
}
IPvNugsSemaphoreInfo
Describes the state and metadata of a semaphore instance:
public interface IPvNugsSemaphoreInfo
{
SemaphoreStatusEnu Status { get; }
string Name { get; }
string Owner { get; }
TimeSpan Timeout { get; }
DateTime ExpiresAtUtc { get; }
DateTime CreateDateUtc { get; }
DateTime UpdateDateUtc { get; }
}
๐ Usage Example
var info = await semaphoreService.AcquireSemaphoreAsync(
"my-resource",
Environment.MachineName,
TimeSpan.FromMinutes(5),
ct
);
if (info.Status == SemaphoreStatusEnu.Acquired)
{
// Do exclusive work here
await semaphoreService.ReleaseSemaphoreAsync("my-resource", ct);
}
else if (info.Status == SemaphoreStatusEnu.OwnedBySomeoneElse)
{
// Handle contention
}
Or, to run code in an isolated context:
await semaphoreService.IsolateWorkAsync(
"my-resource",
Environment.MachineName,
TimeSpan.FromMinutes(5),
async () =>
{
// Your exclusive work here
},
notify: msg => Console.WriteLine(msg),
sleepBetweenAttemptsInSeconds: 10,
ct
);
๐งช Testing & Extensibility
- All interfaces are mockable for unit testing.
- Designed for extension: implement your own distributed semaphore provider.
๐ License
MIT License. See LICENSE file for details.
๐ Related Packages
pvNugsSemaphoreNc9Local
โ Local in-memory implementationpvNugsSemaphoreNc9Abstractions
โ This package (interfaces & contracts)
๐ท๏ธ NuGet Package Tags
distributed-semaphore, distributed-mutex, in-process-synchronization, thread-safe, locking, concurrency, .NET, abstractions, DI, dependency-injection, testable, extensible
Add these tags to your NuGet package metadata for better discoverability and clarity.
For more information, see the source repository or contact the maintainer.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on pvNugsSemaphoreNc9Abstractions:
Package | Downloads |
---|---|
pvNugsSemaphoreNc9MsSql
Distributed and in-process semaphore/mutex implementation for .NET using SQL Server as the backend. Provides robust, atomic distributed locking, timeouts, and lock stealing (forced acquisition) for high-availability and scalable applications. Designed for dependency injection, extensibility, and production-grade reliability. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Review state machine, improve readme look and feel