source: rtems-libbsd/rtemsbsd/rtems/rtems-kernel-sx.c @ 9c1490a

55-freebsd-126-freebsd-12
Last change on this file since 9c1490a was 9c1490a, checked in by Sebastian Huber <sebastian.huber@…>, on 11/14/17 at 12:06:39

LOCKING(9): Update to current FreeBSD version

  • Property mode set to 100644
File size: 5.5 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup rtems_bsd_rtems
5 *
6 * @brief TODO.
7 */
8
9/*
10 * Copyright (c) 2009-2015 embedded brains GmbH.  All rights reserved.
11 *
12 *  embedded brains GmbH
13 *  Dornierstr. 4
14 *  82178 Puchheim
15 *  Germany
16 *  <rtems@embedded-brains.de>
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#include <machine/rtems-bsd-kernel-space.h>
41#include <machine/rtems-bsd-muteximpl.h>
42#include <machine/rtems-bsd-thread.h>
43
44#include <sys/param.h>
45#include <sys/types.h>
46#include <sys/systm.h>
47#include <sys/lock.h>
48#include <sys/sx.h>
49
50static void     assert_sx(const struct lock_object *lock, int what);
51static void     lock_sx(struct lock_object *lock, uintptr_t how);
52#ifdef KDTRACE_HOOKS
53static int      owner_sx(const struct lock_object *lock, struct thread **owner);
54#endif
55static uintptr_t unlock_sx(struct lock_object *lock);
56
57struct lock_class lock_class_sx = {
58        .lc_name = "sx",
59        .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
60        .lc_assert = assert_sx,
61#ifdef DDB
62        .lc_ddb_show = db_show_sx,
63#endif
64        .lc_lock = lock_sx,
65        .lc_unlock = unlock_sx,
66#ifdef KDTRACE_HOOKS
67        .lc_owner = owner_sx,
68#endif
69};
70
71#define sx_xholder(sx) rtems_bsd_mutex_owner(&(sx)->mutex)
72
73#define sx_recursed(sx) rtems_bsd_mutex_recursed(&(sx)->mutex)
74
75void
76assert_sx(const struct lock_object *lock, int what)
77{
78
79        sx_assert((const struct sx *)lock, what);
80}
81
82void
83lock_sx(struct lock_object *lock, uintptr_t how)
84{
85
86        sx_xlock((struct sx *)lock);
87}
88
89uintptr_t
90unlock_sx(struct lock_object *lock)
91{
92
93        sx_xunlock((struct sx *)lock);
94        return (0);
95}
96
97#ifdef KDTRACE_HOOKS
98int
99owner_sx(struct lock_object *lock, struct thread **owner)
100{
101        struct sx *sx = (struct sx *)lock;
102  uintptr_t x = sx->sx_lock;
103
104        *owner = (struct thread *)SX_OWNER(x);
105        return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) :
106      (*owner != NULL));
107}
108#endif
109
110void
111sx_sysinit(void *arg)
112{
113        struct sx_args *sargs = arg;
114
115        sx_init(sargs->sa_sx, sargs->sa_desc);
116}
117
118void
119sx_init_flags(struct sx *sx, const char *description, int opts)
120{
121        int flags;
122
123        flags = LO_SLEEPABLE | LO_UPGRADABLE;
124        if (opts & SX_RECURSE)
125                flags |= LO_RECURSABLE;
126
127        rtems_bsd_mutex_init(&sx->lock_object, &sx->mutex, &lock_class_sx,
128            description, NULL, flags);
129}
130
131void
132sx_destroy(struct sx *sx)
133{
134
135        rtems_bsd_mutex_destroy(&sx->lock_object, &sx->mutex);
136}
137
138int
139_sx_xlock(struct sx *sx, int opts, const char *file, int line)
140{
141        rtems_bsd_mutex_lock(&sx->lock_object, &sx->mutex);
142
143        return (0);
144}
145
146int
147sx_try_xlock_(struct sx *sx, const char *file, int line)
148{
149        return (rtems_bsd_mutex_trylock(&sx->lock_object, &sx->mutex));
150}
151
152void
153_sx_xunlock(struct sx *sx, const char *file, int line)
154{
155        rtems_bsd_mutex_unlock(&sx->mutex);
156}
157
158int
159sx_try_upgrade_(struct sx *sx, const char *file, int line)
160{
161        return (1);
162}
163
164void
165sx_downgrade_(struct sx *sx, const char *file, int line)
166{
167        /* Do nothing */
168}
169
170#ifdef INVARIANT_SUPPORT
171/*
172 * In the non-WITNESS case, sx_assert() can only detect that at least
173 * *some* thread owns an slock, but it cannot guarantee that *this*
174 * thread owns an slock.
175 */
176void
177_sx_assert(const struct sx *sx, int what, const char *file, int line)
178{
179        const char *name = rtems_bsd_mutex_name(&sx->mutex);
180
181        switch (what) {
182        case SA_SLOCKED:
183        case SA_SLOCKED | SA_NOTRECURSED:
184        case SA_SLOCKED | SA_RECURSED:
185        case SA_LOCKED:
186        case SA_LOCKED | SA_NOTRECURSED:
187        case SA_LOCKED | SA_RECURSED:
188        case SA_XLOCKED:
189        case SA_XLOCKED | SA_NOTRECURSED:
190        case SA_XLOCKED | SA_RECURSED:
191                if (sx_xholder(sx) != _Thread_Get_executing())
192                        panic("Lock %s not exclusively locked @ %s:%d\n", name,
193                            file, line);
194                if (sx_recursed(sx)) {
195                        if (what & SA_NOTRECURSED)
196                                panic("Lock %s recursed @ %s:%d\n", name, file,
197                                    line);
198                } else if (what & SA_RECURSED)
199                        panic("Lock %s not recursed @ %s:%d\n", name, file,
200                            line);
201                break;
202        case SA_UNLOCKED:
203#ifdef WITNESS
204                witness_assert(&sx->lock_object, what, file, line);
205#else
206                /*
207                 * If we hold an exclusve lock fail.  We can't
208                 * reliably check to see if we hold a shared lock or
209                 * not.
210                 */
211                if (sx_xholder(sx) == _Thread_Get_executing())
212                        panic("Lock %s exclusively locked @ %s:%d\n", name,
213                            file, line);
214#endif
215                break;
216        default:
217                panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
218                    line);
219        }
220}
221#endif  /* INVARIANT_SUPPORT */
222
223int
224sx_xlocked(struct sx *sx)
225{
226        return (rtems_bsd_mutex_owned(&sx->mutex));
227}
Note: See TracBrowser for help on using the repository browser.