Compare commits

...

3 Commits

Author SHA1 Message Date
Arnaud Kleinpeter
a0b4e4a4cc
Merge c13a88404705813f445c218fb2b914e73a1602a1 into 1b96fa13f549387b7549cc89e1a785cf143a1a50 2025-11-14 12:21:03 +08:00
Derek Mauro
1b96fa13f5 Switch to referenceful lock holder for Abseil compatibility
PiperOrigin-RevId: 831156684
Change-Id: I8a8b017ec2fc318a65f57e04428c030c707ee682
2025-11-11 18:56:52 -08:00
Arnaud Kleinpeter
c13a884047 add SEH exception name to exception message 2024-11-08 15:34:26 +01:00
7 changed files with 106 additions and 49 deletions

View File

@ -1467,7 +1467,7 @@ class FunctionMocker<R(Args...)> final : public UntypedFunctionMockerBase {
// function have been satisfied. If not, it will report Google Test // function have been satisfied. If not, it will report Google Test
// non-fatal failures for the violations. // non-fatal failures for the violations.
~FunctionMocker() override GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { ~FunctionMocker() override GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
MutexLock l(&g_gmock_mutex); MutexLock l(g_gmock_mutex);
VerifyAndClearExpectationsLocked(); VerifyAndClearExpectationsLocked();
Mock::UnregisterLocked(this); Mock::UnregisterLocked(this);
ClearDefaultActionsLocked(); ClearDefaultActionsLocked();
@ -1646,7 +1646,7 @@ class FunctionMocker<R(Args...)> final : public UntypedFunctionMockerBase {
GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
const ArgumentTuple& args = const ArgumentTuple& args =
*static_cast<const ArgumentTuple*>(untyped_args); *static_cast<const ArgumentTuple*>(untyped_args);
MutexLock l(&g_gmock_mutex); MutexLock l(g_gmock_mutex);
TypedExpectation<F>* exp = this->FindMatchingExpectationLocked(args); TypedExpectation<F>* exp = this->FindMatchingExpectationLocked(args);
if (exp == nullptr) { // A match wasn't found. if (exp == nullptr) { // A match wasn't found.
this->FormatUnexpectedCallMessageLocked(args, what, why); this->FormatUnexpectedCallMessageLocked(args, what, why);

View File

@ -156,7 +156,7 @@ GTEST_API_ void Log(LogSeverity severity, const std::string& message,
if (!LogIsVisible(severity)) return; if (!LogIsVisible(severity)) return;
// Ensures that logs from different threads don't interleave. // Ensures that logs from different threads don't interleave.
MutexLock l(&g_log_mutex); MutexLock l(g_log_mutex);
if (severity == kWarning) { if (severity == kWarning) {
// Prints a GMOCK WARNING marker to make the warnings easily searchable. // Prints a GMOCK WARNING marker to make the warnings easily searchable.

View File

@ -212,7 +212,7 @@ void ExpectationBase::CheckActionCountIfNotDone() const
GTEST_LOCK_EXCLUDED_(mutex_) { GTEST_LOCK_EXCLUDED_(mutex_) {
bool should_check = false; bool should_check = false;
{ {
MutexLock l(&mutex_); MutexLock l(mutex_);
if (!action_count_checked_) { if (!action_count_checked_) {
action_count_checked_ = true; action_count_checked_ = true;
should_check = true; should_check = true;
@ -318,7 +318,7 @@ UntypedFunctionMockerBase::~UntypedFunctionMockerBase() = default;
void UntypedFunctionMockerBase::RegisterOwner(const void* mock_obj) void UntypedFunctionMockerBase::RegisterOwner(const void* mock_obj)
GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
{ {
MutexLock l(&g_gmock_mutex); MutexLock l(g_gmock_mutex);
mock_obj_ = mock_obj; mock_obj_ = mock_obj;
} }
Mock::Register(mock_obj, this); Mock::Register(mock_obj, this);
@ -332,7 +332,7 @@ void UntypedFunctionMockerBase::SetOwnerAndName(const void* mock_obj,
GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
// We protect name_ under g_gmock_mutex in case this mock function // We protect name_ under g_gmock_mutex in case this mock function
// is called from two threads concurrently. // is called from two threads concurrently.
MutexLock l(&g_gmock_mutex); MutexLock l(g_gmock_mutex);
mock_obj_ = mock_obj; mock_obj_ = mock_obj;
name_ = name; name_ = name;
} }
@ -345,7 +345,7 @@ const void* UntypedFunctionMockerBase::MockObject() const
{ {
// We protect mock_obj_ under g_gmock_mutex in case this mock // We protect mock_obj_ under g_gmock_mutex in case this mock
// function is called from two threads concurrently. // function is called from two threads concurrently.
MutexLock l(&g_gmock_mutex); MutexLock l(g_gmock_mutex);
Assert(mock_obj_ != nullptr, __FILE__, __LINE__, Assert(mock_obj_ != nullptr, __FILE__, __LINE__,
"MockObject() must not be called before RegisterOwner() or " "MockObject() must not be called before RegisterOwner() or "
"SetOwnerAndName() has been called."); "SetOwnerAndName() has been called.");
@ -362,7 +362,7 @@ const char* UntypedFunctionMockerBase::Name() const
{ {
// We protect name_ under g_gmock_mutex in case this mock // We protect name_ under g_gmock_mutex in case this mock
// function is called from two threads concurrently. // function is called from two threads concurrently.
MutexLock l(&g_gmock_mutex); MutexLock l(g_gmock_mutex);
Assert(name_ != nullptr, __FILE__, __LINE__, Assert(name_ != nullptr, __FILE__, __LINE__,
"Name() must not be called before SetOwnerAndName() has " "Name() must not be called before SetOwnerAndName() has "
"been called."); "been called.");
@ -490,7 +490,7 @@ class MockObjectRegistry {
// failure, unless the user explicitly asked us to ignore it. // failure, unless the user explicitly asked us to ignore it.
~MockObjectRegistry() { ~MockObjectRegistry() {
if (!GMOCK_FLAG_GET(catch_leaked_mocks)) return; if (!GMOCK_FLAG_GET(catch_leaked_mocks)) return;
internal::MutexLock l(&internal::g_gmock_mutex); internal::MutexLock l(internal::g_gmock_mutex);
int leaked_count = 0; int leaked_count = 0;
for (StateMap::const_iterator it = states_.begin(); it != states_.end(); for (StateMap::const_iterator it = states_.begin(); it != states_.end();
@ -559,7 +559,7 @@ UninterestingCallReactionMap() {
void SetReactionOnUninterestingCalls(uintptr_t mock_obj, void SetReactionOnUninterestingCalls(uintptr_t mock_obj,
internal::CallReaction reaction) internal::CallReaction reaction)
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
internal::MutexLock l(&internal::g_gmock_mutex); internal::MutexLock l(internal::g_gmock_mutex);
UninterestingCallReactionMap()[mock_obj] = reaction; UninterestingCallReactionMap()[mock_obj] = reaction;
} }
@ -590,7 +590,7 @@ void Mock::FailUninterestingCalls(uintptr_t mock_obj)
// entry in the call-reaction table should be removed. // entry in the call-reaction table should be removed.
void Mock::UnregisterCallReaction(uintptr_t mock_obj) void Mock::UnregisterCallReaction(uintptr_t mock_obj)
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
internal::MutexLock l(&internal::g_gmock_mutex); internal::MutexLock l(internal::g_gmock_mutex);
UninterestingCallReactionMap().erase(static_cast<uintptr_t>(mock_obj)); UninterestingCallReactionMap().erase(static_cast<uintptr_t>(mock_obj));
} }
@ -598,7 +598,7 @@ void Mock::UnregisterCallReaction(uintptr_t mock_obj)
// made on the given mock object. // made on the given mock object.
internal::CallReaction Mock::GetReactionOnUninterestingCalls( internal::CallReaction Mock::GetReactionOnUninterestingCalls(
const void* mock_obj) GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { const void* mock_obj) GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
internal::MutexLock l(&internal::g_gmock_mutex); internal::MutexLock l(internal::g_gmock_mutex);
return (UninterestingCallReactionMap().count( return (UninterestingCallReactionMap().count(
reinterpret_cast<uintptr_t>(mock_obj)) == 0) reinterpret_cast<uintptr_t>(mock_obj)) == 0)
? internal::intToCallReaction( ? internal::intToCallReaction(
@ -611,7 +611,7 @@ internal::CallReaction Mock::GetReactionOnUninterestingCalls(
// objects. // objects.
void Mock::AllowLeak(const void* mock_obj) void Mock::AllowLeak(const void* mock_obj)
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
internal::MutexLock l(&internal::g_gmock_mutex); internal::MutexLock l(internal::g_gmock_mutex);
g_mock_object_registry.states()[mock_obj].leakable = true; g_mock_object_registry.states()[mock_obj].leakable = true;
} }
@ -620,7 +620,7 @@ void Mock::AllowLeak(const void* mock_obj)
// Test non-fatal failures and returns false. // Test non-fatal failures and returns false.
bool Mock::VerifyAndClearExpectations(void* mock_obj) bool Mock::VerifyAndClearExpectations(void* mock_obj)
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
internal::MutexLock l(&internal::g_gmock_mutex); internal::MutexLock l(internal::g_gmock_mutex);
return VerifyAndClearExpectationsLocked(mock_obj); return VerifyAndClearExpectationsLocked(mock_obj);
} }
@ -629,7 +629,7 @@ bool Mock::VerifyAndClearExpectations(void* mock_obj)
// verification was successful. // verification was successful.
bool Mock::VerifyAndClear(void* mock_obj) bool Mock::VerifyAndClear(void* mock_obj)
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
internal::MutexLock l(&internal::g_gmock_mutex); internal::MutexLock l(internal::g_gmock_mutex);
ClearDefaultActionsLocked(mock_obj); ClearDefaultActionsLocked(mock_obj);
return VerifyAndClearExpectationsLocked(mock_obj); return VerifyAndClearExpectationsLocked(mock_obj);
} }
@ -679,7 +679,7 @@ bool Mock::IsStrict(void* mock_obj)
void Mock::Register(const void* mock_obj, void Mock::Register(const void* mock_obj,
internal::UntypedFunctionMockerBase* mocker) internal::UntypedFunctionMockerBase* mocker)
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
internal::MutexLock l(&internal::g_gmock_mutex); internal::MutexLock l(internal::g_gmock_mutex);
g_mock_object_registry.states()[mock_obj].function_mockers.insert(mocker); g_mock_object_registry.states()[mock_obj].function_mockers.insert(mocker);
} }
@ -689,7 +689,7 @@ void Mock::Register(const void* mock_obj,
void Mock::RegisterUseByOnCallOrExpectCall(const void* mock_obj, void Mock::RegisterUseByOnCallOrExpectCall(const void* mock_obj,
const char* file, int line) const char* file, int line)
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
internal::MutexLock l(&internal::g_gmock_mutex); internal::MutexLock l(internal::g_gmock_mutex);
MockObjectState& state = g_mock_object_registry.states()[mock_obj]; MockObjectState& state = g_mock_object_registry.states()[mock_obj];
if (state.first_used_file == nullptr) { if (state.first_used_file == nullptr) {
state.first_used_file = file; state.first_used_file = file;

View File

@ -1424,12 +1424,11 @@ class GTEST_API_ Mutex {
// "MutexLock l(&mu)". Hence the typedef trick below. // "MutexLock l(&mu)". Hence the typedef trick below.
class GTestMutexLock { class GTestMutexLock {
public: public:
explicit GTestMutexLock(Mutex* mutex) : mutex_(mutex) { mutex_->lock(); } explicit GTestMutexLock(Mutex& mutex) : mutex_(mutex) { mutex_.lock(); }
~GTestMutexLock() { mutex_.unlock(); }
~GTestMutexLock() { mutex_->unlock(); }
private: private:
Mutex* const mutex_; Mutex& mutex_;
GTestMutexLock(const GTestMutexLock&) = delete; GTestMutexLock(const GTestMutexLock&) = delete;
GTestMutexLock& operator=(const GTestMutexLock&) = delete; GTestMutexLock& operator=(const GTestMutexLock&) = delete;
@ -1716,12 +1715,11 @@ class Mutex : public MutexBase {
// "MutexLock l(&mu)". Hence the typedef trick below. // "MutexLock l(&mu)". Hence the typedef trick below.
class GTestMutexLock { class GTestMutexLock {
public: public:
explicit GTestMutexLock(MutexBase* mutex) : mutex_(mutex) { mutex_->lock(); } explicit GTestMutexLock(MutexBase& mutex) : mutex_(mutex) { mutex_.lock(); }
~GTestMutexLock() { mutex_.unlock(); }
~GTestMutexLock() { mutex_->unlock(); }
private: private:
MutexBase* const mutex_; MutexBase& mutex_;
GTestMutexLock(const GTestMutexLock&) = delete; GTestMutexLock(const GTestMutexLock&) = delete;
GTestMutexLock& operator=(const GTestMutexLock&) = delete; GTestMutexLock& operator=(const GTestMutexLock&) = delete;
@ -1881,7 +1879,7 @@ class Mutex {
// "MutexLock l(&mu)". Hence the typedef trick below. // "MutexLock l(&mu)". Hence the typedef trick below.
class GTestMutexLock { class GTestMutexLock {
public: public:
explicit GTestMutexLock(Mutex*) {} // NOLINT explicit GTestMutexLock(Mutex&) {} // NOLINT
}; };
typedef GTestMutexLock MutexLock; typedef GTestMutexLock MutexLock;

View File

@ -499,7 +499,7 @@ class ThreadLocalRegistryImpl {
MemoryIsNotDeallocated memory_is_not_deallocated; MemoryIsNotDeallocated memory_is_not_deallocated;
#endif // _MSC_VER #endif // _MSC_VER
DWORD current_thread = ::GetCurrentThreadId(); DWORD current_thread = ::GetCurrentThreadId();
MutexLock lock(&mutex_); MutexLock lock(mutex_);
ThreadIdToThreadLocals* const thread_to_thread_locals = ThreadIdToThreadLocals* const thread_to_thread_locals =
GetThreadLocalsMapLocked(); GetThreadLocalsMapLocked();
ThreadIdToThreadLocals::iterator thread_local_pos = ThreadIdToThreadLocals::iterator thread_local_pos =
@ -532,7 +532,7 @@ class ThreadLocalRegistryImpl {
// Clean up the ThreadLocalValues data structure while holding the lock, but // Clean up the ThreadLocalValues data structure while holding the lock, but
// defer the destruction of the ThreadLocalValueHolderBases. // defer the destruction of the ThreadLocalValueHolderBases.
{ {
MutexLock lock(&mutex_); MutexLock lock(mutex_);
ThreadIdToThreadLocals* const thread_to_thread_locals = ThreadIdToThreadLocals* const thread_to_thread_locals =
GetThreadLocalsMapLocked(); GetThreadLocalsMapLocked();
for (ThreadIdToThreadLocals::iterator it = for (ThreadIdToThreadLocals::iterator it =
@ -559,7 +559,7 @@ class ThreadLocalRegistryImpl {
// Clean up the ThreadIdToThreadLocals data structure while holding the // Clean up the ThreadIdToThreadLocals data structure while holding the
// lock, but defer the destruction of the ThreadLocalValueHolderBases. // lock, but defer the destruction of the ThreadLocalValueHolderBases.
{ {
MutexLock lock(&mutex_); MutexLock lock(mutex_);
ThreadIdToThreadLocals* const thread_to_thread_locals = ThreadIdToThreadLocals* const thread_to_thread_locals =
GetThreadLocalsMapLocked(); GetThreadLocalsMapLocked();
ThreadIdToThreadLocals::iterator thread_local_pos = ThreadIdToThreadLocals::iterator thread_local_pos =

View File

@ -907,11 +907,68 @@ bool UnitTestOptions::FilterMatchesTest(const std::string& test_suite_name,
} }
#if GTEST_HAS_SEH #if GTEST_HAS_SEH
static std::string GetSEHExceptionName(const DWORD exception_code) {
static const std::unordered_map<DWORD, std::string> seh_messages = {
{EXCEPTION_ACCESS_VIOLATION, "Access violation (invalid memory access)"},
{EXCEPTION_ARRAY_BOUNDS_EXCEEDED, "Array bounds exceeded"},
{EXCEPTION_BREAKPOINT, "Breakpoint encountered"},
{EXCEPTION_DATATYPE_MISALIGNMENT, "Data misalignment"},
{EXCEPTION_FLT_DENORMAL_OPERAND, "Floating-point denormal operand"},
{EXCEPTION_FLT_DIVIDE_BY_ZERO, "Floating-point divide by zero"},
{EXCEPTION_FLT_INEXACT_RESULT, "Floating-point inexact result"},
{EXCEPTION_FLT_INVALID_OPERATION, "Floating-point invalid operation"},
{EXCEPTION_FLT_OVERFLOW, "Floating-point overflow"},
{EXCEPTION_FLT_STACK_CHECK, "Floating-point stack check"},
{EXCEPTION_FLT_UNDERFLOW, "Floating-point underflow"},
{EXCEPTION_ILLEGAL_INSTRUCTION, "Illegal instruction"},
{EXCEPTION_IN_PAGE_ERROR, "Page not found or access denied"},
{EXCEPTION_INT_DIVIDE_BY_ZERO, "Integer divide by zero"},
{EXCEPTION_INT_OVERFLOW, "Integer overflow"},
{EXCEPTION_INVALID_DISPOSITION, "Invalid disposition"},
{EXCEPTION_NONCONTINUABLE_EXCEPTION, "Noncontinuable exception"},
{EXCEPTION_PRIV_INSTRUCTION, "Privileged instruction"},
{EXCEPTION_SINGLE_STEP, "Single step"},
{EXCEPTION_STACK_OVERFLOW, "Stack overflow"}};
// Check if we have a custom message for the code
const auto it = seh_messages.find(exception_code);
if (it != seh_messages.end()) {
return it->second;
}
// Fallback to system message if available
LPVOID message_buffer;
const DWORD buffer_size = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, exception_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&message_buffer, 0, nullptr);
if (buffer_size) {
std::wstring message((LPTSTR)message_buffer, buffer_size);
LocalFree(message_buffer);
int size_needed =
WideCharToMultiByte(CP_UTF8, 0, message.c_str(), (int)message.size(),
nullptr, 0, nullptr, nullptr);
std::string str(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, message.c_str(), (int)message.size(),
&str[0], size_needed, nullptr, nullptr);
return str;
}
return "Unknown SEH exception";
}
static std::string FormatSehExceptionMessage(DWORD exception_code, static std::string FormatSehExceptionMessage(DWORD exception_code,
const char* location) { const char* location) {
const std::string messageText = GetSEHExceptionName(exception_code);
Message message; Message message;
message << "SEH exception with code 0x" << std::setbase(16) << exception_code message << "SEH exception with code 0x" << std::setbase(16) << exception_code
<< std::setbase(10) << " thrown in " << location << "."; << std::setbase(10) << " - " << messageText << " thrown in "
<< location << ".";
return message.GetString(); return message.GetString();
} }
@ -1086,14 +1143,14 @@ void DefaultPerThreadTestPartResultReporter::ReportTestPartResult(
// Returns the global test part result reporter. // Returns the global test part result reporter.
TestPartResultReporterInterface* TestPartResultReporterInterface*
UnitTestImpl::GetGlobalTestPartResultReporter() { UnitTestImpl::GetGlobalTestPartResultReporter() {
internal::MutexLock lock(&global_test_part_result_reporter_mutex_); internal::MutexLock lock(global_test_part_result_reporter_mutex_);
return global_test_part_result_reporter_; return global_test_part_result_reporter_;
} }
// Sets the global test part result reporter. // Sets the global test part result reporter.
void UnitTestImpl::SetGlobalTestPartResultReporter( void UnitTestImpl::SetGlobalTestPartResultReporter(
TestPartResultReporterInterface* reporter) { TestPartResultReporterInterface* reporter) {
internal::MutexLock lock(&global_test_part_result_reporter_mutex_); internal::MutexLock lock(global_test_part_result_reporter_mutex_);
global_test_part_result_reporter_ = reporter; global_test_part_result_reporter_ = reporter;
} }
@ -2347,7 +2404,7 @@ void TestResult::RecordProperty(const std::string& xml_element,
if (!ValidateTestProperty(xml_element, test_property)) { if (!ValidateTestProperty(xml_element, test_property)) {
return; return;
} }
internal::MutexLock lock(&test_properties_mutex_); internal::MutexLock lock(test_properties_mutex_);
const std::vector<TestProperty>::iterator property_with_matching_key = const std::vector<TestProperty>::iterator property_with_matching_key =
std::find_if(test_properties_.begin(), test_properties_.end(), std::find_if(test_properties_.begin(), test_properties_.end(),
internal::TestPropertyKeyIs(test_property.key())); internal::TestPropertyKeyIs(test_property.key()));
@ -5088,7 +5145,7 @@ std::string OsStackTraceGetter::CurrentStackTrace(int max_depth, int skip_count)
void* caller_frame = nullptr; void* caller_frame = nullptr;
{ {
MutexLock lock(&mutex_); MutexLock lock(mutex_);
caller_frame = caller_frame_; caller_frame = caller_frame_;
} }
@ -5127,7 +5184,7 @@ void OsStackTraceGetter::UponLeavingGTest() GTEST_LOCK_EXCLUDED_(mutex_) {
caller_frame = nullptr; caller_frame = nullptr;
} }
MutexLock lock(&mutex_); MutexLock lock(mutex_);
caller_frame_ = caller_frame; caller_frame_ = caller_frame;
#endif // GTEST_HAS_ABSL #endif // GTEST_HAS_ABSL
} }
@ -5390,13 +5447,13 @@ void UnitTest::UponLeavingGTest() {
// Sets the TestSuite object for the test that's currently running. // Sets the TestSuite object for the test that's currently running.
void UnitTest::set_current_test_suite(TestSuite* a_current_test_suite) { void UnitTest::set_current_test_suite(TestSuite* a_current_test_suite) {
internal::MutexLock lock(&mutex_); internal::MutexLock lock(mutex_);
impl_->set_current_test_suite(a_current_test_suite); impl_->set_current_test_suite(a_current_test_suite);
} }
// Sets the TestInfo object for the test that's currently running. // Sets the TestInfo object for the test that's currently running.
void UnitTest::set_current_test_info(TestInfo* a_current_test_info) { void UnitTest::set_current_test_info(TestInfo* a_current_test_info) {
internal::MutexLock lock(&mutex_); internal::MutexLock lock(mutex_);
impl_->set_current_test_info(a_current_test_info); impl_->set_current_test_info(a_current_test_info);
} }
@ -5435,7 +5492,7 @@ void UnitTest::AddTestPartResult(TestPartResult::Type result_type,
Message msg; Message msg;
msg << message; msg << message;
internal::MutexLock lock(&mutex_); internal::MutexLock lock(mutex_);
if (!impl_->gtest_trace_stack().empty()) { if (!impl_->gtest_trace_stack().empty()) {
msg << "\n" << GTEST_NAME_ << " trace:"; msg << "\n" << GTEST_NAME_ << " trace:";
@ -5618,7 +5675,7 @@ const char* UnitTest::original_working_dir() const {
// or NULL if no test is running. // or NULL if no test is running.
const TestSuite* UnitTest::current_test_suite() const const TestSuite* UnitTest::current_test_suite() const
GTEST_LOCK_EXCLUDED_(mutex_) { GTEST_LOCK_EXCLUDED_(mutex_) {
internal::MutexLock lock(&mutex_); internal::MutexLock lock(mutex_);
return impl_->current_test_suite(); return impl_->current_test_suite();
} }
@ -5626,7 +5683,7 @@ const TestSuite* UnitTest::current_test_suite() const
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
const TestCase* UnitTest::current_test_case() const const TestCase* UnitTest::current_test_case() const
GTEST_LOCK_EXCLUDED_(mutex_) { GTEST_LOCK_EXCLUDED_(mutex_) {
internal::MutexLock lock(&mutex_); internal::MutexLock lock(mutex_);
return impl_->current_test_suite(); return impl_->current_test_suite();
} }
#endif #endif
@ -5635,7 +5692,7 @@ const TestCase* UnitTest::current_test_case() const
// or NULL if no test is running. // or NULL if no test is running.
const TestInfo* UnitTest::current_test_info() const const TestInfo* UnitTest::current_test_info() const
GTEST_LOCK_EXCLUDED_(mutex_) { GTEST_LOCK_EXCLUDED_(mutex_) {
internal::MutexLock lock(&mutex_); internal::MutexLock lock(mutex_);
return impl_->current_test_info(); return impl_->current_test_info();
} }
@ -5659,13 +5716,13 @@ UnitTest::~UnitTest() { delete impl_; }
// Google Test trace stack. // Google Test trace stack.
void UnitTest::PushGTestTrace(const internal::TraceInfo& trace) void UnitTest::PushGTestTrace(const internal::TraceInfo& trace)
GTEST_LOCK_EXCLUDED_(mutex_) { GTEST_LOCK_EXCLUDED_(mutex_) {
internal::MutexLock lock(&mutex_); internal::MutexLock lock(mutex_);
impl_->gtest_trace_stack().push_back(trace); impl_->gtest_trace_stack().push_back(trace);
} }
// Pops a trace from the per-thread Google Test trace stack. // Pops a trace from the per-thread Google Test trace stack.
void UnitTest::PopGTestTrace() GTEST_LOCK_EXCLUDED_(mutex_) { void UnitTest::PopGTestTrace() GTEST_LOCK_EXCLUDED_(mutex_) {
internal::MutexLock lock(&mutex_); internal::MutexLock lock(mutex_);
impl_->gtest_trace_stack().pop_back(); impl_->gtest_trace_stack().pop_back();
} }

View File

@ -308,7 +308,7 @@ TEST(GetThreadCountTest, ReturnsCorrectValue) {
internal::Mutex mutex; internal::Mutex mutex;
{ {
internal::MutexLock lock(&mutex); internal::MutexLock lock(mutex);
pthread_attr_t attr; pthread_attr_t attr;
ASSERT_EQ(0, pthread_attr_init(&attr)); ASSERT_EQ(0, pthread_attr_init(&attr));
ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE)); ASSERT_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
@ -1028,7 +1028,9 @@ TEST(MutexDeathTest, AssertHeldShouldAssertWhenNotLocked) {
EXPECT_DEATH_IF_SUPPORTED( EXPECT_DEATH_IF_SUPPORTED(
{ {
Mutex m; Mutex m;
{ MutexLock lock(&m); } {
MutexLock lock(m);
}
m.AssertHeld(); m.AssertHeld();
}, },
"thread .*hold"); "thread .*hold");
@ -1036,13 +1038,13 @@ TEST(MutexDeathTest, AssertHeldShouldAssertWhenNotLocked) {
TEST(MutexTest, AssertHeldShouldNotAssertWhenLocked) { TEST(MutexTest, AssertHeldShouldNotAssertWhenLocked) {
Mutex m; Mutex m;
MutexLock lock(&m); MutexLock lock(m);
m.AssertHeld(); m.AssertHeld();
} }
class AtomicCounterWithMutex { class AtomicCounterWithMutex {
public: public:
explicit AtomicCounterWithMutex(Mutex* mutex) explicit AtomicCounterWithMutex(Mutex& mutex)
: value_(0), mutex_(mutex), random_(42) {} : value_(0), mutex_(mutex), random_(42) {}
void Increment() { void Increment() {
@ -1083,7 +1085,7 @@ class AtomicCounterWithMutex {
private: private:
volatile int value_; volatile int value_;
Mutex* const mutex_; // Protects value_. Mutex& mutex_; // Protects value_.
Random random_; Random random_;
}; };
@ -1094,7 +1096,7 @@ void CountingThreadFunc(pair<AtomicCounterWithMutex*, int> param) {
// Tests that the mutex only lets one thread at a time to lock it. // Tests that the mutex only lets one thread at a time to lock it.
TEST(MutexTest, OnlyOneThreadCanLockAtATime) { TEST(MutexTest, OnlyOneThreadCanLockAtATime) {
Mutex mutex; Mutex mutex;
AtomicCounterWithMutex locked_counter(&mutex); AtomicCounterWithMutex locked_counter(mutex);
typedef ThreadWithParam<pair<AtomicCounterWithMutex*, int> > ThreadType; typedef ThreadWithParam<pair<AtomicCounterWithMutex*, int> > ThreadType;
const int kCycleCount = 20; const int kCycleCount = 20;