proxygen
LockTraitsBoost.h
Go to the documentation of this file.
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 
23 #pragma once
24 
25 #include <boost/thread.hpp>
26 #include <folly/LockTraits.h>
27 
28 #if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
29 
30 namespace folly {
31 
32 namespace detail {
34 template <class Rep, std::intmax_t Num, std::intmax_t Denom>
35 boost::chrono::duration<Rep, boost::ratio<Num, Denom>> toBoostDuration(
36  const std::chrono::duration<Rep, std::ratio<Num, Denom>>& d) {
37  return boost::chrono::duration<Rep, boost::ratio<Num, Denom>>(d.count());
38 }
39 } // namespace detail
40 
44 template <>
45 struct LockTraits<boost::shared_mutex>
46  : public LockTraitsBase<boost::shared_mutex> {
47  static constexpr bool is_shared = true;
48  static constexpr bool is_timed = true;
49 
50  template <class Rep, class Period>
51  static bool try_lock_for(
52  boost::shared_mutex& mutex,
53  const std::chrono::duration<Rep, Period>& timeout) {
54  return mutex.try_lock_for(detail::toBoostDuration(timeout));
55  }
56 
57  template <class Rep, class Period>
58  static bool try_lock_shared_for(
59  boost::shared_mutex& mutex,
60  const std::chrono::duration<Rep, Period>& timeout) {
61  return mutex.try_lock_shared_for(detail::toBoostDuration(timeout));
62  }
63 };
64 
68 template <>
69 struct LockTraits<boost::timed_mutex>
70  : public LockTraitsBase<boost::timed_mutex> {
71  static constexpr bool is_shared = false;
72  static constexpr bool is_timed = true;
73 
74  template <class Rep, class Period>
75  static bool try_lock_for(
76  boost::timed_mutex& mutex,
77  const std::chrono::duration<Rep, Period>& timeout) {
78  return mutex.try_lock_for(detail::toBoostDuration(timeout));
79  }
80 };
81 
85 template <>
86 struct LockTraits<boost::recursive_timed_mutex>
87  : public LockTraitsBase<boost::recursive_timed_mutex> {
88  static constexpr bool is_shared = false;
89  static constexpr bool is_timed = true;
90 
91  template <class Rep, class Period>
92  static bool try_lock_for(
93  boost::recursive_timed_mutex& mutex,
94  const std::chrono::duration<Rep, Period>& timeout) {
95  return mutex.try_lock_for(detail::toBoostDuration(timeout));
96  }
97 };
98 } // namespace folly
99 
100 #endif // FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
std::mutex mutex