CRWLock Class Reference
[Threads]

Search Toolkit Book for CRWLock

#include <ncbimtx.hpp>

Collaboration diagram for CRWLock:

Collaboration graph
[legend]
List of all members.

Detailed Description

CRWLock --.

Read/Write lock related data and methods.

Allows multiple readers or single writer with recursive locks. R-after-W is considered to be a recursive Write-lock. W-after-R is not allowed.

NOTE: When _DEBUG is not defined, does not always detect W-after-R correctly, so that deadlock may happen. Test your application in _DEBUG mode first!

Definition at line 910 of file ncbimtx.hpp.

Public Types

typedef int TFlags
 binary OR of EFlags
typedef CReadLockGuard TReadLockGuard
 Define Read Lock Guard.
typedef CWriteLockGuard TWriteLockGuard
 Define Write Lock Guard.
enum  EFlags { fFavorWriters = 0x1 }
 Flags (passed at construction time) for fine-tuning lock behavior. More...

Public Member Functions

 CRWLock (TFlags flags=0)
 Constructor.
 ~CRWLock (void)
 Destructor.
void ReadLock (void)
 Read lock.
void WriteLock (void)
 Write lock.
bool TryReadLock (void)
 Try read lock.
bool TryWriteLock (void)
 Try write lock.
void Unlock (void)
 Release the RW-lock.

Private Types

enum  EInternalFlags { fTrackReaders = 0x40000000 }

Private Member Functions

bool x_MayAcquireForReading (CThreadSystemID self_id)
 CRWLock (const CRWLock &)
 Private copy constructor to disallow initialization.
CRWLockoperator= (const CRWLock &)
 Private assignment operator to disallow assignment.

Private Attributes

TFlags m_Flags
 Configuration flags.
auto_ptr< CInternalRWLockm_RW
 Platform-dependent RW-lock data.
volatile CThreadSystemID m_Owner
 Writer ID, one of the readers ID.
volatile int m_Count
 Number of readers (if >0) or writers (if <0).
volatile unsigned int m_WaitingWriters
 Number of writers waiting; zero if not keeping track.
vector< CThreadSystemIDm_Readers
 List of all readers or writers for debugging.


Member Typedef Documentation

typedef int CRWLock::TFlags
 

binary OR of EFlags

Definition at line 919 of file ncbimtx.hpp.

typedef CReadLockGuard CRWLock::TReadLockGuard
 

Define Read Lock Guard.

Definition at line 928 of file ncbimtx.hpp.

typedef CWriteLockGuard CRWLock::TWriteLockGuard
 

Define Write Lock Guard.

Definition at line 931 of file ncbimtx.hpp.


Member Enumeration Documentation

enum CRWLock::EFlags
 

Flags (passed at construction time) for fine-tuning lock behavior.

Enumerator:
fFavorWriters  Forbid further readers from acquiring the lock if any writers are waiting for it, to keep would-be writers from starving.

Definition at line 914 of file ncbimtx.hpp.

enum CRWLock::EInternalFlags [private]
 

Enumerator:
fTrackReaders  Keep track of which threads have read locks.

Definition at line 965 of file ncbimtx.hpp.


Constructor & Destructor Documentation

CRWLock::CRWLock TFlags  flags = 0  ) 
 

Constructor.

Definition at line 581 of file ncbimtx.cpp.

References fFavorWriters, fTrackReaders, m_Flags, and m_Readers.

CRWLock::~CRWLock void   ) 
 

Destructor.

Definition at line 600 of file ncbimtx.cpp.

CRWLock::CRWLock const CRWLock  )  [private]
 

Private copy constructor to disallow initialization.


Member Function Documentation

CRWLock& CRWLock::operator= const CRWLock  )  [private]
 

Private assignment operator to disallow assignment.

void CRWLock::ReadLock void   ) 
 

Read lock.

Acquire the R-lock. If W-lock is already acquired by another thread, then wait until it is released.

Definition at line 651 of file ncbimtx.cpp.

References CThreadSystemID::GetCurrent(), CThreadSystemID::Is(), m_Count, m_Owner, m_RW, TRUE, x_MayAcquireForReading(), and xncbi_Validate.

Referenced by CProjectConstPrx::CProjectConstPrx(), CQueueConstIterator::CQueueConstIterator(), CQueueIterator::CQueueIterator(), CWorkerNodeAffinityGuard::CWorkerNodeAffinityGuard(), CWorkspaceConstPrx::CWorkspaceConstPrx(), IRegistry::ReadLock(), and s_LOCK_Handler().

