fix multi construct for g_gmock_implicit_sequence when libgmock.a is linked to many shared library

Signed-off-by: arvin <icemanpeng@foxmail.com>
This commit is contained in:
arvin 2024-05-09 01:43:42 +08:00
parent fa6de7f438
commit 1ae48fa1f1
2 changed files with 12 additions and 7 deletions

View File

@ -666,7 +666,7 @@ namespace internal {
// Points to the implicit sequence introduced by a living InSequence // Points to the implicit sequence introduced by a living InSequence
// object (if any) in the current thread or NULL. // object (if any) in the current thread or NULL.
GTEST_API_ extern ThreadLocal<Sequence*> g_gmock_implicit_sequence; GTEST_API_ ThreadLocal<Sequence*>& GetGMockImplicitSequence(void);
// Base class for implementing expectations. // Base class for implementing expectations.
// //
@ -1580,7 +1580,7 @@ class FunctionMocker<R(Args...)> final : public UntypedFunctionMockerBase {
untyped_expectations_.push_back(untyped_expectation); untyped_expectations_.push_back(untyped_expectation);
// Adds this expectation into the implicit sequence if there is one. // Adds this expectation into the implicit sequence if there is one.
Sequence* const implicit_sequence = g_gmock_implicit_sequence.get(); Sequence* const implicit_sequence = GetGMockImplicitSequence().get();
if (implicit_sequence != nullptr) { if (implicit_sequence != nullptr) {
implicit_sequence->AddExpectation(Expectation(untyped_expectation)); implicit_sequence->AddExpectation(Expectation(untyped_expectation));
} }

View File

@ -277,7 +277,12 @@ void ExpectationBase::UntypedTimes(const Cardinality& a_cardinality) {
// Points to the implicit sequence introduced by a living InSequence // Points to the implicit sequence introduced by a living InSequence
// object (if any) in the current thread or NULL. // object (if any) in the current thread or NULL.
GTEST_API_ ThreadLocal<Sequence*> g_gmock_implicit_sequence;
GTEST_API_ ThreadLocal<Sequence*>& GetGMockImplicitSequence(void)
{
static ThreadLocal<Sequence*> implicit_sequence;
return implicit_sequence;
}
// Reports an uninteresting call (whose description is in msg) in the // Reports an uninteresting call (whose description is in msg) in the
// manner specified by 'reaction'. // manner specified by 'reaction'.
@ -768,8 +773,8 @@ void Sequence::AddExpectation(const Expectation& expectation) const {
// Creates the implicit sequence if there isn't one. // Creates the implicit sequence if there isn't one.
InSequence::InSequence() { InSequence::InSequence() {
if (internal::g_gmock_implicit_sequence.get() == nullptr) { if (internal::GetGMockImplicitSequence().get() == nullptr) {
internal::g_gmock_implicit_sequence.set(new Sequence); internal::GetGMockImplicitSequence().set(new Sequence);
sequence_created_ = true; sequence_created_ = true;
} else { } else {
sequence_created_ = false; sequence_created_ = false;
@ -780,8 +785,8 @@ InSequence::InSequence() {
// of this object. // of this object.
InSequence::~InSequence() { InSequence::~InSequence() {
if (sequence_created_) { if (sequence_created_) {
delete internal::g_gmock_implicit_sequence.get(); delete internal::GetGMockImplicitSequence().get();
internal::g_gmock_implicit_sequence.set(nullptr); internal::GetGMockImplicitSequence().set(nullptr);
} }
} }