Beispiel #1
0
        public void CompositeDisposable_Clear()
        {
            var disp1 = false;
            var disp2 = false;

            var d1 = Disposable.Create(() => { disp1 = true; });
            var d2 = Disposable.Create(() => { disp2 = true; });
            var g = new CompositeDisposable(d1, d2);
            Assert.AreEqual(2, g.Count);

            g.Clear();
            Assert.IsTrue(disp1);
            Assert.IsTrue(disp2);
            Assert.AreEqual(0, g.Count);

            var disp3 = false;
            var d3 = Disposable.Create(() => { disp3 = true; });
            g.Add(d3);
            Assert.IsFalse(disp3);
            Assert.AreEqual(1, g.Count);
        }
        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;
            }
        }