title: Any problems in this rwlock implementation?
body: <p>I just implemented a reader-writer lock in C. I want to limit the number of readers, so I use 'num' to count it. I'm not sure whether this implementation has some potential data race or deadlock conditions. So could you help me figuring them out please?</p>
  
  <p>Another question is can I remove the 'spin_lock' in struct _rwlock in someway? Thanks!   </p>
  
  <pre><code>#define MAX_READER 16; 
  typedef _rwlock *rwlock;
  struct _rwlock{
      spin_lock   lk;
      unint32_t   num;
  };
  void wr_lock(rwlock lock){
      while (1){
          if (lock-&gt;num &gt; 0) continue;
          lock(lock-&gt;lk);
          lock-&gt;num += MAX_READER;
          return;
      }
  }
  void wr_unlock(rwlock lock){
      lock-&gt;num -= MAX_READER;
      unlock(lock-&gt;lk);
  }
  void rd_lock(rwlock lock){
      while (1){
          if (lock-&gt;num &gt;= MAX_READER) continue;
          atom_inc(num);
          return;
      }
  }
  void rd_unlock(rwlock lock){
      atom_dec(num);
  }
  </code></pre>