nisdb_rw.c revision a87701e9837f8a9ee9e4c4d3186295c0e29f743f
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2015 Gary Mills
* Copyright (c) 2001 by Sun Microsystems, Inc.
* All rights reserved.
*/
#include <stdio.h>
#include "db_dictionary_c.h"
#include "nisdb_rw.h"
#include "nisdb_ldap.h"
/*
* Nesting-safe RW locking functions. Return 0 when successful, an
* error number from the E-series when not.
*/
int
int ret;
if (rw == 0) {
#ifdef NISDB_MT_DEBUG
abort();
#endif /* NISDB_MT_DEBUG */
return (EFAULT);
}
return (ret);
return (ret);
/*
* If we allow read-to-write lock migration, there's a potential
* race condition if two or more threads want to upgrade at the
* same time. The simple and safe (but crude and possibly costly)
* method to fix this is to always use exclusive locks, and so
* that has to be the default.
*
* There are two conditions under which it is safe to set
* 'force_write' to zero for a certain lock structure:
*
* (1) The lock will never be subject to migration, or
*
* (2) It's OK if the data protected by the lock has changed
* (a) Every time the lock (read or write) has been
* acquired (even if the lock already was held by
* the thread), and
* (b) After every call to a function that might have
* acquired the lock.
*/
return (0);
}
static __nisdb_rl_t *
rr = 0;
break;
}
break;
}
return (rr);
}
int
int ret;
if (rw == 0)
return (EFAULT);
return (ESHUTDOWN);
return (ret);
/*
* Only allow changing 'force_write' when it's really safe; i.e.,
* the lock hasn't been destroyed, and there are no readers.
*/
rw->force_write = 0;
ret = 0;
} else {
}
return (ret);
}
int
int ret;
return (ESHUTDOWN);
return (ret);
/*
* Only allow changing 'force_write' when it's really safe; i.e.,
* the lock hasn't been destroyed, and there are no readers.
*/
ret = 0;
} else {
}
return (ret);
}
int
int ret;
int all_readers_blocked = 0;
__nisdb_rl_t *rr = 0;
if (rw == 0) {
#ifdef NISDB_MT_DEBUG
/* This shouldn't happen */
abort();
#endif /* NISDB_MT_DEBUG */
return (EFAULT);
}
return (ESHUTDOWN);
return (ret);
return (ESHUTDOWN);
}
/* Simplest (and probably most common) case: no readers or writers */
}
/*
* Need to know if we're holding a read lock already, and if
* all other readers are blocked waiting for the mutex.
*/
if (rw->reader_count > 0) {
/*
* We're already holding a read lock, so
* if the number of readers equals the number
* of blocked readers plus one, all other
* readers are blocked.
*/
if (rw->reader_count ==
all_readers_blocked = 1;
} else {
/*
* We're not holding a read lock, so the
* number of readers should equal the number
* of blocked readers if all readers are
* blocked.
*/
all_readers_blocked = 1;
}
}
}
/* Wait for reader(s) or writer to finish */
while (1) {
/*
* We can stop looping if one of the following holds:
* - No readers, no writers
* - No writers (or writer is myself), and one of:
* - No readers
* - One reader, and it's us
* - N readers, but all blocked on the mutex
*/
if (
(rw->reader_count == 0)) ||
break;
}
/*
* Provided that all readers are blocked on the mutex
* we break a potential dead-lock by acquiring the
* write lock.
*/
if (all_readers_blocked) {
break;
}
}
/*
* If 'trylock' is set, tell the caller that we'd have to
* block to obtain the lock.
*/
if (trylock) {
return (EBUSY);
}
/* If we're also a reader, indicate that we're blocking */
if (rr != 0) {
rw->reader_blocked++;
}
if (rr != 0) {
if (rw->reader_blocked > 0)
rw->reader_blocked--;
#ifdef NISDB_MT_DEBUG
else
abort();
#endif /* NISDB_MT_DEBUG */
}
return (ret);
}
if (rr != 0) {
if (rw->reader_blocked > 0)
rw->reader_blocked--;
#ifdef NISDB_MT_DEBUG
else
abort();
#endif /* NISDB_MT_DEBUG */
}
}
/* OK to grab the write lock */
/* Increment lock depth */
/* Set number of writers (doesn't increase with lock depth) */
if (rw->writer_count == 0)
}
int
return (__nisdb_wlock_trylock(rw, 0));
}
static __nisdb_rl_t *
break;
}
/* No previous reader */
} else if (rr == 0) {
return (0);
/*
* For insertion simplicity, make it the second item
* on the list.
*/
rw->reader_count++;
}
return (rr);
}
int
int ret;
if (rw == 0) {
#ifdef NISDB_MT_DEBUG
/* This shouldn't happen */
abort();
#endif /* NISDB_MT_DEBUG */
return (EFAULT);
}
return (ESHUTDOWN);
if (rw->force_write)
return (__nisdb_wlock(rw));
return (ret);
return (ESHUTDOWN);
}
/* Wait for writer to complete; writer == myself also OK */
if (rr != 0) {
rw->reader_blocked++;
}
if (rr != 0) {
if (rw->reader_blocked > 0)
rw->reader_blocked--;
#ifdef NISDB_MT_DEBUG
else
abort();
#endif /* NISDB_MT_DEBUG */
}
return (ret);
}
if (rr != 0) {
if (rw->reader_blocked > 0)
rw->reader_blocked--;
#ifdef NISDB_MT_DEBUG
else
abort();
#endif /* NISDB_MT_DEBUG */
}
}
}
int
int ret;
if (rw == 0) {
#ifdef NISDB_MT_DEBUG
/* This shouldn't happen */
abort();
#endif /* NISDB_MT_DEBUG */
return (EFAULT);
}
return (ESHUTDOWN);
return (ret);
return (ESHUTDOWN);
}
/* Sanity check */
if (rw->writer_count == 0 ||
#ifdef NISDB_MT_DEBUG
abort();
#endif /* NISDB_MT_DEBUG */
return (ENOLCK);
}
rw->writer_count = 0;
return (ret);
}
}
}
int
int ret;
if (rw == 0) {
#ifdef NISDB_MT_DEBUG
abort();
#endif /* NISDB_MT_DEBUG */
return (EFAULT);
}
return (ESHUTDOWN);
if (rw->force_write)
return (__nisdb_wulock(rw));
return (ret);
return (ESHUTDOWN);
}
/* Sanity check */
if (rw->reader_count == 0 ||
#ifdef NISDB_MT_DEBUG
abort();
#endif /* NISDB_MT_DEBUG */
return (ENOLCK);
}
/* Find the reader record */
break;
}
#ifdef NISDB_MT_DEBUG
abort();
#endif /* NISDB_MT_DEBUG */
return (ENOLCK);
}
/* Remove item from list and free it */
} else {
/*
* First record: copy second to first, and free second
* record.
*/
} else {
/* Decomission the first record */
}
}
rw->reader_count--;
}
/* If there are no readers, wake up any waiting writer */
if (rw->reader_count == 0) {
return (ret);
}
}
}
/* Return zero if write lock held by this thread, non-zero otherwise */
int
int ret;
if (rw == 0) {
#ifdef NISDB_MT_DEBUG
abort();
#endif /* NISDB_MT_DEBUG */
return (EFAULT);
}
return (ESHUTDOWN);
return (ret);
return (ESHUTDOWN);
}
}
/*
* We're holding the lock, so we should return zero. Since
* that's what mutex_unlock() does if it succeeds, we just
* return the value of mutex_unlock().
*/
}
/* Return zero if read lock held by this thread, non-zero otherwise */
int
int ret;
if (rw == 0) {
#ifdef NISDB_MT_DEBUG
abort();
#endif /* NISDB_MT_DEBUG */
return (EFAULT);
}
return (ESHUTDOWN);
if (rw->force_write)
return (__nisdb_assert_wheld(rw));
return (ret);
return (ESHUTDOWN);
}
/* Write lock also OK */
return (0);
}
if (rw->reader_count == 0) {
return (EBUSY);
}
do {
return (0);
}
} while (rr != 0);
}
int
int ret;
if (rw == 0) {
#ifdef NISDB_MT_DEBUG
abort();
#endif /* NISDB_MT_DEBUG */
return (EFAULT);
}
return (ESHUTDOWN);
return (ret);
return (ESHUTDOWN);
}
/*
* Only proceed if if there are neither readers nor writers
* other than this thread. Also, no nested locks may be in
* effect.
*/
if (((rw->writer_count > 0 &&
(rw->reader_count > 0 &&
#ifdef NISDB_MT_DEBUG
abort();
#endif /* NISDB_MT_DEBUG */
return (ENOLCK);
}
/*
* Mark lock destroyed, so that any thread waiting on the mutex
* will know what's what. Of course, this is a bit iffy, since
* we're probably being called from a destructor, and the structure
* where we live will soon cease to exist (i.e., be freed and
* perhaps re-used). Still, we can only do our best, and give
* those other threads the best chance possible.
*/
}
void
char *myself = "__nisdb_lock_report";
if (rw == 0) {
return;
}
printf("0x%x: Read locking %s\n",
if (rw->writer_count == 0)
printf("0x%x: Write locked by %d, depth = %d\n",
} else
printf("0x%x: Invalid writer count = %d\n",
if (rw->reader_count == 0)
else {
__nisdb_rl_t *r;
printf("0x%x: %d readers, %d blocked\n",
printf("0x%x:\tthread %d, depth = %d%s\n",
}
}
}