Bug 786573: The XPCOM thread manager can't shut down the sensor thread, so use one of our other various thread types for the sensor thread. r=bent

--HG--
extra : rebase_source : 07b0fc9ccd710601948c1cd10098fa2b8c681932
This commit is contained in:
Chris Jones 2012-08-29 12:26:18 -03:00
Родитель dae032a5cb
Коммит a10a3c3145
1 изменённых файлов: 62 добавлений и 80 удалений

Просмотреть файл

@ -17,17 +17,20 @@
#include <pthread.h>
#include <stdio.h>
#include "base/basictypes.h"
#include "base/thread.h"
#include "Hal.h"
#include "HalSensor.h"
#include "hardware/sensors.h"
#include "mozilla/Util.h"
#include "SensorDevice.h"
#include "nsThreadUtils.h"
#undef LOG
#include <android/log.h>
using namespace mozilla::hal;
using namespace android;
using namespace mozilla::hal;
#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "GonkSensor" , ## args)
@ -169,80 +172,58 @@ public:
static int sActivatedSensors = 0;
static SensorStatus sSensorStatus[NUM_SENSOR_TYPE];
static nsCOMPtr<nsIThread> sSwitchThread;
static base::Thread* sSwitchThread;
class PollSensor {
public:
NS_INLINE_DECL_REFCOUNTING(PollSensor);
static nsCOMPtr<nsIRunnable> GetRunnable() {
if (!mRunnable)
mRunnable = NS_NewRunnableMethod(new PollSensor(), &PollSensor::Poll);
return mRunnable;
static void
PollSensorsOnce()
{
if (!sActivatedSensors) {
return;
}
SensorDevice &device = SensorDevice::getInstance();
const size_t numEventMax = 16;
sensors_event_t buffer[numEventMax];
int n = device.poll(buffer, numEventMax);
if (n < 0) {
LOG("Error polling for sensor data (err=%d)", n);
return;
}
for (int i = 0; i < n; ++i) {
NS_DispatchToMainThread(new SensorRunnable(buffer[i]));
}
if (sActivatedSensors) {
MessageLoop::current()->PostTask(FROM_HERE,
NewRunnableFunction(PollSensorsOnce));
}
}
static void
SwitchSensor(bool aActivate, sensor_t aSensor, pthread_t aThreadId)
{
int index = HardwareSensorToHalSensor(aSensor.type);
MOZ_ASSERT(sSensorStatus[index].count || aActivate);
SensorDevice& device = SensorDevice::getInstance();
device.activate((void*)aThreadId, aSensor.handle, aActivate);
device.setDelay((void*)aThreadId, aSensor.handle, DEFAULT_DEVICE_POLL_RATE);
if (aActivate) {
if (++sActivatedSensors == 1) {
MessageLoop::current()->PostTask(FROM_HERE,
NewRunnableFunction(PollSensorsOnce));
}
void Poll() {
if (!sActivatedSensors) {
return;
}
SensorDevice &device = SensorDevice::getInstance();
const size_t numEventMax = 16;
sensors_event_t buffer[numEventMax];
int n = device.poll(buffer, numEventMax);
if (n < 0) {
LOG("Error polling for sensor data (err=%d)", n);
return;
}
for (int i = 0; i < n; ++i) {
NS_DispatchToMainThread(new SensorRunnable(buffer[i]));
}
if (sActivatedSensors) {
sSwitchThread->Dispatch(GetRunnable(), NS_DISPATCH_NORMAL);
}
}
private:
static nsCOMPtr<nsIRunnable> mRunnable;
};
nsCOMPtr<nsIRunnable> PollSensor::mRunnable = NULL;
class SwitchSensor : public RefCounted<SwitchSensor> {
public:
SwitchSensor(bool aActivate, sensor_t aSensor, pthread_t aThreadId) :
mActivate(aActivate), mSensor(aSensor), mThreadId(aThreadId) { }
void Switch() {
int index = HardwareSensorToHalSensor(mSensor.type);
MOZ_ASSERT(sSensorStatus[index].count || mActivate);
SensorDevice& device = SensorDevice::getInstance();
device.activate((void*)mThreadId, mSensor.handle, mActivate);
device.setDelay((void*)mThreadId, mSensor.handle, DEFAULT_DEVICE_POLL_RATE);
if (mActivate) {
if (++sActivatedSensors == 1) {
sSwitchThread->Dispatch(PollSensor::GetRunnable(), NS_DISPATCH_NORMAL);
}
sSensorStatus[index].count++;
} else {
sSensorStatus[index].count--;
--sActivatedSensors;
}
}
protected:
SwitchSensor() { };
bool mActivate;
sensor_t mSensor;
pthread_t mThreadId;
};
sSensorStatus[index].count++;
} else {
sSensorStatus[index].count--;
--sActivatedSensors;
}
}
static void
SetSensorState(SensorType aSensor, bool activate)
@ -254,10 +235,10 @@ SetSensorState(SensorType aSensor, bool activate)
for (ssize_t i = 0; i < size; i++) {
if (sensors[i].type == type) {
// Post an event to the sensor thread
nsCOMPtr<nsIRunnable> event = NS_NewRunnableMethod(new SwitchSensor(activate, sensors[i], pthread_self()),
&SwitchSensor::Switch);
sSwitchThread->Dispatch(event, NS_DISPATCH_NORMAL);
sSwitchThread->message_loop()->PostTask(
FROM_HERE,
NewRunnableFunction(SwitchSensor,
activate, sensors[i], pthread_self()));
break;
}
}
@ -266,8 +247,9 @@ SetSensorState(SensorType aSensor, bool activate)
void
EnableSensorNotifications(SensorType aSensor)
{
if (sSwitchThread == nullptr) {
NS_NewThread(getter_AddRefs(sSwitchThread));
if (!sSwitchThread) {
sSwitchThread = new base::Thread("GonkSensors");
sSwitchThread->Start();
}
SetSensorState(aSensor, true);