Common/Analytics: Replace mutex with shared_mutex and minor cleanups.

This commit is contained in:
Jordan Woyak 2020-01-23 20:42:52 -06:00
parent 42c03c4dad
commit 5e3472eba9
1 changed files with 8 additions and 12 deletions

View File

@ -6,7 +6,7 @@
#include <chrono> #include <chrono>
#include <memory> #include <memory>
#include <mutex> #include <shared_mutex>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <thread> #include <thread>
@ -59,19 +59,16 @@ public:
AnalyticsReportBuilder(); AnalyticsReportBuilder();
~AnalyticsReportBuilder() = default; ~AnalyticsReportBuilder() = default;
AnalyticsReportBuilder(const AnalyticsReportBuilder& other) { *this = other; } AnalyticsReportBuilder(const AnalyticsReportBuilder& other) : m_report{other.Get()} {}
AnalyticsReportBuilder(AnalyticsReportBuilder&& other) AnalyticsReportBuilder(AnalyticsReportBuilder&& other) : m_report{other.Consume()} {}
{
std::lock_guard lk{other.m_lock};
m_report = std::move(other.m_report);
}
const AnalyticsReportBuilder& operator=(const AnalyticsReportBuilder& other) const AnalyticsReportBuilder& operator=(const AnalyticsReportBuilder& other)
{ {
if (this != &other) if (this != &other)
{ {
std::scoped_lock lk{m_lock, other.m_lock}; std::string other_report = other.Get();
m_report = other.m_report; std::lock_guard lk{m_lock};
m_report = std::move(other_report);
} }
return *this; return *this;
} }
@ -106,7 +103,7 @@ public:
std::string Get() const std::string Get() const
{ {
std::lock_guard lk{m_lock}; std::shared_lock lk{m_lock};
return m_report; return m_report;
} }
@ -129,8 +126,7 @@ protected:
static void AppendSerializedValueVector(std::string* report, const std::vector<u32>& v); static void AppendSerializedValueVector(std::string* report, const std::vector<u32>& v);
// Should really be a std::shared_mutex, unfortunately that's C++17 only. mutable std::shared_mutex m_lock;
mutable std::mutex m_lock;
std::string m_report; std::string m_report;
}; };