Public Member Functions | Private Types | Private Attributes

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

Is a template to lock object by its ID. More...

#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.

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

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 82 of file cmn_LockID.h.


Member Typedef Documentation

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

Definition at line 99 of file cmn_LockID.h.


Constructor & Destructor Documentation

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

Definition at line 84 of file cmn_LockID.h.

        : 
        m_releasedLockID_c(&m_lockID_x) {
        this->reserve(100);
    // Empty
    }

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

Definition at line 90 of file cmn_LockID.h.

        : 
        m_releasedLockID_c(&m_lockID_x) {
        this->reserve(a_size);
    // Empty
    }


Member Function Documentation

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

Definition at line 125 of file cmn_LockID.h.

References cmn_LockID< _T >::m_lockID_x.

                               {
        cmn_MutexLock l(m_lockID_x);

        cmn_LockID_v_i iter  = this->begin();
        cmn_LockID_v_i endel = this->end();
        while (iter != endel) {
            if (*iter == a_ID) {
                return false;
             }
             iter++;
        }
        insert(endel, a_ID);
        return true;
    };

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

Definition at line 107 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().

                                     {
        cmn_MutexLock l(m_lockID_x);

loop:
        cmn_LockID_v_i endel = this->end();
        cmn_LockID_v_i iter  = this->begin();
        while (iter != this->end()) { // don't know why but using end() is faster than endel
            if (*iter == a_ID) {
                m_releasedLockID_c.Wait();
                goto loop;
             }
             iter++;
        }
        insert(endel, a_ID); // here is oposite
    };

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 142 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().

                                       {
        cmn_MutexLock l(m_lockID_x);

        cmn_LockID_v_i endel = this->end();
        cmn_LockID_v_i iter  = this->begin();
        while (iter != this->end()) {  // call end() is optimized by compiler ??? it is faster than endel use
            if (*iter == a_ID) {
                goto remove;
             }
             iter++;
        }
//      exit from while loop mean error
        throw ivd_InternalError(ie_INVALID_ARG, 
            "Tried to unlock ID which hasn't been locked.");

remove:
        if (iter != --endel) { // endel become last
            *iter = *endel;  // if iter is not last move last to current
        }
        erase(endel);  // remove last

        m_releasedLockID_c.Broadcast(); //TODO where to set timeout period, use ETIMEDOUT
    }

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 103 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: