From 02a7d97b17c22cc01bd1b6e2725d6dbde4b37332 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Wed, 12 Nov 2014 06:33:20 +0100 Subject: [PATCH] Allow locks to be inlined refs #7622 --- lib/base/CMakeLists.txt | 2 +- lib/base/object.cpp | 7 +--- lib/base/object.hpp | 1 - lib/base/objectlock.cpp | 79 ----------------------------------------- lib/base/objectlock.hpp | 55 ++++++++++++++++++++++++---- 5 files changed, 51 insertions(+), 93 deletions(-) delete mode 100644 lib/base/objectlock.cpp diff --git a/lib/base/CMakeLists.txt b/lib/base/CMakeLists.txt index e9f388f12..293dea452 100644 --- a/lib/base/CMakeLists.txt +++ b/lib/base/CMakeLists.txt @@ -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 diff --git a/lib/base/object.cpp b/lib/base/object.cpp index b36bd4a59..5d2b31f7c 100644 --- a/lib/base/object.cpp +++ b/lib/base/object.cpp @@ -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 */ diff --git a/lib/base/object.hpp b/lib/base/object.hpp index 2b6bb8c93..fbaaf8640 100644 --- a/lib/base/object.hpp +++ b/lib/base/object.hpp @@ -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 */ diff --git a/lib/base/objectlock.cpp b/lib/base/objectlock.cpp deleted file mode 100644 index 17a7a4995..000000000 --- a/lib/base/objectlock.cpp +++ /dev/null @@ -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; - } -} diff --git a/lib/base/objectlock.hpp b/lib/base/objectlock.hpp index bcb293830..3aa0e39b1 100644 --- a/lib/base/objectlock.hpp +++ b/lib/base/objectlock.hpp @@ -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;