Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ae29339c authored by Josh Gao's avatar Josh Gao
Browse files

base: make boot_clock work on host linux, hide it on non-linux.

boot_clock was previously returning zero on any platform that doesn't
define __ANDROID__, including host bionic. Instead of returning a bogus
value, just hide it on non-Linux platforms.

Bug: http://b/37758947
Test: libbase_test32/64 on linux
Change-Id: I96e1d8b92dc44c6308408900cf0d27e1e7db5569
parent 263e1e95
Loading
Loading
Loading
Loading
+1 −4
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ cc_library {
    clang: true,
    host_supported: true,
    srcs: [
        "chrono_utils.cpp",
        "file.cpp",
        "logging.cpp",
        "parsenetaddress.cpp",
@@ -64,7 +65,6 @@ cc_library {
            srcs: [
                "errors_unix.cpp",
                "properties.cpp",
                "chrono_utils.cpp",
            ],
            cppflags: ["-Wexit-time-destructors"],
            sanitize: {
@@ -74,14 +74,12 @@ cc_library {
        },
        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 @@ cc_library {
        },
        linux: {
            srcs: [
                "chrono_utils.cpp",
                "errors_unix.cpp",
            ],
            cppflags: ["-Wexit-time-destructors"],
+2 −5
Original line number Diff line number Diff line
@@ -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
+7 −2
Original line number Diff line number Diff line
@@ -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 @@ TEST(ChronoUtilsTest, BootClockNowSeconds) {
      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
+2 −0
Original line number Diff line number Diff line
@@ -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 @@ class boot_clock {

  static time_point now();
};
#endif  // defined(__linux__)

}  // namespace base
}  // namespace android