libbinder: 'RpcSession::addIncomingConnection'
Factor out complex bit of logic to setup an incoming connection into a
new function, in preparation for being able to set up the connection in
a different way.
Bug: 193801719
Test: binderRpcTest
Change-Id: I09a4520a5f00bb658c864000f7861a687a47efb0
diff --git a/libs/binder/RpcSession.cpp b/libs/binder/RpcSession.cpp
index fc6a900..8ab2875 100644
--- a/libs/binder/RpcSession.cpp
+++ b/libs/binder/RpcSession.cpp
@@ -476,32 +476,7 @@
LOG_RPC_DETAIL("Socket at %s client with fd %d", addr.toString().c_str(), serverFd.get());
if (reverse) {
- std::mutex mutex;
- std::condition_variable joinCv;
- std::unique_lock<std::mutex> lock(mutex);
- std::thread thread;
- sp<RpcSession> thiz = sp<RpcSession>::fromExisting(this);
- bool ownershipTransferred = false;
- thread = std::thread([&]() {
- std::unique_lock<std::mutex> threadLock(mutex);
- unique_fd fd = std::move(serverFd);
- // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
- sp<RpcSession> session = thiz;
- session->preJoinThreadOwnership(std::move(thread));
-
- // only continue once we have a response or the connection fails
- auto setupResult = session->preJoinSetup(std::move(fd));
-
- ownershipTransferred = true;
- threadLock.unlock();
- joinCv.notify_one();
- // do not use & vars below
-
- RpcSession::join(std::move(session), std::move(setupResult));
- });
- joinCv.wait(lock, [&] { return ownershipTransferred; });
- LOG_ALWAYS_FATAL_IF(!ownershipTransferred);
- return true;
+ return addIncomingConnection(std::move(serverFd));
} else {
return addOutgoingConnection(std::move(serverFd), true);
}
@@ -511,6 +486,35 @@
return false;
}
+bool RpcSession::addIncomingConnection(unique_fd fd) {
+ std::mutex mutex;
+ std::condition_variable joinCv;
+ std::unique_lock<std::mutex> lock(mutex);
+ std::thread thread;
+ sp<RpcSession> thiz = sp<RpcSession>::fromExisting(this);
+ bool ownershipTransferred = false;
+ thread = std::thread([&]() {
+ std::unique_lock<std::mutex> threadLock(mutex);
+ unique_fd movedFd = std::move(fd);
+ // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
+ sp<RpcSession> session = thiz;
+ session->preJoinThreadOwnership(std::move(thread));
+
+ // only continue once we have a response or the connection fails
+ auto setupResult = session->preJoinSetup(std::move(movedFd));
+
+ ownershipTransferred = true;
+ threadLock.unlock();
+ joinCv.notify_one();
+ // do not use & vars below
+
+ RpcSession::join(std::move(session), std::move(setupResult));
+ });
+ joinCv.wait(lock, [&] { return ownershipTransferred; });
+ LOG_ALWAYS_FATAL_IF(!ownershipTransferred);
+ return true;
+}
+
bool RpcSession::addOutgoingConnection(unique_fd fd, bool init) {
sp<RpcConnection> connection = sp<RpcConnection>::make();
{