DotNext by Roman Sakno

<PackageReference Include="DotNext" Version="2.8.0" />

 ReaderWriterSpinLock

public struct ReaderWriterSpinLock
Represents lightweight reader-writer lock based on spin loop.
public struct LockStamp : IEquatable<LockStamp>

Represents lock stamp used for optimistic reading.

public int CurrentReadCount { get; }

Gets the total number of unique threads that have entered the lock in read mode.

public bool IsReadLockHeld { get; }

Gets a value that indicates whether the current thread has entered the lock in read mode.

public bool IsWriteLockHeld { get; }

Gets a value that indicates whether the current thread has entered the lock in write mode.

public void EnterReadLock()

Enters the lock in read mode.

public void EnterWriteLock()

Enters the lock in write mode.

public void ExitReadLock()

Exits read mode.

public void ExitWriteLock()

Exits the write lock.

public bool TryEnterReadLock()

Attempts to enter reader lock without blocking the caller thread.

public bool TryEnterReadLock(TimeSpan timeout, CancellationToken token = default)

Tries to enter the lock in read mode.

public bool TryEnterWriteLock()

Attempts to enter writer lock without blocking the caller thread.

public bool TryEnterWriteLock(TimeSpan timeout, CancellationToken token = default)

Tries to enter the lock in write mode.

Returns a stamp that can be validated later.

public bool Validate(ref LockStamp stamp)

Returns true if the lock has not been exclusively acquired since issuance of the given stamp.