mirror of
https://git.FreeBSD.org/src.git
synced 2024-12-22 11:17:19 +00:00
3f08bd8bce
implementation is by no means perfect as far as some of the algorithms that it uses and the fact that it is missing some functionality (try locks and upgrades/downgrades are not there yet), however it does seem to work in my local testing. There is more detail in the comments in the code, but the short version follows. A reader/writer lock is very much like a regular mutex: it cannot be held across a voluntary sleep; it can be acquired in an interrupt thread; if the lock is held by a writer then the priority of any threads that block on the lock will be lent to the owner; the simple case lock operations all are done in a single atomic op. It also shares some similiarities with sx locks: it supports reader/writer semantics (multiple readers, but single writers); readers are allowed to recurse, but writers are not. We can extend this implementation further by either improving algorithms or adding new functionality, but this should at least give us a base to work with now. Reviewed by: arch (in theory) Tested on: i386 (4 cpu box with a kernel module that used 4 threads that randomly chose between read locks and write locks that ran w/o panicing for over a day solid. It usually panic'd within a few seconds when there were bugs during testing. :) The kernel module source is available on request.)
116 lines
3.4 KiB
C
116 lines
3.4 KiB
C
/*-
|
|
* Copyright (c) 2006 John Baldwin <jhb@FreeBSD.org>
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
* 3. Neither the name of the author nor the names of any co-contributors
|
|
* may be used to endorse or promote products derived from this software
|
|
* without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
* SUCH DAMAGE.
|
|
*/
|
|
|
|
/*
|
|
* This module holds the global variables and functions used to maintain
|
|
* lock_object structures.
|
|
*/
|
|
|
|
#include <sys/cdefs.h>
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
#include "opt_ddb.h"
|
|
|
|
#include <sys/param.h>
|
|
#include <sys/systm.h>
|
|
#include <sys/ktr.h>
|
|
#include <sys/linker_set.h>
|
|
#include <sys/lock.h>
|
|
|
|
#ifdef DDB
|
|
#include <ddb/ddb.h>
|
|
#endif
|
|
|
|
CTASSERT(LOCK_CLASS_MAX == 15);
|
|
|
|
struct lock_class *lock_classes[LOCK_CLASS_MAX + 1] = {
|
|
&lock_class_mtx_spin,
|
|
&lock_class_mtx_sleep,
|
|
&lock_class_sx,
|
|
&lock_class_rw,
|
|
};
|
|
|
|
void
|
|
lock_init(struct lock_object *lock, struct lock_class *class, const char *name,
|
|
const char *type, int flags)
|
|
{
|
|
int i;
|
|
|
|
/* Check for double-init and zero object. */
|
|
KASSERT(!lock_initalized(lock), ("lock \"%s\" %p already initialized",
|
|
name, lock));
|
|
|
|
/* Look up lock class to find its index. */
|
|
for (i = 0; i < LOCK_CLASS_MAX; i++)
|
|
if (lock_classes[i] == class) {
|
|
lock->lo_flags = i << LO_CLASSSHIFT;
|
|
break;
|
|
}
|
|
KASSERT(i < LOCK_CLASS_MAX, ("unknown lock class %p", class));
|
|
|
|
/* Initialize the lock object. */
|
|
lock->lo_name = name;
|
|
lock->lo_type = type != NULL ? type : name;
|
|
lock->lo_flags |= flags | LO_INITIALIZED;
|
|
LOCK_LOG_INIT(lock, 0);
|
|
WITNESS_INIT(lock);
|
|
}
|
|
|
|
void
|
|
lock_destroy(struct lock_object *lock)
|
|
{
|
|
|
|
KASSERT(lock_initalized(lock), ("lock %p is not initialized", lock));
|
|
WITNESS_DESTROY(lock);
|
|
LOCK_LOG_DESTROY(lock, 0);
|
|
lock->lo_flags &= ~LO_INITIALIZED;
|
|
}
|
|
|
|
#ifdef DDB
|
|
DB_SHOW_COMMAND(lock, db_show_lock)
|
|
{
|
|
struct lock_object *lock;
|
|
struct lock_class *class;
|
|
|
|
if (!have_addr)
|
|
return;
|
|
lock = (struct lock_object *)addr;
|
|
if (LO_CLASSINDEX(lock) > LOCK_CLASS_MAX) {
|
|
db_printf("Unknown lock class: %d\n", LO_CLASSINDEX(lock));
|
|
return;
|
|
}
|
|
class = LOCK_CLASS(lock);
|
|
db_printf(" class: %s\n", class->lc_name);
|
|
db_printf(" name: %s\n", lock->lo_name);
|
|
if (lock->lo_type && lock->lo_type != lock->lo_name)
|
|
db_printf(" type: %s\n", lock->lo_type);
|
|
class->lc_ddb_show(lock);
|
|
}
|
|
#endif
|