Skip to content

Commit 1c52425

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 e150738 commit 1c52425

2 files changed

Lines changed: 14 additions & 21 deletions

File tree

src/llmq/signing_shares.cpp

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

16341634
void CSigSharesManager::HousekeepingThreadMain()
16351635
{
1636-
int64_t lastSendTime = 0;
1637-
16381636
while (!workInterrupt) {
16391637
RemoveBannedNodeStates();
1640-
1641-
if (TicksSinceEpoch<std::chrono::milliseconds>(SystemClock::now()) - lastSendTime > 100) {
1642-
SendMessages();
1643-
lastSendTime = TicksSinceEpoch<std::chrono::milliseconds>(SystemClock::now());
1644-
}
1645-
1638+
SendMessages();
16461639
Cleanup();
16471640

16481641
workInterrupt.sleep_for(std::chrono::milliseconds(100));
@@ -1665,18 +1658,18 @@ void CSigSharesManager::WorkDispatcherThreadMain()
16651658

16661659
void CSigSharesManager::DispatchPendingSigns()
16671660
{
1668-
// Pop and dispatch ALL pending signs until queue is empty
1669-
while (!workInterrupt) {
1670-
std::optional<PendingSignatureData> work;
1671-
{
1672-
LOCK(cs_pendingSigns);
1673-
if (pendingSigns.empty()) break;
1674-
// Move the data out of the vector
1675-
work.emplace(std::move(pendingSigns.back()));
1676-
pendingSigns.pop_back();
1677-
}
1661+
// Swap out entire vector to avoid lock thrashing
1662+
std::vector<PendingSignatureData> signs;
1663+
{
1664+
LOCK(cs_pendingSigns);
1665+
signs.swap(pendingSigns);
1666+
}
1667+
1668+
// Dispatch all signs to worker pool
1669+
for (auto& work : signs) {
1670+
if (workInterrupt) break;
16781671

1679-
workerPool.push([this, work = std::move(*work)](int) {
1672+
workerPool.push([this, work = std::move(work)](int) {
16801673
SignAndProcessSingleShare(std::move(work));
16811674
});
16821675
}

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)