Skip to content

Commit

Permalink
] feedback on static analysis marks
Browse files Browse the repository at this point in the history
Summary: Conditionally mark lock types for static analysis

Reviewed By: Gownta

Differential Revision: D51114189

fbshipit-source-id: 9747b06761c46da51402ce3743e5458814e6d61a
  • Loading branch information
DenisYaroshevskiy authored and facebook-github-bot committed Nov 9, 2023
1 parent b17b880 commit 3faf49b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
23 changes: 23 additions & 0 deletions folly/lang/Hint.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsafe_for_async_usage>, "");

namespace detail {

template <typename T>
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 <bool If>
struct unsafe_for_async_usage_if {};

template <>
struct unsafe_for_async_usage_if<true> {
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 <typename T>
constexpr bool is_coro_aware_mutex_v =
is_detected_v<detail::detect_folly_is_coro_aware_mutex, T>;

} // namespace folly

#include <folly/lang/Hint-inl.h>
11 changes: 11 additions & 0 deletions folly/lang/test/HintTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ static_assert(detail::is_unsafe_for_async_usage_v<UnsafeForAsyncUsageYes>, "");
static_assert(!detail::is_unsafe_for_async_usage_v<UnsafeForAsyncUsageNo0>, "");
static_assert(!detail::is_unsafe_for_async_usage_v<UnsafeForAsyncUsageNo1>, "");

// is_coro_aware_mutex ----------------------------

struct CoroAwareMutexYes {
using folly_coro_aware_mutex = std::true_type;
};

struct CoroAwareMutexNo {};

static_assert(is_coro_aware_mutex_v<CoroAwareMutexYes>, "");
static_assert(!is_coro_aware_mutex_v<CoroAwareMutexNo>, "");

//-----------------

TEST(Hint, CompilerMayUnsafelyAssume) {
Expand Down
14 changes: 3 additions & 11 deletions folly/synchronization/Lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <folly/Traits.h>
#include <folly/functional/Invoke.h>
#include <folly/lang/Exception.h>
#include <folly/lang/Hint.h>

namespace folly {

Expand Down Expand Up @@ -284,7 +285,8 @@ class lock_base //
};

template <typename Mutex, typename Policy>
class lock_guard_base {
class lock_guard_base
: unsafe_for_async_usage_if<!is_coro_aware_mutex_v<Mutex>> {
private:
using lock_type_ = lock_base<Mutex, Policy>;
using lock_state_type_ = typename lock_type_::state_type;
Expand Down Expand Up @@ -489,8 +491,6 @@ using std::shared_lock;
template <typename Mutex>
class upgrade_lock : public upgrade_lock_base<Mutex> {
public:
using folly_is_unsafe_for_async_usage = std::true_type;

using upgrade_lock_base<Mutex>::upgrade_lock_base;
};

Expand All @@ -503,8 +503,6 @@ class upgrade_lock : public upgrade_lock_base<Mutex> {
template <typename Mutex>
class hybrid_lock : public hybrid_lock_base<Mutex> {
public:
using folly_is_unsafe_for_async_usage = std::true_type;

using hybrid_lock_base<Mutex>::hybrid_lock_base;
};

Expand All @@ -525,8 +523,6 @@ template <typename Mutex>
class unique_lock_guard_base
: public detail::lock_guard_base<Mutex, detail::lock_policy_unique> {
private:
using folly_is_unsafe_for_async_usage = std::true_type;

using base = detail::lock_guard_base<Mutex, detail::lock_policy_unique>;

public:
Expand All @@ -548,8 +544,6 @@ template <typename Mutex>
class shared_lock_guard
: public detail::lock_guard_base<Mutex, detail::lock_policy_shared> {
private:
using folly_is_unsafe_for_async_usage = std::true_type;

using base = detail::lock_guard_base<Mutex, detail::lock_policy_shared>;

public:
Expand All @@ -564,8 +558,6 @@ template <typename Mutex>
class hybrid_lock_guard
: public detail::lock_guard_base<Mutex, detail::lock_policy_hybrid<Mutex>> {
private:
using folly_is_unsafe_for_async_usage = std::true_type;

using base =
detail::lock_guard_base<Mutex, detail::lock_policy_hybrid<Mutex>>;

Expand Down

0 comments on commit 3faf49b

Please sign in to comment.