From 2dec3d7021fca57721ca7cd973d63ef817557aee Mon Sep 17 00:00:00 2001 From: Yabin Cui Date: Fri, 2 Feb 2018 15:45:24 -0800 Subject: [PATCH] Avoid abort when calling pthread_mutex_destroy more than once. Bug: http://b/72878088 Test: run bionic-unit-tests. Change-Id: I0c3a6c5a625d187d5f32ec8c821cfdd5e807a671 --- libc/bionic/pthread_mutex.cpp | 5 +++++ tests/pthread_test.cpp | 1 + 2 files changed, 6 insertions(+) diff --git a/libc/bionic/pthread_mutex.cpp b/libc/bionic/pthread_mutex.cpp index e5f2a31bf..f1f72944d 100644 --- a/libc/bionic/pthread_mutex.cpp +++ b/libc/bionic/pthread_mutex.cpp @@ -943,11 +943,16 @@ int pthread_mutex_timedlock(pthread_mutex_t* mutex_interface, const timespec* ab int pthread_mutex_destroy(pthread_mutex_t* mutex_interface) { pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface); uint16_t old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed); + if (old_state == 0xffff) { + // The mutex has been destroyed. + return EBUSY; + } uint16_t mtype = (old_state & MUTEX_TYPE_MASK); if (mtype == MUTEX_TYPE_BITS_WITH_PI) { int result = PIMutexDestroy(mutex->ToPIMutex()); if (result == 0) { mutex->FreePIMutex(); + atomic_store(&mutex->state, 0xffff); } return result; } diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp index 40059d027..8ac522344 100644 --- a/tests/pthread_test.cpp +++ b/tests/pthread_test.cpp @@ -1672,6 +1672,7 @@ struct PthreadMutex { void destroy() { ASSERT_EQ(0, pthread_mutex_destroy(&lock)); + ASSERT_EQ(EBUSY, pthread_mutex_destroy(&lock)); } DISALLOW_COPY_AND_ASSIGN(PthreadMutex);