| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "mojo/edk/system/mutex.h" | 5 #include "mojo/edk/util/mutex.h" |
| 6 | 6 |
| 7 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) | 7 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
| 8 #include <errno.h> | 8 #include <errno.h> |
| 9 #include <string.h> | |
| 10 | 9 |
| 11 #include "base/logging.h" | 10 #include "mojo/edk/util/logging_internal.h" |
| 12 | 11 |
| 13 namespace mojo { | 12 namespace mojo { |
| 14 namespace system { | 13 namespace util { |
| 15 | 14 |
| 16 Mutex::Mutex() { | 15 Mutex::Mutex() { |
| 17 pthread_mutexattr_t attr; | 16 pthread_mutexattr_t attr; |
| 18 int error = pthread_mutexattr_init(&attr); | 17 int error = pthread_mutexattr_init(&attr); |
| 19 DCHECK(!error) << "pthread_mutexattr_init: " << strerror(error); | 18 INTERNAL_DCHECK_WITH_ERRNO(!error, "pthread_mutexattr_init", error); |
| 20 error = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); | 19 error = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); |
| 21 DCHECK(!error) << "pthread_mutexattr_settype: " << strerror(error); | 20 INTERNAL_DCHECK_WITH_ERRNO(!error, "pthread_mutexattr_settype", error); |
| 22 error = pthread_mutex_init(&impl_, &attr); | 21 error = pthread_mutex_init(&impl_, &attr); |
| 23 DCHECK(!error) << "pthread_mutex_init: " << strerror(error); | 22 INTERNAL_DCHECK_WITH_ERRNO(!error, "pthread_mutex_init", error); |
| 24 error = pthread_mutexattr_destroy(&attr); | 23 error = pthread_mutexattr_destroy(&attr); |
| 25 DCHECK(!error) << "pthread_mutexattr_destroy: " << strerror(error); | 24 INTERNAL_DCHECK_WITH_ERRNO(!error, "pthread_mutexattr_destroy", error); |
| 26 } | 25 } |
| 27 | 26 |
| 28 Mutex::~Mutex() { | 27 Mutex::~Mutex() { |
| 29 int error = pthread_mutex_destroy(&impl_); | 28 int error = pthread_mutex_destroy(&impl_); |
| 30 DCHECK(!error) << "pthread_mutex_destroy: " << strerror(error); | 29 INTERNAL_DCHECK_WITH_ERRNO(!error, "pthread_mutex_destroy", error); |
| 31 } | 30 } |
| 32 | 31 |
| 33 void Mutex::Lock() MOJO_EXCLUSIVE_LOCK_FUNCTION() { | 32 void Mutex::Lock() MOJO_EXCLUSIVE_LOCK_FUNCTION() { |
| 34 int error = pthread_mutex_lock(&impl_); | 33 int error = pthread_mutex_lock(&impl_); |
| 35 DCHECK(!error) << "pthread_mutex_lock: " << strerror(error); | 34 INTERNAL_DCHECK_WITH_ERRNO(!error, "pthread_mutex_lock", error); |
| 36 } | 35 } |
| 37 | 36 |
| 38 void Mutex::Unlock() MOJO_UNLOCK_FUNCTION() { | 37 void Mutex::Unlock() MOJO_UNLOCK_FUNCTION() { |
| 39 int error = pthread_mutex_unlock(&impl_); | 38 int error = pthread_mutex_unlock(&impl_); |
| 40 DCHECK(!error) << "pthread_mutex_unlock: " << strerror(error); | 39 INTERNAL_DCHECK_WITH_ERRNO(!error, "pthread_mutex_unlock", error); |
| 41 } | 40 } |
| 42 | 41 |
| 43 bool Mutex::TryLock() MOJO_EXCLUSIVE_TRYLOCK_FUNCTION(true) { | 42 bool Mutex::TryLock() MOJO_EXCLUSIVE_TRYLOCK_FUNCTION(true) { |
| 44 int error = pthread_mutex_trylock(&impl_); | 43 int error = pthread_mutex_trylock(&impl_); |
| 45 DCHECK(!error || error == EBUSY) << "pthread_mutex_trylock: " | 44 INTERNAL_DCHECK_WITH_ERRNO(!error || error == EBUSY, "pthread_mutex_trylock", |
| 46 << strerror(error); | 45 error); |
| 47 return !error; | 46 return !error; |
| 48 } | 47 } |
| 49 | 48 |
| 50 void Mutex::AssertHeld() MOJO_ASSERT_EXCLUSIVE_LOCK() { | 49 void Mutex::AssertHeld() MOJO_ASSERT_EXCLUSIVE_LOCK() { |
| 51 int error = pthread_mutex_lock(&impl_); | 50 int error = pthread_mutex_lock(&impl_); |
| 52 DCHECK_EQ(error, EDEADLK) << ". pthread_mutex_lock: " << strerror(error); | 51 INTERNAL_DCHECK_WITH_ERRNO(error == EDEADLK, "pthread_mutex_lock", error); |
| 53 } | 52 } |
| 54 | 53 |
| 55 } // namespace system | 54 } // namespace util |
| 56 } // namespace mojo | 55 } // namespace mojo |
| 57 | 56 |
| 58 #endif // !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) | 57 #endif // !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
| OLD | NEW |