From 05fc30c1ad90392e4ef356be863c4ee4d6a2c8d3 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sun, 29 Jan 2017 19:14:12 +0100 Subject: [PATCH] make use of convenience class QMutexLocker in ConcurrentMap --- concurrentmap.h | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/concurrentmap.h b/concurrentmap.h index 3ce3e31bf..f26a4bd37 100644 --- a/concurrentmap.h +++ b/concurrentmap.h @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -21,54 +22,54 @@ public: bool tryGet(const TKey &name, TValue &value) const { - this->mutex.lock(); + QMutexLocker lock(&this->mutex); + auto a = map.find(name); if (a == map.end()) { - this->mutex.unlock(); return false; } + value = a.value(); - this->mutex.unlock(); + return true; } TValue getOrAdd(const TKey &name, std::function addLambda) { - this->mutex.lock(); - auto a = map.find(name); + QMutexLocker lock(&this->mutex); + auto a = map.find(name); if (a == map.end()) { TValue value = addLambda(); map.insert(name, value); - this->mutex.unlock(); return value; } - this->mutex.unlock(); return a.value(); } void clear() { - this->mutex.lock(); + QMutexLocker lock(&this->mutex); + map.clear(); - this->mutex.unlock(); } void insert(const TKey &name, const TValue &value) { - this->mutex.lock(); + QMutexLocker lock(&this->mutex); + map.insert(name, value); - this->mutex.unlock(); } private: mutable QMutex mutex; QMap map; }; -} + +} // namespace chatterino #endif // CONCURRENTMAP_H