public void TestConstructor() { ReentryGuard test = new ReentryGuard(); Assert.True(test.CanEnter); Assert.False(test.HasEntered); }
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); }
public WeakDelegateCollection(bool autoRemoveDeadItems, bool reentryGuard) : base(autoRemoveDeadItems) { if (reentryGuard) { _invoke = InvokeExtracted; } else { var guard = new ReentryGuard(); _invoke = input => guard.Execute(() => InvokeExtracted(input)); } }
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)); } }
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); }
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)); } }
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); }
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) { } } }); } }
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); }
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); }
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); }