336 lines
9.6 KiB
C++
336 lines
9.6 KiB
C++
/**
|
|
* @file include/ActivMiddleware/ActivBase/MutexHelper.h
|
|
*
|
|
* @brief Header file for the mutex helper classes.
|
|
*/
|
|
|
|
#if (!defined ACTIV_BASE_MUTEX_HELPER_H)
|
|
#define ACTIV_BASE_MUTEX_HELPER_H
|
|
|
|
#include "ActivMiddleware/ActivBase/IMutex.h"
|
|
|
|
#include ACTIV_BOOST_INCLUDE(utility/explicit_operator_bool.hpp)
|
|
|
|
#include <cassert>
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
/**
|
|
* @brief Scoped mutex locking helper class.
|
|
*
|
|
* The ScopedMutexLocker class wraps up locking / unlocking a mutex.
|
|
*/
|
|
class ScopedMutexLocker : private Boost::noncopyable
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor which obtains the mutex lock.
|
|
*
|
|
* @param pMutex pointer to the mutex to be locked. It can be null, in which case this is a no-op (as is the dtor).
|
|
*/
|
|
ScopedMutexLocker(IMutex * const pMutex);
|
|
|
|
/**
|
|
* @brief Constructor which obtains the mutex lock.
|
|
*
|
|
* @param pMutex pointer to the mutex to be locked. It can be null, in which case this is a no-op (as is the dtor).
|
|
*/
|
|
ScopedMutexLocker(const IMutex * const pMutex);
|
|
|
|
/**
|
|
* @brief Constructor which obtains the mutex lock.
|
|
*
|
|
* @param pMutex smart pointer to the mutex to be locked. It can be null, in which case this is a no-op (as is the dtor).
|
|
*/
|
|
template<typename T>
|
|
ScopedMutexLocker(T &pMutex);
|
|
|
|
//! Destructor releases the mutex lock, if a non-null mutex pointer was provided to the constructor.
|
|
~ScopedMutexLocker();
|
|
|
|
private:
|
|
const IMutex * const m_pMutex; ///< The mutex object.
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline ScopedMutexLocker::ScopedMutexLocker(IMutex * const pMutex) :
|
|
m_pMutex(pMutex)
|
|
{
|
|
if (0 != m_pMutex)
|
|
(void) m_pMutex->Acquire();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline ScopedMutexLocker::ScopedMutexLocker(const IMutex * const pMutex) :
|
|
m_pMutex(pMutex)
|
|
{
|
|
if (0 != m_pMutex)
|
|
(void) m_pMutex->Acquire();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
inline ScopedMutexLocker::ScopedMutexLocker(T &pMutex) :
|
|
m_pMutex(pMutex.get())
|
|
{
|
|
if (0 != m_pMutex)
|
|
(void) m_pMutex->Acquire();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline ScopedMutexLocker::~ScopedMutexLocker()
|
|
{
|
|
if (0 != m_pMutex)
|
|
(void) m_pMutex->Release();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Try acquire mutex helper class.
|
|
*
|
|
* The ScopedMutexTryLocker class wraps up try-locking / unlocking a mutex.
|
|
*/
|
|
class ScopedMutexTryLocker : private Boost::noncopyable
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor. The constructor attempts to lock the mutex.
|
|
*
|
|
* @param pMutex pointer to the mutex to be locked. It can be null. If a null mutex is provided, it is
|
|
* treated as if it is "locked". I.e. operator bool() will return true.
|
|
*/
|
|
ScopedMutexTryLocker(IMutex * const pMutex);
|
|
|
|
/**
|
|
* @brief Constructor. The constructor attempts to lock the mutex.
|
|
*
|
|
* @param pMutex pointer to the mutex to be locked. It can be null. If a null mutex is provided, it is
|
|
* treated as if it is "locked". I.e. operator bool() will return true.
|
|
*/
|
|
ScopedMutexTryLocker(const IMutex * const pMutex);
|
|
|
|
/**
|
|
* @brief Constructor. The constructor attempts to lock the mutex.
|
|
*
|
|
* @param pMutex smart pointer to the mutex to be locked. It can be null. If a null mutex is provided, it is
|
|
* treated as if it is "locked". I.e. operator bool() will return true.
|
|
*/
|
|
template<typename T>
|
|
ScopedMutexTryLocker(T &pMutex);
|
|
|
|
//! Destructor releases the mutex lock, if the lock was acquired.
|
|
~ScopedMutexTryLocker();
|
|
|
|
/**
|
|
* @brief Acquire the mutex.
|
|
*
|
|
* If the constructor acquired the mutex, this is a no-op. Otherwise, it will block until the mutex is unlocked
|
|
* by another thread.
|
|
*/
|
|
void Acquire();
|
|
|
|
//! Is the mutex locked?
|
|
ACTIV_BOOST_EXPLICIT_OPERATOR_BOOL()
|
|
|
|
//! Is the mutex not locked?
|
|
bool operator!() const;
|
|
|
|
private:
|
|
const IMutex * const m_pMutex; ///< The mutex object.
|
|
bool m_isLocked;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline ScopedMutexTryLocker::ScopedMutexTryLocker(IMutex * const pMutex) :
|
|
m_pMutex(pMutex),
|
|
m_isLocked((0 != m_pMutex) ? (STATUS_CODE_SUCCESS == m_pMutex->Try()) : true)
|
|
{
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline ScopedMutexTryLocker::ScopedMutexTryLocker(const IMutex * const pMutex) :
|
|
m_pMutex(pMutex),
|
|
m_isLocked((0 != m_pMutex) ? (STATUS_CODE_SUCCESS == m_pMutex->Try()) : true)
|
|
{
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
inline ScopedMutexTryLocker::ScopedMutexTryLocker(T &pMutex) :
|
|
m_pMutex(pMutex.get()),
|
|
m_isLocked((0 != m_pMutex) ? (STATUS_CODE_SUCCESS == m_pMutex->Try()) : true)
|
|
{
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline ScopedMutexTryLocker::~ScopedMutexTryLocker()
|
|
{
|
|
if ((0 != m_pMutex) && m_isLocked)
|
|
(void) m_pMutex->Release();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void ScopedMutexTryLocker::Acquire()
|
|
{
|
|
if ((0 != m_pMutex) && !m_isLocked)
|
|
{
|
|
(void) m_pMutex->Acquire();
|
|
m_isLocked = true;
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool ScopedMutexTryLocker::operator!() const
|
|
{
|
|
return !m_isLocked;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Deferred acquire mutex helper class.
|
|
*
|
|
* This version does not attempt to acquire the mutex in the constructor, it is left
|
|
* to a later method call.
|
|
*/
|
|
class DeferredScopedMutexLocker : private Boost::noncopyable
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param pMutex pointer to the mutex to be locked. If a null mutex is provided, Try() will always succeed.
|
|
*/
|
|
DeferredScopedMutexLocker(IMutex * const pMutex);
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param pMutex pointer to the mutex to be locked. If a null mutex is provided, Try() will always succeed.
|
|
*/
|
|
DeferredScopedMutexLocker(const IMutex * const pMutex);
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param pMutex smart pointer to the mutex to be locked. If a null mutex is provided, Try() will always succeed.
|
|
*/
|
|
template<typename T>
|
|
DeferredScopedMutexLocker(T &pMutex);
|
|
|
|
//! Destructor releases the mutex lock, if the lock was acquired.
|
|
~DeferredScopedMutexLocker();
|
|
|
|
//! Acquire the mutex.
|
|
void Acquire();
|
|
|
|
/**
|
|
* @brief Attempt to acquire the mutex.
|
|
*
|
|
* @return True if the mutex was already locked by this thread, or was successfully locked by this call.
|
|
*/
|
|
bool Try();
|
|
|
|
//! Is the mutex locked?
|
|
ACTIV_BOOST_EXPLICIT_OPERATOR_BOOL()
|
|
|
|
//! Is the mutex not locked?
|
|
bool operator!() const;
|
|
|
|
private:
|
|
const IMutex * const m_pMutex; ///< The mutex object.
|
|
bool m_isLocked;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline DeferredScopedMutexLocker::DeferredScopedMutexLocker(IMutex * const pMutex) :
|
|
m_pMutex(pMutex),
|
|
m_isLocked(false)
|
|
{
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline DeferredScopedMutexLocker::DeferredScopedMutexLocker(const IMutex * const pMutex) :
|
|
m_pMutex(pMutex),
|
|
m_isLocked(false)
|
|
{
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
inline DeferredScopedMutexLocker::DeferredScopedMutexLocker(T &pMutex) :
|
|
m_pMutex(pMutex.get()),
|
|
m_isLocked(false)
|
|
{
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline DeferredScopedMutexLocker::~DeferredScopedMutexLocker()
|
|
{
|
|
if ((0 != m_pMutex) && m_isLocked)
|
|
(void) m_pMutex->Release();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void DeferredScopedMutexLocker::Acquire()
|
|
{
|
|
if (!m_isLocked)
|
|
{
|
|
if (0 != m_pMutex)
|
|
(void) m_pMutex->Acquire();
|
|
|
|
m_isLocked = true;
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool DeferredScopedMutexLocker::Try()
|
|
{
|
|
if (!m_isLocked)
|
|
{
|
|
if (0 != m_pMutex)
|
|
{
|
|
if (STATUS_CODE_SUCCESS == m_pMutex->Try())
|
|
m_isLocked = true;
|
|
}
|
|
else
|
|
{
|
|
m_isLocked = true;
|
|
}
|
|
}
|
|
|
|
return m_isLocked;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool DeferredScopedMutexLocker::operator!() const
|
|
{
|
|
return !m_isLocked;
|
|
}
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_BASE_MUTEX_HELPER_H)
|