Allow locks to be inlined

refs #7622
This commit is contained in:
Gunnar Beutner 2014-11-12 06:33:20 +01:00
parent ecd31b86ad
commit 02a7d97b17
5 changed files with 51 additions and 93 deletions

View File

@ -26,7 +26,7 @@ set(base_SOURCES
application.cpp application.thpp array.cpp configerror.cpp console.cpp context.cpp
convert.cpp debuginfo.cpp dictionary.cpp dynamicobject.cpp dynamicobject.thpp dynamictype.cpp
exception.cpp fifo.cpp filelogger.cpp filelogger.thpp initialize.cpp json.cpp logger.cpp logger.thpp
netstring.cpp networkstream.cpp object.cpp objectlock.cpp primitivetype.cpp process.cpp
netstring.cpp networkstream.cpp object.cpp primitivetype.cpp process.cpp
ringbuffer.cpp scriptfunction.cpp scriptfunctionwrapper.cpp
scriptutils.cpp scriptvariable.cpp serializer.cpp socket.cpp stacktrace.cpp
statsfunction.cpp stdiostream.cpp stream.cpp streamlogger.cpp streamlogger.thpp string.cpp

View File

@ -25,10 +25,6 @@ using namespace icinga;
REGISTER_PRIMITIVE_TYPE(Object);
#ifdef _DEBUG
boost::mutex Object::m_DebugMutex;
#endif /* _DEBUG */
/**
* Default constructor for the Object class.
*/
@ -53,8 +49,7 @@ Object::~Object(void)
*/
bool Object::OwnsLock(void) const
{
boost::mutex::scoped_lock lock(m_DebugMutex);
// TODO: barrier before reading m_Locked
return (m_Locked && m_LockOwner == boost::this_thread::get_id());
}
#endif /* _DEBUG */

View File

@ -109,7 +109,6 @@ private:
mutable ThinMutex m_Mutex;
#ifdef _DEBUG
static boost::mutex m_DebugMutex;
mutable bool m_Locked;
mutable boost::thread::id m_LockOwner;
#endif /* _DEBUG */

View File

@ -1,79 +0,0 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#include "base/objectlock.hpp"
#include "base/debug.hpp"
using namespace icinga;
ObjectLock::ObjectLock(void)
: m_Object(NULL), m_Locked(false)
{ }
ObjectLock::~ObjectLock(void)
{
Unlock();
}
ObjectLock::ObjectLock(const Object::Ptr& object)
: m_Object(object.get()), m_Locked(false)
{
if (m_Object)
Lock();
}
ObjectLock::ObjectLock(const Object *object)
: m_Object(object), m_Locked(false)
{
if (m_Object)
Lock();
}
void ObjectLock::Lock(void)
{
ASSERT(!m_Locked && m_Object != NULL);
ASSERT(!m_Object->OwnsLock());
m_Object->m_Mutex.Lock();
m_Locked = true;
#ifdef _DEBUG
{
boost::mutex::scoped_lock lock(Object::m_DebugMutex);
m_Object->m_Locked = true;
m_Object->m_LockOwner = boost::this_thread::get_id();
}
#endif /* _DEBUG */
}
void ObjectLock::Unlock(void)
{
#ifdef _DEBUG
{
boost::mutex::scoped_lock lock(Object::m_DebugMutex);
if (m_Locked)
m_Object->m_Locked = false;
}
#endif /* _DEBUG */
if (m_Locked) {
m_Object->m_Mutex.Unlock();
m_Locked = false;
}
}

View File

@ -30,13 +30,56 @@ namespace icinga
*/
struct I2_BASE_API ObjectLock {
public:
ObjectLock(void);
ObjectLock(const Object::Ptr& object);
ObjectLock(const Object *object);
~ObjectLock(void);
inline ObjectLock(void)
: m_Object(NULL), m_Locked(false)
{ }
void Lock(void);
void Unlock(void);
inline ~ObjectLock(void)
{
Unlock();
}
inline ObjectLock(const Object::Ptr& object)
: m_Object(object.get()), m_Locked(false)
{
if (m_Object)
Lock();
}
inline ObjectLock(const Object *object)
: m_Object(object), m_Locked(false)
{
if (m_Object)
Lock();
}
inline void Lock(void)
{
ASSERT(!m_Locked && m_Object != NULL);
ASSERT(!m_Object->OwnsLock());
m_Object->m_Mutex.Lock();
m_Locked = true;
#ifdef _DEBUG
m_Object->m_Locked = true;
// TODO: barrier after writing m_Locked
m_Object->m_LockOwner = boost::this_thread::get_id();
#endif /* _DEBUG */
}
inline void Unlock(void)
{
#ifdef _DEBUG
if (m_Locked)
m_Object->m_Locked = false;
#endif /* _DEBUG */
if (m_Locked) {
m_Object->m_Mutex.Unlock();
m_Locked = false;
}
}
private:
const Object *m_Object;