Hi all,

A series of patch for using monotonic time for pthread timeout and 
direct_clock_get_micros.
To avoid the problem when system date/time is changed.

1 of 8 ) Add an interface direct_monotonic_gettimeofday for get a 
monotonic time and direct_clock_get_micros will call it.

Regards,


diff --git a/lib/direct/clock.h b/lib/direct/clock.h
index 1bf5ea2..3712632 100644
--- a/lib/direct/clock.h
+++ b/lib/direct/clock.h
@@ -31,6 +31,8 @@

 #include <sys/time.h>

+struct timeval* direct_monotonic_gettimeofday( struct timeval *tv );
+
 long long direct_clock_get_micros( void );
 long long direct_clock_get_millis( void );

diff --git a/lib/direct/clock.c b/lib/direct/clock.c
index 10fdd0d..d82c497 100644
--- a/lib/direct/clock.c
+++ b/lib/direct/clock.c
@@ -32,6 +32,7 @@
 #include <direct/debug.h>
 #include <direct/util.h>

+#include <sys/syscall.h>
 #include <sys/time.h>
 #include <time.h>

@@ -39,6 +40,32 @@ D_DEBUG_DOMAIN( Direct_Clock, "Direct/Clock", "Time 
measurement etc." );

 static struct timeval start_time = { 0, 0 };

+/* libc has incredibly messy way of doing this,
+ * typically requiring -lrt. We just skip all this mess */
+struct timeval*
+direct_monotonic_gettimeofday( struct timeval *tv )
+{
+     struct timespec ts;
+
+#ifdef CLOCK_MONOTONIC
+     /* No locking or atomic ops for no_monotonic here */
+     static int no_monotonic = 0;
+
+     if (!no_monotonic)
+          if(syscall( __NR_clock_gettime, CLOCK_MONOTONIC, &ts ))
+               no_monotonic = 1;
+
+     if (no_monotonic)
+#endif
+          if(syscall(__NR_clock_gettime, CLOCK_REALTIME, &ts))
+               return gettimeofday( tv, NULL );
+
+     tv->tv_sec = ts.tv_sec;
+     tv->tv_usec = ts.tv_nsec / 1000;
+
+     return tv;
+}
+
 long long
 direct_clock_get_micros()
 {
@@ -46,11 +73,11 @@ direct_clock_get_micros()
      long long      ret;

      if (start_time.tv_sec == 0) {
-          gettimeofday( &start_time, NULL );
+          direct_monotonic_gettimeofday( &start_time );
           return 0;
      }

-     gettimeofday( &tv, NULL );
+     direct_monotonic_gettimeofday( &tv );

      ret = (long long)(tv.tv_sec - start_time.tv_sec) * 1000000LL +
            (long long)(tv.tv_usec - start_time.tv_usec);
@@ -90,7 +117,7 @@ direct_clock_get_abs_micros()
 {
      struct timeval tv;

-     gettimeofday( &tv, NULL );
+     direct_monotonic_gettimeofday( &tv );

      return (long long)(tv.tv_sec) * 1000000LL + (long long)(tv.tv_usec);
 }


_______________________________________________
directfb-dev mailing list
directfb-dev@directfb.org
http://mail.directfb.org/cgi-bin/mailman/listinfo/directfb-dev

Reply via email to