Skip to content

Commit e9f73b8

Browse files
committed
Merge #17135: gui: Make polling in ClientModel asynchronous
6b6be41 gui: Make polling in ClientModel asynchronous (João Barbosa) Pull request description: After #14193 `ClientModel::updateTimer` can take some time, as such the GUI hangs, like #17112. Fixes this by polling in a background thread and updating the GUI asynchronously. ACKs for top commit: laanwj: ACK 6b6be41 Sjors: Code review re-ACK 6b6be41; only replaced the scary cast with `{ timer->start(); }` Tree-SHA512: fd98b0c6535441aee3ee03c48b58b4b1f9bdd172ec6b8150da883022f719df34cabfd4c133412bf410e7f709f7bf1e9ef16dca05ef1f3689d526ceaeee51de38
2 parents d91af47 + 6b6be41 commit e9f73b8

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

src/qt/clientmodel.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <stdint.h>
2020

2121
#include <QDebug>
22+
#include <QThread>
2223
#include <QTimer>
2324

2425
static int64_t nLastHeaderTipUpdateNotification = 0;
@@ -30,22 +31,36 @@ ClientModel::ClientModel(interfaces::Node& node, OptionsModel *_optionsModel, QO
3031
optionsModel(_optionsModel),
3132
peerTableModel(nullptr),
3233
banTableModel(nullptr),
33-
pollTimer(nullptr)
34+
m_thread(new QThread(this))
3435
{
3536
cachedBestHeaderHeight = -1;
3637
cachedBestHeaderTime = -1;
3738
peerTableModel = new PeerTableModel(m_node, this);
3839
banTableModel = new BanTableModel(m_node, this);
39-
pollTimer = new QTimer(this);
40-
connect(pollTimer, &QTimer::timeout, this, &ClientModel::updateTimer);
41-
pollTimer->start(MODEL_UPDATE_DELAY);
40+
41+
QTimer* timer = new QTimer;
42+
timer->setInterval(MODEL_UPDATE_DELAY);
43+
connect(timer, &QTimer::timeout, [this] {
44+
// no locking required at this point
45+
// the following calls will acquire the required lock
46+
Q_EMIT mempoolSizeChanged(m_node.getMempoolSize(), m_node.getMempoolDynamicUsage());
47+
Q_EMIT bytesChanged(m_node.getTotalBytesRecv(), m_node.getTotalBytesSent());
48+
});
49+
connect(m_thread, &QThread::finished, timer, &QObject::deleteLater);
50+
connect(m_thread, &QThread::started, [timer] { timer->start(); });
51+
// move timer to thread so that polling doesn't disturb main event loop
52+
timer->moveToThread(m_thread);
53+
m_thread->start();
4254

4355
subscribeToCoreSignals();
4456
}
4557

4658
ClientModel::~ClientModel()
4759
{
4860
unsubscribeFromCoreSignals();
61+
62+
m_thread->quit();
63+
m_thread->wait();
4964
}
5065

5166
int ClientModel::getNumConnections(unsigned int flags) const
@@ -90,14 +105,6 @@ int64_t ClientModel::getHeaderTipTime() const
90105
return cachedBestHeaderTime;
91106
}
92107

93-
void ClientModel::updateTimer()
94-
{
95-
// no locking required at this point
96-
// the following calls will acquire the required lock
97-
Q_EMIT mempoolSizeChanged(m_node.getMempoolSize(), m_node.getMempoolDynamicUsage());
98-
Q_EMIT bytesChanged(m_node.getTotalBytesRecv(), m_node.getTotalBytesSent());
99-
}
100-
101108
void ClientModel::updateNumConnections(int numConnections)
102109
{
103110
Q_EMIT numConnectionsChanged(numConnections);

src/qt/clientmodel.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ class ClientModel : public QObject
9090
PeerTableModel *peerTableModel;
9191
BanTableModel *banTableModel;
9292

93-
QTimer *pollTimer;
93+
//! A thread to interact with m_node asynchronously
94+
QThread* const m_thread;
9495

9596
void subscribeToCoreSignals();
9697
void unsubscribeFromCoreSignals();
@@ -110,7 +111,6 @@ class ClientModel : public QObject
110111
void showProgress(const QString &title, int nProgress);
111112

112113
public Q_SLOTS:
113-
void updateTimer();
114114
void updateNumConnections(int numConnections);
115115
void updateNetworkActive(bool networkActive);
116116
void updateAlert();

0 commit comments

Comments
 (0)