Class ReadWriteLock
A reader/writer lock that manages access to a resource via disposable guards, allowing multiple threads for reading or exclusive access for writing.
Implements
Namespace: Singulink.Threading
Assembly: Singulink.Threading.dll
Syntax
public sealed class ReadWriteLock : IDisposable
Remarks
Wraps a ReaderWriterLockSlim with NoRecursion, exposing only guard-based entry so lock modes cannot be manipulated behind the guards' backs and recursive entry (which has no guard representation) is excluded by construction. Guard geometry follows the underlying lock: any number of threads can hold read guards, one thread can hold an upgradeable read guard (which can temporarily upgrade to scoped write access or permanently downgrade to a read lock), and write guards are exclusive.
The lock has managed thread affinity: each guard must be entered and disposed on the same thread, and guards must not be used across await boundaries.
Naming convention: Enter…Guard methods return a guard that entered the lock;
TryEnter…Guard methods return a guard that may not have entered the lock (check
IsEntered; disposal of a non-entered guard is a safe no-op, so results can be assigned directly
to using declarations); methods ending in …Lock operate on the current guard in
place.
Properties
| Name | Description |
|---|---|
| IsReadLockHeld | Gets a value indicating whether the current thread has entered the lock in read mode. |
| IsUpgradeableReadLockHeld | Gets a value indicating whether the current thread has entered the lock in upgradeable read mode. |
| IsWriteLockHeld | Gets a value indicating whether the current thread has entered the lock in write mode. |
Methods
| Name | Description |
|---|---|
| Dispose() | Releases all resources used by the lock. The lock must not be held by (or waited on by) any threads when it is disposed. |
| EnterReadGuard() | Enters the lock in read mode and returns a guard that releases the lock upon disposal. |
| EnterUpgradeableReadGuard() | Enters the lock in upgradeable read mode and returns a guard that releases the lock upon disposal. The guard can temporarily upgrade to scoped write access or permanently downgrade to a plain read lock. Only one thread can hold the lock in upgradeable read mode at a time. |
| EnterWriteGuard() | Enters the lock in write mode and returns a guard that releases the lock upon disposal. |
| TryEnterReadGuard(int) | Tries to enter the lock in read mode within the given timeout, returning a guard whose IsEntered property indicates whether the lock was entered. Disposal releases the lock, or is a no-op if the lock was not entered. |
| TryEnterReadGuard(TimeSpan) | Tries to enter the lock in read mode within the given timeout, returning a guard whose IsEntered property indicates whether the lock was entered. Disposal releases the lock, or is a no-op if the lock was not entered. |
| TryEnterUpgradeableReadGuard(int) | Tries to enter the lock in upgradeable read mode within the given timeout, returning a guard whose IsEntered property indicates whether the lock was entered. Disposal releases the lock, or is a no-op if the lock was not entered. |
| TryEnterUpgradeableReadGuard(TimeSpan) | Tries to enter the lock in upgradeable read mode within the given timeout, returning a guard whose IsEntered property indicates whether the lock was entered. Disposal releases the lock, or is a no-op if the lock was not entered. |
| TryEnterWriteGuard(int) | Tries to enter the lock in write mode within the given timeout, returning a guard whose IsEntered property indicates whether the lock was entered. Disposal releases the lock, or is a no-op if the lock was not entered. |
| TryEnterWriteGuard(TimeSpan) | Tries to enter the lock in write mode within the given timeout, returning a guard whose IsEntered property indicates whether the lock was entered. Disposal releases the lock, or is a no-op if the lock was not entered. |