cmn_LockID< _T > Class Template Reference
[Common, basic classes, functions and types]

#include <cmn_LockID.h>

Inheritance diagram for cmn_LockID< _T >:

Inheritance graph
[legend]
Collaboration diagram for cmn_LockID< _T >:

Collaboration graph
[legend]

List of all members.


Detailed Description

template<class _T>
class cmn_LockID< _T >

Is a template to lock object by its ID.

Alghorith use vector<_T> for locking elements, thus ID can be any type that vector can handle it.

Instantiation of lockID object

    cmn_LockID<ivd_RecordIDX_t> m_LockByRecordIdx;
    

Lock element with index 1

    ivd_RecordIdx_t idx = 1;
    m_LockByRecordIdx.LockByID(idx);
    

Try to lock and if lock it. The return value is true if locked othervice is false.

    bool locked = m_LockByRecordIdx.CanLockByID(<elementID>);
    

UnLock element with index 1

    ivd_RecordIdx_t idx = 1;
    m_LockByRecordIdx.UnLockByID(idx);
    

Definition at line 68 of file cmn_LockID.h.


Public Member Functions

 cmn_LockID ()
 cmn_LockID (int a_size)
void LockByID (register _T &a_ID)
bool CanLockByID (_T &a_ID)
void UnLockByID (register _T &a_ID)

Private Types

typedef cmn_LockID< _T >::iterator cmn_LockID_v_i

Private Attributes

cmn_Mutex m_lockID_x
cmn_Condition m_releasedLockID_c

Member Typedef Documentation

template<class _T>
typedef cmn_LockID<_T>::iterator cmn_LockID< _T >::cmn_LockID_v_i [private]

Definition at line 85 of file cmn_LockID.h.


Constructor & Destructor Documentation

template<class _T>
cmn_LockID< _T >::cmn_LockID (  )  [inline]

Definition at line 70 of file cmn_LockID.h.

00071         : 
00072         m_releasedLockID_c(&m_lockID_x) {
00073         this->reserve(100);
00074     // Empty
00075     }

template<class _T>
cmn_LockID< _T >::cmn_LockID ( int  a_size  )  [inline]

Definition at line 76 of file cmn_LockID.h.

00077         : 
00078         m_releasedLockID_c(&m_lockID_x) {
00079         this->reserve(a_size);
00080     // Empty
00081     }


Member Function Documentation

template<class _T>
void cmn_LockID< _T >::LockByID ( register _T &  a_ID  )  [inline]

Definition at line 93 of file cmn_LockID.h.

References cmn_LockID< _T >::m_lockID_x, cmn_LockID< _T >::m_releasedLockID_c, and cmn_Condition::Wait().

Referenced by fsc_fileIDLock::fsc_fileIDLock().

00093                                      {
00094         cmn_MutexLock l(m_lockID_x);
00095 
00096 loop:
00097         cmn_LockID_v_i endel = this->end();
00098         cmn_LockID_v_i iter  = this->begin();
00099         while (iter != this->end()) { // don't know why but using end() is faster than endel
00100             if (*iter == a_ID) {
00101                 m_releasedLockID_c.Wait();
00102                 goto loop;
00103              }
00104              iter++;
00105         }
00106         insert(endel, a_ID); // here is oposite
00107     };

Here is the call graph for this function:

Here is the caller graph for this function:

template<class _T>
bool cmn_LockID< _T >::CanLockByID ( _T &  a_ID  )  [inline]

Definition at line 111 of file cmn_LockID.h.

References cmn_LockID< _T >::m_lockID_x.

00111                                {
00112         cmn_MutexLock l(m_lockID_x);
00113 
00114         cmn_LockID_v_i iter  = this->begin();
00115         cmn_LockID_v_i endel = this->end();
00116         while (iter != endel) {
00117             if (*iter == a_ID) {
00118                 return false;
00119              }
00120              iter++;
00121         }
00122         insert(endel, a_ID);
00123         return true;
00124     };

template<class _T>
void cmn_LockID< _T >::UnLockByID ( register _T &  a_ID  )  [inline]

Definition at line 128 of file cmn_LockID.h.

References cmn_Condition::Broadcast(), ie_INVALID_ARG, cmn_LockID< _T >::m_lockID_x, and cmn_LockID< _T >::m_releasedLockID_c.

Referenced by fsc_fileIDLock::~fsc_fileIDLock().

00128                                        {
00129         cmn_MutexLock l(m_lockID_x);
00130 
00131         cmn_LockID_v_i endel = this->end();
00132         cmn_LockID_v_i iter  = this->begin();
00133         while (iter != this->end()) {  // call end() is optimized by compiler ??? it is faster than endel use
00134             if (*iter == a_ID) {
00135                 goto remove;
00136              }
00137              iter++;
00138         }
00139 //      exit from while loop mean error
00140         throw ivd_InternalError(ie_INVALID_ARG, 
00141             "Tried to unlock ID which hasn't been locked.");
00142 
00143 remove:
00144         if (iter != --endel) { // endel become last
00145             *iter = *endel;  // if iter is not last move last to current
00146         }
00147         erase(endel);  // remove last
00148 
00149         m_releasedLockID_c.Broadcast(); //TODO where to set timeout period, use ETIMEDOUT
00150     }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

template<class _T>
cmn_Mutex cmn_LockID< _T >::m_lockID_x [private]

template<class _T>
cmn_Condition cmn_LockID< _T >::m_releasedLockID_c [private]

Definition at line 89 of file cmn_LockID.h.

Referenced by cmn_LockID< _T >::LockByID(), and cmn_LockID< _T >::UnLockByID().


The documentation for this class was generated from the following file:

Generated on Mon Feb 27 19:05:08 2012 for OPENARCHIVE by  doxygen 1.5.6