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:
38
thirdparty/liblastfm2/src/ws/ws.cpp
vendored
38
thirdparty/liblastfm2/src/ws/ws.cpp
vendored
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user