1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-23 08:13:27 +00:00

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.
This commit is contained in:
Juliusz Chroboczek 2011-03-29 23:15:38 +00:00
parent 3043893968
commit 74c0721a2c

View file

@ -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 <mach/mach_time.h>
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;