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 "vm/simulator.h" |
| 6 #include "vm/thread_interrupter.h" |
| 7 |
| 8 namespace dart { |
| 9 |
| 10 // Notes: |
| 11 // |
| 12 // The ThreadInterrupter interrupts all registered threads once per |
| 13 // interrupt period (default is every millisecond). While the thread is |
| 14 // interrupted, the thread's interrupt callback is invoked. Callbacks cannot |
| 15 // rely on being executed on the interrupted thread. |
| 16 // |
| 17 // There are two mechanisms used to interrupt a thread. The first, used on OSs |
| 18 // with pthreads (Android, Linux, and Mac), is thread specific signal delivery. |
| 19 // The second, used on Windows, is explicit suspend and resume thread system |
| 20 // calls. Signal delivery forbids taking locks and allocating memory (which |
| 21 // takes a lock). Explicit suspend and resume means that the interrupt callback |
| 22 // will not be executing on the interrupted thread, making it meaningless to |
| 23 // access TLS from within the thread interrupt callback. Combining these |
| 24 // limitations, thread interrupt callbacks are forbidden from: |
| 25 // |
| 26 // * Accessing TLS. |
| 27 // * Allocating memory. |
| 28 // * Taking a lock. |
| 29 // |
| 30 // The ThreadInterrupter has a single monitor (monitor_). This monitor guards |
| 31 // access to the list of threads registered to receive interrupts (threads_). |
| 32 // |
| 33 // A thread can only register and unregister itself. Each thread has a heap |
| 34 // allocated ThreadState. A thread's ThreadState is lazily allocated the first |
| 35 // time the thread is registered. A pointer to a thread's ThreadState is stored |
| 36 // in the list of threads registered to receive interrupts (threads_) and in |
| 37 // thread local storage. When a thread's ThreadState is being modified, the |
| 38 // thread local storage pointer is temporarily set to NULL while the |
| 39 // modification is occurring. After the ThreadState has been updated, the |
| 40 // thread local storage pointer is set again. This has an important side |
| 41 // effect: if the thread is interrupted by a signal handler during a ThreadState |
| 42 // update the signal handler will immediately return. |
| 43 |
| 44 DEFINE_FLAG(bool, thread_interrupter, true, "Enable thread interrupter"); |
| 45 DEFINE_FLAG(bool, trace_thread_interrupter, false, |
| 46 "Trace thread interrupter"); |
| 47 |
| 48 bool ThreadInterrupter::initialized_ = false; |
| 49 bool ThreadInterrupter::shutdown_ = false; |
| 50 bool ThreadInterrupter::thread_running_ = false; |
| 51 ThreadId ThreadInterrupter::interrupter_thread_id_ = Thread::kInvalidThreadId; |
| 52 Monitor* ThreadInterrupter::monitor_ = NULL; |
| 53 Monitor* ThreadInterrupter::start_stop_monitor_ = NULL; |
| 54 intptr_t ThreadInterrupter::interrupt_period_ = 1000; |
| 55 ThreadLocalKey ThreadInterrupter::thread_state_key_ = |
| 56 Thread::kUnsetThreadLocalKey; |
| 57 ThreadInterrupter::ThreadState** ThreadInterrupter::threads_ = NULL; |
| 58 intptr_t ThreadInterrupter::threads_capacity_ = 0; |
| 59 intptr_t ThreadInterrupter::threads_size_ = 0; |
| 60 |
| 61 |
| 62 void ThreadInterrupter::InitOnce() { |
| 63 #if defined(USING_SIMULATOR) |
| 64 FLAG_thread_interrupter = false; |
| 65 #endif |
| 66 if (!FLAG_thread_interrupter) { |
| 67 return; |
| 68 } |
| 69 ASSERT(!initialized_); |
| 70 initialized_ = true; |
| 71 ASSERT(thread_state_key_ == Thread::kUnsetThreadLocalKey); |
| 72 thread_state_key_ = Thread::CreateThreadLocal(); |
| 73 ASSERT(thread_state_key_ != Thread::kUnsetThreadLocalKey); |
| 74 monitor_ = new Monitor(); |
| 75 start_stop_monitor_ = new Monitor(); |
| 76 ResizeThreads(16); |
| 77 if (FLAG_trace_thread_interrupter) { |
| 78 OS::Print("ThreadInterrupter starting up.\n"); |
| 79 } |
| 80 ASSERT(interrupter_thread_id_ == Thread::kInvalidThreadId); |
| 81 { |
| 82 MonitorLocker startup_ml(start_stop_monitor_); |
| 83 Thread::Start(ThreadMain, 0); |
| 84 while (!thread_running_) { |
| 85 startup_ml.Wait(); |
| 86 } |
| 87 } |
| 88 ASSERT(interrupter_thread_id_ != Thread::kInvalidThreadId); |
| 89 if (FLAG_trace_thread_interrupter) { |
| 90 OS::Print("ThreadInterrupter running.\n"); |
| 91 } |
| 92 } |
| 93 |
| 94 |
| 95 void ThreadInterrupter::Shutdown() { |
| 96 if (!FLAG_thread_interrupter) { |
| 97 return; |
| 98 } |
| 99 if (shutdown_) { |
| 100 // Already shutdown. |
| 101 return; |
| 102 } |
| 103 ASSERT(initialized_); |
| 104 if (FLAG_trace_thread_interrupter) { |
| 105 OS::Print("ThreadInterrupter shutting down.\n"); |
| 106 } |
| 107 intptr_t size_at_shutdown = 0; |
| 108 { |
| 109 MonitorLocker ml(monitor_); |
| 110 shutdown_ = true; |
| 111 size_at_shutdown = threads_size_; |
| 112 threads_size_ = 0; |
| 113 threads_capacity_ = 0; |
| 114 free(threads_); |
| 115 threads_ = NULL; |
| 116 ml.Notify(); |
| 117 } |
| 118 { |
| 119 MonitorLocker shutdown_ml(start_stop_monitor_); |
| 120 while (thread_running_) { |
| 121 shutdown_ml.Wait(); |
| 122 } |
| 123 } |
| 124 if (FLAG_trace_thread_interrupter) { |
| 125 OS::Print("ThreadInterrupter shut down (%" Pd ").\n", size_at_shutdown); |
| 126 } |
| 127 } |
| 128 |
| 129 // Delay between interrupts. |
| 130 void ThreadInterrupter::SetInterruptPeriod(intptr_t period) { |
| 131 if (!FLAG_thread_interrupter) { |
| 132 return; |
| 133 } |
| 134 ASSERT(period > 0); |
| 135 { |
| 136 MonitorLocker ml(monitor_); |
| 137 interrupt_period_ = period; |
| 138 } |
| 139 } |
| 140 |
| 141 |
| 142 // Register the currently running thread for interrupts. If the current thread |
| 143 // is already registered, callback and data will be updated. |
| 144 void ThreadInterrupter::Register(ThreadInterruptCallback callback, void* data) { |
| 145 if (!FLAG_thread_interrupter) { |
| 146 return; |
| 147 } |
| 148 { |
| 149 MonitorLocker ml(monitor_); |
| 150 _EnsureThreadStateCreated(); |
| 151 // Set callback and data. |
| 152 UpdateStateObject(callback, data); |
| 153 _Enable(); |
| 154 ml.Notify(); |
| 155 } |
| 156 } |
| 157 |
| 158 |
| 159 // Unregister the currently running thread for interrupts. |
| 160 void ThreadInterrupter::Unregister() { |
| 161 if (!FLAG_thread_interrupter) { |
| 162 return; |
| 163 } |
| 164 { |
| 165 MonitorLocker ml(monitor_); |
| 166 _EnsureThreadStateCreated(); |
| 167 // Clear callback and data. |
| 168 UpdateStateObject(NULL, NULL); |
| 169 _Disable(); |
| 170 ml.Notify(); |
| 171 } |
| 172 } |
| 173 |
| 174 |
| 175 void ThreadInterrupter::Enable() { |
| 176 if (!FLAG_thread_interrupter) { |
| 177 return; |
| 178 } |
| 179 { |
| 180 MonitorLocker ml(monitor_); |
| 181 _EnsureThreadStateCreated(); |
| 182 _Enable(); |
| 183 ml.Notify(); |
| 184 } |
| 185 } |
| 186 |
| 187 |
| 188 void ThreadInterrupter::Disable() { |
| 189 if (!FLAG_thread_interrupter) { |
| 190 return; |
| 191 } |
| 192 { |
| 193 MonitorLocker ml(monitor_); |
| 194 _EnsureThreadStateCreated(); |
| 195 _Disable(); |
| 196 ml.Notify(); |
| 197 } |
| 198 } |
| 199 |
| 200 |
| 201 void ThreadInterrupter::_EnsureThreadStateCreated() { |
| 202 ThreadState* state = CurrentThreadState(); |
| 203 if (state == NULL) { |
| 204 // Create thread state object lazily. |
| 205 ThreadId current_thread = Thread::GetCurrentThreadId(); |
| 206 if (FLAG_trace_thread_interrupter) { |
| 207 intptr_t tid = Thread::ThreadIdToIntPtr(current_thread); |
| 208 OS::Print("ThreadInterrupter Tracking %p\n", |
| 209 reinterpret_cast<void*>(tid)); |
| 210 } |
| 211 state = new ThreadState(); |
| 212 state->callback = NULL; |
| 213 state->data = NULL; |
| 214 state->id = current_thread; |
| 215 SetCurrentThreadState(state); |
| 216 } |
| 217 } |
| 218 |
| 219 |
| 220 void ThreadInterrupter::_Enable() { |
| 221 // Must be called with monitor_ locked. |
| 222 ThreadId current_thread = Thread::GetCurrentThreadId(); |
| 223 ASSERT(!Thread::Compare(current_thread, interrupter_thread_id_)); |
| 224 intptr_t i = FindThreadIndex(current_thread); |
| 225 if (i >= 0) { |
| 226 return; |
| 227 } |
| 228 AddThread(current_thread); |
| 229 if (FLAG_trace_thread_interrupter) { |
| 230 intptr_t tid = Thread::ThreadIdToIntPtr(current_thread); |
| 231 OS::Print("ThreadInterrupter Added %p\n", reinterpret_cast<void*>(tid)); |
| 232 } |
| 233 } |
| 234 |
| 235 void ThreadInterrupter::_Disable() { |
| 236 // Must be called with monitor_ locked. |
| 237 ThreadId current_thread = Thread::GetCurrentThreadId(); |
| 238 ASSERT(!Thread::Compare(current_thread, interrupter_thread_id_)); |
| 239 intptr_t index = FindThreadIndex(current_thread); |
| 240 if (index < 0) { |
| 241 // Not registered. |
| 242 return; |
| 243 } |
| 244 ThreadState* state = RemoveThread(index); |
| 245 ASSERT(state != NULL); |
| 246 ASSERT(state == ThreadInterrupter::CurrentThreadState()); |
| 247 if (FLAG_trace_thread_interrupter) { |
| 248 intptr_t tid = Thread::ThreadIdToIntPtr(current_thread); |
| 249 OS::Print("ThreadInterrupter Removed %p\n", reinterpret_cast<void*>(tid)); |
| 250 } |
| 251 } |
| 252 |
| 253 void ThreadInterrupter::UpdateStateObject(ThreadInterruptCallback callback, |
| 254 void* data) { |
| 255 // Must be called with monitor_ locked. |
| 256 ThreadState* state = CurrentThreadState(); |
| 257 ThreadId current_thread = Thread::GetCurrentThreadId(); |
| 258 ASSERT(state != NULL); |
| 259 ASSERT(Thread::Compare(state->id, Thread::GetCurrentThreadId())); |
| 260 SetCurrentThreadState(NULL); |
| 261 // It is now safe to modify the state object. If an interrupt occurs, |
| 262 // the current thread state will be NULL. |
| 263 state->callback = callback; |
| 264 state->data = data; |
| 265 SetCurrentThreadState(state); |
| 266 if (FLAG_trace_thread_interrupter) { |
| 267 intptr_t tid = Thread::ThreadIdToIntPtr(current_thread); |
| 268 if (callback == NULL) { |
| 269 OS::Print("ThreadInterrupter Cleared %p\n", reinterpret_cast<void*>(tid)); |
| 270 } else { |
| 271 OS::Print("ThreadInterrupter Updated %p\n", reinterpret_cast<void*>(tid)); |
| 272 } |
| 273 } |
| 274 } |
| 275 |
| 276 |
| 277 ThreadInterrupter::ThreadState* ThreadInterrupter::CurrentThreadState() { |
| 278 ThreadState* state = reinterpret_cast<ThreadState*>( |
| 279 Thread::GetThreadLocal(thread_state_key_)); |
| 280 return state; |
| 281 } |
| 282 |
| 283 |
| 284 void ThreadInterrupter::SetCurrentThreadState(ThreadState* state) { |
| 285 Thread::SetThreadLocal(thread_state_key_, reinterpret_cast<uword>(state)); |
| 286 } |
| 287 |
| 288 |
| 289 void ThreadInterrupter::ResizeThreads(intptr_t new_capacity) { |
| 290 // Must be called with monitor_ locked. |
| 291 ASSERT(new_capacity < kMaxThreads); |
| 292 ASSERT(new_capacity > threads_capacity_); |
| 293 ThreadState* state = NULL; |
| 294 threads_ = reinterpret_cast<ThreadState**>( |
| 295 realloc(threads_, sizeof(state) * new_capacity)); |
| 296 for (intptr_t i = threads_capacity_; i < new_capacity; i++) { |
| 297 threads_[i] = NULL; |
| 298 } |
| 299 threads_capacity_ = new_capacity; |
| 300 } |
| 301 |
| 302 |
| 303 void ThreadInterrupter::AddThread(ThreadId id) { |
| 304 // Must be called with monitor_ locked. |
| 305 if (threads_ == NULL) { |
| 306 // We are shutting down. |
| 307 return; |
| 308 } |
| 309 ThreadState* state = CurrentThreadState(); |
| 310 if (state->callback == NULL) { |
| 311 // No callback. |
| 312 return; |
| 313 } |
| 314 if (threads_size_ == threads_capacity_) { |
| 315 ResizeThreads(threads_capacity_ == 0 ? 16 : threads_capacity_ * 2); |
| 316 } |
| 317 threads_[threads_size_] = state; |
| 318 threads_size_++; |
| 319 } |
| 320 |
| 321 |
| 322 intptr_t ThreadInterrupter::FindThreadIndex(ThreadId id) { |
| 323 // Must be called with monitor_ locked. |
| 324 if (threads_ == NULL) { |
| 325 // We are shutting down. |
| 326 return -1; |
| 327 } |
| 328 for (intptr_t i = 0; i < threads_size_; i++) { |
| 329 if (threads_[i]->id == id) { |
| 330 return i; |
| 331 } |
| 332 } |
| 333 return -1; |
| 334 } |
| 335 |
| 336 |
| 337 ThreadInterrupter::ThreadState* ThreadInterrupter::RemoveThread(intptr_t i) { |
| 338 // Must be called with monitor_ locked. |
| 339 if (threads_ == NULL) { |
| 340 // We are shutting down. |
| 341 return NULL; |
| 342 } |
| 343 ASSERT(i < threads_size_); |
| 344 ThreadState* state = threads_[i]; |
| 345 ASSERT(state != NULL); |
| 346 intptr_t last = threads_size_ - 1; |
| 347 if (i != last) { |
| 348 threads_[i] = threads_[last]; |
| 349 } |
| 350 // Mark last as NULL. |
| 351 threads_[last] = NULL; |
| 352 // Pop. |
| 353 threads_size_--; |
| 354 return state; |
| 355 } |
| 356 |
| 357 |
| 358 void ThreadInterruptNoOp(const InterruptedThreadState& state, void* data) { |
| 359 // NoOp. |
| 360 } |
| 361 |
| 362 void ThreadInterrupter::ThreadMain(uword parameters) { |
| 363 ASSERT(FLAG_thread_interrupter); |
| 364 ASSERT(initialized_); |
| 365 InstallSignalHandler(); |
| 366 if (FLAG_trace_thread_interrupter) { |
| 367 OS::Print("ThreadInterrupter thread running.\n"); |
| 368 } |
| 369 { |
| 370 // Signal to main thread we are ready. |
| 371 MonitorLocker startup_ml(start_stop_monitor_); |
| 372 thread_running_ = true; |
| 373 interrupter_thread_id_ = Thread::GetCurrentThreadId(); |
| 374 startup_ml.Notify(); |
| 375 } |
| 376 monitor_->Enter(); |
| 377 while (!shutdown_) { |
| 378 int64_t current_time = OS::GetCurrentTimeMicros(); |
| 379 InterruptThreads(current_time); |
| 380 monitor_->Exit(); |
| 381 OS::SleepMicros(interrupt_period_); |
| 382 monitor_->Enter(); |
| 383 } |
| 384 if (FLAG_trace_thread_interrupter) { |
| 385 OS::Print("ThreadInterrupter thread exiting.\n"); |
| 386 } |
| 387 { |
| 388 // Signal to main thread we are exiting. |
| 389 MonitorLocker shutdown_ml(start_stop_monitor_); |
| 390 thread_running_ = false; |
| 391 shutdown_ml.Notify(); |
| 392 } |
| 393 } |
| 394 |
| 395 } // namespace dart |
OLD | NEW |