From 804743d804519c53215f77cd402b8f0cbc869c40 Mon Sep 17 00:00:00 2001 From: Adam Simpkins Date: Fri, 8 Jul 2016 11:42:35 -0700 Subject: [PATCH] update LockTraitsBoost to handle arbitrary duration types Summary: Update the boost LockTraits classes to accept arbitrary std::chrono::duration types, rather than just std::chrono::milliseconds. Reviewed By: yfeldblum Differential Revision: D3533556 fbshipit-source-id: e764cdf4dafa0b11cd7558c2d5bfb6f80dce88cf --- folly/LockTraitsBoost.h | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/folly/LockTraitsBoost.h b/folly/LockTraitsBoost.h index dd9a17b6..914efd58 100644 --- a/folly/LockTraitsBoost.h +++ b/folly/LockTraitsBoost.h @@ -30,6 +30,15 @@ namespace folly { +namespace detail { +/// Convert a std::chrono::duration argument to boost::chrono::duration +template +boost::chrono::duration> toBoostDuration( + const std::chrono::duration>& d) { + return boost::chrono::duration>(d.count()); +} +} + /** * LockTraits specialization for boost::shared_mutex */ @@ -39,19 +48,18 @@ struct LockTraits static constexpr bool is_shared = true; static constexpr bool is_timed = true; + template static bool try_lock_for( boost::shared_mutex& mutex, - std::chrono::milliseconds timeout) { - // Convert the std::chrono argument to boost::chrono - return mutex.try_lock_for(boost::chrono::milliseconds(timeout.count())); + const std::chrono::duration& timeout) { + return mutex.try_lock_for(detail::toBoostDuration(timeout)); } + template static bool try_lock_shared_for( boost::shared_mutex& mutex, - std::chrono::milliseconds timeout) { - // Convert the std::chrono argument to boost::chrono - return mutex.try_lock_shared_for( - boost::chrono::milliseconds(timeout.count())); + const std::chrono::duration& timeout) { + return mutex.try_lock_shared_for(detail::toBoostDuration(timeout)); } }; @@ -64,11 +72,11 @@ struct LockTraits static constexpr bool is_shared = false; static constexpr bool is_timed = true; + template static bool try_lock_for( boost::timed_mutex& mutex, - std::chrono::milliseconds timeout) { - // Convert the std::chrono argument to boost::chrono - return mutex.try_lock_for(boost::chrono::milliseconds(timeout.count())); + const std::chrono::duration& timeout) { + return mutex.try_lock_for(detail::toBoostDuration(timeout)); } }; @@ -81,11 +89,11 @@ struct LockTraits static constexpr bool is_shared = false; static constexpr bool is_timed = true; + template static bool try_lock_for( boost::recursive_timed_mutex& mutex, - std::chrono::milliseconds timeout) { - // Convert the std::chrono argument to boost::chrono - return mutex.try_lock_for(boost::chrono::milliseconds(timeout.count())); + const std::chrono::duration& timeout) { + return mutex.try_lock_for(detail::toBoostDuration(timeout)); } }; } // folly -- 2.34.1