mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
Made mutex into a value instead of a pointer in ConcurrentMap
This commit is contained in:
parent
8fea0542a3
commit
ed59c3f77f
1 changed files with 11 additions and 12 deletions
|
@ -16,58 +16,57 @@ public:
|
||||||
ConcurrentMap()
|
ConcurrentMap()
|
||||||
: map()
|
: map()
|
||||||
{
|
{
|
||||||
this->mutex = new QMutex();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
tryGet(const TKey &name, TValue &value) const
|
tryGet(const TKey &name, TValue &value) const
|
||||||
{
|
{
|
||||||
this->mutex->lock();
|
this->mutex.lock();
|
||||||
auto a = map.find(name);
|
auto a = map.find(name);
|
||||||
if (a == map.end()) {
|
if (a == map.end()) {
|
||||||
this->mutex->unlock();
|
this->mutex.unlock();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
value = a.value();
|
value = a.value();
|
||||||
this->mutex->unlock();
|
this->mutex.unlock();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
TValue
|
TValue
|
||||||
getOrAdd(const TKey &name, std::function<TValue()> addLambda)
|
getOrAdd(const TKey &name, std::function<TValue()> addLambda)
|
||||||
{
|
{
|
||||||
this->mutex->lock();
|
this->mutex.lock();
|
||||||
auto a = map.find(name);
|
auto a = map.find(name);
|
||||||
|
|
||||||
if (a == map.end()) {
|
if (a == map.end()) {
|
||||||
TValue value = addLambda();
|
TValue value = addLambda();
|
||||||
map.insert(name, value);
|
map.insert(name, value);
|
||||||
this->mutex->unlock();
|
this->mutex.unlock();
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->mutex->unlock();
|
this->mutex.unlock();
|
||||||
return a.value();
|
return a.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
clear()
|
clear()
|
||||||
{
|
{
|
||||||
this->mutex->lock();
|
this->mutex.lock();
|
||||||
map.clear();
|
map.clear();
|
||||||
this->mutex->unlock();
|
this->mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
insert(const TKey &name, const TValue &value)
|
insert(const TKey &name, const TValue &value)
|
||||||
{
|
{
|
||||||
this->mutex->lock();
|
this->mutex.lock();
|
||||||
map.insert(name, value);
|
map.insert(name, value);
|
||||||
this->mutex->unlock();
|
this->mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QMutex *mutex;
|
mutable QMutex mutex;
|
||||||
QMap<TKey, TValue> map;
|
QMap<TKey, TValue> map;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue