Bug 968275: Remove task classes from RawDBusConnection, r=qdot
authorThomas Zimmermann <tdz@users.sourceforge.net>
Thu, 06 Feb 2014 17:28:44 +0100
changeset 167317 9307f298477dbd08abec0dbde8af12cf47cffb1a
parent 167316 c550c472ae2041b93c3513ed8eecf023fe8e8078
child 167318 709c82037b7e2ffd199d6205577cd9a05980b2fd
push id4902
push userryanvm@gmail.com
push dateThu, 06 Feb 2014 21:34:39 +0000
treeherderfx-team@373a24c89dd9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersqdot
bugs968275
milestone30.0a1
Bug 968275: Remove task classes from RawDBusConnection, r=qdot This patch removes the internal tasks from the implementation of RawDBusConnection. All calls to RawDBusConnection are invoked on the I/O thread, so the sender tasks only impose an overhead.
ipc/dbus/RawDBusConnection.cpp
ipc/dbus/RawDBusConnection.h
--- a/ipc/dbus/RawDBusConnection.cpp
+++ b/ipc/dbus/RawDBusConnection.cpp
@@ -1,186 +1,81 @@
 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
 /* vim: set ts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include <dbus/dbus.h>
 #include "base/message_loop.h"
-#include "mozilla/Monitor.h"
 #include "nsThreadUtils.h"
-#include "DBusThread.h"
-#include "DBusUtils.h"
 #include "RawDBusConnection.h"
 
 #ifdef CHROMIUM_LOG
 #undef CHROMIUM_LOG
 #endif
 
 #if defined(MOZ_WIDGET_GONK)
 #include <android/log.h>
 #define CHROMIUM_LOG(args...)  __android_log_print(ANDROID_LOG_INFO, "Gonk", args);
 #else
 #define CHROMIUM_LOG(args...)  printf(args);
 #endif
 
 /* TODO: Remove BlueZ constant */
 #define BLUEZ_DBUS_BASE_IFC "org.bluez"
 
