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 // A mutex class, with support for thread annotations. | 5 // A mutex class, with support for thread annotations. |
6 // | 6 // |
7 // TODO(vtl): Currently, this is a fork of Chromium's | 7 // TODO(vtl): Add support for non-exclusive (reader) locks. |
8 // base/synchronization/lock.h (with names changed and minor modifications; it | |
9 // still cheats and uses Chromium's lock_impl.*), but eventually we'll want our | |
10 // own and, e.g., add support for non-exclusive (reader) locks. | |
11 | 8 |
12 #ifndef MOJO_EDK_SYSTEM_MUTEX_H_ | 9 #ifndef MOJO_EDK_SYSTEM_MUTEX_H_ |
13 #define MOJO_EDK_SYSTEM_MUTEX_H_ | 10 #define MOJO_EDK_SYSTEM_MUTEX_H_ |
14 | 11 |
15 #include "base/synchronization/lock_impl.h" | 12 #include <pthread.h> |
16 #include "base/threading/platform_thread.h" | 13 |
17 #include "mojo/edk/system/thread_annotations.h" | 14 #include "mojo/edk/system/thread_annotations.h" |
18 #include "mojo/public/cpp/system/macros.h" | 15 #include "mojo/public/cpp/system/macros.h" |
19 | 16 |
20 namespace mojo { | 17 namespace mojo { |
21 namespace system { | 18 namespace system { |
22 | 19 |
23 // Mutex ----------------------------------------------------------------------- | 20 // Mutex ----------------------------------------------------------------------- |
24 | 21 |
25 class MOJO_LOCKABLE Mutex { | 22 class MOJO_LOCKABLE Mutex { |
26 public: | 23 public: |
27 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | 24 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
28 Mutex() : lock_() {} | 25 Mutex() { pthread_mutex_init(&impl_, nullptr); } |
29 ~Mutex() {} | 26 ~Mutex() { pthread_mutex_destroy(&impl_); } |
30 | 27 |
31 void Lock() MOJO_EXCLUSIVE_LOCK_FUNCTION() { lock_.Lock(); } | 28 // Takes an exclusive lock. |
32 void Unlock() MOJO_UNLOCK_FUNCTION() { lock_.Unlock(); } | 29 void Lock() MOJO_EXCLUSIVE_LOCK_FUNCTION() { pthread_mutex_lock(&impl_); } |
33 | 30 |
34 bool TryLock() MOJO_EXCLUSIVE_TRYLOCK_FUNCTION(true) { return lock_.Try(); } | 31 // Releases a lock. |
| 32 void Unlock() MOJO_UNLOCK_FUNCTION() { pthread_mutex_unlock(&impl_); } |
35 | 33 |
36 void AssertHeld() const MOJO_ASSERT_EXCLUSIVE_LOCK() {} | 34 // Tries to take an exclusive lock, returning true if successful. |
| 35 bool TryLock() MOJO_EXCLUSIVE_TRYLOCK_FUNCTION(true) { |
| 36 return !pthread_mutex_trylock(&impl_); |
| 37 } |
| 38 |
| 39 // Asserts that an exclusive lock is held by the calling thread. (Does nothing |
| 40 // for non-Debug builds.) |
| 41 void AssertHeld() MOJO_ASSERT_EXCLUSIVE_LOCK() {} |
37 #else | 42 #else |
38 Mutex(); | 43 Mutex(); |
39 ~Mutex(); | 44 ~Mutex(); |
40 | 45 |
41 void Lock() MOJO_EXCLUSIVE_LOCK_FUNCTION() { | 46 void Lock() MOJO_EXCLUSIVE_LOCK_FUNCTION(); |
42 lock_.Lock(); | 47 void Unlock() MOJO_UNLOCK_FUNCTION(); |
43 CheckUnheldAndMark(); | |
44 } | |
45 void Unlock() MOJO_UNLOCK_FUNCTION() { | |
46 CheckHeldAndUnmark(); | |
47 lock_.Unlock(); | |
48 } | |
49 | 48 |
50 bool TryLock() MOJO_EXCLUSIVE_TRYLOCK_FUNCTION(true) { | 49 bool TryLock() MOJO_EXCLUSIVE_TRYLOCK_FUNCTION(true); |
51 bool rv = lock_.Try(); | |
52 if (rv) | |
53 CheckUnheldAndMark(); | |
54 return rv; | |
55 } | |
56 | 50 |
57 void AssertHeld() const MOJO_ASSERT_EXCLUSIVE_LOCK(); | 51 void AssertHeld() MOJO_ASSERT_EXCLUSIVE_LOCK(); |
58 #endif // NDEBUG && !DCHECK_ALWAYS_ON | 52 #endif // defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
59 | 53 |
60 private: | 54 private: |
61 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) | 55 pthread_mutex_t impl_; |
62 void CheckHeldAndUnmark(); | |
63 void CheckUnheldAndMark(); | |
64 | |
65 base::PlatformThreadRef owning_thread_ref_; | |
66 #endif // !NDEBUG || DCHECK_ALWAYS_ON | |
67 | |
68 base::internal::LockImpl lock_; | |
69 | 56 |
70 MOJO_DISALLOW_COPY_AND_ASSIGN(Mutex); | 57 MOJO_DISALLOW_COPY_AND_ASSIGN(Mutex); |
71 }; | 58 }; |
72 | 59 |
73 // MutexLocker ----------------------------------------------------------------- | 60 // MutexLocker ----------------------------------------------------------------- |
74 | 61 |
75 class MOJO_SCOPED_LOCKABLE MutexLocker { | 62 class MOJO_SCOPED_LOCKABLE MutexLocker { |
76 public: | 63 public: |
77 explicit MutexLocker(Mutex* mutex) MOJO_EXCLUSIVE_LOCK_FUNCTION(mutex) | 64 explicit MutexLocker(Mutex* mutex) MOJO_EXCLUSIVE_LOCK_FUNCTION(mutex) |
78 : mutex_(mutex) { | 65 : mutex_(mutex) { |
79 this->mutex_->Lock(); | 66 this->mutex_->Lock(); |
80 } | 67 } |
81 ~MutexLocker() MOJO_UNLOCK_FUNCTION() { this->mutex_->Unlock(); } | 68 ~MutexLocker() MOJO_UNLOCK_FUNCTION() { this->mutex_->Unlock(); } |
82 | 69 |
83 private: | 70 private: |
84 Mutex* const mutex_; | 71 Mutex* const mutex_; |
85 | 72 |
86 MOJO_DISALLOW_COPY_AND_ASSIGN(MutexLocker); | 73 MOJO_DISALLOW_COPY_AND_ASSIGN(MutexLocker); |
87 }; | 74 }; |
88 | 75 |
89 } // namespace system | 76 } // namespace system |
90 } // namespace mojo | 77 } // namespace mojo |
91 | 78 |
92 #endif // MOJO_EDK_SYSTEM_MUTEX_H_ | 79 #endif // MOJO_EDK_SYSTEM_MUTEX_H_ |
OLD | NEW |