Introduce ConfigObjects*Lock

This commit is contained in:
Alexander A. Klimov 2022-07-15 17:00:50 +02:00
parent a66ace7245
commit 64e000df56
3 changed files with 97 additions and 0 deletions

View File

@ -14,6 +14,7 @@ set(remote_SOURCES
apilistener-authority.cpp
apiuser.cpp apiuser.hpp apiuser-ti.hpp
configfileshandler.cpp configfileshandler.hpp
configobjectslock.cpp configobjectslock.hpp
configobjectutility.cpp configobjectutility.hpp
configpackageshandler.cpp configpackageshandler.hpp
configpackageutility.cpp configpackageutility.hpp

View File

@ -0,0 +1,24 @@
/* Icinga 2 | (c) 2022 Icinga GmbH | GPLv2+ */
#ifndef _WIN32
#include "base/shared-memory.hpp"
#include "remote/configobjectslock.hpp"
#include <boost/interprocess/sync/lock_options.hpp>
using namespace icinga;
// On *nix one process may write config objects while another is loading the config, so this uses IPC.
static SharedMemory<boost::interprocess::interprocess_sharable_mutex> l_ConfigObjectsMutex;
ConfigObjectsExclusiveLock::ConfigObjectsExclusiveLock()
: m_Lock(l_ConfigObjectsMutex.Get())
{
}
ConfigObjectsSharedLock::ConfigObjectsSharedLock(std::try_to_lock_t)
: m_Lock(l_ConfigObjectsMutex.Get(), boost::interprocess::try_to_lock)
{
}
#endif /* _WIN32 */

View File

@ -0,0 +1,72 @@
/* Icinga 2 | (c) 2023 Icinga GmbH | GPLv2+ */
#pragma once
#include <mutex>
#ifndef _WIN32
#include <boost/interprocess/sync/interprocess_sharable_mutex.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/sync/sharable_lock.hpp>
#endif /* _WIN32 */
namespace icinga
{
#ifdef _WIN32
class ConfigObjectsSharedLock
{
public:
inline ConfigObjectsSharedLock(std::try_to_lock_t)
{
}
constexpr explicit operator bool() const
{
return true;
}
};
#else /* _WIN32 */
/**
* Waits until all ConfigObjects*Lock-s have vanished. For its lifetime disallows such.
* Keep an instance alive during reload to forbid runtime config changes!
* This way Icinga reads a consistent config which doesn't suddenly get runtime-changed.
*
* @ingroup remote
*/
class ConfigObjectsExclusiveLock
{
public:
ConfigObjectsExclusiveLock();
private:
boost::interprocess::scoped_lock<boost::interprocess::interprocess_sharable_mutex> m_Lock;
};
/**
* Waits until the only ConfigObjectsExclusiveLock has vanished (if any). For its lifetime disallows such.
* Keep an instance alive during runtime config changes to delay a reload (if any)!
* This way Icinga reads a consistent config which doesn't suddenly get runtime-changed.
*
* @ingroup remote
*/
class ConfigObjectsSharedLock
{
public:
ConfigObjectsSharedLock(std::try_to_lock_t);
inline explicit operator bool() const
{
return m_Lock.owns();
}
private:
boost::interprocess::sharable_lock<boost::interprocess::interprocess_sharable_mutex> m_Lock;
};
#endif /* _WIN32 */
}