Module: Mesa
Branch: main
Commit: 8e452e385b5adb7fe49bd97da96393d6697989c5
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=8e452e385b5adb7fe49bd97da96393d6697989c5

Author: Yonggang Luo <[email protected]>
Date:   Tue Feb 14 02:49:56 2023 +0800

c11: Implement os_time_get_nano with timespec_get(&ts, TIME_MONOTONIC)

Signed-off-by: Yonggang Luo <[email protected]>
Reviewed-by: Jesse Natalie <[email protected]>
Acked-by: David Heidelberg <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/23733>

---

 src/util/os_time.c | 37 +++++--------------------------------
 1 file changed, 5 insertions(+), 32 deletions(-)

diff --git a/src/util/os_time.c b/src/util/os_time.c
index c207b8fdb77..7fb31342bb3 100644
--- a/src/util/os_time.c
+++ b/src/util/os_time.c
@@ -35,6 +35,8 @@
 #include "os_time.h"
 #include "detect_os.h"
 
+#include "c11/time.h"
+
 #include "util/u_atomic.h"
 
 #if DETECT_OS_UNIX
@@ -53,38 +55,9 @@
 int64_t
 os_time_get_nano(void)
 {
-#if DETECT_OS_LINUX || DETECT_OS_BSD
-
-   struct timespec tv;
-   clock_gettime(CLOCK_MONOTONIC, &tv);
-   return tv.tv_nsec + tv.tv_sec*INT64_C(1000000000);
-
-#elif DETECT_OS_UNIX
-
-   struct timeval tv;
-   gettimeofday(&tv, NULL);
-   return tv.tv_usec*INT64_C(1000) + tv.tv_sec*INT64_C(1000000000);
-
-#elif DETECT_OS_WINDOWS
-
-   LARGE_INTEGER frequency;
-   LARGE_INTEGER counter;
-   int64_t secs, nanosecs;
-   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)
-      / frequency.QuadPart;
-   return secs*INT64_C(1000000000) + nanosecs;
-
-#else
-
-#error Unsupported OS
-
-#endif
+   struct timespec ts;
+   timespec_get(&ts, TIME_MONOTONIC);
+   return ts.tv_nsec + ts.tv_sec*INT64_C(1000000000);
 }
 
 

Reply via email to