From 9a8366b3d9560c35bd658454cc7a66952a07d8d7 Mon Sep 17 00:00:00 2001 From: Josh Gao Date: Mon, 9 Dec 2019 13:45:31 -0800 Subject: [PATCH] adb: remove atransport ref counting. The reference count is always 0 or 1. Test: ./test_adb.py Change-Id: I669f98f4996f4e41ac037f1add9c47819d4003d4 --- adb/transport.cpp | 40 +++++++++++++++------------------------- adb/transport.h | 2 +- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/adb/transport.cpp b/adb/transport.cpp index d9749ac35..3e9ab865e 100644 --- a/adb/transport.cpp +++ b/adb/transport.cpp @@ -55,7 +55,7 @@ using android::base::ScopedLockAssertion; static void remove_transport(atransport* transport); -static void transport_unref(atransport* transport); +static void transport_destroy(atransport* transport); // TODO: unordered_map static auto& transport_list = *new std::list(); @@ -676,7 +676,6 @@ static void transport_registration_func(int _fd, unsigned ev, void*) { if (t->GetConnectionState() != kCsNoPerm) { // The connection gets a reference to the atransport. It will release it // upon a read/write error. - t->ref_count++; t->connection()->SetTransportName(t->serial_name()); t->connection()->SetReadCallback([t](Connection*, std::unique_ptr p) { if (!check_header(p.get(), t)) { @@ -695,7 +694,7 @@ static void transport_registration_func(int _fd, unsigned ev, void*) { LOG(INFO) << t->serial_name() << ": connection terminated: " << error; fdevent_run_on_main_thread([t]() { handle_offline(t); - transport_unref(t); + transport_destroy(t); }); }); @@ -771,36 +770,27 @@ static void remove_transport(atransport* transport) { } } -static void transport_unref(atransport* t) { +static void transport_destroy(atransport* t) { check_main_thread(); CHECK(t != nullptr); std::lock_guard lock(transport_lock); - CHECK_GT(t->ref_count, 0u); - t->ref_count--; - if (t->ref_count == 0) { - LOG(INFO) << "destroying transport " << t->serial_name(); - t->connection()->Stop(); + LOG(INFO) << "destroying transport " << t->serial_name(); + t->connection()->Stop(); #if ADB_HOST - if (t->IsTcpDevice() && !t->kicked()) { - D("transport: %s unref (attempting reconnection)", t->serial.c_str()); + if (t->IsTcpDevice() && !t->kicked()) { + D("transport: %s destroy (attempting reconnection)", t->serial.c_str()); - // We need to clear the transport's keys, so that on the next connection, it tries - // again from the beginning. - t->ResetKeys(); - reconnect_handler.TrackTransport(t); - } else { - D("transport: %s unref (kicking and closing)", t->serial.c_str()); - remove_transport(t); - } -#else - D("transport: %s unref (kicking and closing)", t->serial.c_str()); - remove_transport(t); + // We need to clear the transport's keys, so that on the next connection, it tries + // again from the beginning. + t->ResetKeys(); + reconnect_handler.TrackTransport(t); + return; + } #endif - } else { - D("transport: %s unref (count=%zu)", t->serial.c_str(), t->ref_count); - } + D("transport: %s destroy (kicking and closing)", t->serial.c_str()); + remove_transport(t); } static int qual_match(const std::string& to_test, const char* prefix, const std::string& qual, diff --git a/adb/transport.h b/adb/transport.h index 89d76b8c9..0de385910 100644 --- a/adb/transport.h +++ b/adb/transport.h @@ -266,7 +266,7 @@ class atransport { usb_handle* GetUsbHandle() { return usb_handle_; } const TransportId id; - size_t ref_count = 0; + bool online = false; TransportType type = kTransportAny;