Implement support for recursive object locks

fixes #10596
This commit is contained in:
Gunnar Beutner 2015-11-11 10:21:30 +01:00
parent 356c4cc170
commit e3c75faabc
17 changed files with 6 additions and 275 deletions

View File

@ -34,7 +34,7 @@ set(base_SOURCES
function.cpp function-script.cpp functionwrapper.cpp scriptglobal.cpp
scriptutils.cpp serializer.cpp socket.cpp socketevents.cpp stacktrace.cpp
statsfunction.cpp stdiostream.cpp stream.cpp streamlogger.cpp streamlogger.thpp string.cpp string-script.cpp
sysloglogger.cpp sysloglogger.thpp tcpsocket.cpp thinmutex.cpp threadpool.cpp timer.cpp
sysloglogger.cpp sysloglogger.thpp tcpsocket.cpp threadpool.cpp timer.cpp
tlsstream.cpp tlsutility.cpp type.cpp typetype-script.cpp unixsocket.cpp utility.cpp value.cpp
value-operators.cpp workqueue.cpp
)

View File

@ -982,7 +982,6 @@ int Application::Run(void)
*/
void Application::UpdatePidFile(const String& filename, pid_t pid)
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
if (m_PidFile != NULL)
@ -1038,7 +1037,6 @@ void Application::UpdatePidFile(const String& filename, pid_t pid)
*/
void Application::ClosePidFile(bool unlink)
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
if (m_PidFile != NULL)

View File

@ -37,7 +37,6 @@ REGISTER_PRIMITIVE_TYPE(Array, Object, Array::GetPrototype());
*/
Value Array::Get(unsigned int index) const
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
return m_Data.at(index);
@ -51,7 +50,6 @@ Value Array::Get(unsigned int index) const
*/
void Array::Set(unsigned int index, const Value& value)
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
m_Data.at(index) = value;
@ -64,7 +62,6 @@ void Array::Set(unsigned int index, const Value& value)
*/
void Array::Add(const Value& value)
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
m_Data.push_back(value);
@ -77,7 +74,6 @@ void Array::Add(const Value& value)
*/
size_t Array::GetLength(void) const
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
return m_Data.size();
@ -91,7 +87,6 @@ size_t Array::GetLength(void) const
*/
bool Array::Contains(const Value& value) const
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
return (std::find(m_Data.begin(), m_Data.end(), value) != m_Data.end());
@ -105,7 +100,6 @@ bool Array::Contains(const Value& value) const
*/
void Array::Insert(unsigned int index, const Value& value)
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
ASSERT(index <= m_Data.size());
@ -120,7 +114,6 @@ void Array::Insert(unsigned int index, const Value& value)
*/
void Array::Remove(unsigned int index)
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
m_Data.erase(m_Data.begin() + index);
@ -140,7 +133,6 @@ void Array::Remove(Array::Iterator it)
void Array::Resize(size_t new_size)
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
m_Data.resize(new_size);
@ -148,7 +140,6 @@ void Array::Resize(size_t new_size)
void Array::Clear(void)
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
m_Data.clear();
@ -156,7 +147,6 @@ void Array::Clear(void)
void Array::Reserve(size_t new_size)
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
m_Data.reserve(new_size);
@ -164,7 +154,6 @@ void Array::Reserve(size_t new_size)
void Array::CopyTo(const Array::Ptr& dest) const
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
ObjectLock xlock(dest);

View File

