Index: runtime/vm/thread_interrupter.cc |
diff --git a/runtime/vm/thread_interrupter.cc b/runtime/vm/thread_interrupter.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b1b79e15bac9d89d266155bc9880d95be84a8327 |
--- /dev/null |
+++ b/runtime/vm/thread_interrupter.cc |
@@ -0,0 +1,308 @@ |
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+#include <cstdio> |
siva
2013/12/11 02:52:21
what is this?
We don't include c++ header files n
Cutch
2013/12/11 17:44:56
Done.
|
+ |
+#include "vm/thread_interrupter.h" |
+ |
+namespace dart { |
+ |
+// Notes: |
+// |
+// The ThreadInterrupter interrupts all registered threads once per |
+// interrupt period (default is every millisecond). While the thread is |
+// interrupted, the thread's interrupt callback is invoked. Callbacks cannot |
+// rely on being executed on the interrupted thread. |
+// |
+// There are two mechanisms used to interrupt a thread. The first, used on OSs |
+// with pthreads (Android, Linux, and Mac), is thread specific signal delivery. |
+// The second, used on Windows, is explicit suspend and resume thread system |
+// calls. Signal delivery forbids taking locks and allocating memory (which |
+// takes a lock). Explicit suspend and resume means that the interrupt callback |
+// will not be executing on the interrupted thread, making it meaningless to |
+// access TLS from within the thread interrupt callback. Combining these |
+// limitations, thread interrupt callbacks are forbidden from: |
+// |
+// * Accessing TLS. |
+// * Allocating memory. |
+// * Taking a lock. |
+// |
+// The ThreadInterrupter has a single monitor (monitor_). This monitor guards |
+// access to the list of threads registered to receive interrupts (threads_). |
+// |
+// A thread can only register and unregister itself. Each thread has a heap |
+// allocated ThreadState. A thread's ThreadState is lazily allocated the first |
+// time the thread is registered. A pointer to a thread's ThreadState is stored |
+// in the list of threads registered to receive interrupts (threads_) and in |
+// thread local storage. When a thread's ThreadState is being modified, the |
+// thread local storage pointer is temporarily set to NULL while the |
+// modification is occurring. After the ThreadState has been updated, the |
+// thread local storage pointer is set again. This has an important side |
+// effect: if the thread is interrupted by a signal handler during a ThreadState |
+// update the signal handler will immediately return. |
+ |
+DEFINE_FLAG(bool, thread_interrupter, true, "Enable thread interrupter"); |
+DEFINE_FLAG(bool, trace_thread_interrupter, false, |
+ "Trace thread interrupter"); |
+ |
+bool ThreadInterrupter::initialized_ = false; |
+bool ThreadInterrupter::shutdown_ = false; |
+bool ThreadInterrupter::thread_running_ = false; |
+ThreadId ThreadInterrupter::interrupter_thread_id_ = Thread::kInvalidThreadId; |
+Monitor* ThreadInterrupter::monitor_ = NULL; |
+Monitor* ThreadInterrupter::start_stop_monitor_ = NULL; |
+intptr_t ThreadInterrupter::interrupt_period_ = 1000; |
+ThreadLocalKey ThreadInterrupter::thread_state_key_ = |
+ Thread::kUnsetThreadLocalKey; |
+ThreadInterrupter::ThreadState** ThreadInterrupter::threads_ = NULL; |
+intptr_t ThreadInterrupter::threads_capacity_ = 0; |
+intptr_t ThreadInterrupter::threads_size_ = 0; |
+ |
+ |
+void ThreadInterrupter::InitOnce() { |
+ if (!FLAG_thread_interrupter) { |
+ return; |
+ } |
+ ASSERT(!initialized_); |
+ initialized_ = true; |
+ ASSERT(thread_state_key_ == Thread::kUnsetThreadLocalKey); |
+ thread_state_key_ = Thread::CreateThreadLocal(); |
+ ASSERT(thread_state_key_ != Thread::kUnsetThreadLocalKey); |
+ monitor_ = new Monitor(); |
+ start_stop_monitor_ = new Monitor(); |
+ ResizeThreads(16); |
+ if (FLAG_trace_thread_interrupter) { |
+ OS::Print("ThreadInterrupter starting up.\n"); |
+ } |
+ ASSERT(interrupter_thread_id_ == Thread::kInvalidThreadId); |
+ { |
+ MonitorLocker startup_ml(start_stop_monitor_); |
+ Thread::Start(ThreadMain, 0); |
+ while (!thread_running_) { |
+ startup_ml.Wait(); |
+ } |
+ } |
+ ASSERT(interrupter_thread_id_ != Thread::kInvalidThreadId); |
+ if (FLAG_trace_thread_interrupter) { |
+ OS::Print("ThreadInterrupter running.\n"); |
+ } |
+} |
+ |
+ |
+void ThreadInterrupter::Shutdown() { |
+ if (!FLAG_thread_interrupter) { |
+ return; |
+ } |
+ ASSERT(initialized_); |
+ if (FLAG_trace_thread_interrupter) { |
+ OS::Print("ThreadInterrupter shutting down.\n"); |
+ } |
+ intptr_t size_at_shutdown = 0; |
+ { |
+ MonitorLocker ml(monitor_); |
+ shutdown_ = true; |
+ size_at_shutdown = threads_size_; |
+ threads_size_ = 0; |
+ threads_capacity_ = 0; |
+ free(threads_); |
+ threads_ = NULL; |
+ ml.Notify(); |
+ } |
+ { |
+ MonitorLocker shutdown_ml(start_stop_monitor_); |
+ while (thread_running_) { |
+ shutdown_ml.Wait(); |
+ } |
+ } |
+ if (FLAG_trace_thread_interrupter) { |
+ OS::Print("ThreadInterrupter shut down (%" Pd ").\n", size_at_shutdown); |
+ } |
+} |
+ |
+// Delay between interrupts. |
+void ThreadInterrupter::SetInterruptPeriod(intptr_t period) { |
+ if (!FLAG_thread_interrupter) { |
+ return; |
+ } |
+ ASSERT(period > 0); |
+ { |
+ MonitorLocker ml(monitor_); |
+ interrupt_period_ = period; |
+ } |
+} |
+ |
+ |
+// Register the currently running thread for interrupts. If the current thread |
+// is already registered, callback and data will be updated. |
+void ThreadInterrupter::Register(ThreadInterruptCallback callback, void* data) { |
+ if (!FLAG_thread_interrupter) { |
+ return; |
+ } |
+ ThreadId current_thread = Thread::GetCurrentThreadId(); |
+ ASSERT(!Thread::Compare(current_thread, interrupter_thread_id_)); |
+ { |
+ MonitorLocker ml(monitor_); |
+ // Set callback and data. |
+ UpdateStateObject(callback, data); |
+ |
+ intptr_t i = FindThreadIndex(current_thread); |
+ if (i >= 0) { |
+ return; |
+ } |
+ AddThread(current_thread, callback, data); |
+ if (FLAG_trace_thread_interrupter) { |
+ intptr_t tid = Thread::ThreadIdToIntPtr(current_thread); |
+ OS::Print("ThreadInterrupter Added %p\n", reinterpret_cast<void*>(tid)); |
+ } |
+ ml.Notify(); |
siva
2013/12/11 02:52:21
Why is this notify needed? The next wake up of the
Cutch
2013/12/11 17:44:56
The notify is there in case we switch the thread t
|
+ } |
+} |
+ |
+ |
+// Unregister the currently running thread for interrupts. |
+void ThreadInterrupter::Unregister() { |
+ if (!FLAG_thread_interrupter) { |
+ return; |
+ } |
+ ThreadId current_thread = Thread::GetCurrentThreadId(); |
+ ASSERT(!Thread::Compare(current_thread, interrupter_thread_id_)); |
+ { |
+ MonitorLocker ml(monitor_); |
+ // Clear callback and data. |
+ UpdateStateObject(NULL, NULL); |
+ |
+ intptr_t index = FindThreadIndex(current_thread); |
+ if (index < 0) { |
+ // Not registered. |
+ return; |
+ } |
+ ThreadState* state = RemoveThread(index); |
+ ASSERT(state != NULL); |
+ ASSERT(state == ThreadInterrupter::CurrentThreadState()); |
+ if (FLAG_trace_thread_interrupter) { |
+ intptr_t tid = Thread::ThreadIdToIntPtr(current_thread); |
+ OS::Print("ThreadInterrupter Removed %p\n", reinterpret_cast<void*>(tid)); |
+ } |
+ ml.Notify(); |
siva
2013/12/11 02:52:21
Ditto comment about this Notify.
|
+ } |
+} |
+ |
+ |
+void ThreadInterrupter::UpdateStateObject(ThreadInterruptCallback callback, |
+ void* data) { |
+ ThreadState* state = CurrentThreadState(); |
+ ThreadId current_thread = Thread::GetCurrentThreadId(); |
+ if (state == NULL) { |
+ // Create thread state object lazily. |
+ if (FLAG_trace_thread_interrupter) { |
+ intptr_t tid = Thread::ThreadIdToIntPtr(current_thread); |
+ OS::Print("ThreadInterrupter Tracking %p\n", |
+ reinterpret_cast<void*>(tid)); |
+ } |
+ state = new ThreadState(); |
+ state->id = current_thread; |
+ SetCurrentThreadState(state); |
+ } |
+ ASSERT(state != NULL); |
+ ASSERT(Thread::Compare(state->id, Thread::GetCurrentThreadId())); |
+ SetCurrentThreadState(NULL); |
+ // It is now safe to modify the state object. If an interrupt occurs, |
+ // the current thread state will be NULL. |
+ state->callback = callback; |
+ state->data = data; |
+ SetCurrentThreadState(state); |
+ if (FLAG_trace_thread_interrupter) { |
+ intptr_t tid = Thread::ThreadIdToIntPtr(current_thread); |
+ if (callback == NULL) { |
+ OS::Print("ThreadInterrupter Cleared %p\n", reinterpret_cast<void*>(tid)); |
+ } else { |
+ OS::Print("ThreadInterrupter Updated %p\n", reinterpret_cast<void*>(tid)); |
+ } |
+ } |
+} |
+ |
+ |
+ThreadInterrupter::ThreadState* ThreadInterrupter::CurrentThreadState() { |
+ return reinterpret_cast<ThreadState*>( |
+ Thread::GetThreadLocal(thread_state_key_)); |
+} |
+ |
+ |
+void ThreadInterrupter::SetCurrentThreadState(ThreadState* state) { |
+ Thread::SetThreadLocal(thread_state_key_, reinterpret_cast<uword>(state)); |
+} |
+ |
+ |
+void ThreadInterrupter::ResizeThreads(intptr_t new_capacity) { |
siva
2013/12/11 02:52:21
// Must be called with monitor_ locked.
Cutch
2013/12/11 17:44:56
Done.
|
+ ASSERT(new_capacity < kMaxThreads); |
+ ASSERT(new_capacity > threads_capacity_); |
+ ThreadState* state = NULL; |
+ threads_ = reinterpret_cast<ThreadState**>( |
+ realloc(threads_, sizeof(state) * new_capacity)); |
+ for (intptr_t i = threads_capacity_; i < new_capacity; i++) { |
+ threads_[i] = NULL; |
+ } |
+ threads_capacity_ = new_capacity; |
+} |
+ |
+ |
+void ThreadInterrupter::AddThread(ThreadId id, ThreadInterruptCallback callback, |
+ void* data) { |
+ // Must be called with monitor_ locked. |
+ if (threads_ == NULL) { |
+ // We are shutting down. |
+ return; |
+ } |
+ if (threads_size_ == threads_capacity_) { |
+ ResizeThreads(threads_capacity_ == 0 ? 16 : threads_capacity_ * 2); |
+ } |
+ UpdateStateObject(callback, data); |
+ ThreadState* state = CurrentThreadState(); |
+ ASSERT(state != NULL); |
+ threads_[threads_size_] = state; |
+ threads_size_++; |
+} |
+ |
+ |
+intptr_t ThreadInterrupter::FindThreadIndex(ThreadId id) { |
+ // Must be called with monitor_ locked. |
+ if (threads_ == NULL) { |
+ // We are shutting down. |
+ return -1; |
+ } |
+ for (intptr_t i = 0; i < threads_size_; i++) { |
+ if (threads_[i]->id == id) { |
+ return i; |
+ } |
+ } |
+ return -1; |
+} |
+ |
+ |
+ThreadInterrupter::ThreadState* ThreadInterrupter::RemoveThread(intptr_t i) { |
+ // Must be called with monitor_ locked. |
+ if (threads_ == NULL) { |
+ // We are shutting down. |
+ return NULL; |
+ } |
+ ASSERT(i < threads_size_); |
+ ThreadState* state = threads_[i]; |
+ ASSERT(state != NULL); |
+ intptr_t last = threads_size_ - 1; |
+ if (i != last) { |
+ threads_[i] = threads_[last]; |
+ } |
+ // Mark last as NULL. |
+ threads_[last] = NULL; |
+ // Pop. |
+ threads_size_--; |
+ return state; |
+} |
+ |
+ |
+void ThreadInterruptNoOp(const InterruptedThreadState& state, void* data) { |
+ // NoOp. |
+} |
+ |
+} // namespace dart |