src/lib/rwlock.c
int rwl_writeunlock(brwlock_t *rwl)
{
   int stat, stat2;

   if (rwl->valid != RWLOCK_VALID) {
      return EINVAL;
   }
   if ((stat = pthread_mutex_lock(&rwl->mutex)) != 0) {
      return stat;
   }
   if (rwl->w_active <= 0) {
      pthread_mutex_unlock(&rwl->mutex);
      Jmsg0(NULL, M_ABORT, 0, _("rwl_writeunlock called too many times.\n"));
   }
   rwl->w_active--;
   if (!pthread_equal(pthread_self(), rwl->writer_id)) {
      pthread_mutex_unlock(&rwl->mutex);
      Jmsg0(NULL, M_ABORT, 0, _("rwl_writeunlock by non-owner.\n"));
   }
   if (rwl->w_active > 0) {
      stat = 0;                       /* writers still active */
   } else {
      lmgr_do_unlock(rwl);
      /* No more writers, awaken someone */
      if (rwl->r_wait > 0) {         /* if readers waiting */
         stat = pthread_cond_broadcast(&rwl->read);
      } else if (rwl->w_wait > 0) {
         stat = pthread_cond_broadcast(&rwl->write);
      }
   }
   stat2 = pthread_mutex_unlock(&rwl->mutex);
   return (stat == 0 ? stat2 : stat);
}
/*
 * Unlock write lock
 *  Start any waiting writers in preference to waiting readers
 */
Unlock write lock
Start any waiting writers in preference to waiting readers
Unlock write lock Start any waiting writers in preference to waiting readers
int rwl_writeunlock(brwlock_t *rwl)
{
   int stat, stat2;
   if (rwl->valid != RWLOCK_VALID) {
      return EINVAL;
   }
   if ((stat = pthread_mutex_lock(&rwl->mutex)) != 0) {
      return stat;
   }
   if (rwl->w_active <= 0) {
      pthread_mutex_unlock(&rwl->mutex);
      Jmsg0(NULL, M_ABORT, 0, _("rwl_writeunlock called too many times.\n"));
   }
   rwl->w_active--;
   if (!pthread_equal(pthread_self(), rwl->writer_id)) {
      pthread_mutex_unlock(&rwl->mutex);
      Jmsg0(NULL, M_ABORT, 0, _("rwl_writeunlock by non-owner.\n"));
   }
   if (rwl->w_active > 0) {
      stat = 0;                       
   } else {
      lmgr_do_unlock(rwl);
      if (rwl->r_wait > 0) {         
         stat = pthread_cond_broadcast(&rwl->read);
      } else if (rwl->w_wait > 0) {
         stat = pthread_cond_broadcast(&rwl->write);
      }
   }
   stat2 = pthread_mutex_unlock(&rwl->mutex);
   return (stat == 0 ? stat2 : stat);
}
int rwl_writeunlock ( brwlock_t * rwl ) { int stat , stat2 ; if ( rwl -> valid != RWLOCK_VALID ) { return EINVAL ; } if ( ( stat = pthread_mutex_lock ( & rwl -> mutex ) ) != 0 ) { return stat ; } if ( rwl -> w_active <= 0 ) { pthread_mutex_unlock ( & rwl -> mutex ) ; Jmsg0 ( NULL , M_ABORT , 0 , _ ( " \n " ) ) ; } rwl -> w_active -- ; if ( ! pthread_equal ( pthread_self ( ) , rwl -> writer_id ) ) { pthread_mutex_unlock ( & rwl -> mutex ) ; Jmsg0 ( NULL , M_ABORT , 0 , _ ( " \n " ) ) ; } if ( rwl -> w_active > 0 ) { stat = 0 ; } else { lmgr_do_unlock ( rwl ) ; if ( rwl -> r_wait > 0 ) { stat = pthread_cond_broadcast ( & rwl -> read ) ; } else if ( rwl -> w_wait > 0 ) { stat = pthread_cond_broadcast ( & rwl -> write ) ; } } stat2 = pthread_mutex_unlock ( & rwl -> mutex ) ; return ( stat == 0 ? stat2 : stat ) ; }
Unlock write lock
  Start any waiting writers in preference to waiting readers
Unlock write lock Start any waiting writers in preference to waiting readers
/* writers still active */ /* No more writers, awaken someone */ /* if readers waiting */