source: rtems/cpukit/score/include/rtems/score/mrspimpl.h @ 913864c

5
Last change on this file since 913864c was 913864c, checked in by Sebastian Huber <sebastian.huber@…>, on 10/13/16 at 06:57:29

score: Use scheduler instance specific locks

Update #2556.

  • Property mode set to 100644
File size: 13.6 KB
Line 
1/*
2 * Copyright (c) 2014, 2016 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/status.h>
27#include <rtems/score/threadqimpl.h>
28#include <rtems/score/watchdogimpl.h>
29#include <rtems/score/wkspace.h>
30
31#ifdef __cplusplus
32extern "C" {
33#endif /* __cplusplus */
34
35/**
36 * @addtogroup ScoreMRSP
37 *
38 * @{
39 */
40
41/**
42 * @brief Internal state used for MRSP_Rival::status to indicate that this
43 * rival waits for resource ownership.
44 */
45#define MRSP_WAIT_FOR_OWNERSHIP STATUS_MINUS_ONE
46
47/*
48 * FIXME: Operations with the resource dependency tree are protected by the
49 * global scheduler lock.  Since the scheduler lock should be scheduler
50 * instance specific in the future this will only work temporarily.  A more
51 * sophisticated locking strategy is necessary.
52 */
53
54RTEMS_INLINE_ROUTINE void _MRSP_Giant_acquire( ISR_lock_Context *lock_context )
55{
56  /* FIXME: MrsP protocol implementation will be reworked soon */
57}
58
59RTEMS_INLINE_ROUTINE void _MRSP_Giant_release( ISR_lock_Context *lock_context )
60{
61  /* FIXME: MrsP protocol implementation will be reworked soon */
62}
63
64RTEMS_INLINE_ROUTINE void _MRSP_Acquire_critical(
65  MRSP_Control         *mrsp,
66  Thread_queue_Context *queue_context
67)
68{
69  _Thread_queue_Acquire_critical( &mrsp->Wait_queue, queue_context );
70}
71
72RTEMS_INLINE_ROUTINE void _MRSP_Release(
73  MRSP_Control         *mrsp,
74  Thread_queue_Context *queue_context
75)
76{
77  _Thread_queue_Release( &mrsp->Wait_queue, queue_context );
78}
79
80RTEMS_INLINE_ROUTINE Priority_Control _MRSP_Get_priority(
81  const MRSP_Control      *mrsp,
82  const Scheduler_Control *scheduler
83)
84{
85  uint32_t scheduler_index;
86
87  scheduler_index = _Scheduler_Get_index( scheduler );
88  return mrsp->ceiling_priorities[ scheduler_index ];
89}
90
91RTEMS_INLINE_ROUTINE void _MRSP_Set_priority(
92  MRSP_Control            *mrsp,
93  const Scheduler_Control *scheduler,
94  Priority_Control         new_priority
95)
96{
97  uint32_t scheduler_index;
98
99  scheduler_index = _Scheduler_Get_index( scheduler );
100  mrsp->ceiling_priorities[ scheduler_index ] = new_priority;
101}
102
103RTEMS_INLINE_ROUTINE Status_Control _MRSP_Raise_priority(
104  MRSP_Control         *mrsp,
105  Thread_Control       *thread,
106  Priority_Node        *priority_node,
107  Thread_queue_Context *queue_context
108)
109{
110  Status_Control           status;
111  ISR_lock_Context         lock_context;
112  const Scheduler_Control *scheduler;
113  Priority_Control         ceiling_priority;
114  Scheduler_Node          *own_node;
115
116  _Thread_queue_Context_clear_priority_updates( queue_context );
117  _Thread_Wait_acquire_default_critical( thread, &lock_context );
118
119  scheduler = _Scheduler_Get_own( thread );
120  own_node = _Thread_Scheduler_get_own_node( thread );
121  ceiling_priority = _MRSP_Get_priority( mrsp, scheduler );
122
123  if ( ceiling_priority <= own_node->Wait.Priority.Node.priority ) {
124    _Priority_Node_initialize( priority_node, ceiling_priority );
125    _Thread_Priority_add( thread, priority_node, queue_context );
126    status = STATUS_SUCCESSFUL;
127  } else {
128    status = STATUS_MUTEX_CEILING_VIOLATED;
129  }
130
131  _Thread_Wait_release_default_critical( thread, &lock_context );
132  return status;
133}
134
135RTEMS_INLINE_ROUTINE void _MRSP_Remove_priority(
136  Thread_Control       *thread,
137  Priority_Node        *priority_node,
138  Thread_queue_Context *queue_context
139)
140{
141  ISR_lock_Context  lock_context;
142
143  _Thread_queue_Context_clear_priority_updates( queue_context );
144  _Thread_Wait_acquire_default_critical( thread, &lock_context );
145  _Thread_Priority_remove( thread, priority_node, queue_context );
146  _Thread_Wait_release_default_critical( thread, &lock_context );
147}
148
149RTEMS_INLINE_ROUTINE void _MRSP_Replace_priority(
150  MRSP_Control   *mrsp,
151  Thread_Control *thread,
152  MRSP_Rival     *rival
153)
154{
155  ISR_lock_Context lock_context;
156
157  _Thread_Wait_acquire_default_critical( thread, &lock_context );
158  _Thread_Priority_replace(
159    thread,
160    &rival->Ceiling_priority,
161    &mrsp->Ceiling_priority
162  );
163  _Thread_Wait_release_default_critical( thread, &lock_context );
164}
165
166RTEMS_INLINE_ROUTINE Status_Control _MRSP_Claim_ownership(
167  MRSP_Control         *mrsp,
168  Thread_Control       *new_owner,
169  Thread_queue_Context *queue_context
170)
171{
172  Status_Control   status;
173  Per_CPU_Control *cpu_self;
174
175  status = _MRSP_Raise_priority(
176    mrsp,
177    new_owner,
178    &mrsp->Ceiling_priority,
179    queue_context
180  );
181
182  if ( status != STATUS_SUCCESSFUL ) {
183    _MRSP_Release( mrsp, queue_context );
184    return status;
185  }
186
187  _Resource_Node_add_resource( &new_owner->Resource_node, &mrsp->Resource );
188  _Resource_Set_owner( &mrsp->Resource, &new_owner->Resource_node );
189  _Scheduler_Thread_change_help_state( new_owner, SCHEDULER_HELP_ACTIVE_OWNER );
190
191  cpu_self = _Thread_Dispatch_disable_critical(
192    &queue_context->Lock_context.Lock_context
193  );
194  _MRSP_Release( mrsp, queue_context );
195
196  _Thread_Priority_update( queue_context );
197
198  _Thread_Dispatch_enable( cpu_self );
199  return STATUS_SUCCESSFUL;
200}
201
202RTEMS_INLINE_ROUTINE Status_Control _MRSP_Initialize(
203  MRSP_Control            *mrsp,
204  const Scheduler_Control *scheduler,
205  Priority_Control         ceiling_priority,
206  Thread_Control          *executing,
207  bool                     initially_locked
208)
209{
210  uint32_t scheduler_count = _Scheduler_Count;
211  uint32_t i;
212
213  if ( initially_locked ) {
214    return STATUS_INVALID_NUMBER;
215  }
216
217  mrsp->ceiling_priorities = _Workspace_Allocate(
218    sizeof( *mrsp->ceiling_priorities ) * scheduler_count
219  );
220  if ( mrsp->ceiling_priorities == NULL ) {
221    return STATUS_NO_MEMORY;
222  }
223
224  for ( i = 0 ; i < scheduler_count ; ++i ) {
225    const Scheduler_Control *scheduler_of_cpu;
226
227    scheduler_of_cpu = _Scheduler_Get_by_CPU_index( i );
228
229    if ( scheduler != scheduler_of_cpu ) {
230      mrsp->ceiling_priorities[ i ] =
231        _Scheduler_Map_priority( scheduler_of_cpu, 0 );
232    } else {
233      mrsp->ceiling_priorities[ i ] = ceiling_priority;
234    }
235  }
236
237  _Resource_Initialize( &mrsp->Resource );
238  _Chain_Initialize_empty( &mrsp->Rivals );
239  _Thread_queue_Initialize( &mrsp->Wait_queue );
240
241  return STATUS_SUCCESSFUL;
242}
243
244RTEMS_INLINE_ROUTINE void _MRSP_Timeout( Watchdog_Control *watchdog )
245{
246  MRSP_Rival           *rival;
247  MRSP_Control         *mrsp;
248  Thread_Control       *thread;
249  Thread_queue_Context  queue_context;
250
251  rival = RTEMS_CONTAINER_OF( watchdog, MRSP_Rival, Watchdog );
252  mrsp = rival->resource;
253  thread = rival->thread;
254
255  _Thread_queue_Context_initialize( &queue_context );
256  _ISR_lock_ISR_disable( &queue_context.Lock_context.Lock_context );
257  _MRSP_Acquire_critical( mrsp, &queue_context );
258
259  if ( rival->status == MRSP_WAIT_FOR_OWNERSHIP ) {
260    ISR_lock_Context giant_lock_context;
261
262    _MRSP_Remove_priority( thread, &rival->Ceiling_priority, &queue_context );
263
264    _MRSP_Giant_acquire( &giant_lock_context );
265
266    _Chain_Extract_unprotected( &rival->Node );
267    _Resource_Node_extract( &thread->Resource_node );
268    _Resource_Node_set_dependency( &thread->Resource_node, NULL );
269    _Scheduler_Thread_change_help_state( thread, rival->initial_help_state );
270    _Scheduler_Thread_change_resource_root( thread, thread );
271
272    _MRSP_Giant_release( &giant_lock_context );
273
274    rival->status = STATUS_TIMEOUT;
275
276    _MRSP_Release( mrsp, &queue_context );
277
278    _Thread_Priority_update( &queue_context );
279  } else {
280    _MRSP_Release( mrsp, &queue_context );
281  }
282}
283
284RTEMS_INLINE_ROUTINE Status_Control _MRSP_Wait_for_ownership(
285  MRSP_Control         *mrsp,
286  Resource_Node        *owner,
287  Thread_Control       *executing,
288  Thread_queue_Context *queue_context
289)
290{
291  Status_Control     status;
292  MRSP_Rival         rival;
293  Thread_Life_state  life_state;
294  Per_CPU_Control   *cpu_self;
295  ISR_lock_Context   giant_lock_context;
296  ISR_Level          level;
297  Watchdog_Interval  timeout;
298
299  _Assert( queue_context->timeout_discipline == WATCHDOG_RELATIVE );
300
301  status = _MRSP_Raise_priority(
302    mrsp,
303    executing,
304    &rival.Ceiling_priority,
305    queue_context
306  );
307
308  if ( status != STATUS_SUCCESSFUL ) {
309    _MRSP_Release( mrsp, queue_context );
310    return status;
311  }
312
313  rival.thread = executing;
314  rival.resource = mrsp;
315  _Chain_Initialize_node( &rival.Node );
316
317  _MRSP_Giant_acquire( &giant_lock_context );
318
319  rival.initial_help_state =
320    _Scheduler_Thread_change_help_state( executing, SCHEDULER_HELP_ACTIVE_RIVAL );
321  rival.status = MRSP_WAIT_FOR_OWNERSHIP;
322
323  _Chain_Initialize_node( &rival.Node );
324  _Chain_Append_unprotected( &mrsp->Rivals, &rival.Node );
325  _Resource_Add_rival( &mrsp->Resource, &executing->Resource_node );
326  _Resource_Node_set_dependency( &executing->Resource_node, &mrsp->Resource );
327  _Scheduler_Thread_change_resource_root(
328    executing,
329    THREAD_RESOURCE_NODE_TO_THREAD( _Resource_Node_get_root( owner ) )
330  );
331
332  _MRSP_Giant_release( &giant_lock_context );
333
334  cpu_self = _Thread_Dispatch_disable_critical(
335    &queue_context->Lock_context.Lock_context
336  );
337  _MRSP_Release( mrsp, queue_context );
338
339  _Thread_Priority_update( queue_context );
340
341  timeout = (Watchdog_Interval) queue_context->timeout;
342
343  if ( timeout > 0 ) {
344    _Watchdog_Preinitialize( &rival.Watchdog, cpu_self );
345    _Watchdog_Initialize( &rival.Watchdog, _MRSP_Timeout );
346    _ISR_Local_disable( level );
347    _Watchdog_Per_CPU_insert_relative( &rival.Watchdog, cpu_self, timeout );
348    _ISR_Local_enable( level );
349  }
350
351  life_state = _Thread_Set_life_protection( THREAD_LIFE_PROTECTED );
352  _Thread_Dispatch_enable( cpu_self );
353
354  _Assert( _Debug_Is_thread_dispatching_allowed() );
355
356  /* Wait for state change */
357  do {
358    status = rival.status;
359  } while ( status == MRSP_WAIT_FOR_OWNERSHIP );
360
361  _Thread_Set_life_protection( life_state );
362
363  if ( timeout > 0 ) {
364    _ISR_Local_disable( level );
365    _Watchdog_Per_CPU_remove(
366      &rival.Watchdog,
367      cpu_self,
368      &cpu_self->Watchdog.Header[ PER_CPU_WATCHDOG_RELATIVE ]
369    );
370    _ISR_Local_enable( level );
371  }
372
373  return status;
374}
375
376RTEMS_INLINE_ROUTINE Status_Control _MRSP_Seize(
377  MRSP_Control         *mrsp,
378  Thread_Control       *executing,
379  bool                  wait,
380  Thread_queue_Context *queue_context
381)
382{
383  Status_Control  status;
384  Resource_Node  *owner;
385
386  _MRSP_Acquire_critical( mrsp, queue_context );
387
388  owner = _Resource_Get_owner( &mrsp->Resource );
389
390  if ( owner == NULL ) {
391    status = _MRSP_Claim_ownership( mrsp, executing, queue_context );
392  } else if (
393    wait
394      && _Resource_Node_get_root( owner ) != &executing->Resource_node
395  ) {
396    status = _MRSP_Wait_for_ownership( mrsp, owner, executing, queue_context );
397  } else {
398    _MRSP_Release( mrsp, queue_context );
399    /* Not available, nested access or deadlock */
400    status = STATUS_UNAVAILABLE;
401  }
402
403  return status;
404}
405
406RTEMS_INLINE_ROUTINE Status_Control _MRSP_Surrender(
407  MRSP_Control         *mrsp,
408  Thread_Control       *executing,
409  Thread_queue_Context *queue_context
410)
411{
412  ISR_lock_Context  giant_lock_context;
413  Per_CPU_Control  *cpu_self;
414
415  if ( _Resource_Get_owner( &mrsp->Resource ) != &executing->Resource_node ) {
416    _ISR_lock_ISR_enable( &queue_context->Lock_context.Lock_context );
417    return STATUS_NOT_OWNER;
418  }
419
420  if (
421    !_Resource_Is_most_recently_obtained(
422      &mrsp->Resource,
423      &executing->Resource_node
424    )
425  ) {
426    _ISR_lock_ISR_enable( &queue_context->Lock_context.Lock_context );
427    return STATUS_RELEASE_ORDER_VIOLATION;
428  }
429
430  _MRSP_Acquire_critical( mrsp, queue_context );
431  _MRSP_Remove_priority( executing, &mrsp->Ceiling_priority, queue_context );
432  _MRSP_Giant_acquire( &giant_lock_context );
433
434  _Resource_Extract( &mrsp->Resource );
435
436  if ( _Chain_Is_empty( &mrsp->Rivals ) ) {
437    _Resource_Set_owner( &mrsp->Resource, NULL );
438  } else {
439    MRSP_Rival     *rival;
440    Thread_Control *new_owner;
441
442    rival = (MRSP_Rival *) _Chain_Get_first_unprotected( &mrsp->Rivals );
443
444    /*
445     * This must be inside the critical section since the status prevents a
446     * potential double extraction in _MRSP_Timeout().
447     */
448    rival->status = STATUS_SUCCESSFUL;
449
450    new_owner = rival->thread;
451
452    _MRSP_Replace_priority( mrsp, new_owner, rival );
453
454    _Resource_Node_extract( &new_owner->Resource_node );
455    _Resource_Node_set_dependency( &new_owner->Resource_node, NULL );
456    _Resource_Node_add_resource( &new_owner->Resource_node, &mrsp->Resource );
457    _Resource_Set_owner( &mrsp->Resource, &new_owner->Resource_node );
458    _Scheduler_Thread_change_help_state( new_owner, SCHEDULER_HELP_ACTIVE_OWNER );
459    _Scheduler_Thread_change_resource_root( new_owner, new_owner );
460  }
461
462  if ( !_Resource_Node_owns_resources( &executing->Resource_node ) ) {
463    _Scheduler_Thread_change_help_state( executing, SCHEDULER_HELP_YOURSELF );
464  }
465
466  _MRSP_Giant_release( &giant_lock_context );
467
468  cpu_self = _Thread_Dispatch_disable_critical(
469    &queue_context->Lock_context.Lock_context
470  );
471  _MRSP_Release( mrsp, queue_context );
472
473  _Thread_Priority_update( queue_context );
474
475  _Thread_Dispatch_enable( cpu_self );
476
477  return STATUS_SUCCESSFUL;
478}
479
480RTEMS_INLINE_ROUTINE Status_Control _MRSP_Can_destroy( MRSP_Control *mrsp )
481{
482  if ( _Resource_Get_owner( &mrsp->Resource ) != NULL ) {
483    return STATUS_RESOURCE_IN_USE;
484  }
485
486  return STATUS_SUCCESSFUL;
487}
488
489RTEMS_INLINE_ROUTINE void _MRSP_Destroy(
490  MRSP_Control         *mrsp,
491  Thread_queue_Context *queue_context
492)
493{
494  _MRSP_Release( mrsp, queue_context );
495  _Thread_queue_Destroy( &mrsp->Wait_queue );
496  _Workspace_Free( mrsp->ceiling_priorities );
497}
498
499/** @} */
500
501#ifdef __cplusplus
502}
503#endif /* __cplusplus */
504
505#endif /* RTEMS_SMP */
506
507#endif /* _RTEMS_SCORE_MRSPIMPL_H */
Note: See TracBrowser for help on using the repository browser.