| 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_WINDOWS) | 6 #if defined(TARGET_OS_WINDOWS) |
| 7 | 7 |
| 8 #include "bin/thread.h" | 8 #include "bin/thread.h" |
| 9 #include "bin/thread_win.h" |
| 9 | 10 |
| 10 #include <process.h> // NOLINT | 11 #include <process.h> // NOLINT |
| 11 | 12 |
| 12 #include "platform/assert.h" | 13 #include "platform/assert.h" |
| 13 | 14 |
| 14 namespace dart { | 15 namespace dart { |
| 15 namespace bin { | 16 namespace bin { |
| 16 | 17 |
| 17 class ThreadStartData { | 18 class ThreadStartData { |
| 18 public: | 19 public: |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 | 51 |
| 51 return 0; | 52 return 0; |
| 52 } | 53 } |
| 53 | 54 |
| 54 | 55 |
| 55 int Thread::Start(ThreadStartFunction function, uword parameter) { | 56 int Thread::Start(ThreadStartFunction function, uword parameter) { |
| 56 ThreadStartData* start_data = new ThreadStartData(function, parameter); | 57 ThreadStartData* start_data = new ThreadStartData(function, parameter); |
| 57 uint32_t tid; | 58 uint32_t tid; |
| 58 uintptr_t thread = _beginthreadex(NULL, Thread::GetMaxStackSize(), | 59 uintptr_t thread = _beginthreadex(NULL, Thread::GetMaxStackSize(), |
| 59 ThreadEntry, start_data, 0, &tid); | 60 ThreadEntry, start_data, 0, &tid); |
| 60 if (thread == -1L || thread == 0) { | 61 if ((thread == -1L) || (thread == 0)) { |
| 61 #ifdef DEBUG | 62 #ifdef DEBUG |
| 62 fprintf(stderr, "_beginthreadex error: %d (%s)\n", errno, strerror(errno)); | 63 fprintf(stderr, "_beginthreadex error: %d (%s)\n", errno, strerror(errno)); |
| 63 #endif | 64 #endif |
| 64 return errno; | 65 return errno; |
| 65 } | 66 } |
| 66 | 67 |
| 67 // Close the handle, so we don't leak the thread object. | 68 // Close the handle, so we don't leak the thread object. |
| 68 CloseHandle(reinterpret_cast<HANDLE>(thread)); | 69 CloseHandle(reinterpret_cast<HANDLE>(thread)); |
| 69 | 70 |
| 70 return 0; | 71 return 0; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 } | 124 } |
| 124 | 125 |
| 125 | 126 |
| 126 intptr_t Thread::ThreadIdToIntPtr(ThreadId id) { | 127 intptr_t Thread::ThreadIdToIntPtr(ThreadId id) { |
| 127 ASSERT(sizeof(id) <= sizeof(intptr_t)); | 128 ASSERT(sizeof(id) <= sizeof(intptr_t)); |
| 128 return static_cast<intptr_t>(id); | 129 return static_cast<intptr_t>(id); |
| 129 } | 130 } |
| 130 | 131 |
| 131 | 132 |
| 132 bool Thread::Compare(ThreadId a, ThreadId b) { | 133 bool Thread::Compare(ThreadId a, ThreadId b) { |
| 133 return a == b; | 134 return (a == b); |
| 134 } | 135 } |
| 135 | 136 |
| 136 | 137 |
| 137 void Thread::GetThreadCpuUsage(ThreadId thread_id, int64_t* cpu_usage) { | 138 void Thread::GetThreadCpuUsage(ThreadId thread_id, int64_t* cpu_usage) { |
| 138 static const int64_t kTimeEpoc = 116444736000000000LL; | 139 static const int64_t kTimeEpoc = 116444736000000000LL; |
| 139 static const int64_t kTimeScaler = 10; // 100 ns to us. | 140 static const int64_t kTimeScaler = 10; // 100 ns to us. |
| 140 // Although win32 uses 64-bit integers for representing timestamps, | 141 // Although win32 uses 64-bit integers for representing timestamps, |
| 141 // these are packed into a FILETIME structure. The FILETIME | 142 // these are packed into a FILETIME structure. The FILETIME |
| 142 // structure is just a struct representing a 64-bit integer. The | 143 // structure is just a struct representing a 64-bit integer. The |
| 143 // TimeStamp union allows access to both a FILETIME and an integer | 144 // TimeStamp union allows access to both a FILETIME and an integer |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 } | 203 } |
| 203 } | 204 } |
| 204 | 205 |
| 205 | 206 |
| 206 bool Mutex::TryLock() { | 207 bool Mutex::TryLock() { |
| 207 // Attempt to pass the semaphore but return immediately. | 208 // Attempt to pass the semaphore but return immediately. |
| 208 DWORD result = WaitForSingleObject(data_.semaphore_, 0); | 209 DWORD result = WaitForSingleObject(data_.semaphore_, 0); |
| 209 if (result == WAIT_OBJECT_0) { | 210 if (result == WAIT_OBJECT_0) { |
| 210 return true; | 211 return true; |
| 211 } | 212 } |
| 212 if (result == WAIT_ABANDONED || result == WAIT_FAILED) { | 213 if ((result == WAIT_ABANDONED) || (result == WAIT_FAILED)) { |
| 213 FATAL1("Mutex try lock failed %d", GetLastError()); | 214 FATAL1("Mutex try lock failed %d", GetLastError()); |
| 214 } | 215 } |
| 215 ASSERT(result == WAIT_TIMEOUT); | 216 ASSERT(result == WAIT_TIMEOUT); |
| 216 return false; | 217 return false; |
| 217 } | 218 } |
| 218 | 219 |
| 219 | 220 |
| 220 void Mutex::Unlock() { | 221 void Mutex::Unlock() { |
| 221 BOOL result = ReleaseSemaphore(data_.semaphore_, 1, NULL); | 222 BOOL result = ReleaseSemaphore(data_.semaphore_, 1, NULL); |
| 222 if (result == 0) { | 223 if (result == 0) { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 } | 267 } |
| 267 } | 268 } |
| 268 | 269 |
| 269 | 270 |
| 270 void MonitorData::AddWaiter(MonitorWaitData* wait_data) { | 271 void MonitorData::AddWaiter(MonitorWaitData* wait_data) { |
| 271 // Add the MonitorWaitData object to the list of objects waiting for | 272 // Add the MonitorWaitData object to the list of objects waiting for |
| 272 // this monitor. | 273 // this monitor. |
| 273 EnterCriticalSection(&waiters_cs_); | 274 EnterCriticalSection(&waiters_cs_); |
| 274 if (waiters_tail_ == NULL) { | 275 if (waiters_tail_ == NULL) { |
| 275 ASSERT(waiters_head_ == NULL); | 276 ASSERT(waiters_head_ == NULL); |
| 276 waiters_head_ = waiters_tail_ = wait_data; | 277 waiters_head_ = wait_data; |
| 278 waiters_tail_ = wait_data; |
| 277 } else { | 279 } else { |
| 278 waiters_tail_->next_ = wait_data; | 280 waiters_tail_->next_ = wait_data; |
| 279 waiters_tail_ = wait_data; | 281 waiters_tail_ = wait_data; |
| 280 } | 282 } |
| 281 LeaveCriticalSection(&waiters_cs_); | 283 LeaveCriticalSection(&waiters_cs_); |
| 282 } | 284 } |
| 283 | 285 |
| 284 | 286 |
| 285 void MonitorData::RemoveWaiter(MonitorWaitData* wait_data) { | 287 void MonitorData::RemoveWaiter(MonitorWaitData* wait_data) { |
| 286 // Remove the MonitorWaitData object from the list of objects | 288 // Remove the MonitorWaitData object from the list of objects |
| 287 // waiting for this monitor. | 289 // waiting for this monitor. |
| 288 EnterCriticalSection(&waiters_cs_); | 290 EnterCriticalSection(&waiters_cs_); |
| 289 MonitorWaitData* previous = NULL; | 291 MonitorWaitData* previous = NULL; |
| 290 MonitorWaitData* current = waiters_head_; | 292 MonitorWaitData* current = waiters_head_; |
| 291 while (current != NULL) { | 293 while (current != NULL) { |
| 292 if (current == wait_data) { | 294 if (current == wait_data) { |
| 293 if (waiters_head_ == waiters_tail_) { | 295 if (waiters_head_ == waiters_tail_) { |
| 294 waiters_head_ = waiters_tail_ = NULL; | 296 waiters_head_ = NULL; |
| 297 waiters_tail_ = NULL; |
| 295 } else if (current == waiters_head_) { | 298 } else if (current == waiters_head_) { |
| 296 waiters_head_ = waiters_head_->next_; | 299 waiters_head_ = waiters_head_->next_; |
| 297 } else if (current == waiters_tail_) { | 300 } else if (current == waiters_tail_) { |
| 298 ASSERT(previous != NULL); | 301 ASSERT(previous != NULL); |
| 299 waiters_tail_ = previous; | 302 waiters_tail_ = previous; |
| 300 previous->next_ = NULL; | 303 previous->next_ = NULL; |
| 301 } else { | 304 } else { |
| 302 ASSERT(previous != NULL); | 305 ASSERT(previous != NULL); |
| 303 previous->next_ = current->next_; | 306 previous->next_ = current->next_; |
| 304 } | 307 } |
| 305 // Clear next. | 308 // Clear next. |
| 306 wait_data->next_ = NULL; | 309 wait_data->next_ = NULL; |
| 307 break; | 310 break; |
| 308 } | 311 } |
| 309 previous = current; | 312 previous = current; |
| 310 current = current->next_; | 313 current = current->next_; |
| 311 } | 314 } |
| 312 LeaveCriticalSection(&waiters_cs_); | 315 LeaveCriticalSection(&waiters_cs_); |
| 313 } | 316 } |
| 314 | 317 |
| 315 | 318 |
| 316 void MonitorData::SignalAndRemoveFirstWaiter() { | 319 void MonitorData::SignalAndRemoveFirstWaiter() { |
| 317 EnterCriticalSection(&waiters_cs_); | 320 EnterCriticalSection(&waiters_cs_); |
| 318 MonitorWaitData* first = waiters_head_; | 321 MonitorWaitData* first = waiters_head_; |
| 319 if (first != NULL) { | 322 if (first != NULL) { |
| 320 // Remove from list. | 323 // Remove from list. |
| 321 if (waiters_head_ == waiters_tail_) { | 324 if (waiters_head_ == waiters_tail_) { |
| 322 waiters_tail_ = waiters_head_ = NULL; | 325 waiters_tail_ = NULL; |
| 326 waiters_head_ = NULL; |
| 323 } else { | 327 } else { |
| 324 waiters_head_ = waiters_head_->next_; | 328 waiters_head_ = waiters_head_->next_; |
| 325 } | 329 } |
| 326 // Clear next. | 330 // Clear next. |
| 327 first->next_ = NULL; | 331 first->next_ = NULL; |
| 328 // Signal event. | 332 // Signal event. |
| 329 BOOL result = SetEvent(first->event_); | 333 BOOL result = SetEvent(first->event_); |
| 330 if (result == 0) { | 334 if (result == 0) { |
| 331 FATAL1("Monitor::Notify failed to signal event %d", GetLastError()); | 335 FATAL1("Monitor::Notify failed to signal event %d", GetLastError()); |
| 332 } | 336 } |
| 333 } | 337 } |
| 334 LeaveCriticalSection(&waiters_cs_); | 338 LeaveCriticalSection(&waiters_cs_); |
| 335 } | 339 } |
| 336 | 340 |
| 337 | 341 |
| 338 void MonitorData::SignalAndRemoveAllWaiters() { | 342 void MonitorData::SignalAndRemoveAllWaiters() { |
| 339 EnterCriticalSection(&waiters_cs_); | 343 EnterCriticalSection(&waiters_cs_); |
| 340 // Extract list to signal. | 344 // Extract list to signal. |
| 341 MonitorWaitData* current = waiters_head_; | 345 MonitorWaitData* current = waiters_head_; |
| 342 // Clear list. | 346 // Clear list. |
| 343 waiters_head_ = waiters_tail_ = NULL; | 347 waiters_head_ = NULL; |
| 348 waiters_tail_ = NULL; |
| 344 // Iterate and signal all events. | 349 // Iterate and signal all events. |
| 345 while (current != NULL) { | 350 while (current != NULL) { |
| 346 // Copy next. | 351 // Copy next. |
| 347 MonitorWaitData* next = current->next_; | 352 MonitorWaitData* next = current->next_; |
| 348 // Clear next. | 353 // Clear next. |
| 349 current->next_ = NULL; | 354 current->next_ = NULL; |
| 350 // Signal event. | 355 // Signal event. |
| 351 BOOL result = SetEvent(current->event_); | 356 BOOL result = SetEvent(current->event_); |
| 352 if (result == 0) { | 357 if (result == 0) { |
| 353 FATAL1("Failed to set event for NotifyAll %d", GetLastError()); | 358 FATAL1("Failed to set event for NotifyAll %d", GetLastError()); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 // signal. This will be treated as a spurious wake-up and is OK | 453 // signal. This will be treated as a spurious wake-up and is OK |
| 449 // since all uses of monitors should recheck the condition after a | 454 // since all uses of monitors should recheck the condition after a |
| 450 // Wait. | 455 // Wait. |
| 451 data_.SignalAndRemoveAllWaiters(); | 456 data_.SignalAndRemoveAllWaiters(); |
| 452 } | 457 } |
| 453 | 458 |
| 454 } // namespace bin | 459 } // namespace bin |
| 455 } // namespace dart | 460 } // namespace dart |
| 456 | 461 |
| 457 #endif // defined(TARGET_OS_WINDOWS) | 462 #endif // defined(TARGET_OS_WINDOWS) |
| OLD | NEW |