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 "vm/thread_interrupter.h" | 5 #include "vm/thread_interrupter.h" |
6 | 6 |
7 #include "vm/flags.h" | 7 #include "vm/flags.h" |
8 #include "vm/lockers.h" | 8 #include "vm/lockers.h" |
9 #include "vm/os.h" | 9 #include "vm/os.h" |
10 #include "vm/simulator.h" | 10 #include "vm/simulator.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 // thread local storage pointer is set again. This has an important side | 44 // thread local storage pointer is set again. This has an important side |
45 // effect: if the thread is interrupted by a signal handler during a ThreadState | 45 // effect: if the thread is interrupted by a signal handler during a ThreadState |
46 // update the signal handler will immediately return. | 46 // update the signal handler will immediately return. |
47 | 47 |
48 DEFINE_FLAG(bool, trace_thread_interrupter, false, | 48 DEFINE_FLAG(bool, trace_thread_interrupter, false, |
49 "Trace thread interrupter"); | 49 "Trace thread interrupter"); |
50 | 50 |
51 bool ThreadInterrupter::initialized_ = false; | 51 bool ThreadInterrupter::initialized_ = false; |
52 bool ThreadInterrupter::shutdown_ = false; | 52 bool ThreadInterrupter::shutdown_ = false; |
53 bool ThreadInterrupter::thread_running_ = false; | 53 bool ThreadInterrupter::thread_running_ = false; |
54 ThreadId ThreadInterrupter::interrupter_thread_id_ = | 54 ThreadJoinId ThreadInterrupter::interrupter_thread_id_ = |
55 OSThread::kInvalidThreadId; | 55 OSThread::kInvalidThreadJoinId; |
56 Monitor* ThreadInterrupter::monitor_ = NULL; | 56 Monitor* ThreadInterrupter::monitor_ = NULL; |
57 intptr_t ThreadInterrupter::interrupt_period_ = 1000; | 57 intptr_t ThreadInterrupter::interrupt_period_ = 1000; |
58 intptr_t ThreadInterrupter::current_wait_time_ = Monitor::kNoTimeout; | 58 intptr_t ThreadInterrupter::current_wait_time_ = Monitor::kNoTimeout; |
59 | 59 |
60 | 60 |
61 void ThreadInterrupter::InitOnce() { | 61 void ThreadInterrupter::InitOnce() { |
62 ASSERT(!initialized_); | 62 ASSERT(!initialized_); |
63 monitor_ = new Monitor(); | 63 monitor_ = new Monitor(); |
64 ASSERT(monitor_ != NULL); | 64 ASSERT(monitor_ != NULL); |
65 initialized_ = true; | 65 initialized_ = true; |
66 } | 66 } |
67 | 67 |
68 | 68 |
69 void ThreadInterrupter::Startup() { | 69 void ThreadInterrupter::Startup() { |
70 ASSERT(initialized_); | 70 ASSERT(initialized_); |
71 if (FLAG_trace_thread_interrupter) { | 71 if (FLAG_trace_thread_interrupter) { |
72 OS::Print("ThreadInterrupter starting up.\n"); | 72 OS::Print("ThreadInterrupter starting up.\n"); |
73 } | 73 } |
74 ASSERT(interrupter_thread_id_ == OSThread::kInvalidThreadId); | 74 ASSERT(interrupter_thread_id_ == OSThread::kInvalidThreadJoinId); |
75 { | 75 { |
76 MonitorLocker startup_ml(monitor_); | 76 MonitorLocker startup_ml(monitor_); |
77 OSThread::Start(ThreadMain, 0); | 77 OSThread::Start(ThreadMain, 0); |
78 while (!thread_running_) { | 78 while (!thread_running_) { |
79 startup_ml.Wait(); | 79 startup_ml.Wait(); |
80 } | 80 } |
81 } | 81 } |
82 ASSERT(interrupter_thread_id_ != OSThread::kInvalidThreadId); | 82 ASSERT(interrupter_thread_id_ != OSThread::kInvalidThreadJoinId); |
83 if (FLAG_trace_thread_interrupter) { | 83 if (FLAG_trace_thread_interrupter) { |
84 OS::Print("ThreadInterrupter running.\n"); | 84 OS::Print("ThreadInterrupter running.\n"); |
85 } | 85 } |
86 } | 86 } |
87 | 87 |
88 | 88 |
89 void ThreadInterrupter::Shutdown() { | 89 void ThreadInterrupter::Shutdown() { |
90 { | 90 { |
91 MonitorLocker shutdown_ml(monitor_); | 91 MonitorLocker shutdown_ml(monitor_); |
92 if (shutdown_) { | 92 if (shutdown_) { |
93 // Already shutdown. | 93 // Already shutdown. |
94 return; | 94 return; |
95 } | 95 } |
96 shutdown_ = true; | 96 shutdown_ = true; |
97 // Notify. | 97 // Notify. |
98 monitor_->Notify(); | 98 monitor_->Notify(); |
99 ASSERT(initialized_); | 99 ASSERT(initialized_); |
100 if (FLAG_trace_thread_interrupter) { | 100 if (FLAG_trace_thread_interrupter) { |
101 OS::Print("ThreadInterrupter shutting down.\n"); | 101 OS::Print("ThreadInterrupter shutting down.\n"); |
102 } | 102 } |
103 } | 103 } |
104 #if defined(TARGET_OS_WINDOWS) | 104 |
105 // On Windows, a thread's exit-code can leak into the process's exit-code, | 105 // Join the thread. |
106 // if exiting 'at same time' as the process ends. By joining with the thread | 106 ASSERT(interrupter_thread_id_ != OSThread::kInvalidThreadJoinId); |
107 // here, we avoid this race condition. | |
108 ASSERT(interrupter_thread_id_ != OSThread::kInvalidThreadId); | |
109 OSThread::Join(interrupter_thread_id_); | 107 OSThread::Join(interrupter_thread_id_); |
110 interrupter_thread_id_ = OSThread::kInvalidThreadId; | 108 interrupter_thread_id_ = OSThread::kInvalidThreadJoinId; |
111 #else | 109 |
112 // On non-Windows platforms, just wait for the thread interrupter to signal | |
113 // that it has exited the loop. | |
114 { | |
115 MonitorLocker shutdown_ml(monitor_); | |
116 while (thread_running_) { | |
117 // Wait for thread to exit. | |
118 shutdown_ml.Wait(); | |
119 } | |
120 } | |
121 #endif | |
122 if (FLAG_trace_thread_interrupter) { | 110 if (FLAG_trace_thread_interrupter) { |
123 OS::Print("ThreadInterrupter shut down.\n"); | 111 OS::Print("ThreadInterrupter shut down.\n"); |
124 } | 112 } |
125 } | 113 } |
126 | 114 |
127 // Delay between interrupts. | 115 // Delay between interrupts. |
128 void ThreadInterrupter::SetInterruptPeriod(intptr_t period) { | 116 void ThreadInterrupter::SetInterruptPeriod(intptr_t period) { |
129 if (shutdown_) { | 117 if (shutdown_) { |
130 return; | 118 return; |
131 } | 119 } |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 | 255 |
268 void ThreadInterrupter::ThreadMain(uword parameters) { | 256 void ThreadInterrupter::ThreadMain(uword parameters) { |
269 ASSERT(initialized_); | 257 ASSERT(initialized_); |
270 InstallSignalHandler(); | 258 InstallSignalHandler(); |
271 if (FLAG_trace_thread_interrupter) { | 259 if (FLAG_trace_thread_interrupter) { |
272 OS::Print("ThreadInterrupter thread running.\n"); | 260 OS::Print("ThreadInterrupter thread running.\n"); |
273 } | 261 } |
274 { | 262 { |
275 // Signal to main thread we are ready. | 263 // Signal to main thread we are ready. |
276 MonitorLocker startup_ml(monitor_); | 264 MonitorLocker startup_ml(monitor_); |
277 interrupter_thread_id_ = OSThread::GetCurrentThreadId(); | 265 interrupter_thread_id_ = OSThread::GetCurrentThreadJoinId(); |
278 thread_running_ = true; | 266 thread_running_ = true; |
279 startup_ml.Notify(); | 267 startup_ml.Notify(); |
280 } | 268 } |
281 { | 269 { |
282 ThreadInterrupterVisitIsolates visitor; | 270 ThreadInterrupterVisitIsolates visitor; |
283 current_wait_time_ = interrupt_period_; | 271 current_wait_time_ = interrupt_period_; |
284 MonitorLocker wait_ml(monitor_); | 272 MonitorLocker wait_ml(monitor_); |
285 while (!shutdown_) { | 273 while (!shutdown_) { |
286 intptr_t r = wait_ml.WaitMicros(current_wait_time_); | 274 intptr_t r = wait_ml.WaitMicros(current_wait_time_); |
287 | 275 |
(...skipping 25 matching lines...) Expand all Loading... |
313 } | 301 } |
314 { | 302 { |
315 // Signal to main thread we are exiting. | 303 // Signal to main thread we are exiting. |
316 MonitorLocker shutdown_ml(monitor_); | 304 MonitorLocker shutdown_ml(monitor_); |
317 thread_running_ = false; | 305 thread_running_ = false; |
318 shutdown_ml.Notify(); | 306 shutdown_ml.Notify(); |
319 } | 307 } |
320 } | 308 } |
321 | 309 |
322 } // namespace dart | 310 } // namespace dart |
OLD | NEW |