adb: initialize mDNS asynchronously.

Use fdevent_run_on_main_thread to initialize mDNS in a thread and
register an fdevent from the main thread upon success.

This reduces the startup time of `adb server` by ~3 seconds when mDNS
can't be successfully started. With an already running adb server,
`time adb server nodaemon` goes from:

    adb server nodaemon  0.00s user 0.16s system 4% cpu 3.817 total

to:

    adb server nodaemon  0.00s user 0.01s system 1% cpu 0.665 total

Bug: http://b/37869663
Test: `adb server nodaemon` with an existing adb server
Change-Id: Ia5a1a2a138610f3bf6792400050ca68f95ae3734
This commit is contained in:
Josh Gao 2017-05-03 14:23:09 -07:00
parent 4c936397c1
commit 6f46e6b912
1 changed files with 14 additions and 9 deletions

View File

@ -24,6 +24,8 @@
#include <arpa/inet.h>
#endif
#include <thread>
#include <android-base/stringprintf.h>
#include <dns_sd.h>
@ -262,19 +264,22 @@ static void DNSSD_API register_mdns_transport(DNSServiceRef sdRef,
}
}
void init_mdns_transport_discovery(void) {
DNSServiceErrorType errorCode =
DNSServiceBrowse(&service_ref, 0, 0, kADBServiceType, nullptr,
register_mdns_transport, nullptr);
void init_mdns_transport_discovery_thread(void) {
DNSServiceErrorType errorCode = DNSServiceBrowse(&service_ref, 0, 0, kADBServiceType, nullptr,
register_mdns_transport, nullptr);
if (errorCode != kDNSServiceErr_NoError) {
D("Got %d initiating mDNS browse.", errorCode);
return;
}
fdevent_install(&service_ref_fde,
adb_DNSServiceRefSockFD(service_ref),
pump_service_ref,
&service_ref);
fdevent_set(&service_ref_fde, FDE_READ);
fdevent_run_on_main_thread([]() {
fdevent_install(&service_ref_fde, adb_DNSServiceRefSockFD(service_ref), pump_service_ref,
&service_ref);
fdevent_set(&service_ref_fde, FDE_READ);
});
}
void init_mdns_transport_discovery(void) {
std::thread(init_mdns_transport_discovery_thread).detach();
}