Skip to content

Commit 361c8fb

Browse files
Added support for platforms where pthread_t is a struct rather than an integral
type. git-svn-id: http://googletest.googlecode.com/svn/trunk@613 861a406c-534a-0410-8894-cb66d6ee9925
1 parent 7a574c9 commit 361c8fb

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

cmake/internal_utils.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ macro(config_compiler_and_linker)
7979
# whether RTTI is enabled. Therefore we define GTEST_HAS_RTTI
8080
# explicitly.
8181
set(cxx_no_rtti_flags "-fno-rtti -DGTEST_HAS_RTTI=0")
82-
set(cxx_strict_flags "-Wextra")
82+
set(cxx_strict_flags
83+
"-Wextra -Wno-unused-parameter -Wno-missing-field-initializers")
8384
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "SunPro")
8485
set(cxx_exception_flags "-features=except")
8586
# Sun Pro doesn't provide macros to indicate whether exceptions and

include/gtest/internal/gtest-port.h

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,21 +1240,23 @@ class MutexBase {
12401240
void Lock() {
12411241
GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_));
12421242
owner_ = pthread_self();
1243+
has_owner_ = true;
12431244
}
12441245

12451246
// Releases this mutex.
12461247
void Unlock() {
1247-
// We don't protect writing to owner_ here, as it's the caller's
1248-
// responsibility to ensure that the current thread holds the
1248+
// Since the lock is being released the owner_ field should no longer be
1249+
// considered valid. We don't protect writing to has_owner_ here, as it's
1250+
// the caller's responsibility to ensure that the current thread holds the
12491251
// mutex when this is called.
1250-
owner_ = 0;
1252+
has_owner_ = false;
12511253
GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_));
12521254
}
12531255

12541256
// Does nothing if the current thread holds the mutex. Otherwise, crashes
12551257
// with high probability.
12561258
void AssertHeld() const {
1257-
GTEST_CHECK_(owner_ == pthread_self())
1259+
GTEST_CHECK_(has_owner_ && pthread_equal(owner_, pthread_self()))
12581260
<< "The current thread is not holding the mutex @" << this;
12591261
}
12601262

@@ -1265,24 +1267,36 @@ class MutexBase {
12651267
// have to be public.
12661268
public:
12671269
pthread_mutex_t mutex_; // The underlying pthread mutex.
1268-
pthread_t owner_; // The thread holding the mutex; 0 means no one holds it.
1270+
// has_owner_ indicates whether the owner_ field below contains a valid thread
1271+
// ID and is therefore safe to inspect (e.g., to use in pthread_equal()). All
1272+
// accesses to the owner_ field should be protected by a check of this field.
1273+
// An alternative might be to memset() owner_ to all zeros, but there's no
1274+
// guarantee that a zero'd pthread_t is necessarily invalid or even different
1275+
// from pthread_self().
1276+
bool has_owner_;
1277+
pthread_t owner_; // The thread holding the mutex.
12691278
};
12701279

12711280
// Forward-declares a static mutex.
12721281
# define GTEST_DECLARE_STATIC_MUTEX_(mutex) \
12731282
extern ::testing::internal::MutexBase mutex
12741283

12751284
// Defines and statically (i.e. at link time) initializes a static mutex.
1285+
// The initialization list here does not explicitly initialize each field,
1286+
// instead relying on default initialization for the unspecified fields. In
1287+
// particular, the owner_ field (a pthread_t) is not explicitly initialized.
1288+
// This allows initialization to work whether pthread_t is a scalar or struct.
1289+
// The flag -Wmissing-field-initializers must not be specified for this to work.
12761290
# define GTEST_DEFINE_STATIC_MUTEX_(mutex) \
1277-
::testing::internal::MutexBase mutex = { PTHREAD_MUTEX_INITIALIZER, 0 }
1291+
::testing::internal::MutexBase mutex = { PTHREAD_MUTEX_INITIALIZER, false }
12781292

12791293
// The Mutex class can only be used for mutexes created at runtime. It
12801294
// shares its API with MutexBase otherwise.
12811295
class Mutex : public MutexBase {
12821296
public:
12831297
Mutex() {
12841298
GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, NULL));
1285-
owner_ = 0;
1299+
has_owner_ = false;
12861300
}
12871301
~Mutex() {
12881302
GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_));

0 commit comments

Comments
 (0)