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_RESOURCEIMPL_H |
---|
16 | #define _RTEMS_SCORE_RESOURCEIMPL_H |
---|
17 | |
---|
18 | #include <rtems/score/resource.h> |
---|
19 | #include <rtems/score/chainimpl.h> |
---|
20 | |
---|
21 | #ifdef __cplusplus |
---|
22 | extern "C" { |
---|
23 | #endif /* __cplusplus */ |
---|
24 | |
---|
25 | /** |
---|
26 | * @addtogroup ScoreResource |
---|
27 | * |
---|
28 | * @{ |
---|
29 | */ |
---|
30 | |
---|
31 | /** |
---|
32 | * @brief Visitor function for resource node iteration. |
---|
33 | * |
---|
34 | * The visitor is allowed to extract the node. |
---|
35 | * |
---|
36 | * @param[in] node The current resource node. |
---|
37 | * @param[in] arg The argument passed to _Resource_Iterate(). |
---|
38 | * |
---|
39 | * @retval true Stop the iteration. |
---|
40 | * @retval false Continue the iteration. |
---|
41 | */ |
---|
42 | typedef bool (*Resource_Node_visitor)( Resource_Node *node, void *arg ); |
---|
43 | |
---|
44 | /** |
---|
45 | * @brief Iterates over all nodes of a resource dependency tree. |
---|
46 | * |
---|
47 | * @param[in] top The top node to start the iteration. The visitor function is |
---|
48 | * not invoked for the top node. |
---|
49 | * @param[in] visitor The visitor function. |
---|
50 | * @param[in] arg The argument for the visitor function. |
---|
51 | */ |
---|
52 | void _Resource_Iterate( |
---|
53 | Resource_Node *top, |
---|
54 | Resource_Node_visitor visitor, |
---|
55 | void *arg |
---|
56 | ); |
---|
57 | |
---|
58 | RTEMS_INLINE_ROUTINE void _Resource_Node_initialize( Resource_Node *node ) |
---|
59 | { |
---|
60 | node->dependency = NULL; |
---|
61 | node->root = node; |
---|
62 | _Chain_Initialize_empty( &node->Resources ); |
---|
63 | } |
---|
64 | |
---|
65 | RTEMS_INLINE_ROUTINE void _Resource_Node_set_dependency( |
---|
66 | Resource_Node *node, |
---|
67 | Resource_Control *dependency |
---|
68 | ) |
---|
69 | { |
---|
70 | node->dependency = dependency; |
---|
71 | } |
---|
72 | |
---|
73 | RTEMS_INLINE_ROUTINE Resource_Node *_Resource_Node_get_root( |
---|
74 | const Resource_Node *node |
---|
75 | ) |
---|
76 | { |
---|
77 | return node->root; |
---|
78 | } |
---|
79 | |
---|
80 | RTEMS_INLINE_ROUTINE void _Resource_Node_set_root( |
---|
81 | Resource_Node *node, |
---|
82 | Resource_Node *root |
---|
83 | ) |
---|
84 | { |
---|
85 | node->root = root; |
---|
86 | } |
---|
87 | |
---|
88 | RTEMS_INLINE_ROUTINE bool _Resource_Node_owns_resources( const Resource_Node *node ) |
---|
89 | { |
---|
90 | return !_Chain_Is_empty( &node->Resources ); |
---|
91 | } |
---|
92 | |
---|
93 | RTEMS_INLINE_ROUTINE void _Resource_Node_add_resource( |
---|
94 | Resource_Node *node, |
---|
95 | Resource_Control *resource |
---|
96 | ) |
---|
97 | { |
---|
98 | _Chain_Prepend_unprotected( &node->Resources, &resource->Node ); |
---|
99 | } |
---|
100 | |
---|
101 | RTEMS_INLINE_ROUTINE void _Resource_Node_extract( Resource_Node *node ) |
---|
102 | { |
---|
103 | _Chain_Extract_unprotected( &node->Node ); |
---|
104 | } |
---|
105 | |
---|
106 | RTEMS_INLINE_ROUTINE void _Resource_Initialize( Resource_Control *resource ) |
---|
107 | { |
---|
108 | resource->owner = NULL; |
---|
109 | _Chain_Initialize_empty( &resource->Rivals ); |
---|
110 | } |
---|
111 | |
---|
112 | RTEMS_INLINE_ROUTINE void _Resource_Add_rival( |
---|
113 | Resource_Control *resource, |
---|
114 | Resource_Node *node |
---|
115 | ) |
---|
116 | { |
---|
117 | _Chain_Append_unprotected( &resource->Rivals, &node->Node ); |
---|
118 | } |
---|
119 | |
---|
120 | RTEMS_INLINE_ROUTINE void _Resource_Extract( Resource_Control *resource ) |
---|
121 | { |
---|
122 | _Chain_Extract_unprotected( &resource->Node ); |
---|
123 | } |
---|
124 | |
---|
125 | RTEMS_INLINE_ROUTINE Resource_Node *_Resource_Get_owner( |
---|
126 | const Resource_Control *resource |
---|
127 | ) |
---|
128 | { |
---|
129 | return resource->owner; |
---|
130 | } |
---|
131 | |
---|
132 | RTEMS_INLINE_ROUTINE void _Resource_Set_owner( |
---|
133 | Resource_Control *resource, |
---|
134 | Resource_Node *owner |
---|
135 | ) |
---|
136 | { |
---|
137 | resource->owner = owner; |
---|
138 | } |
---|
139 | |
---|
140 | /** |
---|
141 | * @brief Returns true if this is the most recently obtained resource of the |
---|
142 | * node, and false otherwise. |
---|
143 | * |
---|
144 | * Resources are organized in last in first out order (LIFO). |
---|
145 | * |
---|
146 | * @param[in] resource The resource in question. |
---|
147 | * @param[in] node The node that obtained the resource. |
---|
148 | */ |
---|
149 | RTEMS_INLINE_ROUTINE bool _Resource_Is_most_recently_obtained( |
---|
150 | const Resource_Control *resource, |
---|
151 | const Resource_Node *node |
---|
152 | ) |
---|
153 | { |
---|
154 | return &resource->Node == _Chain_Immutable_first( &node->Resources ); |
---|
155 | } |
---|
156 | |
---|
157 | /** @} */ |
---|
158 | |
---|
159 | #ifdef __cplusplus |
---|
160 | } |
---|
161 | #endif /* __cplusplus */ |
---|
162 | |
---|
163 | #endif /* _RTEMS_SCORE_RESOURCEIMPL_H */ |
---|