Skip to content

Commit

Permalink
Consistently use Atomic, and not std::atomic
Browse files Browse the repository at this point in the history
Atomic enforces usage of its only safe constructor,
in contrast to std::atomic.

"The default-initialized std::atomic<T> 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
  • Loading branch information
Al2Klimov committed Nov 6, 2024
1 parent 48c27cc commit 33181a3
Show file tree
Hide file tree
Showing 21 changed files with 40 additions and 45 deletions.
5 changes: 3 additions & 2 deletions lib/base/io-engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions lib/base/io-engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#ifndef IO_ENGINE_H
#define IO_ENGINE_H

#include "base/atomic.hpp"
#include "base/exception.hpp"
#include "base/lazy-init.hpp"
#include "base/logger.hpp"
#include "base/shared-object.hpp"
#include <atomic>
#include <cstdint>
#include <exception>
#include <memory>
#include <thread>
Expand Down Expand Up @@ -135,7 +136,7 @@ class IoEngine
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> m_KeepAlive;
std::vector<std::thread> m_Threads;
boost::asio::deadline_timer m_AlreadyExpiredTimer;
std::atomic_int_fast32_t m_CpuBoundSemaphore;
Atomic<int_fast32_t> m_CpuBoundSemaphore;
};

class TerminateIoThread : public std::exception
Expand Down Expand Up @@ -176,7 +177,6 @@ class Timeout : public SharedObject
{
Ptr keepAlive (this);

m_Cancelled.store(false);
m_Timer.expires_from_now(std::move(timeoutFromNow));

IoEngine::SpawnCoroutine(executor, [this, keepAlive, onTimeout](boost::asio::yield_context yc) {
Expand Down Expand Up @@ -207,7 +207,7 @@ class Timeout : public SharedObject

private:
boost::asio::deadline_timer m_Timer;
std::atomic<bool> m_Cancelled;
Atomic<bool> m_Cancelled {false};
};

}
Expand Down
5 changes: 2 additions & 3 deletions lib/base/lazy-init.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#ifndef LAZY_INIT
#define LAZY_INIT

#include <atomic>
#include "base/atomic.hpp"
#include <functional>
#include <mutex>
#include <utility>
Expand All @@ -24,7 +24,6 @@ class LazyInit
inline
LazyInit(std::function<T()> initializer = []() { return T(); }) : m_Initializer(std::move(initializer))
{
m_Underlying.store(nullptr, std::memory_order_release);
}

LazyInit(const LazyInit&) = delete;
Expand Down Expand Up @@ -64,7 +63,7 @@ class LazyInit
private:
std::function<T()> m_Initializer;
std::mutex m_Mutex;
std::atomic<T*> m_Underlying;
Atomic<T*> m_Underlying {nullptr};
};

}
Expand Down
2 changes: 1 addition & 1 deletion lib/base/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ REGISTER_TYPE(Logger);
std::set<Logger::Ptr> Logger::m_Loggers;
std::mutex Logger::m_Mutex;
bool Logger::m_ConsoleLogEnabled = true;
std::atomic<bool> Logger::m_EarlyLoggingEnabled (true);
Atomic<bool> Logger::m_EarlyLoggingEnabled (true);
bool Logger::m_TimestampEnabled = true;
LogSeverity Logger::m_ConsoleLogSeverity = LogInformation;
std::mutex Logger::m_UpdateMinLogSeverityMutex;
Expand Down
2 changes: 1 addition & 1 deletion lib/base/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Logger : public ObjectImpl<Logger>
static std::mutex m_Mutex;
static std::set<Logger::Ptr> m_Loggers;
static bool m_ConsoleLogEnabled;
static std::atomic<bool> m_EarlyLoggingEnabled;
static Atomic<bool> m_EarlyLoggingEnabled;
static bool m_TimestampEnabled;
static LogSeverity m_ConsoleLogSeverity;
static std::mutex m_UpdateMinLogSeverityMutex;
Expand Down
4 changes: 2 additions & 2 deletions lib/base/namespace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <atomic>
#include <map>
#include <vector>
#include <memory>
Expand Down Expand Up @@ -92,7 +92,7 @@ class Namespace final : public Object
std::map<String, NamespaceValue> m_Data;
mutable std::shared_timed_mutex m_DataMutex;
bool m_ConstValues;
std::atomic<bool> m_Frozen;
Atomic<bool> m_Frozen;
};

