| 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): Add support for non-exclusive (reader) locks. | 7 // TODO(vtl): Add support for non-exclusive (reader) locks. |
| 8 | 8 |
| 9 #ifndef MOJO_EDK_UTIL_MUTEX_H_ | 9 #ifndef MOJO_EDK_UTIL_MUTEX_H_ |
| 10 #define MOJO_EDK_UTIL_MUTEX_H_ | 10 #define MOJO_EDK_UTIL_MUTEX_H_ |
| 11 | 11 |
| 12 #include <pthread.h> | 12 #include <pthread.h> |
| 13 | 13 |
| 14 #include "mojo/edk/util/thread_annotations.h" | 14 #include "mojo/edk/util/thread_annotations.h" |
| 15 #include "mojo/public/cpp/system/macros.h" | 15 #include "mojo/public/cpp/system/macros.h" |
| 16 | 16 |
| 17 namespace mojo { | 17 namespace mojo { |
| 18 namespace util { | 18 namespace util { |
| 19 | 19 |
| 20 // Mutex ----------------------------------------------------------------------- | 20 // Mutex ----------------------------------------------------------------------- |
| 21 | 21 |
| 22 class CondVar; | 22 class CondVar; |
| 23 | 23 |
| 24 class MOJO_LOCKABLE Mutex { | 24 class MOJO_LOCKABLE Mutex final { |
| 25 public: | 25 public: |
| 26 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | 26 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 27 Mutex() { pthread_mutex_init(&impl_, nullptr); } | 27 Mutex() { pthread_mutex_init(&impl_, nullptr); } |
| 28 ~Mutex() { pthread_mutex_destroy(&impl_); } | 28 ~Mutex() { pthread_mutex_destroy(&impl_); } |
| 29 | 29 |
| 30 // Takes an exclusive lock. | 30 // Takes an exclusive lock. |
| 31 void Lock() MOJO_EXCLUSIVE_LOCK_FUNCTION() { pthread_mutex_lock(&impl_); } | 31 void Lock() MOJO_EXCLUSIVE_LOCK_FUNCTION() { pthread_mutex_lock(&impl_); } |
| 32 | 32 |
| 33 // Releases a lock. | 33 // Releases a lock. |
| 34 void Unlock() MOJO_UNLOCK_FUNCTION() { pthread_mutex_unlock(&impl_); } | 34 void Unlock() MOJO_UNLOCK_FUNCTION() { pthread_mutex_unlock(&impl_); } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 56 private: | 56 private: |
| 57 friend class CondVar; | 57 friend class CondVar; |
| 58 | 58 |
| 59 pthread_mutex_t impl_; | 59 pthread_mutex_t impl_; |
| 60 | 60 |
| 61 MOJO_DISALLOW_COPY_AND_ASSIGN(Mutex); | 61 MOJO_DISALLOW_COPY_AND_ASSIGN(Mutex); |
| 62 }; | 62 }; |
| 63 | 63 |
| 64 // MutexLocker ----------------------------------------------------------------- | 64 // MutexLocker ----------------------------------------------------------------- |
| 65 | 65 |
| 66 class MOJO_SCOPED_LOCKABLE MutexLocker { | 66 class MOJO_SCOPED_LOCKABLE MutexLocker final { |
| 67 public: | 67 public: |
| 68 explicit MutexLocker(Mutex* mutex) MOJO_EXCLUSIVE_LOCK_FUNCTION(mutex) | 68 explicit MutexLocker(Mutex* mutex) MOJO_EXCLUSIVE_LOCK_FUNCTION(mutex) |
| 69 : mutex_(mutex) { | 69 : mutex_(mutex) { |
| 70 this->mutex_->Lock(); | 70 this->mutex_->Lock(); |
| 71 } | 71 } |
| 72 ~MutexLocker() MOJO_UNLOCK_FUNCTION() { this->mutex_->Unlock(); } | 72 ~MutexLocker() MOJO_UNLOCK_FUNCTION() { this->mutex_->Unlock(); } |
| 73 | 73 |
| 74 private: | 74 private: |
| 75 Mutex* const mutex_; | 75 Mutex* const mutex_; |
| 76 | 76 |
| 77 MOJO_DISALLOW_COPY_AND_ASSIGN(MutexLocker); | 77 MOJO_DISALLOW_COPY_AND_ASSIGN(MutexLocker); |
| 78 }; | 78 }; |
| 79 | 79 |
| 80 } // namespace util | 80 } // namespace util |
| 81 } // namespace mojo | 81 } // namespace mojo |
| 82 | 82 |
| 83 #endif // MOJO_EDK_UTIL_MUTEX_H_ | 83 #endif // MOJO_EDK_UTIL_MUTEX_H_ |
| OLD | NEW |