From f5a0e3f1f4787b6a48880e42463ae38dce336a8f Mon Sep 17 00:00:00 2001
From: Milian Wolff <mail@milianw.de>
Date: Mon, 1 Dec 2014 11:36:31 +0100
Subject: [PATCH 15/30] Use QMutexLocker instead of manual lock/unlock calls.
Just a minor cleanup patch, no change of behavior.
---
server/src/storage/entities-source.xsl | 17 +++++------------
server/src/storage/entities.xsl | 4 +---
2 files changed, 6 insertions(+), 15 deletions(-)
diff --git a/server/src/storage/entities-source.xsl b/server/src/storage/entities-source.xsl
index 7090c31..05a8cb1 100644
--- a/server/src/storage/entities-source.xsl
+++ b/server/src/storage/entities-source.xsl
@@ -125,14 +125,13 @@ void <xsl:value-of select="$className"/>::Private::addToCache( const <xsl:value-
{
Q_ASSERT( cacheEnabled );
Q_UNUSED( entry ); <!-- in case the table has neither an id nor name column -->
- cacheMutex.lock();
+ QMutexLocker lock(&cacheMutex);
<xsl:if test="column[@name = 'id']">
idCache.insert( entry.id(), entry );
</xsl:if>
<xsl:if test="column[@name = 'name']">
nameCache.insert( entry.name(), entry );
</xsl:if>
- cacheMutex.unlock();
}
@@ -264,12 +263,10 @@ int <xsl:value-of select="$className"/>::count( const QString &column, const
bool <xsl:value-of select="$className"/>::exists( qint64 id )
{
if ( Private::cacheEnabled ) {
- Private::cacheMutex.lock();
+ QMutexLocker lock(&Private::cacheMutex);
if ( Private::idCache.contains( id ) ) {
- Private::cacheMutex.unlock();
return true;
}
- Private::cacheMutex.unlock();
}
return count( idColumn(), id ) > 0;
}
@@ -278,12 +275,10 @@ bool <xsl:value-of select="$className"/>::exists( qint64 id )
bool <xsl:value-of select="$className"/>::exists( const <xsl:value-of select="column[@name = 'name']/@type"/> &name )
{
if ( Private::cacheEnabled ) {
- Private::cacheMutex.lock();
+ QMutexLocker lock(&Private::cacheMutex);
if ( Private::nameCache.contains( name ) ) {
- Private::cacheMutex.unlock();
return true;
}
- Private::cacheMutex.unlock();
}
return count( nameColumn(), name ) > 0;
}
@@ -588,28 +583,26 @@ bool <xsl:value-of select="$className"/>::remove( qint64 id )
void <xsl:value-of select="$className"/>::invalidateCache() const
{
if ( Private::cacheEnabled ) {
- Private::cacheMutex.lock();
+ QMutexLocker lock(&Private::cacheMutex);
<xsl:if test="column[@name = 'id']">
Private::idCache.remove( id() );
</xsl:if>
<xsl:if test="column[@name = 'name']">
Private::nameCache.remove( name() );
</xsl:if>
- Private::cacheMutex.unlock();
}
}
void <xsl:value-of select="$className"/>::invalidateCompleteCache()
{
if ( Private::cacheEnabled ) {
- Private::cacheMutex.lock();
+ QMutexLocker lock(&Private::cacheMutex);
<xsl:if test="column[@name = 'id']">
Private::idCache.clear();
</xsl:if>
<xsl:if test="column[@name = 'name']">
Private::nameCache.clear();
</xsl:if>
- Private::cacheMutex.unlock();
}
}
diff --git a/server/src/storage/entities.xsl b/server/src/storage/entities.xsl
index 8b0ed03..a397544 100644
--- a/server/src/storage/entities.xsl
+++ b/server/src/storage/entities.xsl
@@ -167,13 +167,11 @@ set<xsl:value-of select="$methodName"/>( <xsl:call-template name="argument"/> )
<xsl:variable name="className"><xsl:value-of select="@name"/></xsl:variable>
<xsl:if test="$cache != ''">
if ( Private::cacheEnabled ) {
- Private::cacheMutex.lock();
+ QMutexLocker lock(&Private::cacheMutex);
if ( Private::<xsl:value-of select="$cache"/>.contains( <xsl:value-of select="$key"/> ) ) {
const <xsl:value-of select="$className"/> tmp = Private::<xsl:value-of select="$cache"/>.value( <xsl:value-of select="$key"/> );
- Private::cacheMutex.unlock();
return tmp;
}
- Private::cacheMutex.unlock();
}
</xsl:if>
QSqlDatabase db = DataStore::self()->database();
--
2.1.0