| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project 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 #ifndef V8_PLATFORM_MUTEX_H_ | 5 #ifndef V8_PLATFORM_MUTEX_H_ |
| 6 #define V8_PLATFORM_MUTEX_H_ | 6 #define V8_PLATFORM_MUTEX_H_ |
| 7 | 7 |
| 8 #include "src/base/lazy-instance.h" |
| 9 #if V8_OS_WIN |
| 10 #include "src/base/win32-headers.h" |
| 11 #endif |
| 8 #include "src/checks.h" | 12 #include "src/checks.h" |
| 9 #include "src/lazy-instance.h" | |
| 10 #if V8_OS_WIN | |
| 11 #include "src/win32-headers.h" | |
| 12 #endif | |
| 13 | 13 |
| 14 #if V8_OS_POSIX | 14 #if V8_OS_POSIX |
| 15 #include <pthread.h> // NOLINT | 15 #include <pthread.h> // NOLINT |
| 16 #endif | 16 #endif |
| 17 | 17 |
| 18 namespace v8 { | 18 namespace v8 { |
| 19 namespace internal { | 19 namespace internal { |
| 20 | 20 |
| 21 // ---------------------------------------------------------------------------- | 21 // ---------------------------------------------------------------------------- |
| 22 // Mutex | 22 // Mutex |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 | 94 |
| 95 // POD Mutex initialized lazily (i.e. the first time Pointer() is called). | 95 // POD Mutex initialized lazily (i.e. the first time Pointer() is called). |
| 96 // Usage: | 96 // Usage: |
| 97 // static LazyMutex my_mutex = LAZY_MUTEX_INITIALIZER; | 97 // static LazyMutex my_mutex = LAZY_MUTEX_INITIALIZER; |
| 98 // | 98 // |
| 99 // void my_function() { | 99 // void my_function() { |
| 100 // LockGuard<Mutex> guard(my_mutex.Pointer()); | 100 // LockGuard<Mutex> guard(my_mutex.Pointer()); |
| 101 // // Do something. | 101 // // Do something. |
| 102 // } | 102 // } |
| 103 // | 103 // |
| 104 typedef LazyStaticInstance<Mutex, | 104 typedef v8::base::LazyStaticInstance< |
| 105 DefaultConstructTrait<Mutex>, | 105 Mutex, v8::base::DefaultConstructTrait<Mutex>, |
| 106 ThreadSafeInitOnceTrait>::type LazyMutex; | 106 v8::base::ThreadSafeInitOnceTrait>::type LazyMutex; |
| 107 | 107 |
| 108 #define LAZY_MUTEX_INITIALIZER LAZY_STATIC_INSTANCE_INITIALIZER | 108 #define LAZY_MUTEX_INITIALIZER LAZY_STATIC_INSTANCE_INITIALIZER |
| 109 | 109 |
| 110 | 110 |
| 111 // ----------------------------------------------------------------------------- | 111 // ----------------------------------------------------------------------------- |
| 112 // RecursiveMutex | 112 // RecursiveMutex |
| 113 // | 113 // |
| 114 // This class is a synchronization primitive that can be used to protect shared | 114 // This class is a synchronization primitive that can be used to protect shared |
| 115 // data from being simultaneously accessed by multiple threads. A recursive | 115 // data from being simultaneously accessed by multiple threads. A recursive |
| 116 // mutex offers exclusive, recursive ownership semantics: | 116 // mutex offers exclusive, recursive ownership semantics: |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 // POD RecursiveMutex initialized lazily (i.e. the first time Pointer() is | 175 // POD RecursiveMutex initialized lazily (i.e. the first time Pointer() is |
| 176 // called). | 176 // called). |
| 177 // Usage: | 177 // Usage: |
| 178 // static LazyRecursiveMutex my_mutex = LAZY_RECURSIVE_MUTEX_INITIALIZER; | 178 // static LazyRecursiveMutex my_mutex = LAZY_RECURSIVE_MUTEX_INITIALIZER; |
| 179 // | 179 // |
| 180 // void my_function() { | 180 // void my_function() { |
| 181 // LockGuard<RecursiveMutex> guard(my_mutex.Pointer()); | 181 // LockGuard<RecursiveMutex> guard(my_mutex.Pointer()); |
| 182 // // Do something. | 182 // // Do something. |
| 183 // } | 183 // } |
| 184 // | 184 // |
| 185 typedef LazyStaticInstance<RecursiveMutex, | 185 typedef v8::base::LazyStaticInstance< |
| 186 DefaultConstructTrait<RecursiveMutex>, | 186 RecursiveMutex, v8::base::DefaultConstructTrait<RecursiveMutex>, |
| 187 ThreadSafeInitOnceTrait>::type LazyRecursiveMutex; | 187 v8::base::ThreadSafeInitOnceTrait>::type LazyRecursiveMutex; |
| 188 | 188 |
| 189 #define LAZY_RECURSIVE_MUTEX_INITIALIZER LAZY_STATIC_INSTANCE_INITIALIZER | 189 #define LAZY_RECURSIVE_MUTEX_INITIALIZER LAZY_STATIC_INSTANCE_INITIALIZER |
| 190 | 190 |
| 191 | 191 |
| 192 // ----------------------------------------------------------------------------- | 192 // ----------------------------------------------------------------------------- |
| 193 // LockGuard | 193 // LockGuard |
| 194 // | 194 // |
| 195 // This class is a mutex wrapper that provides a convenient RAII-style mechanism | 195 // This class is a mutex wrapper that provides a convenient RAII-style mechanism |
| 196 // for owning a mutex for the duration of a scoped block. | 196 // for owning a mutex for the duration of a scoped block. |
| 197 // When a LockGuard object is created, it attempts to take ownership of the | 197 // When a LockGuard object is created, it attempts to take ownership of the |
| 198 // mutex it is given. When control leaves the scope in which the LockGuard | 198 // mutex it is given. When control leaves the scope in which the LockGuard |
| 199 // object was created, the LockGuard is destructed and the mutex is released. | 199 // object was created, the LockGuard is destructed and the mutex is released. |
| 200 // The LockGuard class is non-copyable. | 200 // The LockGuard class is non-copyable. |
| 201 | 201 |
| 202 template <typename Mutex> | 202 template <typename Mutex> |
| 203 class LockGuard V8_FINAL { | 203 class LockGuard V8_FINAL { |
| 204 public: | 204 public: |
| 205 explicit LockGuard(Mutex* mutex) : mutex_(mutex) { mutex_->Lock(); } | 205 explicit LockGuard(Mutex* mutex) : mutex_(mutex) { mutex_->Lock(); } |
| 206 ~LockGuard() { mutex_->Unlock(); } | 206 ~LockGuard() { mutex_->Unlock(); } |
| 207 | 207 |
| 208 private: | 208 private: |
| 209 Mutex* mutex_; | 209 Mutex* mutex_; |
| 210 | 210 |
| 211 DISALLOW_COPY_AND_ASSIGN(LockGuard); | 211 DISALLOW_COPY_AND_ASSIGN(LockGuard); |
| 212 }; | 212 }; |
| 213 | 213 |
| 214 } } // namespace v8::internal | 214 } } // namespace v8::internal |
| 215 | 215 |
| 216 #endif // V8_PLATFORM_MUTEX_H_ | 216 #endif // V8_PLATFORM_MUTEX_H_ |
| OLD | NEW |