Bug 743933: Update B2G Bluetooth to ICS API; r=echou
authorKyle Machulis <kyle@nonpolynomial.com>
Mon, 17 Sep 2012 23:11:00 -0700
changeset 107336 176b61afc41d8fc6fc5780648332a22fa57659f0
parent 107335 6b719da931a8a9a557a5b803662ff54ef8fffb15
child 107337 1f8b8d69d23ac65e36f3bb5af07059f05096635e
child 107365 e4757379b99a45135be2606b20bc14c6695f0495
push id14986
push userkmachulis@mozilla.com
push dateTue, 18 Sep 2012 06:11:03 +0000
treeherdermozilla-inbound@176b61afc41d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersechou
bugs743933
milestone18.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 743933: Update B2G Bluetooth to ICS API; r=echou
ipc/dbus/DBusThread.cpp
--- a/ipc/dbus/DBusThread.cpp
+++ b/ipc/dbus/DBusThread.cpp
@@ -70,16 +70,17 @@
 // and their dbus definitions
 
 // TODO Add Wakeup to this list once we've moved to ics
 
 enum {
   DBUS_EVENT_LOOP_EXIT = 1,
   DBUS_EVENT_LOOP_ADD = 2,
   DBUS_EVENT_LOOP_REMOVE = 3,
+  DBUS_EVENT_LOOP_WAKEUP = 4,
 } DBusEventTypes;
 
 static unsigned int UnixEventsToDBusFlags(short events)
 {
   return (events & DBUS_WATCH_READABLE ? POLLIN : 0) |
     (events & DBUS_WATCH_WRITABLE ? POLLOUT : 0) |
     (events & DBUS_WATCH_ERROR ? POLLERR : 0) |
     (events & DBUS_WATCH_HANGUP ? POLLHUP : 0);
@@ -153,18 +154,17 @@ AddWatch(DBusWatch *aWatch, void *aData)
     // to by our eventloop and remove this watch..  reading the add first
     // and then inspecting the recently deceased watch would be bad.
     char control = DBUS_EVENT_LOOP_ADD;
     if (write(dbt->mControlFdW.get(), &control, sizeof(char)) < 0) {
       LOG("Cannot write DBus add watch control data to socket!\n");
       return false;
     }
 
-    // TODO change this to dbus_watch_get_unix_fd once we move to ics
-    int fd = dbus_watch_get_fd(aWatch);
+    int fd = dbus_watch_get_unix_fd(aWatch);
     if (write(dbt->mControlFdW.get(), &fd, sizeof(int)) < 0) {
       LOG("Cannot write DBus add watch descriptor data to socket!\n");
       return false;
     }
 
     unsigned int flags = dbus_watch_get_flags(aWatch);
     if (write(dbt->mControlFdW.get(), &flags, sizeof(unsigned int)) < 0) {
       LOG("Cannot write DBus add watch flag data to socket!\n");
@@ -185,18 +185,17 @@ RemoveWatch(DBusWatch *aWatch, void *aDa
   DBusThread *dbt = (DBusThread *)aData;
 
   char control = DBUS_EVENT_LOOP_REMOVE;
   if (write(dbt->mControlFdW.get(), &control, sizeof(char)) < 0) {
     LOG("Cannot write DBus remove watch control data to socket!\n");
     return;
   }
 
-  // TODO change this to dbus_watch_get_unix_fd once we move to ics
-  int fd = dbus_watch_get_fd(aWatch);
+  int fd = dbus_watch_get_unix_fd(aWatch);
   if (write(dbt->mControlFdW.get(), &fd, sizeof(int)) < 0) {
     LOG("Cannot write DBus remove watch descriptor data to socket!\n");
     return;
   }
 
   unsigned int flags = dbus_watch_get_flags(aWatch);
   if (write(dbt->mControlFdW.get(), &flags, sizeof(unsigned int)) < 0) {
     LOG("Cannot write DBus remove watch flag data to socket!\n");
@@ -238,17 +237,18 @@ HandleWatchAdd(DBusThread* aDbt)
   p.fd = newFD;
   p.revents = 0;
   p.events = events;
   if (aDbt->mPollData.Contains(p, PollFdComparator())) return;
   aDbt->mPollData.AppendElement(p);
   aDbt->mWatchData.AppendElement(watch);
 }
 
-static void HandleWatchRemove(DBusThread* aDbt)
+static void
+HandleWatchRemove(DBusThread* aDbt)
 {
   int removeFD;
   unsigned int flags;
 
   if (read(aDbt->mControlFdR.get(), &removeFD, sizeof(int)) < 0) {
     LOG("Cannot read DBus watch remove descriptor data from socket!\n");
     return;
   }
@@ -271,16 +271,26 @@ static void HandleWatchRemove(DBusThread
   }
   aDbt->mPollData.RemoveElementAt(index);
 
   // DBusWatch pointers are maintained by DBus, so we won't leak by
   // removing.
   aDbt->mWatchData.RemoveElementAt(index);
 }
 
+static
+void DBusWakeup(void* aData)
+{
+  DBusThread *dbt = (DBusThread *)aData;
+  char control = DBUS_EVENT_LOOP_WAKEUP;
+  if (write(dbt->mControlFdW.get(), &control, sizeof(char)) < 0) {
+    NS_WARNING("Cannot write wakeup bit to DBus controller!");
+  }
+}
+
 // DBus Thread Implementation
 
 DBusThread::DBusThread()
 {
 }
 
 DBusThread::~DBusThread()
 {
@@ -307,17 +317,19 @@ DBusThread::SetUpEventLoop()
   }
 
   return true;
 }
 
 bool
 DBusThread::TearDownData()
 {
+#ifdef DEBUG
   LOG("Removing DBus Sockets\n");
+#endif
   if (mControlFdW.get()) {
     mControlFdW.dispose();
   }
   if (mControlFdR.get()) {
     mControlFdR.dispose();
   }
   mPollData.Clear();
 
@@ -327,42 +339,49 @@ DBusThread::TearDownData()
   return true;
 }
 
 void
 DBusThread::EventLoop()
 {
   dbus_connection_set_watch_functions(mConnection, AddWatch,
                                       RemoveWatch, ToggleWatch, this, NULL);
-
+  dbus_connection_set_wakeup_main_function(mConnection, DBusWakeup, this, NULL);
+#ifdef DEBUG
   LOG("DBus Event Loop Starting\n");
+#endif
   while (1) {
     poll(mPollData.Elements(), mPollData.Length(), -1);
 
     for (uint32_t i = 0; i < mPollData.Length(); i++) {
       if (!mPollData[i].revents) {
         continue;
       }
 
       if (mPollData[i].fd == mControlFdR.get()) {
         char data;
         while (recv(mControlFdR.get(), &data, sizeof(char), MSG_DONTWAIT)
                != -1) {
           switch (data) {
-            case DBUS_EVENT_LOOP_EXIT:
-              LOG("DBus Event Loop Exiting\n");
-              dbus_connection_set_watch_functions(mConnection,
-                                                  NULL, NULL, NULL, NULL, NULL);
-              return;
-            case DBUS_EVENT_LOOP_ADD:
-              HandleWatchAdd(this);
-              break;
-            case DBUS_EVENT_LOOP_REMOVE:
-              HandleWatchRemove(this);
-              break;
+          case DBUS_EVENT_LOOP_EXIT:
+#ifdef DEBUG
+            LOG("DBus Event Loop Exiting\n");
+#endif
+            dbus_connection_set_watch_functions(mConnection,
+                                                NULL, NULL, NULL, NULL, NULL);
+            return;
+          case DBUS_EVENT_LOOP_ADD:
+            HandleWatchAdd(this);
+            break;
+          case DBUS_EVENT_LOOP_REMOVE:
+            HandleWatchRemove(this);
+            break;
+          case DBUS_EVENT_LOOP_WAKEUP:
+            // noop
+            break;
           }
         }
       } else {
         short events = mPollData[i].revents;
         unsigned int flags = UnixEventsToDBusFlags(events);
         dbus_watch_handle(mWatchData[i], flags);
         mPollData[i].revents = 0;
         // Break at this point since we don't know if the operation
@@ -409,39 +428,45 @@ DBusThread::StartEventLoop()
   }
   if (NS_FAILED(NS_NewNamedThread("DBus Poll",
                                   getter_AddRefs(mThread),
                                   NS_NewNonOwningRunnableMethod(this,
                                                                 &DBusThread::EventLoop)))) {
     NS_WARNING("Cannot create DBus Thread!");
     return false;    
   }
+#ifdef DEBUG
   LOG("DBus Thread Starting\n");
+#endif
   return true;
 }
 
 bool
 DBusThread::StopEventLoop()
 {
   if (!mThread) {
     return true;
   }
   char data = DBUS_EVENT_LOOP_EXIT;
   ssize_t wret = write(mControlFdW.get(), &data, sizeof(char));
   if(wret < 0) {
     NS_ERROR("Cannot write exit flag to Dbus Thread!");
     return false;
   }
+#ifdef DEBUG
   LOG("DBus Thread Joining\n");
+#endif
   nsCOMPtr<nsIThread> tmpThread;
   mThread.swap(tmpThread);
   if(NS_FAILED(tmpThread->Shutdown())) {
     NS_WARNING("DBus thread shutdown failed!");
   }
+#ifdef DEBUG
   LOG("DBus Thread Joined\n");
+#endif
   TearDownData();
   return true;
 }
 
 // Startup/Shutdown utility functions
 
 bool
 StartDBus()