Commit 081b4648 authored by zhujiashun's avatar zhujiashun

Put Impl of get_clocktime into butil/time.cpp

parent ace912ba
......@@ -63,7 +63,15 @@ execute_process(
OUTPUT_VARIABLE BRPC_REVISION
)
set(CMAKE_CPP_FLAGS "-DBRPC_WITH_GLOG=${WITH_GLOG_VAL} -DGFLAGS_NS=${GFLAGS_NS}")
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
include(CheckFunctionExists)
CHECK_FUNCTION_EXISTS(clock_gettime HAVE_CLOCK_GETTIME)
if(NOT HAVE_CLOCK_GETTIME)
set(DEFINE_CLOCK_GETTIME "-DNO_CLOCK_GETTIME_IN_MAC")
endif()
endif()
set(CMAKE_CPP_FLAGS "${DEFINE_CLOCK_GETTIME} -DBRPC_WITH_GLOG=${WITH_GLOG_VAL} -DGFLAGS_NS=${GFLAGS_NS}")
set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} -DBTHREAD_USE_FAST_PTHREAD_MUTEX -D__const__= -D_GNU_SOURCE -DUSE_SYMBOLIZE -DNO_TCMALLOC -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DBRPC_REVISION=\\\"${BRPC_REVISION}\\\" -D__STRICT_ANSI__")
set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} ${DEBUG_SYMBOL}")
set(CMAKE_CXX_FLAGS "${CMAKE_CPP_FLAGS} -O2 -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-invalid-offsetof -Wno-unused-parameter -fno-omit-frame-pointer")
......
......@@ -28,6 +28,46 @@
#include "butil/time.h"
#if defined(NO_CLOCK_GETTIME_IN_MAC)
#include <mach/clock.h> // mach_absolute_time
#include <mach/mach_time.h> // mach_timebase_info
#include <pthread.h> // pthread_once
#include <stdlib.h> // exit
static mach_timebase_info_data_t s_timebase;
static timespec s_init_time;
static uint64_t s_init_ticks;
static pthread_once_t s_init_clock_once = PTHREAD_ONCE_INIT;
static void InitClock() {
if (mach_timebase_info(&s_timebase) != 0) {
exit(1);
}
timeval now;
if (gettimeofday(&now, NULL) != 0) {
exit(1);
}
s_init_time.tv_sec = now.tv_sec;
s_init_time.tv_nsec = now.tv_usec * 1000L;
s_init_ticks = mach_absolute_time();
}
int clock_gettime(clockid_t id, timespec* time) {
if (pthread_once(&s_init_clock_once, InitClock) != 0) {
exit(1);
}
uint64_t clock = mach_absolute_time() - s_init_ticks;
uint64_t elapsed = clock * (uint64_t)s_timebase.numer / (uint64_t)s_timebase.denom;
*time = s_init_time;
time->tv_sec += elapsed / 1000000000L;
time->tv_nsec += elapsed % 1000000000L;
time->tv_sec += time->tv_nsec / 1000000000L;
time->tv_nsec = time->tv_nsec % 1000000000L;
return 0;
}
#endif
namespace butil {
int64_t monotonic_time_ns() {
......
......@@ -24,53 +24,16 @@
#include <sys/time.h> // timeval, gettimeofday
#include <stdint.h> // int64_t, uint64_t
#ifdef __MACH__
#include <mach/clock.h>
#if defined(NO_CLOCK_GETTIME_IN_MAC)
#include <mach/mach.h>
#include <mach/mach_time.h>
# ifndef clock_gettime
# define CLOCK_REALTIME CALENDAR_CLOCK
# define CLOCK_MONOTONIC SYSTEM_CLOCK
#include <pthread.h>
#include <stdlib.h> // exit
typedef int clockid_t;
static mach_timebase_info_data_t timebase;
static timespec inittime;
static uint64_t initticks;
static pthread_once_t init_clock_once = PTHREAD_ONCE_INIT;
static void InitClock() {
if (mach_timebase_info(&timebase) != 0) {
exit(1);
}
timeval micro;
if (gettimeofday(&micro, NULL) != 0) {
exit(1);
}
inittime.tv_sec = micro.tv_sec;
inittime.tv_nsec = micro.tv_usec * 1000L;
initticks = mach_absolute_time();
}
// clock_gettime is not available in MacOS < 10.12
inline int clock_gettime(clockid_t id, timespec* time) {
if (pthread_once(&init_clock_once, InitClock) != 0) {
exit(1);
}
uint64_t clock = mach_absolute_time() - initticks;
uint64_t elapsed = clock * (uint64_t)timebase.numer / (uint64_t)timebase.denom;
*time = inittime;
time->tv_sec += elapsed / 1000000000L;
time->tv_nsec += elapsed % 1000000000L;
time->tv_sec += time->tv_nsec / 1000000000L;
time->tv_nsec = time->tv_nsec % 1000000000L;
return 0;
}
# endif
int clock_gettime(clockid_t id, timespec* time);
#endif
namespace butil {
......
......@@ -33,7 +33,7 @@ else()
message(FATAL_ERROR "Googletest is not available")
endif()
set(CMAKE_CPP_FLAGS "-DBRPC_WITH_GLOG=${WITH_GLOG_VAL} -DGFLAGS_NS=${GFLAGS_NS}")
set(CMAKE_CPP_FLAGS "${DEFINE_CLOCK_GETTIME} -DBRPC_WITH_GLOG=${WITH_GLOG_VAL} -DGFLAGS_NS=${GFLAGS_NS}")
set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} -DBTHREAD_USE_FAST_PTHREAD_MUTEX -D__const__= -D_GNU_SOURCE -DUSE_SYMBOLIZE -DNO_TCMALLOC -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DUNIT_TEST -Dprivate=public -Dprotected=public -DBVAR_NOT_LINK_DEFAULT_VARIABLES -D__STRICT_ANSI__ -include ${CMAKE_SOURCE_DIR}/test/sstream_workaround.h")
set(CMAKE_CXX_FLAGS "${CMAKE_CPP_FLAGS} -O2 -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-invalid-offsetof -Wno-unused-parameter -fno-omit-frame-pointer")
use_cxx11()
......
......@@ -295,7 +295,7 @@ TEST(ButexTest, join_cant_be_wakeup) {
ASSERT_EQ(0, bthread_join(th2, NULL));
ASSERT_EQ(0, bthread_join(th, NULL));
tm.stop();
ASSERT_LT(tm.m_elapsed(), WAIT_MSEC + 10);
ASSERT_LT(tm.m_elapsed(), WAIT_MSEC + 15);
ASSERT_EQ(EINVAL, bthread_stop(th));
ASSERT_EQ(EINVAL, bthread_stop(th2));
}
......
set(CMAKE_CPP_FLAGS "-DGFLAGS_NS=${GFLAGS_NS}")
set(CMAKE_CPP_FLAGS "${DEFINE_CLOCK_GETTIME} -DGFLAGS_NS=${GFLAGS_NS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CPP_FLAGS} -DNDEBUG -O2 -D__const__= -D__STRICT_ANSI__ -pipe -W -Wall -Wno-unused-parameter -fPIC -fno-omit-frame-pointer")
use_cxx11()
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/output/bin)
......
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