summaryrefslogtreecommitdiffstats
path: root/rtemsbsd/rtems/rtems-bsd-mutex.c
blob: 37cc04ca2110d4cf1bfe0ac7fc1fa2f7b0a42eaf (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
/**
 * @file
 *
 * @ingroup rtems_bsd_rtems
 *
 * @brief TODO.
 */

/*
 * Copyright (c) 2009-2014 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <machine/rtems-bsd-kernel-space.h>
#include <machine/rtems-bsd-muteximpl.h>

#include <rtems/bsd/sys/param.h>
#include <rtems/bsd/sys/types.h>
#include <sys/systm.h>
#include <rtems/bsd/sys/lock.h>
#include <sys/mutex.h>
#include <sys/proc.h>

static void assert_mtx(struct lock_object *lock, int what);
static void lock_mtx(struct lock_object *lock, int how);
#ifdef KDTRACE_HOOKS
static int  owner_mtx(struct lock_object *lock, struct thread **owner);
#endif
static int  unlock_mtx(struct lock_object *lock);

/*
 * Lock classes for sleep and spin mutexes.
 */
struct lock_class lock_class_mtx_sleep = {
  .lc_name = "sleep mutex",
  .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE,
  .lc_assert = assert_mtx,
#ifdef DDB
  .lc_ddb_show = db_show_mtx,
#endif
  .lc_lock = lock_mtx,
  .lc_unlock = unlock_mtx,
#ifdef KDTRACE_HOOKS
  .lc_owner = owner_mtx,
#endif
};

struct lock_class lock_class_mtx_spin = {
  .lc_name = "spin mutex",
  .lc_flags = LC_SPINLOCK | LC_RECURSABLE,
  .lc_assert = assert_mtx,
#ifdef DDB
  .lc_ddb_show = db_show_mtx,
#endif
  .lc_lock = lock_mtx,
  .lc_unlock = unlock_mtx,
#ifdef KDTRACE_HOOKS
  .lc_owner = owner_mtx,
#endif
};

struct mtx Giant;

void
assert_mtx(struct lock_object *lock, int what)
{
  mtx_assert((struct mtx *)lock, what);
}

void
lock_mtx(struct lock_object *lock, int how)
{
	mtx_lock((struct mtx *)lock);
}

int
unlock_mtx(struct lock_object *lock)
{
	mtx_unlock((struct mtx *)lock);

	return (0);
}


#ifdef KDTRACE_HOOKS
int
owner_mtx(struct lock_object *lock, struct thread **owner)
{
  struct mtx *m = (struct mtx *)lock;

  *owner = mtx_owner(m);
  return (mtx_unowned(m) == 0);
}
#endif

void
mtx_init(struct mtx *m, const char *name, const char *type, int opts)
{
	struct lock_class *class;
	int flags;

	/* Determine lock class and lock flags. */
	if (opts & MTX_SPIN)
		class = &lock_class_mtx_spin;
	else
		class = &lock_class_mtx_sleep;
	flags = 0;
	if (opts & MTX_RECURSE)
		flags |= LO_RECURSABLE;

	rtems_bsd_mutex_init(&m->lock_object, &m->mutex, class, name, type,
	    flags);
}

void
_mtx_lock_flags(struct mtx *m, int opts, const char *file, int line)
{
	rtems_bsd_mutex_lock(&m->lock_object, &m->mutex);
}

int
_mtx_trylock(struct mtx *m, int opts, const char *file, int line)
{
	return (rtems_bsd_mutex_trylock(&m->lock_object, &m->mutex));
}

void
_mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line)
{
	rtems_bsd_mutex_unlock(&m->mutex);
}

/*
 * The backing function for the INVARIANTS-enabled mtx_assert()
 */
#ifdef INVARIANT_SUPPORT
void
_mtx_assert(struct mtx *m, int what, const char *file, int line)
{

  if (panicstr != NULL || dumping)
    return;
  switch (what) {
  case MA_OWNED:
  case MA_OWNED | MA_RECURSED:
  case MA_OWNED | MA_NOTRECURSED:
    if (!mtx_owned(m))
      panic("mutex %s not owned at %s:%d",
          m->lock_object.lo_name, file, line);
    if (mtx_recursed(m)) {
      if ((what & MA_NOTRECURSED) != 0)
        panic("mutex %s recursed at %s:%d",
            m->lock_object.lo_name, file, line);
    } else if ((what & MA_RECURSED) != 0) {
      panic("mutex %s unrecursed at %s:%d",
          m->lock_object.lo_name, file, line);
    }
    break;
  case MA_NOTOWNED:
    if (mtx_owned(m))
      panic("mutex %s owned at %s:%d",
          m->lock_object.lo_name, file, line);
    break;
  default:
    panic("unknown mtx_assert at %s:%d", file, line);
  }
}
#endif

int mtx_owned(struct mtx *m)
{
	return (rtems_bsd_mutex_owned(&m->mutex));
}

int mtx_recursed(struct mtx *m)
{
	return (rtems_bsd_mutex_recursed(&m->mutex));
}

void
mtx_sysinit(void *arg)
{
	struct mtx_args *margs = arg;

	mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts);
}

void
mtx_destroy(struct mtx *m)
{

	rtems_bsd_mutex_destroy(&m->lock_object, &m->mutex);
}

void
mutex_init(void)
{
	mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
}