kern_mutex_obj.c revision 1.5.2.2 1 /* $NetBSD: kern_mutex_obj.c,v 1.5.2.2 2011/12/26 16:03:10 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 2008 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.5.2.2 2011/12/26 16:03:10 yamt 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.
85 */
86 kmutex_t *
87 mutex_obj_alloc(kmutex_type_t type, int ipl)
88 {
89 struct kmutexobj *mo;
90
91 mo = pool_cache_get(mutex_obj_cache, PR_WAITOK);
92 mutex_init(&mo->mo_lock, type, ipl);
93 mo->mo_refcnt = 1;
94
95 return (kmutex_t *)mo;
96 }
97
98 /*
99 * mutex_obj_hold:
100 *
101 * Add a single reference to a lock object. A reference to the object
102 * must already be held, and must be held across this call.
103 */
104 void
105 mutex_obj_hold(kmutex_t *lock)
106 {
107 struct kmutexobj *mo = (struct kmutexobj *)lock;
108
109 KASSERTMSG(mo->mo_magic == MUTEX_OBJ_MAGIC,
110 "%s: lock %p: mo->mo_magic (%#x) != MUTEX_OBJ_MAGIC (%#x)",
111 __func__, mo, mo->mo_magic, MUTEX_OBJ_MAGIC);
112 KASSERTMSG(mo->mo_refcnt > 0,
113 "%s: lock %p: mo->mo_refcnt (%#x) == 0",
114 __func__, mo, mo->mo_refcnt);
115
116 atomic_inc_uint(&mo->mo_refcnt);
117 }
118
119 /*
120 * mutex_obj_free:
121 *
122 * Drop a reference from a lock object. If the last reference is being
123 * dropped, free the object and return true. Otherwise, return false.
124 */
125 bool
126 mutex_obj_free(kmutex_t *lock)
127 {
128 struct kmutexobj *mo = (struct kmutexobj *)lock;
129
130 KASSERTMSG(mo->mo_magic == MUTEX_OBJ_MAGIC,
131 "%s: lock %p: mo->mo_magic (%#x) != MUTEX_OBJ_MAGIC (%#x)",
132 __func__, mo, mo->mo_magic, MUTEX_OBJ_MAGIC);
133 KASSERTMSG(mo->mo_refcnt > 0,
134 "%s: lock %p: mo->mo_refcnt (%#x) == 0",
135 __func__, mo, mo->mo_refcnt);
136
137 /*
138 * if mo_refcnt is 1, no one except us have a reference to it and
139 * thus it's stable.
140 */
141 if (mo->mo_refcnt != 1 && atomic_dec_uint_nv(&mo->mo_refcnt) > 0) {
142 return false;
143 }
144 mutex_destroy(&mo->mo_lock);
145 pool_cache_put(mutex_obj_cache, mo);
146 return true;
147 }
148
149 /*
150 * mutex_obj_free_if_last:
151 *
152 * Drop a reference from a lock object if it's the last reference.
153 * If the last reference is being dropped, free the object and return
154 * true. Otherwise, return false.
155 */
156 bool
157 mutex_obj_free_if_last(kmutex_t *lock)
158 {
159 struct kmutexobj *mo = (struct kmutexobj *)lock;
160 bool ret;
161
162 KASSERTMSG(mo->mo_magic == MUTEX_OBJ_MAGIC,
163 "%s: lock %p: mo->mo_magic (%#x) != MUTEX_OBJ_MAGIC (%#x)",
164 __func__, mo, mo->mo_magic, MUTEX_OBJ_MAGIC);
165 KASSERTMSG(mo->mo_refcnt > 0,
166 "%s: lock %p: mo->mo_refcnt (%#x) == 0",
167 __func__, mo, mo->mo_refcnt);
168
169 /*
170 * if mo_refcnt is 1, no one except us have a reference to it and
171 * thus it's stable.
172 */
173 if (mo->mo_refcnt != 1) {
174 return false;
175 }
176 ret = mutex_obj_free(lock);
177 KASSERT(ret);
178 return true;
179 }
180
181 /*
182 * mutex_obj_pause:
183 *
184 * Pause until lock1 is available.
185 * Temporarily release and reacquire lock2.
186 *
187 * Typically used when we need to acquire locks in a reversed order
188 * and trylock failed.
189 */
190 void
191 mutex_obj_pause(kmutex_t *lock1, kmutex_t *lock2)
192 {
193
194 KASSERT(mutex_owned(lock2));
195 mutex_obj_hold(lock1);
196 mutex_exit(lock2);
197 /*
198 * acquire and release lock1.
199 * this can involve priority lending.
200 */
201 mutex_enter(lock1);
202 mutex_exit(lock1);
203 mutex_obj_free(lock1);
204 mutex_enter(lock2);
205 }
206
207 /*
208 * mutex_obj_alloc_kernel_obj_lock:
209 *
210 * mutex_obj_alloc for kernel object lock.
211 * used for bootstrap.
212 */
213 kmutex_t *
214 mutex_obj_alloc_kernel_obj_lock(kmutex_type_t type, int ipl)
215 {
216 static struct kmutexobj kernel_obj_lock;
217 struct kmutexobj *mo = &kernel_obj_lock;
218
219 KASSERT(mo->mo_refcnt == 0);
220 mutex_obj_ctor(NULL, mo, 0);
221 mutex_init(&mo->mo_lock, type, ipl);
222 mo->mo_refcnt = 1;
223 return (kmutex_t *)mo;
224 }
225
226