| 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_BASE_PLATFORM_MUTEX_H_ |
| 6 #define V8_PLATFORM_MUTEX_H_ | 6 #define V8_BASE_PLATFORM_MUTEX_H_ |
| 7 | 7 |
| 8 #include "src/base/lazy-instance.h" | 8 #include "src/base/lazy-instance.h" |
| 9 #if V8_OS_WIN | 9 #if V8_OS_WIN |
| 10 #include "src/base/win32-headers.h" | 10 #include "src/base/win32-headers.h" |
| 11 #endif | 11 #endif |
| 12 #include "src/checks.h" | 12 #include "src/base/logging.h" |
| 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 base { |
| 20 | 20 |
| 21 // ---------------------------------------------------------------------------- | 21 // ---------------------------------------------------------------------------- |
| 22 // Mutex | 22 // Mutex |
| 23 // | 23 // |
| 24 // This class is a synchronization primitive that can be used to protect shared | 24 // This class is a synchronization primitive that can be used to protect shared |
| 25 // data from being simultaneously accessed by multiple threads. A mutex offers | 25 // data from being simultaneously accessed by multiple threads. A mutex offers |
| 26 // exclusive, non-recursive ownership semantics: | 26 // exclusive, non-recursive ownership semantics: |
| 27 // - A calling thread owns a mutex from the time that it successfully calls | 27 // - A calling thread owns a mutex from the time that it successfully calls |
| 28 // either |Lock()| or |TryLock()| until it calls |Unlock()|. | 28 // either |Lock()| or |TryLock()| until it calls |Unlock()|. |
| 29 // - When a thread owns a mutex, all other threads will block (for calls to | 29 // - When a thread owns a mutex, all other threads will block (for calls to |
| (...skipping 64 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 v8::base::LazyStaticInstance< | 104 typedef LazyStaticInstance<Mutex, DefaultConstructTrait<Mutex>, |
| 105 Mutex, v8::base::DefaultConstructTrait<Mutex>, | 105 ThreadSafeInitOnceTrait>::type LazyMutex; |
| 106 v8::base::ThreadSafeInitOnceTrait>::type LazyMutex; | |
| 107 | 106 |
| 108 #define LAZY_MUTEX_INITIALIZER LAZY_STATIC_INSTANCE_INITIALIZER | 107 #define LAZY_MUTEX_INITIALIZER LAZY_STATIC_INSTANCE_INITIALIZER |
| 109 | 108 |
| 110 | 109 |
| 111 // ----------------------------------------------------------------------------- | 110 // ----------------------------------------------------------------------------- |
| 112 // RecursiveMutex | 111 // RecursiveMutex |
| 113 // | 112 // |
| 114 // This class is a synchronization primitive that can be used to protect shared | 113 // This class is a synchronization primitive that can be used to protect shared |
| 115 // data from being simultaneously accessed by multiple threads. A recursive | 114 // data from being simultaneously accessed by multiple threads. A recursive |
| 116 // mutex offers exclusive, recursive ownership semantics: | 115 // 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 | 174 // POD RecursiveMutex initialized lazily (i.e. the first time Pointer() is |
| 176 // called). | 175 // called). |
| 177 // Usage: | 176 // Usage: |
| 178 // static LazyRecursiveMutex my_mutex = LAZY_RECURSIVE_MUTEX_INITIALIZER; | 177 // static LazyRecursiveMutex my_mutex = LAZY_RECURSIVE_MUTEX_INITIALIZER; |
| 179 // | 178 // |
| 180 // void my_function() { | 179 // void my_function() { |
| 181 // LockGuard<RecursiveMutex> guard(my_mutex.Pointer()); | 180 // LockGuard<RecursiveMutex> guard(my_mutex.Pointer()); |
| 182 // // Do something. | 181 // // Do something. |
| 183 // } | 182 // } |
| 184 // | 183 // |
| 185 typedef v8::base::LazyStaticInstance< | 184 typedef LazyStaticInstance<RecursiveMutex, |
| 186 RecursiveMutex, v8::base::DefaultConstructTrait<RecursiveMutex>, | 185 DefaultConstructTrait<RecursiveMutex>, |
| 187 v8::base::ThreadSafeInitOnceTrait>::type LazyRecursiveMutex; | 186 ThreadSafeInitOnceTrait>::type LazyRecursiveMutex; |
| 188 | 187 |
| 189 #define LAZY_RECURSIVE_MUTEX_INITIALIZER LAZY_STATIC_INSTANCE_INITIALIZER | 188 #define LAZY_RECURSIVE_MUTEX_INITIALIZER LAZY_STATIC_INSTANCE_INITIALIZER |
| 190 | 189 |
| 191 | 190 |
| 192 // ----------------------------------------------------------------------------- | 191 // ----------------------------------------------------------------------------- |
| 193 // LockGuard | 192 // LockGuard |
| 194 // | 193 // |
| 195 // This class is a mutex wrapper that provides a convenient RAII-style mechanism | 194 // 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. | 195 // 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 | 196 // 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 | 197 // 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. | 198 // object was created, the LockGuard is destructed and the mutex is released. |
| 200 // The LockGuard class is non-copyable. | 199 // The LockGuard class is non-copyable. |
| 201 | 200 |
| 202 template <typename Mutex> | 201 template <typename Mutex> |
| 203 class LockGuard V8_FINAL { | 202 class LockGuard V8_FINAL { |
| 204 public: | 203 public: |
| 205 explicit LockGuard(Mutex* mutex) : mutex_(mutex) { mutex_->Lock(); } | 204 explicit LockGuard(Mutex* mutex) : mutex_(mutex) { mutex_->Lock(); } |
| 206 ~LockGuard() { mutex_->Unlock(); } | 205 ~LockGuard() { mutex_->Unlock(); } |
| 207 | 206 |
| 208 private: | 207 private: |
| 209 Mutex* mutex_; | 208 Mutex* mutex_; |
| 210 | 209 |
| 211 DISALLOW_COPY_AND_ASSIGN(LockGuard); | 210 DISALLOW_COPY_AND_ASSIGN(LockGuard); |
| 212 }; | 211 }; |
| 213 | 212 |
| 214 } } // namespace v8::internal | 213 } } // namespace v8::base |
| 215 | 214 |
| 216 #endif // V8_PLATFORM_MUTEX_H_ | 215 #endif // V8_BASE_PLATFORM_MUTEX_H_ |
| OLD | NEW |