Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/synchronization/lock_impl.h" | 5 #include "base/synchronization/lock_impl.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/synchronization/lock.h" | |
| 12 #include "base/version.h" | |
| 13 | |
| 14 #if defined(OS_LINUX) | |
| 15 #include <gnu/libc-version.h> | |
| 16 #endif | |
| 11 | 17 |
| 12 namespace base { | 18 namespace base { |
| 13 namespace internal { | 19 namespace internal { |
| 14 | 20 |
| 15 LockImpl::LockImpl() { | 21 LockImpl::LockImpl() { |
| 16 #ifndef NDEBUG | |
| 17 // In debug, setup attributes for lock error checking. | |
| 18 pthread_mutexattr_t mta; | 22 pthread_mutexattr_t mta; |
| 19 int rv = pthread_mutexattr_init(&mta); | 23 int rv = pthread_mutexattr_init(&mta); |
| 20 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 24 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| 25 #if PRIORITY_INHERITANCE_LOCKS_POSSIBLE() | |
| 26 if (PriorityInheritanceAvailable()) { | |
| 27 rv = pthread_mutexattr_setprotocol(&mta, PTHREAD_PRIO_INHERIT); | |
| 28 } | |
| 29 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | |
| 30 #endif | |
| 31 #ifndef NDEBUG | |
| 32 // In debug, setup attributes for lock error checking. | |
| 21 rv = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK); | 33 rv = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK); |
| 22 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 34 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| 35 #endif | |
| 23 rv = pthread_mutex_init(&native_handle_, &mta); | 36 rv = pthread_mutex_init(&native_handle_, &mta); |
| 24 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 37 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| 25 rv = pthread_mutexattr_destroy(&mta); | 38 rv = pthread_mutexattr_destroy(&mta); |
| 26 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 39 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| 27 #else | |
| 28 // In release, go with the default lock attributes. | |
| 29 pthread_mutex_init(&native_handle_, NULL); | |
| 30 #endif | |
| 31 } | 40 } |
| 32 | 41 |
| 33 LockImpl::~LockImpl() { | 42 LockImpl::~LockImpl() { |
| 34 int rv = pthread_mutex_destroy(&native_handle_); | 43 int rv = pthread_mutex_destroy(&native_handle_); |
| 35 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 44 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| 36 } | 45 } |
| 37 | 46 |
| 38 bool LockImpl::Try() { | 47 bool LockImpl::Try() { |
| 39 int rv = pthread_mutex_trylock(&native_handle_); | 48 int rv = pthread_mutex_trylock(&native_handle_); |
| 40 DCHECK(rv == 0 || rv == EBUSY) << ". " << strerror(rv); | 49 DCHECK(rv == 0 || rv == EBUSY) << ". " << strerror(rv); |
| 41 return rv == 0; | 50 return rv == 0; |
| 42 } | 51 } |
| 43 | 52 |
| 44 void LockImpl::Lock() { | 53 void LockImpl::Lock() { |
| 45 int rv = pthread_mutex_lock(&native_handle_); | 54 int rv = pthread_mutex_lock(&native_handle_); |
| 46 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 55 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| 47 } | 56 } |
| 48 | 57 |
| 49 void LockImpl::Unlock() { | 58 void LockImpl::Unlock() { |
| 50 int rv = pthread_mutex_unlock(&native_handle_); | 59 int rv = pthread_mutex_unlock(&native_handle_); |
| 51 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 60 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| 52 } | 61 } |
| 53 | 62 |
| 63 // static | |
| 64 bool LockImpl::PriorityInheritanceAvailable() { | |
| 65 #if defined(OS_LINUX) | |
| 66 // Due to glibc Bug 14652, priority inheritance mutexes may deadlock with | |
| 67 // condition variables during recacquisition of the mutex after the condition | |
| 68 // variable is signalled. This bug was fixed in glibc 2.17. | |
| 69 Version version(gnu_get_libc_version()); | |
| 70 if (!version.IsValid() || version.components().size() != 2) | |
| 71 return false; | |
| 72 | |
| 73 uint32_t glibc_major_version = version.components()[0]; | |
| 74 uint32_t glibc_minor_version = version.components()[1]; | |
| 75 | |
| 76 if (glibc_major_version < 2) | |
| 77 return false; | |
| 78 | |
| 79 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.
| |
| 80 #else | |
| 81 return true; | |
| 82 #endif | |
| 83 } | |
| 84 | |
| 54 } // namespace internal | 85 } // namespace internal |
| 55 } // namespace base | 86 } // namespace base |
| OLD | NEW |