Skip to content

Commit 33181a3

Browse files
committed
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<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
1 parent 48c27cc commit 33181a3

21 files changed

Lines changed: 40 additions & 45 deletions

lib/base/io-engine.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,11 @@ boost::asio::io_context& IoEngine::GetIoContext()
8585
return m_IoContext;
8686
}
8787

88-
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)
88+
IoEngine::IoEngine() : m_IoContext(), m_KeepAlive(boost::asio::make_work_guard(m_IoContext)),
89+
m_Threads(decltype(m_Threads)::size_type(Configuration::Concurrency * 2u)),
90+
m_AlreadyExpiredTimer(m_IoContext), m_CpuBoundSemaphore(Configuration::Concurrency * 3u / 2u)
8991
{
9092
m_AlreadyExpiredTimer.expires_at(boost::posix_time::neg_infin);
91-
m_CpuBoundSemaphore.store(Configuration::Concurrency * 3u / 2u);
9293

9394
for (auto& thread : m_Threads) {
9495
thread = std::thread(&IoEngine::RunEventLoop, this);

lib/base/io-engine.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
#ifndef IO_ENGINE_H
44
#define IO_ENGINE_H
55

6+
#include "base/atomic.hpp"
67
#include "base/exception.hpp"
78
#include "base/lazy-init.hpp"
89
#include "base/logger.hpp"
910
#include "base/shared-object.hpp"
10-
#include <atomic>
11+
#include <cstdint>
1112
#include <exception>
1213
#include <memory>
1314
#include <thread>
@@ -135,7 +136,7 @@ class IoEngine
135136
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> m_KeepAlive;
136137
std::vector<std::thread> m_Threads;
137138
boost::asio::deadline_timer m_AlreadyExpiredTimer;
138-
std::atomic_int_fast32_t m_CpuBoundSemaphore;
139+
Atomic<int_fast32_t> m_CpuBoundSemaphore;
139140
};
140141

141142
class TerminateIoThread : public std::exception
@@ -176,7 +177,6 @@ class Timeout : public SharedObject
176177
{
177178
Ptr keepAlive (this);
178179

179-
m_Cancelled.store(false);
180180
m_Timer.expires_from_now(std::move(timeoutFromNow));
181181

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

208208
private:
209209
boost::asio::deadline_timer m_Timer;
210-
std::atomic<bool> m_Cancelled;
210+
Atomic<bool> m_Cancelled {false};
211211
};
212212

213213
}

lib/base/lazy-init.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#ifndef LAZY_INIT
44
#define LAZY_INIT
55

6-
#include <atomic>
6+
#include "base/atomic.hpp"
77
#include <functional>
88
#include <mutex>
99
#include <utility>
@@ -24,7 +24,6 @@ class LazyInit
2424
inline
2525
LazyInit(std::function<T()> initializer = []() { return T(); }) : m_Initializer(std::move(initializer))
2626
{
27-
m_Underlying.store(nullptr, std::memory_order_release);
2827
}
2928

3029
LazyInit(const LazyInit&) = delete;
@@ -64,7 +63,7 @@ class LazyInit
6463
private:
6564
std::function<T()> m_Initializer;
6665
std::mutex m_Mutex;
67-
std::atomic<T*> m_Underlying;
66+
Atomic<T*> m_Underlying {nullptr};
6867
};
6968

7069
}

lib/base/logger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ REGISTER_TYPE(Logger);
3333
std::set<Logger::Ptr> Logger::m_Loggers;
3434
std::mutex Logger::m_Mutex;
3535
bool Logger::m_ConsoleLogEnabled = true;
36-
std::atomic<bool> Logger::m_EarlyLoggingEnabled (true);
36+
Atomic<bool> Logger::m_EarlyLoggingEnabled (true);
3737
bool Logger::m_TimestampEnabled = true;
3838
LogSeverity Logger::m_ConsoleLogSeverity = LogInformation;
3939
std::mutex Logger::m_UpdateMinLogSeverityMutex;

