summaryrefslogtreecommitdiff
path: root/libs/binder/IServiceManager.cpp
diff options
context:
space:
mode:
author Jon Spivack <spivack@google.com> 2019-11-20 11:47:54 -0800
committer android-build-merger <android-build-merger@google.com> 2019-11-20 11:47:54 -0800
commitf4e507c9f95d339f822034f952f26dabe25240ed (patch)
tree375e7cd4e6a3e76e2bd1ade5269e754cb5b5c223 /libs/binder/IServiceManager.cpp
parente05d6d937afa82f1f9df80e2d25af4faf258ab82 (diff)
parent9187f1d3b8fd5a58aeeebb2f6d27315852583eef (diff)
Merge "Unregister Waiter in waitForService" am: e395da3058 am: 7e1ed44b1b
am: 9187f1d3b8 Change-Id: Ia70928ea149f491999dd7eb2f3d49d7dd5a89470
Diffstat (limited to 'libs/binder/IServiceManager.cpp')
-rw-r--r--libs/binder/IServiceManager.cpp16
1 files changed, 14 insertions, 2 deletions
diff --git a/libs/binder/IServiceManager.cpp b/libs/binder/IServiceManager.cpp
index 4f47db199e..bac8b6604b 100644
--- a/libs/binder/IServiceManager.cpp
+++ b/libs/binder/IServiceManager.cpp
@@ -280,19 +280,31 @@ sp<IBinder> ServiceManagerShim::waitForService(const String16& name16)
std::condition_variable mCv;
};
+ // Simple RAII object to ensure a function call immediately before going out of scope
+ class Defer {
+ public:
+ Defer(std::function<void()>&& f) : mF(std::move(f)) {}
+ ~Defer() { mF(); }
+ private:
+ std::function<void()> mF;
+ };
+
const std::string name = String8(name16).c_str();
sp<IBinder> out;
if (!mTheRealServiceManager->getService(name, &out).isOk()) {
return nullptr;
}
- if(out != nullptr) return out;
+ if (out != nullptr) return out;
sp<Waiter> waiter = new Waiter;
if (!mTheRealServiceManager->registerForNotifications(
name, waiter).isOk()) {
return nullptr;
}
+ Defer unregister ([&] {
+ mTheRealServiceManager->unregisterForNotifications(name, waiter);
+ });
while(true) {
{
@@ -316,7 +328,7 @@ sp<IBinder> ServiceManagerShim::waitForService(const String16& name16)
if (!mTheRealServiceManager->getService(name, &out).isOk()) {
return nullptr;
}
- if(out != nullptr) return out;
+ if (out != nullptr) return out;
ALOGW("Waited one second for %s", name.c_str());
}