1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup rtems_bsd_machine |
---|
5 | * |
---|
6 | * @brief Implementation of a mutex with a simple priority inheritance |
---|
7 | * protocol. |
---|
8 | */ |
---|
9 | |
---|
10 | /* |
---|
11 | * Copyright (c) 2014, 2016 embedded brains GmbH. All rights reserved. |
---|
12 | * |
---|
13 | * embedded brains GmbH |
---|
14 | * Dornierstr. 4 |
---|
15 | * 82178 Puchheim |
---|
16 | * Germany |
---|
17 | * <rtems@embedded-brains.de> |
---|
18 | * |
---|
19 | * Redistribution and use in source and binary forms, with or without |
---|
20 | * modification, are permitted provided that the following conditions |
---|
21 | * are met: |
---|
22 | * 1. Redistributions of source code must retain the above copyright |
---|
23 | * notice, this list of conditions and the following disclaimer. |
---|
24 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
25 | * notice, this list of conditions and the following disclaimer in the |
---|
26 | * documentation and/or other materials provided with the distribution. |
---|
27 | * |
---|
28 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
29 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
34 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
37 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
38 | * SUCH DAMAGE. |
---|
39 | */ |
---|
40 | |
---|
41 | #ifndef _RTEMS_BSD_MACHINE_RTEMS_BSD_MUTEXIMPL_H_ |
---|
42 | #define _RTEMS_BSD_MACHINE_RTEMS_BSD_MUTEXIMPL_H_ |
---|
43 | |
---|
44 | #include <machine/rtems-bsd-mutex.h> |
---|
45 | #include <machine/rtems-bsd-support.h> |
---|
46 | |
---|
47 | #include <sys/types.h> |
---|
48 | #include <rtems/bsd/sys/lock.h> |
---|
49 | |
---|
50 | #include <rtems/score/threadimpl.h> |
---|
51 | |
---|
52 | #ifdef __cplusplus |
---|
53 | extern "C" { |
---|
54 | #endif /* __cplusplus */ |
---|
55 | |
---|
56 | static inline void |
---|
57 | rtems_bsd_mutex_init(struct lock_object *lock, rtems_bsd_mutex *m, |
---|
58 | struct lock_class *class, const char *name, const char *type, int flags) |
---|
59 | { |
---|
60 | _Thread_queue_Initialize(&m->queue); |
---|
61 | m->nest_level = 0; |
---|
62 | |
---|
63 | lock_init(lock, class, name, type, flags); |
---|
64 | } |
---|
65 | |
---|
66 | void rtems_bsd_mutex_lock_more(struct lock_object *lock, rtems_bsd_mutex *m, |
---|
67 | Thread_Control *owner, Thread_Control *executing, |
---|
68 | Thread_queue_Context *queue_context); |
---|
69 | |
---|
70 | static inline void |
---|
71 | rtems_bsd_mutex_lock(struct lock_object *lock, rtems_bsd_mutex *m) |
---|
72 | { |
---|
73 | Thread_queue_Context queue_context; |
---|
74 | Thread_Control *executing; |
---|
75 | Thread_Control *owner; |
---|
76 | |
---|
77 | _Thread_queue_Context_initialize(&queue_context); |
---|
78 | _Thread_queue_Acquire(&m->queue, &queue_context.Lock_context); |
---|
79 | |
---|
80 | owner = m->queue.Queue.owner; |
---|
81 | executing = _Thread_Executing; |
---|
82 | |
---|
83 | if (__predict_true(owner == NULL)) { |
---|
84 | m->queue.Queue.owner = executing; |
---|
85 | ++executing->resource_count; |
---|
86 | |
---|
87 | _Thread_queue_Release(&m->queue, &queue_context.Lock_context); |
---|
88 | } else { |
---|
89 | rtems_bsd_mutex_lock_more(lock, m, owner, executing, |
---|
90 | &queue_context); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | static inline int |
---|
95 | rtems_bsd_mutex_trylock(struct lock_object *lock, rtems_bsd_mutex *m) |
---|
96 | { |
---|
97 | int success; |
---|
98 | Thread_queue_Context queue_context; |
---|
99 | Thread_Control *executing; |
---|
100 | Thread_Control *owner; |
---|
101 | |
---|
102 | _Thread_queue_Context_initialize(&queue_context); |
---|
103 | _Thread_queue_Acquire(&m->queue, &queue_context.Lock_context); |
---|
104 | |
---|
105 | owner = m->queue.Queue.owner; |
---|
106 | executing = _Thread_Executing; |
---|
107 | |
---|
108 | if (owner == NULL) { |
---|
109 | m->queue.Queue.owner = executing; |
---|
110 | ++executing->resource_count; |
---|
111 | success = 1; |
---|
112 | } else if (owner == executing) { |
---|
113 | BSD_ASSERT(lock->lo_flags & LO_RECURSABLE); |
---|
114 | ++m->nest_level; |
---|
115 | success = 1; |
---|
116 | } else { |
---|
117 | success = 0; |
---|
118 | } |
---|
119 | |
---|
120 | _Thread_queue_Release(&m->queue, &queue_context.Lock_context); |
---|
121 | |
---|
122 | return (success); |
---|
123 | } |
---|
124 | |
---|
125 | void rtems_bsd_mutex_unlock_more(rtems_bsd_mutex *m, Thread_Control *owner, |
---|
126 | int keep_priority, Thread_queue_Heads *heads, |
---|
127 | Thread_queue_Context *queue_context); |
---|
128 | |
---|
129 | static inline void |
---|
130 | rtems_bsd_mutex_unlock(rtems_bsd_mutex *m) |
---|
131 | { |
---|
132 | Thread_queue_Context queue_context; |
---|
133 | Thread_Control *owner; |
---|
134 | int nest_level; |
---|
135 | |
---|
136 | _Thread_queue_Context_initialize(&queue_context); |
---|
137 | _Thread_queue_Acquire(&m->queue, &queue_context.Lock_context); |
---|
138 | |
---|
139 | nest_level = m->nest_level; |
---|
140 | owner = m->queue.Queue.owner; |
---|
141 | |
---|
142 | BSD_ASSERT(owner == _Thread_Executing); |
---|
143 | |
---|
144 | if (__predict_true(nest_level == 0)) { |
---|
145 | Thread_queue_Heads *heads; |
---|
146 | int keep_priority; |
---|
147 | |
---|
148 | --owner->resource_count; |
---|
149 | |
---|
150 | /* |
---|
151 | * Ensure that the owner resource count is visible to all other |
---|
152 | * processors and that we read the latest priority restore |
---|
153 | * hint. |
---|
154 | */ |
---|
155 | _Atomic_Fence( ATOMIC_ORDER_ACQ_REL ); |
---|
156 | |
---|
157 | heads = m->queue.Queue.heads; |
---|
158 | keep_priority = _Thread_Owns_resources(owner) |
---|
159 | || !owner->priority_restore_hint; |
---|
160 | |
---|
161 | m->queue.Queue.owner = NULL; |
---|
162 | |
---|
163 | if (__predict_true(heads == NULL && keep_priority)) { |
---|
164 | _Thread_queue_Release(&m->queue, &queue_context.Lock_context); |
---|
165 | } else { |
---|
166 | rtems_bsd_mutex_unlock_more(m, owner, keep_priority, |
---|
167 | heads, &queue_context); |
---|
168 | } |
---|
169 | |
---|
170 | } else { |
---|
171 | m->nest_level = nest_level - 1; |
---|
172 | |
---|
173 | _Thread_queue_Release(&m->queue, &queue_context.Lock_context); |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | static inline int |
---|
178 | rtems_bsd_mutex_owned(rtems_bsd_mutex *m) |
---|
179 | { |
---|
180 | |
---|
181 | return (m->queue.Queue.owner == _Thread_Get_executing()); |
---|
182 | } |
---|
183 | |
---|
184 | static inline int |
---|
185 | rtems_bsd_mutex_recursed(rtems_bsd_mutex *m) |
---|
186 | { |
---|
187 | |
---|
188 | return (m->nest_level); |
---|
189 | } |
---|
190 | |
---|
191 | static inline void |
---|
192 | rtems_bsd_mutex_destroy(struct lock_object *lock, rtems_bsd_mutex *m) |
---|
193 | { |
---|
194 | BSD_ASSERT(m->queue.Queue.heads == NULL); |
---|
195 | |
---|
196 | if (rtems_bsd_mutex_owned(m)) { |
---|
197 | m->nest_level = 0; |
---|
198 | rtems_bsd_mutex_unlock(m); |
---|
199 | } |
---|
200 | |
---|
201 | _Thread_queue_Destroy(&m->queue); |
---|
202 | lock_destroy(lock); |
---|
203 | } |
---|
204 | |
---|
205 | #ifdef __cplusplus |
---|
206 | } |
---|
207 | #endif /* __cplusplus */ |
---|
208 | |
---|
209 | #endif /* _RTEMS_BSD_MACHINE_RTEMS_BSD_MUTEXIMPL_H_ */ |
---|