Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Token_Invariants.cpp

Go to the documentation of this file.
00001 #include "ace_pch.h"
00002 // $Id: Token_Invariants.cpp,v 1.1.1.3.40.1 2003/03/13 19:44:23 chad Exp $
00003 
00004 #include "ace/Token_Invariants.h"
00005 #include "ace/Object_Manager.h"
00006 
00007 #if defined (ACE_HAS_TOKENS_LIBRARY)
00008 
00009 ACE_RCSID(ace, Token_Invariants, "$Id: Token_Invariants.cpp,v 1.1.1.3.40.1 2003/03/13 19:44:23 chad Exp $")
00010 
00011 ACE_Token_Invariant_Manager *ACE_Token_Invariant_Manager::instance_ = 0;
00012 
00013 ACE_Token_Invariant_Manager *
00014 ACE_Token_Invariant_Manager::instance (void)
00015 {
00016   ACE_TRACE ("ACE_Token_Invariant_Manager::instance");
00017 
00018   // Perform the Double-Check pattern...
00019   if (instance_ == 0)
00020     {
00021       ACE_MT (ACE_TOKEN_CONST::MUTEX *lock =
00022         ACE_Managed_Object<ACE_TOKEN_CONST::MUTEX>::get_preallocated_object
00023           (ACE_Object_Manager::ACE_TOKEN_INVARIANTS_CREATION_LOCK);
00024         ACE_GUARD_RETURN (ACE_TOKEN_CONST::MUTEX, ace_mon, *lock, 0));
00025 
00026       if (instance_ == 0)
00027         {
00028           ACE_NEW_RETURN (instance_,
00029                           ACE_Token_Invariant_Manager,
00030                           0);
00031           // Register for destruction with ACE_Object_Manager.
00032           ACE_Object_Manager::at_exit (instance_);
00033         }
00034     }
00035 
00036   return instance_;
00037 }
00038 
00039 ACE_Token_Invariant_Manager::ACE_Token_Invariant_Manager (void)
00040 {
00041   ACE_TRACE ("ACE_Token_Invariant_Manager::ACE_Token_Invariant_Manager");
00042 }
00043 
00044 int
00045 ACE_Token_Invariant_Manager::mutex_acquired (const ACE_TCHAR *token_name)
00046 {
00047   ACE_TRACE ("ACE_Token_Invariant_Manager::mutex_acquired");
00048 
00049   ACE_GUARD_RETURN (ACE_TOKEN_CONST::MUTEX, ace_mon, this->lock_, -1);
00050 
00051   ACE_Mutex_Invariants *inv = 0;
00052   if (this->get_mutex (token_name, inv) == -1)
00053     return -1;
00054 
00055   return inv->acquired ();
00056 }
00057 
00058 int
00059 ACE_Token_Invariant_Manager::acquired (const ACE_Token_Proxy *proxy)
00060 {
00061   ACE_TRACE ("ACE_Token_Invariant_Manager::acquired");
00062 
00063   // Reach into the proxy to find the token type.
00064   if (proxy->token_->type () == ACE_Tokens::MUTEX)
00065     return this->mutex_acquired (proxy->name ());
00066   else // ACE_Tokens::RWLOCK.
00067     {
00068       if (proxy->type () == ACE_RW_Token::READER)
00069         return this->reader_acquired (proxy->name ());
00070       else // ACE_RW_Token::WRITER.
00071         return this->writer_acquired (proxy->name ());
00072     }
00073 }
00074 
00075 void
00076 ACE_Token_Invariant_Manager::releasing (const ACE_Token_Proxy *proxy)
00077 {
00078   ACE_TRACE ("ACE_Token_Invariant_Manager::releasing");
00079 
00080   // Reach into the proxy to find the token type.
00081   if (proxy->token_->type () == ACE_Tokens::MUTEX)
00082     this->mutex_releasing (proxy->name ());
00083   else // ACE_Tokens::RWLOCK.
00084     this->rwlock_releasing (proxy->name ());
00085 }
00086 
00087 void
00088 ACE_Token_Invariant_Manager::mutex_releasing (const ACE_TCHAR *token_name)
00089 {
00090   ACE_TRACE ("ACE_Token_Invariant_Manager::mutex_releasing");
00091   ACE_GUARD (ACE_TOKEN_CONST::MUTEX, ace_mon, this->lock_);
00092 
00093   ACE_Mutex_Invariants *inv = 0;
00094   if (this->get_mutex (token_name, inv) == 0)
00095     inv->releasing ();
00096 }
00097 
00098 int
00099 ACE_Token_Invariant_Manager::reader_acquired (const ACE_TCHAR *token_name)
00100 {
00101   ACE_TRACE ("ACE_Token_Invariant_Manager::reader_acquired");
00102   ACE_GUARD_RETURN (ACE_TOKEN_CONST::MUTEX, ace_mon, this->lock_, -1);
00103 
00104   ACE_RWLock_Invariants *inv = 0;
00105   if (this->get_rwlock (token_name, inv) == -1)
00106     return -1;
00107 
00108   return inv->reader_acquired ();
00109 }
00110 
00111 int
00112 ACE_Token_Invariant_Manager::writer_acquired (const ACE_TCHAR *token_name)
00113 {
00114   ACE_TRACE ("ACE_Token_Invariant_Manager::writer_acquired");
00115 
00116   ACE_GUARD_RETURN (ACE_TOKEN_CONST::MUTEX, ace_mon, this->lock_, -1);
00117 
00118   ACE_RWLock_Invariants *inv = 0;
00119   if (this->get_rwlock (token_name, inv) == -1)
00120     return -1;
00121 
00122   return inv->writer_acquired ();
00123 }
00124 
00125 void
00126 ACE_Token_Invariant_Manager::rwlock_releasing (const ACE_TCHAR *token_name)
00127 {
00128   ACE_TRACE ("ACE_Token_Invariant_Manager::rwlock_releasing");
00129 
00130   ACE_GUARD (ACE_TOKEN_CONST::MUTEX, ace_mon, this->lock_);
00131 
00132   ACE_RWLock_Invariants *inv = 0;
00133   if (this->get_rwlock (token_name, inv) == 0)
00134     inv->releasing ();
00135 }
00136 
00137 void
00138 ACE_Token_Invariant_Manager::dump (void) const
00139 {
00140   ACE_TRACE ("ACE_Token_Invariant_Manager::dump");
00141   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00142   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("mutex_collection_:\n")));
00143   mutex_collection_.dump ();
00144   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("rwlock_collection_:\n")));
00145   rwlock_collection_.dump ();
00146   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00147 }
00148 
00149 
00150 int
00151 ACE_Token_Invariant_Manager::get_mutex (const ACE_TCHAR *token_name,
00152                                         ACE_Mutex_Invariants *&inv)
00153 {
00154   ACE_TRACE ("ACE_Token_Invariant_Manager::get_mutex");
00155   TOKEN_NAME name (token_name);
00156   if (mutex_collection_.find (name, inv) == -1)
00157     // We did not find one in the collection.
00158     {
00159       ACE_Mutex_Invariants *new_invariant;
00160 
00161       ACE_NEW_RETURN (new_invariant,
00162                       ACE_Mutex_Invariants,
00163                       -1);
00164       if (mutex_collection_.bind (name, new_invariant) == -1)
00165         {
00166           delete new_invariant;
00167           return -1;
00168         }
00169 
00170       if (mutex_collection_.find (name, inv) == -1)
00171         // We did not find one in the collection.
00172         return -1;
00173     }
00174 
00175   return 0;
00176 }
00177 
00178 int
00179 ACE_Token_Invariant_Manager::get_rwlock (const ACE_TCHAR *token_name,
00180                                          ACE_RWLock_Invariants *&inv)
00181 {
00182   ACE_TRACE ("ACE_Token_Invariant_Manager::get_rwlock");
00183   TOKEN_NAME name (token_name);
00184   if (rwlock_collection_.find (name, inv) == -1)
00185     // We did not find one in the collection.
00186     {
00187       ACE_RWLock_Invariants *new_invariant;
00188 
00189       ACE_NEW_RETURN (new_invariant,
00190                       ACE_RWLock_Invariants,
00191                       -1);
00192       if (rwlock_collection_.bind (name, new_invariant) == -1)
00193         return -1;
00194 
00195       if (rwlock_collection_.find (name, inv) == -1)
00196         // We did not find one in the collection.
00197         return -1;
00198     }
00199 
00200   return 0;
00201 }
00202 
00203 
00204 ACE_Token_Invariant_Manager::~ACE_Token_Invariant_Manager (void)
00205 {
00206   ACE_TRACE ("ACE_Token_Invariant_Manager::~ACE_Token_Invariant_Manager");
00207   MUTEX_COLLECTION_ITERATOR iterator (mutex_collection_);
00208 
00209   for (MUTEX_COLLECTION_ENTRY *temp = 0;
00210        iterator.next (temp) != 0;
00211        iterator.advance ())
00212     delete temp->int_id_;
00213 
00214   RWLOCK_COLLECTION_ITERATOR iterator2 (rwlock_collection_);
00215 
00216   for (RWLOCK_COLLECTION_ENTRY *temp2 = 0;
00217        iterator2.next (temp2) != 0;
00218        iterator2.advance ())
00219     delete temp2->int_id_;
00220 }
00221 
00222 // **************************************************
00223 // **************************************************
00224 // **************************************************
00225 
00226 ACE_Mutex_Invariants::ACE_Mutex_Invariants (void)
00227 : owners_ (0)
00228 {
00229 }
00230 
00231 int
00232 ACE_Mutex_Invariants::acquired (void)
00233 {
00234   if (++owners_ > 1)
00235     {
00236       owners_ = 42;
00237       return 0;
00238     }
00239   else
00240     return 1;
00241 }
00242 
00243 void
00244 ACE_Mutex_Invariants::releasing (void)
00245 {
00246   if (owners_ == 1)
00247     --owners_;
00248 }
00249 
00250 ACE_Mutex_Invariants::ACE_Mutex_Invariants (const ACE_Mutex_Invariants &rhs)
00251 : owners_ (rhs.owners_)
00252 {
00253 }
00254 
00255 void
00256 ACE_Mutex_Invariants::operator= (const ACE_Mutex_Invariants &rhs)
00257 {
00258   owners_ = rhs.owners_;
00259 }
00260 
00261 void
00262 ACE_Mutex_Invariants::dump (void) const
00263 {
00264   ACE_TRACE ("ACE_Mutex_Invariants::dump");
00265   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00266   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("owners_ = %d\n"), owners_));
00267   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00268 }
00269 
00270 // **************************************************
00271 // **************************************************
00272 // **************************************************
00273 
00274 ACE_RWLock_Invariants::ACE_RWLock_Invariants (void)
00275 : writers_ (0),
00276   readers_ (0)
00277 {
00278 }
00279 
00280 int
00281 ACE_RWLock_Invariants::writer_acquired (void)
00282 {
00283   if (readers_ > 0)
00284     {
00285       writers_ = readers_ = 42;
00286       return 0;
00287     }
00288   else if (++writers_ > 1)
00289     {
00290       writers_ = readers_ = 42;
00291       return 0;
00292     }
00293   else
00294     return 1;
00295 }
00296 
00297 int
00298 ACE_RWLock_Invariants::reader_acquired (void)
00299 {
00300   if (writers_ > 0)
00301     {
00302       writers_ = readers_ = 42;
00303       return 0;
00304     }
00305   else
00306     {
00307       ++readers_;
00308       return 1;
00309     }
00310 }
00311 
00312 void
00313 ACE_RWLock_Invariants::releasing (void)
00314 {
00315   if (writers_ == 1)
00316     writers_ = 0;
00317   else if (readers_ > 0)
00318     --readers_;
00319 }
00320 
00321 ACE_RWLock_Invariants::ACE_RWLock_Invariants (const ACE_RWLock_Invariants &rhs)
00322 : writers_ (rhs.writers_),
00323   readers_ (rhs.readers_)
00324 {
00325 }
00326 
00327 void
00328 ACE_RWLock_Invariants::operator= (const ACE_RWLock_Invariants &rhs)
00329 {
00330   writers_ = rhs.writers_;
00331   readers_ = rhs.readers_;
00332 }
00333 
00334 void
00335 ACE_RWLock_Invariants::dump (void) const
00336 {
00337   ACE_TRACE ("ACE_RWLock_Invariants::dump");
00338   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00339   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("writers_ = %d\n"),
00340               "readers_ = %d\n",
00341               writers_, readers_));
00342   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00343 }
00344 
00345 #if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
00346 template class ACE_Map_Manager<ACE_Token_Name, ACE_Mutex_Invariants *, ACE_Null_Mutex>;
00347 template class ACE_Map_Iterator_Base<ACE_Token_Name, ACE_Mutex_Invariants *, ACE_Null_Mutex>;
00348 template class ACE_Map_Iterator<ACE_Token_Name, ACE_Mutex_Invariants *, ACE_Null_Mutex>;
00349 template class ACE_Map_Reverse_Iterator<ACE_Token_Name, ACE_Mutex_Invariants *, ACE_Null_Mutex>;
00350 template class ACE_Map_Entry<ACE_Token_Name, ACE_Mutex_Invariants *>;
00351 template class ACE_Map_Manager<ACE_Token_Name, ACE_RWLock_Invariants *, ACE_Null_Mutex>;
00352 template class ACE_Map_Iterator_Base<ACE_Token_Name, ACE_RWLock_Invariants *, ACE_Null_Mutex>;
00353 template class ACE_Map_Iterator<ACE_Token_Name, ACE_RWLock_Invariants *, ACE_Null_Mutex>;
00354 template class ACE_Map_Reverse_Iterator<ACE_Token_Name, ACE_RWLock_Invariants *, ACE_Null_Mutex>;
00355 template class ACE_Map_Entry<ACE_Token_Name, ACE_RWLock_Invariants *>;
00356 #elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
00357 #pragma instantiate ACE_Map_Manager<ACE_Token_Name, ACE_Mutex_Invariants *, ACE_Null_Mutex>
00358 #pragma instantiate ACE_Map_Iterator_Base<ACE_Token_Name, ACE_Mutex_Invariants *, ACE_Null_Mutex>
00359 #pragma instantiate ACE_Map_Iterator<ACE_Token_Name, ACE_Mutex_Invariants *, ACE_Null_Mutex>
00360 #pragma instantiate ACE_Map_Reverse_Iterator<ACE_Token_Name, ACE_Mutex_Invariants *, ACE_Null_Mutex>
00361 #pragma instantiate ACE_Map_Entry<ACE_Token_Name, ACE_Mutex_Invariants *>
00362 #pragma instantiate ACE_Map_Manager<ACE_Token_Name, ACE_RWLock_Invariants *, ACE_Null_Mutex>
00363 #pragma instantiate ACE_Map_Iterator_Base<ACE_Token_Name, ACE_RWLock_Invariants *, ACE_Null_Mutex>
00364 #pragma instantiate ACE_Map_Iterator<ACE_Token_Name, ACE_RWLock_Invariants *, ACE_Null_Mutex>
00365 #pragma instantiate ACE_Map_Reverse_Iterator<ACE_Token_Name, ACE_RWLock_Invariants *, ACE_Null_Mutex>
00366 #pragma instantiate ACE_Map_Entry<ACE_Token_Name, ACE_RWLock_Invariants *>
00367 #endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */
00368 
00369 #endif /* ACE_HAS_TOKENS_LIBRARY */

Generated on Mon Jun 16 11:21:57 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002