public void OnWaitOne(MethodInterceptionArgs args) { DeadlockMonitor.ExecuteAction( () => { if (!(args.Instance is Mutex)) { return; } if (args.Arguments.Count == 0 || args.Arguments[0] is bool || (args.Arguments[0] is int && (int)args.Arguments[0] == Timeout.Infinite)) { Mutex mutex = args.Instance as Mutex; bool?exitContext = args.Arguments.Count > 0 ? (bool?)args.Arguments[0] : null; LockAspectHelper.NoTimeoutAcquire( () => DeadlockMonitor.EnterWaiting(mutex, ResourceType.Lock), timeout => exitContext.HasValue ? mutex.WaitOne(timeout, exitContext.Value) : mutex.WaitOne(timeout), () => DeadlockMonitor.ConvertWaitingToAcquired(mutex, ResourceType.Lock), () => DeadlockMonitor.ExitWaiting(mutex, ResourceType.Lock)); } else { args.Proceed(); if ((bool)args.ReturnValue) { DeadlockMonitor.EnterAcquired(args.Instance, ResourceType.Lock); } } }); }
public void OnTryEnterWriteLock(MethodExecutionArgs args) { DeadlockMonitor.ExecuteAction( () => { if ((bool)args.ReturnValue) { DeadlockMonitor.EnterAcquired(args.Instance, ResourceType.Read); } }); }
public void OnWaitAll(MethodInterceptionArgs args) { DeadlockMonitor.ExecuteAction( () => { WaitHandle[] waitHandles = (WaitHandle[])args.Arguments[0]; if (args.Arguments.Count == 1 || (args.Arguments[0] is int && (int)args.Arguments[0] == Timeout.Infinite)) { bool?exitContext = args.Arguments.Count > 2 ? (bool?)args.Arguments[2] : null; LockAspectHelper.NoTimeoutAcquire( () => { foreach (Mutex mutex in waitHandles.OfType <Mutex>()) { DeadlockMonitor.EnterWaiting(mutex, ResourceType.Lock); } }, timeout => exitContext.HasValue ? WaitHandle.WaitAll(waitHandles, timeout, exitContext.Value) : WaitHandle.WaitAll(waitHandles, timeout), () => { foreach (Mutex mutex in waitHandles.OfType <Mutex>()) { DeadlockMonitor.ConvertWaitingToAcquired(mutex, ResourceType.Lock); } }, () => { foreach (Mutex mutex in waitHandles.OfType <Mutex>()) { DeadlockMonitor.ExitWaiting(mutex, ResourceType.Lock); } }); } else { args.Proceed(); if ((bool)args.ReturnValue) { foreach (Mutex mutex in waitHandles.OfType <Mutex>()) { DeadlockMonitor.EnterAcquired(mutex, ResourceType.Lock); } } } }); }