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/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