Loop8ack.AsyncTicketLock 1.1.0

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

// Install Loop8ack.AsyncTicketLock as a Cake Tool
#tool nuget:?package=Loop8ack.AsyncTicketLock&version=1.1.0                

Loop8ack.AsyncTicketLock

Nuget

An asynchronous class that functions as a lock based on provided ticket objects instead of the current thread. Only one ticket object can hold the lock at a time, accesses with the same ticket object are allowed, accesses with other ticket objects are not allowed.

I addressed the original idea in the following forum topic:
https://mycsharp.de/forum/threads/124593/async-task-synchronisation

Special thanks to gfoidl, who contributed the basic implementation and who gave me advice and support.

How to Use

Enter/Release with using

var ticketLock = new AsyncTicketLock();
var ticket = new object();

// async
using (await ticketLock.EnterAsync(ticket))
{
    // Do work
}

// sync
if (ticketLock.TryEnter(ticket, out var releaser))
{
    using (releaser)
    {
        // ...
    }
}

Enter/Release manually

var ticketLock = new AsyncTicketLock();
var ticket = new object();

// async
await ticketLock.EnterAsync(ticket);
// Do work
ticketLock.Release(ticket); // true

// sync
if (ticketLock.TryEnter(ticket))
{
    ticketLock.TryEnter(new object()); // false

    ticketLock.Release(ticket);
}

Nested lock

var ticketLock = new AsyncTicketLock();
var ticket = new object();

await ticketLock.EnterAsync(ticket);
await ticketLock.EnterAsync(ticket);
await ticketLock.EnterAsync(ticket);
await ticketLock.EnterAsync(ticket);

ticketLock.Release(ticket, 2); // returns 2
ticketLock.Release(ticket);    // returns true
ticketLock.Release(ticket, 2); // returns 1
ticketLock.ReleaseAll(ticket); // returns false

With timeout

var ticketLock = new AsyncTicketLock();
var ticket1 = new object();
var ticket2 = new object();

await ticketLock.EnterAsync(ticket1);

// TimeoutException after one second
await ticketLock.EnterAsync(ticket2, TimeSpan.FromSeconds(1));

With maximum entered count

var ticketLock = new AsyncTicketLock(3);
var ticket = new object();

ticketLock.TryEnter(ticket); // true
ticketLock.TryEnter(ticket); // true
ticketLock.TryEnter(ticket); // true
ticketLock.TryEnter(ticket); // false

ticketLock.Release(ticket, 2);

ticketLock.TryEnter(ticket); // true
ticketLock.TryEnter(ticket); // true
ticketLock.TryEnter(ticket); // false

Release Notes

1.1.0

  • Added support for MaxEnteredCount parameter: Users can now limit the number of times a ticket object can enter the lock.

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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. 
.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 was computed. 
.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

  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.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.1.0 209 8/2/2023
1.0.0 539 6/22/2022

Added support for MaxEnteredCount parameter: Users can now limit the number of times a ticket object can enter the lock.