summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/timer.cpp22
-rw-r--r--src/utils/timer.h6
2 files changed, 19 insertions, 9 deletions
diff --git a/src/utils/timer.cpp b/src/utils/timer.cpp
index b0a05bc5..a1d8bd46 100644
--- a/src/utils/timer.cpp
+++ b/src/utils/timer.cpp
@@ -19,9 +19,15 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-#include <time.h>
#include "timer.h"
+#include <time.h>
+#include <sys/time.h>
+
+#ifdef _WIN32
+#include "wingettimeofday.h"
+#endif
+
namespace utils
{
@@ -52,8 +58,18 @@ int Timer::poll()
int elapsed = 0;
if (active)
{
- elapsed = (getTimeInMillisec() - lastpulse) / interval;
- lastpulse += interval * elapsed;
+ uint64_t now = getTimeInMillisec();
+ if (now > lastpulse)
+ {
+ elapsed = (now - lastpulse) / interval;
+ lastpulse += interval * elapsed;
+ }
+ else
+ {
+ // Time has made a jump to the past. This should be a rare
+ // occurence, so just reset lastpulse to prevent problems.
+ lastpulse = now;
+ }
};
return elapsed;
}
diff --git a/src/utils/timer.h b/src/utils/timer.h
index 69ca634e..da3a1d07 100644
--- a/src/utils/timer.h
+++ b/src/utils/timer.h
@@ -22,8 +22,6 @@
#ifndef _TMWSERV_TIMER_H_
#define _TMWSERV_TIMER_H_
-#include <sys/time.h>
-
/* I need a 64-bit unsigned integer */
#ifdef _MSC_VER
typedef __uint64 uint64_t // when using MSVC use its internal type
@@ -31,10 +29,6 @@
#include <stdint.h> // on other compilers use the C99 official header
#endif
-#ifdef _WIN32
- #include "wingettimeofday.h"
-#endif
-
namespace utils
{