Bug 956841: Respect DBusWatch flags in I/O-thread watchers, r=qdot
authorThomas Zimmermann <tdz@users.sourceforge.net>
Wed, 15 Jan 2014 09:32:11 +0100
changeset 163495 559465ad3fd8b32535adec33ae5802d5b9116f9c
parent 163494 818236db09b4ac2f08ececd9adf410c6e628a3f1
child 163496 2b43a2aa29d9d172df389a8b716bb39c53c47276
push idunknown
push userunknown
push dateunknown
reviewersqdot
bugs956841
milestone29.0a1
Bug 956841: Respect DBusWatch flags in I/O-thread watchers, r=qdot Currently DBusWatcher registers read and write watchers on the I/O thread unconditionally. Thus for read-only DBusWatch structures, the write watcher on the I/O thread generates a significant amount of CPU overhead by constantly reporting success via calls to OnFileCanWriteWithoutBlocking. This patch changes DBusWatcher to respect the status flags of supplied DBusWatch structures. It will only poll for read or write status if the DBus watch has the corresponding flag set. DBus keeps track of these flags and calls the toggle callback when it needs to wait on a certain DBusWatch.
ipc/dbus/DBusThread.cpp
--- a/ipc/dbus/DBusThread.cpp
+++ b/ipc/dbus/DBusThread.cpp
@@ -117,28 +117,41 @@ DBusWatcher::GetConnection()
 void DBusWatcher::StartWatching()
 {
   MOZ_ASSERT(!NS_IsMainThread());
   MOZ_ASSERT(mWatch);
 
   int fd = dbus_watch_get_unix_fd(mWatch);
 
   MessageLoopForIO* ioLoop = MessageLoopForIO::current();
-  ioLoop->WatchFileDescriptor(fd, true, MessageLoopForIO::WATCH_READ,
-                              &mReadWatcher, this);
-  ioLoop->WatchFileDescriptor(fd, true, MessageLoopForIO::WATCH_WRITE,
-                              &mWriteWatcher, this);
+
+  unsigned int flags = dbus_watch_get_flags(mWatch);
+
+  if (flags & DBUS_WATCH_READABLE) {
+    ioLoop->WatchFileDescriptor(fd, true, MessageLoopForIO::WATCH_READ,
+                                &mReadWatcher, this);
+  }
+  if (flags & DBUS_WATCH_WRITABLE) {
+    ioLoop->WatchFileDescriptor(fd, true, MessageLoopForIO::WATCH_WRITE,
+                                &mWriteWatcher, this);
+  }
 }
 
 void DBusWatcher::StopWatching()
 {
   MOZ_ASSERT(!NS_IsMainThread());
 
-  mReadWatcher.StopWatchingFileDescriptor();
-  mWriteWatcher.StopWatchingFileDescriptor();
+  unsigned int flags = dbus_watch_get_flags(mWatch);
+
+  if (flags & DBUS_WATCH_READABLE) {
+    mReadWatcher.StopWatchingFileDescriptor();
+  }
+  if (flags & DBUS_WATCH_WRITABLE) {
+    mWriteWatcher.StopWatchingFileDescriptor();
+  }
 }
 
 // DBus utility functions, used as function pointers in DBus setup
 
 void
 DBusWatcher::FreeFunction(void* aData)
 {
   delete static_cast<DBusWatcher*>(aData);