1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup rtems_bsd_rtems |
---|
5 | * |
---|
6 | * @brief TODO. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2009-2014 embedded brains GmbH. All rights reserved. |
---|
11 | * |
---|
12 | * embedded brains GmbH |
---|
13 | * Dornierstr. 4 |
---|
14 | * 82178 Puchheim |
---|
15 | * Germany |
---|
16 | * <rtems@embedded-brains.de> |
---|
17 | * |
---|
18 | * Redistribution and use in source and binary forms, with or without |
---|
19 | * modification, are permitted provided that the following conditions |
---|
20 | * are met: |
---|
21 | * 1. Redistributions of source code must retain the above copyright |
---|
22 | * notice, this list of conditions and the following disclaimer. |
---|
23 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
24 | * notice, this list of conditions and the following disclaimer in the |
---|
25 | * documentation and/or other materials provided with the distribution. |
---|
26 | * |
---|
27 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
28 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
30 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
31 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
33 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
34 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
35 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
36 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
37 | * SUCH DAMAGE. |
---|
38 | */ |
---|
39 | |
---|
40 | #include <machine/rtems-bsd-kernel-space.h> |
---|
41 | #include <machine/rtems-bsd-muteximpl.h> |
---|
42 | |
---|
43 | #include <sys/param.h> |
---|
44 | #include <sys/types.h> |
---|
45 | #include <sys/systm.h> |
---|
46 | #include <sys/lock.h> |
---|
47 | #include <sys/mutex.h> |
---|
48 | #include <sys/proc.h> |
---|
49 | #include <sys/conf.h> |
---|
50 | |
---|
51 | static void assert_mtx(struct lock_object *lock, int what); |
---|
52 | static void lock_mtx(struct lock_object *lock, int how); |
---|
53 | #ifdef KDTRACE_HOOKS |
---|
54 | static int owner_mtx(struct lock_object *lock, struct thread **owner); |
---|
55 | #endif |
---|
56 | static int unlock_mtx(struct lock_object *lock); |
---|
57 | |
---|
58 | /* |
---|
59 | * Lock classes for sleep and spin mutexes. |
---|
60 | */ |
---|
61 | struct lock_class lock_class_mtx_sleep = { |
---|
62 | .lc_name = "sleep mutex", |
---|
63 | .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE, |
---|
64 | .lc_assert = assert_mtx, |
---|
65 | #ifdef DDB |
---|
66 | .lc_ddb_show = db_show_mtx, |
---|
67 | #endif |
---|
68 | .lc_lock = lock_mtx, |
---|
69 | .lc_unlock = unlock_mtx, |
---|
70 | #ifdef KDTRACE_HOOKS |
---|
71 | .lc_owner = owner_mtx, |
---|
72 | #endif |
---|
73 | }; |
---|
74 | |
---|
75 | struct lock_class lock_class_mtx_spin = { |
---|
76 | .lc_name = "spin mutex", |
---|
77 | .lc_flags = LC_SPINLOCK | LC_RECURSABLE, |
---|
78 | .lc_assert = assert_mtx, |
---|
79 | #ifdef DDB |
---|
80 | .lc_ddb_show = db_show_mtx, |
---|
81 | #endif |
---|
82 | .lc_lock = lock_mtx, |
---|
83 | .lc_unlock = unlock_mtx, |
---|
84 | #ifdef KDTRACE_HOOKS |
---|
85 | .lc_owner = owner_mtx, |
---|
86 | #endif |
---|
87 | }; |
---|
88 | |
---|
89 | struct mtx Giant; |
---|
90 | |
---|
91 | void |
---|
92 | assert_mtx(struct lock_object *lock, int what) |
---|
93 | { |
---|
94 | mtx_assert((struct mtx *)lock, what); |
---|
95 | } |
---|
96 | |
---|
97 | void |
---|
98 | lock_mtx(struct lock_object *lock, int how) |
---|
99 | { |
---|
100 | mtx_lock((struct mtx *)lock); |
---|
101 | } |
---|
102 | |
---|
103 | int |
---|
104 | unlock_mtx(struct lock_object *lock) |
---|
105 | { |
---|
106 | mtx_unlock((struct mtx *)lock); |
---|
107 | |
---|
108 | return (0); |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | #ifdef KDTRACE_HOOKS |
---|
113 | int |
---|
114 | owner_mtx(struct lock_object *lock, struct thread **owner) |
---|
115 | { |
---|
116 | struct mtx *m = (struct mtx *)lock; |
---|
117 | |
---|
118 | *owner = mtx_owner(m); |
---|
119 | return (mtx_unowned(m) == 0); |
---|
120 | } |
---|
121 | #endif |
---|
122 | |
---|
123 | void |
---|
124 | mtx_init(struct mtx *m, const char *name, const char *type, int opts) |
---|
125 | { |
---|
126 | struct lock_class *class; |
---|
127 | int flags; |
---|
128 | |
---|
129 | /* Determine lock class and lock flags. */ |
---|
130 | if (opts & MTX_SPIN) |
---|
131 | class = &lock_class_mtx_spin; |
---|
132 | else |
---|
133 | class = &lock_class_mtx_sleep; |
---|
134 | flags = 0; |
---|
135 | if (opts & MTX_RECURSE) |
---|
136 | flags |= LO_RECURSABLE; |
---|
137 | |
---|
138 | rtems_bsd_mutex_init(&m->lock_object, &m->mutex, class, name, type, |
---|
139 | flags); |
---|
140 | } |
---|
141 | |
---|
142 | void |
---|
143 | _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line) |
---|
144 | { |
---|
145 | rtems_bsd_mutex_lock(&m->lock_object, &m->mutex); |
---|
146 | } |
---|
147 | |
---|
148 | int |
---|
149 | mtx_trylock_flags_(struct mtx *m, int opts, const char *file, int line) |
---|
150 | { |
---|
151 | return (rtems_bsd_mutex_trylock(&m->lock_object, &m->mutex)); |
---|
152 | } |
---|
153 | |
---|
154 | void |
---|
155 | _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line) |
---|
156 | { |
---|
157 | rtems_bsd_mutex_unlock(&m->mutex); |
---|
158 | } |
---|
159 | |
---|
160 | /* |
---|
161 | * The backing function for the INVARIANTS-enabled mtx_assert() |
---|
162 | */ |
---|
163 | #ifdef INVARIANT_SUPPORT |
---|
164 | void |
---|
165 | _mtx_assert(struct mtx *m, int what, const char *file, int line) |
---|
166 | { |
---|
167 | |
---|
168 | if (panicstr != NULL || dumping) |
---|
169 | return; |
---|
170 | switch (what) { |
---|
171 | case MA_OWNED: |
---|
172 | case MA_OWNED | MA_RECURSED: |
---|
173 | case MA_OWNED | MA_NOTRECURSED: |
---|
174 | if (!mtx_owned(m)) |
---|
175 | panic("mutex %s not owned at %s:%d", |
---|
176 | m->lock_object.lo_name, file, line); |
---|
177 | if (mtx_recursed(m)) { |
---|
178 | if ((what & MA_NOTRECURSED) != 0) |
---|
179 | panic("mutex %s recursed at %s:%d", |
---|
180 | m->lock_object.lo_name, file, line); |
---|
181 | } else if ((what & MA_RECURSED) != 0) { |
---|
182 | panic("mutex %s unrecursed at %s:%d", |
---|
183 | m->lock_object.lo_name, file, line); |
---|
184 | } |
---|
185 | break; |
---|
186 | case MA_NOTOWNED: |
---|
187 | if (mtx_owned(m)) |
---|
188 | panic("mutex %s owned at %s:%d", |
---|
189 | m->lock_object.lo_name, file, line); |
---|
190 | break; |
---|
191 | default: |
---|
192 | panic("unknown mtx_assert at %s:%d", file, line); |
---|
193 | } |
---|
194 | } |
---|
195 | #endif |
---|
196 | |
---|
197 | int mtx_owned(struct mtx *m) |
---|
198 | { |
---|
199 | return (rtems_bsd_mutex_owned(&m->mutex)); |
---|
200 | } |
---|
201 | |
---|
202 | int mtx_recursed(struct mtx *m) |
---|
203 | { |
---|
204 | return (rtems_bsd_mutex_recursed(&m->mutex)); |
---|
205 | } |
---|
206 | |
---|
207 | void |
---|
208 | mtx_sysinit(void *arg) |
---|
209 | { |
---|
210 | struct mtx_args *margs = arg; |
---|
211 | |
---|
212 | mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts); |
---|
213 | } |
---|
214 | |
---|
215 | void |
---|
216 | mtx_destroy(struct mtx *m) |
---|
217 | { |
---|
218 | |
---|
219 | rtems_bsd_mutex_destroy(&m->lock_object, &m->mutex); |
---|
220 | } |
---|
221 | |
---|
222 | void |
---|
223 | mutex_init(void) |
---|
224 | { |
---|
225 | mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE); |
---|
226 | mtx_lock(&Giant); |
---|
227 | } |
---|