From 12812e24ff2c590fae39c48a76c5dcd8b37ac8b0 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sun, 13 Aug 2017 14:52:31 +0200 Subject: [PATCH] Make ConcurrentMap more correct --- src/concurrentmap.hpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/concurrentmap.hpp b/src/concurrentmap.hpp index 5d92d3e5a..fe87a773a 100644 --- a/src/concurrentmap.hpp +++ b/src/concurrentmap.hpp @@ -6,6 +6,7 @@ #include #include +#include namespace chatterino { @@ -19,7 +20,7 @@ public: bool tryGet(const TKey &name, TValue &value) const { - QMutexLocker lock(&this->mutex); + QMutexLocker lock(this->mutex.get()); auto a = this->data.find(name); if (a == this->data.end()) { @@ -33,7 +34,7 @@ public: TValue getOrAdd(const TKey &name, std::function addLambda) { - QMutexLocker lock(&this->mutex); + QMutexLocker lock(this->mutex.get()); auto a = this->data.find(name); if (a == this->data.end()) { @@ -47,36 +48,41 @@ public: TValue &operator[](const TKey &name) { - QMutexLocker lock(&this->mutex); + QMutexLocker lock(this->mutex.get()); return this->data[name]; } ConcurrentMap(const ConcurrentMap &o) + : mutex(std::move(o.mutex)) + , data(std::move(o.data)) { } ConcurrentMap &operator=(const ConcurrentMap &rhs) { + this->mutex = std::move(rhs.mutex); + this->data = std::move(rhs.data); + return *this; } void clear() { - QMutexLocker lock(&this->mutex); + QMutexLocker lock(this->mutex.get()); this->data.clear(); } void insert(const TKey &name, const TValue &value) { - QMutexLocker lock(&this->mutex); + QMutexLocker lock(this->mutex.get()); this->data.insert(name, value); } private: - mutable QMutex mutex; + mutable std::unique_ptr mutex; QMap data; };