Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: runtime/vm/thread_interrupter.cc

Issue 128653004: Use list of isolates in profiler (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/thread_interrupter.h ('k') | runtime/vm/thread_interrupter_android.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/simulator.h" 5 #include "vm/simulator.h"
6 #include "vm/thread_interrupter.h" 6 #include "vm/thread_interrupter.h"
7 7
8 namespace dart { 8 namespace dart {
9 9
10 // Notes: 10 // Notes:
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 "Trace thread interrupter"); 45 "Trace thread interrupter");
46 46
47 bool ThreadInterrupter::initialized_ = false; 47 bool ThreadInterrupter::initialized_ = false;
48 bool ThreadInterrupter::shutdown_ = false; 48 bool ThreadInterrupter::shutdown_ = false;
49 bool ThreadInterrupter::thread_running_ = false; 49 bool ThreadInterrupter::thread_running_ = false;
50 ThreadId ThreadInterrupter::interrupter_thread_id_ = Thread::kInvalidThreadId; 50 ThreadId ThreadInterrupter::interrupter_thread_id_ = Thread::kInvalidThreadId;
51 Monitor* ThreadInterrupter::monitor_ = NULL; 51 Monitor* ThreadInterrupter::monitor_ = NULL;
52 intptr_t ThreadInterrupter::interrupt_period_ = 1000; 52 intptr_t ThreadInterrupter::interrupt_period_ = 1000;
53 ThreadLocalKey ThreadInterrupter::thread_state_key_ = 53 ThreadLocalKey ThreadInterrupter::thread_state_key_ =
54 Thread::kUnsetThreadLocalKey; 54 Thread::kUnsetThreadLocalKey;
55 ThreadInterrupter::ThreadState** ThreadInterrupter::threads_ = NULL;
56 intptr_t ThreadInterrupter::threads_capacity_ = 0;
57 intptr_t ThreadInterrupter::threads_size_ = 0;
58 55
59 56
60 void ThreadInterrupter::InitOnce() { 57 void ThreadInterrupter::InitOnce() {
61 ASSERT(!initialized_); 58 ASSERT(!initialized_);
62 initialized_ = true;
63 ASSERT(thread_state_key_ == Thread::kUnsetThreadLocalKey); 59 ASSERT(thread_state_key_ == Thread::kUnsetThreadLocalKey);
64 thread_state_key_ = Thread::CreateThreadLocal(); 60 thread_state_key_ = Thread::CreateThreadLocal();
65 ASSERT(thread_state_key_ != Thread::kUnsetThreadLocalKey); 61 ASSERT(thread_state_key_ != Thread::kUnsetThreadLocalKey);
66 monitor_ = new Monitor(); 62 monitor_ = new Monitor();
67 ResizeThreads(16); 63 ASSERT(monitor_ != NULL);
64 initialized_ = true;
65 }
66
67
68 void ThreadInterrupter::Startup() {
69 ASSERT(initialized_);
68 if (FLAG_trace_thread_interrupter) { 70 if (FLAG_trace_thread_interrupter) {
69 OS::Print("ThreadInterrupter starting up.\n"); 71 OS::Print("ThreadInterrupter starting up.\n");
70 } 72 }
71 ASSERT(interrupter_thread_id_ == Thread::kInvalidThreadId); 73 ASSERT(interrupter_thread_id_ == Thread::kInvalidThreadId);
72 { 74 {
73 MonitorLocker startup_ml(monitor_); 75 MonitorLocker startup_ml(monitor_);
74 Thread::Start(ThreadMain, 0); 76 Thread::Start(ThreadMain, 0);
75 while (!thread_running_) { 77 while (!thread_running_) {
76 startup_ml.Wait(); 78 startup_ml.Wait();
77 } 79 }
78 } 80 }
79 ASSERT(interrupter_thread_id_ != Thread::kInvalidThreadId); 81 ASSERT(interrupter_thread_id_ != Thread::kInvalidThreadId);
80 if (FLAG_trace_thread_interrupter) { 82 if (FLAG_trace_thread_interrupter) {
81 OS::Print("ThreadInterrupter running.\n"); 83 OS::Print("ThreadInterrupter running.\n");
82 } 84 }
83 } 85 }
84 86
85 87
86 void ThreadInterrupter::Shutdown() { 88 void ThreadInterrupter::Shutdown() {
87 if (shutdown_) { 89 if (shutdown_) {
88 // Already shutdown. 90 // Already shutdown.
89 return; 91 return;
90 } 92 }
91 ASSERT(initialized_); 93 ASSERT(initialized_);
92 if (FLAG_trace_thread_interrupter) { 94 if (FLAG_trace_thread_interrupter) {
93 OS::Print("ThreadInterrupter shutting down.\n"); 95 OS::Print("ThreadInterrupter shutting down.\n");
94 } 96 }
95 intptr_t size_at_shutdown = 0;
96 { 97 {
97 MonitorLocker ml(monitor_); 98 MonitorLocker ml(monitor_);
98 shutdown_ = true; 99 shutdown_ = true;
99 size_at_shutdown = threads_size_;
100 threads_size_ = 0;
101 threads_capacity_ = 0;
102 free(threads_);
103 threads_ = NULL;
104 } 100 }
105 { 101 {
106 MonitorLocker shutdown_ml(monitor_); 102 MonitorLocker shutdown_ml(monitor_);
107 while (thread_running_) { 103 while (thread_running_) {
108 shutdown_ml.Wait(); 104 shutdown_ml.Wait();
109 } 105 }
110 } 106 }
111 interrupter_thread_id_ = Thread::kInvalidThreadId; 107 interrupter_thread_id_ = Thread::kInvalidThreadId;
112 if (FLAG_trace_thread_interrupter) { 108 if (FLAG_trace_thread_interrupter) {
113 OS::Print("ThreadInterrupter shut down (%" Pd ").\n", size_at_shutdown); 109 OS::Print("ThreadInterrupter shut down.\n");
114 } 110 }
115 } 111 }
116 112
117 // Delay between interrupts. 113 // Delay between interrupts.
118 void ThreadInterrupter::SetInterruptPeriod(intptr_t period) { 114 void ThreadInterrupter::SetInterruptPeriod(intptr_t period) {
119 if (shutdown_) { 115 if (shutdown_) {
120 return; 116 return;
121 } 117 }
122 ASSERT(initialized_); 118 ASSERT(initialized_);
123 ASSERT(period > 0); 119 ASSERT(period > 0);
124 { 120 interrupt_period_ = period;
125 MonitorLocker ml(monitor_);
126 interrupt_period_ = period;
127 }
128 } 121 }
129 122
130 123
131 // Register the currently running thread for interrupts. If the current thread 124 // Register the currently running thread for interrupts. If the current thread
132 // is already registered, callback and data will be updated. 125 // is already registered, callback and data will be updated.
133 void ThreadInterrupter::Register(ThreadInterruptCallback callback, void* data) { 126 InterruptableThreadState* ThreadInterrupter::Register(
127 ThreadInterruptCallback callback, void* data) {
134 if (shutdown_) { 128 if (shutdown_) {
135 return; 129 return NULL;
136 } 130 }
137 ASSERT(initialized_); 131 ASSERT(initialized_);
138 { 132 InterruptableThreadState* state = _EnsureThreadStateCreated();
139 MonitorLocker ml(monitor_); 133 // Set callback and data.
140 _EnsureThreadStateCreated(); 134 UpdateStateObject(callback, data);
141 // Set callback and data. 135 return state;
142 UpdateStateObject(callback, data);
143 _Enable();
144 }
145 } 136 }
146 137
147 138
148 // Unregister the currently running thread for interrupts. 139 // Unregister the currently running thread for interrupts.
149 void ThreadInterrupter::Unregister() { 140 void ThreadInterrupter::Unregister() {
150 if (shutdown_) { 141 if (shutdown_) {
151 return; 142 return;
152 } 143 }
153 ASSERT(initialized_); 144 ASSERT(initialized_);
154 { 145 _EnsureThreadStateCreated();
155 MonitorLocker ml(monitor_); 146 // Clear callback and data.
156 _EnsureThreadStateCreated(); 147 UpdateStateObject(NULL, NULL);
157 // Clear callback and data.
158 UpdateStateObject(NULL, NULL);
159 _Disable();
160 }
161 } 148 }
162 149
163 150
164 void ThreadInterrupter::Enable() { 151 InterruptableThreadState* ThreadInterrupter::_EnsureThreadStateCreated() {
165 if (shutdown_) { 152 InterruptableThreadState* state = CurrentThreadState();
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) { 153 if (state == NULL) {
193 // Create thread state object lazily. 154 // Create thread state object lazily.
194 ThreadId current_thread = Thread::GetCurrentThreadId(); 155 ThreadId current_thread = Thread::GetCurrentThreadId();
195 if (FLAG_trace_thread_interrupter) { 156 if (FLAG_trace_thread_interrupter) {
196 intptr_t tid = Thread::ThreadIdToIntPtr(current_thread); 157 intptr_t tid = Thread::ThreadIdToIntPtr(current_thread);
197 OS::Print("ThreadInterrupter Tracking %p\n", 158 OS::Print("ThreadInterrupter Tracking %p\n",
198 reinterpret_cast<void*>(tid)); 159 reinterpret_cast<void*>(tid));
199 } 160 }
200 state = new ThreadState(); 161 // Note: We currently do not free a thread's InterruptableThreadState.
162 state = new InterruptableThreadState();
163 ASSERT(state != NULL);
201 state->callback = NULL; 164 state->callback = NULL;
202 state->data = NULL; 165 state->data = NULL;
203 state->id = current_thread; 166 state->id = current_thread;
204 SetCurrentThreadState(state); 167 SetCurrentThreadState(state);
205 } 168 }
169 return state;
206 } 170 }
207 171
208 172
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, 173 void ThreadInterrupter::UpdateStateObject(ThreadInterruptCallback callback,
247 void* data) { 174 void* data) {
248 // Must be called with monitor_ locked. 175 InterruptableThreadState* state = CurrentThreadState();
249 ThreadState* state = CurrentThreadState();
250 ThreadId current_thread = Thread::GetCurrentThreadId(); 176 ThreadId current_thread = Thread::GetCurrentThreadId();
251 ASSERT(state != NULL); 177 ASSERT(state != NULL);
252 ASSERT(Thread::Compare(state->id, Thread::GetCurrentThreadId())); 178 ASSERT(Thread::Compare(state->id, Thread::GetCurrentThreadId()));
253 SetCurrentThreadState(NULL); 179 SetCurrentThreadState(NULL);
254 // It is now safe to modify the state object. If an interrupt occurs, 180 // It is now safe to modify the state object. If an interrupt occurs,
255 // the current thread state will be NULL. 181 // the current thread state will be NULL.
256 state->callback = callback; 182 state->callback = callback;
257 state->data = data; 183 state->data = data;
258 SetCurrentThreadState(state); 184 SetCurrentThreadState(state);
259 if (FLAG_trace_thread_interrupter) { 185 if (FLAG_trace_thread_interrupter) {
260 intptr_t tid = Thread::ThreadIdToIntPtr(current_thread); 186 intptr_t tid = Thread::ThreadIdToIntPtr(current_thread);
261 if (callback == NULL) { 187 if (callback == NULL) {
262 OS::Print("ThreadInterrupter Cleared %p\n", reinterpret_cast<void*>(tid)); 188 OS::Print("ThreadInterrupter Cleared %p\n", reinterpret_cast<void*>(tid));
263 } else { 189 } else {
264 OS::Print("ThreadInterrupter Updated %p\n", reinterpret_cast<void*>(tid)); 190 OS::Print("ThreadInterrupter Updated %p\n", reinterpret_cast<void*>(tid));
265 } 191 }
266 } 192 }
267 } 193 }
268 194
269 195
270 ThreadInterrupter::ThreadState* ThreadInterrupter::CurrentThreadState() { 196 InterruptableThreadState* ThreadInterrupter::GetCurrentThreadState() {
271 ThreadState* state = reinterpret_cast<ThreadState*>( 197 return _EnsureThreadStateCreated();
198 }
199
200
201 InterruptableThreadState* ThreadInterrupter::CurrentThreadState() {
202 InterruptableThreadState* state = reinterpret_cast<InterruptableThreadState*>(
272 Thread::GetThreadLocal(thread_state_key_)); 203 Thread::GetThreadLocal(thread_state_key_));
273 return state; 204 return state;
274 } 205 }
275 206
276 207
277 void ThreadInterrupter::SetCurrentThreadState(ThreadState* state) { 208 void ThreadInterrupter::SetCurrentThreadState(InterruptableThreadState* state) {
278 Thread::SetThreadLocal(thread_state_key_, reinterpret_cast<uword>(state)); 209 Thread::SetThreadLocal(thread_state_key_, reinterpret_cast<uword>(state));
279 } 210 }
280 211
281 212
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) { 213 void ThreadInterruptNoOp(const InterruptedThreadState& state, void* data) {
352 // NoOp. 214 // NoOp.
353 } 215 }
354 216
355 217
218 class ThreadInterrupterVisitIsolates : public IsolateVisitor {
219 public:
220 ThreadInterrupterVisitIsolates() { }
221 void VisitIsolate(Isolate* isolate) {
222 ASSERT(isolate != NULL);
223 InterruptableThreadState* state = isolate->thread_state();
224 if (state == NULL) {
225 return;
226 }
227 ASSERT(state->id != Thread::kInvalidThreadId);
228 ThreadInterrupter::InterruptThread(state);
229 }
230 };
231
232
356 void ThreadInterrupter::ThreadMain(uword parameters) { 233 void ThreadInterrupter::ThreadMain(uword parameters) {
357 ASSERT(initialized_); 234 ASSERT(initialized_);
358 InstallSignalHandler(); 235 InstallSignalHandler();
359 if (FLAG_trace_thread_interrupter) { 236 if (FLAG_trace_thread_interrupter) {
360 OS::Print("ThreadInterrupter thread running.\n"); 237 OS::Print("ThreadInterrupter thread running.\n");
361 } 238 }
362 { 239 {
363 // Signal to main thread we are ready. 240 // Signal to main thread we are ready.
364 MonitorLocker startup_ml(monitor_); 241 MonitorLocker startup_ml(monitor_);
365 thread_running_ = true; 242 thread_running_ = true;
366 interrupter_thread_id_ = Thread::GetCurrentThreadId(); 243 interrupter_thread_id_ = Thread::GetCurrentThreadId();
367 startup_ml.Notify(); 244 startup_ml.Notify();
368 } 245 }
369 { 246 {
370 MonitorLocker ml(monitor_); 247 MonitorLocker wait_ml(monitor_);
248 ThreadInterrupterVisitIsolates visitor;
371 while (!shutdown_) { 249 while (!shutdown_) {
372 int64_t current_time = OS::GetCurrentTimeMicros(); 250 Isolate::VisitIsolates(&visitor);
373 InterruptThreads(current_time); 251 wait_ml.WaitMicros(interrupt_period_);
374 ml.WaitMicros(interrupt_period_);
375 } 252 }
376 } 253 }
377 if (FLAG_trace_thread_interrupter) { 254 if (FLAG_trace_thread_interrupter) {
378 OS::Print("ThreadInterrupter thread exiting.\n"); 255 OS::Print("ThreadInterrupter thread exiting.\n");
379 } 256 }
380 { 257 {
381 // Signal to main thread we are exiting. 258 // Signal to main thread we are exiting.
382 MonitorLocker shutdown_ml(monitor_); 259 MonitorLocker shutdown_ml(monitor_);
383 thread_running_ = false; 260 thread_running_ = false;
384 shutdown_ml.Notify(); 261 shutdown_ml.Notify();
385 } 262 }
386 } 263 }
387 264
388 } // namespace dart 265 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/thread_interrupter.h ('k') | runtime/vm/thread_interrupter_android.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698