1 #include "ace/Token_Invariants.h"
3 #if defined (ACE_HAS_TOKENS_LIBRARY)
5 #include "ace/Object_Manager.h"
6 #include "ace/os_include/os_typeinfo.h"
8 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
10 ACE_Token_Invariant_Manager
*ACE_Token_Invariant_Manager::instance_
= 0;
12 ACE_Token_Invariant_Manager
*
13 ACE_Token_Invariant_Manager::instance (void)
15 ACE_TRACE ("ACE_Token_Invariant_Manager::instance");
17 // Perform the Double-Check pattern...
20 ACE_MT (ACE_TOKEN_CONST::MUTEX
*lock
=
21 ACE_Managed_Object
<ACE_TOKEN_CONST::MUTEX
>::get_preallocated_object
22 (ACE_Object_Manager::ACE_TOKEN_INVARIANTS_CREATION_LOCK
);
23 ACE_GUARD_RETURN (ACE_TOKEN_CONST::MUTEX
, ace_mon
, *lock
, 0));
27 ACE_NEW_RETURN (instance_
,
28 ACE_Token_Invariant_Manager
,
30 // Register for destruction with ACE_Object_Manager.
31 ACE_Object_Manager::at_exit (instance_
, 0, typeid (instance_
).name ());
38 ACE_Token_Invariant_Manager::ACE_Token_Invariant_Manager (void)
40 ACE_TRACE ("ACE_Token_Invariant_Manager::ACE_Token_Invariant_Manager");
44 ACE_Token_Invariant_Manager::mutex_acquired (const ACE_TCHAR
*token_name
)
46 ACE_TRACE ("ACE_Token_Invariant_Manager::mutex_acquired");
48 ACE_GUARD_RETURN (ACE_TOKEN_CONST::MUTEX
, ace_mon
, this->lock_
, -1);
50 ACE_Mutex_Invariants
*inv
= 0;
51 if (this->get_mutex (token_name
, inv
) == -1)
54 return inv
->acquired ();
58 ACE_Token_Invariant_Manager::acquired (const ACE_Token_Proxy
*proxy
)
60 ACE_TRACE ("ACE_Token_Invariant_Manager::acquired");
62 // Reach into the proxy to find the token type.
63 if (proxy
->token_
->type () == ACE_Tokens::MUTEX
)
64 return this->mutex_acquired (proxy
->name ());
65 else // ACE_Tokens::RWLOCK.
67 if (proxy
->type () == ACE_RW_Token::READER
)
68 return this->reader_acquired (proxy
->name ());
69 else // ACE_RW_Token::WRITER.
70 return this->writer_acquired (proxy
->name ());
75 ACE_Token_Invariant_Manager::releasing (const ACE_Token_Proxy
*proxy
)
77 ACE_TRACE ("ACE_Token_Invariant_Manager::releasing");
79 // Reach into the proxy to find the token type.
80 if (proxy
->token_
->type () == ACE_Tokens::MUTEX
)
81 this->mutex_releasing (proxy
->name ());
82 else // ACE_Tokens::RWLOCK.
83 this->rwlock_releasing (proxy
->name ());
87 ACE_Token_Invariant_Manager::mutex_releasing (const ACE_TCHAR
*token_name
)
89 ACE_TRACE ("ACE_Token_Invariant_Manager::mutex_releasing");
90 ACE_GUARD (ACE_TOKEN_CONST::MUTEX
, ace_mon
, this->lock_
);
92 ACE_Mutex_Invariants
*inv
= 0;
93 if (this->get_mutex (token_name
, inv
) == 0)
98 ACE_Token_Invariant_Manager::reader_acquired (const ACE_TCHAR
*token_name
)
100 ACE_TRACE ("ACE_Token_Invariant_Manager::reader_acquired");
101 ACE_GUARD_RETURN (ACE_TOKEN_CONST::MUTEX
, ace_mon
, this->lock_
, -1);
103 ACE_RWLock_Invariants
*inv
= 0;
104 if (this->get_rwlock (token_name
, inv
) == -1)
107 return inv
->reader_acquired ();
111 ACE_Token_Invariant_Manager::writer_acquired (const ACE_TCHAR
*token_name
)
113 ACE_TRACE ("ACE_Token_Invariant_Manager::writer_acquired");
115 ACE_GUARD_RETURN (ACE_TOKEN_CONST::MUTEX
, ace_mon
, this->lock_
, -1);
117 ACE_RWLock_Invariants
*inv
= 0;
118 if (this->get_rwlock (token_name
, inv
) == -1)
121 return inv
->writer_acquired ();
125 ACE_Token_Invariant_Manager::rwlock_releasing (const ACE_TCHAR
*token_name
)
127 ACE_TRACE ("ACE_Token_Invariant_Manager::rwlock_releasing");
129 ACE_GUARD (ACE_TOKEN_CONST::MUTEX
, ace_mon
, this->lock_
);
131 ACE_RWLock_Invariants
*inv
= 0;
132 if (this->get_rwlock (token_name
, inv
) == 0)
137 ACE_Token_Invariant_Manager::dump (void) const
139 #if defined (ACE_HAS_DUMP)
140 ACE_TRACE ("ACE_Token_Invariant_Manager::dump");
141 ACELIB_DEBUG ((LM_DEBUG
, ACE_BEGIN_DUMP
, this));
142 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("mutex_collection_:\n")));
143 mutex_collection_
.dump ();
144 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("rwlock_collection_:\n")));
145 rwlock_collection_
.dump ();
146 ACELIB_DEBUG ((LM_DEBUG
, ACE_END_DUMP
));
147 #endif /* ACE_HAS_DUMP */
152 ACE_Token_Invariant_Manager::get_mutex (const ACE_TCHAR
*token_name
,
153 ACE_Mutex_Invariants
*&inv
)
155 ACE_TRACE ("ACE_Token_Invariant_Manager::get_mutex");
156 TOKEN_NAME
name (token_name
);
157 if (mutex_collection_
.find (name
, inv
) == -1)
158 // We did not find one in the collection.
160 ACE_Mutex_Invariants
*new_invariant
;
162 ACE_NEW_RETURN (new_invariant
,
163 ACE_Mutex_Invariants
,
165 if (mutex_collection_
.bind (name
, new_invariant
) == -1)
167 delete new_invariant
;
171 if (mutex_collection_
.find (name
, inv
) == -1)
172 // We did not find one in the collection.
180 ACE_Token_Invariant_Manager::get_rwlock (const ACE_TCHAR
*token_name
,
181 ACE_RWLock_Invariants
*&inv
)
183 ACE_TRACE ("ACE_Token_Invariant_Manager::get_rwlock");
184 TOKEN_NAME
name (token_name
);
185 if (rwlock_collection_
.find (name
, inv
) == -1)
186 // We did not find one in the collection.
188 ACE_RWLock_Invariants
*new_invariant
;
190 ACE_NEW_RETURN (new_invariant
,
191 ACE_RWLock_Invariants
,
193 if (rwlock_collection_
.bind (name
, new_invariant
) == -1)
196 if (rwlock_collection_
.find (name
, inv
) == -1)
197 // We did not find one in the collection.
205 ACE_Token_Invariant_Manager::~ACE_Token_Invariant_Manager (void)
207 ACE_TRACE ("ACE_Token_Invariant_Manager::~ACE_Token_Invariant_Manager");
209 MUTEX_COLLECTION::ITERATOR
iterator (mutex_collection_
);
211 for (MUTEX_COLLECTION::ENTRY
*temp
= 0;
212 iterator
.next (temp
) != 0;
214 delete temp
->int_id_
;
216 RWLOCK_COLLECTION::ITERATOR
iterator2 (rwlock_collection_
);
218 for (RWLOCK_COLLECTION::ENTRY
*temp2
= 0;
219 iterator2
.next (temp2
) != 0;
220 iterator2
.advance ())
221 delete temp2
->int_id_
;
224 // **************************************************
225 // **************************************************
226 // **************************************************
228 ACE_Mutex_Invariants::ACE_Mutex_Invariants (void)
234 ACE_Mutex_Invariants::acquired (void)
246 ACE_Mutex_Invariants::releasing (void)
252 ACE_Mutex_Invariants::ACE_Mutex_Invariants (const ACE_Mutex_Invariants
&rhs
)
253 : owners_ (rhs
.owners_
)
258 ACE_Mutex_Invariants::operator= (const ACE_Mutex_Invariants
&rhs
)
260 owners_
= rhs
.owners_
;
264 ACE_Mutex_Invariants::dump (void) const
266 #if defined (ACE_HAS_DUMP)
267 ACE_TRACE ("ACE_Mutex_Invariants::dump");
268 ACELIB_DEBUG ((LM_DEBUG
, ACE_BEGIN_DUMP
, this));
269 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("owners_ = %d\n"), owners_
));
270 ACELIB_DEBUG ((LM_DEBUG
, ACE_END_DUMP
));
271 #endif /* ACE_HAS_DUMP */
274 // **************************************************
275 // **************************************************
276 // **************************************************
278 ACE_RWLock_Invariants::ACE_RWLock_Invariants (void)
285 ACE_RWLock_Invariants::writer_acquired (void)
289 writers_
= readers_
= 42;
292 else if (++writers_
> 1)
294 writers_
= readers_
= 42;
302 ACE_RWLock_Invariants::reader_acquired (void)
306 writers_
= readers_
= 42;
317 ACE_RWLock_Invariants::releasing (void)
321 else if (readers_
> 0)
325 ACE_RWLock_Invariants::ACE_RWLock_Invariants (const ACE_RWLock_Invariants
&rhs
)
326 : writers_ (rhs
.writers_
),
327 readers_ (rhs
.readers_
)
332 ACE_RWLock_Invariants::operator= (const ACE_RWLock_Invariants
&rhs
)
334 writers_
= rhs
.writers_
;
335 readers_
= rhs
.readers_
;
339 ACE_RWLock_Invariants::dump (void) const
341 #if defined (ACE_HAS_DUMP)
342 ACE_TRACE ("ACE_RWLock_Invariants::dump");
343 ACELIB_DEBUG ((LM_DEBUG
, ACE_BEGIN_DUMP
, this));
344 ACELIB_DEBUG ((LM_DEBUG
, ACE_TEXT ("writers_ = %d readers_ = %d\n"),
345 writers_
, readers_
));
346 ACELIB_DEBUG ((LM_DEBUG
, ACE_END_DUMP
));
347 #endif /* ACE_HAS_DUMP */
350 ACE_END_VERSIONED_NAMESPACE_DECL
352 #endif /* ACE_HAS_TOKENS_LIBRARY */