]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/rwlock.c
Fix reporting jobs from state file + misc
[bacula/bacula] / bacula / src / lib / rwlock.c
index 60e45dab34a1c406b5a867ff8e23b2fce3481d3b..f6912b519de5eac9632687353607cabc47dd1706 100644 (file)
@@ -13,7 +13,7 @@
  *
  */
 /*
-   Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
+   Copyright (C) 2000-2003 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
@@ -40,7 +40,7 @@
  *  Returns: 0 on success
  *          errno on failure
  */
-int rwl_init(rwlock_t *rwl)
+int rwl_init(brwlock_t *rwl)
 {
    int stat;
                        
@@ -68,7 +68,7 @@ int rwl_init(rwlock_t *rwl)
  * Returns: 0 on success
  *         errno on failure
  */
-int rwl_destroy(rwlock_t *rwl)
+int rwl_destroy(brwlock_t *rwl)
 {
    int stat, stat1, stat2;
 
@@ -111,7 +111,7 @@ int rwl_destroy(rwlock_t *rwl)
  */
 static void rwl_read_release(void *arg)
 {
-   rwlock_t *rwl = (rwlock_t *)arg;
+   brwlock_t *rwl = (brwlock_t *)arg;
 
    rwl->r_wait--;
    pthread_mutex_unlock(&rwl->mutex);
@@ -123,7 +123,7 @@ static void rwl_read_release(void *arg)
  */
 static void rwl_write_release(void *arg)
 {
-   rwlock_t *rwl = (rwlock_t *)arg;
+   brwlock_t *rwl = (brwlock_t *)arg;
 
    rwl->w_wait--;
    pthread_mutex_unlock(&rwl->mutex);
@@ -132,7 +132,7 @@ static void rwl_write_release(void *arg)
 /*
  * Lock for read access, wait until locked (or error).
  */
-int rwl_readlock(rwlock_t *rwl)
+int rwl_readlock(brwlock_t *rwl)
 {
    int stat;
     
@@ -164,7 +164,7 @@ int rwl_readlock(rwlock_t *rwl)
 /* 
  * Attempt to lock for read access, don't wait
  */
-int rwl_readtrylock(rwlock_t *rwl)
+int rwl_readtrylock(brwlock_t *rwl)
 {
    int stat, stat2;
     
@@ -186,7 +186,7 @@ int rwl_readtrylock(rwlock_t *rwl)
 /* 
  * Unlock read lock
  */
-int rwl_readunlock(rwlock_t *rwl)
+int rwl_readunlock(brwlock_t *rwl)
 {
    int stat, stat2;
     
@@ -209,7 +209,7 @@ int rwl_readunlock(rwlock_t *rwl)
  * Lock for write access, wait until locked (or error).
  *   Multiple nested write locking is permitted.
  */
-int rwl_writelock(rwlock_t *rwl)
+int rwl_writelock(brwlock_t *rwl)
 {
    int stat;
     
@@ -236,7 +236,7 @@ int rwl_writelock(rwlock_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);
@@ -246,7 +246,7 @@ int rwl_writelock(rwlock_t *rwl)
 /* 
  * Attempt to lock for write access, don't wait
  */
-int rwl_writetrylock(rwlock_t *rwl)
+int rwl_writetrylock(brwlock_t *rwl)
 {
    int stat, stat2;
     
@@ -275,7 +275,7 @@ int rwl_writetrylock(rwlock_t *rwl)
  * Unlock write lock
  *  Start any waiting writers in preference to waiting readers
  */
-int rwl_writeunlock(rwlock_t *rwl)
+int rwl_writeunlock(brwlock_t *rwl)
 {
    int stat, stat2;
     
@@ -285,8 +285,11 @@ int rwl_writeunlock(rwlock_t *rwl)
    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) {
@@ -324,7 +327,7 @@ typedef struct thread_tag {
  * Read/write lock and shared data.
  */
 typedef struct data_tag {
-   rwlock_t lock;
+   brwlock_t lock;
    int data;
    int writes;
 } data_t;
@@ -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;
@@ -470,7 +473,7 @@ int main (int argc, char *argv[])
 
 #ifdef TEST_RW_TRY_LOCK
 /*
- * rwlock_try_main.c
+ * brwlock_try_main.c
  *
  * Demonstrate use of non-blocking read-write locks.
  *
@@ -502,7 +505,7 @@ typedef struct thread_tag {
  * Read-write lock and shared data
  */
 typedef struct data_tag {
-    rwlock_t   lock;
+    brwlock_t   lock;
     int        data;
     int        updates;
 } data_t;