ReconArt.Synchronization.ObjectLock 1.0.0

Prefix Reserved
dotnet add package ReconArt.Synchronization.ObjectLock --version 1.0.0                
NuGet\Install-Package ReconArt.Synchronization.ObjectLock -Version 1.0.0                
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="ReconArt.Synchronization.ObjectLock" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ReconArt.Synchronization.ObjectLock --version 1.0.0                
#r "nuget: ReconArt.Synchronization.ObjectLock, 1.0.0"                
#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.
// Install ReconArt.Synchronization.ObjectLock as a Cake Addin
#addin nuget:?package=ReconArt.Synchronization.ObjectLock&version=1.0.0

// Install ReconArt.Synchronization.ObjectLock as a Cake Tool
#tool nuget:?package=ReconArt.Synchronization.ObjectLock&version=1.0.0                

ReconArt.Synchronization.ObjectLock

Overview

ReconArt.Synchronization.ObjectLock is a .NET library that provides a synchronization primitive that limits the number of threads that can access an object-scoped resource concurrently.

Features

  • Object-scoped synchronization using unique identifiers
  • Support for both synchronous and asynchronous locking mechanisms
  • Configurable concurrency limits for locks
  • Compatible with .NET 9, .NET 8, .NET Standard 2.1, and .NET Standard 2.0

Installation

To install the ReconArt.Synchronization.ObjectLock package, use the NuGet Package Manager or the Package Manager Console with the following command:

Install-Package ReconArt.Synchronization.ObjectLock

Usage

Creating an Object Lock

You can create an ObjectLock instance by utilizing the overloads found in ObjectLockFactory:

using ReconArt.Synchronization;

// Using an object, that implements IObjectLockIdentifier
var obj = new MyClass();
using var objectLock = ObjectLockFactory.From(obj);

// Using an object, that does not implement IObjectLockIdentifier
using var simpleObjectLock = ObjectLockFactory.From(new object(), "my unique identifier");

// Using a type and a unique identifier
using var typeLock = ObjectLockFactory.From<MyClass>("my unique identifier");

Note: To ensure effective locking, make sure the unique identifier is unique within the scope of the object and will not be mutated throughout the lifetime of the lock. The underlying implementation will use a copy of this identifier - any changes made to it, will not be reflected.

Waiting for a Lock

You can wait for a lock synchronously or asynchronously:

// Synchronous wait
objectLock.Wait();

// Asynchronous wait
await objectLock.WaitAsync();

You can also specify a timeout and a cancellation token:

try
{

    // Synchronous wait with milliseconds timeout
    bool entered = objectLock.Wait(10 * 1000);

    // Synchronous wait with timeout
    bool entered = objectLock.Wait(TimeSpan.FromSeconds(10));

    // Asynchronous wait with timeout and cancellation token.
    bool enteredAsync = await objectLock.WaitAsync(TimeSpan.FromSeconds(10), cancellationToken);
}
catch (OperationCanceledException)
{
    // Handle cancellation
}

Releasing a Lock

To release a lock, call the Release method:

objectLock.Release();

Disposing an Object Lock

To release all resources used by the ObjectLock, call the Dispose method:

objectLock.Dispose();

Background Disposal

To avoid contention in performance-critical paths such as the creation and disposal of an ObjectLock, the library delays disposing of the underlying resources until one of the following is true:

  • 24 hours have passed since the last cleanup operation
  • There are more than 50,000 locks in the global dictionary of locks

The above conditions are checked every 30 seconds in a background thread, and are non-configurable.

Contributing

If you'd like to contribute to the project, please reach out to the ReconArt/synchronization-primitives team.

Support

If you encounter any issues or require assistance, please file an issue in the GitHub Issues section of the repository.

Authors and Acknowledgments

Developed by ReconArt, Inc..

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 61 11/18/2024