adb: add sysdeps/chrono.h for chrono literals on Win32.
Bug: http://b/32878766
Test: mma
Change-Id: Iaa89d4eadf07e63d6d7205057435a6c7fb5d4ec5
diff --git a/adb/adb_client.cpp b/adb/adb_client.cpp
index 1291364..0b1ba32 100644
--- a/adb/adb_client.cpp
+++ b/adb/adb_client.cpp
@@ -28,7 +28,6 @@
#include <sys/stat.h>
#include <sys/types.h>
-#include <chrono>
#include <string>
#include <thread>
#include <vector>
@@ -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 @@
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 @@
}
/* 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 fe2ece5..ca8729e 100644
--- a/adb/adb_io.cpp
+++ b/adb/adb_io.cpp
@@ -20,7 +20,6 @@
#include <unistd.h>
-#include <chrono>
#include <thread>
#include <android-base/stringprintf.h>
diff --git a/adb/commandline.cpp b/adb/commandline.cpp
index 87c5cfb..a064de2 100644
--- a/adb/commandline.cpp
+++ b/adb/commandline.cpp
@@ -31,7 +31,6 @@
#include <sys/stat.h>
#include <sys/types.h>
-#include <chrono>
#include <memory>
#include <string>
#include <thread>
@@ -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 @@
// 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 b6aff54..f56f7f7 100644
--- a/adb/socket_test.cpp
+++ b/adb/socket_test.cpp
@@ -19,7 +19,6 @@
#include <gtest/gtest.h>
#include <array>
-#include <chrono>
#include <limits>
#include <queue>
#include <string>
@@ -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 @@
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 @@
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 0000000..c73a638
--- /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 <chrono>
+
+#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<long double> operator"" s(long double s) {
+ return std::chrono::duration<long double>(s);
+}
+
+constexpr std::chrono::milliseconds operator"" ms(unsigned long long ms) {
+ return std::chrono::milliseconds(ms);
+}
+
+constexpr std::chrono::duration<long double, std::milli> operator"" ms(long double ms) {
+ return std::chrono::duration<long double, std::milli>(ms);
+}
+#else
+using namespace std::chrono_literals;
+#endif
diff --git a/adb/sysdeps_test.cpp b/adb/sysdeps_test.cpp
index 9491952..9007e75 100644
--- a/adb/sysdeps_test.cpp
+++ b/adb/sysdeps_test.cpp
@@ -18,15 +18,15 @@
#include <unistd.h>
#include <atomic>
-#include <chrono>
#include <condition_variable>
#include <thread>
#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<std::atomic<int>*>(c)->fetch_add(1);
}
@@ -37,7 +37,7 @@
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 @@
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 @@
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 92010c0..c17f869 100644
--- a/adb/transport_local.cpp
+++ b/adb/transport_local.cpp
@@ -25,7 +25,6 @@
#include <string.h>
#include <sys/types.h>
-#include <chrono>
#include <condition_variable>
#include <mutex>
#include <thread>
@@ -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 @@
// 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 @@
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 1364537..640e91e 100644
--- a/adb/usb_windows.cpp
+++ b/adb/usb_windows.cpp
@@ -35,6 +35,7 @@
#include <android-base/errors.h>
#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 @@
while (true) {
find_devices();
- std::this_thread::sleep_for(std::chrono::seconds(1));
+ std::this_thread::sleep_for(1s);
}
}