Chromium Code Reviews| Index: base/synchronization/lock_impl_posix.cc |
| diff --git a/base/synchronization/lock_impl_posix.cc b/base/synchronization/lock_impl_posix.cc |
| index 5619adaf5d829df72f93fd57b0687d551fb9390d..90c3fa8cc3cfea55386f4698f2d9ac768a61d7da 100644 |
| --- a/base/synchronization/lock_impl_posix.cc |
| +++ b/base/synchronization/lock_impl_posix.cc |
| @@ -8,26 +8,35 @@ |
| #include <string.h> |
| #include "base/logging.h" |
| +#include "base/synchronization/lock.h" |
| +#include "base/version.h" |
| + |
| +#if defined(OS_LINUX) |
| +#include <gnu/libc-version.h> |
| +#endif |
| namespace base { |
| namespace internal { |
| LockImpl::LockImpl() { |
| -#ifndef NDEBUG |
| - // In debug, setup attributes for lock error checking. |
| pthread_mutexattr_t mta; |
| int rv = pthread_mutexattr_init(&mta); |
| DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| +#if PRIORITY_INHERITANCE_LOCKS_POSSIBLE() |
| + if (PriorityInheritanceAvailable()) { |
| + rv = pthread_mutexattr_setprotocol(&mta, PTHREAD_PRIO_INHERIT); |
| + } |
| + DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| +#endif |
| +#ifndef NDEBUG |
| + // In debug, setup attributes for lock error checking. |
| rv = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK); |
| DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| +#endif |
| rv = pthread_mutex_init(&native_handle_, &mta); |
| DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| rv = pthread_mutexattr_destroy(&mta); |
| DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| -#else |
| - // In release, go with the default lock attributes. |
| - pthread_mutex_init(&native_handle_, NULL); |
| -#endif |
| } |
| LockImpl::~LockImpl() { |
| @@ -51,5 +60,27 @@ void LockImpl::Unlock() { |
| DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| } |
| +// static |
| +bool LockImpl::PriorityInheritanceAvailable() { |
| +#if defined(OS_LINUX) |
| + // Due to glibc Bug 14652, priority inheritance mutexes may deadlock with |
| + // condition variables during recacquisition of the mutex after the condition |
| + // variable is signalled. This bug was fixed in glibc 2.17. |
| + Version version(gnu_get_libc_version()); |
| + if (!version.IsValid() || version.components().size() != 2) |
| + return false; |
| + |
| + uint32_t glibc_major_version = version.components()[0]; |
| + uint32_t glibc_minor_version = version.components()[1]; |
| + |
| + if (glibc_major_version < 2) |
| + return false; |
| + |
| + return glibc_major_version != 2 || glibc_minor_version >= 17; |
|
danakj
2016/06/02 21:59:21
You can use std::tie to combine these into one a <
robliao
2016/06/02 22:17:48
Neat! Done.
|
| +#else |
| + return true; |
| +#endif |
| +} |
| + |
| } // namespace internal |
| } // namespace base |