// A useful utility but it's completely fine to create your own write lock under an upgradable one. public void UpgradeToWriteLock(int?millisecondsTimeout = null) { if (!LockHeld) { throw new InvalidOperationException("Cannot upgrade a lock when the lock was not held."); } lock (_sync) { if (_upgraded != null) { throw new InvalidOperationException("A write lock is already in effect."); } _upgraded = new WriteLock(_target, millisecondsTimeout); } }
public void Downgrade() { if (!LockHeld) { throw new InvalidOperationException("Cannot upgrade a lock when the lock was not held."); } lock (_sync) { if (_upgraded == null) { throw new InvalidOperationException("There is no write lock in effect to downgrade from."); } _upgraded.Dispose(); _upgraded = null; } }