From 8cbefd7af4801686c27d5802cfd087b2812110b6 Mon Sep 17 00:00:00 2001 From: Peng Xu Date: Mon, 10 Jul 2017 16:41:08 -0700 Subject: [PATCH] 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 --- libs/sensor/ISensorEventConnection.cpp | 20 ++++++++++++++++++-- libs/sensor/include/sensor/ISensorEventConnection.h | 2 ++ services/sensorservice/SensorDirectConnection.cpp | 12 +++++++++++- services/sensorservice/SensorDirectConnection.h | 7 +++++-- services/sensorservice/SensorEventConnection.cpp | 15 ++++++++++++++- services/sensorservice/SensorEventConnection.h | 3 +++ 6 files changed, 53 insertions(+), 6 deletions(-) diff --git a/libs/sensor/ISensorEventConnection.cpp b/libs/sensor/ISensorEventConnection.cpp index 8a3a623983..1cd8e01643 100644 --- a/libs/sensor/ISensorEventConnection.cpp +++ b/libs/sensor/ISensorEventConnection.cpp @@ -36,7 +36,8 @@ enum { ENABLE_DISABLE, SET_EVENT_RATE, FLUSH_SENSOR, - CONFIGURE_CHANNEL + CONFIGURE_CHANNEL, + DESTROY, }; class BpSensorEventConnection : public BpInterface @@ -96,11 +97,22 @@ public: remote()->transact(CONFIGURE_CHANNEL, data, &reply); return reply.readInt32(); } + + virtual void onLastStrongRef(const void* id) { + destroy(); + BpInterface::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 @@ status_t BnSensorEventConnection::onTransact( 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 07cc7e84ad..b62e18c63c 100644 --- a/libs/sensor/include/sensor/ISensorEventConnection.h +++ b/libs/sensor/include/sensor/ISensorEventConnection.h @@ -42,6 +42,8 @@ public: 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 870635bf05..538d72822e 100644 --- a/services/sensorservice/SensorDirectConnection.cpp +++ b/services/sensorservice/SensorDirectConnection.cpp @@ -27,12 +27,21 @@ SensorService::SensorDirectConnection::SensorDirectConnection(const spcleanupConnection(this); @@ -40,6 +49,7 @@ SensorService::SensorDirectConnection::~SensorDirectConnection() { native_handle_close(mMem.handle); native_handle_delete(const_cast(mMem.handle)); } + mDestroyed = true; } void SensorService::SensorDirectConnection::onFirstRef() { diff --git a/services/sensorservice/SensorDirectConnection.h b/services/sensorservice/SensorDirectConnection.h index 27458d4a2f..5c398a8caa 100644 --- a/services/sensorservice/SensorDirectConnection.h +++ b/services/sensorservice/SensorDirectConnection.h @@ -47,7 +47,7 @@ public: // 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 @@ protected: 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 mService; const uid_t mUid; @@ -74,6 +74,9 @@ private: mutable Mutex mConnectionLock; std::unordered_map mActivated; std::unordered_map mActivatedBackup; + + mutable Mutex mDestroyLock; + bool mDestroyed; }; } // namepsace android diff --git a/services/sensorservice/SensorEventConnection.cpp b/services/sensorservice/SensorEventConnection.cpp index bfe4c09248..0a05dd1b18 100644 --- a/services/sensorservice/SensorEventConnection.cpp +++ b/services/sensorservice/SensorEventConnection.cpp @@ -32,7 +32,8 @@ SensorService::SensorEventConnection::SensorEventConnection( 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( 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 c81e015dbe..6f282cdc60 100644 --- a/services/sensorservice/SensorEventConnection.h +++ b/services/sensorservice/SensorEventConnection.h @@ -75,6 +75,7 @@ private: 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 @@ private: int mTotalAcksNeeded, mTotalAcksReceived; #endif + mutable Mutex mDestroyLock; + bool mDestroyed; }; } // namepsace android -- 2.11.0