Module Name: src Committed By: ad Date: Wed Jan 1 21:34:39 UTC 2020
Modified Files: src/sys/kern: kern_mutex_obj.c kern_rwlock_obj.c src/sys/sys: mutex.h rwlock.h Log Message: Add some new functions for lock objects: mutex_obj_refcnt(), mutex_obj_tryalloc() rw_obj_refcnt(), rw_obj_tryalloc() To generate a diff of this commit: cvs rdiff -u -r1.6 -r1.7 src/sys/kern/kern_mutex_obj.c cvs rdiff -u -r1.4 -r1.5 src/sys/kern/kern_rwlock_obj.c cvs rdiff -u -r1.24 -r1.25 src/sys/sys/mutex.h cvs rdiff -u -r1.11 -r1.12 src/sys/sys/rwlock.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/kern/kern_mutex_obj.c diff -u src/sys/kern/kern_mutex_obj.c:1.6 src/sys/kern/kern_mutex_obj.c:1.7 --- src/sys/kern/kern_mutex_obj.c:1.6 Mon Feb 5 04:25:04 2018 +++ src/sys/kern/kern_mutex_obj.c Wed Jan 1 21:34:39 2020 @@ -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 <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/atomic.h> @@ -81,7 +81,7 @@ mutex_obj_ctor(void *arg, void *obj, int /* * 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) @@ -98,6 +98,27 @@ mutex_obj_alloc(kmutex_type_t type, int } /* + * 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: * * Add a single reference to a lock object. A reference to the object @@ -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; +} Index: src/sys/kern/kern_rwlock_obj.c diff -u src/sys/kern/kern_rwlock_obj.c:1.4 src/sys/kern/kern_rwlock_obj.c:1.5 --- src/sys/kern/kern_rwlock_obj.c:1.4 Mon Feb 5 04:25:04 2018 +++ src/sys/kern/kern_rwlock_obj.c Wed Jan 1 21:34:39 2020 @@ -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 <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/atomic.h> @@ -81,7 +81,7 @@ rw_obj_ctor(void *arg, void *obj, int fl /* * rw_obj_alloc: * - * Allocate a single lock object. + * Allocate a single lock object, waiting for memory if needed. */ krwlock_t * rw_obj_alloc(void) @@ -97,6 +97,26 @@ rw_obj_alloc(void) } /* + * 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: * * Add a single reference to a lock object. A reference to the object @@ -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; +} Index: src/sys/sys/mutex.h diff -u src/sys/sys/mutex.h:1.24 src/sys/sys/mutex.h:1.25 --- src/sys/sys/mutex.h:1.24 Mon Dec 9 21:08:56 2019 +++ src/sys/sys/mutex.h Wed Jan 1 21:34:39 2020 @@ -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 */ Index: src/sys/sys/rwlock.h diff -u src/sys/sys/rwlock.h:1.11 src/sys/sys/rwlock.h:1.12 --- src/sys/sys/rwlock.h:1.11 Fri Nov 29 20:04:54 2019 +++ src/sys/sys/rwlock.h Wed Jan 1 21:34:39 2020 @@ -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 */