OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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/message_handler.h" | 5 #include "vm/message_handler.h" |
6 | 6 |
7 #include "vm/dart.h" | 7 #include "vm/dart.h" |
8 #include "vm/lockers.h" | 8 #include "vm/lockers.h" |
9 #include "vm/os.h" | 9 #include "vm/os.h" |
10 #include "vm/port.h" | 10 #include "vm/port.h" |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 bool MessageHandler::HasOOBMessages() { | 231 bool MessageHandler::HasOOBMessages() { |
232 MonitorLocker ml(&monitor_); | 232 MonitorLocker ml(&monitor_); |
233 return !oob_queue_->IsEmpty(); | 233 return !oob_queue_->IsEmpty(); |
234 } | 234 } |
235 | 235 |
236 | 236 |
237 void MessageHandler::TaskCallback() { | 237 void MessageHandler::TaskCallback() { |
238 ASSERT(Isolate::Current() == NULL); | 238 ASSERT(Isolate::Current() == NULL); |
239 bool ok = true; | 239 bool ok = true; |
240 bool run_end_callback = false; | 240 bool run_end_callback = false; |
241 bool notify_paused_on_exit = false; | |
242 { | 241 { |
243 MonitorLocker ml(&monitor_); | 242 MonitorLocker ml(&monitor_); |
244 // Initialize the message handler by running its start function, | 243 // Initialize the message handler by running its start function, |
245 // if we have one. For an isolate, this will run the isolate's | 244 // if we have one. For an isolate, this will run the isolate's |
246 // main() function. | 245 // main() function. |
247 if (pause_on_start()) { | 246 if (pause_on_start()) { |
248 if (!paused_on_start_) { | 247 if (!paused_on_start_) { |
249 // Temporarily drop the lock when calling out to NotifyPauseOnStart. | 248 // Temporarily drop the lock when calling out to NotifyPauseOnStart. |
250 // This avoids a dead lock that can occur when this message handler | 249 // This avoids a dead lock that can occur when this message handler |
251 // tries to post a message while a message is being posted to it. | 250 // tries to post a message while a message is being posted to it. |
252 paused_on_start_ = true; | 251 paused_on_start_ = true; |
253 paused_timestamp_ = OS::GetCurrentTimeMillis(); | 252 paused_timestamp_ = OS::GetCurrentTimeMillis(); |
254 monitor_.Exit(); | 253 monitor_.Exit(); |
255 NotifyPauseOnStart(); | 254 NotifyPauseOnStart(); |
256 monitor_.Enter(); | 255 monitor_.Enter(); |
257 } | 256 } |
| 257 // More messages may have come in while we released monitor_. |
258 HandleMessages(false, false); | 258 HandleMessages(false, false); |
259 if (pause_on_start()) { | 259 if (pause_on_start()) { |
260 // Still paused. | 260 // Still paused. |
| 261 ASSERT(oob_queue_->IsEmpty()); |
261 task_ = NULL; // No task in queue. | 262 task_ = NULL; // No task in queue. |
262 return; | 263 return; |
263 } else { | 264 } else { |
264 paused_on_start_ = false; | 265 paused_on_start_ = false; |
265 paused_timestamp_ = -1; | 266 paused_timestamp_ = -1; |
266 } | 267 } |
267 } | 268 } |
268 | 269 |
269 if (start_callback_) { | 270 if (start_callback_) { |
270 // Release the monitor_ temporarily while we call the start callback. | 271 // Release the monitor_ temporarily while we call the start callback. |
271 // The monitor was acquired with the MonitorLocker above. | 272 // The monitor was acquired with the MonitorLocker above. |
272 monitor_.Exit(); | 273 monitor_.Exit(); |
273 ok = start_callback_(callback_data_); | 274 ok = start_callback_(callback_data_); |
274 ASSERT(Isolate::Current() == NULL); | 275 ASSERT(Isolate::Current() == NULL); |
275 start_callback_ = NULL; | 276 start_callback_ = NULL; |
276 monitor_.Enter(); | 277 monitor_.Enter(); |
277 } | 278 } |
278 | 279 |
279 // Handle any pending messages for this message handler. | 280 // Handle any pending messages for this message handler. |
280 if (ok) { | 281 if (ok) { |
281 ok = HandleMessages(true, true); | 282 ok = HandleMessages(true, true); |
282 } | 283 } |
283 task_ = NULL; // No task in queue. | |
284 | 284 |
285 if (!ok || !HasLivePorts()) { | 285 if (!ok || !HasLivePorts()) { |
286 if (pause_on_exit()) { | 286 if (pause_on_exit()) { |
287 if (!paused_on_exit_) { | 287 if (!paused_on_exit_) { |
288 if (FLAG_trace_service_pause_events) { | 288 if (FLAG_trace_service_pause_events) { |
289 OS::PrintErr("Isolate %s paused before exiting. " | 289 OS::PrintErr("Isolate %s paused before exiting. " |
290 "Use the Observatory to release it.\n", name()); | 290 "Use the Observatory to release it.\n", name()); |
291 } | 291 } |
292 notify_paused_on_exit = true; | 292 // Temporarily drop the lock when calling out to NotifyPauseOnExit. |
| 293 // This avoids a dead lock that can occur when this message handler |
| 294 // tries to post a message while a message is being posted to it. |
293 paused_on_exit_ = true; | 295 paused_on_exit_ = true; |
294 paused_timestamp_ = OS::GetCurrentTimeMillis(); | 296 paused_timestamp_ = OS::GetCurrentTimeMillis(); |
| 297 monitor_.Exit(); |
| 298 NotifyPauseOnExit(); |
| 299 monitor_.Enter(); |
295 } | 300 } |
296 } else { | 301 // More messages may have come in while we released monitor_. |
297 if (FLAG_trace_isolates) { | 302 HandleMessages(false, false); |
| 303 if (pause_on_exit()) { |
| 304 // Still paused. |
| 305 ASSERT(oob_queue_->IsEmpty()); |
| 306 task_ = NULL; // No task in queue. |
| 307 return; |
| 308 } else { |
| 309 paused_on_exit_ = false; |
| 310 paused_timestamp_ = -1; |
| 311 } |
| 312 } |
| 313 if (FLAG_trace_isolates) { |
298 OS::Print("[-] Stopping message handler (%s):\n" | 314 OS::Print("[-] Stopping message handler (%s):\n" |
299 "\thandler: %s\n", | 315 "\thandler: %s\n", |
300 (ok ? "no live ports" : "error"), | 316 (ok ? "no live ports" : "error"), |
301 name()); | 317 name()); |
302 } | |
303 pool_ = NULL; | |
304 run_end_callback = true; | |
305 paused_on_exit_ = false; | |
306 paused_timestamp_ = -1; | |
307 } | 318 } |
| 319 pool_ = NULL; |
| 320 run_end_callback = true; |
308 } | 321 } |
309 } | 322 |
310 // At this point we no longer hold the message handler lock. | 323 // Clear the task_ last. We don't want any other tasks to start up |
311 if (notify_paused_on_exit) { | 324 // until we are done with all messages and pause notifications. |
312 NotifyPauseOnExit(); | 325 ASSERT(oob_queue_->IsEmpty()); |
| 326 ASSERT(!ok || queue_->IsEmpty()); |
| 327 task_ = NULL; |
313 } | 328 } |
314 if (run_end_callback && end_callback_ != NULL) { | 329 if (run_end_callback && end_callback_ != NULL) { |
315 end_callback_(callback_data_); | 330 end_callback_(callback_data_); |
316 // The handler may have been deleted after this point. | 331 // The handler may have been deleted after this point. |
317 } | 332 } |
318 } | 333 } |
319 | 334 |
320 | 335 |
321 void MessageHandler::ClosePort(Dart_Port port) { | 336 void MessageHandler::ClosePort(Dart_Port port) { |
322 MonitorLocker ml(&monitor_); | 337 MonitorLocker ml(&monitor_); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 | 403 |
389 | 404 |
390 void MessageHandler::AcquireQueues(AcquiredQueues* acquired_queues) { | 405 void MessageHandler::AcquireQueues(AcquiredQueues* acquired_queues) { |
391 ASSERT(acquired_queues != NULL); | 406 ASSERT(acquired_queues != NULL); |
392 // No double dipping. | 407 // No double dipping. |
393 ASSERT(acquired_queues->handler_ == NULL); | 408 ASSERT(acquired_queues->handler_ == NULL); |
394 acquired_queues->Reset(this); | 409 acquired_queues->Reset(this); |
395 } | 410 } |
396 | 411 |
397 } // namespace dart | 412 } // namespace dart |
OLD | NEW |