2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * This file contains LockTraits specializations for boost mutex types.
20 * These need to be specialized simply due to the fact that the timed
21 * methods take boost::chrono arguments instead of std::chrono.
25 #include <boost/thread.hpp>
26 #include <folly/LockTraits.h>
28 #if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
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());
43 * LockTraits specialization for boost::shared_mutex
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;
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));
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));
67 * LockTraits specialization for boost::timed_mutex
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;
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));
84 * LockTraits specialization for boost::recursive_timed_mutex
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;
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));
101 #endif // FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES