Skip to content

Commit 70d510d

Browse files
author
Jim Posen
committed
[index] Allow TxIndex sync thread to be interrupted.
1 parent 94b4f8b commit 70d510d

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

src/index/txindex.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ TxIndex::TxIndex(std::unique_ptr<TxIndexDB> db) :
3030
m_db(std::move(db)), m_synced(false), m_best_block_index(nullptr)
3131
{}
3232

33+
TxIndex::~TxIndex()
34+
{
35+
Interrupt();
36+
Stop();
37+
}
38+
3339
bool TxIndex::Init()
3440
{
3541
LOCK(cs_main);
@@ -76,6 +82,11 @@ void TxIndex::ThreadSync()
7682
int64_t last_log_time = 0;
7783
int64_t last_locator_write_time = 0;
7884
while (true) {
85+
if (m_interrupt) {
86+
WriteBestBlock(pindex);
87+
return;
88+
}
89+
7990
{
8091
LOCK(cs_main);
8192
const CBlockIndex* pindex_next = NextSyncBlock(pindex);
@@ -222,6 +233,11 @@ bool TxIndex::FindTx(const uint256& txid, CDiskTxPos& pos) const
222233
return m_db->ReadTxPos(txid, pos);
223234
}
224235

236+
void TxIndex::Interrupt()
237+
{
238+
m_interrupt();
239+
}
240+
225241
void TxIndex::Start()
226242
{
227243
// Need to register this ValidationInterface before running Init(), so that

src/index/txindex.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define BITCOIN_INDEX_TXINDEX_H
77

88
#include <primitives/block.h>
9+
#include <threadinterrupt.h>
910
#include <txdb.h>
1011
#include <uint256.h>
1112
#include <validationinterface.h>
@@ -31,14 +32,16 @@ class TxIndex final : public CValidationInterface
3132
std::atomic<const CBlockIndex*> m_best_block_index;
3233

3334
std::thread m_thread_sync;
35+
CThreadInterrupt m_interrupt;
3436

3537
/// Initialize internal state from the database and block index.
3638
bool Init();
3739

3840
/// Sync the tx index with the block index starting from the current best
39-
/// block. Intended to be run in its own thread, m_thread_sync. Once the
40-
/// txindex gets in sync, the m_synced flag is set and the BlockConnected
41-
/// ValidationInterface callback takes over and the sync thread exits.
41+
/// block. Intended to be run in its own thread, m_thread_sync, and can be
42+
/// interrupted with m_interrupt. Once the txindex gets in sync, the
43+
/// m_synced flag is set and the BlockConnected ValidationInterface callback
44+
/// takes over and the sync thread exits.
4245
void ThreadSync();
4346

4447
/// Write update index entries for a newly connected block.
@@ -57,9 +60,14 @@ class TxIndex final : public CValidationInterface
5760
/// Constructs the TxIndex, which becomes available to be queried.
5861
explicit TxIndex(std::unique_ptr<TxIndexDB> db);
5962

63+
/// Destructor interrupts sync thread if running and blocks until it exits.
64+
~TxIndex();
65+
6066
/// Look up the on-disk location of a transaction by hash.
6167
bool FindTx(const uint256& txid, CDiskTxPos& pos) const;
6268

69+
void Interrupt();
70+
6371
/// Start initializes the sync state and registers the instance as a
6472
/// ValidationInterface so that it stays in sync with blockchain updates.
6573
void Start();

src/threadinterrupt.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#include <threadinterrupt.h>
77

8+
CThreadInterrupt::CThreadInterrupt() : flag(false) {}
9+
810
CThreadInterrupt::operator bool() const
911
{
1012
return flag.load(std::memory_order_acquire);

src/threadinterrupt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
class CThreadInterrupt
1919
{
2020
public:
21+
CThreadInterrupt();
2122
explicit operator bool() const;
2223
void operator()();
2324
void reset();

0 commit comments

Comments
 (0)