From a46c0dae812d59d5f40d7293724e1d99909fdb34 Mon Sep 17 00:00:00 2001 From: Yifan Hong Date: Tue, 2 Oct 2018 14:06:51 -0700 Subject: [PATCH] health: use recursive_mutex. Need to use an reentrant lock. Bug: 117167903 Test: vts Change-Id: I25a366e06ed9d983823f7276668b7f45ddcf1c2b --- health/2.0/default/Health.cpp | 6 +++--- health/2.0/default/include/health2/Health.h | 4 +--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/health/2.0/default/Health.cpp b/health/2.0/default/Health.cpp index e02bfa98df..4a4bee06fb 100644 --- a/health/2.0/default/Health.cpp +++ b/health/2.0/default/Health.cpp @@ -46,7 +46,7 @@ Return Health::registerCallback(const sp& callback) } { - std::lock_guard _lock(callbacks_lock_); + std::lock_guard lock(callbacks_lock_); callbacks_.push_back(callback); // unlock } @@ -65,7 +65,7 @@ bool Health::unregisterCallbackInternal(const sp& callback) { if (callback == nullptr) return false; bool removed = false; - std::lock_guard _lock(callbacks_lock_); + std::lock_guard lock(callbacks_lock_); for (auto it = callbacks_.begin(); it != callbacks_.end();) { if (interfacesEqual(*it, callback)) { it = callbacks_.erase(it); @@ -175,7 +175,7 @@ void Health::notifyListeners(HealthInfo* healthInfo) { healthInfo->diskStats = stats; healthInfo->storageInfos = info; - std::lock_guard _lock(callbacks_lock_); + std::lock_guard lock(callbacks_lock_); for (auto it = callbacks_.begin(); it != callbacks_.end();) { auto ret = (*it)->healthInfoChanged(*healthInfo); if (!ret.isOk() && ret.isDeadObject()) { diff --git a/health/2.0/default/include/health2/Health.h b/health/2.0/default/include/health2/Health.h index 134cdc6e69..b960358bfd 100644 --- a/health/2.0/default/include/health2/Health.h +++ b/health/2.0/default/include/health2/Health.h @@ -31,12 +31,10 @@ struct Health : public IHealth, hidl_death_recipient { // Should only be called by implementation itself (-impl, -service). // Clients should not call this function. Instead, initInstance() initializes and returns the // global instance that has fewer functions. - // TODO(b/62229583): clean up and hide these functions after update() logic is simplified. static sp getImplementation(); Health(struct healthd_config* c); - // TODO(b/62229583): clean up and hide these functions after update() logic is simplified. void notifyListeners(HealthInfo* info); // Methods from IHealth follow. @@ -61,7 +59,7 @@ struct Health : public IHealth, hidl_death_recipient { private: static sp instance_; - std::mutex callbacks_lock_; + std::recursive_mutex callbacks_lock_; std::vector> callbacks_; std::unique_ptr battery_monitor_;