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

Side by Side Diff: ipc/ipc_sync_channel.cc

Issue 12094106: Refactor: Simplify WaitableEventWatcher. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 7 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
« no previous file with comments | « ipc/ipc_sync_channel.h ('k') | no next file » | 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ipc/ipc_sync_channel.h" 5 #include "ipc/ipc_sync_channel.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/debug/trace_event.h" 8 #include "base/debug/trace_event.h"
9 #include "base/lazy_instance.h" 9 #include "base/lazy_instance.h"
10 #include "base/location.h" 10 #include "base/location.h"
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 return Context::OnMessageReceivedNoFilter(msg); 346 return Context::OnMessageReceivedNoFilter(msg);
347 } 347 }
348 348
349 void SyncChannel::SyncContext::OnChannelError() { 349 void SyncChannel::SyncContext::OnChannelError() {
350 CancelPendingSends(); 350 CancelPendingSends();
351 shutdown_watcher_.StopWatching(); 351 shutdown_watcher_.StopWatching();
352 Context::OnChannelError(); 352 Context::OnChannelError();
353 } 353 }
354 354
355 void SyncChannel::SyncContext::OnChannelOpened() { 355 void SyncChannel::SyncContext::OnChannelOpened() {
356 shutdown_watcher_.StartWatching(shutdown_event_, this); 356 shutdown_watcher_.StartWatching(
357 shutdown_event_,
358 base::Bind(&SyncChannel::SyncContext::OnWaitableEventSignaled,
359 base::Unretained(this)));
357 Context::OnChannelOpened(); 360 Context::OnChannelOpened();
358 } 361 }
359 362
360 void SyncChannel::SyncContext::OnChannelClosed() { 363 void SyncChannel::SyncContext::OnChannelClosed() {
361 CancelPendingSends(); 364 CancelPendingSends();
362 shutdown_watcher_.StopWatching(); 365 shutdown_watcher_.StopWatching();
363 Context::OnChannelClosed(); 366 Context::OnChannelClosed();
364 } 367 }
365 368
366 void SyncChannel::SyncContext::OnSendTimeout(int message_id) { 369 void SyncChannel::SyncContext::OnSendTimeout(int message_id) {
(...skipping 22 matching lines...) Expand all
389 // Process shut down before we can get a reply to a synchronous message. 392 // Process shut down before we can get a reply to a synchronous message.
390 // Cancel pending Send calls, which will end up setting the send done event. 393 // Cancel pending Send calls, which will end up setting the send done event.
391 CancelPendingSends(); 394 CancelPendingSends();
392 } else { 395 } else {
393 // We got the reply, timed out or the process shutdown. 396 // We got the reply, timed out or the process shutdown.
394 DCHECK_EQ(GetSendDoneEvent(), event); 397 DCHECK_EQ(GetSendDoneEvent(), event);
395 MessageLoop::current()->QuitNow(); 398 MessageLoop::current()->QuitNow();
396 } 399 }
397 } 400 }
398 401
402 base::WaitableEventWatcher::EventCallback
403 SyncChannel::SyncContext::MakeWaitableEventCallback() {
404 return base::Bind(&SyncChannel::SyncContext::OnWaitableEventSignaled, this);
405 }
399 406
400 SyncChannel::SyncChannel( 407 SyncChannel::SyncChannel(
401 const IPC::ChannelHandle& channel_handle, 408 const IPC::ChannelHandle& channel_handle,
402 Channel::Mode mode, 409 Channel::Mode mode,
403 Listener* listener, 410 Listener* listener,
404 base::SingleThreadTaskRunner* ipc_task_runner, 411 base::SingleThreadTaskRunner* ipc_task_runner,
405 bool create_pipe_now, 412 bool create_pipe_now,
406 WaitableEvent* shutdown_event) 413 WaitableEvent* shutdown_event)
407 : ChannelProxy(new SyncContext(listener, ipc_task_runner, shutdown_event)), 414 : ChannelProxy(new SyncContext(listener, ipc_task_runner, shutdown_event)),
408 sync_messages_with_no_timeout_allowed_(true) { 415 sync_messages_with_no_timeout_allowed_(true) {
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
510 517
511 void SyncChannel::WaitForReplyWithNestedMessageLoop(SyncContext* context) { 518 void SyncChannel::WaitForReplyWithNestedMessageLoop(SyncContext* context) {
512 base::WaitableEventWatcher send_done_watcher; 519 base::WaitableEventWatcher send_done_watcher;
513 520
514 ReceivedSyncMsgQueue* sync_msg_queue = context->received_sync_msgs(); 521 ReceivedSyncMsgQueue* sync_msg_queue = context->received_sync_msgs();
515 DCHECK(sync_msg_queue != NULL); 522 DCHECK(sync_msg_queue != NULL);
516 523
517 base::WaitableEventWatcher* old_send_done_event_watcher = 524 base::WaitableEventWatcher* old_send_done_event_watcher =
518 sync_msg_queue->top_send_done_watcher(); 525 sync_msg_queue->top_send_done_watcher();
519 526
520 base::WaitableEventWatcher::Delegate* old_delegate = NULL; 527 base::WaitableEventWatcher::EventCallback old_callback;
521 base::WaitableEvent* old_event = NULL; 528 base::WaitableEvent* old_event = NULL;
522 529
523 // Maintain a local global stack of send done delegates to ensure that 530 // Maintain a local global stack of send done delegates to ensure that
524 // nested sync calls complete in the correct sequence, i.e. the 531 // nested sync calls complete in the correct sequence, i.e. the
525 // outermost call completes first, etc. 532 // outermost call completes first, etc.
526 if (old_send_done_event_watcher) { 533 if (old_send_done_event_watcher) {
527 old_delegate = old_send_done_event_watcher->delegate(); 534 old_callback = old_send_done_event_watcher->callback();
528 old_event = old_send_done_event_watcher->GetWatchedEvent(); 535 old_event = old_send_done_event_watcher->GetWatchedEvent();
529 old_send_done_event_watcher->StopWatching(); 536 old_send_done_event_watcher->StopWatching();
530 } 537 }
531 538
532 sync_msg_queue->set_top_send_done_watcher(&send_done_watcher); 539 sync_msg_queue->set_top_send_done_watcher(&send_done_watcher);
533 540
534 send_done_watcher.StartWatching(context->GetSendDoneEvent(), context); 541 send_done_watcher.StartWatching(context->GetSendDoneEvent(),
542 context->MakeWaitableEventCallback());
535 543
536 { 544 {
537 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); 545 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current());
538 MessageLoop::current()->Run(); 546 MessageLoop::current()->Run();
539 } 547 }
540 548
541 sync_msg_queue->set_top_send_done_watcher(old_send_done_event_watcher); 549 sync_msg_queue->set_top_send_done_watcher(old_send_done_event_watcher);
542 if (old_send_done_event_watcher && old_event) { 550 if (old_send_done_event_watcher && old_event) {
543 old_send_done_event_watcher->StartWatching(old_event, old_delegate); 551 old_send_done_event_watcher->StartWatching(old_event, old_callback);
544 } 552 }
545 } 553 }
546 554
547 void SyncChannel::OnWaitableEventSignaled(WaitableEvent* event) { 555 void SyncChannel::OnWaitableEventSignaled(WaitableEvent* event) {
548 DCHECK(event == sync_context()->GetDispatchEvent()); 556 DCHECK(event == sync_context()->GetDispatchEvent());
549 // The call to DispatchMessages might delete this object, so reregister 557 // The call to DispatchMessages might delete this object, so reregister
550 // the object watcher first. 558 // the object watcher first.
551 event->Reset(); 559 event->Reset();
552 dispatch_watcher_.StartWatching(event, this); 560 dispatch_watcher_.StartWatching(event, dispatch_watcher_callback_);
553 sync_context()->DispatchMessages(); 561 sync_context()->DispatchMessages();
554 } 562 }
555 563
556 void SyncChannel::StartWatching() { 564 void SyncChannel::StartWatching() {
557 // Ideally we only want to watch this object when running a nested message 565 // Ideally we only want to watch this object when running a nested message
558 // loop. However, we don't know when it exits if there's another nested 566 // loop. However, we don't know when it exits if there's another nested
559 // message loop running under it or not, so we wouldn't know whether to 567 // message loop running under it or not, so we wouldn't know whether to
560 // stop or keep watching. So we always watch it, and create the event as 568 // stop or keep watching. So we always watch it, and create the event as
561 // manual reset since the object watcher might otherwise reset the event 569 // manual reset since the object watcher might otherwise reset the event
562 // when we're doing a WaitMany. 570 // when we're doing a WaitMany.
563 dispatch_watcher_.StartWatching(sync_context()->GetDispatchEvent(), this); 571 dispatch_watcher_callback_ =
572 base::Bind(&SyncChannel::OnWaitableEventSignaled,
573 base::Unretained(this));
574 dispatch_watcher_.StartWatching(sync_context()->GetDispatchEvent(),
575 dispatch_watcher_callback_);
564 } 576 }
565 577
566 } // namespace IPC 578 } // namespace IPC
OLDNEW
« no previous file with comments | « ipc/ipc_sync_channel.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698