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" // NOLINT | 5 #include "platform/globals.h" // NOLINT |
6 #if defined(TARGET_OS_WINDOWS) | 6 #if defined(TARGET_OS_WINDOWS) |
7 | 7 |
8 #include "vm/os_thread.h" | 8 #include "vm/os_thread.h" |
9 | 9 |
10 #include <process.h> // NOLINT | 10 #include <process.h> // NOLINT |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 } | 64 } |
65 | 65 |
66 // Close the handle, so we don't leak the thread object. | 66 // Close the handle, so we don't leak the thread object. |
67 CloseHandle(reinterpret_cast<HANDLE>(thread)); | 67 CloseHandle(reinterpret_cast<HANDLE>(thread)); |
68 | 68 |
69 return 0; | 69 return 0; |
70 } | 70 } |
71 | 71 |
72 ThreadLocalKey OSThread::kUnsetThreadLocalKey = TLS_OUT_OF_INDEXES; | 72 ThreadLocalKey OSThread::kUnsetThreadLocalKey = TLS_OUT_OF_INDEXES; |
73 ThreadId OSThread::kInvalidThreadId = 0; | 73 ThreadId OSThread::kInvalidThreadId = 0; |
| 74 ThreadJoinId OSThread::kInvalidThreadJoinId = 0; |
74 | 75 |
75 ThreadLocalKey OSThread::CreateThreadLocal(ThreadDestructor unused) { | 76 ThreadLocalKey OSThread::CreateThreadLocal(ThreadDestructor unused) { |
76 ThreadLocalKey key = TlsAlloc(); | 77 ThreadLocalKey key = TlsAlloc(); |
77 if (key == kUnsetThreadLocalKey) { | 78 if (key == kUnsetThreadLocalKey) { |
78 FATAL1("TlsAlloc failed %d", GetLastError()); | 79 FATAL1("TlsAlloc failed %d", GetLastError()); |
79 } | 80 } |
80 return key; | 81 return key; |
81 } | 82 } |
82 | 83 |
83 | 84 |
(...skipping 10 matching lines...) Expand all Loading... |
94 const int kStackSize = (128 * kWordSize * KB); | 95 const int kStackSize = (128 * kWordSize * KB); |
95 return kStackSize; | 96 return kStackSize; |
96 } | 97 } |
97 | 98 |
98 | 99 |
99 ThreadId OSThread::GetCurrentThreadId() { | 100 ThreadId OSThread::GetCurrentThreadId() { |
100 return ::GetCurrentThreadId(); | 101 return ::GetCurrentThreadId(); |
101 } | 102 } |
102 | 103 |
103 | 104 |
104 bool OSThread::Join(ThreadId id) { | 105 ThreadJoinId OSThread::GetCurrentThreadJoinId() { |
105 HANDLE handle = OpenThread(SYNCHRONIZE, false, id); | 106 return ::GetCurrentThreadId(); |
106 if (handle == INVALID_HANDLE_VALUE) { | |
107 return false; | |
108 } | |
109 DWORD res = WaitForSingleObject(handle, INFINITE); | |
110 CloseHandle(handle); | |
111 return res == WAIT_OBJECT_0; | |
112 } | 107 } |
113 | 108 |
114 | 109 |
| 110 void OSThread::Join(ThreadJoinId id) { |
| 111 HANDLE handle = OpenThread(SYNCHRONIZE, false, id); |
| 112 |
| 113 // TODO(zra): OSThread::Start() closes the handle to the thread. Thus, by the |
| 114 // time we try to join the thread, its resources may have already been |
| 115 // reclaimed, and joining will fail. This can be avoided in a couple of ways. |
| 116 // First, GetCurrentThreadJoinId could call OpenThread and return a handle. |
| 117 // This is bad, because each of those handles would have to be closed. |
| 118 // Second OSThread could be refactored to no longer be AllStatic. Then the |
| 119 // handle could be cached in the object by the Start method. |
| 120 if (handle == NULL) { |
| 121 ASSERT(GetLastError() == ERROR_INVALID_PARAMETER); |
| 122 return; |
| 123 } |
| 124 |
| 125 DWORD res = WaitForSingleObject(handle, INFINITE); |
| 126 CloseHandle(handle); |
| 127 ASSERT(res == WAIT_OBJECT_0); |
| 128 } |
| 129 |
| 130 |
115 intptr_t OSThread::ThreadIdToIntPtr(ThreadId id) { | 131 intptr_t OSThread::ThreadIdToIntPtr(ThreadId id) { |
116 ASSERT(sizeof(id) <= sizeof(intptr_t)); | 132 ASSERT(sizeof(id) <= sizeof(intptr_t)); |
117 return static_cast<intptr_t>(id); | 133 return static_cast<intptr_t>(id); |
118 } | 134 } |
119 | 135 |
120 | 136 |
121 ThreadId OSThread::ThreadIdFromIntPtr(intptr_t id) { | 137 ThreadId OSThread::ThreadIdFromIntPtr(intptr_t id) { |
122 return static_cast<ThreadId>(id); | 138 return static_cast<ThreadId>(id); |
123 } | 139 } |
124 | 140 |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 // timeout before we signal it, that object will get an extra | 472 // timeout before we signal it, that object will get an extra |
457 // signal. This will be treated as a spurious wake-up and is OK | 473 // signal. This will be treated as a spurious wake-up and is OK |
458 // since all uses of monitors should recheck the condition after a | 474 // since all uses of monitors should recheck the condition after a |
459 // Wait. | 475 // Wait. |
460 data_.SignalAndRemoveAllWaiters(); | 476 data_.SignalAndRemoveAllWaiters(); |
461 } | 477 } |
462 | 478 |
463 } // namespace dart | 479 } // namespace dart |
464 | 480 |
465 #endif // defined(TARGET_OS_WINDOWS) | 481 #endif // defined(TARGET_OS_WINDOWS) |
OLD | NEW |