Removed ACE_HAS_BSTRING, not used
[ACE_TAO.git] / ACE / ace / Token_Invariants.cpp
blob4d664c02b2f3b0a19022e2d7bd9242d906ce87d2
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...
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 (void)
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 (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,
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 (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;
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 // **************************************************
228 ACE_Mutex_Invariants::ACE_Mutex_Invariants (void)
229 : owners_ (0)
234 ACE_Mutex_Invariants::acquired (void)
236 if (++owners_ > 1)
238 owners_ = 42;
239 return 0;
241 else
242 return 1;
245 void
246 ACE_Mutex_Invariants::releasing (void)
248 if (owners_ == 1)
249 --owners_;
252 ACE_Mutex_Invariants::ACE_Mutex_Invariants (const ACE_Mutex_Invariants &rhs)
253 : owners_ (rhs.owners_)
257 void
258 ACE_Mutex_Invariants::operator= (const ACE_Mutex_Invariants &rhs)
260 owners_ = rhs.owners_;
263 void
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)
279 : writers_ (0),
280 readers_ (0)
285 ACE_RWLock_Invariants::writer_acquired (void)
287 if (readers_ > 0)
289 writers_ = readers_ = 42;
290 return 0;
292 else if (++writers_ > 1)
294 writers_ = readers_ = 42;
295 return 0;
297 else
298 return 1;
302 ACE_RWLock_Invariants::reader_acquired (void)
304 if (writers_ > 0)
306 writers_ = readers_ = 42;
307 return 0;
309 else
311 ++readers_;
312 return 1;
316 void
317 ACE_RWLock_Invariants::releasing (void)
319 if (writers_ == 1)
320 writers_ = 0;
321 else if (readers_ > 0)
322 --readers_;
325 ACE_RWLock_Invariants::ACE_RWLock_Invariants (const ACE_RWLock_Invariants &rhs)
326 : writers_ (rhs.writers_),
327 readers_ (rhs.readers_)
331 void
332 ACE_RWLock_Invariants::operator= (const ACE_RWLock_Invariants &rhs)
334 writers_ = rhs.writers_;
335 readers_ = rhs.readers_;
338 void
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 */