From: Yedidya Feldblum Date: Tue, 2 Jan 2018 01:05:02 +0000 (-0800) Subject: Use member-invoke traits in LockTraits X-Git-Tag: v2018.01.08.00~20 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=43523e0ce6bfe87a6576fb1bb50337afe0114eb5;p=folly.git Use member-invoke traits in LockTraits Summary: [Folly] Use member-invoke traits in `LockTraits`. V.s. spelling out the SFINAE manually. Reviewed By: Orvid Differential Revision: D6637380 fbshipit-source-id: 212e0cc5e54503b31e458b6bda93b2a3b24e6c59 --- diff --git a/folly/LockTraits.h b/folly/LockTraits.h index a6cf12b5..2f051f5c 100644 --- a/folly/LockTraits.h +++ b/folly/LockTraits.h @@ -27,6 +27,8 @@ #include #include +#include + // Android, OSX, and Cygwin don't have timed mutexes #if defined(ANDROID) || defined(__ANDROID__) || defined(__APPLE__) || \ defined(__CYGWIN__) @@ -38,6 +40,13 @@ namespace folly { namespace detail { +namespace member { +FOLLY_CREATE_MEMBER_INVOKE_TRAITS(lock, lock); +FOLLY_CREATE_MEMBER_INVOKE_TRAITS(try_lock_for, try_lock_for); +FOLLY_CREATE_MEMBER_INVOKE_TRAITS(lock_shared, lock_shared); +FOLLY_CREATE_MEMBER_INVOKE_TRAITS(lock_upgrade, lock_upgrade); +} // namespace member + /** * An enum to describe the "level" of a mutex. The supported levels are * Unique - a normal mutex that supports only exclusive locking @@ -71,46 +80,25 @@ struct MutexLevelValueImpl { * mutex. This is used in conjunction with the above MutexLevel * specializations and the LockTraitsImpl to determine what functions are * supported by objects of type Mutex - * - * The implementation uses SINAE in the return value with trailing return - * types to figure out what level a mutex is */ template class LockInterfaceDispatcher { private: // assert that the mutex type has basic lock and unlock functions static_assert( - std::is_same().lock()), void>::value, + member::lock::is_invocable::value, "The mutex type must support lock and unlock functions"); - // Helper functions for implementing the traits using SFINAE - template - static auto timed_lock_test(T*) -> typename std::is_same< - decltype(std::declval().try_lock_for(std::chrono::milliseconds(0))), - bool>::type; - template - static std::false_type timed_lock_test(...); - - template - static auto lock_shared_test(T*) -> typename std:: - is_same().lock_shared()), void>::type; - template - static std::false_type lock_shared_test(...); - - template - static auto lock_upgrade_test(T*) -> typename std:: - is_same().lock_upgrade()), void>::type; - template - static std::false_type lock_upgrade_test(...); + using duration = std::chrono::milliseconds; public: static constexpr bool has_lock_unique = true; static constexpr bool has_lock_timed = - decltype(timed_lock_test(nullptr))::value; + member::try_lock_for::is_invocable::value; static constexpr bool has_lock_shared = - decltype(lock_shared_test(nullptr))::value; + member::lock_shared::is_invocable::value; static constexpr bool has_lock_upgrade = - decltype(lock_upgrade_test(nullptr))::value; + member::lock_upgrade::is_invocable::value; }; /**