From 8ce608c9568ea021d0e77b25cb5eda1968d79a37 Mon Sep 17 00:00:00 2001 From: ad Date: Wed, 1 Jan 2020 21:34:39 +0000 Subject: [PATCH] Add some new functions for lock objects: mutex_obj_refcnt(), mutex_obj_tryalloc() rw_obj_refcnt(), rw_obj_tryalloc() --- sys/kern/kern_mutex_obj.c | 42 ++++++++++++++++++++++++++++++++++---- sys/kern/kern_rwlock_obj.c | 41 +++++++++++++++++++++++++++++++++---- sys/sys/mutex.h | 4 +++- sys/sys/rwlock.h | 6 ++++-- 4 files changed, 82 insertions(+), 11 deletions(-) diff --git a/sys/kern/kern_mutex_obj.c b/sys/kern/kern_mutex_obj.c index 833e953e0774..b11cf2c95414 100644 --- a/sys/kern/kern_mutex_obj.c +++ b/sys/kern/kern_mutex_obj.c @@ -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. * * This code is derived from software contributed to The NetBSD Foundation @@ -30,7 +30,7 @@ */ #include -__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 #include @@ -81,7 +81,7 @@ mutex_obj_ctor(void *arg, void *obj, int flags) /* * mutex_obj_alloc: * - * Allocate a single lock object. + * Allocate a single lock object, waiting for memory if needed. */ kmutex_t * 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; } +/* + * 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: * @@ -143,3 +164,16 @@ mutex_obj_free(kmutex_t *lock) pool_cache_put(mutex_obj_cache, mo); 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; +} diff --git a/sys/kern/kern_rwlock_obj.c b/sys/kern/kern_rwlock_obj.c index 334830b12f32..9a26023585a3 100644 --- a/sys/kern/kern_rwlock_obj.c +++ b/sys/kern/kern_rwlock_obj.c @@ -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. * * This code is derived from software contributed to The NetBSD Foundation @@ -30,7 +30,7 @@ */ #include -__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 #include @@ -81,7 +81,7 @@ rw_obj_ctor(void *arg, void *obj, int flags) /* * rw_obj_alloc: * - * Allocate a single lock object. + * Allocate a single lock object, waiting for memory if needed. */ krwlock_t * rw_obj_alloc(void) @@ -96,6 +96,26 @@ rw_obj_alloc(void) 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: * @@ -134,3 +154,16 @@ rw_obj_free(krwlock_t *lock) pool_cache_put(rw_obj_cache, ro); 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; +} diff --git a/sys/sys/mutex.h b/sys/sys/mutex.h index e4be69c79534..43b751e5afac 100644 --- a/sys/sys/mutex.h +++ b/sys/sys/mutex.h @@ -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. @@ -205,6 +205,8 @@ void mutex_obj_init(void); kmutex_t *mutex_obj_alloc(kmutex_type_t, int); void mutex_obj_hold(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 */ diff --git a/sys/sys/rwlock.h b/sys/sys/rwlock.h index 243b0d0c6c59..b30caf463817 100644 --- a/sys/sys/rwlock.h +++ b/sys/sys/rwlock.h @@ -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. * * 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); void rw_obj_hold(krwlock_t *); bool rw_obj_free(krwlock_t *); +u_int rw_obj_refcnt(krwlock_t *); +krwlock_t *rw_obj_tryalloc(void); #endif /* _KERNEL */