Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / Token_Invariants.cpp
blob7360a94a7db0c5e880a66ab762ad88508a8c2571
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 ()
15 ACE_TRACE ("ACE_Token_Invariant_Manager::instance");
17 // Perform the Double-Check pattern...
18 if (instance_ == 0)
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));
25 if (instance_ == 0)
27 ACE_NEW_RETURN (instance_,
28 ACE_Token_Invariant_Manager,
29 0);
30 // Register for destruction with ACE_Object_Manager.
31 ACE_Object_Manager::at_exit (instance_, 0, typeid (instance_).name ());
35 return instance_;
38 ACE_Token_Invariant_Manager::ACE_Token_Invariant_Manager ()
40 ACE_TRACE ("ACE_Token_Invariant_Manager::ACE_Token_Invariant_Manager");
43 int
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)
52 return -1;
54 return inv->acquired ();
57 int
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 ());
74 void
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 ());
86 void
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)
94 inv->releasing ();
97 int
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)
105 return -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)
119 return -1;
121 return inv->writer_acquired ();
124 void
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)
133 inv->releasing ();
136 void
137 ACE_Token_Invariant_Manager::dump () 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,
164 -1);
165 if (mutex_collection_.bind (name, new_invariant) == -1)
167 delete new_invariant;
168 return -1;
171 if (mutex_collection_.find (name, inv) == -1)
172 // We did not find one in the collection.
173 return -1;
176 return 0;
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,
192 -1);
193 if (rwlock_collection_.bind (name, new_invariant) == -1)
194 return -1;
196 if (rwlock_collection_.find (name, inv) == -1)
197 // We did not find one in the collection.
198 return -1;
201 return 0;
205 ACE_Token_Invariant_Manager::~ACE_Token_Invariant_Manager ()
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;
213 iterator.advance ())
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 // **************************************************
229 ACE_Mutex_Invariants::acquired ()
231 if (++owners_ > 1)
233 owners_ = 42;
234 return 0;
236 else
237 return 1;
240 void
241 ACE_Mutex_Invariants::releasing ()
243 if (owners_ == 1)
244 --owners_;
247 void
248 ACE_Mutex_Invariants::dump () const
250 #if defined (ACE_HAS_DUMP)
251 ACE_TRACE ("ACE_Mutex_Invariants::dump");
252 ACELIB_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
253 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("owners_ = %d\n"), owners_));
254 ACELIB_DEBUG ((LM_DEBUG, ACE_END_DUMP));
255 #endif /* ACE_HAS_DUMP */
258 // **************************************************
261 ACE_RWLock_Invariants::writer_acquired ()
263 if (readers_ > 0)
265 writers_ = readers_ = 42;
266 return 0;
268 else if (++writers_ > 1)
270 writers_ = readers_ = 42;
271 return 0;
273 else
274 return 1;
278 ACE_RWLock_Invariants::reader_acquired ()
280 if (writers_ > 0)
282 writers_ = readers_ = 42;
283 return 0;
285 else
287 ++readers_;
288 return 1;
292 void
293 ACE_RWLock_Invariants::releasing ()
295 if (writers_ == 1)
296 writers_ = 0;
297 else if (readers_ > 0)
298 --readers_;
301 void
302 ACE_RWLock_Invariants::dump () const
304 #if defined (ACE_HAS_DUMP)
305 ACE_TRACE ("ACE_RWLock_Invariants::dump");
306 ACELIB_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
307 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("writers_ = %d readers_ = %d\n"),
308 writers_, readers_));
309 ACELIB_DEBUG ((LM_DEBUG, ACE_END_DUMP));
310 #endif /* ACE_HAS_DUMP */
313 ACE_END_VERSIONED_NAMESPACE_DECL
315 #endif /* ACE_HAS_TOKENS_LIBRARY */