-using namespace mozilla::ipc;
-
 //
 // Runnables
 //
 
 namespace mozilla {
 namespace ipc {
 
-class DBusConnectionSendTaskBase : public Task
+class Notification
 {
 public:
-  virtual ~DBusConnectionSendTaskBase()
+  Notification(DBusReplyCallback aCallback, void* aData)
+  : mCallback(aCallback),
+    mData(aData)
   { }
 
-protected:
-  DBusConnectionSendTaskBase(DBusConnection* aConnection,
-                             DBusMessage* aMessage)
-  : mConnection(aConnection),
-    mMessage(aMessage)
+  // Callback function for DBus replies. Only run it on I/O thread.
+  //
+  static void Handle(DBusPendingCall* aCall, void* aData)
   {
-    MOZ_ASSERT(mConnection);
-    MOZ_ASSERT(mMessage);
-  }
-
-  DBusConnection*   mConnection;
-  DBusMessageRefPtr mMessage;
-};
-
-//
-// Sends a message and returns the message's serial number to the
-// disaptching thread. Only run it in DBus thread.
-//
-class DBusConnectionSendTask : public DBusConnectionSendTaskBase
-{
-public:
-  DBusConnectionSendTask(DBusConnection* aConnection,
-                         DBusMessage* aMessage)
-  : DBusConnectionSendTaskBase(aConnection, aMessage)
-  { }
-
-  virtual ~DBusConnectionSendTask()
-  { }
-
-  void Run() MOZ_OVERRIDE
-  {
+    MOZ_ASSERT(!NS_IsMainThread());
     MOZ_ASSERT(MessageLoop::current());
 
-    dbus_bool_t success = dbus_connection_send(mConnection,
-                                               mMessage,
-                                               nullptr);
-    NS_ENSURE_TRUE_VOID(success == TRUE);
-  }
-};
-
-//
-// Sends a message and executes a callback function for the reply. Only
-// run it in DBus thread.
-//
-class DBusConnectionSendWithReplyTask : public DBusConnectionSendTaskBase
-{
-private:
-  class NotifyData
-  {
-  public:
-    NotifyData(DBusReplyCallback aCallback, void* aData)
-    : mCallback(aCallback),
-      mData(aData)
-    { }
+    nsAutoPtr<Notification> ntfn(static_cast<Notification*>(aData));
 
-    void RunNotifyCallback(DBusMessage* aMessage)
-    {
-      if (mCallback) {
-        mCallback(aMessage, mData);
-      }
-    }
-
-  private:
-    DBusReplyCallback mCallback;
-    void*             mData;
-  };
-
-  // Callback function for DBus replies. Only run it in DBus thread.
-  //
-  static void Notify(DBusPendingCall* aCall, void* aData)
-  {
-    MOZ_ASSERT(!NS_IsMainThread());
-
-    nsAutoPtr<NotifyData> data(static_cast<NotifyData*>(aData));
-
-    // The reply can be non-null if the timeout
-    // has been reached.
+    // The reply can be non-null if the timeout has been reached.
     DBusMessage* reply = dbus_pending_call_steal_reply(aCall);
 
     if (reply) {
-      data->RunNotifyCallback(reply);
+      ntfn->RunCallback(reply);
       dbus_message_unref(reply);
     }
 
     dbus_pending_call_cancel(aCall);
     dbus_pending_call_unref(aCall);
   }
 
-public:
-  DBusConnectionSendWithReplyTask(DBusConnection* aConnection,
-                                  DBusMessage* aMessage,
-                                  int aTimeout,
-                                  DBusReplyCallback aCallback,
-                                  void* aData)
-  : DBusConnectionSendTaskBase(aConnection, aMessage),
-    mCallback(aCallback),
-    mData(aData),
-    mTimeout(aTimeout)
-  { }
-
-  virtual ~DBusConnectionSendWithReplyTask()
-  { }
-
-  void Run() MOZ_OVERRIDE
+private:
+  void RunCallback(DBusMessage* aMessage)
   {
-    MOZ_ASSERT(MessageLoop::current());
+    if (mCallback) {
+      mCallback(aMessage, mData);
+    }
+  }
 
-    // Freed at end of Notify
-    nsAutoPtr<NotifyData> data(new NotifyData(mCallback, mData));
-    NS_ENSURE_TRUE_VOID(data);
-
-    DBusPendingCall* call;
-
-    dbus_bool_t success = dbus_connection_send_with_reply(mConnection,
-                                                          mMessage,
-                                                          &call,
-                                                          mTimeout);
-    NS_ENSURE_TRUE_VOID(success == TRUE);
-
-    success = dbus_pending_call_set_notify(call, Notify, data, nullptr);
-    NS_ENSURE_TRUE_VOID(success == TRUE);
-
-    data.forget();
-    dbus_message_unref(mMessage);
-  };
-
-private:
   DBusReplyCallback mCallback;
   void*             mData;
-  int               mTimeout;
 };
 
-}
-}
-
 //
 // RawDBusConnection
 //
 
 bool RawDBusConnection::sDBusIsInit(false);
 
 RawDBusConnection::RawDBusConnection()
 {
@@ -213,63 +108,70 @@ void RawDBusConnection::ScopedDBusConnec
   if (ptr) {
     dbus_connection_close(ptr);
     dbus_connection_unref(ptr);
   }
 }
 
 bool RawDBusConnection::Send(DBusMessage* aMessage)
 {
-  DBusConnectionSendTask* t =
-    new DBusConnectionSendTask(mConnection, aMessage);
-  MOZ_ASSERT(t);
+  MOZ_ASSERT(aMessage);
+  MOZ_ASSERT(!NS_IsMainThread());
+  MOZ_ASSERT(MessageLoop::current());
 
-  nsresult rv = DispatchToDBusThread(t);
-
-  if (NS_FAILED(rv)) {
-    if (aMessage) {
-      dbus_message_unref(aMessage);
-    }
+  dbus_bool_t success = dbus_connection_send(mConnection,
+                                             aMessage,
+                                             nullptr);
+  if (success != TRUE) {
+    dbus_message_unref(aMessage);
     return false;
   }
-
   return true;
 }
 
 bool RawDBusConnection::SendWithReply(DBusReplyCallback aCallback,
                                       void* aData,
                                       int aTimeout,
                                       DBusMessage* aMessage)
 {
-  DBusConnectionSendWithReplyTask* t =
-    new DBusConnectionSendWithReplyTask(mConnection, aMessage, aTimeout,
-                                        aCallback, aData);
-  MOZ_ASSERT(t);
+  MOZ_ASSERT(aMessage);
+  MOZ_ASSERT(!NS_IsMainThread());
+  MOZ_ASSERT(MessageLoop::current());
 
-  nsresult rv = DispatchToDBusThread(t);
+  nsAutoPtr<Notification> ntfn(new Notification(aCallback, aData));
+  NS_ENSURE_TRUE(ntfn, false);
+
+  DBusPendingCall* call;
 
-  if (NS_FAILED(rv)) {
-    if (aMessage) {
-      dbus_message_unref(aMessage);
-    }
-    return false;
-  }
+  dbus_bool_t success = dbus_connection_send_with_reply(mConnection,
+                                                        aMessage,
+                                                        &call,
+                                                        aTimeout);
+  NS_ENSURE_TRUE(success == TRUE, false);
+
+  success = dbus_pending_call_set_notify(call, Notification::Handle,
+                                         ntfn, nullptr);
+  NS_ENSURE_TRUE(success == TRUE, false);
+
+  ntfn.forget();
+  dbus_message_unref(aMessage);
 
   return true;
 }
 
 bool RawDBusConnection::SendWithReply(DBusReplyCallback aCallback,
                                       void* aData,
                                       int aTimeout,
                                       const char* aPath,
                                       const char* aIntf,
                                       const char* aFunc,
                                       int aFirstArgType,
                                       ...)
 {
+  MOZ_ASSERT(!NS_IsMainThread());
   va_list args;
 
   va_start(args, aFirstArgType);
   DBusMessage* msg = BuildDBusMessage(aPath, aIntf, aFunc,
                                       aFirstArgType, args);
   va_end(args);
 
   if (!msg) {
@@ -296,8 +198,11 @@ DBusMessage* RawDBusConnection::BuildDBu
   if (!dbus_message_append_args_valist(msg, aFirstArgType, aArgs)) {
     CHROMIUM_LOG("Could not append argument to method call!");
     dbus_message_unref(msg);
     return nullptr;
   }
 
   return msg;
 }
+
+}
+}
--- a/ipc/dbus/RawDBusConnection.h
+++ b/ipc/dbus/RawDBusConnection.h
@@ -2,24 +2,17 @@
 /* vim: set ts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_ipc_dbus_gonk_rawdbusconnection_h__
 #define mozilla_ipc_dbus_gonk_rawdbusconnection_h__
 
-#include <stdarg.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string>
-#include "nscore.h"
 #include "mozilla/Scoped.h"
-#include <mozilla/Mutex.h>
 
 struct DBusConnection;
 struct DBusError;
 struct DBusMessage;
 
 namespace mozilla {
 namespace ipc {