| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2007, 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007, 2009 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com) | 3 * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com) |
| 4 * Copyright (C) 2011 Research In Motion Limited. All rights reserved. | 4 * Copyright (C) 2011 Research In Motion Limited. All rights reserved. |
| 5 * | 5 * |
| 6 * Redistribution and use in source and binary forms, with or without | 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions | 7 * modification, are permitted provided that the following conditions |
| 8 * are met: | 8 * are met: |
| 9 * | 9 * |
| 10 * 1. Redistributions of source code must retain the above copyright | 10 * 1. Redistributions of source code must retain the above copyright |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 } | 103 } |
| 104 | 104 |
| 105 MutexBase::MutexBase(bool recursive) | 105 MutexBase::MutexBase(bool recursive) |
| 106 { | 106 { |
| 107 pthread_mutexattr_t attr; | 107 pthread_mutexattr_t attr; |
| 108 pthread_mutexattr_init(&attr); | 108 pthread_mutexattr_init(&attr); |
| 109 pthread_mutexattr_settype(&attr, recursive ? PTHREAD_MUTEX_RECURSIVE : PTHRE
AD_MUTEX_NORMAL); | 109 pthread_mutexattr_settype(&attr, recursive ? PTHREAD_MUTEX_RECURSIVE : PTHRE
AD_MUTEX_NORMAL); |
| 110 | 110 |
| 111 int result = pthread_mutex_init(&m_mutex.m_internalMutex, &attr); | 111 int result = pthread_mutex_init(&m_mutex.m_internalMutex, &attr); |
| 112 ASSERT_UNUSED(result, !result); | 112 ASSERT_UNUSED(result, !result); |
| 113 #if ENABLE(ASSERT) | |
| 114 m_mutex.m_recursionCount = 0; | 113 m_mutex.m_recursionCount = 0; |
| 115 #endif | |
| 116 | 114 |
| 117 pthread_mutexattr_destroy(&attr); | 115 pthread_mutexattr_destroy(&attr); |
| 118 } | 116 } |
| 119 | 117 |
| 120 MutexBase::~MutexBase() | 118 MutexBase::~MutexBase() |
| 121 { | 119 { |
| 122 int result = pthread_mutex_destroy(&m_mutex.m_internalMutex); | 120 int result = pthread_mutex_destroy(&m_mutex.m_internalMutex); |
| 123 ASSERT_UNUSED(result, !result); | 121 ASSERT_UNUSED(result, !result); |
| 124 } | 122 } |
| 125 | 123 |
| 126 void MutexBase::lock() | 124 void MutexBase::lock() |
| 127 { | 125 { |
| 128 int result = pthread_mutex_lock(&m_mutex.m_internalMutex); | 126 int result = pthread_mutex_lock(&m_mutex.m_internalMutex); |
| 129 ASSERT_UNUSED(result, !result); | 127 ASSERT_UNUSED(result, !result); |
| 130 #if ENABLE(ASSERT) | |
| 131 ++m_mutex.m_recursionCount; | 128 ++m_mutex.m_recursionCount; |
| 132 #endif | |
| 133 } | 129 } |
| 134 | 130 |
| 135 void MutexBase::unlock() | 131 void MutexBase::unlock() |
| 136 { | 132 { |
| 137 #if ENABLE(ASSERT) | |
| 138 ASSERT(m_mutex.m_recursionCount); | 133 ASSERT(m_mutex.m_recursionCount); |
| 139 --m_mutex.m_recursionCount; | 134 --m_mutex.m_recursionCount; |
| 140 #endif | |
| 141 int result = pthread_mutex_unlock(&m_mutex.m_internalMutex); | 135 int result = pthread_mutex_unlock(&m_mutex.m_internalMutex); |
| 142 ASSERT_UNUSED(result, !result); | 136 ASSERT_UNUSED(result, !result); |
| 143 } | 137 } |
| 144 | 138 |
| 145 // There is a separate tryLock implementation for the Mutex and the | 139 // There is a separate tryLock implementation for the Mutex and the |
| 146 // RecursiveMutex since on Windows we need to manually check if tryLock should | 140 // RecursiveMutex since on Windows we need to manually check if tryLock should |
| 147 // succeed or not for the non-recursive mutex. On Linux the two implementations | 141 // succeed or not for the non-recursive mutex. On Linux the two implementations |
| 148 // are equal except we can assert the recursion count is always zero for the | 142 // are equal except we can assert the recursion count is always zero for the |
| 149 // non-recursive mutex. | 143 // non-recursive mutex. |
| 150 bool Mutex::tryLock() | 144 bool Mutex::tryLock() |
| 151 { | 145 { |
| 152 int result = pthread_mutex_trylock(&m_mutex.m_internalMutex); | 146 int result = pthread_mutex_trylock(&m_mutex.m_internalMutex); |
| 153 if (result == 0) { | 147 if (result == 0) { |
| 154 #if ENABLE(ASSERT) | |
| 155 // The Mutex class is not recursive, so the recursionCount should be | 148 // The Mutex class is not recursive, so the recursionCount should be |
| 156 // zero after getting the lock. | 149 // zero after getting the lock. |
| 157 ASSERT(!m_mutex.m_recursionCount); | 150 ASSERT(!m_mutex.m_recursionCount); |
| 158 ++m_mutex.m_recursionCount; | 151 ++m_mutex.m_recursionCount; |
| 159 #endif | |
| 160 return true; | 152 return true; |
| 161 } | 153 } |
| 162 if (result == EBUSY) | 154 if (result == EBUSY) |
| 163 return false; | 155 return false; |
| 164 | 156 |
| 165 ASSERT_NOT_REACHED(); | 157 ASSERT_NOT_REACHED(); |
| 166 return false; | 158 return false; |
| 167 } | 159 } |
| 168 | 160 |
| 169 bool RecursiveMutex::tryLock() | 161 bool RecursiveMutex::tryLock() |
| 170 { | 162 { |
| 171 int result = pthread_mutex_trylock(&m_mutex.m_internalMutex); | 163 int result = pthread_mutex_trylock(&m_mutex.m_internalMutex); |
| 172 if (result == 0) { | 164 if (result == 0) { |
| 173 #if ENABLE(ASSERT) | |
| 174 ++m_mutex.m_recursionCount; | 165 ++m_mutex.m_recursionCount; |
| 175 #endif | |
| 176 return true; | 166 return true; |
| 177 } | 167 } |
| 178 if (result == EBUSY) | 168 if (result == EBUSY) |
| 179 return false; | 169 return false; |
| 180 | 170 |
| 181 ASSERT_NOT_REACHED(); | 171 ASSERT_NOT_REACHED(); |
| 182 return false; | 172 return false; |
| 183 } | 173 } |
| 184 | 174 |
| 185 ThreadCondition::ThreadCondition() | 175 ThreadCondition::ThreadCondition() |
| 186 { | 176 { |
| 187 pthread_cond_init(&m_condition, nullptr); | 177 pthread_cond_init(&m_condition, nullptr); |
| 188 } | 178 } |
| 189 | 179 |
| 190 ThreadCondition::~ThreadCondition() | 180 ThreadCondition::~ThreadCondition() |
| 191 { | 181 { |
| 192 pthread_cond_destroy(&m_condition); | 182 pthread_cond_destroy(&m_condition); |
| 193 } | 183 } |
| 194 | 184 |
| 195 void ThreadCondition::wait(MutexBase& mutex) | 185 void ThreadCondition::wait(MutexBase& mutex) |
| 196 { | 186 { |
| 197 PlatformMutex& platformMutex = mutex.impl(); | 187 PlatformMutex& platformMutex = mutex.impl(); |
| 198 int result = pthread_cond_wait(&m_condition, &platformMutex.m_internalMutex)
; | 188 int result = pthread_cond_wait(&m_condition, &platformMutex.m_internalMutex)
; |
| 199 ASSERT_UNUSED(result, !result); | 189 ASSERT_UNUSED(result, !result); |
| 200 #if ENABLE(ASSERT) | |
| 201 ++platformMutex.m_recursionCount; | 190 ++platformMutex.m_recursionCount; |
| 202 #endif | |
| 203 } | 191 } |
| 204 | 192 |
| 205 bool ThreadCondition::timedWait(MutexBase& mutex, double absoluteTime) | 193 bool ThreadCondition::timedWait(MutexBase& mutex, double absoluteTime) |
| 206 { | 194 { |
| 207 if (absoluteTime < currentTime()) | 195 if (absoluteTime < currentTime()) |
| 208 return false; | 196 return false; |
| 209 | 197 |
| 210 if (absoluteTime > INT_MAX) { | 198 if (absoluteTime > INT_MAX) { |
| 211 wait(mutex); | 199 wait(mutex); |
| 212 return true; | 200 return true; |
| 213 } | 201 } |
| 214 | 202 |
| 215 int timeSeconds = static_cast<int>(absoluteTime); | 203 int timeSeconds = static_cast<int>(absoluteTime); |
| 216 int timeNanoseconds = static_cast<int>((absoluteTime - timeSeconds) * 1E9); | 204 int timeNanoseconds = static_cast<int>((absoluteTime - timeSeconds) * 1E9); |
| 217 | 205 |
| 218 timespec targetTime; | 206 timespec targetTime; |
| 219 targetTime.tv_sec = timeSeconds; | 207 targetTime.tv_sec = timeSeconds; |
| 220 targetTime.tv_nsec = timeNanoseconds; | 208 targetTime.tv_nsec = timeNanoseconds; |
| 221 | 209 |
| 222 PlatformMutex& platformMutex = mutex.impl(); | 210 PlatformMutex& platformMutex = mutex.impl(); |
| 223 int result = pthread_cond_timedwait(&m_condition, &platformMutex.m_internalM
utex, &targetTime); | 211 int result = pthread_cond_timedwait(&m_condition, &platformMutex.m_internalM
utex, &targetTime); |
| 224 #if ENABLE(ASSERT) | |
| 225 ++platformMutex.m_recursionCount; | 212 ++platformMutex.m_recursionCount; |
| 226 #endif | |
| 227 return result == 0; | 213 return result == 0; |
| 228 } | 214 } |
| 229 | 215 |
| 230 void ThreadCondition::signal() | 216 void ThreadCondition::signal() |
| 231 { | 217 { |
| 232 int result = pthread_cond_signal(&m_condition); | 218 int result = pthread_cond_signal(&m_condition); |
| 233 ASSERT_UNUSED(result, !result); | 219 ASSERT_UNUSED(result, !result); |
| 234 } | 220 } |
| 235 | 221 |
| 236 void ThreadCondition::broadcast() | 222 void ThreadCondition::broadcast() |
| (...skipping 17 matching lines...) Expand all Loading... |
| 254 | 240 |
| 255 void willCreateThread() | 241 void willCreateThread() |
| 256 { | 242 { |
| 257 s_threadCreated = true; | 243 s_threadCreated = true; |
| 258 } | 244 } |
| 259 #endif | 245 #endif |
| 260 | 246 |
| 261 } // namespace WTF | 247 } // namespace WTF |
| 262 | 248 |
| 263 #endif // OS(POSIX) | 249 #endif // OS(POSIX) |
| OLD | NEW |