Fix a null initializer for pthread_t in GTest's Mutex.

Static (linker-initialized) global mutex has a
pthread_t member, which is currently initialized
with 0. This is incorrect, since pthread_t is
an opaque type, which can be a struct in some
libc implementations (e.g. in LLVM-libc). Using
POSIX-specified PTHREAD_NULL is a better option,
however it may not be available on older platforms.
So, use PTHREAD_NULL if it's available, but
fallback to the original behavior for compatibility.

PiperOrigin-RevId: 979239391
Change-Id: I79a292868f85f62bb1962b3ac956ad40eb145907
This commit is contained in:
Abseil Team 2026-09-10 09:46:43 -07:00 committed by Copybara-Service
parent dd0a37b843
commit 8eff9e3366

View File

@ -1755,14 +1755,16 @@ class [[nodiscard]] MutexBase {
#define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
extern ::testing::internal::MutexBase mutex
#if defined(PTHREAD_NULL)
#define GTEST_INTERNAL_PTHREAD_NULL PTHREAD_NULL
#else
#define GTEST_INTERNAL_PTHREAD_NULL (pthread_t{})
#endif
// Defines and statically (i.e. at link time) initializes a static mutex.
// The initialization list here does not explicitly initialize each field,
// instead relying on default initialization for the unspecified fields. In
// particular, the owner_ field (a pthread_t) is not explicitly initialized.
// This allows initialization to work whether pthread_t is a scalar or struct.
// The flag -Wmissing-field-initializers must not be specified for this to work.
#define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
::testing::internal::MutexBase mutex = {PTHREAD_MUTEX_INITIALIZER, false, 0}
#define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
::testing::internal::MutexBase mutex = {PTHREAD_MUTEX_INITIALIZER, false, \
GTEST_INTERNAL_PTHREAD_NULL}
// The Mutex class can only be used for mutexes created at runtime. It
// shares its API with MutexBase otherwise.