pvNugsSemaphoreNc9Abstractions 9.0.1

dotnet add package pvNugsSemaphoreNc9Abstractions --version 9.0.1
                    
NuGet\Install-Package pvNugsSemaphoreNc9Abstractions -Version 9.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="pvNugsSemaphoreNc9Abstractions" Version="9.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="pvNugsSemaphoreNc9Abstractions" Version="9.0.1" />
                    
Directory.Packages.props
<PackageReference Include="pvNugsSemaphoreNc9Abstractions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add pvNugsSemaphoreNc9Abstractions --version 9.0.1
                    
#r "nuget: pvNugsSemaphoreNc9Abstractions, 9.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package pvNugsSemaphoreNc9Abstractions@9.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=pvNugsSemaphoreNc9Abstractions&version=9.0.1
                    
Install as a Cake Addin
#tool nuget:?package=pvNugsSemaphoreNc9Abstractions&version=9.0.1
                    
Install as a Cake Tool

โณ 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.


  • pvNugsSemaphoreNc9Local โ€“ Local in-memory implementation
  • pvNugsSemaphoreNc9Abstractions โ€“ 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last Updated
9.0.1 144 10/1/2025
9.0.0 136 10/1/2025

Review state machine, improve readme look and feel