Add some new functions for lock objects:

mutex_obj_refcnt(), mutex_obj_tryalloc()
rw_obj_refcnt(), rw_obj_tryalloc()
This commit is contained in:
ad 2020-01-01 21:34:39 +00:00
parent a1e56ffa5f
commit 8ce608c956
4 changed files with 82 additions and 11 deletions

View File

@ -1,7 +1,7 @@
/* $NetBSD: kern_mutex_obj.c,v 1.6 2018/02/05 04:25:04 ozaki-r Exp $ */ /* $NetBSD: kern_mutex_obj.c,v 1.7 2020/01/01 21:34:39 ad Exp $ */
/*- /*-
* Copyright (c) 2008 The NetBSD Foundation, Inc. * Copyright (c) 2008, 2019 The NetBSD Foundation, Inc.
* All rights reserved. * All rights reserved.
* *
* This code is derived from software contributed to The NetBSD Foundation * This code is derived from software contributed to The NetBSD Foundation
@ -30,7 +30,7 @@
*/ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_mutex_obj.c,v 1.6 2018/02/05 04:25:04 ozaki-r Exp $"); __KERNEL_RCSID(0, "$NetBSD: kern_mutex_obj.c,v 1.7 2020/01/01 21:34:39 ad Exp $");
#include <sys/param.h> #include <sys/param.h>
#include <sys/atomic.h> #include <sys/atomic.h>
@ -81,7 +81,7 @@ mutex_obj_ctor(void *arg, void *obj, int flags)
/* /*
* mutex_obj_alloc: * mutex_obj_alloc:
* *
* Allocate a single lock object. * Allocate a single lock object, waiting for memory if needed.
*/ */
kmutex_t * kmutex_t *
mutex_obj_alloc(kmutex_type_t type, int ipl) mutex_obj_alloc(kmutex_type_t type, int ipl)
@ -97,6 +97,27 @@ mutex_obj_alloc(kmutex_type_t type, int ipl)
return (kmutex_t *)mo; return (kmutex_t *)mo;
} }
/*
* mutex_obj_alloc:
*
* Allocate a single lock object, failing if no memory available.
*/
kmutex_t *
mutex_obj_tryalloc(kmutex_type_t type, int ipl)
{
struct kmutexobj *mo;
extern void _mutex_init(kmutex_t *, kmutex_type_t, int, uintptr_t);
mo = pool_cache_get(mutex_obj_cache, PR_NOWAIT);
if (__predict_true(mo != NULL)) {
_mutex_init(&mo->mo_lock, type, ipl,
(uintptr_t)__builtin_return_address(0));
mo->mo_refcnt = 1;
}
return (kmutex_t *)mo;
}
/* /*
* mutex_obj_hold: * mutex_obj_hold:
* *
@ -143,3 +164,16 @@ mutex_obj_free(kmutex_t *lock)
pool_cache_put(mutex_obj_cache, mo); pool_cache_put(mutex_obj_cache, mo);
return true; return true;
} }
/*
* mutex_obj_refcnt:
*
* Return the reference count on a lock object.
*/
u_int
mutex_obj_refcnt(kmutex_t *lock)
{
struct kmutexobj *mo = (struct kmutexobj *)lock;
return mo->mo_refcnt;
}

View File

