Heave-ho mutex/rwlock object routines into separate modules -- they

don't have anything to do with the lock internals.
This commit is contained in:
pooka 2009-11-04 13:29:45 +00:00
parent 61d1f4a9a4
commit 83685e650c
5 changed files with 278 additions and 201 deletions

View File

@ -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_stdtype.c
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_rate.c
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

View File

@ -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 @@ __KERNEL_RCSID(0, "$NetBSD: kern_mutex.c,v 1.45 2009/01/25 04:45:14 rmind Exp $"
#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 @@ syncobj_t mutex_syncobj = {
(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 @@ mutex_spin_retry(kmutex_t *mtx)
#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;
}

135
sys/kern/kern_mutex_obj.c Normal file
View File

@ -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;
}

View File

@ -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 @@ syncobj_t rw_syncobj = {
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 @@ rw_owner(wchan_t obj)
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;
}

135
sys/kern/kern_rwlock_obj.c Normal file
View File

@ -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;
}