diff options
author | Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> | 2021-07-14 11:48:21 +0300 |
---|---|---|
committer | Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> | 2021-07-14 21:53:09 +0300 |
commit | 19a1d008310f250b69b7aa764a9f26384d5a4a85 (patch) | |
tree | ae096361d7ea2a4b2eb50d0b1484c4c4ab5225ac /src/qt | |
parent | 531c2b7c04898f5a2097f44e8c12bfb2f53aaf9b (diff) | |
download | bitcoin-19a1d008310f250b69b7aa764a9f26384d5a4a85.tar.xz |
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.
Diffstat (limited to 'src/qt')
-rw-r--r-- | src/qt/bitcoin.cpp | 41 | ||||
-rw-r--r-- | src/qt/bitcoin.h | 11 |
2 files changed, 26 insertions, 26 deletions
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<char **>(&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 <config/bitcoin-config.h> #endif -#include <QApplication> +#include <interfaces/node.h> + #include <assert.h> #include <memory> +#include <optional> -#include <interfaces/node.h> +#include <QApplication> +#include <QThread> 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<BitcoinCore> m_executor; OptionsModel *optionsModel; ClientModel *clientModel; BitcoinGUI *window; |