source: rtems-libbsd/rtemsbsd/rtems/rtems-kernel-rwlock.c

6-freebsd-12
Last change on this file was 46a15fa, checked in by Chris Johns <chrisj@…>, on 07/20/21 at 06:24:54

sys/kern: Add lockmgr support

  • See man lockmgr
  • Implement the lock_object and move the RTEMS mutex to that object
  • Add debug support to track the locks with gdb

Update #4475

  • Property mode set to 100644
File size: 5.4 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)->lock_object)
74
75#define rw_recursed(rw) rtems_bsd_mutex_recursed(&(rw)->lock_object)
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 | LO_RECURSABLE;
105
106        rtems_bsd_mutex_init(&rw->lock_object, &lock_class_rw,
107            name, NULL, flags);
108}
109
110void
111rw_destroy(struct rwlock *rw)
112{
113
114        rtems_bsd_mutex_destroy(&rw->lock_object);
115}
116
117void
118rw_sysinit(void *arg)
119{
120        struct rw_args *args;
121
122        args = arg;
123        rw_init(args->ra_rw, args->ra_desc);
124}
125
126int
127rw_wowned(struct rwlock *rw)
128{
129        return (rtems_bsd_mutex_owned(&rw->lock_object));
130}
131
132void
133_rw_wlock(struct rwlock *rw, const char *file, int line)
134{
135        rtems_bsd_mutex_lock(&rw->lock_object);
136}
137
138int
139_rw_try_wlock(struct rwlock *rw, const char *file, int line)
140{
141        return (rtems_bsd_mutex_trylock(&rw->lock_object));
142}
143
144void
145_rw_wunlock(struct rwlock *rw, const char *file, int line)
146{
147        rtems_bsd_mutex_unlock(&rw->lock_object);
148}
149
150void
151_rw_rlock(struct rwlock *rw, const char *file, int line)
152{
153        rtems_bsd_mutex_lock(&rw->lock_object);
154}
155
156int
157_rw_try_rlock(struct rwlock *rw, const char *file, int line)
158{
159        return (rtems_bsd_mutex_trylock(&rw->lock_object));
160}
161
162void
163_rw_runlock(struct rwlock *rw, const char *file, int line)
164{
165        rtems_bsd_mutex_unlock(&rw->lock_object);
166}
167
168int
169_rw_try_upgrade(struct rwlock *rw, const char *file, int line)
170{
171        return (1);
172}
173
174void
175_rw_downgrade(struct rwlock *rw, const char *file, int line)
176{
177        /* Nothing to do */
178}
179
180#ifdef INVARIANT_SUPPORT
181/*
182 * In the non-WITNESS case, rw_assert() can only detect that at least
183 * *some* thread owns an rlock, but it cannot guarantee that *this*
184 * thread owns an rlock.
185 */
186void
187_rw_assert(const struct rwlock *rw, int what, const char *file, int line)
188{
189        const char *name = rtems_bsd_mutex_name(&rw->lock_object);
190
191        switch (what) {
192        case RA_LOCKED:
193        case RA_LOCKED | RA_RECURSED:
194        case RA_LOCKED | RA_NOTRECURSED:
195        case RA_RLOCKED:
196        case RA_RLOCKED | RA_RECURSED:
197        case RA_RLOCKED | RA_NOTRECURSED:
198        case RA_WLOCKED:
199        case RA_WLOCKED | RA_RECURSED:
200        case RA_WLOCKED | RA_NOTRECURSED:
201                if (rw_wowner(rw) != _Thread_Get_executing())
202                        panic("Lock %s not exclusively locked @ %s:%d\n",
203                            name, file, line);
204                if (rw_recursed(rw)) {
205                        if (what & RA_NOTRECURSED)
206                                panic("Lock %s recursed @ %s:%d\n", name, file,
207                                    line);
208                } else if (what & RA_RECURSED)
209                        panic("Lock %s not recursed @ %s:%d\n", name, file,
210                            line);
211                break;
212        case RA_UNLOCKED:
213#ifdef WITNESS
214                witness_assert(&rw->lock_object, what, file, line);
215#else
216                /*
217                 * If we hold a write lock fail.  We can't reliably check
218                 * to see if we hold a read lock or not.
219                 */
220                if (rw_wowner(rw) == _Thread_Get_executing())
221                        panic("Lock %s exclusively locked @ %s:%d\n", name,
222                            file, line);
223#endif
224                break;
225        default:
226                panic("Unknown rw lock assertion: %d @ %s:%d", what, file,
227                    line);
228        }
229}
230#endif /* INVARIANT_SUPPORT */
Note: See TracBrowser for help on using the repository browser.