1 | /* |
---|
2 | * Copyright (c) 2014 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Dornierstr. 4 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.org/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #ifndef _RTEMS_SCORE_MRSPIMPL_H |
---|
16 | #define _RTEMS_SCORE_MRSPIMPL_H |
---|
17 | |
---|
18 | #include <rtems/score/mrsp.h> |
---|
19 | |
---|
20 | #if defined(RTEMS_SMP) |
---|
21 | |
---|
22 | #include <rtems/score/assert.h> |
---|
23 | #include <rtems/score/chainimpl.h> |
---|
24 | #include <rtems/score/resourceimpl.h> |
---|
25 | #include <rtems/score/schedulerimpl.h> |
---|
26 | #include <rtems/score/watchdogimpl.h> |
---|
27 | #include <rtems/score/wkspace.h> |
---|
28 | |
---|
29 | #ifdef __cplusplus |
---|
30 | extern "C" { |
---|
31 | #endif /* __cplusplus */ |
---|
32 | |
---|
33 | /** |
---|
34 | * @addtogroup ScoreMRSP |
---|
35 | * |
---|
36 | * @{ |
---|
37 | */ |
---|
38 | |
---|
39 | #define MRSP_RIVAL_STATE_WAITING 0x0U |
---|
40 | |
---|
41 | #define MRSP_RIVAL_STATE_NEW_OWNER 0x1U |
---|
42 | |
---|
43 | #define MRSP_RIVAL_STATE_TIMEOUT 0x2U |
---|
44 | |
---|
45 | RTEMS_INLINE_ROUTINE bool _MRSP_Set_root_visitor( |
---|
46 | Resource_Node *node, |
---|
47 | void *arg |
---|
48 | ) |
---|
49 | { |
---|
50 | _Resource_Node_set_root( node, arg ); |
---|
51 | |
---|
52 | return false; |
---|
53 | } |
---|
54 | |
---|
55 | RTEMS_INLINE_ROUTINE void _MRSP_Set_root( |
---|
56 | Resource_Node *top, |
---|
57 | Resource_Node *root |
---|
58 | ) |
---|
59 | { |
---|
60 | _Resource_Node_set_root( top, root ); |
---|
61 | _Resource_Iterate( top, _MRSP_Set_root_visitor, root ); |
---|
62 | } |
---|
63 | |
---|
64 | RTEMS_INLINE_ROUTINE void _MRSP_Elevate_priority( |
---|
65 | MRSP_Control *mrsp, |
---|
66 | Thread_Control *new_owner, |
---|
67 | Priority_Control ceiling_priority |
---|
68 | ) |
---|
69 | { |
---|
70 | _Thread_Change_priority( new_owner, ceiling_priority, false ); |
---|
71 | } |
---|
72 | |
---|
73 | RTEMS_INLINE_ROUTINE void _MRSP_Restore_priority( |
---|
74 | const MRSP_Control *mrsp, |
---|
75 | Thread_Control *thread, |
---|
76 | Priority_Control initial_priority |
---|
77 | ) |
---|
78 | { |
---|
79 | /* |
---|
80 | * The Thread_Control::resource_count is used by the normal priority ceiling |
---|
81 | * or priority inheritance semaphores. |
---|
82 | */ |
---|
83 | if ( thread->resource_count == 0 ) { |
---|
84 | Priority_Control new_priority = _Scheduler_Highest_priority_of_two( |
---|
85 | _Scheduler_Get( thread ), |
---|
86 | initial_priority, |
---|
87 | thread->real_priority |
---|
88 | ); |
---|
89 | |
---|
90 | _Thread_Change_priority( thread, new_priority, true ); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | RTEMS_INLINE_ROUTINE void _MRSP_Claim_ownership( |
---|
95 | MRSP_Control *mrsp, |
---|
96 | Thread_Control *new_owner, |
---|
97 | Priority_Control initial_priority, |
---|
98 | Priority_Control ceiling_priority |
---|
99 | ) |
---|
100 | { |
---|
101 | _Resource_Node_add_resource( &new_owner->Resource_node, &mrsp->Resource ); |
---|
102 | _Resource_Set_owner( &mrsp->Resource, &new_owner->Resource_node ); |
---|
103 | mrsp->initial_priority_of_owner = initial_priority; |
---|
104 | _MRSP_Elevate_priority( mrsp, new_owner, ceiling_priority ); |
---|
105 | } |
---|
106 | |
---|
107 | RTEMS_INLINE_ROUTINE MRSP_Status _MRSP_Initialize( |
---|
108 | MRSP_Control *mrsp, |
---|
109 | Priority_Control ceiling_priority, |
---|
110 | Thread_Control *executing, |
---|
111 | bool initially_locked |
---|
112 | ) |
---|
113 | { |
---|
114 | uint32_t scheduler_count = _Scheduler_Count; |
---|
115 | uint32_t i; |
---|
116 | |
---|
117 | if ( initially_locked ) { |
---|
118 | return MRSP_INVALID_NUMBER; |
---|
119 | } |
---|
120 | |
---|
121 | mrsp->ceiling_priorities = _Workspace_Allocate( |
---|
122 | sizeof( *mrsp->ceiling_priorities ) * scheduler_count |
---|
123 | ); |
---|
124 | if ( mrsp->ceiling_priorities == NULL ) { |
---|
125 | return MRSP_NO_MEMORY; |
---|
126 | } |
---|
127 | |
---|
128 | for ( i = 0 ; i < scheduler_count ; ++i ) { |
---|
129 | mrsp->ceiling_priorities[ i ] = ceiling_priority; |
---|
130 | } |
---|
131 | |
---|
132 | _Resource_Initialize( &mrsp->Resource ); |
---|
133 | _Chain_Initialize_empty( &mrsp->Rivals ); |
---|
134 | |
---|
135 | return MRSP_SUCCESSFUL; |
---|
136 | } |
---|
137 | |
---|
138 | RTEMS_INLINE_ROUTINE Priority_Control _MRSP_Get_ceiling_priority( |
---|
139 | MRSP_Control *mrsp, |
---|
140 | uint32_t scheduler_index |
---|
141 | ) |
---|
142 | { |
---|
143 | return mrsp->ceiling_priorities[ scheduler_index ]; |
---|
144 | } |
---|
145 | |
---|
146 | RTEMS_INLINE_ROUTINE void _MRSP_Set_ceiling_priority( |
---|
147 | MRSP_Control *mrsp, |
---|
148 | uint32_t scheduler_index, |
---|
149 | Priority_Control ceiling_priority |
---|
150 | ) |
---|
151 | { |
---|
152 | mrsp->ceiling_priorities[ scheduler_index ] = ceiling_priority; |
---|
153 | } |
---|
154 | |
---|
155 | RTEMS_INLINE_ROUTINE void _MRSP_Add_state( |
---|
156 | MRSP_Rival *rival, |
---|
157 | unsigned int state |
---|
158 | ) |
---|
159 | { |
---|
160 | _Atomic_Fetch_or_uint( &rival->state, state, ATOMIC_ORDER_RELEASE ); |
---|
161 | } |
---|
162 | |
---|
163 | RTEMS_INLINE_ROUTINE void _MRSP_Timeout( |
---|
164 | Objects_Id id, |
---|
165 | void *arg |
---|
166 | ) |
---|
167 | { |
---|
168 | MRSP_Rival *rival = arg; |
---|
169 | |
---|
170 | (void) id; |
---|
171 | |
---|
172 | _MRSP_Add_state( rival, MRSP_RIVAL_STATE_TIMEOUT ); |
---|
173 | } |
---|
174 | |
---|
175 | RTEMS_INLINE_ROUTINE MRSP_Status _MRSP_Wait_for_ownership( |
---|
176 | MRSP_Control *mrsp, |
---|
177 | Resource_Node *owner, |
---|
178 | Thread_Control *executing, |
---|
179 | Priority_Control initial_priority, |
---|
180 | Priority_Control ceiling_priority, |
---|
181 | Watchdog_Interval timeout |
---|
182 | ) |
---|
183 | { |
---|
184 | MRSP_Status status; |
---|
185 | MRSP_Rival rival; |
---|
186 | bool previous_life_protection; |
---|
187 | unsigned int state; |
---|
188 | |
---|
189 | _MRSP_Elevate_priority( mrsp, executing, ceiling_priority ); |
---|
190 | |
---|
191 | rival.thread = executing; |
---|
192 | _Atomic_Init_uint( &rival.state, MRSP_RIVAL_STATE_WAITING ); |
---|
193 | _Chain_Append_unprotected( &mrsp->Rivals, &rival.Node ); |
---|
194 | _Resource_Add_rival( &mrsp->Resource, &executing->Resource_node ); |
---|
195 | _Resource_Node_set_dependency( &executing->Resource_node, &mrsp->Resource ); |
---|
196 | _MRSP_Set_root( &executing->Resource_node, owner ); |
---|
197 | |
---|
198 | if ( timeout > 0 ) { |
---|
199 | _Watchdog_Initialize( |
---|
200 | &executing->Timer, |
---|
201 | _MRSP_Timeout, |
---|
202 | 0, |
---|
203 | &rival |
---|
204 | ); |
---|
205 | _Watchdog_Insert_ticks( &executing->Timer, timeout ); |
---|
206 | } |
---|
207 | |
---|
208 | previous_life_protection = _Thread_Set_life_protection( true ); |
---|
209 | _Thread_Enable_dispatch(); |
---|
210 | |
---|
211 | _Assert( _Debug_Is_thread_dispatching_allowed() ); |
---|
212 | |
---|
213 | while ( |
---|
214 | _Atomic_Load_uint( &rival.state, ATOMIC_ORDER_ACQUIRE ) |
---|
215 | == MRSP_RIVAL_STATE_WAITING |
---|
216 | ) { |
---|
217 | /* Wait for state change */ |
---|
218 | } |
---|
219 | |
---|
220 | _Thread_Disable_dispatch(); |
---|
221 | _Thread_Set_life_protection( previous_life_protection ); |
---|
222 | |
---|
223 | if ( timeout > 0 ) { |
---|
224 | _Watchdog_Remove( &executing->Timer ); |
---|
225 | } |
---|
226 | |
---|
227 | _Chain_Extract_unprotected( &rival.Node ); |
---|
228 | state = _Atomic_Load_uint( &rival.state, ATOMIC_ORDER_RELAXED ); |
---|
229 | |
---|
230 | if ( ( state & MRSP_RIVAL_STATE_NEW_OWNER ) != 0 ) { |
---|
231 | mrsp->initial_priority_of_owner = initial_priority; |
---|
232 | status = MRSP_SUCCESSFUL; |
---|
233 | } else { |
---|
234 | Resource_Node *executing_node = &executing->Resource_node; |
---|
235 | |
---|
236 | _Resource_Node_extract( executing_node ); |
---|
237 | _Resource_Node_set_dependency( executing_node, NULL ); |
---|
238 | _MRSP_Set_root( executing_node, executing_node ); |
---|
239 | _MRSP_Restore_priority( mrsp, executing, initial_priority ); |
---|
240 | |
---|
241 | status = MRSP_TIMEOUT; |
---|
242 | } |
---|
243 | |
---|
244 | return status; |
---|
245 | } |
---|
246 | |
---|
247 | RTEMS_INLINE_ROUTINE MRSP_Status _MRSP_Obtain( |
---|
248 | MRSP_Control *mrsp, |
---|
249 | Thread_Control *executing, |
---|
250 | bool wait, |
---|
251 | Watchdog_Interval timeout |
---|
252 | ) |
---|
253 | { |
---|
254 | MRSP_Status status; |
---|
255 | const Scheduler_Control *scheduler = _Scheduler_Get( executing ); |
---|
256 | uint32_t scheduler_index = _Scheduler_Get_index( scheduler ); |
---|
257 | Priority_Control initial_priority = executing->current_priority; |
---|
258 | Priority_Control ceiling_priority = |
---|
259 | _MRSP_Get_ceiling_priority( mrsp, scheduler_index ); |
---|
260 | bool priority_ok = !_Scheduler_Is_priority_higher_than( |
---|
261 | scheduler, |
---|
262 | initial_priority, |
---|
263 | ceiling_priority |
---|
264 | ); |
---|
265 | Resource_Node *owner; |
---|
266 | |
---|
267 | if ( !priority_ok) { |
---|
268 | return MRSP_INVALID_PRIORITY; |
---|
269 | } |
---|
270 | |
---|
271 | owner = _Resource_Get_owner( &mrsp->Resource ); |
---|
272 | if ( owner == NULL ) { |
---|
273 | _MRSP_Claim_ownership( |
---|
274 | mrsp, |
---|
275 | executing, |
---|
276 | initial_priority, |
---|
277 | ceiling_priority |
---|
278 | ); |
---|
279 | status = MRSP_SUCCESSFUL; |
---|
280 | } else if ( _Resource_Node_get_root( owner ) == &executing->Resource_node ) { |
---|
281 | /* Nested access or deadlock */ |
---|
282 | status = MRSP_UNSATISFIED; |
---|
283 | } else if ( wait ) { |
---|
284 | status = _MRSP_Wait_for_ownership( |
---|
285 | mrsp, |
---|
286 | owner, |
---|
287 | executing, |
---|
288 | initial_priority, |
---|
289 | ceiling_priority, |
---|
290 | timeout |
---|
291 | ); |
---|
292 | } else { |
---|
293 | status = MRSP_UNSATISFIED; |
---|
294 | } |
---|
295 | |
---|
296 | return status; |
---|
297 | } |
---|
298 | |
---|
299 | RTEMS_INLINE_ROUTINE MRSP_Status _MRSP_Release( |
---|
300 | MRSP_Control *mrsp, |
---|
301 | Thread_Control *executing |
---|
302 | ) |
---|
303 | { |
---|
304 | if ( _Resource_Get_owner( &mrsp->Resource ) != &executing->Resource_node ) { |
---|
305 | return MRSP_NOT_OWNER_OF_RESOURCE; |
---|
306 | } |
---|
307 | |
---|
308 | if ( |
---|
309 | !_Resource_Is_most_recently_obtained( |
---|
310 | &mrsp->Resource, |
---|
311 | &executing->Resource_node |
---|
312 | ) |
---|
313 | ) { |
---|
314 | return MRSP_INCORRECT_STATE; |
---|
315 | } |
---|
316 | |
---|
317 | _Resource_Extract( &mrsp->Resource ); |
---|
318 | _MRSP_Restore_priority( mrsp, executing, mrsp->initial_priority_of_owner ); |
---|
319 | |
---|
320 | if ( _Chain_Is_empty( &mrsp->Rivals ) ) { |
---|
321 | _Resource_Set_owner( &mrsp->Resource, NULL ); |
---|
322 | } else { |
---|
323 | MRSP_Rival *rival = (MRSP_Rival *) _Chain_First( &mrsp->Rivals ); |
---|
324 | Resource_Node *new_owner = &rival->thread->Resource_node; |
---|
325 | |
---|
326 | _Resource_Node_extract( new_owner ); |
---|
327 | _Resource_Node_set_dependency( new_owner, NULL ); |
---|
328 | _MRSP_Set_root( new_owner, new_owner ); |
---|
329 | _Resource_Node_add_resource( new_owner, &mrsp->Resource ); |
---|
330 | _Resource_Set_owner( &mrsp->Resource, new_owner ); |
---|
331 | _MRSP_Add_state( rival, MRSP_RIVAL_STATE_NEW_OWNER ); |
---|
332 | } |
---|
333 | |
---|
334 | return MRSP_SUCCESSFUL; |
---|
335 | } |
---|
336 | |
---|
337 | RTEMS_INLINE_ROUTINE MRSP_Status _MRSP_Destroy( MRSP_Control *mrsp ) |
---|
338 | { |
---|
339 | if ( _Resource_Get_owner( &mrsp->Resource ) != NULL ) { |
---|
340 | return MRSP_RESOUCE_IN_USE; |
---|
341 | } |
---|
342 | |
---|
343 | _Workspace_Free( mrsp->ceiling_priorities ); |
---|
344 | |
---|
345 | return MRSP_SUCCESSFUL; |
---|
346 | } |
---|
347 | |
---|
348 | /** @} */ |
---|
349 | |
---|
350 | #ifdef __cplusplus |
---|
351 | } |
---|
352 | #endif /* __cplusplus */ |
---|
353 | |
---|
354 | #endif /* RTEMS_SMP */ |
---|
355 | |
---|
356 | #endif /* _RTEMS_SCORE_MRSPIMPL_H */ |
---|