Skip to content

Commit

Permalink
Merge pull request #35 from mika-fischer/libzmq-290
Browse files Browse the repository at this point in the history
Use monotonic clock for clock_t
  • Loading branch information
hintjens committed Dec 5, 2011
2 parents 04b2ef2 + 1f1b454 commit 354faeb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions configure.in
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ LIBZMQ_CHECK_DOC_BUILD
# Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS(errno.h arpa/inet.h netinet/tcp.h netinet/in.h stddef.h \
stdlib.h string.h sys/socket.h sys/time.h unistd.h limits.h)
stdlib.h string.h sys/socket.h sys/time.h time.h unistd.h limits.h)

# Check if we have ifaddrs.h header file.
AC_CHECK_HEADERS(ifaddrs.h, [AC_DEFINE(ZMQ_HAVE_IFADDRS, 1, [Have ifaddrs.h header.])])
Expand Down Expand Up @@ -376,7 +376,7 @@ AM_CONDITIONAL(ON_MINGW, test "x$libzmq_on_mingw32" = "xyes")

# Checks for library functions.
AC_TYPE_SIGNAL
AC_CHECK_FUNCS(perror gettimeofday memset socket getifaddrs freeifaddrs)
AC_CHECK_FUNCS(perror gettimeofday clock_gettime memset socket getifaddrs freeifaddrs)
AC_CHECK_HEADERS([alloca.h])
LIBZMQ_CHECK_SOCK_CLOEXEC([AC_DEFINE(
[HAVE_SOCK_CLOEXEC],
Expand Down
12 changes: 12 additions & 0 deletions src/clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
#include <sys/time.h>
#endif

#if defined HAVE_CLOCK_GETTIME
#include <time.h>
#endif

zmq::clock_t::clock_t () :
last_tsc (rdtsc ()),
last_time (now_us () / 1000)
Expand Down Expand Up @@ -61,6 +65,14 @@ uint64_t zmq::clock_t::now_us ()
double ticks_div = (double) (ticksPerSecond.QuadPart / 1000000);
return (uint64_t) (tick.QuadPart / ticks_div);

#elif defined HAVE_CLOCK_GETTIME

// Use POSIX clock_gettime function to get precise monotonic time.
struct timespec tv;
int rc = clock_gettime (CLOCK_MONOTONIC, &tv);
errno_assert (rc == 0);
return (tv.tv_sec * (uint64_t) 1000000 + tv.tv_nsec / 1000);

#else

// Use POSIX gettimeofday function to get precise time.
Expand Down

0 comments on commit 354faeb

Please sign in to comment.