diff --git a/adb/adb_client.cpp b/adb/adb_client.cpp index 1291364fd..0b1ba32be 100644 --- a/adb/adb_client.cpp +++ b/adb/adb_client.cpp @@ -28,7 +28,6 @@ #include #include -#include #include #include #include @@ -40,6 +39,7 @@ #include "adb_io.h" #include "adb_utils.h" #include "socket_spec.h" +#include "sysdeps/chrono.h" static TransportType __adb_transport = kTransportAny; static const char* __adb_serial = NULL; @@ -191,7 +191,7 @@ int adb_connect(const std::string& service, std::string* error) { fprintf(stdout,"* daemon started successfully *\n"); } // Give the server some time to start properly and detect devices. - std::this_thread::sleep_for(std::chrono::seconds(3)); + std::this_thread::sleep_for(3s); // fall through to _adb_connect } else { // If a server is already running, check its version matches. @@ -236,7 +236,7 @@ int adb_connect(const std::string& service, std::string* error) { } /* XXX can we better detect its death? */ - std::this_thread::sleep_for(std::chrono::seconds(2)); + std::this_thread::sleep_for(2s); goto start_server; } } diff --git a/adb/adb_io.cpp b/adb/adb_io.cpp index fe2ece50e..ca8729eb1 100644 --- a/adb/adb_io.cpp +++ b/adb/adb_io.cpp @@ -20,7 +20,6 @@ #include -#include #include #include diff --git a/adb/commandline.cpp b/adb/commandline.cpp index 87c5cfb18..a064de27f 100644 --- a/adb/commandline.cpp +++ b/adb/commandline.cpp @@ -31,7 +31,6 @@ #include #include -#include #include #include #include @@ -61,6 +60,7 @@ #include "file_sync_service.h" #include "services.h" #include "shell_service.h" +#include "sysdeps/chrono.h" static int install_app(TransportType t, const char* serial, int argc, const char** argv); static int install_multiple_app(TransportType t, const char* serial, int argc, const char** argv); @@ -1082,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. - std::this_thread::sleep_for(std::chrono::seconds(3)); + std::this_thread::sleep_for(3s); return true; } diff --git a/adb/socket_test.cpp b/adb/socket_test.cpp index b6aff54e5..f56f7f790 100644 --- a/adb/socket_test.cpp +++ b/adb/socket_test.cpp @@ -19,7 +19,6 @@ #include #include -#include #include #include #include @@ -33,6 +32,7 @@ #include "fdevent_test.h" #include "socket.h" #include "sysdeps.h" +#include "sysdeps/chrono.h" struct ThreadArg { int first_read_fd; @@ -46,7 +46,7 @@ static void FdEventThreadFunc(void*) { fdevent_loop(); } -constexpr auto SLEEP_FOR_FDEVENT = std::chrono::milliseconds(100); +constexpr auto SLEEP_FOR_FDEVENT = 100ms; TEST_F(LocalSocketTest, smoke) { // Join two socketpairs with a chain of intermediate socketpairs. @@ -231,7 +231,7 @@ static void ClientThreadFunc() { std::string error; int fd = network_loopback_client(5038, SOCK_STREAM, &error); ASSERT_GE(fd, 0) << error; - std::this_thread::sleep_for(std::chrono::milliseconds(200)); + std::this_thread::sleep_for(200ms); ASSERT_EQ(0, adb_close(fd)); } diff --git a/adb/sysdeps/chrono.h b/adb/sysdeps/chrono.h new file mode 100644 index 000000000..c73a638ad --- /dev/null +++ b/adb/sysdeps/chrono.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +#if defined(_WIN32) +// We don't have C++14 on Windows yet. +// Reimplement std::chrono_literals ourselves until we do. + +// Silence the following warning (which gets promoted to an error): +// error: literal operator suffixes not preceded by ‘_’ are reserved for future standardization +#pragma GCC system_header + +constexpr std::chrono::seconds operator"" s(unsigned long long s) { + return std::chrono::seconds(s); +} + +constexpr std::chrono::duration operator"" s(long double s) { + return std::chrono::duration(s); +} + +constexpr std::chrono::milliseconds operator"" ms(unsigned long long ms) { + return std::chrono::milliseconds(ms); +} + +constexpr std::chrono::duration operator"" ms(long double ms) { + return std::chrono::duration(ms); +} +#else +using namespace std::chrono_literals; +#endif diff --git a/adb/sysdeps_test.cpp b/adb/sysdeps_test.cpp index 9491952ea..9007e75f3 100644 --- a/adb/sysdeps_test.cpp +++ b/adb/sysdeps_test.cpp @@ -18,15 +18,15 @@ #include #include -#include #include #include #include "adb_io.h" #include "sysdeps.h" +#include "sysdeps/chrono.h" static void increment_atomic_int(void* c) { - std::this_thread::sleep_for(std::chrono::seconds(1)); + std::this_thread::sleep_for(1s); reinterpret_cast*>(c)->fetch_add(1); } @@ -37,7 +37,7 @@ TEST(sysdeps_thread, smoke) { ASSERT_TRUE(adb_thread_create(increment_atomic_int, &counter)); } - std::this_thread::sleep_for(std::chrono::seconds(2)); + std::this_thread::sleep_for(2s); ASSERT_EQ(100, counter.load()); } @@ -258,15 +258,15 @@ TEST(sysdeps_mutex, mutex_smoke) { ASSERT_FALSE(m.try_lock()); m.lock(); finished.store(true); - std::this_thread::sleep_for(std::chrono::milliseconds(200)); + std::this_thread::sleep_for(200ms); m.unlock(); }, nullptr); ASSERT_FALSE(finished.load()); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + std::this_thread::sleep_for(100ms); ASSERT_FALSE(finished.load()); m.unlock(); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + std::this_thread::sleep_for(100ms); m.lock(); ASSERT_TRUE(finished.load()); m.unlock(); @@ -282,13 +282,13 @@ TEST(sysdeps_mutex, recursive_mutex_smoke) { adb_thread_create([](void*) { ASSERT_FALSE(m.try_lock()); m.lock(); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); + std::this_thread::sleep_for(500ms); m.unlock(); }, nullptr); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + std::this_thread::sleep_for(100ms); m.unlock(); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + std::this_thread::sleep_for(100ms); ASSERT_FALSE(m.try_lock()); m.lock(); m.unlock(); diff --git a/adb/transport_local.cpp b/adb/transport_local.cpp index 92010c0e0..c17f86914 100644 --- a/adb/transport_local.cpp +++ b/adb/transport_local.cpp @@ -25,7 +25,6 @@ #include #include -#include #include #include #include @@ -41,6 +40,7 @@ #include "adb.h" #include "adb_io.h" #include "adb_utils.h" +#include "sysdeps/chrono.h" #if ADB_HOST @@ -146,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 auto LOCAL_PORT_RETRY_INTERVAL = std::chrono::seconds(1); +constexpr auto LOCAL_PORT_RETRY_INTERVAL = 1s; struct RetryPort { int port; @@ -216,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()); - std::this_thread::sleep_for(std::chrono::seconds(1)); + std::this_thread::sleep_for(1s); continue; } close_on_exec(serverfd); diff --git a/adb/usb_windows.cpp b/adb/usb_windows.cpp index 136453773..640e91ec3 100644 --- a/adb/usb_windows.cpp +++ b/adb/usb_windows.cpp @@ -35,6 +35,7 @@ #include #include "adb.h" +#include "sysdeps/chrono.h" #include "transport.h" /** Structure usb_handle describes our connection to the usb device via @@ -179,7 +180,7 @@ void device_poll_thread(void*) { while (true) { find_devices(); - std::this_thread::sleep_for(std::chrono::seconds(1)); + std::this_thread::sleep_for(1s); } }