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 | /** |
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 | namespace folly { |
31 | |
32 | namespace detail { |
33 | /// Convert a std::chrono::duration argument to boost::chrono::duration |
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 | |
41 | /** |
42 | * LockTraits specialization for boost::shared_mutex |
43 | */ |
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 | |
65 | /** |
66 | * LockTraits specialization for boost::timed_mutex |
67 | */ |
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 | |
82 | /** |
83 | * LockTraits specialization for boost::recursive_timed_mutex |
84 | */ |
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 |
101 | |