Fix GlogFormatterTest on Windows
[folly.git] / folly / LockTraitsBoost.h
1 /*
2  * Copyright 2017 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 /**
18  * This file contains LockTraits specializations for boost mutex types.
19  *
20  * These need to be specialized simply due to the fact that the timed
21  * methods take boost::chrono arguments instead of std::chrono.
22  */
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
31 namespace folly {
32
33 namespace detail {
34 /// Convert a std::chrono::duration argument to boost::chrono::duration
35 template <class Rep, std::intmax_t Num, std::intmax_t Denom>
36 boost::chrono::duration<Rep, boost::ratio<Num, Denom>> toBoostDuration(
37     const std::chrono::duration<Rep, std::ratio<Num, Denom>>& d) {
38   return boost::chrono::duration<Rep, boost::ratio<Num, Denom>>(d.count());
39 }
40 }
41
42 /**
43  * LockTraits specialization for boost::shared_mutex
44  */
45 template <>
46 struct LockTraits<boost::shared_mutex>
47     : public LockTraitsBase<boost::shared_mutex> {
48   static constexpr bool is_shared = true;
49   static constexpr bool is_timed = true;
50
51   template <class Rep, class Period>
52   static bool try_lock_for(
53       boost::shared_mutex& mutex,
54       const std::chrono::duration<Rep, Period>& timeout) {
55     return mutex.try_lock_for(detail::toBoostDuration(timeout));
56   }
57
58   template <class Rep, class Period>
59   static bool try_lock_shared_for(
60       boost::shared_mutex& mutex,
61       const std::chrono::duration<Rep, Period>& timeout) {
62     return mutex.try_lock_shared_for(detail::toBoostDuration(timeout));
63   }
64 };
65
66 /**
67  * LockTraits specialization for boost::timed_mutex
68  */
69 template <>
70 struct LockTraits<boost::timed_mutex>
71     : public LockTraitsBase<boost::timed_mutex> {
72   static constexpr bool is_shared = false;
73   static constexpr bool is_timed = true;
74
75   template <class Rep, class Period>
76   static bool try_lock_for(
77       boost::timed_mutex& mutex,
78       const std::chrono::duration<Rep, Period>& timeout) {
79     return mutex.try_lock_for(detail::toBoostDuration(timeout));
80   }
81 };
82
83 /**
84  * LockTraits specialization for boost::recursive_timed_mutex
85  */
86 template <>
87 struct LockTraits<boost::recursive_timed_mutex>
88     : public LockTraitsBase<boost::recursive_timed_mutex> {
89   static constexpr bool is_shared = false;
90   static constexpr bool is_timed = true;
91
92   template <class Rep, class Period>
93   static bool try_lock_for(
94       boost::recursive_timed_mutex& mutex,
95       const std::chrono::duration<Rep, Period>& timeout) {
96     return mutex.try_lock_for(detail::toBoostDuration(timeout));
97   }
98 };
99 } // folly
100
101 #endif // FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES