Skip to content

Commit 228e568

Browse files
refactor: streamline CSigSharesManager's message handling and improve thread safety
- Removed unnecessary lastSendTime variable and simplified message sending in HousekeepingThreadMain. - Enhanced DispatchPendingSigns by swapping the entire vector of pending signs to reduce lock contention and improve performance. - Updated ActiveContext to start and stop the share manager more efficiently.
1 parent f892f79 commit 228e568

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

src/llmq/signing_shares.cpp

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,16 +1613,9 @@ void CSigSharesManager::BanNode(NodeId nodeId)
16131613

16141614
void CSigSharesManager::HousekeepingThreadMain()
16151615
{
1616-
int64_t lastSendTime = 0;
1617-
16181616
while (!workInterrupt) {
16191617
RemoveBannedNodeStates();
1620-
1621-
if (TicksSinceEpoch<std::chrono::milliseconds>(SystemClock::now()) - lastSendTime > 100) {
1622-
SendMessages();
1623-
lastSendTime = TicksSinceEpoch<std::chrono::milliseconds>(SystemClock::now());
1624-
}
1625-
1618+
SendMessages();
16261619
Cleanup();
16271620

16281621
workInterrupt.sleep_for(std::chrono::milliseconds(100));
@@ -1645,18 +1638,18 @@ void CSigSharesManager::WorkDispatcherThreadMain()
16451638

16461639
void CSigSharesManager::DispatchPendingSigns()
16471640
{
1648-
// Pop and dispatch ALL pending signs until queue is empty
1649-
while (!workInterrupt) {
1650-
std::optional<PendingSignatureData> work;
1651-
{
1652-
LOCK(cs_pendingSigns);
1653-
if (pendingSigns.empty()) break;
1654-
// Move the data out of the vector
1655-
work.emplace(std::move(pendingSigns.back()));
1656-
pendingSigns.pop_back();
1657-
}
1641+
// Swap out entire vector to avoid lock thrashing
1642+
std::vector<PendingSignatureData> signs;
1643+
{
1644+
LOCK(cs_pendingSigns);
1645+
signs.swap(pendingSigns);
1646+
}
1647+
1648+
// Dispatch all signs to worker pool
1649+
for (auto& work : signs) {
1650+
if (workInterrupt) break;
16581651

1659-
workerPool.push([this, work = std::move(*work)](int) {
1652+
workerPool.push([this, work = std::move(work)](int) {
16601653
SignAndProcessSingleShare(std::move(work));
16611654
});
16621655
}

src/masternode/active/context.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ void ActiveContext::Start(CConnman& connman, PeerManager& peerman)
5555
{
5656
m_llmq_ctx.qdkgsman->StartThreads(connman, peerman);
5757
shareman->RegisterAsRecoveredSigsListener();
58-
shareman->StartWorkerThread();
58+
shareman->Start();
5959
}
6060

6161
void ActiveContext::Stop()
6262
{
63-
shareman->StopWorkerThread();
63+
shareman->Stop();
6464
shareman->UnregisterAsRecoveredSigsListener();
6565
m_llmq_ctx.qdkgsman->StopThreads();
6666
}

0 commit comments

Comments
 (0)