[lld] r241481 - COFF: Make ArchiveFile::getMember lock-free.

Rui Ueyama ruiu at google.com
Mon Jul 6 11:22:16 PDT 2015


Author: ruiu
Date: Mon Jul  6 13:22:16 2015
New Revision: 241481

URL: http://llvm.org/viewvc/llvm-project?rev=241481&view=rev
Log:
COFF: Make ArchiveFile::getMember lock-free.

The previous code was not even safe with MSVC 2013 because the compiler
doesn't guarantee that static variables (in this case, a mutex) are
initialized in a thread-safe manner.

Modified:
    lld/trunk/COFF/InputFiles.cpp
    lld/trunk/COFF/InputFiles.h

Modified: lld/trunk/COFF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/InputFiles.cpp?rev=241481&r1=241480&r2=241481&view=diff
==============================================================================
--- lld/trunk/COFF/InputFiles.cpp (original)
+++ lld/trunk/COFF/InputFiles.cpp Mon Jul  6 13:22:16 2015
@@ -73,13 +73,18 @@ std::error_code ArchiveFile::parse() {
     if (B->getName() != "__NULL_IMPORT_DESCRIPTOR")
       LazySymbols.push_back(B);
   }
+
+  // Seen is a map from member files to boolean values. Initially
+  // all members are mapped to false, which indicates all these files
+  // are not read yet.
+  for (const Archive::Child &Child : File->children())
+    Seen[Child.getBuffer().data()].clear();
   return std::error_code();
 }
 
 // Returns a buffer pointing to a member file containing a given symbol.
 // This function is thread-safe.
 ErrorOr<MemoryBufferRef> ArchiveFile::getMember(const Archive::Symbol *Sym) {
-  static std::mutex Mu;
   auto ItOrErr = Sym->getMember();
   if (auto EC = ItOrErr.getError())
     return EC;
@@ -87,10 +92,7 @@ ErrorOr<MemoryBufferRef> ArchiveFile::ge
 
   // Return an empty buffer if we have already returned the same buffer.
   const char *StartAddr = It->getBuffer().data();
-  Mu.lock();
-  auto Pair = Seen.insert(StartAddr);
-  Mu.unlock();
-  if (!Pair.second)
+  if (Seen[StartAddr].test_and_set())
     return MemoryBufferRef();
   return It->getMemoryBufferRef();
 }

Modified: lld/trunk/COFF/InputFiles.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/InputFiles.h?rev=241481&r1=241480&r2=241481&view=diff
==============================================================================
--- lld/trunk/COFF/InputFiles.h (original)
+++ lld/trunk/COFF/InputFiles.h Mon Jul  6 13:22:16 2015
@@ -102,7 +102,7 @@ private:
   std::unique_ptr<Archive> File;
   std::string Filename;
   std::vector<Lazy *> LazySymbols;
-  std::set<const char *> Seen;
+  std::map<const char *, std::atomic_flag> Seen;
   llvm::MallocAllocator Alloc;
 };
 





More information about the llvm-commits mailing list