Namespace::Iterator begin(const Namespace::Ptr& x);
Expand Down
5 changes: 0 additions & 5 deletions lib/base/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/base/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#define OBJECT_H

#include "base/i2-base.hpp"
#include "base/atomic.hpp"
#include "base/debug.hpp"
#include <boost/smart_ptr/intrusive_ptr.hpp>
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <mutex>
Expand Down Expand Up @@ -191,11 +191,11 @@ class Object
Object(const Object& other) = delete;
Object& operator=(const Object& rhs) = delete;

std::atomic<uint_fast64_t> m_References;
Atomic<uint_fast64_t> m_References {0};
mutable std::recursive_mutex m_Mutex;

#ifdef I2_DEBUG
mutable std::atomic<std::thread::id> m_LockOwner;
mutable Atomic<std::thread::id> m_LockOwner {std::thread::id()};
mutable size_t m_LockCount = 0;
#endif /* I2_DEBUG */

Expand Down
5 changes: 2 additions & 3 deletions lib/base/tlsstream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <atomic>
#include <memory>
#include <utility>
#include <boost/asio/buffered_stream.hpp>
Expand All @@ -30,7 +30,6 @@ class SeenStream : public ARS
template<class... Args>
SeenStream(Args&&... args) : ARS(std::forward<Args>(args)...)
{
m_Seen.store(nullptr);
}

template<class... Args>
Expand All @@ -53,7 +52,7 @@ class SeenStream : public ARS
}

private:
std::atomic<double*> m_Seen;
Atomic<double*> m_Seen {nullptr};
};

struct UnbufferedAsioTlsStreamParams
Expand Down
2 changes: 1 addition & 1 deletion lib/base/workqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

using namespace icinga;

std::atomic<int> WorkQueue::m_NextID(1);
Atomic<int> WorkQueue::m_NextID (1);
boost::thread_specific_ptr<WorkQueue *> l_ThreadWorkQueue;

WorkQueue::WorkQueue(size_t maxItems, int threadCount, LogSeverity statsLogLevel)
Expand Down
4 changes: 2 additions & 2 deletions lib/base/workqueue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -13,7 +14,6 @@
#include <mutex>
#include <queue>
#include <deque>
#include <atomic>

