Core/LockGuard: Improve LockGuard
Former-commit-id: e9313d81c8cd9cb2cefef64a7c54713062ad9d6a
This commit is contained in:
parent
1ec8c8c5e0
commit
104e393d65
|
|
@ -13,15 +13,22 @@ namespace Nz
|
||||||
{
|
{
|
||||||
class Mutex;
|
class Mutex;
|
||||||
|
|
||||||
class NAZARA_CORE_API LockGuard
|
class LockGuard
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
LockGuard(Mutex& mutex);
|
inline LockGuard(Mutex& mutex, bool lock = true);
|
||||||
~LockGuard();
|
inline ~LockGuard();
|
||||||
|
|
||||||
|
inline void Lock();
|
||||||
|
inline bool TryLock();
|
||||||
|
inline void Unlock();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Mutex& m_mutex;
|
Mutex& m_mutex;
|
||||||
|
bool m_locked;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include <Nazara/Core/LockGuard.inl>
|
||||||
|
|
||||||
#endif // NAZARA_LOCKGUARD_HPP
|
#endif // NAZARA_LOCKGUARD_HPP
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,83 @@
|
||||||
|
// Copyright (C) 2015 Jérôme Leclercq
|
||||||
|
// This file is part of the "Nazara Engine - Core module"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
#include <Nazara/Core/LockGuard.hpp>
|
||||||
|
#include <Nazara/Core/Error.hpp>
|
||||||
|
#include <Nazara/Core/Mutex.hpp>
|
||||||
|
#include <Nazara/Core/Debug.hpp>
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
/*!
|
||||||
|
* \ingroup core
|
||||||
|
* \class Nz::LockGuard
|
||||||
|
* \brief Core class that represents a mutex wrapper that provides a convenient RAII-style mechanism
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Constructs a LockGuard object with a mutex
|
||||||
|
*
|
||||||
|
* \param mutex Mutex to lock
|
||||||
|
* \param lock Should the mutex be locked by the constructor
|
||||||
|
*/
|
||||||
|
inline LockGuard::LockGuard(Mutex& mutex, bool lock) :
|
||||||
|
m_mutex(mutex),
|
||||||
|
m_locked(false)
|
||||||
|
{
|
||||||
|
if (lock)
|
||||||
|
{
|
||||||
|
m_mutex.Lock();
|
||||||
|
m_locked = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Destructs a LockGuard object and unlocks the mutex if it was previously locked
|
||||||
|
*/
|
||||||
|
inline LockGuard::~LockGuard()
|
||||||
|
{
|
||||||
|
if (m_locked)
|
||||||
|
m_mutex.Unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Locks the underlying mutex
|
||||||
|
*
|
||||||
|
* \see Mutex::Lock
|
||||||
|
*/
|
||||||
|
inline void LockGuard::Lock()
|
||||||
|
{
|
||||||
|
NazaraAssert(!m_locked, "Mutex is already locked");
|
||||||
|
|
||||||
|
m_mutex.Lock();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Tries to lock the underlying mutex
|
||||||
|
*
|
||||||
|
* \see Mutex::TryLock
|
||||||
|
*
|
||||||
|
* \return true if the lock was acquired successfully
|
||||||
|
*/
|
||||||
|
inline bool LockGuard::TryLock()
|
||||||
|
{
|
||||||
|
NazaraAssert(!m_locked, "Mutex is already locked");
|
||||||
|
|
||||||
|
return m_mutex.TryLock();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Unlocks the underlying mutex
|
||||||
|
*
|
||||||
|
* \see Mutex::Unlock
|
||||||
|
*/
|
||||||
|
inline void LockGuard::Unlock()
|
||||||
|
{
|
||||||
|
NazaraAssert(m_locked, "Mutex is not locked");
|
||||||
|
|
||||||
|
m_mutex.Unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Nazara/Core/DebugOff.hpp>
|
||||||
|
|
@ -1,37 +0,0 @@
|
||||||
// Copyright (C) 2015 Jérôme Leclercq
|
|
||||||
// This file is part of the "Nazara Engine - Core module"
|
|
||||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
|
||||||
|
|
||||||
#include <Nazara/Core/LockGuard.hpp>
|
|
||||||
#include <Nazara/Core/Mutex.hpp>
|
|
||||||
#include <Nazara/Core/Debug.hpp>
|
|
||||||
|
|
||||||
namespace Nz
|
|
||||||
{
|
|
||||||
/*!
|
|
||||||
* \ingroup core
|
|
||||||
* \class Nz::LockGuard
|
|
||||||
* \brief Core class that represents a mutex wrapper that provides a convenient RAII-style mechanism
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*!
|
|
||||||
* \brief Constructs a LockGuard object with a mutex
|
|
||||||
*
|
|
||||||
* \param mutex Mutex to lock
|
|
||||||
*/
|
|
||||||
|
|
||||||
LockGuard::LockGuard(Mutex& mutex) :
|
|
||||||
m_mutex(mutex)
|
|
||||||
{
|
|
||||||
m_mutex.Lock();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
* \brief Destructs a LockGuard object and unlocks the mutex
|
|
||||||
*/
|
|
||||||
|
|
||||||
LockGuard::~LockGuard()
|
|
||||||
{
|
|
||||||
m_mutex.Unlock();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue