From 5f6c265e4baa7b7ada085088e626de80c6155723 Mon Sep 17 00:00:00 2001 From: Daniel Wang Date: Tue, 17 Jul 2018 14:37:41 -0400 Subject: [PATCH] Moved current_utc_time to tlUtils and fixed tlTimer --- src/tl/tl/tlThreads.cc | 25 +------------------------ src/tl/tl/tlTimer.cc | 6 +++--- src/tl/tl/tlUtils.h | 27 +++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/tl/tl/tlThreads.cc b/src/tl/tl/tlThreads.cc index 70aa11f6c..239dcaa5f 100644 --- a/src/tl/tl/tlThreads.cc +++ b/src/tl/tl/tlThreads.cc @@ -23,44 +23,21 @@ #if !defined(HAVE_QT) #include "tlThreads.h" +#include "tlUtils.h" #include "tlLog.h" #include "tlInternational.h" #include #include -#include #include #include #if defined(_WIN32) # include #endif -#if defined(__MACH__) -#include -#include -#endif namespace tl { -// ------------------------------------------------------------------------------- -// clock_gettime is not implemented in Mac OS X 10.11 and lower -// From: https://gist.githubusercontent.com/jbenet/1087739/raw/638b37f76cdd9dc46d617443cab27eac297e2ee3/current_utc_time.c - -void current_utc_time(struct timespec *ts) { - -#if defined(__MACH__) - 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); - ts->tv_sec = mts.tv_sec; - ts->tv_nsec = mts.tv_nsec; -#else - clock_gettime(CLOCK_REALTIME, ts); -#endif - -} // ------------------------------------------------------------------------------- // WaitCondition implementation diff --git a/src/tl/tl/tlTimer.cc b/src/tl/tl/tlTimer.cc index de3181891..c8058f30e 100644 --- a/src/tl/tl/tlTimer.cc +++ b/src/tl/tl/tlTimer.cc @@ -22,6 +22,7 @@ #include "tlTimer.h" +#include "tlUtils.h" #include "tlLog.h" #include "tlString.h" @@ -30,7 +31,6 @@ #endif #include -#include #ifndef _MSC_VER // not available on MS VC++ # include @@ -45,7 +45,7 @@ namespace tl static int64_t ms_time () { struct timespec spec; - clock_gettime (CLOCK_REALTIME, &spec); + current_utc_time (&spec); return int64_t (spec.tv_sec) * 1000 + int64_t (0.5 + spec.tv_nsec / 1.0e6); } @@ -77,7 +77,7 @@ Timer::start () #endif struct timespec spec; - clock_gettime (CLOCK_REALTIME, &spec); + current_utc_time (&spec); m_wall_ms += ms_time (); } diff --git a/src/tl/tl/tlUtils.h b/src/tl/tl/tlUtils.h index 84bdf5a5f..498fd4fd1 100644 --- a/src/tl/tl/tlUtils.h +++ b/src/tl/tl/tlUtils.h @@ -27,10 +27,37 @@ #include "tlAssert.h" #include +#include + +#if defined(__MACH__) +#include +#include +#endif namespace tl { +/** +* @brief clock_gettime is not implemented in Mac OS X 10.11 and lower +* From: https://gist.githubusercontent.com/jbenet/1087739/raw/638b37f76cdd9dc46d617443cab27eac297e2ee3/current_utc_time.c +*/ + +inline void current_utc_time(struct timespec *ts) { + +#if defined(__MACH__) + 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); + ts->tv_sec = mts.tv_sec; + ts->tv_nsec = mts.tv_nsec; +#else + clock_gettime(CLOCK_REALTIME, ts); +#endif + +} + /** * @brief A template class mapping a begin .. end iterator pair to the at_end semantics */