Merge "PIMutexUnlock: load owner_tid in non-common case"

This commit is contained in:
Ryan Prichard 2019-04-22 21:09:52 +00:00 committed by Gerrit Code Review
commit b250766282
2 changed files with 20 additions and 0 deletions

View file

@ -199,6 +199,8 @@ static int PIMutexUnlock(PIMutex& mutex) {
memory_order_relaxed))) {
return 0;
}
} else {
old_owner = atomic_load_explicit(&mutex.owner_tid, memory_order_relaxed);
}
if (tid != (old_owner & FUTEX_TID_MASK)) {

View file

@ -1843,10 +1843,25 @@ struct PthreadMutex {
DISALLOW_COPY_AND_ASSIGN(PthreadMutex);
};
static int UnlockFromAnotherThread(pthread_mutex_t* mutex) {
pthread_t thread;
pthread_create(&thread, nullptr, [](void* mutex_voidp) -> void* {
pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(mutex_voidp);
intptr_t result = pthread_mutex_unlock(mutex);
return reinterpret_cast<void*>(result);
}, mutex);
void* result;
EXPECT_EQ(0, pthread_join(thread, &result));
return reinterpret_cast<intptr_t>(result);
};
static void TestPthreadMutexLockNormal(int protocol) {
PthreadMutex m(PTHREAD_MUTEX_NORMAL, protocol);
ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
if (protocol == PTHREAD_PRIO_INHERIT) {
ASSERT_EQ(EPERM, UnlockFromAnotherThread(&m.lock));
}
ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
ASSERT_EQ(EBUSY, pthread_mutex_trylock(&m.lock));
@ -1857,6 +1872,7 @@ static void TestPthreadMutexLockErrorCheck(int protocol) {
PthreadMutex m(PTHREAD_MUTEX_ERRORCHECK, protocol);
ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
ASSERT_EQ(EPERM, UnlockFromAnotherThread(&m.lock));
ASSERT_EQ(EDEADLK, pthread_mutex_lock(&m.lock));
ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));
@ -1873,7 +1889,9 @@ static void TestPthreadMutexLockRecursive(int protocol) {
PthreadMutex m(PTHREAD_MUTEX_RECURSIVE, protocol);
ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
ASSERT_EQ(EPERM, UnlockFromAnotherThread(&m.lock));
ASSERT_EQ(0, pthread_mutex_lock(&m.lock));
ASSERT_EQ(EPERM, UnlockFromAnotherThread(&m.lock));
ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
ASSERT_EQ(0, pthread_mutex_unlock(&m.lock));
ASSERT_EQ(0, pthread_mutex_trylock(&m.lock));