*
*/
/*
- 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
* Returns: 0 on success
* errno on failure
*/
-int rwl_init(rwlock_t *rwl)
+int rwl_init(brwlock_t *rwl)
{
int stat;
* Returns: 0 on success
* errno on failure
*/
-int rwl_destroy(rwlock_t *rwl)
+int rwl_destroy(brwlock_t *rwl)
{
int stat, stat1, stat2;
*/
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);
*/
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);
/*
* Lock for read access, wait until locked (or error).
*/
-int rwl_readlock(rwlock_t *rwl)
+int rwl_readlock(brwlock_t *rwl)
{
int stat;
/*
* Attempt to lock for read access, don't wait
*/
-int rwl_readtrylock(rwlock_t *rwl)
+int rwl_readtrylock(brwlock_t *rwl)
{
int stat, stat2;
/*
* Unlock read lock
*/
-int rwl_readunlock(rwlock_t *rwl)
+int rwl_readunlock(brwlock_t *rwl)
{
int stat, stat2;
* 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;
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);
/*
* Attempt to lock for write access, don't wait
*/
-int rwl_writetrylock(rwlock_t *rwl)
+int rwl_writetrylock(brwlock_t *rwl)
{
int stat, stat2;
* 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;
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) {
* Read/write lock and shared data.
*/
typedef struct data_tag {
- rwlock_t lock;
+ brwlock_t lock;
int data;
int writes;
} data_t;
}
}
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;
#ifdef TEST_RW_TRY_LOCK
/*
- * rwlock_try_main.c
+ * brwlock_try_main.c
*
* Demonstrate use of non-blocking read-write locks.
*
* Read-write lock and shared data
*/
typedef struct data_tag {
- rwlock_t lock;
+ brwlock_t lock;
int data;
int updates;
} data_t;