summaryrefslogtreecommitdiffstats
path: root/rtemsbsd/src/rtems-bsd-rwlock.c
blob: fcd2b5c780984f16e28676eccb8d870db8237df8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/**
 * @file
 *
 * @ingroup rtems_bsd_rtems
 *
 * @brief TODO.
 */

/*
 * Copyright (c) 2011 OPTI Medical.  All rights reserved.
 *
 *  OPTI Medical
 *  235 Hembree Park Drive
 *  Roswell, GA 30076
 *  USA
 *  <kevin.kirspel@optimedical.com>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.com/license/LICENSE.
 */

/* Necessary to obtain some internal functions */
#define __RTEMS_VIOLATE_KERNEL_VISIBILITY__

#include <freebsd/machine/rtems-bsd-config.h>

#include <sys/types.h>
#include <freebsd/sys/param.h>
#include <freebsd/sys/types.h>
#include <freebsd/sys/systm.h>
#include <freebsd/sys/lock.h>
#include <freebsd/sys/rwlock.h>
#include <pthread.h>

#ifndef INVARIANTS
#define _rw_assert(rw, what, file, line)
#endif

static void assert_rw(struct lock_object *lock, int what);
static void lock_rw(struct lock_object *lock, int how);
#ifdef KDTRACE_HOOKS
static int  owner_rw(struct lock_object *lock, struct thread **owner);
#endif
static int  unlock_rw(struct lock_object *lock);

typedef uint32_t pthread_rwlock_t;

struct lock_class lock_class_rw = {
  .lc_name = "rw",
  .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE | LC_UPGRADABLE,
  .lc_assert = assert_rw,
#ifdef DDB
  .lc_ddb_show = db_show_rwlock,
#endif
  .lc_lock = lock_rw,
  .lc_unlock = unlock_rw,
#ifdef KDTRACE_HOOKS
  .lc_owner = owner_rw,
#endif
};

RTEMS_CHAIN_DEFINE_EMPTY(rtems_bsd_rwlock_chain);

void
assert_rw(struct lock_object *lock, int what)
{
  rw_assert((struct rwlock *)lock, what);
}

void
lock_rw(struct lock_object *lock, int how)
{
  struct rwlock *rw;

  rw = (struct rwlock *)lock;
  if (how)
    rw_wlock(rw);
  else
    rw_rlock(rw);
}

int
unlock_rw(struct lock_object *lock)
{
  struct rwlock *rw;

  rw = (struct rwlock *)lock;
  rw_assert(rw, RA_LOCKED | LA_NOTRECURSED);
  if (rw->rw_lock & RW_LOCK_READ) {
    rw_runlock(rw);
    return (0);
  } else {
    rw_wunlock(rw);
    return (1);
  }
}

#ifdef KDTRACE_HOOKS
int
owner_rw(struct lock_object *lock, struct thread **owner)
{
  struct rwlock *rw = (struct rwlock *)lock;
  uintptr_t x = rw->rw_lock;

  *owner = rw_wowner(rw);
  return ((x & RW_LOCK_READ) != 0 ?  (RW_READERS(x) != 0) :
      (*owner != NULL));
}
#endif

void
rw_init_flags(struct rwlock *rw, const char *name, int opts)
{
  struct lock_class *class;
  int i;
  pthread_rwlock_t lock;
  int iret;

  if ((opts & RW_RECURSE) != 0) {
    /* FIXME */
  }

  class = &lock_class_rw;

  /* Check for double-init and zero object. */
  KASSERT(!lock_initalized(&rw->lock_object), ("lock \"%s\" %p already initialized", name, rw->lock_object));

  /* Look up lock class to find its index. */
  for (i = 0; i < LOCK_CLASS_MAX; i++)
  {
    if (lock_classes[i] == class)
    {
      rw->lock_object.lo_flags = i << LO_CLASSSHIFT;
      break;
    }
  }
  KASSERT(i < LOCK_CLASS_MAX, ("unknown lock class %p", class));

  iret = pthread_rwlock_init( &lock, NULL );
  BSD_ASSERT( iret == 0 );

  rw->lock_object.lo_name = name;
  rw->lock_object.lo_flags |= LO_INITIALIZED;
  rw->lock_object.lo_id = lock;

  rtems_chain_append(&rtems_bsd_rwlock_chain, &rw->lock_object.lo_node);
}

void
rw_destroy(struct rwlock *rw)
{
  int iret;
  pthread_rwlock_destroy( rw->lock_object.lo_id );
  BSD_ASSERT( iret == 0 );
  rtems_chain_extract( &rw->lock_object.lo_node );
  rw->lock_object.lo_id = 0;
  rw->lock_object.lo_flags &= ~LO_INITIALIZED;
}

void
rw_sysinit(void *arg)
{
  struct rw_args *args = arg;

  rw_init(args->ra_rw, args->ra_desc);
}

void
rw_sysinit_flags(void *arg)
{
  struct rw_args_flags *args = arg;

  rw_init_flags(args->ra_rw, args->ra_desc, args->ra_flags);
}