bool CRWLock::TryReadLock void   ) 
 

Try read lock.

Try to acquire R-lock and return immediately.

Returns:
TRUE if the R-lock has been successfully acquired; FALSE, otherwise.

Definition at line 732 of file ncbimtx.cpp.

References CThreadSystemID::GetCurrent(), CThreadSystemID::IsNot(), m_Count, m_Owner, m_RW, and x_MayAcquireForReading().

Referenced by s_LOCK_Handler().

bool CRWLock::TryWriteLock void   ) 
 

Try write lock.

Try to acquire W-lock and return immediately.

Returns:
TRUE if the W-lock has been successfully acquired; FALSE, otherwise.

Definition at line 869 of file ncbimtx.cpp.

References CThreadSystemID::GetCurrent(), CThreadSystemID::IsNot(), m_Count, m_Owner, and m_RW.

Referenced by s_LOCK_Handler().

void CRWLock::Unlock void   ) 
 

Release the RW-lock.

Definition at line 920 of file ncbimtx.cpp.

References _ASSERT, CThreadSystemID::GetCurrent(), CThreadSystemID::Is(), m_Count, m_Flags, m_Owner, m_Readers, m_RW, m_WaitingWriters, and xncbi_Validate.

Referenced by s_LOCK_Handler(), IRegistry::Unlock(), CProjectConstPrx::~CProjectConstPrx(), CQueueConstIterator::~CQueueConstIterator(), CQueueIterator::~CQueueIterator(), CQueueWorkerNodeListGuard::~CQueueWorkerNodeListGuard(), CWorkerNodeAffinityGuard::~CWorkerNodeAffinityGuard(), and CWorkspaceConstPrx::~CWorkspaceConstPrx().

void CRWLock::WriteLock void   ) 
 

Write lock.

Acquire the W-lock. If R-lock or W-lock is already acquired by another thread, then wait until it is released.

Definition at line 776 of file ncbimtx.cpp.

References CThreadSystemID::GetCurrent(), CThreadSystemID::Is(), m_Count, m_Owner, and m_RW.

Referenced by CProjectPrx::CProjectPrx(), CQueueWorkerNodeListGuard::CQueueWorkerNodeListGuard(), s_LOCK_Handler(), and IRegistry::WriteLock().

bool CRWLock::x_MayAcquireForReading CThreadSystemID  self_id  )  [private]
 

Definition at line 605 of file ncbimtx.cpp.

References _ASSERT, fFavorWriters, CThreadSystemID::GetCurrent(), m_Count, m_Flags, and m_Readers.

Referenced by ReadLock(), and TryReadLock().


Member Data Documentation

volatile int CRWLock::m_Count [private]
 

Number of readers (if >0) or writers (if <0).

Definition at line 976 of file ncbimtx.hpp.

Referenced by ReadLock(), TryReadLock(), TryWriteLock(), Unlock(), WriteLock(), and x_MayAcquireForReading().

TFlags CRWLock::m_Flags [private]
 

Configuration flags.

Definition at line 970 of file ncbimtx.hpp.

Referenced by CRWLock(), Unlock(), and x_MayAcquireForReading().

volatile CThreadSystemID CRWLock::m_Owner [private]
 

Writer ID, one of the readers ID.

Definition at line 974 of file ncbimtx.hpp.

Referenced by ReadLock(), TryReadLock(), TryWriteLock(), Unlock(), and WriteLock().

vector<CThreadSystemID> CRWLock::m_Readers [private]
 

List of all readers or writers for debugging.

Definition at line 982 of file ncbimtx.hpp.

Referenced by CRWLock(), Unlock(), and x_MayAcquireForReading().

auto_ptr<CInternalRWLock> CRWLock::m_RW [private]
 

Platform-dependent RW-lock data.

Definition at line 972 of file ncbimtx.hpp.

Referenced by ReadLock(), TryReadLock(), TryWriteLock(), Unlock(), and WriteLock().

volatile unsigned int CRWLock::m_WaitingWriters [private]
 

Number of writers waiting; zero if not keeping track.

Definition at line 979 of file ncbimtx.hpp.

Referenced by Unlock().


The documentation for this class was generated from the following files:
Generated on Mon Dec 7 13:53:03 2009 for NCBI C++ ToolKit by  doxygen 1.4.6
Modified on Mon Dec 07 16:23:54 2009 by modify_doxy.py rev. 173732