1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-07-31 19:30:21 +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 <QStringList>
#include <QUrl>
static QNetworkAccessManager* nam = 0;
#include <QThread>
#include <QMutex>
static QMap< QThread*, QNetworkAccessManager* > threadNamHash;
static QSet< QThread* > ourNamSet;
static QMutex namAccessMutex;
QString
lastfm::ws::host()
@@ -130,17 +134,39 @@ lastfm::ws::post( QMap<QString, QString> params, bool sk )
QNetworkAccessManager*
lastfm::nam()
{
if (!::nam) ::nam = new NetworkAccessManager( qApp );
return ::nam;
QMutexLocker l( &namAccessMutex );
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
lastfm::setNetworkAccessManager( QNetworkAccessManager* nam )
{
delete ::nam;
::nam = nam;
nam->setParent( qApp ); // ensure it isn't deleted out from under us
if ( !nam )
return;
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;
}