Skip to content
Permalink
Browse files
Merge pull request #8577 from jordan-woyak/shared_mutex
Common/Analytics: Replace mutex with shared_mutex and minor cleanups.
  • Loading branch information
Tilka committed Jan 25, 2020
2 parents 119ccc5 + 5e3472e commit 9827aa7
Showing 1 changed file with 8 additions and 12 deletions.
@@ -6,7 +6,7 @@

#include <chrono>
#include <memory>
#include <mutex>
#include <shared_mutex>
#include <string>
#include <string_view>
#include <thread>
@@ -59,19 +59,16 @@ class AnalyticsReportBuilder
AnalyticsReportBuilder();
~AnalyticsReportBuilder() = default;

AnalyticsReportBuilder(const AnalyticsReportBuilder& other) { *this = other; }
AnalyticsReportBuilder(AnalyticsReportBuilder&& other)
{
std::lock_guard lk{other.m_lock};
m_report = std::move(other.m_report);
}
AnalyticsReportBuilder(const AnalyticsReportBuilder& other) : m_report{other.Get()} {}
AnalyticsReportBuilder(AnalyticsReportBuilder&& other) : m_report{other.Consume()} {}

const AnalyticsReportBuilder& operator=(const AnalyticsReportBuilder& other)
{
if (this != &other)
{
std::scoped_lock lk{m_lock, other.m_lock};
m_report = other.m_report;
std::string other_report = other.Get();
std::lock_guard lk{m_lock};
m_report = std::move(other_report);
}
return *this;
}
@@ -106,7 +103,7 @@ class AnalyticsReportBuilder

std::string Get() const
{
std::lock_guard lk{m_lock};
std::shared_lock lk{m_lock};
return m_report;
}

@@ -129,8 +126,7 @@ class AnalyticsReportBuilder

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::mutex m_lock;
mutable std::shared_mutex m_lock;
std::string m_report;
};

0 comments on commit 9827aa7

Please sign in to comment.