| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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" |
| 6 #if defined(TARGET_OS_WINDOWS) | 6 #if defined(TARGET_OS_WINDOWS) |
| 7 | 7 |
| 8 #include "vm/thread_interrupter.h" | 8 #include "vm/thread_interrupter.h" |
| 9 | 9 |
| 10 namespace dart { | 10 namespace dart { |
| 11 | 11 |
| 12 DECLARE_FLAG(bool, thread_interrupter); | 12 DECLARE_FLAG(bool, thread_interrupter); |
| 13 DECLARE_FLAG(bool, trace_thread_interrupter); | 13 DECLARE_FLAG(bool, trace_thread_interrupter); |
| 14 | 14 |
| 15 #define kThreadError -1 | 15 #define kThreadError -1 |
| 16 | 16 |
| 17 class ThreadInterrupterWin : public AllStatic { | 17 class ThreadInterrupterWin : public AllStatic { |
| 18 public: | 18 public: |
| 19 static bool GrabRegisters(ThreadId thread, InterruptedThreadState* state) { | 19 static bool GrabRegisters(HANDLE handle, InterruptedThreadState* state) { |
| 20 CONTEXT context; | 20 CONTEXT context; |
| 21 memset(&context, 0, sizeof(context)); | 21 memset(&context, 0, sizeof(context)); |
| 22 context.ContextFlags = CONTEXT_FULL; | 22 context.ContextFlags = CONTEXT_FULL; |
| 23 if (GetThreadContext(thread, &context) != 0) { | 23 if (GetThreadContext(handle, &context) != 0) { |
| 24 #if defined(TARGET_ARCH_IA32) | 24 #if defined(TARGET_ARCH_IA32) |
| 25 state->pc = static_cast<uintptr_t>(context.Eip); | 25 state->pc = static_cast<uintptr_t>(context.Eip); |
| 26 state->fp = static_cast<uintptr_t>(context.Ebp); | 26 state->fp = static_cast<uintptr_t>(context.Ebp); |
| 27 state->sp = static_cast<uintptr_t>(context.Esp); | 27 state->sp = static_cast<uintptr_t>(context.Esp); |
| 28 #elif defined(TARGET_ARCH_X64) | 28 #elif defined(TARGET_ARCH_X64) |
| 29 state->pc = reinterpret_cast<uintptr_t>(context.Rip); | 29 state->pc = reinterpret_cast<uintptr_t>(context.Rip); |
| 30 state->fp = reinterpret_cast<uintptr_t>(context.Rbp); | 30 state->fp = reinterpret_cast<uintptr_t>(context.Rbp); |
| 31 state->sp = reinterpret_cast<uintptr_t>(context.Rsp); | 31 state->sp = reinterpret_cast<uintptr_t>(context.Rsp); |
| 32 #else | 32 #else |
| 33 UNIMPLEMENTED(); | 33 UNIMPLEMENTED(); |
| 34 #endif | 34 #endif |
| 35 return true; | 35 return true; |
| 36 } | 36 } |
| 37 return false; | 37 return false; |
| 38 } | 38 } |
| 39 | 39 |
| 40 | 40 |
| 41 static void Interrupt(InterruptableThreadState* state) { | 41 static void Interrupt(InterruptableThreadState* state) { |
| 42 ASSERT(GetCurrentThread() != state->id); | 42 ASSERT(!Thread::Compare(GetCurrentThreadId(), state->id)); |
| 43 DWORD result = SuspendThread(state->id); | 43 HANDLE handle = OpenThread(THREAD_GET_CONTEXT | |
| 44 THREAD_SUSPEND_RESUME, |
| 45 false, |
| 46 state->id); |
| 47 ASSERT(handle != NULL); |
| 48 DWORD result = SuspendThread(handle); |
| 44 if (result == kThreadError) { | 49 if (result == kThreadError) { |
| 45 if (FLAG_trace_thread_interrupter) { | 50 if (FLAG_trace_thread_interrupter) { |
| 46 OS::Print("ThreadInterrupted failed to suspend thread %p\n", | 51 OS::Print("ThreadInterrupted failed to suspend thread %p\n", |
| 47 reinterpret_cast<void*>(state->id)); | 52 reinterpret_cast<void*>(state->id)); |
| 48 } | 53 } |
| 54 CloseHandle(handle); |
| 49 return; | 55 return; |
| 50 } | 56 } |
| 51 InterruptedThreadState its; | 57 InterruptedThreadState its; |
| 52 its.tid = state->id; | 58 its.tid = state->id; |
| 53 if (!GrabRegisters(state->id, &its)) { | 59 if (!GrabRegisters(handle, &its)) { |
| 54 // Failed to get thread registers. | 60 // Failed to get thread registers. |
| 55 ResumeThread(state->id); | 61 ResumeThread(handle); |
| 56 if (FLAG_trace_thread_interrupter) { | 62 if (FLAG_trace_thread_interrupter) { |
| 57 OS::Print("ThreadInterrupted failed to get registers for %p\n", | 63 OS::Print("ThreadInterrupted failed to get registers for %p\n", |
| 58 reinterpret_cast<void*>(state->id)); | 64 reinterpret_cast<void*>(state->id)); |
| 59 } | 65 } |
| 66 CloseHandle(handle); |
| 60 return; | 67 return; |
| 61 } | 68 } |
| 62 if (state->callback == NULL) { | 69 if (state->callback == NULL) { |
| 63 // No callback registered. | 70 // No callback registered. |
| 64 ResumeThread(state->id); | 71 ResumeThread(handle); |
| 72 CloseHandle(handle); |
| 65 return; | 73 return; |
| 66 } | 74 } |
| 67 state->callback(its, state->data); | 75 state->callback(its, state->data); |
| 68 ResumeThread(state->id); | 76 ResumeThread(handle); |
| 77 CloseHandle(handle); |
| 69 } | 78 } |
| 70 }; | 79 }; |
| 71 | 80 |
| 72 | 81 |
| 73 void ThreadInterrupter::InterruptThread(InterruptableThreadState* state) { | 82 void ThreadInterrupter::InterruptThread(InterruptableThreadState* state) { |
| 74 if (FLAG_trace_thread_interrupter) { | 83 if (FLAG_trace_thread_interrupter) { |
| 75 OS::Print("ThreadInterrupter suspending %p\n", | 84 OS::Print("ThreadInterrupter suspending %p\n", |
| 76 reinterpret_cast<void*>(state->id)); | 85 reinterpret_cast<void*>(state->id)); |
| 77 } | 86 } |
| 78 ThreadInterrupterWin::Interrupt(state); | 87 ThreadInterrupterWin::Interrupt(state); |
| 79 if (FLAG_trace_thread_interrupter) { | 88 if (FLAG_trace_thread_interrupter) { |
| 80 OS::Print("ThreadInterrupter resuming %p\n", | 89 OS::Print("ThreadInterrupter resuming %p\n", |
| 81 reinterpret_cast<void*>(state->id)); | 90 reinterpret_cast<void*>(state->id)); |
| 82 } | 91 } |
| 83 } | 92 } |
| 84 | 93 |
| 85 | 94 |
| 86 void ThreadInterrupter::InstallSignalHandler() { | 95 void ThreadInterrupter::InstallSignalHandler() { |
| 87 // Nothing to do on Windows. | 96 // Nothing to do on Windows. |
| 88 } | 97 } |
| 89 | 98 |
| 90 | 99 |
| 91 } // namespace dart | 100 } // namespace dart |
| 92 | 101 |
| 93 #endif // defined(TARGET_OS_WINDOWS) | 102 #endif // defined(TARGET_OS_WINDOWS) |
| 94 | 103 |
| OLD | NEW |