Struct UpgradeableReadGuard
Represents a disposable upgradeable read guard over a ReadWriteLock. The guard has read access to the protected resource and can temporarily upgrade to scoped write access (see EnterUpgradedWriteGuard()) or permanently downgrade to a plain read lock (see DowngradeToReadLock()).
Implements
Namespace: Singulink.Threading
Assembly: Singulink.Threading.dll
Syntax
public struct UpgradeableReadGuard : IDisposable
Remarks
Guards returned from TryEnter methods may not have entered the lock, so check
IsEntered after acquiring. Disposing a guard that did not enter the lock is a safe no-op,
so guards can always be assigned directly to a using declaration.
Guards are thread-affine: all operations on an entered guard must be performed on the thread that entered the lock, which is enforced with an InvalidOperationException when violated.
Properties
| Name | Description |
|---|---|
| IsDefault | Gets a value indicating whether the instance is in its default, uninitialized state. |
| IsDisposed | Gets a value indicating whether the object has been disposed. |
| IsDowngraded | Gets a value indicating whether the guard has been downgraded to a plain read lock via DowngradeToReadLock(). |
| IsEntered | Gets a value indicating whether the guard entered the lock. Always true for guards
obtained from EnterUpgradeableReadGuard(); guards obtained from
|
| IsUpgraded | Gets a value indicating whether the guard is currently upgraded to write access (i.e. a guard returned from EnterUpgradedWriteGuard() or a successful TryEnterUpgradedWriteGuard(int) has not been disposed yet). |
Methods
| Name | Description |
|---|---|
| Dispose() | Releases whatever the guard currently holds: the upgradeable read lock (and the write lock first, if an upgraded write guard was abandoned without being disposed), or the read lock if the guard was downgraded. No-op if the guard did not enter the lock. |
| DowngradeToReadLock() | Downgrades the guard to a plain read lock, allowing another thread to enter upgradeable read (or write) mode while this thread retains read access. The downgrade is gapless (no other thread can enter write mode during the transition) and never blocks. Downgrading is one-way: the guard cannot be upgraded afterward, and disposal releases the read lock. |
| EnterUpgradedWriteGuard() | Upgrades to write access, returning a write guard whose disposal exits write mode and returns this thread to upgradeable read mode. The guard can be upgraded again after the returned write guard is disposed, so scoped write access can be repeated within one upgradeable read session. |
| TryEnterUpgradedWriteGuard(int) | Tries to upgrade to write access within the given timeout, returning a write guard whose IsEntered property indicates whether write mode was entered. Disposing the returned guard exits write mode (returning this thread to upgradeable read mode), or is a no-op if write mode was not entered. |
| TryEnterUpgradedWriteGuard(TimeSpan) | Tries to upgrade to write access within the given timeout, returning a write guard whose IsEntered property indicates whether write mode was entered. Disposing the returned guard exits write mode (returning this thread to upgradeable read mode), or is a no-op if write mode was not entered. |