#include <MboSynch.h>
Public Member Functions | |
CPotentialLock (const IMutex *pmx, DWORD nMilliseconds=0, bool bThrowAlreadyEntered=true) throw ( logic_error , CAlreadyEntered ) | |
CPotentialLock (const CPotentialLock &lx) throw ( logic_error , CAlreadyEntered ) | |
virtual | ~CPotentialLock () throw ( logic_error ) |
virtual bool | IsValid () const |
virtual void | Enter () const |
virtual void | Leave () const throw ( logic_error ) |
virtual unsigned long | GetLevel () const |
Protected Member Functions | |
virtual void | Enter (DWORD nMilliseconds, bool bThrowAlreadyEntered=true) const throw ( logic_error , CAlreadyEntered ) |
This lock operates on a Mutex rather than a CriticalSection hence it is recognizeable slower than using normal locks with CriticalSections.
This lock tries to enter the mutex. If entering fails IsValid() will return false. For a valid lock IsValid() will even return true if it is temporarily unlocked with an CUnlock. Thus IsValid() does not say anything about the enter level of the underlying mutex.
The lock can operate in two ways. First setting bThrowAlreadyEntered true results in throwing an exception of type CAlreadyEntered once when the constructor cannot enter the mutex in the specified time. Therefor the block in which the lock is being initalized automatically gets destructed and no operation with the underlying mutex being unentered is possible. In contrast setting bThrowAlreadyEntered false does not terminate that stackframe and hence requires the user to check IsValid() before continuing. Thus the second is dangerous and not recommended - however it allows to omit the try/catch block and can therefor be much faster (which is the reason for its existence).
Definition at line 544 of file MboSynch.h.
mbo::CPotentialLock::CPotentialLock | ( | const IMutex * | pmx, | |
DWORD | nMilliseconds = 0 , |
|||
bool | bThrowAlreadyEntered = true | |||
) | throw ( logic_error , CAlreadyEntered ) [inline, explicit] |
constructor to allow selective call to Enter()
/*! default constructor which directly calls Enter()
pmx | Mutex to use for Lock | |
nMilliseconds | time to try to enter in milliseconds | |
bThrowAlreadyEntered | throws an exception if another thread has entered the mutex already |
CAlreadyLocked | if another thread has entered the mutex already and parameter bThrowAlreadyEntered is set true. | |
logic_error | if mutex was not entered in constructor |
Definition at line 562 of file MboSynch.h.
00567 : m_pmx(const_cast<IMutex*>(pmx)) 00568 { 00569 assert(pmx); 00570 Enter(nMilliseconds, bThrowAlreadyEntered); 00571 }
mbo::CPotentialLock::CPotentialLock | ( | const CPotentialLock & | lx | ) | throw ( logic_error , CAlreadyEntered ) [inline, explicit] |
copy constructor
Ensure the lock is entered since the destructor will call Leave().
lx | reference to lock to be copied |
CAlreadyLocked | if another thread has entered the mutex already and parameter bThrowAlreadyEntered is set true. | |
logic_error | if mutex was not entered in constructor |
Definition at line 583 of file MboSynch.h.
00586 : m_pmx(lx.m_pmx) 00587 { 00588 if (m_pmx) 00589 { 00590 Enter(INFINITE, true); 00591 } 00592 }
virtual mbo::CPotentialLock::~CPotentialLock | ( | ) | throw ( logic_error ) [inline, virtual] |
destructor
logic_error | if mutex was not entered in constructor |
Definition at line 597 of file MboSynch.h.
00598 { 00599 // Otherwise destructor would throw an exception. Since that might 00600 // happen in an exception handler that would lead to an unconditious 00601 // termination. 00602 if (m_pmx) 00603 { 00604 Leave(); 00605 } 00606 }
virtual bool mbo::CPotentialLock::IsValid | ( | ) | const [inline, virtual] |
virtual void mbo::CPotentialLock::Enter | ( | DWORD | nMilliseconds, | |
bool | bThrowAlreadyEntered = true | |||
) | const throw ( logic_error , CAlreadyEntered ) [inline, protected, virtual] |
enter the Lock
nMilliseconds | time to try to enter in milliseconds | |
bThrowAlreadyEntered | throws an exception if another thread has entered the mutex already |
CAlreadyLocked | if another thread has entered the mutex already and parameter bThrowAlreadyEntered is set true. | |
logic_error | if mutex was not entered in constructor |
Definition at line 630 of file MboSynch.h.
00634 { 00635 if (!m_pmx) 00636 { 00637 throw logic_error("CPotentialLock is invalid"); 00638 } 00639 if (!m_pmx->Try(nMilliseconds)) 00640 { 00641 m_pmx = NULL; 00642 if (bThrowAlreadyEntered) 00643 { 00644 throw CAlreadyEntered(); 00645 } 00646 } 00647 }
virtual void mbo::CPotentialLock::Enter | ( | ) | const [inline, virtual] |
default enter lock implements ILock::Enter()
Implements mbo::ILock.
Definition at line 654 of file MboSynch.h.
00655 { 00656 Enter(INFINITE,true); 00657 }
virtual void mbo::CPotentialLock::Leave | ( | ) | const throw ( logic_error ) [inline, virtual] |
leave the Lock
logic_error | if mutex was not entered in constructor |
Implements mbo::ILock.
Definition at line 662 of file MboSynch.h.
00663 { 00664 if (!m_pmx) 00665 { 00666 throw logic_error("CPotentialLock is invalid"); 00667 } 00668 m_pmx->Leave(); 00669 }
virtual unsigned long mbo::CPotentialLock::GetLevel | ( | ) | const [inline, virtual] |
This may not be correctly available in which case 1 is returned to signal Enter state.
Implements mbo::ILock.
Definition at line 672 of file MboSynch.h.
Hosted on code.google.com | © Marcus Börger | Generated on Fri Jan 18 21:21:12 2008 for MBO-lib by ![]() |