*
*/
/*
- 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
#include "bacula.h"
-/*
+/*
* Initialize a read/write lock
*
* Returns: 0 on success
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) {
return stat;
}
- /*
+ /*
* If any threads are active, report EBUSY
*/
if (rwl->r_active > 0 || rwl->w_active) {
/*
* 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;
}
int rwl_readlock(brwlock_t *rwl)
{
int stat;
-
+
if (rwl->valid != RWLOCK_VALID) {
return EINVAL;
}
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;
}
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;
}
}
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);
int rwl_writelock(brwlock_t *rwl)
{
int stat;
-
+
if (rwl->valid != RWLOCK_VALID) {
return EINVAL;
}
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;
}
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) {
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);
int interval;
} thread_t;
-/*
+/*
* Read/write lock and shared data.
*/
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)
}
}
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;