Skip to content

Commit bedfc26

Browse files
UdjinM6codablock
authored andcommitted
Rework handling of CSigSharesManager worker thread (#2703)
1 parent 3e4286a commit bedfc26

File tree

6 files changed

+35
-19
lines changed

6 files changed

+35
-19
lines changed

src/init.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ void Interrupt(boost::thread_group& threadGroup)
211211
InterruptRPC();
212212
InterruptREST();
213213
InterruptTorControl();
214+
llmq::InterruptLLMQSystem();
214215
if (g_connman)
215216
g_connman->Interrupt();
216217
threadGroup.interrupt_all();

src/llmq/quorums_init.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,17 @@ void InitLLMQSystem(CEvoDB& evoDb, CScheduler* scheduler, bool unitTests)
2929
quorumSigSharesManager = new CSigSharesManager();
3030
quorumSigningManager = new CSigningManager(unitTests);
3131
chainLocksHandler = new CChainLocksHandler(scheduler);
32-
33-
quorumSigSharesManager->StartWorkerThread();
3432
}
3533

36-
void DestroyLLMQSystem()
34+
void InterruptLLMQSystem()
3735
{
3836
if (quorumSigSharesManager) {
39-
quorumSigSharesManager->StopWorkerThread();
37+
quorumSigSharesManager->InterruptWorkerThread();
4038
}
39+
}
4140

41+
void DestroyLLMQSystem()
42+
{
4243
delete chainLocksHandler;
4344
chainLocksHandler = nullptr;
4445
delete quorumSigningManager;

src/llmq/quorums_init.h

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace llmq
1515
static const bool DEFAULT_WATCH_QUORUMS = false;
1616

1717
void InitLLMQSystem(CEvoDB& evoDb, CScheduler* scheduler, bool unitTests);
18+
void InterruptLLMQSystem();
1819
void DestroyLLMQSystem();
1920

2021
}

src/llmq/quorums_signing_shares.cpp

+22-12
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ CSigSharesInv CBatchedSigShares::ToInv() const
176176

177177
CSigSharesManager::CSigSharesManager()
178178
{
179+
StartWorkerThread();
179180
}
180181

181182
CSigSharesManager::~CSigSharesManager()
@@ -185,24 +186,23 @@ CSigSharesManager::~CSigSharesManager()
185186

186187
void CSigSharesManager::StartWorkerThread()
187188
{
188-
workThread = std::thread([this]() {
189-
RenameThread("quorum-sigshares");
190-
WorkThreadMain();
191-
});
189+
workThread = std::thread(&TraceThread<std::function<void()> >,
190+
"sigshares",
191+
std::function<void()>(std::bind(&CSigSharesManager::WorkThreadMain, this)));
192192
}
193193

194194
void CSigSharesManager::StopWorkerThread()
195195
{
196-
if (stopWorkThread) {
197-
return;
198-
}
199-
200-
stopWorkThread = true;
201196
if (workThread.joinable()) {
202197
workThread.join();
203198
}
204199
}
205200

201+
void CSigSharesManager::InterruptWorkerThread()
202+
{
203+
workInterrupt();
204+
}
205+
206206
void CSigSharesManager::ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman)
207207
{
208208
// non-masternodes are not interested in sigshares
@@ -1096,8 +1096,16 @@ void CSigSharesManager::BanNode(NodeId nodeId)
10961096

10971097
void CSigSharesManager::WorkThreadMain()
10981098
{
1099-
int64_t lastProcessTime = GetTimeMillis();
1100-
while (!stopWorkThread && !ShutdownRequested()) {
1099+
workInterrupt.reset();
1100+
1101+
while (!workInterrupt) {
1102+
if (!quorumSigningManager || !g_connman || !quorumSigningManager) {
1103+
if (!workInterrupt.sleep_for(std::chrono::milliseconds(100))) {
1104+
return;
1105+
}
1106+
continue;
1107+
}
1108+
11011109
RemoveBannedNodeStates();
11021110
quorumSigningManager->ProcessPendingRecoveredSigs(*g_connman);
11031111
ProcessPendingSigShares(*g_connman);
@@ -1107,7 +1115,9 @@ void CSigSharesManager::WorkThreadMain()
11071115
quorumSigningManager->Cleanup();
11081116

11091117
// TODO Wakeup when pending signing is needed?
1110-
MilliSleep(100);
1118+
if (!workInterrupt.sleep_for(std::chrono::milliseconds(100))) {
1119+
return;
1120+
}
11111121
}
11121122
}
11131123

src/llmq/quorums_signing_shares.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class CSigSharesManager
194194
CCriticalSection cs;
195195

196196
std::thread workThread;
197-
std::atomic<bool> stopWorkThread{false};
197+
CThreadInterrupt workInterrupt;
198198

199199
std::map<SigShareKey, CSigShare> sigShares;
200200
std::map<uint256, int64_t> firstSeenForSessions;
@@ -214,8 +214,7 @@ class CSigSharesManager
214214
CSigSharesManager();
215215
~CSigSharesManager();
216216

217-
void StartWorkerThread();
218-
void StopWorkerThread();
217+
void InterruptWorkerThread();
219218

220219
public:
221220
void ProcessMessage(CNode* pnode, const std::string& strCommand, CDataStream& vRecv, CConnman& connman);
@@ -224,6 +223,9 @@ class CSigSharesManager
224223
void Sign(const CQuorumCPtr& quorum, const uint256& id, const uint256& msgHash);
225224

226225
private:
226+
void StartWorkerThread();
227+
void StopWorkerThread();
228+
227229
void ProcessMessageSigSharesInv(CNode* pfrom, const CSigSharesInv& inv, CConnman& connman);
228230
void ProcessMessageGetSigShares(CNode* pfrom, const CSigSharesInv& inv, CConnman& connman);
229231
void ProcessMessageBatchedSigShares(CNode* pfrom, const CBatchedSigShares& batchedSigShares, CConnman& connman);

src/test/test_dash.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
9797
TestingSetup::~TestingSetup()
9898
{
9999
UnregisterNodeSignals(GetNodeSignals());
100+
llmq::InterruptLLMQSystem();
100101
threadGroup.interrupt_all();
101102
threadGroup.join_all();
102103
UnloadBlockIndex();

0 commit comments

Comments
 (0)