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