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:
54
thirdparty/liblastfm2/src/ws/ws.cpp
vendored
54
thirdparty/liblastfm2/src/ws/ws.cpp
vendored
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
Copyright 2009 Last.fm Ltd.
|
Copyright 2009 Last.fm Ltd.
|
||||||
- Primarily authored by Max Howell, Jono Cole and Doug Mansell
|
- Primarily authored by Max Howell, Jono Cole and Doug Mansell
|
||||||
|
|
||||||
This file is part of liblastfm.
|
This file is part of liblastfm.
|
||||||
@@ -26,10 +26,14 @@
|
|||||||
#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()
|
||||||
{
|
{
|
||||||
QStringList const args = QCoreApplication::arguments();
|
QStringList const args = QCoreApplication::arguments();
|
||||||
@@ -53,8 +57,8 @@ static QUrl baseUrl()
|
|||||||
}
|
}
|
||||||
|
|
||||||
static QString iso639()
|
static QString iso639()
|
||||||
{
|
{
|
||||||
return QLocale().name().left( 2 ).toLower();
|
return QLocale().name().left( 2 ).toLower();
|
||||||
}
|
}
|
||||||
|
|
||||||
void autograph( QMap<QString, QString>& params )
|
void autograph( QMap<QString, QString>& params )
|
||||||
@@ -109,8 +113,8 @@ lastfm::ws::get( QMap<QString, QString> params )
|
|||||||
|
|
||||||
QNetworkReply*
|
QNetworkReply*
|
||||||
lastfm::ws::post( QMap<QString, QString> params, bool sk )
|
lastfm::ws::post( QMap<QString, QString> params, bool sk )
|
||||||
{
|
{
|
||||||
sign( params, sk );
|
sign( params, sk );
|
||||||
QByteArray query;
|
QByteArray query;
|
||||||
QMapIterator<QString, QString> i( params );
|
QMapIterator<QString, QString> i( params );
|
||||||
while (i.hasNext()) {
|
while (i.hasNext()) {
|
||||||
@@ -129,18 +133,40 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -198,7 +224,7 @@ namespace lastfm
|
|||||||
const char* ApiKey;
|
const char* ApiKey;
|
||||||
|
|
||||||
/** if this is found set to "" we conjure ourselves a suitable one */
|
/** if this is found set to "" we conjure ourselves a suitable one */
|
||||||
const char* UserAgent = 0;
|
const char* UserAgent = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user