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

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since be8032d was a7bbe3f, checked in by Jennifer Averett <jennifer.averett@…>, on 04/16/12 at 16:58:01

Added _rw_try_upgrade & _rw_try_upgrade to resolve linker errors.

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