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, 2015 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 <rtems/bsd/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, THREAD_QUEUE_DISCIPLINE_PRIORITY); |
---|
61 | m->owner = NULL; |
---|
62 | m->nest_level = 0; |
---|
63 | |
---|
64 | lock_init(lock, class, name, type, flags); |
---|
65 | } |
---|
66 | |
---|
67 | void rtems_bsd_mutex_lock_more(struct lock_object *lock, rtems_bsd_mutex *m, |
---|
68 | Thread_Control *owner, Thread_Control *executing, |
---|
69 | ISR_lock_Context *lock_context); |
---|
70 | |
---|
71 | static inline void |
---|
72 | rtems_bsd_mutex_lock(struct lock_object *lock, rtems_bsd_mutex *m) |
---|
73 | { |
---|
74 | ISR_lock_Context lock_context; |
---|
75 | Thread_Control *executing; |
---|
76 | Thread_Control *owner; |
---|
77 | |
---|
78 | _Thread_queue_Acquire(&m->queue, &lock_context); |
---|
79 | |
---|
80 | owner = m->owner; |
---|
81 | executing = _Thread_Executing; |
---|
82 | |
---|
83 | if (__predict_true(owner == NULL)) { |
---|
84 | m->owner = executing; |
---|
85 | ++executing->resource_count; |
---|
86 | |
---|
87 | _Thread_queue_Release(&m->queue, &lock_context); |
---|
88 | } else { |
---|
89 | rtems_bsd_mutex_lock_more(lock, m, owner, executing, |
---|
90 | &lock_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 | ISR_lock_Context lock_context; |
---|
99 | Thread_Control *executing; |
---|
100 | Thread_Control *owner; |
---|
101 | |
---|
102 | _Thread_queue_Acquire(&m->queue, &lock_context); |
---|
103 | |
---|
104 | owner = m->owner; |
---|
105 | executing = _Thread_Executing; |
---|
106 | |
---|
107 | if (owner == NULL) { |
---|
108 | m->owner = executing; |
---|
109 | ++executing->resource_count; |
---|
110 | success = 1; |
---|
111 | } else if (owner == executing) { |
---|
112 | BSD_ASSERT(lock->lo_flags & LO_RECURSABLE); |
---|
113 | ++m->nest_level; |
---|
114 | success = 1; |
---|
115 | } else { |
---|
116 | success = 0; |
---|
117 | } |
---|
118 | |
---|
119 | _Thread_queue_Release(&m->queue, &lock_context); |
---|
120 | |
---|
121 | return (success); |
---|
122 | } |
---|
123 | |
---|
124 | void rtems_bsd_mutex_unlock_more(rtems_bsd_mutex *m, Thread_Control *owner, |
---|
125 | int keep_priority, Thread_queue_Heads *heads, |
---|
126 | ISR_lock_Context *lock_context); |
---|
127 | |
---|
128 | static inline void |
---|
129 | rtems_bsd_mutex_unlock(rtems_bsd_mutex *m) |
---|
130 | { |
---|
131 | ISR_lock_Context lock_context; |
---|
132 | Thread_Control *owner; |
---|
133 | int nest_level; |
---|
134 | |
---|
135 | _Thread_queue_Acquire(&m->queue, &lock_context); |
---|
136 | |
---|
137 | nest_level = m->nest_level; |
---|
138 | owner = m->owner; |
---|
139 | |
---|
140 | BSD_ASSERT(owner == _Thread_Executing); |
---|
141 | |
---|
142 | if (__predict_true(nest_level == 0)) { |
---|
143 | Thread_queue_Heads *heads; |
---|
144 | int keep_priority; |
---|
145 | |
---|
146 | --owner->resource_count; |
---|
147 | |
---|
148 | /* |
---|
149 | * Ensure that the owner resource count is visible to all other |
---|
150 | * processors and that we read the latest priority restore |
---|
151 | * hint. |
---|
152 | */ |
---|
153 | _Atomic_Fence( ATOMIC_ORDER_ACQ_REL ); |
---|
154 | |
---|
155 | heads = m->queue.heads; |
---|
156 | keep_priority = _Thread_Owns_resources(owner) |
---|
157 | || !owner->priority_restore_hint; |
---|
158 | |
---|
159 | m->owner = NULL; |
---|
160 | |
---|
161 | if (__predict_true(heads == NULL && keep_priority)) { |
---|
162 | _Thread_queue_Release(&m->queue, &lock_context); |
---|
163 | } else { |
---|
164 | rtems_bsd_mutex_unlock_more(m, owner, keep_priority, |
---|
165 | heads, &lock_context); |
---|
166 | } |
---|
167 | |
---|
168 | } else { |
---|
169 | m->nest_level = nest_level - 1; |
---|
170 | |
---|
171 | _Thread_queue_Release(&m->queue, &lock_context); |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | static inline int |
---|
176 | rtems_bsd_mutex_owned(rtems_bsd_mutex *m) |
---|
177 | { |
---|
178 | |
---|
179 | return (m->owner == _Thread_Get_executing()); |
---|
180 | } |
---|
181 | |
---|
182 | static inline int |
---|
183 | rtems_bsd_mutex_recursed(rtems_bsd_mutex *m) |
---|
184 | { |
---|
185 | |
---|
186 | return (m->nest_level); |
---|
187 | } |
---|
188 | |
---|
189 | static inline void |
---|
190 | rtems_bsd_mutex_destroy(struct lock_object *lock, rtems_bsd_mutex *m) |
---|
191 | { |
---|
192 | BSD_ASSERT(m->queue.heads == NULL); |
---|
193 | |
---|
194 | if (rtems_bsd_mutex_owned(m)) { |
---|
195 | m->nest_level = 0; |
---|
196 | rtems_bsd_mutex_unlock(m); |
---|
197 | } |
---|
198 | |
---|
199 | _Thread_queue_Destroy(&m->queue); |
---|
200 | lock_destroy(lock); |
---|
201 | } |
---|
202 | |
---|
203 | #ifdef __cplusplus |
---|
204 | } |
---|
205 | #endif /* __cplusplus */ |
---|
206 | |
---|
207 | #endif /* _RTEMS_BSD_MACHINE_RTEMS_BSD_MUTEXIMPL_H_ */ |
---|