| 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 #include "src/base/platform/mutex.h" | 5 #include "src/base/platform/mutex.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 | 8 |
| 9 namespace v8 { | 9 namespace v8 { |
| 10 namespace base { | 10 namespace base { |
| 11 | 11 |
| 12 #if V8_OS_POSIX | 12 #if V8_OS_POSIX |
| 13 | 13 |
| 14 static V8_INLINE void InitializeNativeHandle(pthread_mutex_t* mutex) { | 14 static V8_INLINE void InitializeNativeHandle(pthread_mutex_t* mutex) { |
| 15 int result; | 15 int result; |
| 16 #if defined(DEBUG) | 16 #if defined(DEBUG) |
| 17 // Use an error checking mutex in debug mode. | 17 // Use an error checking mutex in debug mode. |
| 18 pthread_mutexattr_t attr; | 18 pthread_mutexattr_t attr; |
| 19 result = pthread_mutexattr_init(&attr); | 19 result = pthread_mutexattr_init(&attr); |
| 20 ASSERT_EQ(0, result); | 20 DCHECK_EQ(0, result); |
| 21 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); | 21 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); |
| 22 ASSERT_EQ(0, result); | 22 DCHECK_EQ(0, result); |
| 23 result = pthread_mutex_init(mutex, &attr); | 23 result = pthread_mutex_init(mutex, &attr); |
| 24 ASSERT_EQ(0, result); | 24 DCHECK_EQ(0, result); |
| 25 result = pthread_mutexattr_destroy(&attr); | 25 result = pthread_mutexattr_destroy(&attr); |
| 26 #else | 26 #else |
| 27 // Use a fast mutex (default attributes). | 27 // Use a fast mutex (default attributes). |
| 28 result = pthread_mutex_init(mutex, NULL); | 28 result = pthread_mutex_init(mutex, NULL); |
| 29 #endif // defined(DEBUG) | 29 #endif // defined(DEBUG) |
| 30 ASSERT_EQ(0, result); | 30 DCHECK_EQ(0, result); |
| 31 USE(result); | 31 USE(result); |
| 32 } | 32 } |
| 33 | 33 |
| 34 | 34 |
| 35 static V8_INLINE void InitializeRecursiveNativeHandle(pthread_mutex_t* mutex) { | 35 static V8_INLINE void InitializeRecursiveNativeHandle(pthread_mutex_t* mutex) { |
| 36 pthread_mutexattr_t attr; | 36 pthread_mutexattr_t attr; |
| 37 int result = pthread_mutexattr_init(&attr); | 37 int result = pthread_mutexattr_init(&attr); |
| 38 ASSERT_EQ(0, result); | 38 DCHECK_EQ(0, result); |
| 39 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); | 39 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); |
| 40 ASSERT_EQ(0, result); | 40 DCHECK_EQ(0, result); |
| 41 result = pthread_mutex_init(mutex, &attr); | 41 result = pthread_mutex_init(mutex, &attr); |
| 42 ASSERT_EQ(0, result); | 42 DCHECK_EQ(0, result); |
| 43 result = pthread_mutexattr_destroy(&attr); | 43 result = pthread_mutexattr_destroy(&attr); |
| 44 ASSERT_EQ(0, result); | 44 DCHECK_EQ(0, result); |
| 45 USE(result); | 45 USE(result); |
| 46 } | 46 } |
| 47 | 47 |
| 48 | 48 |
| 49 static V8_INLINE void DestroyNativeHandle(pthread_mutex_t* mutex) { | 49 static V8_INLINE void DestroyNativeHandle(pthread_mutex_t* mutex) { |
| 50 int result = pthread_mutex_destroy(mutex); | 50 int result = pthread_mutex_destroy(mutex); |
| 51 ASSERT_EQ(0, result); | 51 DCHECK_EQ(0, result); |
| 52 USE(result); | 52 USE(result); |
| 53 } | 53 } |
| 54 | 54 |
| 55 | 55 |
| 56 static V8_INLINE void LockNativeHandle(pthread_mutex_t* mutex) { | 56 static V8_INLINE void LockNativeHandle(pthread_mutex_t* mutex) { |
| 57 int result = pthread_mutex_lock(mutex); | 57 int result = pthread_mutex_lock(mutex); |
| 58 ASSERT_EQ(0, result); | 58 DCHECK_EQ(0, result); |
| 59 USE(result); | 59 USE(result); |
| 60 } | 60 } |
| 61 | 61 |
| 62 | 62 |
| 63 static V8_INLINE void UnlockNativeHandle(pthread_mutex_t* mutex) { | 63 static V8_INLINE void UnlockNativeHandle(pthread_mutex_t* mutex) { |
| 64 int result = pthread_mutex_unlock(mutex); | 64 int result = pthread_mutex_unlock(mutex); |
| 65 ASSERT_EQ(0, result); | 65 DCHECK_EQ(0, result); |
| 66 USE(result); | 66 USE(result); |
| 67 } | 67 } |
| 68 | 68 |
| 69 | 69 |
| 70 static V8_INLINE bool TryLockNativeHandle(pthread_mutex_t* mutex) { | 70 static V8_INLINE bool TryLockNativeHandle(pthread_mutex_t* mutex) { |
| 71 int result = pthread_mutex_trylock(mutex); | 71 int result = pthread_mutex_trylock(mutex); |
| 72 if (result == EBUSY) { | 72 if (result == EBUSY) { |
| 73 return false; | 73 return false; |
| 74 } | 74 } |
| 75 ASSERT_EQ(0, result); | 75 DCHECK_EQ(0, result); |
| 76 return true; | 76 return true; |
| 77 } | 77 } |
| 78 | 78 |
| 79 #elif V8_OS_WIN | 79 #elif V8_OS_WIN |
| 80 | 80 |
| 81 static V8_INLINE void InitializeNativeHandle(PCRITICAL_SECTION cs) { | 81 static V8_INLINE void InitializeNativeHandle(PCRITICAL_SECTION cs) { |
| 82 InitializeCriticalSection(cs); | 82 InitializeCriticalSection(cs); |
| 83 } | 83 } |
| 84 | 84 |
| 85 | 85 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 113 Mutex::Mutex() { | 113 Mutex::Mutex() { |
| 114 InitializeNativeHandle(&native_handle_); | 114 InitializeNativeHandle(&native_handle_); |
| 115 #ifdef DEBUG | 115 #ifdef DEBUG |
| 116 level_ = 0; | 116 level_ = 0; |
| 117 #endif | 117 #endif |
| 118 } | 118 } |
| 119 | 119 |
| 120 | 120 |
| 121 Mutex::~Mutex() { | 121 Mutex::~Mutex() { |
| 122 DestroyNativeHandle(&native_handle_); | 122 DestroyNativeHandle(&native_handle_); |
| 123 ASSERT_EQ(0, level_); | 123 DCHECK_EQ(0, level_); |
| 124 } | 124 } |
| 125 | 125 |
| 126 | 126 |
| 127 void Mutex::Lock() { | 127 void Mutex::Lock() { |
| 128 LockNativeHandle(&native_handle_); | 128 LockNativeHandle(&native_handle_); |
| 129 AssertUnheldAndMark(); | 129 AssertUnheldAndMark(); |
| 130 } | 130 } |
| 131 | 131 |
| 132 | 132 |
| 133 void Mutex::Unlock() { | 133 void Mutex::Unlock() { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 148 RecursiveMutex::RecursiveMutex() { | 148 RecursiveMutex::RecursiveMutex() { |
| 149 InitializeRecursiveNativeHandle(&native_handle_); | 149 InitializeRecursiveNativeHandle(&native_handle_); |
| 150 #ifdef DEBUG | 150 #ifdef DEBUG |
| 151 level_ = 0; | 151 level_ = 0; |
| 152 #endif | 152 #endif |
| 153 } | 153 } |
| 154 | 154 |
| 155 | 155 |
| 156 RecursiveMutex::~RecursiveMutex() { | 156 RecursiveMutex::~RecursiveMutex() { |
| 157 DestroyNativeHandle(&native_handle_); | 157 DestroyNativeHandle(&native_handle_); |
| 158 ASSERT_EQ(0, level_); | 158 DCHECK_EQ(0, level_); |
| 159 } | 159 } |
| 160 | 160 |
| 161 | 161 |
| 162 void RecursiveMutex::Lock() { | 162 void RecursiveMutex::Lock() { |
| 163 LockNativeHandle(&native_handle_); | 163 LockNativeHandle(&native_handle_); |
| 164 #ifdef DEBUG | 164 #ifdef DEBUG |
| 165 ASSERT_LE(0, level_); | 165 DCHECK_LE(0, level_); |
| 166 level_++; | 166 level_++; |
| 167 #endif | 167 #endif |
| 168 } | 168 } |
| 169 | 169 |
| 170 | 170 |
| 171 void RecursiveMutex::Unlock() { | 171 void RecursiveMutex::Unlock() { |
| 172 #ifdef DEBUG | 172 #ifdef DEBUG |
| 173 ASSERT_LT(0, level_); | 173 DCHECK_LT(0, level_); |
| 174 level_--; | 174 level_--; |
| 175 #endif | 175 #endif |
| 176 UnlockNativeHandle(&native_handle_); | 176 UnlockNativeHandle(&native_handle_); |
| 177 } | 177 } |
| 178 | 178 |
| 179 | 179 |
| 180 bool RecursiveMutex::TryLock() { | 180 bool RecursiveMutex::TryLock() { |
| 181 if (!TryLockNativeHandle(&native_handle_)) { | 181 if (!TryLockNativeHandle(&native_handle_)) { |
| 182 return false; | 182 return false; |
| 183 } | 183 } |
| 184 #ifdef DEBUG | 184 #ifdef DEBUG |
| 185 ASSERT_LE(0, level_); | 185 DCHECK_LE(0, level_); |
| 186 level_++; | 186 level_++; |
| 187 #endif | 187 #endif |
| 188 return true; | 188 return true; |
| 189 } | 189 } |
| 190 | 190 |
| 191 } } // namespace v8::base | 191 } } // namespace v8::base |
| OLD | NEW |