Remove strict tid checks from the mac implementation of BlockingMutex

Summary:
This patch unifies the behavior of BlockingMutex on linux and mac,
resolving problems that can arise when BlockingMutex is used in
code shared by the two platforms but has different behavior depending
on the platform.

No longer requires that the calling thread own the mutex for
CheckLocked calls to pass.

Reviewers: dvyukov, kubamracek

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D29728

llvm-svn: 294614
This commit is contained in:
Francis Ricci 2017-02-09 19:29:11 +00:00
parent 60437620db
commit 2f1f1616ae
2 changed files with 10 additions and 6 deletions

View File

@ -348,20 +348,16 @@ BlockingMutex::BlockingMutex() {
void BlockingMutex::Lock() {
CHECK(sizeof(OSSpinLock) <= sizeof(opaque_storage_));
CHECK_EQ(OS_SPINLOCK_INIT, 0);
CHECK_NE(owner_, (uptr)pthread_self());
CHECK_EQ(owner_, 0);
OSSpinLockLock((OSSpinLock*)&opaque_storage_);
CHECK(!owner_);
owner_ = (uptr)pthread_self();
}
void BlockingMutex::Unlock() {
CHECK(owner_ == (uptr)pthread_self());
owner_ = 0;
OSSpinLockUnlock((OSSpinLock*)&opaque_storage_);
}
void BlockingMutex::CheckLocked() {
CHECK_EQ((uptr)pthread_self(), owner_);
CHECK_NE(*(OSSpinLock*)&opaque_storage_, 0);
}
u64 NanoTime() {

View File

@ -83,6 +83,14 @@ class BlockingMutex {
BlockingMutex();
void Lock();
void Unlock();
// This function does not guarantee an explicit check that the calling thread
// is the thread which owns the mutex. This behavior, while more strictly
// correct, causes problems in cases like StopTheWorld, where a parent thread
// owns the mutex but a child checks that it is locked. Rather than
// maintaining complex state to work around those situations, the check only
// checks that the mutex is owned, and assumes callers to be generally
// well-behaved.
void CheckLocked();
private:
uptr opaque_storage_[10];