OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/edk/system/mutex.h" | |
6 | |
7 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) | |
8 #include <errno.h> | |
9 #include <string.h> | |
10 | |
11 #include "base/logging.h" | |
12 | |
13 namespace mojo { | |
14 namespace system { | |
15 | |
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); | |
26 } | |
27 | |
28 Mutex::~Mutex() { | |
29 int error = pthread_mutex_destroy(&impl_); | |
30 DCHECK(!error) << "pthread_mutex_destroy: " << strerror(error); | |
31 } | |
32 | |
33 void Mutex::Lock() MOJO_EXCLUSIVE_LOCK_FUNCTION() { | |
34 int error = pthread_mutex_lock(&impl_); | |
35 DCHECK(!error) << "pthread_mutex_lock: " << strerror(error); | |
36 } | |
37 | |
38 void Mutex::Unlock() MOJO_UNLOCK_FUNCTION() { | |
39 int error = pthread_mutex_unlock(&impl_); | |
40 DCHECK(!error) << "pthread_mutex_unlock: " << strerror(error); | |
41 } | |
42 | |
43 bool Mutex::TryLock() MOJO_EXCLUSIVE_TRYLOCK_FUNCTION(true) { | |
44 int error = pthread_mutex_trylock(&impl_); | |
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); | |
53 } | |
54 | |
55 } // namespace system | |
56 } // namespace mojo | |
57 | |
58 #endif // !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) | |
OLD | NEW |