Commit 4793dfa1 authored by zhujiashun's avatar zhujiashun

put clock_gettime into time.h in macos

parent 77180e84
......@@ -450,14 +450,8 @@ int pthread_fd_wait(int fd, unsigned events,
int diff_ms = -1;
if (abstime) {
timespec now;
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
now.tv_sec = mts.tv_sec;
now.tv_nsec = mts.tv_nsec;
#ifdef __MACH__
clock_gettime(CALENDAR_CLOCK, &now);
#else
clock_gettime(CLOCK_REALTIME, &now);
#endif
......
......@@ -36,16 +36,9 @@ int64_t monotonic_time_ns() {
// NOTE: Not inline to keep ABI-compatible with previous versions.
timespec now;
#ifdef __MACH__
// OS X does not have clock_gettime, use clock_get_time.
// The value returned is a monotonically increasing value according to
// https://opensource.apple.com/source/xnu/xnu-792.13.8/osfmk/man/clock_get_time.html
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
now.tv_sec = mts.tv_sec;
now.tv_nsec = mts.tv_nsec;
clock_gettime(CALENDAR_CLOCK, &now);
#else
clock_gettime(CLOCK_MONOTONIC, &now);
#endif
......
......@@ -20,14 +20,25 @@
#ifndef BUTIL_BAIDU_TIME_H
#define BUTIL_BAIDU_TIME_H
#include <time.h> // timespec, clock_gettime
#include <sys/time.h> // timeval, gettimeofday
#include <stdint.h> // int64_t, uint64_t
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
#include <time.h> // timespec, clock_gettime
#include <sys/time.h> // timeval, gettimeofday
#include <stdint.h> // int64_t, uint64_t
inline void clock_gettime(clock_id_t id, timespec* time) {
// clock_gettime is not available in MacOS, use clock_get_time instead
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), id, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
time->tv_sec = mts.tv_sec;
time->tv_nsec = mts.tv_nsec;
}
#endif
namespace butil {
......@@ -92,14 +103,8 @@ inline timespec seconds_from(timespec start_time, int64_t seconds) {
// --------------------------------------------------------------------
inline timespec nanoseconds_from_now(int64_t nanoseconds) {
timespec time;
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
time.tv_sec = mts.tv_sec;
time.tv_nsec = mts.tv_nsec;
#ifdef __MACH__
clock_gettime(CALENDAR_CLOCK, &time);
#else
clock_gettime(CLOCK_REALTIME, &time);
#endif
......@@ -120,14 +125,8 @@ inline timespec seconds_from_now(int64_t seconds) {
inline timespec timespec_from_now(const timespec& span) {
timespec time;
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
time.tv_sec = mts.tv_sec;
time.tv_nsec = mts.tv_nsec;
#ifdef __MACH__
clock_gettime(CALENDAR_CLOCK, &time);
#else
clock_gettime(CLOCK_REALTIME, &time);
#endif
......
......@@ -20,13 +20,7 @@ TEST(BaiduTimeTest, diff_between_gettimeofday_and_REALTIME) {
long t1 = butil::gettimeofday_us();
timespec time;
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
time.tv_sec = mts.tv_sec;
time.tv_nsec = mts.tv_nsec;
clock_gettime(CALENDAR_CLOCK, &time);
#else
clock_gettime(CLOCK_REALTIME, &time);
#endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment