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 | * 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 | /* Necessary to obtain some internal functions */ |
---|
41 | #define __RTEMS_VIOLATE_KERNEL_VISIBILITY__ |
---|
42 | |
---|
43 | #include <freebsd/machine/rtems-bsd-config.h> |
---|
44 | |
---|
45 | #include <sys/types.h> |
---|
46 | #include <freebsd/sys/param.h> |
---|
47 | #include <freebsd/sys/types.h> |
---|
48 | #include <freebsd/sys/systm.h> |
---|
49 | #include <freebsd/sys/lock.h> |
---|
50 | #include <freebsd/sys/rwlock.h> |
---|
51 | #include <pthread.h> |
---|
52 | |
---|
53 | #ifndef INVARIANTS |
---|
54 | #define _rw_assert(rw, what, file, line) |
---|
55 | #endif |
---|
56 | |
---|
57 | static void assert_rw(struct lock_object *lock, int what); |
---|
58 | static void lock_rw(struct lock_object *lock, int how); |
---|
59 | #ifdef KDTRACE_HOOKS |
---|
60 | static int owner_rw(struct lock_object *lock, struct thread **owner); |
---|
61 | #endif |
---|
62 | static int unlock_rw(struct lock_object *lock); |
---|
63 | |
---|
64 | typedef uint32_t pthread_rwlock_t; |
---|
65 | |
---|
66 | struct lock_class lock_class_rw = { |
---|
67 | .lc_name = "rw", |
---|
68 | .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE | LC_UPGRADABLE, |
---|
69 | .lc_assert = assert_rw, |
---|
70 | #ifdef DDB |
---|
71 | .lc_ddb_show = db_show_rwlock, |
---|
72 | #endif |
---|
73 | .lc_lock = lock_rw, |
---|
74 | .lc_unlock = unlock_rw, |
---|
75 | #ifdef KDTRACE_HOOKS |
---|
76 | .lc_owner = owner_rw, |
---|
77 | #endif |
---|
78 | }; |
---|
79 | |
---|
80 | RTEMS_CHAIN_DEFINE_EMPTY(rtems_bsd_rwlock_chain); |
---|
81 | |
---|
82 | void |
---|
83 | assert_rw(struct lock_object *lock, int what) |
---|
84 | { |
---|
85 | rw_assert((struct rwlock *)lock, what); |
---|
86 | } |
---|
87 | |
---|
88 | void |
---|
89 | lock_rw(struct lock_object *lock, int how) |
---|
90 | { |
---|
91 | struct rwlock *rw; |
---|
92 | |
---|
93 | rw = (struct rwlock *)lock; |
---|
94 | if (how) |
---|
95 | rw_wlock(rw); |
---|
96 | else |
---|
97 | rw_rlock(rw); |
---|
98 | } |
---|
99 | |
---|
100 | int |
---|
101 | unlock_rw(struct lock_object *lock) |
---|
102 | { |
---|
103 | struct rwlock *rw; |
---|
104 | |
---|
105 | rw = (struct rwlock *)lock; |
---|
106 | rw_assert(rw, RA_LOCKED | LA_NOTRECURSED); |
---|
107 | if (rw->rw_lock & RW_LOCK_READ) { |
---|
108 | rw_runlock(rw); |
---|
109 | return (0); |
---|
110 | } else { |
---|
111 | rw_wunlock(rw); |
---|
112 | return (1); |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | #ifdef KDTRACE_HOOKS |
---|
117 | int |
---|
118 | owner_rw(struct lock_object *lock, struct thread **owner) |
---|
119 | { |
---|
120 | struct rwlock *rw = (struct rwlock *)lock; |
---|
121 | uintptr_t x = rw->rw_lock; |
---|
122 | |
---|
123 | *owner = rw_wowner(rw); |
---|
124 | return ((x & RW_LOCK_READ) != 0 ? (RW_READERS(x) != 0) : |
---|
125 | (*owner != NULL)); |
---|
126 | } |
---|
127 | #endif |
---|
128 | |
---|
129 | void |
---|
130 | rw_init_flags(struct rwlock *rw, const char *name, int opts) |
---|
131 | { |
---|
132 | struct lock_class *class; |
---|
133 | int i; |
---|
134 | pthread_rwlock_t lock; |
---|
135 | int iret; |
---|
136 | |
---|
137 | if ((opts & RW_RECURSE) != 0) { |
---|
138 | /* FIXME */ |
---|
139 | } |
---|
140 | |
---|
141 | class = &lock_class_rw; |
---|
142 | |
---|
143 | /* Check for double-init and zero object. */ |
---|
144 | KASSERT(!lock_initalized(&rw->lock_object), ("lock \"%s\" %p already initialized", name, rw->lock_object)); |
---|
145 | |
---|
146 | /* Look up lock class to find its index. */ |
---|
147 | for (i = 0; i < LOCK_CLASS_MAX; i++) |
---|
148 | { |
---|
149 | if (lock_classes[i] == class) |
---|
150 | { |
---|
151 | rw->lock_object.lo_flags = i << LO_CLASSSHIFT; |
---|
152 | break; |
---|
153 | } |
---|
154 | } |
---|
155 | KASSERT(i < LOCK_CLASS_MAX, ("unknown lock class %p", class)); |
---|
156 | |
---|
157 | iret = pthread_rwlock_init( &lock, NULL ); |
---|
158 | BSD_ASSERT( iret == 0 ); |
---|
159 | |
---|
160 | rw->lock_object.lo_name = name; |
---|
161 | rw->lock_object.lo_flags |= LO_INITIALIZED; |
---|
162 | rw->lock_object.lo_id = lock; |
---|
163 | |
---|
164 | rtems_chain_append(&rtems_bsd_rwlock_chain, &rw->lock_object.lo_node); |
---|
165 | } |
---|
166 | |
---|
167 | void |
---|
168 | rw_destroy(struct rwlock *rw) |
---|
169 | { |
---|
170 | int iret; |
---|
171 | pthread_rwlock_destroy( rw->lock_object.lo_id ); |
---|
172 | BSD_ASSERT( iret == 0 ); |
---|
173 | rtems_chain_extract( &rw->lock_object.lo_node ); |
---|
174 | rw->lock_object.lo_id = 0; |
---|
175 | rw->lock_object.lo_flags &= ~LO_INITIALIZED; |
---|
176 | } |
---|
177 | |
---|
178 | void |
---|
179 | rw_sysinit(void *arg) |
---|
180 | { |
---|
181 | struct rw_args *args = arg; |
---|
182 | |
---|
183 | rw_init(args->ra_rw, args->ra_desc); |
---|
184 | } |
---|
185 | |
---|
186 | void |
---|
187 | rw_sysinit_flags(void *arg) |
---|
188 | { |
---|
189 | struct rw_args_flags *args = arg; |
---|
190 | |
---|
191 | rw_init_flags(args->ra_rw, args->ra_desc, args->ra_flags); |
---|
192 | } |
---|
193 | |
---|
194 | int |
---|
195 | rw_wowned(struct rwlock *rw) |
---|
196 | { |
---|
197 | Objects_Locations location; |
---|
198 | Semaphore_Control *sema = _Semaphore_Get(rw->lock_object.lo_id, &location); |
---|
199 | |
---|
200 | if (location == OBJECTS_LOCAL && !_Attributes_Is_counting_semaphore(sema->attribute_set)) { |
---|
201 | int owned = sema->Core_control.mutex.holder_id == rtems_task_self(); |
---|
202 | |
---|
203 | _Thread_Enable_dispatch(); |
---|
204 | |
---|
205 | return owned; |
---|
206 | } else { |
---|
207 | _Thread_Enable_dispatch(); |
---|
208 | |
---|
209 | BSD_PANIC("unexpected semaphore location or attributes"); |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | void |
---|
214 | _rw_wlock(struct rwlock *rw, const char *file, int line) |
---|
215 | { |
---|
216 | int iret; |
---|
217 | |
---|
218 | pthread_rwlock_wrlock( &rw->lock_object.lo_id ); |
---|
219 | BSD_ASSERT( iret == 0 ); |
---|
220 | |
---|
221 | return 0; |
---|
222 | } |
---|
223 | |
---|
224 | int |
---|
225 | _rw_try_wlock(struct rwlock *rw, const char *file, int line) |
---|
226 | { |
---|
227 | int iret; |
---|
228 | |
---|
229 | iret = pthread_rwlock_trywrlock( &rw->lock_object.lo_id ); |
---|
230 | if (iret == 0) { |
---|
231 | return 1; |
---|
232 | } else { |
---|
233 | return 0; |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | void |
---|
238 | _rw_wunlock(struct rwlock *rw, const char *file, int line) |
---|
239 | { |
---|
240 | int iret; |
---|
241 | |
---|
242 | iret = pthread_rwlock_unlock( &rw->lock_object.lo_id ); |
---|
243 | BSD_ASSERT( iret == 0 ); |
---|
244 | } |
---|
245 | |
---|
246 | void |
---|
247 | _rw_rlock(struct rwlock *rw, const char *file, int line) |
---|
248 | { |
---|
249 | int iret; |
---|
250 | |
---|
251 | iret = pthread_rwlock_rdlock( &rw->lock_object.lo_id ); |
---|
252 | BSD_ASSERT( iret == 0 ); |
---|
253 | } |
---|
254 | |
---|
255 | int |
---|
256 | _rw_try_rlock(struct rwlock *rw, const char *file, int line) |
---|
257 | { |
---|
258 | int iret; |
---|
259 | |
---|
260 | iret = pthread_rwlock_tryrdlock( &rw->lock_object.lo_id ); |
---|
261 | if (iret == 0) { |
---|
262 | return 1; |
---|
263 | } else { |
---|
264 | return 0; |
---|
265 | } |
---|
266 | } |
---|
267 | |
---|
268 | void |
---|
269 | _rw_runlock(struct rwlock *rw, const char *file, int line) |
---|
270 | { |
---|
271 | int iret; |
---|
272 | |
---|
273 | iret = pthread_rwlock_unlock( &rw->lock_object.lo_id ); |
---|
274 | BSD_ASSERT( iret == 0 ); |
---|
275 | } |
---|
276 | |
---|
277 | /* |
---|
278 | * Attempt to do a non-blocking upgrade from a read lock to a write |
---|
279 | * lock. This will only succeed if this thread holds a single read |
---|
280 | * lock. Returns true if the upgrade succeeded and false otherwise. |
---|
281 | */ |
---|
282 | int |
---|
283 | _rw_try_upgrade(struct rwlock *rw, const char *file, int line) |
---|
284 | { |
---|
285 | return 0; /* XXX */ |
---|
286 | } |
---|
287 | |
---|
288 | /* |
---|
289 | * Downgrade a write lock into a single read lock. |
---|
290 | */ |
---|
291 | void |
---|
292 | _rw_downgrade(struct rwlock *rw, const char *file, int line) |
---|
293 | { |
---|
294 | /* XXX */ |
---|
295 | } |
---|
296 | |
---|
297 | #ifdef INVARIANT_SUPPORT |
---|
298 | #ifndef INVARIANTS |
---|
299 | #undef _rw_assert |
---|
300 | #endif |
---|
301 | |
---|
302 | /* |
---|
303 | * In the non-WITNESS case, rw_assert() can only detect that at least |
---|
304 | * *some* thread owns an rlock, but it cannot guarantee that *this* |
---|
305 | * thread owns an rlock. |
---|
306 | */ |
---|
307 | void |
---|
308 | _rw_assert(struct rwlock *rw, int what, const char *file, int line) |
---|
309 | { |
---|
310 | |
---|
311 | if (panicstr != NULL) |
---|
312 | return; |
---|
313 | switch (what) { |
---|
314 | case RA_LOCKED: |
---|
315 | case RA_LOCKED | RA_RECURSED: |
---|
316 | case RA_LOCKED | RA_NOTRECURSED: |
---|
317 | case RA_RLOCKED: |
---|
318 | #ifdef WITNESS |
---|
319 | witness_assert(&rw->lock_object, what, file, line); |
---|
320 | #else |
---|
321 | /* |
---|
322 | * If some other thread has a write lock or we have one |
---|
323 | * and are asserting a read lock, fail. Also, if no one |
---|
324 | * has a lock at all, fail. |
---|
325 | */ |
---|
326 | if (rw->rw_lock == RW_UNLOCKED || |
---|
327 | (!(rw->rw_lock & RW_LOCK_READ) && (what == RA_RLOCKED || |
---|
328 | rw_wowner(rw) != curthread))) |
---|
329 | panic("Lock %s not %slocked @ %s:%d\n", |
---|
330 | rw->lock_object.lo_name, (what == RA_RLOCKED) ? |
---|
331 | "read " : "", file, line); |
---|
332 | |
---|
333 | if (!(rw->rw_lock & RW_LOCK_READ)) { |
---|
334 | if (rw_recursed(rw)) { |
---|
335 | if (what & RA_NOTRECURSED) |
---|
336 | panic("Lock %s recursed @ %s:%d\n", |
---|
337 | rw->lock_object.lo_name, file, |
---|
338 | line); |
---|
339 | } else if (what & RA_RECURSED) |
---|
340 | panic("Lock %s not recursed @ %s:%d\n", |
---|
341 | rw->lock_object.lo_name, file, line); |
---|
342 | } |
---|
343 | #endif |
---|
344 | break; |
---|
345 | case RA_WLOCKED: |
---|
346 | case RA_WLOCKED | RA_RECURSED: |
---|
347 | case RA_WLOCKED | RA_NOTRECURSED: |
---|
348 | if (rw_wowner(rw) != curthread) |
---|
349 | panic("Lock %s not exclusively locked @ %s:%d\n", |
---|
350 | rw->lock_object.lo_name, file, line); |
---|
351 | if (rw_recursed(rw)) { |
---|
352 | if (what & RA_NOTRECURSED) |
---|
353 | panic("Lock %s recursed @ %s:%d\n", |
---|
354 | rw->lock_object.lo_name, file, line); |
---|
355 | } else if (what & RA_RECURSED) |
---|
356 | panic("Lock %s not recursed @ %s:%d\n", |
---|
357 | rw->lock_object.lo_name, file, line); |
---|
358 | break; |
---|
359 | case RA_UNLOCKED: |
---|
360 | #ifdef WITNESS |
---|
361 | witness_assert(&rw->lock_object, what, file, line); |
---|
362 | #else |
---|
363 | /* |
---|
364 | * If we hold a write lock fail. We can't reliably check |
---|
365 | * to see if we hold a read lock or not. |
---|
366 | */ |
---|
367 | if (rw_wowner(rw) == curthread) |
---|
368 | panic("Lock %s exclusively locked @ %s:%d\n", |
---|
369 | rw->lock_object.lo_name, file, line); |
---|
370 | #endif |
---|
371 | break; |
---|
372 | default: |
---|
373 | panic("Unknown rw lock assertion: %d @ %s:%d", what, file, |
---|
374 | line); |
---|
375 | } |
---|
376 | } |
---|
377 | #endif /* INVARIANT_SUPPORT */ |
---|