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, trace_thread_interrupter, false, |
| 45 "Trace thread interrupter"); |
| 46 |
| 47 bool ThreadInterrupter::initialized_ = false; |
| 48 bool ThreadInterrupter::shutdown_ = false; |
| 49 bool ThreadInterrupter::thread_running_ = false; |
| 50 ThreadId ThreadInterrupter::interrupter_thread_id_ = Thread::kInvalidThreadId; |
| 51 Monitor* ThreadInterrupter::monitor_ = NULL; |
| 52 intptr_t ThreadInterrupter::interrupt_period_ = 1000; |
| 53 ThreadLocalKey ThreadInterrupter::thread_state_key_ = |
| 54 Thread::kUnsetThreadLocalKey; |
| 55 ThreadInterrupter::ThreadState** ThreadInterrupter::threads_ = NULL; |
| 56 intptr_t ThreadInterrupter::threads_capacity_ = 0; |
| 57 intptr_t ThreadInterrupter::threads_size_ = 0; |
| 58 |
| 59 |
| 60 void ThreadInterrupter::InitOnce() { |
| 61 ASSERT(!initialized_); |
| 62 initialized_ = true; |
| 63 ASSERT(thread_state_key_ == Thread::kUnsetThreadLocalKey); |
| 64 thread_state_key_ = Thread::CreateThreadLocal(); |
| 65 ASSERT(thread_state_key_ != Thread::kUnsetThreadLocalKey); |
| 66 monitor_ = new Monitor(); |
| 67 ResizeThreads(16); |
| 68 if (FLAG_trace_thread_interrupter) { |
| 69 OS::Print("ThreadInterrupter starting up.\n"); |
| 70 } |
| 71 ASSERT(interrupter_thread_id_ == Thread::kInvalidThreadId); |
| 72 { |
| 73 MonitorLocker startup_ml(monitor_); |
| 74 Thread::Start(ThreadMain, 0); |
| 75 while (!thread_running_) { |
| 76 startup_ml.Wait(); |
| 77 } |
| 78 } |
| 79 ASSERT(interrupter_thread_id_ != Thread::kInvalidThreadId); |
| 80 if (FLAG_trace_thread_interrupter) { |
| 81 OS::Print("ThreadInterrupter running.\n"); |
| 82 } |
| 83 } |
| 84 |
| 85 |
| 86 void ThreadInterrupter::Shutdown() { |
| 87 if (shutdown_) { |
| 88 // Already shutdown. |
| 89 return; |
| 90 } |
| 91 ASSERT(initialized_); |
| 92 if (FLAG_trace_thread_interrupter) { |
| 93 OS::Print("ThreadInterrupter shutting down.\n"); |
| 94 } |
| 95 intptr_t size_at_shutdown = 0; |
| 96 { |
| 97 MonitorLocker ml(monitor_); |
| 98 shutdown_ = true; |
| 99 size_at_shutdown = threads_size_; |
| 100 threads_size_ = 0; |
| 101 threads_capacity_ = 0; |
| 102 free(threads_); |
| 103 threads_ = NULL; |
| 104 } |
| 105 { |
| 106 MonitorLocker shutdown_ml(monitor_); |
| 107 while (thread_running_) { |
| 108 shutdown_ml.Wait(); |
| 109 } |
| 110 } |
| 111 interrupter_thread_id_ = Thread::kInvalidThreadId; |
| 112 if (FLAG_trace_thread_interrupter) { |
| 113 OS::Print("ThreadInterrupter shut down (%" Pd ").\n", size_at_shutdown); |
| 114 } |
| 115 } |
| 116 |
| 117 // Delay between interrupts. |
| 118 void ThreadInterrupter::SetInterruptPeriod(intptr_t period) { |
| 119 if (shutdown_) { |
| 120 return; |
| 121 } |
| 122 ASSERT(initialized_); |
| 123 ASSERT(period > 0); |
| 124 { |
| 125 MonitorLocker ml(monitor_); |
| 126 interrupt_period_ = period; |
| 127 } |
| 128 } |
| 129 |
| 130 |
| 131 // Register the currently running thread for interrupts. If the current thread |
| 132 // is already registered, callback and data will be updated. |
| 133 void ThreadInterrupter::Register(ThreadInterruptCallback callback, void* data) { |
| 134 if (shutdown_) { |
| 135 return; |
| 136 } |
| 137 ASSERT(initialized_); |
| 138 { |
| 139 MonitorLocker ml(monitor_); |
| 140 _EnsureThreadStateCreated(); |
| 141 // Set callback and data. |
| 142 UpdateStateObject(callback, data); |
| 143 _Enable(); |
| 144 } |
| 145 } |
| 146 |
| 147 |
| 148 // Unregister the currently running thread for interrupts. |
| 149 void ThreadInterrupter::Unregister() { |
| 150 if (shutdown_) { |
| 151 return; |
| 152 } |
| 153 ASSERT(initialized_); |
| 154 { |
| 155 MonitorLocker ml(monitor_); |
| 156 _EnsureThreadStateCreated(); |
| 157 // Clear callback and data. |
| 158 UpdateStateObject(NULL, NULL); |
| 159 _Disable(); |
| 160 } |
| 161 } |
| 162 |
| 163 |
| 164 void ThreadInterrupter::Enable() { |
| 165 if (shutdown_) { |
| 166 return; |
| 167 } |
| 168 ASSERT(initialized_); |
| 169 { |
| 170 MonitorLocker ml(monitor_); |
| 171 _EnsureThreadStateCreated(); |
| 172 _Enable(); |
| 173 } |
| 174 } |
| 175 |
| 176 |
| 177 void ThreadInterrupter::Disable() { |
| 178 if (shutdown_) { |
| 179 return; |
| 180 } |
| 181 ASSERT(initialized_); |
| 182 { |
| 183 MonitorLocker ml(monitor_); |
| 184 _EnsureThreadStateCreated(); |
| 185 _Disable(); |
| 186 } |
| 187 } |
| 188 |
| 189 |
| 190 void ThreadInterrupter::_EnsureThreadStateCreated() { |
| 191 ThreadState* state = CurrentThreadState(); |
| 192 if (state == NULL) { |
| 193 // Create thread state object lazily. |
| 194 ThreadId current_thread = Thread::GetCurrentThreadId(); |
| 195 if (FLAG_trace_thread_interrupter) { |
| 196 intptr_t tid = Thread::ThreadIdToIntPtr(current_thread); |
| 197 OS::Print("ThreadInterrupter Tracking %p\n", |
| 198 reinterpret_cast<void*>(tid)); |
| 199 } |
| 200 state = new ThreadState(); |
| 201 state->callback = NULL; |
| 202 state->data = NULL; |
| 203 state->id = current_thread; |
| 204 SetCurrentThreadState(state); |
| 205 } |
| 206 } |
| 207 |
| 208 |
| 209 void ThreadInterrupter::_Enable() { |
| 210 // Must be called with monitor_ locked. |
| 211 ThreadId current_thread = Thread::GetCurrentThreadId(); |
| 212 if (Thread::Compare(current_thread, interrupter_thread_id_)) { |
| 213 return; |
| 214 } |
| 215 intptr_t i = FindThreadIndex(current_thread); |
| 216 if (i >= 0) { |
| 217 return; |
| 218 } |
| 219 AddThread(current_thread); |
| 220 if (FLAG_trace_thread_interrupter) { |
| 221 intptr_t tid = Thread::ThreadIdToIntPtr(current_thread); |
| 222 OS::Print("ThreadInterrupter Added %p\n", reinterpret_cast<void*>(tid)); |
| 223 } |
| 224 } |
| 225 |
| 226 void ThreadInterrupter::_Disable() { |
| 227 // Must be called with monitor_ locked. |
| 228 ThreadId current_thread = Thread::GetCurrentThreadId(); |
| 229 if (Thread::Compare(current_thread, interrupter_thread_id_)) { |
| 230 return; |
| 231 } |
| 232 intptr_t index = FindThreadIndex(current_thread); |
| 233 if (index < 0) { |
| 234 // Not registered. |
| 235 return; |
| 236 } |
| 237 ThreadState* state = RemoveThread(index); |
| 238 ASSERT(state != NULL); |
| 239 ASSERT(state == ThreadInterrupter::CurrentThreadState()); |
| 240 if (FLAG_trace_thread_interrupter) { |
| 241 intptr_t tid = Thread::ThreadIdToIntPtr(current_thread); |
| 242 OS::Print("ThreadInterrupter Removed %p\n", reinterpret_cast<void*>(tid)); |
| 243 } |
| 244 } |
| 245 |
| 246 void ThreadInterrupter::UpdateStateObject(ThreadInterruptCallback callback, |
| 247 void* data) { |
| 248 // Must be called with monitor_ locked. |
| 249 ThreadState* state = CurrentThreadState(); |
| 250 ThreadId current_thread = Thread::GetCurrentThreadId(); |
| 251 ASSERT(state != NULL); |
| 252 ASSERT(Thread::Compare(state->id, Thread::GetCurrentThreadId())); |
| 253 SetCurrentThreadState(NULL); |
| 254 // It is now safe to modify the state object. If an interrupt occurs, |
| 255 // the current thread state will be NULL. |
| 256 state->callback = callback; |
| 257 state->data = data; |
| 258 SetCurrentThreadState(state); |
| 259 if (FLAG_trace_thread_interrupter) { |
| 260 intptr_t tid = Thread::ThreadIdToIntPtr(current_thread); |
| 261 if (callback == NULL) { |
| 262 OS::Print("ThreadInterrupter Cleared %p\n", reinterpret_cast<void*>(tid)); |
| 263 } else { |
| 264 OS::Print("ThreadInterrupter Updated %p\n", reinterpret_cast<void*>(tid)); |
| 265 } |
| 266 } |
| 267 } |
| 268 |
| 269 |
| 270 ThreadInterrupter::ThreadState* ThreadInterrupter::CurrentThreadState() { |
| 271 ThreadState* state = reinterpret_cast<ThreadState*>( |
| 272 Thread::GetThreadLocal(thread_state_key_)); |
| 273 return state; |
| 274 } |
| 275 |
| 276 |
| 277 void ThreadInterrupter::SetCurrentThreadState(ThreadState* state) { |
| 278 Thread::SetThreadLocal(thread_state_key_, reinterpret_cast<uword>(state)); |
| 279 } |
| 280 |
| 281 |
| 282 void ThreadInterrupter::ResizeThreads(intptr_t new_capacity) { |
| 283 // Must be called with monitor_ locked. |
| 284 ASSERT(new_capacity < kMaxThreads); |
| 285 ASSERT(new_capacity > threads_capacity_); |
| 286 ThreadState* state = NULL; |
| 287 threads_ = reinterpret_cast<ThreadState**>( |
| 288 realloc(threads_, sizeof(state) * new_capacity)); |
| 289 for (intptr_t i = threads_capacity_; i < new_capacity; i++) { |
| 290 threads_[i] = NULL; |
| 291 } |
| 292 threads_capacity_ = new_capacity; |
| 293 } |
| 294 |
| 295 |
| 296 void ThreadInterrupter::AddThread(ThreadId id) { |
| 297 // Must be called with monitor_ locked. |
| 298 if (threads_ == NULL) { |
| 299 // We are shutting down. |
| 300 return; |
| 301 } |
| 302 ThreadState* state = CurrentThreadState(); |
| 303 if (state->callback == NULL) { |
| 304 // No callback. |
| 305 return; |
| 306 } |
| 307 if (threads_size_ == threads_capacity_) { |
| 308 ResizeThreads(threads_capacity_ == 0 ? 16 : threads_capacity_ * 2); |
| 309 } |
| 310 threads_[threads_size_] = state; |
| 311 threads_size_++; |
| 312 } |
| 313 |
| 314 |
| 315 intptr_t ThreadInterrupter::FindThreadIndex(ThreadId id) { |
| 316 // Must be called with monitor_ locked. |
| 317 if (threads_ == NULL) { |
| 318 // We are shutting down. |
| 319 return -1; |
| 320 } |
| 321 for (intptr_t i = 0; i < threads_size_; i++) { |
| 322 if (threads_[i]->id == id) { |
| 323 return i; |
| 324 } |
| 325 } |
| 326 return -1; |
| 327 } |
| 328 |
| 329 |
| 330 ThreadInterrupter::ThreadState* ThreadInterrupter::RemoveThread(intptr_t i) { |
| 331 // Must be called with monitor_ locked. |
| 332 if (threads_ == NULL) { |
| 333 // We are shutting down. |
| 334 return NULL; |
| 335 } |
| 336 ASSERT(i < threads_size_); |
| 337 ThreadState* state = threads_[i]; |
| 338 ASSERT(state != NULL); |
| 339 intptr_t last = threads_size_ - 1; |
| 340 if (i != last) { |
| 341 threads_[i] = threads_[last]; |
| 342 } |
| 343 // Mark last as NULL. |
| 344 threads_[last] = NULL; |
| 345 // Pop. |
| 346 threads_size_--; |
| 347 return state; |
| 348 } |
| 349 |
| 350 |
| 351 void ThreadInterruptNoOp(const InterruptedThreadState& state, void* data) { |
| 352 // NoOp. |
| 353 } |
| 354 |
| 355 void ThreadInterrupter::ThreadMain(uword parameters) { |
| 356 ASSERT(initialized_); |
| 357 InstallSignalHandler(); |
| 358 if (FLAG_trace_thread_interrupter) { |
| 359 OS::Print("ThreadInterrupter thread running.\n"); |
| 360 } |
| 361 { |
| 362 // Signal to main thread we are ready. |
| 363 MonitorLocker startup_ml(monitor_); |
| 364 thread_running_ = true; |
| 365 interrupter_thread_id_ = Thread::GetCurrentThreadId(); |
| 366 startup_ml.Notify(); |
| 367 } |
| 368 { |
| 369 MonitorLocker ml(monitor_); |
| 370 while (!shutdown_) { |
| 371 int64_t current_time = OS::GetCurrentTimeMicros(); |
| 372 InterruptThreads(current_time); |
| 373 ml.WaitMicros(interrupt_period_); |
| 374 } |
| 375 } |
| 376 if (FLAG_trace_thread_interrupter) { |
| 377 OS::Print("ThreadInterrupter thread exiting.\n"); |
| 378 } |
| 379 { |
| 380 // Signal to main thread we are exiting. |
| 381 MonitorLocker shutdown_ml(monitor_); |
| 382 thread_running_ = false; |
| 383 shutdown_ml.Notify(); |
| 384 } |
| 385 } |
| 386 |
| 387 } // namespace dart |
OLD | NEW |