source: rtems/cpukit/include/rtems/score/mrspimpl.h @ 5803f37

5
Last change on this file since 5803f37 was 0b1bfab, checked in by Andreas Dachsberger <andreas.dachsberger@…>, on 04/10/19 at 07:12:01

doxygen: score: adjust doc in mrspimpl.h to doxygen guidelines

Update #3706.

  • Property mode set to 100644
File size: 14.4 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup RTEMSScoreMRSP
5 *
6 * @brief Definitions for Multiprocessor Resource Sharing Protocol (MrsP) Implementation.
7 */
8
9/*
10 * Copyright (c) 2014, 2016 embedded brains GmbH.  All rights reserved.
11 *
12 *  embedded brains GmbH
13 *  Dornierstr. 4
14 *  82178 Puchheim
15 *  Germany
16 *  <rtems@embedded-brains.de>
17 *
18 * The license and distribution terms for this file may be
19 * found in the file LICENSE in this distribution or at
20 * http://www.rtems.org/license/LICENSE.
21 */
22
23#ifndef _RTEMS_SCORE_MRSPIMPL_H
24#define _RTEMS_SCORE_MRSPIMPL_H
25
26#include <rtems/score/mrsp.h>
27
28#if defined(RTEMS_SMP)
29
30#include <rtems/score/assert.h>
31#include <rtems/score/status.h>
32#include <rtems/score/threadqimpl.h>
33#include <rtems/score/watchdogimpl.h>
34#include <rtems/score/wkspace.h>
35
36#ifdef __cplusplus
37extern "C" {
38#endif /* __cplusplus */
39
40/**
41 * @addtogroup RTEMSScoreMRSP
42 *
43 * @{
44 */
45
46#define MRSP_TQ_OPERATIONS &_Thread_queue_Operations_priority_inherit
47
48/**
49 * @brief Acquires critical accordingt to MrsP.
50 *
51 * @param mrsp The MrsP control for the operation.
52 * @param queue_context The thread queue context.
53 */
54RTEMS_INLINE_ROUTINE void _MRSP_Acquire_critical(
55  MRSP_Control         *mrsp,
56  Thread_queue_Context *queue_context
57)
58{
59  _Thread_queue_Acquire_critical( &mrsp->Wait_queue, queue_context );
60}
61
62/**
63 * @brief Releases according to MrsP.
64 *
65 * @param mrsp The MrsP control for the operation.
66 * @param queue_context The thread queue context.
67 */
68RTEMS_INLINE_ROUTINE void _MRSP_Release(
69  MRSP_Control         *mrsp,
70  Thread_queue_Context *queue_context
71)
72{
73  _Thread_queue_Release( &mrsp->Wait_queue, queue_context );
74}
75
76/**
77 * @brief Gets owner of the MrsP control.
78 *
79 * @param mrsp The MrsP control to get the owner from.
80 *
81 * @return The owner of the Mrsp control.
82 */
83RTEMS_INLINE_ROUTINE Thread_Control *_MRSP_Get_owner(
84  const MRSP_Control *mrsp
85)
86{
87  return mrsp->Wait_queue.Queue.owner;
88}
89
90/**
91 * @brief Sets owner of the MrsP control.
92 *
93 * @param[out] mrsp The MrsP control to set the owner of.
94 * @param owner The desired new owner for @a mrsp.
95 */
96RTEMS_INLINE_ROUTINE void _MRSP_Set_owner(
97  MRSP_Control   *mrsp,
98  Thread_Control *owner
99)
100{
101  mrsp->Wait_queue.Queue.owner = owner;
102}
103
104/**
105 * @brief Gets priority of the MrsP control.
106 *
107 * @param mrsp The mrsp to get the priority from.
108 * @param scheduler The corresponding scheduler.
109 *
110 * @return The priority of the MrsP control.
111 */
112RTEMS_INLINE_ROUTINE Priority_Control _MRSP_Get_priority(
113  const MRSP_Control      *mrsp,
114  const Scheduler_Control *scheduler
115)
116{
117  uint32_t scheduler_index;
118
119  scheduler_index = _Scheduler_Get_index( scheduler );
120  return mrsp->ceiling_priorities[ scheduler_index ];
121}
122
123/**
124 * @brief Sets priority of the MrsP control
125 *
126 * @param[out] mrsp The MrsP control to set the priority of.
127 * @param schedulger The corresponding scheduler.
128 * @param new_priority The new priority for the MrsP control
129 */
130RTEMS_INLINE_ROUTINE void _MRSP_Set_priority(
131  MRSP_Control            *mrsp,
132  const Scheduler_Control *scheduler,
133  Priority_Control         new_priority
134)
135{
136  uint32_t scheduler_index;
137
138  scheduler_index = _Scheduler_Get_index( scheduler );
139  mrsp->ceiling_priorities[ scheduler_index ] = new_priority;
140}
141
142/**
143 * @brief Adds the priority to the given thread.
144 *
145 * @param mrsp The MrsP control for the operation.
146 * @param[in, out] thread The thread to add the priority node to.
147 * @param[out] priority_node The priority node to initialize and add to
148 *      the thread.
149 * @param queue_context The thread queue context.
150 *
151 * @retval STATUS_SUCCESSFUL The operation succeeded.
152 * @retval STATUS_MUTEX_CEILING_VIOLATED The wait priority of the thread
153 *      exceeds the ceiling priority.
154 */
155RTEMS_INLINE_ROUTINE Status_Control _MRSP_Raise_priority(
156  MRSP_Control         *mrsp,
157  Thread_Control       *thread,
158  Priority_Node        *priority_node,
159  Thread_queue_Context *queue_context
160)
161{
162  Status_Control           status;
163  ISR_lock_Context         lock_context;
164  const Scheduler_Control *scheduler;
165  Priority_Control         ceiling_priority;
166  Scheduler_Node          *scheduler_node;
167
168  _Thread_queue_Context_clear_priority_updates( queue_context );
169  _Thread_Wait_acquire_default_critical( thread, &lock_context );
170
171  scheduler = _Thread_Scheduler_get_home( thread );
172  scheduler_node = _Thread_Scheduler_get_home_node( thread );
173  ceiling_priority = _MRSP_Get_priority( mrsp, scheduler );
174
175  if (
176    ceiling_priority
177      <= _Priority_Get_priority( &scheduler_node->Wait.Priority )
178  ) {
179    _Priority_Node_initialize( priority_node, ceiling_priority );
180    _Thread_Priority_add( thread, priority_node, queue_context );
181    status = STATUS_SUCCESSFUL;
182  } else {
183    status = STATUS_MUTEX_CEILING_VIOLATED;
184  }
185
186  _Thread_Wait_release_default_critical( thread, &lock_context );
187  return status;
188}
189
190/**
191 * @brief Removes the priority from the given thread.
192 *
193 * @param[in, out] The thread to remove the priority from.
194 * @param priority_node The priority node to remove from the thread
195 * @param queue_context The thread queue context.
196 */
197RTEMS_INLINE_ROUTINE void _MRSP_Remove_priority(
198  Thread_Control       *thread,
199  Priority_Node        *priority_node,
200  Thread_queue_Context *queue_context
201)
202{
203  ISR_lock_Context lock_context;
204
205  _Thread_queue_Context_clear_priority_updates( queue_context );
206  _Thread_Wait_acquire_default_critical( thread, &lock_context );
207  _Thread_Priority_remove( thread, priority_node, queue_context );
208  _Thread_Wait_release_default_critical( thread, &lock_context );
209}
210
211/**
212 * @brief Replaces the given priority node with the ceiling priority of
213 *      the MrsP control.
214 *
215 * @param mrsp The mrsp control for the operation.
216 * @param[out] thread The thread to replace the priorities.
217 * @param ceiling_priority The node to be replaced.
218 */
219RTEMS_INLINE_ROUTINE void _MRSP_Replace_priority(
220  MRSP_Control   *mrsp,
221  Thread_Control *thread,
222  Priority_Node  *ceiling_priority
223)
224{
225  ISR_lock_Context lock_context;
226
227  _Thread_Wait_acquire_default( thread, &lock_context );
228  _Thread_Priority_replace(
229    thread,
230    ceiling_priority,
231    &mrsp->Ceiling_priority
232  );
233  _Thread_Wait_release_default( thread, &lock_context );
234}
235
236/**
237 * @brief Claims ownership of the MrsP control.
238 *
239 * @param mrsp The MrsP control to claim the ownership of.
240 * @param[in, out] executing The currently executing thread.
241 * @param queue_context The thread queue context.
242 *
243 * @retval STATUS_SUCCESSFUL The operation succeeded.
244 * @retval STATUS_MUTEX_CEILING_VIOLATED The wait priority of the executing
245 *      thread exceeds the ceiling priority.
246 */
247RTEMS_INLINE_ROUTINE Status_Control _MRSP_Claim_ownership(
248  MRSP_Control         *mrsp,
249  Thread_Control       *executing,
250  Thread_queue_Context *queue_context
251)
252{
253  Status_Control   status;
254  Per_CPU_Control *cpu_self;
255
256  status = _MRSP_Raise_priority(
257    mrsp,
258    executing,
259    &mrsp->Ceiling_priority,
260    queue_context
261  );
262
263  if ( status != STATUS_SUCCESSFUL ) {
264    _MRSP_Release( mrsp, queue_context );
265    return status;
266  }
267
268  _MRSP_Set_owner( mrsp, executing );
269  cpu_self = _Thread_queue_Dispatch_disable( queue_context );
270  _MRSP_Release( mrsp, queue_context );
271  _Thread_Priority_and_sticky_update( executing, 1 );
272  _Thread_Dispatch_enable( cpu_self );
273  return STATUS_SUCCESSFUL;
274}
275
276/**
277 * @brief Initializes a MrsP control.
278 *
279 * @param[out] mrsp The MrsP control that is initialized.
280 * @param scheduler The scheduler for the operation.
281 * @param ceiling_priority
282 * @param executing The currently executing thread.  Ignored in this method.
283 * @param initially_locked Indicates whether the MrsP control shall be initally
284 *      locked. If it is initially locked, this method returns STATUS_INVALID_NUMBER.
285 *
286 * @retval STATUS_SUCCESSFUL The operation succeeded.
287 * @retval STATUS_INVALID_NUMBER The MrsP control is initially locked.
288 * @retval STATUS_NO_MEMORY There is not enough memory to allocate.
289 */
290RTEMS_INLINE_ROUTINE Status_Control _MRSP_Initialize(
291  MRSP_Control            *mrsp,
292  const Scheduler_Control *scheduler,
293  Priority_Control         ceiling_priority,
294  Thread_Control          *executing,
295  bool                     initially_locked
296)
297{
298  uint32_t scheduler_count = _Scheduler_Count;
299  uint32_t i;
300
301  if ( initially_locked ) {
302    return STATUS_INVALID_NUMBER;
303  }
304
305  mrsp->ceiling_priorities = _Workspace_Allocate(
306    sizeof( *mrsp->ceiling_priorities ) * scheduler_count
307  );
308  if ( mrsp->ceiling_priorities == NULL ) {
309    return STATUS_NO_MEMORY;
310  }
311
312  for ( i = 0 ; i < scheduler_count ; ++i ) {
313    const Scheduler_Control *scheduler_of_index;
314
315    scheduler_of_index = &_Scheduler_Table[ i ];
316
317    if ( scheduler != scheduler_of_index ) {
318      mrsp->ceiling_priorities[ i ] =
319        _Scheduler_Map_priority( scheduler_of_index, 0 );
320    } else {
321      mrsp->ceiling_priorities[ i ] = ceiling_priority;
322    }
323  }
324
325  _Thread_queue_Object_initialize( &mrsp->Wait_queue );
326  return STATUS_SUCCESSFUL;
327}
328
329/**
330 * @brief Waits for the ownership of the MrsP control.
331 *
332 * @param[in, out] mrsp The MrsP control to get the ownership of.
333 * @param[in, out] executing The currently executing thread.
334 * @param queue_context the thread queue context.
335 *
336 * @retval STATUS_SUCCESSFUL The operation succeeded.
337 * @retval STATUS_MUTEX_CEILING_VIOLATED The wait priority of the
338 *      currently executing thread exceeds the ceiling priority.
339 * @retval STATUS_DEADLOCK A deadlock occured.
340 * @retval STATUS_TIMEOUT A timeout occured.
341 */
342RTEMS_INLINE_ROUTINE Status_Control _MRSP_Wait_for_ownership(
343  MRSP_Control         *mrsp,
344  Thread_Control       *executing,
345  Thread_queue_Context *queue_context
346)
347{
348  Status_Control status;
349  Priority_Node  ceiling_priority;
350
351  status = _MRSP_Raise_priority(
352    mrsp,
353    executing,
354    &ceiling_priority,
355    queue_context
356  );
357
358  if ( status != STATUS_SUCCESSFUL ) {
359    _MRSP_Release( mrsp, queue_context );
360    return status;
361  }
362
363  _Thread_queue_Context_set_deadlock_callout(
364    queue_context,
365    _Thread_queue_Deadlock_status
366  );
367  status = _Thread_queue_Enqueue_sticky(
368    &mrsp->Wait_queue.Queue,
369    MRSP_TQ_OPERATIONS,
370    executing,
371    queue_context
372  );
373
374  if ( status == STATUS_SUCCESSFUL ) {
375    _MRSP_Replace_priority( mrsp, executing, &ceiling_priority );
376  } else {
377    Thread_queue_Context  queue_context;
378    Per_CPU_Control      *cpu_self;
379    int                   sticky_level_change;
380
381    if ( status != STATUS_DEADLOCK ) {
382      sticky_level_change = -1;
383    } else {
384      sticky_level_change = 0;
385    }
386
387    _ISR_lock_ISR_disable( &queue_context.Lock_context.Lock_context );
388    _MRSP_Remove_priority( executing, &ceiling_priority, &queue_context );
389    cpu_self = _Thread_Dispatch_disable_critical(
390      &queue_context.Lock_context.Lock_context
391    );
392    _ISR_lock_ISR_enable( &queue_context.Lock_context.Lock_context );
393    _Thread_Priority_and_sticky_update( executing, sticky_level_change );
394    _Thread_Dispatch_enable( cpu_self );
395  }
396
397  return status;
398}
399
400/**
401 * @brief Seizes the MrsP control.
402 *
403 * @param[in, out] mrsp The MrsP control to seize the control of.
404 * @param[in, out] executing The currently executing thread.
405 * @param wait Indicates whether the calling thread is willing to wait.
406 * @param queue_context The thread queue context.
407 *
408 * @retval STATUS_SUCCESSFUL The operation succeeded.
409 * @retval STATUS_MUTEX_CEILING_VIOLATED The wait priority of the executing
410 *      thread exceeds the ceiling priority.
411 * @retval STATUS_UNAVAILABLE The executing thread is already the owner of
412 *      the MrsP control.  Seizing it is not possible.
413 */
414RTEMS_INLINE_ROUTINE Status_Control _MRSP_Seize(
415  MRSP_Control         *mrsp,
416  Thread_Control       *executing,
417  bool                  wait,
418  Thread_queue_Context *queue_context
419)
420{
421  Status_Control  status;
422  Thread_Control *owner;
423
424  _MRSP_Acquire_critical( mrsp, queue_context );
425
426  owner = _MRSP_Get_owner( mrsp );
427
428  if ( owner == NULL ) {
429    status = _MRSP_Claim_ownership( mrsp, executing, queue_context );
430  } else if ( owner == executing ) {
431    _MRSP_Release( mrsp, queue_context );
432    status = STATUS_UNAVAILABLE;
433  } else if ( wait ) {
434    status = _MRSP_Wait_for_ownership( mrsp, executing, queue_context );
435  } else {
436    _MRSP_Release( mrsp, queue_context );
437    status = STATUS_UNAVAILABLE;
438  }
439
440  return status;
441}
442
443/**
444 * @brief Surrenders the MrsP control.
445 *
446 * @param[in, out] mrsp The MrsP control to surrender the control of.
447 * @param[in, out] executing The currently executing thread.
448 * @param queue_context The thread queue context.
449 *
450 * @retval STATUS_SUCCESSFUL The operation succeeded.
451 * @retval STATUS_NOT_OWNER The executing thread does not own the MrsP control.
452 */
453RTEMS_INLINE_ROUTINE Status_Control _MRSP_Surrender(
454  MRSP_Control         *mrsp,
455  Thread_Control       *executing,
456  Thread_queue_Context *queue_context
457)
458{
459  Thread_queue_Heads *heads;
460
461  if ( _MRSP_Get_owner( mrsp ) != executing ) {
462    _ISR_lock_ISR_enable( &queue_context->Lock_context.Lock_context );
463    return STATUS_NOT_OWNER;
464  }
465
466  _MRSP_Acquire_critical( mrsp, queue_context );
467
468  _MRSP_Set_owner( mrsp, NULL );
469  _MRSP_Remove_priority( executing, &mrsp->Ceiling_priority, queue_context );
470
471  heads = mrsp->Wait_queue.Queue.heads;
472
473  if ( heads == NULL ) {
474    Per_CPU_Control *cpu_self;
475
476    cpu_self = _Thread_Dispatch_disable_critical(
477      &queue_context->Lock_context.Lock_context
478    );
479    _MRSP_Release( mrsp, queue_context );
480    _Thread_Priority_and_sticky_update( executing, -1 );
481    _Thread_Dispatch_enable( cpu_self );
482    return STATUS_SUCCESSFUL;
483  }
484
485  _Thread_queue_Surrender_sticky(
486    &mrsp->Wait_queue.Queue,
487    heads,
488    executing,
489    queue_context,
490    MRSP_TQ_OPERATIONS
491  );
492  return STATUS_SUCCESSFUL;
493}
494
495/**
496 * @brief Checks if the MrsP control can be destroyed.
497 *
498 * @param mrsp The MrsP control for the operation.
499 *
500 * @retval STATUS_SUCCESSFUL The MrsP is currently not used
501 *      and can be destroyed.
502 * @retval STATUS_RESOURCE_IN_USE The MrsP control is in use,
503 *      it cannot be destroyed.
504 */
505RTEMS_INLINE_ROUTINE Status_Control _MRSP_Can_destroy( MRSP_Control *mrsp )
506{
507  if ( _MRSP_Get_owner( mrsp ) != NULL ) {
508    return STATUS_RESOURCE_IN_USE;
509  }
510
511  return STATUS_SUCCESSFUL;
512}
513
514/**
515 * @brief Destroys the MrsP control
516 *
517 * @param[in, out] The mrsp that is about to be destroyed.
518 * @param queue_context The thread queue context.
519 */
520RTEMS_INLINE_ROUTINE void _MRSP_Destroy(
521  MRSP_Control         *mrsp,
522  Thread_queue_Context *queue_context
523)
524{
525  _MRSP_Release( mrsp, queue_context );
526  _Thread_queue_Destroy( &mrsp->Wait_queue );
527  _Workspace_Free( mrsp->ceiling_priorities );
528}
529
530/** @} */
531
532#ifdef __cplusplus
533}
534#endif /* __cplusplus */
535
536#endif /* RTEMS_SMP */
537
538#endif /* _RTEMS_SCORE_MRSPIMPL_H */
Note: See TracBrowser for help on using the repository browser.