================
@@ -184,6 +184,101 @@ DependencyScanningFilesystemSharedCache::CacheShard::
   return *CachedEntry;
 }
 
+DependencyScanningFilesystemSharedCache::SlotAcquisitionResult
+DependencyScanningFilesystemSharedCache::CacheShard::acquireFilenameSlot(
+    StringRef Filename) {
+  assert(llvm::sys::path::is_absolute_gnu(Filename));
+  std::unique_lock<std::mutex> LockGuard(CacheLock);
+  // Cache hit.
+  if (auto It = CacheByFilename.find(Filename); It != CacheByFilename.end()) {
+    if (const auto *Entry = It->getValue().first)
+      return SlotAcquisitionResult{Entry, nullptr};
+  }
+
+  // Another worker is producing for this filename, wait for it.
+  if (auto It = InProgressByFilename.find(Filename);
+      It != InProgressByFilename.end()) {
+    std::shared_ptr<InProgressEntry> Pending = It->second;
+    Pending->CondVar.wait(LockGuard, [&] { return Pending->Done; });
+    assert(Pending->Result &&
+           "in-progress filename slot fulfilled without an entry");
+    return SlotAcquisitionResult{Pending->Result, nullptr};
+  }
+
+  // Install an in-progress entry and return it.
+  auto Pending = std::make_shared<InProgressEntry>();
+  InProgressByFilename.try_emplace(Filename, Pending);
+  return SlotAcquisitionResult{nullptr, std::move(Pending)};
+}
+
+DependencyScanningFilesystemSharedCache::SlotAcquisitionResult
+DependencyScanningFilesystemSharedCache::CacheShard::acquireUIDSlot(
+    llvm::sys::fs::UniqueID UID) {
+  std::unique_lock<std::mutex> LockGuard(CacheLock);
+  // Cache hit.
+  if (auto It = EntriesByUID.find(UID); It != EntriesByUID.end())
+    return SlotAcquisitionResult{It->getSecond(), nullptr};
+
+  // Another worker is producing for this UID, wait for it.
+  if (auto It = InProgressByUID.find(UID); It != InProgressByUID.end()) {
+    std::shared_ptr<InProgressEntry> Pending = It->second;
+    Pending->CondVar.wait(LockGuard, [&] { return Pending->Done; });
+    assert(Pending->Result &&
+           "in-progress UID slot fulfilled without an entry");
+    return SlotAcquisitionResult{Pending->Result, nullptr};
+  }
+
+  // Install an in-progress entry and return it.
+  auto Pending = std::make_shared<InProgressEntry>();
+  InProgressByUID.try_emplace(UID, Pending);
+  return SlotAcquisitionResult{nullptr, std::move(Pending)};
+}
+
+void DependencyScanningFilesystemSharedCache::CacheShard::fulfilFilenameSlot(
+    StringRef Filename,
+    const std::shared_ptr<
+        DependencyScanningFilesystemSharedCache::InProgressEntry> &IPE,
+    const CachedFileSystemEntry *Result) {
+  {
+    std::lock_guard<std::mutex> LockGuard(CacheLock);
+    if (Result) {
+      // Publish the entry under this filename for future direct lookups,
+      // mirroring the semantics of getOrInsertEntryForFilename.
+      auto [It, Inserted] =
+          CacheByFilename.insert({Filename, {Result, nullptr}});
+      auto &[CachedEntry, CachedRealPath] = It->getValue();
+      if (!Inserted || !CachedEntry)
----------------
jansvoboda11 wrote:

You check `Result` is not `nullptr`, how could `CachedEntry` be `nullptr` if 
`Inserted` is true?

https://github.com/llvm/llvm-project/pull/199680
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to