Table of Contents

Struct AsyncReaderWriterLock.Releaser

Namespace
CryptoHives.Foundation.Threading.Async.Pooled
Assembly
CryptoHives.Foundation.Threading.dll

A small value type returned by awaiting a lock acquisition. Disposing the releaser releases the lock.

public readonly struct AsyncReaderWriterLock.Releaser : IDisposable, IAsyncDisposable, IEquatable<AsyncReaderWriterLock.Releaser>
Implements
Inherited Members

Remarks

Due to the different usage options for this releaser with the method to upgrade an upgradeable reader, it had been preferred to have a dedicated struct instead of using a common struct with flags to reduce the risk of incorrect usage. However multiple releaser struct types had resulted in seperate ObjectPools for each struct type which had reduced the overall pool efficiency and increased the memory usage, so a single struct with an enum to distinguish the different usage scenarios had been chosen as a good balance between usability and performance. Since any releaser can call the UpgradeToWriterLockAsync(CancellationToken) method, an InvalidOperationException is thrown if the method is called on a releaser that is not for the upgradeable reader state.

Methods

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

DisposeAsync()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.

public ValueTask DisposeAsync()

Returns

ValueTask

Equals(Releaser)

Indicates whether the current object is equal to another object of the same type.

public bool Equals(AsyncReaderWriterLock.Releaser other)

Parameters

other AsyncReaderWriterLock.Releaser

An object to compare with this object.

Returns

bool

true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)

Indicates whether this instance and a specified object are equal.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current instance.

Returns

bool

true if obj and this instance are the same type and represent the same value; otherwise, false.

GetHashCode()

Returns the hash code for this instance.

public override int GetHashCode()

Returns

int

A 32-bit signed integer that is the hash code for this instance.

UpgradeToWriterLockAsync(CancellationToken)

Upgrades an upgradable reader to a writer lock.

public ValueTask<AsyncReaderWriterLock.Releaser> UpgradeToWriterLockAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

A cancellation token that can be used to cancel the attempt to upgrade to the write lock.

Returns

ValueTask<AsyncReaderWriterLock.Releaser>

Remarks

This method must only be called when the Releaser instance is in the upgradeable reader state. Failing to release the write lock by disposing the returned releaser may result in deadlocks.

Exceptions

InvalidOperationException

Thrown if the current instance is not in the upgradeable reader state.

UpgradeToWriterLockAsync(TimeSpan, CancellationToken)

Upgrades an upgradable reader to a writer lock.

public ValueTask<AsyncReaderWriterLock.Releaser> UpgradeToWriterLockAsync(TimeSpan timeout, CancellationToken cancellationToken = default)

Parameters

timeout TimeSpan

The maximum time to wait. Use InfiniteTimeSpan to wait indefinitely.

cancellationToken CancellationToken

A cancellation token that can be used to cancel the attempt to upgrade to the write lock.

Returns

ValueTask<AsyncReaderWriterLock.Releaser>

Remarks

This method must only be called when the Releaser instance is in the upgradeable reader state. Failing to release the write lock by disposing the returned releaser may result in deadlocks.

Exceptions

InvalidOperationException

Thrown if the current instance is not in the upgradeable reader state.

TimeoutException

Thrown when the timeout elapses before the lock can be upgraded.

OperationCanceledException

Thrown when cancellationToken is cancelled before the lock can be upgraded.

Operators

operator ==(Releaser, Releaser)

Determines whether two Releaser instances are equal.

public static bool operator ==(AsyncReaderWriterLock.Releaser left, AsyncReaderWriterLock.Releaser right)

Parameters

left AsyncReaderWriterLock.Releaser
right AsyncReaderWriterLock.Releaser

Returns

bool

operator !=(Releaser, Releaser)

Determines whether two Releaser instances are not equal.

public static bool operator !=(AsyncReaderWriterLock.Releaser left, AsyncReaderWriterLock.Releaser right)

Parameters

left AsyncReaderWriterLock.Releaser
right AsyncReaderWriterLock.Releaser

Returns

bool