1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup rtems_bsd_rtems |
---|
5 | * |
---|
6 | * @brief TODO. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2009-2014 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 | |
---|
43 | #include <sys/param.h> |
---|
44 | #include <sys/types.h> |
---|
45 | #include <sys/systm.h> |
---|
46 | #include <sys/lock.h> |
---|
47 | #include <sys/mutex.h> |
---|
48 | #include <sys/proc.h> |
---|
49 | #include <sys/conf.h> |
---|
50 | |
---|
51 | #if RTEMS_DEBUG |
---|
52 | struct _bsd_mutex_list _bsd_mutexlist = TAILQ_HEAD_INITIALIZER(_bsd_mutexlist); |
---|
53 | rtems_mutex _bsd_mutexlist_lock = RTEMS_MUTEX_INITIALIZER("mmutexlist"); |
---|
54 | #endif /* RTEMS_DEBUG */ |
---|
55 | |
---|
56 | static void assert_mtx(const struct lock_object *lock, int what); |
---|
57 | static void lock_mtx(struct lock_object *lock, uintptr_t how); |
---|
58 | static uintptr_t unlock_mtx(struct lock_object *lock); |
---|
59 | |
---|
60 | /* |
---|
61 | * Lock classes for sleep and spin mutexes. |
---|
62 | */ |
---|
63 | struct lock_class lock_class_mtx_sleep = { |
---|
64 | .lc_name = "sleep mutex", |
---|
65 | .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE, |
---|
66 | .lc_assert = assert_mtx, |
---|
67 | .lc_lock = lock_mtx, |
---|
68 | .lc_unlock = unlock_mtx, |
---|
69 | }; |
---|
70 | |
---|
71 | struct lock_class lock_class_mtx_spin = { |
---|
72 | .lc_name = "spin mutex", |
---|
73 | .lc_flags = LC_SPINLOCK | LC_RECURSABLE, |
---|
74 | .lc_assert = assert_mtx, |
---|
75 | .lc_lock = lock_mtx, |
---|
76 | .lc_unlock = unlock_mtx, |
---|
77 | }; |
---|
78 | |
---|
79 | struct mtx Giant; |
---|
80 | |
---|
81 | void |
---|
82 | assert_mtx(const struct lock_object *lock, int what) |
---|
83 | { |
---|
84 | |
---|
85 | mtx_assert((const struct mtx *)lock, what); |
---|
86 | } |
---|
87 | |
---|
88 | void |
---|
89 | lock_mtx(struct lock_object *lock, uintptr_t how) |
---|
90 | { |
---|
91 | |
---|
92 | mtx_lock((struct mtx *)lock); |
---|
93 | } |
---|
94 | |
---|
95 | uintptr_t |
---|
96 | unlock_mtx(struct lock_object *lock) |
---|
97 | { |
---|
98 | |
---|
99 | mtx_unlock((struct mtx *)lock); |
---|
100 | return (0); |
---|
101 | } |
---|
102 | |
---|
103 | void |
---|
104 | mtx_init(struct mtx *m, const char *name, const char *type, int opts) |
---|
105 | { |
---|
106 | struct lock_class *class; |
---|
107 | int flags; |
---|
108 | |
---|
109 | /* Determine lock class and lock flags. */ |
---|
110 | if (opts & MTX_SPIN) |
---|
111 | class = &lock_class_mtx_spin; |
---|
112 | else |
---|
113 | class = &lock_class_mtx_sleep; |
---|
114 | flags = 0; |
---|
115 | if (opts & MTX_RECURSE) |
---|
116 | flags |= LO_RECURSABLE; |
---|
117 | |
---|
118 | rtems_bsd_mutex_init(&m->lock_object, class, name, type, |
---|
119 | flags); |
---|
120 | } |
---|
121 | |
---|
122 | void |
---|
123 | _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line) |
---|
124 | { |
---|
125 | rtems_bsd_mutex_lock(&m->lock_object); |
---|
126 | } |
---|
127 | |
---|
128 | int |
---|
129 | mtx_trylock_flags_(struct mtx *m, int opts, const char *file, int line) |
---|
130 | { |
---|
131 | return (rtems_bsd_mutex_trylock(&m->lock_object)); |
---|
132 | } |
---|
133 | |
---|
134 | void |
---|
135 | _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line) |
---|
136 | { |
---|
137 | rtems_bsd_mutex_unlock(&m->lock_object); |
---|
138 | } |
---|
139 | |
---|
140 | /* |
---|
141 | * The backing function for the INVARIANTS-enabled mtx_assert() |
---|
142 | */ |
---|
143 | #ifdef INVARIANT_SUPPORT |
---|
144 | void |
---|
145 | _mtx_assert(struct mtx *m, int what, const char *file, int line) |
---|
146 | { |
---|
147 | const char *name = rtems_bsd_mutex_name(&m->lock_object); |
---|
148 | |
---|
149 | switch (what) { |
---|
150 | case MA_OWNED: |
---|
151 | case MA_OWNED | MA_RECURSED: |
---|
152 | case MA_OWNED | MA_NOTRECURSED: |
---|
153 | if (!mtx_owned(m)) |
---|
154 | panic("mutex %s not owned at %s:%d", name, file, line); |
---|
155 | if (mtx_recursed(m)) { |
---|
156 | if ((what & MA_NOTRECURSED) != 0) |
---|
157 | panic("mutex %s recursed at %s:%d", name, file, |
---|
158 | line); |
---|
159 | } else if ((what & MA_RECURSED) != 0) { |
---|
160 | panic("mutex %s unrecursed at %s:%d", name, file, |
---|
161 | line); |
---|
162 | } |
---|
163 | break; |
---|
164 | case MA_NOTOWNED: |
---|
165 | if (mtx_owned(m)) |
---|
166 | panic("mutex %s owned at %s:%d", name, file, line); |
---|
167 | break; |
---|
168 | default: |
---|
169 | panic("unknown mtx_assert at %s:%d", file, line); |
---|
170 | } |
---|
171 | } |
---|
172 | #endif |
---|
173 | |
---|
174 | int mtx_owned(struct mtx *m) |
---|
175 | { |
---|
176 | return (rtems_bsd_mutex_owned(&m->lock_object)); |
---|
177 | } |
---|
178 | |
---|
179 | int mtx_recursed(struct mtx *m) |
---|
180 | { |
---|
181 | return (rtems_bsd_mutex_recursed(&m->lock_object)); |
---|
182 | } |
---|
183 | |
---|
184 | void |
---|
185 | mtx_sysinit(void *arg) |
---|
186 | { |
---|
187 | struct mtx_args *margs = arg; |
---|
188 | |
---|
189 | mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts); |
---|
190 | } |
---|
191 | |
---|
192 | void |
---|
193 | mtx_destroy(struct mtx *m) |
---|
194 | { |
---|
195 | |
---|
196 | rtems_bsd_mutex_destroy(&m->lock_object); |
---|
197 | } |
---|
198 | |
---|
199 | void |
---|
200 | mutex_init(void) |
---|
201 | { |
---|
202 | mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE); |
---|
203 | mtx_lock(&Giant); |
---|
204 | } |
---|