Skip to content

Commit 3ef77a9

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 e79f489 commit 3ef77a9

22 files changed

Lines changed: 41 additions & 46 deletions

lib/base/atomic.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,17 @@ class Locked
7474
};
7575

7676
/**
77-
* Type alias for std::atomic<T> if possible, otherwise Locked<T> is used as a fallback.
77+
* Type alias for Atomic<T> if possible, otherwise Locked<T> is used as a fallback.
7878
*
7979
* @ingroup base
8080
*/
8181
template <typename T>
8282
using AtomicOrLocked =
8383
#if defined(__GNUC__) && __GNUC__ < 5
8484
// GCC does not implement std::is_trivially_copyable until version 5.
85-
typename std::conditional<std::is_fundamental<T>::value || std::is_pointer<T>::value, std::atomic<T>, Locked<T>>::type;
85+
typename std::conditional<std::is_fundamental<T>::value || std::is_pointer<T>::value, Atomic<T>, Locked<T>>::type;
8686
#else /* defined(__GNUC__) && __GNUC__ < 5 */
87-
typename std::conditional<std::is_trivially_copyable<T>::value, std::atomic<T>, Locked<T>>::type;
87+
typename std::conditional<std::is_trivially_copyable<T>::value, Atomic<T>, Locked<T>>::type;
8888
#endif /* defined(__GNUC__) && __GNUC__ < 5 */
8989

9090
}

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "base/lazy-init.hpp"
1010
#include "base/logger.hpp"
1111
#include "base/shared.hpp"
12-
#include <atomic>
12+
#include <cstdint>
1313
#include <exception>
1414
#include <memory>
1515
#include <thread>
@@ -150,7 +150,7 @@ class IoEngine
150150
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> m_KeepAlive;
151151
std::vector<std::thread> m_Threads;
152152
boost::asio::deadline_timer m_AlreadyExpiredTimer;
153-
std::atomic_int_fast32_t m_CpuBoundSemaphore;
153+
Atomic<int_fast32_t> m_CpuBoundSemaphore;
154154
};
155155

156156
class TerminateIoThread : public std::exception

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,10 +4,10 @@
44
#define OBJECT_H
55

66
#include "base/i2-base.hpp"
7+
#include "base/atomic.hpp"
78
#include "base/debug.hpp"
89
#include "base/intrusive-ptr.hpp"
910
#include <boost/smart_ptr/intrusive_ptr.hpp>
10-
#include <atomic>
1111
#include <cstddef>
1212
#include <cstdint>
1313
#include <mutex>
@@ -192,11 +192,11 @@ class Object
192192
Object(const Object& other) = delete;
193193
Object& operator=(const Object& rhs) = delete;
194194

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

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

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

0 commit comments

Comments
 (0)