Skip to content

Commit 0faafb5

Browse files
committed
index: decrease ThreadSync cs_main contention
Only NextSyncBlock requires cs_main lock. The other function calls like Commit or Rewind will lock or not cs_main internally when they need it. Avoiding keeping cs_main locked when Commit() or Rewind() write data to disk.
1 parent f1469eb commit 0faafb5

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

src/index/base.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -159,23 +159,20 @@ void BaseIndex::Sync()
159159
return;
160160
}
161161

162-
{
163-
LOCK(cs_main);
164-
const CBlockIndex* pindex_next = NextSyncBlock(pindex, m_chainstate->m_chain);
165-
if (!pindex_next) {
166-
SetBestBlockIndex(pindex);
167-
m_synced = true;
168-
// No need to handle errors in Commit. See rationale above.
169-
Commit();
170-
break;
171-
}
172-
if (pindex_next->pprev != pindex && !Rewind(pindex, pindex_next->pprev)) {
173-
FatalErrorf("%s: Failed to rewind index %s to a previous chain tip",
174-
__func__, GetName());
175-
return;
176-
}
177-
pindex = pindex_next;
162+
const CBlockIndex* pindex_next = WITH_LOCK(cs_main, return NextSyncBlock(pindex, m_chainstate->m_chain));
163+
if (!pindex_next) {
164+
SetBestBlockIndex(pindex);
165+
m_synced = true;
166+
// No need to handle errors in Commit. See rationale above.
167+
Commit();
168+
break;
178169
}
170+
if (pindex_next->pprev != pindex && !Rewind(pindex, pindex_next->pprev)) {
171+
FatalErrorf("%s: Failed to rewind index %s to a previous chain tip", __func__, GetName());
172+
return;
173+
}
174+
pindex = pindex_next;
175+
179176

180177
auto current_time{std::chrono::steady_clock::now()};
181178
if (last_log_time + SYNC_LOG_INTERVAL < current_time) {

0 commit comments

Comments
 (0)