| 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/debug/activity_tracker.h" | 10 #include "base/debug/activity_tracker.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
| 13 | 13 |
| 14 #if defined(OS_MACOSX) |
| 15 #include <pthread_spis.h> |
| 16 #endif |
| 17 |
| 14 namespace base { | 18 namespace base { |
| 15 namespace internal { | 19 namespace internal { |
| 16 | 20 |
| 17 // Determines which platforms can consider using priority inheritance locks. Use | 21 // Determines which platforms can consider using priority inheritance locks. Use |
| 18 // this define for platform code that may not compile if priority inheritance | 22 // this define for platform code that may not compile if priority inheritance |
| 19 // locks aren't available. For this platform code, | 23 // locks aren't available. For this platform code, |
| 20 // PRIORITY_INHERITANCE_LOCKS_POSSIBLE() is a necessary but insufficient check. | 24 // PRIORITY_INHERITANCE_LOCKS_POSSIBLE() is a necessary but insufficient check. |
| 21 // Lock::PriorityInheritanceAvailable still must be checked as the code may | 25 // Lock::PriorityInheritanceAvailable still must be checked as the code may |
| 22 // compile but the underlying platform still may not correctly support priority | 26 // compile but the underlying platform still may not correctly support priority |
| 23 // inheritance locks. | 27 // inheritance locks. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 35 if (PriorityInheritanceAvailable()) { | 39 if (PriorityInheritanceAvailable()) { |
| 36 rv = pthread_mutexattr_setprotocol(&mta, PTHREAD_PRIO_INHERIT); | 40 rv = pthread_mutexattr_setprotocol(&mta, PTHREAD_PRIO_INHERIT); |
| 37 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 41 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| 38 } | 42 } |
| 39 #endif | 43 #endif |
| 40 #ifndef NDEBUG | 44 #ifndef NDEBUG |
| 41 // In debug, setup attributes for lock error checking. | 45 // In debug, setup attributes for lock error checking. |
| 42 rv = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK); | 46 rv = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK); |
| 43 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 47 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| 44 #endif | 48 #endif |
| 49 #if defined(OS_MACOSX) |
| 50 // macOS defaults to fair locks which can be orders of magnitude slower in the |
| 51 // contention case, so default to first fit, which avoids queuing. |
| 52 pthread_mutexattr_setpolicy_np(&mta, _PTHREAD_MUTEX_POLICY_FIRSTFIT); |
| 53 #endif |
| 45 rv = pthread_mutex_init(&native_handle_, &mta); | 54 rv = pthread_mutex_init(&native_handle_, &mta); |
| 46 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 55 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| 47 rv = pthread_mutexattr_destroy(&mta); | 56 rv = pthread_mutexattr_destroy(&mta); |
| 48 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 57 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| 49 } | 58 } |
| 50 | 59 |
| 51 LockImpl::~LockImpl() { | 60 LockImpl::~LockImpl() { |
| 52 int rv = pthread_mutex_destroy(&native_handle_); | 61 int rv = pthread_mutex_destroy(&native_handle_); |
| 53 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 62 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| 54 } | 63 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 // Fixed in glibc 2.17. | 98 // Fixed in glibc 2.17. |
| 90 // Priority inheritance mutexes may deadlock with condition variables | 99 // Priority inheritance mutexes may deadlock with condition variables |
| 91 // during recacquisition of the mutex after the condition variable is | 100 // during recacquisition of the mutex after the condition variable is |
| 92 // signalled. | 101 // signalled. |
| 93 return false; | 102 return false; |
| 94 #endif | 103 #endif |
| 95 } | 104 } |
| 96 | 105 |
| 97 } // namespace internal | 106 } // namespace internal |
| 98 } // namespace base | 107 } // namespace base |
| OLD | NEW |