summaryrefslogtreecommitdiff
path: root/recipes/forte/forte-0.3.5/forte-0.3.5_timer.patch
blob: 2064cda581d73c3af2c660660757636d39045c61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
diff -Naur FORTE-0.3.5.orig/src/arch/pc/pctimeha.cpp FORTE-0.3.5/src/arch/pc/pctimeha.cpp
--- FORTE-0.3.5.orig/src/arch/pc/pctimeha.cpp	2009-01-13 16:17:18.000000000 +0000
+++ FORTE-0.3.5/src/arch/pc/pctimeha.cpp	2009-02-16 10:16:37.000000000 +0000
@@ -11,6 +11,7 @@
 #include "pctimeha.h"
 #include "../../core/devexec.h"
 #include <time.h>
+#include <sys/time.h>
 
 const TINT32 CPCTimerHandler::csm_nTicksPerSecond = 1000;
 
@@ -28,14 +29,34 @@
   disableHandler();
 }
 
+static inline unsigned long long GetTicks(struct timeval* stTmVal, TINT32 ticksPerSecond)
+{
+  unsigned long long ret = stTmVal->tv_sec * 1000LL;// sec -> ms
+  ret += stTmVal->tv_usec/1000;// + us -> ms
+  ret /= 1000/ticksPerSecond; // ms -> ticks
+  return ret;
+}
+
 bool CPCTimerHandler::run(){
   struct timespec stReq;
+  struct timeval stTmNow;
+  unsigned long long tickStart;
+  unsigned long long tickNow;
+  unsigned long long tickCalled = 0;
   stReq.tv_sec = 0;
-  stReq.tv_nsec = 1000000 / (csm_nTicksPerSecond / 1000);
+  stReq.tv_nsec = 1000000000 / (csm_nTicksPerSecond);
+
+  gettimeofday(&stTmNow, NULL);
+  tickNow = tickStart = GetTicks(&stTmNow, csm_nTicksPerSecond);
   
   while(m_bAlive){
-    nanosleep(&stReq, NULL);
+    if( (tickNow-tickStart) <= (tickCalled+1) ) {
+      nanosleep(&stReq, NULL);
+    }
     nextTick(); 
+    ++tickCalled;
+    gettimeofday(&stTmNow, NULL);
+    tickNow = GetTicks(&stTmNow, csm_nTicksPerSecond);
   } 
   return true;
 }