Merge "Simplify "adb sync" documentation."
diff --git a/base/Android.bp b/base/Android.bp
index 81b96db..14bfe36 100644
--- a/base/Android.bp
+++ b/base/Android.bp
@@ -42,6 +42,7 @@
     clang: true,
     host_supported: true,
     srcs: [
+        "chrono_utils.cpp",
         "file.cpp",
         "logging.cpp",
         "parsenetaddress.cpp",
@@ -64,7 +65,6 @@
             srcs: [
                 "errors_unix.cpp",
                 "properties.cpp",
-                "chrono_utils.cpp",
             ],
             cppflags: ["-Wexit-time-destructors"],
             sanitize: {
@@ -74,14 +74,12 @@
         },
         darwin: {
             srcs: [
-                "chrono_utils.cpp",
                 "errors_unix.cpp",
             ],
             cppflags: ["-Wexit-time-destructors"],
         },
         linux_bionic: {
             srcs: [
-                "chrono_utils.cpp",
                 "errors_unix.cpp",
             ],
             cppflags: ["-Wexit-time-destructors"],
@@ -89,7 +87,6 @@
         },
         linux: {
             srcs: [
-                "chrono_utils.cpp",
                 "errors_unix.cpp",
             ],
             cppflags: ["-Wexit-time-destructors"],
diff --git a/base/chrono_utils.cpp b/base/chrono_utils.cpp
index 5eedf3b..d73b551 100644
--- a/base/chrono_utils.cpp
+++ b/base/chrono_utils.cpp
@@ -21,17 +21,14 @@
 namespace android {
 namespace base {
 
+#if defined(__linux__)
 boot_clock::time_point boot_clock::now() {
-#ifdef __ANDROID__
   timespec ts;
   clock_gettime(CLOCK_BOOTTIME, &ts);
   return boot_clock::time_point(std::chrono::seconds(ts.tv_sec) +
                                 std::chrono::nanoseconds(ts.tv_nsec));
-#else
-  // Darwin does not support clock_gettime.
-  return boot_clock::time_point();
-#endif  // __ANDROID__
 }
+#endif
 
 }  // namespace base
 }  // namespace android
diff --git a/base/chrono_utils_test.cpp b/base/chrono_utils_test.cpp
index 057132d..1f1ce15 100644
--- a/base/chrono_utils_test.cpp
+++ b/base/chrono_utils_test.cpp
@@ -16,6 +16,7 @@
 
 #include "android-base/chrono_utils.h"
 
+#include <err.h>
 #include <time.h>
 
 #include <chrono>
@@ -25,9 +26,12 @@
 namespace android {
 namespace base {
 
+#if defined(__linux__)
 std::chrono::seconds GetBootTimeSeconds() {
   struct timespec now;
-  clock_gettime(CLOCK_BOOTTIME, &now);
+  if (clock_gettime(CLOCK_BOOTTIME, &now) != 0) {
+    err(1, "clock_gettime failed");
+  }
 
   auto now_tp = boot_clock::time_point(std::chrono::seconds(now.tv_sec) +
                                        std::chrono::nanoseconds(now.tv_nsec));
@@ -41,6 +45,7 @@
       std::chrono::duration_cast<std::chrono::seconds>(boot_clock::now().time_since_epoch());
   EXPECT_EQ(now, boot_seconds);
 }
+#endif  // defined(__linux__)
 
 }  // namespace base
-}  // namespace android
\ No newline at end of file
+}  // namespace android
diff --git a/base/include/android-base/chrono_utils.h b/base/include/android-base/chrono_utils.h
index 0086425..795225b 100644
--- a/base/include/android-base/chrono_utils.h
+++ b/base/include/android-base/chrono_utils.h
@@ -22,6 +22,7 @@
 namespace android {
 namespace base {
 
+#if defined(__linux__)
 // A std::chrono clock based on CLOCK_BOOTTIME.
 class boot_clock {
  public:
@@ -30,6 +31,7 @@
 
   static time_point now();
 };
+#endif  // defined(__linux__)
 
 }  // namespace base
 }  // namespace android
diff --git a/libcutils/sched_policy.cpp b/libcutils/sched_policy.cpp
index 217733a..e29a844 100644
--- a/libcutils/sched_policy.cpp
+++ b/libcutils/sched_policy.cpp
@@ -343,16 +343,25 @@
 static void set_timerslack_ns(int tid, unsigned long long slack) {
     // v4.6+ kernels support the /proc/<tid>/timerslack_ns interface.
     // TODO: once we've backported this, log if the open(2) fails.
-    char buf[64];
-    snprintf(buf, sizeof(buf), "/proc/%d/timerslack_ns", tid);
-    int fd = open(buf, O_WRONLY | O_CLOEXEC);
-    if (fd != -1) {
-        int len = snprintf(buf, sizeof(buf), "%llu", slack);
-        if (write(fd, buf, len) != len) {
-            SLOGE("set_timerslack_ns write failed: %s\n", strerror(errno));
+    if (__sys_supports_timerslack) {
+        char buf[64];
+        snprintf(buf, sizeof(buf), "/proc/%d/timerslack_ns", tid);
+        int fd = open(buf, O_WRONLY | O_CLOEXEC);
+        if (fd != -1) {
+            int len = snprintf(buf, sizeof(buf), "%llu", slack);
+            if (write(fd, buf, len) != len) {
+                SLOGE("set_timerslack_ns write failed: %s\n", strerror(errno));
+            }
+            close(fd);
+            return;
         }
-        close(fd);
-        return;
+    }
+
+    // TODO: Remove when /proc/<tid>/timerslack_ns interface is backported.
+    if ((tid == 0) || (tid == gettid())) {
+        if (prctl(PR_SET_TIMERSLACK, slack) == -1) {
+            SLOGE("set_timerslack_ns prctl failed: %s\n", strerror(errno));
+        }
     }
 }
 
@@ -431,10 +440,7 @@
 
     }
 
-    if (__sys_supports_timerslack) {
-        set_timerslack_ns(tid, policy == SP_BACKGROUND ?
-                               TIMER_SLACK_BG : TIMER_SLACK_FG);
-    }
+    set_timerslack_ns(tid, policy == SP_BACKGROUND ? TIMER_SLACK_BG : TIMER_SLACK_FG);
 
     return 0;
 }