summaryrefslogtreecommitdiff
path: root/libs/binder/RpcSession.cpp
diff options
context:
space:
mode:
author Steven Moreland <smoreland@google.com> 2021-07-15 22:45:09 +0000
committer Steven Moreland <smoreland@google.com> 2021-07-16 00:02:02 +0000
commitfba6f77ac6498aa35cd709ba2aa492d89df02399 (patch)
treebbb6ba897310150e84bf469a3bf6ac561efcde3e /libs/binder/RpcSession.cpp
parent798e0d1a492ae39ecba6b2b695808699234374f0 (diff)
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
Diffstat (limited to 'libs/binder/RpcSession.cpp')
-rw-r--r--libs/binder/RpcSession.cpp56
1 files changed, 30 insertions, 26 deletions
diff --git a/libs/binder/RpcSession.cpp b/libs/binder/RpcSession.cpp
index fc6a900885..8ab2875e92 100644
--- a/libs/binder/RpcSession.cpp
+++ b/libs/binder/RpcSession.cpp
@@ -476,32 +476,7 @@ bool RpcSession::setupOneSocketConnection(const RpcSocketAddress& addr, const Rp
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 @@ bool RpcSession::setupOneSocketConnection(const RpcSocketAddress& addr, const Rp
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();
{