Example #1
0
/*
 * Attempt to lock for write access, don't wait
 */
int devlock::writetrylock()
{
    devlock *rwl = this;
    int status, status2;

    if (rwl->valid != DEVLOCK_VALID) {
        return EINVAL;
    }
    if ((status = pthread_mutex_lock(&rwl->mutex)) != 0) {
        return status;
    }
    if (rwl->w_active && pthread_equal(rwl->writer_id, pthread_self())) {
        rwl->w_active++;
        pthread_mutex_unlock(&rwl->mutex);
        return 0;
    }
    if (rwl->w_active || rwl->r_active > 0) {
        status = EBUSY;
    } else {
        rwl->w_active = 1;              /* we are running */
        rwl->writer_id = pthread_self(); /* save writer thread's id */
        lmgr_do_lock(rwl, rwl->priority, __FILE__, __LINE__);
    }
    status2 = pthread_mutex_unlock(&rwl->mutex);
    return (status == 0 ? status2 : status);
}
Example #2
0
/*
 * Attempt to lock for write access, don't wait
 */
int rwl_writetrylock(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 && pthread_equal(rwl->writer_id, pthread_self())) {
      rwl->w_active++;
      pthread_mutex_unlock(&rwl->mutex);
      return 0;
   }
   if (rwl->w_active || rwl->r_active > 0) {
      stat = EBUSY;
   } else {
      rwl->w_active = 1;              /* we are running */
      rwl->writer_id = pthread_self(); /* save writer thread's id */
      lmgr_do_lock(rwl, rwl->priority, __FILE__, __LINE__);
   }
   stat2 = pthread_mutex_unlock(&rwl->mutex);
   return (stat == 0 ? stat2 : stat);
}