Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.19: gui: Make polling in ClientModel asynchronous #17252

Merged
merged 1 commit into from Oct 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 19 additions & 12 deletions src/qt/clientmodel.cpp
Expand Up @@ -19,6 +19,7 @@
#include <stdint.h>

#include <QDebug>
#include <QThread>
#include <QTimer>

static int64_t nLastHeaderTipUpdateNotification = 0;
Expand All @@ -30,22 +31,36 @@ ClientModel::ClientModel(interfaces::Node& node, OptionsModel *_optionsModel, QO
optionsModel(_optionsModel),
peerTableModel(nullptr),
banTableModel(nullptr),
pollTimer(nullptr)
m_thread(new QThread(this))
{
cachedBestHeaderHeight = -1;
cachedBestHeaderTime = -1;
peerTableModel = new PeerTableModel(m_node, this);
banTableModel = new BanTableModel(m_node, this);
pollTimer = new QTimer(this);
connect(pollTimer, &QTimer::timeout, this, &ClientModel::updateTimer);
pollTimer->start(MODEL_UPDATE_DELAY);

QTimer* timer = new QTimer;
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();
}

ClientModel::~ClientModel()
{
unsubscribeFromCoreSignals();

m_thread->quit();
m_thread->wait();
}

int ClientModel::getNumConnections(unsigned int flags) const
Expand Down Expand Up @@ -90,14 +105,6 @@ int64_t ClientModel::getHeaderTipTime() const
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)
{
Q_EMIT numConnectionsChanged(numConnections);
Expand Down
4 changes: 2 additions & 2 deletions src/qt/clientmodel.h
Expand Up @@ -90,7 +90,8 @@ class ClientModel : public QObject
PeerTableModel *peerTableModel;
BanTableModel *banTableModel;

QTimer *pollTimer;
//! A thread to interact with m_node asynchronously
QThread* const m_thread;

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

public Q_SLOTS:
void updateTimer();
void updateNumConnections(int numConnections);
void updateNetworkActive(bool networkActive);
void updateAlert();
Expand Down