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

Author: Yonggang Luo <[email protected]>
Date:   Mon Feb 13 11:07:59 2023 +0800

c11: Improve mtx_timedlock to use timespec_get instead of time(NULL)

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/c11/impl/threads_posix.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/src/c11/impl/threads_posix.c b/src/c11/impl/threads_posix.c
index f8ebbab5cec..614bc17cd21 100644
--- a/src/c11/impl/threads_posix.c
+++ b/src/c11/impl/threads_posix.c
@@ -202,6 +202,21 @@ mtx_lock(mtx_t *mtx)
     return (pthread_mutex_lock(mtx) == 0) ? thrd_success : thrd_error;
 }
 
+static int
+threads_timespec_compare(struct timespec *a, struct timespec *b)
+{
+    if (a->tv_sec < b->tv_sec) {
+        return -1;
+    } else if (a->tv_sec > b->tv_sec) {
+        return 1;
+    } else if (a->tv_nsec < b->tv_nsec) {
+        return -1;
+    } else if (a->tv_nsec > b->tv_nsec) {
+        return 1;
+    }
+    return 0;
+}
+
 // 7.25.4.4
 int
 mtx_timedlock(mtx_t *mtx, const struct timespec *ts)
@@ -217,11 +232,12 @@ mtx_timedlock(mtx_t *mtx, const struct timespec *ts)
         return thrd_success;
     return (rt == ETIMEDOUT) ? thrd_timedout : thrd_error;
 #else
-    time_t expire = time(NULL);
-    expire += ts->tv_sec;
     while (mtx_trylock(mtx) != thrd_success) {
-        time_t now = time(NULL);
-        if (expire < now)
+        struct timespec now;
+        if (timespec_get(&now, TIME_UTC) != TIME_UTC) {
+            return thrd_error;
+        }
+        if (threads_timespec_compare(ts, &now) < 0)
             return thrd_timedout;
         // busy loop!
         thrd_yield();

Reply via email to