Merge "Fix a race condition in sensormanager" into gingerbread

This commit is contained in:
Mathias Agopian
2010-11-15 13:14:59 -08:00
committed by Android (Google) Code Review
2 changed files with 8 additions and 1 deletions

View File

@ -464,6 +464,7 @@ void SensorService::SensorEventConnection::onFirstRef()
} }
bool SensorService::SensorEventConnection::addSensor(int32_t handle) { bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
Mutex::Autolock _l(mConnectionLock);
if (mSensorInfo.indexOfKey(handle) <= 0) { if (mSensorInfo.indexOfKey(handle) <= 0) {
SensorInfo info; SensorInfo info;
mSensorInfo.add(handle, info); mSensorInfo.add(handle, info);
@ -473,6 +474,7 @@ bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
} }
bool SensorService::SensorEventConnection::removeSensor(int32_t handle) { bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
Mutex::Autolock _l(mConnectionLock);
if (mSensorInfo.removeItem(handle) >= 0) { if (mSensorInfo.removeItem(handle) >= 0) {
return true; return true;
} }
@ -480,16 +482,19 @@ bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
} }
bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const { bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
Mutex::Autolock _l(mConnectionLock);
return mSensorInfo.indexOfKey(handle) >= 0; return mSensorInfo.indexOfKey(handle) >= 0;
} }
bool SensorService::SensorEventConnection::hasAnySensor() const { bool SensorService::SensorEventConnection::hasAnySensor() const {
Mutex::Autolock _l(mConnectionLock);
return mSensorInfo.size() ? true : false; return mSensorInfo.size() ? true : false;
} }
status_t SensorService::SensorEventConnection::setEventRateLocked( status_t SensorService::SensorEventConnection::setEventRateLocked(
int handle, nsecs_t ns) int handle, nsecs_t ns)
{ {
Mutex::Autolock _l(mConnectionLock);
ssize_t index = mSensorInfo.indexOfKey(handle); ssize_t index = mSensorInfo.indexOfKey(handle);
if (index >= 0) { if (index >= 0) {
SensorInfo& info = mSensorInfo.editValueFor(handle); SensorInfo& info = mSensorInfo.editValueFor(handle);
@ -506,6 +511,7 @@ status_t SensorService::SensorEventConnection::sendEvents(
// filter out events not for this connection // filter out events not for this connection
size_t count = 0; size_t count = 0;
if (scratch) { if (scratch) {
Mutex::Autolock _l(mConnectionLock);
size_t i=0; size_t i=0;
while (i<numEvents) { while (i<numEvents) {
const int32_t curr = buffer[i].sensor; const int32_t curr = buffer[i].sensor;

View File

@ -75,8 +75,9 @@ class SensorService :
sp<SensorService> const mService; sp<SensorService> const mService;
sp<SensorChannel> const mChannel; sp<SensorChannel> const mChannel;
mutable Mutex mConnectionLock;
// protected by SensorService::mLock // protected mConnectionLock
struct SensorInfo { struct SensorInfo {
SensorInfo() : ns(DEFAULT_EVENTS_PERIOD) { } SensorInfo() : ns(DEFAULT_EVENTS_PERIOD) { }
nsecs_t ns; nsecs_t ns;