LockTraitsBoost.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright 2016-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. /**
  17. * This file contains LockTraits specializations for boost mutex types.
  18. *
  19. * These need to be specialized simply due to the fact that the timed
  20. * methods take boost::chrono arguments instead of std::chrono.
  21. */
  22. #pragma once
  23. #include <boost/thread.hpp>
  24. #include <folly/LockTraits.h>
  25. #if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
  26. namespace folly {
  27. namespace detail {
  28. /// Convert a std::chrono::duration argument to boost::chrono::duration
  29. template <class Rep, std::intmax_t Num, std::intmax_t Denom>
  30. boost::chrono::duration<Rep, boost::ratio<Num, Denom>> toBoostDuration(
  31. const std::chrono::duration<Rep, std::ratio<Num, Denom>>& d) {
  32. return boost::chrono::duration<Rep, boost::ratio<Num, Denom>>(d.count());
  33. }
  34. } // namespace detail
  35. /**
  36. * LockTraits specialization for boost::shared_mutex
  37. */
  38. template <>
  39. struct LockTraits<boost::shared_mutex>
  40. : public LockTraitsBase<boost::shared_mutex> {
  41. static constexpr bool is_shared = true;
  42. static constexpr bool is_timed = true;
  43. template <class Rep, class Period>
  44. static bool try_lock_for(
  45. boost::shared_mutex& mutex,
  46. const std::chrono::duration<Rep, Period>& timeout) {
  47. return mutex.try_lock_for(detail::toBoostDuration(timeout));
  48. }
  49. template <class Rep, class Period>
  50. static bool try_lock_shared_for(
  51. boost::shared_mutex& mutex,
  52. const std::chrono::duration<Rep, Period>& timeout) {
  53. return mutex.try_lock_shared_for(detail::toBoostDuration(timeout));
  54. }
  55. };
  56. /**
  57. * LockTraits specialization for boost::timed_mutex
  58. */
  59. template <>
  60. struct LockTraits<boost::timed_mutex>
  61. : public LockTraitsBase<boost::timed_mutex> {
  62. static constexpr bool is_shared = false;
  63. static constexpr bool is_timed = true;
  64. template <class Rep, class Period>
  65. static bool try_lock_for(
  66. boost::timed_mutex& mutex,
  67. const std::chrono::duration<Rep, Period>& timeout) {
  68. return mutex.try_lock_for(detail::toBoostDuration(timeout));
  69. }
  70. };
  71. /**
  72. * LockTraits specialization for boost::recursive_timed_mutex
  73. */
  74. template <>
  75. struct LockTraits<boost::recursive_timed_mutex>
  76. : public LockTraitsBase<boost::recursive_timed_mutex> {
  77. static constexpr bool is_shared = false;
  78. static constexpr bool is_timed = true;
  79. template <class Rep, class Period>
  80. static bool try_lock_for(
  81. boost::recursive_timed_mutex& mutex,
  82. const std::chrono::duration<Rep, Period>& timeout) {
  83. return mutex.try_lock_for(detail::toBoostDuration(timeout));
  84. }
  85. };
  86. } // namespace folly
  87. #endif // FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES