OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/globals.h" | 5 #include "platform/globals.h" |
6 #if defined(TARGET_OS_ANDROID) | 6 #if defined(TARGET_OS_ANDROID) |
7 | 7 |
8 #include "vm/thread.h" | 8 #include "vm/thread.h" |
9 | 9 |
10 #include <errno.h> // NOLINT | 10 #include <errno.h> // NOLINT |
11 #include <sys/time.h> // NOLINT | 11 #include <sys/time.h> // NOLINT |
12 | 12 |
13 #include "platform/assert.h" | 13 #include "platform/assert.h" |
| 14 #include "vm/isolate.h" |
14 | 15 |
15 namespace dart { | 16 namespace dart { |
16 | 17 |
17 #define VALIDATE_PTHREAD_RESULT(result) \ | 18 #define VALIDATE_PTHREAD_RESULT(result) \ |
18 if (result != 0) { \ | 19 if (result != 0) { \ |
19 const int kBufferSize = 1024; \ | 20 const int kBufferSize = 1024; \ |
20 char error_message[kBufferSize]; \ | 21 char error_message[kBufferSize]; \ |
21 strerror_r(result, error_message, kBufferSize); \ | 22 strerror_r(result, error_message, kBufferSize); \ |
22 FATAL2("pthread error: %d (%s)", result, error_message); \ | 23 FATAL2("pthread error: %d (%s)", result, error_message); \ |
23 } | 24 } |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); | 186 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); |
186 VALIDATE_PTHREAD_RESULT(result); | 187 VALIDATE_PTHREAD_RESULT(result); |
187 #endif // defined(DEBUG) | 188 #endif // defined(DEBUG) |
188 | 189 |
189 result = pthread_mutex_init(data_.mutex(), &attr); | 190 result = pthread_mutex_init(data_.mutex(), &attr); |
190 // Verify that creating a pthread_mutex succeeded. | 191 // Verify that creating a pthread_mutex succeeded. |
191 VALIDATE_PTHREAD_RESULT(result); | 192 VALIDATE_PTHREAD_RESULT(result); |
192 | 193 |
193 result = pthread_mutexattr_destroy(&attr); | 194 result = pthread_mutexattr_destroy(&attr); |
194 VALIDATE_PTHREAD_RESULT(result); | 195 VALIDATE_PTHREAD_RESULT(result); |
| 196 |
| 197 // When running with assertions enabled we do track the owner. |
| 198 #if defined(DEBUG) |
| 199 ASSERT(owner_ == Isolate::Current()); |
| 200 owner_ = NULL; |
| 201 #endif // defined(DEBUG) |
195 } | 202 } |
196 | 203 |
197 | 204 |
198 Mutex::~Mutex() { | 205 Mutex::~Mutex() { |
199 int result = pthread_mutex_destroy(data_.mutex()); | 206 int result = pthread_mutex_destroy(data_.mutex()); |
200 // Verify that the pthread_mutex was destroyed. | 207 // Verify that the pthread_mutex was destroyed. |
201 VALIDATE_PTHREAD_RESULT(result); | 208 VALIDATE_PTHREAD_RESULT(result); |
| 209 |
| 210 // When running with assertions enabled we do track the owner. |
| 211 #if defined(DEBUG) |
| 212 ASSERT(owner_ == NULL); |
| 213 #endif // defined(DEBUG) |
202 } | 214 } |
203 | 215 |
204 | 216 |
205 void Mutex::Lock() { | 217 void Mutex::Lock() { |
206 int result = pthread_mutex_lock(data_.mutex()); | 218 int result = pthread_mutex_lock(data_.mutex()); |
207 // Specifically check for dead lock to help debugging. | 219 // Specifically check for dead lock to help debugging. |
208 ASSERT(result != EDEADLK); | 220 ASSERT(result != EDEADLK); |
209 ASSERT(result == 0); // Verify no other errors. | 221 ASSERT(result == 0); // Verify no other errors. |
210 // TODO(iposva): Do we need to track lock owners? | 222 // When running with assertions enabled we do track the owner. |
| 223 #if defined(DEBUG) |
| 224 owner_ = Isolate::Current(); |
| 225 #endif // defined(DEBUG) |
211 } | 226 } |
212 | 227 |
213 | 228 |
214 bool Mutex::TryLock() { | 229 bool Mutex::TryLock() { |
215 int result = pthread_mutex_trylock(data_.mutex()); | 230 int result = pthread_mutex_trylock(data_.mutex()); |
216 // Return false if the lock is busy and locking failed. | 231 // Return false if the lock is busy and locking failed. |
217 if (result == EBUSY) { | 232 if (result == EBUSY) { |
218 return false; | 233 return false; |
219 } | 234 } |
220 ASSERT(result == 0); // Verify no other errors. | 235 ASSERT(result == 0); // Verify no other errors. |
221 // TODO(iposva): Do we need to track lock owners? | 236 // When running with assertions enabled we do track the owner. |
| 237 #if defined(DEBUG) |
| 238 owner_ = Isolate::Current(); |
| 239 #endif // defined(DEBUG) |
222 return true; | 240 return true; |
223 } | 241 } |
224 | 242 |
225 | 243 |
226 void Mutex::Unlock() { | 244 void Mutex::Unlock() { |
227 // TODO(iposva): Do we need to track lock owners? | 245 // When running with assertions enabled we do track the owner. |
| 246 #if defined(DEBUG) |
| 247 ASSERT(owner_ == Isolate::Current()); |
| 248 owner_ = NULL; |
| 249 #endif // defined(DEBUG) |
228 int result = pthread_mutex_unlock(data_.mutex()); | 250 int result = pthread_mutex_unlock(data_.mutex()); |
229 // Specifically check for wrong thread unlocking to aid debugging. | 251 // Specifically check for wrong thread unlocking to aid debugging. |
230 ASSERT(result != EPERM); | 252 ASSERT(result != EPERM); |
231 ASSERT(result == 0); // Verify no other errors. | 253 ASSERT(result == 0); // Verify no other errors. |
232 } | 254 } |
233 | 255 |
234 | 256 |
235 Monitor::Monitor() { | 257 Monitor::Monitor() { |
236 pthread_mutexattr_t mutex_attr; | 258 pthread_mutexattr_t mutex_attr; |
237 int result = pthread_mutexattr_init(&mutex_attr); | 259 int result = pthread_mutexattr_init(&mutex_attr); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 | 339 |
318 void Monitor::NotifyAll() { | 340 void Monitor::NotifyAll() { |
319 // TODO(iposva): Do we need to track lock owners? | 341 // TODO(iposva): Do we need to track lock owners? |
320 int result = pthread_cond_broadcast(data_.cond()); | 342 int result = pthread_cond_broadcast(data_.cond()); |
321 VALIDATE_PTHREAD_RESULT(result); | 343 VALIDATE_PTHREAD_RESULT(result); |
322 } | 344 } |
323 | 345 |
324 } // namespace dart | 346 } // namespace dart |
325 | 347 |
326 #endif // defined(TARGET_OS_ANDROID) | 348 #endif // defined(TARGET_OS_ANDROID) |
OLD | NEW |