| 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/thread.h" | 5 #include "platform/thread.h" |
| 6 | 6 |
| 7 #include <process.h> | 7 #include <process.h> |
| 8 | 8 |
| 9 #include "platform/assert.h" | 9 #include "platform/assert.h" |
| 10 | 10 |
| 11 namespace dart { | 11 namespace dart { |
| 12 | 12 |
| 13 const ThreadHandle Thread::kInvalidThreadHandle = 0; |
| 14 |
| 13 class ThreadStartData { | 15 class ThreadStartData { |
| 14 public: | 16 public: |
| 15 ThreadStartData(Thread::ThreadStartFunction function, | 17 ThreadStartData(Thread::ThreadStartFunction function, |
| 16 uword parameter, | 18 uword parameter) |
| 17 Thread* thread) | 19 : function_(function), parameter_(parameter) {} |
| 18 : function_(function), parameter_(parameter), thread_(thread) {} | |
| 19 | 20 |
| 20 Thread::ThreadStartFunction function() const { return function_; } | 21 Thread::ThreadStartFunction function() const { return function_; } |
| 21 uword parameter() const { return parameter_; } | 22 uword parameter() const { return parameter_; } |
| 22 Thread* thread() const { return thread_; } | |
| 23 | 23 |
| 24 private: | 24 private: |
| 25 Thread::ThreadStartFunction function_; | 25 Thread::ThreadStartFunction function_; |
| 26 uword parameter_; | 26 uword parameter_; |
| 27 Thread* thread_; | |
| 28 | 27 |
| 29 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); | 28 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); |
| 30 }; | 29 }; |
| 31 | 30 |
| 32 | 31 |
| 33 // Dispatch to the thread start function provided by the caller. This trampoline | 32 // Dispatch to the thread start function provided by the caller. This trampoline |
| 34 // is used to ensure that the thread is properly destroyed if the thread just | 33 // is used to ensure that the thread is properly destroyed if the thread just |
| 35 // exits. | 34 // exits. |
| 36 static unsigned int __stdcall ThreadEntry(void* data_ptr) { | 35 static unsigned int __stdcall ThreadEntry(void* data_ptr) { |
| 37 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); | 36 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); |
| 38 | 37 |
| 39 Thread::ThreadStartFunction function = data->function(); | 38 Thread::ThreadStartFunction function = data->function(); |
| 40 uword parameter = data->parameter(); | 39 uword parameter = data->parameter(); |
| 41 Thread* thread = data->thread(); | |
| 42 delete data; | 40 delete data; |
| 43 | 41 |
| 44 // Call the supplied thread start function handing it its parameters. | 42 // Call the supplied thread start function handing it its parameters. |
| 45 function(parameter); | 43 function(parameter); |
| 46 | 44 |
| 47 // When the function returns here, make sure that the thread is deleted. | 45 // When the function returns here, make sure to close the handle. |
| 48 delete thread; | 46 CloseHandle(GetCurrentThread()); |
| 49 | 47 |
| 50 return 0; | 48 return 0; |
| 51 } | 49 } |
| 52 | 50 |
| 53 | 51 |
| 54 Thread::Thread(ThreadStartFunction function, uword parameter) { | 52 ThreadHandle Thread::Start(ThreadStartFunction function, uword parameter) { |
| 55 ThreadStartData* start_data = new ThreadStartData(function, parameter, this); | 53 ThreadStartData* start_data = new ThreadStartData(function, parameter); |
| 56 uint32_t tid; | 54 uint32_t tid; |
| 57 data_.thread_handle_ = | 55 ThreadHandle thread = |
| 58 _beginthreadex(NULL, 64 * KB, ThreadEntry, start_data, 0, &tid); | 56 _beginthreadex(NULL, 64 * KB, ThreadEntry, start_data, 0, &tid); |
| 59 if (data_.thread_handle_ == -1) { | 57 if (thread == kInvalidThreadHandle) { |
| 60 FATAL("Thread creation failed"); | 58 FATAL("Thread creation failed"); |
| 61 } | 59 } |
| 62 data_.tid_ = tid; | 60 return thread; |
| 63 } | 61 } |
| 64 | 62 |
| 65 | 63 |
| 66 Thread::~Thread() { | 64 bool Thread::Join(ThreadHandle thread, int64_t millis) { |
| 67 CloseHandle(reinterpret_cast<HANDLE>(data_.thread_handle_)); | 65 HANDLE handle = reinterpret_cast<HANDLE>(thread); |
| 66 if (millis == kNoTimeout) { |
| 67 DWORD result = WaitForSingleObject(handle, INFINITE); |
| 68 if (result != WAIT_OBJECT_0) { |
| 69 FATAL("Thread join failed"); |
| 70 } |
| 71 return true; |
| 72 } else { |
| 73 DWORD result = WaitForSingleObject(handle, millis); |
| 74 return (result == WAIT_OBJECT_0); |
| 75 } |
| 68 } | 76 } |
| 69 | 77 |
| 70 | 78 |
| 71 Mutex::Mutex() { | 79 Mutex::Mutex() { |
| 72 // Allocate unnamed semaphore with initial count 1 and max count 1. | 80 // Allocate unnamed semaphore with initial count 1 and max count 1. |
| 73 data_.semaphore_ = CreateSemaphore(NULL, 1, 1, NULL); | 81 data_.semaphore_ = CreateSemaphore(NULL, 1, 1, NULL); |
| 74 if (data_.semaphore_ == NULL) { | 82 if (data_.semaphore_ == NULL) { |
| 75 FATAL("Mutex allocation failed"); | 83 FATAL("Mutex allocation failed"); |
| 76 } | 84 } |
| 77 } | 85 } |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 void Monitor::Notify() { | 171 void Monitor::Notify() { |
| 164 WakeConditionVariable(&data_.cond_); | 172 WakeConditionVariable(&data_.cond_); |
| 165 } | 173 } |
| 166 | 174 |
| 167 | 175 |
| 168 void Monitor::NotifyAll() { | 176 void Monitor::NotifyAll() { |
| 169 WakeAllConditionVariable(&data_.cond_); | 177 WakeAllConditionVariable(&data_.cond_); |
| 170 } | 178 } |
| 171 | 179 |
| 172 } // namespace dart | 180 } // namespace dart |
| OLD | NEW |