kern_mutex.c revision 1.36 1 1.36 ad /* $NetBSD: kern_mutex.c,v 1.36 2008/04/27 14:29:09 ad Exp $ */
2 1.2 ad
3 1.2 ad /*-
4 1.30 ad * Copyright (c) 2002, 2006, 2007, 2008 The NetBSD Foundation, Inc.
5 1.2 ad * All rights reserved.
6 1.2 ad *
7 1.2 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.2 ad * by Jason R. Thorpe and Andrew Doran.
9 1.2 ad *
10 1.2 ad * Redistribution and use in source and binary forms, with or without
11 1.2 ad * modification, are permitted provided that the following conditions
12 1.2 ad * are met:
13 1.2 ad * 1. Redistributions of source code must retain the above copyright
14 1.2 ad * notice, this list of conditions and the following disclaimer.
15 1.2 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 ad * notice, this list of conditions and the following disclaimer in the
17 1.2 ad * documentation and/or other materials provided with the distribution.
18 1.2 ad * 3. All advertising materials mentioning features or use of this software
19 1.2 ad * must display the following acknowledgement:
20 1.2 ad * This product includes software developed by the NetBSD
21 1.2 ad * Foundation, Inc. and its contributors.
22 1.2 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.2 ad * contributors may be used to endorse or promote products derived
24 1.2 ad * from this software without specific prior written permission.
25 1.2 ad *
26 1.2 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.2 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.2 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.2 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.2 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.2 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.2 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.2 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.2 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.2 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.2 ad * POSSIBILITY OF SUCH DAMAGE.
37 1.2 ad */
38 1.2 ad
39 1.2 ad /*
40 1.2 ad * Kernel mutex implementation, modeled after those found in Solaris,
41 1.2 ad * a description of which can be found in:
42 1.2 ad *
43 1.2 ad * Solaris Internals: Core Kernel Architecture, Jim Mauro and
44 1.2 ad * Richard McDougall.
45 1.2 ad */
46 1.2 ad
47 1.2 ad #define __MUTEX_PRIVATE
48 1.2 ad
49 1.2 ad #include <sys/cdefs.h>
50 1.36 ad __KERNEL_RCSID(0, "$NetBSD: kern_mutex.c,v 1.36 2008/04/27 14:29:09 ad Exp $");
51 1.18 dsl
52 1.18 dsl #include "opt_multiprocessor.h"
53 1.2 ad
54 1.2 ad #include <sys/param.h>
55 1.2 ad #include <sys/proc.h>
56 1.2 ad #include <sys/mutex.h>
57 1.2 ad #include <sys/sched.h>
58 1.2 ad #include <sys/sleepq.h>
59 1.2 ad #include <sys/systm.h>
60 1.2 ad #include <sys/lockdebug.h>
61 1.2 ad #include <sys/kernel.h>
62 1.24 ad #include <sys/atomic.h>
63 1.24 ad #include <sys/intr.h>
64 1.29 xtraeme #include <sys/lock.h>
65 1.31 ad #include <sys/pool.h>
66 1.2 ad
67 1.2 ad #include <dev/lockstat.h>
68 1.2 ad
69 1.28 ad #include <machine/lock.h>
70 1.28 ad
71 1.2 ad /*
72 1.2 ad * When not running a debug kernel, spin mutexes are not much
73 1.2 ad * more than an splraiseipl() and splx() pair.
74 1.2 ad */
75 1.2 ad
76 1.2 ad #if defined(DIAGNOSTIC) || defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
77 1.2 ad #define FULL
78 1.2 ad #endif
79 1.2 ad
80 1.2 ad /*
81 1.2 ad * Debugging support.
82 1.2 ad */
83 1.2 ad
84 1.2 ad #define MUTEX_WANTLOCK(mtx) \
85 1.23 yamt LOCKDEBUG_WANTLOCK(MUTEX_DEBUG_P(mtx), (mtx), \
86 1.2 ad (uintptr_t)__builtin_return_address(0), 0)
87 1.2 ad #define MUTEX_LOCKED(mtx) \
88 1.23 yamt LOCKDEBUG_LOCKED(MUTEX_DEBUG_P(mtx), (mtx), \
89 1.2 ad (uintptr_t)__builtin_return_address(0), 0)
90 1.2 ad #define MUTEX_UNLOCKED(mtx) \
91 1.23 yamt LOCKDEBUG_UNLOCKED(MUTEX_DEBUG_P(mtx), (mtx), \
92 1.2 ad (uintptr_t)__builtin_return_address(0), 0)
93 1.2 ad #define MUTEX_ABORT(mtx, msg) \
94 1.17 ad mutex_abort(mtx, __func__, msg)
95 1.2 ad
96 1.2 ad #if defined(LOCKDEBUG)
97 1.2 ad
98 1.2 ad #define MUTEX_DASSERT(mtx, cond) \
99 1.2 ad do { \
100 1.2 ad if (!(cond)) \
101 1.2 ad MUTEX_ABORT(mtx, "assertion failed: " #cond); \
102 1.2 ad } while (/* CONSTCOND */ 0);
103 1.2 ad
104 1.2 ad #else /* LOCKDEBUG */
105 1.2 ad
106 1.2 ad #define MUTEX_DASSERT(mtx, cond) /* nothing */
107 1.2 ad
108 1.2 ad #endif /* LOCKDEBUG */
109 1.2 ad
110 1.2 ad #if defined(DIAGNOSTIC)
111 1.2 ad
112 1.2 ad #define MUTEX_ASSERT(mtx, cond) \
113 1.2 ad do { \
114 1.2 ad if (!(cond)) \
115 1.2 ad MUTEX_ABORT(mtx, "assertion failed: " #cond); \
116 1.2 ad } while (/* CONSTCOND */ 0)
117 1.2 ad
118 1.2 ad #else /* DIAGNOSTIC */
119 1.2 ad
120 1.2 ad #define MUTEX_ASSERT(mtx, cond) /* nothing */
121 1.2 ad
122 1.2 ad #endif /* DIAGNOSTIC */
123 1.2 ad
124 1.2 ad /*
125 1.2 ad * Spin mutex SPL save / restore.
126 1.2 ad */
127 1.12 matt #ifndef MUTEX_COUNT_BIAS
128 1.12 matt #define MUTEX_COUNT_BIAS 0
129 1.12 matt #endif
130 1.2 ad
131 1.2 ad #define MUTEX_SPIN_SPLRAISE(mtx) \
132 1.2 ad do { \
133 1.36 ad struct cpu_info *x__ci; \
134 1.2 ad int x__cnt, s; \
135 1.36 ad s = splraiseipl(mtx->mtx_ipl); \
136 1.36 ad x__ci = curcpu(); \
137 1.2 ad x__cnt = x__ci->ci_mtx_count--; \
138 1.12 matt if (x__cnt == MUTEX_COUNT_BIAS) \
139 1.2 ad x__ci->ci_mtx_oldspl = (s); \
140 1.2 ad } while (/* CONSTCOND */ 0)
141 1.2 ad
142 1.2 ad #define MUTEX_SPIN_SPLRESTORE(mtx) \
143 1.2 ad do { \
144 1.2 ad struct cpu_info *x__ci = curcpu(); \
145 1.2 ad int s = x__ci->ci_mtx_oldspl; \
146 1.2 ad __insn_barrier(); \
147 1.12 matt if (++(x__ci->ci_mtx_count) == MUTEX_COUNT_BIAS) \
148 1.2 ad splx(s); \
149 1.2 ad } while (/* CONSTCOND */ 0)
150 1.2 ad
151 1.2 ad /*
152 1.2 ad * For architectures that provide 'simple' mutexes: they provide a
153 1.2 ad * CAS function that is either MP-safe, or does not need to be MP
154 1.2 ad * safe. Adaptive mutexes on these architectures do not require an
155 1.2 ad * additional interlock.
156 1.2 ad */
157 1.2 ad
158 1.2 ad #ifdef __HAVE_SIMPLE_MUTEXES
159 1.2 ad
160 1.2 ad #define MUTEX_OWNER(owner) \
161 1.2 ad (owner & MUTEX_THREAD)
162 1.2 ad #define MUTEX_HAS_WAITERS(mtx) \
163 1.2 ad (((int)(mtx)->mtx_owner & MUTEX_BIT_WAITERS) != 0)
164 1.2 ad
165 1.23 yamt #define MUTEX_INITIALIZE_ADAPTIVE(mtx, dodebug) \
166 1.2 ad do { \
167 1.23 yamt if (dodebug) \
168 1.23 yamt (mtx)->mtx_owner |= MUTEX_BIT_DEBUG; \
169 1.2 ad } while (/* CONSTCOND */ 0);
170 1.2 ad
171 1.23 yamt #define MUTEX_INITIALIZE_SPIN(mtx, dodebug, ipl) \
172 1.2 ad do { \
173 1.2 ad (mtx)->mtx_owner = MUTEX_BIT_SPIN; \
174 1.23 yamt if (dodebug) \
175 1.23 yamt (mtx)->mtx_owner |= MUTEX_BIT_DEBUG; \
176 1.2 ad (mtx)->mtx_ipl = makeiplcookie((ipl)); \
177 1.2 ad __cpu_simple_lock_init(&(mtx)->mtx_lock); \
178 1.2 ad } while (/* CONSTCOND */ 0)
179 1.2 ad
180 1.2 ad #define MUTEX_DESTROY(mtx) \
181 1.2 ad do { \
182 1.2 ad (mtx)->mtx_owner = MUTEX_THREAD; \
183 1.2 ad } while (/* CONSTCOND */ 0);
184 1.2 ad
185 1.2 ad #define MUTEX_SPIN_P(mtx) \
186 1.2 ad (((mtx)->mtx_owner & MUTEX_BIT_SPIN) != 0)
187 1.2 ad #define MUTEX_ADAPTIVE_P(mtx) \
188 1.2 ad (((mtx)->mtx_owner & MUTEX_BIT_SPIN) == 0)
189 1.2 ad
190 1.23 yamt #define MUTEX_DEBUG_P(mtx) (((mtx)->mtx_owner & MUTEX_BIT_DEBUG) != 0)
191 1.23 yamt #if defined(LOCKDEBUG)
192 1.23 yamt #define MUTEX_OWNED(owner) (((owner) & ~MUTEX_BIT_DEBUG) != 0)
193 1.23 yamt #define MUTEX_INHERITDEBUG(new, old) (new) |= (old) & MUTEX_BIT_DEBUG
194 1.23 yamt #else /* defined(LOCKDEBUG) */
195 1.23 yamt #define MUTEX_OWNED(owner) ((owner) != 0)
196 1.23 yamt #define MUTEX_INHERITDEBUG(new, old) /* nothing */
197 1.23 yamt #endif /* defined(LOCKDEBUG) */
198 1.2 ad
199 1.2 ad static inline int
200 1.2 ad MUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread)
201 1.2 ad {
202 1.2 ad int rv;
203 1.23 yamt uintptr_t old = 0;
204 1.23 yamt uintptr_t new = curthread;
205 1.23 yamt
206 1.23 yamt MUTEX_INHERITDEBUG(old, mtx->mtx_owner);
207 1.23 yamt MUTEX_INHERITDEBUG(new, old);
208 1.23 yamt rv = MUTEX_CAS(&mtx->mtx_owner, old, new);
209 1.7 itohy MUTEX_RECEIVE(mtx);
210 1.2 ad return rv;
211 1.2 ad }
212 1.2 ad
213 1.2 ad static inline int
214 1.2 ad MUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner)
215 1.2 ad {
216 1.2 ad int rv;
217 1.2 ad rv = MUTEX_CAS(&mtx->mtx_owner, owner, owner | MUTEX_BIT_WAITERS);
218 1.7 itohy MUTEX_RECEIVE(mtx);
219 1.2 ad return rv;
220 1.2 ad }
221 1.2 ad
222 1.2 ad static inline void
223 1.2 ad MUTEX_RELEASE(kmutex_t *mtx)
224 1.2 ad {
225 1.23 yamt uintptr_t new;
226 1.23 yamt
227 1.7 itohy MUTEX_GIVE(mtx);
228 1.23 yamt new = 0;
229 1.23 yamt MUTEX_INHERITDEBUG(new, mtx->mtx_owner);
230 1.23 yamt mtx->mtx_owner = new;
231 1.2 ad }
232 1.4 ad
233 1.4 ad static inline void
234 1.4 ad MUTEX_CLEAR_WAITERS(kmutex_t *mtx)
235 1.4 ad {
236 1.4 ad /* nothing */
237 1.4 ad }
238 1.2 ad #endif /* __HAVE_SIMPLE_MUTEXES */
239 1.2 ad
240 1.2 ad /*
241 1.2 ad * Patch in stubs via strong alias where they are not available.
242 1.2 ad */
243 1.2 ad
244 1.2 ad #if defined(LOCKDEBUG)
245 1.2 ad #undef __HAVE_MUTEX_STUBS
246 1.2 ad #undef __HAVE_SPIN_MUTEX_STUBS
247 1.2 ad #endif
248 1.2 ad
249 1.2 ad #ifndef __HAVE_MUTEX_STUBS
250 1.8 itohy __strong_alias(mutex_enter,mutex_vector_enter);
251 1.8 itohy __strong_alias(mutex_exit,mutex_vector_exit);
252 1.2 ad #endif
253 1.2 ad
254 1.2 ad #ifndef __HAVE_SPIN_MUTEX_STUBS
255 1.8 itohy __strong_alias(mutex_spin_enter,mutex_vector_enter);
256 1.8 itohy __strong_alias(mutex_spin_exit,mutex_vector_exit);
257 1.2 ad #endif
258 1.2 ad
259 1.2 ad void mutex_abort(kmutex_t *, const char *, const char *);
260 1.2 ad void mutex_dump(volatile void *);
261 1.2 ad int mutex_onproc(uintptr_t, struct cpu_info **);
262 1.2 ad
263 1.2 ad lockops_t mutex_spin_lockops = {
264 1.2 ad "Mutex",
265 1.2 ad 0,
266 1.2 ad mutex_dump
267 1.2 ad };
268 1.2 ad
269 1.2 ad lockops_t mutex_adaptive_lockops = {
270 1.2 ad "Mutex",
271 1.2 ad 1,
272 1.2 ad mutex_dump
273 1.2 ad };
274 1.2 ad
275 1.5 yamt syncobj_t mutex_syncobj = {
276 1.5 yamt SOBJ_SLEEPQ_SORTED,
277 1.5 yamt turnstile_unsleep,
278 1.5 yamt turnstile_changepri,
279 1.5 yamt sleepq_lendpri,
280 1.27 ad (void *)mutex_owner,
281 1.5 yamt };
282 1.5 yamt
283 1.31 ad /* Mutex cache */
284 1.31 ad #define MUTEX_OBJ_MAGIC 0x5aa3c85d
285 1.31 ad struct kmutexobj {
286 1.31 ad kmutex_t mo_lock;
287 1.31 ad u_int mo_magic;
288 1.31 ad u_int mo_refcnt;
289 1.31 ad };
290 1.31 ad
291 1.31 ad static int mutex_obj_ctor(void *, void *, int);
292 1.31 ad
293 1.31 ad static pool_cache_t mutex_obj_cache;
294 1.31 ad
295 1.2 ad /*
296 1.2 ad * mutex_dump:
297 1.2 ad *
298 1.2 ad * Dump the contents of a mutex structure.
299 1.2 ad */
300 1.2 ad void
301 1.2 ad mutex_dump(volatile void *cookie)
302 1.2 ad {
303 1.2 ad volatile kmutex_t *mtx = cookie;
304 1.2 ad
305 1.2 ad printf_nolog("owner field : %#018lx wait/spin: %16d/%d\n",
306 1.2 ad (long)MUTEX_OWNER(mtx->mtx_owner), MUTEX_HAS_WAITERS(mtx),
307 1.2 ad MUTEX_SPIN_P(mtx));
308 1.2 ad }
309 1.2 ad
310 1.2 ad /*
311 1.2 ad * mutex_abort:
312 1.2 ad *
313 1.3 ad * Dump information about an error and panic the system. This
314 1.3 ad * generates a lot of machine code in the DIAGNOSTIC case, so
315 1.3 ad * we ask the compiler to not inline it.
316 1.2 ad */
317 1.8 itohy
318 1.8 itohy #if __GNUC_PREREQ__(3, 0)
319 1.8 itohy __attribute ((noinline)) __attribute ((noreturn))
320 1.8 itohy #endif
321 1.8 itohy void
322 1.2 ad mutex_abort(kmutex_t *mtx, const char *func, const char *msg)
323 1.2 ad {
324 1.2 ad
325 1.23 yamt LOCKDEBUG_ABORT(mtx, (MUTEX_SPIN_P(mtx) ?
326 1.3 ad &mutex_spin_lockops : &mutex_adaptive_lockops), func, msg);
327 1.2 ad /* NOTREACHED */
328 1.2 ad }
329 1.2 ad
330 1.2 ad /*
331 1.2 ad * mutex_init:
332 1.2 ad *
333 1.2 ad * Initialize a mutex for use. Note that adaptive mutexes are in
334 1.2 ad * essence spin mutexes that can sleep to avoid deadlock and wasting
335 1.2 ad * CPU time. We can't easily provide a type of mutex that always
336 1.2 ad * sleeps - see comments in mutex_vector_enter() about releasing
337 1.2 ad * mutexes unlocked.
338 1.2 ad */
339 1.2 ad void
340 1.2 ad mutex_init(kmutex_t *mtx, kmutex_type_t type, int ipl)
341 1.2 ad {
342 1.23 yamt bool dodebug;
343 1.2 ad
344 1.2 ad memset(mtx, 0, sizeof(*mtx));
345 1.2 ad
346 1.15 ad switch (type) {
347 1.15 ad case MUTEX_ADAPTIVE:
348 1.15 ad KASSERT(ipl == IPL_NONE);
349 1.15 ad break;
350 1.22 ad case MUTEX_DEFAULT:
351 1.15 ad case MUTEX_DRIVER:
352 1.26 ad if (ipl == IPL_NONE || ipl == IPL_SOFTCLOCK ||
353 1.26 ad ipl == IPL_SOFTBIO || ipl == IPL_SOFTNET ||
354 1.26 ad ipl == IPL_SOFTSERIAL) {
355 1.22 ad type = MUTEX_ADAPTIVE;
356 1.26 ad } else {
357 1.22 ad type = MUTEX_SPIN;
358 1.22 ad }
359 1.15 ad break;
360 1.15 ad default:
361 1.15 ad break;
362 1.15 ad }
363 1.2 ad
364 1.2 ad switch (type) {
365 1.11 ad case MUTEX_NODEBUG:
366 1.23 yamt dodebug = LOCKDEBUG_ALLOC(mtx, NULL,
367 1.19 ad (uintptr_t)__builtin_return_address(0));
368 1.23 yamt MUTEX_INITIALIZE_SPIN(mtx, dodebug, ipl);
369 1.11 ad break;
370 1.2 ad case MUTEX_ADAPTIVE:
371 1.23 yamt dodebug = LOCKDEBUG_ALLOC(mtx, &mutex_adaptive_lockops,
372 1.19 ad (uintptr_t)__builtin_return_address(0));
373 1.23 yamt MUTEX_INITIALIZE_ADAPTIVE(mtx, dodebug);
374 1.2 ad break;
375 1.2 ad case MUTEX_SPIN:
376 1.23 yamt dodebug = LOCKDEBUG_ALLOC(mtx, &mutex_spin_lockops,
377 1.19 ad (uintptr_t)__builtin_return_address(0));
378 1.23 yamt MUTEX_INITIALIZE_SPIN(mtx, dodebug, ipl);
379 1.2 ad break;
380 1.2 ad default:
381 1.2 ad panic("mutex_init: impossible type");
382 1.2 ad break;
383 1.2 ad }
384 1.2 ad }
385 1.2 ad
386 1.2 ad /*
387 1.2 ad * mutex_destroy:
388 1.2 ad *
389 1.2 ad * Tear down a mutex.
390 1.2 ad */
391 1.2 ad void
392 1.2 ad mutex_destroy(kmutex_t *mtx)
393 1.2 ad {
394 1.2 ad
395 1.2 ad if (MUTEX_ADAPTIVE_P(mtx)) {
396 1.2 ad MUTEX_ASSERT(mtx, !MUTEX_OWNED(mtx->mtx_owner) &&
397 1.2 ad !MUTEX_HAS_WAITERS(mtx));
398 1.2 ad } else {
399 1.16 skrll MUTEX_ASSERT(mtx, !__SIMPLELOCK_LOCKED_P(&mtx->mtx_lock));
400 1.2 ad }
401 1.2 ad
402 1.23 yamt LOCKDEBUG_FREE(MUTEX_DEBUG_P(mtx), mtx);
403 1.2 ad MUTEX_DESTROY(mtx);
404 1.2 ad }
405 1.2 ad
406 1.2 ad /*
407 1.2 ad * mutex_onproc:
408 1.2 ad *
409 1.2 ad * Return true if an adaptive mutex owner is running on a CPU in the
410 1.2 ad * system. If the target is waiting on the kernel big lock, then we
411 1.15 ad * must release it. This is necessary to avoid deadlock.
412 1.2 ad *
413 1.2 ad * Note that we can't use the mutex owner field as an LWP pointer. We
414 1.2 ad * don't have full control over the timing of our execution, and so the
415 1.2 ad * pointer could be completely invalid by the time we dereference it.
416 1.2 ad */
417 1.2 ad #ifdef MULTIPROCESSOR
418 1.2 ad int
419 1.2 ad mutex_onproc(uintptr_t owner, struct cpu_info **cip)
420 1.2 ad {
421 1.2 ad CPU_INFO_ITERATOR cii;
422 1.2 ad struct cpu_info *ci;
423 1.2 ad struct lwp *l;
424 1.2 ad
425 1.2 ad if (!MUTEX_OWNED(owner))
426 1.2 ad return 0;
427 1.2 ad l = (struct lwp *)MUTEX_OWNER(owner);
428 1.2 ad
429 1.15 ad /* See if the target is running on a CPU somewhere. */
430 1.10 ad if ((ci = *cip) != NULL && ci->ci_curlwp == l)
431 1.15 ad goto run;
432 1.15 ad for (CPU_INFO_FOREACH(cii, ci))
433 1.15 ad if (ci->ci_curlwp == l)
434 1.15 ad goto run;
435 1.2 ad
436 1.15 ad /* No: it may be safe to block now. */
437 1.2 ad *cip = NULL;
438 1.2 ad return 0;
439 1.15 ad
440 1.15 ad run:
441 1.15 ad /* Target is running; do we need to block? */
442 1.15 ad *cip = ci;
443 1.15 ad return ci->ci_biglock_wanted != l;
444 1.2 ad }
445 1.15 ad #endif /* MULTIPROCESSOR */
446 1.2 ad
447 1.2 ad /*
448 1.2 ad * mutex_vector_enter:
449 1.2 ad *
450 1.2 ad * Support routine for mutex_enter() that must handles all cases. In
451 1.2 ad * the LOCKDEBUG case, mutex_enter() is always aliased here, even if
452 1.2 ad * fast-path stubs are available. If an mutex_spin_enter() stub is
453 1.2 ad * not available, then it is also aliased directly here.
454 1.2 ad */
455 1.2 ad void
456 1.2 ad mutex_vector_enter(kmutex_t *mtx)
457 1.2 ad {
458 1.2 ad uintptr_t owner, curthread;
459 1.2 ad turnstile_t *ts;
460 1.2 ad #ifdef MULTIPROCESSOR
461 1.2 ad struct cpu_info *ci = NULL;
462 1.2 ad u_int count;
463 1.2 ad #endif
464 1.2 ad LOCKSTAT_COUNTER(spincnt);
465 1.2 ad LOCKSTAT_COUNTER(slpcnt);
466 1.2 ad LOCKSTAT_TIMER(spintime);
467 1.2 ad LOCKSTAT_TIMER(slptime);
468 1.2 ad LOCKSTAT_FLAG(lsflag);
469 1.2 ad
470 1.2 ad /*
471 1.2 ad * Handle spin mutexes.
472 1.2 ad */
473 1.2 ad if (MUTEX_SPIN_P(mtx)) {
474 1.2 ad #if defined(LOCKDEBUG) && defined(MULTIPROCESSOR)
475 1.2 ad u_int spins = 0;
476 1.2 ad #endif
477 1.2 ad MUTEX_SPIN_SPLRAISE(mtx);
478 1.2 ad MUTEX_WANTLOCK(mtx);
479 1.2 ad #ifdef FULL
480 1.2 ad if (__cpu_simple_lock_try(&mtx->mtx_lock)) {
481 1.2 ad MUTEX_LOCKED(mtx);
482 1.2 ad return;
483 1.2 ad }
484 1.2 ad #if !defined(MULTIPROCESSOR)
485 1.2 ad MUTEX_ABORT(mtx, "locking against myself");
486 1.2 ad #else /* !MULTIPROCESSOR */
487 1.2 ad
488 1.2 ad LOCKSTAT_ENTER(lsflag);
489 1.2 ad LOCKSTAT_START_TIMER(lsflag, spintime);
490 1.2 ad count = SPINLOCK_BACKOFF_MIN;
491 1.2 ad
492 1.2 ad /*
493 1.2 ad * Spin testing the lock word and do exponential backoff
494 1.2 ad * to reduce cache line ping-ponging between CPUs.
495 1.2 ad */
496 1.2 ad do {
497 1.2 ad if (panicstr != NULL)
498 1.2 ad break;
499 1.16 skrll while (__SIMPLELOCK_LOCKED_P(&mtx->mtx_lock)) {
500 1.2 ad SPINLOCK_BACKOFF(count);
501 1.2 ad #ifdef LOCKDEBUG
502 1.2 ad if (SPINLOCK_SPINOUT(spins))
503 1.2 ad MUTEX_ABORT(mtx, "spinout");
504 1.2 ad #endif /* LOCKDEBUG */
505 1.2 ad }
506 1.2 ad } while (!__cpu_simple_lock_try(&mtx->mtx_lock));
507 1.2 ad
508 1.2 ad if (count != SPINLOCK_BACKOFF_MIN) {
509 1.2 ad LOCKSTAT_STOP_TIMER(lsflag, spintime);
510 1.2 ad LOCKSTAT_EVENT(lsflag, mtx,
511 1.2 ad LB_SPIN_MUTEX | LB_SPIN, 1, spintime);
512 1.2 ad }
513 1.2 ad LOCKSTAT_EXIT(lsflag);
514 1.2 ad #endif /* !MULTIPROCESSOR */
515 1.2 ad #endif /* FULL */
516 1.2 ad MUTEX_LOCKED(mtx);
517 1.2 ad return;
518 1.2 ad }
519 1.2 ad
520 1.2 ad curthread = (uintptr_t)curlwp;
521 1.2 ad
522 1.2 ad MUTEX_DASSERT(mtx, MUTEX_ADAPTIVE_P(mtx));
523 1.2 ad MUTEX_ASSERT(mtx, curthread != 0);
524 1.2 ad MUTEX_WANTLOCK(mtx);
525 1.2 ad
526 1.2 ad if (panicstr == NULL) {
527 1.2 ad LOCKDEBUG_BARRIER(&kernel_lock, 1);
528 1.2 ad }
529 1.2 ad
530 1.2 ad LOCKSTAT_ENTER(lsflag);
531 1.2 ad
532 1.2 ad /*
533 1.2 ad * Adaptive mutex; spin trying to acquire the mutex. If we
534 1.2 ad * determine that the owner is not running on a processor,
535 1.2 ad * then we stop spinning, and sleep instead.
536 1.2 ad */
537 1.34 ad for (owner = mtx->mtx_owner;;) {
538 1.2 ad if (!MUTEX_OWNED(owner)) {
539 1.2 ad /*
540 1.2 ad * Mutex owner clear could mean two things:
541 1.2 ad *
542 1.2 ad * * The mutex has been released.
543 1.2 ad * * The owner field hasn't been set yet.
544 1.2 ad *
545 1.2 ad * Try to acquire it again. If that fails,
546 1.2 ad * we'll just loop again.
547 1.2 ad */
548 1.2 ad if (MUTEX_ACQUIRE(mtx, curthread))
549 1.2 ad break;
550 1.34 ad owner = mtx->mtx_owner;
551 1.2 ad continue;
552 1.2 ad }
553 1.2 ad
554 1.2 ad if (panicstr != NULL)
555 1.2 ad return;
556 1.2 ad if (MUTEX_OWNER(owner) == curthread)
557 1.2 ad MUTEX_ABORT(mtx, "locking against myself");
558 1.2 ad
559 1.2 ad #ifdef MULTIPROCESSOR
560 1.2 ad /*
561 1.2 ad * Check to see if the owner is running on a processor.
562 1.2 ad * If so, then we should just spin, as the owner will
563 1.2 ad * likely release the lock very soon.
564 1.2 ad */
565 1.2 ad if (mutex_onproc(owner, &ci)) {
566 1.2 ad LOCKSTAT_START_TIMER(lsflag, spintime);
567 1.2 ad count = SPINLOCK_BACKOFF_MIN;
568 1.2 ad for (;;) {
569 1.34 ad SPINLOCK_BACKOFF(count);
570 1.2 ad owner = mtx->mtx_owner;
571 1.2 ad if (!mutex_onproc(owner, &ci))
572 1.2 ad break;
573 1.2 ad }
574 1.2 ad LOCKSTAT_STOP_TIMER(lsflag, spintime);
575 1.2 ad LOCKSTAT_COUNT(spincnt, 1);
576 1.2 ad if (!MUTEX_OWNED(owner))
577 1.2 ad continue;
578 1.2 ad }
579 1.2 ad #endif
580 1.2 ad
581 1.2 ad ts = turnstile_lookup(mtx);
582 1.2 ad
583 1.2 ad /*
584 1.2 ad * Once we have the turnstile chain interlock, mark the
585 1.2 ad * mutex has having waiters. If that fails, spin again:
586 1.2 ad * chances are that the mutex has been released.
587 1.2 ad */
588 1.2 ad if (!MUTEX_SET_WAITERS(mtx, owner)) {
589 1.2 ad turnstile_exit(mtx);
590 1.34 ad owner = mtx->mtx_owner;
591 1.2 ad continue;
592 1.2 ad }
593 1.2 ad
594 1.2 ad #ifdef MULTIPROCESSOR
595 1.2 ad /*
596 1.2 ad * mutex_exit() is permitted to release the mutex without
597 1.2 ad * any interlocking instructions, and the following can
598 1.2 ad * occur as a result:
599 1.2 ad *
600 1.2 ad * CPU 1: MUTEX_SET_WAITERS() CPU2: mutex_exit()
601 1.2 ad * ---------------------------- ----------------------------
602 1.2 ad * .. acquire cache line
603 1.2 ad * .. test for waiters
604 1.2 ad * acquire cache line <- lose cache line
605 1.2 ad * lock cache line ..
606 1.2 ad * verify mutex is held ..
607 1.2 ad * set waiters ..
608 1.2 ad * unlock cache line ..
609 1.2 ad * lose cache line -> acquire cache line
610 1.2 ad * .. clear lock word, waiters
611 1.2 ad * return success
612 1.2 ad *
613 1.2 ad * There is a another race that can occur: a third CPU could
614 1.2 ad * acquire the mutex as soon as it is released. Since
615 1.2 ad * adaptive mutexes are primarily spin mutexes, this is not
616 1.2 ad * something that we need to worry about too much. What we
617 1.2 ad * do need to ensure is that the waiters bit gets set.
618 1.2 ad *
619 1.2 ad * To allow the unlocked release, we need to make some
620 1.2 ad * assumptions here:
621 1.2 ad *
622 1.2 ad * o Release is the only non-atomic/unlocked operation
623 1.2 ad * that can be performed on the mutex. (It must still
624 1.2 ad * be atomic on the local CPU, e.g. in case interrupted
625 1.2 ad * or preempted).
626 1.2 ad *
627 1.2 ad * o At any given time, MUTEX_SET_WAITERS() can only ever
628 1.21 pooka * be in progress on one CPU in the system - guaranteed
629 1.2 ad * by the turnstile chain lock.
630 1.2 ad *
631 1.2 ad * o No other operations other than MUTEX_SET_WAITERS()
632 1.2 ad * and release can modify a mutex with a non-zero
633 1.2 ad * owner field.
634 1.2 ad *
635 1.2 ad * o The result of a successful MUTEX_SET_WAITERS() call
636 1.2 ad * is an unbuffered write that is immediately visible
637 1.2 ad * to all other processors in the system.
638 1.2 ad *
639 1.2 ad * o If the holding LWP switches away, it posts a store
640 1.2 ad * fence before changing curlwp, ensuring that any
641 1.2 ad * overwrite of the mutex waiters flag by mutex_exit()
642 1.2 ad * completes before the modification of curlwp becomes
643 1.2 ad * visible to this CPU.
644 1.2 ad *
645 1.14 yamt * o mi_switch() posts a store fence before setting curlwp
646 1.2 ad * and before resuming execution of an LWP.
647 1.2 ad *
648 1.2 ad * o _kernel_lock() posts a store fence before setting
649 1.2 ad * curcpu()->ci_biglock_wanted, and after clearing it.
650 1.2 ad * This ensures that any overwrite of the mutex waiters
651 1.2 ad * flag by mutex_exit() completes before the modification
652 1.2 ad * of ci_biglock_wanted becomes visible.
653 1.2 ad *
654 1.2 ad * We now post a read memory barrier (after setting the
655 1.2 ad * waiters field) and check the lock holder's status again.
656 1.2 ad * Some of the possible outcomes (not an exhaustive list):
657 1.2 ad *
658 1.2 ad * 1. The onproc check returns true: the holding LWP is
659 1.2 ad * running again. The lock may be released soon and
660 1.2 ad * we should spin. Importantly, we can't trust the
661 1.2 ad * value of the waiters flag.
662 1.2 ad *
663 1.2 ad * 2. The onproc check returns false: the holding LWP is
664 1.2 ad * not running. We now have the oppertunity to check
665 1.2 ad * if mutex_exit() has blatted the modifications made
666 1.2 ad * by MUTEX_SET_WAITERS().
667 1.2 ad *
668 1.2 ad * 3. The onproc check returns false: the holding LWP may
669 1.2 ad * or may not be running. It has context switched at
670 1.2 ad * some point during our check. Again, we have the
671 1.2 ad * chance to see if the waiters bit is still set or
672 1.2 ad * has been overwritten.
673 1.2 ad *
674 1.2 ad * 4. The onproc check returns false: the holding LWP is
675 1.2 ad * running on a CPU, but wants the big lock. It's OK
676 1.2 ad * to check the waiters field in this case.
677 1.2 ad *
678 1.2 ad * 5. The has-waiters check fails: the mutex has been
679 1.2 ad * released, the waiters flag cleared and another LWP
680 1.2 ad * now owns the mutex.
681 1.2 ad *
682 1.2 ad * 6. The has-waiters check fails: the mutex has been
683 1.2 ad * released.
684 1.2 ad *
685 1.2 ad * If the waiters bit is not set it's unsafe to go asleep,
686 1.2 ad * as we might never be awoken.
687 1.2 ad */
688 1.24 ad if ((membar_consumer(), mutex_onproc(owner, &ci)) ||
689 1.24 ad (membar_consumer(), !MUTEX_HAS_WAITERS(mtx))) {
690 1.2 ad turnstile_exit(mtx);
691 1.34 ad owner = mtx->mtx_owner;
692 1.2 ad continue;
693 1.2 ad }
694 1.2 ad #endif /* MULTIPROCESSOR */
695 1.2 ad
696 1.2 ad LOCKSTAT_START_TIMER(lsflag, slptime);
697 1.2 ad
698 1.5 yamt turnstile_block(ts, TS_WRITER_Q, mtx, &mutex_syncobj);
699 1.2 ad
700 1.2 ad LOCKSTAT_STOP_TIMER(lsflag, slptime);
701 1.2 ad LOCKSTAT_COUNT(slpcnt, 1);
702 1.34 ad
703 1.34 ad owner = mtx->mtx_owner;
704 1.2 ad }
705 1.2 ad
706 1.2 ad LOCKSTAT_EVENT(lsflag, mtx, LB_ADAPTIVE_MUTEX | LB_SLEEP1,
707 1.2 ad slpcnt, slptime);
708 1.2 ad LOCKSTAT_EVENT(lsflag, mtx, LB_ADAPTIVE_MUTEX | LB_SPIN,
709 1.2 ad spincnt, spintime);
710 1.2 ad LOCKSTAT_EXIT(lsflag);
711 1.2 ad
712 1.2 ad MUTEX_DASSERT(mtx, MUTEX_OWNER(mtx->mtx_owner) == curthread);
713 1.2 ad MUTEX_LOCKED(mtx);
714 1.2 ad }
715 1.2 ad
716 1.2 ad /*
717 1.2 ad * mutex_vector_exit:
718 1.2 ad *
719 1.2 ad * Support routine for mutex_exit() that handles all cases.
720 1.2 ad */
721 1.2 ad void
722 1.2 ad mutex_vector_exit(kmutex_t *mtx)
723 1.2 ad {
724 1.2 ad turnstile_t *ts;
725 1.2 ad uintptr_t curthread;
726 1.2 ad
727 1.2 ad if (MUTEX_SPIN_P(mtx)) {
728 1.2 ad #ifdef FULL
729 1.33 ad if (__predict_false(!__SIMPLELOCK_LOCKED_P(&mtx->mtx_lock))) {
730 1.33 ad if (panicstr != NULL)
731 1.33 ad return;
732 1.2 ad MUTEX_ABORT(mtx, "exiting unheld spin mutex");
733 1.33 ad }
734 1.2 ad MUTEX_UNLOCKED(mtx);
735 1.2 ad __cpu_simple_unlock(&mtx->mtx_lock);
736 1.2 ad #endif
737 1.2 ad MUTEX_SPIN_SPLRESTORE(mtx);
738 1.2 ad return;
739 1.2 ad }
740 1.2 ad
741 1.11 ad if (__predict_false((uintptr_t)panicstr | cold)) {
742 1.2 ad MUTEX_UNLOCKED(mtx);
743 1.2 ad MUTEX_RELEASE(mtx);
744 1.2 ad return;
745 1.2 ad }
746 1.2 ad
747 1.2 ad curthread = (uintptr_t)curlwp;
748 1.2 ad MUTEX_DASSERT(mtx, curthread != 0);
749 1.2 ad MUTEX_ASSERT(mtx, MUTEX_OWNER(mtx->mtx_owner) == curthread);
750 1.2 ad MUTEX_UNLOCKED(mtx);
751 1.2 ad
752 1.15 ad #ifdef LOCKDEBUG
753 1.15 ad /*
754 1.15 ad * Avoid having to take the turnstile chain lock every time
755 1.15 ad * around. Raise the priority level to splhigh() in order
756 1.15 ad * to disable preemption and so make the following atomic.
757 1.15 ad */
758 1.15 ad {
759 1.15 ad int s = splhigh();
760 1.15 ad if (!MUTEX_HAS_WAITERS(mtx)) {
761 1.15 ad MUTEX_RELEASE(mtx);
762 1.15 ad splx(s);
763 1.15 ad return;
764 1.15 ad }
765 1.15 ad splx(s);
766 1.15 ad }
767 1.15 ad #endif
768 1.15 ad
769 1.2 ad /*
770 1.2 ad * Get this lock's turnstile. This gets the interlock on
771 1.2 ad * the sleep queue. Once we have that, we can clear the
772 1.2 ad * lock. If there was no turnstile for the lock, there
773 1.2 ad * were no waiters remaining.
774 1.2 ad */
775 1.2 ad ts = turnstile_lookup(mtx);
776 1.2 ad
777 1.2 ad if (ts == NULL) {
778 1.2 ad MUTEX_RELEASE(mtx);
779 1.2 ad turnstile_exit(mtx);
780 1.2 ad } else {
781 1.2 ad MUTEX_RELEASE(mtx);
782 1.2 ad turnstile_wakeup(ts, TS_WRITER_Q,
783 1.2 ad TS_WAITERS(ts, TS_WRITER_Q), NULL);
784 1.2 ad }
785 1.2 ad }
786 1.2 ad
787 1.4 ad #ifndef __HAVE_SIMPLE_MUTEXES
788 1.4 ad /*
789 1.4 ad * mutex_wakeup:
790 1.4 ad *
791 1.4 ad * Support routine for mutex_exit() that wakes up all waiters.
792 1.4 ad * We assume that the mutex has been released, but it need not
793 1.4 ad * be.
794 1.4 ad */
795 1.4 ad void
796 1.4 ad mutex_wakeup(kmutex_t *mtx)
797 1.4 ad {
798 1.4 ad turnstile_t *ts;
799 1.4 ad
800 1.4 ad ts = turnstile_lookup(mtx);
801 1.4 ad if (ts == NULL) {
802 1.4 ad turnstile_exit(mtx);
803 1.4 ad return;
804 1.4 ad }
805 1.4 ad MUTEX_CLEAR_WAITERS(mtx);
806 1.4 ad turnstile_wakeup(ts, TS_WRITER_Q, TS_WAITERS(ts, TS_WRITER_Q), NULL);
807 1.4 ad }
808 1.4 ad #endif /* !__HAVE_SIMPLE_MUTEXES */
809 1.4 ad
810 1.2 ad /*
811 1.2 ad * mutex_owned:
812 1.2 ad *
813 1.3 ad * Return true if the current LWP (adaptive) or CPU (spin)
814 1.3 ad * holds the mutex.
815 1.2 ad */
816 1.2 ad int
817 1.2 ad mutex_owned(kmutex_t *mtx)
818 1.2 ad {
819 1.2 ad
820 1.35 ad if (mtx == NULL)
821 1.35 ad return 0;
822 1.2 ad if (MUTEX_ADAPTIVE_P(mtx))
823 1.2 ad return MUTEX_OWNER(mtx->mtx_owner) == (uintptr_t)curlwp;
824 1.2 ad #ifdef FULL
825 1.16 skrll return __SIMPLELOCK_LOCKED_P(&mtx->mtx_lock);
826 1.2 ad #else
827 1.2 ad return 1;
828 1.2 ad #endif
829 1.2 ad }
830 1.2 ad
831 1.2 ad /*
832 1.2 ad * mutex_owner:
833 1.2 ad *
834 1.6 ad * Return the current owner of an adaptive mutex. Used for
835 1.6 ad * priority inheritance.
836 1.2 ad */
837 1.27 ad lwp_t *
838 1.27 ad mutex_owner(kmutex_t *mtx)
839 1.2 ad {
840 1.2 ad
841 1.2 ad MUTEX_ASSERT(mtx, MUTEX_ADAPTIVE_P(mtx));
842 1.2 ad return (struct lwp *)MUTEX_OWNER(mtx->mtx_owner);
843 1.2 ad }
844 1.2 ad
845 1.2 ad /*
846 1.2 ad * mutex_tryenter:
847 1.2 ad *
848 1.2 ad * Try to acquire the mutex; return non-zero if we did.
849 1.2 ad */
850 1.2 ad int
851 1.2 ad mutex_tryenter(kmutex_t *mtx)
852 1.2 ad {
853 1.2 ad uintptr_t curthread;
854 1.2 ad
855 1.2 ad /*
856 1.2 ad * Handle spin mutexes.
857 1.2 ad */
858 1.2 ad if (MUTEX_SPIN_P(mtx)) {
859 1.2 ad MUTEX_SPIN_SPLRAISE(mtx);
860 1.2 ad #ifdef FULL
861 1.2 ad if (__cpu_simple_lock_try(&mtx->mtx_lock)) {
862 1.4 ad MUTEX_WANTLOCK(mtx);
863 1.2 ad MUTEX_LOCKED(mtx);
864 1.2 ad return 1;
865 1.2 ad }
866 1.2 ad MUTEX_SPIN_SPLRESTORE(mtx);
867 1.2 ad #else
868 1.4 ad MUTEX_WANTLOCK(mtx);
869 1.2 ad MUTEX_LOCKED(mtx);
870 1.2 ad return 1;
871 1.2 ad #endif
872 1.2 ad } else {
873 1.2 ad curthread = (uintptr_t)curlwp;
874 1.2 ad MUTEX_ASSERT(mtx, curthread != 0);
875 1.2 ad if (MUTEX_ACQUIRE(mtx, curthread)) {
876 1.4 ad MUTEX_WANTLOCK(mtx);
877 1.2 ad MUTEX_LOCKED(mtx);
878 1.2 ad MUTEX_DASSERT(mtx,
879 1.2 ad MUTEX_OWNER(mtx->mtx_owner) == curthread);
880 1.2 ad return 1;
881 1.2 ad }
882 1.2 ad }
883 1.2 ad
884 1.2 ad return 0;
885 1.2 ad }
886 1.2 ad
887 1.2 ad #if defined(__HAVE_SPIN_MUTEX_STUBS) || defined(FULL)
888 1.2 ad /*
889 1.2 ad * mutex_spin_retry:
890 1.2 ad *
891 1.2 ad * Support routine for mutex_spin_enter(). Assumes that the caller
892 1.2 ad * has already raised the SPL, and adjusted counters.
893 1.2 ad */
894 1.2 ad void
895 1.2 ad mutex_spin_retry(kmutex_t *mtx)
896 1.2 ad {
897 1.2 ad #ifdef MULTIPROCESSOR
898 1.2 ad u_int count;
899 1.2 ad LOCKSTAT_TIMER(spintime);
900 1.2 ad LOCKSTAT_FLAG(lsflag);
901 1.2 ad #ifdef LOCKDEBUG
902 1.2 ad u_int spins = 0;
903 1.2 ad #endif /* LOCKDEBUG */
904 1.2 ad
905 1.2 ad MUTEX_WANTLOCK(mtx);
906 1.2 ad
907 1.2 ad LOCKSTAT_ENTER(lsflag);
908 1.2 ad LOCKSTAT_START_TIMER(lsflag, spintime);
909 1.2 ad count = SPINLOCK_BACKOFF_MIN;
910 1.2 ad
911 1.2 ad /*
912 1.2 ad * Spin testing the lock word and do exponential backoff
913 1.2 ad * to reduce cache line ping-ponging between CPUs.
914 1.2 ad */
915 1.2 ad do {
916 1.2 ad if (panicstr != NULL)
917 1.2 ad break;
918 1.16 skrll while (__SIMPLELOCK_LOCKED_P(&mtx->mtx_lock)) {
919 1.2 ad SPINLOCK_BACKOFF(count);
920 1.2 ad #ifdef LOCKDEBUG
921 1.2 ad if (SPINLOCK_SPINOUT(spins))
922 1.2 ad MUTEX_ABORT(mtx, "spinout");
923 1.2 ad #endif /* LOCKDEBUG */
924 1.2 ad }
925 1.2 ad } while (!__cpu_simple_lock_try(&mtx->mtx_lock));
926 1.2 ad
927 1.2 ad LOCKSTAT_STOP_TIMER(lsflag, spintime);
928 1.2 ad LOCKSTAT_EVENT(lsflag, mtx, LB_SPIN_MUTEX | LB_SPIN, 1, spintime);
929 1.2 ad LOCKSTAT_EXIT(lsflag);
930 1.2 ad
931 1.2 ad MUTEX_LOCKED(mtx);
932 1.2 ad #else /* MULTIPROCESSOR */
933 1.2 ad MUTEX_ABORT(mtx, "locking against myself");
934 1.2 ad #endif /* MULTIPROCESSOR */
935 1.2 ad }
936 1.2 ad #endif /* defined(__HAVE_SPIN_MUTEX_STUBS) || defined(FULL) */
937 1.31 ad
938 1.31 ad /*
939 1.31 ad * mutex_obj_init:
940 1.31 ad *
941 1.31 ad * Initialize the mutex object store.
942 1.31 ad */
943 1.31 ad void
944 1.31 ad mutex_obj_init(void)
945 1.31 ad {
946 1.31 ad
947 1.31 ad mutex_obj_cache = pool_cache_init(sizeof(struct kmutexobj),
948 1.31 ad coherency_unit, 0, 0, "mutex", NULL, IPL_NONE, mutex_obj_ctor,
949 1.31 ad NULL, NULL);
950 1.31 ad }
951 1.31 ad
952 1.31 ad /*
953 1.31 ad * mutex_obj_ctor:
954 1.31 ad *
955 1.31 ad * Initialize a new lock for the cache.
956 1.31 ad */
957 1.31 ad static int
958 1.31 ad mutex_obj_ctor(void *arg, void *obj, int flags)
959 1.31 ad {
960 1.31 ad struct kmutexobj * mo = obj;
961 1.31 ad
962 1.31 ad mo->mo_magic = MUTEX_OBJ_MAGIC;
963 1.31 ad
964 1.31 ad return 0;
965 1.31 ad }
966 1.31 ad
967 1.31 ad /*
968 1.31 ad * mutex_obj_alloc:
969 1.31 ad *
970 1.31 ad * Allocate a single lock object.
971 1.31 ad */
972 1.31 ad kmutex_t *
973 1.31 ad mutex_obj_alloc(kmutex_type_t type, int ipl)
974 1.31 ad {
975 1.31 ad struct kmutexobj *mo;
976 1.31 ad
977 1.31 ad mo = pool_cache_get(mutex_obj_cache, PR_WAITOK);
978 1.31 ad mutex_init(&mo->mo_lock, type, ipl);
979 1.31 ad mo->mo_refcnt = 1;
980 1.31 ad
981 1.31 ad return (kmutex_t *)mo;
982 1.31 ad }
983 1.31 ad
984 1.31 ad /*
985 1.31 ad * mutex_obj_hold:
986 1.31 ad *
987 1.31 ad * Add a single reference to a lock object. A reference to the object
988 1.31 ad * must already be held, and must be held across this call.
989 1.31 ad */
990 1.31 ad void
991 1.31 ad mutex_obj_hold(kmutex_t *lock)
992 1.31 ad {
993 1.31 ad struct kmutexobj *mo = (struct kmutexobj *)lock;
994 1.31 ad
995 1.31 ad KASSERT(mo->mo_magic == MUTEX_OBJ_MAGIC);
996 1.31 ad KASSERT(mo->mo_refcnt > 0);
997 1.31 ad
998 1.31 ad atomic_inc_uint(&mo->mo_refcnt);
999 1.31 ad }
1000 1.31 ad
1001 1.31 ad /*
1002 1.31 ad * mutex_obj_free:
1003 1.31 ad *
1004 1.31 ad * Drop a reference from a lock object. If the last reference is being
1005 1.31 ad * dropped, free the object and return true. Otherwise, return false.
1006 1.31 ad */
1007 1.31 ad bool
1008 1.31 ad mutex_obj_free(kmutex_t *lock)
1009 1.31 ad {
1010 1.31 ad struct kmutexobj *mo = (struct kmutexobj *)lock;
1011 1.31 ad
1012 1.31 ad KASSERT(mo->mo_magic == MUTEX_OBJ_MAGIC);
1013 1.31 ad KASSERT(mo->mo_refcnt > 0);
1014 1.31 ad
1015 1.31 ad if (atomic_dec_uint_nv(&mo->mo_refcnt) > 0) {
1016 1.31 ad return false;
1017 1.31 ad }
1018 1.31 ad mutex_destroy(&mo->mo_lock);
1019 1.31 ad pool_cache_put(mutex_obj_cache, mo);
1020 1.31 ad return true;
1021 1.31 ad }
1022