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

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, this));
brettw 2013/02/01 18:19:25 Does this have the same leak? It seems like any ti
dmichael (off chromium) 2013/02/01 18:26:28 Oh yeah, I think so. In this case, since the watch
teravest 2013/02/01 18:30:47 Done.
357 Context::OnChannelOpened(); 359 Context::OnChannelOpened();
358 } 360 }
359 361
360 void SyncChannel::SyncContext::OnChannelClosed() { 362 void SyncChannel::SyncContext::OnChannelClosed() {
361 CancelPendingSends(); 363 CancelPendingSends();
362 shutdown_watcher_.StopWatching(); 364 shutdown_watcher_.StopWatching();
363 Context::OnChannelClosed(); 365 Context::OnChannelClosed();
364 } 366 }
365 367
366 void SyncChannel::SyncContext::OnSendTimeout(int message_id) { 368 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. 391 // 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. 392 // Cancel pending Send calls, which will end up setting the send done event.
391 CancelPendingSends(); 393 CancelPendingSends();
392 } else { 394 } else {
393 // We got the reply, timed out or the process shutdown. 395 // We got the reply, timed out or the process shutdown.
394 DCHECK_EQ(GetSendDoneEvent(), event); 396 DCHECK_EQ(GetSendDoneEvent(), event);
395 MessageLoop::current()->QuitNow(); 397 MessageLoop::current()->QuitNow();
396 } 398 }
397 } 399 }
398 400
401 base::WaitableEventWatcher::EventCallback
402 SyncChannel::SyncContext::MakeWaitableEventCallback() {
403 return base::Bind(&SyncChannel::SyncContext::OnWaitableEventSignaled, this);
404 }
399 405
400 SyncChannel::SyncChannel( 406 SyncChannel::SyncChannel(
401 const IPC::ChannelHandle& channel_handle, 407 const IPC::ChannelHandle& channel_handle,
402 Channel::Mode mode, 408 Channel::Mode mode,
403 Listener* listener, 409 Listener* listener,
404 base::SingleThreadTaskRunner* ipc_task_runner, 410 base::SingleThreadTaskRunner* ipc_task_runner,
405 bool create_pipe_now, 411 bool create_pipe_now,
406 WaitableEvent* shutdown_event) 412 WaitableEvent* shutdown_event)
407 : ChannelProxy(new SyncContext(listener, ipc_task_runner, shutdown_event)), 413 : ChannelProxy(new SyncContext(listener, ipc_task_runner, shutdown_event)),
408 sync_messages_with_no_timeout_allowed_(true) { 414 sync_messages_with_no_timeout_allowed_(true) {
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
510 516
511 void SyncChannel::WaitForReplyWithNestedMessageLoop(SyncContext* context) { 517 void SyncChannel::WaitForReplyWithNestedMessageLoop(SyncContext* context) {
512 base::WaitableEventWatcher send_done_watcher; 518 base::WaitableEventWatcher send_done_watcher;
513 519
514 ReceivedSyncMsgQueue* sync_msg_queue = context->received_sync_msgs(); 520 ReceivedSyncMsgQueue* sync_msg_queue = context->received_sync_msgs();
515 DCHECK(sync_msg_queue != NULL); 521 DCHECK(sync_msg_queue != NULL);
516 522
517 base::WaitableEventWatcher* old_send_done_event_watcher = 523 base::WaitableEventWatcher* old_send_done_event_watcher =
518 sync_msg_queue->top_send_done_watcher(); 524 sync_msg_queue->top_send_done_watcher();
519 525
520 base::WaitableEventWatcher::Delegate* old_delegate = NULL; 526 base::WaitableEventWatcher::EventCallback old_callback;
521 base::WaitableEvent* old_event = NULL; 527 base::WaitableEvent* old_event = NULL;
522 528
523 // Maintain a local global stack of send done delegates to ensure that 529 // Maintain a local global stack of send done delegates to ensure that
524 // nested sync calls complete in the correct sequence, i.e. the 530 // nested sync calls complete in the correct sequence, i.e. the
525 // outermost call completes first, etc. 531 // outermost call completes first, etc.
526 if (old_send_done_event_watcher) { 532 if (old_send_done_event_watcher) {
527 old_delegate = old_send_done_event_watcher->delegate(); 533 old_callback = old_send_done_event_watcher->callback();
528 old_event = old_send_done_event_watcher->GetWatchedEvent(); 534 old_event = old_send_done_event_watcher->GetWatchedEvent();
529 old_send_done_event_watcher->StopWatching(); 535 old_send_done_event_watcher->StopWatching();
530 } 536 }
531 537
532 sync_msg_queue->set_top_send_done_watcher(&send_done_watcher); 538 sync_msg_queue->set_top_send_done_watcher(&send_done_watcher);
533 539
534 send_done_watcher.StartWatching(context->GetSendDoneEvent(), context); 540 send_done_watcher.StartWatching(context->GetSendDoneEvent(),
541 context->MakeWaitableEventCallback());
535 542
536 { 543 {
537 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); 544 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current());
538 MessageLoop::current()->Run(); 545 MessageLoop::current()->Run();
539 } 546 }
540 547
541 sync_msg_queue->set_top_send_done_watcher(old_send_done_event_watcher); 548 sync_msg_queue->set_top_send_done_watcher(old_send_done_event_watcher);
542 if (old_send_done_event_watcher && old_event) { 549 if (old_send_done_event_watcher && old_event) {
543 old_send_done_event_watcher->StartWatching(old_event, old_delegate); 550 old_send_done_event_watcher->StartWatching(old_event, old_callback);
544 } 551 }
545 } 552 }
546 553
547 void SyncChannel::OnWaitableEventSignaled(WaitableEvent* event) { 554 void SyncChannel::OnWaitableEventSignaled(WaitableEvent* event) {
548 DCHECK(event == sync_context()->GetDispatchEvent()); 555 DCHECK(event == sync_context()->GetDispatchEvent());
549 // The call to DispatchMessages might delete this object, so reregister 556 // The call to DispatchMessages might delete this object, so reregister
550 // the object watcher first. 557 // the object watcher first.
551 event->Reset(); 558 event->Reset();
552 dispatch_watcher_.StartWatching(event, this); 559 dispatch_watcher_.StartWatching(event, dispatch_watcher_callback_);
553 sync_context()->DispatchMessages(); 560 sync_context()->DispatchMessages();
554 } 561 }
555 562
556 void SyncChannel::StartWatching() { 563 void SyncChannel::StartWatching() {
557 // Ideally we only want to watch this object when running a nested message 564 // 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 565 // 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 566 // 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 567 // 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 568 // manual reset since the object watcher might otherwise reset the event
562 // when we're doing a WaitMany. 569 // when we're doing a WaitMany.
563 dispatch_watcher_.StartWatching(sync_context()->GetDispatchEvent(), this); 570 dispatch_watcher_callback_ =
571 base::Bind(&SyncChannel::OnWaitableEventSignaled,
572 base::Unretained(this));
573 dispatch_watcher_.StartWatching(sync_context()->GetDispatchEvent(),
574 dispatch_watcher_callback_);
564 } 575 }
565 576
566 } // namespace IPC 577 } // 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