Merge "Switch adb over to <chrono>."

am: 59826ddee5

Change-Id: I673ce3c9f4a104df3197ec7120a126155e18918e
This commit is contained in:
Elliott Hughes 2016-11-16 20:14:20 +00:00 committed by android-build-merger
commit 2278b50f24
12 changed files with 65 additions and 47 deletions

View File

@ -30,7 +30,9 @@
#include <sys/time.h>
#include <time.h>
#include <chrono>
#include <string>
#include <thread>
#include <vector>
#include <android-base/errors.h>
@ -51,6 +53,7 @@
#include <sys/capability.h>
#include <sys/mount.h>
#include <android-base/properties.h>
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;

View File

@ -28,7 +28,9 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <chrono>
#include <string>
#include <thread>
#include <vector>
#include <android-base/stringprintf.h>
@ -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;
}
}

View File

@ -20,6 +20,9 @@
#include <unistd.h>
#include <chrono>
#include <thread>
#include <android-base/stringprintf.h>
#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);

View File

@ -31,8 +31,10 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <chrono>
#include <memory>
#include <string>
#include <thread>
#include <vector>
#include <android-base/file.h>
@ -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;
}

View File

@ -19,9 +19,11 @@
#include <gtest/gtest.h>
#include <array>
#include <chrono>
#include <limits>
#include <queue>
#include <string>
#include <thread>
#include <vector>
#include <unistd.h>
@ -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<void (*)(void*)>(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<void (*)(void*)>(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);
}

View File

@ -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);

View File

@ -16,14 +16,17 @@
#include <gtest/gtest.h>
#include <unistd.h>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <thread>
#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<std::atomic<int>*>(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();

View File

@ -25,8 +25,10 @@
#include <string.h>
#include <sys/types.h>
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <vector>
#include <android-base/stringprintf.h>
@ -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<RetryPort> 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);

View File

@ -38,6 +38,7 @@
#include <list>
#include <mutex>
#include <string>
#include <thread>
#include <android-base/file.h>
#include <android-base/stringprintf.h>
@ -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);
}
}

View File

@ -31,8 +31,10 @@
#include <algorithm>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <android-base/logging.h>
#include <android-base/properties.h>
@ -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");

View File

@ -30,8 +30,10 @@
#include <stdio.h>
#include <atomic>
#include <chrono>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>
#include <android-base/logging.h>
@ -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;

View File

@ -28,6 +28,7 @@
#include <stdlib.h>
#include <mutex>
#include <thread>
#include <adb_api.h>
@ -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));
}
}