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

Unified Diff: ipc/ipc_sync_message_filter.cc

Issue 2033243003: Use Mojo pipes to signal sync IPC events (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix gyp Created 4 years, 6 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ipc/ipc_sync_message_filter.h ('k') | ipc/mojo_event.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ipc/ipc_sync_message_filter.cc
diff --git a/ipc/ipc_sync_message_filter.cc b/ipc/ipc_sync_message_filter.cc
index e917655cd85912b15210818516433c04cbeefa98..5fe03a269d948f0986156b0366f59f42462660ca 100644
--- a/ipc/ipc_sync_message_filter.cc
+++ b/ipc/ipc_sync_message_filter.cc
@@ -7,20 +7,36 @@
#include "base/bind.h"
#include "base/location.h"
#include "base/logging.h"
+#include "base/memory/ptr_util.h"
+#include "base/memory/ref_counted.h"
+#include "base/message_loop/message_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread_task_runner_handle.h"
#include "ipc/ipc_channel.h"
#include "ipc/ipc_sync_message.h"
+#include "ipc/mojo_event.h"
+#include "mojo/public/cpp/bindings/sync_handle_registry.h"
namespace IPC {
+namespace {
+
+// A generic callback used when watching handles synchronously. Sets |*signal|
+// to true. Also sets |*error| to true in case of an error.
+void OnSyncHandleReady(bool* signal, bool* error, MojoResult result) {
+ *signal = true;
+ *error = result != MOJO_RESULT_OK;
+}
+
+} // namespace
+
bool SyncMessageFilter::Send(Message* message) {
if (!message->is_sync()) {
{
base::AutoLock auto_lock(lock_);
if (!io_task_runner_.get()) {
- pending_messages_.push_back(message);
+ pending_messages_.emplace_back(base::WrapUnique(message));
return true;
}
}
@@ -30,9 +46,7 @@ bool SyncMessageFilter::Send(Message* message) {
return true;
}
- base::WaitableEvent done_event(
- base::WaitableEvent::ResetPolicy::MANUAL,
- base::WaitableEvent::InitialState::NOT_SIGNALED);
+ MojoEvent done_event;
PendingSyncMsg pending_message(
SyncMessage::GetMessageId(*message),
static_cast<SyncMessage*>(message)->GetReplyDeserializer(),
@@ -53,15 +67,33 @@ bool SyncMessageFilter::Send(Message* message) {
FROM_HERE,
base::Bind(&SyncMessageFilter::SendOnIOThread, this, message));
} else {
- pending_messages_.push_back(message);
+ pending_messages_.emplace_back(base::WrapUnique(message));
}
}
- base::WaitableEvent* events[2] = { shutdown_event_, &done_event };
- if (base::WaitableEvent::WaitMany(events, 2) == 1) {
+ bool done = false;
+ bool shutdown = false;
+ bool error = false;
+ scoped_refptr<mojo::SyncHandleRegistry> registry =
+ mojo::SyncHandleRegistry::current();
+ registry->RegisterHandle(shutdown_mojo_event_.GetHandle(),
+ MOJO_HANDLE_SIGNAL_READABLE,
+ base::Bind(&OnSyncHandleReady, &shutdown, &error));
+ registry->RegisterHandle(done_event.GetHandle(),
+ MOJO_HANDLE_SIGNAL_READABLE,
+ base::Bind(&OnSyncHandleReady, &done, &error));
+
+ const bool* stop_flags[] = { &done, &shutdown };
+ bool result = registry->WatchAllHandles(stop_flags, 2);
+ DCHECK(result);
+ DCHECK(!error);
+
+ if (done) {
TRACE_EVENT_FLOW_END0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"),
"SyncMessageFilter::Send", &done_event);
}
+ registry->UnregisterHandle(shutdown_mojo_event_.GetHandle());
+ registry->UnregisterHandle(done_event.GetHandle());
{
base::AutoLock auto_lock(lock_);
@@ -73,26 +105,31 @@ bool SyncMessageFilter::Send(Message* message) {
}
void SyncMessageFilter::OnFilterAdded(Sender* sender) {
- std::vector<Message*> pending_messages;
+ std::vector<std::unique_ptr<Message>> pending_messages;
{
base::AutoLock auto_lock(lock_);
sender_ = sender;
io_task_runner_ = base::ThreadTaskRunnerHandle::Get();
- pending_messages_.release(&pending_messages);
+ shutdown_watcher_.StartWatching(
+ shutdown_event_,
+ base::Bind(&SyncMessageFilter::OnShutdownEventSignaled, this));
+ std::swap(pending_messages_, pending_messages);
}
- for (auto* msg : pending_messages)
- SendOnIOThread(msg);
+ for (auto& msg : pending_messages)
+ SendOnIOThread(msg.release());
}
void SyncMessageFilter::OnChannelError() {
base::AutoLock auto_lock(lock_);
sender_ = NULL;
+ shutdown_watcher_.StopWatching();
SignalAllEvents();
}
void SyncMessageFilter::OnChannelClosing() {
base::AutoLock auto_lock(lock_);
sender_ = NULL;
+ shutdown_watcher_.StopWatching();
SignalAllEvents();
}
@@ -167,4 +204,9 @@ void SyncMessageFilter::SignalAllEvents() {
}
}
+void SyncMessageFilter::OnShutdownEventSignaled(base::WaitableEvent* event) {
+ DCHECK_EQ(event, shutdown_event_);
+ shutdown_mojo_event_.Signal();
+}
+
} // namespace IPC
« no previous file with comments | « ipc/ipc_sync_message_filter.h ('k') | ipc/mojo_event.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698