rwlock.c revision dafcb997e390efa4423883dafd100c975c4095d6
/*
* Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
* Copyright (C) 1998-2001, 2003 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: rwlock.c,v 1.37 2004/03/05 05:10:49 marka Exp $ */
#include <config.h>
#include <stddef.h>
#include <isc/platform.h>
#ifdef ISC_PLATFORM_USETHREADS
#ifndef RWLOCK_DEFAULT_READ_QUOTA
#define RWLOCK_DEFAULT_READ_QUOTA 4
#endif
#ifndef RWLOCK_DEFAULT_WRITE_QUOTA
#define RWLOCK_DEFAULT_WRITE_QUOTA 4
#endif
#ifdef ISC_RWLOCK_TRACE
static void
"rwlock %p thread %lu %s(%s): %s, %u active, "
"%u granted, %u rwaiting, %u wwaiting\n"),
(type == isc_rwlocktype_read ?
ISC_MSG_READ, "read") :
ISC_MSG_WRITE, "write")),
ISC_MSG_READING, "reading") :
ISC_MSG_WRITING, "writing")),
}
#endif
unsigned int write_quota)
{
/*
* In case there's trouble initializing, we zero magic now. If all
* goes well, we'll set it to RWLOCK_MAGIC.
*/
rwl->readers_waiting = 0;
rwl->writers_waiting = 0;
if (read_quota == 0)
if (write_quota == 0)
if (result != ISC_R_SUCCESS) {
"isc_mutex_init() %s: %s",
ISC_MSG_FAILED, "failed"),
return (ISC_R_UNEXPECTED);
}
if (result != ISC_R_SUCCESS) {
"isc_condition_init(readable) %s: %s",
ISC_MSG_FAILED, "failed"),
return (ISC_R_UNEXPECTED);
}
if (result != ISC_R_SUCCESS) {
"isc_condition_init(writeable) %s: %s",
ISC_MSG_FAILED, "failed"),
return (ISC_R_UNEXPECTED);
}
return (ISC_R_SUCCESS);
}
static isc_result_t
#ifdef ISC_RWLOCK_TRACE
#endif
if (type == isc_rwlocktype_read) {
if (rwl->readers_waiting != 0)
while (!done) {
if (!skip &&
(rwl->writers_waiting == 0 ||
{
} else if (nonblock) {
} else {
rwl->readers_waiting++;
rwl->readers_waiting--;
}
}
} else {
if (rwl->writers_waiting != 0)
while (!done) {
} else if (nonblock) {
} else {
rwl->writers_waiting++;
rwl->writers_waiting--;
}
}
}
#ifdef ISC_RWLOCK_TRACE
#endif
return (result);
}
}
}
/* If we are the only reader then succeed. */
} else
return (result);
}
void
/*
* Resume processing any read request that were blocked when
* we upgraded.
*/
rwl->readers_waiting > 0)
}
#ifdef ISC_RWLOCK_TRACE
#endif
}
if (rwl->writers_waiting > 0) {
} else if (rwl->readers_waiting > 0) {
/* Does this case ever happen? */
}
} else {
if (rwl->readers_waiting > 0) {
if (rwl->writers_waiting > 0 &&
} else {
}
} else if (rwl->writers_waiting > 0) {
} else {
}
}
}
#ifdef ISC_RWLOCK_TRACE
ISC_MSG_POSTUNLOCK, "postunlock"),
#endif
return (ISC_R_SUCCESS);
}
void
rwl->readers_waiting == 0 &&
rwl->writers_waiting == 0);
}
#else /* ISC_PLATFORM_USETHREADS */
unsigned int write_quota)
{
return (ISC_R_SUCCESS);
}
if (type == isc_rwlocktype_read) {
return (ISC_R_LOCKBUSY);
} else {
return (ISC_R_LOCKBUSY);
}
return (ISC_R_SUCCESS);
}
}
/* If we are the only reader then succeed. */
else
return (result);
}
void
}
return (ISC_R_SUCCESS);
}
void
}
#endif /* ISC_PLATFORM_USETHREADS */