src/lib/rwlock.c
int rwl_readlock(brwlock_t *rwl)
{
   int stat;

   if (rwl->valid != RWLOCK_VALID) {
      return EINVAL;
   }
   if ((stat = pthread_mutex_lock(&rwl->mutex)) != 0) {
      return stat;
   }
   if (rwl->w_active) {
      rwl->r_wait++;                  /* indicate that we are waiting */
      pthread_cleanup_push(rwl_read_release, (void *)rwl);
      while (rwl->w_active) {
         stat = pthread_cond_wait(&rwl->read, &rwl->mutex);
         if (stat != 0) {
            break;                    /* error, bail out */
         }
      }
      pthread_cleanup_pop(0);
      rwl->r_wait--;                  /* we are no longer waiting */
   }
   if (stat == 0) {
      rwl->r_active++;                /* we are running */
   }
   pthread_mutex_unlock(&rwl->mutex);
   return stat;
}
/*
 * Lock for read access, wait until locked (or error).
 */
Lock for read access, wait until locked (or error).
Lock for read access wait until locked ( or error ) .
int rwl_readlock(brwlock_t *rwl)
{
   int stat;
   if (rwl->valid != RWLOCK_VALID) {
      return EINVAL;
   }
   if ((stat = pthread_mutex_lock(&rwl->mutex)) != 0) {
      return stat;
   }
   if (rwl->w_active) {
      rwl->r_wait++;                  
      pthread_cleanup_push(rwl_read_release, (void *)rwl);
      while (rwl->w_active) {
         stat = pthread_cond_wait(&rwl->read, &rwl->mutex);
         if (stat != 0) {
            break;                    
         }
      }
      pthread_cleanup_pop(0);
      rwl->r_wait--;                  
   }
   if (stat == 0) {
      rwl->r_active++;                
   }
   pthread_mutex_unlock(&rwl->mutex);
   return stat;
}
int rwl_readlock ( brwlock_t * rwl ) { int stat ; if ( rwl -> valid != RWLOCK_VALID ) { return EINVAL ; } if ( ( stat = pthread_mutex_lock ( & rwl -> mutex ) ) != 0 ) { return stat ; } if ( rwl -> w_active ) { rwl -> r_wait ++ ; pthread_cleanup_push ( rwl_read_release , ( void * ) rwl ) ; while ( rwl -> w_active ) { stat = pthread_cond_wait ( & rwl -> read , & rwl -> mutex ) ; if ( stat != 0 ) { break ; } } pthread_cleanup_pop ( 0 ) ; rwl -> r_wait -- ; } if ( stat == 0 ) { rwl -> r_active ++ ; } pthread_mutex_unlock ( & rwl -> mutex ) ; return stat ; }
Lock for read access, wait until locked (or error).
Lock for read access wait until locked ( or error ) .
/* indicate that we are waiting */ /* error, bail out */ /* we are no longer waiting */ /* we are running */