source: rtems-libbsd/rtemsbsd/src/rtems-bsd-rwlock.c @ 3bc5984

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 3bc5984 was 3bc5984, checked in by Jennifer Averett <jennifer.averett@…>, on 05/30/12 at 18:42:49

Debug of rw_wowned()
This implementation violates the API layer and we should
add a pthread_rwlock_is_rlocked_np() method to the API layer.

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