Skip to content

Commit 1b96fa1

Browse files
derekmaurocopybara-github
authored andcommitted
Switch to referenceful lock holder for Abseil compatibility
PiperOrigin-RevId: 831156684 Change-Id: I8a8b017ec2fc318a65f57e04428c030c707ee682
1 parent 085af2c commit 1b96fa1

File tree

7 files changed

+48
-48
lines changed

7 files changed

+48
-48
lines changed

googlemock/include/gmock/gmock-spec-builders.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,7 +1467,7 @@ class FunctionMocker<R(Args...)> final : public UntypedFunctionMockerBase {
14671467
// function have been satisfied. If not, it will report Google Test
14681468
// non-fatal failures for the violations.
14691469
~FunctionMocker() override GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
1470-
MutexLock l(&g_gmock_mutex);
1470+
MutexLock l(g_gmock_mutex);
14711471
VerifyAndClearExpectationsLocked();
14721472
Mock::UnregisterLocked(this);
14731473
ClearDefaultActionsLocked();
@@ -1646,7 +1646,7 @@ class FunctionMocker<R(Args...)> final : public UntypedFunctionMockerBase {
16461646
GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
16471647
const ArgumentTuple& args =
16481648
*static_cast<const ArgumentTuple*>(untyped_args);
1649-
MutexLock l(&g_gmock_mutex);
1649+
MutexLock l(g_gmock_mutex);
16501650
TypedExpectation<F>* exp = this->FindMatchingExpectationLocked(args);
16511651
if (exp == nullptr) { // A match wasn't found.
16521652
this->FormatUnexpectedCallMessageLocked(args, what, why);

googlemock/src/gmock-internal-utils.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ GTEST_API_ void Log(LogSeverity severity, const std::string& message,
156156
if (!LogIsVisible(severity)) return;
157157

158158
// Ensures that logs from different threads don't interleave.
159-
MutexLock l(&g_log_mutex);
159+
MutexLock l(g_log_mutex);
160160

161161
if (severity == kWarning) {
162162
// Prints a GMOCK WARNING marker to make the warnings easily searchable.

googlemock/src/gmock-spec-builders.cc

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ void ExpectationBase::CheckActionCountIfNotDone() const
212212
GTEST_LOCK_EXCLUDED_(mutex_) {
213213
bool should_check = false;
214214
{
215-
MutexLock l(&mutex_);
215+
MutexLock l(mutex_);
216216
if (!action_count_checked_) {
217217
action_count_checked_ = true;
218218
should_check = true;
@@ -318,7 +318,7 @@ UntypedFunctionMockerBase::~UntypedFunctionMockerBase() = default;
318318
void UntypedFunctionMockerBase::RegisterOwner(const void* mock_obj)
319319
GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
320320
{
321-
MutexLock l(&g_gmock_mutex);
321+
MutexLock l(g_gmock_mutex);
322322
mock_obj_ = mock_obj;
323323
}
324324
Mock::Register(mock_obj, this);
@@ -332,7 +332,7 @@ void UntypedFunctionMockerBase::SetOwnerAndName(const void* mock_obj,
332332
GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
333333
// We protect name_ under g_gmock_mutex in case this mock function
334334
// is called from two threads concurrently.
335-
MutexLock l(&g_gmock_mutex);
335+
MutexLock l(g_gmock_mutex);
336336
mock_obj_ = mock_obj;
337337
name_ = name;
338338
}
@@ -345,7 +345,7 @@ const void* UntypedFunctionMockerBase::MockObject() const
345345
{
346346
// We protect mock_obj_ under g_gmock_mutex in case this mock
347347
// function is called from two threads concurrently.
348-
MutexLock l(&g_gmock_mutex);
348+
MutexLock l(g_gmock_mutex);
349349
Assert(mock_obj_ != nullptr, __FILE__, __LINE__,
350350
"MockObject() must not be called before RegisterOwner() or "
351351
"SetOwnerAndName() has been called.");
@@ -362,7 +362,7 @@ const char* UntypedFunctionMockerBase::Name() const
362362
{
363363
// We protect name_ under g_gmock_mutex in case this mock
364364
// function is called from two threads concurrently.
365-
MutexLock l(&g_gmock_mutex);
365+
MutexLock l(g_gmock_mutex);
366366
Assert(name_ != nullptr, __FILE__, __LINE__,
367367
"Name() must not be called before SetOwnerAndName() has "
368368
"been called.");
@@ -490,7 +490,7 @@ class MockObjectRegistry {
490490
// failure, unless the user explicitly asked us to ignore it.
491491
~MockObjectRegistry() {
492492
if (!GMOCK_FLAG_GET(catch_leaked_mocks)) return;
493-
internal::MutexLock l(&internal::g_gmock_mutex);
493+
internal::MutexLock l(internal::g_gmock_mutex);
494494

495495
int leaked_count = 0;
496496
for (StateMap::const_iterator it = states_.begin(); it != states_.end();
@@ -559,7 +559,7 @@ UninterestingCallReactionMap() {
559559
void SetReactionOnUninterestingCalls(uintptr_t mock_obj,
560560
internal::CallReaction reaction)
561561
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
562-
internal::MutexLock l(&internal::g_gmock_mutex);
562+
internal::MutexLock l(internal::g_gmock_mutex);
563563
UninterestingCallReactionMap()[mock_obj] = reaction;
564564
}
565565

@@ -590,15 +590,15 @@ void Mock::FailUninterestingCalls(uintptr_t mock_obj)
590590
// entry in the call-reaction table should be removed.
591591
void Mock::UnregisterCallReaction(uintptr_t mock_obj)
592592
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
593-
internal::MutexLock l(&internal::g_gmock_mutex);
593+
internal::MutexLock l(internal::g_gmock_mutex);
594594
UninterestingCallReactionMap().erase(static_cast<uintptr_t>(mock_obj));
595595
}
596596

597597
// Returns the reaction Google Mock will have on uninteresting calls
598598
// made on the given mock object.
599599
internal::CallReaction Mock::GetReactionOnUninterestingCalls(
600600
const void* mock_obj) GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
601-
internal::MutexLock l(&internal::g_gmock_mutex);
601+
internal::MutexLock l(internal::g_gmock_mutex);
602602
return (UninterestingCallReactionMap().count(
603603
reinterpret_cast<uintptr_t>(mock_obj)) == 0)
604604
? internal::intToCallReaction(
@@ -611,7 +611,7 @@ internal::CallReaction Mock::GetReactionOnUninterestingCalls(
611611
// objects.
612612
void Mock::AllowLeak(const void* mock_obj)
613613
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
614-
internal::MutexLock l(&internal::g_gmock_mutex);
614+
internal::MutexLock l(internal::g_gmock_mutex);
615615
g_mock_object_registry.states()[mock_obj].leakable = true;
616616
}
617617

@@ -620,7 +620,7 @@ void Mock::AllowLeak(const void* mock_obj)
620620
// Test non-fatal failures and returns false.
621621
bool Mock::VerifyAndClearExpectations(void* mock_obj)
622622
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
623-
internal::MutexLock l(&internal::g_gmock_mutex);
623+
internal::MutexLock l(internal::g_gmock_mutex);
624624
return VerifyAndClearExpectationsLocked(mock_obj);
625625
}
626626

@@ -629,7 +629,7 @@ bool Mock::VerifyAndClearExpectations(void* mock_obj)
629629
// verification was successful.
630630
bool Mock::VerifyAndClear(void* mock_obj)
631631
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
632-
internal::MutexLock l(&internal::g_gmock_mutex);
632+
internal::MutexLock l(internal::g_gmock_mutex);
633633
ClearDefaultActionsLocked(mock_obj);
634634
return VerifyAndClearExpectationsLocked(mock_obj);
635635
}
@@ -679,7 +679,7 @@ bool Mock::IsStrict(void* mock_obj)
679679
void Mock::Register(const void* mock_obj,
680680
internal::UntypedFunctionMockerBase* mocker)
681681
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
682-
internal::MutexLock l(&internal::g_gmock_mutex);
682+
internal::MutexLock l(internal::g_gmock_mutex);
683683
g_mock_object_registry.states()[mock_obj].function_mockers.insert(mocker);
684684
}
685685

@@ -689,7 +689,7 @@ void Mock::Register(const void* mock_obj,
689689
void Mock::RegisterUseByOnCallOrExpectCall(const void* mock_obj,
690690
const char* file, int line)
691691
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
692-
internal::MutexLock l(&internal::g_gmock_mutex);
692+
internal::MutexLock l(internal::g_gmock_mutex);
693693
MockObjectState& state = g_mock_object_registry.states()[mock_obj];
694694
if (state.first_used_file == nullptr) {
695695
state.first_used_file = file;

googletest/include/gtest/internal/gtest-port.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,12 +1424,11 @@ class GTEST_API_ Mutex {
14241424
// "MutexLock l(&mu)". Hence the typedef trick below.
14251425
class GTestMutexLock {
14261426
public:
1427-
explicit GTestMutexLock(Mutex* mutex) : mutex_(mutex) { mutex_->lock(); }
1428-
1429-
~GTestMutexLock() { mutex_->unlock(); }
1427+
explicit GTestMutexLock(Mutex& mutex) : mutex_(mutex) { mutex_.lock(); }
1428+
~GTestMutexLock() { mutex_.unlock(); }
14301429

14311430
private:
1432-
Mutex* const mutex_;
1431+
Mutex& mutex_;
14331432

14341433
GTestMutexLock(const GTestMutexLock&) = delete;
14351434
GTestMutexLock& operator=(const GTestMutexLock&) = delete;
@@ -1716,12 +1715,11 @@ class Mutex : public MutexBase {
17161715
// "MutexLock l(&mu)". Hence the typedef trick below.
17171716
class GTestMutexLock {
17181717
public:
1719-
explicit GTestMutexLock(MutexBase* mutex) : mutex_(mutex) { mutex_->lock(); }
1720-
1721-
~GTestMutexLock() { mutex_->unlock(); }
1718+
explicit GTestMutexLock(MutexBase& mutex) : mutex_(mutex) { mutex_.lock(); }
1719+
~GTestMutexLock() { mutex_.unlock(); }
17221720

17231721
private:
1724-
MutexBase* const mutex_;
1722+
MutexBase& mutex_;
17251723

17261724
GTestMutexLock(const GTestMutexLock&) = delete;
17271725
GTestMutexLock& operator=(const GTestMutexLock&) = delete;
@@ -1881,7 +1879,7 @@ class Mutex {
18811879
// "MutexLock l(&mu)". Hence the typedef trick below.
18821880
class GTestMutexLock {
18831881
public:
1884-
explicit GTestMutexLock(Mutex*) {} // NOLINT
1882+
explicit GTestMutexLock(Mutex&) {} // NOLINT
18851883
};
18861884

18871885
typedef GTestMutexLock MutexLock;

googletest/src/gtest-port.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ class ThreadLocalRegistryImpl {
499499
MemoryIsNotDeallocated memory_is_not_deallocated;
500500
#endif // _MSC_VER
501501
DWORD current_thread = ::GetCurrentThreadId();
502-
MutexLock lock(&mutex_);
502+
MutexLock lock(mutex_);
503503
ThreadIdToThreadLocals* const thread_to_thread_locals =
504504
GetThreadLocalsMapLocked();
505505
ThreadIdToThreadLocals::iterator thread_local_pos =
@@ -532,7 +532,7 @@ class ThreadLocalRegistryImpl {
532532
// Clean up the ThreadLocalValues data structure while holding the lock, but
533533
// defer the destruction of the ThreadLocalValueHolderBases.
534534
{
535-
MutexLock lock(&mutex_);
535+
MutexLock lock(mutex_);
536536
ThreadIdToThreadLocals* const thread_to_thread_locals =
537537
GetThreadLocalsMapLocked();
538538
for (ThreadIdToThreadLocals::iterator it =
@@ -559,7 +559,7 @@ class ThreadLocalRegistryImpl {
559559
// Clean up the ThreadIdToThreadLocals data structure while holding the
560560
// lock, but defer the destruction of the ThreadLocalValueHolderBases.
561561
{
562-
MutexLock lock(&mutex_);
562+
MutexLock lock(mutex_);
563563
ThreadIdToThreadLocals* const thread_to_thread_locals =
564564
GetThreadLocalsMapLocked();
565565
ThreadIdToThreadLocals::iterator thread_local_pos =

googletest/src/gtest.cc

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,14 +1086,14 @@ void DefaultPerThreadTestPartResultReporter::ReportTestPartResult(
10861086
// Returns the global test part result reporter.
10871087
TestPartResultReporterInterface*
10881088
UnitTestImpl::GetGlobalTestPartResultReporter() {
1089-
internal::MutexLock lock(&global_test_part_result_reporter_mutex_);
1089+
internal::MutexLock lock(global_test_part_result_reporter_mutex_);
10901090
return global_test_part_result_reporter_;
10911091
}
10921092

10931093
// Sets the global test part result reporter.
10941094
void UnitTestImpl::SetGlobalTestPartResultReporter(
10951095
TestPartResultReporterInterface* reporter) {
1096-
internal::MutexLock lock(&global_test_part_result_reporter_mutex_);
1096+
internal::MutexLock lock(global_test_part_result_reporter_mutex_);
10971097
global_test_part_result_reporter_ = reporter;
10981098
}
10991099

@@ -2347,7 +2347,7 @@ void TestResult::RecordProperty(const std::string& xml_element,
23472347
if (!ValidateTestProperty(xml_element, test_property)) {
23482348
return;
23492349
}
2350-
internal::MutexLock lock(&test_properties_mutex_);
2350+
internal::MutexLock lock(test_properties_mutex_);
23512351
const std::vector<TestProperty>::iterator property_with_matching_key =
23522352
std::find_if(test_properties_.begin(), test_properties_.end(),
23532353
internal::TestPropertyKeyIs(test_property.key()));
@@ -5088,7 +5088,7 @@ std::string OsStackTraceGetter::CurrentStackTrace(int max_depth, int skip_count)
50885088

50895089
void* caller_frame = nullptr;
50905090
{
5091-
MutexLock lock(&mutex_);
5091+
MutexLock lock(mutex_);
50925092
caller_frame = caller_frame_;
50935093
}
50945094

@@ -5127,7 +5127,7 @@ void OsStackTraceGetter::UponLeavingGTest() GTEST_LOCK_EXCLUDED_(mutex_) {
51275127
caller_frame = nullptr;
51285128
}
51295129

5130-
MutexLock lock(&mutex_);
5130+
MutexLock lock(mutex_);
51315131
caller_frame_ = caller_frame;
51325132
#endif // GTEST_HAS_ABSL
51335133
}
@@ -5390,13 +5390,13 @@ void UnitTest::UponLeavingGTest() {
53905390

53915391
// Sets the TestSuite object for the test that's currently running.
53925392
void UnitTest::set_current_test_suite(TestSuite* a_current_test_suite) {
5393-
internal::MutexLock lock(&mutex_);
5393+
internal::MutexLock lock(mutex_);
53945394
impl_->set_current_test_suite(a_current_test_suite);
53955395
}
53965396

53975397
// Sets the TestInfo object for the test that's currently running.
53985398
void UnitTest::set_current_test_info(TestInfo* a_current_test_info) {
5399-
internal::MutexLock lock(&mutex_);
5399+
internal::MutexLock lock(mutex_);
54005400
impl_->set_current_test_info(a_current_test_info);
54015401
}
54025402

@@ -5435,7 +5435,7 @@ void UnitTest::AddTestPartResult(TestPartResult::Type result_type,
54355435
Message msg;
54365436
msg << message;
54375437

5438-
internal::MutexLock lock(&mutex_);
5438+
internal::MutexLock lock(mutex_);
54395439
if (!impl_->gtest_trace_stack().empty()) {
54405440
msg << "\n" << GTEST_NAME_ << " trace:";
54415441

@@ -5618,15 +5618,15 @@ const char* UnitTest::original_working_dir() const {
56185618
// or NULL if no test is running.
56195619
const TestSuite* UnitTest::current_test_suite() const
56205620
GTEST_LOCK_EXCLUDED_(mutex_) {
5621-
internal::MutexLock lock(&mutex_);
5621+
internal::MutexLock lock(mutex_);
56225622
return impl_->current_test_suite();
56235623
}
56245624

56255625
// Legacy API is still available but deprecated
56265626
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
56275627
const TestCase* UnitTest::current_test_case() const
56285628
GTEST_LOCK_EXCLUDED_(mutex_) {
5629-
internal::MutexLock lock(&mutex_);
5629+
internal::MutexLock lock(mutex_);
56305630
return impl_->current_test_suite();
56315631
}
56325632
#endif
@@ -5635,7 +5635,7 @@ const TestCase* UnitTest::current_test_case() const
56355635
// or NULL if no test is running.
56365636
const TestInfo* UnitTest::current_test_info() const
56375637
GTEST_LOCK_EXCLUDED_(mutex_) {
5638-
internal::MutexLock lock(&mutex_);
5638+
internal::MutexLock lock(mutex_);
56395639
return impl_->current_test_info();
56405640
}
56415641

@@ -5659,13 +5659,13 @@ UnitTest::~UnitTest() { delete impl_; }
56595659
// Google Test trace stack.
56605660
void UnitTest::PushGTestTrace(const internal::TraceInfo& trace)
56615661
GTEST_LOCK_EXCLUDED_(mutex_) {
5662-
internal::MutexLock lock(&mutex_);
5662+
internal::MutexLock lock(mutex_);
56635663
impl_->gtest_trace_stack().push_back(trace);
56645664
}
56655665

56665666
// Pops a trace from the per-thread Google Test trace stack.
56675667
void UnitTest::PopGTestTrace() GTEST_LOCK_EXCLUDED_(mutex_) {
5668-
internal::MutexLock lock(&mutex_);
5668+
internal::MutexLock lock(mutex_);
56695669
impl_->gtest_trace_stack().pop_back();
56705670
}
56715671

googletest/test/googletest-port-test.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ TEST(GetThreadCountTest, ReturnsCorrectValue) {
308308

309309
internal::Mutex mutex;
310310
{
311-
internal::MutexLock lock(&mutex);
311+
internal::MutexLock lock(mutex);
312312
pthread_attr_t attr;
313313
ASSERT_EQ(0, pthread_attr_init(&attr));
314314
ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
@@ -1028,21 +1028,23 @@ TEST(MutexDeathTest, AssertHeldShouldAssertWhenNotLocked) {
10281028
EXPECT_DEATH_IF_SUPPORTED(
10291029
{
10301030
Mutex m;
1031-
{ MutexLock lock(&m); }
1031+
{
1032+
MutexLock lock(m);
1033+
}
10321034
m.AssertHeld();
10331035
},
10341036
"thread .*hold");
10351037
}
10361038

10371039
TEST(MutexTest, AssertHeldShouldNotAssertWhenLocked) {
10381040
Mutex m;
1039-
MutexLock lock(&m);
1041+
MutexLock lock(m);
10401042
m.AssertHeld();
10411043
}
10421044

10431045
class AtomicCounterWithMutex {
10441046
public:
1045-
explicit AtomicCounterWithMutex(Mutex* mutex)
1047+
explicit AtomicCounterWithMutex(Mutex& mutex)
10461048
: value_(0), mutex_(mutex), random_(42) {}
10471049

10481050
void Increment() {
@@ -1083,7 +1085,7 @@ class AtomicCounterWithMutex {
10831085

10841086
private:
10851087
volatile int value_;
1086-
Mutex* const mutex_; // Protects value_.
1088+
Mutex& mutex_; // Protects value_.
10871089
Random random_;
10881090
};
10891091

@@ -1094,7 +1096,7 @@ void CountingThreadFunc(pair<AtomicCounterWithMutex*, int> param) {
10941096
// Tests that the mutex only lets one thread at a time to lock it.
10951097
TEST(MutexTest, OnlyOneThreadCanLockAtATime) {
10961098
Mutex mutex;
1097-
AtomicCounterWithMutex locked_counter(&mutex);
1099+
AtomicCounterWithMutex locked_counter(mutex);
10981100

10991101
typedef ThreadWithParam<pair<AtomicCounterWithMutex*, int> > ThreadType;
11001102
const int kCycleCount = 20;

0 commit comments

Comments
 (0)