mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-09 10:43:19 -05:00
Merge #17135: gui: Make polling in ClientModel asynchronous
6b6be41c36
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: ACK6b6be41c36
Sjors: Code review re-ACK 6b6be41; only replaced the scary cast with `{ timer->start(); }` Tree-SHA512: fd98b0c6535441aee3ee03c48b58b4b1f9bdd172ec6b8150da883022f719df34cabfd4c133412bf410e7f709f7bf1e9ef16dca05ef1f3689d526ceaeee51de38
This commit is contained in:
commit
e9f73b839a
2 changed files with 21 additions and 14 deletions
|
@ -19,6 +19,7 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
#include <QThread>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
|
|
||||||
static int64_t nLastHeaderTipUpdateNotification = 0;
|
static int64_t nLastHeaderTipUpdateNotification = 0;
|
||||||
|
@ -30,15 +31,26 @@ ClientModel::ClientModel(interfaces::Node& node, OptionsModel *_optionsModel, QO
|
||||||
optionsModel(_optionsModel),
|
optionsModel(_optionsModel),
|
||||||
peerTableModel(nullptr),
|
peerTableModel(nullptr),
|
||||||
banTableModel(nullptr),
|
banTableModel(nullptr),
|
||||||
pollTimer(nullptr)
|
m_thread(new QThread(this))
|
||||||
{
|
{
|
||||||
cachedBestHeaderHeight = -1;
|
cachedBestHeaderHeight = -1;
|
||||||
cachedBestHeaderTime = -1;
|
cachedBestHeaderTime = -1;
|
||||||
peerTableModel = new PeerTableModel(m_node, this);
|
peerTableModel = new PeerTableModel(m_node, this);
|
||||||
banTableModel = new BanTableModel(m_node, this);
|
banTableModel = new BanTableModel(m_node, this);
|
||||||
pollTimer = new QTimer(this);
|
|
||||||
connect(pollTimer, &QTimer::timeout, this, &ClientModel::updateTimer);
|
QTimer* timer = new QTimer;
|
||||||
pollTimer->start(MODEL_UPDATE_DELAY);
|
timer->setInterval(MODEL_UPDATE_DELAY);
|
||||||
|
connect(timer, &QTimer::timeout, [this] {
|
||||||
|
// no locking required at this point
|
||||||
|
// the following calls will acquire the required lock
|
||||||
|
Q_EMIT mempoolSizeChanged(m_node.getMempoolSize(), m_node.getMempoolDynamicUsage());
|
||||||
|
Q_EMIT bytesChanged(m_node.getTotalBytesRecv(), m_node.getTotalBytesSent());
|
||||||
|
});
|
||||||
|
connect(m_thread, &QThread::finished, timer, &QObject::deleteLater);
|
||||||
|
connect(m_thread, &QThread::started, [timer] { timer->start(); });
|
||||||
|
// move timer to thread so that polling doesn't disturb main event loop
|
||||||
|
timer->moveToThread(m_thread);
|
||||||
|
m_thread->start();
|
||||||
|
|
||||||
subscribeToCoreSignals();
|
subscribeToCoreSignals();
|
||||||
}
|
}
|
||||||
|
@ -46,6 +58,9 @@ ClientModel::ClientModel(interfaces::Node& node, OptionsModel *_optionsModel, QO
|
||||||
ClientModel::~ClientModel()
|
ClientModel::~ClientModel()
|
||||||
{
|
{
|
||||||
unsubscribeFromCoreSignals();
|
unsubscribeFromCoreSignals();
|
||||||
|
|
||||||
|
m_thread->quit();
|
||||||
|
m_thread->wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
int ClientModel::getNumConnections(unsigned int flags) const
|
int ClientModel::getNumConnections(unsigned int flags) const
|
||||||
|
@ -90,14 +105,6 @@ int64_t ClientModel::getHeaderTipTime() const
|
||||||
return cachedBestHeaderTime;
|
return cachedBestHeaderTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientModel::updateTimer()
|
|
||||||
{
|
|
||||||
// no locking required at this point
|
|
||||||
// the following calls will acquire the required lock
|
|
||||||
Q_EMIT mempoolSizeChanged(m_node.getMempoolSize(), m_node.getMempoolDynamicUsage());
|
|
||||||
Q_EMIT bytesChanged(m_node.getTotalBytesRecv(), m_node.getTotalBytesSent());
|
|
||||||
}
|
|
||||||
|
|
||||||
void ClientModel::updateNumConnections(int numConnections)
|
void ClientModel::updateNumConnections(int numConnections)
|
||||||
{
|
{
|
||||||
Q_EMIT numConnectionsChanged(numConnections);
|
Q_EMIT numConnectionsChanged(numConnections);
|
||||||
|
|
|
@ -90,7 +90,8 @@ private:
|
||||||
PeerTableModel *peerTableModel;
|
PeerTableModel *peerTableModel;
|
||||||
BanTableModel *banTableModel;
|
BanTableModel *banTableModel;
|
||||||
|
|
||||||
QTimer *pollTimer;
|
//! A thread to interact with m_node asynchronously
|
||||||
|
QThread* const m_thread;
|
||||||
|
|
||||||
void subscribeToCoreSignals();
|
void subscribeToCoreSignals();
|
||||||
void unsubscribeFromCoreSignals();
|
void unsubscribeFromCoreSignals();
|
||||||
|
@ -110,7 +111,6 @@ Q_SIGNALS:
|
||||||
void showProgress(const QString &title, int nProgress);
|
void showProgress(const QString &title, int nProgress);
|
||||||
|
|
||||||
public Q_SLOTS:
|
public Q_SLOTS:
|
||||||
void updateTimer();
|
|
||||||
void updateNumConnections(int numConnections);
|
void updateNumConnections(int numConnections);
|
||||||
void updateNetworkActive(bool networkActive);
|
void updateNetworkActive(bool networkActive);
|
||||||
void updateAlert();
|
void updateAlert();
|
||||||
|
|
Loading…
Add table
Reference in a new issue