namespace icinga
{
Expand Down Expand Up @@ -122,7 +122,7 @@ class WorkQueue
private:
int m_ID;
String m_Name;
static std::atomic<int> m_NextID;
static Atomic<int> m_NextID;
int m_ThreadCount;
bool m_Spawned{false};

Expand Down
4 changes: 2 additions & 2 deletions lib/config/applyrule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <unordered_map>
#include <atomic>

namespace icinga
{
Expand Down Expand Up @@ -104,7 +104,7 @@ class ApplyRule : public SharedObject
bool m_IgnoreOnError;
DebugInfo m_DebugInfo;
Dictionary::Ptr m_Scope;
std::atomic<bool> m_HasMatches;
Atomic<bool> m_HasMatches;

static TypeMap m_Types;
static RuleMap m_Rules;
Expand Down
6 changes: 3 additions & 3 deletions lib/config/configitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -21,7 +22,6 @@
#include "base/function.hpp"
#include "base/utility.hpp"
#include <boost/algorithm/string/join.hpp>
#include <atomic>
#include <sstream>
#include <fstream>
#include <algorithm>
Expand Down Expand Up @@ -447,7 +447,7 @@ bool ConfigItem::CommitNewItems(const ActivationContext::Ptr& context, WorkQueue
int itemsCount {0};

for (auto& type : Type::GetConfigTypesSortedByLoadDependencies()) {
std::atomic<int> committed_items(0);
Atomic committed_items (0);

{
auto items (itemsByType.find(type.get()));
Expand Down Expand Up @@ -493,7 +493,7 @@ bool ConfigItem::CommitNewItems(const ActivationContext::Ptr& context, WorkQueue
#endif /* I2_DEBUG */

for (auto& type : Type::GetConfigTypesSortedByLoadDependencies()) {
std::atomic<int> notified_items(0);
Atomic notified_items (0);

{
auto items (itemsByType.find(type.get()));
Expand Down
2 changes: 1 addition & 1 deletion lib/icinga/scheduleddowntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,4 +390,4 @@ bool ScheduledDowntime::AllConfigIsLoaded()
return m_AllConfigLoaded.load();
}

std::atomic<bool> ScheduledDowntime::m_AllConfigLoaded (false);
Atomic<bool> ScheduledDowntime::m_AllConfigLoaded (false);
4 changes: 2 additions & 2 deletions lib/icinga/scheduleddowntime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <atomic>

namespace icinga
{
Expand Down Expand Up @@ -49,7 +49,7 @@ class ScheduledDowntime final : public ObjectImpl<ScheduledDowntime>
void CreateNextDowntime();
void RemoveObsoleteDowntimes();

static std::atomic<bool> m_AllConfigLoaded;
static Atomic<bool> 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);
Expand Down
4 changes: 2 additions & 2 deletions lib/icingadb/icingadb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#include "icinga/service.hpp"
#include "icinga/downtime.hpp"
#include "remote/messageorigin.hpp"
#include <atomic>
#include <chrono>
#include <cstddef>
#include <future>
#include <memory>
#include <mutex>
Expand Down Expand Up @@ -222,7 +222,7 @@ class IcingaDB : public ObjectImpl<IcingaDB>
// syncronization to m_Rcon within the IcingaDB feature itself.
Locked<RedisConnection::Ptr> m_RconLocked;
std::unordered_map<ConfigType*, RedisConnection::Ptr> m_Rcons;
std::atomic_size_t m_PendingRcons;
Atomic<size_t> m_PendingRcons {0};

struct {
DumpedGlobals CustomVar, ActionUrl, NotesUrl, IconImage;
Expand Down
5 changes: 3 additions & 2 deletions lib/perfdata/influxdbcommonwriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -14,7 +15,7 @@
#include "remote/url.hpp"
#include <boost/beast/http/message.hpp>
#include <boost/beast/http/string_body.hpp>
#include <atomic>
#include <cstddef>
#include <fstream>

namespace icinga
Expand Down Expand Up @@ -51,7 +52,7 @@ class InfluxdbCommonWriter : public ObjectImpl<InfluxdbCommonWriter>
Timer::Ptr m_FlushTimer;
WorkQueue m_WorkQueue{10000000, 1};
std::vector<String> m_DataBuffer;
std::atomic_size_t m_DataBufferSize{0};
Atomic<size_t> m_DataBufferSize {0};

void CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr);
void CheckResultHandlerWQ(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr);
Expand Down
2 changes: 1 addition & 1 deletion lib/remote/apilistener-authority.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

using namespace icinga;

std::atomic<bool> ApiListener::m_UpdatedObjectAuthority (false);
Atomic<bool> ApiListener::m_UpdatedObjectAuthority (false);

void ApiListener::UpdateObjectAuthority()
{
Expand Down
4 changes: 2 additions & 2 deletions lib/remote/apilistener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -16,7 +17,6 @@
#include "base/tcpsocket.hpp"
#include "base/tlsstream.hpp"
#include "base/threadpool.hpp"
#include <atomic>
#include <boost/asio/io_context.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/spawn.hpp>
Expand Down Expand Up @@ -179,7 +179,7 @@ class ApiListener final : public ObjectImpl<ApiListener>
Endpoint::Ptr m_LocalEndpoint;

static ApiListener::Ptr m_Instance;
static std::atomic<bool> m_UpdatedObjectAuthority;
static Atomic<bool> m_UpdatedObjectAuthority;

void ApiTimerHandler();
void ApiReconnectTimerHandler();
Expand Down
2 changes: 1 addition & 1 deletion lib/remote/configstageshandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using namespace icinga;

REGISTER_URLHANDLER("/v1/config/stages", ConfigStagesHandler);

std::atomic<bool> ConfigStagesHandler::m_RunningPackageUpdates (false);
Atomic<bool> ConfigStagesHandler::m_RunningPackageUpdates (false);

bool ConfigStagesHandler::HandleRequest(
AsioTlsStream& stream,
Expand Down
Loading

0 comments on commit 33181a3

Please sign in to comment.