source: rtems-libbsd/rtemsbsd/rtems/rtems-kernel-rwlock.c @ 344b8e6

55-freebsd-126-freebsd-12
Last change on this file since 344b8e6 was 344b8e6, checked in by Sebastian Huber <sebastian.huber@…>, on 11/16/17 at 07:17:03

LOCKING(9): Remove dead code (DDB)

  • Property mode set to 100644
File size: 5.6 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 * 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
61static void     assert_rw(const struct lock_object *lock, int what);
62static void     lock_rw(struct lock_object *lock, uintptr_t how);
63static uintptr_t unlock_rw(struct lock_object *lock);
64
65struct lock_class lock_class_rw = {
66        .lc_name = "rw",
67        .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE | LC_UPGRADABLE,
68        .lc_assert = assert_rw,
69        .lc_lock = lock_rw,
70        .lc_unlock = unlock_rw,
71};
72
73#define rw_wowner(rw) rtems_bsd_mutex_owner(&(rw)->mutex)
74
75#define rw_recursed(rw) rtems_bsd_mutex_recursed(&(rw)->mutex)
76
77void
78assert_rw(const struct lock_object *lock, int what)
79{
80
81        rw_assert((const struct rwlock *)lock, what);
82}
83
84void
85lock_rw(struct lock_object *lock, uintptr_t how)
86{
87
88        rw_wlock((struct rwlock *)lock);
89}
90
91uintptr_t
92unlock_rw(struct lock_object *lock)
93{
94
95        rw_unlock((struct rwlock *)lock);
96        return (0);
97}
98
99void
100rw_init_flags(struct rwlock *rw, const char *name, int opts)
101{
102        int flags;
103
104        flags = LO_UPGRADABLE;
105        if (opts & RW_RECURSE)
106                flags |= LO_RECURSABLE;
107
108        rtems_bsd_mutex_init(&rw->lock_object, &rw->mutex, &lock_class_rw,
109            name, NULL, flags);
110}
111
112void
113rw_destroy(struct rwlock *rw)
114{
115
116        rtems_bsd_mutex_destroy(&rw->lock_object, &rw->mutex);
117}
118
119void
120rw_sysinit(void *arg)
121{
122  struct rw_args *args = arg;
123
124  rw_init(args->ra_rw, args->ra_desc);
125}
126
127void
128rw_sysinit_flags(void *arg)
129{
130  struct rw_args_flags *args = arg;
131
132  rw_init_flags(args->ra_rw, args->ra_desc, args->ra_flags);
133}
134
135int
136rw_wowned(struct rwlock *rw)
137{
138        return (rtems_bsd_mutex_owned(&rw->mutex));
139}
140
141void
142_rw_wlock(struct rwlock *rw, const char *file, int line)
143{
144        rtems_bsd_mutex_lock(&rw->lock_object, &rw->mutex);
145}
146
147int
148_rw_try_wlock(struct rwlock *rw, const char *file, int line)
149{
150        return (rtems_bsd_mutex_trylock(&rw->lock_object, &rw->mutex));
151}
152
153void
154_rw_wunlock(struct rwlock *rw, const char *file, int line)
155{
156        rtems_bsd_mutex_unlock(&rw->mutex);
157}
158
159void
160_rw_rlock(struct rwlock *rw, const char *file, int line)
161{
162        rtems_bsd_mutex_lock(&rw->lock_object, &rw->mutex);
163}
164
165int
166_rw_try_rlock(struct rwlock *rw, const char *file, int line)
167{
168        return (rtems_bsd_mutex_trylock(&rw->lock_object, &rw->mutex));
169}
170
171void
172_rw_runlock(struct rwlock *rw, const char *file, int line)
173{
174        rtems_bsd_mutex_unlock(&rw->mutex);
175}
176
177int
178_rw_try_upgrade(struct rwlock *rw, const char *file, int line)
179{
180        return (1);
181}
182
183void
184_rw_downgrade(struct rwlock *rw, const char *file, int line)
185{
186        /* Nothing to do */
187}
188
189#ifdef INVARIANT_SUPPORT
190/*
191 * In the non-WITNESS case, rw_assert() can only detect that at least
192 * *some* thread owns an rlock, but it cannot guarantee that *this*
193 * thread owns an rlock.
194 */
195void
196_rw_assert(const struct rwlock *rw, int what, const char *file, int line)
197{
198        const char *name = rtems_bsd_mutex_name(&rw->mutex);
199
200        switch (what) {
201        case RA_LOCKED:
202        case RA_LOCKED | RA_RECURSED:
203        case RA_LOCKED | RA_NOTRECURSED:
204        case RA_RLOCKED:
205        case RA_RLOCKED | RA_RECURSED:
206        case RA_RLOCKED | RA_NOTRECURSED:
207        case RA_WLOCKED:
208        case RA_WLOCKED | RA_RECURSED:
209        case RA_WLOCKED | RA_NOTRECURSED:
210                if (rw_wowner(rw) != _Thread_Get_executing())
211                        panic("Lock %s not exclusively locked @ %s:%d\n",
212                            name, file, line);
213                if (rw_recursed(rw)) {
214                        if (what & RA_NOTRECURSED)
215                                panic("Lock %s recursed @ %s:%d\n", name, file,
216                                    line);
217                } else if (what & RA_RECURSED)
218                        panic("Lock %s not recursed @ %s:%d\n", name, file,
219                            line);
220                break;
221        case RA_UNLOCKED:
222#ifdef WITNESS
223                witness_assert(&rw->lock_object, what, file, line);
224#else
225                /*
226                 * If we hold a write lock fail.  We can't reliably check
227                 * to see if we hold a read lock or not.
228                 */
229                if (rw_wowner(rw) == _Thread_Get_executing())
230                        panic("Lock %s exclusively locked @ %s:%d\n", name,
231                            file, line);
232#endif
233                break;
234        default:
235                panic("Unknown rw lock assertion: %d @ %s:%d", what, file,
236                    line);
237        }
238}
239#endif /* INVARIANT_SUPPORT */
Note: See TracBrowser for help on using the repository browser.