================
@@ -63,6 +65,86 @@ void ModuleCacheEntries::flush() {
   }
 }
 
+void ModuleCacheEntries::startFlushing(AtomicLineLogger &Logger) {
+  assert(!Flusher.joinable() && "Already flushing in the background");
+  Flusher = std::thread([this, &Logger] { flushInBackground(Logger); });
+}
+
+void ModuleCacheEntries::stopFlushing() {
+  if (!Flusher.joinable())
+    return;
+  {
+    std::lock_guard<std::mutex> Lock(PendingMutex);
+    StopFlushing = true;
+  }
+  PendingCondVar.notify_one();
+  Flusher.join();
+}
+
+void ModuleCacheEntries::addPending(ModuleCacheMapEntry &MapEntry) {
+  // Note: The background thread only gets started or stopped while no PCMs
+  // are being written.
+  if (!Flusher.joinable())
+    return;
+  {
+    std::lock_guard<std::mutex> Lock(PendingMutex);
+    Pending.push_back(&MapEntry);
+  }
+  PendingCondVar.notify_one();
+}
+
+void ModuleCacheEntries::flushInBackground(AtomicLineLogger &Logger) {
+  // This is a compiler-internal output, let's bypass the sandbox.
+  auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
+  std::unique_lock<std::mutex> Lock(PendingMutex);
+  while (true) {
+    PendingCondVar.wait(Lock, [&] { return StopFlushing || !Pending.empty(); 
});
+    // Flush everything that's been written before stopping.
+    if (Pending.empty())
+      return;
+    std::vector<ModuleCacheMapEntry *> Batch;
+    std::swap(Batch, Pending);
+    Lock.unlock();
+    for (ModuleCacheMapEntry *MapEntry : Batch)
+      flushEntry(*MapEntry, Logger);
+    Lock.lock();
+  }
+}
+
+void ModuleCacheEntries::flushEntry(ModuleCacheMapEntry &MapEntry,
+                                    AtomicLineLogger &Logger) {
+  StringRef Path = MapEntry.getKey();
+  ModuleCacheEntry &Entry = *MapEntry.second;
+  std::shared_ptr<const llvm::MemoryBuffer> InMemory;
+  {
+    std::lock_guard<std::mutex> Lock(Entry.Mutex);
+    if (Entry.State != ModuleCacheEntry::S_Written)
+      return;
+    InMemory = Entry.Buffer;
+  }
+
+  // The contents of an entry in the S_Written state don't change, so we can
+  // write them without holding the lock. This way, importers of the freshly
+  // built PCM don't need to wait for the IO.
+  off_t Size;
+  time_t ModTime;
+  auto Written = writeImpl(Path, *InMemory, Size, ModTime, /*ReadBack=*/true);
----------------
jansvoboda11 wrote:

Yes, I also noticed that case is not handled. I'm not too concerned about 
producing vs not producing scan results in out-of-disk-space situations - such 
builds are doomed anyways. I do plan to fix that issue and propagate errors up 
to terminate cleanly, but I don't think that should block this change, which 
solves a real issue people are already observing. I'll attempt to put up a PR 
and cherry-pick that separate fix into a release branch if I can get a PR and 
an approval quick enough.

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

Reply via email to