From 74c0721a2c467369e02a23c021707b30a5ae15c5 Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Tue, 29 Mar 2011 23:15:38 +0000 Subject: [PATCH] Protect against monotonic time being non-monotonic. Within utp.cpp, there's a bunch of assertions that will cause us to crash if monotonic time isn't. While I have no idea about Windows, under Unix we use real time instead of monotonic time if POSIX clocks are not available, and real time can be stepped backwards. Since the protection is cheap, we perform it on all platforms. --- third-party/libutp/utp_utils.cpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/third-party/libutp/utp_utils.cpp b/third-party/libutp/utp_utils.cpp index 235303ad9..93a8cebe4 100644 --- a/third-party/libutp/utp_utils.cpp +++ b/third-party/libutp/utp_utils.cpp @@ -54,7 +54,7 @@ void Time_Initialize() int64 abs64(int64 x) { return x < 0 ? -x : x; } -uint64 UTP_GetMicroseconds() +static uint64 GetMicroseconds() { static bool time_init = false; if (!time_init) { @@ -94,7 +94,7 @@ uint64 UTP_GetMicroseconds() #if defined(__APPLE__) #include -uint64 UTP_GetMicroseconds() +static uint64 GetMicroseconds() { // http://developer.apple.com/mac/library/qa/qa2004/qa1398.html // http://www.macresearch.org/tutorial_performance_and_time @@ -118,7 +118,7 @@ uint64 UTP_GetMicroseconds() POSIX clocks work -- we could be running a recent libc with an ancient kernel (think OpenWRT). -- jch */ -uint64 UTP_GetMicroseconds() +static uint64_t GetMicroseconds() { static int have_posix_clocks = -1; int rc; @@ -155,6 +155,22 @@ uint64 UTP_GetMicroseconds() } #endif +uint64 UTP_GetMicroseconds() +{ + static bool valid = false; + static uint64 last_time; + + uint64 now = GetMicroseconds(); + if (valid) { + if (last_time > now) + /* Eek! */ + now = last_time; + } + last_time = now; + valid = true; + return now; +} + uint32 UTP_GetMilliseconds() { return UTP_GetMicroseconds() / 1000;