Module Name: src Committed By: pooka Date: Wed Nov 4 13:29:46 UTC 2009
Modified Files: src/sys/conf: files src/sys/kern: kern_mutex.c kern_rwlock.c Added Files: src/sys/kern: kern_mutex_obj.c kern_rwlock_obj.c Log Message: Heave-ho mutex/rwlock object routines into separate modules -- they don't have anything to do with the lock internals. To generate a diff of this commit: cvs rdiff -u -r1.960 -r1.961 src/sys/conf/files cvs rdiff -u -r1.45 -r1.46 src/sys/kern/kern_mutex.c cvs rdiff -u -r0 -r1.1 src/sys/kern/kern_mutex_obj.c \ src/sys/kern/kern_rwlock_obj.c cvs rdiff -u -r1.32 -r1.33 src/sys/kern/kern_rwlock.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/conf/files diff -u src/sys/conf/files:1.960 src/sys/conf/files:1.961 --- src/sys/conf/files:1.960 Tue Nov 3 00:24:05 2009 +++ src/sys/conf/files Wed Nov 4 13:29:45 2009 @@ -1,4 +1,4 @@ -# $NetBSD: files,v 1.960 2009/11/03 00:24:05 dyoung Exp $ +# $NetBSD: files,v 1.961 2009/11/04 13:29:45 pooka Exp $ # @(#)files.newconf 7.5 (Berkeley) 5/10/93 version 20090313 @@ -1439,6 +1439,7 @@ file kern/kern_malloc_debug.c malloc_debug file kern/kern_module.c file kern/kern_mutex.c +file kern/kern_mutex_obj.c file kern/kern_fileassoc.c fileassoc file kern/kern_ntptime.c file kern/kern_pax.c pax_mprotect | pax_segvguard @@ -1451,6 +1452,7 @@ file kern/kern_resource.c file kern/kern_runq.c file kern/kern_rwlock.c +file kern/kern_rwlock_obj.c file kern/kern_sig.c file kern/kern_sleepq.c file kern/kern_softint.c Index: src/sys/kern/kern_mutex.c diff -u src/sys/kern/kern_mutex.c:1.45 src/sys/kern/kern_mutex.c:1.46 --- src/sys/kern/kern_mutex.c:1.45 Sun Jan 25 04:45:14 2009 +++ src/sys/kern/kern_mutex.c Wed Nov 4 13:29:45 2009 @@ -1,4 +1,4 @@ -/* $NetBSD: kern_mutex.c,v 1.45 2009/01/25 04:45:14 rmind Exp $ */ +/* $NetBSD: kern_mutex.c,v 1.46 2009/11/04 13:29:45 pooka Exp $ */ /*- * Copyright (c) 2002, 2006, 2007, 2008 The NetBSD Foundation, Inc. @@ -40,9 +40,10 @@ #define __MUTEX_PRIVATE #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: kern_mutex.c,v 1.45 2009/01/25 04:45:14 rmind Exp $"); +__KERNEL_RCSID(0, "$NetBSD: kern_mutex.c,v 1.46 2009/11/04 13:29:45 pooka Exp $"); #include <sys/param.h> +#include <sys/atomic.h> #include <sys/proc.h> #include <sys/mutex.h> #include <sys/sched.h> @@ -50,10 +51,8 @@ #include <sys/systm.h> #include <sys/lockdebug.h> #include <sys/kernel.h> -#include <sys/atomic.h> #include <sys/intr.h> #include <sys/lock.h> -#include <sys/pool.h> #include <dev/lockstat.h> @@ -274,18 +273,6 @@ (void *)mutex_owner, }; -/* Mutex cache */ -#define MUTEX_OBJ_MAGIC 0x5aa3c85d -struct kmutexobj { - kmutex_t mo_lock; - u_int mo_magic; - u_int mo_refcnt; -}; - -static int mutex_obj_ctor(void *, void *, int); - -static pool_cache_t mutex_obj_cache; - /* * mutex_dump: * @@ -940,88 +927,3 @@ #endif /* MULTIPROCESSOR */ } #endif /* defined(__HAVE_SPIN_MUTEX_STUBS) || defined(FULL) */ - -/* - * mutex_obj_init: - * - * Initialize the mutex object store. - */ -void -mutex_obj_init(void) -{ - - mutex_obj_cache = pool_cache_init(sizeof(struct kmutexobj), - coherency_unit, 0, 0, "mutex", NULL, IPL_NONE, mutex_obj_ctor, - NULL, NULL); -} - -/* - * mutex_obj_ctor: - * - * Initialize a new lock for the cache. - */ -static int -mutex_obj_ctor(void *arg, void *obj, int flags) -{ - struct kmutexobj * mo = obj; - - mo->mo_magic = MUTEX_OBJ_MAGIC; - - return 0; -} - -/* - * mutex_obj_alloc: - * - * Allocate a single lock object. - */ -kmutex_t * -mutex_obj_alloc(kmutex_type_t type, int ipl) -{ - struct kmutexobj *mo; - - mo = pool_cache_get(mutex_obj_cache, PR_WAITOK); - mutex_init(&mo->mo_lock, type, ipl); - mo->mo_refcnt = 1; - - return (kmutex_t *)mo; -} - -/* - * mutex_obj_hold: - * - * Add a single reference to a lock object. A reference to the object - * must already be held, and must be held across this call. - */ -void -mutex_obj_hold(kmutex_t *lock) -{ - struct kmutexobj *mo = (struct kmutexobj *)lock; - - KASSERT(mo->mo_magic == MUTEX_OBJ_MAGIC); - KASSERT(mo->mo_refcnt > 0); - - atomic_inc_uint(&mo->mo_refcnt); -} - -/* - * mutex_obj_free: - * - * Drop a reference from a lock object. If the last reference is being - * dropped, free the object and return true. Otherwise, return false. - */ -bool -mutex_obj_free(kmutex_t *lock) -{ - struct kmutexobj *mo = (struct kmutexobj *)lock; - - KASSERT(mo->mo_magic == MUTEX_OBJ_MAGIC); - KASSERT(mo->mo_refcnt > 0); - - if (atomic_dec_uint_nv(&mo->mo_refcnt) > 0) { - return false; - } - mutex_destroy(&mo->mo_lock); - pool_cache_put(mutex_obj_cache, mo); - return true; -} Index: src/sys/kern/kern_rwlock.c diff -u src/sys/kern/kern_rwlock.c:1.32 src/sys/kern/kern_rwlock.c:1.33 --- src/sys/kern/kern_rwlock.c:1.32 Sat May 16 08:36:32 2009 +++ src/sys/kern/kern_rwlock.c Wed Nov 4 13:29:45 2009 @@ -1,4 +1,4 @@ -/* $NetBSD: kern_rwlock.c,v 1.32 2009/05/16 08:36:32 yamt Exp $ */ +/* $NetBSD: kern_rwlock.c,v 1.33 2009/11/04 13:29:45 pooka Exp $ */ /*- * Copyright (c) 2002, 2006, 2007, 2008, 2009 The NetBSD Foundation, Inc. @@ -38,7 +38,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: kern_rwlock.c,v 1.32 2009/05/16 08:36:32 yamt Exp $"); +__KERNEL_RCSID(0, "$NetBSD: kern_rwlock.c,v 1.33 2009/11/04 13:29:45 pooka Exp $"); #define __RWLOCK_PRIVATE @@ -161,18 +161,6 @@ rw_owner, }; -/* Mutex cache */ -#define RW_OBJ_MAGIC 0x85d3c85d -struct krwobj { - krwlock_t ro_lock; - u_int ro_magic; - u_int ro_refcnt; -}; - -static int rw_obj_ctor(void *, void *, int); - -static pool_cache_t rw_obj_cache; - /* * rw_dump: * @@ -778,88 +766,3 @@ return (void *)(owner & RW_THREAD); } - -/* - * rw_obj_init: - * - * Initialize the rw object store. - */ -void -rw_obj_init(void) -{ - - rw_obj_cache = pool_cache_init(sizeof(struct krwobj), - coherency_unit, 0, 0, "rwlock", NULL, IPL_NONE, rw_obj_ctor, - NULL, NULL); -} - -/* - * rw_obj_ctor: - * - * Initialize a new lock for the cache. - */ -static int -rw_obj_ctor(void *arg, void *obj, int flags) -{ - struct krwobj * ro = obj; - - ro->ro_magic = RW_OBJ_MAGIC; - - return 0; -} - -/* - * rw_obj_alloc: - * - * Allocate a single lock object. - */ -krwlock_t * -rw_obj_alloc(void) -{ - struct krwobj *ro; - - ro = pool_cache_get(rw_obj_cache, PR_WAITOK); - rw_init(&ro->ro_lock); - ro->ro_refcnt = 1; - - return (krwlock_t *)ro; -} - -/* - * rw_obj_hold: - * - * Add a single reference to a lock object. A reference to the object - * must already be held, and must be held across this call. - */ -void -rw_obj_hold(krwlock_t *lock) -{ - struct krwobj *ro = (struct krwobj *)lock; - - KASSERT(ro->ro_magic == RW_OBJ_MAGIC); - KASSERT(ro->ro_refcnt > 0); - - atomic_inc_uint(&ro->ro_refcnt); -} - -/* - * rw_obj_free: - * - * Drop a reference from a lock object. If the last reference is being - * dropped, free the object and return true. Otherwise, return false. - */ -bool -rw_obj_free(krwlock_t *lock) -{ - struct krwobj *ro = (struct krwobj *)lock; - - KASSERT(ro->ro_magic == RW_OBJ_MAGIC); - KASSERT(ro->ro_refcnt > 0); - - if (atomic_dec_uint_nv(&ro->ro_refcnt) > 0) { - return false; - } - rw_destroy(&ro->ro_lock); - pool_cache_put(rw_obj_cache, ro); - return true; -} Added files: Index: src/sys/kern/kern_mutex_obj.c diff -u /dev/null src/sys/kern/kern_mutex_obj.c:1.1 --- /dev/null Wed Nov 4 13:29:46 2009 +++ src/sys/kern/kern_mutex_obj.c Wed Nov 4 13:29:45 2009 @@ -0,0 +1,135 @@ +/* $NetBSD: kern_mutex_obj.c,v 1.1 2009/11/04 13:29:45 pooka Exp $ */ + +/*- + * Copyright (c) 2002, 2006, 2007, 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe and Andrew Doran. + * + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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. + */ + +#include <sys/cdefs.h> +__KERNEL_RCSID(0, "$NetBSD: kern_mutex_obj.c,v 1.1 2009/11/04 13:29:45 pooka Exp $"); + +#include <sys/param.h> +#include <sys/atomic.h> +#include <sys/mutex.h> +#include <sys/pool.h> + +/* Mutex cache */ +#define MUTEX_OBJ_MAGIC 0x5aa3c85d +struct kmutexobj { + kmutex_t mo_lock; + u_int mo_magic; + u_int mo_refcnt; +}; + +static int mutex_obj_ctor(void *, void *, int); + +static pool_cache_t mutex_obj_cache; + +/* + * mutex_obj_init: + * + * Initialize the mutex object store. + */ +void +mutex_obj_init(void) +{ + + mutex_obj_cache = pool_cache_init(sizeof(struct kmutexobj), + coherency_unit, 0, 0, "mutex", NULL, IPL_NONE, mutex_obj_ctor, + NULL, NULL); +} + +/* + * mutex_obj_ctor: + * + * Initialize a new lock for the cache. + */ +static int +mutex_obj_ctor(void *arg, void *obj, int flags) +{ + struct kmutexobj * mo = obj; + + mo->mo_magic = MUTEX_OBJ_MAGIC; + + return 0; +} + +/* + * mutex_obj_alloc: + * + * Allocate a single lock object. + */ +kmutex_t * +mutex_obj_alloc(kmutex_type_t type, int ipl) +{ + struct kmutexobj *mo; + + mo = pool_cache_get(mutex_obj_cache, PR_WAITOK); + mutex_init(&mo->mo_lock, type, ipl); + mo->mo_refcnt = 1; + + return (kmutex_t *)mo; +} + +/* + * mutex_obj_hold: + * + * Add a single reference to a lock object. A reference to the object + * must already be held, and must be held across this call. + */ +void +mutex_obj_hold(kmutex_t *lock) +{ + struct kmutexobj *mo = (struct kmutexobj *)lock; + + KASSERT(mo->mo_magic == MUTEX_OBJ_MAGIC); + KASSERT(mo->mo_refcnt > 0); + + atomic_inc_uint(&mo->mo_refcnt); +} + +/* + * mutex_obj_free: + * + * Drop a reference from a lock object. If the last reference is being + * dropped, free the object and return true. Otherwise, return false. + */ +bool +mutex_obj_free(kmutex_t *lock) +{ + struct kmutexobj *mo = (struct kmutexobj *)lock; + + KASSERT(mo->mo_magic == MUTEX_OBJ_MAGIC); + KASSERT(mo->mo_refcnt > 0); + + if (atomic_dec_uint_nv(&mo->mo_refcnt) > 0) { + return false; + } + mutex_destroy(&mo->mo_lock); + pool_cache_put(mutex_obj_cache, mo); + return true; +} Index: src/sys/kern/kern_rwlock_obj.c diff -u /dev/null src/sys/kern/kern_rwlock_obj.c:1.1 --- /dev/null Wed Nov 4 13:29:46 2009 +++ src/sys/kern/kern_rwlock_obj.c Wed Nov 4 13:29:45 2009 @@ -0,0 +1,135 @@ +/* $NetBSD: kern_rwlock_obj.c,v 1.1 2009/11/04 13:29:45 pooka Exp $ */ + +/*- + * Copyright (c) 2002, 2006, 2007, 2008, 2009 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe and Andrew Doran. + * + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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. + */ + +#include <sys/cdefs.h> +__KERNEL_RCSID(0, "$NetBSD: kern_rwlock_obj.c,v 1.1 2009/11/04 13:29:45 pooka Exp $"); + +#include <sys/param.h> +#include <sys/atomic.h> +#include <sys/pool.h> +#include <sys/rwlock.h> + +/* Mutex cache */ +#define RW_OBJ_MAGIC 0x85d3c85d +struct krwobj { + krwlock_t ro_lock; + u_int ro_magic; + u_int ro_refcnt; +}; + +static int rw_obj_ctor(void *, void *, int); + +static pool_cache_t rw_obj_cache; + +/* + * rw_obj_init: + * + * Initialize the rw object store. + */ +void +rw_obj_init(void) +{ + + rw_obj_cache = pool_cache_init(sizeof(struct krwobj), + coherency_unit, 0, 0, "rwlock", NULL, IPL_NONE, rw_obj_ctor, + NULL, NULL); +} + +/* + * rw_obj_ctor: + * + * Initialize a new lock for the cache. + */ +static int +rw_obj_ctor(void *arg, void *obj, int flags) +{ + struct krwobj * ro = obj; + + ro->ro_magic = RW_OBJ_MAGIC; + + return 0; +} + +/* + * rw_obj_alloc: + * + * Allocate a single lock object. + */ +krwlock_t * +rw_obj_alloc(void) +{ + struct krwobj *ro; + + ro = pool_cache_get(rw_obj_cache, PR_WAITOK); + rw_init(&ro->ro_lock); + ro->ro_refcnt = 1; + + return (krwlock_t *)ro; +} + +/* + * rw_obj_hold: + * + * Add a single reference to a lock object. A reference to the object + * must already be held, and must be held across this call. + */ +void +rw_obj_hold(krwlock_t *lock) +{ + struct krwobj *ro = (struct krwobj *)lock; + + KASSERT(ro->ro_magic == RW_OBJ_MAGIC); + KASSERT(ro->ro_refcnt > 0); + + atomic_inc_uint(&ro->ro_refcnt); +} + +/* + * rw_obj_free: + * + * Drop a reference from a lock object. If the last reference is being + * dropped, free the object and return true. Otherwise, return false. + */ +bool +rw_obj_free(krwlock_t *lock) +{ + struct krwobj *ro = (struct krwobj *)lock; + + KASSERT(ro->ro_magic == RW_OBJ_MAGIC); + KASSERT(ro->ro_refcnt > 0); + + if (atomic_dec_uint_nv(&ro->ro_refcnt) > 0) { + return false; + } + rw_destroy(&ro->ro_lock); + pool_cache_put(rw_obj_cache, ro); + return true; +}