@ -378,7 +378,6 @@ void ConfigObject::Start(bool runtimeCreated)
{
ObjectImpl<ConfigObject>::Start(runtimeCreated);
ASSERT(!OwnsLock());
ObjectLock olock(this);
SetStartCalled(true);
@ -388,8 +387,6 @@ void ConfigObject::Activate(bool runtimeCreated)
{
CONTEXT("Activating object '" + GetName() + "' of type '" + GetType()->GetName() + "'");
ASSERT(!OwnsLock());
Start(runtimeCreated);
ASSERT(GetStartCalled());
@ -409,7 +406,6 @@ void ConfigObject::Stop(bool runtimeRemoved)
{
ObjectImpl<ConfigObject>::Stop(runtimeRemoved);
ASSERT(!OwnsLock());
ObjectLock olock(this);
SetStopCalled(true);
@ -419,8 +415,6 @@ void ConfigObject::Deactivate(bool runtimeRemoved)
{
CONTEXT("Deactivating object '" + GetName() + "' of type '" + GetType()->GetName() + "'");
ASSERT(!OwnsLock());
SetAuthority(false);
{

View File

@ -28,9 +28,7 @@ using namespace icinga;
ConfigType::ConfigType(const String& name)
: m_Name(name)
{
InflateMutex();
}
{ }
ConfigType::Ptr ConfigType::GetByName(const String& name)
{

View File

@ -36,7 +36,6 @@ REGISTER_PRIMITIVE_TYPE(Dictionary, Object, Dictionary::GetPrototype());
*/
Value Dictionary::Get(const String& key) const
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
std::map<String, Value>::const_iterator it = m_Data.find(key);
@ -56,7 +55,6 @@ Value Dictionary::Get(const String& key) const
*/
bool Dictionary::Get(const String& key, Value *result) const
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
std::map<String, Value>::const_iterator it = m_Data.find(key);
@ -76,7 +74,6 @@ bool Dictionary::Get(const String& key, Value *result) const
*/
void Dictionary::Set(const String& key, const Value& value)
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
m_Data[key] = value;
@ -90,7 +87,6 @@ void Dictionary::Set(const String& key, const Value& value)
*/
size_t Dictionary::GetLength(void) const
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
return m_Data.size();
@ -104,7 +100,6 @@ size_t Dictionary::GetLength(void) const
*/
bool Dictionary::Contains(const String& key) const
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
return (m_Data.find(key) != m_Data.end());
@ -117,7 +112,6 @@ bool Dictionary::Contains(const String& key) const
*/
void Dictionary::Remove(const String& key)
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
Dictionary::Iterator it;
@ -134,7 +128,6 @@ void Dictionary::Remove(const String& key)
*/
void Dictionary::Clear(void)
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
m_Data.clear();
@ -142,7 +135,6 @@ void Dictionary::Clear(void)
void Dictionary::CopyTo(const Dictionary::Ptr& dest) const
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
BOOST_FOREACH(const Dictionary::Pair& kv, m_Data) {
@ -188,7 +180,6 @@ Object::Ptr Dictionary::Clone(void) const
*/
std::vector<String> Dictionary::GetKeys(void) const
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
std::vector<String> keys;

View File

@ -71,11 +71,6 @@ bool Object::OwnsLock(void) const
}
#endif /* I2_DEBUG */
void Object::InflateMutex(void)
{
m_Mutex.Inflate();
}
void Object::SetField(int id, const Value&, bool, const Value&)
{
if (id == 0)

View File

@ -22,16 +22,8 @@
#include "base/i2-base.hpp"
#include "base/debug.hpp"
#include "base/thinmutex.hpp"
#ifndef I2_DEBUG
#include <boost/thread/mutex.hpp>
#else /* I2_DEBUG */
#include <boost/thread/recursive_mutex.hpp>
#endif /* I2_DEBUG */
#include <boost/thread/condition_variable.hpp>
#include <boost/smart_ptr/intrusive_ptr.hpp>
using boost::intrusive_ptr;
@ -115,8 +107,6 @@ public:
bool OwnsLock(void) const;
#endif /* I2_DEBUG */
void InflateMutex(void);
static Object::Ptr GetPrototype(void);
virtual Object::Ptr Clone(void) const;
@ -128,7 +118,7 @@ private:
Object& operator=(const Object& rhs);
uintptr_t m_References;
mutable ThinMutex m_Mutex;
mutable boost::recursive_mutex m_Mutex;
#ifdef I2_DEBUG
# ifndef _WIN32

View File

@ -56,9 +56,8 @@ public:
inline void Lock(void)
{
ASSERT(!m_Locked && m_Object != NULL);
ASSERT(!m_Object->OwnsLock());
m_Object->m_Mutex.Lock();
m_Object->m_Mutex.lock();
m_Locked = true;
#ifdef I2_DEBUG
@ -83,7 +82,7 @@ public:
#endif /* I2_DEBUG */
if (m_Locked) {
m_Object->m_Mutex.Unlock();
m_Object->m_Mutex.unlock();
m_Locked = false;
}
}

View File

@ -1,66 +0,0 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2013 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/thinmutex.hpp"
#include "base/timer.hpp"
#include "base/convert.hpp"
#include "base/logger.hpp"
using namespace icinga;
/**
* Locks the mutex and inflates the lock.
*/
void ThinMutex::LockSlowPath(void)
{
unsigned int it = 0;
#ifdef _WIN32
# ifdef _WIN64
while (InterlockedCompareExchange64(&m_Data, THINLOCK_LOCKED, THINLOCK_UNLOCKED) != THINLOCK_UNLOCKED) {
# else /* _WIN64 */
while (InterlockedCompareExchange(&m_Data, THINLOCK_LOCKED, THINLOCK_UNLOCKED) != THINLOCK_UNLOCKED) {
# endif /* _WIN64 */
#else /* _WIN32 */
while (!__sync_bool_compare_and_swap(&m_Data, THINLOCK_UNLOCKED, THINLOCK_LOCKED)) {
#endif /* _WIN32 */
if (m_Data > THINLOCK_LOCKED) {
boost::mutex *mtx = reinterpret_cast<boost::mutex *>(m_Data);
mtx->lock();
return;
}
Spin(it);
it++;
}
boost::mutex *mtx = new boost::mutex();
mtx->lock();
#ifdef _WIN32
# ifdef _WIN64
InterlockedCompareExchange64(&m_Data, reinterpret_cast<LONGLONG>(mtx), THINLOCK_LOCKED);
# else /* _WIN64 */
InterlockedCompareExchange(&m_Data, reinterpret_cast<LONG>(mtx), THINLOCK_LOCKED);
# endif /* _WIN64 */
#else /* _WIN32 */
__sync_bool_compare_and_swap(&m_Data, THINLOCK_LOCKED, reinterpret_cast<uintptr_t>(mtx));
#endif /* _WIN32 */
}

View File

@ -1,128 +0,0 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2013 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. *
******************************************************************************/
#ifndef THINMUTEX_H
#define THINMUTEX_H
#include "base/i2-base.hpp"
#include <boost/thread/mutex.hpp>
#ifndef _WIN32
#include <sched.h>
#endif /* _WIN32 */
#if defined(_MSC_VER) && _MSC_VER >= 1310 && (defined(_M_IX86) || defined(_M_X64))
extern "C" void _mm_pause();
#pragma intrinsic(_mm_pause)
#define SPIN_PAUSE() _mm_pause()
#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
#define SPIN_PAUSE() __asm__ __volatile__("rep; nop" : : : "memory")
#endif
#define THINLOCK_UNLOCKED 0
#define THINLOCK_LOCKED 1
namespace icinga {
class I2_BASE_API ThinMutex
{
public:
inline ThinMutex(void)
: m_Data(THINLOCK_UNLOCKED)
{ }
inline ~ThinMutex(void)
{
if (m_Data > THINLOCK_LOCKED)
delete reinterpret_cast<boost::mutex *>(m_Data);
}
inline void Lock(void)
{
#ifdef _WIN32
# ifdef _WIN64
if (InterlockedCompareExchange64(&m_Data, THINLOCK_LOCKED, THINLOCK_UNLOCKED) != THINLOCK_UNLOCKED) {
# else /* _WIN64 */
if (InterlockedCompareExchange(&m_Data, THINLOCK_LOCKED, THINLOCK_UNLOCKED) != THINLOCK_UNLOCKED) {
# endif /* _WIN64 */
#else /* _WIN32 */
if (!__sync_bool_compare_and_swap(&m_Data, THINLOCK_UNLOCKED, THINLOCK_LOCKED)) {
#endif /* _WIN32 */
LockSlowPath();
}
}
inline void Unlock(void)
{
#ifdef _WIN32
# ifdef _WIN64
if (InterlockedCompareExchange64(&m_Data, THINLOCK_UNLOCKED, THINLOCK_LOCKED) != THINLOCK_LOCKED) {
# else /* _WIN64 */
if (InterlockedCompareExchange(&m_Data, THINLOCK_UNLOCKED, THINLOCK_LOCKED) != THINLOCK_LOCKED) {
# endif /* _WIN64 */
#else /* _WIN32 */
if (!__sync_bool_compare_and_swap(&m_Data, THINLOCK_LOCKED, THINLOCK_UNLOCKED)) {
#endif /* _WIN32 */
boost::mutex *mtx = reinterpret_cast<boost::mutex *>(m_Data);
mtx->unlock();
}
}
inline void Inflate(void)
{
LockSlowPath();
Unlock();
}
private:
inline void Spin(unsigned int it)
{
if (it < 8) {
/* Do nothing. */
}
#ifdef SPIN_PAUSE
else if (it < 16) {
SPIN_PAUSE();
}
#endif /* SPIN_PAUSE */
else {
#ifdef _WIN32
Sleep(0);
#else /* _WIN32 */
sched_yield();
#endif /* _WIN32 */
}
}
private:
#ifdef _WIN32
# ifdef _WIN64
LONGLONG m_Data;
# else /* _WIN64 */
LONG m_Data;
# endif /* _WIN64 */
#else /* _WIN32 */
uintptr_t m_Data;
#endif /* _WIN32 */
void LockSlowPath(void);
};
}
#endif /* THINMUTEX_H */

View File

@ -90,8 +90,6 @@ void Timer::Uninitialize(void)
*/
void Timer::Call(void)
{
ASSERT(!OwnsLock());
try {
OnTimerExpired(Timer::Ptr(this));
} catch (...) {
@ -110,8 +108,6 @@ void Timer::Call(void)
*/
void Timer::SetInterval(double interval)
{
ASSERT(!OwnsLock());
boost::mutex::scoped_lock lock(l_TimerMutex);
m_Interval = interval;
}
@ -123,8 +119,6 @@ void Timer::SetInterval(double interval)
*/
double Timer::GetInterval(void) const
{
ASSERT(!OwnsLock());
boost::mutex::scoped_lock lock(l_TimerMutex);
return m_Interval;
}
@ -134,8 +128,6 @@ double Timer::GetInterval(void) const
*/
void Timer::Start(void)
{
ASSERT(!OwnsLock());
{
boost::mutex::scoped_lock lock(l_TimerMutex);
m_Started = true;
@ -149,8 +141,6 @@ void Timer::Start(void)
*/
void Timer::Stop(bool wait)
{
ASSERT(!OwnsLock());
if (l_StopTimerThread)
return;
@ -180,8 +170,6 @@ void Timer::Reschedule(double next)
*/
void Timer::InternalReschedule(bool completed, double next)
{
ASSERT(!OwnsLock());
boost::mutex::scoped_lock lock(l_TimerMutex);
if (completed)
@ -214,8 +202,6 @@ void Timer::InternalReschedule(bool completed, double next)
*/
double Timer::GetNext(void) const
{
ASSERT(!OwnsLock());
boost::mutex::scoped_lock lock(l_TimerMutex);
return m_Next;
}

View File

@ -157,8 +157,6 @@ public:
*/
ConfigObject::Ptr ConfigItem::Commit(bool discard)
{
ASSERT(!OwnsLock());
#ifdef I2_DEBUG
Log(LogDebug, "ConfigItem")
<< "Commit called for ConfigItem Type=" << GetType() << ", Name=" << GetName();

View File

@ -83,8 +83,6 @@ DbType::Ptr DbType::GetByID(long tid)
DbObject::Ptr DbType::GetOrCreateObjectByName(const String& name1, const String& name2)
{
ASSERT(!OwnsLock());
ObjectLock olock(this);
DbType::ObjectMap::const_iterator it = m_Objects.find(std::make_pair(name1, name2));

View File

@ -151,7 +151,6 @@ void Checkable::ProcessCheckResult(const CheckResult::Ptr& cr, const MessageOrig
bool reachable = IsReachable();
bool notification_reachable = IsReachable(DependencyNotification);
ASSERT(!OwnsLock());
ObjectLock olock(this);
CheckResult::Ptr old_cr = GetLastCheckResult();
@ -383,12 +382,10 @@ void Checkable::ExecuteRemoteCheck(const Dictionary::Ptr& resolvedMacros)
GetCheckCommand()->Execute(this, cr, resolvedMacros, true);
}
void Checkable::ExecuteCheck()
void Checkable::ExecuteCheck(void)
{
CONTEXT("Executing check for object '" + GetName() + "'");
ASSERT(!OwnsLock());
UpdateNextCheck();
bool reachable = IsReachable();

View File

@ -125,8 +125,6 @@ int IcingaApplication::Main(void)
void IcingaApplication::OnShutdown(void)
{
ASSERT(!OwnsLock());
{
ObjectLock olock(this);
l_RetentionTimer->Stop();

View File

@ -240,8 +240,6 @@ String Notification::NotificationTypeToString(NotificationType type)
void Notification::BeginExecuteNotification(NotificationType type, const CheckResult::Ptr& cr, bool force, const String& author, const String& text)
{
ASSERT(!OwnsLock());
Log(LogNotice, "Notification")
<< "Attempting to send notifications for notification object '" << GetName() << "'.";
@ -398,8 +396,6 @@ void Notification::BeginExecuteNotification(NotificationType type, const CheckRe
bool Notification::CheckNotificationUserFilters(NotificationType type, const User::Ptr& user, bool force)
{
ASSERT(!OwnsLock());
if (!force) {
TimePeriod::Ptr tp = user->GetPeriod();
@ -465,8 +461,6 @@ bool Notification::CheckNotificationUserFilters(NotificationType type, const Use
void Notification::ExecuteNotificationHelper(NotificationType type, const User::Ptr& user, const CheckResult::Ptr& cr, bool force, const String& author, const String& text)
{
ASSERT(!OwnsLock());
try {
NotificationCommand::Ptr command = GetCommand();