]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/rwlock.c
04Jul05
[bacula/bacula] / bacula / src / lib / rwlock.c
index 4ec8b858092496695a410c4bbea77aa9688e6526..16ce457fb043659370e3591d86376d835d82f3ef 100644 (file)
@@ -13,7 +13,7 @@
  *
  */
 /*
-   Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
+   Copyright (C) 2000-2004 Kern Sibbald and John Walker
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
@@ -34,7 +34,7 @@
 
 #include "bacula.h"
 
-/*   
+/*
  * Initialize a read/write lock
  *
  *  Returns: 0 on success
@@ -43,7 +43,7 @@
 int rwl_init(brwlock_t *rwl)
 {
    int stat;
-                        
+
    rwl->r_active = rwl->w_active = 0;
    rwl->r_wait = rwl->w_wait = 0;
    if ((stat = pthread_mutex_init(&rwl->mutex, NULL)) != 0) {
@@ -79,7 +79,7 @@ int rwl_destroy(brwlock_t *rwl)
      return stat;
   }
 
-  /* 
+  /*
    * If any threads are active, report EBUSY
    */
   if (rwl->r_active > 0 || rwl->w_active) {
@@ -90,7 +90,7 @@ int rwl_destroy(brwlock_t *rwl)
   /*
    * If any threads are waiting, report EBUSY
    */
-  if (rwl->r_wait > 0 || rwl->w_wait > 0) { 
+  if (rwl->r_wait > 0 || rwl->w_wait > 0) {
      pthread_mutex_unlock(&rwl->mutex);
      return EBUSY;
   }
@@ -135,7 +135,7 @@ static void rwl_write_release(void *arg)
 int rwl_readlock(brwlock_t *rwl)
 {
    int stat;
-    
+
    if (rwl->valid != RWLOCK_VALID) {
       return EINVAL;
    }
@@ -161,13 +161,13 @@ int rwl_readlock(brwlock_t *rwl)
    return stat;
 }
 
-/* 
+/*
  * Attempt to lock for read access, don't wait
  */
 int rwl_readtrylock(brwlock_t *rwl)
 {
    int stat, stat2;
-    
+
    if (rwl->valid != RWLOCK_VALID) {
       return EINVAL;
    }
@@ -182,14 +182,14 @@ int rwl_readtrylock(brwlock_t *rwl)
    stat2 = pthread_mutex_unlock(&rwl->mutex);
    return (stat == 0 ? stat2 : stat);
 }
-   
-/* 
+
+/*
  * Unlock read lock
  */
 int rwl_readunlock(brwlock_t *rwl)
 {
    int stat, stat2;
-    
+
    if (rwl->valid != RWLOCK_VALID) {
       return EINVAL;
    }
@@ -198,7 +198,7 @@ int rwl_readunlock(brwlock_t *rwl)
    }
    rwl->r_active--;
    if (rwl->r_active == 0 && rwl->w_wait > 0) { /* if writers waiting */
-      stat = pthread_cond_signal(&rwl->write);
+      stat = pthread_cond_broadcast(&rwl->write);
    }
    stat2 = pthread_mutex_unlock(&rwl->mutex);
    return (stat == 0 ? stat2 : stat);
@@ -212,7 +212,7 @@ int rwl_readunlock(brwlock_t *rwl)
 int rwl_writelock(brwlock_t *rwl)
 {
    int stat;
-    
+
    if (rwl->valid != RWLOCK_VALID) {
       return EINVAL;
    }
@@ -236,20 +236,20 @@ int rwl_writelock(brwlock_t *rwl)
       rwl->w_wait--;                  /* we are no longer waiting */
    }
    if (stat == 0) {
-      rwl->w_active = 1;              /* we are running */
+      rwl->w_active++;                /* we are running */
       rwl->writer_id = pthread_self(); /* save writer thread's id */
    }
    pthread_mutex_unlock(&rwl->mutex);
    return stat;
 }
 
-/* 
+/*
  * 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;
    }
@@ -270,23 +270,26 @@ int rwl_writetrylock(brwlock_t *rwl)
    stat2 = pthread_mutex_unlock(&rwl->mutex);
    return (stat == 0 ? stat2 : stat);
 }
-   
-/* 
+
+/*
  * 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) {
+      Emsg0(M_ABORT, 0, "rwl_writeunlock called too many times.\n");
+   }
    rwl->w_active--;
-   if (rwl->w_active < 0 || !pthread_equal(pthread_self(), rwl->writer_id)) {
+   if (!pthread_equal(pthread_self(), rwl->writer_id)) {
       Emsg0(M_ABORT, 0, "rwl_writeunlock by non-owner.\n");
    }
    if (rwl->w_active > 0) {
@@ -296,7 +299,7 @@ int rwl_writeunlock(brwlock_t *rwl)
       if (rwl->r_wait > 0) {         /* if readers waiting */
          stat = pthread_cond_broadcast(&rwl->read);
       } else if (rwl->w_wait > 0) {
-         stat = pthread_cond_signal(&rwl->write);
+         stat = pthread_cond_broadcast(&rwl->write);
       }
    }
    stat2 = pthread_mutex_unlock(&rwl->mutex);
@@ -320,7 +323,7 @@ typedef struct thread_tag {
    int interval;
 } thread_t;
 
-/* 
+/*
  * Read/write lock and shared data.
  */
 typedef struct data_tag {
@@ -332,7 +335,7 @@ typedef struct data_tag {
 thread_t threads[THREADS];
 data_t data[DATASIZE];
 
-/* 
+/*
  * Thread start routine that uses read/write locks.
  */
 void *thread_routine(void *arg)
@@ -385,7 +388,7 @@ void *thread_routine(void *arg)
       }
    }
    if (repeats > 0) {
-      Dmsg2(000, "Thread %d found unchanged elements %d times\n",
+      Pmsg2(000, "Thread %d found unchanged elements %d times\n",
          self->thread_num, repeats);
    }
    return NULL;