1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup rtems_bsd_rtems |
---|
5 | * |
---|
6 | * @brief TODO. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2011 OPTI Medical. All rights reserved. |
---|
11 | * |
---|
12 | * OPTI Medical |
---|
13 | * 235 Hembree Park Drive |
---|
14 | * Roswell, GA 30076 |
---|
15 | * USA |
---|
16 | * <kevin.kirspel@optimedical.com> |
---|
17 | * |
---|
18 | * Copyright (c) 2013-2015 embedded brains GmbH. All rights reserved. |
---|
19 | * |
---|
20 | * embedded brains GmbH |
---|
21 | * Dornierstr. 4 |
---|
22 | * 82178 Puchheim |
---|
23 | * Germany |
---|
24 | * <rtems@embedded-brains.de> |
---|
25 | * |
---|
26 | * Redistribution and use in source and binary forms, with or without |
---|
27 | * modification, are permitted provided that the following conditions |
---|
28 | * are met: |
---|
29 | * 1. Redistributions of source code must retain the above copyright |
---|
30 | * notice, this list of conditions and the following disclaimer. |
---|
31 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
32 | * notice, this list of conditions and the following disclaimer in the |
---|
33 | * documentation and/or other materials provided with the distribution. |
---|
34 | * |
---|
35 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
36 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
38 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
39 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
40 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
41 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
42 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
43 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
44 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
45 | * SUCH DAMAGE. |
---|
46 | */ |
---|
47 | |
---|
48 | #include <machine/rtems-bsd-kernel-space.h> |
---|
49 | #include <machine/rtems-bsd-muteximpl.h> |
---|
50 | |
---|
51 | #include <sys/param.h> |
---|
52 | #include <sys/types.h> |
---|
53 | #include <sys/systm.h> |
---|
54 | #include <sys/lock.h> |
---|
55 | #include <sys/rwlock.h> |
---|
56 | |
---|
57 | #ifndef INVARIANTS |
---|
58 | #define _rw_assert(rw, what, file, line) |
---|
59 | #endif |
---|
60 | |
---|
61 | static void assert_rw(const struct lock_object *lock, int what); |
---|
62 | static void lock_rw(struct lock_object *lock, uintptr_t how); |
---|
63 | #ifdef KDTRACE_HOOKS |
---|
64 | static int owner_rw(const struct lock_object *lock, struct thread **owner); |
---|
65 | #endif |
---|
66 | static uintptr_t unlock_rw(struct lock_object *lock); |
---|
67 | |
---|
68 | struct lock_class lock_class_rw = { |
---|
69 | .lc_name = "rw", |
---|
70 | .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE | LC_UPGRADABLE, |
---|
71 | .lc_assert = assert_rw, |
---|
72 | #ifdef DDB |
---|
73 | .lc_ddb_show = db_show_rwlock, |
---|
74 | #endif |
---|
75 | .lc_lock = lock_rw, |
---|
76 | .lc_unlock = unlock_rw, |
---|
77 | #ifdef KDTRACE_HOOKS |
---|
78 | .lc_owner = owner_rw, |
---|
79 | #endif |
---|
80 | }; |
---|
81 | |
---|
82 | #define rw_wowner(rw) rtems_bsd_mutex_owner(&(rw)->mutex) |
---|
83 | |
---|
84 | #define rw_recursed(rw) rtems_bsd_mutex_recursed(&(rw)->mutex) |
---|
85 | |
---|
86 | void |
---|
87 | assert_rw(const struct lock_object *lock, int what) |
---|
88 | { |
---|
89 | |
---|
90 | rw_assert((const struct rwlock *)lock, what); |
---|
91 | } |
---|
92 | |
---|
93 | void |
---|
94 | lock_rw(struct lock_object *lock, uintptr_t how) |
---|
95 | { |
---|
96 | |
---|
97 | rw_wlock((struct rwlock *)lock); |
---|
98 | } |
---|
99 | |
---|
100 | uintptr_t |
---|
101 | unlock_rw(struct lock_object *lock) |
---|
102 | { |
---|
103 | |
---|
104 | rw_unlock((struct rwlock *)lock); |
---|
105 | return (0); |
---|
106 | } |
---|
107 | |
---|
108 | #ifdef KDTRACE_HOOKS |
---|
109 | int |
---|
110 | owner_rw(struct lock_object *lock, struct thread **owner) |
---|
111 | { |
---|
112 | struct rwlock *rw = (struct rwlock *)lock; |
---|
113 | uintptr_t x = rw->rw_lock; |
---|
114 | |
---|
115 | *owner = rw_wowner(rw); |
---|
116 | return ((x & RW_LOCK_READ) != 0 ? (RW_READERS(x) != 0) : |
---|
117 | (*owner != NULL)); |
---|
118 | } |
---|
119 | #endif |
---|
120 | |
---|
121 | void |
---|
122 | rw_init_flags(struct rwlock *rw, const char *name, int opts) |
---|
123 | { |
---|
124 | int flags; |
---|
125 | |
---|
126 | flags = LO_UPGRADABLE; |
---|
127 | if (opts & RW_RECURSE) |
---|
128 | flags |= LO_RECURSABLE; |
---|
129 | |
---|
130 | rtems_bsd_mutex_init(&rw->lock_object, &rw->mutex, &lock_class_rw, |
---|
131 | name, NULL, flags); |
---|
132 | } |
---|
133 | |
---|
134 | void |
---|
135 | rw_destroy(struct rwlock *rw) |
---|
136 | { |
---|
137 | |
---|
138 | rtems_bsd_mutex_destroy(&rw->lock_object, &rw->mutex); |
---|
139 | } |
---|
140 | |
---|
141 | void |
---|
142 | rw_sysinit(void *arg) |
---|
143 | { |
---|
144 | struct rw_args *args = arg; |
---|
145 | |
---|
146 | rw_init(args->ra_rw, args->ra_desc); |
---|
147 | } |
---|
148 | |
---|
149 | void |
---|
150 | rw_sysinit_flags(void *arg) |
---|
151 | { |
---|
152 | struct rw_args_flags *args = arg; |
---|
153 | |
---|
154 | rw_init_flags(args->ra_rw, args->ra_desc, args->ra_flags); |
---|
155 | } |
---|
156 | |
---|
157 | int |
---|
158 | rw_wowned(struct rwlock *rw) |
---|
159 | { |
---|
160 | return (rtems_bsd_mutex_owned(&rw->mutex)); |
---|
161 | } |
---|
162 | |
---|
163 | void |
---|
164 | _rw_wlock(struct rwlock *rw, const char *file, int line) |
---|
165 | { |
---|
166 | rtems_bsd_mutex_lock(&rw->lock_object, &rw->mutex); |
---|
167 | } |
---|
168 | |
---|
169 | int |
---|
170 | _rw_try_wlock(struct rwlock *rw, const char *file, int line) |
---|
171 | { |
---|
172 | return (rtems_bsd_mutex_trylock(&rw->lock_object, &rw->mutex)); |
---|
173 | } |
---|
174 | |
---|
175 | void |
---|
176 | _rw_wunlock(struct rwlock *rw, const char *file, int line) |
---|
177 | { |
---|
178 | rtems_bsd_mutex_unlock(&rw->mutex); |
---|
179 | } |
---|
180 | |
---|
181 | void |
---|
182 | _rw_rlock(struct rwlock *rw, const char *file, int line) |
---|
183 | { |
---|
184 | rtems_bsd_mutex_lock(&rw->lock_object, &rw->mutex); |
---|
185 | } |
---|
186 | |
---|
187 | int |
---|
188 | _rw_try_rlock(struct rwlock *rw, const char *file, int line) |
---|
189 | { |
---|
190 | return (rtems_bsd_mutex_trylock(&rw->lock_object, &rw->mutex)); |
---|
191 | } |
---|
192 | |
---|
193 | void |
---|
194 | _rw_runlock(struct rwlock *rw, const char *file, int line) |
---|
195 | { |
---|
196 | rtems_bsd_mutex_unlock(&rw->mutex); |
---|
197 | } |
---|
198 | |
---|
199 | int |
---|
200 | _rw_try_upgrade(struct rwlock *rw, const char *file, int line) |
---|
201 | { |
---|
202 | return (1); |
---|
203 | } |
---|
204 | |
---|
205 | void |
---|
206 | _rw_downgrade(struct rwlock *rw, const char *file, int line) |
---|
207 | { |
---|
208 | /* Nothing to do */ |
---|
209 | } |
---|
210 | |
---|
211 | #ifdef INVARIANT_SUPPORT |
---|
212 | /* |
---|
213 | * In the non-WITNESS case, rw_assert() can only detect that at least |
---|
214 | * *some* thread owns an rlock, but it cannot guarantee that *this* |
---|
215 | * thread owns an rlock. |
---|
216 | */ |
---|
217 | void |
---|
218 | _rw_assert(const struct rwlock *rw, int what, const char *file, int line) |
---|
219 | { |
---|
220 | const char *name = rtems_bsd_mutex_name(&rw->mutex); |
---|
221 | |
---|
222 | switch (what) { |
---|
223 | case RA_LOCKED: |
---|
224 | case RA_LOCKED | RA_RECURSED: |
---|
225 | case RA_LOCKED | RA_NOTRECURSED: |
---|
226 | case RA_RLOCKED: |
---|
227 | case RA_RLOCKED | RA_RECURSED: |
---|
228 | case RA_RLOCKED | RA_NOTRECURSED: |
---|
229 | case RA_WLOCKED: |
---|
230 | case RA_WLOCKED | RA_RECURSED: |
---|
231 | case RA_WLOCKED | RA_NOTRECURSED: |
---|
232 | if (rw_wowner(rw) != _Thread_Get_executing()) |
---|
233 | panic("Lock %s not exclusively locked @ %s:%d\n", |
---|
234 | name, file, line); |
---|
235 | if (rw_recursed(rw)) { |
---|
236 | if (what & RA_NOTRECURSED) |
---|
237 | panic("Lock %s recursed @ %s:%d\n", name, file, |
---|
238 | line); |
---|
239 | } else if (what & RA_RECURSED) |
---|
240 | panic("Lock %s not recursed @ %s:%d\n", name, file, |
---|
241 | line); |
---|
242 | break; |
---|
243 | case RA_UNLOCKED: |
---|
244 | #ifdef WITNESS |
---|
245 | witness_assert(&rw->lock_object, what, file, line); |
---|
246 | #else |
---|
247 | /* |
---|
248 | * If we hold a write lock fail. We can't reliably check |
---|
249 | * to see if we hold a read lock or not. |
---|
250 | */ |
---|
251 | if (rw_wowner(rw) == _Thread_Get_executing()) |
---|
252 | panic("Lock %s exclusively locked @ %s:%d\n", name, |
---|
253 | file, line); |
---|
254 | #endif |
---|
255 | break; |
---|
256 | default: |
---|
257 | panic("Unknown rw lock assertion: %d @ %s:%d", what, file, |
---|
258 | line); |
---|
259 | } |
---|
260 | } |
---|
261 | #endif /* INVARIANT_SUPPORT */ |
---|