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 | |
---|
50 | static void assert_sx(const struct lock_object *lock, int what); |
---|
51 | static void lock_sx(struct lock_object *lock, uintptr_t how); |
---|
52 | static uintptr_t unlock_sx(struct lock_object *lock); |
---|
53 | |
---|
54 | struct lock_class lock_class_sx = { |
---|
55 | .lc_name = "sx", |
---|
56 | .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE, |
---|
57 | .lc_assert = assert_sx, |
---|
58 | .lc_lock = lock_sx, |
---|
59 | .lc_unlock = unlock_sx, |
---|
60 | }; |
---|
61 | |
---|
62 | #define sx_xholder(sx) rtems_bsd_mutex_owner(&(sx)->mutex) |
---|
63 | |
---|
64 | #define sx_recursed(sx) rtems_bsd_mutex_recursed(&(sx)->mutex) |
---|
65 | |
---|
66 | void |
---|
67 | assert_sx(const struct lock_object *lock, int what) |
---|
68 | { |
---|
69 | |
---|
70 | sx_assert((const struct sx *)lock, what); |
---|
71 | } |
---|
72 | |
---|
73 | void |
---|
74 | lock_sx(struct lock_object *lock, uintptr_t how) |
---|
75 | { |
---|
76 | |
---|
77 | sx_xlock((struct sx *)lock); |
---|
78 | } |
---|
79 | |
---|
80 | uintptr_t |
---|
81 | unlock_sx(struct lock_object *lock) |
---|
82 | { |
---|
83 | |
---|
84 | sx_xunlock((struct sx *)lock); |
---|
85 | return (0); |
---|
86 | } |
---|
87 | |
---|
88 | void |
---|
89 | sx_sysinit(void *arg) |
---|
90 | { |
---|
91 | struct sx_args *sargs = arg; |
---|
92 | |
---|
93 | sx_init(sargs->sa_sx, sargs->sa_desc); |
---|
94 | } |
---|
95 | |
---|
96 | void |
---|
97 | sx_init_flags(struct sx *sx, const char *description, int opts) |
---|
98 | { |
---|
99 | int flags; |
---|
100 | |
---|
101 | flags = LO_SLEEPABLE | LO_UPGRADABLE; |
---|
102 | if (opts & SX_RECURSE) |
---|
103 | flags |= LO_RECURSABLE; |
---|
104 | |
---|
105 | rtems_bsd_mutex_init(&sx->lock_object, &sx->mutex, &lock_class_sx, |
---|
106 | description, NULL, flags); |
---|
107 | } |
---|
108 | |
---|
109 | void |
---|
110 | sx_destroy(struct sx *sx) |
---|
111 | { |
---|
112 | |
---|
113 | rtems_bsd_mutex_destroy(&sx->lock_object, &sx->mutex); |
---|
114 | } |
---|
115 | |
---|
116 | int |
---|
117 | _sx_xlock(struct sx *sx, int opts, const char *file, int line) |
---|
118 | { |
---|
119 | rtems_bsd_mutex_lock(&sx->lock_object, &sx->mutex); |
---|
120 | |
---|
121 | return (0); |
---|
122 | } |
---|
123 | |
---|
124 | int |
---|
125 | sx_try_xlock_(struct sx *sx, const char *file, int line) |
---|
126 | { |
---|
127 | return (rtems_bsd_mutex_trylock(&sx->lock_object, &sx->mutex)); |
---|
128 | } |
---|
129 | |
---|
130 | void |
---|
131 | _sx_xunlock(struct sx *sx, const char *file, int line) |
---|
132 | { |
---|
133 | rtems_bsd_mutex_unlock(&sx->mutex); |
---|
134 | } |
---|
135 | |
---|
136 | int |
---|
137 | sx_try_upgrade_(struct sx *sx, const char *file, int line) |
---|
138 | { |
---|
139 | return (1); |
---|
140 | } |
---|
141 | |
---|
142 | void |
---|
143 | sx_downgrade_(struct sx *sx, const char *file, int line) |
---|
144 | { |
---|
145 | /* Do nothing */ |
---|
146 | } |
---|
147 | |
---|
148 | #ifdef INVARIANT_SUPPORT |
---|
149 | /* |
---|
150 | * In the non-WITNESS case, sx_assert() can only detect that at least |
---|
151 | * *some* thread owns an slock, but it cannot guarantee that *this* |
---|
152 | * thread owns an slock. |
---|
153 | */ |
---|
154 | void |
---|
155 | _sx_assert(const struct sx *sx, int what, const char *file, int line) |
---|
156 | { |
---|
157 | const char *name = rtems_bsd_mutex_name(&sx->mutex); |
---|
158 | |
---|
159 | switch (what) { |
---|
160 | case SA_SLOCKED: |
---|
161 | case SA_SLOCKED | SA_NOTRECURSED: |
---|
162 | case SA_SLOCKED | SA_RECURSED: |
---|
163 | case SA_LOCKED: |
---|
164 | case SA_LOCKED | SA_NOTRECURSED: |
---|
165 | case SA_LOCKED | SA_RECURSED: |
---|
166 | case SA_XLOCKED: |
---|
167 | case SA_XLOCKED | SA_NOTRECURSED: |
---|
168 | case SA_XLOCKED | SA_RECURSED: |
---|
169 | if (sx_xholder(sx) != _Thread_Get_executing()) |
---|
170 | panic("Lock %s not exclusively locked @ %s:%d\n", name, |
---|
171 | file, line); |
---|
172 | if (sx_recursed(sx)) { |
---|
173 | if (what & SA_NOTRECURSED) |
---|
174 | panic("Lock %s recursed @ %s:%d\n", name, file, |
---|
175 | line); |
---|
176 | } else if (what & SA_RECURSED) |
---|
177 | panic("Lock %s not recursed @ %s:%d\n", name, file, |
---|
178 | line); |
---|
179 | break; |
---|
180 | case SA_UNLOCKED: |
---|
181 | #ifdef WITNESS |
---|
182 | witness_assert(&sx->lock_object, what, file, line); |
---|
183 | #else |
---|
184 | /* |
---|
185 | * If we hold an exclusve lock fail. We can't |
---|
186 | * reliably check to see if we hold a shared lock or |
---|
187 | * not. |
---|
188 | */ |
---|
189 | if (sx_xholder(sx) == _Thread_Get_executing()) |
---|
190 | panic("Lock %s exclusively locked @ %s:%d\n", name, |
---|
191 | file, line); |
---|
192 | #endif |
---|
193 | break; |
---|
194 | default: |
---|
195 | panic("Unknown sx lock assertion: %d @ %s:%d", what, file, |
---|
196 | line); |
---|
197 | } |
---|
198 | } |
---|
199 | #endif /* INVARIANT_SUPPORT */ |
---|
200 | |
---|
201 | int |
---|
202 | sx_xlocked(struct sx *sx) |
---|
203 | { |
---|
204 | return (rtems_bsd_mutex_owned(&sx->mutex)); |
---|
205 | } |
---|