Skip to content

Commit

Permalink
QDnsLookup: replace Q_GLOBAL_STATIC with Q_APPLICATION_STATIC
Browse files Browse the repository at this point in the history
Replaces a lot of the manual content.

Change-Id: I3e3bfef633af4130a03afffd175e839f4b05975c
Reviewed-by: Mårten Nordheim <[email protected]>
  • Loading branch information
thiagomacieira committed May 24, 2023
1 parent 9a4eb45 commit f5b5840
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 53 deletions.
55 changes: 18 additions & 37 deletions src/network/kernel/qdnslookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "qdnslookup.h"
#include "qdnslookup_p.h"

#include <qapplicationstatic.h>
#include <qcoreapplication.h>
#include <qdatetime.h>
#include <qrandom.h>
Expand All @@ -14,7 +15,18 @@

QT_BEGIN_NAMESPACE

Q_GLOBAL_STATIC(QDnsLookupThreadPool, theDnsLookupThreadPool);
namespace {
struct QDnsLookupThreadPool : QThreadPool
{
QDnsLookupThreadPool()
{
// Run up to 5 lookups in parallel.
setMaxThreadCount(5);
}
};
}

Q_APPLICATION_STATIC(QDnsLookupThreadPool, theDnsLookupThreadPool);

static bool qt_qdnsmailexchangerecord_less_than(const QDnsMailExchangeRecord &r1, const QDnsMailExchangeRecord &r2)
{
Expand Down Expand Up @@ -531,6 +543,11 @@ void QDnsLookup::lookup()
Q_D(QDnsLookup);
d->isFinished = false;
d->reply = QDnsLookupReply();
if (!QCoreApplication::instance()) {
qWarning("QDnsLookup requires a QCoreApplication");
return;
}

auto l = [this](const QDnsLookupReply &reply) {
Q_D(QDnsLookup);
if (d->runnable == sender()) {
Expand Down Expand Up @@ -1056,42 +1073,6 @@ void QDnsLookupRunnable::run()
emit finished(reply);
}

QDnsLookupThreadPool::QDnsLookupThreadPool()
: signalsConnected(false)
{
// Run up to 5 lookups in parallel.
setMaxThreadCount(5);
}

void QDnsLookupThreadPool::start(QRunnable *runnable)
{
// Ensure threads complete at application destruction.
if (!signalsConnected) {
QMutexLocker signalsLocker(&signalsMutex);
if (!signalsConnected) {
QCoreApplication *app = QCoreApplication::instance();
if (!app) {
qWarning("QDnsLookup requires a QCoreApplication");
delete runnable;
return;
}

moveToThread(app->thread());
connect(app, SIGNAL(destroyed()),
SLOT(_q_applicationDestroyed()), Qt::DirectConnection);
signalsConnected = true;
}
}

QThreadPool::start(runnable);
}

void QDnsLookupThreadPool::_q_applicationDestroyed()
{
waitForDone();
signalsConnected = false;
}

QT_END_NAMESPACE

#include "moc_qdnslookup.cpp"
Expand Down
16 changes: 0 additions & 16 deletions src/network/kernel/qdnslookup_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,6 @@ class QDnsLookupRunnable : public QObject, public QRunnable
quint16 port;
};

class QDnsLookupThreadPool : public QThreadPool
{
Q_OBJECT

public:
QDnsLookupThreadPool();
void start(QRunnable *runnable);

private slots:
void _q_applicationDestroyed();

private:
QMutex signalsMutex;
bool signalsConnected;
};

class QDnsRecordPrivate : public QSharedData
{
public:
Expand Down

0 comments on commit f5b5840

Please sign in to comment.