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,19 +172,11 @@ 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;
}
void Poll() {
static void
PollSensorsOnce()
{
if (!sActivatedSensors) {
return;
}
@ -201,33 +196,27 @@ class PollSensor {
}
if (sActivatedSensors) {
sSwitchThread->Dispatch(GetRunnable(), NS_DISPATCH_NORMAL);
MessageLoop::current()->PostTask(FROM_HERE,
NewRunnableFunction(PollSensorsOnce));
}
}
private:
static nsCOMPtr<nsIRunnable> mRunnable;
};
nsCOMPtr<nsIRunnable> PollSensor::mRunnable = NULL;
static void
SwitchSensor(bool aActivate, sensor_t aSensor, pthread_t aThreadId)
{
int index = HardwareSensorToHalSensor(aSensor.type);
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);
MOZ_ASSERT(sSensorStatus[index].count || aActivate);
SensorDevice& device = SensorDevice::getInstance();
device.activate((void*)mThreadId, mSensor.handle, mActivate);
device.setDelay((void*)mThreadId, mSensor.handle, DEFAULT_DEVICE_POLL_RATE);
device.activate((void*)aThreadId, aSensor.handle, aActivate);
device.setDelay((void*)aThreadId, aSensor.handle, DEFAULT_DEVICE_POLL_RATE);
if (mActivate) {
if (aActivate) {
if (++sActivatedSensors == 1) {
sSwitchThread->Dispatch(PollSensor::GetRunnable(), NS_DISPATCH_NORMAL);
MessageLoop::current()->PostTask(FROM_HERE,
NewRunnableFunction(PollSensorsOnce));
}
sSensorStatus[index].count++;
} else {
@ -236,14 +225,6 @@ class SwitchSensor : public RefCounted<SwitchSensor> {
}
}
protected:
SwitchSensor() { };
bool mActivate;
sensor_t mSensor;
pthread_t mThreadId;
};
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);