libbinder: add IBinder::withLock
BpBinder (and BBinder, once its mExtra allocation is made) have very
nice and shiny locks which they keep all for themselves! Stop it,
IBinder! Share that lock!
This provides convenient access to IBinder's lock, in order to avoid
needing additional locks elsewhere.
Bug: 192023359
Test: N/A
Change-Id: Id3485a2ac66d19379dcad2f0b41d6cb7a8a96725
diff --git a/libs/binder/Binder.cpp b/libs/binder/Binder.cpp
index 4965386..53750c9 100644
--- a/libs/binder/Binder.cpp
+++ b/libs/binder/Binder.cpp
@@ -179,6 +179,17 @@
return transact(SET_RPC_CLIENT_TRANSACTION, data, &reply);
}
+void IBinder::withLock(const std::function<void()>& doWithLock) {
+ BBinder* local = localBinder();
+ if (local) {
+ local->withLock(doWithLock);
+ return;
+ }
+ BpBinder* proxy = this->remoteBinder();
+ LOG_ALWAYS_FATAL_IF(proxy == nullptr, "binder object must be either local or remote");
+ proxy->withLock(doWithLock);
+}
+
// ---------------------------------------------------------------------------
class BBinder::RpcServerLink : public IBinder::DeathRecipient {
@@ -337,6 +348,14 @@
return e->mObjects.detach(objectID);
}
+void BBinder::withLock(const std::function<void()>& doWithLock) {
+ Extras* e = getOrCreateExtras();
+ LOG_ALWAYS_FATAL_IF(!e, "no memory");
+
+ AutoMutex _l(e->mLock);
+ doWithLock();
+}
+
BBinder* BBinder::localBinder()
{
return this;