[PATCH 04/10] os: Add GetTimeInNanos

Keith Packard keithp at keithp.com
Thu Oct 31 23:43:36 CET 2013


64-bit higher resolution current time value.

Signed-off-by: Keith Packard <keithp at keithp.com>
---
 include/os.h |  1 +
 os/utils.c   | 27 +++++++++++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/include/os.h b/include/os.h
index c7108a5..3840ab9 100644
--- a/include/os.h
+++ b/include/os.h
@@ -165,6 +165,7 @@ extern void ListenOnOpenFD(int /* fd */ , int /* noxauth */ );
 #endif
 
 extern _X_EXPORT CARD32 GetTimeInMillis(void);
+extern _X_EXPORT CARD64 GetTimeInMicros(void);
 
 extern _X_EXPORT void AdjustWaitForDelay(pointer /*waitTime */ ,
                                          unsigned long /*newdelay */ );
diff --git a/os/utils.c b/os/utils.c
index 97c3125..995f62a 100644
--- a/os/utils.c
+++ b/os/utils.c
@@ -430,6 +430,11 @@ GetTimeInMillis(void)
 {
     return GetTickCount();
 }
+CARD64
+GetTimeInMicros(void)
+{
+    return (CARD64) GetTickCount() * 1000;
+}
 #else
 CARD32
 GetTimeInMillis(void)
@@ -460,6 +465,28 @@ GetTimeInMillis(void)
     X_GETTIMEOFDAY(&tv);
     return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
 }
+
+CARD64
+GetTimeInMicros(void)
+{
+    struct timeval tv;
+#ifdef MONOTONIC_CLOCK
+    struct timespec tp;
+    static clockid_t clockid;
+
+    if (!clockid) {
+        if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
+            clockid = CLOCK_MONOTONIC;
+        else
+            clockid = ~0L;
+    }
+    if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
+        return (CARD64) tp.tv_sec * (CARD64)1000000 + tp.tv_nsec / 1000;
+#endif
+
+    X_GETTIMEOFDAY(&tv);
+    return (CARD64) tv.tv_sec * (CARD64)1000000000 + (CARD64) tv.tv_usec * 1000;
+}
 #endif
 
 void
-- 
1.8.4.rc3



More information about the xorg-devel mailing list