diff options
author | Josh Gao <jmgao@google.com> | 2017-05-03 22:37:10 -0700 |
---|---|---|
committer | Josh Gao <jmgao@google.com> | 2017-05-04 16:03:12 -0700 |
commit | 19bdfb09e3de6f46f06211c3edc3f57e11e62054 (patch) | |
tree | e31cc6a7a30963a7c1c0186668674c7dc3db1446 /client/main.cpp | |
parent | b69ee0b93c1f29c27365e373da5143faff04d6a6 (diff) | |
download | adb-19bdfb09e3de6f46f06211c3edc3f57e11e62054.tar.gz |
adb: wait for devices to come up instead of sleeping for 3s.
Replace a hard-coded 3 second sleep with logic to wait until we've
scanned USB devices once and they've all come online.
Before:
adb shell true 0.00s user 0.00s system 0% cpu 3.047 total
After:
adb shell true 0.00s user 0.00s system 9% cpu 0.041 total
Bug: http://b/37869663
Test: `time adb shell true` after adb kill-server
Change-Id: I251d42afb885908ed9d03167287594ea16650d3f
Diffstat (limited to 'client/main.cpp')
-rw-r--r-- | client/main.cpp | 53 |
1 files changed, 29 insertions, 24 deletions
diff --git a/client/main.cpp b/client/main.cpp index 606203c..fe5099c 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -156,33 +156,38 @@ int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply } #endif - // Inform our parent that we are up and running. - - // Any error output written to stderr now goes to adb.log. We could - // keep around a copy of the stderr fd and use that to write any errors - // encountered by the following code, but that is probably overkill. + // Wait for the USB scan to complete before notifying the parent that we're up. + // We need to perform this in a thread, because we would otherwise block the event loop. + std::thread notify_thread([ack_reply_fd]() { + adb_wait_for_device_initialization(); + + // Any error output written to stderr now goes to adb.log. We could + // keep around a copy of the stderr fd and use that to write any errors + // encountered by the following code, but that is probably overkill. #if defined(_WIN32) - const HANDLE ack_reply_handle = cast_int_to_handle(ack_reply_fd); - const CHAR ack[] = "OK\n"; - const DWORD bytes_to_write = arraysize(ack) - 1; - DWORD written = 0; - if (!WriteFile(ack_reply_handle, ack, bytes_to_write, &written, NULL)) { - fatal("adb: cannot write ACK to handle 0x%p: %s", ack_reply_handle, - android::base::SystemErrorCodeToString(GetLastError()).c_str()); - } - if (written != bytes_to_write) { - fatal("adb: cannot write %lu bytes of ACK: only wrote %lu bytes", - bytes_to_write, written); - } - CloseHandle(ack_reply_handle); + const HANDLE ack_reply_handle = cast_int_to_handle(ack_reply_fd); + const CHAR ack[] = "OK\n"; + const DWORD bytes_to_write = arraysize(ack) - 1; + DWORD written = 0; + if (!WriteFile(ack_reply_handle, ack, bytes_to_write, &written, NULL)) { + fatal("adb: cannot write ACK to handle 0x%p: %s", ack_reply_handle, + android::base::SystemErrorCodeToString(GetLastError()).c_str()); + } + if (written != bytes_to_write) { + fatal("adb: cannot write %lu bytes of ACK: only wrote %lu bytes", bytes_to_write, + written); + } + CloseHandle(ack_reply_handle); #else - // TODO(danalbert): Can't use SendOkay because we're sending "OK\n", not - // "OKAY". - if (!android::base::WriteStringToFd("OK\n", ack_reply_fd)) { - fatal_errno("error writing ACK to fd %d", ack_reply_fd); - } - unix_close(ack_reply_fd); + // TODO(danalbert): Can't use SendOkay because we're sending "OK\n", not + // "OKAY". + if (!android::base::WriteStringToFd("OK\n", ack_reply_fd)) { + fatal_errno("error writing ACK to fd %d", ack_reply_fd); + } + unix_close(ack_reply_fd); #endif + }); + notify_thread.detach(); } D("Event loop starting"); |