[PATCH] D58246: [lld] Fix elf::unlinkAsync detached thread

Vitaly Buka via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 14 12:56:14 PST 2019


vitalybuka updated this revision to Diff 186901.
vitalybuka added a comment.

moved comment


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D58246/new/

https://reviews.llvm.org/D58246

Files:
  lld/ELF/Filesystem.cpp


Index: lld/ELF/Filesystem.cpp
===================================================================
--- lld/ELF/Filesystem.cpp
+++ lld/ELF/Filesystem.cpp
@@ -58,9 +58,28 @@
   std::error_code EC = sys::fs::openFileForRead(Path, FD);
   sys::fs::remove(Path);
 
+  if (EC)
+    return;
+
   // close and therefore remove TempPath in background.
-  if (!EC)
-    std::thread([=] { ::close(FD); }).detach();
+  std::mutex M;
+  std::condition_variable CV;
+  bool Started = false;
+  std::thread([&, FD] {
+    {
+      std::lock_guard<std::mutex> L(M);
+      Started = true;
+      CV.notify_all();
+    }
+    ::close(FD);
+  }).detach();
+  std::unique_lock<std::mutex> L(M);
+  // Wait the thread to start. std::thread will touch function local variables
+  // installing atexing handlers. We want to make it happen before libc started
+  // to call handlers.
+  // Additionally GLIBC before 2.27 had race between __internal_atexit and
+  // __run_exit_handlers.
+  CV.wait(L, [&] { return Started; });
 #endif
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D58246.186901.patch
Type: text/x-patch
Size: 1016 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190214/6805b4fb/attachment.bin>


More information about the llvm-commits mailing list