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_SYSTEM_MUTEX_H_ | 9 #ifndef MOJO_EDK_UTIL_MUTEX_H_ |
10 #define MOJO_EDK_SYSTEM_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/system/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 system { | 18 namespace util { |
19 | |
20 // So |Mutex| can friend it. | |
21 class CondVar; | |
22 | 19 |
23 // Mutex ----------------------------------------------------------------------- | 20 // Mutex ----------------------------------------------------------------------- |
24 | 21 |
| 22 class CondVar; |
| 23 |
25 class MOJO_LOCKABLE Mutex { | 24 class MOJO_LOCKABLE Mutex { |
26 public: | 25 public: |
27 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | 26 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
28 Mutex() { pthread_mutex_init(&impl_, nullptr); } | 27 Mutex() { pthread_mutex_init(&impl_, nullptr); } |
29 ~Mutex() { pthread_mutex_destroy(&impl_); } | 28 ~Mutex() { pthread_mutex_destroy(&impl_); } |
30 | 29 |
31 // Takes an exclusive lock. | 30 // Takes an exclusive lock. |
32 void Lock() MOJO_EXCLUSIVE_LOCK_FUNCTION() { pthread_mutex_lock(&impl_); } | 31 void Lock() MOJO_EXCLUSIVE_LOCK_FUNCTION() { pthread_mutex_lock(&impl_); } |
33 | 32 |
34 // Releases a lock. | 33 // Releases a lock. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 this->mutex_->Lock(); | 70 this->mutex_->Lock(); |
72 } | 71 } |
73 ~MutexLocker() MOJO_UNLOCK_FUNCTION() { this->mutex_->Unlock(); } | 72 ~MutexLocker() MOJO_UNLOCK_FUNCTION() { this->mutex_->Unlock(); } |
74 | 73 |
75 private: | 74 private: |
76 Mutex* const mutex_; | 75 Mutex* const mutex_; |
77 | 76 |
78 MOJO_DISALLOW_COPY_AND_ASSIGN(MutexLocker); | 77 MOJO_DISALLOW_COPY_AND_ASSIGN(MutexLocker); |
79 }; | 78 }; |
80 | 79 |
81 } // namespace system | 80 } // namespace util |
82 } // namespace mojo | 81 } // namespace mojo |
83 | 82 |
84 #endif // MOJO_EDK_SYSTEM_MUTEX_H_ | 83 #endif // MOJO_EDK_UTIL_MUTEX_H_ |
OLD | NEW |