[Mesa-dev] [PATCH 2/2] util: fix compatibility of timespec_get() across OSes

George Kyriazis george.kyriazis at intel.com
Wed Nov 15 23:24:57 UTC 2017


while timespec_get() is supposed to hide OS differences, compatibility doesn't
cover old OSes (like CentOS 6) where timespec_get() does not exist.

Fall back to using os_get_time_nano(), but separate out the functionality
that populates struct timespec, so it can also be called from
_util_queue_fence_wait_timeout(), where timespec_get() was initially called.
---
 src/util/os_time.c | 31 ++++++++++++++++++++-----------
 src/util/os_time.h |  6 ++++++
 src/util/u_queue.c |  5 ++++-
 3 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/src/util/os_time.c b/src/util/os_time.c
index 1c34bfd..a3cef81 100644
--- a/src/util/os_time.c
+++ b/src/util/os_time.c
@@ -48,37 +48,36 @@
 #  error Unsupported OS
 #endif
 
-
-int64_t
-os_time_get_nano(void)
+void
+os_time_get_sec_nano(int64_t *sec, int64_t *nsec)
 {
 #if defined(__linux__)
 
-   struct timespec tv;
-   clock_gettime(CLOCK_MONOTONIC, &tv);
-   return tv.tv_nsec + tv.tv_sec*INT64_C(1000000000);
+   struct timespec ts;
+   clock_gettime(CLOCK_MONOTONIC, &ts);
+   *sec = ts.tv_sec;
+   *nsec = ts.tv_nsec;
 
 #elif defined(__unix__)
 
    struct timeval tv;
    gettimeofday(&tv, NULL);
-   return tv.tv_usec*INT64_C(1000) + tv.tv_sec*INT64_C(1000000000);
+   *sec = tv.tv_sec;
+   *nsec = tv.tv_usec * INT64_T(1000);
 
 #elif defined(_MSC_VER)
 
    static LARGE_INTEGER frequency;
    LARGE_INTEGER counter;
-   int64_t secs, nanosecs;
    if(!frequency.QuadPart)
       QueryPerformanceFrequency(&frequency);
    QueryPerformanceCounter(&counter);
    /* Compute seconds and nanoseconds parts separately to
     * reduce severity of precision loss.
     */
-   secs = counter.QuadPart / frequency.QuadPart;
-   nanosecs = (counter.QuadPart % frequency.QuadPart) * INT64_C(1000000000)
+   *sec = counter.QuadPart / frequency.QuadPart;
+   *nsec = (counter.QuadPart % frequency.QuadPart) * INT64_C(1000000000)
       / frequency.QuadPart;
-   return secs*INT64_C(1000000000) + nanosecs;
 
 #else
 
@@ -87,6 +86,16 @@ os_time_get_nano(void)
 #endif
 }
 
+int64_t
+os_time_get_nano(void)
+{
+   int64_t secs, nanosecs;
+
+   os_time_get_sec_nano(&secs, &nanosecs);
+
+   return secs*INT64_C(1000000000) + nanosecs;
+}
+
 
 
 void
diff --git a/src/util/os_time.h b/src/util/os_time.h
index 049ab11..6169431 100644
--- a/src/util/os_time.h
+++ b/src/util/os_time.h
@@ -46,6 +46,12 @@ extern "C" {
 #define OS_TIMEOUT_INFINITE 0xffffffffffffffffull
 
 /*
+ * Get the current time in seconds and nanoseconds from an unknown base.
+ */
+void
+os_time_get_sec_nano(int64_t *sec, int64_t *nsec);
+
+/*
  * Get the current time in nanoseconds from an unknown base.
  */
 int64_t
diff --git a/src/util/u_queue.c b/src/util/u_queue.c
index 43c28ac..753d59e 100644
--- a/src/util/u_queue.c
+++ b/src/util/u_queue.c
@@ -168,9 +168,12 @@ _util_queue_fence_wait_timeout(struct util_queue_fence *fence,
    int64_t rel = abs_timeout - os_time_get_nano();
 
    if (rel > 0) {
+      int64_t sec, nsec;
       struct timespec ts;
 
-      timespec_get(&ts, TIME_UTC);
+      os_time_get_sec_nano(&sec, &nsec);
+      ts.tv_sec = sec;
+      ts.tv_nsec = nsec;
 
       ts.tv_sec += abs_timeout / (1000*1000*1000);
       ts.tv_nsec += abs_timeout % (1000*1000*1000);
-- 
2.7.4



More information about the mesa-dev mailing list