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 // A mutex class, with support for thread annotations. |
| 6 // |
| 7 // TODO(vtl): Currently, this is a fork of Chromium's |
| 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 |
| 12 #ifndef MOJO_EDK_SYSTEM_MUTEX_H_ |
| 13 #define MOJO_EDK_SYSTEM_MUTEX_H_ |
| 14 |
| 15 #include "base/synchronization/lock_impl.h" |
| 16 #include "base/threading/platform_thread.h" |
| 17 #include "mojo/edk/system/system_impl_export.h" |
| 18 #include "mojo/edk/system/thread_annotations.h" |
| 19 #include "mojo/public/cpp/system/macros.h" |
| 20 |
| 21 namespace mojo { |
| 22 namespace system { |
| 23 |
| 24 // Mutex ----------------------------------------------------------------------- |
| 25 |
| 26 class MOJO_SYSTEM_IMPL_EXPORT MOJO_LOCKABLE Mutex { |
| 27 public: |
| 28 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 29 Mutex() : lock_() {} |
| 30 ~Mutex() {} |
| 31 |
| 32 void Lock() MOJO_EXCLUSIVE_LOCK_FUNCTION() { lock_.Lock(); } |
| 33 void Unlock() MOJO_UNLOCK_FUNCTION() { lock_.Unlock(); } |
| 34 |
| 35 bool TryLock() MOJO_EXCLUSIVE_TRYLOCK_FUNCTION(true) { return lock_.Try(); } |
| 36 |
| 37 void AssertHeld() const MOJO_ASSERT_EXCLUSIVE_LOCK() {} |
| 38 #else |
| 39 Mutex(); |
| 40 ~Mutex(); |
| 41 |
| 42 void Lock() MOJO_EXCLUSIVE_LOCK_FUNCTION() { |
| 43 lock_.Lock(); |
| 44 CheckUnheldAndMark(); |
| 45 } |
| 46 void Unlock() MOJO_UNLOCK_FUNCTION() { |
| 47 CheckHeldAndUnmark(); |
| 48 lock_.Unlock(); |
| 49 } |
| 50 |
| 51 bool TryLock() MOJO_EXCLUSIVE_TRYLOCK_FUNCTION(true) { |
| 52 bool rv = lock_.Try(); |
| 53 if (rv) |
| 54 CheckUnheldAndMark(); |
| 55 return rv; |
| 56 } |
| 57 |
| 58 void AssertHeld() const MOJO_ASSERT_EXCLUSIVE_LOCK(); |
| 59 #endif // NDEBUG && !DCHECK_ALWAYS_ON |
| 60 |
| 61 private: |
| 62 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) |
| 63 void CheckHeldAndUnmark(); |
| 64 void CheckUnheldAndMark(); |
| 65 |
| 66 base::PlatformThreadRef owning_thread_ref_; |
| 67 #endif // !NDEBUG || DCHECK_ALWAYS_ON |
| 68 |
| 69 base::internal::LockImpl lock_; |
| 70 |
| 71 MOJO_DISALLOW_COPY_AND_ASSIGN(Mutex); |
| 72 }; |
| 73 |
| 74 // MutexLocker ----------------------------------------------------------------- |
| 75 |
| 76 class MOJO_SYSTEM_IMPL_EXPORT MOJO_SCOPED_LOCKABLE MutexLocker { |
| 77 public: |
| 78 explicit MutexLocker(Mutex* mutex) MOJO_EXCLUSIVE_LOCK_FUNCTION(mutex) |
| 79 : mutex_(mutex) { |
| 80 this->mutex_->Lock(); |
| 81 } |
| 82 ~MutexLocker() MOJO_UNLOCK_FUNCTION() { this->mutex_->Unlock(); } |
| 83 |
| 84 private: |
| 85 Mutex* const mutex_; |
| 86 |
| 87 MOJO_DISALLOW_COPY_AND_ASSIGN(MutexLocker); |
| 88 }; |
| 89 |
| 90 } // namespace system |
| 91 } // namespace mojo |
| 92 |
| 93 #endif // MOJO_EDK_SYSTEM_MUTEX_H_ |
OLD | NEW |