Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6f46e6b9 authored by Josh Gao's avatar Josh Gao
Browse files

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
parent 4c936397
Loading
Loading
Loading
Loading
+14 −9
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@
#include <arpa/inet.h>
#endif

#include <thread>

#include <android-base/stringprintf.h>
#include <dns_sd.h>

@@ -262,9 +264,8 @@ static void DNSSD_API register_mdns_transport(DNSServiceRef sdRef,
    }
}

void init_mdns_transport_discovery(void) {
    DNSServiceErrorType errorCode =
        DNSServiceBrowse(&service_ref, 0, 0, kADBServiceType, 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) {
@@ -272,9 +273,13 @@ void init_mdns_transport_discovery(void) {
        return;
    }

    fdevent_install(&service_ref_fde,
                    adb_DNSServiceRefSockFD(service_ref),
                    pump_service_ref,
    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();
}