Implementation of ordered lock pattern Below is attempt to catch potential deadlocks at runtime in C#. Basically this class allows user to acquire locks in a pre-defined order only. I ran some basic tests and it appears to work. Please review and let me know if you find something wrong with implementation.

public class OrderedLock
{
    [ThreadStatic]
    static HashSet&lt;int&gt; mAcquiredLocks;
    static ConcurrentDictionary&lt;int, object&gt; mCreatedLocks = new ConcurrentDictionary&lt;int, object&gt;();

    private int mId;
    private ThreadLocal&lt;int&gt; mRefCount = new ThreadLocal&lt;int&gt;(false);
    private ReaderWriterLockSlim mLock;

    public OrderedLock(int id)
    {
        if (!mCreatedLocks.TryAdd(id, null)) throw new ApplicationException("Duplicate identifier detected");

        mId = id;
        mRefCount.Value = 0;
        mLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
    }

    public void AcquireReadLock()
    {
        CheckLockOrder();
        mLock.EnterReadLock();
    }

    public void AcquireWriteLock()
    {
        CheckLockOrder();
        mLock.EnterWriteLock();
    }

    public void ReleaseReadLock()
    {
        mRefCount.Value -= 1;
        mLock.ExitReadLock();
        if (mRefCount.Value == 0) mAcquiredLocks.Remove(mId);
    }

    public void ReleaseWriteLock()
    {
        mRefCount.Value -= 1;
        mLock.ExitWriteLock();
        if (mRefCount.Value == 0) mAcquiredLocks.Remove(mId);
    }

    private void CheckLockOrder()
    {
        if (mAcquiredLocks == null) mAcquiredLocks = new HashSet&lt;int&gt;();
        if (!mAcquiredLocks.Contains(mId))
        {
            if (mAcquiredLocks.Count &gt; 0 &amp;&amp; mAcquiredLocks.Max() &gt; mId)
                throw new ApplicationException("Invalid order of locking detected");
            else
                mAcquiredLocks.Add(mId);
        }

        mRefCount.Value += 1;
    }
}
I haven't extensively tested it, but there are a few things I've noted:


The class has two members which hold on to IDisposable resources. Therefore, the class must implement IDisposable as well and Dispose() of those members.
Many of the class members are assigned at construction time only. Mark them as readonly to signify intent and to potentially allow the runtime to perform some optimizations.
Current naming conventions favor camelCasing over Hungarian Notation for class members.
+= 1 and -= 1 can be simplified to ++ and --, respectively.
ApplicationException has been out of favor for quite a long time.
Identify your class members with the this keyword.
Forgot to put this bullet point in on the first cut: replace .Count() &gt; 0 with .Any(). This is idiomatic to what you want, rather than how to do it. In other words, the implementation of the former could very well iterate over the entire collection to get the count, while the latter may just check for the existence of one in its collection.
Another omission: I replaced the declaration of HashSet&lt;int&gt; with ISet&lt;int&gt; since coding to interfaces allows for more flexibility in changing implementations later on.


All that being said, here's a first-cut rework:

public class OrderedLock : IDisposable
{
    private static readonly ConcurrentDictionary&lt;int, object&gt; createdLocks = new ConcurrentDictionary&lt;int, object&gt;();
    [ThreadStatic]
    private static ISet&lt;int&gt; acquiredLocks;

    private readonly ThreadLocal&lt;int&gt; refCount = new ThreadLocal&lt;int&gt;(false);
    private readonly ReaderWriterLockSlim locker = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
    private readonly int id;

    /// &lt;exception cref="InvalidOperationException"&gt;Duplicate identifier detected&lt;/exception&gt;
    public OrderedLock(int id)
    {
        if (!createdLocks.TryAdd(id, null))
        {
            throw new InvalidOperationException("Duplicate identifier detected");
        }

        this.id = id;
        this.refCount.Value = 0;
    }

    public void AcquireReadLock()
    {
        this.CheckLockOrder();
        this.locker.EnterReadLock();
    }

    public void AcquireWriteLock()
    {
        this.CheckLockOrder();
        this.locker.EnterWriteLock();
    }

    public void ReleaseReadLock()
    {
        this.refCount.Value--;
        this.locker.ExitReadLock();
        if (this.refCount.Value == 0)
        {
            acquiredLocks.Remove(this.id);
        }
    }

    public void ReleaseWriteLock()
    {
        this.refCount.Value--;
        this.locker.ExitWriteLock();
        if (this.refCount.Value == 0)
        {
            acquiredLocks.Remove(this.id);
        }
    }

    public void Dispose()
    {
        while (this.locker.IsWriteLockHeld)
        {
            this.ReleaseWriteLock();
        }

        while (this.locker.IsReadLockHeld)
        {
            ReleaseReadLock();
        }

        this.locker.Dispose();
        this.refCount.Dispose();
        GC.SuppressFinalize(this);
    }

    /// &lt;exception cref="InvalidOperationException"&gt;Invalid order of locking detected&lt;/exception&gt;
    private void CheckLockOrder()
    {
        if (acquiredLocks == null)
        {
            acquiredLocks = new HashSet&lt;int&gt;();
        }

        if (!acquiredLocks.Contains(this.id))
        {
            if (acquiredLocks.Any() &amp;&amp; acquiredLocks.Max() &gt; this.id)
            {
                throw new InvalidOperationException("Invalid order of locking detected");
            }

            acquiredLocks.Add(this.id);
        }

        this.refCount.Value++;
    }
}