Implement IBufferClient::close on BufferClient
The close() function will mark a BufferClient as closed (set mClosed =
true), remove it from service's mClientList, and remove all of its token
in service's mTokenMap.
Add test cases to make sure it works in all situations.
Test: BufferHubBuffer_test (passed)
Bug: 119623209
Change-Id: Ic1d17ced97b67ef5432c9d341469d8e6105e2717
diff --git a/services/bufferhub/BufferClient.cpp b/services/bufferhub/BufferClient.cpp
index 37fd75f..7459517 100644
--- a/services/bufferhub/BufferClient.cpp
+++ b/services/bufferhub/BufferClient.cpp
@@ -39,7 +39,29 @@
return new BufferClient(service, node);
}
+BufferClient::~BufferClient() {
+ close();
+}
+
+Return<BufferHubStatus> BufferClient::close() {
+ std::lock_guard<std::mutex> lock(mClosedMutex);
+ if (mClosed) {
+ return BufferHubStatus::CLIENT_CLOSED;
+ }
+
+ getService()->onClientClosed(this);
+ mBufferNode.reset();
+ mClosed = true;
+ return BufferHubStatus::NO_ERROR;
+}
+
Return<void> BufferClient::duplicate(duplicate_cb _hidl_cb) {
+ std::lock_guard<std::mutex> lock(mClosedMutex);
+ if (mClosed) {
+ _hidl_cb(/*token=*/hidl_handle(), /*status=*/BufferHubStatus::CLIENT_CLOSED);
+ return Void();
+ }
+
if (!mBufferNode) {
// Should never happen
ALOGE("%s: node is missing.", __FUNCTION__);
@@ -47,15 +69,19 @@
return Void();
}
+ const hidl_handle token = getService()->registerToken(this);
+ _hidl_cb(/*token=*/token, /*status=*/BufferHubStatus::NO_ERROR);
+ return Void();
+}
+
+sp<BufferHubService> BufferClient::getService() {
sp<BufferHubService> service = mService.promote();
if (service == nullptr) {
// Should never happen. Kill the process.
LOG_FATAL("%s: service died.", __FUNCTION__);
}
- const hidl_handle token = service->registerToken(this);
- _hidl_cb(/*token=*/token, /*status=*/BufferHubStatus::NO_ERROR);
- return Void();
+ return service;
}
} // namespace implementation
diff --git a/services/bufferhub/BufferHubService.cpp b/services/bufferhub/BufferHubService.cpp
index 6f97f0d..409f2ea 100644
--- a/services/bufferhub/BufferHubService.cpp
+++ b/services/bufferhub/BufferHubService.cpp
@@ -111,6 +111,30 @@
return returnToken;
}
+void BufferHubService::onClientClosed(const BufferClient* client) {
+ removeTokenByClient(client);
+
+ std::lock_guard<std::mutex> lock(mClientListMutex);
+ auto iter = std::find(mClientList.begin(), mClientList.end(), client);
+ if (iter != mClientList.end()) {
+ mClientList.erase(iter);
+ }
+}
+
+void BufferHubService::removeTokenByClient(const BufferClient* client) {
+ std::lock_guard<std::mutex> lock(mTokenMapMutex);
+ auto iter = mTokenMap.begin();
+ while (iter != mTokenMap.end()) {
+ if (iter->second == client) {
+ auto oldIter = iter;
+ ++iter;
+ mTokenMap.erase(oldIter);
+ } else {
+ ++iter;
+ }
+ }
+}
+
} // namespace implementation
} // namespace V1_0
} // namespace bufferhub
diff --git a/services/bufferhub/include/bufferhub/BufferClient.h b/services/bufferhub/include/bufferhub/BufferClient.h
index 769ec86..7f5d3a6 100644
--- a/services/bufferhub/include/bufferhub/BufferClient.h
+++ b/services/bufferhub/include/bufferhub/BufferClient.h
@@ -44,14 +44,22 @@
// Creates a BufferClient from an existing BufferClient. Will share the same BufferNode.
explicit BufferClient(const BufferClient& other)
: mService(other.mService), mBufferNode(other.mBufferNode) {}
+ ~BufferClient();
+ Return<BufferHubStatus> close() override;
Return<void> duplicate(duplicate_cb _hidl_cb) override;
private:
BufferClient(wp<BufferHubService> service, const std::shared_ptr<BufferNode>& node)
: mService(service), mBufferNode(node) {}
+ sp<BufferHubService> getService();
+
wp<BufferHubService> mService;
+
+ std::mutex mClosedMutex;
+ bool mClosed GUARDED_BY(mClosedMutex) = false;
+
std::shared_ptr<BufferNode> mBufferNode;
};
diff --git a/services/bufferhub/include/bufferhub/BufferHubService.h b/services/bufferhub/include/bufferhub/BufferHubService.h
index 6535659..1c328b3 100644
--- a/services/bufferhub/include/bufferhub/BufferHubService.h
+++ b/services/bufferhub/include/bufferhub/BufferHubService.h
@@ -48,7 +48,12 @@
// Internal help function for IBufferClient::duplicate.
hidl_handle registerToken(const wp<BufferClient>& client);
+ void onClientClosed(const BufferClient* client);
+
private:
+ // Helper function to remove all the token belongs to a specific client.
+ void removeTokenByClient(const BufferClient* client);
+
// List of active BufferClient for bookkeeping.
std::mutex mClientListMutex;
std::vector<wp<BufferClient>> mClientList GUARDED_BY(mClientListMutex);