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_BASE_PLATFORM_MUTEX_H_ | 5 #ifndef V8_BASE_PLATFORM_MUTEX_H_ |
6 #define V8_BASE_PLATFORM_MUTEX_H_ | 6 #define V8_BASE_PLATFORM_MUTEX_H_ |
7 | 7 |
8 #include "src/base/base-export.h" | |
9 #include "src/base/lazy-instance.h" | 8 #include "src/base/lazy-instance.h" |
10 #if V8_OS_WIN | 9 #if V8_OS_WIN |
11 #include "src/base/win32-headers.h" | 10 #include "src/base/win32-headers.h" |
12 #endif | 11 #endif |
13 #include "src/base/logging.h" | 12 #include "src/base/logging.h" |
14 | 13 |
15 #if V8_OS_POSIX | 14 #if V8_OS_POSIX |
16 #include <pthread.h> // NOLINT | 15 #include <pthread.h> // NOLINT |
17 #endif | 16 #endif |
18 | 17 |
19 namespace v8 { | 18 namespace v8 { |
20 namespace base { | 19 namespace base { |
21 | 20 |
22 // ---------------------------------------------------------------------------- | 21 // ---------------------------------------------------------------------------- |
23 // Mutex | 22 // Mutex |
24 // | 23 // |
25 // 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 |
26 // data from being simultaneously accessed by multiple threads. A mutex offers | 25 // data from being simultaneously accessed by multiple threads. A mutex offers |
27 // exclusive, non-recursive ownership semantics: | 26 // exclusive, non-recursive ownership semantics: |
28 // - 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 |
29 // either |Lock()| or |TryLock()| until it calls |Unlock()|. | 28 // either |Lock()| or |TryLock()| until it calls |Unlock()|. |
30 // - 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 |
31 // |Lock()|) or receive a |false| return value (for |TryLock()|) if they | 30 // |Lock()|) or receive a |false| return value (for |TryLock()|) if they |
32 // attempt to claim ownership of the mutex. | 31 // attempt to claim ownership of the mutex. |
33 // A calling thread must not own the mutex prior to calling |Lock()| or | 32 // A calling thread must not own the mutex prior to calling |Lock()| or |
34 // |TryLock()|. The behavior of a program is undefined if a mutex is destroyed | 33 // |TryLock()|. The behavior of a program is undefined if a mutex is destroyed |
35 // while still owned by some thread. The Mutex class is non-copyable. | 34 // while still owned by some thread. The Mutex class is non-copyable. |
36 | 35 |
37 class V8_BASE_EXPORT Mutex final { | 36 class Mutex final { |
38 public: | 37 public: |
39 Mutex(); | 38 Mutex(); |
40 ~Mutex(); | 39 ~Mutex(); |
41 | 40 |
42 // Locks the given mutex. If the mutex is currently unlocked, it becomes | 41 // Locks the given mutex. If the mutex is currently unlocked, it becomes |
43 // locked and owned by the calling thread, and immediately. If the mutex | 42 // locked and owned by the calling thread, and immediately. If the mutex |
44 // is already locked by another thread, suspends the calling thread until | 43 // is already locked by another thread, suspends the calling thread until |
45 // the mutex is unlocked. | 44 // the mutex is unlocked. |
46 void Lock(); | 45 void Lock(); |
47 | 46 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 // calls to |Unlock()|. | 120 // calls to |Unlock()|. |
122 // - When a thread owns a recursive mutex, all other threads will block (for | 121 // - When a thread owns a recursive mutex, all other threads will block (for |
123 // calls to |Lock()|) or receive a |false| return value (for |TryLock()|) if | 122 // calls to |Lock()|) or receive a |false| return value (for |TryLock()|) if |
124 // they attempt to claim ownership of the recursive mutex. | 123 // they attempt to claim ownership of the recursive mutex. |
125 // - The maximum number of times that a recursive mutex may be locked is | 124 // - The maximum number of times that a recursive mutex may be locked is |
126 // unspecified, but after that number is reached, calls to |Lock()| will | 125 // unspecified, but after that number is reached, calls to |Lock()| will |
127 // probably abort the process and calls to |TryLock()| return false. | 126 // probably abort the process and calls to |TryLock()| return false. |
128 // The behavior of a program is undefined if a recursive mutex is destroyed | 127 // The behavior of a program is undefined if a recursive mutex is destroyed |
129 // while still owned by some thread. The RecursiveMutex class is non-copyable. | 128 // while still owned by some thread. The RecursiveMutex class is non-copyable. |
130 | 129 |
131 class V8_BASE_EXPORT RecursiveMutex final { | 130 class RecursiveMutex final { |
132 public: | 131 public: |
133 RecursiveMutex(); | 132 RecursiveMutex(); |
134 ~RecursiveMutex(); | 133 ~RecursiveMutex(); |
135 | 134 |
136 // Locks the mutex. If another thread has already locked the mutex, a call to | 135 // Locks the mutex. If another thread has already locked the mutex, a call to |
137 // |Lock()| will block execution until the lock is acquired. A thread may call | 136 // |Lock()| will block execution until the lock is acquired. A thread may call |
138 // |Lock()| on a recursive mutex repeatedly. Ownership will only be released | 137 // |Lock()| on a recursive mutex repeatedly. Ownership will only be released |
139 // after the thread makes a matching number of calls to |Unlock()|. | 138 // after the thread makes a matching number of calls to |Unlock()|. |
140 // The behavior is undefined if the mutex is not unlocked before being | 139 // The behavior is undefined if the mutex is not unlocked before being |
141 // destroyed, i.e. some thread still owns it. | 140 // destroyed, i.e. some thread still owns it. |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 base | 213 } // namespace base |
215 } // namespace v8 | 214 } // namespace v8 |
216 | 215 |
217 #endif // V8_BASE_PLATFORM_MUTEX_H_ | 216 #endif // V8_BASE_PLATFORM_MUTEX_H_ |
OLD | NEW |