xref: /netbsd-src/sys/kern/kern_mutex_obj.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: kern_mutex_obj.c,v 1.7 2020/01/01 21:34:39 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008, 2019 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: kern_mutex_obj.c,v 1.7 2020/01/01 21:34:39 ad Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/atomic.h>
37 #include <sys/mutex.h>
38 #include <sys/pool.h>
39 
40 /* Mutex cache */
41 #define	MUTEX_OBJ_MAGIC	0x5aa3c85d
42 struct kmutexobj {
43 	kmutex_t	mo_lock;
44 	u_int		mo_magic;
45 	u_int		mo_refcnt;
46 };
47 
48 static int	mutex_obj_ctor(void *, void *, int);
49 
50 static pool_cache_t	mutex_obj_cache		__read_mostly;
51 
52 /*
53  * mutex_obj_init:
54  *
55  *	Initialize the mutex object store.
56  */
57 void
58 mutex_obj_init(void)
59 {
60 
61 	mutex_obj_cache = pool_cache_init(sizeof(struct kmutexobj),
62 	    coherency_unit, 0, 0, "mutex", NULL, IPL_NONE, mutex_obj_ctor,
63 	    NULL, NULL);
64 }
65 
66 /*
67  * mutex_obj_ctor:
68  *
69  *	Initialize a new lock for the cache.
70  */
71 static int
72 mutex_obj_ctor(void *arg, void *obj, int flags)
73 {
74 	struct kmutexobj * mo = obj;
75 
76 	mo->mo_magic = MUTEX_OBJ_MAGIC;
77 
78 	return 0;
79 }
80 
81 /*
82  * mutex_obj_alloc:
83  *
84  *	Allocate a single lock object, waiting for memory if needed.
85  */
86 kmutex_t *
87 mutex_obj_alloc(kmutex_type_t type, int ipl)
88 {
89 	struct kmutexobj *mo;
90 	extern void _mutex_init(kmutex_t *, kmutex_type_t, int, uintptr_t);
91 
92 	mo = pool_cache_get(mutex_obj_cache, PR_WAITOK);
93 	_mutex_init(&mo->mo_lock, type, ipl,
94 	    (uintptr_t)__builtin_return_address(0));
95 	mo->mo_refcnt = 1;
96 
97 	return (kmutex_t *)mo;
98 }
99 
100 /*
101  * mutex_obj_alloc:
102  *
103  *	Allocate a single lock object, failing if no memory available.
104  */
105 kmutex_t *
106 mutex_obj_tryalloc(kmutex_type_t type, int ipl)
107 {
108 	struct kmutexobj *mo;
109 	extern void _mutex_init(kmutex_t *, kmutex_type_t, int, uintptr_t);
110 
111 	mo = pool_cache_get(mutex_obj_cache, PR_NOWAIT);
112 	if (__predict_true(mo != NULL)) {
113 		_mutex_init(&mo->mo_lock, type, ipl,
114 		    (uintptr_t)__builtin_return_address(0));
115 		mo->mo_refcnt = 1;
116 	}
117 
118 	return (kmutex_t *)mo;
119 }
120 
121 /*
122  * mutex_obj_hold:
123  *
124  *	Add a single reference to a lock object.  A reference to the object
125  *	must already be held, and must be held across this call.
126  */
127 void
128 mutex_obj_hold(kmutex_t *lock)
129 {
130 	struct kmutexobj *mo = (struct kmutexobj *)lock;
131 
132 	KASSERTMSG(mo->mo_magic == MUTEX_OBJ_MAGIC,
133 	    "%s: lock %p: mo->mo_magic (%#x) != MUTEX_OBJ_MAGIC (%#x)",
134 	     __func__, mo, mo->mo_magic, MUTEX_OBJ_MAGIC);
135 	KASSERTMSG(mo->mo_refcnt > 0,
136 	    "%s: lock %p: mo->mo_refcnt (%#x) == 0",
137 	     __func__, mo, mo->mo_refcnt);
138 
139 	atomic_inc_uint(&mo->mo_refcnt);
140 }
141 
142 /*
143  * mutex_obj_free:
144  *
145  *	Drop a reference from a lock object.  If the last reference is being
146  *	dropped, free the object and return true.  Otherwise, return false.
147  */
148 bool
149 mutex_obj_free(kmutex_t *lock)
150 {
151 	struct kmutexobj *mo = (struct kmutexobj *)lock;
152 
153 	KASSERTMSG(mo->mo_magic == MUTEX_OBJ_MAGIC,
154 	    "%s: lock %p: mo->mo_magic (%#x) != MUTEX_OBJ_MAGIC (%#x)",
155 	     __func__, mo, mo->mo_magic, MUTEX_OBJ_MAGIC);
156 	KASSERTMSG(mo->mo_refcnt > 0,
157 	    "%s: lock %p: mo->mo_refcnt (%#x) == 0",
158 	     __func__, mo, mo->mo_refcnt);
159 
160 	if (atomic_dec_uint_nv(&mo->mo_refcnt) > 0) {
161 		return false;
162 	}
163 	mutex_destroy(&mo->mo_lock);
164 	pool_cache_put(mutex_obj_cache, mo);
165 	return true;
166 }
167 
168 /*
169  * mutex_obj_refcnt:
170  *
171  *	Return the reference count on a lock object.
172  */
173 u_int
174 mutex_obj_refcnt(kmutex_t *lock)
175 {
176 	struct kmutexobj *mo = (struct kmutexobj *)lock;
177 
178 	return mo->mo_refcnt;
179 }
180