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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
// Token_Manager.cpp
// $Id$
#include "ace/Token_Manager.h"
#include "ace/Object_Manager.h"
#if !defined (__ACE_INLINE__)
#include "ace/Token_Manager.i"
#endif /* __ACE_INLINE__ */
ACE_RCSID(ace, Token_Manager, "$Id$")
// singleton token manager
ACE_Token_Manager *ACE_Token_Manager::token_manager_ = 0;
ACE_Token_Manager::ACE_Token_Manager ()
{
ACE_TRACE ("ACE_Token_Manager::ACE_Token_Manager");
}
ACE_Token_Manager::~ACE_Token_Manager ()
{
ACE_TRACE ("ACE_Token_Manager::~ACE_Token_Manager");
COLLECTION_ITERATOR iterator (collection_);
for (COLLECTION_ENTRY *temp = 0;
iterator.next (temp) != 0;
iterator.advance ())
{
// @ should I be doing an unbind here?
delete temp->int_id_;
// The ext_id_'s delete themselves when the array of
// COLLECTION_ENTRYs goes away.
}
}
ACE_Token_Manager *
ACE_Token_Manager::instance (void)
{
ACE_TRACE ("ACE_Token_Manager::token_manager");
// This first check is to avoid acquiring the mutex in the common
// case. Double-Check pattern rules.
if (token_manager_ == 0)
{
#if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
ACE_TOKEN_CONST::MUTEX *lock =
ACE_Managed_Object<ACE_TOKEN_CONST::MUTEX>::get_preallocated_object
(ACE_Object_Manager::ACE_TOKEN_MANAGER_CREATION_LOCK);
ACE_GUARD_RETURN (ACE_TOKEN_CONST::MUTEX, ace_mon, *lock, 0);
#endif /* ACE_MT_SAFE */
if (token_manager_ == 0)
{
ACE_NEW_RETURN (token_manager_,
ACE_Token_Manager,
0);
// Register for destruction with ACE_Object_Manager.
ACE_Object_Manager::at_exit (token_manager_);
}
}
return token_manager_;
}
void
ACE_Token_Manager::get_token (ACE_Token_Proxy *proxy,
const ASYS_TCHAR *token_name)
{
ACE_TRACE ("ACE_Token_Manager::get");
// Hmm. I think this makes sense. We perform our own locking here
// (see safe_acquire.) We have to make sure that only one thread
// uses the collection at a time.
ACE_GUARD (ACE_TOKEN_CONST::MUTEX, ace_mon, this->lock_);
TOKEN_NAME name (token_name);
if (collection_.find (name, proxy->token_) == -1)
// We did not find one in the collection.
{
// Make one.
proxy->token_ = proxy->create_token (token_name);
// Put it in the collection.
if (collection_.bind (name, proxy->token_) == -1)
{
delete proxy->token_;
proxy->token_ = 0;
}
}
if (proxy->token_ != 0)
proxy->token_->inc_reference ();
// We may be returning proxy->token_ == 0 if new failed, caller must
// check.
}
// 0. check_deadlock (TOKEN)
// 1. if TOKEN->visited (), return 0.
// 2. mark TOKEN visited.
// 3. get ALL_OWNERS
// 4. if CLIENT in ALL_OWNERS, return *DEADLOCK*.
// 5. for each OWNER in ALL_OWNERS,
// 6. if OWNER is not waiting for a NEW_TOKEN, continue.
// 7. else, if check_deadlock (NEW_TOKEN) == 1, return *DEADLOCK*
// 8. return 0.
int
ACE_Token_Manager::check_deadlock (ACE_Token_Proxy *proxy)
{
ACE_TRACE ("ACE_Token_Manager::check_deadlock");
// Start the recursive deadlock detection algorithm.
int result = this->check_deadlock (proxy->token_, proxy);
// Whether or not we detect deadlock, we have to unmark all tokens
// for the next time.
COLLECTION_ITERATOR iterator (collection_);
for (COLLECTION_ENTRY *temp = 0;
iterator.next (temp) != 0;
iterator.advance ())
temp->int_id_->visit (0);
return result;
}
int
ACE_Token_Manager::check_deadlock (ACE_Tokens *token, ACE_Token_Proxy *proxy)
{
ACE_TRACE ("ACE_Token_Manager::check_deadlock");
if (token->visited ())
return 0;
token->visit (1);
ACE_Tokens::OWNER_STACK owners;
int is_owner = token->owners (owners, proxy->client_id ());
switch (is_owner)
{
case -1:
// Error.
return -1;
case 1:
// The caller is an owner, so we have a deadlock situation.
if (debug_)
{
ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("(%t) Deadlock detected.\n")));
ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("%s owns %s and is waiting for %s.\n"),
proxy->client_id (),
token->name (),
proxy->token_->name ()));
}
return 1;
case 0:
default:
// Recurse on each owner.
while (!owners.is_empty ())
{
ACE_TPQ_Entry *e;
owners.pop (e);
// If the owner is waiting on another token, recurse.
ACE_Tokens *twf = this->token_waiting_for (e->client_id ());
if ((twf != 0) &&
(this->check_deadlock (twf, proxy) == 1))
{
if (debug_)
{
ACE_DEBUG ((LM_DEBUG,
ASYS_TEXT ("%s owns %s and is waiting for %s.\n"),
e->client_id (),
token->name (),
twf->name ()));
}
return 1;
}
// else, check the next owner.
}
// We've checked all the owners and found no deadlock.
return 0;
}
}
ACE_Tokens *
ACE_Token_Manager::token_waiting_for (const ASYS_TCHAR *client_id)
{
COLLECTION_ITERATOR iterator (collection_);
for (COLLECTION_ENTRY *temp = 0;
iterator.next (temp) != 0;
iterator.advance ())
{
if (temp->int_id_->is_waiting_for (client_id))
return temp->int_id_;
}
// nothing was found, return NULL.
return 0;
}
// Notify the token manager that a token is has been released. If
// as a result, there is no owner of the token, the token is
// deleted.
void
ACE_Token_Manager::release_token (ACE_Tokens *&token)
{
ACE_TRACE ("ACE_Token_Manager::release");
// again, let's perform our own locking here.
ACE_GUARD (ACE_TOKEN_CONST::MUTEX, ace_mon, this->lock_);
if (token->dec_reference () == 0)
{
// No one has the token, so we can delete it and remove it from
// our collection. First, let's get it from the collection.
TOKEN_NAME token_name (token->name ());
ACE_Tokens *temp;
if (collection_.unbind (token_name, temp) == -1)
// we did not find one in the collection
{
errno = ENOENT;
ACE_ERROR ((LM_ERROR, ASYS_TEXT ("Token Manager could not release %s:%d\n"),
ASYS_ONLY_MULTIBYTE_STRING (token->name ()), token->type ()));
// @@ bad
}
else
// we found it
{
// sanity pointer comparison. The token referenced by the
// proxy better be the one we found in the list.
ACE_ASSERT (token == temp);
delete token; // or delete temp
// we set their token to zero. if the calling proxy is
// still going to be used, it had better check it's token
// value before calling a method on it!
token = 0;
}
}
// else
// someone is still interested in the token, so keep it around.
}
void
ACE_Token_Manager::dump (void) const
{
ACE_TRACE ("ACE_Token_Manager::dump");
ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("ACE_Token_Manager::dump:\n")));
ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("lock_\n")));
lock_.dump ();
ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("collection_\n")));
collection_.dump ();
ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
}
#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Map_Manager <ACE_Token_Name, ACE_Tokens *, ACE_Null_Mutex>;
template class ACE_Map_Iterator_Base<ACE_Token_Name, ACE_Tokens *, ACE_Null_Mutex>;
template class ACE_Map_Iterator<ACE_Token_Name, ACE_Tokens *, ACE_Null_Mutex>;
template class ACE_Map_Reverse_Iterator<ACE_Token_Name, ACE_Tokens *, ACE_Null_Mutex>;
template class ACE_Map_Entry <ACE_Token_Name, ACE_Tokens *>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Map_Manager <ACE_Token_Name, ACE_Tokens *, ACE_Null_Mutex>
#pragma instantiate ACE_Map_Iterator_Base<ACE_Token_Name, ACE_Tokens *, ACE_Null_Mutex>
#pragma instantiate ACE_Map_Iterator<ACE_Token_Name, ACE_Tokens *, ACE_Null_Mutex>
#pragma instantiate ACE_Map_Reverse_Iterator<ACE_Token_Name, ACE_Tokens *, ACE_Null_Mutex>
#pragma instantiate ACE_Map_Entry <ACE_Token_Name, ACE_Tokens *>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */
|