From e423fcc68b0187182ef621cfaade055bbbc19271 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 | 4 ++-- 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 | 3 +-- lib/remote/configstageshandler.hpp | 1 + 21 files changed, 37 insertions(+), 42 deletions(-) diff --git a/lib/base/atomic.hpp b/lib/base/atomic.hpp index 745b7c33e..426c0c3f4 100644 --- a/lib/base/atomic.hpp +++ b/lib/base/atomic.hpp @@ -112,12 +112,12 @@ private: }; /** - * 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 */ template -using AtomicOrLocked = std::conditional_t, std::atomic, Locked>; +using AtomicOrLocked = std::conditional_t, Atomic, Locked>; } diff --git a/lib/base/io-engine.cpp b/lib/base/io-engine.cpp index 0792be5cc..e072af626 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 0883d7810..a333844e7 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 @@ -150,7 +150,7 @@ private: 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 c1da2cd93..65c99d89f 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 @@ public: 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 @@ public: 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 f28a19bbd..10961c5a8 100644 --- a/lib/base/logger.cpp +++ b/lib/base/logger.cpp @@ -34,7 +34,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 d7f30bcee..3c544639c 100644 --- a/lib/base/logger.hpp +++ b/lib/base/logger.hpp @@ -100,7 +100,7 @@ private: 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 194671099..053163c5d 100644 --- a/lib/base/namespace.hpp +++ b/lib/base/namespace.hpp @@ -4,12 +4,12 @@ #define NAMESPACE_H #include "base/i2-base.hpp" +#include "base/atomic.hpp" #include "base/object.hpp" #include "base/objectlock.hpp" #include "base/shared-object.hpp" #include "base/value.hpp" #include "base/debuginfo.hpp" -#include #include #include #include @@ -95,7 +95,7 @@ private: 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 0c1e69e8e..ca5258de4 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 dc08db043..f81e21351 100644 --- a/lib/base/object.hpp +++ b/lib/base/object.hpp @@ -4,10 +4,10 @@ #define OBJECT_H #include "base/i2-base.hpp" +#include "base/atomic.hpp" #include "base/debug.hpp" #include "base/intrusive-ptr.hpp" #include -#include #include #include #include @@ -193,11 +193,11 @@ private: Object(const Object& other) = delete; Object& operator=(const Object& rhs) = delete; - mutable std::atomic m_References; + mutable 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 9eed8d3b1..2120353b1 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 @@ public: template SeenStream(Args&&... args) : ARS(std::forward(args)...) { - m_Seen.store(nullptr); } template @@ -53,7 +52,7 @@ public: } private: - std::atomic m_Seen; + Atomic m_Seen {nullptr}; }; struct UnbufferedAsioTlsStreamParams diff --git a/lib/base/workqueue.cpp b/lib/base/workqueue.cpp index 711f05124..1f31c0935 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 2cfec6471..0e58715ee 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 @@ protected: 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 cf9b6e5e6..0914ea1a5 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 @@ private: 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 b154683cd..516451c6e 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 3447b2a19..4a00b02e4 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 e70123616..f0b60b42e 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 @@ private: 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 01fa6bbd6..9a834ef05 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 @@ -250,7 +250,7 @@ private: // 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, DependencyGroup; diff --git a/lib/perfdata/influxdbcommonwriter.hpp b/lib/perfdata/influxdbcommonwriter.hpp index 9d3427f7e..d1573b660 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 @@ private: 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 SendMetric(const Checkable::Ptr& checkable, const Dictionary::Ptr& tmpl, diff --git a/lib/remote/apilistener-authority.cpp b/lib/remote/apilistener-authority.cpp index e169ad4b1..452448841 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 ea5ed8cb9..9dacece22 100644 --- a/lib/remote/apilistener.hpp +++ b/lib/remote/apilistener.hpp @@ -18,7 +18,6 @@ #include "base/tlsstream.hpp" #include "base/threadpool.hpp" #include "base/wait-group.hpp" -#include #include #include #include @@ -192,7 +191,7 @@ private: StoppableWaitGroup::Ptr m_WaitGroup = new StoppableWaitGroup(); static ApiListener::Ptr m_Instance; - static std::atomic m_UpdatedObjectAuthority; + static Atomic m_UpdatedObjectAuthority; boost::signals2::signal m_OnListenerShutdown; StoppableWaitGroup::Ptr m_ListenerWaitGroup = new StoppableWaitGroup(); diff --git a/lib/remote/configstageshandler.hpp b/lib/remote/configstageshandler.hpp index ec333cc50..66022b63d 100644 --- a/lib/remote/configstageshandler.hpp +++ b/lib/remote/configstageshandler.hpp @@ -3,6 +3,7 @@ #ifndef CONFIGSTAGESHANDLER_H #define CONFIGSTAGESHANDLER_H +#include "base/atomic.hpp" #include "remote/httphandler.hpp" namespace icinga