summaryrefslogtreecommitdiffstats
path: root/sys/kern/subr_lock.c
diff options
context:
space:
mode:
authorjhb <jhb@FreeBSD.org>2006-01-17 16:55:17 +0000
committerjhb <jhb@FreeBSD.org>2006-01-17 16:55:17 +0000
commitc0cf4870f410060c24b4fada37aa68f397098325 (patch)
tree39ef317a70787cab889c6304585f543a64e07ebc /sys/kern/subr_lock.c
parentb12ec6b7ed4f791a27a5b65c12d5e1f21fa43ece (diff)
downloadFreeBSD-src-c0cf4870f410060c24b4fada37aa68f397098325.zip
FreeBSD-src-c0cf4870f410060c24b4fada37aa68f397098325.tar.gz
Add a new file (kern/subr_lock.c) for holding code related to struct
lock_obj objects: - Add new lock_init() and lock_destroy() functions to setup and teardown lock_object objects including KTR logging and registering with WITNESS. - Move all the handling of LO_INITIALIZED out of witness and the various lock init functions into lock_init() and lock_destroy(). - Remove the constants for static indices into the lock_classes[] array and change the code outside of subr_lock.c to use LOCK_CLASS to compare against a known lock class. - Move the 'show lock' ddb function and lock_classes[] array out of kern_mutex.c over to subr_lock.c.
Diffstat (limited to 'sys/kern/subr_lock.c')
-rw-r--r--sys/kern/subr_lock.c113
1 files changed, 113 insertions, 0 deletions
diff --git a/sys/kern/subr_lock.c b/sys/kern/subr_lock.c
new file mode 100644
index 0000000..3273d91
--- /dev/null
+++ b/sys/kern/subr_lock.c
@@ -0,0 +1,113 @@
+/*-
+ * 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 <sys/param.h>
+#include <sys/systm.h>
+#include <sys/ktr.h>
+#include <sys/lock.h>
+
+#ifdef DDB
+#include <ddb/ddb.h>
+#endif
+
+CTASSERT(LOCK_CLASS_MAX == 15);
+
+#if LOCK_DEBUG > 0 || defined(DDB)
+struct lock_class *lock_classes[LOCK_CLASS_MAX + 1] = {
+ &lock_class_mtx_spin,
+ &lock_class_mtx_sleep,
+ &lock_class_sx,
+};
+#endif
+
+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
OpenPOWER on IntegriCloud