/*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
static void duration_to_ts(
std::chrono::duration<_Rep, _Period> d,
struct timespec* ts) {
- ts->tv_sec = std::chrono::duration_cast<std::chrono::seconds>(d).count();
- ts->tv_nsec = std::chrono::duration_cast<std::chrono::nanoseconds>(
- d % std::chrono::seconds(1))
- .count();
+ ts->tv_sec =
+ time_t(std::chrono::duration_cast<std::chrono::seconds>(d).count());
+ ts->tv_nsec = long(std::chrono::duration_cast<std::chrono::nanoseconds>(
+ d % std::chrono::seconds(1))
+ .count());
}
-#if !FOLLY_HAVE_CLOCK_GETTIME
+#if !FOLLY_HAVE_CLOCK_GETTIME || FOLLY_FORCE_CLOCK_GETTIME_DEFINITION
#if __MACH__
#include <errno.h>
#include <mach/mach_init.h>
static int clock_process_cputime(struct timespec* ts) {
// Get CPU usage for live threads.
task_thread_times_info thread_times_info;
- mach_msg_type_number_t thread_times_info_count;
- TASK_THREAD_TIMES_INFO_COUNT;
+ mach_msg_type_number_t thread_times_info_count = TASK_THREAD_TIMES_INFO_COUNT;
kern_return_t kern_result = task_info(
mach_task_self(),
TASK_THREAD_TIMES_INFO,
return -1;
}
+ static constexpr size_t kNsPerSec = 1000000000;
switch (clock_id) {
+ case CLOCK_REALTIME: {
+ constexpr auto perSec = double(std::chrono::system_clock::period::num) /
+ std::chrono::system_clock::period::den;
+ res->tv_sec = time_t(perSec);
+ res->tv_nsec = time_t(perSec * kNsPerSec);
+ return 0;
+ }
case CLOCK_MONOTONIC: {
- LARGE_INTEGER freq = performanceFrequency();
- if (freq.QuadPart == -1) {
- errno = EINVAL;
- return -1;
- }
-
- static constexpr size_t kNsPerSec = 1000000000;
-
- res->tv_sec = 0;
- res->tv_nsec = (long)((kNsPerSec + (freq.QuadPart >> 1)) / freq.QuadPart);
- if (res->tv_nsec < 1) {
- res->tv_nsec = 1;
- }
-
+ constexpr auto perSec = double(std::chrono::steady_clock::period::num) /
+ std::chrono::steady_clock::period::den;
+ res->tv_sec = time_t(perSec);
+ res->tv_nsec = time_t(perSec * kNsPerSec);
return 0;
}
-
- case CLOCK_REALTIME:
case CLOCK_PROCESS_CPUTIME_ID:
case CLOCK_THREAD_CPUTIME_ID: {
DWORD adj, timeIncrement;
}
res->tv_sec = 0;
- res->tv_nsec = timeIncrement * 100;
+ res->tv_nsec = long(timeIncrement * 100);
return 0;
}
}
const auto unanosToTimespec = [](timespec* tp, unsigned_nanos t) -> int {
- static constexpr unsigned_nanos one_sec(std::chrono::seconds(1));
- tp->tv_sec = std::chrono::duration_cast<std::chrono::seconds>(t).count();
- tp->tv_nsec = (t % one_sec).count();
+ static constexpr unsigned_nanos one_sec{std::chrono::seconds(1)};
+ tp->tv_sec =
+ time_t(std::chrono::duration_cast<std::chrono::seconds>(t).count());
+ tp->tv_nsec = long((t % one_sec).count());
return 0;
};
int nanosleep(const struct timespec* request, struct timespec* remain) {
Sleep((DWORD)((request->tv_sec * 1000) + (request->tv_nsec / 1000000)));
- remain->tv_nsec = 0;
- remain->tv_sec = 0;
+ if (remain != nullptr) {
+ remain->tv_nsec = 0;
+ remain->tv_sec = 0;
+ }
return 0;
}
-char* strptime(const char* __restrict s,
- const char* __restrict f,
- struct tm* __restrict tm) {
+char* strptime(
+ const char* __restrict s,
+ const char* __restrict f,
+ struct tm* __restrict tm) {
// Isn't the C++ standard lib nice? std::get_time is defined such that its
// format parameters are the exact same as strptime. Of course, we have to
// create a string stream first, and imbue it with the current C locale, and