mutex added when accessing the cache
This commit is contained in:
parent
65a73f949e
commit
31244327f2
@ -7,6 +7,7 @@
|
||||
#include "QskGraphic.h"
|
||||
#include "QskSetup.h"
|
||||
|
||||
#include <qmutex.h>
|
||||
#include <qcache.h>
|
||||
#include <qdebug.h>
|
||||
#include <qurl.h>
|
||||
@ -16,6 +17,7 @@ class QskGraphicProvider::PrivateData
|
||||
public:
|
||||
// caching of graphics
|
||||
QCache< QString, const QskGraphic > cache;
|
||||
QMutex mutex;
|
||||
};
|
||||
|
||||
QskGraphicProvider::QskGraphicProvider( QObject* parent )
|
||||
@ -33,34 +35,55 @@ void QskGraphicProvider::setCacheSize( int size )
|
||||
if ( size < 0 )
|
||||
size = 0;
|
||||
|
||||
QMutexLocker locker( &m_data->mutex );
|
||||
m_data->cache.setMaxCost( size );
|
||||
}
|
||||
|
||||
int QskGraphicProvider::cacheSize() const
|
||||
{
|
||||
QMutexLocker locker( &m_data->mutex );
|
||||
return m_data->cache.maxCost();
|
||||
}
|
||||
|
||||
void QskGraphicProvider::clearCache()
|
||||
{
|
||||
QMutexLocker locker( &m_data->mutex );
|
||||
m_data->cache.clear();
|
||||
}
|
||||
|
||||
const QskGraphic* QskGraphicProvider::requestGraphic( const QString& id ) const
|
||||
{
|
||||
const QskGraphic* graphic = m_data->cache.object( id );
|
||||
const QskGraphic* graphic = nullptr;
|
||||
|
||||
{
|
||||
QMutexLocker locker( &m_data->mutex );
|
||||
graphic = m_data->cache.object( id );
|
||||
}
|
||||
|
||||
if ( graphic == nullptr )
|
||||
{
|
||||
graphic = loadGraphic( id );
|
||||
|
||||
if ( graphic == nullptr )
|
||||
{
|
||||
qWarning() << "QskGraphicProvider: can't load" << id;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
const int cost = 1; // TODO ...
|
||||
m_data->cache.insert( id, graphic, cost );
|
||||
{
|
||||
QMutexLocker locker( &m_data->mutex );
|
||||
|
||||
if( auto cached = m_data->cache.object( id ) )
|
||||
{
|
||||
delete graphic;
|
||||
graphic = cached;
|
||||
}
|
||||
else
|
||||
{
|
||||
const int cost = 1; // TODO ...
|
||||
m_data->cache.insert( id, graphic, cost );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return graphic;
|
||||
|
Loading…
x
Reference in New Issue
Block a user