27 #include "clang/Basic/SourceLocation.h"
28 #include "clang/Basic/SourceManager.h"
29 #include "llvm/ADT/Hashing.h"
30 #include "llvm/ADT/STLExtras.h"
31 #include "llvm/ADT/ScopeExit.h"
32 #include "llvm/ADT/StringMap.h"
33 #include "llvm/ADT/StringRef.h"
34 #include "llvm/ADT/StringSet.h"
35 #include "llvm/Support/Error.h"
36 #include "llvm/Support/Threading.h"
40 #include <condition_variable>
52 bool BackgroundIndexRebuilder::enoughTUsToRebuild()
const {
60 maybeRebuild(
"after indexing enough files", [
this] {
64 if (ActiveVersion != StartedVersion)
66 return enoughTUsToRebuild();
71 maybeRebuild(
"when background indexer is idle", [
this] {
74 return IndexedTUs > IndexedTUsAtLastRebuild;
79 std::lock_guard<std::mutex> Lock(Mu);
85 std::lock_guard<std::mutex> Lock(Mu);
87 LoadedShards += ShardCount;
90 maybeRebuild(
"after loading index from disk", [
this] {
96 return LoadedShards > 0 || enoughTUsToRebuild();
101 std::lock_guard<std::mutex> Lock(Mu);
105 void BackgroundIndexRebuilder::maybeRebuild(
const char *Reason,
106 std::function<
bool()> Check) {
107 unsigned BuildVersion = 0;
109 std::lock_guard<std::mutex> Lock(Mu);
110 if (!ShouldStop && Check()) {
111 BuildVersion = ++StartedVersion;
112 IndexedTUsAtLastRebuild = IndexedTUs;
116 std::unique_ptr<SymbolIndex> NewIndex;
118 vlog(
"BackgroundIndex: building version {0} {1}", BuildVersion, Reason);
119 trace::Span
Tracer(
"RebuildBackgroundIndex");
124 std::lock_guard<std::mutex> Lock(Mu);
126 if (BuildVersion > ActiveVersion) {
127 ActiveVersion = BuildVersion;
128 vlog(
"BackgroundIndex: serving version {0} ({1} bytes)", BuildVersion,
129 NewIndex->estimateMemoryUsage());
130 Target->
reset(std::move(NewIndex));