4 * Copyright (c) 2002, 2006, 2007, 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe and Andrew Doran.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD$");
35 #include <sys/param.h>
36 #include <sys/atomic.h>
37 #include <sys/mutex.h>
41 #define MUTEX_OBJ_MAGIC 0x5aa3c85d
48 static int mutex_obj_ctor(void *, void *, int);
50 static pool_cache_t mutex_obj_cache
;
55 * Initialize the mutex object store.
61 mutex_obj_cache
= pool_cache_init(sizeof(struct kmutexobj
),
62 coherency_unit
, 0, 0, "mutex", NULL
, IPL_NONE
, mutex_obj_ctor
,
69 * Initialize a new lock for the cache.
72 mutex_obj_ctor(void *arg
, void *obj
, int flags
)
74 struct kmutexobj
* mo
= obj
;
76 mo
->mo_magic
= MUTEX_OBJ_MAGIC
;
84 * Allocate a single lock object.
87 mutex_obj_alloc(kmutex_type_t type
, int ipl
)
91 mo
= pool_cache_get(mutex_obj_cache
, PR_WAITOK
);
92 mutex_init(&mo
->mo_lock
, type
, ipl
);
95 return (kmutex_t
*)mo
;
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.
105 mutex_obj_hold(kmutex_t
*lock
)
107 struct kmutexobj
*mo
= (struct kmutexobj
*)lock
;
109 KASSERT(mo
->mo_magic
== MUTEX_OBJ_MAGIC
);
110 KASSERT(mo
->mo_refcnt
> 0);
112 atomic_inc_uint(&mo
->mo_refcnt
);
118 * Drop a reference from a lock object. If the last reference is being
119 * dropped, free the object and return true. Otherwise, return false.
122 mutex_obj_free(kmutex_t
*lock
)
124 struct kmutexobj
*mo
= (struct kmutexobj
*)lock
;
126 KASSERT(mo
->mo_magic
== MUTEX_OBJ_MAGIC
);
127 KASSERT(mo
->mo_refcnt
> 0);
129 if (atomic_dec_uint_nv(&mo
->mo_refcnt
) > 0) {
132 mutex_destroy(&mo
->mo_lock
);
133 pool_cache_put(mutex_obj_cache
, mo
);