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