From e9e88ad9f2490782d0563fe666d16b4dd1b85e9a Mon Sep 17 00:00:00 2001 From: CismonX Date: Fri, 8 May 2020 09:27:03 +0800 Subject: [PATCH] Fix gethrtime for Solaris. --- base/htime.c | 6 +++--- base/htime.h | 5 +---- docs/apis.md | 2 +- event/hloop.c | 6 +++--- protocol/icmp.c | 4 ++-- 5 files changed, 10 insertions(+), 13 deletions(-) diff --git a/base/htime.c b/base/htime.c index 8b94c757e..2b993673e 100644 --- a/base/htime.c +++ b/base/htime.c @@ -41,8 +41,7 @@ unsigned int gettick() { #endif } -#ifndef OS_SOLARIS -unsigned long long gethrtime() { +unsigned long long gethrtime_usec() { #ifdef OS_WIN static LONGLONG s_freq = 0; if (s_freq == 0) { @@ -56,6 +55,8 @@ unsigned long long gethrtime() { return (unsigned long long)(count.QuadPart / (double)s_freq * 1000000); } return 0; +#elif defined(OS_SOLARIS) + return gethrtime() / 1000; #elif HAVE_CLOCK_GETTIME struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); @@ -66,7 +67,6 @@ unsigned long long gethrtime() { return tv.tv_sec*(unsigned long long)1000000 + tv.tv_usec; #endif } -#endif datetime_t datetime_now() { datetime_t dt; diff --git a/base/htime.h b/base/htime.h index 34ea206f1..b1a9ec1f8 100644 --- a/base/htime.h +++ b/base/htime.h @@ -72,11 +72,8 @@ static inline unsigned long long timestamp_ms() { void msleep(unsigned int ms); // ms unsigned int gettick(); - -#ifndef OS_SOLARIS // Solaris has built-in gethrtime(). // us -unsigned long long gethrtime(); -#endif +unsigned long long gethrtime_usec(); datetime_t datetime_now(); time_t datetime_mktime(datetime_t* dt); diff --git a/docs/apis.md b/docs/apis.md index 7ac749f5b..d45ab3555 100644 --- a/docs/apis.md +++ b/docs/apis.md @@ -34,7 +34,7 @@ - datetime_fmt - gmtime_fmt - gettick -- gethrtime +- gethrtime_usec - sleep - msleep - usleep diff --git a/event/hloop.c b/event/hloop.c index 6129cfb05..641c95fbf 100644 --- a/event/hloop.c +++ b/event/hloop.c @@ -175,7 +175,7 @@ static void hloop_init(hloop_t* loop) { hmutex_init(&loop->custom_events_mutex); // NOTE: init start_time here, because htimer_add use it. loop->start_ms = timestamp_ms(); - loop->start_hrtime = loop->cur_hrtime = gethrtime(); + loop->start_hrtime = loop->cur_hrtime = gethrtime_usec(); } static void hloop_cleanup(hloop_t* loop) { @@ -268,7 +268,7 @@ int hloop_run(hloop_t* loop) { } } loop->status = HLOOP_STATUS_STOP; - loop->end_hrtime = gethrtime(); + loop->end_hrtime = gethrtime_usec(); if (loop->flags & HLOOP_FLAG_AUTO_FREE) { hloop_cleanup(loop); SAFE_FREE(loop); @@ -296,7 +296,7 @@ int hloop_resume(hloop_t* loop) { } void hloop_update_time(hloop_t* loop) { - loop->cur_hrtime = gethrtime(); + loop->cur_hrtime = gethrtime_usec(); if (ABS((int64_t)hloop_now(loop) - (int64_t)time(NULL)) > 1) { // systemtime changed, we adjust start_ms loop->start_ms = timestamp_ms() - (loop->cur_hrtime - loop->start_hrtime) / 1000; diff --git a/protocol/icmp.c b/protocol/icmp.c index ee8467248..f2df49588 100644 --- a/protocol/icmp.c +++ b/protocol/icmp.c @@ -67,7 +67,7 @@ int ping(const char* host, int cnt) { icmp_req->icmp_seq = ++seq; icmp_req->icmp_cksum = 0; icmp_req->icmp_cksum = checksum((uint8_t*)icmp_req, sendbytes); - start_hrtime = gethrtime(); + start_hrtime = gethrtime_usec(); addrlen = sockaddrlen(&peeraddr); int nsend = sendto(sockfd, sendbuf, sendbytes, 0, &peeraddr.sa, addrlen); if (nsend < 0) { @@ -82,7 +82,7 @@ int ping(const char* host, int cnt) { continue; } ++recv_cnt; - end_hrtime = gethrtime(); + end_hrtime = gethrtime_usec(); // check valid bool valid = false; int iphdr_len = ipheader->ihl * 4;