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::ThreadMain(uword parameters) { | |
90 ASSERT(FLAG_thread_interrupter); | |
91 ASSERT(initialized_); | |
92 if (FLAG_trace_thread_interrupter) { | |
93 OS::Print("ThreadInterrupter Windows ready.\n"); | |
94 } | |
95 { | |
96 // Signal to main thread we are ready. | |
97 MonitorLocker startup_ml(start_stop_monitor_); | |
98 thread_running_ = true; | |
99 interrupter_thread_id_ = Thread::GetCurrentThreadId(); | |
100 startup_ml.Notify(); | |
101 } | |
102 monitor_->Enter(); | |
103 while (!shutdown_) { | |
104 int64_t current_time = OS::GetCurrentTimeMicros(); | |
105 InterruptThreads(current_time); | |
106 monitor_->Exit(); | |
107 OS::SleepMicros(interrupt_period_); | |
108 monitor_->Enter(); | |
109 } | |
siva
2013/12/11 02:52:21
Why is the pattern different here?
We have a Mutex
| |
110 if (FLAG_trace_thread_interrupter) { | |
111 OS::Print("ThreadInterrupter Windows exiting.\n"); | |
112 } | |
113 { | |
114 // Signal to main thread we are exiting. | |
115 MonitorLocker shutdown_ml(start_stop_monitor_); | |
116 thread_running_ = false; | |
117 shutdown_ml.Notify(); | |
118 } | |
119 } | |
120 | |
121 | |
122 } // namespace dart | |
123 | |
124 #endif // defined(TARGET_OS_WINDOWS) | |
125 | |
OLD | NEW |