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_RESOURCE_H |
---|
16 | #define _RTEMS_SCORE_RESOURCE_H |
---|
17 | |
---|
18 | #include <rtems/score/basedefs.h> |
---|
19 | #include <rtems/score/chain.h> |
---|
20 | |
---|
21 | #ifdef __cplusplus |
---|
22 | extern "C" { |
---|
23 | #endif /* __cplusplus */ |
---|
24 | |
---|
25 | /** |
---|
26 | * @defgroup ScoreResource Resource Handler |
---|
27 | * |
---|
28 | * @ingroup Score |
---|
29 | * |
---|
30 | * @brief Support for resource dependency management. |
---|
31 | * |
---|
32 | * A resource is something that has at most one owner at a time and may have |
---|
33 | * multiple rivals in case an owner is present. The owner and rivals are |
---|
34 | * impersonated via resource nodes. A resource is represented via the resource |
---|
35 | * control structure. The resource controls and nodes are organized as trees. |
---|
36 | * It is possible to detect deadlocks via such a resource tree. The |
---|
37 | * _Resource_Iterate() function can be used to iterate through such a resource |
---|
38 | * tree starting at a top node. |
---|
39 | * |
---|
40 | * The following diagram shows an example resource tree with sixteen resource |
---|
41 | * nodes n0 up to n15 and sixteen resources r0 up to r15. The root of this |
---|
42 | * tree is n0. As a use case threads can be associated with resource nodes. |
---|
43 | * In this case a thread represented by node n0 owns resources r0, r1, r2, r3, |
---|
44 | * r6, r11 and r12 and is in the ready state. The threads represented by nodes |
---|
45 | * n1 up to n15 wait directly or indirectly via resources owned by n0 and are |
---|
46 | * in a blocked state. |
---|
47 | * |
---|
48 | * @dot |
---|
49 | * digraph { |
---|
50 | * n0 [style=filled, fillcolor=green]; |
---|
51 | * n0 -> r0; |
---|
52 | * subgraph { |
---|
53 | * rank=same; |
---|
54 | * n1 [style=filled, fillcolor=green]; |
---|
55 | * r0 -> n1; |
---|
56 | * n2 [style=filled, fillcolor=green]; |
---|
57 | * n1 -> n2; |
---|
58 | * n4 [style=filled, fillcolor=green]; |
---|
59 | * n2 -> n4; |
---|
60 | * n6 [style=filled, fillcolor=green]; |
---|
61 | * n4 -> n6; |
---|
62 | * n8 [style=filled, fillcolor=green]; |
---|
63 | * n6 -> n8; |
---|
64 | * n15 [style=filled, fillcolor=green]; |
---|
65 | * n8 -> n15; |
---|
66 | * } |
---|
67 | * n1 -> r5; |
---|
68 | * subgraph { |
---|
69 | * rank=same; |
---|
70 | * n3 [style=filled, fillcolor=green]; |
---|
71 | * r5 -> n3; |
---|
72 | * n12 [style=filled, fillcolor=green]; |
---|
73 | * n3 -> n12; |
---|
74 | * } |
---|
75 | * n3 -> r10; |
---|
76 | * r10 -> r13; |
---|
77 | * r13 -> r15; |
---|
78 | * subgraph { |
---|
79 | * rank=same; |
---|
80 | * n10 [style=filled, fillcolor=green]; |
---|
81 | * r15 -> n10; |
---|
82 | * } |
---|
83 | * r5 -> r7; |
---|
84 | * subgraph { |
---|
85 | * rank=same; |
---|
86 | * n11 [style=filled, fillcolor=green]; |
---|
87 | * r7 -> n11; |
---|
88 | * n14 [style=filled, fillcolor=green]; |
---|
89 | * n11 -> n14; |
---|
90 | * } |
---|
91 | * n14 -> r4; |
---|
92 | * r7 -> r8; |
---|
93 | * subgraph { |
---|
94 | * rank=same; |
---|
95 | * n13 [style=filled, fillcolor=green]; |
---|
96 | * r8 -> n13; |
---|
97 | * } |
---|
98 | * r8 -> r9; |
---|
99 | * n8 -> r14; |
---|
100 | * r0 -> r1; |
---|
101 | * subgraph { |
---|
102 | * rank=same; |
---|
103 | * n7 [style=filled, fillcolor=green]; |
---|
104 | * r1 -> n7; |
---|
105 | * } |
---|
106 | * r1 -> r2; |
---|
107 | * r2 -> r3; |
---|
108 | * r3 -> r6; |
---|
109 | * r6 -> r11; |
---|
110 | * r11 -> r12; |
---|
111 | * subgraph { |
---|
112 | * rank=same; |
---|
113 | * n5 [style=filled, fillcolor=green]; |
---|
114 | * r12 -> n5; |
---|
115 | * n9 [style=filled, fillcolor=green]; |
---|
116 | * n5 -> n9; |
---|
117 | * } |
---|
118 | * } |
---|
119 | * @enddot |
---|
120 | * |
---|
121 | * The following example illustrates a deadlock situation. The root of the |
---|
122 | * tree tries to get ownership of a resource owned by one of its children. |
---|
123 | * |
---|
124 | * @dot |
---|
125 | * digraph { |
---|
126 | * n0 [style=filled, fillcolor=green]; |
---|
127 | * n0 -> r0; |
---|
128 | * subgraph { |
---|
129 | * rank=same; |
---|
130 | * n1 [style=filled, fillcolor=green]; |
---|
131 | * r0 -> n1; |
---|
132 | * } |
---|
133 | * n1 -> r1; |
---|
134 | * n0 -> r1 [label=deadlock, style=dotted]; |
---|
135 | * } |
---|
136 | * @enddot |
---|
137 | * |
---|
138 | * @{ |
---|
139 | */ |
---|
140 | |
---|
141 | typedef struct Resource_Node Resource_Node; |
---|
142 | |
---|
143 | typedef struct Resource_Control Resource_Control; |
---|
144 | |
---|
145 | /** |
---|
146 | * @brief Resource node to reflect ownership of resources and a dependency on a |
---|
147 | * resource. |
---|
148 | */ |
---|
149 | struct Resource_Node { |
---|
150 | /** |
---|
151 | * @brief Node to build a chain of rivals depending on a resource. |
---|
152 | * |
---|
153 | * @see Resource_Control::Rivals. |
---|
154 | */ |
---|
155 | Chain_Node Node; |
---|
156 | |
---|
157 | /** |
---|
158 | * @brief A chain of resources owned by this node. |
---|
159 | * |
---|
160 | * @see Resource_Control::Node. |
---|
161 | */ |
---|
162 | Chain_Control Resources; |
---|
163 | |
---|
164 | /** |
---|
165 | * @brief Reference to a resource in case this node has to wait for ownership |
---|
166 | * of this resource. |
---|
167 | * |
---|
168 | * It is @c NULL in case this node has no open resource dependency. |
---|
169 | */ |
---|
170 | Resource_Control *dependency; |
---|
171 | |
---|
172 | /** |
---|
173 | * @brief Reference to the root of the resource tree. |
---|
174 | * |
---|
175 | * The root references itself. |
---|
176 | */ |
---|
177 | Resource_Node *root; |
---|
178 | }; |
---|
179 | |
---|
180 | /** |
---|
181 | * @brief Resource control to manage ownership and rival nodes depending on a |
---|
182 | * resource. |
---|
183 | */ |
---|
184 | struct Resource_Control { |
---|
185 | /** |
---|
186 | * @brief Node to build a chain of resources owned by a resource node. |
---|
187 | * |
---|
188 | * @see Resource_Node::Resources. |
---|
189 | */ |
---|
190 | Chain_Node Node; |
---|
191 | |
---|
192 | /** |
---|
193 | * @brief A chain of rivals waiting for resource ownership. |
---|
194 | * |
---|
195 | * @see Resource_Node::Node. |
---|
196 | */ |
---|
197 | Chain_Control Rivals; |
---|
198 | |
---|
199 | /** |
---|
200 | * @brief The owner of this resource. |
---|
201 | */ |
---|
202 | Resource_Node *owner; |
---|
203 | }; |
---|
204 | |
---|
205 | /** @} */ |
---|
206 | |
---|
207 | #ifdef __cplusplus |
---|
208 | } |
---|
209 | #endif /* __cplusplus */ |
---|
210 | |
---|
211 | #endif /* _RTEMS_SCORE_RESOURCE_H */ |
---|