1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-01 20:00:13 +02:00

* Thread-safety for liblastfm.

This commit is contained in:
Christian Muehlhaeuser
2011-09-10 05:45:57 +02:00
parent 859d2e8641
commit 6fe1d178dd

View File

@@ -26,8 +26,12 @@
#include <QLocale> #include <QLocale>
#include <QStringList> #include <QStringList>
#include <QUrl> #include <QUrl>
static QNetworkAccessManager* nam = 0; #include <QThread>
#include <QMutex>
static QMap< QThread*, QNetworkAccessManager* > threadNamHash;
static QSet< QThread* > ourNamSet;
static QMutex namAccessMutex;
QString QString
lastfm::ws::host() lastfm::ws::host()
@@ -130,17 +134,39 @@ lastfm::ws::post( QMap<QString, QString> params, bool sk )
QNetworkAccessManager* QNetworkAccessManager*
lastfm::nam() lastfm::nam()
{ {
if (!::nam) ::nam = new NetworkAccessManager( qApp ); QMutexLocker l( &namAccessMutex );
return ::nam; QThread* thread = QThread::currentThread();
if ( !threadNamHash.contains( thread ) )
{
NetworkAccessManager* newNam = new NetworkAccessManager( qApp );
threadNamHash[thread] = newNam;
ourNamSet.insert( thread );
return newNam;
}
return threadNamHash[thread];
} }
void void
lastfm::setNetworkAccessManager( QNetworkAccessManager* nam ) lastfm::setNetworkAccessManager( QNetworkAccessManager* nam )
{ {
delete ::nam; if ( !nam )
::nam = nam; return;
nam->setParent( qApp ); // ensure it isn't deleted out from under us
QMutexLocker l( &namAccessMutex );
QThread* thread = QThread::currentThread();
QNetworkAccessManager* oldNam = 0;
if ( threadNamHash.contains( thread ) && ourNamSet.contains( thread ) )
oldNam = threadNamHash[thread];
if ( oldNam == nam )
return;
threadNamHash[thread] = nam;
ourNamSet.remove( thread );
if ( oldNam )
delete oldNam;
} }