From 27cb54c1dbce437a2d2acb20aba3fa6ffa80576c Mon Sep 17 00:00:00 2001 From: sfan5 Date: Tue, 14 May 2024 19:26:22 +0200 Subject: [PATCH] Stop misusing volatile keyword --- src/log.cpp | 4 ++-- src/log.h | 10 +++++----- src/network/connection.cpp | 4 ++-- src/unittest/test_threading.cpp | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/log.cpp b/src/log.cpp index 5ee66c070..6dbc43372 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -253,7 +253,7 @@ const std::string Logger::getThreadName() void Logger::log(LogLevel lev, const std::string &text) { - if (m_silenced_levels[lev]) + if (isLevelSilenced(lev)) return; const std::string thread_name = getThreadName(); @@ -267,7 +267,7 @@ void Logger::log(LogLevel lev, const std::string &text) void Logger::logRaw(LogLevel lev, const std::string &text) { - if (m_silenced_levels[lev]) + if (isLevelSilenced(lev)) return; logToOutputsRaw(lev, text); diff --git a/src/log.h b/src/log.h index 4255e55bc..f92d17737 100644 --- a/src/log.h +++ b/src/log.h @@ -79,6 +79,10 @@ class Logger { return m_has_outputs[level].load(std::memory_order_relaxed); } + bool isLevelSilenced(LogLevel level) { + return m_silenced_levels[level].load(std::memory_order_relaxed); + } + static LogColor color_mode; private: @@ -91,11 +95,7 @@ class Logger { std::vector m_outputs[LL_MAX]; std::atomic m_has_outputs[LL_MAX]; - - // Should implement atomic loads and stores (even though it's only - // written to when one thread has access currently). - // Works on all known architectures (x86, ARM, MIPS). - volatile bool m_silenced_levels[LL_MAX]; + std::atomic m_silenced_levels[LL_MAX]; std::map m_thread_names; mutable std::mutex m_mutex; }; diff --git a/src/network/connection.cpp b/src/network/connection.cpp index 84a6c53c5..00b4fe4b0 100644 --- a/src/network/connection.cpp +++ b/src/network/connection.cpp @@ -1078,7 +1078,7 @@ bool UDPPeer::processReliableSendCommand( bool have_sequence_number = false; bool have_initial_sequence_number = false; std::queue toadd; - volatile u16 initial_sequence_number = 0; + u16 initial_sequence_number = 0; for (SharedBuffer &original : originals) { u16 seqnum = chan.getOutgoingSequenceNumber(have_sequence_number); @@ -1118,7 +1118,7 @@ bool UDPPeer::processReliableSendCommand( return true; } - volatile u16 packets_available = toadd.size(); + u16 packets_available = toadd.size(); /* we didn't get a single sequence number no need to fill queue */ if (!have_initial_sequence_number) { LOG(derr_con << m_connection->getDesc() << "Ran out of sequence numbers!" << std::endl); diff --git a/src/unittest/test_threading.cpp b/src/unittest/test_threading.cpp index 9d7b7c808..a91e1b532 100644 --- a/src/unittest/test_threading.cpp +++ b/src/unittest/test_threading.cpp @@ -161,7 +161,7 @@ void TestThreading::testAtomicSemaphoreThread() -static volatile bool g_tls_broken; +static std::atomic g_tls_broken; class TLSTestThread : public Thread { public: @@ -226,7 +226,7 @@ class TLSTestThread : public Thread { */ void TestThreading::testTLS() { - static const int num_threads = 10; + constexpr int num_threads = 10; for (int j = 0; j < num_threads; j++) { g_tls_broken = false;