Skip to content

Commit ed9cce5

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 8e10d09 commit ed9cce5

21 files changed

Lines changed: 38 additions & 43 deletions

lib/base/atomic.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,17 @@ class Locked
112112
};
113113

114114
/**
115-
* Type alias for std::atomic<T> if possible, otherwise Locked<T> is used as a fallback.
115+
* Type alias for Atomic<T> if possible, otherwise Locked<T> is used as a fallback.
116116
*
117117
* @ingroup base
118118
*/
119119
template <typename T>
120120
using AtomicOrLocked =
121121
#if defined(__GNUC__) && __GNUC__ < 5
122122
// GCC does not implement std::is_trivially_copyable until version 5.
123-
typename std::conditional<std::is_fundamental<T>::value || std::is_pointer<T>::value, std::atomic<T>, Locked<T>>::type;
123+
typename std::conditional<std::is_fundamental<T>::value || std::is_pointer<T>::value, Atomic<T>, Locked<T>>::type;
124124
#else /* defined(__GNUC__) && __GNUC__ < 5 */
125-
typename std::conditional<std::is_trivially_copyable<T>::value, std::atomic<T>, Locked<T>>::type;
125+
typename std::conditional<std::is_trivially_copyable<T>::value, Atomic<T>, Locked<T>>::type;
126126
#endif /* defined(__GNUC__) && __GNUC__ < 5 */
127127

128128
}

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
@@ -34,7 +34,7 @@ REGISTER_TYPE(Logger);
3434
std::set<Logger::Ptr> Logger::m_Loggers;
3535
std::mutex Logger::m_Mutex;
3636
bool Logger::m_ConsoleLogEnabled = true;
37-
std::atomic<bool> Logger::m_EarlyLoggingEnabled (true);
37+
Atomic<bool> Logger::m_EarlyLoggingEnabled (true);
3838
bool Logger::m_TimestampEnabled = true;
3939
LogSeverity Logger::m_ConsoleLogSeverity = LogInformation;
4040
std::mutex Logger::m_UpdateMinLogSeverityMutex;

lib/base/logger.hpp

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

lib/base/namespace.hpp

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

66
#include "base/i2-base.hpp"
7+
#include "base/atomic.hpp"
78
#include "base/object.hpp"
89
#include "base/objectlock.hpp"
910
#include "base/shared-object.hpp"
1011
#include "base/value.hpp"
1112
#include "base/debuginfo.hpp"
12-
#include <atomic>
1313
#include <map>
1414
#include <vector>
1515
#include <memory>
@@ -95,7 +95,7 @@ class Namespace final : public Object
9595
std::map<String, NamespaceValue> m_Data;
9696
mutable std::shared_timed_mutex m_DataMutex;
9797
bool m_ConstValues;
98-
std::atomic<bool> m_Frozen;
98+
Atomic<bool> m_Frozen;
9999
};
100100

101101
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>
@@ -193,11 +193,11 @@ class Object
193193
Object(const Object& other) = delete;
194194
Object& operator=(const Object& rhs) = delete;
195195

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

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

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)