SharedMutex.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /*
  2. * Copyright 2015-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/SharedMutex.h>
  17. namespace folly {
  18. // Explicitly instantiate SharedMutex here:
  19. template class SharedMutexImpl<true>;
  20. template class SharedMutexImpl<false>;
  21. namespace detail {
  22. std::unique_lock<std::mutex> sharedMutexAnnotationGuard(void* ptr) {
  23. if (folly::kIsSanitizeThread) {
  24. // On TSAN builds, we have an array of mutexes and index into them based on
  25. // the address. If the array is of prime size things will work out okay
  26. // without a complicated hash function.
  27. static constexpr std::size_t kNumAnnotationMutexes = 251;
  28. static std::array<std::mutex, kNumAnnotationMutexes> kAnnotationMutexes{};
  29. auto index = reinterpret_cast<uintptr_t>(ptr) % kNumAnnotationMutexes;
  30. return std::unique_lock<std::mutex>(kAnnotationMutexes[index]);
  31. } else {
  32. return std::unique_lock<std::mutex>();
  33. }
  34. }
  35. } // namespace detail
  36. } // namespace folly