Token_Invariants.cpp

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

Generated on Thu Nov 9 09:42:08 2006 for ACE by doxygen 1.3.6