public virtual bool TryLockChildren(LockTypes lockType, TimeSpan timeout, out IDisposable childDisposables) { var all_child_disposables = new CompositeDisposable(); childDisposables = all_child_disposables; bool all_good = true; foreach(var child in ChildLocks) { if (!all_good) break; if (lockType == LockTypes.Read) { var acqusition = (IReadLockAcquisition)null; if (child.TryAcquireReadLock(timeout, out acqusition)) { all_child_disposables.AddIfNotNull(acqusition); } else { all_good = false; } } else if (lockType == LockTypes.UpgradeableRead) { var acqusition = (IUpgradeableReadLockAcquisition)null; if (child.TryAcquireUpgradeableReadLock(timeout, out acqusition)) { all_child_disposables.AddIfNotNull(acqusition); } else { all_good = false; } } else if (lockType == LockTypes.Write) { var acqusition = (IWriteLockAcquisition)null; if (child.TryAcquireWriteLock(timeout, out acqusition)) { all_child_disposables.AddIfNotNull(acqusition); } else { all_good = false; } } else { throw new NotSupportedException(lockType.ToString()); } } if(all_good) { return true; } else { all_child_disposables.Clear(); return false; } }
protected virtual IDisposable LockChildren(LockTypes lockType) { var result = new CompositeDisposable(); lock (ChildLocksSync) { foreach (var child in ChildLocks) { if (lockType == LockTypes.Read) { result.AddIfNotNull(child.AcquireReadLockIfNotHeld()); } else if (lockType == LockTypes.UpgradeableRead) { result.AddIfNotNull(child.AcquireUpgradeableReadLock()); } else if (lockType == LockTypes.Write) { result.AddIfNotNull(child.AcquireWriteLockIfNotHeld()); } else { throw new NotSupportedException(lockType.ToString()); } } } return result; }