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" // NOLINT |
6 #if defined(TARGET_OS_WINDOWS) | 6 #if defined(TARGET_OS_WINDOWS) |
7 | 7 |
8 #include "vm/thread.h" | 8 #include "vm/os_thread.h" |
9 | 9 |
10 #include <process.h> // NOLINT | 10 #include <process.h> // NOLINT |
11 | 11 |
12 #include "platform/assert.h" | 12 #include "platform/assert.h" |
13 #include "vm/isolate.h" | 13 #include "vm/isolate.h" |
14 | 14 |
15 namespace dart { | 15 namespace dart { |
16 | 16 |
17 class ThreadStartData { | 17 class ThreadStartData { |
18 public: | 18 public: |
19 ThreadStartData(Thread::ThreadStartFunction function, uword parameter) | 19 ThreadStartData(OSThread::ThreadStartFunction function, uword parameter) |
20 : function_(function), parameter_(parameter) {} | 20 : function_(function), parameter_(parameter) {} |
21 | 21 |
22 Thread::ThreadStartFunction function() const { return function_; } | 22 OSThread::ThreadStartFunction function() const { return function_; } |
23 uword parameter() const { return parameter_; } | 23 uword parameter() const { return parameter_; } |
24 | 24 |
25 private: | 25 private: |
26 Thread::ThreadStartFunction function_; | 26 OSThread::ThreadStartFunction function_; |
27 uword parameter_; | 27 uword parameter_; |
28 | 28 |
29 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); | 29 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); |
30 }; | 30 }; |
31 | 31 |
32 | 32 |
33 // Dispatch to the thread start function provided by the caller. This trampoline | 33 // 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 | 34 // is used to ensure that the thread is properly destroyed if the thread just |
35 // exits. | 35 // exits. |
36 static unsigned int __stdcall ThreadEntry(void* data_ptr) { | 36 static unsigned int __stdcall ThreadEntry(void* data_ptr) { |
37 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); | 37 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); |
38 | 38 |
39 Thread::ThreadStartFunction function = data->function(); | 39 OSThread::ThreadStartFunction function = data->function(); |
40 uword parameter = data->parameter(); | 40 uword parameter = data->parameter(); |
41 delete data; | 41 delete data; |
42 | 42 |
43 MonitorData::GetMonitorWaitDataForThread(); | 43 MonitorData::GetMonitorWaitDataForThread(); |
44 | 44 |
45 // Call the supplied thread start function handing it its parameters. | 45 // Call the supplied thread start function handing it its parameters. |
46 function(parameter); | 46 function(parameter); |
47 | 47 |
48 // Clean up the monitor wait data for this thread. | 48 // Clean up the monitor wait data for this thread. |
49 MonitorWaitData::ThreadExit(); | 49 MonitorWaitData::ThreadExit(); |
50 | 50 |
51 return 0; | 51 return 0; |
52 } | 52 } |
53 | 53 |
54 | 54 |
55 int Thread::Start(ThreadStartFunction function, uword parameter) { | 55 int OSThread::Start(ThreadStartFunction function, uword parameter) { |
56 ThreadStartData* start_data = new ThreadStartData(function, parameter); | 56 ThreadStartData* start_data = new ThreadStartData(function, parameter); |
57 uint32_t tid; | 57 uint32_t tid; |
58 uintptr_t thread = _beginthreadex(NULL, Thread::GetMaxStackSize(), | 58 uintptr_t thread = _beginthreadex(NULL, OSThread::GetMaxStackSize(), |
59 ThreadEntry, start_data, 0, &tid); | 59 ThreadEntry, start_data, 0, &tid); |
60 if (thread == -1L || thread == 0) { | 60 if (thread == -1L || thread == 0) { |
61 #ifdef DEBUG | 61 #ifdef DEBUG |
62 fprintf(stderr, "_beginthreadex error: %d (%s)\n", errno, strerror(errno)); | 62 fprintf(stderr, "_beginthreadex error: %d (%s)\n", errno, strerror(errno)); |
63 #endif | 63 #endif |
64 return errno; | 64 return errno; |
65 } | 65 } |
66 | 66 |
67 // Close the handle, so we don't leak the thread object. | 67 // Close the handle, so we don't leak the thread object. |
68 CloseHandle(reinterpret_cast<HANDLE>(thread)); | 68 CloseHandle(reinterpret_cast<HANDLE>(thread)); |
69 | 69 |
70 return 0; | 70 return 0; |
71 } | 71 } |
72 | 72 |
73 ThreadLocalKey Thread::kUnsetThreadLocalKey = TLS_OUT_OF_INDEXES; | 73 ThreadLocalKey OSThread::kUnsetThreadLocalKey = TLS_OUT_OF_INDEXES; |
74 ThreadId Thread::kInvalidThreadId = 0; | 74 ThreadId OSThread::kInvalidThreadId = 0; |
75 | 75 |
76 ThreadLocalKey Thread::CreateThreadLocal() { | 76 ThreadLocalKey OSThread::CreateThreadLocal() { |
77 ThreadLocalKey key = TlsAlloc(); | 77 ThreadLocalKey key = TlsAlloc(); |
78 if (key == kUnsetThreadLocalKey) { | 78 if (key == kUnsetThreadLocalKey) { |
79 FATAL1("TlsAlloc failed %d", GetLastError()); | 79 FATAL1("TlsAlloc failed %d", GetLastError()); |
80 } | 80 } |
81 return key; | 81 return key; |
82 } | 82 } |
83 | 83 |
84 | 84 |
85 void Thread::DeleteThreadLocal(ThreadLocalKey key) { | 85 void OSThread::DeleteThreadLocal(ThreadLocalKey key) { |
86 ASSERT(key != kUnsetThreadLocalKey); | 86 ASSERT(key != kUnsetThreadLocalKey); |
87 BOOL result = TlsFree(key); | 87 BOOL result = TlsFree(key); |
88 if (!result) { | 88 if (!result) { |
89 FATAL1("TlsFree failed %d", GetLastError()); | 89 FATAL1("TlsFree failed %d", GetLastError()); |
90 } | 90 } |
91 } | 91 } |
92 | 92 |
93 | 93 |
94 intptr_t Thread::GetMaxStackSize() { | 94 intptr_t OSThread::GetMaxStackSize() { |
95 const int kStackSize = (128 * kWordSize * KB); | 95 const int kStackSize = (128 * kWordSize * KB); |
96 return kStackSize; | 96 return kStackSize; |
97 } | 97 } |
98 | 98 |
99 | 99 |
100 ThreadId Thread::GetCurrentThreadId() { | 100 ThreadId OSThread::GetCurrentThreadId() { |
101 return ::GetCurrentThreadId(); | 101 return ::GetCurrentThreadId(); |
102 } | 102 } |
103 | 103 |
104 | 104 |
105 bool Thread::Join(ThreadId id) { | 105 bool OSThread::Join(ThreadId id) { |
106 HANDLE handle = OpenThread(SYNCHRONIZE, false, id); | 106 HANDLE handle = OpenThread(SYNCHRONIZE, false, id); |
107 if (handle == INVALID_HANDLE_VALUE) { | 107 if (handle == INVALID_HANDLE_VALUE) { |
108 return false; | 108 return false; |
109 } | 109 } |
110 DWORD res = WaitForSingleObject(handle, INFINITE); | 110 DWORD res = WaitForSingleObject(handle, INFINITE); |
111 CloseHandle(handle); | 111 CloseHandle(handle); |
112 return res == WAIT_OBJECT_0; | 112 return res == WAIT_OBJECT_0; |
113 } | 113 } |
114 | 114 |
115 | 115 |
116 intptr_t Thread::ThreadIdToIntPtr(ThreadId id) { | 116 intptr_t OSThread::ThreadIdToIntPtr(ThreadId id) { |
117 ASSERT(sizeof(id) <= sizeof(intptr_t)); | 117 ASSERT(sizeof(id) <= sizeof(intptr_t)); |
118 return static_cast<intptr_t>(id); | 118 return static_cast<intptr_t>(id); |
119 } | 119 } |
120 | 120 |
121 | 121 |
122 bool Thread::Compare(ThreadId a, ThreadId b) { | 122 bool OSThread::Compare(ThreadId a, ThreadId b) { |
123 return a == b; | 123 return a == b; |
124 } | 124 } |
125 | 125 |
126 | 126 |
127 void Thread::GetThreadCpuUsage(ThreadId thread_id, int64_t* cpu_usage) { | 127 void OSThread::GetThreadCpuUsage(ThreadId thread_id, int64_t* cpu_usage) { |
128 static const int64_t kTimeEpoc = 116444736000000000LL; | 128 static const int64_t kTimeEpoc = 116444736000000000LL; |
129 static const int64_t kTimeScaler = 10; // 100 ns to us. | 129 static const int64_t kTimeScaler = 10; // 100 ns to us. |
130 // Although win32 uses 64-bit integers for representing timestamps, | 130 // Although win32 uses 64-bit integers for representing timestamps, |
131 // these are packed into a FILETIME structure. The FILETIME | 131 // these are packed into a FILETIME structure. The FILETIME |
132 // structure is just a struct representing a 64-bit integer. The | 132 // structure is just a struct representing a 64-bit integer. The |
133 // TimeStamp union allows access to both a FILETIME and an integer | 133 // TimeStamp union allows access to both a FILETIME and an integer |
134 // representation of the timestamp. The Windows timestamp is in | 134 // representation of the timestamp. The Windows timestamp is in |
135 // 100-nanosecond intervals since January 1, 1601. | 135 // 100-nanosecond intervals since January 1, 1601. |
136 union TimeStamp { | 136 union TimeStamp { |
137 FILETIME ft_; | 137 FILETIME ft_; |
(...skipping 11 matching lines...) Expand all Loading... |
149 &kernel.ft_, | 149 &kernel.ft_, |
150 &user.ft_); | 150 &user.ft_); |
151 CloseHandle(handle); | 151 CloseHandle(handle); |
152 if (!result) { | 152 if (!result) { |
153 FATAL1("GetThreadCpuUsage failed %d\n", GetLastError()); | 153 FATAL1("GetThreadCpuUsage failed %d\n", GetLastError()); |
154 } | 154 } |
155 *cpu_usage = (user.t_ - kTimeEpoc) / kTimeScaler; | 155 *cpu_usage = (user.t_ - kTimeEpoc) / kTimeScaler; |
156 } | 156 } |
157 | 157 |
158 | 158 |
159 void Thread::SetThreadLocal(ThreadLocalKey key, uword value) { | 159 void OSThread::SetThreadLocal(ThreadLocalKey key, uword value) { |
160 ASSERT(key != kUnsetThreadLocalKey); | 160 ASSERT(key != kUnsetThreadLocalKey); |
161 BOOL result = TlsSetValue(key, reinterpret_cast<void*>(value)); | 161 BOOL result = TlsSetValue(key, reinterpret_cast<void*>(value)); |
162 if (!result) { | 162 if (!result) { |
163 FATAL1("TlsSetValue failed %d", GetLastError()); | 163 FATAL1("TlsSetValue failed %d", GetLastError()); |
164 } | 164 } |
165 } | 165 } |
166 | 166 |
167 | 167 |
168 Mutex::Mutex() { | 168 Mutex::Mutex() { |
169 // Allocate unnamed semaphore with initial count 1 and max count 1. | 169 // Allocate unnamed semaphore with initial count 1 and max count 1. |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 owner_ = NULL; | 224 owner_ = NULL; |
225 #endif // defined(DEBUG) | 225 #endif // defined(DEBUG) |
226 BOOL result = ReleaseSemaphore(data_.semaphore_, 1, NULL); | 226 BOOL result = ReleaseSemaphore(data_.semaphore_, 1, NULL); |
227 if (result == 0) { | 227 if (result == 0) { |
228 FATAL1("Mutex unlock failed %d", GetLastError()); | 228 FATAL1("Mutex unlock failed %d", GetLastError()); |
229 } | 229 } |
230 } | 230 } |
231 | 231 |
232 | 232 |
233 ThreadLocalKey MonitorWaitData::monitor_wait_data_key_ = | 233 ThreadLocalKey MonitorWaitData::monitor_wait_data_key_ = |
234 Thread::kUnsetThreadLocalKey; | 234 OSThread::kUnsetThreadLocalKey; |
235 | 235 |
236 | 236 |
237 Monitor::Monitor() { | 237 Monitor::Monitor() { |
238 InitializeCriticalSection(&data_.cs_); | 238 InitializeCriticalSection(&data_.cs_); |
239 InitializeCriticalSection(&data_.waiters_cs_); | 239 InitializeCriticalSection(&data_.waiters_cs_); |
240 data_.waiters_head_ = NULL; | 240 data_.waiters_head_ = NULL; |
241 data_.waiters_tail_ = NULL; | 241 data_.waiters_tail_ = NULL; |
242 } | 242 } |
243 | 243 |
244 | 244 |
245 Monitor::~Monitor() { | 245 Monitor::~Monitor() { |
246 DeleteCriticalSection(&data_.cs_); | 246 DeleteCriticalSection(&data_.cs_); |
247 DeleteCriticalSection(&data_.waiters_cs_); | 247 DeleteCriticalSection(&data_.waiters_cs_); |
248 } | 248 } |
249 | 249 |
250 | 250 |
251 void Monitor::Enter() { | 251 void Monitor::Enter() { |
252 EnterCriticalSection(&data_.cs_); | 252 EnterCriticalSection(&data_.cs_); |
253 } | 253 } |
254 | 254 |
255 | 255 |
256 void Monitor::Exit() { | 256 void Monitor::Exit() { |
257 LeaveCriticalSection(&data_.cs_); | 257 LeaveCriticalSection(&data_.cs_); |
258 } | 258 } |
259 | 259 |
260 | 260 |
261 void MonitorWaitData::ThreadExit() { | 261 void MonitorWaitData::ThreadExit() { |
262 if (MonitorWaitData::monitor_wait_data_key_ != | 262 if (MonitorWaitData::monitor_wait_data_key_ != |
263 Thread::kUnsetThreadLocalKey) { | 263 OSThread::kUnsetThreadLocalKey) { |
264 uword raw_wait_data = | 264 uword raw_wait_data = |
265 Thread::GetThreadLocal(MonitorWaitData::monitor_wait_data_key_); | 265 OSThread::GetThreadLocal(MonitorWaitData::monitor_wait_data_key_); |
266 if (raw_wait_data != 0) { | 266 if (raw_wait_data != 0) { |
267 MonitorWaitData* wait_data = | 267 MonitorWaitData* wait_data = |
268 reinterpret_cast<MonitorWaitData*>(raw_wait_data); | 268 reinterpret_cast<MonitorWaitData*>(raw_wait_data); |
269 delete wait_data; | 269 delete wait_data; |
270 } | 270 } |
271 } | 271 } |
272 } | 272 } |
273 | 273 |
274 | 274 |
275 void MonitorData::AddWaiter(MonitorWaitData* wait_data) { | 275 void MonitorData::AddWaiter(MonitorWaitData* wait_data) { |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 current = next; | 360 current = next; |
361 } | 361 } |
362 LeaveCriticalSection(&waiters_cs_); | 362 LeaveCriticalSection(&waiters_cs_); |
363 } | 363 } |
364 | 364 |
365 | 365 |
366 MonitorWaitData* MonitorData::GetMonitorWaitDataForThread() { | 366 MonitorWaitData* MonitorData::GetMonitorWaitDataForThread() { |
367 // Ensure that the thread local key for monitor wait data objects is | 367 // Ensure that the thread local key for monitor wait data objects is |
368 // initialized. | 368 // initialized. |
369 ASSERT(MonitorWaitData::monitor_wait_data_key_ != | 369 ASSERT(MonitorWaitData::monitor_wait_data_key_ != |
370 Thread::kUnsetThreadLocalKey); | 370 OSThread::kUnsetThreadLocalKey); |
371 | 371 |
372 // Get the MonitorWaitData object containing the event for this | 372 // Get the MonitorWaitData object containing the event for this |
373 // thread from thread local storage. Create it if it does not exist. | 373 // thread from thread local storage. Create it if it does not exist. |
374 uword raw_wait_data = | 374 uword raw_wait_data = |
375 Thread::GetThreadLocal(MonitorWaitData::monitor_wait_data_key_); | 375 OSThread::GetThreadLocal(MonitorWaitData::monitor_wait_data_key_); |
376 MonitorWaitData* wait_data = NULL; | 376 MonitorWaitData* wait_data = NULL; |
377 if (raw_wait_data == 0) { | 377 if (raw_wait_data == 0) { |
378 HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL); | 378 HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL); |
379 wait_data = new MonitorWaitData(event); | 379 wait_data = new MonitorWaitData(event); |
380 Thread::SetThreadLocal(MonitorWaitData::monitor_wait_data_key_, | 380 OSThread::SetThreadLocal(MonitorWaitData::monitor_wait_data_key_, |
381 reinterpret_cast<uword>(wait_data)); | 381 reinterpret_cast<uword>(wait_data)); |
382 } else { | 382 } else { |
383 wait_data = reinterpret_cast<MonitorWaitData*>(raw_wait_data); | 383 wait_data = reinterpret_cast<MonitorWaitData*>(raw_wait_data); |
384 wait_data->next_ = NULL; | 384 wait_data->next_ = NULL; |
385 } | 385 } |
386 return wait_data; | 386 return wait_data; |
387 } | 387 } |
388 | 388 |
389 | 389 |
390 Monitor::WaitResult Monitor::Wait(int64_t millis) { | 390 Monitor::WaitResult Monitor::Wait(int64_t millis) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 // timeout before we signal it, that object will get an extra | 452 // timeout before we signal it, that object will get an extra |
453 // 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 |
454 // since all uses of monitors should recheck the condition after a | 454 // since all uses of monitors should recheck the condition after a |
455 // Wait. | 455 // Wait. |
456 data_.SignalAndRemoveAllWaiters(); | 456 data_.SignalAndRemoveAllWaiters(); |
457 } | 457 } |
458 | 458 |
459 } // namespace dart | 459 } // namespace dart |
460 | 460 |
461 #endif // defined(TARGET_OS_WINDOWS) | 461 #endif // defined(TARGET_OS_WINDOWS) |
OLD | NEW |