| 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 | 168 |
| 181 void ThreadInterrupter::ThreadMain(uword parameters) { | 169 void ThreadInterrupter::ThreadMain(uword parameters) { |
| 182 ASSERT(initialized_); | 170 ASSERT(initialized_); |
| 183 InstallSignalHandler(); | 171 InstallSignalHandler(); |
| 184 if (FLAG_trace_thread_interrupter) { | 172 if (FLAG_trace_thread_interrupter) { |
| 185 OS::Print("ThreadInterrupter thread running.\n"); | 173 OS::Print("ThreadInterrupter thread running.\n"); |
| 186 } | 174 } |
| 187 { | 175 { |
| 188 // Signal to main thread we are ready. | 176 // Signal to main thread we are ready. |
| 189 MonitorLocker startup_ml(monitor_); | 177 MonitorLocker startup_ml(monitor_); |
| 190 interrupter_thread_id_ = OSThread::GetCurrentThreadId(); | 178 interrupter_thread_id_ = OSThread::GetCurrentThreadJoinId(); |
| 191 thread_running_ = true; | 179 thread_running_ = true; |
| 192 startup_ml.Notify(); | 180 startup_ml.Notify(); |
| 193 } | 181 } |
| 194 { | 182 { |
| 195 ThreadInterrupterVisitIsolates visitor; | 183 ThreadInterrupterVisitIsolates visitor; |
| 196 current_wait_time_ = interrupt_period_; | 184 current_wait_time_ = interrupt_period_; |
| 197 MonitorLocker wait_ml(monitor_); | 185 MonitorLocker wait_ml(monitor_); |
| 198 while (!shutdown_) { | 186 while (!shutdown_) { |
| 199 intptr_t r = wait_ml.WaitMicros(current_wait_time_); | 187 intptr_t r = wait_ml.WaitMicros(current_wait_time_); |
| 200 | 188 |
| 189 if ((r == Monitor::kNotified) && shutdown_) { |
| 190 break; |
| 191 } |
| 192 |
| 201 if ((r == Monitor::kNotified) && InDeepSleep()) { | 193 if ((r == Monitor::kNotified) && InDeepSleep()) { |
| 202 // Woken up from deep sleep. | 194 // Woken up from deep sleep. |
| 203 ASSERT(visitor.profiled_thread_count() == 0); | 195 ASSERT(visitor.profiled_thread_count() == 0); |
| 204 // Return to regular interrupts. | 196 // Return to regular interrupts. |
| 205 current_wait_time_ = interrupt_period_; | 197 current_wait_time_ = interrupt_period_; |
| 206 } | 198 } |
| 207 | 199 |
| 208 // Reset count before visiting isolates. | 200 // Reset count before visiting isolates. |
| 209 visitor.set_profiled_thread_count(0); | 201 visitor.set_profiled_thread_count(0); |
| 210 Isolate::VisitIsolates(&visitor); | 202 Isolate::VisitIsolates(&visitor); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 226 } | 218 } |
| 227 { | 219 { |
| 228 // Signal to main thread we are exiting. | 220 // Signal to main thread we are exiting. |
| 229 MonitorLocker shutdown_ml(monitor_); | 221 MonitorLocker shutdown_ml(monitor_); |
| 230 thread_running_ = false; | 222 thread_running_ = false; |
| 231 shutdown_ml.Notify(); | 223 shutdown_ml.Notify(); |
| 232 } | 224 } |
| 233 } | 225 } |
| 234 | 226 |
| 235 } // namespace dart | 227 } // namespace dart |
| OLD | NEW |