Commit 23d6ecc7 authored by zhujiashun's avatar zhujiashun

use gettimeofday implement realtime

parent 22e14ab6
...@@ -33,15 +33,24 @@ ...@@ -33,15 +33,24 @@
# define CLOCK_MONOTONIC SYSTEM_CLOCK # define CLOCK_MONOTONIC SYSTEM_CLOCK
typedef int clockid_t; typedef int clockid_t;
// clock_gettime is not available in MacOS < 10.12
inline int clock_gettime(clockid_t id, timespec* time) { inline int clock_gettime(clockid_t id, timespec* time) {
// clock_gettime is not available in MacOS, use clock_get_time instead if (id == CLOCK_MONOTONIC) {
clock_serv_t cclock; clock_serv_t cclock;
mach_timespec_t mts; mach_timespec_t mts;
host_get_clock_service(mach_host_self(), id, &cclock); host_get_clock_service(mach_host_self(), id, &cclock);
clock_get_time(cclock, &mts); clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock); mach_port_deallocate(mach_task_self(), cclock);
time->tv_sec = mts.tv_sec; time->tv_sec = mts.tv_sec;
time->tv_nsec = mts.tv_nsec; time->tv_nsec = mts.tv_nsec;
} else if (id == CLOCK_REALTIME) {
struct timeval now;
if (gettimeofday(&now, NULL) < 0) {
return -1;
}
time->tv_sec = now.tv_sec;
time->tv_nsec = now.tv_usec * 1000;
}
return 0; return 0;
} }
# endif # endif
......
...@@ -19,11 +19,7 @@ namespace { ...@@ -19,11 +19,7 @@ namespace {
TEST(BaiduTimeTest, diff_between_gettimeofday_and_REALTIME) { TEST(BaiduTimeTest, diff_between_gettimeofday_and_REALTIME) {
long t1 = butil::gettimeofday_us(); long t1 = butil::gettimeofday_us();
timespec time; timespec time;
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_gettime(CALENDAR_CLOCK, &time);
#else
clock_gettime(CLOCK_REALTIME, &time); clock_gettime(CLOCK_REALTIME, &time);
#endif
long t2 = butil::timespec_to_microseconds(time); long t2 = butil::timespec_to_microseconds(time);
LOG(INFO) << "t1=" << t1 << " t2=" << t2; LOG(INFO) << "t1=" << t1 << " t2=" << t2;
} }
...@@ -104,7 +100,6 @@ TEST(BaiduTimeTest, cost_of_timer) { ...@@ -104,7 +100,6 @@ TEST(BaiduTimeTest, cost_of_timer) {
clock_desc[i], t1.n_elapsed() / N); clock_desc[i], t1.n_elapsed() / N);
} }
#endif #endif
#if !defined(OS_MACOSX)
if (0 == clock_gettime((clockid_t)i, &ts)) { if (0 == clock_gettime((clockid_t)i, &ts)) {
t1.start(); t1.start();
for (size_t j = 0; j < N; ++j) { for (size_t j = 0; j < N; ++j) {
...@@ -114,7 +109,6 @@ TEST(BaiduTimeTest, cost_of_timer) { ...@@ -114,7 +109,6 @@ TEST(BaiduTimeTest, cost_of_timer) {
printf("glibc clock_gettime(%s) takes %" PRId64 "ns\n", printf("glibc clock_gettime(%s) takes %" PRId64 "ns\n",
clock_desc[i], t1.n_elapsed() / N); clock_desc[i], t1.n_elapsed() / N);
} }
#endif
} }
} }
......
...@@ -25,11 +25,7 @@ TEST(ButexTest, wait_on_already_timedout_butex) { ...@@ -25,11 +25,7 @@ TEST(ButexTest, wait_on_already_timedout_butex) {
uint32_t* butex = bthread::butex_create_checked<uint32_t>(); uint32_t* butex = bthread::butex_create_checked<uint32_t>();
ASSERT_TRUE(butex); ASSERT_TRUE(butex);
timespec now; timespec now;
#if defined(OS_MACOSX)
ASSERT_EQ(0, clock_gettime(CALENDAR_CLOCK, &now));
#else
ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &now)); ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &now));
#endif
*butex = 1; *butex = 1;
ASSERT_EQ(-1, bthread::butex_wait(butex, 1, &now)); ASSERT_EQ(-1, bthread::butex_wait(butex, 1, &now));
ASSERT_EQ(ETIMEDOUT, errno); ASSERT_EQ(ETIMEDOUT, errno);
......
...@@ -171,11 +171,7 @@ public: ...@@ -171,11 +171,7 @@ public:
void run() void run()
{ {
#if defined(OS_MACOSX)
clock_gettime(CALENDAR_CLOCK, &_running_time);
#else
clock_gettime(CLOCK_REALTIME, &_running_time); clock_gettime(CLOCK_REALTIME, &_running_time);
#endif
EXPECT_EQ(_expected_unschedule_result, EXPECT_EQ(_expected_unschedule_result,
_timer_thread->unschedule(_keeper1->_task_id)); _timer_thread->unschedule(_keeper1->_task_id));
_keeper2->schedule(_timer_thread); _keeper2->schedule(_timer_thread);
...@@ -235,11 +231,7 @@ TEST(TimerThreadTest, schedule_and_unschedule_in_task) { ...@@ -235,11 +231,7 @@ TEST(TimerThreadTest, schedule_and_unschedule_in_task) {
timer_thread.stop_and_join(); timer_thread.stop_and_join();
timespec finish_time; timespec finish_time;
#if defined(OS_MACOSX)
clock_gettime(CALENDAR_CLOCK, &finish_time);
#else
clock_gettime(CLOCK_REALTIME, &finish_time); clock_gettime(CLOCK_REALTIME, &finish_time);
#endif
keeper1.expect_not_run(); keeper1.expect_not_run();
keeper2.expect_first_run(test_task1._running_time); keeper2.expect_first_run(test_task1._running_time);
......
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