ReaderWriterLock AcquireLock/ReleaseLock must be both called within the same method
Warns when System.Threading.ReaderWriterLock acquire and release (reader or writer) methods are not called within the same method: harder to locate where critical sections begin and end, and complex error-prone scenarios.
How to fix
Refactor so ReaderWriterLock read or write critical sections begin and end in the same method.
CQLinq source
This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.
// <Name>ReaderWriterLock AcquireLock/ReleaseLock must be both called within the same method</Name>
// <Id>ND2503:ReaderWriterLockAcquireLockReleaseLockMustBeBothCalledWithinTheSameMethod</Id>
warnif count > 0
let acquireLockMethods = ThirdParty.Methods.WithFullNameWildcardMatch(
"System.Threading.ReaderWriterLock.Acquire*Lock(*")
let releaseLockMethods = ThirdParty.Methods.WithFullNameWildcardMatch(
"System.Threading.ReaderWriterLock.Release*Lock(*")
from m in Application.Methods.UsingAny(acquireLockMethods)
.Except(Application.Methods.UsingAny(releaseLockMethods))
select new {
m,
acquireLockMethods = m.MethodsCalled.Intersect(acquireLockMethods),
Debt = 20.ToMinutes().ToDebt(),
Severity = Severity.High
}
//<Expl>
// The *{0}* calls *{1}* but doesn't call corresponding *ReaderWriterLock.ReleaseLock()* method{1?:s}.
// Refactor the code so that a *critical section* is contained within a single method.
//</Expl>
//<Desc>
// Warns when **System.Threading.ReaderWriterLock** acquire and release
// (reader or writer) methods are not called within the same method: harder
// to locate where **critical sections** begin and end, and complex
// error-prone scenarios.
//</Desc>
//<HowToFix>
// Refactor so *ReaderWriterLock read or write critical sections* begin and
// end in the same method.
//</HowToFix>
Open in the interactive Rules Explorer · All default rules
.NET Framework Usage / System.Threading: all 6 items
- Don't create threads explicitly ND2500
- Don't use dangerous threading methods ND2501
- Monitor TryEnter/Exit must be both called within the same method ND2502
- ReaderWriterLock AcquireLock/ReleaseLock must be both called within the same method ND2503
- Don't tag instance fields with ThreadStaticAttribute ND2504
- Method non-synchronized that read mutable states ND2505