title: RW locks with hierarchy locking
body: <p>I am trying to build an hierarchy locking system to avoid deadlocks. Meaning break the code into layers (or levels) and at each level to only allow locks in the next level. Inside the same level you should lock with predefined order. This prevents most deadlocks.
  Something like this <a href="http://www.drdobbs.com/parallel/use-lock-hierarchies-to-avoid-deadlock/204801163?pgno=1" rel="nofollow">http://www.drdobbs.com/parallel/use-lock-hierarchies-to-avoid-deadlock/204801163?pgno=1</a></p>
  
  <p>For mutex this will work like a charm but for read write locks it fails. For instance if I take a read lock and then want to turn it to write lock it means I am taking a new lock in the same level, which is forbidden with this algorithem.</p>
  
  <p>How do you change the algorithm to work for RW and mutex locks? Is there some other algorithm I should consider?</p>
  
  <p>PS: I am programing C++ but the question is actually rellevant as a theoretical one also</p>