00001 #if defined(CORELIB___NCBIMTX__HPP) && !defined(CORELIB___NCBIMTX__INL) 00002 #define CORELIB___NCBIMTX__INL 00003 00004 /* $Id: ncbimtx.inl 173634 2009-10-20 13:02:43Z ivanovp $ 00005 * =========================================================================== 00006 * 00007 * PUBLIC DOMAIN NOTICE 00008 * National Center for Biotechnology Information 00009 * 00010 * This software/database is a "United States Government Work" under the 00011 * terms of the United States Copyright Act. It was written as part of 00012 * the author's official duties as a United States Government employee and 00013 * thus cannot be copyrighted. This software/database is freely available 00014 * to the public for use. The National Library of Medicine and the U.S. 00015 * Government have not placed any restriction on its use or reproduction. 00016 * 00017 * Although all reasonable efforts have been taken to ensure the accuracy 00018 * and reliability of the software and data, the NLM and the U.S. 00019 * Government do not and cannot warrant the performance or results that 00020 * may be obtained by using this software or data. The NLM and the U.S. 00021 * Government disclaim all warranties, express or implied, including 00022 * warranties of performance, merchantability or fitness for any particular 00023 * purpose. 00024 * 00025 * Please cite the author in any work or product based on this material. 00026 * 00027 * =========================================================================== 00028 * 00029 * Author: Eugene Vasilchenko 00030 * 00031 * File Description: 00032 * Mutex classes' inline functions 00033 * 00034 */ 00035 00036 ///////////////////////////////////////////////////////////////////////////// 00037 // SSystemFastMutexStruct 00038 // 00039 00040 inline 00041 bool SSystemFastMutex::IsInitialized(void) const 00042 { 00043 return m_Magic == eMutexInitialized; 00044 } 00045 00046 inline 00047 bool SSystemFastMutex::IsUninitialized(void) const 00048 { 00049 return m_Magic == eMutexUninitialized; 00050 } 00051 00052 inline 00053 void SSystemFastMutex::CheckInitialized(void) const 00054 { 00055 #if defined(INTERNAL_MUTEX_DEBUG) 00056 if ( !IsInitialized() ) { 00057 ThrowUninitialized(); 00058 } 00059 #endif 00060 } 00061 00062 #if defined(NCBI_NO_THREADS) 00063 // empty version of Lock/Unlock methods for inlining 00064 inline 00065 void SSystemFastMutex::Lock(void) 00066 { 00067 } 00068 00069 00070 inline 00071 bool SSystemFastMutex::TryLock(void) 00072 { 00073 return true; 00074 } 00075 00076 00077 inline 00078 void SSystemFastMutex::Unlock(void) 00079 { 00080 } 00081 #endif 00082 00083 ///////////////////////////////////////////////////////////////////////////// 00084 // SSystemMutex 00085 // 00086 00087 inline 00088 bool SSystemMutex::IsInitialized(void) const 00089 { 00090 return m_Mutex.IsInitialized(); 00091 } 00092 00093 inline 00094 bool SSystemMutex::IsUninitialized(void) const 00095 { 00096 return m_Mutex.IsUninitialized(); 00097 } 00098 00099 inline 00100 void SSystemMutex::InitializeStatic(void) 00101 { 00102 m_Mutex.InitializeStatic(); 00103 } 00104 00105 inline 00106 void SSystemMutex::InitializeDynamic(void) 00107 { 00108 m_Mutex.InitializeDynamic(); 00109 m_Count = 0; 00110 } 00111 00112 #if defined(NCBI_NO_THREADS) 00113 // empty version of Lock/Unlock methods for inlining 00114 inline 00115 void SSystemMutex::Lock(void) 00116 { 00117 } 00118 00119 00120 inline 00121 bool SSystemMutex::TryLock(void) 00122 { 00123 return true; 00124 } 00125 00126 00127 inline 00128 void SSystemMutex::Unlock(void) 00129 { 00130 } 00131 #endif 00132 00133 #if defined(NEED_AUTO_INITIALIZE_MUTEX) 00134 00135 inline 00136 CAutoInitializeStaticFastMutex::TObject& 00137 CAutoInitializeStaticFastMutex::Get(void) 00138 { 00139 if ( !m_Mutex.IsInitialized() ) { 00140 Initialize(); 00141 } 00142 return m_Mutex; 00143 } 00144 00145 inline 00146 CAutoInitializeStaticFastMutex:: 00147 operator CAutoInitializeStaticFastMutex::TObject&(void) 00148 { 00149 return Get(); 00150 } 00151 00152 inline 00153 void CAutoInitializeStaticFastMutex::Lock(void) 00154 { 00155 Get().Lock(); 00156 } 00157 00158 inline 00159 void CAutoInitializeStaticFastMutex::Unlock(void) 00160 { 00161 Get().Unlock(); 00162 } 00163 00164 inline 00165 bool CAutoInitializeStaticFastMutex::TryLock(void) 00166 { 00167 return Get().TryLock(); 00168 } 00169 00170 inline 00171 CAutoInitializeStaticMutex::TObject& 00172 CAutoInitializeStaticMutex::Get(void) 00173 { 00174 if ( !m_Mutex.IsInitialized() ) { 00175 Initialize(); 00176 } 00177 return m_Mutex; 00178 } 00179 00180 inline 00181 CAutoInitializeStaticMutex:: 00182 operator CAutoInitializeStaticMutex::TObject&(void) 00183 { 00184 return Get(); 00185 } 00186 00187 inline 00188 void CAutoInitializeStaticMutex::Lock(void) 00189 { 00190 Get().Lock(); 00191 } 00192 00193 inline 00194 void CAutoInitializeStaticMutex::Unlock(void) 00195 { 00196 Get().Unlock(); 00197 } 00198 00199 inline 00200 bool CAutoInitializeStaticMutex::TryLock(void) 00201 { 00202 return Get().TryLock(); 00203 } 00204 00205 #endif 00206 00207 ///////////////////////////////////////////////////////////////////////////// 00208 // CFastMutex:: 00209 // 00210 00211 inline 00212 CFastMutex::CFastMutex(void) 00213 { 00214 m_Mutex.InitializeDynamic(); 00215 } 00216 00217 inline 00218 CFastMutex::~CFastMutex(void) 00219 { 00220 m_Mutex.Destroy(); 00221 } 00222 00223 inline 00224 CFastMutex::operator SSystemFastMutex&(void) 00225 { 00226 return m_Mutex; 00227 } 00228 00229 inline 00230 void CFastMutex::Lock(void) 00231 { 00232 m_Mutex.Lock(); 00233 } 00234 00235 inline 00236 void CFastMutex::Unlock(void) 00237 { 00238 m_Mutex.Unlock(); 00239 } 00240 00241 inline 00242 bool CFastMutex::TryLock(void) 00243 { 00244 return m_Mutex.TryLock(); 00245 } 00246 00247 inline 00248 CMutex::CMutex(void) 00249 { 00250 m_Mutex.InitializeDynamic(); 00251 } 00252 00253 inline 00254 CMutex::~CMutex(void) 00255 { 00256 m_Mutex.Destroy(); 00257 } 00258 00259 inline 00260 CMutex::operator SSystemMutex&(void) 00261 { 00262 return m_Mutex; 00263 } 00264 00265 inline 00266 void CMutex::Lock(void) 00267 { 00268 m_Mutex.Lock(); 00269 } 00270 00271 inline 00272 void CMutex::Unlock(void) 00273 { 00274 m_Mutex.Unlock(); 00275 } 00276 00277 inline 00278 bool CMutex::TryLock(void) 00279 { 00280 return m_Mutex.TryLock(); 00281 } 00282 00283 00284 inline 00285 CSpinLock::CSpinLock(void) 00286 : m_Value(NULL) 00287 {} 00288 00289 inline 00290 CSpinLock::~CSpinLock(void) 00291 { 00292 _ASSERT(m_Value == NULL); 00293 } 00294 00295 inline 00296 void CSpinLock::Lock(void) 00297 { 00298 while (SwapPointers(&m_Value, (void*)1)) { 00299 NCBI_SCHED_YIELD(); 00300 } 00301 } 00302 00303 inline 00304 bool CSpinLock::TryLock(void) 00305 { 00306 return SwapPointers(&m_Value, (void*)1) == NULL; 00307 } 00308 00309 inline 00310 void CSpinLock::Unlock(void) 00311 { 00312 _VERIFY(SwapPointers(&m_Value, NULL) != NULL); 00313 } 00314 00315 00316 inline 00317 CFastRWLock::CFastRWLock(void) 00318 { 00319 m_LockCount.Set(0); 00320 } 00321 00322 inline 00323 CFastRWLock::~CFastRWLock(void) 00324 { 00325 _ASSERT(m_LockCount.Get() == 0); 00326 } 00327 00328 inline void 00329 CFastRWLock::ReadLock(void) 00330 { 00331 while (m_LockCount.Add(1) > kWriteLockValue) { 00332 m_LockCount.Add(-1); 00333 m_WriteLock.Lock(); 00334 m_WriteLock.Unlock(); 00335 } 00336 } 00337 00338 inline void 00339 CFastRWLock::ReadUnlock(void) 00340 { 00341 m_LockCount.Add(-1); 00342 } 00343 00344 inline void 00345 CFastRWLock::WriteUnlock(void) 00346 { 00347 m_LockCount.Add(-kWriteLockValue); 00348 m_WriteLock.Unlock(); 00349 } 00350 00351 00352 inline void 00353 CRWLockHolder::Init(CYieldingRWLock* lock, ERWLockType typ) 00354 { 00355 _ASSERT(lock); 00356 00357 m_Lock = lock; 00358 m_Type = typ; 00359 } 00360 00361 inline void 00362 CRWLockHolder::Reset(void) 00363 { 00364 m_Lock = NULL; 00365 m_LockAcquired = false; 00366 m_Listeners.clear(); 00367 } 00368 00369 inline 00370 CRWLockHolder::CRWLockHolder(IRWLockHolder_Factory* factory) 00371 : m_Factory(factory) 00372 { 00373 _ASSERT(factory); 00374 00375 Reset(); 00376 } 00377 00378 inline 00379 IRWLockHolder_Factory* CRWLockHolder::GetFactory(void) const 00380 { 00381 return m_Factory; 00382 } 00383 00384 inline 00385 CYieldingRWLock* CRWLockHolder::GetRWLock(void) const 00386 { 00387 return m_Lock; 00388 } 00389 00390 inline 00391 ERWLockType CRWLockHolder::GetLockType(void) const 00392 { 00393 return m_Type; 00394 } 00395 00396 inline 00397 bool CRWLockHolder::IsLockAcquired(void) const 00398 { 00399 return m_LockAcquired; 00400 } 00401 00402 inline 00403 void CRWLockHolder::ReleaseLock(void) 00404 { 00405 _ASSERT(m_Lock); 00406 00407 m_Lock->x_ReleaseLock(this); 00408 } 00409 00410 inline 00411 void CRWLockHolder::AddListener(IRWLockHolder_Listener* listener) 00412 { 00413 _ASSERT(m_Lock); 00414 00415 m_ObjLock.Lock(); 00416 m_Listeners.push_back(TRWLockHolder_ListenerWeakRef(listener)); 00417 m_ObjLock.Unlock(); 00418 } 00419 00420 inline 00421 void CRWLockHolder::RemoveListener(IRWLockHolder_Listener* listener) 00422 { 00423 _ASSERT(m_Lock); 00424 00425 m_ObjLock.Lock(); 00426 m_Listeners.remove(TRWLockHolder_ListenerWeakRef(listener)); 00427 m_ObjLock.Unlock(); 00428 } 00429 00430 00431 inline 00432 TRWLockHolderRef CYieldingRWLock::AcquireReadLock(void) 00433 { 00434 return AcquireLock(eReadLock); 00435 } 00436 00437 inline 00438 TRWLockHolderRef CYieldingRWLock::AcquireWriteLock(void) 00439 { 00440 return AcquireLock(eWriteLock); 00441 } 00442 00443 inline 00444 bool CYieldingRWLock::IsLocked(void) 00445 { 00446 m_ObjLock.Lock(); 00447 bool locked = m_Locks[eReadLock] + m_Locks[eWriteLock] != 0; 00448 m_ObjLock.Unlock(); 00449 return locked; 00450 } 00451 00452 #endif 00453 00454
1.4.6
Modified on Mon Dec 07 16:20:35 2009 by modify_doxy.py rev. 173732