source: rtems/cpukit/score/include/rtems/score/mrspimpl.h @ 40dcafa

4.11
Last change on this file since 40dcafa was 40dcafa, checked in by Sebastian Huber <sebastian.huber@…>, on Aug 2, 2014 at 2:22:31 PM

Add and use RTEMS_CONTAINER_OF()

  • Property mode set to 100644
File size: 8.8 KB
Line 
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
30extern "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
45RTEMS_INLINE_ROUTINE void _MRSP_Elevate_priority(
46  MRSP_Control     *mrsp,
47  Thread_Control   *new_owner,
48  Priority_Control  ceiling_priority
49)
50{
51  _Thread_Change_priority( new_owner, ceiling_priority, false );
52}
53
54RTEMS_INLINE_ROUTINE void _MRSP_Restore_priority(
55  const MRSP_Control *mrsp,
56  Thread_Control     *thread,
57  Priority_Control    initial_priority
58)
59{
60  /*
61   * The Thread_Control::resource_count is used by the normal priority ceiling
62   * or priority inheritance semaphores.
63   */
64  if ( thread->resource_count == 0 ) {
65    Priority_Control new_priority = _Scheduler_Highest_priority_of_two(
66      _Scheduler_Get( thread ),
67      initial_priority,
68      thread->real_priority
69    );
70
71    _Thread_Change_priority( thread, new_priority, true );
72  }
73}
74
75RTEMS_INLINE_ROUTINE void _MRSP_Claim_ownership(
76  MRSP_Control     *mrsp,
77  Thread_Control   *new_owner,
78  Priority_Control  initial_priority,
79  Priority_Control  ceiling_priority
80)
81{
82  _Resource_Node_add_resource( &new_owner->Resource_node, &mrsp->Resource );
83  _Resource_Set_owner( &mrsp->Resource, &new_owner->Resource_node );
84  mrsp->initial_priority_of_owner = initial_priority;
85  _MRSP_Elevate_priority( mrsp, new_owner, ceiling_priority );
86  _Scheduler_Thread_change_help_state( new_owner, SCHEDULER_HELP_ACTIVE_OWNER );
87}
88
89RTEMS_INLINE_ROUTINE MRSP_Status _MRSP_Initialize(
90  MRSP_Control     *mrsp,
91  Priority_Control  ceiling_priority,
92  Thread_Control   *executing,
93  bool              initially_locked
94)
95{
96  uint32_t scheduler_count = _Scheduler_Count;
97  uint32_t i;
98
99  if ( initially_locked ) {
100    return MRSP_INVALID_NUMBER;
101  }
102
103  mrsp->ceiling_priorities = _Workspace_Allocate(
104    sizeof( *mrsp->ceiling_priorities ) * scheduler_count
105  );
106  if ( mrsp->ceiling_priorities == NULL ) {
107    return MRSP_NO_MEMORY;
108  }
109
110  for ( i = 0 ; i < scheduler_count ; ++i ) {
111    mrsp->ceiling_priorities[ i ] = ceiling_priority;
112  }
113
114  _Resource_Initialize( &mrsp->Resource );
115  _Chain_Initialize_empty( &mrsp->Rivals );
116
117  return MRSP_SUCCESSFUL;
118}
119
120RTEMS_INLINE_ROUTINE Priority_Control _MRSP_Get_ceiling_priority(
121  MRSP_Control *mrsp,
122  uint32_t      scheduler_index
123)
124{
125  return mrsp->ceiling_priorities[ scheduler_index ];
126}
127
128RTEMS_INLINE_ROUTINE void _MRSP_Set_ceiling_priority(
129  MRSP_Control      *mrsp,
130  uint32_t           scheduler_index,
131  Priority_Control   ceiling_priority
132)
133{
134  mrsp->ceiling_priorities[ scheduler_index ] = ceiling_priority;
135}
136
137RTEMS_INLINE_ROUTINE void _MRSP_Add_state(
138  MRSP_Rival   *rival,
139  unsigned int  state
140)
141{
142  _Atomic_Fetch_or_uint( &rival->state, state, ATOMIC_ORDER_RELEASE );
143}
144
145RTEMS_INLINE_ROUTINE void _MRSP_Timeout(
146  Objects_Id  id,
147  void       *arg
148)
149{
150  MRSP_Rival *rival = arg;
151
152  (void) id;
153
154  _MRSP_Add_state( rival, MRSP_RIVAL_STATE_TIMEOUT );
155}
156
157RTEMS_INLINE_ROUTINE MRSP_Status _MRSP_Wait_for_ownership(
158  MRSP_Control      *mrsp,
159  Resource_Node     *owner,
160  Thread_Control    *executing,
161  Priority_Control   initial_priority,
162  Priority_Control   ceiling_priority,
163  Watchdog_Interval  timeout
164)
165{
166  MRSP_Status status;
167  MRSP_Rival rival;
168  bool previous_life_protection;
169  unsigned int state;
170  Scheduler_Help_state previous_help_state;
171
172  _MRSP_Elevate_priority( mrsp, executing, ceiling_priority );
173
174  rival.thread = executing;
175  _Atomic_Init_uint( &rival.state, MRSP_RIVAL_STATE_WAITING );
176  _Chain_Append_unprotected( &mrsp->Rivals, &rival.Node );
177  _Resource_Add_rival( &mrsp->Resource, &executing->Resource_node );
178  _Resource_Node_set_dependency( &executing->Resource_node, &mrsp->Resource );
179  previous_help_state =
180    _Scheduler_Thread_change_help_state( executing, SCHEDULER_HELP_ACTIVE_RIVAL );
181
182  _Scheduler_Thread_change_resource_root(
183    executing,
184    THREAD_RESOURCE_NODE_TO_THREAD( _Resource_Node_get_root( owner ) )
185  );
186
187  if ( timeout > 0 ) {
188    _Watchdog_Initialize(
189      &executing->Timer,
190      _MRSP_Timeout,
191      0,
192      &rival
193    );
194    _Watchdog_Insert_ticks( &executing->Timer, timeout );
195  }
196
197  previous_life_protection = _Thread_Set_life_protection( true );
198  _Thread_Enable_dispatch();
199
200  _Assert( _Debug_Is_thread_dispatching_allowed() );
201
202  while (
203    _Atomic_Load_uint( &rival.state, ATOMIC_ORDER_ACQUIRE )
204      == MRSP_RIVAL_STATE_WAITING
205  ) {
206    /* Wait for state change */
207  }
208
209  _Thread_Disable_dispatch();
210  _Thread_Set_life_protection( previous_life_protection );
211
212  if ( timeout > 0 ) {
213    _Watchdog_Remove( &executing->Timer );
214  }
215
216  _Chain_Extract_unprotected( &rival.Node );
217  state = _Atomic_Load_uint( &rival.state, ATOMIC_ORDER_RELAXED );
218
219  if ( ( state & MRSP_RIVAL_STATE_NEW_OWNER ) != 0 ) {
220    mrsp->initial_priority_of_owner = initial_priority;
221    status = MRSP_SUCCESSFUL;
222  } else {
223    _Resource_Node_extract( &executing->Resource_node );
224    _Resource_Node_set_dependency( &executing->Resource_node, NULL );
225    _Scheduler_Thread_change_help_state( executing, previous_help_state );
226    _Scheduler_Thread_change_resource_root( executing, executing );
227    _MRSP_Restore_priority( mrsp, executing, initial_priority );
228
229    status = MRSP_TIMEOUT;
230  }
231
232  return status;
233}
234
235RTEMS_INLINE_ROUTINE MRSP_Status _MRSP_Obtain(
236  MRSP_Control      *mrsp,
237  Thread_Control    *executing,
238  bool               wait,
239  Watchdog_Interval  timeout
240)
241{
242  MRSP_Status status;
243  const Scheduler_Control *scheduler = _Scheduler_Get( executing );
244  uint32_t scheduler_index = _Scheduler_Get_index( scheduler );
245  Priority_Control initial_priority = executing->current_priority;
246  Priority_Control ceiling_priority =
247    _MRSP_Get_ceiling_priority( mrsp, scheduler_index );
248  bool priority_ok = !_Scheduler_Is_priority_higher_than(
249    scheduler,
250    initial_priority,
251    ceiling_priority
252  );
253  Resource_Node *owner;
254
255  if ( !priority_ok) {
256    return MRSP_INVALID_PRIORITY;
257  }
258
259  owner = _Resource_Get_owner( &mrsp->Resource );
260  if ( owner == NULL ) {
261    _MRSP_Claim_ownership(
262      mrsp,
263      executing,
264      initial_priority,
265      ceiling_priority
266    );
267    status = MRSP_SUCCESSFUL;
268  } else if ( _Resource_Node_get_root( owner ) == &executing->Resource_node ) {
269    /* Nested access or deadlock */
270    status = MRSP_UNSATISFIED;
271  } else if ( wait ) {
272    status = _MRSP_Wait_for_ownership(
273      mrsp,
274      owner,
275      executing,
276      initial_priority,
277      ceiling_priority,
278      timeout
279    );
280  } else {
281    status = MRSP_UNSATISFIED;
282  }
283
284  return status;
285}
286
287RTEMS_INLINE_ROUTINE MRSP_Status _MRSP_Release(
288  MRSP_Control   *mrsp,
289  Thread_Control *executing
290)
291{
292  if ( _Resource_Get_owner( &mrsp->Resource ) != &executing->Resource_node ) {
293    return MRSP_NOT_OWNER_OF_RESOURCE;
294  }
295
296  if (
297    !_Resource_Is_most_recently_obtained(
298      &mrsp->Resource,
299      &executing->Resource_node
300    )
301  ) {
302    return MRSP_INCORRECT_STATE;
303  }
304
305  _Resource_Extract( &mrsp->Resource );
306  _MRSP_Restore_priority( mrsp, executing, mrsp->initial_priority_of_owner );
307
308  if ( _Chain_Is_empty( &mrsp->Rivals ) ) {
309    _Resource_Set_owner( &mrsp->Resource, NULL );
310  } else {
311    MRSP_Rival *rival = (MRSP_Rival *) _Chain_First( &mrsp->Rivals );
312    Thread_Control *new_owner = rival->thread;
313
314    _Resource_Node_extract( &new_owner->Resource_node );
315    _Resource_Node_set_dependency( &new_owner->Resource_node, NULL );
316    _Resource_Node_add_resource( &new_owner->Resource_node, &mrsp->Resource );
317    _Resource_Set_owner( &mrsp->Resource, &new_owner->Resource_node );
318    _Scheduler_Thread_change_help_state( new_owner, SCHEDULER_HELP_ACTIVE_OWNER );
319    _Scheduler_Thread_change_resource_root( new_owner, new_owner );
320    _MRSP_Add_state( rival, MRSP_RIVAL_STATE_NEW_OWNER );
321  }
322
323  if ( !_Resource_Node_owns_resources( &executing->Resource_node ) ) {
324    _Scheduler_Thread_change_help_state( executing, SCHEDULER_HELP_YOURSELF );
325  }
326
327  return MRSP_SUCCESSFUL;
328}
329
330RTEMS_INLINE_ROUTINE MRSP_Status _MRSP_Destroy( MRSP_Control *mrsp )
331{
332  if ( _Resource_Get_owner( &mrsp->Resource ) != NULL ) {
333    return MRSP_RESOUCE_IN_USE;
334  }
335
336  _Workspace_Free( mrsp->ceiling_priorities );
337
338  return MRSP_SUCCESSFUL;
339}
340
341/** @} */
342
343#ifdef __cplusplus
344}
345#endif /* __cplusplus */
346
347#endif /* RTEMS_SMP */
348
349#endif /* _RTEMS_SCORE_MRSPIMPL_H */
Note: See TracBrowser for help on using the repository browser.