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" | |
| 11 | 12 |
| 12 namespace base { | 13 namespace base { |
| 13 namespace internal { | 14 namespace internal { |
| 14 | 15 |
| 15 LockImpl::LockImpl() { | 16 LockImpl::LockImpl() { |
| 16 #ifndef NDEBUG | |
| 17 // In debug, setup attributes for lock error checking. | |
| 18 pthread_mutexattr_t mta; | 17 pthread_mutexattr_t mta; |
| 19 int rv = pthread_mutexattr_init(&mta); | 18 int rv = pthread_mutexattr_init(&mta); |
| 20 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 19 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| 20 #if PRIORITY_INHERITANCE_LOCKS_POSSIBLE() | |
| 21 if (PriorityInheritanceAvailable()) { | |
| 22 rv = pthread_mutexattr_setprotocol(&mta, PTHREAD_PRIO_INHERIT); | |
| 23 } | |
| 24 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | |
| 25 #endif | |
| 26 #ifndef NDEBUG | |
| 27 // In debug, setup attributes for lock error checking. | |
| 21 rv = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK); | 28 rv = pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_ERRORCHECK); |
| 22 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 29 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| 30 #endif | |
| 23 rv = pthread_mutex_init(&native_handle_, &mta); | 31 rv = pthread_mutex_init(&native_handle_, &mta); |
| 24 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 32 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| 25 rv = pthread_mutexattr_destroy(&mta); | 33 rv = pthread_mutexattr_destroy(&mta); |
| 26 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 34 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 } | 35 } |
| 32 | 36 |
| 33 LockImpl::~LockImpl() { | 37 LockImpl::~LockImpl() { |
| 34 int rv = pthread_mutex_destroy(&native_handle_); | 38 int rv = pthread_mutex_destroy(&native_handle_); |
| 35 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 39 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| 36 } | 40 } |
| 37 | 41 |
| 38 bool LockImpl::Try() { | 42 bool LockImpl::Try() { |
| 39 int rv = pthread_mutex_trylock(&native_handle_); | 43 int rv = pthread_mutex_trylock(&native_handle_); |
| 40 DCHECK(rv == 0 || rv == EBUSY) << ". " << strerror(rv); | 44 DCHECK(rv == 0 || rv == EBUSY) << ". " << strerror(rv); |
| 41 return rv == 0; | 45 return rv == 0; |
| 42 } | 46 } |
| 43 | 47 |
| 44 void LockImpl::Lock() { | 48 void LockImpl::Lock() { |
| 45 int rv = pthread_mutex_lock(&native_handle_); | 49 int rv = pthread_mutex_lock(&native_handle_); |
| 46 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 50 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| 47 } | 51 } |
| 48 | 52 |
| 49 void LockImpl::Unlock() { | 53 void LockImpl::Unlock() { |
| 50 int rv = pthread_mutex_unlock(&native_handle_); | 54 int rv = pthread_mutex_unlock(&native_handle_); |
| 51 DCHECK_EQ(rv, 0) << ". " << strerror(rv); | 55 DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| 52 } | 56 } |
| 53 | 57 |
| 58 // static | |
| 59 bool LockImpl::PriorityInheritanceAvailable() { | |
|
gab
2016/07/22 20:07:31
There's no longer anything run-time about this met
robliao
2016/07/25 20:53:19
This future-proofs lock against the known runtime
gab
2016/07/26 17:31:27
Ok, can you add a TODO here then to indicate the i
gab
2016/07/26 17:32:11
Nvm, see you addressed this already with other com
| |
| 60 #if PRIORITY_INHERITANCE_LOCKS_POSSIBLE() && defined(OS_MACOSX) | |
|
gab
2016/07/22 20:07:31
Please add a comment explaining why it doesn't wor
robliao
2016/07/25 20:53:19
Done.
| |
| 61 return true; | |
| 62 #else | |
| 63 return false; | |
| 64 #endif | |
| 65 } | |
| 66 | |
| 54 } // namespace internal | 67 } // namespace internal |
| 55 } // namespace base | 68 } // namespace base |
| OLD | NEW |