From 8eb59a0050da42162b78444538e271989fed23f6 Mon Sep 17 00:00:00 2001 From: Tri Vo Date: Sun, 14 Oct 2018 16:09:51 -0700 Subject: [PATCH] Simplify libpower Changed: - simplified getting system suspend service - added isOk() check when releasing a wake lock Bug: 117575503 Test: presubmit Change-Id: I78f1f50a7670e7df88e59999c35f99a6df06e8ef --- power.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/power.cpp b/power.cpp index 8e07280..2e6d806 100644 --- a/power.cpp +++ b/power.cpp @@ -36,13 +36,7 @@ static std::mutex gLock; static std::unordered_map> gWakeLockMap; static sp getSystemSuspendServiceOnce() { - static std::once_flag initFlag; - static sp suspendService = nullptr; - std::call_once(initFlag, []() { - // It's possible for the calling process to not have permissions to - // ISystemSuspend. getService will then return nullptr. - suspendService = ISystemSuspend::getService(); - }); + static sp suspendService = ISystemSuspend::getService(); return suspendService; } @@ -64,8 +58,13 @@ int release_wake_lock(const char* id) { ATRACE_CALL(); std::lock_guard l{gLock}; if (gWakeLockMap[id]) { - gWakeLockMap[id]->release(); - gWakeLockMap[id] = nullptr; + // Ignore errors on release() call since hwbinder driver will clean up the underlying object + // once we clear the corresponding strong pointer. + auto ret = gWakeLockMap[id]->release(); + if (!ret.isOk()) { + LOG(ERROR) << "IWakeLock::release() call failed: " << ret.description(); + } + gWakeLockMap[id].clear(); return 0; } return -1;