Beispiel #1
0
        public override void Init()
        {
            bits     = new List <bool>(Length);
            ReadLock = new OrderedLock();
            random   = new Random();
            Mins     = new ushort[Length];
            Maxs     = new ushort[Length];

            for (int i = 0; i < Length; i++)
            {
                Maxs[i] = 65535;
            }
        }
        public void AlreadyOwnedTwoInnerLocks()
        {
            DateTime start, end;

            object monitor = new object();

            lock (monitor)
            {
                start = DateTime.UtcNow;
                for (int i = 0; i < Iterations; i++)
                {
                    lock (monitor)
                    {
                    }
                }
                end = DateTime.UtcNow;
            }
            TimeSpan nativeTime = end - start;

            OrderedLock orderedLock = new OrderedLock(Timeout.Infinite);
            OrderedLock inner1      = new OrderedLock(Timeout.Infinite);
            OrderedLock inner2      = new OrderedLock(Timeout.Infinite);

            orderedLock.InnerLock = inner1;
            inner1.InnerLock      = inner2;

            using (orderedLock.Lock())
            {
                start = DateTime.UtcNow;
                for (int i = 0; i < Iterations; i++)
                {
                    using (orderedLock.Lock())
                    {
                    }
                }
                end = DateTime.UtcNow;
            }
            TimeSpan syncLockTime = end - start;
            double   factor       = syncLockTime.TotalMilliseconds / nativeTime.TotalMilliseconds;

            Console.WriteLine("Performance with two inner locks (lock already owned):");
            Console.WriteLine("Native: {0}", nativeTime);
            Console.WriteLine("OrderedLock: {0}", syncLockTime);
            Console.WriteLine("Performance penalty factor: {0:0.00}", factor);
            Console.WriteLine();

            Assert.IsTrue(factor < 10, "OrderedLock must not be ridiculously slow");
        }