Synchronous resource recover mechanism for ISensorEventConnection
Add synchronous destroy() function to recover resource used by
remote ISensorEventConnection object.
Bug: 63542033
Test: SensorDirectReportTest pass
Change-Id: If98782ee12c7b1a733eb15a2fd8d7c5dacde243b
diff --git a/libs/sensor/ISensorEventConnection.cpp b/libs/sensor/ISensorEventConnection.cpp
index 8a3a623..1cd8e01 100644
--- a/libs/sensor/ISensorEventConnection.cpp
+++ b/libs/sensor/ISensorEventConnection.cpp
@@ -36,7 +36,8 @@
ENABLE_DISABLE,
SET_EVENT_RATE,
FLUSH_SENSOR,
- CONFIGURE_CHANNEL
+ CONFIGURE_CHANNEL,
+ DESTROY,
};
class BpSensorEventConnection : public BpInterface<ISensorEventConnection>
@@ -96,11 +97,22 @@
remote()->transact(CONFIGURE_CHANNEL, data, &reply);
return reply.readInt32();
}
+
+ virtual void onLastStrongRef(const void* id) {
+ destroy();
+ BpInterface<ISensorEventConnection>::onLastStrongRef(id);
+ }
+
+protected:
+ virtual void destroy() {
+ Parcel data, reply;
+ remote()->transact(DESTROY, data, &reply);
+ }
};
// Out-of-line virtual method definition to trigger vtable emission in this
// translation unit (see clang warning -Wweak-vtables)
-BpSensorEventConnection::~BpSensorEventConnection() {}
+BpSensorEventConnection::~BpSensorEventConnection() { }
IMPLEMENT_META_INTERFACE(SensorEventConnection, "android.gui.SensorEventConnection");
@@ -150,6 +162,10 @@
reply->writeInt32(result);
return NO_ERROR;
}
+ case DESTROY: {
+ destroy();
+ return NO_ERROR;
+ }
}
return BBinder::onTransact(code, data, reply, flags);
diff --git a/libs/sensor/include/sensor/ISensorEventConnection.h b/libs/sensor/include/sensor/ISensorEventConnection.h
index 07cc7e8..b62e18c 100644
--- a/libs/sensor/include/sensor/ISensorEventConnection.h
+++ b/libs/sensor/include/sensor/ISensorEventConnection.h
@@ -42,6 +42,8 @@
virtual status_t setEventRate(int handle, nsecs_t ns) = 0;
virtual status_t flush() = 0;
virtual int32_t configureChannel(int32_t handle, int32_t rateLevel) = 0;
+protected:
+ virtual void destroy() = 0; // synchronously release resource hold by remote object
};
// ----------------------------------------------------------------------------
diff --git a/services/sensorservice/SensorDirectConnection.cpp b/services/sensorservice/SensorDirectConnection.cpp
index 870635b..538d728 100644
--- a/services/sensorservice/SensorDirectConnection.cpp
+++ b/services/sensorservice/SensorDirectConnection.cpp
@@ -27,12 +27,21 @@
const String16& opPackageName)
: mService(service), mUid(uid), mMem(*mem),
mHalChannelHandle(halChannelHandle),
- mOpPackageName(opPackageName) {
+ mOpPackageName(opPackageName), mDestroyed(false) {
ALOGD_IF(DEBUG_CONNECTIONS, "Created SensorDirectConnection");
}
SensorService::SensorDirectConnection::~SensorDirectConnection() {
ALOGD_IF(DEBUG_CONNECTIONS, "~SensorDirectConnection %p", this);
+ destroy();
+}
+
+void SensorService::SensorDirectConnection::destroy() {
+ Mutex::Autolock _l(mDestroyLock);
+ // destroy once only
+ if (mDestroyed) {
+ return;
+ }
stopAll();
mService->cleanupConnection(this);
@@ -40,6 +49,7 @@
native_handle_close(mMem.handle);
native_handle_delete(const_cast<struct native_handle*>(mMem.handle));
}
+ mDestroyed = true;
}
void SensorService::SensorDirectConnection::onFirstRef() {
diff --git a/services/sensorservice/SensorDirectConnection.h b/services/sensorservice/SensorDirectConnection.h
index 27458d4..5c398a8 100644
--- a/services/sensorservice/SensorDirectConnection.h
+++ b/services/sensorservice/SensorDirectConnection.h
@@ -47,7 +47,7 @@
// stop all active sensor report. if backupRecord is set to false,
// those report can be recovered by recoverAll
// called by SensorService when enter restricted mode
- void stopAll(bool clearRecord = false);
+ void stopAll(bool backupRecord = false);
// recover sensor reports previously stopped by stopAll(true)
// called by SensorService when return to NORMAL mode.
@@ -63,7 +63,7 @@
virtual status_t setEventRate(int handle, nsecs_t samplingPeriodNs);
virtual status_t flush();
virtual int32_t configureChannel(int handle, int rateLevel);
-
+ virtual void destroy();
private:
const sp<SensorService> mService;
const uid_t mUid;
@@ -74,6 +74,9 @@
mutable Mutex mConnectionLock;
std::unordered_map<int, int> mActivated;
std::unordered_map<int, int> mActivatedBackup;
+
+ mutable Mutex mDestroyLock;
+ bool mDestroyed;
};
} // namepsace android
diff --git a/services/sensorservice/SensorEventConnection.cpp b/services/sensorservice/SensorEventConnection.cpp
index bfe4c09..0a05dd1 100644
--- a/services/sensorservice/SensorEventConnection.cpp
+++ b/services/sensorservice/SensorEventConnection.cpp
@@ -32,7 +32,8 @@
const String16& opPackageName)
: mService(service), mUid(uid), mWakeLockRefCount(0), mHasLooperCallbacks(false),
mDead(false), mDataInjectionMode(isDataInjectionMode), mEventCache(NULL),
- mCacheSize(0), mMaxCacheSize(0), mPackageName(packageName), mOpPackageName(opPackageName) {
+ mCacheSize(0), mMaxCacheSize(0), mPackageName(packageName), mOpPackageName(opPackageName),
+ mDestroyed(false) {
mChannel = new BitTube(mService->mSocketBufferSize);
#if DEBUG_CONNECTIONS
mEventsReceived = mEventsSentFromCache = mEventsSent = 0;
@@ -42,10 +43,22 @@
SensorService::SensorEventConnection::~SensorEventConnection() {
ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this);
+ destroy();
+}
+
+void SensorService::SensorEventConnection::destroy() {
+ Mutex::Autolock _l(mDestroyLock);
+
+ // destroy once only
+ if (mDestroyed) {
+ return;
+ }
+
mService->cleanupConnection(this);
if (mEventCache != NULL) {
delete mEventCache;
}
+ mDestroyed = true;
}
void SensorService::SensorEventConnection::onFirstRef() {
diff --git a/services/sensorservice/SensorEventConnection.h b/services/sensorservice/SensorEventConnection.h
index c81e015..6f282cd 100644
--- a/services/sensorservice/SensorEventConnection.h
+++ b/services/sensorservice/SensorEventConnection.h
@@ -75,6 +75,7 @@
virtual status_t setEventRate(int handle, nsecs_t samplingPeriodNs);
virtual status_t flush();
virtual int32_t configureChannel(int handle, int rateLevel);
+ virtual void destroy();
// Count the number of flush complete events which are about to be dropped in the buffer.
// Increment mPendingFlushEventsToSend in mSensorInfo. These flush complete events will be sent
@@ -164,6 +165,8 @@
int mTotalAcksNeeded, mTotalAcksReceived;
#endif
+ mutable Mutex mDestroyLock;
+ bool mDestroyed;
};
} // namepsace android