@ -1,7 +1,7 @@
/* $NetBSD: kern_rwlock_obj.c,v 1.4 2018/02/05 04:25:04 ozaki-r Exp $ */ /* $NetBSD: kern_rwlock_obj.c,v 1.5 2020/01/01 21:34:39 ad Exp $ */
/*- /*-
* Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. * Copyright (c) 2008, 2009, 2019 The NetBSD Foundation, Inc.
* All rights reserved. * All rights reserved.
* *
* This code is derived from software contributed to The NetBSD Foundation * This code is derived from software contributed to The NetBSD Foundation
@ -30,7 +30,7 @@
*/ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_rwlock_obj.c,v 1.4 2018/02/05 04:25:04 ozaki-r Exp $"); __KERNEL_RCSID(0, "$NetBSD: kern_rwlock_obj.c,v 1.5 2020/01/01 21:34:39 ad Exp $");
#include <sys/param.h> #include <sys/param.h>
#include <sys/atomic.h> #include <sys/atomic.h>
@ -81,7 +81,7 @@ rw_obj_ctor(void *arg, void *obj, int flags)
/* /*
* rw_obj_alloc: * rw_obj_alloc:
* *
* Allocate a single lock object. * Allocate a single lock object, waiting for memory if needed.
*/ */
krwlock_t * krwlock_t *
rw_obj_alloc(void) rw_obj_alloc(void)
@ -96,6 +96,26 @@ rw_obj_alloc(void)
return (krwlock_t *)ro; return (krwlock_t *)ro;
} }
/*
* rw_obj_tryalloc:
*
* Allocate a single lock object, but fail if no memory is available.
*/
krwlock_t *
rw_obj_tryalloc(void)
{
struct krwobj *ro;
extern void _rw_init(krwlock_t *, uintptr_t);
ro = pool_cache_get(rw_obj_cache, PR_NOWAIT);
if (__predict_true(ro != NULL)) {
_rw_init(&ro->ro_lock, (uintptr_t)__builtin_return_address(0));
ro->ro_refcnt = 1;
}
return (krwlock_t *)ro;
}
/* /*
* rw_obj_hold: * rw_obj_hold:
* *
@ -134,3 +154,16 @@ rw_obj_free(krwlock_t *lock)
pool_cache_put(rw_obj_cache, ro); pool_cache_put(rw_obj_cache, ro);
return true; return true;
} }
/*
* rw_obj_refcnt:
*
* Return the reference count for a lock object.
*/
u_int
rw_obj_refcnt(krwlock_t *lock)
{
struct krwobj *ro = (struct krwobj *)lock;
return ro->ro_refcnt;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: mutex.h,v 1.24 2019/12/09 21:08:56 ad Exp $ */ /* $NetBSD: mutex.h,v 1.25 2020/01/01 21:34:39 ad Exp $ */
/*- /*-
* Copyright (c) 2002, 2006, 2007, 2008, 2009, 2019 The NetBSD Foundation, Inc. * Copyright (c) 2002, 2006, 2007, 2008, 2009, 2019 The NetBSD Foundation, Inc.
@ -205,6 +205,8 @@ void mutex_obj_init(void);
kmutex_t *mutex_obj_alloc(kmutex_type_t, int); kmutex_t *mutex_obj_alloc(kmutex_type_t, int);
void mutex_obj_hold(kmutex_t *); void mutex_obj_hold(kmutex_t *);
bool mutex_obj_free(kmutex_t *); bool mutex_obj_free(kmutex_t *);
u_int mutex_obj_refcnt(kmutex_t *);
kmutex_t *mutex_obj_tryalloc(kmutex_type_t, int);
#endif /* _KERNEL */ #endif /* _KERNEL */

View File

@ -1,7 +1,7 @@
/* $NetBSD: rwlock.h,v 1.11 2019/11/29 20:04:54 riastradh Exp $ */ /* $NetBSD: rwlock.h,v 1.12 2020/01/01 21:34:39 ad Exp $ */
/*- /*-
* Copyright (c) 2002, 2006, 2007, 2008 The NetBSD Foundation, Inc. * Copyright (c) 2002, 2006, 2007, 2008, 2019 The NetBSD Foundation, Inc.
* All rights reserved. * All rights reserved.
* *
* This code is derived from software contributed to The NetBSD Foundation * This code is derived from software contributed to The NetBSD Foundation
@ -117,6 +117,8 @@ void rw_obj_init(void);
krwlock_t *rw_obj_alloc(void); krwlock_t *rw_obj_alloc(void);
void rw_obj_hold(krwlock_t *); void rw_obj_hold(krwlock_t *);
bool rw_obj_free(krwlock_t *); bool rw_obj_free(krwlock_t *);
u_int rw_obj_refcnt(krwlock_t *);
krwlock_t *rw_obj_tryalloc(void);
#endif /* _KERNEL */ #endif /* _KERNEL */