Skip to content

Commit f000cdc

Browse files
committed
Merge bitcoin-core#434: Keep InitExecutor in main gui thread
03a5fe0 qt: Keep InitExecutor in main gui thread (João Barbosa) Pull request description: The `InitExecutor` constructor moves the instance to a dedicated thread. This PR changes that by using `GUIUtil::ObjectInvoke` to run the relevant code in that thread. A possible follow-up is to ditch the dedicated thread and use `QThreadPool` or even `QtConcurrent::run` (if we want to enable that). ACKs for top commit: hebasto: ACK 03a5fe0, tested on Linux Mint 20.2 (Qt 5.12.8). jarolrod: ACK 03a5fe0 Tree-SHA512: 8b40300371d4c04efb9913600a06ba4899af0b5f50fdb26ea23ec751df6d3bd52f8bd693a5e8f6a94ebf3790583dc96c6070e6878d247dece62347aa9bd99031
2 parents efa227f + 03a5fe0 commit f000cdc

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

src/qt/initexecutor.cpp

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <qt/initexecutor.h>
66

77
#include <interfaces/node.h>
8+
#include <qt/guiutil.h>
89
#include <util/system.h>
910
#include <util/threadnames.h>
1011

@@ -18,7 +19,7 @@
1819
InitExecutor::InitExecutor(interfaces::Node& node)
1920
: QObject(), m_node(node)
2021
{
21-
this->moveToThread(&m_thread);
22+
m_context.moveToThread(&m_thread);
2223
m_thread.start();
2324
}
2425

@@ -38,29 +39,33 @@ void InitExecutor::handleRunawayException(const std::exception* e)
3839

3940
void InitExecutor::initialize()
4041
{
41-
try {
42-
util::ThreadRename("qt-init");
43-
qDebug() << __func__ << ": Running initialization in thread";
44-
interfaces::BlockAndHeaderTipInfo tip_info;
45-
bool rv = m_node.appInitMain(&tip_info);
46-
Q_EMIT initializeResult(rv, tip_info);
47-
} catch (const std::exception& e) {
48-
handleRunawayException(&e);
49-
} catch (...) {
50-
handleRunawayException(nullptr);
51-
}
42+
GUIUtil::ObjectInvoke(&m_context, [this] {
43+
try {
44+
util::ThreadRename("qt-init");
45+
qDebug() << "Running initialization in thread";
46+
interfaces::BlockAndHeaderTipInfo tip_info;
47+
bool rv = m_node.appInitMain(&tip_info);
48+
Q_EMIT initializeResult(rv, tip_info);
49+
} catch (const std::exception& e) {
50+
handleRunawayException(&e);
51+
} catch (...) {
52+
handleRunawayException(nullptr);
53+
}
54+
});
5255
}
5356

5457
void InitExecutor::shutdown()
5558
{
56-
try {
57-
qDebug() << __func__ << ": Running Shutdown in thread";
58-
m_node.appShutdown();
59-
qDebug() << __func__ << ": Shutdown finished";
60-
Q_EMIT shutdownResult();
61-
} catch (const std::exception& e) {
62-
handleRunawayException(&e);
63-
} catch (...) {
64-
handleRunawayException(nullptr);
65-
}
59+
GUIUtil::ObjectInvoke(&m_context, [this] {
60+
try {
61+
qDebug() << "Running Shutdown in thread";
62+
m_node.appShutdown();
63+
qDebug() << "Shutdown finished";
64+
Q_EMIT shutdownResult();
65+
} catch (const std::exception& e) {
66+
handleRunawayException(&e);
67+
} catch (...) {
68+
handleRunawayException(nullptr);
69+
}
70+
});
6671
}

src/qt/initexecutor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public Q_SLOTS:
4040
void handleRunawayException(const std::exception* e);
4141

4242
interfaces::Node& m_node;
43+
QObject m_context;
4344
QThread m_thread;
4445
};
4546

src/qt/test/test_main.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <interfaces/init.h>
1010
#include <interfaces/node.h>
1111
#include <qt/bitcoin.h>
12-
#include <qt/initexecutor.h>
1312
#include <qt/test/apptests.h>
1413
#include <qt/test/rpcnestedtests.h>
1514
#include <qt/test/uritests.h>

0 commit comments

Comments
 (0)