Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys Add some new functions for lock objects:



details:   https://anonhg.NetBSD.org/src/rev/dd6162d43fa0
branches:  trunk
changeset: 1006108:dd6162d43fa0
user:      ad <ad%NetBSD.org@localhost>
date:      Wed Jan 01 21:34:39 2020 +0000

description:
Add some new functions for lock objects:

mutex_obj_refcnt(), mutex_obj_tryalloc()
rw_obj_refcnt(), rw_obj_tryalloc()

diffstat:

 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(-)

diffs (191 lines):

diff -r 84d8ddfa85b0 -r dd6162d43fa0 sys/kern/kern_mutex_obj.c
--- a/sys/kern/kern_mutex_obj.c Wed Jan 01 21:09:11 2020 +0000
+++ b/sys/kern/kern_mutex_obj.c Wed Jan 01 21:34:39 2020 +0000
@@ -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_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:
+ *
+ *     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 @@
        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 -r 84d8ddfa85b0 -r dd6162d43fa0 sys/kern/kern_rwlock_obj.c
--- a/sys/kern/kern_rwlock_obj.c        Wed Jan 01 21:09:11 2020 +0000
+++ b/sys/kern/kern_rwlock_obj.c        Wed Jan 01 21:34:39 2020 +0000
@@ -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_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_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 @@
        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 -r 84d8ddfa85b0 -r dd6162d43fa0 sys/sys/mutex.h
--- a/sys/sys/mutex.h   Wed Jan 01 21:09:11 2020 +0000
+++ b/sys/sys/mutex.h   Wed Jan 01 21:34:39 2020 +0000
@@ -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 @@
 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 -r 84d8ddfa85b0 -r dd6162d43fa0 sys/sys/rwlock.h
--- a/sys/sys/rwlock.h  Wed Jan 01 21:09:11 2020 +0000
+++ b/sys/sys/rwlock.h  Wed Jan 01 21:34:39 2020 +0000
@@ -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 @@
 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 */
 



Home | Main Index | Thread Index | Old Index