<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Sun, Jul 5, 2015 at 3:52 PM Rui Ueyama <<a href="mailto:ruiu@google.com">ruiu@google.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: ruiu<br>
Date: Sun Jul  5 17:50:00 2015<br>
New Revision: 241420<br>
<br>
URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject-3Frev-3D241420-26view-3Drev&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=x9VkhSvfwCDVcJSVNOrX5uHyPFslPSWk1WiE0E3vFcY&s=857w5XtsQSgYoB0XiMgCJPsbTEmaS_EsXerFK9DKHnc&e=" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=241420&view=rev</a><br>
Log:<br>
COFF: Make ArchiveFile::getMember thread-safe.<br>
<br>
This function is called SymbolTable::readObjects, so in order to<br>
parallelize that function, we have to make this function thread-safe.<br></blockquote><div><br></div><div>I think it would be better to make an entire interface thread-safe, and document its thread safety guarantees than to do this one function at a time.</div><div><br></div><div>Also, have you thought about a testing strategy for the concurrency functionality you're adding? I'm wondering if this would be a place where unittesting would be effective. There you could spawn a collection of threads with a lambda to query the thread safe API and ensure they all produce the same result. When run with ThreadSanitizer, this would provide a pretty useful way to ensure the APIs that are documented as thread safe are in fact safe.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Modified:<br>
    lld/trunk/COFF/InputFiles.cpp<br>
<br>
Modified: lld/trunk/COFF/InputFiles.cpp<br>
URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_lld_trunk_COFF_InputFiles.cpp-3Frev-3D241420-26r1-3D241419-26r2-3D241420-26view-3Ddiff&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=x9VkhSvfwCDVcJSVNOrX5uHyPFslPSWk1WiE0E3vFcY&s=pwthDRa8B3aVwGpMbEkjQvK6MhvfeLQWed_FjcvCCIk&e=" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/InputFiles.cpp?rev=241420&r1=241419&r2=241420&view=diff</a><br>
==============================================================================<br>
--- lld/trunk/COFF/InputFiles.cpp (original)<br>
+++ lld/trunk/COFF/InputFiles.cpp Sun Jul  5 17:50:00 2015<br>
@@ -18,6 +18,7 @@<br>
 #include "llvm/Support/Debug.h"<br>
 #include "llvm/Support/Endian.h"<br>
 #include "llvm/Support/raw_ostream.h"<br>
+#include <mutex><br>
<br>
 using namespace llvm::object;<br>
 using namespace llvm::support::endian;<br>
@@ -76,7 +77,9 @@ std::error_code ArchiveFile::parse() {<br>
 }<br>
<br>
 // Returns a buffer pointing to a member file containing a given symbol.<br>
+// This function is thread-safe.<br>
 ErrorOr<MemoryBufferRef> ArchiveFile::getMember(const Archive::Symbol *Sym) {<br>
+  static std::mutex Mu;<br></blockquote><div><br></div><div>A function-local static mutex is more expensive than necessary.</div><div><br></div><div>1) It is a global lock, which seems like it should be avoided.</div><div><br></div><div>2) It requires a thread-safe initialization check on each call through the function in addition to the mutex lock.</div><div><br></div><div>It would seem more natural for the ArchiveFile object to carry the mutex used to establish thread safety for its member functions.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
   auto ItOrErr = Sym->getMember();<br>
   if (auto EC = ItOrErr.getError())<br>
     return EC;<br>
@@ -84,7 +87,9 @@ ErrorOr<MemoryBufferRef> ArchiveFile::ge<br>
<br>
   // Return an empty buffer if we have already returned the same buffer.<br>
   const char *StartAddr = It->getBuffer().data();<br>
+  Mu.lock();<br>
   auto Pair = Seen.insert(StartAddr);<br>
+  Mu.unlock();<br>
   if (!Pair.second)<br>
     return MemoryBufferRef();<br>
   return It->getMemoryBufferRef();<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div></div>