[libcalamares] Apply locking to GS access

main
Adriaan de Groot 4 years ago
parent dc5d98af7d
commit ac713d8c4b

@ -27,12 +27,35 @@
#include <QFile> #include <QFile>
#include <QJsonDocument> #include <QJsonDocument>
#include <QMutexLocker>
using CalamaresUtils::operator""_MiB; using CalamaresUtils::operator""_MiB;
namespace Calamares namespace Calamares
{ {
class GlobalStorage::ReadLock : public QMutexLocker
{
public:
ReadLock( const GlobalStorage* gs )
: QMutexLocker( &gs->m_mutex )
{
}
};
class GlobalStorage::WriteLock : public QMutexLocker
{
public:
WriteLock( GlobalStorage* gs )
: QMutexLocker( &gs->m_mutex )
, m_gs( gs )
{
}
~WriteLock() { m_gs->changed(); }
GlobalStorage* m_gs;
};
GlobalStorage::GlobalStorage( QObject* parent ) GlobalStorage::GlobalStorage( QObject* parent )
: QObject( parent ) : QObject( parent )
{ {
@ -42,6 +65,7 @@ GlobalStorage::GlobalStorage( QObject* parent )
bool bool
GlobalStorage::contains( const QString& key ) const GlobalStorage::contains( const QString& key ) const
{ {
ReadLock l( this );
return m.contains( key ); return m.contains( key );
} }
@ -49,6 +73,7 @@ GlobalStorage::contains( const QString& key ) const
int int
GlobalStorage::count() const GlobalStorage::count() const
{ {
ReadLock l( this );
return m.count(); return m.count();
} }
@ -56,14 +81,15 @@ GlobalStorage::count() const
void void
GlobalStorage::insert( const QString& key, const QVariant& value ) GlobalStorage::insert( const QString& key, const QVariant& value )
{ {
WriteLock l( this );
m.insert( key, value ); m.insert( key, value );
emit changed();
} }
QStringList QStringList
GlobalStorage::keys() const GlobalStorage::keys() const
{ {
ReadLock l( this );
return m.keys(); return m.keys();
} }
@ -71,8 +97,8 @@ GlobalStorage::keys() const
int int
GlobalStorage::remove( const QString& key ) GlobalStorage::remove( const QString& key )
{ {
WriteLock l( this );
int nItems = m.remove( key ); int nItems = m.remove( key );
emit changed();
return nItems; return nItems;
} }
@ -80,13 +106,15 @@ GlobalStorage::remove( const QString& key )
QVariant QVariant
GlobalStorage::value( const QString& key ) const GlobalStorage::value( const QString& key ) const
{ {
ReadLock l( this );
return m.value( key ); return m.value( key );
} }
void void
GlobalStorage::debugDump() const GlobalStorage::debugDump() const
{ {
cDebug() << "GlobalStorage" << Logger::Pointer(this) << m.count() << "items"; ReadLock l( this );
cDebug() << "GlobalStorage" << Logger::Pointer( this ) << m.count() << "items";
for ( auto it = m.cbegin(); it != m.cend(); ++it ) for ( auto it = m.cbegin(); it != m.cend(); ++it )
{ {
cDebug() << Logger::SubEntry << it.key() << '\t' << it.value(); cDebug() << Logger::SubEntry << it.key() << '\t' << it.value();
@ -96,6 +124,7 @@ GlobalStorage::debugDump() const
bool bool
GlobalStorage::saveJson( const QString& filename ) const GlobalStorage::saveJson( const QString& filename ) const
{ {
ReadLock l( this );
QFile f( filename ); QFile f( filename );
if ( !f.open( QFile::WriteOnly ) ) if ( !f.open( QFile::WriteOnly ) )
{ {
@ -128,6 +157,7 @@ GlobalStorage::loadJson( const QString& filename )
} }
else else
{ {
WriteLock l( this );
auto map = d.toVariant().toMap(); auto map = d.toVariant().toMap();
for ( auto i = map.constBegin(); i != map.constEnd(); ++i ) for ( auto i = map.constBegin(); i != map.constEnd(); ++i )
{ {
@ -141,6 +171,7 @@ GlobalStorage::loadJson( const QString& filename )
bool bool
GlobalStorage::saveYaml( const QString& filename ) const GlobalStorage::saveYaml( const QString& filename ) const
{ {
ReadLock l( this );
return CalamaresUtils::saveYaml( filename, m ); return CalamaresUtils::saveYaml( filename, m );
} }
@ -151,9 +182,11 @@ GlobalStorage::loadYaml( const QString& filename )
auto gs = CalamaresUtils::loadYaml( filename, &ok ); auto gs = CalamaresUtils::loadYaml( filename, &ok );
if ( ok ) if ( ok )
{ {
WriteLock l( this );
m = gs; m = gs;
return true;
} }
return ok; return false;
} }

Loading…
Cancel
Save