1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
#include <climits>
#include "ace/RB_Tree.h"
/*
Much of this is credited to "Efficient Distrubuted Deadlock
Avoidance with Liveness Guarentees" by Sanchez, Sipma, and Manna,
EMSOFT 2006
*/
struct AnnotationNode {
AnnotationNode()
:count(0), size(0), larger(0), larger_me(0), larger_left(INT_MAX), larger_right(INT_MAX)
{
}
int count; //number of processes with this annotation
int size; //total number of processes in subtree including this node
int larger; //minimum of larger_left, larger_me, and, larger_right
int larger_me;
int larger_left;
int larger_right;
};
class Live_P_Tree : public ACE_RB_Tree<int, AnnotationNode, ACE_Equal_To<int>, ACE_Thread_Mutex> {
public:
Live_P_Tree(int maxThreads);
virtual ~Live_P_Tree();
int bind(const int& ext_id);
int unbind (const int &ext_id);
int calc_max() const;
protected:
void RB_rotate_right(ACE_RB_Tree_Node<int, AnnotationNode> *x);
void RB_rotate_left(ACE_RB_Tree_Node<int, AnnotationNode> *x);
private:
void recalculate_augmentation(ACE_RB_Tree_Node<int, AnnotationNode>* nodePtr);
void recalculate_augmentation_up(ACE_RB_Tree_Node<int, AnnotationNode>* x);
int calc_max_i(ACE_RB_Tree_Node<int, AnnotationNode>* nodePtr, int extra) const;
static int MIN(int a, int b) { return (a<b)?a:b; }
static int MIN_THREE(int a, int b, int c) {
return (a<b)?MIN(a,c):MIN(b,c);
}
int T_;
};
ACE_INLINE
Live_P_Tree::Live_P_Tree(int maxThreads)
:ACE_RB_Tree(),
T_(maxThreads) {
}
ACE_INLINE
Live_P_Tree::~Live_P_Tree() {
}
ACE_INLINE
int
Live_P_Tree::bind(const int& ext_id)
{
ACE_RB_Tree_Node<int, AnnotationNode>* entry = 0;
int returnVal = -1; //return error unless we return
//something else from the parent unbind
RB_SearchResult result = LEFT;
entry = find_node (ext_id, result);
// If there is a matching node, don't add a new one, just mod the existing one
if (entry && result == EXACT) {
entry->item().count++;
} else {
returnVal = ACE_RB_Tree::bind(ext_id, AnnotationNode(), entry);
}
recalculate_augmentation_up(entry);
return returnVal;
}
void
Live_P_Tree::RB_rotate_right (ACE_RB_Tree_Node<int, AnnotationNode> *x)
{
ACE_RB_Tree::RB_rotate_right(x);
recalculate_augmentation_up(x);
}
void
Live_P_Tree::RB_rotate_left (ACE_RB_Tree_Node<int, AnnotationNode> *x)
{
ACE_RB_Tree::RB_rotate_left(x);
recalculate_augmentation_up(x);
}
ACE_INLINE
int
Live_P_Tree::unbind(const int& ext_id)
{
ACE_RB_Tree_Node<int, AnnotationNode>* entry = 0;
RB_SearchResult result = LEFT;
int returnVal = -1; //return error unless we return
//something else from the parent unbind
entry = find_node (ext_id, result);
// If there is a matching node, don't add a new one, just mod the existing one
if (entry && result == EXACT) {
if (--(entry->item().count) == 0) {
entry = entry->parent();
returnVal = ACE_RB_Tree::unbind(ext_id);
}
} else {
//exception? probably bad if we try to unbind something not in the tree
}
if (entry) {
recalculate_augmentation_up(entry);
}
return returnVal;
}
ACE_INLINE void
Live_P_Tree::recalculate_augmentation(ACE_RB_Tree_Node<int, AnnotationNode>* nodePtr) {
AnnotationNode& node = nodePtr->item();
AnnotationNode& left = nodePtr->left() ? AnnotationNode() : nodePtr->left()->item();
AnnotationNode& right = nodePtr->right() ? AnnotationNode() : nodePtr->right()->item();
// (1) size
node.size = left.size + right.size + node.count;
// (2) larger_me
node.larger_me = T_ - (node.count + right.size + nodePtr->key());
// (3) larger_right
node.larger_right = right.larger;
// (4) larger_left
node.larger_left = left.larger - (right.size + node.count);
//(5) larger
node.larger = MIN_THREE(node.larger_me, node.larger_left, node.larger_right);
}
ACE_INLINE void
Live_P_Tree::recalculate_augmentation_up(ACE_RB_Tree_Node<int, AnnotationNode>* x) {
while (x) {
recalculate_augmentation(x);
x = x->parent();
}
}
ACE_INLINE int
Live_P_Tree::calc_max() const {
//note: need to add get_root method to RB_Tree
return calc_max_i(get_root(), 0);
}
ACE_INLINE int
Live_P_Tree::calc_max_i(ACE_RB_Tree_Node<int, AnnotationNode>* nodePtr, int extra) const {
AnnotationNode& n = nodePtr->item();
if ( n.larger_left - extra==0) {
return calc_max_i(nodePtr->left(), extra + nodePtr->right()->item().size + n.count); }
else if (n.larger_me - extra==0) { return (nodePtr->key()); }
else if (n.larger_right - extra==0) { return calc_max_i(nodePtr->right(), extra); }
else { return T_; }
}
template <typename AnnotationId>
ACE_INLINE
Live_P_Strategy<AnnotationId>::Live_P_Strategy(int maxThreads)
:DA_Strategy_Base(maxThreads),
min_illegal_is_computed_(false),
min_illegal_(0)
{
}
template <typename AnnotationId>
ACE_INLINE
Live_P_Strategy<AnnotationId>::~Live_P_Strategy()
{
}
template <typename AnnotationId>
ACE_INLINE
bool
Live_P_Strategy<AnnotationId>::is_deadlock_potential(AnnotationId handle)
{
int annotation = get_annotation(handle);
computation_mutex_.acquire();
if (!min_illegal_is_computed_)
{
if (tree_pimpl_->current_size() > 1)
{
min_illegal_ = tree_pimpl_->calc_max();
}
min_illegal_is_computed_ = true;
}
computation_mutex_.release();
return annotation >= min_illegal_;
}
template <typename AnnotationId>
ACE_INLINE
void
Live_P_Strategy<AnnotationId>::grant(AnnotationId handle)
{
int annotation = get_annotation(handle);
//since the state of the tree is involved in calculation
//of max, we must aquire the lock before changing the
//structure of the tree
computation_mutex_.acquire();
tree_pimpl_->bind(annotation);
min_illegal_is_computed_ = false;
computation_mutex_.release();
}
template <typename AnnotationId>
ACE_INLINE
void
Live_P_Strategy<AnnotationId>::release(AnnotationId handle)
{
//since the state of the tree is involved in calculation
//of max, we must aquire the lock before changing the
//structure of the tree
computation_mutex_.acquire();
min_illegal_is_computed_ = false;
int annotation = get_annotation(handle);
tree_pimpl_->unbind(annotation);
computation_mutex_.release();
}
|