From 19a1d008310f250b69b7aa764a9f26384d5a4a85 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Wed, 14 Jul 2021 11:48:21 +0300 Subject: qt: Add BitcoinCore::m_thread member This change makes BitcoinCore self-contained to improve its re-usability. BitcoinApplication::coreThread member is now unused, and removed. --- src/qt/bitcoin.cpp | 41 ++++++++++++++++++----------------------- src/qt/bitcoin.h | 11 ++++++++--- 2 files changed, 26 insertions(+), 26 deletions(-) (limited to 'src/qt') diff --git a/src/qt/bitcoin.cpp b/src/qt/bitcoin.cpp index 442c813a5a..e5caafe290 100644 --- a/src/qt/bitcoin.cpp +++ b/src/qt/bitcoin.cpp @@ -158,6 +158,16 @@ void DebugMessageHandler(QtMsgType type, const QMessageLogContext& context, cons BitcoinCore::BitcoinCore(interfaces::Node& node) : QObject(), m_node(node) { + this->moveToThread(&m_thread); + m_thread.start(); +} + +BitcoinCore::~BitcoinCore() +{ + qDebug() << __func__ << ": Stopping thread"; + m_thread.quit(); + m_thread.wait(); + qDebug() << __func__ << ": Stopped thread"; } void BitcoinCore::handleRunawayException(const std::exception *e) @@ -202,7 +212,6 @@ static const char* qt_argv = "bitcoin-qt"; BitcoinApplication::BitcoinApplication(): QApplication(qt_argc, const_cast(&qt_argv)), - coreThread(nullptr), optionsModel(nullptr), clientModel(nullptr), window(nullptr), @@ -230,13 +239,7 @@ void BitcoinApplication::setupPlatformStyle() BitcoinApplication::~BitcoinApplication() { - if(coreThread) - { - qDebug() << __func__ << ": Stopping thread"; - coreThread->quit(); - coreThread->wait(); - qDebug() << __func__ << ": Stopped thread"; - } + m_executor.reset(); delete window; window = nullptr; @@ -291,22 +294,15 @@ bool BitcoinApplication::baseInitialize() void BitcoinApplication::startThread() { - if(coreThread) - return; - coreThread = new QThread(this); - BitcoinCore *executor = new BitcoinCore(node()); - executor->moveToThread(coreThread); + assert(!m_executor); + m_executor.emplace(node()); /* communication to and from thread */ - connect(executor, &BitcoinCore::initializeResult, this, &BitcoinApplication::initializeResult); - connect(executor, &BitcoinCore::shutdownResult, this, &BitcoinApplication::shutdownResult); - connect(executor, &BitcoinCore::runawayException, this, &BitcoinApplication::handleRunawayException); - connect(this, &BitcoinApplication::requestedInitialize, executor, &BitcoinCore::initialize); - connect(this, &BitcoinApplication::requestedShutdown, executor, &BitcoinCore::shutdown); - /* make sure executor object is deleted in its own thread */ - connect(coreThread, &QThread::finished, executor, &QObject::deleteLater); - - coreThread->start(); + connect(&m_executor.value(), &BitcoinCore::initializeResult, this, &BitcoinApplication::initializeResult); + connect(&m_executor.value(), &BitcoinCore::shutdownResult, this, &BitcoinApplication::shutdownResult); + connect(&m_executor.value(), &BitcoinCore::runawayException, this, &BitcoinApplication::handleRunawayException); + connect(this, &BitcoinApplication::requestedInitialize, &m_executor.value(), &BitcoinCore::initialize); + connect(this, &BitcoinApplication::requestedShutdown, &m_executor.value(), &BitcoinCore::shutdown); } void BitcoinApplication::parameterSetup() @@ -339,7 +335,6 @@ void BitcoinApplication::requestShutdown() shutdownWindow.reset(ShutdownWindow::showShutdownWindow(window)); qDebug() << __func__ << ": Requesting shutdown"; - startThread(); window->hide(); // Must disconnect node signals otherwise current thread can deadlock since // no event loop is running. diff --git a/src/qt/bitcoin.h b/src/qt/bitcoin.h index f9fab0534b..d977062d6b 100644 --- a/src/qt/bitcoin.h +++ b/src/qt/bitcoin.h @@ -9,11 +9,14 @@ #include #endif -#include +#include + #include #include +#include -#include +#include +#include class BitcoinGUI; class ClientModel; @@ -34,6 +37,7 @@ class BitcoinCore: public QObject Q_OBJECT public: explicit BitcoinCore(interfaces::Node& node); + ~BitcoinCore(); public Q_SLOTS: void initialize(); @@ -49,6 +53,7 @@ private: void handleRunawayException(const std::exception *e); interfaces::Node& m_node; + QThread m_thread; }; /** Main Bitcoin application object */ @@ -112,7 +117,7 @@ Q_SIGNALS: void windowShown(BitcoinGUI* window); private: - QThread *coreThread; + std::optional m_executor; OptionsModel *optionsModel; ClientModel *clientModel; BitcoinGUI *window; -- cgit v1.2.3 From dbcf56b6c6e939923673b3f07bed7bb3632dbeb1 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Wed, 14 Jul 2021 19:03:42 +0300 Subject: scripted-diff: Rename BitcoinCore class to InitExecutor -BEGIN VERIFY SCRIPT- sed -i -e 's/BitcoinCore/InitExecutor/g' src/qt/bitcoin.* -END VERIFY SCRIPT- --- src/qt/bitcoin.cpp | 20 ++++++++++---------- src/qt/bitcoin.h | 8 ++++---- 2 files changed, 14 insertions(+), 14 deletions(-) (limited to 'src/qt') diff --git a/src/qt/bitcoin.cpp b/src/qt/bitcoin.cpp index e5caafe290..dae56aa56c 100644 --- a/src/qt/bitcoin.cpp +++ b/src/qt/bitcoin.cpp @@ -155,14 +155,14 @@ void DebugMessageHandler(QtMsgType type, const QMessageLogContext& context, cons } } -BitcoinCore::BitcoinCore(interfaces::Node& node) : +InitExecutor::InitExecutor(interfaces::Node& node) : QObject(), m_node(node) { this->moveToThread(&m_thread); m_thread.start(); } -BitcoinCore::~BitcoinCore() +InitExecutor::~InitExecutor() { qDebug() << __func__ << ": Stopping thread"; m_thread.quit(); @@ -170,13 +170,13 @@ BitcoinCore::~BitcoinCore() qDebug() << __func__ << ": Stopped thread"; } -void BitcoinCore::handleRunawayException(const std::exception *e) +void InitExecutor::handleRunawayException(const std::exception *e) { PrintExceptionContinue(e, "Runaway exception"); Q_EMIT runawayException(QString::fromStdString(m_node.getWarnings().translated)); } -void BitcoinCore::initialize() +void InitExecutor::initialize() { try { @@ -192,7 +192,7 @@ void BitcoinCore::initialize() } } -void BitcoinCore::shutdown() +void InitExecutor::shutdown() { try { @@ -298,11 +298,11 @@ void BitcoinApplication::startThread() m_executor.emplace(node()); /* communication to and from thread */ - connect(&m_executor.value(), &BitcoinCore::initializeResult, this, &BitcoinApplication::initializeResult); - connect(&m_executor.value(), &BitcoinCore::shutdownResult, this, &BitcoinApplication::shutdownResult); - connect(&m_executor.value(), &BitcoinCore::runawayException, this, &BitcoinApplication::handleRunawayException); - connect(this, &BitcoinApplication::requestedInitialize, &m_executor.value(), &BitcoinCore::initialize); - connect(this, &BitcoinApplication::requestedShutdown, &m_executor.value(), &BitcoinCore::shutdown); + connect(&m_executor.value(), &InitExecutor::initializeResult, this, &BitcoinApplication::initializeResult); + connect(&m_executor.value(), &InitExecutor::shutdownResult, this, &BitcoinApplication::shutdownResult); + connect(&m_executor.value(), &InitExecutor::runawayException, this, &BitcoinApplication::handleRunawayException); + connect(this, &BitcoinApplication::requestedInitialize, &m_executor.value(), &InitExecutor::initialize); + connect(this, &BitcoinApplication::requestedShutdown, &m_executor.value(), &InitExecutor::shutdown); } void BitcoinApplication::parameterSetup() diff --git a/src/qt/bitcoin.h b/src/qt/bitcoin.h index d977062d6b..d3ef809582 100644 --- a/src/qt/bitcoin.h +++ b/src/qt/bitcoin.h @@ -32,12 +32,12 @@ class WalletModel; /** Class encapsulating Bitcoin Core startup and shutdown. * Allows running startup and shutdown in a different thread from the UI thread. */ -class BitcoinCore: public QObject +class InitExecutor: public QObject { Q_OBJECT public: - explicit BitcoinCore(interfaces::Node& node); - ~BitcoinCore(); + explicit InitExecutor(interfaces::Node& node); + ~InitExecutor(); public Q_SLOTS: void initialize(); @@ -117,7 +117,7 @@ Q_SIGNALS: void windowShown(BitcoinGUI* window); private: - std::optional m_executor; + std::optional m_executor; OptionsModel *optionsModel; ClientModel *clientModel; BitcoinGUI *window; -- cgit v1.2.3 From c82165a55701fe4ff604d7f30163051cd47c2363 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Wed, 14 Jul 2021 12:03:25 +0300 Subject: qt, refactor: Move InitExecutor class into its own module This change makes InitExecutor class re-usable by an alternative GUI, e.g., QML-based one. --- src/qt/bitcoin.cpp | 78 ++++++++--------------------------------------- src/qt/bitcoin.h | 29 +----------------- src/qt/initexecutor.cpp | 68 +++++++++++++++++++++++++++++++++++++++++ src/qt/initexecutor.h | 46 ++++++++++++++++++++++++++++ src/qt/test/test_main.cpp | 1 + 5 files changed, 129 insertions(+), 93 deletions(-) create mode 100644 src/qt/initexecutor.cpp create mode 100644 src/qt/initexecutor.h (limited to 'src/qt') diff --git a/src/qt/bitcoin.cpp b/src/qt/bitcoin.cpp index dae56aa56c..4ab4e388d9 100644 --- a/src/qt/bitcoin.cpp +++ b/src/qt/bitcoin.cpp @@ -7,12 +7,19 @@ #endif #include -#include #include +#include +#include +#include +#include +#include +#include +#include #include #include #include +#include #include #include #include @@ -20,6 +27,11 @@ #include #include #include +#include +#include +#include +#include +#include #ifdef ENABLE_WALLET #include @@ -27,18 +39,6 @@ #include #endif // ENABLE_WALLET -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - #include #include @@ -155,58 +155,6 @@ void DebugMessageHandler(QtMsgType type, const QMessageLogContext& context, cons } } -InitExecutor::InitExecutor(interfaces::Node& node) : - QObject(), m_node(node) -{ - this->moveToThread(&m_thread); - m_thread.start(); -} - -InitExecutor::~InitExecutor() -{ - qDebug() << __func__ << ": Stopping thread"; - m_thread.quit(); - m_thread.wait(); - qDebug() << __func__ << ": Stopped thread"; -} - -void InitExecutor::handleRunawayException(const std::exception *e) -{ - PrintExceptionContinue(e, "Runaway exception"); - Q_EMIT runawayException(QString::fromStdString(m_node.getWarnings().translated)); -} - -void InitExecutor::initialize() -{ - try - { - util::ThreadRename("qt-init"); - qDebug() << __func__ << ": Running initialization in thread"; - interfaces::BlockAndHeaderTipInfo tip_info; - bool rv = m_node.appInitMain(&tip_info); - Q_EMIT initializeResult(rv, tip_info); - } catch (const std::exception& e) { - handleRunawayException(&e); - } catch (...) { - handleRunawayException(nullptr); - } -} - -void InitExecutor::shutdown() -{ - try - { - qDebug() << __func__ << ": Running Shutdown in thread"; - m_node.appShutdown(); - qDebug() << __func__ << ": Shutdown finished"; - Q_EMIT shutdownResult(); - } catch (const std::exception& e) { - handleRunawayException(&e); - } catch (...) { - handleRunawayException(nullptr); - } -} - static int qt_argc = 1; static const char* qt_argv = "bitcoin-qt"; diff --git a/src/qt/bitcoin.h b/src/qt/bitcoin.h index d3ef809582..ed2f26b7f3 100644 --- a/src/qt/bitcoin.h +++ b/src/qt/bitcoin.h @@ -10,13 +10,13 @@ #endif #include +#include #include #include #include #include -#include class BitcoinGUI; class ClientModel; @@ -29,33 +29,6 @@ class WalletController; class WalletModel; -/** Class encapsulating Bitcoin Core startup and shutdown. - * Allows running startup and shutdown in a different thread from the UI thread. - */ -class InitExecutor: public QObject -{ - Q_OBJECT -public: - explicit InitExecutor(interfaces::Node& node); - ~InitExecutor(); - -public Q_SLOTS: - void initialize(); - void shutdown(); - -Q_SIGNALS: - void initializeResult(bool success, interfaces::BlockAndHeaderTipInfo tip_info); - void shutdownResult(); - void runawayException(const QString &message); - -private: - /// Pass fatal exception message to UI thread - void handleRunawayException(const std::exception *e); - - interfaces::Node& m_node; - QThread m_thread; -}; - /** Main Bitcoin application object */ class BitcoinApplication: public QApplication { diff --git a/src/qt/initexecutor.cpp b/src/qt/initexecutor.cpp new file mode 100644 index 0000000000..697c8bd4da --- /dev/null +++ b/src/qt/initexecutor.cpp @@ -0,0 +1,68 @@ +// Copyright (c) 2014-2021 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include + +#include +#include +#include + +#include + +#include +#include +#include +#include + +InitExecutor::InitExecutor(interfaces::Node& node) : + QObject(), m_node(node) +{ + this->moveToThread(&m_thread); + m_thread.start(); +} + +InitExecutor::~InitExecutor() +{ + qDebug() << __func__ << ": Stopping thread"; + m_thread.quit(); + m_thread.wait(); + qDebug() << __func__ << ": Stopped thread"; +} + +void InitExecutor::handleRunawayException(const std::exception *e) +{ + PrintExceptionContinue(e, "Runaway exception"); + Q_EMIT runawayException(QString::fromStdString(m_node.getWarnings().translated)); +} + +void InitExecutor::initialize() +{ + try + { + util::ThreadRename("qt-init"); + qDebug() << __func__ << ": Running initialization in thread"; + interfaces::BlockAndHeaderTipInfo tip_info; + bool rv = m_node.appInitMain(&tip_info); + Q_EMIT initializeResult(rv, tip_info); + } catch (const std::exception& e) { + handleRunawayException(&e); + } catch (...) { + handleRunawayException(nullptr); + } +} + +void InitExecutor::shutdown() +{ + try + { + qDebug() << __func__ << ": Running Shutdown in thread"; + m_node.appShutdown(); + qDebug() << __func__ << ": Shutdown finished"; + Q_EMIT shutdownResult(); + } catch (const std::exception& e) { + handleRunawayException(&e); + } catch (...) { + handleRunawayException(nullptr); + } +} diff --git a/src/qt/initexecutor.h b/src/qt/initexecutor.h new file mode 100644 index 0000000000..6c014677ee --- /dev/null +++ b/src/qt/initexecutor.h @@ -0,0 +1,46 @@ +// Copyright (c) 2014-2021 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_QT_INITEXECUTOR_H +#define BITCOIN_QT_INITEXECUTOR_H + +#include + +#include + +#include +#include + +QT_BEGIN_NAMESPACE +class QString; +QT_END_NAMESPACE + +/** Class encapsulating Bitcoin Core startup and shutdown. + * Allows running startup and shutdown in a different thread from the UI thread. + */ +class InitExecutor: public QObject +{ + Q_OBJECT +public: + explicit InitExecutor(interfaces::Node& node); + ~InitExecutor(); + +public Q_SLOTS: + void initialize(); + void shutdown(); + +Q_SIGNALS: + void initializeResult(bool success, interfaces::BlockAndHeaderTipInfo tip_info); + void shutdownResult(); + void runawayException(const QString &message); + +private: + /// Pass fatal exception message to UI thread + void handleRunawayException(const std::exception *e); + + interfaces::Node& m_node; + QThread m_thread; +}; + +#endif // BITCOIN_QT_INITEXECUTOR_H diff --git a/src/qt/test/test_main.cpp b/src/qt/test/test_main.cpp index eb86f027ef..7d66f67f8a 100644 --- a/src/qt/test/test_main.cpp +++ b/src/qt/test/test_main.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include -- cgit v1.2.3 From 8169fc4e73a87331e02502fc24e293831765c8b1 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Wed, 14 Jul 2021 12:21:54 +0300 Subject: qt, refactor: Fix code styling of moved InitExecutor class --- src/qt/initexecutor.cpp | 12 +++++------- src/qt/initexecutor.h | 6 +++--- 2 files changed, 8 insertions(+), 10 deletions(-) (limited to 'src/qt') diff --git a/src/qt/initexecutor.cpp b/src/qt/initexecutor.cpp index 697c8bd4da..7060f74dab 100644 --- a/src/qt/initexecutor.cpp +++ b/src/qt/initexecutor.cpp @@ -15,8 +15,8 @@ #include #include -InitExecutor::InitExecutor(interfaces::Node& node) : - QObject(), m_node(node) +InitExecutor::InitExecutor(interfaces::Node& node) + : QObject(), m_node(node) { this->moveToThread(&m_thread); m_thread.start(); @@ -30,7 +30,7 @@ InitExecutor::~InitExecutor() qDebug() << __func__ << ": Stopped thread"; } -void InitExecutor::handleRunawayException(const std::exception *e) +void InitExecutor::handleRunawayException(const std::exception* e) { PrintExceptionContinue(e, "Runaway exception"); Q_EMIT runawayException(QString::fromStdString(m_node.getWarnings().translated)); @@ -38,8 +38,7 @@ void InitExecutor::handleRunawayException(const std::exception *e) void InitExecutor::initialize() { - try - { + try { util::ThreadRename("qt-init"); qDebug() << __func__ << ": Running initialization in thread"; interfaces::BlockAndHeaderTipInfo tip_info; @@ -54,8 +53,7 @@ void InitExecutor::initialize() void InitExecutor::shutdown() { - try - { + try { qDebug() << __func__ << ": Running Shutdown in thread"; m_node.appShutdown(); qDebug() << __func__ << ": Shutdown finished"; diff --git a/src/qt/initexecutor.h b/src/qt/initexecutor.h index 6c014677ee..319ce40465 100644 --- a/src/qt/initexecutor.h +++ b/src/qt/initexecutor.h @@ -19,7 +19,7 @@ QT_END_NAMESPACE /** Class encapsulating Bitcoin Core startup and shutdown. * Allows running startup and shutdown in a different thread from the UI thread. */ -class InitExecutor: public QObject +class InitExecutor : public QObject { Q_OBJECT public: @@ -33,11 +33,11 @@ public Q_SLOTS: Q_SIGNALS: void initializeResult(bool success, interfaces::BlockAndHeaderTipInfo tip_info); void shutdownResult(); - void runawayException(const QString &message); + void runawayException(const QString& message); private: /// Pass fatal exception message to UI thread - void handleRunawayException(const std::exception *e); + void handleRunawayException(const std::exception* e); interfaces::Node& m_node; QThread m_thread; -- cgit v1.2.3