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