Skip to content

Commit f23c5f3

Browse files
committed
Fix concurrency related issue
1 parent bdd925d commit f23c5f3

2 files changed

Lines changed: 28 additions & 14 deletions

File tree

interpreter/llvm-project/llvm/include/llvm/ExecutionEngine/Orc/Shared/LibraryResolver.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,22 @@ class LibraryManager {
212212
return FilteredView(Libraries.begin(), Libraries.end(), S, K);
213213
}
214214

215+
using LibraryFilterFn = std::function<bool(const LibraryInfo &)>;
216+
void getLibraries(
217+
LibState S, PathType K,
218+
std::vector<std::shared_ptr<LibraryInfo>> &Outs,
219+
LibraryFilterFn Filter = nullptr) const {
220+
std::shared_lock<std::shared_mutex> Lock(Mtx);
221+
for (const auto &[_, Entry] : Libraries) {
222+
const auto &Info = *Entry;
223+
if (Info.getKind() != K || Info.getState() != S)
224+
continue;
225+
if (Filter && !Filter(Info))
226+
continue;
227+
Outs.push_back(Entry);
228+
}
229+
}
230+
215231
void forEachLibrary(const LibraryVisitor &visitor) const {
216232
std::unique_lock<std::shared_mutex> Lock(Mtx);
217233
for (const auto &[_, entry] : Libraries) {
@@ -221,14 +237,14 @@ class LibraryManager {
221237
}
222238

223239
bool isLoaded(StringRef Path) const {
224-
std::unique_lock<std::shared_mutex> Lock(Mtx);
240+
std::shared_lock<std::shared_mutex> Lock(Mtx);
225241
if (auto It = Libraries.find(Path.str()); It != Libraries.end())
226242
return It->second->getState() == LibState::Loaded;
227243
return false;
228244
}
229245

230246
bool isQueried(StringRef Path) const {
231-
std::unique_lock<std::shared_mutex> Lock(Mtx);
247+
std::shared_lock<std::shared_mutex> Lock(Mtx);
232248
if (auto It = Libraries.find(Path.str()); It != Libraries.end())
233249
return It->second->getState() == LibState::Queried;
234250
return false;

interpreter/llvm-project/llvm/lib/ExecutionEngine/Orc/Shared/LibraryResolver.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,17 @@ class SymbolSearchContext {
185185
public:
186186
SymbolSearchContext(SymbolQuery &Q) : Q(Q) {}
187187

188-
bool hasSearched(LibraryInfo *Lib) const { return Searched.count(Lib); }
188+
bool hasSearched(const LibraryInfo *Lib) const { return Searched.count(Lib); }
189189

190-
void markSearched(LibraryInfo *Lib) { Searched.insert(Lib); }
190+
void markSearched(const LibraryInfo *Lib) { Searched.insert(Lib); }
191191

192192
inline bool allResolved() const { return Q.allResolved(); }
193193

194194
SymbolQuery &query() { return Q; }
195195

196196
private:
197197
SymbolQuery &Q;
198-
DenseSet<LibraryInfo *> Searched;
198+
DenseSet<const LibraryInfo *> Searched;
199199
};
200200

201201
void LibraryResolver::resolveSymbolsInLibrary(
@@ -288,24 +288,22 @@ void LibraryResolver::searchSymbolsInLibraries(
288288

289289
SymbolSearchContext Ctx(Q);
290290
while (!Ctx.allResolved()) {
291+
std::vector<std::shared_ptr<LibraryInfo>> Libs;
292+
LibMgr.getLibraries(S, K, Libs, [&](const LibraryInfo &Lib) {
293+
return !Ctx.hasSearched(&Lib);
294+
});
291295

292-
for (auto &Lib : LibMgr.getView(S, K)) {
293-
if (Ctx.hasSearched(Lib.get()))
294-
continue;
296+
if (Libs.empty() && !scanLibrariesIfNeeded(K, scanBatchSize))
297+
break; // no more new libs to scan
295298

299+
for (auto &Lib : Libs) {
296300
// can use Async here?
297301
resolveSymbolsInLibrary(*Lib, Ctx.query(), Config.Options);
298302
Ctx.markSearched(Lib.get());
299303

300304
if (Ctx.allResolved())
301305
return;
302306
}
303-
304-
if (Ctx.allResolved())
305-
return;
306-
307-
if (!scanLibrariesIfNeeded(K, scanBatchSize))
308-
break; // no more new libs to scan
309307
}
310308
};
311309

0 commit comments

Comments
 (0)