Skip to content

Commit 4e53f36

Browse files
yfeldblumfacebook-github-bot
authored andcommitted
another adopt-lock tag for state
Summary: This allows folly to add only CTAD rules that name folly types explicitly. The use of `std::adopt_lock` with state will continue to work but will not participate in CTAD. The use of `folly::adopt_lock_state` will work with CTAD. Reviewed By: Gownta, skrueger Differential Revision: D70046346 fbshipit-source-id: 024cffd891e0df9c65021e4b8b7940549a11125d
1 parent 35ed51c commit 4e53f36

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

folly/synchronization/Lock.h

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ FOLLY_CREATE_MEMBER_INVOKER_SUITE(unlock_upgrade_and_lock_shared);
6666

6767
} // namespace access
6868

69+
struct adopt_lock_state_t {};
70+
inline constexpr adopt_lock_state_t adopt_lock_state{};
71+
6972
namespace detail {
7073

7174
// A lock base class with a mostly-complete implementation suitable for either
@@ -119,6 +122,10 @@ class lock_base {
119122
: mutex_{std::addressof(mutex)}, state_{state} {
120123
state_ || (check_fail_<true>(), 0);
121124
}
125+
template <typename M = mutex_type, if_<has_state_, M>* = nullptr>
126+
FOLLY_NODISCARD lock_base(
127+
type_t<M>& mutex, adopt_lock_state_t, owner_type const& state)
128+
: lock_base{mutex, std::adopt_lock, state} {}
122129
FOLLY_NODISCARD explicit lock_base(mutex_type& mutex)
123130
: mutex_{std::addressof(mutex)} {
124131
lock();
@@ -264,7 +271,10 @@ class lock_guard_base
264271
lock_guard_base(
265272
mutex_type& mutex, std::adopt_lock_t, state_type_ const& state)
266273
: lock_{mutex, std::adopt_lock, state} {}
267-
274+
template <bool C = has_state_, if_<C> = 0>
275+
lock_guard_base(
276+
mutex_type& mutex, adopt_lock_state_t, state_type_ const& state)
277+
: lock_{mutex, std::adopt_lock, state} {}
268278
void operator=(lock_guard_base const&) = delete;
269279
void operator=(lock_guard_base&&) = delete;
270280

@@ -521,24 +531,21 @@ class hybrid_lock_guard
521531
using base::base;
522532
};
523533

524-
template <typename Mutex, typename... A>
525-
explicit hybrid_lock_guard(Mutex&, A const&...) -> hybrid_lock_guard<Mutex>;
534+
template <typename Mutex, typename S>
535+
hybrid_lock_guard(Mutex&, adopt_lock_state_t, S) -> hybrid_lock_guard<Mutex>;
526536

527537
} // namespace folly
528538

529539
FOLLY_NAMESPACE_STD_BEGIN
530540

531-
template <typename Mutex, typename LockFn = ::folly::access::lock_fn>
532-
unique_lock(Mutex&, adopt_lock_t, invoke_result_t<LockFn, Mutex&> const&)
533-
-> unique_lock<Mutex>;
541+
template <typename Mutex, typename S>
542+
unique_lock(Mutex&, folly::adopt_lock_state_t, S) -> unique_lock<Mutex>;
534543

535-
template <typename Mutex, typename LockFn = ::folly::access::lock_shared_fn>
536-
shared_lock(Mutex&, adopt_lock_t, invoke_result_t<LockFn, Mutex&> const&)
537-
-> shared_lock<Mutex>;
544+
template <typename Mutex, typename S>
545+
shared_lock(Mutex&, folly::adopt_lock_state_t, S) -> shared_lock<Mutex>;
538546

539-
template <typename Mutex, typename LockFn = ::folly::access::lock_upgrade_fn>
540-
lock_guard(Mutex&, adopt_lock_t, invoke_result_t<LockFn, Mutex&> const&)
541-
-> lock_guard<Mutex>;
547+
template <typename Mutex, typename S>
548+
lock_guard(Mutex&, folly::adopt_lock_state_t, S) -> lock_guard<Mutex>;
542549

543550
FOLLY_NAMESPACE_STD_END
544551

@@ -597,7 +604,7 @@ auto transition_lock_0_(From& lock, Transition transition, A const&... a) {
597604
if constexpr (std::is_void_v<ToState>) {
598605
return !s ? To{} : To{mutex, std::adopt_lock};
599606
} else {
600-
return !s ? To{} : To{mutex, std::adopt_lock, s};
607+
return !s ? To{} : To{mutex, folly::adopt_lock_state, s};
601608
}
602609
}
603610
template <

folly/synchronization/test/LockTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ struct LockTest : testing::TestWithParam<Param> {
424424
std::unique_lock<mutex_type>,
425425
deduction_unique<
426426
mutex_type&,
427-
std::adopt_lock_t,
427+
folly::adopt_lock_state_t,
428428
unique_lock_state>>();
429429
}
430430

@@ -437,7 +437,7 @@ struct LockTest : testing::TestWithParam<Param> {
437437
std::shared_lock<mutex_type>,
438438
deduction_shared<
439439
mutex_type&,
440-
std::adopt_lock_t,
440+
folly::adopt_lock_state_t,
441441
shared_lock_state>>();
442442
}
443443

@@ -450,7 +450,7 @@ struct LockTest : testing::TestWithParam<Param> {
450450
folly::upgrade_lock<mutex_type>,
451451
deduction_upgrade<
452452
mutex_type&,
453-
std::adopt_lock_t,
453+
folly::adopt_lock_state_t,
454454
upgrade_lock_state>>();
455455
}
456456
}

0 commit comments

Comments
 (0)