From ae0442b487f04006a14cb6a5c36dbc7ca3b9ed1d Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 6 Nov 2024 13:30:28 +0100 Subject: [PATCH] Consistently use Atomic, and not std::atomic Atomic enforces usage of its only safe constructor, in contrast to std::atomic. "The default-initialized std::atomic does not contain a T object, and its only valid uses are destruction and initialization by std::atomic_init, see LWG issue 2334." -- https://en.cppreference.com/w/cpp/atomic/atomic/atomic --- lib/base/atomic.hpp | 6 +++--- lib/base/io-engine.cpp | 5 +++-- lib/base/io-engine.hpp | 4 ++-- lib/base/lazy-init.hpp | 5 ++--- lib/base/logger.cpp | 2 +- lib/base/logger.hpp | 2 +- lib/base/namespace.hpp | 4 ++-- lib/base/object.cpp | 5 ----- lib/base/object.hpp | 6 +++--- lib/base/tlsstream.hpp | 5 ++--- lib/base/workqueue.cpp | 2 +- lib/base/workqueue.hpp | 4 ++-- lib/config/applyrule.hpp | 4 ++-- lib/config/configitem.cpp | 6 +++--- lib/icinga/scheduleddowntime.cpp | 2 +- lib/icinga/scheduleddowntime.hpp | 4 ++-- lib/icingadb/icingadb.hpp | 4 ++-- lib/perfdata/influxdbcommonwriter.hpp | 5 +++-- lib/remote/apilistener-authority.cpp | 2 +- lib/remote/apilistener.hpp | 4 ++-- lib/remote/configstageshandler.cpp | 2 +- lib/remote/configstageshandler.hpp | 4 ++-- 22 files changed, 41 insertions(+), 46 deletions(-) diff --git a/lib/base/atomic.hpp b/lib/base/atomic.hpp index dcd81d7c646..ea9f3aa17e3 100644 --- a/lib/base/atomic.hpp +++ b/lib/base/atomic.hpp @@ -74,7 +74,7 @@ class Locked }; /** - * Type alias for std::atomic if possible, otherwise Locked is used as a fallback. + * Type alias for Atomic if possible, otherwise Locked is used as a fallback. * * @ingroup base */ @@ -82,9 +82,9 @@ template using AtomicOrLocked = #if defined(__GNUC__) && __GNUC__ < 5 // GCC does not implement std::is_trivially_copyable until version 5. - typename std::conditional::value || std::is_pointer::value, std::atomic, Locked>::type; + typename std::conditional::value || std::is_pointer::value, Atomic, Locked>::type; #else /* defined(__GNUC__) && __GNUC__ < 5 */ - typename std::conditional::value, std::atomic, Locked>::type; + typename std::conditional::value, Atomic, Locked>::type; #endif /* defined(__GNUC__) && __GNUC__ < 5 */ } diff --git a/lib/base/io-engine.cpp b/lib/base/io-engine.cpp index 3190ed03d82..72245c6eeb8 100644 --- a/lib/base/io-engine.cpp +++ b/lib/base/io-engine.cpp @@ -85,10 +85,11 @@ boost::asio::io_context& IoEngine::GetIoContext() return m_IoContext; } -IoEngine::IoEngine() : m_IoContext(), m_KeepAlive(boost::asio::make_work_guard(m_IoContext)), m_Threads(decltype(m_Threads)::size_type(Configuration::Concurrency * 2u)), m_AlreadyExpiredTimer(m_IoContext) +IoEngine::IoEngine() : m_IoContext(), m_KeepAlive(boost::asio::make_work_guard(m_IoContext)), + m_Threads(decltype(m_Threads)::size_type(Configuration::Concurrency * 2u)), + m_AlreadyExpiredTimer(m_IoContext), m_CpuBoundSemaphore(Configuration::Concurrency * 3u / 2u) { m_AlreadyExpiredTimer.expires_at(boost::posix_time::neg_infin); - m_CpuBoundSemaphore.store(Configuration::Concurrency * 3u / 2u); for (auto& thread : m_Threads) { thread = std::thread(&IoEngine::RunEventLoop, this); diff --git a/lib/base/io-engine.hpp b/lib/base/io-engine.hpp index 0350d45b83d..e3e166c8038 100644 --- a/lib/base/io-engine.hpp +++ b/lib/base/io-engine.hpp @@ -9,7 +9,7 @@ #include "base/lazy-init.hpp" #include "base/logger.hpp" #include "base/shared.hpp" -#include +#include #include #include #include @@ -137,7 +137,7 @@ class IoEngine boost::asio::executor_work_guard m_KeepAlive; std::vector m_Threads; boost::asio::deadline_timer m_AlreadyExpiredTimer; - std::atomic_int_fast32_t m_CpuBoundSemaphore; + Atomic m_CpuBoundSemaphore; }; class TerminateIoThread : public std::exception diff --git a/lib/base/lazy-init.hpp b/lib/base/lazy-init.hpp index c1da2cd9375..65c99d89f26 100644 --- a/lib/base/lazy-init.hpp +++ b/lib/base/lazy-init.hpp @@ -3,7 +3,7 @@ #ifndef LAZY_INIT #define LAZY_INIT -#include +#include "base/atomic.hpp" #include #include #include @@ -24,7 +24,6 @@ class LazyInit inline LazyInit(std::function initializer = []() { return T(); }) : m_Initializer(std::move(initializer)) { - m_Underlying.store(nullptr, std::memory_order_release); } LazyInit(const LazyInit&) = delete; @@ -64,7 +63,7 @@ class LazyInit private: std::function m_Initializer; std::mutex m_Mutex; - std::atomic m_Underlying; + Atomic m_Underlying {nullptr}; }; } diff --git a/lib/base/logger.cpp b/lib/base/logger.cpp index 38a2c6721b4..0185230f8e1 100644 --- a/lib/base/logger.cpp +++ b/lib/base/logger.cpp @@ -33,7 +33,7 @@ REGISTER_TYPE(Logger); std::set Logger::m_Loggers; std::mutex Logger::m_Mutex; bool Logger::m_ConsoleLogEnabled = true; -std::atomic Logger::m_EarlyLoggingEnabled (true); +Atomic Logger::m_EarlyLoggingEnabled (true); bool Logger::m_TimestampEnabled = true; LogSeverity Logger::m_ConsoleLogSeverity = LogInformation; std::mutex Logger::m_UpdateMinLogSeverityMutex; diff --git a/lib/base/logger.hpp b/lib/base/logger.hpp index 7b4758d8b72..b5512b912c0 100644 --- a/lib/base/logger.hpp +++ b/lib/base/logger.hpp @@ -99,7 +99,7 @@ class Logger : public ObjectImpl static std::mutex m_Mutex; static std::set m_Loggers; static bool m_ConsoleLogEnabled; - static std::atomic m_EarlyLoggingEnabled; + static Atomic m_EarlyLoggingEnabled; static bool m_TimestampEnabled; static LogSeverity m_ConsoleLogSeverity; static std::mutex m_UpdateMinLogSeverityMutex; diff --git a/lib/base/namespace.hpp b/lib/base/namespace.hpp index 94f2055d373..e15d4eed7d5 100644 --- a/lib/base/namespace.hpp +++ b/lib/base/namespace.hpp @@ -4,11 +4,11 @@ #define NAMESPACE_H #include "base/i2-base.hpp" +#include "base/atomic.hpp" #include "base/object.hpp" #include "base/shared-object.hpp" #include "base/value.hpp" #include "base/debuginfo.hpp" -#include #include #include #include @@ -92,7 +92,7 @@ class Namespace final : public Object std::map m_Data; mutable std::shared_timed_mutex m_DataMutex; bool m_ConstValues; - std::atomic m_Frozen; + Atomic m_Frozen; }; Namespace::Iterator begin(const Namespace::Ptr& x); diff --git a/lib/base/object.cpp b/lib/base/object.cpp index 92a43b912df..198f26478c3 100644 --- a/lib/base/object.cpp +++ b/lib/base/object.cpp @@ -27,11 +27,6 @@ static Timer::Ptr l_ObjectCountTimer; */ Object::Object() { - m_References.store(0); - -#ifdef I2_DEBUG - m_LockOwner.store(decltype(m_LockOwner.load())()); -#endif /* I2_DEBUG */ } /** diff --git a/lib/base/object.hpp b/lib/base/object.hpp index aae28ae88d7..c158f8c207b 100644 --- a/lib/base/object.hpp +++ b/lib/base/object.hpp @@ -4,9 +4,9 @@ #define OBJECT_H #include "base/i2-base.hpp" +#include "base/atomic.hpp" #include "base/debug.hpp" #include -#include #include #include #include @@ -191,11 +191,11 @@ class Object Object(const Object& other) = delete; Object& operator=(const Object& rhs) = delete; - std::atomic m_References; + Atomic m_References {0}; mutable std::recursive_mutex m_Mutex; #ifdef I2_DEBUG - mutable std::atomic m_LockOwner; + mutable Atomic m_LockOwner {std::thread::id()}; mutable size_t m_LockCount = 0; #endif /* I2_DEBUG */ diff --git a/lib/base/tlsstream.hpp b/lib/base/tlsstream.hpp index 9eed8d3b112..2120353b171 100644 --- a/lib/base/tlsstream.hpp +++ b/lib/base/tlsstream.hpp @@ -4,13 +4,13 @@ #define TLSSTREAM_H #include "base/i2-base.hpp" +#include "base/atomic.hpp" #include "base/shared.hpp" #include "base/socket.hpp" #include "base/stream.hpp" #include "base/tlsutility.hpp" #include "base/fifo.hpp" #include "base/utility.hpp" -#include #include #include #include @@ -30,7 +30,6 @@ class SeenStream : public ARS template SeenStream(Args&&... args) : ARS(std::forward(args)...) { - m_Seen.store(nullptr); } template @@ -53,7 +52,7 @@ class SeenStream : public ARS } private: - std::atomic m_Seen; + Atomic m_Seen {nullptr}; }; struct UnbufferedAsioTlsStreamParams diff --git a/lib/base/workqueue.cpp b/lib/base/workqueue.cpp index 0b1214bafa3..1b1befb35ab 100644 --- a/lib/base/workqueue.cpp +++ b/lib/base/workqueue.cpp @@ -11,7 +11,7 @@ using namespace icinga; -std::atomic WorkQueue::m_NextID(1); +Atomic WorkQueue::m_NextID (1); boost::thread_specific_ptr l_ThreadWorkQueue; WorkQueue::WorkQueue(size_t maxItems, int threadCount, LogSeverity statsLogLevel) diff --git a/lib/base/workqueue.hpp b/lib/base/workqueue.hpp index 2cfec64718e..0e58715eeda 100644 --- a/lib/base/workqueue.hpp +++ b/lib/base/workqueue.hpp @@ -4,6 +4,7 @@ #define WORKQUEUE_H #include "base/i2-base.hpp" +#include "base/atomic.hpp" #include "base/timer.hpp" #include "base/ringbuffer.hpp" #include "base/logger.hpp" @@ -13,7 +14,6 @@ #include #include #include -#include namespace icinga { @@ -122,7 +122,7 @@ class WorkQueue private: int m_ID; String m_Name; - static std::atomic m_NextID; + static Atomic m_NextID; int m_ThreadCount; bool m_Spawned{false}; diff --git a/lib/config/applyrule.hpp b/lib/config/applyrule.hpp index cf9b6e5e67e..0914ea1a5df 100644 --- a/lib/config/applyrule.hpp +++ b/lib/config/applyrule.hpp @@ -5,11 +5,11 @@ #include "config/i2-config.hpp" #include "config/expression.hpp" +#include "base/atomic.hpp" #include "base/debuginfo.hpp" #include "base/shared-object.hpp" #include "base/type.hpp" #include -#include namespace icinga { @@ -104,7 +104,7 @@ class ApplyRule : public SharedObject bool m_IgnoreOnError; DebugInfo m_DebugInfo; Dictionary::Ptr m_Scope; - std::atomic m_HasMatches; + Atomic m_HasMatches; static TypeMap m_Types; static RuleMap m_Rules; diff --git a/lib/config/configitem.cpp b/lib/config/configitem.cpp index e8a50927543..2a958b723b6 100644 --- a/lib/config/configitem.cpp +++ b/lib/config/configitem.cpp @@ -6,6 +6,7 @@ #include "config/objectrule.hpp" #include "config/configcompiler.hpp" #include "base/application.hpp" +#include "base/atomic.hpp" #include "base/configtype.hpp" #include "base/objectlock.hpp" #include "base/convert.hpp" @@ -21,7 +22,6 @@ #include "base/function.hpp" #include "base/utility.hpp" #include -#include #include #include #include @@ -447,7 +447,7 @@ bool ConfigItem::CommitNewItems(const ActivationContext::Ptr& context, WorkQueue int itemsCount {0}; for (auto& type : Type::GetConfigTypesSortedByLoadDependencies()) { - std::atomic committed_items(0); + Atomic committed_items (0); { auto items (itemsByType.find(type.get())); @@ -493,7 +493,7 @@ bool ConfigItem::CommitNewItems(const ActivationContext::Ptr& context, WorkQueue #endif /* I2_DEBUG */ for (auto& type : Type::GetConfigTypesSortedByLoadDependencies()) { - std::atomic notified_items(0); + Atomic notified_items (0); { auto items (itemsByType.find(type.get())); diff --git a/lib/icinga/scheduleddowntime.cpp b/lib/icinga/scheduleddowntime.cpp index f9ddc438588..ad3b348a238 100644 --- a/lib/icinga/scheduleddowntime.cpp +++ b/lib/icinga/scheduleddowntime.cpp @@ -390,4 +390,4 @@ bool ScheduledDowntime::AllConfigIsLoaded() return m_AllConfigLoaded.load(); } -std::atomic ScheduledDowntime::m_AllConfigLoaded (false); +Atomic ScheduledDowntime::m_AllConfigLoaded (false); diff --git a/lib/icinga/scheduleddowntime.hpp b/lib/icinga/scheduleddowntime.hpp index e70123616b1..f0b60b42e9b 100644 --- a/lib/icinga/scheduleddowntime.hpp +++ b/lib/icinga/scheduleddowntime.hpp @@ -4,9 +4,9 @@ #define SCHEDULEDDOWNTIME_H #include "icinga/i2-icinga.hpp" +#include "base/atomic.hpp" #include "icinga/scheduleddowntime-ti.hpp" #include "icinga/checkable.hpp" -#include namespace icinga { @@ -49,7 +49,7 @@ class ScheduledDowntime final : public ObjectImpl void CreateNextDowntime(); void RemoveObsoleteDowntimes(); - static std::atomic m_AllConfigLoaded; + static Atomic m_AllConfigLoaded; static bool EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, const String& name, ScriptFrame& frame, const ApplyRule& rule, bool skipFilter); static bool EvaluateApplyRule(const Checkable::Ptr& checkable, const ApplyRule& rule, bool skipFilter = false); diff --git a/lib/icingadb/icingadb.hpp b/lib/icingadb/icingadb.hpp index 6652d9c1f4d..c8a10cf91e0 100644 --- a/lib/icingadb/icingadb.hpp +++ b/lib/icingadb/icingadb.hpp @@ -14,8 +14,8 @@ #include "icinga/service.hpp" #include "icinga/downtime.hpp" #include "remote/messageorigin.hpp" -#include #include +#include #include #include #include @@ -222,7 +222,7 @@ class IcingaDB : public ObjectImpl // syncronization to m_Rcon within the IcingaDB feature itself. Locked m_RconLocked; std::unordered_map m_Rcons; - std::atomic_size_t m_PendingRcons; + Atomic m_PendingRcons {0}; struct { DumpedGlobals CustomVar, ActionUrl, NotesUrl, IconImage; diff --git a/lib/perfdata/influxdbcommonwriter.hpp b/lib/perfdata/influxdbcommonwriter.hpp index 380b20c9f64..48ee78f75e6 100644 --- a/lib/perfdata/influxdbcommonwriter.hpp +++ b/lib/perfdata/influxdbcommonwriter.hpp @@ -5,6 +5,7 @@ #include "perfdata/influxdbcommonwriter-ti.hpp" #include "icinga/service.hpp" +#include "base/atomic.hpp" #include "base/configobject.hpp" #include "base/perfdatavalue.hpp" #include "base/tcpsocket.hpp" @@ -14,7 +15,7 @@ #include "remote/url.hpp" #include #include -#include +#include #include namespace icinga @@ -51,7 +52,7 @@ class InfluxdbCommonWriter : public ObjectImpl Timer::Ptr m_FlushTimer; WorkQueue m_WorkQueue{10000000, 1}; std::vector m_DataBuffer; - std::atomic_size_t m_DataBufferSize{0}; + Atomic m_DataBufferSize {0}; void CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); void CheckResultHandlerWQ(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr); diff --git a/lib/remote/apilistener-authority.cpp b/lib/remote/apilistener-authority.cpp index f33a1905bf1..d5e43064f85 100644 --- a/lib/remote/apilistener-authority.cpp +++ b/lib/remote/apilistener-authority.cpp @@ -8,7 +8,7 @@ using namespace icinga; -std::atomic ApiListener::m_UpdatedObjectAuthority (false); +Atomic ApiListener::m_UpdatedObjectAuthority (false); void ApiListener::UpdateObjectAuthority() { diff --git a/lib/remote/apilistener.hpp b/lib/remote/apilistener.hpp index eae1fa03e61..5cad87ee4e1 100644 --- a/lib/remote/apilistener.hpp +++ b/lib/remote/apilistener.hpp @@ -8,6 +8,7 @@ #include "remote/httpserverconnection.hpp" #include "remote/endpoint.hpp" #include "remote/messageorigin.hpp" +#include "base/atomic.hpp" #include "base/configobject.hpp" #include "base/process.hpp" #include "base/shared.hpp" @@ -16,7 +17,6 @@ #include "base/tcpsocket.hpp" #include "base/tlsstream.hpp" #include "base/threadpool.hpp" -#include #include #include #include @@ -179,7 +179,7 @@ class ApiListener final : public ObjectImpl Endpoint::Ptr m_LocalEndpoint; static ApiListener::Ptr m_Instance; - static std::atomic m_UpdatedObjectAuthority; + static Atomic m_UpdatedObjectAuthority; void ApiTimerHandler(); void ApiReconnectTimerHandler(); diff --git a/lib/remote/configstageshandler.cpp b/lib/remote/configstageshandler.cpp index edbb767e5b5..ccb2b317bef 100644 --- a/lib/remote/configstageshandler.cpp +++ b/lib/remote/configstageshandler.cpp @@ -12,7 +12,7 @@ using namespace icinga; REGISTER_URLHANDLER("/v1/config/stages", ConfigStagesHandler); -std::atomic ConfigStagesHandler::m_RunningPackageUpdates (false); +Atomic ConfigStagesHandler::m_RunningPackageUpdates (false); bool ConfigStagesHandler::HandleRequest( AsioTlsStream& stream, diff --git a/lib/remote/configstageshandler.hpp b/lib/remote/configstageshandler.hpp index 88f248c8fd2..b0a56ce2eeb 100644 --- a/lib/remote/configstageshandler.hpp +++ b/lib/remote/configstageshandler.hpp @@ -3,8 +3,8 @@ #ifndef CONFIGSTAGESHANDLER_H #define CONFIGSTAGESHANDLER_H +#include "base/atomic.hpp" #include "remote/httphandler.hpp" -#include namespace icinga { @@ -48,7 +48,7 @@ class ConfigStagesHandler final : public HttpHandler const Dictionary::Ptr& params ); - static std::atomic m_RunningPackageUpdates; + static Atomic m_RunningPackageUpdates; }; }