lib/base/logger.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class Logger : public ObjectImpl<Logger>
9999
static std::mutex m_Mutex;
100100
static std::set<Logger::Ptr> m_Loggers;
101101
static bool m_ConsoleLogEnabled;
102-
static std::atomic<bool> m_EarlyLoggingEnabled;
102+
static Atomic<bool> m_EarlyLoggingEnabled;
103103
static bool m_TimestampEnabled;
104104
static LogSeverity m_ConsoleLogSeverity;
105105
static std::mutex m_UpdateMinLogSeverityMutex;

lib/base/namespace.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
#define NAMESPACE_H
55

66
#include "base/i2-base.hpp"
7+
#include "base/atomic.hpp"
78
#include "base/object.hpp"
89
#include "base/shared-object.hpp"
910
#include "base/value.hpp"
1011
#include "base/debuginfo.hpp"
11-
#include <atomic>
1212
#include <map>
1313
#include <vector>
1414
#include <memory>
@@ -92,7 +92,7 @@ class Namespace final : public Object
9292
std::map<String, NamespaceValue> m_Data;
9393
mutable std::shared_timed_mutex m_DataMutex;
9494
bool m_ConstValues;
95-
std::atomic<bool> m_Frozen;
95+
Atomic<bool> m_Frozen;
9696
};
9797

9898
Namespace::Iterator begin(const Namespace::Ptr& x);

lib/base/object.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ static Timer::Ptr l_ObjectCountTimer;
2727
*/
2828
Object::Object()
2929
{
30-
m_References.store(0);
31-
32-
#ifdef I2_DEBUG
33-
m_LockOwner.store(decltype(m_LockOwner.load())());
34-
#endif /* I2_DEBUG */
3530
}
3631

3732
/**

lib/base/object.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
#define OBJECT_H
55

66
#include "base/i2-base.hpp"
7+
#include "base/atomic.hpp"
78
#include "base/debug.hpp"
89
#include <boost/smart_ptr/intrusive_ptr.hpp>
9-
#include <atomic>
1010
#include <cstddef>
1111
#include <cstdint>
1212
#include <mutex>
@@ -191,11 +191,11 @@ class Object
191191
Object(const Object& other) = delete;
192192
Object& operator=(const Object& rhs) = delete;
193193

194-
std::atomic<uint_fast64_t> m_References;
194+
Atomic<uint_fast64_t> m_References {0};
195195
mutable std::recursive_mutex m_Mutex;
196196

197197
#ifdef I2_DEBUG
198-
mutable std::atomic<std::thread::id> m_LockOwner;
198+
mutable Atomic<std::thread::id> m_LockOwner {std::thread::id()};
199199
mutable size_t m_LockCount = 0;
200200
#endif /* I2_DEBUG */
201201

lib/base/tlsstream.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
#define TLSSTREAM_H
55

66
#include "base/i2-base.hpp"
7+
#include "base/atomic.hpp"
78
#include "base/shared.hpp"
89
#include "base/socket.hpp"
910
#include "base/stream.hpp"
1011
#include "base/tlsutility.hpp"
1112
#include "base/fifo.hpp"
1213
#include "base/utility.hpp"
13-
#include <atomic>
1414
#include <memory>
1515
#include <utility>
1616
#include <boost/asio/buffered_stream.hpp>
@@ -30,7 +30,6 @@ class SeenStream : public ARS
3030
template<class... Args>
3131
SeenStream(Args&&... args) : ARS(std::forward<Args>(args)...)
3232
{
33-
m_Seen.store(nullptr);
3433
}
3534

3635
template<class... Args>
@@ -53,7 +52,7 @@ class SeenStream : public ARS
5352
}
5453

5554
private:
56-
std::atomic<double*> m_Seen;
55+
Atomic<double*> m_Seen {nullptr};
5756
};
5857

5958
struct UnbufferedAsioTlsStreamParams

lib/base/workqueue.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
using namespace icinga;
1313

14-
std::atomic<int> WorkQueue::m_NextID(1);
14+
Atomic<int> WorkQueue::m_NextID (1);
1515
boost::thread_specific_ptr<WorkQueue *> l_ThreadWorkQueue;
1616

1717
WorkQueue::WorkQueue(size_t maxItems, int threadCount, LogSeverity statsLogLevel)

0 commit comments

Comments
 (0)