| 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 class ThreadStartData { | 13 class ThreadStartData { | 
| 14  public: | 14  public: | 
| 15   ThreadStartData(Thread::ThreadStartFunction function, | 15   ThreadStartData(Thread::ThreadStartFunction function, uword parameter) | 
| 16                   uword parameter, | 16       : function_(function), parameter_(parameter) {} | 
| 17                   Thread* thread) |  | 
| 18       : function_(function), parameter_(parameter), thread_(thread) {} |  | 
| 19 | 17 | 
| 20   Thread::ThreadStartFunction function() const { return function_; } | 18   Thread::ThreadStartFunction function() const { return function_; } | 
| 21   uword parameter() const { return parameter_; } | 19   uword parameter() const { return parameter_; } | 
| 22   Thread* thread() const { return thread_; } |  | 
| 23 | 20 | 
| 24  private: | 21  private: | 
| 25   Thread::ThreadStartFunction function_; | 22   Thread::ThreadStartFunction function_; | 
| 26   uword parameter_; | 23   uword parameter_; | 
| 27   Thread* thread_; |  | 
| 28 | 24 | 
| 29   DISALLOW_COPY_AND_ASSIGN(ThreadStartData); | 25   DISALLOW_COPY_AND_ASSIGN(ThreadStartData); | 
| 30 }; | 26 }; | 
| 31 | 27 | 
| 32 | 28 | 
| 33 // Dispatch to the thread start function provided by the caller. This trampoline | 29 // 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 | 30 // is used to ensure that the thread is properly destroyed if the thread just | 
| 35 // exits. | 31 // exits. | 
| 36 static unsigned int __stdcall ThreadEntry(void* data_ptr) { | 32 static unsigned int __stdcall ThreadEntry(void* data_ptr) { | 
| 37   ThreadStartData* data =  reinterpret_cast<ThreadStartData*>(data_ptr); | 33   ThreadStartData* data =  reinterpret_cast<ThreadStartData*>(data_ptr); | 
| 38 | 34 | 
| 39   Thread::ThreadStartFunction function = data->function(); | 35   Thread::ThreadStartFunction function = data->function(); | 
| 40   uword parameter = data->parameter(); | 36   uword parameter = data->parameter(); | 
| 41   Thread* thread = data->thread(); |  | 
| 42   delete data; | 37   delete data; | 
| 43 | 38 | 
| 44   // Call the supplied thread start function handing it its parameters. | 39   // Call the supplied thread start function handing it its parameters. | 
| 45   function(parameter); | 40   function(parameter); | 
| 46 | 41 | 
| 47   // When the function returns here, make sure that the thread is deleted. | 42   // When the function returns here close the handle. | 
| 48   delete thread; | 43   CloseHandle(GetCurrentThread()); | 
| 49 | 44 | 
| 50   return 0; | 45   return 0; | 
| 51 } | 46 } | 
| 52 | 47 | 
| 53 | 48 | 
| 54 Thread::Thread(ThreadStartFunction function, uword parameter) { | 49 int Thread::Start(ThreadStartFunction function, uword parameter) { | 
| 55   ThreadStartData* start_data = new ThreadStartData(function, parameter, this); | 50   ThreadStartData* start_data = new ThreadStartData(function, parameter); | 
| 56   uint32_t tid; | 51   uint32_t tid; | 
| 57   data_.thread_handle_ = | 52   uintptr_t thread = | 
| 58     _beginthreadex(NULL, 64 * KB, ThreadEntry, start_data, 0, &tid); | 53       _beginthreadex(NULL, 64 * KB, ThreadEntry, start_data, 0, &tid); | 
| 59   if (data_.thread_handle_ == -1) { | 54   if (thread == -1L || thread == 0) { | 
| 60     FATAL("Thread creation failed"); | 55 #ifdef DEBUG | 
|  | 56     fprintf(stderr, "_beginthreadex error: %d (%s)\n", errno, strerror(errno)); | 
|  | 57 #endif | 
|  | 58     return errno; | 
| 61   } | 59   } | 
| 62   data_.tid_ = tid; | 60 | 
|  | 61   return 0; | 
| 63 } | 62 } | 
| 64 | 63 | 
| 65 | 64 | 
| 66 Thread::~Thread() { |  | 
| 67   CloseHandle(reinterpret_cast<HANDLE>(data_.thread_handle_)); |  | 
| 68 } |  | 
| 69 |  | 
| 70 |  | 
| 71 Mutex::Mutex() { | 65 Mutex::Mutex() { | 
| 72   // Allocate unnamed semaphore with initial count 1 and max count 1. | 66   // Allocate unnamed semaphore with initial count 1 and max count 1. | 
| 73   data_.semaphore_ = CreateSemaphore(NULL, 1, 1, NULL); | 67   data_.semaphore_ = CreateSemaphore(NULL, 1, 1, NULL); | 
| 74   if (data_.semaphore_ == NULL) { | 68   if (data_.semaphore_ == NULL) { | 
| 75     FATAL("Mutex allocation failed"); | 69     FATAL("Mutex allocation failed"); | 
| 76   } | 70   } | 
| 77 } | 71 } | 
| 78 | 72 | 
| 79 | 73 | 
| 80 Mutex::~Mutex() { | 74 Mutex::~Mutex() { | 
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 163 void Monitor::Notify() { | 157 void Monitor::Notify() { | 
| 164   WakeConditionVariable(&data_.cond_); | 158   WakeConditionVariable(&data_.cond_); | 
| 165 } | 159 } | 
| 166 | 160 | 
| 167 | 161 | 
| 168 void Monitor::NotifyAll() { | 162 void Monitor::NotifyAll() { | 
| 169   WakeAllConditionVariable(&data_.cond_); | 163   WakeAllConditionVariable(&data_.cond_); | 
| 170 } | 164 } | 
| 171 | 165 | 
| 172 }  // namespace dart | 166 }  // namespace dart | 
| OLD | NEW | 
|---|