Skip to content

Commit

Permalink
qt: Replace QMetaObject::invokeMethod with Mutex locking
Browse files Browse the repository at this point in the history
Mutex locking is a standard way to access to a data member from multiple
threads.
  • Loading branch information
hebasto committed Nov 2, 2020
1 parent 867dbeb commit 223dc9e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
6 changes: 2 additions & 4 deletions src/qt/transactiontablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -726,14 +726,12 @@ static void ShowProgress(TransactionTableModel *ttm, const std::string &title, i
{
fQueueNotifications = false;
if (vQueueNotifications.size() > 10) { // prevent balloon spam, show maximum 10 balloons
bool invoked = QMetaObject::invokeMethod(ttm, "setProcessingQueuedTransactions", Qt::QueuedConnection, Q_ARG(bool, true));
assert(invoked);
ttm->setProcessingQueuedTransactions(true);
}
for (unsigned int i = 0; i < vQueueNotifications.size(); ++i)
{
if (vQueueNotifications.size() - i <= 10) {
bool invoked = QMetaObject::invokeMethod(ttm, "setProcessingQueuedTransactions", Qt::QueuedConnection, Q_ARG(bool, false));
assert(invoked);
ttm->setProcessingQueuedTransactions(false);
}

vQueueNotifications[i].invoke(ttm);
Expand Down
19 changes: 15 additions & 4 deletions src/qt/transactiontablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define BITCOIN_QT_TRANSACTIONTABLEMODEL_H

#include <qt/bitcoinunits.h>
#include <sync.h>

#include <QAbstractTableModel>
#include <QStringList>
Expand Down Expand Up @@ -81,15 +82,27 @@ class TransactionTableModel : public QAbstractTableModel
QVariant data(const QModelIndex &index, int role) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const override;
bool processingQueuedTransactions() const { return fProcessingQueuedTransactions; }

bool processingQueuedTransactions() const
{
LOCK(m_processing_queued_transactions);
return fProcessingQueuedTransactions;
}

void setProcessingQueuedTransactions(bool value)
{
LOCK(m_processing_queued_transactions);
fProcessingQueuedTransactions = value;
}

private:
WalletModel *walletModel;
std::unique_ptr<interfaces::Handler> m_handler_transaction_changed;
std::unique_ptr<interfaces::Handler> m_handler_show_progress;
QStringList columns;
TransactionTablePriv *priv;
bool fProcessingQueuedTransactions;
mutable Mutex m_processing_queued_transactions;
bool fProcessingQueuedTransactions GUARDED_BY(m_processing_queued_transactions);
const PlatformStyle *platformStyle;

void subscribeToCoreSignals();
Expand All @@ -114,8 +127,6 @@ public Q_SLOTS:
void updateDisplayUnit();
/** Updates the column title to "Amount (DisplayUnit)" and emits headerDataChanged() signal for table headers to react. */
void updateAmountColumnTitle();
/* Needed to update fProcessingQueuedTransactions through a QueuedConnection */
void setProcessingQueuedTransactions(bool value) { fProcessingQueuedTransactions = value; }

friend class TransactionTablePriv;
};
Expand Down

0 comments on commit 223dc9e

Please sign in to comment.