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