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

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since f0aaa04 was 3d1e767, checked in by Sebastian Huber <sebastian.huber@…>, on 04/27/16 at 08:25:22

Directly use <sys/types.h> provided by Newlib

  • Property mode set to 100644
File size: 6.8 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 <rtems/bsd/sys/param.h>
45#include <sys/types.h>
46#include <sys/systm.h>
47#include <rtems/bsd/sys/lock.h>
48#include <sys/sx.h>
49
50#ifndef INVARIANTS
51#define _sx_assert(sx, what, file, line)
52#endif
53
54static void assert_sx(struct lock_object *lock, int what);
55static void lock_sx(struct lock_object *lock, int how);
56#ifdef KDTRACE_HOOKS
57static int  owner_sx(struct lock_object *lock, struct thread **owner);
58#endif
59static int  unlock_sx(struct lock_object *lock);
60
61struct lock_class lock_class_sx = {
62  .lc_name = "sx",
63  .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
64  .lc_assert = assert_sx,
65#ifdef DDB
66  .lc_ddb_show = db_show_sx,
67#endif
68  .lc_lock = lock_sx,
69  .lc_unlock = unlock_sx,
70#ifdef KDTRACE_HOOKS
71  .lc_owner = owner_sx,
72#endif
73};
74
75#define sx_xholder(sx) ((sx)->mutex.owner)
76
77#define sx_recursed(sx) ((sx)->mutex.nest_level != 0)
78
79void
80assert_sx(struct lock_object *lock, int what)
81{
82  sx_assert((struct sx *)lock, what);
83}
84
85void
86lock_sx(struct lock_object *lock, int how)
87{
88        sx_xlock((struct sx *)lock);
89}
90
91int
92unlock_sx(struct lock_object *lock)
93{
94        sx_xunlock((struct sx *)lock);
95
96        return (0);
97}
98
99#ifdef KDTRACE_HOOKS
100int
101owner_sx(struct lock_object *lock, struct thread **owner)
102{
103        struct sx *sx = (struct sx *)lock;
104  uintptr_t x = sx->sx_lock;
105
106        *owner = (struct thread *)SX_OWNER(x);
107        return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) :
108      (*owner != NULL));
109}
110#endif
111
112void
113sx_sysinit(void *arg)
114{
115        struct sx_args *sargs = arg;
116
117        sx_init(sargs->sa_sx, sargs->sa_desc);
118}
119
120void
121sx_init_flags(struct sx *sx, const char *description, int opts)
122{
123        int flags;
124
125        flags = LO_SLEEPABLE | LO_UPGRADABLE;
126        if (opts & SX_RECURSE)
127                flags |= LO_RECURSABLE;
128
129        rtems_bsd_mutex_init(&sx->lock_object, &sx->mutex, &lock_class_sx,
130            description, NULL, flags);
131}
132
133void
134sx_destroy(struct sx *sx)
135{
136
137        rtems_bsd_mutex_destroy(&sx->lock_object, &sx->mutex);
138}
139
140int
141_sx_xlock(struct sx *sx, int opts, const char *file, int line)
142{
143        rtems_bsd_mutex_lock(&sx->lock_object, &sx->mutex);
144
145        return (0);
146}
147
148int
149_sx_try_xlock(struct sx *sx, const char *file, int line)
150{
151        return (rtems_bsd_mutex_trylock(&sx->lock_object, &sx->mutex));
152}
153
154void
155_sx_xunlock(struct sx *sx, const char *file, int line)
156{
157        rtems_bsd_mutex_unlock(&sx->mutex);
158}
159
160int
161_sx_try_upgrade(struct sx *sx, const char *file, int line)
162{
163        return (1);
164}
165
166void
167_sx_downgrade(struct sx *sx, const char *file, int line)
168{
169        /* Do nothing */
170}
171
172#ifdef INVARIANT_SUPPORT
173#ifndef INVARIANTS
174#undef  _sx_assert
175#endif
176
177/*
178 * In the non-WITNESS case, sx_assert() can only detect that at least
179 * *some* thread owns an slock, but it cannot guarantee that *this*
180 * thread owns an slock.
181 */
182void
183_sx_assert(struct sx *sx, int what, const char *file, int line)
184{
185#ifndef __rtems__
186#ifndef WITNESS
187  int slocked = 0;
188#endif
189#endif /* __rtems__ */
190
191  if (panicstr != NULL)
192    return;
193  switch (what) {
194  case SA_SLOCKED:
195  case SA_SLOCKED | SA_NOTRECURSED:
196  case SA_SLOCKED | SA_RECURSED:
197#ifndef __rtems__
198#ifndef WITNESS
199    slocked = 1;
200    /* FALLTHROUGH */
201#endif
202#endif /* __rtems__ */
203  case SA_LOCKED:
204  case SA_LOCKED | SA_NOTRECURSED:
205  case SA_LOCKED | SA_RECURSED:
206#ifndef __rtems__
207#ifdef WITNESS
208    witness_assert(&sx->lock_object, what, file, line);
209#else
210    /*
211     * If some other thread has an exclusive lock or we
212     * have one and are asserting a shared lock, fail.
213     * Also, if no one has a lock at all, fail.
214     */
215    if (sx->sx_lock == SX_LOCK_UNLOCKED ||
216        (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
217        sx_xholder(sx) != curthread)))
218      panic("Lock %s not %slocked @ %s:%d\n",
219          sx->lock_object.lo_name, slocked ? "share " : "",
220          file, line);
221
222    if (!(sx->sx_lock & SX_LOCK_SHARED)) {
223      if (sx_recursed(sx)) {
224        if (what & SA_NOTRECURSED)
225          panic("Lock %s recursed @ %s:%d\n",
226              sx->lock_object.lo_name, file,
227              line);
228      } else if (what & SA_RECURSED)
229        panic("Lock %s not recursed @ %s:%d\n",
230            sx->lock_object.lo_name, file, line);
231    }
232#endif
233    break;
234#else /* __rtems__ */
235    /* FALLTHROUGH */
236#endif /* __rtems__ */
237  case SA_XLOCKED:
238  case SA_XLOCKED | SA_NOTRECURSED:
239  case SA_XLOCKED | SA_RECURSED:
240    if (sx_xholder(sx) != _Thread_Get_executing())
241      panic("Lock %s not exclusively locked @ %s:%d\n",
242          sx->lock_object.lo_name, file, line);
243    if (sx_recursed(sx)) {
244      if (what & SA_NOTRECURSED)
245        panic("Lock %s recursed @ %s:%d\n",
246            sx->lock_object.lo_name, file, line);
247    } else if (what & SA_RECURSED)
248      panic("Lock %s not recursed @ %s:%d\n",
249          sx->lock_object.lo_name, file, line);
250    break;
251  case SA_UNLOCKED:
252#ifdef WITNESS
253    witness_assert(&sx->lock_object, what, file, line);
254#else
255    /*
256     * If we hold an exclusve lock fail.  We can't
257     * reliably check to see if we hold a shared lock or
258     * not.
259     */
260    if (sx_xholder(sx) == _Thread_Get_executing())
261      panic("Lock %s exclusively locked @ %s:%d\n",
262          sx->lock_object.lo_name, file, line);
263#endif
264    break;
265  default:
266    panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
267        line);
268  }
269}
270#endif  /* INVARIANT_SUPPORT */
271
272int
273sx_xlocked(struct sx *sx)
274{
275        return (rtems_bsd_mutex_owned(&sx->mutex));
276}
Note: See TracBrowser for help on using the repository browser.