Bug 730363 - startup slowdown waiting for batteryinfo [r=cjones]
authorFabrice Desré <fabrice@mozilla.com>
Wed, 07 Mar 2012 08:27:09 -0800
changeset 88452 c2eec826a786ade86cdf11021d82fdd7eab91898
parent 88451 35aa165a23c925ac893ef9e3b23b6f9757cdaad1
child 88453 c8ff0f77a129bbf7f1656aa6067664fc5043b29e
push id22199
push userbmo@edmorley.co.uk
push dateThu, 08 Mar 2012 13:15:53 +0000
treeherdermozilla-central@09a8a48476e5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscjones
bugs730363
milestone13.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 730363 - startup slowdown waiting for batteryinfo [r=cjones]
hal/Makefile.in
hal/gonk/GonkSensor.cpp
--- a/hal/Makefile.in
+++ b/hal/Makefile.in
@@ -110,9 +110,9 @@ ifneq (gonk,$(MOZ_WIDGET_TOOLKIT)) #{
 CPPSRCS += FallbackLights.cpp FallbackTime.cpp
 endif #}
 
 include $(topsrcdir)/config/config.mk
 include $(topsrcdir)/ipc/chromium/chromium-config.mk
 include $(topsrcdir)/config/rules.mk
 
 CFLAGS          += $(MOZ_DBUS_GLIB_CFLAGS)
-CXXFLAGS        += $(MOZ_DBUS_GLIB_CFLAGS) -DHAVE_PTHREADS
+CXXFLAGS        += $(MOZ_DBUS_GLIB_CFLAGS)
--- a/hal/gonk/GonkSensor.cpp
+++ b/hal/gonk/GonkSensor.cpp
@@ -6,20 +6,23 @@
 #include <pthread.h>
 #include <stdio.h>
 
 #include "Hal.h"
 #include "HalSensor.h"
 #include "hardware/sensors.h"
 #include "mozilla/Util.h"
 #include "SensorDevice.h"
+#include "nsThreadUtils.h"
 
 using namespace mozilla::hal;
 using namespace android;
 
+namespace mozilla {
+
 static SensorType
 HardwareSensorToHalSensor(int type)
 {     
   switch(type) {
     case SENSOR_TYPE_ORIENTATION:
       return SENSOR_ORIENTATION;
     case SENSOR_TYPE_ACCELEROMETER:
       return SENSOR_ACCELERATION;
@@ -58,29 +61,29 @@ SensorseventToSensorData(const sensors_e
   aSensorData->values()[1] = data.data[1];
   aSensorData->values()[2] = data.data[2];
   return true;
 }
 
 static void
 onSensorChanged(const sensors_event_t& data, SensorData* aSensorData)
 {
-  mozilla::DebugOnly<bool> convertedData = SensorseventToSensorData(data, aSensorData);
+  DebugOnly<bool> convertedData = SensorseventToSensorData(data, aSensorData);
   MOZ_ASSERT(convertedData);
   NotifySensorChange(*aSensorData);
 }
 
-namespace mozilla {
 namespace hal_impl {
 
 static pthread_t sThread;
-static bool sInitialized = false;
-static bool sContinue = false;
-static int sActivatedSensors = 0;
+static bool sInitialized;
+static bool sContinue;
+static int sActivatedSensors;
 static SensorData sSensordata[NUM_SENSOR_TYPE];
+static nsCOMPtr<nsIThread> sSwitchThread;
 
 static void*
 UpdateSensorData(void* /*unused*/)
 {
   SensorDevice &device = SensorDevice::getInstance();
   const size_t numEventMax = 16;
   sensors_event_t buffer[numEventMax];
   int count = 0;
@@ -95,52 +98,76 @@ UpdateSensorData(void* /*unused*/)
       onSensorChanged(buffer[i], &sSensordata[HardwareSensorToHalSensor(buffer[i].type)]);
     }
   }
 
   return NULL;
 }
 
 static void 
-InitialResources()
+InitializeResources()
 {
   pthread_create(&sThread, NULL, &UpdateSensorData, NULL);
+  NS_NewThread(getter_AddRefs(sSwitchThread));
   sInitialized = true;
   sContinue = true;
 }
 
 static void 
 ReleaseResources()
 {
   sContinue = false;
   pthread_join(sThread, NULL);
+  sSwitchThread->Shutdown();
   sInitialized = false;
 }
 
+// This class is used as a runnable on the sSwitchThread
+class SensorInfo {
+  public:
+    NS_INLINE_DECL_REFCOUNTING(SensorInfo)
+
+    SensorInfo(bool aActivate, sensor_t aSensor, pthread_t aThreadId) :
+               activate(aActivate), sensor(aSensor), threadId(aThreadId) { }
+
+    void Switch() {
+     SensorDevice& device = SensorDevice::getInstance();
+     device.activate((void*)threadId, sensor.handle, activate);
+    }
+
+  protected:
+    SensorInfo() { };
+    bool      activate;
+    sensor_t  sensor;
+    pthread_t threadId;
+};
+
 static void
 SensorSwitch(SensorType aSensor, bool activate)
 {
   int type = HalSensorToHardwareSensor(aSensor);
   const sensor_t* sensors = NULL;
   SensorDevice& device = SensorDevice::getInstance();
   size_t size = device.getSensorList(&sensors);
-
-  for (size_t i=0; i<size; i++) {
+  for (size_t i = 0; i < size; i++) {
     if (sensors[i].type == type) {
-      device.activate((void*)pthread_self(), sensors[i].handle, activate);
+      // Post an event to the activation thread
+      nsCOMPtr<nsIRunnable> event = NS_NewRunnableMethod(new SensorInfo(activate, sensors[i], pthread_self()),
+                                                         &SensorInfo::Switch);
+      sSwitchThread->Dispatch(event, NS_DISPATCH_NORMAL);
       break;
     }
   }
 }
 
 void
 EnableSensorNotifications(SensorType aSensor) 
 {
   if (!sInitialized) {
-    InitialResources();
+    InitializeResources();
   }
   
   SensorSwitch(aSensor, true);
   sActivatedSensors++;
 }
 
 void
 DisableSensorNotifications(SensorType aSensor)