X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Flib%2Fplatform%2Futil%2Ftimeutils.h;h=e95c10df1ff44c3074a7f35d35f141531ad160b7;hb=95587b956c69ead0b46d301f5ca70e977890c3fd;hp=12c2145844489ab3ab84f4f84af30814d81fbc7c;hpb=ba65909d0a9c43a1bac71c6182c53f202285cec5;p=deb_libcec.git diff --git a/src/lib/platform/util/timeutils.h b/src/lib/platform/util/timeutils.h index 12c2145..e95c10d 100644 --- a/src/lib/platform/util/timeutils.h +++ b/src/lib/platform/util/timeutils.h @@ -2,7 +2,7 @@ /* * This file is part of the libCEC(R) library. * - * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved. + * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved. * libCEC(R) is an original work, containing original code. * * libCEC(R) is a trademark of Pulse-Eight Limited. @@ -31,7 +31,8 @@ * http://www.pulse-eight.net/ */ -#include "../os.h" +#include "env.h" +#include "lib/platform/os.h" #if defined(__APPLE__) #include @@ -39,6 +40,7 @@ #elif defined(__WINDOWS__) #include #else +#include #include #endif @@ -78,23 +80,20 @@ namespace PLATFORM inline int64_t GetTimeMs() { #if defined(__APPLE__) - return (int64_t) (CVGetCurrentHostTime() * 1000 / CVGetHostClockFrequency()); + return (int64_t) (CVGetCurrentHostTime() / (int64_t)(CVGetHostClockFrequency() * 0.001)); #elif defined(__WINDOWS__) - time_t rawtime; - time(&rawtime); - LARGE_INTEGER tickPerSecond; LARGE_INTEGER tick; if (QueryPerformanceFrequency(&tickPerSecond)) { QueryPerformanceCounter(&tick); - return (int64_t) (tick.QuadPart / 1000.); + return (int64_t) (tick.QuadPart / (tickPerSecond.QuadPart / 1000.)); } return -1; #else - timeval time; - gettimeofday(&time, NULL); - return (int64_t) (time.tv_sec * 1000 + time.tv_usec / 1000); + timespec time; + clock_gettime(CLOCK_MONOTONIC, &time); + return (int64_t)time.tv_sec * 1000 + time.tv_nsec / 1000000; #endif } @@ -103,4 +102,23 @@ namespace PLATFORM { return (T)GetTimeMs() / (T)1000.0; } + + class CTimeout + { + public: + CTimeout(void) : m_iTarget(0) {} + CTimeout(uint32_t iTimeout) { Init(iTimeout); } + + bool IsSet(void) const { return m_iTarget > 0; } + void Init(uint32_t iTimeout) { m_iTarget = GetTimeMs() + iTimeout; } + + uint32_t TimeLeft(void) const + { + uint64_t iNow = GetTimeMs(); + return (iNow > m_iTarget) ? 0 : (uint32_t)(m_iTarget - iNow); + } + + private: + uint64_t m_iTarget; + }; };