icinga2/lib/remote/configobjectslock.hpp

113 lines
2.3 KiB
C++
Raw Permalink Normal View History

Replace all existing copyright headers with `SPDX` headers I've used the following command to replace the original copyright header lines in a C-style comment block: ``` $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{/\*[^*]*\(\s*c\s*\)\s*(\d{4})\s*Icinga\s+GmbH[^*]*\*/}{// SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n// SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + ``` For files that use shell-style comments (#) like CMakeLists.txt, I've used this command: ``` $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{#.*\(\s*c\s*\)\s(\d{4})\sIcinga\s+GmbH.*}{# SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n# SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + ``` And for SQL files: ``` $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{--.*\(c\)\s(\d{4})\sIcinga\sGmbH.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + $ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{-- Copyright \(c\)\s(\d{4})\sIcinga\s+Development\sTeam.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} + ```
2026-01-27 09:06:40 -05:00
// SPDX-FileCopyrightText: 2023 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-2.0-or-later
2022-07-15 11:00:50 -04:00
#pragma once
#include "base/type.hpp"
#include "base/string.hpp"
#include <condition_variable>
#include <map>
2022-07-15 11:00:50 -04:00
#include <mutex>
#include <set>
2022-07-15 11:00:50 -04:00
#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;
}
constexpr void Unlock()
{
}
2022-07-15 11:00:50 -04:00
};
#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();
}
void Unlock()
{
m_Lock.unlock();
}
2022-07-15 11:00:50 -04:00
private:
boost::interprocess::sharable_lock<boost::interprocess::interprocess_sharable_mutex> m_Lock;
};
#endif /* _WIN32 */
/**
* Allows you to easily lock/unlock a specific object of a given type by its name.
*
* That way, locking an object "this" of type Host does not affect an object "this" of
* type "Service" nor an object "other" of type "Host".
*
* @ingroup remote
*/
class ObjectNameLock
{
public:
ObjectNameLock(const Type::Ptr& ptype, const String& objName);
~ObjectNameLock();
private:
String m_ObjectName;
Type::Ptr m_Type;
static std::mutex m_Mutex;
static std::condition_variable m_CV;
static std::map<Type*, std::set<String>> m_LockedObjectNames;
};
2022-07-15 11:00:50 -04:00
}