2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
28 * This function returns the system clock's number of milliseconds but with
29 * an arbitrary reference point. It handles the wrapping of any underlying
30 * system clock by setting a starting point at the first call. It should
31 * only be used for measuring time durations.
33 * Of course, on windows it just calls timeGetTime, so you're on your own.
35 unsigned int SystemClockMillis();
38 * DO NOT compare the results from SystemClockMillis() to an expected end time
39 * that was calculated by adding a number of milliseconds to some start time.
40 * The reason is because the SystemClockMillis could wrap. Instead use this
41 * class which uses differences (which are safe accross a wrap).
45 unsigned int startTime;
46 unsigned int totalWaitTime;
48 static const unsigned int InfiniteValue;
49 inline EndTime() : startTime(0), totalWaitTime(0) {}
50 inline EndTime(unsigned int millisecondsIntoTheFuture) : startTime(SystemClockMillis()), totalWaitTime(millisecondsIntoTheFuture) {}
52 inline void Set(unsigned int millisecondsIntoTheFuture) { startTime = SystemClockMillis(); totalWaitTime = millisecondsIntoTheFuture; }
54 inline bool IsTimePast() const { return totalWaitTime == InfiniteValue ? false : (totalWaitTime == 0 ? true : (SystemClockMillis() - startTime) >= totalWaitTime); }
56 inline unsigned int MillisLeft() const
58 if (totalWaitTime == InfiniteValue)
60 if (totalWaitTime == 0)
62 unsigned int timeWaitedAlready = (SystemClockMillis() - startTime);
63 return (timeWaitedAlready >= totalWaitTime) ? 0 : (totalWaitTime - timeWaitedAlready);
66 inline void SetExpired() { totalWaitTime = 0; }
67 inline void SetInfinite() { totalWaitTime = InfiniteValue; }
68 inline bool IsInfinite(void) const { return (totalWaitTime == InfiniteValue); }
69 inline unsigned int GetInitialTimeoutValue(void) const { return totalWaitTime; }
70 inline unsigned int GetStartTime(void) const { return startTime; }