From 3faf49bd41a3302dc5c477510233ba5c60db771d Mon Sep 17 00:00:00 2001 From: Denis Yaroshevskiy Date: Thu, 9 Nov 2023 05:46:59 -0800 Subject: [PATCH] ] feedback on static analysis marks Summary: Conditionally mark lock types for static analysis Reviewed By: Gownta Differential Revision: D51114189 fbshipit-source-id: 9747b06761c46da51402ce3743e5458814e6d61a --- folly/lang/Hint.h | 23 +++++++++++++++++++++++ folly/lang/test/HintTest.cpp | 11 +++++++++++ folly/synchronization/Lock.h | 14 +++----------- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/folly/lang/Hint.h b/folly/lang/Hint.h index 28e5a0a257b..400d0cee1c3 100644 --- a/folly/lang/Hint.h +++ b/folly/lang/Hint.h @@ -213,6 +213,29 @@ struct unsafe_for_async_usage { // a convenience wrapper for the marker below: }; static_assert(detail::is_unsafe_for_async_usage_v, ""); +namespace detail { + +template +using detect_folly_is_coro_aware_mutex = typename T::folly_coro_aware_mutex; + +} // namespace detail + +// Inheriting or having a member `unsafe_for_async_usage_if` will conditionally +// tag the type. +template +struct unsafe_for_async_usage_if {}; + +template <> +struct unsafe_for_async_usage_if { + using folly_is_unsafe_for_async_usage = std::true_type; +}; + +// Detects the presense of folly_coro_aware_mutex nested typedef. +// This helps custom lock guards have the same behavior as std::lock_guard. +template +constexpr bool is_coro_aware_mutex_v = + is_detected_v; + } // namespace folly #include diff --git a/folly/lang/test/HintTest.cpp b/folly/lang/test/HintTest.cpp index 38f4c1147d3..1dcadc1cc7b 100644 --- a/folly/lang/test/HintTest.cpp +++ b/folly/lang/test/HintTest.cpp @@ -41,6 +41,17 @@ static_assert(detail::is_unsafe_for_async_usage_v, ""); static_assert(!detail::is_unsafe_for_async_usage_v, ""); static_assert(!detail::is_unsafe_for_async_usage_v, ""); +// is_coro_aware_mutex ---------------------------- + +struct CoroAwareMutexYes { + using folly_coro_aware_mutex = std::true_type; +}; + +struct CoroAwareMutexNo {}; + +static_assert(is_coro_aware_mutex_v, ""); +static_assert(!is_coro_aware_mutex_v, ""); + //----------------- TEST(Hint, CompilerMayUnsafelyAssume) { diff --git a/folly/synchronization/Lock.h b/folly/synchronization/Lock.h index 2bd013a7395..05fc2e2ba38 100644 --- a/folly/synchronization/Lock.h +++ b/folly/synchronization/Lock.h @@ -26,6 +26,7 @@ #include #include #include +#include namespace folly { @@ -284,7 +285,8 @@ class lock_base // }; template -class lock_guard_base { +class lock_guard_base + : unsafe_for_async_usage_if> { private: using lock_type_ = lock_base; using lock_state_type_ = typename lock_type_::state_type; @@ -489,8 +491,6 @@ using std::shared_lock; template class upgrade_lock : public upgrade_lock_base { public: - using folly_is_unsafe_for_async_usage = std::true_type; - using upgrade_lock_base::upgrade_lock_base; }; @@ -503,8 +503,6 @@ class upgrade_lock : public upgrade_lock_base { template class hybrid_lock : public hybrid_lock_base { public: - using folly_is_unsafe_for_async_usage = std::true_type; - using hybrid_lock_base::hybrid_lock_base; }; @@ -525,8 +523,6 @@ template class unique_lock_guard_base : public detail::lock_guard_base { private: - using folly_is_unsafe_for_async_usage = std::true_type; - using base = detail::lock_guard_base; public: @@ -548,8 +544,6 @@ template class shared_lock_guard : public detail::lock_guard_base { private: - using folly_is_unsafe_for_async_usage = std::true_type; - using base = detail::lock_guard_base; public: @@ -564,8 +558,6 @@ template class hybrid_lock_guard : public detail::lock_guard_base> { private: - using folly_is_unsafe_for_async_usage = std::true_type; - using base = detail::lock_guard_base>;