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