Remove the use of QMutexPool in QHostInfo and QDnsLookup

Q_GLOBAL_STATIC does the thread-safe protection for us. And who said we
could only use non-POD types? We can just use a boolean!

Change-Id: Ifea6e497f11a461db432ffff1449b0a88d75d194
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
Thiago Macieira 2016-04-28 18:36:31 -07:00
parent fda6324b4d
commit c9f9f54d3f
3 changed files with 16 additions and 32 deletions

View File

@ -36,7 +36,6 @@
#include <qlibrary.h> #include <qlibrary.h>
#include <qscopedpointer.h> #include <qscopedpointer.h>
#include <qurl.h> #include <qurl.h>
#include <private/qmutexpool_p.h>
#include <sys/types.h> #include <sys/types.h>
#include <netinet/in.h> #include <netinet/in.h>
@ -71,7 +70,7 @@ struct QDnsLookupStateDeleter
} }
}; };
static void resolveLibrary() static bool resolveLibraryInternal()
{ {
QLibrary lib; QLibrary lib;
#ifdef LIBRESOLV_SO #ifdef LIBRESOLV_SO
@ -81,7 +80,7 @@ static void resolveLibrary()
{ {
lib.setFileName(QLatin1String("resolv")); lib.setFileName(QLatin1String("resolv"));
if (!lib.load()) if (!lib.load())
return; return false;
} }
local_dn_expand = dn_expand_proto(lib.resolve("__dn_expand")); local_dn_expand = dn_expand_proto(lib.resolve("__dn_expand"));
@ -105,19 +104,15 @@ static void resolveLibrary()
local_res_nquery = res_nquery_proto(lib.resolve("res_9_nquery")); local_res_nquery = res_nquery_proto(lib.resolve("res_9_nquery"));
if (!local_res_nquery) if (!local_res_nquery)
local_res_nquery = res_nquery_proto(lib.resolve("res_nquery")); local_res_nquery = res_nquery_proto(lib.resolve("res_nquery"));
return true;
} }
Q_GLOBAL_STATIC_WITH_ARGS(bool, resolveLibrary, (resolveLibraryInternal()))
void QDnsLookupRunnable::query(const int requestType, const QByteArray &requestName, const QHostAddress &nameserver, QDnsLookupReply *reply) void QDnsLookupRunnable::query(const int requestType, const QByteArray &requestName, const QHostAddress &nameserver, QDnsLookupReply *reply)
{ {
// Load dn_expand, res_ninit and res_nquery on demand. // Load dn_expand, res_ninit and res_nquery on demand.
static QBasicAtomicInt triedResolve = Q_BASIC_ATOMIC_INITIALIZER(false); resolveLibrary();
if (!triedResolve.loadAcquire()) {
QMutexLocker locker(QMutexPool::globalInstanceGet(&local_res_ninit));
if (!triedResolve.load()) {
resolveLibrary();
triedResolve.storeRelease(true);
}
}
// If dn_expand, res_ninit or res_nquery is missing, fail. // If dn_expand, res_ninit or res_nquery is missing, fail.
if (!local_dn_expand || !local_res_nclose || !local_res_ninit || !local_res_nquery) { if (!local_dn_expand || !local_res_nclose || !local_res_ninit || !local_res_nquery) {

View File

@ -43,7 +43,6 @@
#include <qbasicatomic.h> #include <qbasicatomic.h>
#include <qurl.h> #include <qurl.h>
#include <qfile.h> #include <qfile.h>
#include <private/qmutexpool_p.h>
#include <private/qnet_unix_p.h> #include <private/qnet_unix_p.h>
#include <sys/types.h> #include <sys/types.h>
@ -86,7 +85,7 @@ typedef void (*res_nclose_proto)(res_state_ptr);
static res_nclose_proto local_res_nclose = 0; static res_nclose_proto local_res_nclose = 0;
static res_state_ptr local_res = 0; static res_state_ptr local_res = 0;
static void resolveLibrary() static bool resolveLibraryInternal()
{ {
#if !defined(QT_NO_LIBRARY) && !defined(Q_OS_QNX) #if !defined(QT_NO_LIBRARY) && !defined(Q_OS_QNX)
QLibrary lib; QLibrary lib;
@ -97,7 +96,7 @@ static void resolveLibrary()
{ {
lib.setFileName(QLatin1String("resolv")); lib.setFileName(QLatin1String("resolv"));
if (!lib.load()) if (!lib.load())
return; return false;
} }
local_res_init = res_init_proto(lib.resolve("__res_init")); local_res_init = res_init_proto(lib.resolve("__res_init"));
@ -119,7 +118,10 @@ static void resolveLibrary()
local_res_ninit = 0; local_res_ninit = 0;
} }
#endif #endif
return true;
} }
Q_GLOBAL_STATIC_WITH_ARGS(bool, resolveLibrary, (resolveLibraryInternal()))
QHostInfo QHostInfoAgent::fromName(const QString &hostName) QHostInfo QHostInfoAgent::fromName(const QString &hostName)
{ {
@ -131,14 +133,7 @@ QHostInfo QHostInfoAgent::fromName(const QString &hostName)
#endif #endif
// Load res_init on demand. // Load res_init on demand.
static QBasicAtomicInt triedResolve = Q_BASIC_ATOMIC_INITIALIZER(false); resolveLibrary();
if (!triedResolve.loadAcquire()) {
QMutexLocker locker(QMutexPool::globalInstanceGet(&local_res_init));
if (!triedResolve.load()) {
resolveLibrary();
triedResolve.storeRelease(true);
}
}
// If res_init is available, poll it. // If res_init is available, poll it.
if (local_res_init) if (local_res_init)

View File

@ -39,7 +39,6 @@
#include <qmutex.h> #include <qmutex.h>
#include <qbasicatomic.h> #include <qbasicatomic.h>
#include <qurl.h> #include <qurl.h>
#include <private/qmutexpool_p.h>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -72,7 +71,7 @@ static getnameinfoProto local_getnameinfo = 0;
static getaddrinfoProto local_getaddrinfo = 0; static getaddrinfoProto local_getaddrinfo = 0;
static freeaddrinfoProto local_freeaddrinfo = 0; static freeaddrinfoProto local_freeaddrinfo = 0;
static void resolveLibrary() static bool resolveLibraryInternal()
{ {
// Attempt to resolve getaddrinfo(); without it we'll have to fall // Attempt to resolve getaddrinfo(); without it we'll have to fall
// back to gethostbyname(), which has no IPv6 support. // back to gethostbyname(), which has no IPv6 support.
@ -89,7 +88,9 @@ static void resolveLibrary()
local_freeaddrinfo = (freeaddrinfoProto) QSystemLibrary::resolve(QLatin1String("ws2_32"), "freeaddrinfo"); local_freeaddrinfo = (freeaddrinfoProto) QSystemLibrary::resolve(QLatin1String("ws2_32"), "freeaddrinfo");
local_getnameinfo = (getnameinfoProto) QSystemLibrary::resolve(QLatin1String("ws2_32"), "getnameinfo"); local_getnameinfo = (getnameinfoProto) QSystemLibrary::resolve(QLatin1String("ws2_32"), "getnameinfo");
#endif #endif
return true;
} }
Q_GLOBAL_STATIC_WITH_ARGS(bool, resolveLibrary, (resolveLibraryInternal()))
static void translateWSAError(int error, QHostInfo *results) static void translateWSAError(int error, QHostInfo *results)
{ {
@ -117,14 +118,7 @@ QHostInfo QHostInfoAgent::fromName(const QString &hostName)
QSysInfo::machineHostName(); // this initializes ws2_32.dll QSysInfo::machineHostName(); // this initializes ws2_32.dll
// Load res_init on demand. // Load res_init on demand.
static QBasicAtomicInt triedResolve = Q_BASIC_ATOMIC_INITIALIZER(false); resolveLibrary();
if (!triedResolve.loadAcquire()) {
QMutexLocker locker(QMutexPool::globalInstanceGet(&local_getaddrinfo));
if (!triedResolve.load()) {
resolveLibrary();
triedResolve.storeRelease(true);
}
}
QHostInfo results; QHostInfo results;