int
rw_wowned(struct rwlock *rw)
{
  Objects_Locations location;
  Semaphore_Control *sema = _Semaphore_Get(rw->lock_object.lo_id, &location);

  if (location == OBJECTS_LOCAL && !_Attributes_Is_counting_semaphore(sema->attribute_set)) {
    int owned = sema->Core_control.mutex.holder_id == rtems_task_self();

    _Thread_Enable_dispatch();

    return owned;
  } else {
    _Thread_Enable_dispatch();

    BSD_PANIC("unexpected semaphore location or attributes");
  }
}

void
_rw_wlock(struct rwlock *rw, const char *file, int line)
{
  int iret;

  pthread_rwlock_wrlock( &rw->lock_object.lo_id );
  BSD_ASSERT( iret == 0 );

  return 0;
}

int
_rw_try_wlock(struct rwlock *rw, const char *file, int line)
{
  int iret;

  iret = pthread_rwlock_trywrlock( &rw->lock_object.lo_id );
  if (iret == 0) {
    return 1;
  } else {
    return 0;
  }
}

void
_rw_wunlock(struct rwlock *rw, const char *file, int line)
{
  int iret;

  iret = pthread_rwlock_unlock( &rw->lock_object.lo_id );
  BSD_ASSERT( iret == 0 );
}

void
_rw_rlock(struct rwlock *rw, const char *file, int line)
{
  int iret;

  iret = pthread_rwlock_rdlock( &rw->lock_object.lo_id );
  BSD_ASSERT( iret == 0 );
}

int
_rw_try_rlock(struct rwlock *rw, const char *file, int line)
{
  int iret;

  iret = pthread_rwlock_tryrdlock( &rw->lock_object.lo_id );
  if (iret == 0) {
    return 1;
  } else {
    return 0;
  }
}

void
_rw_runlock(struct rwlock *rw, const char *file, int line)
{
  int iret;

  iret = pthread_rwlock_unlock( &rw->lock_object.lo_id );
  BSD_ASSERT( iret == 0 );
}

/*
 * Attempt to do a non-blocking upgrade from a read lock to a write
 * lock.  This will only succeed if this thread holds a single read
 * lock.  Returns true if the upgrade succeeded and false otherwise.
 */
int
_rw_try_upgrade(struct rwlock *rw, const char *file, int line)
{
  return 0; /* XXX */
}

/*
 * Downgrade a write lock into a single read lock.
 */
void
_rw_downgrade(struct rwlock *rw, const char *file, int line)
{
  /* XXX */ 
}

#ifdef INVARIANT_SUPPORT
#ifndef INVARIANTS
#undef _rw_assert
#endif

/*
 * In the non-WITNESS case, rw_assert() can only detect that at least
 * *some* thread owns an rlock, but it cannot guarantee that *this*
 * thread owns an rlock.
 */
void
_rw_assert(struct rwlock *rw, int what, const char *file, int line)
{

  if (panicstr != NULL)
    return;
  switch (what) {
  case RA_LOCKED:
  case RA_LOCKED | RA_RECURSED:
  case RA_LOCKED | RA_NOTRECURSED:
  case RA_RLOCKED:
#ifdef WITNESS
    witness_assert(&rw->lock_object, what, file, line);
#else
    /*
     * If some other thread has a write lock or we have one
     * and are asserting a read lock, fail.  Also, if no one
     * has a lock at all, fail.
     */
    if (rw->rw_lock == RW_UNLOCKED ||
        (!(rw->rw_lock & RW_LOCK_READ) && (what == RA_RLOCKED ||
        rw_wowner(rw) != curthread)))
      panic("Lock %s not %slocked @ %s:%d\n",
          rw->lock_object.lo_name, (what == RA_RLOCKED) ?
          "read " : "", file, line);

    if (!(rw->rw_lock & RW_LOCK_READ)) {
      if (rw_recursed(rw)) {
        if (what & RA_NOTRECURSED)
          panic("Lock %s recursed @ %s:%d\n",
              rw->lock_object.lo_name, file,
              line);
      } else if (what & RA_RECURSED)
        panic("Lock %s not recursed @ %s:%d\n",
            rw->lock_object.lo_name, file, line);
    }
#endif
    break;
  case RA_WLOCKED:
  case RA_WLOCKED | RA_RECURSED:
  case RA_WLOCKED | RA_NOTRECURSED:
    if (rw_wowner(rw) != curthread)
      panic("Lock %s not exclusively locked @ %s:%d\n",
          rw->lock_object.lo_name, file, line);
    if (rw_recursed(rw)) {
      if (what & RA_NOTRECURSED)
        panic("Lock %s recursed @ %s:%d\n",
            rw->lock_object.lo_name, file, line);
    } else if (what & RA_RECURSED)
      panic("Lock %s not recursed @ %s:%d\n",
          rw->lock_object.lo_name, file, line);
    break;
  case RA_UNLOCKED:
#ifdef WITNESS
    witness_assert(&rw->lock_object, what, file, line);
#else
    /*
     * If we hold a write lock fail.  We can't reliably check
     * to see if we hold a read lock or not.
     */
    if (rw_wowner(rw) == curthread)
      panic("Lock %s exclusively locked @ %s:%d\n",
          rw->lock_object.lo_name, file, line);
#endif
    break;
  default:
    panic("Unknown rw lock assertion: %d @ %s:%d", what, file,
        line);
  }
}
#endif /* INVARIANT_SUPPORT */