Fix TLRefCount::useGlobal to work with TSAN
authorAndrii Grynenko <andrii@fb.com>
Tue, 11 Apr 2017 06:30:46 +0000 (23:30 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 11 Apr 2017 06:35:17 +0000 (23:35 -0700)
Summary: TSAN has a limitation for the number of locks held concurrently - https://github.com/llvm-mirror/compiler-rt/blob/master/lib/sanitizer_common/sanitizer_deadlock_detector.h#L126, so we have to work around that.

Reviewed By: yfeldblum

Differential Revision: D4866477

fbshipit-source-id: 958251e97d91c1c46ef4e907de2cf189fb04f88e

folly/experimental/TLRefCount.h

index a9da2d8..9e2a1da 100644 (file)
@@ -87,6 +87,17 @@ class TLRefCount {
 
   template <typename Container>
   static void useGlobal(const Container& refCountPtrs) {
+#ifdef FOLLY_SANITIZE_THREAD
+    // TSAN has a limitation for the number of locks held concurrently, so it's
+    // safer to call useGlobal() serially.
+    if (refCountPtrs.size() > 1) {
+      for (auto refCountPtr : refCountPtrs) {
+        refCountPtr->useGlobal();
+      }
+      return;
+    }
+#endif
+
     std::vector<std::unique_lock<std::mutex>> lgs_;
     for (auto refCountPtr : refCountPtrs) {
       lgs_.emplace_back(refCountPtr->globalMutex_);