public void TestConstructor()
        {
            ReentryGuard test = new ReentryGuard();

            Assert.True(test.CanEnter);
            Assert.False(test.HasEntered);
        }
示例#2
0
        public void ThreadedReentry()
        {
            var guard = new ReentryGuard();
            var count = 0;

            void Action()
            {
                if (count >= 255)
                {
                    return;
                }

                count++;
                guard.Execute(Action);
            }

            var a = new Thread(() => guard.Execute(Action));
            var b = new Thread(() => guard.Execute(Action));

            a.Start();
            b.Start();
            a.Join();
            b.Join();
            Assert.AreEqual(255, count);
        }
        public void TestFailedEntry()
        {
            ReentryGuard test = new ReentryGuard();

            using (test.EnterAndExit())
            {
                Assert.Throws <InvalidOperationException>(delegate { test.EnterAndExit(); });
            }
        }
        public void TestEnterAndExit()
        {
            ReentryGuard test = new ReentryGuard();

            Assert.True(test.CanEnter);
            using (test.EnterAndExit())
            {
                Assert.True(!test.CanEnter);
            }
            Assert.True(test.CanEnter);
        }
示例#5
0
 public WeakDelegateCollection(bool autoRemoveDeadItems, bool reentryGuard)
     : base(autoRemoveDeadItems)
 {
     if (reentryGuard)
     {
         _invoke = InvokeExtracted;
     }
     else
     {
         var guard = new ReentryGuard();
         _invoke = input => guard.Execute(() => InvokeExtracted(input));
     }
 }
示例#6
0
 public WeakDelegateCollection(bool autoRemoveDeadItems, bool freeReentry)
     : base(autoRemoveDeadItems)
 {
     if (freeReentry)
     {
         _invoke = InvokeExtracted;
         _invokeWithException = InvokeExtracted;
     }
     else
     {
         var guard = new ReentryGuard();
         _invoke = input => guard.Execute(() => InvokeExtracted(input));
         _invokeWithException = (onException, input) => guard.Execute(() => InvokeExtracted(onException, input));
     }
 }
示例#7
0
 public void SingleThreadReentry()
 {
     var guard = new ReentryGuard();
     int count = 0;
     Action action = null;
     action = () =>
     {
         if (count < 255)
         {
             count++;
             guard.Execute(action);
         }
     };
     guard.Execute(action);
     Assert.AreEqual(255, count);
 }
示例#8
0
        public StrongDelegateCollection(bool freeReentry)
        {
            IEqualityComparer <Delegate> comparer = EqualityComparer <Delegate> .Default;

            _wrapped = new SafeCollection <Delegate>(comparer);
            if (freeReentry)
            {
                _invoke = InvokeExtracted;
                _invokeWithException = InvokeExtracted;
            }
            else
            {
                var guard = new ReentryGuard();
                _invoke = input => guard.Execute(() => InvokeExtracted(input));
                _invokeWithException = (onException, input) => guard.Execute(() => InvokeExtracted(onException, input));
            }
        }
示例#9
0
        public void SingleThreadReentry()
        {
            var    guard  = new ReentryGuard();
            var    count  = 0;
            Action action = null;

            action = () =>
            {
                if (count < 255)
                {
                    count++;
                    guard.Execute(action);
                }
            };
            guard.Execute(action);
            Assert.AreEqual(255, count);
        }
示例#10
0
        public void SingleThreadReentry()
        {
            var guard = new ReentryGuard();
            var count = 0;

            void Action()
            {
                if (count >= 255)
                {
                    return;
                }

                count++;
                guard.Execute(Action);
            }

            guard.Execute(Action);
            Assert.AreEqual(255, count);
        }
        public void TestEnterAndExitMultiple()
        {
            ReentryGuard test = new ReentryGuard();

            using (IDisposable guard = test.EnterAndExitMultiple())
            {
                Assert.True(test.HasEntered);
                Assert.False(test.CanEnter);
                Assert.DoesNotThrow(delegate
                {
                    using (guard)
                    {
                        using (guard)
                        {
                        }
                    }
                });
            }
        }
示例#12
0
        internal bool Donate(T entry)
        {
            // Assume anything could have been set to null, start no sync operation, this could be running during DomainUnload
            if (entry != null && ReentryGuard.Enter(_reentryGuardId))
            {
                try
                {
                    var entries  = _entries;
                    var recycler = _recycler;
                    if (entries == null || recycler == null)
                    {
                        return(false);
                    }

                    recycler.Invoke(entry);
                    return(entries.TryAdd(entry));
                }
                catch (ObjectDisposedException exception)
                {
                    No.Op(exception);
                }
                catch (InvalidOperationException exception)
                {
                    No.Op(exception);
                }
                catch (NullReferenceException exception)
                {
                    No.Op(exception);
                }
                finally
                {
                    ReentryGuard.Leave(_reentryGuardId);
                }
            }

            if (entry is IDisposable disposable)
            {
                disposable.Dispose();
            }

            return(false);
        }
示例#13
0
 public void ThreadedReentry()
 {
     var guard = new ReentryGuard();
     int count = 0;
     Action action = null;
     action = () =>
     {
         if (count < 255)
         {
             count++;
             guard.Execute(action);
         }
     };
     var a = new Thread(() => guard.Execute(action));
     var b = new Thread(() => guard.Execute(action));
     a.Start();
     b.Start();
     a.Join();
     b.Join();
     Assert.AreEqual(255, count);
 }
示例#14
0
        public void ThreadedReentry()
        {
            var    guard  = new ReentryGuard();
            var    count  = 0;
            Action action = null;

            action = () =>
            {
                if (count < 255)
                {
                    count++;
                    guard.Execute(action);
                }
            };
            var a = new Thread(() => guard.Execute(action));
            var b = new Thread(() => guard.Execute(action));

            a.Start();
            b.Start();
            a.Join();
            b.Join();
            Assert.AreEqual(255, count);
        }