diff --git a/adb/adb.cpp b/adb/adb.cpp index 8b6b2b5ed..3cd50bad4 100644 --- a/adb/adb.cpp +++ b/adb/adb.cpp @@ -30,7 +30,9 @@ #include #include +#include #include +#include #include #include @@ -51,6 +53,7 @@ #include #include #include +using namespace std::chrono_literals; #endif std::string adb_version() { @@ -375,7 +378,7 @@ void handle_packet(apacket *p, atransport *t) adbd_auth_verified(t); t->failed_auth_attempts = 0; } else { - if (t->failed_auth_attempts++ > 256) adb_sleep_ms(1000); + if (t->failed_auth_attempts++ > 256) std::this_thread::sleep_for(1s); send_auth_request(t); } break; diff --git a/adb/adb_client.cpp b/adb/adb_client.cpp index 919e1c19a..1291364fd 100644 --- a/adb/adb_client.cpp +++ b/adb/adb_client.cpp @@ -28,7 +28,9 @@ #include #include +#include #include +#include #include #include @@ -188,8 +190,8 @@ int adb_connect(const std::string& service, std::string* error) { } else { fprintf(stdout,"* daemon started successfully *\n"); } - /* give the server some time to start properly and detect devices */ - adb_sleep_ms(3000); + // Give the server some time to start properly and detect devices. + std::this_thread::sleep_for(std::chrono::seconds(3)); // fall through to _adb_connect } else { // If a server is already running, check its version matches. @@ -234,7 +236,7 @@ int adb_connect(const std::string& service, std::string* error) { } /* XXX can we better detect its death? */ - adb_sleep_ms(2000); + std::this_thread::sleep_for(std::chrono::seconds(2)); goto start_server; } } diff --git a/adb/adb_io.cpp b/adb/adb_io.cpp index ae1683408..fe2ece50e 100644 --- a/adb/adb_io.cpp +++ b/adb/adb_io.cpp @@ -20,6 +20,9 @@ #include +#include +#include + #include #include "adb.h" @@ -104,7 +107,7 @@ bool WriteFdExactly(int fd, const void* buf, size_t len) { if (r == -1) { D("writex: fd=%d error %d: %s", fd, errno, strerror(errno)); if (errno == EAGAIN) { - adb_sleep_ms(1); // just yield some cpu time + std::this_thread::yield(); continue; } else if (errno == EPIPE) { D("writex: fd=%d disconnected", fd); diff --git a/adb/commandline.cpp b/adb/commandline.cpp index e15bcad8f..87c5cfb18 100644 --- a/adb/commandline.cpp +++ b/adb/commandline.cpp @@ -31,8 +31,10 @@ #include #include +#include #include #include +#include #include #include @@ -1080,7 +1082,7 @@ static bool adb_root(const char* command) { // Give adbd some time to kill itself and come back up. // We can't use wait-for-device because devices (e.g. adb over network) might not come back. - adb_sleep_ms(3000); + std::this_thread::sleep_for(std::chrono::seconds(3)); return true; } diff --git a/adb/socket_test.cpp b/adb/socket_test.cpp index 5e79b5e2e..b6aff54e5 100644 --- a/adb/socket_test.cpp +++ b/adb/socket_test.cpp @@ -19,9 +19,11 @@ #include #include +#include #include #include #include +#include #include #include @@ -44,7 +46,7 @@ static void FdEventThreadFunc(void*) { fdevent_loop(); } -const size_t SLEEP_FOR_FDEVENT_IN_MS = 100; +constexpr auto SLEEP_FOR_FDEVENT = std::chrono::milliseconds(100); TEST_F(LocalSocketTest, smoke) { // Join two socketpairs with a chain of intermediate socketpairs. @@ -101,7 +103,7 @@ TEST_F(LocalSocketTest, smoke) { ASSERT_EQ(0, adb_close(last[1])); // Wait until the local sockets are closed. - adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); + std::this_thread::sleep_for(SLEEP_FOR_FDEVENT); ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); TerminateThread(thread); } @@ -154,12 +156,12 @@ TEST_F(LocalSocketTest, close_socket_with_packet) { ASSERT_TRUE(adb_thread_create(reinterpret_cast(CloseWithPacketThreadFunc), &arg, &thread)); // Wait until the fdevent_loop() starts. - adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); + std::this_thread::sleep_for(SLEEP_FOR_FDEVENT); ASSERT_EQ(0, adb_close(cause_close_fd[0])); - adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); + std::this_thread::sleep_for(SLEEP_FOR_FDEVENT); EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); ASSERT_EQ(0, adb_close(socket_fd[0])); - adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); + std::this_thread::sleep_for(SLEEP_FOR_FDEVENT); ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); TerminateThread(thread); } @@ -179,9 +181,9 @@ TEST_F(LocalSocketTest, read_from_closing_socket) { ASSERT_TRUE(adb_thread_create(reinterpret_cast(CloseWithPacketThreadFunc), &arg, &thread)); // Wait until the fdevent_loop() starts. - adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); + std::this_thread::sleep_for(SLEEP_FOR_FDEVENT); ASSERT_EQ(0, adb_close(cause_close_fd[0])); - adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); + std::this_thread::sleep_for(SLEEP_FOR_FDEVENT); EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); // Verify if we can read successfully. @@ -190,7 +192,7 @@ TEST_F(LocalSocketTest, read_from_closing_socket) { ASSERT_EQ(true, ReadFdExactly(socket_fd[0], buf.data(), buf.size())); ASSERT_EQ(0, adb_close(socket_fd[0])); - adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); + std::this_thread::sleep_for(SLEEP_FOR_FDEVENT); ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); TerminateThread(thread); } @@ -214,11 +216,11 @@ TEST_F(LocalSocketTest, write_error_when_having_packets) { &arg, &thread)); // Wait until the fdevent_loop() starts. - adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); + std::this_thread::sleep_for(SLEEP_FOR_FDEVENT); EXPECT_EQ(2u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); ASSERT_EQ(0, adb_close(socket_fd[0])); - adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); + std::this_thread::sleep_for(SLEEP_FOR_FDEVENT); ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); TerminateThread(thread); } @@ -229,7 +231,7 @@ static void ClientThreadFunc() { std::string error; int fd = network_loopback_client(5038, SOCK_STREAM, &error); ASSERT_GE(fd, 0) << error; - adb_sleep_ms(200); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); ASSERT_EQ(0, adb_close(fd)); } @@ -265,13 +267,13 @@ TEST_F(LocalSocketTest, close_socket_in_CLOSE_WAIT_state) { &arg, &thread)); // Wait until the fdevent_loop() starts. - adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); + std::this_thread::sleep_for(SLEEP_FOR_FDEVENT); EXPECT_EQ(1u + GetAdditionalLocalSocketCount(), fdevent_installed_count()); // Wait until the client closes its socket. ASSERT_TRUE(adb_thread_join(client_thread)); - adb_sleep_ms(SLEEP_FOR_FDEVENT_IN_MS); + std::this_thread::sleep_for(SLEEP_FOR_FDEVENT); ASSERT_EQ(GetAdditionalLocalSocketCount(), fdevent_installed_count()); TerminateThread(thread); } diff --git a/adb/sysdeps.h b/adb/sysdeps.h index ad9b9fd61..0489d09a2 100644 --- a/adb/sysdeps.h +++ b/adb/sysdeps.h @@ -248,11 +248,6 @@ extern int unix_open(const char* path, int options, ...); int unix_isatty(int fd); #define isatty ___xxx_isatty -static __inline__ void adb_sleep_ms( int mseconds ) -{ - Sleep( mseconds ); -} - int network_loopback_client(int port, int type, std::string* error); int network_loopback_server(int port, int type, std::string* error); int network_inaddr_any_server(int port, int type, std::string* error); @@ -766,11 +761,6 @@ static __inline__ int adb_poll(adb_pollfd* fds, size_t nfds, int timeout) { #define poll ___xxx_poll -static __inline__ void adb_sleep_ms( int mseconds ) -{ - usleep( mseconds*1000 ); -} - static __inline__ int adb_mkdir(const std::string& path, int mode) { return mkdir(path.c_str(), mode); diff --git a/adb/sysdeps_test.cpp b/adb/sysdeps_test.cpp index 9f779428c..9491952ea 100644 --- a/adb/sysdeps_test.cpp +++ b/adb/sysdeps_test.cpp @@ -16,14 +16,17 @@ #include #include + #include +#include #include +#include #include "adb_io.h" #include "sysdeps.h" static void increment_atomic_int(void* c) { - sleep(1); + std::this_thread::sleep_for(std::chrono::seconds(1)); reinterpret_cast*>(c)->fetch_add(1); } @@ -34,7 +37,7 @@ TEST(sysdeps_thread, smoke) { ASSERT_TRUE(adb_thread_create(increment_atomic_int, &counter)); } - sleep(2); + std::this_thread::sleep_for(std::chrono::seconds(2)); ASSERT_EQ(100, counter.load()); } @@ -255,15 +258,15 @@ TEST(sysdeps_mutex, mutex_smoke) { ASSERT_FALSE(m.try_lock()); m.lock(); finished.store(true); - adb_sleep_ms(200); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); m.unlock(); }, nullptr); ASSERT_FALSE(finished.load()); - adb_sleep_ms(100); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); ASSERT_FALSE(finished.load()); m.unlock(); - adb_sleep_ms(100); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); m.lock(); ASSERT_TRUE(finished.load()); m.unlock(); @@ -279,13 +282,13 @@ TEST(sysdeps_mutex, recursive_mutex_smoke) { adb_thread_create([](void*) { ASSERT_FALSE(m.try_lock()); m.lock(); - adb_sleep_ms(500); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); m.unlock(); }, nullptr); - adb_sleep_ms(100); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); m.unlock(); - adb_sleep_ms(100); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); ASSERT_FALSE(m.try_lock()); m.lock(); m.unlock(); diff --git a/adb/transport_local.cpp b/adb/transport_local.cpp index ba2b28d8b..92010c0e0 100644 --- a/adb/transport_local.cpp +++ b/adb/transport_local.cpp @@ -25,8 +25,10 @@ #include #include +#include #include #include +#include #include #include @@ -144,7 +146,7 @@ static void PollAllLocalPortsForEmulator() { // Retry the disconnected local port for 60 times, and sleep 1 second between two retries. constexpr uint32_t LOCAL_PORT_RETRY_COUNT = 60; -constexpr uint32_t LOCAL_PORT_RETRY_INTERVAL_IN_MS = 1000; +constexpr auto LOCAL_PORT_RETRY_INTERVAL = std::chrono::seconds(1); struct RetryPort { int port; @@ -173,7 +175,7 @@ static void client_socket_thread(void* x) { // Sleep here instead of the end of loop, because if we immediately try to reconnect // the emulator just kicked, the adbd on the emulator may not have time to remove the // just kicked transport. - adb_sleep_ms(LOCAL_PORT_RETRY_INTERVAL_IN_MS); + std::this_thread::sleep_for(LOCAL_PORT_RETRY_INTERVAL); // Try connecting retry ports. std::vector next_ports; @@ -214,7 +216,7 @@ static void server_socket_thread(void* arg) { serverfd = network_inaddr_any_server(port, SOCK_STREAM, &error); if(serverfd < 0) { D("server: cannot bind socket yet: %s", error.c_str()); - adb_sleep_ms(1000); + std::this_thread::sleep_for(std::chrono::seconds(1)); continue; } close_on_exec(serverfd); diff --git a/adb/usb_linux.cpp b/adb/usb_linux.cpp index 3e5028d32..e7f1338b3 100644 --- a/adb/usb_linux.cpp +++ b/adb/usb_linux.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #include @@ -46,6 +47,7 @@ #include "adb.h" #include "transport.h" +using namespace std::chrono_literals; using namespace std::literals; /* usb scan debugging is waaaay too verbose */ @@ -577,7 +579,7 @@ static void device_poll_thread(void*) { // TODO: Use inotify. find_usb_device("/dev/bus/usb", register_device); kick_disconnected_devices(); - sleep(1); + std::this_thread::sleep_for(1s); } } diff --git a/adb/usb_linux_client.cpp b/adb/usb_linux_client.cpp index 6de10f53e..1cc7f68b6 100644 --- a/adb/usb_linux_client.cpp +++ b/adb/usb_linux_client.cpp @@ -31,8 +31,10 @@ #include #include +#include #include #include +#include #include #include @@ -40,6 +42,8 @@ #include "adb.h" #include "transport.h" +using namespace std::chrono_literals; + #define MAX_PACKET_SIZE_FS 64 #define MAX_PACKET_SIZE_HS 512 #define MAX_PACKET_SIZE_SS 1024 @@ -268,7 +272,7 @@ static void usb_adb_open_thread(void* x) { fd = unix_open("/dev/android", O_RDWR); } if (fd < 0) { - adb_sleep_ms(1000); + std::this_thread::sleep_for(1s); } } while (fd < 0); D("[ opening device succeeded ]"); @@ -476,7 +480,7 @@ static void usb_ffs_open_thread(void* x) { if (init_functionfs(usb)) { break; } - adb_sleep_ms(1000); + std::this_thread::sleep_for(1s); } android::base::SetProperty("sys.usb.ffs.ready", "1"); diff --git a/adb/usb_osx.cpp b/adb/usb_osx.cpp index 2ee2aae5d..e541f6ecc 100644 --- a/adb/usb_osx.cpp +++ b/adb/usb_osx.cpp @@ -30,8 +30,10 @@ #include #include +#include #include #include +#include #include #include @@ -40,6 +42,8 @@ #include "adb.h" #include "transport.h" +using namespace std::chrono_literals; + struct usb_handle { UInt8 bulkIn; @@ -411,7 +415,7 @@ static void RunLoopThread(void* unused) { } // Signal the parent that we are running usb_inited_flag = true; - adb_sleep_ms(1000); + std::this_thread::sleep_for(1s); } VLOG(USB) << "RunLoopThread done"; } @@ -436,7 +440,7 @@ void usb_init() { // Wait for initialization to finish while (!usb_inited_flag) { - adb_sleep_ms(100); + std::this_thread::sleep_for(100ms); } initialized = true; diff --git a/adb/usb_windows.cpp b/adb/usb_windows.cpp index 755f07ef0..136453773 100644 --- a/adb/usb_windows.cpp +++ b/adb/usb_windows.cpp @@ -28,6 +28,7 @@ #include #include +#include #include @@ -176,9 +177,9 @@ void device_poll_thread(void*) { adb_thread_setname("Device Poll"); D("Created device thread"); - while(1) { + while (true) { find_devices(); - adb_sleep_ms(1000); + std::this_thread::sleep_for(std::chrono::seconds(1)); } }